-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
123 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package calcium | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
|
||
enginetypes "github.com/projecteru2/core/engine/types" | ||
"github.com/projecteru2/core/log" | ||
"github.com/projecteru2/core/utils" | ||
) | ||
|
||
type remapEntry struct { | ||
ctx context.Context | ||
nodeName string | ||
logger log.Fields | ||
} | ||
|
||
// push node to remap queue | ||
func (c *Calcium) pushNodeToRemapQueue(ctx context.Context, logger log.Fields, nodeName string) { | ||
c.remapChan <- remapEntry{ctx: ctx, nodeName: nodeName, logger: logger} | ||
} | ||
|
||
// watch remap queue | ||
func (c *Calcium) watchRemapChan() { | ||
for { | ||
if entry, ok := <-c.remapChan; ok { | ||
c.doRemapResourceAndLog(entry.ctx, entry.logger, entry.nodeName) | ||
} else { | ||
return | ||
} | ||
} | ||
} | ||
|
||
// called on changes of resource binding, such as cpu binding | ||
func (c *Calcium) remapResource(ctx context.Context, nodeName string) (ch <-chan enginetypes.VirtualizationRemapMessage, err error) { | ||
node, err := c.store.GetNode(ctx, nodeName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
workloads, err := c.store.ListNodeWorkloads(ctx, nodeName, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
remapOpts := &enginetypes.VirtualizationRemapOptions{ | ||
CPUAvailable: node.CPU, | ||
CPUInit: node.InitCPU, | ||
CPUShareBase: int64(c.config.Scheduler.ShareBase), | ||
WorkloadResources: make(map[string]enginetypes.VirtualizationResource), | ||
} | ||
for _, workload := range workloads { | ||
remapOpts.WorkloadResources[workload.ID] = enginetypes.VirtualizationResource{ | ||
CPU: workload.CPU, | ||
Quota: workload.CPUQuotaLimit, | ||
NUMANode: workload.NUMANode, | ||
} | ||
} | ||
ch, err = node.Engine.VirtualizationResourceRemap(ctx, remapOpts) | ||
return ch, errors.WithStack(err) | ||
} | ||
|
||
func (c *Calcium) doRemapResourceAndLog(ctx context.Context, logger log.Fields, nodeName string) { | ||
log.Debugf(ctx, "[doRemapResourceAndLog] remap node %s", nodeName) | ||
ctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.TODO()), c.config.GlobalTimeout) | ||
defer cancel() | ||
logger = logger.WithField("Calcium", "doRemapResourceAndLog").WithField("nodename", nodeName) | ||
if ch, err := c.remapResource(ctx, nodeName); logger.Err(ctx, err) == nil { | ||
for msg := range ch { | ||
log.Debugf(ctx, "[doRemapResourceAndLog] remap node %s, msg: %+v", nodeName, msg) | ||
logger.WithField("id", msg.ID).Err(ctx, msg.Error) // nolint:errcheck | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package calcium | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
enginemocks "github.com/projecteru2/core/engine/mocks" | ||
enginetypes "github.com/projecteru2/core/engine/types" | ||
"github.com/projecteru2/core/log" | ||
storemocks "github.com/projecteru2/core/store/mocks" | ||
"github.com/projecteru2/core/types" | ||
) | ||
|
||
func TestRemapResource(t *testing.T) { | ||
c := NewTestCluster() | ||
store := &storemocks.Store{} | ||
c.store = store | ||
engine := &enginemocks.API{} | ||
node := &types.Node{Engine: engine} | ||
node.Name = "node1" | ||
|
||
workload := &types.Workload{ | ||
ResourceMeta: types.ResourceMeta{ | ||
CPUQuotaLimit: 1, | ||
}, | ||
} | ||
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil) | ||
store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return([]*types.Workload{workload}, nil) | ||
ch := make(chan enginetypes.VirtualizationRemapMessage, 1) | ||
ch <- enginetypes.VirtualizationRemapMessage{} | ||
close(ch) | ||
engine.On("VirtualizationResourceRemap", mock.Anything, mock.Anything).Return((<-chan enginetypes.VirtualizationRemapMessage)(ch), nil) | ||
_, err := c.remapResource(context.Background(), node.Name) | ||
assert.Nil(t, err) | ||
|
||
c.doRemapResourceAndLog(context.TODO(), log.WithField("test", "zc"), node.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters