github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/resource/cobalt/alloc.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  /*
    14  Alloc alloc resource
    15  opts struct
    16  
    17  	{
    18  		"plugin1":{
    19  			"cpu-request": 1.2,
    20  			"cpu-limit": 2.0,
    21  		},
    22  		"plugin2":{
    23  		},
    24  	}
    25  */
    26  func (m Manager) Alloc(ctx context.Context, nodename string, deployCount int, opts resourcetypes.Resources) ([]resourcetypes.Resources, []resourcetypes.Resources, error) {
    27  	logger := log.WithFunc("resource.coblat.Alloc")
    28  
    29  	// index -> no, map by plugin name
    30  	workloadsParams := make([]resourcetypes.Resources, deployCount)
    31  	engineParams := make([]resourcetypes.Resources, deployCount)
    32  
    33  	// init engine args
    34  	for i := 0; i < deployCount; i++ {
    35  		workloadsParams[i] = resourcetypes.Resources{}
    36  		engineParams[i] = resourcetypes.Resources{}
    37  	}
    38  
    39  	return workloadsParams, engineParams, utils.PCR(ctx,
    40  		// prepare: calculate engine args and resource args
    41  		func(ctx context.Context) error {
    42  			resps, err := call(ctx, m.plugins, func(plugin plugins.Plugin) (*plugintypes.CalculateDeployResponse, error) {
    43  				resp, err := plugin.CalculateDeploy(ctx, nodename, deployCount, opts[plugin.Name()])
    44  				if err != nil {
    45  					logger.Errorf(ctx, err, "plugin %+v failed to compute alloc args, request %+v, node %+v, deploy count %+v", plugin.Name(), opts, nodename, deployCount)
    46  				}
    47  				return resp, err
    48  			})
    49  			if err != nil {
    50  				return err
    51  			}
    52  
    53  			// calculate engine args
    54  			for plugin, resp := range resps {
    55  				logger.Debug(ctx, plugin.Name())
    56  				for index, params := range resp.WorkloadsResource {
    57  					workloadsParams[index][plugin.Name()] = params
    58  				}
    59  				for index, params := range resp.EnginesParams {
    60  					v, err := m.mergeEngineParams(ctx, engineParams[index][plugin.Name()], params)
    61  					if err != nil {
    62  						logger.Error(ctx, err, "invalid engine args")
    63  						return err
    64  					}
    65  					engineParams[index][plugin.Name()] = v
    66  				}
    67  			}
    68  			return nil
    69  		},
    70  		// commit: update node resources
    71  		func(ctx context.Context) error {
    72  			// 因为用了资源所以 usage 增加
    73  			if _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, workloadsParams, true, plugins.Incr); err != nil {
    74  				logger.Error(ctx, err, "failed to update node resource")
    75  				return err
    76  			}
    77  			return nil
    78  		},
    79  		// rollback: do nothing
    80  		func(_ context.Context) error {
    81  			return nil
    82  		},
    83  		m.config.GlobalTimeout,
    84  	)
    85  }
    86  
    87  // RollbackAlloc rollbacks the allocated resource
    88  func (m Manager) RollbackAlloc(ctx context.Context, nodename string, workloadsParams []resourcetypes.Resources) error {
    89  	_, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, workloadsParams, true, plugins.Decr)
    90  	return err
    91  }