Categorygithub.com/devMYC/raft
modulepackage
0.0.0-20211203212508-2868bd0c74fd
Repository: https://github.com/devmyc/raft.git
Documentation: pkg.go.dev

# README

This implementation is used to learn/understand the Raft consensus algorithm. The code implements the behaviors shown in Figure 2 of the Raft paper with no log persistence.

The servers in the cluster communicate with each other via grpc calls. Make sure protobuf compiler is installed.

Also, we need Go plugins for the protobuf compiler

$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]

Update PATH so protoc can find the plugins

$ export PATH="$PATH:$(go env GOPATH)/bin"

Generate gRPC stubs from .proto definition file using the following command

$ protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    proto/raft.proto

The docker-compose.yml file will create a Raft cluster with 3 nodes.

$ docker-compose build
$ docker-compose up -d

After a leader is elected, we can then send requests to the leader node using client/client.go.

NOTE: the X in a port shown in the commands should be replaced by the node's ID. (i.e. 8081 for node1)

$ go run client.go -server <dockerHostIP:808X> -command <string>

There's also an http server running in each container that can be queried to modify iptables rules.

$ curl -i http://dockerHostIP:909X/block    # isolate a node from the cluster
$ curl -i http://dockerHostIP:909X/unblock  # reconnect a node with the cluster

To stop the containers and clean up everything, run

$ docker-compose down
$ make clean

# Packages

No description provided by the author
No description provided by the author

# Functions

InitState initializes Raft state for a server.
Min returns the smaller integer between two integers.
NewConsensusModule creates a new consensus module with serverID used as seed to prevent livelock of elections.

# Constants

Each server can act as exactly one of the following roles under normal operation.
Each server can act as exactly one of the following roles under normal operation.
Each server can act as exactly one of the following roles under normal operation.

# Structs

ConsensusModule encapsulates the state and other information used to run Raft algorithm.
Server handles RPC requests between nodes in a cluster to update state of consensus module.
State of Raft algorithm shown in Fig 2 of Raft paper.

# Type aliases

Role is the current role of the server.