Browse Source

feat(standby_server): save/load Running into disk

Yicheng Qin 11 years ago
parent
commit
a824be4c14
2 changed files with 7 additions and 8 deletions
  1. 1 1
      etcd/etcd.go
  2. 6 7
      server/standby_server.go

+ 1 - 1
etcd/etcd.go

@@ -240,7 +240,7 @@ func (e *Etcd) Run() {
 	peerTLSConfig := server.TLSServerConfig(e.Config.PeerTLSInfo())
 	peerTLSConfig := server.TLSServerConfig(e.Config.PeerTLSInfo())
 	etcdTLSConfig := server.TLSServerConfig(e.Config.EtcdTLSInfo())
 	etcdTLSConfig := server.TLSServerConfig(e.Config.EtcdTLSInfo())
 
 
-	if !e.StandbyServer.ClusterRecorded() {
+	if !e.StandbyServer.IsRunning() {
 		startPeerServer, possiblePeers, err := e.PeerServer.FindCluster(e.Config.Discovery, e.Config.Peers)
 		startPeerServer, possiblePeers, err := e.PeerServer.FindCluster(e.Config.Discovery, e.Config.Peers)
 		if err != nil {
 		if err != nil {
 			log.Fatal(err)
 			log.Fatal(err)

+ 6 - 7
server/standby_server.go

@@ -29,6 +29,7 @@ type StandbyServerConfig struct {
 }
 }
 
 
 type standbyInfo struct {
 type standbyInfo struct {
+	Running      bool
 	Cluster      []*machineMessage
 	Cluster      []*machineMessage
 	SyncInterval float64
 	SyncInterval float64
 }
 }
@@ -40,8 +41,7 @@ type StandbyServer struct {
 	standbyInfo
 	standbyInfo
 	joinIndex uint64
 	joinIndex uint64
 
 
-	file     *os.File
-	recorded bool
+	file *os.File
 
 
 	removeNotify chan bool
 	removeNotify chan bool
 	started      bool
 	started      bool
@@ -80,6 +80,7 @@ func (s *StandbyServer) Start() {
 		defer s.routineGroup.Done()
 		defer s.routineGroup.Done()
 		s.monitorCluster()
 		s.monitorCluster()
 	}()
 	}()
+	s.Running = true
 }
 }
 
 
 // Stop stops the server gracefully.
 // Stop stops the server gracefully.
@@ -97,6 +98,7 @@ func (s *StandbyServer) Stop() {
 	if err := s.clearStandbyInfo(); err != nil {
 	if err := s.clearStandbyInfo(); err != nil {
 		log.Warnf("error clearing cluster info for standby")
 		log.Warnf("error clearing cluster info for standby")
 	}
 	}
+	s.Running = false
 }
 }
 
 
 // RemoveNotify notifies the server is removed from standby mode and ready
 // RemoveNotify notifies the server is removed from standby mode and ready
@@ -109,8 +111,8 @@ func (s *StandbyServer) ClientHTTPHandler() http.Handler {
 	return http.HandlerFunc(s.redirectRequests)
 	return http.HandlerFunc(s.redirectRequests)
 }
 }
 
 
-func (s *StandbyServer) ClusterRecorded() bool {
-	return s.recorded
+func (s *StandbyServer) IsRunning() bool {
+	return s.Running
 }
 }
 
 
 func (s *StandbyServer) ClusterURLs() []string {
 func (s *StandbyServer) ClusterURLs() []string {
@@ -304,7 +306,6 @@ func (s *StandbyServer) loadStandbyInfo() ([]*machineMessage, error) {
 	if err := json.NewDecoder(s.file).Decode(&s.standbyInfo); err != nil {
 	if err := json.NewDecoder(s.file).Decode(&s.standbyInfo); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-	s.recorded = true
 	return s.standbyInfo.Cluster, nil
 	return s.standbyInfo.Cluster, nil
 }
 }
 
 
@@ -318,7 +319,6 @@ func (s *StandbyServer) saveStandbyInfo() error {
 	if err := s.file.Sync(); err != nil {
 	if err := s.file.Sync(); err != nil {
 		return err
 		return err
 	}
 	}
-	s.recorded = true
 	return nil
 	return nil
 }
 }
 
 
@@ -329,6 +329,5 @@ func (s *StandbyServer) clearStandbyInfo() error {
 	if err := s.file.Truncate(0); err != nil {
 	if err := s.file.Truncate(0); err != nil {
 		return err
 		return err
 	}
 	}
-	s.recorded = false
 	return nil
 	return nil
 }
 }