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
d0235343
Commit
d0235343
authored
Feb 08, 2015
by
Sushant Mahajan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
complete append code
parent
567b3b54
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
11 deletions
+33
-11
assignment2/replic_kvstore.go
assignment2/replic_kvstore.go
+33
-11
No files found.
assignment2/replic_kvstore.go
View file @
d0235343
...
...
@@ -13,6 +13,7 @@ import (
//constant values used
const
(
CLIENT_PORT
=
9000
ACK_TIMEOUT
=
5
)
type
Lsn
uint64
//Log sequence number, unique for all time.
...
...
@@ -75,26 +76,44 @@ func NewRaft(config *ClusterConfig, thisServerId int, commitCh chan LogEntry) (*
}
//goroutine that monitors channel for commiting log entry
func
monitor
_commitCh
(
raft
*
Raft
,
c
<-
chan
LogEntry
)
{
//unidirectional -- can only read from the channel
func
monitor
CommitChannel
(
raft
*
Raft
)
{
//unidirectional -- can only read from the channel
for
{
//var temp LogEntry
temp
:=
<-
c
//receive from the channel
temp
:=
<-
raft
.
commitCh
//receive from the channel
raft
.
log_array
[
temp
.
(
*
LogEntryData
)
.
id
]
.
committed
=
true
//commit the value
//update the kv store here
}
}
//goroutine that monitors channel to check if the majority of servers have replied
func
monitor
_ackCh
(
rft
*
Raft
,
ack_ch
<-
chan
int
,
log_entry
LogEntry
)
{
func
monitor
AckChannel
(
rft
*
Raft
,
ack_ch
<-
chan
int
,
log_entry
LogEntry
,
majEventCh
chan
int
)
{
acks_received
:=
0
num_servers
:=
len
(
rft
.
cluster_config
.
Servers
)
required_acks
:=
num_servers
/
2
up
:=
make
(
chan
bool
,
1
)
err
:=
false
go
func
()
{
time
.
Sleep
(
ACK_TIMEOUT
*
time
.
Second
)
up
<-
true
}()
for
{
temp
:=
<-
ack_ch
acks_received
+=
temp
if
acks_received
==
required_acks
{
rft
.
commitCh
<-
log_entry
select
{
case
temp
:=
<-
ack_ch
:
acks_received
+=
temp
if
acks_received
==
required_acks
{
rft
.
commitCh
<-
log_entry
majEventCh
<-
1
err
=
true
break
}
case
<-
up
:
err
=
true
break
}
if
err
{
break
}
}
...
...
@@ -125,7 +144,9 @@ func (raft *Raft) Append(data []byte) (LogEntry, error) {
raft
.
log_array
=
append
(
raft
.
log_array
,
temp
)
ackChan
:=
make
(
chan
int
)
go
monitor_ackCh
(
raft
,
ackChan
,
temp
)
majEventCh
:=
make
(
chan
int
)
go
monitorAckChannel
(
raft
,
ackChan
,
temp
,
majEventCh
)
go
monitorCommitChannel
(
raft
)
for
_
,
server
:=
range
cluster_config
.
Servers
[
1
:
]
{
go
func
(
ackChan
chan
int
)
{
...
...
@@ -140,9 +161,10 @@ func (raft *Raft) Append(data []byte) (LogEntry, error) {
ackChan
<-
reply
.
X
}(
ackChan
)
}
//wait for acks
//send commit on channel
raft
.
commitCh
<-
temp
//channel will return 1 if majority
if
<-
majEventCh
==
1
{
raft
.
commitCh
<-
temp
}
return
temp
,
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