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
f18eef0f
Commit
f18eef0f
authored
Apr 16, 2015
by
Sushant Mahajan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
leader election working
parent
cbd36253
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
142 additions
and
181 deletions
+142
-181
assignment4/src/clean.sh
assignment4/src/clean.sh
+1
-1
assignment4/src/raft/raft.go
assignment4/src/raft/raft.go
+112
-116
assignment4/src/raft/server.go
assignment4/src/raft/server.go
+0
-38
assignment4/src/server.go
assignment4/src/server.go
+20
-18
assignment4/src/server_test.go
assignment4/src/server_test.go
+9
-8
No files found.
assignment4/src/clean.sh
View file @
f18eef0f
#! /bin/bash
rm
{
1..5
}
currentTerm
*
votedFor
*
log
*
rm
{
0..4
}
currentTerm
*
votedFor
*
log
*
assignment4/src/raft/raft.go
View file @
f18eef0f
This diff is collapsed.
Click to expand it.
assignment4/src/raft/server.go
deleted
100644 → 0
View file @
cbd36253
// server.go
package
raft
import
(
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
)
var
rafts
map
[
int
]
*
Raft
func
getLogger
(
serverId
int
,
toDebug
bool
)
(
l
*
log
.
Logger
)
{
if
!
toDebug
{
l
=
log
.
New
(
ioutil
.
Discard
,
"INFO: "
,
log
.
Ltime
|
log
.
Lshortfile
)
}
else
{
logf
,
_
:=
os
.
OpenFile
(
strconv
.
Itoa
(
serverId
),
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0666
)
l
=
log
.
New
(
logf
,
"INFO: "
,
log
.
Ltime
|
log
.
Lmicroseconds
|
log
.
Lshortfile
)
}
l
.
Println
(
"Initialized server."
)
return
l
}
func
Start
(
serverId
int
,
toDebug
bool
)
{
eventCh
:=
make
(
chan
RaftEvent
)
commitCh
:=
make
(
chan
LogEntry
)
monitorVotesCh
:=
make
(
chan
bool
)
clusterConfig
,
_
:=
NewClusterConfig
(
5
)
rft
,
_
:=
NewRaft
(
clusterConfig
,
serverId
,
commitCh
,
eventCh
,
monitorVotesCh
,
true
)
if
rafts
==
nil
{
rafts
=
make
(
map
[
int
]
*
Raft
)
}
rafts
[
serverId
]
=
rft
fmt
.
Println
(
len
(
rafts
))
rft
.
loop
()
}
assignment4/src/server.go
View file @
f18eef0f
...
...
@@ -11,6 +11,7 @@ import (
"os"
"raft"
"strconv"
"time"
)
// Logger
...
...
@@ -20,10 +21,10 @@ var Info *log.Logger
var
rft
*
raft
.
Raft
//Receiver for RPC
type
AppendEntries
struct
{}
type
RaftRPCService
struct
{}
//Receiver for voting related RPC
type
Voting
struct
{}
//
type Voting struct{}
//receiver for testing RPC
//only for testing purpose
...
...
@@ -72,7 +73,7 @@ type Reply struct {
//arguments: pointer to argument struct (has LogEntryData), pointer to reply struct
//returns: error
//receiver: pointer to AppendEntries
func
(
t
*
AppendEntries
)
AppendRPC
(
args
*
raft
.
AppendRPC
,
reply
*
Reply
)
error
{
func
(
t
*
RaftRPCService
)
AppendRPC
(
args
*
raft
.
AppendRPC
,
reply
*
Reply
)
error
{
Info
.
Println
(
"append RPC invoked"
)
rft
.
AddToEventChannel
(
args
)
reply
.
X
=
1
...
...
@@ -83,7 +84,7 @@ func (t *AppendEntries) AppendRPC(args *raft.AppendRPC, reply *Reply) error {
return nil*/
}
func
(
t
*
AppendEntries
)
AppendReplyRPC
(
args
*
raft
.
AppendReplyRPC
,
reply
*
Reply
)
error
{
func
(
t
*
RaftRPCService
)
AppendReplyRPC
(
args
*
raft
.
AppendReply
,
reply
*
Reply
)
error
{
Info
.
Println
(
"append reply to leader RPC invoked"
)
rft
.
AddToEventChannel
(
args
)
reply
.
X
=
1
...
...
@@ -98,7 +99,7 @@ func (t *AppendEntries) AppendReplyRPC(args *raft.AppendReplyRPC, reply *Reply)
//arguments: pointer to argument struct (has LogEntry), pointer to reply struct
//returns: error
//receiver: pointer to AppendEntries
func
(
t
*
AppendEntries
)
CommitRPC
(
args
*
raft
.
CommitData
,
reply
*
Reply
)
error
{
func
(
t
*
RaftRPCService
)
CommitRPC
(
args
*
raft
.
CommitData
,
reply
*
Reply
)
error
{
Info
.
Println
(
"Commit RPC invoked"
)
rft
.
LogArray
[(
*
args
)
.
Id
]
.
SetCommitted
(
true
)
rft
.
AddToChannel
(
rft
.
LogArray
[(
*
args
)
.
Id
])
...
...
@@ -106,29 +107,30 @@ func (t *AppendEntries) CommitRPC(args *raft.CommitData, reply *Reply) error {
return
nil
}
func
(
t
*
Voting
)
VoteRequestRPC
(
args
*
raft
.
VoteRequest
,
reply
*
Reply
)
{
Info
.
Println
(
"Request Vote RPC received
from server"
,
id
)
func
(
t
*
RaftRPCService
)
VoteRequestRPC
(
args
*
raft
.
VoteRequest
,
reply
*
Reply
)
error
{
Info
.
Println
(
"Request Vote RPC received
"
)
rft
.
AddToEventChannel
(
args
)
reply
.
X
=
1
return
nil
}
func
(
t
*
Voting
)
CastVoteRPC
(
args
*
raft
.
VoteRequestReply
,
reply
*
Reply
)
{
Info
.
Println
(
"
Request Vote RPC received from server"
,
id
)
func
(
t
*
RaftRPCService
)
CastVoteRPC
(
args
*
raft
.
VoteRequestReply
,
reply
*
Reply
)
error
{
Info
.
Println
(
"
Cast Vote RPC received"
)
rft
.
AddToMonitorVotesChannel
(
args
)
reply
.
X
=
1
return
nil
}
//Initialize all the things necessary for start the server for inter raft communication.
//The servers are running on ports 20000+serverId. {
1..5
}
//The servers are running on ports 20000+serverId. {
0..4
}
//arguments: pointer to current server config, pointer to raft object, a bool channel to set to true to let
//the invoker know that the proc ended.
//returns: none
//receiver: none
func
initInterServerCommunication
(
server
*
raft
.
ServerConfig
,
rft
*
raft
.
Raft
,
ch
chan
bool
)
{
appendRpc
:=
new
(
AppendEntries
)
rpc
.
Register
(
appendRpc
)
raftRPC
:=
new
(
RaftRPCService
)
rpc
.
Register
(
raftRPC
)
listener
,
e
:=
net
.
Listen
(
"tcp"
,
":"
+
strconv
.
Itoa
(
server
.
LogPort
))
if
e
!=
nil
{
Info
.
Fatal
(
"listen error:"
,
e
)
...
...
@@ -152,9 +154,9 @@ func initInterServerCommunication(server *raft.ServerConfig, rft *raft.Raft, ch
func
initLogger
(
serverId
int
,
toDebug
bool
)
{
// Logger Initializaion
if
!
toDebug
{
Info
=
log
.
New
(
ioutil
.
Discard
,
"INFO: "
,
log
.
Ldate
|
log
.
L
time
|
log
.
Lshortfile
)
Info
=
log
.
New
(
ioutil
.
Discard
,
"INFO: "
,
log
.
Ldate
|
log
.
L
microseconds
|
log
.
Lshortfile
)
}
else
{
Info
=
log
.
New
(
os
.
Stdout
,
"INFO: "
,
log
.
Ldate
|
log
.
L
time
|
log
.
Lshortfile
)
Info
=
log
.
New
(
os
.
Stdout
,
"INFO: "
,
log
.
Ldate
|
log
.
L
microseconds
|
log
.
Lshortfile
)
}
Info
.
Println
(
"Initialized server."
)
...
...
@@ -184,12 +186,9 @@ func initClientCommunication(server *raft.ServerConfig, rft *raft.Raft, ch chan
//Entry point for application. Starts all major server go routines and then waits for ever
func
main
()
{
sid
,
err
:=
strconv
.
Atoi
(
os
.
Args
[
1
])
sid
,
_
:=
strconv
.
Atoi
(
os
.
Args
[
1
])
ch1
:=
make
(
chan
bool
)
ch2
:=
make
(
chan
bool
)
if
err
!=
nil
{
Info
.
Println
(
"argument "
,
os
.
Args
[
1
],
"is not string"
)
}
if
len
(
os
.
Args
)
>
3
{
initLogger
(
sid
,
true
)
...
...
@@ -214,6 +213,9 @@ func main() {
go
initClientCommunication
(
server
,
rft
,
ch1
)
go
initInterServerCommunication
(
server
,
rft
,
ch2
)
time
.
Sleep
(
100
*
time
.
Millisecond
)
raft
.
StartRaft
(
rft
)
for
<-
ch1
&&
<-
ch2
{
}
...
...
assignment4/src/server_test.go
View file @
f18eef0f
...
...
@@ -3,13 +3,9 @@
package
main
import
(
"bytes"
//"fmt"
"net"
"net/rpc"
"os"
"os/exec"
"raft"
"strconv"
"testing"
"time"
...
...
@@ -27,16 +23,21 @@ type Testpair struct {
//
func
TestAll
(
t
*
testing
.
T
)
{
dummy
:=
make
(
chan
bool
)
//start the servers
for
i
:=
1
;
i
<=
NUM_SERVERS
;
i
++
{
go
startServers
(
i
,
t
)
for
i
:=
0
;
i
<
NUM_SERVERS
;
i
++
{
go
startServers
(
i
,
t
,
dummy
)
}
//wait for some time so that servers are ready
time
.
Sleep
(
4
*
time
.
Second
)
time
.
Sleep
(
1
*
time
.
Second
)
if
<-
dummy
{
}
}
//run servers
func
startServers
(
i
int
,
t
*
testing
.
T
)
{
func
startServers
(
i
int
,
t
*
testing
.
T
,
dummy
chan
bool
)
{
cmd
:=
exec
.
Command
(
"go"
,
"run"
,
"server.go"
,
strconv
.
Itoa
(
i
),
strconv
.
Itoa
(
NUM_SERVERS
),
"x"
)
f
,
err
:=
os
.
OpenFile
(
strconv
.
Itoa
(
i
),
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0666
)
if
err
!=
nil
{
...
...
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