Commit 92709071 authored by Harshit Pande's avatar Harshit Pande

raft constructor

parents 10833484 79fb8fc8
......@@ -3,8 +3,8 @@ package main
import (
//"log"
"fmt"
"net"
"net/rpc"
//"net"
//"net/rpc"
"os"
"reflect"
"strconv"
......@@ -48,15 +48,15 @@ type SharedLog interface {
}
type Raft struct {
log_array []LogEntryData
commitCh chan *LogEntryData
log_array []*LogEntryData
commitCh chan LogEntry
cluster_config *ClusterConfig //cluster
id uint64 //this server
id int //this server id
}
var cluster_config *ClusterConfig
func NewRaft(config *ClusterConfig, thisServerId int, commitCh chan *LogEntry) (*Raft, error) {
func NewRaft(config *ClusterConfig, thisServerId int, commitCh chan LogEntry) (*Raft, error) {
rft := new(Raft)
rft.commitCh = commitCh
rft.cluster_config = config
......@@ -65,12 +65,11 @@ func NewRaft(config *ClusterConfig, thisServerId int, commitCh chan *LogEntry) (
}
//goroutine that monitors channel for commiting log entry
func monitor_commitCh(c <-chan *LogEntryData) { //unidirectional -- can only read from the channel
func monitor_commitCh(c <-chan LogEntry) { //unidirectional -- can only read from the channel
for {
var temp *LogEntryData
temp = <-c //receive from the channel
temp.committed = true
//var temp LogEntry
temp := <-c //receive from the channel
temp.(*LogEntryData).committed = true
//now update key value store here
}
}
......@@ -90,15 +89,20 @@ func (entry *LogEntryData) Committed() bool {
//make raft implement the append function
func (raft *Raft) Append(data []byte) (LogEntry, error) {
if raft.server.Id != 0 {
return nil, raft.Id
if raft.id != 0 {
return nil, ErrRedirect(0)
}
temp := LogEntry{1, data, false}
temp := new(LogEntryData)
temp.id = 1
temp.committed = false
temp.data = data
raft.log_array = append(raft.log_array, temp)
//broadcast to other servers
//wait for acks
//send commit on channel
raft.commitCh <- temp
return temp, nil
}
type RPChandle struct {
......@@ -132,11 +136,11 @@ func NewClusterConfig(num_servers int) (*ClusterConfig, error) {
}
func (e ErrRedirect) Error() string {
return "Redirect to server " + strconv.Itoa(cluster_config.Servers[0].Id)
return "Redirect to server " + strconv.Itoa(0)
}
func start_rpc(this_server *ServerConfig) {
rpc.Register()
//rpc.Register()
}
func main() {
......
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