server: remove modeC var

release-2.0
Yicheng Qin 2014-07-18 10:01:39 -07:00
parent 5d498918bf
commit 6d46fc39aa
2 changed files with 16 additions and 40 deletions

View File

@ -37,7 +37,6 @@ type Server struct {
client *v2client
peerHub *peerHub
modeC chan int64
stopc chan struct{}
}
@ -74,7 +73,6 @@ func New(c *config.Config, id int64) *Server {
client: newClient(tc),
peerHub: newPeerHub(c.Peers, client),
modeC: make(chan int64, 10),
stopc: make(chan struct{}),
}
for _, seed := range c.Peers {
@ -129,12 +127,10 @@ func (s *Server) ServeRaftHTTP(w http.ResponseWriter, r *http.Request) {
func (s *Server) Run() {
next := participantMode
for {
s.modeC <- next
switch next {
case participantMode:
s.p = newParticipant(s.id, s.pubAddr, s.raftPubAddr, s.nodes, s.client, s.peerHub, s.tickDuration)
s.mode.Set(participantMode)
// TODO: it may block here. move modeC later.
next = s.p.run()
case standbyMode:
s.s = newStandby(s.id, s.pubAddr, s.raftPubAddr, s.nodes, s.client, s.peerHub)

View File

@ -92,7 +92,7 @@ func TestAdd(t *testing.T) {
}
go es[0].Run()
<-es[0].modeC
waitMode(participantMode, es[0])
for i := 1; i < tt; i++ {
id := int64(i)
@ -117,7 +117,7 @@ func TestAdd(t *testing.T) {
}
}
go es[i].Run()
<-es[i].modeC
waitMode(participantMode, es[i])
for j := 0; j <= i; j++ {
p := fmt.Sprintf("%s/%d", v2machineKVPrefix, id)
@ -189,9 +189,7 @@ func TestRemove(t *testing.T) {
}
if g := <-es[i].modeC; g != standbyMode {
t.Errorf("#%d on %d: mode = %d, want standby", k, i, g)
}
waitMode(standbyMode, es[i])
}
for i := range es {
@ -233,12 +231,7 @@ func TestBecomeStandby(t *testing.T) {
t.Fatalf("#%d: remove err = %v", i, err)
}
if g := <-es[i].modeC; g != standbyMode {
t.Fatalf("#%d: mode = %d, want standby", i, g)
}
if g := len(es[i].modeC); g != 0 {
t.Fatalf("#%d: mode to %d, want remain", i, <-es[i].modeC)
}
waitMode(standbyMode, es[i])
for k := 0; k < 4; k++ {
if es[i].s.leader != noneId {
@ -250,10 +243,6 @@ func TestBecomeStandby(t *testing.T) {
t.Errorf("#%d: lead = %d, want %d", i, g, lead)
}
if g := len(es[i].modeC); g != 0 {
t.Fatalf("#%d: mode to %d, want remain", i, <-es[i].modeC)
}
for i := range hs {
es[len(hs)-i-1].Stop()
}
@ -274,10 +263,6 @@ func TestModeSwitch(t *testing.T) {
es, hs := buildCluster(size, false)
waitCluster(t, es)
if g := <-es[i].modeC; g != participantMode {
t.Fatalf("#%d: mode = %d, want participant", i, g)
}
config := config.NewClusterConfig()
config.SyncInterval = 0
id := int64(i)
@ -296,12 +281,7 @@ func TestModeSwitch(t *testing.T) {
t.Fatalf("#%d: remove err = %v", i, err)
}
if g := <-es[i].modeC; g != standbyMode {
t.Fatalf("#%d: mode = %d, want standby", i, g)
}
if g := len(es[i].modeC); g != 0 {
t.Fatalf("#%d: mode to %d, want remain", i, <-es[i].modeC)
}
waitMode(standbyMode, es[i])
for k := 0; k < 4; k++ {
if es[i].s.leader != noneId {
@ -318,22 +298,13 @@ func TestModeSwitch(t *testing.T) {
t.Fatalf("#%d: setClusterConfig err = %v", i, err)
}
if g := <-es[i].modeC; g != participantMode {
t.Fatalf("#%d: mode = %d, want participant", i, g)
}
if g := len(es[i].modeC); g != 0 {
t.Fatalf("#%d: mode to %d, want remain", i, <-es[i].modeC)
}
waitMode(participantMode, es[i])
if err := checkParticipant(i, es); err != nil {
t.Errorf("#%d: check alive err = %v", i, err)
}
}
if g := len(es[i].modeC); g != 0 {
t.Fatalf("#%d: mode to %d, want remain", i, <-es[i].modeC)
}
for i := range hs {
es[len(hs)-i-1].Stop()
}
@ -369,7 +340,7 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
<-w.EventChan
}
go es[i].Run()
<-es[i].modeC
waitMode(participantMode, es[i])
}
return es, hs
}
@ -420,6 +391,15 @@ func waitCluster(t *testing.T, es []*Server) {
}
}
func waitMode(mode int64, e *Server) {
for {
if e.mode.Get() == mode {
return
}
time.Sleep(10 * time.Millisecond)
}
}
// checkParticipant checks the i-th server works well as participant.
func checkParticipant(i int, es []*Server) error {
lead, _ := waitActiveLeader(es)