github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/utils/dmctl.go (about) 1 // Copyright 2019 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 utils 15 16 import ( 17 "context" 18 "os" 19 "strings" 20 "time" 21 22 "github.com/pingcap/errors" 23 "github.com/pingcap/tiflow/dm/pb" 24 "google.golang.org/grpc" 25 ) 26 27 // CreateDmCtl creates a gRPC client to DM-master. 28 func CreateDmCtl(addr string) (pb.MasterClient, error) { 29 // TODO: use tls, this function is not used 30 //nolint:staticcheck 31 conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBackoffMaxDelay(3*time.Second)) 32 if err != nil { 33 return nil, errors.Trace(err) 34 } 35 return pb.NewMasterClient(conn), nil 36 } 37 38 // StartTask starts a task with the specified config. 39 func StartTask(ctx context.Context, cli pb.MasterClient, configFile string, workers []string) error { 40 content, err := os.ReadFile(configFile) 41 if err != nil { 42 return errors.Trace(err) 43 } 44 45 resp, err := cli.StartTask(ctx, &pb.StartTaskRequest{ 46 Task: string(content), 47 Sources: workers, 48 }) 49 if err != nil { 50 return errors.Trace(err) 51 } 52 53 if !resp.GetResult() { 54 return errors.Errorf("start task resp error: %s", resp.GetMsg()) 55 } 56 57 for _, wp := range resp.GetSources() { 58 if !wp.GetResult() && !strings.Contains(wp.GetMsg(), "request is timeout, but request may be successful") { 59 return errors.Errorf("fail to start task %v: %s", string(content), wp.GetMsg()) 60 } 61 } 62 63 return nil 64 } 65 66 // OperateTask does operations on the task. 67 func OperateTask(ctx context.Context, cli pb.MasterClient, op pb.TaskOp, name string, workers []string) error { 68 resp, err := cli.OperateTask(ctx, &pb.OperateTaskRequest{ 69 Op: op, 70 Name: name, 71 Sources: workers, 72 }) 73 if err != nil { 74 return errors.Trace(err) 75 } 76 77 for _, wp := range resp.GetSources() { 78 if !wp.GetResult() { 79 return errors.Errorf("fail to do %v operate on task %s: %s", op, name, wp.GetMsg()) 80 } 81 } 82 83 return nil 84 }