|
@ -166,6 +166,8 @@ func NewMasterServer(r *mux.Router, option *MasterOption, peers map[string]pb.Se |
|
|
|
|
|
|
|
|
func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) { |
|
|
func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) { |
|
|
var raftServerName string |
|
|
var raftServerName string |
|
|
|
|
|
|
|
|
|
|
|
ms.Topo.RaftServerAccessLock.Lock() |
|
|
if raftServer.raftServer != nil { |
|
|
if raftServer.raftServer != nil { |
|
|
ms.Topo.RaftServer = raftServer.raftServer |
|
|
ms.Topo.RaftServer = raftServer.raftServer |
|
|
ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) { |
|
|
ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) { |
|
@ -193,14 +195,18 @@ func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) { |
|
|
}() |
|
|
}() |
|
|
raftServerName = ms.Topo.HashicorpRaft.String() |
|
|
raftServerName = ms.Topo.HashicorpRaft.String() |
|
|
} |
|
|
} |
|
|
|
|
|
ms.Topo.RaftServerAccessLock.Unlock() |
|
|
|
|
|
|
|
|
if ms.Topo.IsLeader() { |
|
|
if ms.Topo.IsLeader() { |
|
|
glog.V(0).Infoln("[", raftServerName, "]", "I am the leader!") |
|
|
glog.V(0).Infoln("[", raftServerName, "]", "I am the leader!") |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
ms.Topo.RaftServerAccessLock.RLock() |
|
|
if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" { |
|
|
if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" { |
|
|
glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.") |
|
|
glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.") |
|
|
} else if ms.Topo.HashicorpRaft != nil && ms.Topo.HashicorpRaft.Leader() != "" { |
|
|
} else if ms.Topo.HashicorpRaft != nil && ms.Topo.HashicorpRaft.Leader() != "" { |
|
|
glog.V(0).Infoln("[", ms.Topo.HashicorpRaft.String(), "]", ms.Topo.HashicorpRaft.Leader(), "is the leader.") |
|
|
glog.V(0).Infoln("[", ms.Topo.HashicorpRaft.String(), "]", ms.Topo.HashicorpRaft.Leader(), "is the leader.") |
|
|
} |
|
|
} |
|
|
|
|
|
ms.Topo.RaftServerAccessLock.RUnlock() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -210,16 +216,15 @@ func (ms *MasterServer) proxyToLeader(f http.HandlerFunc) http.HandlerFunc { |
|
|
f(w, r) |
|
|
f(w, r) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
var raftServerLeader string |
|
|
|
|
|
if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" { |
|
|
|
|
|
raftServerLeader = ms.Topo.RaftServer.Leader() |
|
|
|
|
|
} else if ms.Topo.HashicorpRaft != nil && ms.Topo.HashicorpRaft.Leader() != "" { |
|
|
|
|
|
raftServerLeader = string(ms.Topo.HashicorpRaft.Leader()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the current raft leader
|
|
|
|
|
|
leaderAddr, _ := ms.Topo.MaybeLeader() |
|
|
|
|
|
raftServerLeader := string(leaderAddr) |
|
|
if raftServerLeader == "" { |
|
|
if raftServerLeader == "" { |
|
|
f(w, r) |
|
|
f(w, r) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ms.boundedLeaderChan <- 1 |
|
|
ms.boundedLeaderChan <- 1 |
|
|
defer func() { <-ms.boundedLeaderChan }() |
|
|
defer func() { <-ms.boundedLeaderChan }() |
|
|
targetUrl, err := url.Parse("http://" + raftServerLeader) |
|
|
targetUrl, err := url.Parse("http://" + raftServerLeader) |
|
@ -228,6 +233,8 @@ func (ms *MasterServer) proxyToLeader(f http.HandlerFunc) http.HandlerFunc { |
|
|
fmt.Errorf("Leader URL http://%s Parse Error: %v", raftServerLeader, err)) |
|
|
fmt.Errorf("Leader URL http://%s Parse Error: %v", raftServerLeader, err)) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// proxy to leader
|
|
|
glog.V(4).Infoln("proxying to leader", raftServerLeader) |
|
|
glog.V(4).Infoln("proxying to leader", raftServerLeader) |
|
|
proxy := httputil.NewSingleHostReverseProxy(targetUrl) |
|
|
proxy := httputil.NewSingleHostReverseProxy(targetUrl) |
|
|
director := proxy.Director |
|
|
director := proxy.Director |
|
@ -336,6 +343,9 @@ func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (ms *MasterServer) OnPeerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) { |
|
|
func (ms *MasterServer) OnPeerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) { |
|
|
|
|
|
ms.Topo.RaftServerAccessLock.RLock() |
|
|
|
|
|
defer ms.Topo.RaftServerAccessLock.RUnlock() |
|
|
|
|
|
|
|
|
if update.NodeType != cluster.MasterType || ms.Topo.HashicorpRaft == nil { |
|
|
if update.NodeType != cluster.MasterType || ms.Topo.HashicorpRaft == nil { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|