Documentation
¶
Overview ¶
Package raft provides a simple key-value store coordinated across a raft cluster.
Index ¶
- Variables
- type Getter
- type Service
- func (sv *Service) AddAllowedMember(ctx context.Context, addr string) error
- func (sv *Service) Delete(ctx context.Context, key string) error
- func (sv *Service) Err() error
- func (sv *Service) Get(ctx context.Context, key string) ([]byte, error)
- func (sv *Service) Insert(ctx context.Context, key string, val []byte) error
- func (sv *Service) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (sv *Service) Set(ctx context.Context, key string, val []byte) error
- func (sv *Service) Stale() Getter
Constants ¶
This section is empty.
Variables ¶
var ErrUnsatisfied = errors.New("precondition not satisfied")
Functions ¶
This section is empty.
Types ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service holds the key-value data and performs raft coordination.
func Start ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.