Documentation
¶
Index ¶
- type Raftx
- type SimpleRaftx
- func (r *SimpleRaftx) AddNode(address string) error
- func (r *SimpleRaftx) Command(cmd []byte) error
- func (r *SimpleRaftx) GetLeaderID() (string, int64)
- func (r *SimpleRaftx) GetLocalMemMultiValue(key []byte) (value [][]byte)
- func (r *SimpleRaftx) GetLocalMemMultiValueList(keys [][]byte) (result [][2][]byte)
- func (r *SimpleRaftx) GetLocalMemValue(key []byte) (value []byte)
- func (r *SimpleRaftx) GetLocalMemValueList(keys [][]byte) (result [][2][]byte)
- func (r *SimpleRaftx) GetLocalValue(key []byte) (value []byte, err error)
- func (r *SimpleRaftx) GetLocalValueList(keys [][]byte) (result [][2][]byte, err error)
- func (r *SimpleRaftx) GetMemMultiValue(key []byte) (value [][]byte, err error)
- func (r *SimpleRaftx) GetMemValue(key []byte) (value []byte, err error)
- func (r *SimpleRaftx) GetMemValueList(keys [][]byte) (result [][2][]byte, err error)
- func (r *SimpleRaftx) GetMetrics() *raft.Metrics
- func (r *SimpleRaftx) GetMultiValueList(keys [][]byte) (result [][2][]byte, err error)
- func (r *SimpleRaftx) GetNodeTime() (startTime int64, serviceTime int64)
- func (r *SimpleRaftx) GetNodes() map[string]int64
- func (r *SimpleRaftx) GetState() raft.STATE
- func (r *SimpleRaftx) GetTerm() int64
- func (r *SimpleRaftx) GetValue(key []byte) (value []byte, err error)
- func (r *SimpleRaftx) GetValueList(keys [][]byte) (result [][2][]byte, err error)
- func (r *SimpleRaftx) LastCommitTxId() int64
- func (r *SimpleRaftx) LastTransactionId() int64
- func (r *SimpleRaftx) MemCommand(key, value []byte, ttl uint64, ptype raft.MTYPE) (err error)
- func (r *SimpleRaftx) MemLen() int64
- func (r *SimpleRaftx) MemUnWatch(key []byte)
- func (r *SimpleRaftx) MemUnWatchWithType(key []byte, wt raft.WatchType)
- func (r *SimpleRaftx) MemWatch(key []byte, watchFunc func(key, value []byte, watchType raft.WatchType), ...)
- func (r *SimpleRaftx) Open() error
- func (r *SimpleRaftx) ReSetNodeId(id int64) (prev int64)
- func (r *SimpleRaftx) RemoveNode(address string) bool
- func (r *SimpleRaftx) RestoreSnapshot(data []byte) error
- func (r *SimpleRaftx) Running() bool
- func (r *SimpleRaftx) Stop() error
- func (r *SimpleRaftx) TakeSnapshot(fromTransactionId, toTransactionId int64) ([]byte, error)
- func (r *SimpleRaftx) WaitRun() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Raftx ¶
type Raftx interface {
// LastCommitTxId returns the ID of the last transaction that has been committed to the state machine.
LastCommitTxId() int64
// LastTransactionId returns the ID of the latest transaction, whether it has been committed or not.
LastTransactionId() int64
// GetMetrics retrieves monitoring metrics related to the current state and performance of the Raftx node.
GetMetrics() *raft.Metrics
// Command submits a new proposal for consensus to be applied to the state machine.
Command(cmd []byte) error
// AddNode adds a new member node with the specified address to the Raftx cluster.
AddNode(address string) error
// RemoveNode removes a member node with the specified address from the Raftx cluster.
// Returns true if the removal was successful; otherwise, false.
RemoveNode(address string) bool
// GetNodes returns a map containing information about all nodes in the cluster, including their addresses and terms.
GetNodes() map[string]int64
// TakeSnapshot captures a snapshot of the state machine within a given range of transactions, which can be used to truncate the log.
TakeSnapshot(fromTransactionId, toTransactionId int64) ([]byte, error)
// GetTerm returns the current term of the Raftx node.
GetTerm() int64
// GetLeaderID returns the ID and term of the current leader in the cluster.
GetLeaderID() (string, int64)
// GetState returns the current state of the Raftx node (Follower, Candidate, Leader).
GetState() raft.STATE
// ReSetNodeId Reset the node ID number and return the previous ID number
ReSetNodeId(id int64) int64
// RestoreSnapshot restores the state machine from a provided snapshot, allowing recovery without replaying all logs.
RestoreSnapshot(data []byte) error
// Stop gracefully halts the Raftx node service, ensuring all operations are completed before shutdown.
Stop() error
// Open starts the Raftx node service, initializing any necessary resources and configurations.
Open() error
// GetNodeTime returns two int64 values representing the node's start time and the current service time.
// The start time is the Unix timestamp indicating when the node was started.
// The service time is the duration in seconds that the node has been running since its start time.
//
// Returns:
// - startTime: An int64 value representing the Unix timestamp of when the node was started.
// - serviceTime: An int64 value representing the number of seconds the node has been running.
GetNodeTime() (startTime int64, serviceTime int64)
// GetValue retrieves a value from the state machine using the provided key.
// If the key does not exist, an empty byte slice and an error are returned.
GetValue(key []byte) (value []byte, err error)
// GetLocalValue retrieves a value locally from the state machine using the provided key.
// This bypasses consensus and is intended for read-only operations.
GetLocalValue(key []byte) (value []byte, err error)
// GetValueList retrieves a list of values corresponding to the provided keys from the state machine.
// If a key does not exist, it will not appear in the result.
GetValueList(key [][]byte) (result [][2][]byte, err error)
// GetLocalValueList retrieves a list of values corresponding to the provided keys locally from the state machine.
// This bypasses consensus and is intended for read-only operations.
GetLocalValueList(key [][]byte) (result [][2][]byte, err error)
// GetMemValue retrieves a value from the volatile memory storage using the provided key.
// This method accesses data that may not yet have been committed to the state machine.
GetMemValue(key []byte) (value []byte, err error)
// GetLocalMemValue retrieves a value locally from the volatile memory storage using the provided key.
// This bypasses consensus and is intended for read-only operations on volatile data.
GetLocalMemValue(key []byte) (value []byte)
// GetMemValueList retrieves a list of values corresponding to the provided keys from the volatile memory storage.
// This method accesses data that may not yet have been committed to the state machine.
GetMemValueList(key [][]byte) (result [][2][]byte, err error)
// GetMemMultiValue retrieves a value from the volatile memory storage using the provided key.
// This method accesses data that may not yet have been committed to the state machine.
GetMemMultiValue(key []byte) (value [][]byte, err error)
// GetMultiValueList retrieves a list of values corresponding to the provided keys from the volatile memory storage.
// This method accesses data that may not yet have been committed to the state machine.
GetMultiValueList(keys [][]byte) (result [][2][]byte, err error)
// GetLocalMemValueList retrieves a list of values corresponding to the provided keys locally from the volatile memory storage.
// This bypasses consensus and is intended for read-only operations on volatile data.
GetLocalMemValueList(key [][]byte) (result [][2][]byte)
// GetLocalMemMultiValue retrieves a list of values corresponding to the provided keys from the volatile memory storage.
GetLocalMemMultiValue(key []byte) (value [][]byte)
//GetLocalMemMultiValueList retrieves a list of values corresponding to the provided keys from the volatile memory storage.
GetLocalMemMultiValueList(keys [][]byte) (result [][2][]byte)
// MemCommand applies a command to the volatile memory storage.
// This method does not go through the consensus process but directly modifies the in-memory state.
MemCommand(key, value []byte, ttl uint64, ptype raft.MTYPE) (err error)
// MemLen returns the number of active MemBean items currently stored in fsm
MemLen() int64
// MemWatch listens for changes to the volatile data associated with a specified key.
//
// Parameters:
// - key: The key to watch, provided as a byte slice.
// - watchFunc: A callback function that gets invoked when changes are detected.
// - key: The changed key, passed as a byte slice to the callback function.
// - value: The latest value of the key, passed as a byte slice to the callback function.
// - watchType: Indicates the type of change (created, deleted, modified).
// - isFnSync: Specifies whether the callback function should be executed synchronously.
// If true, the callback is executed immediately upon an event; if false, it may execute asynchronously.
// - watchTypes: An optional variadic parameter list specifying particular types of changes to watch (e.g., only creation and deletion).
// If not provided, defaults to watching all types of changes.
//
// 注意:当使用 MemWatch 时,请确保在不再需要监听时调用相应的取消监听方法 (如 MemUnWatch 或 MemUnWatchWithType) 以避免潜在的内存泄漏或其他性能问题。
MemWatch(key []byte, watchFunc func(key, value []byte, watchType raft.WatchType), isFnSync bool, watchTypes ...raft.WatchType)
// MemUnWatch removes all listeners for the specified key.
//
// Parameters:
// - key: The key to stop watching, provided as a byte slice.
//
// 注意:调用此方法后,对于该键的所有变化将不再触发任何回调函数。
MemUnWatch(key []byte)
// MemUnWatchWithType removes listeners for specific types of changes on the specified key.
//
// Parameters:
// - key: The key to stop watching, provided as a byte slice.
// - wt: The specific type of change to stop listening for.
//
// 注意:与 MemUnWatch 不同,此方法仅取消对指定类型变化的监听。如果要取消对所有类型变化的监听,
// 应使用 MemUnWatch 或多次调用本方法针对每种变化类型。
MemUnWatchWithType(key []byte, wt raft.WatchType)
//Running returns whether the raftx service is running properly
Running() bool
//WaitRun wait for the raftx service until it is ready to run
WaitRun() error
}
Raftx is an interface that defines the behavior of a node participating in a distributed consensus algorithm based on the Raftx protocol. It encapsulates operations for managing cluster membership, log replication, leader election, snapshotting, and state transitions.
type SimpleRaftx ¶
type SimpleRaftx struct {
// contains filtered or unexported fields
}
SimpleRaftx implements the Raftx interface and serves as a concrete implementation of a Raftx node. It manages the core logic for consensus, including log replication, leader election, and snapshot management.
func (*SimpleRaftx) AddNode ¶
func (r *SimpleRaftx) AddNode(address string) error
AddNode adds a new member node with the specified address to the Raftx cluster.
func (*SimpleRaftx) Command ¶
func (r *SimpleRaftx) Command(cmd []byte) error
Command submits a new proposal for consensus to be applied to the state machine. It first checks the rate limiter before submitting the command.
func (*SimpleRaftx) GetLeaderID ¶
func (r *SimpleRaftx) GetLeaderID() (string, int64)
GetLeaderID returns the ID and term of the current leader in the cluster.
func (*SimpleRaftx) GetLocalMemMultiValue ¶
func (r *SimpleRaftx) GetLocalMemMultiValue(key []byte) (value [][]byte)
GetLocalMemMultiValue retrieves a list of values corresponding to the provided keys from the volatile memory storage.
func (*SimpleRaftx) GetLocalMemMultiValueList ¶
func (r *SimpleRaftx) GetLocalMemMultiValueList(keys [][]byte) (result [][2][]byte)
GetLocalMemMultiValueList retrieves a list of values corresponding to the provided keys from the volatile memory storage.
func (*SimpleRaftx) GetLocalMemValue ¶
func (r *SimpleRaftx) GetLocalMemValue(key []byte) (value []byte)
GetLocalMemValue retrieves a value locally from the volatile memory storage using the provided key. This bypasses consensus and is intended for read-only operations on volatile data.
func (*SimpleRaftx) GetLocalMemValueList ¶
func (r *SimpleRaftx) GetLocalMemValueList(keys [][]byte) (result [][2][]byte)
GetLocalMemValueList retrieves a list of values corresponding to the provided keys locally from the volatile memory storage. This bypasses consensus and is intended for read-only operations on volatile data.
func (*SimpleRaftx) GetLocalValue ¶
func (r *SimpleRaftx) GetLocalValue(key []byte) (value []byte, err error)
GetLocalValue retrieves a value locally from the state machine using the provided key. This bypasses consensus and is intended for read-only operations.
func (*SimpleRaftx) GetLocalValueList ¶
func (r *SimpleRaftx) GetLocalValueList(keys [][]byte) (result [][2][]byte, err error)
GetLocalValueList retrieves a list of values corresponding to the provided keys locally from the state machine. This bypasses consensus and is intended for read-only operations.
func (*SimpleRaftx) GetMemMultiValue ¶
func (r *SimpleRaftx) GetMemMultiValue(key []byte) (value [][]byte, err error)
GetMemMultiValue retrieves a value from the volatile memory storage using the provided key. This method accesses data that may not yet have been committed to the state machine.
func (*SimpleRaftx) GetMemValue ¶
func (r *SimpleRaftx) GetMemValue(key []byte) (value []byte, err error)
GetMemValue retrieves a value from the volatile memory storage using the provided key. This method accesses data that may not yet have been committed to the state machine.
func (*SimpleRaftx) GetMemValueList ¶
func (r *SimpleRaftx) GetMemValueList(keys [][]byte) (result [][2][]byte, err error)
GetMemValueList retrieves a list of values corresponding to the provided keys from the volatile memory storage. This method accesses data that may not yet have been committed to the state machine.
func (*SimpleRaftx) GetMetrics ¶
func (r *SimpleRaftx) GetMetrics() *raft.Metrics
GetMetrics retrieves monitoring metrics related to the current state and performance of the Raftx node. Note: This method currently returns nil as no specific metrics are implemented.
func (*SimpleRaftx) GetMultiValueList ¶
func (r *SimpleRaftx) GetMultiValueList(keys [][]byte) (result [][2][]byte, err error)
GetMultiValueList retrieves a list of values corresponding to the provided keys from the volatile memory storage. This method accesses data that may not yet have been committed to the state machine.
func (*SimpleRaftx) GetNodeTime ¶
func (r *SimpleRaftx) GetNodeTime() (startTime int64, serviceTime int64)
GetNodeTime returns two int64 values representing the node's start time and the current service time
func (*SimpleRaftx) GetNodes ¶
func (r *SimpleRaftx) GetNodes() map[string]int64
GetNodes returns a map containing information about all nodes in the cluster, including their addresses and terms.
func (*SimpleRaftx) GetState ¶
func (r *SimpleRaftx) GetState() raft.STATE
GetState returns the current state of the Raftx node (Follower, Candidate, Leader).
func (*SimpleRaftx) GetTerm ¶
func (r *SimpleRaftx) GetTerm() int64
GetTerm returns the current term of the Raftx node.
func (*SimpleRaftx) GetValue ¶
func (r *SimpleRaftx) GetValue(key []byte) (value []byte, err error)
GetValue retrieves a value from the state machine using the provided key. If the key does not exist, an empty byte slice and an error are returned.
func (*SimpleRaftx) GetValueList ¶
func (r *SimpleRaftx) GetValueList(keys [][]byte) (result [][2][]byte, err error)
GetValueList retrieves a list of values corresponding to the provided keys from the state machine. If a key does not exist, it will not appear in the result.
func (*SimpleRaftx) LastCommitTxId ¶
func (r *SimpleRaftx) LastCommitTxId() int64
LastCommitTxId returns the ID of the last transaction that has been committed to the state machine.
func (*SimpleRaftx) LastTransactionId ¶
func (r *SimpleRaftx) LastTransactionId() int64
LastTransactionId returns the ID of the latest transaction, whether it has been committed or not.
func (*SimpleRaftx) MemCommand ¶
MemCommand applies a command to the volatile memory storage. This method does not go through the consensus process but directly modifies the in-memory state.
func (*SimpleRaftx) MemLen ¶
func (r *SimpleRaftx) MemLen() int64
func (*SimpleRaftx) MemUnWatch ¶
func (r *SimpleRaftx) MemUnWatch(key []byte)
func (*SimpleRaftx) MemUnWatchWithType ¶
func (r *SimpleRaftx) MemUnWatchWithType(key []byte, wt raft.WatchType)
func (*SimpleRaftx) Open ¶
func (r *SimpleRaftx) Open() error
Open starts the Raftx node service, initializing any necessary resources and configurations.
func (*SimpleRaftx) ReSetNodeId ¶
func (r *SimpleRaftx) ReSetNodeId(id int64) (prev int64)
ReSetNodeId Reset the node Id number and return the previous ID number
func (*SimpleRaftx) RemoveNode ¶
func (r *SimpleRaftx) RemoveNode(address string) bool
RemoveNode removes a member node with the specified address from the Raftx cluster. Returns true if the removal was successful; otherwise, false.
func (*SimpleRaftx) RestoreSnapshot ¶
func (r *SimpleRaftx) RestoreSnapshot(data []byte) error
RestoreSnapshot restores the state machine from a provided snapshot, allowing recovery without replaying all logs.
func (*SimpleRaftx) Running ¶
func (r *SimpleRaftx) Running() bool
func (*SimpleRaftx) Stop ¶
func (r *SimpleRaftx) Stop() error
Stop gracefully halts the Raftx node service, ensuring all operations are completed before shutdown.
func (*SimpleRaftx) TakeSnapshot ¶
func (r *SimpleRaftx) TakeSnapshot(fromTransactionId, toTransactionId int64) ([]byte, error)
TakeSnapshot captures a snapshot of the state machine within a given range of transactions, which can be used to truncate the log.
func (*SimpleRaftx) WaitRun ¶
func (r *SimpleRaftx) WaitRun() error