Commit 5c9f2304 authored by Sushant Mahajan's avatar Sushant Mahajan

fixed deadlock problem for go routines

parent fe009bef
......@@ -10,11 +10,10 @@ const (
)
func main() {
dummyCh := make(chan bool)
commitCh := make(chan raft.LogEntry)
dummyCh := make(chan bool, 1)
fmt.Println("Started")
for i := 1; i <= 5; i++ {
go raft.Start(i, commitCh, dummyCh, true)
go raft.Start(i, true)
}
if <-dummyCh {
fmt.Println("khattam")
......
......@@ -18,9 +18,9 @@ const (
ACK_TIMEOUT = 5
MIN_TIMEOUT = 300
MAX_TIMEOUT = 500
LEADER = iota
CANDIDATE
FOLLOWER
LEADER = 10
CANDIDATE = 20
FOLLOWER = 30
VOTED_FOR = "votedFor"
CURRENT_TERM = "currentTerm"
FILE_WRITTEN = 0
......@@ -261,20 +261,20 @@ func (e ErrRedirect) Error() string {
func (rft *Raft) loop() {
state := FOLLOWER
for {
rft.Info.Println("hello")
switch state {
case FOLLOWER:
state = rft.follower()
// case CANDIDATE:
// state = candidate()
// case LEADER:
// state = leader()
default:
return
case CANDIDATE:
state = rft.candidate()
case LEADER:
state = rft.leader()
}
}
}
func getTimer() *time.Timer {
rand.Seed(time.Now().UnixNano())
return time.NewTimer(time.Millisecond * time.Duration((rand.Intn(MAX_TIMEOUT)+MIN_TIMEOUT)%MAX_TIMEOUT))
}
......@@ -304,6 +304,7 @@ func (rft *Raft) follower() int {
//start candidate timeout
electionTimeout := getTimer()
for {
rft.Info.Println("xyz")
//wrap in select
select {
case <-electionTimeout.C:
......@@ -378,3 +379,11 @@ func (rft *Raft) follower() int {
}
}
}
func (rft *Raft) candidate() int {
return 1
}
func (rft *Raft) leader() int {
return 1
}
......@@ -23,8 +23,9 @@ func getLogger(serverId int, toDebug bool) (l *log.Logger) {
return l
}
func Start(serverId int, commitCh chan LogEntry, dummyCh chan bool, toDebug bool) {
func Start(serverId int, toDebug bool) {
eventCh := make(chan RaftEvent)
commitCh := make(chan LogEntry)
clusterConfig, _ := NewClusterConfig(5)
rft, _ := NewRaft(clusterConfig, serverId, commitCh, eventCh, true)
if rafts == nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment