Browse Source

Merge pull request #1328 from unihorn/169

skip initial-cluster check when reboot
Yicheng Qin 11 years ago
parent
commit
dcaa7f0a37
4 changed files with 36 additions and 31 deletions
  1. 11 8
      etcdserver/config.go
  2. 22 7
      etcdserver/config_test.go
  3. 2 5
      etcdserver/server.go
  4. 1 11
      main.go

+ 11 - 8
etcdserver/config.go

@@ -22,6 +22,7 @@ import (
 	"path"
 
 	"github.com/coreos/etcd/pkg/types"
+	"github.com/coreos/etcd/raft"
 )
 
 // ServerConfig holds the configuration of etcd as taken from the command line or discovery.
@@ -36,14 +37,21 @@ type ServerConfig struct {
 	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.
 	m := c.Cluster.FindName(c.Name)
 	if m == nil {
 		return fmt.Errorf("could not find name %v in cluster", c.Name)
 	}
+	if m.ID == raft.None {
+		return fmt.Errorf("could not use %x as member id", raft.None)
+	}
 
 	// No identical IPs in the cluster peer list
 	urlMap := make(map[string]bool)
@@ -67,8 +75,3 @@ func (c *ServerConfig) ID() uint64 { return c.Cluster.FindName(c.Name).ID }
 func (c *ServerConfig) ShouldDiscover() bool {
 	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

@@ -20,24 +20,39 @@ import (
 	"testing"
 )
 
-func TestConfigVerify(t *testing.T) {
+func TestBootstrapConfigVerify(t *testing.T) {
 	tests := []struct {
 		clusterSetting string
+		clst           ClusterState
+		disc           string
 		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 {
 		cluster := &Cluster{}
 		cluster.Set(tt.clusterSetting)
 		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 {
 			t.Errorf("#%d: Got no error where one was expected", i)
 		}

+ 2 - 5
etcdserver/server.go

@@ -159,9 +159,6 @@ type EtcdServer struct {
 // NewServer creates a new EtcdServer from the supplied configuration. The
 // configuration is considered static for the lifetime of the EtcdServer.
 func NewServer(cfg *ServerConfig) *EtcdServer {
-	if err := cfg.Verify(); err != nil {
-		log.Fatalln(err)
-	}
 	if err := os.MkdirAll(cfg.SnapDir(), privateDirMode); err != nil {
 		log.Fatalf("etcdserver: cannot create snapshot directory: %v", err)
 	}
@@ -170,8 +167,8 @@ func NewServer(cfg *ServerConfig) *EtcdServer {
 	var w *wal.WAL
 	var n raft.Node
 	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() {
 			d, err := discovery.New(cfg.DiscoveryURL, cfg.ID(), cfg.Cluster.String())

+ 1 - 11
main.go

@@ -30,7 +30,6 @@ import (
 	flagtypes "github.com/coreos/etcd/pkg/flags"
 	"github.com/coreos/etcd/pkg/transport"
 	"github.com/coreos/etcd/proxy"
-	"github.com/coreos/etcd/raft"
 )
 
 const (
@@ -140,17 +139,8 @@ func main() {
 
 // startEtcd launches the etcd server and HTTP handlers for client/server communication.
 func startEtcd() {
-	self := cluster.FindName(*name)
-	if self == nil {
-		log.Fatalf("etcd: no member with name=%q exists", *name)
-	}
-
-	if self.ID == raft.None {
-		log.Fatalf("etcd: cannot use None(%d) as member id", raft.None)
-	}
-
 	if *dir == "" {
-		*dir = fmt.Sprintf("%v_etcd_data", self.Name)
+		*dir = fmt.Sprintf("%v_etcd_data", *name)
 		log.Printf("etcd: no data-dir provided, using default data-dir ./%s", *dir)
 	}
 	if err := os.MkdirAll(*dir, privateDirMode); err != nil {