better input args type in command pool

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>
This commit is contained in:
andrewmatilde 2023-02-08 14:05:42 +08:00
parent 9c9ace0070
commit 0ab6238923
3 changed files with 24 additions and 21 deletions

View File

@ -51,14 +51,15 @@ func (disk diskServerAttack) Attack(options core.AttackConfig, env Environment)
if attackConf.Action == core.DiskFillAction {
cmdPool := pkgUtils.NewCommandPools(context.Background(), nil, poolSize)
env.Chaos.CmdPools[env.AttackUid] = cmdPool
name, args := core.FAllocateCommand.GetCmdArgs(*attackConf.FAllocateOption)
if attackConf.FAllocateOption != nil {
cmdPool.Start(core.FAllocateCommand, *attackConf.FAllocateOption, handleFillingOutput)
cmdPool.Start(name, args, handleFillingOutput)
return nil
}
for _, DdOption := range *attackConf.DdOptions {
cmdPool.Start(core.DdCommand, DdOption, handleFillingOutput)
name, args := core.DdCommand.GetCmdArgs(DdOption)
cmdPool.Start(name, args, handleFillingOutput)
}
return nil
}
@ -78,11 +79,12 @@ func (disk diskServerAttack) Attack(options core.AttackConfig, env Environment)
}
rest := (*attackConf.DdOptions)[len(*attackConf.DdOptions)-1]
*attackConf.DdOptions = (*attackConf.DdOptions)[:len(*attackConf.DdOptions)-1]
cmdPool.Start(core.DdCommand, rest, handleFillingOutput)
name, args := core.DdCommand.GetCmdArgs(rest)
cmdPool.Start(name, args, handleFillingOutput)
for _, ddOpt := range *attackConf.DdOptions {
cmdPool.Start(core.DdCommand, ddOpt, handleFillingOutput)
name, args := core.DdCommand.GetCmdArgs(ddOpt)
cmdPool.Start(name, args, handleFillingOutput)
}
}
return nil

View File

@ -25,16 +25,16 @@ type Command struct {
}
func (c Command) Unmarshal(val interface{}) *exec.Cmd {
options := c.getOption(val)
return exec.Command(c.Name, options...)
name, args := c.GetCmdArgs(val)
return exec.Command(name, args...)
}
func (c Command) UnmarshalWithCtx(ctx context.Context, val interface{}) *exec.Cmd {
options := c.getOption(val)
return exec.CommandContext(ctx, c.Name, options...)
name, args := c.GetCmdArgs(val)
return exec.CommandContext(ctx, name, args...)
}
func (c Command) getOption(val interface{}) []string {
func (c Command) GetCmdArgs(val interface{}) (string, []string) {
v := reflect.ValueOf(val)
var options []string
@ -50,5 +50,5 @@ func (c Command) getOption(val interface{}) []string {
options = append(options, fmt.Sprintf("%s=%v", tag, v.Field(i).String()))
}
}
return options
return c.Name, options
}

View File

@ -16,6 +16,7 @@ package utils
import (
"context"
"fmt"
"os/exec"
"sync"
"time"
@ -42,12 +43,12 @@ func NewCommandPools(ctx context.Context, deadline *time.Time, size int) *Comman
return &CommandPools{
cancel: cancel,
pools: tunny.NewFunc(size, func(payload interface{}) interface{} {
cmdPayload, ok := payload.(lo.Tuple2[Command, interface{}])
cmdPayload, ok := payload.(lo.Tuple2[string, []string])
if !ok {
return mo.Err[[]byte](fmt.Errorf("payload is not CommandPayload"))
}
rawCmd, val := cmdPayload.Unpack()
cmd := rawCmd.UnmarshalWithCtx(ctx2, val)
name, args := cmdPayload.Unpack()
cmd := exec.CommandContext(ctx2, name, args...)
output, err := cmd.CombinedOutput()
if err != nil {
return mo.Err[[]byte](fmt.Errorf("%s: %s", err, string(output)))
@ -57,10 +58,10 @@ func NewCommandPools(ctx context.Context, deadline *time.Time, size int) *Comman
}
}
func (p *CommandPools) Process(cmd Command, val interface{}) ([]byte, error) {
result, ok := p.pools.Process(lo.Tuple2[Command, interface{}]{
A: cmd,
B: val,
func (p *CommandPools) Process(name string, args []string) ([]byte, error) {
result, ok := p.pools.Process(lo.Tuple2[string, []string]{
A: name,
B: args,
}).(mo.Result[[]byte])
if !ok {
return nil, fmt.Errorf("payload is not Result[[]byte]")
@ -68,10 +69,10 @@ func (p *CommandPools) Process(cmd Command, val interface{}) ([]byte, error) {
return result.Get()
}
func (p *CommandPools) Start(cmd Command, val interface{}, outputHandler func([]byte, error)) {
func (p *CommandPools) Start(name string, args []string, outputHandler func([]byte, error)) {
p.wg.Add(1)
go func() {
output, err := p.Process(cmd, val)
output, err := p.Process(name, args)
outputHandler(output, err)
p.wg.Done()
}()