raftserver

package
v0.0.0-...-303e327 Latest Latest
Warning

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

Go to latest
Published: May 22, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidData      = errors.New("raftserver: Invalid data")
	ErrStopped          = errors.New("raftserver: server stopped")
	ErrNotFoundNotifier = errors.New("raftserver: not found notifier")
	ErrTimeout          = errors.New("raftserver: request timed out")
	ErrNoPeers          = errors.New("raftserver: no peers in config")
)
View Source
var (
	ErrInvalidLengthProto        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowProto          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupProto = fmt.Errorf("proto: unexpected end of group")
)

Functions

func NewRaftStorage

func NewRaftStorage(walDir string, sync bool, nodeId uint64, sm StateMachine, shotter *snapshotter) (*raftStorage, error)

Types

type ConfChange

type ConfChange pb.ConfChange

type Config

type Config struct {
	// NodeID is the identity of the local node. NodeID cannot be 0.
	// This parameter is required.
	NodeId     uint64 `json:"nodeId"`
	ListenPort int    `json:"listen_port"`
	WalDir     string `json:"raft_wal_dir"`
	WalSync    bool   `json:"raft_wal_sync"`

	// TickIntervalMs is the milliseconds interval only configure in testing cases.
	TickIntervalMs int `json:"-"`

	// TickInterval is the interval of timer which check heartbeat and election timeout.
	// The default value is 2s.
	TickInterval int `json:"tick_interval"`
	// HeartbeatTick is the heartbeat interval. A leader sends heartbeat
	// message to maintain the leadership every heartbeat interval.
	// The default value is 1.
	HeartbeatTick int `json:"heartbeat_tick"`
	// ElectionTick is the election timeout. If a follower does not receive any message
	// from the leader of current term during ElectionTick, it will become candidate and start an election.
	// ElectionTick must be greater than HeartbeatTick.
	// We suggest to use ElectionTick = 5 * HeartbeatTick to avoid unnecessary leader switching.
	// The default value is 5.
	ElectionTick int `json:"election_tick"`

	// MaxSnapConcurrency limits the max number of snapshot concurrency.
	// the default value is 10.
	MaxSnapConcurrency int `json:"max_snapshots"`

	// SnapshotTimeout is the snapshot timeout in memory.
	// the default value is 10s
	SnapshotTimeout int `json:"snapshot_timeout"`

	ProposeTimeout int `json:"propose_timeout"`

	Members []Member `json:"-"`

	// Applied is the last applied index. It should only be set when restarting
	Applied uint64 `json:"-"`

	SM StateMachine `json:"-"`
}

func (*Config) Verify

func (cfg *Config) Verify() error

type Generator

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

| prefix | suffix | | 2 bytes | 5 bytes | 1 byte | | memberID | timestamp | cnt |

func NewGenerator

func NewGenerator(memberId uint64, now time.Time) *Generator

func (*Generator) Next

func (g *Generator) Next() uint64

Next generates a id that is unique.

type Member

type Member struct {
	NodeID               uint64   `protobuf:"varint,1,opt,name=nodeID,proto3" json:"nodeID,omitempty"`
	Host                 string   `protobuf:"bytes,2,opt,name=host,proto3" json:"host,omitempty"`
	Learner              bool     `protobuf:"varint,3,opt,name=learner,proto3" json:"learner,omitempty"`
	Context              []byte   `protobuf:"bytes,4,opt,name=context,proto3" json:"context,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*Member) Descriptor

func (*Member) Descriptor() ([]byte, []int)

func (*Member) GetContext

func (m *Member) GetContext() []byte

func (*Member) GetHost

func (m *Member) GetHost() string

func (*Member) GetLearner

func (m *Member) GetLearner() bool

func (*Member) GetNodeID

func (m *Member) GetNodeID() uint64

func (*Member) Marshal

func (m *Member) Marshal() (dAtA []byte, err error)

func (*Member) MarshalTo

func (m *Member) MarshalTo(dAtA []byte) (int, error)

func (*Member) MarshalToSizedBuffer

func (m *Member) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Member) ProtoMessage

func (*Member) ProtoMessage()

func (*Member) Reset

func (m *Member) Reset()

func (*Member) Size

func (m *Member) Size() (n int)

func (*Member) String

func (m *Member) String() string

func (*Member) Unmarshal

func (m *Member) Unmarshal(dAtA []byte) error

func (*Member) XXX_DiscardUnknown

func (m *Member) XXX_DiscardUnknown()

func (*Member) XXX_Marshal

func (m *Member) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Member) XXX_Merge

func (m *Member) XXX_Merge(src proto.Message)

func (*Member) XXX_Size

func (m *Member) XXX_Size() int

func (*Member) XXX_Unmarshal

func (m *Member) XXX_Unmarshal(b []byte) error

type Members

type Members struct {
	Mbs []Member `json:"members"`
}

type Peer

type Peer struct {
	Id              uint64 `json:"id"`
	Host            string `json:"host"`
	Match           uint64 `json:"match"`
	Next            uint64 `json:"next"`
	State           string `json:"state"`
	Paused          bool   `json:"paused"`
	PendingSnapshot uint64 `json:"pendingSnapshot"`
	RecentActive    bool   `json:"active"`
	IsLearner       bool   `json:"isLearner"`
	InflightFull    bool   `json:"isInflightFull"`
	InflightCount   int    `json:"inflightCount"`
}

type RaftServer

type RaftServer interface {
	Stop()
	Propose(ctx context.Context, data []byte) error
	ReadIndex(ctx context.Context) error
	TransferLeadership(ctx context.Context, leader, transferee uint64)
	AddMember(ctx context.Context, member Member) error
	RemoveMember(ctx context.Context, nodeID uint64) error
	IsLeader() bool
	Status() Status

	// In order to prevent log expansion, the application needs to call this method.
	Truncate(index uint64) error
}

func NewRaftServer

func NewRaftServer(cfg *Config) (RaftServer, error)

type Snapshot

type Snapshot interface {
	Read() ([]byte, error)
	Name() string
	Index() uint64
	Close()
}

type SnapshotMeta

type SnapshotMeta struct {
	Name                 string    `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	Index                uint64    `protobuf:"varint,2,opt,name=index,proto3" json:"index,omitempty"`
	Term                 uint64    `protobuf:"varint,3,opt,name=term,proto3" json:"term,omitempty"`
	Mbs                  []*Member `protobuf:"bytes,4,rep,name=mbs,proto3" json:"mbs,omitempty"`
	Voters               []uint64  `protobuf:"varint,5,rep,packed,name=voters,proto3" json:"voters,omitempty"`
	Learners             []uint64  `protobuf:"varint,6,rep,packed,name=learners,proto3" json:"learners,omitempty"`
	XXX_NoUnkeyedLiteral struct{}  `json:"-"`
	XXX_unrecognized     []byte    `json:"-"`
	XXX_sizecache        int32     `json:"-"`
}

func (*SnapshotMeta) Descriptor

func (*SnapshotMeta) Descriptor() ([]byte, []int)

func (*SnapshotMeta) GetIndex

func (m *SnapshotMeta) GetIndex() uint64

func (*SnapshotMeta) GetLearners

func (m *SnapshotMeta) GetLearners() []uint64

func (*SnapshotMeta) GetMbs

func (m *SnapshotMeta) GetMbs() []*Member

func (*SnapshotMeta) GetName

func (m *SnapshotMeta) GetName() string

func (*SnapshotMeta) GetTerm

func (m *SnapshotMeta) GetTerm() uint64

func (*SnapshotMeta) GetVoters

func (m *SnapshotMeta) GetVoters() []uint64

func (*SnapshotMeta) Marshal

func (m *SnapshotMeta) Marshal() (dAtA []byte, err error)

func (*SnapshotMeta) MarshalTo

func (m *SnapshotMeta) MarshalTo(dAtA []byte) (int, error)

func (*SnapshotMeta) MarshalToSizedBuffer

func (m *SnapshotMeta) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*SnapshotMeta) ProtoMessage

func (*SnapshotMeta) ProtoMessage()

func (*SnapshotMeta) Reset

func (m *SnapshotMeta) Reset()

func (*SnapshotMeta) Size

func (m *SnapshotMeta) Size() (n int)

func (*SnapshotMeta) String

func (m *SnapshotMeta) String() string

func (*SnapshotMeta) Unmarshal

func (m *SnapshotMeta) Unmarshal(dAtA []byte) error

func (*SnapshotMeta) XXX_DiscardUnknown

func (m *SnapshotMeta) XXX_DiscardUnknown()

func (*SnapshotMeta) XXX_Marshal

func (m *SnapshotMeta) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*SnapshotMeta) XXX_Merge

func (m *SnapshotMeta) XXX_Merge(src proto.Message)

func (*SnapshotMeta) XXX_Size

func (m *SnapshotMeta) XXX_Size() int

func (*SnapshotMeta) XXX_Unmarshal

func (m *SnapshotMeta) XXX_Unmarshal(b []byte) error

type StateMachine

type StateMachine interface {
	Apply(data [][]byte, index uint64) error
	ApplyMemberChange(cc ConfChange, index uint64) error
	Snapshot() (Snapshot, error)
	ApplySnapshot(meta SnapshotMeta, st Snapshot) error
	LeaderChange(leader uint64, host string)
}

The StateMachine interface is supplied by the application to persist/snapshot data of application.

type Status

type Status struct {
	Id             uint64 `json:"nodeId"`
	Term           uint64 `json:"term"`
	Vote           uint64 `json:"vote"`
	Commit         uint64 `json:"commit"`
	Leader         uint64 `json:"leader"`
	RaftState      string `json:"raftState"`
	Applied        uint64 `json:"applied"`
	RaftApplied    uint64 `json:"raftApplied"`
	LeadTransferee uint64 `json:"transferee"`
	ApplyingLength int    `json:"applyingLength"`
	Peers          []Peer `json:"peers"`
}

type Transport

type Transport interface {
	Send(msgs []pb.Message)
	SendSnapshot(to uint64, st *snapshot) error
	RemoveMember(id uint64)
	AddMember(m Member)
	SetMembers(members []*Member)
	Stop()
}

func NewTransport

func NewTransport(port int, handler handler) Transport

type WaitTime

type WaitTime interface {
	Wait(deadline uint64) <-chan struct{}
	Trigger(deadline uint64)
}

func NewTimeList

func NewTimeList() WaitTime

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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