From 764cafb7ac0ead7ec3f24bae52c50b056833fcd1 Mon Sep 17 00:00:00 2001 From: lupengfan1 Date: Fri, 6 Dec 2024 11:00:10 +0800 Subject: [PATCH] follow acelyc111 advice --- admin-cli/cmd/init.go | 2 +- admin-cli/executor/client.go | 35 ++++--------------- .../toolkits/tablemigrator/switcher.go | 3 +- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/admin-cli/cmd/init.go b/admin-cli/cmd/init.go index f910d67eb7..50fcb28395 100644 --- a/admin-cli/cmd/init.go +++ b/admin-cli/cmd/init.go @@ -34,5 +34,5 @@ var pegasusClient *executor.Client func Init(metaList []string) { globalMetaList = metaList - pegasusClient = executor.NewClient(os.Stdout, globalMetaList) + pegasusClient, _ = executor.NewClient(os.Stdout, globalMetaList, true) } diff --git a/admin-cli/executor/client.go b/admin-cli/executor/client.go index edaee5504b..f94deed0e5 100644 --- a/admin-cli/executor/client.go +++ b/admin-cli/executor/client.go @@ -46,39 +46,18 @@ type Client struct { } // NewClient creates a client for accessing Pegasus cluster for use of admin-cli. -// This function will call os.Exit. -func NewClient(writer io.Writer, metaAddrs []string) *Client { +// When listing nodes fails, willExit == true means call os.Exit(). +func NewClient(writer io.Writer, metaAddrs []string, willExit bool) (*Client, error) { meta := client.NewRPCBasedMeta(metaAddrs) // TODO(wutao): initialize replica-nodes lazily nodes, err := meta.ListNodes() if err != nil { - fmt.Fprintf(writer, "fatal: failed to list nodes [%s]\n", err) - os.Exit(1) - } - - var replicaAddrs []string - for _, node := range nodes { - replicaAddrs = append(replicaAddrs, node.Address.GetAddress()) - } - - return &Client{ - Writer: writer, - Meta: meta, - Nodes: util.NewPegasusNodeManager(metaAddrs, replicaAddrs), - Perf: aggregate.NewPerfClient(metaAddrs), - } -} - -// NewClientWithoutExit creates a client for accessing Pegasus cluster for use of admin-cli. -// This function will not call os.Exit. -func NewClientWithoutExit(writer io.Writer, metaAddrs []string) (*Client, error) { - meta := client.NewRPCBasedMeta(metaAddrs) - - nodes, err := meta.ListNodes() - if err != nil { - fmt.Fprintf(writer, "fatal: failed to list nodes [%s]\n", err) - return nil, fmt.Errorf("fatal: failed to list nodes [%s]", err) + fmt.Printf("Error: failed to list nodes [%s]\n", err) + if willExit { + os.Exit(1) + } + return nil, fmt.Errorf("failed to list nodes [%s]", err) } var replicaAddrs []string diff --git a/admin-cli/executor/toolkits/tablemigrator/switcher.go b/admin-cli/executor/toolkits/tablemigrator/switcher.go index 76c223a98d..41f2be6d05 100644 --- a/admin-cli/executor/toolkits/tablemigrator/switcher.go +++ b/admin-cli/executor/toolkits/tablemigrator/switcher.go @@ -59,7 +59,8 @@ func SwitchMetaAddrs(client *executor.Client, zkAddr string, zkRoot string, tabl originMeta := client.Meta targetAddrList := strings.Split(targetAddrs, ",") - targetMeta := executor.NewClient(os.Stdout, targetAddrList).Meta + pegasusClient, _ := executor.NewClient(os.Stdout, targetAddrList, true) + targetMeta := pegasusClient.Meta env := map[string]string{ "replica.deny_client_request": "reconfig*all", }