This commit is contained in:
Xiang Li
2013-08-13 21:35:23 -07:00
committed by Brandon Philips
parent cb33641f5f
commit c3533d6ac2
4 changed files with 75 additions and 24 deletions

15
test.go
View File

@ -118,7 +118,7 @@ func destroyCluster(etcds []*os.Process) error {
}
//
func leaderMonitor(size int, allowDeadNum int, leaderChan chan string) {
func monitor(size int, allowDeadNum int, leaderChan chan string, all chan bool, stop chan bool) {
leaderMap := make(map[int]string)
baseAddrFormat := "http://0.0.0.0:400%d"
@ -131,6 +131,7 @@ func leaderMonitor(size int, allowDeadNum int, leaderChan chan string) {
leader, err := getLeader(fmt.Sprintf(baseAddrFormat, i+1))
if err == nil {
//fmt.Printf("leader:[%d]->%s\n", i, leader)
leaderMap[i] = leader
if knownLeader == "unknown" {
@ -143,6 +144,7 @@ func leaderMonitor(size int, allowDeadNum int, leaderChan chan string) {
}
} else {
//fmt.Printf("dead: [%d]\n", i)
dead++
if dead > allowDeadNum {
break
@ -152,7 +154,10 @@ func leaderMonitor(size int, allowDeadNum int, leaderChan chan string) {
}
if i == size {
//fmt.Println("leader found")
select {
case <- stop:
return
case <-leaderChan:
leaderChan <- knownLeader
default:
@ -160,6 +165,14 @@ func leaderMonitor(size int, allowDeadNum int, leaderChan chan string) {
}
}
if dead == 0 {
select {
case <-all:
all <- true
default:
all <- true
}
}
time.Sleep(time.Millisecond * 10)
}