raft

package
v0.0.0-...-4b4fa20 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTransactionService

func NewTransactionService(nodeID string, dataStore *datastore.DataStore, consensusManager *ConsensusManager) protoc.TransactionServiceServer

NewTransactionService initializes a new TransactionService

func StartServer

func StartServer(nodeID string, port string, dataStore *datastore.DataStore, consensusManager *ConsensusManager, useTLS bool, certDir string)

StartServer starts the gRPC server

Types

type CheckpointData

type CheckpointData struct {
	MerkleRoot string            `json:"merkle_root"`
	KeyValues  map[string]string `json:"key_values"`
	Timestamp  time.Time         `json:"timestamp"`
}

CheckpointData represents the data stored in a checkpoint

type CheckpointManager

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

CheckpointManager manages checkpointing for fault tolerance

func NewCheckpointManager

func NewCheckpointManager(dataStore *datastore.DataStore, checkpointDir string, interval time.Duration) *CheckpointManager

NewCheckpointManager creates a new CheckpointManager

func (*CheckpointManager) CreateCheckpoint

func (cm *CheckpointManager) CreateCheckpoint(checkpointID int64) error

CreateCheckpoint creates a checkpoint with the given ID

func (*CheckpointManager) GetLastCheckpointTime

func (cm *CheckpointManager) GetLastCheckpointTime() time.Time

GetLastCheckpointTime returns the time of the last checkpoint

func (*CheckpointManager) ListCheckpoints

func (cm *CheckpointManager) ListCheckpoints() ([]int64, error)

ListCheckpoints lists all available checkpoints

func (*CheckpointManager) RestoreFromCheckpoint

func (cm *CheckpointManager) RestoreFromCheckpoint(checkpointID int64) error

RestoreFromCheckpoint restores from a checkpoint with the given ID

func (*CheckpointManager) Start

func (cm *CheckpointManager) Start()

Start starts the checkpointing process

func (*CheckpointManager) Stop

func (cm *CheckpointManager) Stop()

Stop stops the checkpointing process

type ConsensusManager

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

ConsensusManager manages the Raft consensus algorithm

func NewConsensusManager

func NewConsensusManager(config RaftConfig, dataStore *datastore.DataStore, networkManager interfaces.NetworkManagerI) *ConsensusManager

NewConsensusManager creates a new ConsensusManager

func (*ConsensusManager) GetLeader

func (cm *ConsensusManager) GetLeader() string

GetLeader returns the ID of the current leader

func (*ConsensusManager) IsLeader

func (cm *ConsensusManager) IsLeader() bool

IsLeader returns true if this node is the leader

func (*ConsensusManager) ProposeTransaction

func (cm *ConsensusManager) ProposeTransaction(key, value string) (bool, error)

ProposeTransaction proposes a transaction to the Raft cluster

func (*ConsensusManager) Start

func (cm *ConsensusManager) Start()

Start starts the ConsensusManager

func (*ConsensusManager) Stop

func (cm *ConsensusManager) Stop()

Stop stops the ConsensusManager

type Node

type Node struct {
	UseTLS bool
	DBPath string
	Port   string
	// contains filtered or unexported fields
}

func NewNode

func NewNode(NodeID string, PeerAddresses map[string]string, ElectionTimeoutMin int, ElectionTimeoutMax int, HeartbeatInterval int, dataStore *datastore.DataStore, UseTLS bool, dbPath string, port string) *Node

func (*Node) Start

func (n *Node) Start()

func (*Node) Stop

func (n *Node) Stop()

type RaftConfig

type RaftConfig struct {
	NodeID             string            // Unique identifier for this node
	PeerAddresses      map[string]string // Map of node IDs to network addresses
	ElectionTimeoutMin int               // Minimum election timeout in milliseconds
	ElectionTimeoutMax int               // Maximum election timeout in milliseconds
	HeartbeatInterval  int               // Heartbeat interval in milliseconds
}

RaftConfig contains configuration parameters for the Raft consensus algorithm

type RaftNode

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

RaftNode represents a node in the Raft consensus algorithm

func NewRaftNode

func NewRaftNode(config RaftConfig, dataStore *datastore.DataStore, transactionHandler func(*protoc.Transaction) error) *RaftNode

NewRaftNode creates a new Raft node

func (*RaftNode) GetLeader

func (rn *RaftNode) GetLeader() string

GetLeader returns the ID of the current leader

func (*RaftNode) GetState

func (rn *RaftNode) GetState() (int, bool)

GetState returns the current state of the Raft node

func (*RaftNode) HandleAppendEntries

func (rn *RaftNode) HandleAppendEntries(leaderID string, term, prevLogIndex, prevLogTerm int, entries []*protoc.LogEntry, leaderCommit int) (int, bool)

HandleAppendEntries handles an AppendEntries RPC from a peer

func (*RaftNode) HanldeRequestVote

func (rn *RaftNode) HanldeRequestVote(candidateID string, term, lastLogIndex, lastLogTerm int) (int, bool)

HandleRequestVote handles a RequestVote RPC from a peer

func (*RaftNode) IsLeader

func (rn *RaftNode) IsLeader() bool

IsLeader returns true if this node is the leader

func (*RaftNode) ProposeCommand

func (rn *RaftNode) ProposeCommand(command *protoc.Transaction) (bool, error)

ProposeCommand proposes a new command to the Raft cluster

func (*RaftNode) RequestVote

func (rn *RaftNode) RequestVote(peerID string, client protoc.TransactionServiceClient, term, lastLogIndex, lastLogTerm int) (bool, error)

requestVote sends a RequestVote RPC to a peer

func (*RaftNode) Start

func (rn *RaftNode) Start()

Start starts the Raft node

func (*RaftNode) Stop

func (rn *RaftNode) Stop()

Stop stops the Raft node

type RaftState

type RaftState int

RaftState represents the state of a node in the Raft consensus algorithm

const (
	// Follower state - receives log entries from leader
	Follower RaftState = iota
	// Candidate state - requests votes from other nodes
	Candidate
	// Leader state - coordinates log replication
	Leader
)

type SyncManager

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

SyncManager manages state synchronization between nodes

func NewSyncManager

func NewSyncManager(dataStore *datastore.DataStore, networkManager interfaces.NetworkManagerI) *SyncManager

NewSyncManager creates a new SyncManager

func (*SyncManager) Start

func (sm *SyncManager) Start()

Start starts the synchronization process

func (*SyncManager) Stop

func (sm *SyncManager) Stop()

Stop stops the synchronization process

func (*SyncManager) SyncWithPeers

func (sm *SyncManager) SyncWithPeers()

SyncWithPeers synchronizes state with all peers

type SyncStats

type SyncStats struct {
	TransactionsReceived int
	ConflictsResolved    int
	Errors               int
}

SyncStats tracks statistics for a sync operation

type TransactionService

type TransactionService struct {
	protoc.UnimplementedTransactionServiceServer
	// contains filtered or unexported fields
}

TransactionService defines the gRPC service for handling transactions

func (*TransactionService) AppendEntries

func (*TransactionService) Auth

func (*TransactionService) Get

func (*TransactionService) HandleTransaction

HandleTransaction handles a transaction request

func (*TransactionService) RequestVote

func (*TransactionService) SyncState

func (s *TransactionService) SyncState(ctx context.Context, req *protoc.SyncRequest) (sr *protoc.SyncResponse, e error)

func (*TransactionService) VerifyState

Jump to

Keyboard shortcuts

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