Browse Source

etcdserver: skip initial-cluster check when reboot

If etcd is provided with data-dir that has data, it will not use
initial-cluster, and initial-cluster could be set to be empty.
Yicheng Qin 11 years ago
parent
commit
17382ec905
3 changed files with 32 additions and 21 deletions
  1. 8 9
      etcdserver/config.go
  2. 22 7
      etcdserver/config_test.go
  3. 2 5
      etcdserver/server.go

+ 8 - 9
etcdserver/config.go

@@ -21,16 +21,20 @@ type ServerConfig struct {
 	Transport    *http.Transport
 	Transport    *http.Transport
 }
 }
 
 
-// Verify sanity-checks the config struct and returns an error for things that
-// should never happen.
-func (c *ServerConfig) Verify() error {
+// VerifyBootstrapConfig sanity-checks the initial config and returns an error
+// for things that should never happen.
+func (c *ServerConfig) VerifyBootstrapConfig() error {
+	if c.DiscoveryURL == "" && c.ClusterState != ClusterStateValueNew {
+		return fmt.Errorf("initial cluster state unset and no wal or discovery URL found")
+	}
+
 	// Make sure the cluster at least contains the local server.
 	// Make sure the cluster at least contains the local server.
 	m := c.Cluster.FindName(c.Name)
 	m := c.Cluster.FindName(c.Name)
 	if m == nil {
 	if m == nil {
 		return fmt.Errorf("could not find name %v in cluster", c.Name)
 		return fmt.Errorf("could not find name %v in cluster", c.Name)
 	}
 	}
 	if m.ID == raft.None {
 	if m.ID == raft.None {
-		return fmt.Errorf("cannot use None(%x) as member id", raft.None)
+		return fmt.Errorf("could not use %x as member id", raft.None)
 	}
 	}
 
 
 	// No identical IPs in the cluster peer list
 	// No identical IPs in the cluster peer list
@@ -55,8 +59,3 @@ func (c *ServerConfig) ID() uint64 { return c.Cluster.FindName(c.Name).ID }
 func (c *ServerConfig) ShouldDiscover() bool {
 func (c *ServerConfig) ShouldDiscover() bool {
 	return c.DiscoveryURL != ""
 	return c.DiscoveryURL != ""
 }
 }
-
-// IsBootstrap returns true if a bootstrap method is provided.
-func (c *ServerConfig) IsBootstrap() bool {
-	return c.DiscoveryURL != "" || c.ClusterState == ClusterStateValueNew
-}

+ 22 - 7
etcdserver/config_test.go

@@ -4,24 +4,39 @@ import (
 	"testing"
 	"testing"
 )
 )
 
 
-func TestConfigVerify(t *testing.T) {
+func TestBootstrapConfigVerify(t *testing.T) {
 	tests := []struct {
 	tests := []struct {
 		clusterSetting string
 		clusterSetting string
+		clst           ClusterState
+		disc           string
 		shouldError    bool
 		shouldError    bool
 	}{
 	}{
-		{"", true},
-		{"node1=http://localhost:7001,node2=http://localhost:7001", true},
-		{"node1=http://localhost:7001,node2=http://localhost:7002", false},
+		{"", ClusterStateValueNew, "", true},
+		{"", "", "http://discovery", true},
+		{
+			"node1=http://localhost:7001,node2=http://localhost:7001",
+			ClusterStateValueNew, "", true,
+		},
+		{
+			"node1=http://localhost:7001,node2=http://localhost:7002",
+			ClusterStateValueNew, "", false,
+		},
+		{
+			"node1=http://localhost:7001",
+			"", "http://discovery", false,
+		},
 	}
 	}
 
 
 	for i, tt := range tests {
 	for i, tt := range tests {
 		cluster := &Cluster{}
 		cluster := &Cluster{}
 		cluster.Set(tt.clusterSetting)
 		cluster.Set(tt.clusterSetting)
 		cfg := ServerConfig{
 		cfg := ServerConfig{
-			Name:    "node1",
-			Cluster: cluster,
+			Name:         "node1",
+			DiscoveryURL: tt.disc,
+			Cluster:      cluster,
+			ClusterState: tt.clst,
 		}
 		}
-		err := cfg.Verify()
+		err := cfg.VerifyBootstrapConfig()
 		if (err == nil) && tt.shouldError {
 		if (err == nil) && tt.shouldError {
 			t.Errorf("#%d: Got no error where one was expected", i)
 			t.Errorf("#%d: Got no error where one was expected", i)
 		}
 		}

+ 2 - 5
etcdserver/server.go

@@ -143,9 +143,6 @@ type EtcdServer struct {
 // NewServer creates a new EtcdServer from the supplied configuration. The
 // NewServer creates a new EtcdServer from the supplied configuration. The
 // configuration is considered static for the lifetime of the EtcdServer.
 // configuration is considered static for the lifetime of the EtcdServer.
 func NewServer(cfg *ServerConfig) *EtcdServer {
 func NewServer(cfg *ServerConfig) *EtcdServer {
-	if err := cfg.Verify(); err != nil {
-		log.Fatalln(err)
-	}
 	if err := os.MkdirAll(cfg.SnapDir(), privateDirMode); err != nil {
 	if err := os.MkdirAll(cfg.SnapDir(), privateDirMode); err != nil {
 		log.Fatalf("etcdserver: cannot create snapshot directory: %v", err)
 		log.Fatalf("etcdserver: cannot create snapshot directory: %v", err)
 	}
 	}
@@ -154,8 +151,8 @@ func NewServer(cfg *ServerConfig) *EtcdServer {
 	var w *wal.WAL
 	var w *wal.WAL
 	var n raft.Node
 	var n raft.Node
 	if !wal.Exist(cfg.WALDir()) {
 	if !wal.Exist(cfg.WALDir()) {
-		if !cfg.IsBootstrap() {
-			log.Fatalf("etcdserver: initial cluster state unset and no wal or discovery URL found")
+		if err := cfg.VerifyBootstrapConfig(); err != nil {
+			log.Fatalf("etcdserver: %v", err)
 		}
 		}
 		if cfg.ShouldDiscover() {
 		if cfg.ShouldDiscover() {
 			d, err := discovery.New(cfg.DiscoveryURL, cfg.ID(), cfg.Cluster.String())
 			d, err := discovery.New(cfg.DiscoveryURL, cfg.ID(), cfg.Cluster.String())