fsm

package
v0.0.0-...-73ed01f Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2021 License: MIT Imports: 5 Imported by: 0

README

fsm

FSM stands for Finite State Machine

The fsm package contains the RavelFSM and Snapshot structs that implements the FSM interface and the FSMSnapshot interface respectively which are required for constructing a new raft node in the hashicorp/raft library

These interfaces are responsible for actually "applying" the log entries to our BadgerDB instance in a persistent manner.

RavelFSM

This struct implements the FSM interface. This interface makes use of the replicated log to "apply" logs, take snapshots and "restore" from snapshots.

  • The Apply function is invoked once a log entry is committed and is responsible for storing the data to the BadgerDB instance. It checks the type of operation it is required to perform and then calls the corresponding function from the db package.

  • The Snapshot function is used for log compaction. Its returns a Snapshot object (a struct which implements the FSMSnapshot interface) which is used to save a snapshot of the FSM at that point in time i.e. its takes the state of the DB and creates a copy of the state so that previous logs can be deleted.

  • The Restore function is used to restore an FSM from a snapshot i.e. restore the state of the DB to when the snapshot was taken thereby discarding all the previous states. This can be done very easily using BadgerDB's inbuilt functions, first we drop all the current keys using the DropAll function and then call the Load function to restore the snapshot from backup.

Snapshot

This struct implements the FSMSnapshot interface. Snapshot is returned by an FSM in response to a Snapshot call. This interface is responsible for dumping the current state of the DB i.e. snapshot of the FSM to the WriteCloser sink which stored by the raft lib.

  • Persist is the main function which dumps the snapshot to the sink. We do this by simply taking a backup of our db and writing it into the sink.

  • Release is called when we are finished with the snapshot and all the data has been safely dumped.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValue

type KeyValue struct {
	Key   []byte `json:"key"`
	Value []byte `json:"value"`
}

type LogData

type LogData struct {
	Operation string `json:"Operation"`
	Key       []byte `json:"Key"`
	Value     []byte `json:"Value"`
}

LogData represents the structure of individual commands on the Logs

type RavelFSM

type RavelFSM struct {
	Db *db.RavelDatabase
}

RavelFSM implements the raft.FSM interface. It represents the Finite State Machine in a RavelNode. The individual logs are "applied" on the FSM on receiving the commit RPC from the leader.

func NewFSM

func NewFSM(path string) (*RavelFSM, error)

NewFSM creates an instance of RavelFSM

func (*RavelFSM) Apply

func (f *RavelFSM) Apply(l *raft.Log) interface{}

Apply commits the given log to the database.

func (*RavelFSM) Close

func (f *RavelFSM) Close()

Close will close the connection to the internal db.RavelDatabase instance

func (*RavelFSM) Get

func (f *RavelFSM) Get(key []byte) ([]byte, error)

Get returns the value for the provided key

func (*RavelFSM) GetAndDelete

func (f *RavelFSM) GetAndDelete(key []byte) ([]byte, error)

Get returns the value for the provided key

func (*RavelFSM) Restore

func (f *RavelFSM) Restore(r io.ReadCloser) error

Restore restores from the data from the last captured snapshot

func (*RavelFSM) Snapshot

func (f *RavelFSM) Snapshot() (raft.FSMSnapshot, error)

Snapshot returns an raft.FSMSnapshot which captures a snapshot of the data at that moment in time

type Snapshot

type Snapshot struct {
	Db *db.RavelDatabase
}

Snapshot implements the raft.Snapshot interface

func (*Snapshot) Persist

func (f *Snapshot) Persist(sink raft.SnapshotSink) error

Persist writes a backup of the db to the sink

func (*Snapshot) Release

func (f *Snapshot) Release()

Release releases the snapshot

Jump to

Keyboard shortcuts

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