raft

package
v0.0.0-...-2e56a82 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2017 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Package raft provides a simple key-value store coordinated across a raft cluster.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsatisfied = errors.New("precondition not satisfied")

Functions

This section is empty.

Types

type Getter

type Getter interface {
	Get(key string) (value []byte)
}

Getter gets a value from a key-value store.

type Service

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

Service holds the key-value data and performs raft coordination.

func Start

func Start(laddr, dir, bootURL string, httpClient *http.Client, useTLS bool) (*Service, error)

Start starts the raft algorithm.

Param laddr is the local address, to be used by peers to send messages to the local node. The returned *Service handles HTTP requests matching the ServeMux pattern /raft/. The caller is responsible for registering this handler to receive incoming requests on laddr. For example:

rs, err := raft.Start(addr, ...)
...
http.Handle("/raft/", rs)
http.ListenAndServe(addr, nil)

Param dir is the filesystem location for all persistent storage for this raft node. If it doesn't exist, Start will create it. It has three entries:

id    file containing the node's member id (never changes)
snap  file containing the last complete state snapshot
wal   dir containing the write-ahead log

Param bootURL gives the location of an existing cluster for the local process to join. It can be either the concrete address of any single cluster member or it can point to a load balancer for the whole cluster, if one exists. An empty bootURL means to start a fresh empty cluster. It is ignored when recovering from existing state in dir.

The returned *Service will use httpClient for outbound connections to peers.

func (*Service) AddAllowedMember

func (sv *Service) AddAllowedMember(ctx context.Context, addr string) error

AddAllowedMember adds an address for a member to the list of allowed cluster members. An address must be listed as a allowed cluster member before the node listening on that address can join the cluster.

func (*Service) Delete

func (sv *Service) Delete(ctx context.Context, key string) error

Delete deletes a value in the key-value storage. if successful, it returns after the value is deleted from the raft log. TODO (ameets): is RawNode possible/applicable?

func (*Service) Err

func (sv *Service) Err() error

Err returns a serious error preventing this process from operating normally or making progress, if any. Note that it is possible for a Service to recover automatically from some errors returned by Err.

func (*Service) Get

func (sv *Service) Get(ctx context.Context, key string) ([]byte, error)

Get gets a value from the key-value store. It is linearizable; that is, if a Set happens before a Get, the Get will observe the effects of the Set. (There is still no guarantee an intervening Set won't have changed the value again, but it is guaranteed not to read stale data.) This can be slow; for faster but possibly stale reads, see Stale.

func (*Service) Insert

func (sv *Service) Insert(ctx context.Context, key string, val []byte) error

Insert inserts a value into key-value storage. It will fail if there's already a value stored at the given key. If successful, it returns after the value is committed to the raft log.

func (*Service) ServeHTTP

func (sv *Service) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP responds to raft consensus messages at /raft/x, where x is any particular raft internode RPC. When sv sends outgoing messages, it acts as an HTTP client and sends requests to its peers at /raft/x.

func (*Service) Set

func (sv *Service) Set(ctx context.Context, key string, val []byte) error

Set sets a value in the key-value storage. If successful, it returns after the value is committed to the raft log. TODO (ameets): possibly RawNode in future to know whether Proposal worked or not

func (*Service) Stale

func (sv *Service) Stale() Getter

Stale returns an object that reads directly from local memory, returning (possibly) stale data. Calls to sv.Get are linearizable, which requires them to go through the raft protocol. The stale getter skips this, so it is much faster, but it can only be used in situations that don't require linearizability.

Directories

Path Synopsis
internal
statepb
Package statepb is a generated protocol buffer package.
Package statepb is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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