Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

*: support sourceID on operate-source stop #855

Merged
merged 6 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 17 additions & 7 deletions dm/ctl/master/operate_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import (
// NewOperateSourceCmd creates a OperateSource command
func NewOperateSourceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operate-source <operate-type> [config-file ...] [--print-sample-config]",
// the leading space could provide more friendly terminal printing
// cobra.Command doesn't support multi-line usage well
Use: `operate-source create|update [config-file ...] [--print-sample-config] [flags]
operate-source stop [config-file | sourceID ...] [--print-sample-config] [flags]
Copy link
Collaborator

@GMHDBJD GMHDBJD Aug 4, 2020

Choose a reason for hiding this comment

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

--print-sample-config cannot work with create/update/stop at the same time. But change to print-sample-config looks strange. 😖

Copy link
Collaborator Author

@lance6716 lance6716 Aug 4, 2020

Choose a reason for hiding this comment

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

seems --print-sample-config will overwrite the running of create/update/stop/show, I guess user will only use --print-sample-config when they don't know how to write a config file (thus they can't do a right source operation), so it's not confusing.

going to change usage to

operate-source <operate-type> [config-file ...] [--print-sample-config]

operate-source show [--print-sample-config] [flags]`,
Short: "create/update/stop/show upstream MySQL/MariaDB source",
RunE: operateSourceFunc,
}
Expand Down Expand Up @@ -96,24 +100,30 @@ func operateSourceFunc(cmd *cobra.Command, _ []string) (err error) {
return
}

contents := make([]string, len(cmd.Flags().Args())-1)
contents := make([]string, 0, len(cmd.Flags().Args())-1)
sourceID := make([]string, 0, len(cmd.Flags().Args())-1)
for i := 1; i < len(cmd.Flags().Args()); i++ {
configFile := cmd.Flags().Arg(i)
arg := cmd.Flags().Arg(i)
var content []byte
content, err = common.GetFileContent(configFile)
content, err = common.GetFileContent(arg)
if err != nil {
if op == pb.SourceOp_StopSource {
sourceID = append(sourceID, arg)
continue
}
return
}
contents[i-1] = string(content)
contents = append(contents, string(content))
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cli := common.MasterClient()
resp, err := cli.OperateSource(ctx, &pb.OperateSourceRequest{
Config: contents,
Op: op,
Config: contents,
Op: op,
SourceID: sourceID,
})
if err != nil {
return
Expand Down
12 changes: 10 additions & 2 deletions dm/master/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,9 +1469,17 @@ func (s *Server) OperateSource(ctx context.Context, req *pb.OperateSourceRequest
resp.Msg = "Update worker config is not supported by dm-ha now"
return resp, nil
case pb.SourceOp_StopSource:
toRemove := make([]string, 0, len(cfgs)+len(req.SourceID))
for _, sid := range req.SourceID {
toRemove = append(toRemove, sid)
}
for _, cfg := range cfgs {
boundM[cfg.SourceID] = s.scheduler.GetWorkerBySource(cfg.SourceID)
err := s.scheduler.RemoveSourceCfg(cfg.SourceID)
toRemove = append(toRemove, cfg.SourceID)
}

for _, sid := range toRemove {
boundM[sid] = s.scheduler.GetWorkerBySource(sid)
err := s.scheduler.RemoveSourceCfg(sid)
// TODO(lance6716):
// user could not copy-paste same command if encounter error halfway:
// `operate-source stop correct-id-1 wrong-id-2`
Expand Down
Loading