Documentation ¶
Overview ¶
Package stateio implements persistent state mechanism based on log files that interleave indexed state snapshots with state updates. Users maintain state by interaction with Reader and Writer objects. A typical application should reconcile to the current state before writing new log entries. New log entries should be written only after they are known to apply cleanly. (In the following examples, error handling is left as an exercise to the reader):
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE) state, epoch, updates, err := stateio.RestoreFile(file) application.Reset(state) for { entry, err := updates.Read() if err == io.EOF { break } application.Update(entry) } w, err := stateio.NewFileWriter(file) // Apply new state updates: var update []byte = ... application.Update(update) err := w.Update(update) // Produce a new snapshot: var snap []byte = application.Snapshot() w.Snapshot(snap)
Data format ¶
State files maintained by package stateio builds on package logio. The log file contains a sequence of epochs, each beginning with a state snapshot (with the exception of the first epoch, which does not need a state snapshot). Each entry is prefixed with the type of the entry as well as the the epoch to which the entry belongs. Snapshot entries are are prefixed with the previous epoch, so that log files can efficiently rewound.
TODO(marius): support log file truncation
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCorrupt = errors.New("corrupt state entry")
ErrCorrupt is returned when a corrupted log is encountered.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads a single epoch state updates.
func Restore ¶
Restore restores the state from the last epoch in the state log read by the provided reader and the given limit. The returned state may be nil if no snapshot was defined for the epoch.
func RestoreFile ¶
RestoreFile is a convenience function that restores the file from the provided os file.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes snapshots and update entries to an underlying log stream.
func NewFileWriter ¶
NewFileWriter initializes a state log writer from the provided os file. The file's contents is committed to stable storage after each log write.
func NewWriter ¶
NewWriter initializes and returns a new state log writer which writes to the stream w, which is positioned at the provided offset. The provided epoch must be the current epoch of the log file. If the provided io.Writer is also a syncer:
type Syncer interface { Sync() error }
Then Sync() is called (and errors returned) after each log entry has been written.