Documentation ¶
Overview ¶
Package braft 提供了 raft 更加方便的集成 API 胶水代码。
Index ¶
- Variables
- func CheckTCP(ipPort string) bool
- func CreateDiscovery(discoveryMethod string, dport int) discovery.Discovery
- func GetPeerDetails(addr string, timeout time.Duration) (*proto.GetDetailsResponse, error)
- func GetRaftClient(addr string, timeout time.Duration) (ctx context.Context, deferFn func(), client proto.RaftClient, err error)
- func Setup()
- type ClientGrpcServices
- type Config
- type ConfigFn
- func WithBizData(s func() any) ConfigFn
- func WithDataDir(s string) ConfigFn
- func WithDiscovery(s discovery.Discovery) ConfigFn
- func WithDiscoveryPort(port int) ConfigFn
- func WithEnableHTTP(v bool) ConfigFn
- func WithGrpcDialOptions(options ...grpc.DialOption) ConfigFn
- func WithHTTPFns(s ...HTTPConfigFn) ConfigFn
- func WithHostIP(v string) ConfigFn
- func WithHttpPort(port int) ConfigFn
- func WithLeaderChange(s NodeStateChanger) ConfigFn
- func WithRaftPort(port int) ConfigFn
- func WithServerID(serverID string) ConfigFn
- func WithServices(s ...fsm.Service) ConfigFn
- func WithShutdownExitCode(shutdownExit bool, shutdownExitCode int) ConfigFn
- func WithTypeRegister(s *marshal.TypeRegister) ConfigFn
- type DistributeOption
- type DistributeOptionFunc
- type HTTPConfig
- type HTTPConfigFn
- type HandlerFunc
- type Node
- func (n *Node) ApplyOnLeader(payload []byte, timeout time.Duration) (any, error)
- func (n *Node) DiscoveryName() string
- func (n *Node) Distribute(bean fsm.Distributable, fns ...DistributeOptionFunc) (any, error)
- func (n *Node) GetDistribute(key string, result any) (nodeID string, err error)
- func (n *Node) GetRaftNodes(raftServers []raft.Server) (nodes []RaftNode)
- func (n *Node) GetRaftNodesInfo() (nodes []RaftNode)
- func (n *Node) GetRaftServers() []raft.Server
- func (n *Node) IsLeader() bool
- func (n *Node) Leader() (*RaftID, error)
- func (n *Node) NotifyJoin(node *memberlist.Node)
- func (n *Node) NotifyLeave(node *memberlist.Node)
- func (n *Node) NotifyUpdate(node *memberlist.Node)
- func (n *Node) RaftApply(request any, timeout time.Duration) (any, error)
- func (n *Node) RegisterServeKV(r gin.IRoutes, path string)
- func (n *Node) ServeDistribute(ctx *gin.Context)
- func (n *Node) ServeKV(ctx *gin.Context)
- func (n *Node) ServeRaft(ctx *gin.Context)
- func (n *Node) ShortNodeIds() (nodeIds []string)
- func (n *Node) Start() (err error)
- func (n *Node) Stop()
- type NodeStateChanger
- type NotifyEvent
- type NotifyType
- type RaftID
- type RaftNode
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultMdnsService 默认 Mdns 服务名称 DefaultMdnsService string // DefaultK8sNamespace 默认 k8s 命名空间名称 DefaultK8sNamespace string // DefaultK8sPortName 默认 k8s 端口名称 DefaultK8sPortName string // DefaultK8sServiceLabels 默认 i8s 服务标签 DefaultK8sServiceLabels map[string]string // DefaultDiscovery 默认发现策略 DefaultDiscovery string // EnvIP IP 值 EnvIP string // EnvRport Raft 端口值 EnvRport int // EnvDport Discovery 端口值 EnvDport int // EnvHport HTTP 端口值 EnvHport int // DefaultStaticPeers 静态端点列表 DefaultStaticPeers []string )
var ErrNoLeader = errors.New("no leader found")
Functions ¶
func CreateDiscovery ¶
CreateDiscovery creates a new discovery from the given discovery method.
func GetPeerDetails ¶
GetPeerDetails returns the remote peer details.
func GetRaftClient ¶
func GetRaftClient(addr string, timeout time.Duration) (ctx context.Context, deferFn func(), client proto.RaftClient, err error)
GetRaftClient returns the raft client with timeout context.
Types ¶
type ClientGrpcServices ¶
type ClientGrpcServices struct { proto.UnimplementedRaftServer Node *Node }
ClientGrpcServices is the client of grpc services.
func NewClientGrpcService ¶
func NewClientGrpcService(node *Node) *ClientGrpcServices
NewClientGrpcService creates a new ClientGrpcService.
func (*ClientGrpcServices) ApplyLog ¶
func (s *ClientGrpcServices) ApplyLog(_ context.Context, r *proto.ApplyRequest) (*proto.ApplyResponse, error)
ApplyLog responses the request.
func (*ClientGrpcServices) GetDetails ¶
func (s *ClientGrpcServices) GetDetails(_ context.Context, r *proto.GetDetailsRequest) (response *proto.GetDetailsResponse, err error)
GetDetails returns the node details.
type Config ¶
type Config struct { TypeRegister *marshal.TypeRegister DataDir string Discovery discovery.Discovery Services []fsm.Service LeaderChange NodeStateChanger BizData func() any HTTPConfigFns []HTTPConfigFn EnableHTTP bool GrpcDialOptions []grpc.DialOption // Rport Raft 监听端口值 Rport int // Dport Discovery 端口值 Dport int // Hport HTTP 端口值 Hport int // Raft ServiceID ServerID string // ShutdownExit 集群停止时,直接退出,方便 systemctl 重启 ShutdownExit bool // ShutdownExitCode 退出时的编码 ShutdownExitCode int // HostIP 当前主机的IP HostIP string }
Config is the configuration of the node.
type ConfigFn ¶
type ConfigFn func(*Config)
ConfigFn is the function option pattern for the NodeConfig.
func WithBizData ¶
WithBizData specifies the biz data of current node for the node for /raft api .
func WithDiscovery ¶
WithDiscovery specifies the discovery method of raft cluster nodes.
func WithDiscoveryPort ¶
WithDiscoveryPort specifies the discovery port.
func WithEnableHTTP ¶
WithEnableHTTP specifies whether to enable the http service.
func WithGrpcDialOptions ¶
func WithGrpcDialOptions(options ...grpc.DialOption) ConfigFn
WithGrpcDialOptions specifies the grpc options.
func WithHTTPFns ¶
func WithHTTPFns(s ...HTTPConfigFn) ConfigFn
WithHTTPFns specifies the http service.
func WithHostIP ¶
func WithLeaderChange ¶
func WithLeaderChange(s NodeStateChanger) ConfigFn
WithLeaderChange specifies the leader change callback.
func WithServices ¶
WithServices specifies the services for the FSM.
func WithShutdownExitCode ¶
WithShutdownExitCode specifies the program should exit or not when the raft cluster shutdown.
func WithTypeRegister ¶
func WithTypeRegister(s *marshal.TypeRegister) ConfigFn
WithTypeRegister specifies the serializer.TypeRegister of the raft log messages.
type DistributeOption ¶
type DistributeOption struct {
Key string // http /distribute/:key
}
type DistributeOptionFunc ¶
type DistributeOptionFunc func(*DistributeOption)
func WithKey ¶
func WithKey(key string) DistributeOptionFunc
type HTTPConfig ¶
type HTTPConfig struct { Handlers []pathHalder EnableKv bool }
HTTPConfig is configuration for HTTP service.
type HTTPConfigFn ¶
type HTTPConfigFn func(*HTTPConfig)
HTTPConfigFn is function options for HTTPConfig.
func WithEnableKV ¶
func WithEnableKV(b bool) HTTPConfigFn
WithEnableKV enables or disables KV service on HTTP.
func WithHandler ¶
func WithHandler(method, path string, handler HandlerFunc) HTTPConfigFn
WithHandler defines the http handler.
type HandlerFunc ¶
HandlerFunc defines the handler used by gin middleware as return value.
type Node ¶
type Node struct { StartTime time.Time Raft *raft.Raft GrpcServer *grpc.Server Conf *Config TransportManager *transport.Manager ID string RaftID RaftID GrpcListen net.Listener DistributeCache sync.Map // map[string]fsm.Distributable // contains filtered or unexported fields }
Node is the raft cluster node.
func (*Node) ApplyOnLeader ¶
ApplyOnLeader apply a payload on the leader node.
func (*Node) DiscoveryName ¶
DiscoveryName returns the name of discovery.
func (*Node) Distribute ¶
func (n *Node) Distribute(bean fsm.Distributable, fns ...DistributeOptionFunc) (any, error)
Distribute distributes the given bean to all the nodes in the cluster.
func (*Node) GetDistribute ¶
func (*Node) GetRaftNodes ¶
GetRaftNodes return the raft nodes information.
func (*Node) GetRaftNodesInfo ¶
GetRaftNodesInfo return the raft nodes information.
func (*Node) GetRaftServers ¶
GetRaftServers 获得 Raft 节点服务器列表.
func (*Node) NotifyJoin ¶
func (n *Node) NotifyJoin(node *memberlist.Node)
NotifyJoin triggered when a new Node has been joined to the cluster (discovery only) and capable of joining the Node to the raft cluster
func (*Node) NotifyLeave ¶
func (n *Node) NotifyLeave(node *memberlist.Node)
NotifyLeave triggered when a Node becomes unavailable after a period of time it will remove the unavailable Node from the Raft cluster
func (*Node) NotifyUpdate ¶
func (n *Node) NotifyUpdate(node *memberlist.Node)
NotifyUpdate responses the update of raft cluster member.
func (*Node) RaftApply ¶
RaftApply is used to apply any new logs to the raft cluster this method will do automatic forwarding to the Leader Node
func (*Node) RegisterServeKV ¶
RegisterServeKV register kv service for the gin route.
func (*Node) ServeDistribute ¶
func (*Node) ShortNodeIds ¶
ShortNodeIds returns a sorted list of short node IDs in the current raft cluster.
type NodeStateChanger ¶
NodeStateChanger defines the leader change callback func prototype.
type NotifyType ¶
type NotifyType int
NotifyType 定义通知类型
const ( // NotifyJoin 通知加入 Raft 集群 NotifyJoin NotifyType // NotifyLeave 通知离开 Raft 集群 NotifyLeave // NotifyUpdate 通知更新 Raft 集群 NotifyUpdate )
func (NotifyType) String ¶
func (t NotifyType) String() string
type RaftID ¶
type RaftID struct { ID string `json:"id,omitempty"` Hostname string `json:"hostname,omitempty"` IP string `json:"ip,omitempty"` Sqid string `json:"sqid,omitempty"` // {RaftPort, Dport, Hport} ServerID string `json:"serverID,omitempty"` ServerAddr string `json:"serverAddr,omitempty"` }
RaftID is the structure of node ID.
func ParseRaftID ¶
ParseRaftID parses the coded raft ID string a RaftID structure.
func UnmarshRaftID ¶
type RaftNode ¶
type RaftNode struct { ServerID string `json:"serverID"` BuildTime string `json:"buildTime"` Duration string `json:"duration"` Address string `json:"address"` RaftState string `json:"raftState"` Leader string `json:"leader"` AppVersion string `json:"appVersion"` StartTime string `json:"startTime"` Error string `json:"error,omitempty"` GoVersion string `json:"goVersion"` GitCommit string `json:"gitCommit"` DiscoveryNodes []string `json:"discoveryNodes"` Addr []string `json:"addr"` BizData json.RawMessage `json:"bizData,omitempty"` RaftID RaftID `json:"raftID"` RaftLogSum uint64 `json:"raftLogSum"` Pid uint64 `json:"pid"` Rss uint64 `json:"rss"` Pcpu float32 `json:"pcpu"` RaftPort int `json:"raftPort"` DiscoveryPort int `json:"discoveryPort"` HttpPort int `json:"httpPort"` NodeIds []string `json:"nodeIds"` }
RaftNode is a node info of raft cluster.