github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/ctl/master/start_stop_relay.go (about) 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 21 "github.com/pingcap/tiflow/dm/ctl/common" 22 "github.com/pingcap/tiflow/dm/pb" 23 "github.com/spf13/cobra" 24 ) 25 26 // NewStartRelayCmd creates a StartRelay command. 27 func NewStartRelayCmd() *cobra.Command { 28 cmd := &cobra.Command{ 29 Use: "start-relay <-s source-id>", 30 Short: "Starts workers pulling relay log for a source", 31 RunE: startRelayFunc, 32 } 33 return cmd 34 } 35 36 // NewStopRelayCmd creates a StartRelay command. 37 func NewStopRelayCmd() *cobra.Command { 38 cmd := &cobra.Command{ 39 Use: "stop-relay <-s source-id>", 40 Short: "Stops workers pulling relay log for a source", 41 RunE: stopRelayFunc, 42 } 43 return cmd 44 } 45 46 func startRelayFunc(cmd *cobra.Command, _ []string) error { 47 return startStopRelay(cmd, pb.RelayOpV2_StartRelayV2) 48 } 49 50 func stopRelayFunc(cmd *cobra.Command, _ []string) error { 51 return startStopRelay(cmd, pb.RelayOpV2_StopRelayV2) 52 } 53 54 func startStopRelay(cmd *cobra.Command, op pb.RelayOpV2) error { 55 sources, err := common.GetSourceArgs(cmd) 56 if err != nil { 57 return err 58 } 59 60 if len(cmd.Flags().Args()) == 0 && len(sources) == 0 { 61 // all args empty 62 cmd.SetOut(os.Stdout) 63 common.PrintCmdUsage(cmd) 64 return errors.New("please check output to see error") 65 } 66 67 // TODO: support multiple sources and all sources 68 if len(sources) != 1 { 69 common.PrintLinesf("must specify one source (`-s` / `--source`)") 70 return errors.New("please check output to see error") 71 } 72 73 workers := cmd.Flags().Args() 74 if len(workers) > 0 { 75 common.PrintLinesf("start-relay/stop-relay with worker name will be deprecated soon. You can try stopping relay first and use start-relay without worker name instead") 76 } 77 78 ctx, cancel := context.WithCancel(context.Background()) 79 defer cancel() 80 81 resp := &pb.OperateRelayResponse{} 82 err = common.SendRequest( 83 ctx, 84 "OperateRelay", 85 &pb.OperateRelayRequest{ 86 Op: op, 87 Source: sources[0], 88 Worker: workers, 89 }, 90 &resp, 91 ) 92 93 if err != nil { 94 return err 95 } 96 97 common.PrettyPrintResponse(resp) 98 return nil 99 }