|
| 1 | +// Copyright 2021 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package master |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "errors" |
| 19 | + "os" |
| 20 | + "sort" |
| 21 | + "strings" |
| 22 | + "sync" |
| 23 | + |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + "github.com/pingcap/dm/dm/ctl/common" |
| 27 | + "github.com/pingcap/dm/dm/pb" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + batchSizeFlag = "batch-size" |
| 32 | + defaultBatchSize = 5 |
| 33 | +) |
| 34 | + |
| 35 | +type batchTaskResult struct { |
| 36 | + Result bool `json:"result"` |
| 37 | + Msg string `json:"msg"` |
| 38 | + Tasks []*operateTaskResult `json:"tasks"` |
| 39 | +} |
| 40 | + |
| 41 | +type operateTaskResult struct { |
| 42 | + Task string `json:"task"` |
| 43 | + Op string `json:"op"` |
| 44 | + Result bool `json:"result"` |
| 45 | + Msg string `json:"msg"` |
| 46 | + Sources []*pb.CommonWorkerResponse `json:"sources"` |
| 47 | +} |
| 48 | + |
| 49 | +func operateTaskFunc(taskOp pb.TaskOp, cmd *cobra.Command) error { |
| 50 | + argLen := len(cmd.Flags().Args()) |
| 51 | + if argLen == 0 { |
| 52 | + // may want to operate tasks bound to a source |
| 53 | + return operateSourceTaskFunc(taskOp, cmd) |
| 54 | + } else if argLen > 1 { |
| 55 | + // can pass at most one task-name/task-conf |
| 56 | + cmd.SetOut(os.Stdout) |
| 57 | + common.PrintCmdUsage(cmd) |
| 58 | + return errors.New("please check output to see error") |
| 59 | + } |
| 60 | + |
| 61 | + name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) |
| 62 | + sources, err := common.GetSourceArgs(cmd) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + resp, err := common.OperateTask(taskOp, name, sources) |
| 68 | + if err != nil { |
| 69 | + common.PrintLinesf("can not %s task %s", strings.ToLower(taskOp.String()), name) |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + common.PrettyPrintResponse(resp) |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func addOperateSourceTaskFlags(cmd *cobra.Command) { |
| 78 | + // control workload to dm-cluster for sources with large number of tasks. |
| 79 | + cmd.Flags().Int(batchSizeFlag, defaultBatchSize, "batch size when operating all (sub)tasks bound to a source") |
| 80 | +} |
| 81 | + |
| 82 | +func operateSourceTaskFunc(taskOp pb.TaskOp, cmd *cobra.Command) error { |
| 83 | + source, batchSize, err := parseOperateSourceTaskParams(cmd) |
| 84 | + if err != nil { |
| 85 | + cmd.SetOut(os.Stdout) |
| 86 | + common.PrintCmdUsage(cmd) |
| 87 | + return errors.New("please check output to see error") |
| 88 | + } |
| 89 | + |
| 90 | + sources := []string{source} |
| 91 | + ctx, cancel := context.WithTimeout(context.Background(), common.GlobalConfig().RPCTimeout) |
| 92 | + defer cancel() |
| 93 | + |
| 94 | + req := pb.QueryStatusListRequest{Sources: sources} |
| 95 | + resp := &pb.QueryStatusListResponse{} |
| 96 | + if err := common.SendRequest(ctx, "QueryStatus", &req, &resp); err != nil { |
| 97 | + common.PrintLinesf("cannot query status of source: %v", sources) |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + if !resp.Result || len(resp.Sources) == 0 { |
| 102 | + common.PrettyPrintInterface(&batchTaskResult{Result: false, Msg: resp.Msg, Tasks: []*operateTaskResult{}}) |
| 103 | + return nil |
| 104 | + } |
| 105 | + |
| 106 | + result := batchOperateTask(taskOp, batchSize, sources, resp.Sources[0].SubTaskStatus) |
| 107 | + common.PrettyPrintInterface(result) |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func batchOperateTask(taskOp pb.TaskOp, batchSize int, sources []string, subTaskStatus []*pb.SubTaskStatus) *batchTaskResult { |
| 113 | + result := batchTaskResult{Result: true, Tasks: []*operateTaskResult{}} |
| 114 | + |
| 115 | + if len(subTaskStatus) < batchSize { |
| 116 | + batchSize = len(subTaskStatus) |
| 117 | + } |
| 118 | + |
| 119 | + workCh := make(chan string) |
| 120 | + go func() { |
| 121 | + for _, subTask := range subTaskStatus { |
| 122 | + workCh <- subTask.Name |
| 123 | + } |
| 124 | + close(workCh) |
| 125 | + }() |
| 126 | + |
| 127 | + var wg sync.WaitGroup |
| 128 | + resultCh := make(chan *operateTaskResult, 1) |
| 129 | + for i := 0; i < batchSize; i++ { |
| 130 | + wg.Add(1) |
| 131 | + go func() { |
| 132 | + defer wg.Done() |
| 133 | + |
| 134 | + for name := range workCh { |
| 135 | + taskResult := operateTaskResult{Task: name, Op: taskOp.String()} |
| 136 | + taskOpResp, err := common.OperateTask(taskOp, name, sources) |
| 137 | + if err != nil { |
| 138 | + taskResult.Result = false |
| 139 | + taskResult.Msg = err.Error() |
| 140 | + } else { |
| 141 | + taskResult.Result = taskOpResp.Result |
| 142 | + taskResult.Msg = taskOpResp.Msg |
| 143 | + taskResult.Sources = taskOpResp.Sources |
| 144 | + } |
| 145 | + resultCh <- &taskResult |
| 146 | + } |
| 147 | + }() |
| 148 | + } |
| 149 | + |
| 150 | + go func() { |
| 151 | + wg.Wait() |
| 152 | + close(resultCh) |
| 153 | + }() |
| 154 | + |
| 155 | + for item := range resultCh { |
| 156 | + result.Tasks = append(result.Tasks, item) |
| 157 | + } |
| 158 | + |
| 159 | + sort.Slice(result.Tasks, func(i, j int) bool { |
| 160 | + return result.Tasks[i].Task < result.Tasks[j].Task |
| 161 | + }) |
| 162 | + |
| 163 | + return &result |
| 164 | +} |
| 165 | + |
| 166 | +func parseOperateSourceTaskParams(cmd *cobra.Command) (string, int, error) { |
| 167 | + sources, err := common.GetSourceArgs(cmd) |
| 168 | + if err != nil { |
| 169 | + return "", 0, err |
| 170 | + } |
| 171 | + if len(sources) == 0 { |
| 172 | + common.PrintLinesf(`must give one source-name when task-name/task-conf is not specified`) |
| 173 | + return "", 0, errors.New("missing source") |
| 174 | + } else if len(sources) > 1 { |
| 175 | + common.PrintLinesf(`can give only one source-name when task-name/task-conf is not specified`) |
| 176 | + return "", 0, errors.New("too many source") |
| 177 | + } |
| 178 | + batchSize, err := cmd.Flags().GetInt(batchSizeFlag) |
| 179 | + if err != nil { |
| 180 | + common.PrintLinesf("error in parse `--" + batchSizeFlag + "`") |
| 181 | + return "", 0, err |
| 182 | + } |
| 183 | + return sources[0], batchSize, nil |
| 184 | +} |
0 commit comments