Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node resource can check volume diff and fix #427

Merged
merged 1 commit into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ func (c *Calcium) doDeployOneWorkload(
if err := c.store.AddWorkload(ctx, workload); err != nil {
return errors.WithStack(err)
}
log.Infof(ctx, "[doDeployOneWorkload] workload created and saved: %s", workload.ID)
msg.WorkloadID = workload.ID
msg.WorkloadName = workload.Name
msg.Podname = workload.Podname
Expand Down
24 changes: 22 additions & 2 deletions cluster/calcium/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@ func (c *Calcium) doGetNodeResource(ctx context.Context, nodename string, fix bo
memory := int64(0)
storage := int64(0)
cpumap := types.CPUMap{}
volumes := int64(0)
volumeMap := types.VolumeMap{}
for _, workload := range workloads {
cpus = utils.Round(cpus + workload.CPUQuotaRequest)
memory += workload.MemoryRequest
storage += workload.StorageRequest
cpumap.Add(workload.CPU)
for _, vmap := range workload.VolumePlanRequest {
volumes += vmap.Total()
volumeMap.Add(vmap)
}
}
nr.CPUPercent = cpus / float64(len(node.InitCPU))
nr.MemoryPercent = float64(memory) / float64(node.InitMemCap)
Expand Down Expand Up @@ -108,12 +114,22 @@ func (c *Calcium) doGetNodeResource(ctx context.Context, nodename string, fix bo
}
}

if node.VolumeUsed != volumes {
nr.Diffs = append(nr.Diffs, fmt.Sprintf("volumes used: %d diff: %d", node.VolumeUsed, volumes))
}
node.Volume.Add(volumeMap)
for vol, cap := range node.Volume {
if node.InitVolume[vol] != cap {
nr.Diffs = append(nr.Diffs, fmt.Sprintf("volume %s diff %d", vol, node.InitVolume[vol]-cap))
}
}

if err := node.Engine.ResourceValidate(ctx, cpus, cpumap, memory, storage); err != nil {
nr.Diffs = append(nr.Diffs, err.Error())
}

if fix {
if err := c.doFixDiffResource(ctx, node, cpus, memory, storage); err != nil {
if err := c.doFixDiffResource(ctx, node, cpus, memory, storage, volumes); err != nil {
log.Warnf(ctx, "[doGetNodeResource] fix node resource failed %v", err)
}
}
Expand All @@ -122,7 +138,7 @@ func (c *Calcium) doGetNodeResource(ctx context.Context, nodename string, fix bo
})
}

func (c *Calcium) doFixDiffResource(ctx context.Context, node *types.Node, cpus float64, memory, storage int64) error {
func (c *Calcium) doFixDiffResource(ctx context.Context, node *types.Node, cpus float64, memory, storage, volumes int64) error {
var n *types.Node
var err error
return utils.Txn(ctx,
Expand All @@ -136,6 +152,10 @@ func (c *Calcium) doFixDiffResource(ctx context.Context, node *types.Node, cpus
}
n.MemCap += node.InitMemCap - (memory + node.MemCap)
n.StorageCap += node.InitStorageCap - (storage + node.StorageCap)
n.VolumeUsed = volumes
for vol, cap := range node.Volume {
n.Volume[vol] += node.InitVolume[vol] - cap
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注意这里可能减成负数, 但是这也是合理的, 这样当容器都删除之后, 资源还回去后才能填平

}
return nil
},
func(ctx context.Context) error {
Expand Down