-
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
6 changed files
with
116 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package calcium | ||
|
||
import ( | ||
"context" | ||
|
||
enginetypes "github.com/projecteru2/core/engine/types" | ||
"github.com/projecteru2/core/log" | ||
"github.com/projecteru2/core/types" | ||
"github.com/projecteru2/core/utils" | ||
) | ||
|
||
type remapMsg struct { | ||
id string | ||
err error | ||
} | ||
|
||
func (c *Calcium) doRemapResourceAndLog(ctx context.Context, logger log.Fields, node *types.Node) { | ||
log.Debugf(ctx, "[doRemapResourceAndLog] remap node %s", node.Name) | ||
ctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.TODO()), c.config.GlobalTimeout) | ||
defer cancel() | ||
|
||
err := c.withNodeOperationLocked(ctx, node.Name, func(ctx context.Context, node *types.Node) error { | ||
logger = logger.WithField("Calcium", "doRemapResourceAndLog").WithField("nodename", node.Name) | ||
if ch, err := c.remapResource(ctx, node); logger.ErrWithTracing(ctx, err) == nil { | ||
for msg := range ch { | ||
log.Infof(ctx, "[doRemapResourceAndLog] id %v", msg.id) | ||
logger.WithField("id", msg.id).ErrWithTracing(ctx, msg.err) // nolint:errcheck | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
log.Errorf(ctx, "[doRemapResourceAndLog] remap node %s failed, err: %v", node.Name, err) | ||
} | ||
} | ||
|
||
// called on changes of resource binding, such as cpu binding | ||
// as an internal api, remap doesn't lock node, the responsibility of that should be taken on by caller | ||
func (c *Calcium) remapResource(ctx context.Context, node *types.Node) (ch chan *remapMsg, err error) { | ||
workloads, err := c.store.ListNodeWorkloads(ctx, node.Name, nil) | ||
if err != nil { | ||
return | ||
} | ||
|
||
workloadMap := map[string]*types.Workload{} | ||
for _, workload := range workloads { | ||
workloadMap[workload.ID] = workload | ||
} | ||
|
||
engineArgsMap, err := c.rmgr.GetRemapArgs(ctx, node.Name, workloadMap) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ch = make(chan *remapMsg, len(engineArgsMap)) | ||
go func() { | ||
defer close(ch) | ||
for workloadID, engineArgs := range engineArgsMap { | ||
ch <- &remapMsg{ | ||
id: workloadID, | ||
err: node.Engine.VirtualizationUpdateResource(ctx, workloadID, &enginetypes.VirtualizationResource{EngineArgs: engineArgs}), | ||
} | ||
} | ||
}() | ||
|
||
return ch, nil | ||
} |
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,45 @@ | ||
package calcium | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
enginemocks "github.com/projecteru2/core/engine/mocks" | ||
enginetypes "github.com/projecteru2/core/engine/types" | ||
"github.com/projecteru2/core/log" | ||
resourcemocks "github.com/projecteru2/core/resources/mocks" | ||
storemocks "github.com/projecteru2/core/store/mocks" | ||
"github.com/projecteru2/core/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestRemapResource(t *testing.T) { | ||
c := NewTestCluster() | ||
store := c.store.(*storemocks.Store) | ||
rmgr := c.rmgr.(*resourcemocks.Manager) | ||
rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( | ||
map[string]types.NodeResourceArgs{"test": map[string]interface{}{"abc": 123}}, | ||
map[string]types.NodeResourceArgs{"test": map[string]interface{}{"abc": 123}}, | ||
[]string{types.ErrNoETCD.Error()}, | ||
nil) | ||
rmgr.On("GetRemapArgs", mock.Anything, mock.Anything, mock.Anything).Return( | ||
map[string]types.EngineArgs{}, | ||
nil, | ||
) | ||
engine := &enginemocks.API{} | ||
node := &types.Node{Engine: engine} | ||
|
||
workload := &types.Workload{ | ||
ResourceArgs: map[string]types.WorkloadResourceArgs{}, | ||
} | ||
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) | ||
assert.Nil(t, err) | ||
|
||
c.doRemapResourceAndLog(context.TODO(), log.WithField("test", "zc"), node) | ||
} |
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