From 9757dcd3a2b3b72b681bf9fb1c0d73ea3f153d3f Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Tue, 27 Oct 2015 14:39:38 -0700 Subject: [PATCH] etcdmain: fix parsing discovery error The discovery error is wrapped into a struct now, and cannot be compared to predefined errors. Correct the comparison behavior to fix the problem. --- etcdmain/etcd.go | 64 +++++++++++++++++++++++--------------------- etcdserver/errors.go | 15 ++++------- etcdserver/server.go | 2 +- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/etcdmain/etcd.go b/etcdmain/etcd.go index 8cde657c7..d67156a58 100644 --- a/etcdmain/etcd.go +++ b/etcdmain/etcd.go @@ -124,9 +124,11 @@ func Main() { shouldProxy := cfg.isProxy() if !shouldProxy { stopped, err = startEtcd(cfg) - if err == discovery.ErrFullCluster && cfg.shouldFallbackToProxy() { - plog.Noticef("discovery cluster full, falling back to %s", fallbackFlagProxy) - shouldProxy = true + if derr, ok := err.(*etcdserver.DiscoveryError); ok && derr.Err == discovery.ErrFullCluster { + if cfg.shouldFallbackToProxy() { + plog.Noticef("discovery cluster full, falling back to %s", fallbackFlagProxy) + shouldProxy = true + } } } if shouldProxy { @@ -135,39 +137,39 @@ func Main() { } if err != nil { - switch err { - case discovery.ErrDuplicateID: - plog.Errorf("member %q has previously registered with discovery service token (%s).", cfg.name, cfg.durl) - plog.Errorf("But etcd could not find valid cluster configuration in the given data dir (%s).", cfg.dir) - plog.Infof("Please check the given data dir path if the previous bootstrap succeeded") - plog.Infof("or use a new discovery token if the previous bootstrap failed.") - case discovery.ErrDuplicateName: - plog.Errorf("member with duplicated name has registered with discovery service token(%s).", cfg.durl) - plog.Errorf("please check (cURL) the discovery token for more information.") - plog.Errorf("please do not reuse the discovery token and generate a new one to bootstrap the cluster.") - default: - if strings.Contains(err.Error(), "include") && strings.Contains(err.Error(), "--initial-cluster") { - plog.Infof("%v", err) - if cfg.initialCluster == initialClusterFromName(cfg.name) { - plog.Infof("forgot to set --initial-cluster flag?") - } - if types.URLs(cfg.apurls).String() == defaultInitialAdvertisePeerURLs { - plog.Infof("forgot to set --initial-advertise-peer-urls flag?") - } - if cfg.initialCluster == initialClusterFromName(cfg.name) && len(cfg.durl) == 0 { - plog.Infof("if you want to use discovery service, please set --discovery flag.") - } - os.Exit(1) - } - if etcdserver.IsDiscoveryError(err) { + if derr, ok := err.(*etcdserver.DiscoveryError); ok { + switch derr.Err { + case discovery.ErrDuplicateID: + plog.Errorf("member %q has previously registered with discovery service token (%s).", cfg.name, cfg.durl) + plog.Errorf("But etcd could not find valid cluster configuration in the given data dir (%s).", cfg.dir) + plog.Infof("Please check the given data dir path if the previous bootstrap succeeded") + plog.Infof("or use a new discovery token if the previous bootstrap failed.") + case discovery.ErrDuplicateName: + plog.Errorf("member with duplicated name has registered with discovery service token(%s).", cfg.durl) + plog.Errorf("please check (cURL) the discovery token for more information.") + plog.Errorf("please do not reuse the discovery token and generate a new one to bootstrap the cluster.") + default: plog.Errorf("%v", err) plog.Infof("discovery token %s was used, but failed to bootstrap the cluster.", cfg.durl) plog.Infof("please generate a new discovery token and try to bootstrap again.") - os.Exit(1) } - plog.Fatalf("%v", err) + os.Exit(1) } - os.Exit(1) + + if strings.Contains(err.Error(), "include") && strings.Contains(err.Error(), "--initial-cluster") { + plog.Infof("%v", err) + if cfg.initialCluster == initialClusterFromName(cfg.name) { + plog.Infof("forgot to set --initial-cluster flag?") + } + if types.URLs(cfg.apurls).String() == defaultInitialAdvertisePeerURLs { + plog.Infof("forgot to set --initial-advertise-peer-urls flag?") + } + if cfg.initialCluster == initialClusterFromName(cfg.name) && len(cfg.durl) == 0 { + plog.Infof("if you want to use discovery service, please set --discovery flag.") + } + os.Exit(1) + } + plog.Fatalf("%v", err) } osutil.HandleInterrupts() diff --git a/etcdserver/errors.go b/etcdserver/errors.go index 0f1c2043c..bd9a07fd4 100644 --- a/etcdserver/errors.go +++ b/etcdserver/errors.go @@ -40,16 +40,11 @@ func isKeyNotFound(err error) bool { return ok && e.ErrorCode == etcdErr.EcodeKeyNotFound } -type discoveryError struct { - op string - err error +type DiscoveryError struct { + Op string + Err error } -func (e discoveryError) Error() string { - return fmt.Sprintf("failed to %s discovery cluster (%v)", e.op, e.err) -} - -func IsDiscoveryError(err error) bool { - _, ok := err.(*discoveryError) - return ok +func (e DiscoveryError) Error() string { + return fmt.Sprintf("failed to %s discovery cluster (%v)", e.Op, e.Err) } diff --git a/etcdserver/server.go b/etcdserver/server.go index 146ba4022..e5c232362 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -258,7 +258,7 @@ func NewServer(cfg *ServerConfig) (*EtcdServer, error) { var err error str, err = discovery.JoinCluster(cfg.DiscoveryURL, cfg.DiscoveryProxy, m.ID, cfg.InitialPeerURLsMap.String()) if err != nil { - return nil, &discoveryError{op: "join", err: err} + return nil, &DiscoveryError{Op: "join", Err: err} } urlsmap, err := types.NewURLsMap(str) if err != nil {