Skip to content

Commit

Permalink
keep --delta for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
tonicmuroq committed May 10, 2021
1 parent 63c812c commit ec85a2c
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 16 deletions.
39 changes: 27 additions & 12 deletions cmd/node/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,47 @@ func Command() *cli.Command {
Usage: "mark workloads down",
},
&cli.StringFlag{
Name: "memory",
Usage: "memory value like -1M or 1G, support K, M, G, T",
Name: "memory",
Usage: `memory, unit can be K/M/G/T,
when using --delta flag, this can be a negtive number indicating how much to add to the current value,
e.g. --memory -10G --delta, means memory will be the current value - 10`,
},
&cli.StringFlag{
Name: "storage",
Usage: "storage value like -1M or 1G, support K, M, G, T",
Name: "storage",
Usage: `storage, unit can be K/M/G/T,
when using --delta flag, this can be a negtive number indicating how much to add to the current value,
e.g. --storage -10G --delta, means storage will be the current value - 10`,
},
&cli.StringFlag{
Name: "cpu",
Usage: "cpu value in string, like 0:100,1:200,3:50",
Usage: "cpu value in string, e.g. 0:100,1:200,3:50",
},
&cli.StringSliceFlag{
Name: "numa-memory",
Usage: "numa memory values, can set multiple times, like -1M or 1G, support K, M, G, T",
Name: "numa-memory",
Usage: `numa memory values, unit can be K/M/G/T,
when using --delta flag, this can be a negtive number indicating how much to add to the current value,
e.g. --numa-memory -10G --delta, means the value will be current value - 10
this value can be set multiple times, the index will be the numa node ID,
e.g. --numa-memory 10G --numa-memory 15G, means node ID 0 will be 10GB, node ID 1 will be 15GB`,
},
&cli.StringFlag{
Name: "volume",
Usage: `volume value in string, like "/data0:-1G,/data1:1G"`,
Name: "volume",
Usage: `volume value in string, like "/data0:10G,/data1:10G"
when using --delta flag, this can be a negtive number indicating how much to add to the current value,
e.g. --volume /data0:-10G,/data1:20G, means /data0 will be subtract 10G and /data1 will be added 20G`,
},
&cli.StringSliceFlag{
Name: "numa-cpu",
Usage: "numa cpu list, can set multiple times, use comma separated",
Name: "numa-cpu",
Usage: `numa cpu list, can be set multiple times, the index will be the numa node ID.
e.g. --numa-cpu 0,1,2,3 --numa-cpu 4,5,6,7 means cpu 0,1,2,3 are bound to node ID 0, cpu 4,5,6,7 are bound to node ID 1`,
},
&cli.StringSliceFlag{
Name: "label",
Usage: "add label for node, like a=1 b=2, can set multiple times",
Usage: "label for the node, can set multiple times, e.g. --label a=1 --label b=2",
},
&cli.BoolFlag{
Name: "delta",
Usage: "delta flag for settings, when set, all values will be relative to the current values, refer to each option for details",
},
},
},
Expand Down
101 changes: 97 additions & 4 deletions cmd/node/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ func cmdNodeSet(c *cli.Context) error {
return err
}

opts, err := generateSetNodeOptions(c, client)
var f func(*cli.Context, corepb.CoreRPCClient) (*corepb.SetNodeOptions, error)
if c.Bool("delta") {
f = generateSetNodeOptionsDelta
} else {
f = generateSetNodeOptionsAbsolute
}

opts, err := f(c, client)
if err != nil {
return err
}
Expand All @@ -45,7 +52,7 @@ func cmdNodeSet(c *cli.Context) error {
return o.run(c.Context)
}

func generateSetNodeOptions(c *cli.Context, client corepb.CoreRPCClient) (*corepb.SetNodeOptions, error) {
func generateSetNodeOptionsAbsolute(c *cli.Context, client corepb.CoreRPCClient) (*corepb.SetNodeOptions, error) {
name := c.Args().First()
if name == "" {
return nil, errors.New("Node name must be given")
Expand Down Expand Up @@ -117,16 +124,20 @@ func generateSetNodeOptions(c *cli.Context, client corepb.CoreRPCClient) (*corep
if err != nil {
return nil, err
}
if memory > 0 {
if memory >= 0 {
memory -= node.InitMemory
} else {
return nil, fmt.Errorf("you can't set memory to a negative number when using absolute value")
}

storage, err := utils.ParseRAMInHuman(c.String("storage"))
if err != nil {
return nil, err
}
if storage > 0 {
if storage >= 0 {
storage -= node.InitStorage
} else {
return nil, fmt.Errorf("you can't set storage to a negative number when using absolute value")
}

return &corepb.SetNodeOptions{
Expand All @@ -142,3 +153,85 @@ func generateSetNodeOptions(c *cli.Context, client corepb.CoreRPCClient) (*corep
WorkloadsDown: c.Bool("mark-workloads-down"),
}, nil
}

func generateSetNodeOptionsDelta(c *cli.Context, _ corepb.CoreRPCClient) (*corepb.SetNodeOptions, error) {
name := c.Args().First()
if name == "" {
return nil, errors.New("Node name must be given")
}

numaMemoryList := c.StringSlice("delta-numa-memory")
numaMemory := map[string]int64{}
for nodeID, memoryStr := range numaMemoryList {
memory, err := utils.ParseRAMInHuman(memoryStr)
if err != nil {
return nil, err
}
numaMemory[strconv.Itoa(nodeID)] = memory
}

numaList := c.StringSlice("numa-cpu")
numa := map[string]string{}
for nodeID, cpuList := range numaList {
for _, cpuID := range strings.Split(cpuList, ",") {
numa[cpuID] = strconv.Itoa(nodeID)
}
}

cpuList := c.String("delta-cpu")
cpuMap := map[string]int32{}
if cpuList != "" {
cpuMapList := strings.Split(cpuList, ",")
for _, cpus := range cpuMapList {
cpuConfigs := strings.Split(cpus, ":")
// G109: Potential Integer overflow made by strconv.Atoi result conversion to int16/32
share, err := strconv.Atoi(cpuConfigs[1]) // nolint
if err != nil {
return nil, err
}
cpuID := cpuConfigs[0]
cpuMap[cpuID] = int32(share)
}
}

volumeMap := map[string]int64{}
deltaVolume := c.String("delta-volume")
if deltaVolume != "" {
for _, volume := range strings.Split(deltaVolume, ",") {
parts := strings.Split(volume, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid volume")
}
delta, err := utils.ParseRAMInHuman(parts[1])
if err != nil {
return nil, err
}
volumeMap[parts[0]] = delta
}
}

var (
deltaMemory int64
deltaStorage int64
err error
)
if deltaMemory, err = utils.ParseRAMInHuman(c.String("delta-memory")); err != nil {
return nil, err
}
if deltaStorage, err = utils.ParseRAMInHuman(c.String("delta-storage")); err != nil {
return nil, err
}

return &corepb.SetNodeOptions{
Nodename: name,
StatusOpt: corepb.TriOpt_KEEP,
DeltaCpu: cpuMap,
DeltaMemory: deltaMemory,
DeltaStorage: deltaStorage,
DeltaNumaMemory: numaMemory,
DeltaVolume: volumeMap,
Numa: numa,
Labels: utils.SplitEquality(c.StringSlice("label")),
WorkloadsDown: c.Bool("mark-workloads-down"),
}, nil
}

0 comments on commit ec85a2c

Please sign in to comment.