Documentation ¶
Overview ¶
Package braft 提供了 raft 更加方便的集成 API 胶水代码。
Index ¶
- Variables
- func CreateDiscovery(discoveryMethod string) 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() interface{}) ConfigFn
- func WithDataDir(s string) ConfigFn
- func WithDiscovery(s discovery.Discovery) ConfigFn
- func WithEnableHTTP(v bool) ConfigFn
- func WithHTTPFns(s ...HTTPConfigFn) ConfigFn
- func WithLeaderChange(s NodeStateChanger) ConfigFn
- func WithServices(s ...fsm.Service) ConfigFn
- func WithTypeRegister(s *marshal.TypeRegister) ConfigFn
- type HTTPConfig
- type HTTPConfigFn
- type HandlerFunc
- type Node
- func (n *Node) ApplyOnLeader(payload []byte, timeout time.Duration) (interface{}, error)
- func (n *Node) DiscoveryName() string
- func (n *Node) Distribute(bean fsm.Distributable) (interface{}, 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) NotifyJoin(node *memberlist.Node)
- func (n *Node) NotifyLeave(node *memberlist.Node)
- func (n *Node) NotifyUpdate(node *memberlist.Node)
- func (n *Node) RaftApply(request interface{}, timeout time.Duration) (interface{}, error)
- func (n *Node) RegisterServeKV(r gin.IRoutes, path string)
- 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 NodeState
- 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 )
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() interface{} HTTPConfigFns []HTTPConfigFn EnableHTTP bool }
Config is the configuration of the node.
type ConfigFn ¶
type ConfigFn func(*Config)
ConfigFn is the function option pattern for the NodeConfig.
func WithBizData ¶
func WithBizData(s func() interface{}) ConfigFn
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 WithEnableHTTP ¶
WithEnableHTTP specifies whether to enable the http service.
func WithHTTPFns ¶
func WithHTTPFns(s ...HTTPConfigFn) ConfigFn
WithHTTPFns specifies the http service.
func WithLeaderChange ¶
func WithLeaderChange(s NodeStateChanger) ConfigFn
WithLeaderChange specifies the leader change callback.
func WithServices ¶
WithServices specifies the services for the FSM.
func WithTypeRegister ¶
func WithTypeRegister(s *marshal.TypeRegister) ConfigFn
WithTypeRegister specifies the serializer.TypeRegister of the raft log messages.
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 // 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) (interface{}, error)
Distribute distributes the given bean to all the nodes in the cluster.
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) 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"` Hostname string `json:"hostname"` IP string `json:"ip"` Sqid string `json:"sqid"` // {RaftPort, Dport, Hport} }
RaftID is the structure of node ID.
func ParseRaftID ¶
ParseRaftID parses the coded raft ID string a RaftID structure.
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"` }
RaftNode is a node info of raft cluster.