Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cs733
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Sushant Mahajan
cs733
Commits
ab7ec028
Commit
ab7ec028
authored
Mar 23, 2015
by
Sushant Mahajan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added partial leader code
parent
90c89103
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
10 deletions
+41
-10
assignment3/src/raft/raft.go
assignment3/src/raft/raft.go
+40
-9
assignment3/src/raft/server.go
assignment3/src/raft/server.go
+1
-1
No files found.
assignment3/src/raft/raft.go
View file @
ab7ec028
...
@@ -16,8 +16,9 @@ const (
...
@@ -16,8 +16,9 @@ const (
CLIENT_PORT
=
9000
CLIENT_PORT
=
9000
LOG_PORT
=
20000
LOG_PORT
=
20000
ACK_TIMEOUT
=
5
ACK_TIMEOUT
=
5
MIN_TIMEOUT
=
300
MIN_TIMEOUT_ELEC
=
300
MAX_TIMEOUT
=
500
MAX_TIMEOUT_ELEC
=
500
HEARTBEAT_TIMEOUT
=
100
LEADER
=
10
LEADER
=
10
CANDIDATE
=
20
CANDIDATE
=
20
FOLLOWER
=
30
FOLLOWER
=
30
...
@@ -283,7 +284,7 @@ func (rft *Raft) loop() {
...
@@ -283,7 +284,7 @@ func (rft *Raft) loop() {
func
getRandTime
(
log
*
log
.
Logger
)
time
.
Duration
{
func
getRandTime
(
log
*
log
.
Logger
)
time
.
Duration
{
rand
.
Seed
(
time
.
Now
()
.
UnixNano
())
rand
.
Seed
(
time
.
Now
()
.
UnixNano
())
t
:=
time
.
Millisecond
*
time
.
Duration
(
rand
.
Intn
(
MAX_TIMEOUT
-
MIN_TIMEOUT
)
+
MIN_TIMEOUT
)
t
:=
time
.
Millisecond
*
time
.
Duration
(
rand
.
Intn
(
MAX_TIMEOUT
_ELEC
-
MIN_TIMEOUT_ELEC
)
+
MIN_TIMEOUT_ELEC
)
log
.
Println
(
"New rand time"
,
t
)
log
.
Println
(
"New rand time"
,
t
)
return
t
return
t
}
}
...
@@ -292,7 +293,8 @@ func (rft *Raft) grantVote(reply bool, currentTerm int) {
...
@@ -292,7 +293,8 @@ func (rft *Raft) grantVote(reply bool, currentTerm int) {
if
reply
{
if
reply
{
rft
.
voters
++
rft
.
voters
++
rft
.
Info
.
Println
(
rft
.
id
,
"got vote"
)
rft
.
Info
.
Println
(
rft
.
id
,
"got vote"
)
if
rft
.
voters
>=
len
(
rafts
)
/
2
+
1
{
if
rft
.
voters
>
len
(
rafts
)
/
2
{
rft
.
LogC
(
"got majority"
)
rft
.
monitorVotesCh
<-
true
rft
.
monitorVotesCh
<-
true
}
}
}
}
...
@@ -328,7 +330,7 @@ func (rft *Raft) follower() int {
...
@@ -328,7 +330,7 @@ func (rft *Raft) follower() int {
//wrap in select
//wrap in select
select
{
select
{
case
<-
rft
.
et
.
C
:
case
<-
rft
.
et
.
C
:
rft
.
LogF
(
"
follower
election timeout"
)
rft
.
LogF
(
"election timeout"
)
return
CANDIDATE
return
CANDIDATE
case
event
:=
<-
rft
.
eventCh
:
case
event
:=
<-
rft
.
eventCh
:
switch
event
.
(
type
)
{
switch
event
.
(
type
)
{
...
@@ -369,6 +371,11 @@ func (rft *Raft) follower() int {
...
@@ -369,6 +371,11 @@ func (rft *Raft) follower() int {
rft
.
et
.
Reset
(
getRandTime
(
rft
.
Info
))
rft
.
et
.
Reset
(
getRandTime
(
rft
.
Info
))
rft
.
LogF
(
"reset timer on appendRPC"
)
rft
.
LogF
(
"reset timer on appendRPC"
)
req
:=
event
.
(
*
AppendRPC
)
req
:=
event
.
(
*
AppendRPC
)
if
len
(
req
.
entries
)
==
0
{
//heartbeat
rft
.
LogF
(
"got hearbeat from "
+
strconv
.
Itoa
(
req
.
leaderId
))
continue
}
reply
:=
true
reply
:=
true
if
req
.
term
<
rft
.
currentTerm
{
if
req
.
term
<
rft
.
currentTerm
{
reply
=
false
reply
=
false
...
@@ -454,6 +461,7 @@ func (rft *Raft) candidate() int {
...
@@ -454,6 +461,7 @@ func (rft *Raft) candidate() int {
switch
event
.
(
type
)
{
switch
event
.
(
type
)
{
case
(
*
AppendRPC
)
:
case
(
*
AppendRPC
)
:
rft
.
LogC
(
"C to F"
)
rft
.
LogC
(
"C to F"
)
rft
.
et
.
Reset
(
getRandTime
(
rft
.
Info
))
return
FOLLOWER
return
FOLLOWER
}
}
}
}
...
@@ -461,9 +469,32 @@ func (rft *Raft) candidate() int {
...
@@ -461,9 +469,32 @@ func (rft *Raft) candidate() int {
}
}
func
(
rft
*
Raft
)
leader
()
int
{
func
(
rft
*
Raft
)
leader
()
int
{
select
{
rft
.
LogL
(
"became leader"
)
case
<-
rft
.
commitCh
:
heartbeat
:=
time
.
NewTimer
(
time
.
Millisecond
*
HEARTBEAT_TIMEOUT
)
rft
.
LogL
(
"hello"
)
heartbeatReq
:=
new
(
AppendRPC
)
heartbeatReq
.
entries
=
[]
*
LogEntryData
{}
heartbeatReq
.
leaderId
=
rft
.
id
for
{
select
{
case
<-
heartbeat
.
C
:
for
k
,
v
:=
range
rafts
{
if
k
!=
rft
.
id
{
rft
.
LogL
(
"sending AppendRPC "
+
strconv
.
Itoa
(
k
))
v
.
eventCh
<-
heartbeatReq
}
}
heartbeat
.
Reset
(
time
.
Millisecond
*
HEARTBEAT_TIMEOUT
)
case
event
:=
<-
rft
.
eventCh
:
switch
event
.
(
type
)
{
case
*
ClientAppend
:
case
*
AppendRPC
:
case
*
VoteRequest
:
}
}
}
}
return
LEADER
}
}
assignment3/src/raft/server.go
View file @
ab7ec028
...
@@ -16,7 +16,7 @@ func getLogger(serverId int, toDebug bool) (l *log.Logger) {
...
@@ -16,7 +16,7 @@ func getLogger(serverId int, toDebug bool) (l *log.Logger) {
l
=
log
.
New
(
ioutil
.
Discard
,
"INFO: "
,
log
.
Ltime
|
log
.
Lshortfile
)
l
=
log
.
New
(
ioutil
.
Discard
,
"INFO: "
,
log
.
Ltime
|
log
.
Lshortfile
)
}
else
{
}
else
{
logf
,
_
:=
os
.
OpenFile
(
strconv
.
Itoa
(
serverId
),
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0666
)
logf
,
_
:=
os
.
OpenFile
(
strconv
.
Itoa
(
serverId
),
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0666
)
l
=
log
.
New
(
logf
,
"INFO: "
,
log
.
Ltime
|
log
.
Lshortfile
)
l
=
log
.
New
(
logf
,
"INFO: "
,
log
.
Ltime
|
log
.
L
microseconds
|
log
.
L
shortfile
)
}
}
l
.
Println
(
"Initialized server."
)
l
.
Println
(
"Initialized server."
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment