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

     1  package strategy
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/projecteru2/core/log"
     7  	"github.com/projecteru2/core/types"
     8  )
     9  
    10  const (
    11  	// Auto .
    12  	Auto = "AUTO"
    13  	// Fill .
    14  	Fill = "FILL"
    15  	// Each .
    16  	Each = "EACH"
    17  	// Global .
    18  	Global = "GLOBAL"
    19  	// Drained .
    20  	Drained = "DRAINED"
    21  	// Dummy for calculate capacity
    22  	Dummy = "DUMMY"
    23  )
    24  
    25  // Plans .
    26  var Plans = map[string]strategyFunc{
    27  	Auto:    CommunismPlan,
    28  	Fill:    FillPlan,
    29  	Each:    AveragePlan,
    30  	Global:  GlobalPlan,
    31  	Drained: DrainedPlan,
    32  }
    33  
    34  type strategyFunc = func(_ context.Context, _ []Info, need, total, limit int) (map[string]int, error)
    35  
    36  // Deploy .
    37  func Deploy(ctx context.Context, strategy string, count, nodesLimit int, strategyInfos []Info, total int) (map[string]int, error) {
    38  	deployMethod, ok := Plans[strategy]
    39  	if !ok {
    40  		return nil, types.ErrInvaildDeployStrategy
    41  	}
    42  	if count <= 0 {
    43  		return nil, types.ErrInvaildDeployCount
    44  	}
    45  
    46  	log.WithFunc("strategy.Deploy").Debugf(ctx, "strategy %s, infos %+v, need %d, total %d, limit %d", strategy, strategyInfos, count, total, nodesLimit)
    47  	return deployMethod(ctx, strategyInfos, count, total, nodesLimit)
    48  }
    49  
    50  // Info .
    51  type Info struct {
    52  	Nodename string
    53  
    54  	Usage float64
    55  	Rate  float64
    56  
    57  	Capacity int
    58  	Count    int
    59  }