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

fixed deadlock problem for go routines

parent fe009bef
...@@ -10,11 +10,10 @@ const ( ...@@ -10,11 +10,10 @@ const (
) )
func main() { func main() {
dummyCh := make(chan bool) dummyCh := make(chan bool, 1)
commitCh := make(chan raft.LogEntry)
fmt.Println("Started") fmt.Println("Started")
for i := 1; i <= 5; i++ { for i := 1; i <= 5; i++ {
go raft.Start(i, commitCh, dummyCh, true) go raft.Start(i, true)
} }
if <-dummyCh { if <-dummyCh {
fmt.Println("khattam") fmt.Println("khattam")
......
...@@ -13,14 +13,14 @@ import ( ...@@ -13,14 +13,14 @@ import (
//constant values used //constant values used
const ( const (
CLIENT_PORT = 9000 CLIENT_PORT = 9000
LOG_PORT = 20000 LOG_PORT = 20000
ACK_TIMEOUT = 5 ACK_TIMEOUT = 5
MIN_TIMEOUT = 300 MIN_TIMEOUT = 300
MAX_TIMEOUT = 500 MAX_TIMEOUT = 500
LEADER = iota LEADER = 10
CANDIDATE CANDIDATE = 20
FOLLOWER FOLLOWER = 30
VOTED_FOR = "votedFor" VOTED_FOR = "votedFor"
CURRENT_TERM = "currentTerm" CURRENT_TERM = "currentTerm"
FILE_WRITTEN = 0 FILE_WRITTEN = 0
...@@ -261,20 +261,20 @@ func (e ErrRedirect) Error() string { ...@@ -261,20 +261,20 @@ func (e ErrRedirect) Error() string {
func (rft *Raft) loop() { func (rft *Raft) loop() {
state := FOLLOWER state := FOLLOWER
for { for {
rft.Info.Println("hello")
switch state { switch state {
case FOLLOWER: case FOLLOWER:
state = rft.follower() state = rft.follower()
// case CANDIDATE: case CANDIDATE:
// state = candidate() state = rft.candidate()
// case LEADER: case LEADER:
// state = leader() state = rft.leader()
default:
return
} }
} }
} }
func getTimer() *time.Timer { func getTimer() *time.Timer {
rand.Seed(time.Now().UnixNano())
return time.NewTimer(time.Millisecond * time.Duration((rand.Intn(MAX_TIMEOUT)+MIN_TIMEOUT)%MAX_TIMEOUT)) return time.NewTimer(time.Millisecond * time.Duration((rand.Intn(MAX_TIMEOUT)+MIN_TIMEOUT)%MAX_TIMEOUT))
} }
...@@ -304,6 +304,7 @@ func (rft *Raft) follower() int { ...@@ -304,6 +304,7 @@ func (rft *Raft) follower() int {
//start candidate timeout //start candidate timeout
electionTimeout := getTimer() electionTimeout := getTimer()
for { for {
rft.Info.Println("xyz")
//wrap in select //wrap in select
select { select {
case <-electionTimeout.C: case <-electionTimeout.C:
...@@ -378,3 +379,11 @@ func (rft *Raft) follower() int { ...@@ -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) { ...@@ -23,8 +23,9 @@ func getLogger(serverId int, toDebug bool) (l *log.Logger) {
return l return l
} }
func Start(serverId int, commitCh chan LogEntry, dummyCh chan bool, toDebug bool) { func Start(serverId int, toDebug bool) {
eventCh := make(chan RaftEvent) eventCh := make(chan RaftEvent)
commitCh := make(chan LogEntry)
clusterConfig, _ := NewClusterConfig(5) clusterConfig, _ := NewClusterConfig(5)
rft, _ := NewRaft(clusterConfig, serverId, commitCh, eventCh, true) rft, _ := NewRaft(clusterConfig, serverId, commitCh, eventCh, true)
if rafts == nil { 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