From aa4e9daf1217e7433218c4c681174b6cd326adf8 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Fri, 29 Jul 2016 20:45:59 -0700 Subject: [PATCH] Touches up Raft integration after latest changes. --- consul/config.go | 5 +++-- consul/server.go | 27 +++++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/consul/config.go b/consul/config.go index 5168ab573c..fb313c0fab 100644 --- a/consul/config.go +++ b/consul/config.go @@ -310,8 +310,9 @@ func DefaultConfig() *Config { conf.SerfLANConfig.MemberlistConfig.BindPort = DefaultLANSerfPort conf.SerfWANConfig.MemberlistConfig.BindPort = DefaultWANSerfPort - // Enable interoperability with unversioned Raft library - conf.RaftConfig.ProtocolVersion = 0 + // Enable interoperability with unversioned Raft library, and don't + // start using new ID-based features yet. + conf.RaftConfig.ProtocolVersion = 1 // Disable shutdown on removal conf.RaftConfig.ShutdownOnRemove = false diff --git a/consul/server.go b/consul/server.go index 0990d4b8f8..f005b23e08 100644 --- a/consul/server.go +++ b/consul/server.go @@ -355,6 +355,15 @@ func (s *Server) setupRaft() error { trans := raft.NewNetworkTransport(s.raftLayer, 3, 10*time.Second, s.config.LogOutput) s.raftTransport = trans + // Make sure we set the LogOutput. + s.config.RaftConfig.LogOutput = s.config.LogOutput + + // Our version of Raft protocol requires the LocalID to match the network + // address of the transport. + s.config.RaftConfig.LocalID = raft.ServerID(trans.LocalAddr()) + + // Build an all in-memory setup for dev mode, otherwise prepare a full + // disk-based setup. var log raft.LogStore var stable raft.StableStore var snap raft.SnapshotStore @@ -394,21 +403,22 @@ func (s *Server) setupRaft() error { snap = snapshots // If we see a peers.json file, attempt recovery based on it. - recovery, err := raft.NewPeersJSONRecovery(path) - if err != nil && !os.IsNotExist(err) { - return fmt.Errorf("recovery failed to parse peers.json: %v", err) - } - if recovery != nil { + peersFile := filepath.Join(path, "peers.json") + if _, err := os.Stat(peersFile); err == nil { s.logger.Printf("[INFO] consul: found peers.json file, recovering Raft configuration...") + configuration, err := raft.ReadPeersJSON(peersFile) + if err != nil { + return fmt.Errorf("recovery failed to parse peers.json: %v", err) + } tmpFsm, err := NewFSM(s.tombstoneGC, s.config.LogOutput) if err != nil { return fmt.Errorf("recovery failed to make temp FSM: %v", err) } if err := raft.RecoverCluster(s.config.RaftConfig, tmpFsm, - log, stable, snap, recovery.Configuration); err != nil { + log, stable, snap, configuration); err != nil { return fmt.Errorf("recovery failed: %v", err) } - if err := recovery.Disarm(); err != nil { + if err := os.Remove(peersFile); err != nil { return fmt.Errorf("recovery failed to delete peers.json, please delete manually: %v", err) } s.logger.Printf("[INFO] consul: deleted peers.json file after successful recovery") @@ -465,9 +475,6 @@ func (s *Server) setupRaft() error { } } - // Make sure we set the LogOutput. - s.config.RaftConfig.LogOutput = s.config.LogOutput - // Setup the Raft store. s.raft, err = raft.NewRaft(s.config.RaftConfig, s.fsm, log, stable, snap, trans) if err != nil {