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.
release-2.0
Yicheng Qin 2014-10-17 15:43:56 -07:00
parent aa176610f3
commit 17382ec905
3 changed files with 32 additions and 21 deletions

View File

@ -21,16 +21,20 @@ 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("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
@ -55,8 +59,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
}

View File

@ -4,24 +4,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)
}

View File

@ -143,9 +143,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)
}
@ -154,8 +151,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())