Browse Source

etcd: fake standby

Xiang Li 11 years ago
parent
commit
8ccb8b1f9f
1 changed files with 28 additions and 1 deletions
  1. 28 1
      etcd/etcd.go

+ 28 - 1
etcd/etcd.go

@@ -30,9 +30,17 @@ const (
 	raftPrefix = "/raft"
 	raftPrefix = "/raft"
 )
 )
 
 
+const (
+	participant = iota
+	standby
+	stop
+)
+
 type Server struct {
 type Server struct {
 	config *config.Config
 	config *config.Config
 
 
+	mode int
+
 	id           int64
 	id           int64
 	pubAddr      string
 	pubAddr      string
 	raftPubAddr  string
 	raftPubAddr  string
@@ -90,7 +98,6 @@ func New(c *config.Config, id int64) *Server {
 	}
 	}
 
 
 	m := http.NewServeMux()
 	m := http.NewServeMux()
-	//m.Handle("/HEAD", handlerErr(s.serveHead))
 	m.Handle(v2Prefix+"/", handlerErr(s.serveValue))
 	m.Handle(v2Prefix+"/", handlerErr(s.serveValue))
 	m.Handle(v2machinePrefix, handlerErr(s.serveMachines))
 	m.Handle(v2machinePrefix, handlerErr(s.serveMachines))
 	m.Handle(v2peersPrefix, handlerErr(s.serveMachines))
 	m.Handle(v2peersPrefix, handlerErr(s.serveMachines))
@@ -153,6 +160,21 @@ func (s *Server) Join() {
 }
 }
 
 
 func (s *Server) run() {
 func (s *Server) run() {
+	for {
+		switch s.mode {
+		case participant:
+			s.runParticipant()
+		case standby:
+			s.runStandby()
+		case stop:
+			return
+		default:
+			panic("unsupport mode")
+		}
+	}
+}
+
+func (s *Server) runParticipant() {
 	node := s.node
 	node := s.node
 	recv := s.t.recv
 	recv := s.t.recv
 	ticker := time.NewTicker(s.tickDuration)
 	ticker := time.NewTicker(s.tickDuration)
@@ -176,6 +198,7 @@ func (s *Server) run() {
 			node.Sync()
 			node.Sync()
 		case <-s.stop:
 		case <-s.stop:
 			log.Printf("Node: %d stopped\n", s.id)
 			log.Printf("Node: %d stopped\n", s.id)
+			s.mode = stop
 			return
 			return
 		}
 		}
 		s.apply(node.Next())
 		s.apply(node.Next())
@@ -183,6 +206,10 @@ func (s *Server) run() {
 	}
 	}
 }
 }
 
 
+func (s *Server) runStandby() {
+	panic("unimplemented")
+}
+
 func (s *Server) apply(ents []raft.Entry) {
 func (s *Server) apply(ents []raft.Entry) {
 	offset := s.node.Applied() - int64(len(ents)) + 1
 	offset := s.node.Applied() - int64(len(ents)) + 1
 	for i, ent := range ents {
 	for i, ent := range ents {