Browse Source

etcd: generate a id for the new participant after a mode switch

Xiang Li 11 years ago
parent
commit
30099d9135
2 changed files with 10 additions and 3 deletions
  1. 8 2
      etcd/etcd.go
  2. 2 1
      etcd/etcd_test.go

+ 8 - 2
etcd/etcd.go

@@ -51,7 +51,7 @@ type Server struct {
 	stopc   chan struct{}
 	stopc   chan struct{}
 }
 }
 
 
-func New(c *config.Config, id int64) *Server {
+func New(c *config.Config) *Server {
 	if err := c.Sanitize(); err != nil {
 	if err := c.Sanitize(); err != nil {
 		log.Fatalf("failed sanitizing configuration: %v", err)
 		log.Fatalf("failed sanitizing configuration: %v", err)
 	}
 	}
@@ -73,7 +73,7 @@ func New(c *config.Config, id int64) *Server {
 
 
 	s := &Server{
 	s := &Server{
 		config:       c,
 		config:       c,
-		id:           id,
+		id:           genId(),
 		pubAddr:      c.Addr,
 		pubAddr:      c.Addr,
 		raftPubAddr:  c.Peer.Addr,
 		raftPubAddr:  c.Peer.Addr,
 		tickDuration: defaultTickDuration,
 		tickDuration: defaultTickDuration,
@@ -162,5 +162,11 @@ func (s *Server) Run() {
 		default:
 		default:
 			panic("unsupport mode")
 			panic("unsupport mode")
 		}
 		}
+		s.id = genId()
 	}
 	}
 }
 }
+
+// setId sets the id for the participant. This should only be used for testing.
+func (s *Server) setId(id int64) {
+	s.id = id
+}

+ 2 - 1
etcd/etcd_test.go

@@ -373,7 +373,8 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
 }
 }
 
 
 func initTestServer(c *config.Config, id int64, tls bool) (e *Server, h *httptest.Server) {
 func initTestServer(c *config.Config, id int64, tls bool) (e *Server, h *httptest.Server) {
-	e = New(c, id)
+	e = New(c)
+	e.setId(id)
 	e.SetTick(time.Millisecond * 5)
 	e.SetTick(time.Millisecond * 5)
 	m := http.NewServeMux()
 	m := http.NewServeMux()
 	m.Handle("/", e)
 	m.Handle("/", e)