From 92738db86affd413446e87e886aba51a0e0b2179 Mon Sep 17 00:00:00 2001 From: hanxinke Date: Wed, 8 Jan 2020 16:07:16 +0800 Subject: [PATCH] atune:add the ability to roll back optimized configuration parameters in the tuning command --- common/config/config.go | 5 ++- common/tuning/optimizer.go | 54 +++++++++++++++++++++++- modules/client/profile/profile_tuning.go | 41 ++++++++++++++++++ modules/server/profile/profile.go | 10 +++++ 4 files changed, 107 insertions(+), 3 deletions(-) diff --git a/common/config/config.go b/common/config/config.go index 0fe77f4..1587268 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -88,8 +88,9 @@ const ( //tuning config const ( - TuningFile string = DefaultTempPath + "/tuning.log" - FilePerm os.FileMode = 0600 + TuningFile string = DefaultTempPath + "/tuning.log" + TuningRestoreConfig string = "-tuning-restore.conf" + FilePerm os.FileMode = 0600 ) // the grpc server config diff --git a/common/tuning/optimizer.go b/common/tuning/optimizer.go index 097ebd4..4cefe86 100644 --- a/common/tuning/optimizer.go +++ b/common/tuning/optimizer.go @@ -22,8 +22,11 @@ import ( "atune/common/project" "atune/common/utils" "fmt" + "io/ioutil" "os" + "path" "strconv" + "strings" ) // Optimizer : the type implement the bayes serch service @@ -77,6 +80,7 @@ func (o *Optimizer) InitTuned(ch chan *PB.AckCheck, askIter int) error { return err } + initConfigure := "" optimizerBody := new(models.OptimizerPostBody) optimizerBody.MaxEval = maxIter @@ -93,6 +97,20 @@ func (o *Optimizer) InitTuned(ch chan *PB.AckCheck, askIter int) error { knob.Step = item.Info.Step knob.Options = item.Info.Options optimizerBody.Knobs = append(optimizerBody.Knobs, *knob) + + out, err := project.ExecCommand(item.Info.GetScript) + if err != nil { + return err + } + initConfigure += strings.TrimSpace(knob.Name+"="+string(out)) + "," + } + + err = utils.WriteFile(path.Join(config.DefaultTempPath, + o.Prj.Project+config.TuningRestoreConfig), initConfigure, + config.FilePerm, os.O_WRONLY|os.O_CREATE|os.O_TRUNC) + if err != nil { + log.Error(err) + return err } respPostIns, err := optimizerBody.Post() @@ -210,10 +228,44 @@ func (bench *BenchMark) DynamicTuned(ch chan *PB.AckCheck) error { return nil } +//restore tuning config +func (o *Optimizer) RestoreConfigTuned() error { + if !optimizer.TryLock() { + return fmt.Errorf("dynamic optimizer search has been in running") + } + defer optimizer.Unlock() + + tuningRestoreConf := path.Join(config.DefaultTempPath, o.Prj.Project+config.TuningRestoreConfig) + exist, err := utils.PathExist(tuningRestoreConf) + if err != nil { + return err + } + if !exist { + log.Errorf("%s project has not been executed the dynamic optimizer search", o.Prj.Project) + return fmt.Errorf("%s project has not been executed the dynamic optimizer search", + o.Prj.Project) + } + + content, err := ioutil.ReadFile(tuningRestoreConf) + if err != nil { + log.Error(err) + return err + } + + log.Infof("restoring params is: %s", string(content)) + if err := o.Prj.RunSet(string(content)); err != nil { + log.Error(err) + return err + } + + log.Infof("restore %s project params success", o.Prj.Project) + return nil +} + func deleteTask(url string) error { resp, err := http.Delete(url) if err != nil { - log.Info("delete task faild:", err) + log.Error("delete task faild:", err) return err } defer resp.Body.Close() diff --git a/modules/client/profile/profile_tuning.go b/modules/client/profile/profile_tuning.go index 4de76d3..97edafb 100644 --- a/modules/client/profile/profile_tuning.go +++ b/modules/client/profile/profile_tuning.go @@ -34,6 +34,17 @@ var profileTunningCommand = cli.Command{ Name: "tuning", Usage: "dynamic bayes search optimal parameter sets", ArgsUsage: "PROJECT_YAML", + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "restore,r", + Usage: "restore pre-optimized initial configuration", + }, + cli.StringFlag{ + Name: "project,p", + Usage: "the project name of the yaml file", + Value: "", + }, + }, Description: func() string { desc := ` tuning command usning bayes method dynamic search optimal parameter sets, @@ -62,6 +73,10 @@ func newProfileTuningCmd(ctx *cli.Context, opts ...interface{}) (interface{}, er } func profileTunning(ctx *cli.Context) error { + if ctx.Bool("restore") { + return checkRestoreConfig(ctx) + } + if err := utils.CheckArgs(ctx, 1, utils.ConstExactArgs); err != nil { return err } @@ -149,3 +164,29 @@ func runTuningRPC(ctx *cli.Context, info *PB.ProfileInfo) (string, error) { return "", nil } + +func checkTuningCtx(ctx *cli.Context) error { + if ctx.String("project") == "" { + _ = cli.ShowCommandHelp(ctx, "tuning") + return fmt.Errorf("error: project name must be specified") + } + + if len(ctx.String("project")) > 255 { + return fmt.Errorf("error: project name length is longer than 255 charaters") + } + + return nil +} + +func checkRestoreConfig(ctx *cli.Context) error { + err := checkTuningCtx(ctx) + if err != nil { + return err + } + _, err = runTuningRPC(ctx, &PB.ProfileInfo{Name: ctx.String("project")}) + if err != nil { + return err + } + return nil +} + diff --git a/modules/server/profile/profile.go b/modules/server/profile/profile.go index 58f38ef..e88150c 100644 --- a/modules/server/profile/profile.go +++ b/modules/server/profile/profile.go @@ -515,6 +515,16 @@ func (s *ProfileServer) Tuning(profileInfo *PB.ProfileInfo, stream PB.ProfileMgr return fmt.Errorf("project:%s not found", data) } + //content == nil means in restore config + if content == nil { + optimizer := tuning.Optimizer{Prj: prj} + err := optimizer.RestoreConfigTuned() + if err != nil { + return err + } + return nil + } + log.Info("begin to dynamic optimizer search") _ = stream.Send(&PB.AckCheck{Name: fmt.Sprintf("begin to dynamic optimizer search")}) -- GitLab