raft

package module
v0.0.0-...-c1f2cb3 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 26 Imported by: 1

README

raft

PkgGoDev Build Status codecov Go Report Card LICENSE

Package raft implements the Raft distributed consensus protocol based on hslam/rpc.

Features

  • Leader election
  • Log replication
  • Membership changes
  • Log compaction (snapshotting)
  • RPC transport
  • Write-ahead logging and LRU cache
  • ReadIndex/LeaseRead
  • Non-voting members (The leader only replicates log entries to them)
  • Snapshot policies (Never/EverySecond/CustomSync)

Get started

Install
go get github.com/hslam/raft
Import
import "github.com/hslam/raft"
Example
package main

import (
	"flag"
	"github.com/hslam/raft"
	"io"
	"io/ioutil"
	"log"
	"strings"
	"time"
)

var host, path, members string
var port int
var join bool

func init() {
	flag.StringVar(&host, "h", "localhost", "hostname")
	flag.IntVar(&port, "p", 9001, "port")
	flag.StringVar(&path, "path", "raft.example/node.1", "data dir")
	flag.BoolVar(&join, "join", false, "")
	flag.StringVar(&members, "members", "localhost:9001", "host:port,nonVoting;host:port")
	flag.Parse()
}

func main() {
	ctx := &Context{data: ""}
	node, err := raft.NewNode(host, port, path, ctx, join, parse(members))
	if err != nil {
		panic(err)
	}
	node.RegisterCommand(&Command{})
	node.SetCodec(&raft.JSONCodec{})
	node.SetSnapshot(ctx)
	node.LeaderChange(func() {
		if node.IsLeader() {
			node.Do(&Command{"foobar"})
			log.Printf("State:%s, Set:foobar\n", node.State())
			if ok := node.ReadIndex(); ok {
				log.Printf("State:%s, Get:%s\n", node.State(), ctx.Get())
			}
		} else {
			for len(ctx.Get()) == 0 {
				time.Sleep(time.Second)
			}
			log.Printf("State:%s, Get:%s\n", node.State(), ctx.Get())
		}
	})
	node.Start()
	for range time.NewTicker(time.Second * 5).C {
		log.Printf("State:%s, Leader:%s\n", node.State(), node.Leader())
	}
}

type Context struct{ data string }

func (ctx *Context) Set(value string) { ctx.data = value }
func (ctx *Context) Get() string      { return ctx.data }

// Save implements the raft.Snapshot Save method.
func (ctx *Context) Save(w io.Writer) (int, error) { return w.Write([]byte(ctx.Get())) }

// Recover implements the raft.Snapshot Recover method.
func (ctx *Context) Recover(r io.Reader) (int, error) {
	raw, err := ioutil.ReadAll(r)
	if err != nil {
		return 0, err
	}
	ctx.Set(string(raw))
	return len(raw), nil
}

// Command implements the raft.Command interface.
type Command struct{ Data string }

func (c *Command) Type() uint64 { return 1 }
func (c *Command) Do(context interface{}) (interface{}, error) {
	context.(*Context).Set(c.Data)
	return nil, nil
}

func parse(members string) (m []*raft.Member) {
	if members != "" {
		for _, member := range strings.Split(members, ";") {
			if len(member) > 0 {
				strs := strings.Split(member, ",")
				m = append(m, &raft.Member{Address: strs[0]})
				if len(strs) > 1 && strs[1] == "true" {
					m[len(m)-1].NonVoting = true
				}
			}
		}
	}
	return
}
Build
go build -o node main.go

One node

./node -h=localhost -p=9001 -path="raft.example/node.1" -join=false

Three nodes

./node -h=localhost -p=9001 -path="raft.example/node.hw.1" -join=false \
-members="localhost:9001;localhost:9002;localhost:9003"

./node -h=localhost -p=9002 -path="raft.example/node.hw.2" -join=false \
-members="localhost:9001;localhost:9002;localhost:9003"

./node -h=localhost -p=9003 -path="raft.example/node.hw.3" -join=false \
-members="localhost:9001;localhost:9002;localhost:9003"

Membership changes

./node -h=localhost -p=9001 -path="raft.example/node.mc.1" -join=false

./node -h=localhost -p=9002 -path="raft.example/node.mc.2" -join=true \
-members="localhost:9001;localhost:9002"

./node -h=localhost -p=9003 -path="raft.example/node.mc.3" -join=true \
-members="localhost:9001;localhost:9002;localhost:9003"

Non-voting

./node -h=localhost -p=9001 -path="raft.example/node.nv.1" -join=false \
-members="localhost:9001;localhost:9002;localhost:9003;localhost:9004,true"

./node -h=localhost -p=9002 -path="raft.example/node.nv.2" -join=false \
-members="localhost:9001;localhost:9002;localhost:9003;localhost:9004,true"

./node -h=localhost -p=9003 -path="raft.example/node.nv.3" -join=false \
-members="localhost:9001;localhost:9002;localhost:9003;localhost:9004,true"

./node -h=localhost -p=9004 -path="raft.example/node.nv.4" -join=false \
-members="localhost:9001;localhost:9002;localhost:9003;localhost:9004,true"
Benchmark

Running on a three nodes cluster.

Write

write-qpswrite-p99

Read Index

read-qpsread-p99

License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

raft was written by Meng Huang.

Documentation

Overview

Package raft implements the Raft distributed consensus protocol.

Index

Constants

View Source
const (

	//AllLogLevel defines the lowest level.
	AllLogLevel = LogLevel(log.AllLevel)
	//TraceLogLevel defines the level of trace in test environments.
	TraceLogLevel = LogLevel(log.TraceLevel)
	//DebugLogLevel defines the level of debug.
	DebugLogLevel = LogLevel(log.DebugLevel)
	//InfoLogLevel defines the level of info.
	InfoLogLevel = LogLevel(log.InfoLevel)
	//NoticeLogLevel defines the level of notice.
	NoticeLogLevel = LogLevel(log.NoticeLevel)
	//WarnLogLevel defines the level of warn.
	WarnLogLevel = LogLevel(log.WarnLevel)
	//ErrorLogLevel defines the level of error.
	ErrorLogLevel = LogLevel(log.ErrorLevel)
	//PanicLogLevel defines the level of panic.
	PanicLogLevel = LogLevel(log.PanicLevel)
	//FatalLogLevel defines the level of fatal.
	FatalLogLevel = LogLevel(log.FatalLevel)
	//OffLogLevel defines the level of no log.
	OffLogLevel = LogLevel(log.OffLevel)
)

Variables

View Source
var (
	// ErrNotLeader is returned when this node is not Leader.
	ErrNotLeader = errors.New("this node is not Leader")
	// ErrNotRunning is returned when this node do not running.
	ErrNotRunning = errors.New("this node do not running")
	// ErrLeaderIsNotReady is returned when the leader is not ready.
	ErrLeaderIsNotReady = errors.New("Leader is not ready")

	// ErrAppendEntriesFailed is returned when append entries failed.
	ErrAppendEntriesFailed = errors.New("AppendEntries failed")
	// ErrAppendEntriesTimeout is returned when append entries timeout.
	ErrAppendEntriesTimeout = errors.New("AppendEntries timeout")

	// ErrCommandNil is returned when the command is nil.
	ErrCommandNil = errors.New("Command can not be nil")
	// ErrCommandTimeout is returned when exec the command timeout.
	ErrCommandTimeout = errors.New("command timeout")
	// ErrCommandNotRegistered is returned when the command is not registered.
	ErrCommandNotRegistered = errors.New("Command is not registered")
	// ErrCommandTypeExisted is returned when the command type is existed.
	ErrCommandTypeExisted = errors.New("CommandType is existed")
	// ErrCommandType is returned when the command type = 0.
	ErrCommandType = errors.New("CommandType must be > 0")

	// ErrSnapshotCodecNil is returned when the snapshotCodec is nil.
	ErrSnapshotCodecNil = errors.New("SnapshotCodec can not be nil")
)
View Source
var ErrorCODE = errors.New("is not Code")

ErrorCODE is the error that v is not Code

View Source
var ErrorGOGOPB = errors.New("is not GoGoProtobuf")

ErrorGOGOPB is the error that v is not GoGoProtobuf

View Source
var ErrorMSGP = errors.New("is not MSGP")

ErrorMSGP is the error that v is not MSGP

Functions

func Address

func Address(host string, port int) string

Address returns a raft node address with the given host and port.

Types

type AddMemberRequest

type AddMemberRequest struct {
	Member *Member
}

AddMemberRequest represents a rpc request of adding peer.

func (*AddMemberRequest) Marshal

func (d *AddMemberRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*AddMemberRequest) MarshalTo

func (d *AddMemberRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*AddMemberRequest) Size

func (d *AddMemberRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*AddMemberRequest) Unmarshal

func (d *AddMemberRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type AddMemberResponse

type AddMemberResponse struct {
	Success  bool
	LeaderID string
}

AddMemberResponse represents a rpc response of adding peer.

func (*AddMemberResponse) Marshal

func (d *AddMemberResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*AddMemberResponse) MarshalTo

func (d *AddMemberResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*AddMemberResponse) Size

func (d *AddMemberResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*AddMemberResponse) Unmarshal

func (d *AddMemberResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type AppendEntriesRequest

type AppendEntriesRequest struct {
	Term         uint64
	LeaderID     string
	PrevLogIndex uint64
	PrevLogTerm  uint64
	LeaderCommit uint64
	Entries      []*Entry
}

AppendEntriesRequest represents a rpc request of appending entries.

func (*AppendEntriesRequest) Marshal

func (d *AppendEntriesRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*AppendEntriesRequest) MarshalTo

func (d *AppendEntriesRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*AppendEntriesRequest) Size

func (d *AppendEntriesRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*AppendEntriesRequest) Unmarshal

func (d *AppendEntriesRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type AppendEntriesResponse

type AppendEntriesResponse struct {
	Term      uint64
	Success   bool
	NextIndex uint64
}

AppendEntriesResponse represents a rpc response of appending entries.

func (*AppendEntriesResponse) Marshal

func (d *AppendEntriesResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*AppendEntriesResponse) MarshalTo

func (d *AppendEntriesResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*AppendEntriesResponse) Size

func (d *AppendEntriesResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*AppendEntriesResponse) Unmarshal

func (d *AppendEntriesResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type BYTESCodec

type BYTESCodec struct {
}

BYTESCodec struct

func (*BYTESCodec) Marshal

func (c *BYTESCodec) Marshal(buf []byte, v interface{}) ([]byte, error)

Marshal returns the BYTES encoding of v.

func (*BYTESCodec) Unmarshal

func (c *BYTESCodec) Unmarshal(data []byte, v interface{}) error

Unmarshal parses the BYTES-encoded data and stores the result in the value pointed to by v.

type CODECodec

type CODECodec struct {
}

CODECodec struct

func (*CODECodec) Marshal

func (c *CODECodec) Marshal(buf []byte, v interface{}) ([]byte, error)

Marshal returns the CODE encoding of v.

func (*CODECodec) Unmarshal

func (c *CODECodec) Unmarshal(data []byte, v interface{}) error

Unmarshal parses the CODE-encoded data and stores the result in the value pointed to by v.

type Code

type Code interface {
	Marshal(buf []byte) ([]byte, error)
	Unmarshal(buf []byte) (uint64, error)
}

Code defines the interface for code.

type Codec

type Codec interface {
	Marshal(buf []byte, v interface{}) ([]byte, error)
	Unmarshal(data []byte, v interface{}) error
}

Codec defines the interface for encoding/decoding.

type Command

type Command interface {
	// Type returns the command type. The type must be > 0.
	Type() uint64
	// Do executes the command with the context.
	Do(context interface{}) (reply interface{}, err error)
}

Command represents a command.

type ConfigurationStorage

type ConfigurationStorage struct {
	Members []*Member
}

ConfigurationStorage represents a configuration storage.

func (*ConfigurationStorage) Marshal

func (d *ConfigurationStorage) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*ConfigurationStorage) MarshalTo

func (d *ConfigurationStorage) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*ConfigurationStorage) Size

func (d *ConfigurationStorage) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*ConfigurationStorage) Unmarshal

func (d *ConfigurationStorage) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type DefaultCommand

type DefaultCommand struct {
	Operation uint64
	Member    *Member
}

DefaultCommand represents a operation command.

func (*DefaultCommand) Do

func (c *DefaultCommand) Do(context interface{}) (interface{}, error)

Do implements the Command Do method.

func (*DefaultCommand) Marshal

func (d *DefaultCommand) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*DefaultCommand) MarshalTo

func (d *DefaultCommand) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*DefaultCommand) Size

func (d *DefaultCommand) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*DefaultCommand) Type

func (c *DefaultCommand) Type() uint64

Type implements the Command Type method.

func (*DefaultCommand) Unmarshal

func (d *DefaultCommand) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type Entry

type Entry struct {
	Index       uint64
	Term        uint64
	CommandType uint64
	Command     []byte
}

Entry represents a log entry.

func (*Entry) Marshal

func (d *Entry) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*Entry) MarshalTo

func (d *Entry) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*Entry) Size

func (d *Entry) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*Entry) Unmarshal

func (d *Entry) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type GOGOPBCodec

type GOGOPBCodec struct {
}

GOGOPBCodec struct

func (*GOGOPBCodec) Marshal

func (c *GOGOPBCodec) Marshal(buf []byte, v interface{}) ([]byte, error)

Marshal returns the GOGOPB encoding of v.

func (*GOGOPBCodec) Unmarshal

func (c *GOGOPBCodec) Unmarshal(data []byte, v interface{}) error

Unmarshal parses the GOGOPB-encoded data and stores the result in the value pointed to by v.

type GetLeaderRequest

type GetLeaderRequest struct {
}

GetLeaderRequest represents a rpc request of getting leader.

func (*GetLeaderRequest) Marshal

func (d *GetLeaderRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*GetLeaderRequest) MarshalTo

func (d *GetLeaderRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*GetLeaderRequest) Size

func (d *GetLeaderRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*GetLeaderRequest) Unmarshal

func (d *GetLeaderRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type GetLeaderResponse

type GetLeaderResponse struct {
	Term     uint64
	LeaderID string
}

GetLeaderResponse represents a rpc response of getting leader.

func (*GetLeaderResponse) Marshal

func (d *GetLeaderResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*GetLeaderResponse) MarshalTo

func (d *GetLeaderResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*GetLeaderResponse) Size

func (d *GetLeaderResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*GetLeaderResponse) Unmarshal

func (d *GetLeaderResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type GetMetaRequest

type GetMetaRequest struct {
}

GetMetaRequest represents a rpc request of getting meta.

func (*GetMetaRequest) Marshal

func (d *GetMetaRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*GetMetaRequest) MarshalTo

func (d *GetMetaRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*GetMetaRequest) Size

func (d *GetMetaRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*GetMetaRequest) Unmarshal

func (d *GetMetaRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type GetMetaResponse

type GetMetaResponse struct {
	Meta []byte
}

GetMetaResponse represents a rpc response of getting meta.

func (*GetMetaResponse) Marshal

func (d *GetMetaResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*GetMetaResponse) MarshalTo

func (d *GetMetaResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*GetMetaResponse) Size

func (d *GetMetaResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*GetMetaResponse) Unmarshal

func (d *GetMetaResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type GoGoProtobuf

type GoGoProtobuf interface {
	Size() (n int)
	Marshal() (data []byte, err error)
	MarshalTo(buf []byte) (int, error)
	Unmarshal(data []byte) error
}

GoGoProtobuf defines the interface for gogo's protobuf.

type InstallSnapshotRequest

type InstallSnapshotRequest struct {
	Term              uint64
	LeaderID          string
	LastIncludedIndex uint64
	LastIncludedTerm  uint64
	Offset            uint64
	Done              bool
	Data              []byte
}

InstallSnapshotRequest represents a rpc request of installing snapshot.

func (*InstallSnapshotRequest) Marshal

func (d *InstallSnapshotRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*InstallSnapshotRequest) MarshalTo

func (d *InstallSnapshotRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*InstallSnapshotRequest) Size

func (d *InstallSnapshotRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*InstallSnapshotRequest) Unmarshal

func (d *InstallSnapshotRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type InstallSnapshotResponse

type InstallSnapshotResponse struct {
	Term      uint64
	Offset    uint64
	NextIndex uint64
}

InstallSnapshotResponse represents a rpc response of installing snapshot.

func (*InstallSnapshotResponse) Marshal

func (d *InstallSnapshotResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*InstallSnapshotResponse) MarshalTo

func (d *InstallSnapshotResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*InstallSnapshotResponse) Size

func (d *InstallSnapshotResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*InstallSnapshotResponse) Unmarshal

func (d *InstallSnapshotResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type JSONCodec

type JSONCodec struct {
}

JSONCodec struct

func (*JSONCodec) Marshal

func (c *JSONCodec) Marshal(buf []byte, v interface{}) ([]byte, error)

Marshal returns the JSON encoding of v.

func (*JSONCodec) Unmarshal

func (c *JSONCodec) Unmarshal(data []byte, v interface{}) error

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.

type LogLevel

type LogLevel log.Level

LogLevel defines the level for log. Higher levels log less info.

type MSGPCodec

type MSGPCodec struct {
}

MSGPCodec struct

func (*MSGPCodec) Marshal

func (c *MSGPCodec) Marshal(buf []byte, v interface{}) ([]byte, error)

Marshal returns the MSGP encoding of v.

func (*MSGPCodec) Unmarshal

func (c *MSGPCodec) Unmarshal(data []byte, v interface{}) error

Unmarshal parses the MSGP-encoded data and stores the result in the value pointed to by v.

type Member

type Member struct {
	Address   string
	NonVoting bool
}

Member represents a node info.

func (*Member) Marshal

func (d *Member) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*Member) MarshalTo

func (d *Member) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*Member) Size

func (d *Member) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*Member) Unmarshal

func (d *Member) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type MsgPack

type MsgPack interface {
	MarshalMsg(buf []byte) ([]byte, error)
	UnmarshalMsg(bts []byte) (o []byte, err error)
}

MsgPack defines the interface for msgp.

type Node

type Node interface {
	Start()
	Stop()
	State() string
	Leader() string
	IsLeader() bool
	Address() string
	SetNodeMeta(address string, meta []byte) bool
	GetNodeMeta(address string) ([]byte, bool)
	Ready() bool
	SetLogLevel(level LogLevel)
	GetLogLevel() LogLevel
	SetCodec(codec Codec)
	SetContext(context interface{})
	SetGzipSnapshot(gzip bool)
	SetSnapshotPolicy(snapshotPolicy SnapshotPolicy)
	SetSnapshot(snapshot Snapshot)
	SetSyncTypes(saves []*SyncType)
	RegisterCommand(command Command) error
	Do(command Command) (interface{}, error)
	ReadIndex() bool
	LeaseRead() bool
	Peers() []string
	Join(member *Member) (success bool)
	Leave(Address string) (success bool)
	Members() []*Member
	LeaderChange(leaderChange func())
}

Node is a raft node.

func NewNode

func NewNode(host string, port int, dataDir string, context interface{}, join bool, members []*Member) (Node, error)

NewNode returns a new raft node.

type RPCs

type RPCs interface {
	Register(s Service) error
	ListenAndServe() error
	Close() error
	Ping(addr string) error
	RequestVote(ctx context.Context, addr string, req *RequestVoteRequest, res *RequestVoteResponse) error
	AppendEntries(ctx context.Context, addr string, req *AppendEntriesRequest, res *AppendEntriesResponse) error
	InstallSnapshot(ctx context.Context, addr string, req *InstallSnapshotRequest, res *InstallSnapshotResponse) error
	GetLeader(ctx context.Context, addr string, req *GetLeaderRequest, res *GetLeaderResponse) error
	AddMember(ctx context.Context, addr string, req *AddMemberRequest, res *AddMemberResponse) error
	RemoveMember(ctx context.Context, addr string, req *RemoveMemberRequest, res *RemoveMemberResponse) error
	SetMeta(ctx context.Context, addr string, req *SetMetaRequest, res *SetMetaResponse) error
	GetMeta(ctx context.Context, addr string, req *GetMetaRequest, res *GetMetaResponse) error
}

RPCs represents the RPCs.

type RemoveMemberRequest

type RemoveMemberRequest struct {
	Address string
}

RemoveMemberRequest represents a rpc request of removing peer.

func (*RemoveMemberRequest) Marshal

func (d *RemoveMemberRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*RemoveMemberRequest) MarshalTo

func (d *RemoveMemberRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*RemoveMemberRequest) Size

func (d *RemoveMemberRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*RemoveMemberRequest) Unmarshal

func (d *RemoveMemberRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type RemoveMemberResponse

type RemoveMemberResponse struct {
	Success  bool
	LeaderID string
}

RemoveMemberResponse represents a rpc response of removing peer.

func (*RemoveMemberResponse) Marshal

func (d *RemoveMemberResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*RemoveMemberResponse) MarshalTo

func (d *RemoveMemberResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*RemoveMemberResponse) Size

func (d *RemoveMemberResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*RemoveMemberResponse) Unmarshal

func (d *RemoveMemberResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type RequestVoteRequest

type RequestVoteRequest struct {
	Term         uint64
	CandidateID  string
	LastLogIndex uint64
	LastLogTerm  uint64
}

RequestVoteRequest represents a rpc request of requesting vote.

func (*RequestVoteRequest) Marshal

func (d *RequestVoteRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*RequestVoteRequest) MarshalTo

func (d *RequestVoteRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*RequestVoteRequest) Size

func (d *RequestVoteRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*RequestVoteRequest) Unmarshal

func (d *RequestVoteRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type RequestVoteResponse

type RequestVoteResponse struct {
	Term        uint64
	VoteGranted bool
}

RequestVoteResponse represents a rpc response of requesting vote.

func (*RequestVoteResponse) Marshal

func (d *RequestVoteResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*RequestVoteResponse) MarshalTo

func (d *RequestVoteResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*RequestVoteResponse) Size

func (d *RequestVoteResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*RequestVoteResponse) Unmarshal

func (d *RequestVoteResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type Service

type Service interface {
	RequestVote(req *RequestVoteRequest, res *RequestVoteResponse) error
	AppendEntries(req *AppendEntriesRequest, res *AppendEntriesResponse) error
	InstallSnapshot(req *InstallSnapshotRequest, res *InstallSnapshotResponse) error
	GetLeader(req *GetLeaderRequest, res *GetLeaderResponse) error
	AddMember(req *AddMemberRequest, res *AddMemberResponse) error
	RemoveMember(req *RemoveMemberRequest, res *RemoveMemberResponse) error
	SetMeta(req *SetMetaRequest, res *SetMetaResponse) error
	GetMeta(req *GetMetaRequest, res *GetMetaResponse) error
}

Service represents the RPCs service.

type SetMetaRequest

type SetMetaRequest struct {
	Meta []byte
}

SetMetaRequest represents a rpc request of setting meta.

func (*SetMetaRequest) Marshal

func (d *SetMetaRequest) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*SetMetaRequest) MarshalTo

func (d *SetMetaRequest) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*SetMetaRequest) Size

func (d *SetMetaRequest) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*SetMetaRequest) Unmarshal

func (d *SetMetaRequest) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type SetMetaResponse

type SetMetaResponse struct {
	Success bool
}

SetMetaResponse represents a rpc response of setting meta.

func (*SetMetaResponse) Marshal

func (d *SetMetaResponse) Marshal() ([]byte, error)

Marshal returns the encoded bytes.

func (*SetMetaResponse) MarshalTo

func (d *SetMetaResponse) MarshalTo(buf []byte) (int, error)

MarshalTo marshals into buf and returns the number of bytes.

func (*SetMetaResponse) Size

func (d *SetMetaResponse) Size() int

Size returns the size of the buffer required to represent the data when encoded.

func (*SetMetaResponse) Unmarshal

func (d *SetMetaResponse) Unmarshal(data []byte) error

Unmarshal unmarshals from data.

type Snapshot

type Snapshot interface {
	// Save writes snapshot data to w until there's no more data to write or
	// when an error occurs. The return value n is the number of bytes
	// written. Any error encountered during the write is also returned.
	Save(w io.Writer) (n int, err error)
	// Recover reads snapshot data from r until EOF or error.
	// The return value n is the number of bytes read.
	// Any error except io.EOF encountered during the read is also returned.
	Recover(r io.Reader) (n int, err error)
}

Snapshot saves a snapshot and recovers from a snapshot.

type SnapshotPolicy

type SnapshotPolicy int

SnapshotPolicy represents a snapshot policy type.

const (
	// Never is a SnapshotPolicy that will never sync the snapshot to the disk.
	Never SnapshotPolicy = 0
	// EverySecond is a SnapshotPolicy that will sync the snapshot to the disk every second.
	EverySecond SnapshotPolicy = 1
	// EveryMinute is a SnapshotPolicy that will sync the snapshot to the disk every minute.
	EveryMinute SnapshotPolicy = 2
	// EveryHour is a SnapshotPolicy that will sync the snapshot to the disk every hour.
	EveryHour SnapshotPolicy = 3
	// EveryDay is a SnapshotPolicy that will sync the snapshot to the disk every day.
	EveryDay SnapshotPolicy = 4
	// DefalutSync is a defalut SnapshotPolicy.
	DefalutSync SnapshotPolicy = 5
	// CustomSync is a custom SnapshotPolicy.
	CustomSync SnapshotPolicy = 6
)

type SyncType

type SyncType struct {
	Seconds int
	Changes int
}

SyncType represents a sync type.

type WAL

type WAL interface {
	FirstIndex() (uint64, error)
	LastIndex() (uint64, error)
	Read(index uint64) ([]byte, error)
	Write(index uint64, data []byte) error
	Flush() error
	Sync() error
	Clean(index uint64) error
	Truncate(index uint64) error
	Reset() error
	Close() error
}

WAL represents the write-ahead log.

type XMLCodec

type XMLCodec struct {
}

XMLCodec struct

func (*XMLCodec) Marshal

func (c *XMLCodec) Marshal(buf []byte, v interface{}) ([]byte, error)

Marshal returns the XML encoding of v.

func (*XMLCodec) Unmarshal

func (c *XMLCodec) Unmarshal(data []byte, v interface{}) error

Unmarshal parses the XML-encoded data and stores the result in the value pointed to by v.

Jump to

Keyboard shortcuts

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