Преглед изворни кода

etcd: remove participant.seeds

Xiang Li пре 11 година
родитељ
комит
7e7cfb1ce8
3 измењених фајлова са 16 додато и 6 уклоњено
  1. 1 1
      etcd/etcd.go
  2. 5 5
      etcd/participant.go
  3. 10 0
      etcd/peer_hub.go

+ 1 - 1
etcd/etcd.go

@@ -134,7 +134,7 @@ func (s *Server) Run() {
 		}
 		switch next {
 		case participantMode:
-			s.p = newParticipant(s.id, s.pubAddr, s.raftPubAddr, s.nodes, s.client, s.peerHub, s.tickDuration)
+			s.p = newParticipant(s.id, s.pubAddr, s.raftPubAddr, s.client, s.peerHub, s.tickDuration)
 			s.mode.Set(participantMode)
 			s.mu.Unlock()
 			next = s.p.run()

+ 5 - 5
etcd/participant.go

@@ -64,12 +64,11 @@ type participant struct {
 	*http.ServeMux
 }
 
-func newParticipant(id int64, pubAddr string, raftPubAddr string, seeds map[string]bool, client *v2client, peerHub *peerHub, tickDuration time.Duration) *participant {
+func newParticipant(id int64, pubAddr string, raftPubAddr string, client *v2client, peerHub *peerHub, tickDuration time.Duration) *participant {
 	p := &participant{
 		id:           id,
 		pubAddr:      pubAddr,
 		raftPubAddr:  raftPubAddr,
-		seeds:        seeds,
 		tickDuration: tickDuration,
 
 		client:  client,
@@ -101,13 +100,14 @@ func newParticipant(id int64, pubAddr string, raftPubAddr string, seeds map[stri
 }
 
 func (p *participant) run() int64 {
-	if len(p.seeds) == 0 {
+	seeds := p.peerHub.getSeeds()
+	if len(seeds) == 0 {
 		log.Println("starting a bootstrap node")
 		p.node.Campaign()
 		p.node.Add(p.id, p.raftPubAddr, []byte(p.pubAddr))
 		p.apply(p.node.Next())
 	} else {
-		log.Println("joining cluster via peers", p.seeds)
+		log.Println("joining cluster via peers", seeds)
 		p.join()
 	}
 
@@ -316,7 +316,7 @@ func (p *participant) join() {
 	}
 
 	for {
-		for seed := range p.seeds {
+		for seed := range p.peerHub.getSeeds() {
 			if err := p.client.AddMachine(seed, fmt.Sprint(p.id), info); err == nil {
 				return
 			} else {

+ 10 - 0
etcd/peer_hub.go

@@ -41,6 +41,16 @@ func newPeerHub(seeds []string, c *http.Client) *peerHub {
 	return h
 }
 
+func (h *peerHub) getSeeds() map[string]bool {
+	h.mu.RLock()
+	defer h.mu.RUnlock()
+	s := make(map[string]bool)
+	for k, v := range h.seeds {
+		s[k] = v
+	}
+	return s
+}
+
 func (h *peerHub) stop() {
 	h.mu.Lock()
 	defer h.mu.Unlock()