Back to godoc.org

Package raft

v0.2.0
Latest Go to latest

The latest major version is .

Published: Dec 23, 2013 | Licenses: Apache-2.0 , MIT | Module: go.etcd.io/etcd

Index

Constants

const (
	Debug = 1
	Trace = 2
)
const (
	Stopped      = "stopped"
	Follower     = "follower"
	Candidate    = "candidate"
	Leader       = "leader"
	Snapshotting = "snapshotting"
)
const (
	MaxLogEntriesPerRequest         = 2000
	NumberOfLogEntriesAfterSnapshot = 200
)
const (
	DefaultHeartbeatTimeout = 50 * time.Millisecond
	DefaultElectionTimeout  = 150 * time.Millisecond
)

Variables

var CommandTimeoutError = errors.New("raft: Command timeout")
var DuplicatePeerError = errors.New("raft.Server: Duplicate peer")
var NotLeaderError = errors.New("raft.Server: Not current leader")

func LogLevel

func LogLevel() int

func RegisterCommand

func RegisterCommand(command Command)

Registers a command by storing a reference to an instance of it.

func SetLogLevel

func SetLogLevel(level int)

type AppendEntriesRequest

type AppendEntriesRequest struct {
	Term         uint64
	PrevLogIndex uint64
	PrevLogTerm  uint64
	CommitIndex  uint64
	LeaderName   string
	Entries      []*LogEntry
}

The request sent to a server to append entries to the log.

func (*AppendEntriesRequest) Decode

func (req *AppendEntriesRequest) Decode(r io.Reader) (int, error)

Decodes the AppendEntriesRequest from a buffer. Returns the number of bytes read and any error that occurs.

func (*AppendEntriesRequest) Encode

func (req *AppendEntriesRequest) Encode(w io.Writer) (int, error)

Encodes the AppendEntriesRequest to a buffer. Returns the number of bytes written and any error that may have occurred.

type AppendEntriesResponse

type AppendEntriesResponse struct {
	Term uint64
	// the current index of the server
	Index       uint64
	Success     bool
	CommitIndex uint64
	// contains filtered or unexported fields
}

The response returned from a server appending entries to the log.

func (*AppendEntriesResponse) Decode

func (resp *AppendEntriesResponse) Decode(r io.Reader) (int, error)

Decodes the AppendEntriesResponse from a buffer. Returns the number of bytes read and any error that occurs.

func (*AppendEntriesResponse) Encode

func (resp *AppendEntriesResponse) Encode(w io.Writer) (int, error)

Encodes the AppendEntriesResponse to a buffer. Returns the number of bytes written and any error that may have occurred.

type Command

type Command interface {
	CommandName() string
	Apply(server Server) (interface{}, error)
}

A command represents an action to be taken on the replicated state machine.

type CommandEncoder

type CommandEncoder interface {
	Encode(w io.Writer) error
	Decode(r io.Reader) error
}

type Config

type Config struct {
	CommitIndex uint64 `json:"commitIndex"`
	// TODO decide what we need to store in peer struct
	Peers []*Peer `json:"peers"`
}

type DefaultJoinCommand

type DefaultJoinCommand struct {
	Name             string `json:"name"`
	ConnectionString string `json:"connectionString"`
}

Join command

func (*DefaultJoinCommand) Apply

func (c *DefaultJoinCommand) Apply(server Server) (interface{}, error)

func (*DefaultJoinCommand) CommandName

func (c *DefaultJoinCommand) CommandName() string

The name of the Join command in the log

func (*DefaultJoinCommand) NodeName

func (c *DefaultJoinCommand) NodeName() string

type DefaultLeaveCommand

type DefaultLeaveCommand struct {
	Name string `json:"name"`
}

Leave command

func (*DefaultLeaveCommand) Apply

func (c *DefaultLeaveCommand) Apply(server Server) (interface{}, error)

func (*DefaultLeaveCommand) CommandName

func (c *DefaultLeaveCommand) CommandName() string

The name of the Leave command in the log

func (*DefaultLeaveCommand) NodeName

func (c *DefaultLeaveCommand) NodeName() string

type HTTPMuxer

type HTTPMuxer interface {
	HandleFunc(string, func(http.ResponseWriter, *http.Request))
}

type HTTPTransporter

type HTTPTransporter struct {
	DisableKeepAlives bool
	// contains filtered or unexported fields
}

An HTTPTransporter is a default transport layer used to communicate between multiple servers.

func NewHTTPTransporter

func NewHTTPTransporter(prefix string) *HTTPTransporter

Creates a new HTTP transporter with the given path prefix.

func (*HTTPTransporter) AppendEntriesPath

func (t *HTTPTransporter) AppendEntriesPath() string

Retrieves the AppendEntries path.

func (*HTTPTransporter) Install

func (t *HTTPTransporter) Install(server Server, mux HTTPMuxer)

Applies Raft routes to an HTTP router for a given server.

func (*HTTPTransporter) Prefix

func (t *HTTPTransporter) Prefix() string

Retrieves the path prefix used by the transporter.

func (*HTTPTransporter) RequestVotePath

func (t *HTTPTransporter) RequestVotePath() string

Retrieves the RequestVote path.

func (*HTTPTransporter) SendAppendEntriesRequest

func (t *HTTPTransporter) SendAppendEntriesRequest(server Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse

Sends an AppendEntries RPC to a peer.

func (*HTTPTransporter) SendSnapshotRecoveryRequest

func (t *HTTPTransporter) SendSnapshotRecoveryRequest(server Server, peer *Peer, req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse

Sends a SnapshotRequest RPC to a peer.

func (*HTTPTransporter) SendSnapshotRequest

func (t *HTTPTransporter) SendSnapshotRequest(server Server, peer *Peer, req *SnapshotRequest) *SnapshotResponse

Sends a SnapshotRequest RPC to a peer.

func (*HTTPTransporter) SendVoteRequest

func (t *HTTPTransporter) SendVoteRequest(server Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse

Sends a RequestVote RPC to a peer.

type JoinCommand

type JoinCommand interface {
	CommandName() string
	Apply(server Server) (interface{}, error)
	NodeName() string
}

Join command interface

type LeaveCommand

type LeaveCommand interface {
	CommandName() string
	Apply(server Server) (interface{}, error)
	NodeName() string
}

Leave command interface

type Log

type Log struct {
	ApplyFunc func(Command) (interface{}, error)
	// contains filtered or unexported fields
}

A log is a collection of log entries that are persisted to durable storage.

func (*Log) CommitIndex

func (l *Log) CommitIndex() uint64

The last committed index in the log.

type LogEntry

type LogEntry struct {
	Index       uint64
	Term        uint64
	CommandName string
	Command     []byte
	Position    int64 // position in the log file
	// contains filtered or unexported fields
}

A log entry stores a single item in the log.

type NOPCommand

type NOPCommand struct {
}

NOP command

func (NOPCommand) Apply

func (c NOPCommand) Apply(server Server) (interface{}, error)

func (NOPCommand) CommandName

func (c NOPCommand) CommandName() string

The name of the NOP command in the log

func (NOPCommand) Decode

func (c NOPCommand) Decode(r io.Reader) error

func (NOPCommand) Encode

func (c NOPCommand) Encode(w io.Writer) error

type Peer

type Peer struct {
	Name             string `json:"name"`
	ConnectionString string `json:"connectionString"`
	// contains filtered or unexported fields
}

A peer is a reference to another server involved in the consensus protocol.

type RequestVoteRequest

type RequestVoteRequest struct {
	Term          uint64
	LastLogIndex  uint64
	LastLogTerm   uint64
	CandidateName string
	// contains filtered or unexported fields
}

The request sent to a server to vote for a candidate to become a leader.

func (*RequestVoteRequest) Decode

func (req *RequestVoteRequest) Decode(r io.Reader) (int, error)

Decodes the RequestVoteRequest from a buffer. Returns the number of bytes read and any error that occurs.

func (*RequestVoteRequest) Encode

func (req *RequestVoteRequest) Encode(w io.Writer) (int, error)

Encodes the RequestVoteRequest to a buffer. Returns the number of bytes written and any error that may have occurred.

type RequestVoteResponse

type RequestVoteResponse struct {
	Term        uint64
	VoteGranted bool
	// contains filtered or unexported fields
}

The response returned from a server after a vote for a candidate to become a leader.

func (*RequestVoteResponse) Decode

func (resp *RequestVoteResponse) Decode(r io.Reader) (int, error)

Decodes the RequestVoteResponse from a buffer. Returns the number of bytes read and any error that occurs.

func (*RequestVoteResponse) Encode

func (resp *RequestVoteResponse) Encode(w io.Writer) (int, error)

Encodes the RequestVoteResponse to a buffer. Returns the number of bytes written and any error that may have occurred.

type Server

type Server interface {
	Name() string
	Context() interface{}
	StateMachine() StateMachine
	Leader() string
	State() string
	Path() string
	LogPath() string
	SnapshotPath(lastIndex uint64, lastTerm uint64) string
	Term() uint64
	CommitIndex() uint64
	VotedFor() string
	MemberCount() int
	QuorumSize() int
	IsLogEmpty() bool
	LogEntries() []*LogEntry
	LastCommandName() string
	GetState() string
	ElectionTimeout() time.Duration
	SetElectionTimeout(duration time.Duration)
	HeartbeatTimeout() time.Duration
	SetHeartbeatTimeout(duration time.Duration)
	Transporter() Transporter
	SetTransporter(t Transporter)
	AppendEntries(req *AppendEntriesRequest) *AppendEntriesResponse
	RequestVote(req *RequestVoteRequest) *RequestVoteResponse
	RequestSnapshot(req *SnapshotRequest) *SnapshotResponse
	SnapshotRecoveryRequest(req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse
	AddPeer(name string, connectiongString string) error
	RemovePeer(name string) error
	Peers() map[string]*Peer
	Start() error
	Stop()
	Running() bool
	Do(command Command) (interface{}, error)
	TakeSnapshot() error
	LoadSnapshot() error
}

A server is involved in the consensus protocol and can act as a follower, candidate or a leader.

func NewServer

func NewServer(name string, path string, transporter Transporter, stateMachine StateMachine, context interface{}, connectionString string) (Server, error)

Creates a new server with a log at the given path.

type Snapshot

type Snapshot struct {
	LastIndex uint64 `json:"lastIndex"`
	LastTerm  uint64 `json:"lastTerm"`
	// cluster configuration.
	Peers []*Peer `json:"peers"`
	State []byte  `json:"state"`
	Path  string  `json:"path"`
}

the in memory SnapShot struct TODO add cluster configuration

type SnapshotRecoveryRequest

type SnapshotRecoveryRequest struct {
	LeaderName string
	LastIndex  uint64
	LastTerm   uint64
	Peers      []*Peer
	State      []byte
}

The request sent to a server to start from the snapshot.

func (*SnapshotRecoveryRequest) Decode

func (req *SnapshotRecoveryRequest) Decode(r io.Reader) (int, error)

Decodes the SnapshotRecoveryRequest from a buffer. Returns the number of bytes read and any error that occurs.

func (*SnapshotRecoveryRequest) Encode

func (req *SnapshotRecoveryRequest) Encode(w io.Writer) (int, error)

Encodes the SnapshotRecoveryRequest to a buffer. Returns the number of bytes written and any error that may have occurred.

type SnapshotRecoveryResponse

type SnapshotRecoveryResponse struct {
	Term        uint64
	Success     bool
	CommitIndex uint64
}

The response returned from a server appending entries to the log.

func (*SnapshotRecoveryResponse) Decode

func (req *SnapshotRecoveryResponse) Decode(r io.Reader) (int, error)

Decodes the SnapshotRecoveryResponse from a buffer. Returns the number of bytes read and any error that occurs.

func (*SnapshotRecoveryResponse) Encode

func (req *SnapshotRecoveryResponse) Encode(w io.Writer) (int, error)

Encodes the SnapshotRecoveryResponse to a buffer. Returns the number of bytes written and any error that may have occurred.

type SnapshotRequest

type SnapshotRequest struct {
	LeaderName string
	LastIndex  uint64
	LastTerm   uint64
}

The request sent to a server to start from the snapshot.

func (*SnapshotRequest) Decode

func (req *SnapshotRequest) Decode(r io.Reader) (int, error)

Decodes the SnapshotRequest from a buffer. Returns the number of bytes read and any error that occurs.

func (*SnapshotRequest) Encode

func (req *SnapshotRequest) Encode(w io.Writer) (int, error)

Encodes the SnapshotRequest to a buffer. Returns the number of bytes written and any error that may have occurred.

type SnapshotResponse

type SnapshotResponse struct {
	Success bool `json:"success"`
}

The response returned if the follower entered snapshot state

func (*SnapshotResponse) Decode

func (resp *SnapshotResponse) Decode(r io.Reader) (int, error)

Decodes the SnapshotResponse from a buffer. Returns the number of bytes read and any error that occurs.

func (*SnapshotResponse) Encode

func (resp *SnapshotResponse) Encode(w io.Writer) (int, error)

Encodes the SnapshotResponse to a buffer. Returns the number of bytes written and any error that may have occurred.

type StateMachine

type StateMachine interface {
	Save() ([]byte, error)
	Recovery([]byte) error
}

StateMachine is the interface for allowing the host application to save and recovery the state machine

type Transporter

type Transporter interface {
	SendVoteRequest(server Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse
	SendAppendEntriesRequest(server Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse
	SendSnapshotRequest(server Server, peer *Peer, req *SnapshotRequest) *SnapshotResponse
	SendSnapshotRecoveryRequest(server Server, peer *Peer, req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse
}

Transporter is the interface for allowing the host application to transport requests to other nodes.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier