فهرست منبع

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.
Yicheng Qin 10 سال پیش
والد
کامیت
9757dcd3a2
3فایلهای تغییر یافته به همراه39 افزوده شده و 42 حذف شده
  1. 33 31
      etcdmain/etcd.go
  2. 5 10
      etcdserver/errors.go
  3. 1 1
      etcdserver/server.go

+ 33 - 31
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()

+ 5 - 10
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)
 }

+ 1 - 1
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 {