Back to godoc.org

Package raft

v0.0.0-...-d4e3a16
Latest Go to latest

The latest major version is .

Published: 0 hours ago | License: AGPL-3.0 | Module: github.com/juju/juju

Index

Constants

const (

	// LoopTimeout is the max time we will wait until the raft object
	// is constructed and the main loop is started. This is to avoid
	// hard-to-debug problems where the transport hung and so this
	// worker wasn't really started even though it seemed like it
	// was. If it crashes instead the logging will give a path to the
	// problem.
	LoopTimeout = 1 * time.Minute
)

Variables

var (
	// ErrWorkerStopped is returned by Worker.Raft if the
	// worker has been explicitly stopped.
	ErrWorkerStopped = errors.New("raft worker stopped")

	// ErrStartTimeout is returned by NewWorker if the worker loop
	// didn't start within LoopTimeout.
	ErrStartTimeout = errors.New("timed out waiting for worker loop")

	// ErrNoLeaderTimeout is returned by the worker loop if we've gone
	// too long without contact from the leader. It gives the worker a
	// chance to see any configuration changes the backstop worker
	// might have force-appended to the raft log.
	ErrNoLeaderTimeout = errors.New("timed out waiting for leader contact")
)

func Bootstrap

func Bootstrap(config Config) error

Bootstrap bootstraps the raft cluster, using the given configuration.

This is only to be called once, at the beginning of the raft cluster's lifetime, by the bootstrap machine agent.

func Manifold

func Manifold(config ManifoldConfig) dependency.Manifold

Manifold returns a dependency.Manifold that will run a raft worker.

func NewLogStore

func NewLogStore(dir string, syncMode SyncMode) (*raftboltdb.BoltStore, error)

NewLogStore opens a boltDB logstore in the specified directory. If the directory doesn't already exist it'll be created. If the caller passes NonSyncedAfterWrite as the value of the syncMode argument, the underlying store will NOT perform fsync calls between log writes.

func NewRaftConfig

func NewRaftConfig(config Config) (*raft.Config, error)

NewRaftConfig makes a raft config struct from the worker config struct passed in.

func NewSnapshotStore

func NewSnapshotStore(
	dir string,
	retain int,
	logger Logger,
) (raft.SnapshotStore, error)

NewSnapshotStore opens a file-based snapshot store in the specified directory. If the directory doesn't exist it'll be created.

func NewWorker

func NewWorker(config Config) (worker.Worker, error)

NewWorker returns a new raft worker, with the given configuration.

func NewWorkerShim

func NewWorkerShim(config Config) (worker.Worker, error)

NewWorkerShim is suitable for use in ManifoldConfig.NewWorker, and simply calls through to NewWorker.

type BootstrapFSM

type BootstrapFSM struct{}

BootstrapFSM is a minimal implementation of raft.FSM for use during bootstrap. Its methods should never be invoked.

func (BootstrapFSM) Apply

func (BootstrapFSM) Apply(log *raft.Log) interface{}

Apply is part of raft.FSM.

func (BootstrapFSM) Restore

func (BootstrapFSM) Restore(io.ReadCloser) error

Restore is part of raft.FSM.

func (BootstrapFSM) Snapshot

func (BootstrapFSM) Snapshot() (raft.FSMSnapshot, error)

Snapshot is part of raft.FSM.

type Config

type Config struct {
	// FSM is the raft.FSM to use for this raft worker. This
	// must be non-nil for NewWorker, and nil for Bootstrap.
	FSM raft.FSM

	// Logger is the logger for this worker.
	Logger Logger

	// StorageDir is the directory in which to store raft
	// artifacts: logs, snapshots, etc. It is expected that
	// this directory is under the full control of the raft
	// worker.
	StorageDir string

	// NonSyncedWritesToRaftLog allows the operator to disable fsync calls
	// after each write to the raft log. This option trades performance for
	// data safety and should be used with caution.
	NonSyncedWritesToRaftLog bool

	// LocalID is the raft.ServerID of this worker.
	LocalID raft.ServerID

	// Transport is the raft.Transport to use for communication
	// between raft servers. This must be non-nil for NewWorker,
	// and nil for Bootstrap.
	//
	// The raft worker expects the server address to exactly
	// match the server ID, which is the stringified agent tag.
	// The transport internally maps the server address to one
	// or more network addresses, i.e. by looking up the API
	// connection information in the state database.
	Transport raft.Transport

	// Clock is used for timeouts in the worker (although not inside
	// raft).
	Clock clock.Clock

	// NoLeaderTimeout, if non-zero, will override the default
	// timeout for leader contact before restarting.
	NoLeaderTimeout time.Duration

	// ElectionTimeout, if non-zero, will override the default
	// raft election timeout.
	ElectionTimeout time.Duration

	// HeartbeatTimeout, if non-zero, will override the default
	// raft heartbeat timeout.
	HeartbeatTimeout time.Duration

	// LeaderLeaseTimeout, if non-zero, will override the default
	// raft leader lease timeout.
	LeaderLeaseTimeout time.Duration

	// SnapshotRetention is the non-negative number of snapshots
	// to retain on disk. If zero, defaults to 2.
	SnapshotRetention int

	// PrometheusRegisterer is used to register the raft metrics.
	PrometheusRegisterer prometheus.Registerer
}

Config is the configuration required for running a raft worker.

func (Config) Validate

func (config Config) Validate() error

Validate validates the raft worker configuration.

type Logger

type Logger interface {
	Warningf(message string, args ...interface{})
	Errorf(message string, args ...interface{})
	Tracef(message string, args ...interface{})
	Logf(level loggo.Level, message string, args ...interface{})
}

Logger represents the logging methods called.

type ManifoldConfig

type ManifoldConfig struct {
	ClockName     string
	AgentName     string
	TransportName string

	FSM                  raft.FSM
	Logger               Logger
	PrometheusRegisterer prometheus.Registerer
	NewWorker            func(Config) (worker.Worker, error)
}

ManifoldConfig holds the information necessary to run a raft worker in a dependency.Engine.

func (ManifoldConfig) Validate

func (config ManifoldConfig) Validate() error

Validate validates the manifold configuration.

type SimpleFSM

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

SimpleFSM is an implementation of raft.FSM, which simply appends the log data to a slice.

func (*SimpleFSM) Apply

func (fsm *SimpleFSM) Apply(log *raft.Log) interface{}

Apply is part of the raft.FSM interface.

func (*SimpleFSM) Logs

func (fsm *SimpleFSM) Logs() [][]byte

Logs returns the accumulated log data.

func (*SimpleFSM) Restore

func (fsm *SimpleFSM) Restore(rc io.ReadCloser) error

Restore is part of the raft.FSM interface.

func (*SimpleFSM) Snapshot

func (fsm *SimpleFSM) Snapshot() (raft.FSMSnapshot, error)

Snapshot is part of the raft.FSM interface.

type SimpleSnapshot

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

SimpleSnapshot is an implementation of raft.FSMSnapshot, returned by the SimpleFSM.Snapshot in this package.

func (*SimpleSnapshot) Persist

func (snap *SimpleSnapshot) Persist(sink raft.SnapshotSink) error

Persist is part of the raft.FSMSnapshot interface.

func (*SimpleSnapshot) Release

func (*SimpleSnapshot) Release()

Release is part of the raft.FSMSnapshot interface.

type SyncMode

type SyncMode bool

SyncMode defines the supported sync modes when writing to the raft log store.

const (
	// SyncWrites ensures that an fsync call is performed after each write.
	SyncAfterWrite SyncMode = false

	// NoSyncAfterWrite ensures that no fsync calls are performed between
	// writes.
	NoSyncAfterWrite SyncMode = true
)

type Worker

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

Worker is a worker that manages a raft.Raft instance.

func (*Worker) Kill

func (w *Worker) Kill()

Kill is part of the worker.Worker interface.

func (*Worker) LogStore

func (w *Worker) LogStore() (raft.LogStore, error)

LogStore returns the raft.LogStore managed by this worker, or an error if the worker has stopped.

func (*Worker) Raft

func (w *Worker) Raft() (*raft.Raft, error)

Raft returns the raft.Raft managed by this worker, or an error if the worker has stopped.

func (*Worker) Report

func (w *Worker) Report() map[string]interface{}

Report is part of the dependency.Reporter interface.

func (*Worker) Wait

func (w *Worker) Wait() error

Wait is part of the worker.Worker interface.

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