model

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorBadCommand = errors.New("bad command")
)

Functions

func HBResponse

func HBResponse(resp *HeartBeatResponse, ok bool, msg string)

func VoteResponse

func VoteResponse(resp *RequestVoteResponse, node Node, vote bool, msg string)

Types

type Client added in v1.0.4

type Client interface {
	// InitConnections initializes a set of connections to the given nodes.
	// It returns an error if any connection fails.
	InitConnections(nodes []*Node, config TransportConfig) error

	// SendRequest sends the command request
	SendRequest(nodeId string, request *Request, response *Response) error
}

Client interface defines the fundamental behaviors of a client.

type ClusterState

type ClusterState struct {
	Nodes map[string]*NodeWithState `json:"nodes" mapstructure:"nodes"`
}

ClusterState represents the state of a cluster, including the nodes that make up the cluster.

type CommandCode added in v1.0.4

type CommandCode uint
const (
	HeartBeat CommandCode = iota + 1
	RequestVote
	State
)

func (CommandCode) String added in v1.0.4

func (c CommandCode) String() string

type CommandHandler added in v1.0.4

type CommandHandler func(request *Request, response *Response) error

CommandHandler represents a function that handles command requests and returns responses.

type ElectNode

type ElectNode struct {
	Node `json:"node" mapstructure:"node"`

	NoVote bool `json:"no_vote" mapstructure:"no_vote"`
}

ElectNode represents a node instance with elect meta

type Header struct {
	// Node field, which represents the information of a node
	Node Node `json:"node"`
}

Header is a common structure for both requests and responses.

type HeartBeatRequest

type HeartBeatRequest struct {
	NodeId string `json:"node_id" mapstructure:"node_id"`
	Term   uint64 `json:"term" mapstructure:"term"`
}

HeartBeatRequest is the heartbeat request

type HeartBeatResponse

type HeartBeatResponse struct {
	Ok      bool   `json:"ok,omitempty" mapstructure:"ok"`
	Message string `json:"message,omitempty" mapstructure:"message"`
}

HeartBeatResponse is the heartbeat response

type Node

type Node struct {
	ID      string            `json:"id" mapstructure:"id"`
	Address string            `json:"address" mapstructure:"address"`
	Tags    map[string]string `json:"tags" mapstructure:"tags"`
}

Node represents a node instance

func (*Node) Validate

func (n *Node) Validate() error

type NodeEvent

type NodeEvent string

NodeEvent represents the related events in the entire lifecycle of the node, used to drive the node Finite State Machine (FSM)

const (
	// EventHeartbeatTimeout represents the node heartbeat timeout
	EventHeartbeatTimeout NodeEvent = "heartbeat_timeout"
	// EventLeaveLeader represents the node leaving the leader state
	EventLeaveLeader NodeEvent = "leave_leader"
	// EventNewLeader represents the node discovering a new leader
	EventNewLeader NodeEvent = "new_leader"
	// EventNewTerm represents the node discovering a new term
	EventNewTerm NodeEvent = "new_term"
	// EventMajorityVotes represents the node receiving the majority of votes
	EventMajorityVotes NodeEvent = "majority_votes"
	// EventDown represents the node going offline
	EventDown NodeEvent = "down"
)

func (NodeEvent) String

func (n NodeEvent) String() string

type NodeState

type NodeState string

NodeState represents the state of a node in a distributed system.

const (
	// NodeStateLeader leader state
	NodeStateLeader NodeState = "leader"
	// NodeStateFollower follower state
	NodeStateFollower NodeState = "follower"
	// NodeStateCandidate candidate state
	NodeStateCandidate NodeState = "candidate"
	// NodeStateDown down state
	NodeStateDown NodeState = "down"
)

func (NodeState) String

func (n NodeState) String() string

type NodeWithState added in v1.0.3

type NodeWithState struct {
	State NodeState `json:"state" mapstructure:"state"`
	Node  ElectNode `json:"node" mapstructure:"node"`
}

type Request added in v1.0.4

type Request struct {
	Header
	// CommandCode is the command code.
	CommandCode CommandCode `json:"command_code"`
	// Command is the actual request payload.
	Command any `json:"command"`
}

Request represents a structure for the requests.

type RequestVoteRequest

type RequestVoteRequest struct {
	NodeId   string `json:"node_id" mapstructure:"node_id"`
	Term     uint64 `json:"term" mapstructure:"term"`
	NodeAddr string `json:"node_addr" mapstructure:"node_addr"`
}

RequestVoteRequest is the vote request

type RequestVoteResponse

type RequestVoteResponse struct {
	Node    Node   `json:"node" mapstructure:"node"`
	Vote    bool   `json:"vote" mapstructure:"vote"`
	Message string `json:"message,omitempty" mapstructure:"message"`
}

RequestVoteResponse is the vote response

type Response added in v1.0.4

type Response struct {
	Header
	// CommandResponse holds the actual response data.
	CommandResponse any `json:"command_response"`
	// Error is an error value; if it's nil, the command was successful.
	Error error `json:"error"`
}

Response defines a structure for responses.

type Server added in v1.0.4

type Server interface {
	// Start initiates the server to begin listening on the specified address.
	Start(listenAddress string, handler CommandHandler, config TransportConfig) error
}

Server interface defines the fundamental behaviors of a server.

type StateTransition

type StateTransition struct {
	// State is the destination state of the transition
	State NodeState
	// SrcState is the source state of the transition
	SrcState NodeState
	// Type is the type of the transition
	Type TransitionType
}

StateTransition represents a transition from one state to another

type TransitionType

type TransitionType int

TransitionType represents the type of state transition of the node

const (
	// TransitionTypeEnter enter a state
	TransitionTypeEnter TransitionType = iota
	// TransitionTypeLeave leave a state
	TransitionTypeLeave
	// TransitionTypeAfter after a state
	TransitionTypeAfter
)

func (TransitionType) String

func (t TransitionType) String() string

type Transport added in v1.0.4

type Transport interface {
	Server
	Client

	// Decode decodes the raw data into the target object
	// Both the request and response both contain fields of the any type, we need to decode it
	Decode(raw any, target any) error
}

Transport interface definition that a provider needs to implement.

type TransportConfig added in v1.0.4

type TransportConfig interface {
	Validate() error
}

TransportConfig is an interface representing the contract for a configuration object that can be validated.

Jump to

Keyboard shortcuts

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