github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/resource/cobalt/realloc.go (about)

     1  package cobalt
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/projecteru2/core/log"
     7  	"github.com/projecteru2/core/resource/plugins"
     8  	plugintypes "github.com/projecteru2/core/resource/plugins/types"
     9  	resourcetypes "github.com/projecteru2/core/resource/types"
    10  	"github.com/projecteru2/core/utils"
    11  )
    12  
    13  // Realloc .
    14  func (m Manager) Realloc(ctx context.Context, nodename string, nodeResource resourcetypes.Resources, opts resourcetypes.Resources) (resourcetypes.Resources, resourcetypes.Resources, resourcetypes.Resources, error) {
    15  	logger := log.WithFunc("resource.cobalt.Realloc").WithField("node", nodename)
    16  	engineParams := resourcetypes.Resources{}
    17  	deltaResources := resourcetypes.Resources{}
    18  	workloadResource := resourcetypes.Resources{}
    19  
    20  	return engineParams, deltaResources, workloadResource, utils.PCR(ctx,
    21  		// prepare: calculate engine args, delta node resource args and final workload resource args
    22  		func(ctx context.Context) error {
    23  			resps, err := call(ctx, m.plugins, func(plugin plugins.Plugin) (*plugintypes.CalculateReallocResponse, error) {
    24  				resp, err := plugin.CalculateRealloc(ctx, nodename, nodeResource[plugin.Name()], opts[plugin.Name()])
    25  				if err != nil {
    26  					logger.Errorf(ctx, err, "plugin %+v failed to calculate realloc args", plugin.Name())
    27  				}
    28  				return resp, err
    29  			})
    30  			if err != nil {
    31  				logger.Errorf(ctx, err, "realloc failed, origin: %+v, opts: %+v", nodeResource, opts)
    32  				return err
    33  			}
    34  
    35  			for plugin, resp := range resps {
    36  				v, err := m.mergeEngineParams(ctx, engineParams[plugin.Name()], resp.EngineParams)
    37  				if err != nil {
    38  					logger.Error(ctx, err, "invalid engine args")
    39  					return err
    40  				}
    41  				engineParams[plugin.Name()] = v
    42  				deltaResources[plugin.Name()] = resp.DeltaResource
    43  				workloadResource[plugin.Name()] = resp.WorkloadResource
    44  			}
    45  			return nil
    46  		},
    47  		// commit: update node resource
    48  		func(ctx context.Context) error {
    49  			// TODO 存在问题??3个参数是完整的变化,差值变化,按照 workloads 的变化
    50  			if _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, []resourcetypes.Resources{deltaResources}, true, plugins.Incr); err != nil {
    51  				logger.Error(ctx, err, "failed to update nodename resource")
    52  				return err
    53  			}
    54  			return nil
    55  		},
    56  		// rollback: do nothing
    57  		func(_ context.Context) error {
    58  			return nil
    59  		},
    60  		m.config.GlobalTimeout,
    61  	)
    62  }
    63  
    64  // RollbackRealloc .
    65  func (m Manager) RollbackRealloc(ctx context.Context, nodename string, workloadParams resourcetypes.Resources) error {
    66  	_, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, []resourcetypes.Resources{workloadParams}, true, plugins.Decr)
    67  	return err
    68  }