raft

package module
v0.0.0-...-0706429 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2014 License: GPL-2.0 Imports: 14 Imported by: 0

README

#Raft

This is a go implementation of Raft consensus algorithm. Raft is a consensus algorithm for managing a log replication on distributed system. The details of Raft can be found in paper In Search of an Understandable Consensus Algorithm

#Installation and Test

// get the sourcecode
go get github.com/sk4x0r/raft
//build the executable for code in folder 'start_server'
go build github.com/sk4x0r/raft/start_server/start_server.go
//execute the test
go test github.com/sk4x0r/raft

#Dependency Library depends upon ZeroMQ 4, which can be installed from github.com/pebbe/zmq4. It also requires library sortutil installed which can be found at github.com/cznic/sortutil.

#Usage New instance of server can be created by method New().

s1:=raft.New(serverId, configFile)

New() method takes two parameter, and returns object of type Server.

Parameter Type Description
serverId int unuque id assigned to each server
configFile string path of the file containing configuration of all the servers

For example of configuration file, see config.json file in source.

For starting newly created server instance, Start() method is used. For instance, instance s1 from previous example can be started using following command

s1.Start()

Running instance of a server can be stopped using Stop() method.

s1.Stop()

Commands can be sent to server over inbox and processed output is received over outbox as shown below:

inbox:=s1.RaftInbox()
outbox:=s1.RaftOutbox()
//if 'cmd' is a command I want to send then
inbox <-c1
//after processing the command by server, result is sent on outbox
resp:= <-outbox
//now 'resp' contains the output after processing command 'cmd'

License

The package is available under GNU General Public License. See the LICENSE file.

References and contribution

  1. Raft paper In Search of an Understandable Consensus Algorithm
  2. Golang Tutorials
  3. The Go Blog
  4. A Go implementation of the Raft distributed consensus protocol
  5. Help of classmates of CS733. The nonexhausting list includes Sagar Sontakke, Pushkar Khadilkar, Amol Bhangdiya, Kallol Dey

Documentation

Index

Constants

View Source
const (
	Get    = "get"
	Put    = "put"
	Delete = "delete"
)
View Source
const (
	Ok       = "ok"
	Error    = "error"
	Redirect = "redirect"
)
View Source
const (
	BROADCAST          = -1
	PATH_TO_CONFIG     = "config.json"
	ELECTION_TIMEOUT   = 300 //in millisecond
	HEARTBEAT_INTERVAL = 50  //in millisecond
)
View Source
const (
	Stopped   = "stopped"
	Follower  = "follower"
	Candidate = "candidate"
	Leader    = "leader"
)

states of server

Variables

This section is empty.

Functions

This section is empty.

Types

type AppendEntriesRequest

type AppendEntriesRequest struct {
	Term         int64     //leader's term
	LeaderId     int       //so follower can redirect clients
	PrevLogIndex int64     //index of log entry immediately preceding new ones
	PrevLogTerm  int64     //term of prevLogIndex entry
	Entries      []LogItem //log items to store
	LeaderCommit int64     //leader’s commitIndex
}

type AppendEntriesResponse

type AppendEntriesResponse struct {
	Term        int64 //currentTerm, for leader to update itself
	Success     bool  //true if follower contained entry matching prevLogIndex and prevLogTerm
	PeerId      int   //id of client
	CommitIndex int64 // commit index of client
}

type Command

type Command struct {
	Cmd   string
	Key   string
	Value string
}

This structure is used by client to send command to raft

type CommitIndexJson

type CommitIndexJson struct {
	CommitIndex int64
}

type Config

type Config struct {
	Timeout int
	Peers   []Peer
}

struct used to marshal/unmarshal json objects of configuration file

type Envelope

type Envelope struct {
	SourceId int
	DestId   int
	MsgId    int64
	Msg      interface{}
}

type Log

type Log struct {
	// contains filtered or unexported fields
}

func (*Log) CommitIndex

func (l *Log) CommitIndex() int64

func (*Log) CurrentIndex

func (l *Log) CurrentIndex() int64

func (*Log) CurrentTerm

func (l *Log) CurrentTerm() int64

type LogItem

type LogItem struct {
	Index int64
	Term  int64
	Data  Command
}

struct to store log entry

type Peer

type Peer struct {
	Pid  int
	Ip   string
	Port int
	// contains filtered or unexported fields
}

type RequestVoteRequest

type RequestVoteRequest struct {
	Term         int64 //candidate's term
	CandidateId  int   //candidate requesting vote
	LastLogIndex int64 //index of candidate's last log entry
	LastLogTerm  int64 //term of candidate's last log entry
}

type RequestVoteResponse

type RequestVoteResponse struct {
	Term        int64 //currentTerm, for candidate to update itself
	VoteGranted bool  //true means candidate received vote
}

type Response

type Response struct {
	Status   string
	LeaderId int
	Value    string
}

this struct is used by raft to send back response to client

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(id int, configFile string) Server

TODO: error handling

func (*Server) Id

func (s *Server) Id() int

func (*Server) Inbox

func (s *Server) Inbox() chan Envelope

func (*Server) Leader

func (s *Server) Leader() bool

func (*Server) LeaderId

func (s *Server) LeaderId() int

func (*Server) Majority

func (s *Server) Majority() int

func (*Server) Outbox

func (s *Server) Outbox() chan Envelope

func (*Server) Peers

func (s *Server) Peers() []int

func (*Server) Port

func (s *Server) Port() int

func (*Server) RaftInbox

func (s *Server) RaftInbox() chan Command

func (*Server) RaftOutbox

func (s *Server) RaftOutbox() chan Response

func (*Server) Start

func (s *Server) Start()

func (*Server) State

func (s *Server) State() string

func (*Server) Stop

func (s *Server) Stop()

depriciated Tries to pause server activity This method doesnt clean up the resources, and has several issues Avoid using this

func (*Server) Term

func (s *Server) Term() int64

type TermJson

type TermJson struct {
	Term int64
}

used to create json object of term for storing on disk

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL