raft

package
v0.0.0-...-d2438c5 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2019 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxPool = 2

DefaultMaxPool describes how large the pool should be.

View Source
const Endpoint = "/internal/raft"

Endpoint API path that gets routed to a rafthttp.Handler for joining/leaving the cluster and exchanging raft commands between nodes.

View Source
const Interval = 4

Interval represents the number of seconds to wait between to gc rounds.

Variables

This section is empty.

Functions

func NewConfig

func NewConfig(latency float64) *raft.Config

NewConfig will create a base raft configuration tweaked for a network with the given latency measure.

func NewDialer

func NewDialer(info *cert.Info) (rafthttp.Dial, error)

NewDialer creates a rafthttp.Dial function that connects over TLS using the given cluster (and optionally CA) certificate both as client and remote certificate.

func NewLogger

func NewLogger(logger gokitlog.Logger) *log.Logger

NewLogger will create a logger that can be used by the raft instance, whilst surfacing information to the internal logger.

Types

type AddressProvider

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

AddressProvider is a address provider that looks up server addresses in the raft_nodes table.

func NewAddressProvider

func NewAddressProvider(db Node) *AddressProvider

NewAddressProvider creates a AddressProvider with sane defaults

func (*AddressProvider) ServerAddr

func (p *AddressProvider) ServerAddr(id raft.ServerID) (raft.ServerAddress, error)

ServerAddr gets the raft ServerAddress from the raft nodes, depending on the id supplied

type AddressResolver

type AddressResolver interface {

	// Resolve returns an address of TCP end point.
	//
	// If the host in the address parameter is not a literal IP address or
	// the port is not a literal port number, ResolveTCPAddr resolves the
	// address to an address of TCP end point.
	// Otherwise, it parses the address as a pair of literal IP address
	// and port number.
	// The address parameter can use a host name, but this is not
	// recommended, because it will return at most one of the host name's
	// IP addresses.
	Resolve(string) (net.Addr, error)
}

AddressResolver defines an handler to resolve TCP addresses.

type CacheAddressProvider

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

CacheAddressProvider provides an implementation that attempts to cache addresses for use for server address function, without hitting the database for every lookup.

func NewCacheAddressProvider

func NewCacheAddressProvider(db Node) *CacheAddressProvider

NewCacheAddressProvider creates a AddressProvider with sane defaults

func (*CacheAddressProvider) Run

Run returns a task function that performs cleanup of raft node addresses

func (*CacheAddressProvider) ServerAddr

ServerAddr gets the raft ServerAddress from the raft nodes, depending on the id supplied

type DialerProvider

type DialerProvider interface {
	// Dial creates a rafthttp.Dial function that connects over TLS using the given
	// cluster (and optionally CA) certificate both as client and remote
	// certificate.
	Dial(certInfo *cert.Info) (rafthttp.Dial, error)
}

DialerProvider provides a way to create a dialer for a raft cluster

type HTTPHandler

type HTTPHandler interface {
	// Requests returns a channel of inbound Raft membership change requests
	// received over HTTP. Consumer code is supposed to process this channel by
	// invoking raftmembership.HandleChangeRequests.
	Requests() <-chan *raftmembership.ChangeRequest

	// ServerHTTP upgrades the given HTTP connection to a raw TCP one for
	// use by raft.
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

HTTPHandler defines an HTTP handler that will look for an Upgrade header in the request to switch the HTTP connection to raw TCP mode, so it can be used as raft.NetworkTransport stream.

type HTTPProvider

type HTTPProvider interface {
	// Handler implements an HTTP handler that will look for an Upgrade
	// header in the request to switch the HTTP connection to raw TCP
	// mode, so it can be used as raft.NetworkTransport stream.
	Handler(*stdlog.Logger) *rafthttp.Handler

	// NewLayer returns a new raft stream layer that initiates connections
	// with HTTP and then uses Upgrade to switch them into raw TCP.
	Layer(string, net.Addr, *rafthttp.Handler, rafthttp.Dial, *stdlog.Logger) *rafthttp.Layer
}

HTTPProvider provides new Handlers or Layers to be consumed

type LogStore

type LogStore interface {
	// FirstIndex returns the first index written. 0 for no entries.
	FirstIndex() (uint64, error)

	// LastIndex returns the last index written. 0 for no entries.
	LastIndex() (uint64, error)

	// GetLog gets a log entry at a given index.
	GetLog(uint64, *raft.Log) error

	// StoreLog stores a log entry.
	StoreLog(*raft.Log) error

	// StoreLogs stores multiple log entries.
	StoreLogs([]*raft.Log) error

	// DeleteRange deletes a range of log entries. The range is inclusive.
	DeleteRange(uint64, uint64) error

	// Set stores the key and value
	Set([]byte, []byte) error

	// Get returns the value for key, or an empty byte slice if key was not
	// found.
	Get([]byte) ([]byte, error)

	// SetUint64 sets a key and a uint64 value
	SetUint64([]byte, uint64) error

	// GetUint64 returns the uint64 value for key, or 0 if key was not found.
	GetUint64([]byte) (uint64, error)

	// Close the log store
	Close() error
}

LogStore is used to provide an interface for storing and retrieving logs in a durable fashion.

type LogsProvider

type LogsProvider interface {

	// Logs returns a LogStore
	Logs(string, time.Duration) (LogStore, error)
}

LogsProvider is used to provide an interface for getting the log store.

type MembershipChangerFunc

type MembershipChangerFunc func(*raft.Raft)

MembershipChangerFunc describes a function that will be called when any *raft.Raft membership changes occur.

type NetworkProvider

type NetworkProvider interface {

	// Network creates a new network transport with the given config struct
	Network(*raft.NetworkTransportConfig) raft.Transport
}

NetworkProvider provides new Transports to be consumed

type Node

type Node interface {
	db.NodeTransactioner

	// Dir returns the directory of the underlying database file.
	Dir() string
}

Node mediates access to the data stored in the node-local SQLite database.

type Option

type Option func(*options)

Option to be passed to NewRaft to customize the resulting instance.

func WithAddressResolver

func WithAddressResolver(addressResolver AddressResolver) Option

WithAddressResolver sets the addressResolver on the options

func WithCert

func WithCert(cert *cert.Info) Option

WithCert sets the certificate information on the options

func WithDialerProvider

func WithDialerProvider(dialerProvider DialerProvider) Option

WithDialerProvider sets the dialerProvider on the options

func WithFileSystem

func WithFileSystem(fileSystem fsys.FileSystem) Option

WithFileSystem sets the file system on the options

func WithHTTPProvider

func WithHTTPProvider(httpProvider HTTPProvider) Option

WithHTTPProvider sets the http provider on the options

func WithLatency

func WithLatency(latency float64) Option

WithLatency is a coarse grain measure of how fast/reliable network links are. This is used to tweak the various timeouts parameters of the raft algorithm. See the raft.Config structure for more details. A value of 1.0 means use the default values from hashicorp's raft package. Values closer to 0 reduce the values of the various timeouts (useful when running unit tests in-memory).

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger sets the logger on the options

func WithLogsProvider

func WithLogsProvider(logsProvider LogsProvider) Option

WithLogsProvider sets the logs provider on the options

func WithMediator

func WithMediator(mediator RaftNodeMediator) Option

WithMediator sets the raftNode mediator on the options

func WithNetworkProvider

func WithNetworkProvider(networkProvider NetworkProvider) Option

WithNetworkProvider sets the network provider on the options

func WithNode

func WithNode(database Node) Option

WithNode sets the database node on the options

func WithNodeConfigSchema

func WithNodeConfigSchema(nodeConfigSchema config.Schema) Option

WithNodeConfigSchema sets the node configuration schema on the options

func WithRaftProvider

func WithRaftProvider(raftProvider RaftProvider) Option

WithRaftProvider sets the raft provider on the options

func WithRegistryProvider

func WithRegistryProvider(registryProvider RegistryProvider) Option

WithRegistryProvider sets the registry provider on the options

func WithSnapshotStoreProvider

func WithSnapshotStoreProvider(snapshotStoreProvider SnapshotStoreProvider) Option

WithSnapshotStoreProvider sets the snapshotStore provider on the options

type Raft

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

Raft instance and all its dependencies, to be used as backend for the dqlite driver running on this node.

If this node should not serve as dqlite node, nil is returned.

The raft instance will use an in-memory transport if clustering is not enabled on this node.

The certInfo parameter should contain the cluster TLS keypair and optional CA certificate.

The latency parameter is a coarse grain measure of how fast/reliable network links are. This is used to tweak the various timeouts parameters of the raft algorithm. See the raft.Config structure for more details. A value of 1.0 means use the default values from hashicorp's raft package. Values closer to 0 reduce the values of the various timeouts (useful when running unit tests in-memory).

func New

func New(database Node, cert *cert.Info, nodeConfigSchema config.Schema, fileSystem fsys.FileSystem, options ...Option) *Raft

New creates a new Raft instance with all it's dependencies.

func (*Raft) FSM

func (r *Raft) FSM() raft.FSM

FSM returns the FSM associated with the raft instance.

func (*Raft) HandlerFunc

func (r *Raft) HandlerFunc() http.HandlerFunc

HandlerFunc can be used to handle HTTP requests performed against the API RaftEndpoint ("/internal/raft"), in order to join/leave/form the raft cluster.

If it returns nil, it means that this node is not supposed to expose a raft endpoint over the network, because it's running as a non-clustered single node.

func (*Raft) Init

func (r *Raft) Init() error

Init the raft instance and all its dependencies, to be used as backend for the dqlite driver running on this node.

func (*Raft) MembershipChanger

func (r *Raft) MembershipChanger() raftmembership.Changer

MembershipChanger returns the underlying rafthttp.Layer, which can be used to change the membership of this node in the cluster.

func (*Raft) Raft

func (r *Raft) Raft() *raft.Raft

Raft returns the actual underlying raft instance

func (*Raft) Registry

func (r *Raft) Registry() *dqlite.Registry

Registry returns the Registry associated with the raft instance.

func (*Raft) Servers

func (r *Raft) Servers() ([]raft.Server, error)

Servers returns the servers that are currently part of the cluster.

If this raft instance is not the leader, an error is returned.

func (*Raft) Shutdown

func (r *Raft) Shutdown() error

Shutdown raft and any raft-related resource we have instantiated.

type RaftNodeMediator

type RaftNodeMediator interface {
	// DetermineRaftNode figures out what raft node ID and address we have, if any.
	//
	// This decision is based on the values of the core.https_address config key
	// and on the rows in the raft_nodes table, both stored in the node-level
	// SQLite database.
	DetermineRaftNode(node.Tx, config.Schema) (*db.RaftNode, error)
}

RaftNodeMediator mediates access to a raft node configuration to gain access to a raftNode

type RaftProvider

type RaftProvider interface {
	Raft(*raft.Config, raft.FSM, LogStore, raft.SnapshotStore, raft.Transport) (*raft.Raft, error)
}

RaftProvider is a point of use alias for creating a Raft instance

type RegistryProvider

type RegistryProvider interface {

	// Registry tracks internal data shared by the dqlite Driver and FSM.
	// The parameter is a string identifying the local node.
	Registry(string) *dqlite.Registry

	// FSM creates a new dqlite FSM, suitable to be passed to raft.NewRaft.
	// It will handle replication of the SQLite write-ahead log.
	FSM(*dqlite.Registry) raft.FSM
}

RegistryProvider creates raft consumeables

type SnapshotStoreProvider

type SnapshotStoreProvider interface {

	// Store returns a new shapshot store for a given path
	Store(string) (raft.SnapshotStore, error)
}

SnapshotStoreProvider creates a raft snapshot store

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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