raft

package
v0.0.0-...-dde4a86 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2022 License: MPL-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package raft stores function used to interface in and out of raft.

Index

Constants

View Source
const (
	PersistentConfigurationFileName = "persistentConfigFile"
	OriginalConfigurationFileName   = "originalConfigFile"
)

Different constants

View Source
const (
	ElectionTimeout     time.Duration = 3000 * time.Millisecond
	Heartbeat                         = 1000 * time.Millisecond
	RequestVoteTimeout                = 5500 * time.Millisecond
	HeartbeatTimeout                  = 3000 * time.Millisecond
	SendEntryTimeout                  = 7500 * time.Millisecond
	EntryAppliedTimeout               = 20000 * time.Millisecond
)

Raft constants

View Source
const (
	PersistentStateFileName string = "persistentStateFile"
	LogDirectory            string = "raft_logs"
)

Constants used by State

View Source
const (
	SnapshotDirectory        = "snapshots"
	CurrentSnapshotDirectory = "currentsnapshot"
	SnapshotMetaFileName     = "snapshotmeta"
	TarFileName              = "snapshot.tar"
)

String constants

View Source
const (
	SnapshortInteval        time.Duration = 1 * time.Minute
	SnapshotLogsize         uint64        = 2 * 1024 * 1024 //2 MegaBytes
	SnapshotChunkSize       int64         = 1024
	MaxInstallSnapshotFails int           = 10
)

Snapshot constants

View Source
const MaxAppendEntries uint64 = 100

MaxAppendEntries that can be send it one append request

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionType

type ActionType uint32

ActionType is the base type of the different action

const (
	TypeWrite ActionType = iota
	TypeCreat
	TypeChmod
	TypeTruncate
	TypeUtimes
	TypeRename
	TypeLink
	TypeSymlink
	TypeUnlink
	TypeMkdir
	TypeRmdir
)

List of the ActionTypes

type Configuration

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

Configuration manages configuration information of a raft server

func (*Configuration) CalculateNewCommitIndex

func (c *Configuration) CalculateNewCommitIndex(
	lastCommitIndex, term uint64, rlog *raftlog.RaftLog,
) uint64

CalculateNewCommitIndex calculates a new commit index in the manner described in the Raft paper

func (*Configuration) ChangeNodeLocation

func (c *Configuration) ChangeNodeLocation(nodeID, IP, Port string)

ChangeNodeLocation changes the IP and Port of a given nodeID

func (*Configuration) GetFutureConfigurationActive

func (c *Configuration) GetFutureConfigurationActive() bool

GetFutureConfigurationActive returns true if the future configuration is active

func (*Configuration) GetMatchIndex

func (c *Configuration) GetMatchIndex(nodeID string) uint64

GetMatchIndex returns the index matched by the node.

func (*Configuration) GetNextIndex

func (c *Configuration) GetNextIndex(nodeID string) uint64

GetNextIndex gets the index of the configuration that he node is in.

func (*Configuration) GetNode

func (c *Configuration) GetNode(nodeID string) (Node, error)

GetNode based on the node ID. If the node is not found, an error is returned

func (*Configuration) GetNodesList

func (c *Configuration) GetNodesList() []Node

GetNodesList returns a list of all the nodes in the cluster including the current nodes information.

func (*Configuration) GetPeersList

func (c *Configuration) GetPeersList() []Node

GetPeersList returns a list of all the nodes that must be queried to decide on state changes or leader election.

func (*Configuration) GetSendingSnapshot

func (c *Configuration) GetSendingSnapshot(nodeID string) bool

GetSendingSnapshot gets the snapshot of the node sending it TODO: Verify this

func (*Configuration) GetTotalPossibleVotes

func (c *Configuration) GetTotalPossibleVotes() int

GetTotalPossibleVotes returns the number of nodes in the current configuration plus the number in the future configuration not also in the current configuration.

func (*Configuration) HasConfiguration

func (c *Configuration) HasConfiguration() bool

HasConfiguration checks if the local node is in the configuration

func (*Configuration) HasMajority

func (c *Configuration) HasMajority(votesRecieved []string) bool

HasMajority checks if the majority of votes have been received given a list of NodeIDs. A majority is needed in both the current and future configurations

func (*Configuration) InConfiguration

func (c *Configuration) InConfiguration(nodeID string) bool

InConfiguration checks is the node ID in the current or future configuration

func (*Configuration) MyConfigurationGood

func (c *Configuration) MyConfigurationGood() bool

MyConfigurationGood checks if the configuration contains the current node and has more than one member.

func (*Configuration) NewFutureConfiguration

func (c *Configuration) NewFutureConfiguration(nodes []Node, lastLogIndex uint64)

NewFutureConfiguration creates a future configuration and sets the next index of those nodes to lastLogIndex + 1

func (*Configuration) ResetNodeIndices

func (c *Configuration) ResetNodeIndices(lastLogIndex uint64)

ResetNodeIndices is used to reset the currentIndex and matchindex of each peer when elected as a leader.

func (*Configuration) SetMatchIndex

func (c *Configuration) SetMatchIndex(nodeID string, x uint64)

SetMatchIndex sets the matched index for the node.

func (*Configuration) SetNextIndex

func (c *Configuration) SetNextIndex(nodeID string, x uint64)

SetNextIndex sets the next index for the node.

func (*Configuration) SetSendingSnapshot

func (c *Configuration) SetSendingSnapshot(nodeID string, x bool)

SetSendingSnapshot of the node

func (*Configuration) UpdateCurrentConfiguration

func (c *Configuration) UpdateCurrentConfiguration(nodes []Node, lastLogIndex uint64)

UpdateCurrentConfiguration updates the current configuration given a set of nodes. If all the nodes are in the future configuration, the future configuration is changed to the current configuration.

func (*Configuration) UpdateFromConfigurationFile

func (c *Configuration) UpdateFromConfigurationFile(
	configurationFilePath string, lastLogIndex uint64,
) error

UpdateFromConfigurationFile updates the configuration based on the provided file path and the last log index. If the configuration cannot be updated, an error is returned

type EntryAppliedInfo

type EntryAppliedInfo struct {
	Index  uint64
	Result *StateMachineResult
}

EntryAppliedInfo stores the information with the state machine results

type NetworkServer

type NetworkServer struct {
	pb.UnimplementedRaftNetworkServiceServer

	State *State
	Wait  sync.WaitGroup

	TLSEnabled    bool
	Encrypted     bool
	TLSSkipVerify bool

	QuitChannelClosed    bool
	Quit                 chan bool
	ElectionTimeoutReset chan bool
	// contains filtered or unexported fields
}

NetworkServer implements the raft protobuf server interface

func NewNetworkServer

func NewNetworkServer(
	nodeDetails Node,
	pfsDirectory, raftInfoDirectory string,
	testConfiguration *StartConfiguration,
	TLSEnabled, TLSSkipVerify, encrypted bool,
) *NetworkServer

NewNetworkServer creates a new instance of the raft server

func StartRaft

func StartRaft(lis *net.Listener, nodeDetails Node, pfsDirectory, raftInfoDirectory string,
	startConfiguration *StartConfiguration) (*NetworkServer, *grpc.Server)

StartRaft server given a listener, node information a directory to store information Only used for testing purposes

func (*NetworkServer) AppendEntries

AppendEntries implementation

func (*NetworkServer) ChangeNodeLocation

func (s *NetworkServer) ChangeNodeLocation(UUID, IP, Port string)

ChangeNodeLocation changes the IP and Port of a given node

func (*NetworkServer) ClientToLeader

ClientToLeaderRequest implementation

func (*NetworkServer) CreateSnapshot

func (s *NetworkServer) CreateSnapshot(lastIncludedIndex uint64) (err error)

CreateSnapshot creates a new snapshot up to the last included index

func (*NetworkServer) Dial

func (s *NetworkServer) Dial(node *Node, timeoutMiliseconds time.Duration) (*grpc.ClientConn, error)

Dial a node

func (*NetworkServer) InstallSnapshot

func (s *NetworkServer) InstallSnapshot(ctx context.Context, req *pb.SnapshotRequest) (*pb.SnapshotResponse, error)

InstallSnapshot performs snapshot installation

func (*NetworkServer) RequestAddLogEntry

func (s *NetworkServer) RequestAddLogEntry(entry *pb.Entry) (*StateMachineResult, error)

RequestAddLogEntry from a client. If the mode is not the leader, it must follow the request to the leader. Only returns once the request has been committed to the State machine

func (*NetworkServer) RequestAddNodeToConfiguration

func (s *NetworkServer) RequestAddNodeToConfiguration(node Node) error

RequestAddNodeToConfiguration adds a node to configuration

func (*NetworkServer) RequestChangeConfiguration

func (s *NetworkServer) RequestChangeConfiguration(nodes []Node) error

RequestChangeConfiguration performs a change in Configuration

func (*NetworkServer) RequestChmodCommand

func (s *NetworkServer) RequestChmodCommand(filePath string, mode uint32) (returnCode returncodes.Code, returnError error)

RequestChmodCommand performs the Chmod command

func (*NetworkServer) RequestCreatCommand

func (s *NetworkServer) RequestCreatCommand(filePath string, mode uint32) (returnCode returncodes.Code, returnError error)

RequestCreatCommand performs the Creat command

func (*NetworkServer) RequestKeyStateUpdate

func (s *NetworkServer) RequestKeyStateUpdate(owner, holder *pb.Node, generation int64) error

RequestKeyStateUpdate requests and update

func (*NetworkServer) RequestLinkCommand

func (s *NetworkServer) RequestLinkCommand(oldPath, newPath string) (returnCode returncodes.Code, returnError error)

RequestLinkCommand performs the link command

func (*NetworkServer) RequestMkdirCommand

func (s *NetworkServer) RequestMkdirCommand(filePath string, mode uint32) (returnCode returncodes.Code, returnError error)

RequestMkdirCommand performs the mkdir command

func (*NetworkServer) RequestNewGeneration

func (s *NetworkServer) RequestNewGeneration(newNode string) (int, []string, error)

RequestNewGeneration retrns a number, a list of peer nodes, and an error.

func (*NetworkServer) RequestOwnerComplete

func (s *NetworkServer) RequestOwnerComplete(nodeID string, generation int64) error

RequestOwnerComplete of the node

func (*NetworkServer) RequestRenameCommand

func (s *NetworkServer) RequestRenameCommand(oldPath, newPath string) (returnCode returncodes.Code, returnError error)

RequestRenameCommand performs the rename command

func (*NetworkServer) RequestRmdirCommand

func (s *NetworkServer) RequestRmdirCommand(filePath string) (returnCode returncodes.Code, returnError error)

RequestRmdirCommand performs the rmdir command

func (*NetworkServer) RequestSymlinkCommand

func (s *NetworkServer) RequestSymlinkCommand(oldPath, newPath string) (returnCode returncodes.Code, returnError error)

RequestSymlinkCommand performs the symlink command

func (*NetworkServer) RequestTruncateCommand

func (s *NetworkServer) RequestTruncateCommand(filePath string, length int64) (returnCode returncodes.Code, returnError error)

RequestTruncateCommand performs the Truncate command

func (*NetworkServer) RequestUnlinkCommand

func (s *NetworkServer) RequestUnlinkCommand(filePath string) (returnCode returncodes.Code, returnError error)

RequestUnlinkCommand performs the unlink command

func (*NetworkServer) RequestUtimesCommand

func (s *NetworkServer) RequestUtimesCommand(filePath string, atime, mtime *time.Time) (returnCode returncodes.Code, returnError error)

RequestUtimesCommand performs the Utimes command

func (*NetworkServer) RequestVote

RequestVote implementation

func (*NetworkServer) RequestWriteCommand

func (s *NetworkServer) RequestWriteCommand(filePath string, offset, length int64, data []byte) (returnCode returncodes.Code, bytesWrote int, returnError error)

RequestWriteCommand performs a write

func (*NetworkServer) RevertToSnapshot

func (s *NetworkServer) RevertToSnapshot(snapshotPath string) error

RevertToSnapshot revets the statemachine to the snapshot state and removes all log entries.

type Node

type Node struct {
	IP         string
	Port       string
	CommonName string
	NodeID     string
}

Node data

func (Node) String

func (n Node) String() string

type NodeType

type NodeType int

NodeType provides information about the type of node

const (
	FOLLOWER NodeType = iota
	CANDIDATE
	LEADER
	INACTIVE
)

Different node types

type SnapShotInfo

type SnapShotInfo struct {
	LastIncludedIndex uint64 `json:"lastincludedindex"`
	LastIncludedTerm  uint64 `json:"lastincludedterm"`
	SelfCreated       bool   `json:"selfcreated"`
}

SnapShotInfo contains the information about the snapshot

type StartConfiguration

type StartConfiguration struct {
	Peers []Node
}

StartConfiguration is used to start a raft node with a specific congiuration for testing purposes or if the node is the first node to join a cluster

type State

type State struct {
	NodeID string

	Log *raftlog.RaftLog

	Configuration *Configuration

	StartElection     chan bool
	StartLeading      chan bool
	StopLeading       chan bool
	SendAppendEntries chan bool
	ApplyEntries      chan bool
	LeaderElected     chan bool

	SendSnapshot          chan Node
	NewSnapshotCreated    chan bool
	SnapshotCounterAtZero chan bool

	EntryApplied         chan *EntryAppliedInfo
	ConfigurationApplied chan *pb.Configuration

	ApplyEntryLock sync.Mutex
	// contains filtered or unexported fields
}

State information

func (*State) ApplyLogEntries

func (s *State) ApplyLogEntries()

ApplyLogEntries applys all log entries that have been committed but not yet applied

func (*State) DecrementSnapshotCounter

func (s *State) DecrementSnapshotCounter()

DecrementSnapshotCounter reduces the snapshot counter. If the snapshotCounter reaches 0, SnapshotCounterAtZero is notified

func (*State) GetCommitIndex

func (s *State) GetCommitIndex() uint64

GetCommitIndex returns the current commit index

func (*State) GetCurrentState

func (s *State) GetCurrentState() NodeType

GetCurrentState of the State

func (*State) GetCurrentTerm

func (s *State) GetCurrentTerm() uint64

GetCurrentTerm returns the current term

func (*State) GetLastApplied

func (s *State) GetLastApplied() uint64

GetLastApplied returns the last applied

func (*State) GetLeaderID

func (s *State) GetLeaderID() string

GetLeaderID returns the ID of the leader

func (*State) GetPerformingSnapshot

func (s *State) GetPerformingSnapshot() bool

GetPerformingSnapshot from the State

func (*State) GetSnapshotCounterValue

func (s *State) GetSnapshotCounterValue() int

GetSnapshotCounterValue returns the current snapshot counter

func (*State) GetSpecialNumber

func (s *State) GetSpecialNumber() uint64

GetSpecialNumber from the raft state

func (*State) GetVotedFor

func (s *State) GetVotedFor() string

GetVotedFor returns the voted for

func (*State) GetWaitingForApplied

func (s *State) GetWaitingForApplied() bool

GetWaitingForApplied sets the value

func (*State) IncrementSnapshotCounter

func (s *State) IncrementSnapshotCounter()

IncrementSnapshotCounter updates the counter

func (*State) SetCommitIndex

func (s *State) SetCommitIndex(x uint64)

SetCommitIndex sets the current commit index to a given value

func (*State) SetCurrentState

func (s *State) SetCurrentState(x NodeType)

SetCurrentState of the State

func (*State) SetCurrentTerm

func (s *State) SetCurrentTerm(x uint64)

SetCurrentTerm sets the current term

func (*State) SetLastApplied

func (s *State) SetLastApplied(x uint64)

SetLastApplied sets the last applied and saves the state

func (*State) SetLeaderID

func (s *State) SetLeaderID(x string)

SetLeaderID sets the ID of the leader

func (*State) SetPerformingSnapshot

func (s *State) SetPerformingSnapshot(x bool)

SetPerformingSnapshot of the State

func (*State) SetSpecialNumber

func (s *State) SetSpecialNumber(x uint64)

SetSpecialNumber sets the number and saves the state

func (*State) SetVotedFor

func (s *State) SetVotedFor(x string)

SetVotedFor sets the voted for

func (*State) SetWaitingForApplied

func (s *State) SetWaitingForApplied(x bool)

SetWaitingForApplied set the value

type StateMachineResult

type StateMachineResult struct {
	Code         returncodes.Code
	Err          error
	BytesWritten int
	KSMResult    *ksmResult
}

StateMachineResult containing the data of the state machine

func PerformKSMCommand

func PerformKSMCommand(sateMachine *keyman.KeyStateMachine, keyCommand *pb.KeyStateCommand) *StateMachineResult

PerformKSMCommand updates the keys

func PerformLibPfsCommand

func PerformLibPfsCommand(directory string, command *pb.StateMachineCommand) *StateMachineResult

PerformLibPfsCommand performs a libpfs command

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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