Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmctl(dm): add config task-template #3991

Merged
merged 20 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions dm/dm/ctl/master/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func NewConfigCmd() *cobra.Command {
newConfigWorkerCmd(),
newExportCfgsCmd(),
newImportCfgsCmd(),
newConfigTaskTemplateCmd(),
)
cmd.PersistentFlags().StringP("path", "p", "", "specify the file path to export/import`")
return cmd
Expand Down Expand Up @@ -79,6 +80,25 @@ func newConfigTaskCmd() *cobra.Command {
return cmd
}

func newConfigTaskTemplateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "template [task-name]",
Ehco1996 marked this conversation as resolved.
Show resolved Hide resolved
Short: "show task template which is created by WebUI",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 || len(args) > 1 {
return cmd.Help()
}
name := args[0]
output, err := cmd.Flags().GetString("path")
if err != nil {
return err
}
return sendGetConfigRequest(pb.CfgType_TaskTemplateType, name, output)
},
}
return cmd
}

// FIXME: implement this later.
func newConfigTaskUpdateCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down
61 changes: 51 additions & 10 deletions dm/dm/master/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/pingcap/tiflow/dm/pkg/cputil"
"github.com/pingcap/tiflow/dm/pkg/election"
"github.com/pingcap/tiflow/dm/pkg/etcdutil"
"github.com/pingcap/tiflow/dm/pkg/ha"
"github.com/pingcap/tiflow/dm/pkg/log"
"github.com/pingcap/tiflow/dm/pkg/terror"
"github.com/pingcap/tiflow/dm/pkg/utils"
Expand Down Expand Up @@ -2141,9 +2142,58 @@ func (s *Server) GetCfg(ctx context.Context, req *pb.GetCfgRequest) (*pb.GetCfgR
if shouldRet {
return resp2, err2
}

formartTaskString := func(subCfgList []*config.SubTaskConfig) string {
sort.Slice(subCfgList, func(i, j int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can move this sort inside SubTaskConfigsToTaskConfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ,but we still need sort for get task config, so left sort part in this func

return subCfgList[i].SourceID < subCfgList[j].SourceID
})
taskCfg := config.SubTaskConfigsToTaskConfig(subCfgList...)
taskCfg.TargetDB.Password = "******"
if taskCfg.TargetDB.Security != nil {
taskCfg.TargetDB.Security.ClearSSLBytesData()
}
return taskCfg.String()
}

// For the get-config command, you want to filter out fields that are not easily readable by humans,
// such as SSLXXBytes field in `Security` struct
switch req.Type {
case pb.CfgType_TaskTemplateType:
task, err := ha.GetOpenAPITaskConfig(s.etcdClient, req.Name)
if err != nil {
resp2.Msg = err.Error()
return resp2, nil
}
if task == nil {
resp2.Msg = "task not found"
return resp2, nil
}
toDBCfg := config.GetTargetDBCfgFromOpenAPITask(task)
if adjustDBErr := adjustTargetDB(ctx, toDBCfg); adjustDBErr != nil {
if err != nil {
Ehco1996 marked this conversation as resolved.
Show resolved Hide resolved
resp2.Msg = err.Error()
return resp2, nil
}
}
sourceCfgMap := make(map[string]*config.SourceConfig)
for _, cfg := range task.SourceConfig.SourceConf {
if sourceCfg := s.scheduler.GetSourceCfgByID(cfg.SourceName); sourceCfg != nil {
sourceCfgMap[cfg.SourceName] = sourceCfg
} else {
resp2.Msg = fmt.Sprintf("the source: %s of task not found", cfg.SourceName)
return resp2, nil
}
}
subTaskConfigList, err := config.OpenAPITaskToSubTaskConfigs(task, toDBCfg, sourceCfgMap)
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
resp2.Msg = err.Error()
return resp2, nil
}
subCfgList := make([]*config.SubTaskConfig, len(subTaskConfigList))
for i, subTaskConfig := range subTaskConfigList {
subCfgList[i] = &subTaskConfig
}
cfg = formartTaskString(subCfgList)
case pb.CfgType_TaskType:
subCfgMap := s.scheduler.GetSubTaskCfgsByTask(req.Name)
if len(subCfgMap) == 0 {
Expand All @@ -2154,16 +2204,7 @@ func (s *Server) GetCfg(ctx context.Context, req *pb.GetCfgRequest) (*pb.GetCfgR
for _, subCfg := range subCfgMap {
subCfgList = append(subCfgList, subCfg)
}
sort.Slice(subCfgList, func(i, j int) bool {
return subCfgList[i].SourceID < subCfgList[j].SourceID
})

taskCfg := config.SubTaskConfigsToTaskConfig(subCfgList...)
taskCfg.TargetDB.Password = "******"
if taskCfg.TargetDB.Security != nil {
taskCfg.TargetDB.Security.ClearSSLBytesData()
}
cfg = taskCfg.String()
cfg = formartTaskString(subCfgList)
case pb.CfgType_MasterType:
if req.Name == s.cfg.Name {
cfg, err2 = s.cfg.Toml()
Expand Down
18 changes: 16 additions & 2 deletions dm/dm/master/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/pingcap/tiflow/dm/dm/master/workerrpc"
"github.com/pingcap/tiflow/dm/dm/pb"
"github.com/pingcap/tiflow/dm/dm/pbmock"
"github.com/pingcap/tiflow/dm/openapi/fixtures"
"github.com/pingcap/tiflow/dm/pkg/conn"
"github.com/pingcap/tiflow/dm/pkg/cputil"
"github.com/pingcap/tiflow/dm/pkg/etcdutil"
Expand Down Expand Up @@ -2034,16 +2035,29 @@ func (t *testMaster) TestGetCfg(c *check.C) {
c.Assert(resp1.Result, check.IsTrue)
c.Assert(strings.Contains(resp1.Cfg, "name: test"), check.IsTrue)

// wrong task name
// not exist task name
notExistTaskName := "wrong"
req2 := &pb.GetCfgRequest{
Name: "haha",
Name: notExistTaskName,
Type: pb.CfgType_TaskType,
}
resp2, err := server.GetCfg(context.Background(), req2)
c.Assert(err, check.IsNil)
c.Assert(resp2.Result, check.IsFalse)
c.Assert(resp2.Msg, check.Equals, "task not found")

// test get task config
openapiTask, err := fixtures.GenNoShardOpenAPITaskForTest()
openapiTask.Name = notExistTaskName
c.Assert(err, check.IsNil)
Ehco1996 marked this conversation as resolved.
Show resolved Hide resolved
c.Assert(ha.PutOpenAPITaskConfig(t.etcdTestCli, openapiTask, true), check.IsNil)
c.Assert(failpoint.Enable("github.com/pingcap/tiflow/dm/dm/master/MockSkipAdjustTargetDB", `return(true)`), check.IsNil)
resp2, err = server.GetCfg(context.Background(), &pb.GetCfgRequest{Name: notExistTaskName, Type: pb.CfgType_TaskTemplateType})
c.Assert(failpoint.Disable("github.com/pingcap/tiflow/dm/dm/master/MockSkipAdjustTargetDB"), check.IsNil)
c.Assert(err, check.IsNil)
c.Assert(resp2.Result, check.IsTrue)
c.Assert(strings.Contains(resp2.Cfg, fmt.Sprintf("name: %s", notExistTaskName)), check.IsTrue)
lance6716 marked this conversation as resolved.
Show resolved Hide resolved

// test restart master
server.scheduler.Close()
c.Assert(server.scheduler.Start(ctx, t.etcdTestCli), check.IsNil)
Expand Down
Loading