Documentation
¶
Index ¶
- Variables
- func CheckErrorFatal(err error)
- type NodeMetadata
- type RaftNode
- func (node *RaftNode) AppendEntries(ctx context.Context, in *protos.AppendEntriesMessage) (*protos.AppendEntriesResponse, error)
- func (node *RaftNode) ApplyToStateMachine(ctx context.Context, testing bool)
- func (node *RaftNode) ConnectToPeerReplicas(ctx context.Context, rep_addrs []string)
- func (node *RaftNode) Connect_raft_node(ctx context.Context, id int, rep_addrs []string, testing bool)
- func (node *RaftNode) DeleteHandler(w http.ResponseWriter, r *http.Request)
- func (node *RaftNode) GetHandler(w http.ResponseWriter, r *http.Request)
- func (node *RaftNode) GetLock(where string)
- func (node *RaftNode) GetRLock(where string)
- func (node *RaftNode) HeartBeats(ctx context.Context)
- func (node *RaftNode) LeaderSendAE(parent_ctx context.Context, replica_id int32, upper_index int32, ...) (status bool)
- func (node *RaftNode) LeaderSendAEs(msg_type string, msg *protos.AppendEntriesMessage, upper_index int32, ...)
- func (node *RaftNode) ListenForShutdown(master_cancel context.CancelFunc)
- func (node *RaftNode) PersistToStorage()
- func (node *RaftNode) PostHandler(w http.ResponseWriter, r *http.Request)
- func (node *RaftNode) PutHandler(w http.ResponseWriter, r *http.Request)
- func (node *RaftNode) ReadCommand(key string) (string, error)
- func (node *RaftNode) ReleaseLock(where string)
- func (node *RaftNode) ReleaseRLock(where string)
- func (node *RaftNode) RequestVote(ctx context.Context, in *protos.RequestVoteMessage) (*protos.RequestVoteResponse, error)
- func (node *RaftNode) RestoreFromStorage(storage *Storage)
- func (node *RaftNode) RunElectionTimer(parent_ctx context.Context)
- func (node *RaftNode) StaleReadCheck(heartbeat_success chan bool)
- func (node *RaftNode) StartElection(ctx context.Context)
- func (node *RaftNode) StartGRPCServer(ctx context.Context, grpc_address string, listener *net.TCPListener, ...)
- func (node *RaftNode) StartKVStore(ctx context.Context, addr string, num int, testing bool)
- func (node *RaftNode) StartRaftServer(ctx context.Context, addr string, testing bool)
- func (node *RaftNode) TestHandler(w http.ResponseWriter, r *http.Request)
- func (node *RaftNode) ToCandidate(ctx context.Context)
- func (node *RaftNode) ToFollower(ctx context.Context, term int32)
- func (node *RaftNode) ToLeader(ctx context.Context)
- func (node *RaftNode) WriteCommand(operation []string, client string) (bool, error)
- type RaftNodeState
- type Storage
Constants ¶
This section is empty.
Variables ¶
var Blue = "\033[34m"
var Cyan = "\033[36m"
var Gray = "\033[37m"
var Green = "\033[32m"
var Purple = "\033[35m"
var Red = "\033[31m"
var Reset = "\033[0m"
var White = "\033[97m"
var Yellow = "\033[33m"
Functions ¶
func CheckErrorFatal ¶
func CheckErrorFatal(err error)
Types ¶
type NodeMetadata ¶
type NodeMetadata struct { Master_ctx context.Context // A context derived from the master context for graceful shutdown Master_cancel context.CancelFunc // The cancel function for the above master context // contains filtered or unexported fields }
Store metadata related to the key value store and the raft node.
type RaftNode ¶
type RaftNode struct { protos.UnimplementedConsensusServiceServer Meta *NodeMetadata // contains filtered or unexported fields }
Main struct storing different aspects of the replica and it's state Refer to figure 2 of the raft paper
func InitializeNode ¶
Initialize the RaftNode (and NodeMetadata) objects. Also restores persisted raft state, if any.
func Setup_raft_node ¶
This function initializes the node and imports the persistent state information to the node.
func (*RaftNode) AppendEntries ¶
func (node *RaftNode) AppendEntries(ctx context.Context, in *protos.AppendEntriesMessage) (*protos.AppendEntriesResponse, error)
Implements the functionality involved when a replica recieves an AppendEntries RPC, as the Raft paper describes it.
func (*RaftNode) ApplyToStateMachine ¶
Apply committed entries to our key-value store.
func (*RaftNode) ConnectToPeerReplicas ¶
Attempt to connect to the gRPC servers of all other replicas, and obtain the client stubs
func (*RaftNode) Connect_raft_node ¶
func (node *RaftNode) Connect_raft_node(ctx context.Context, id int, rep_addrs []string, testing bool)
It connects the current node to the other nodes. This mechanism includes the initiation of their various services, like the KV store server, the gRPC server and the Raft server.
The `connect_chan` channel is used to signify the end of execution of this function for synchronization and error handling.
func (*RaftNode) DeleteHandler ¶
func (node *RaftNode) DeleteHandler(w http.ResponseWriter, r *http.Request)
Handles DELETE requests
func (*RaftNode) GetHandler ¶
func (node *RaftNode) GetHandler(w http.ResponseWriter, r *http.Request)
Handle GET requests
func (*RaftNode) HeartBeats ¶
HeartBeats is a goroutine that periodically sends heartbeats as long as the replicas thinks it's a leader
func (*RaftNode) LeaderSendAE ¶
func (node *RaftNode) LeaderSendAE(parent_ctx context.Context, replica_id int32, upper_index int32, client_obj protos.ConsensusServiceClient, msg *protos.AppendEntriesMessage) (status bool)
To send AppendEntry to single replica, and retry if needed (called by LeaderSendAEs defined below).
func (*RaftNode) LeaderSendAEs ¶
func (node *RaftNode) LeaderSendAEs(msg_type string, msg *protos.AppendEntriesMessage, upper_index int32, successful_write chan bool)
Called when the replica wants to send AppendEntries to all other replicas.
func (*RaftNode) ListenForShutdown ¶
func (node *RaftNode) ListenForShutdown(master_cancel context.CancelFunc)
Listen for termination signal and call master cancel. Wait for spawned goroutines to exit.
func (*RaftNode) PersistToStorage ¶
func (node *RaftNode) PersistToStorage()
func (*RaftNode) PostHandler ¶
func (node *RaftNode) PostHandler(w http.ResponseWriter, r *http.Request)
Handle POST requests
func (*RaftNode) PutHandler ¶
func (node *RaftNode) PutHandler(w http.ResponseWriter, r *http.Request)
Handle PUT requests
func (*RaftNode) ReadCommand ¶
ReadCommand is called when the client sends the replica a read request. Read operations do not need to be added to the log.
func (*RaftNode) ReleaseLock ¶
func (*RaftNode) ReleaseRLock ¶
func (*RaftNode) RequestVote ¶
func (node *RaftNode) RequestVote(ctx context.Context, in *protos.RequestVoteMessage) (*protos.RequestVoteResponse, error)
Implements the functionality involved when a replica recieves a RequestVote RPC. If the received message's term is greater than the replica's current term, transition to follower (if not already a follower) and update term. If in.Term < node.currentTerm, reject vote. If the candidate's log is not atleast as up-to-date as the replica's, reject vote.
func (*RaftNode) RestoreFromStorage ¶
Restore persisted Raft state from non volatile memory.
func (*RaftNode) RunElectionTimer ¶
RunElectionTimer runs an election and initiates transition to candidate if a heartbeat/appendentries RPC is not received within the timeout duration.
func (*RaftNode) StaleReadCheck ¶
StaleReadCheck sends dummy heartbeats to make sure that a new leader has not been elected.
func (*RaftNode) StartElection ¶
StartElection is called when a node transitions to a candidate
func (*RaftNode) StartGRPCServer ¶
func (node *RaftNode) StartGRPCServer(ctx context.Context, grpc_address string, listener *net.TCPListener, testing bool)
This function starts the gRPC server for the raft node and shuts it down when context is cancelled.
func (*RaftNode) StartKVStore ¶
Start the local key-value store and the HTTP server it listens for requests on.
func (*RaftNode) StartRaftServer ¶
HTTP server to listen for client requests
func (*RaftNode) TestHandler ¶
func (node *RaftNode) TestHandler(w http.ResponseWriter, r *http.Request)
Clients can make a request to the /test endpoint to check if the server is up.
func (*RaftNode) ToCandidate ¶
ToCandidate is called when election timer runs out without heartbeat from leader
func (*RaftNode) ToFollower ¶
Method to transition the replica to Follower state.
func (*RaftNode) WriteCommand ¶
WriteCommand is called by the HTTP handler functions when the client sends the replica a write (POST/PUT/DELETE) request. It parses the command and calls LeaderSendAEs for performing the write. If the write request is successfully persisted across a majority of replicas, it informs ApplyToStateMachine to perform the write operation on the key-value store.
type RaftNodeState ¶
type RaftNodeState int32
const ( Follower RaftNodeState = iota Candidate Leader Down )
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}