const metadataType … const entryType … const stateType … const crcType … const snapshotType … const warnSyncDuration … var SegmentSizeBytes … var ErrMetadataConflict … var ErrFileNotFound … var ErrCRCMismatch … var ErrSnapshotMismatch … var ErrSnapshotNotFound … var ErrSliceOutOfRange … var ErrDecoderNotFound … var crcTable … type WAL … // Create creates a WAL ready for appending records. The given metadata is // recorded at the head of each WAL file, and can be retrieved with ReadAll // after the file is Open. func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) { … } func (w *WAL) SetUnsafeNoFsync() { … } func (w *WAL) cleanupWAL(lg *zap.Logger) { … } func (w *WAL) renameWAL(tmpdirpath string) (*WAL, error) { … } func (w *WAL) renameWALUnlock(tmpdirpath string) (*WAL, error) { … } // Open opens the WAL at the given snap. // The snap SHOULD have been previously saved to the WAL, or the following // ReadAll will fail. // The returned WAL is ready to read and the first record will be the one after // the given snap. The WAL cannot be appended to before reading out all of its // previous records. func Open(lg *zap.Logger, dirpath string, snap walpb.Snapshot) (*WAL, error) { … } // OpenForRead only opens the wal files for read. // Write on a read only wal panics. func OpenForRead(lg *zap.Logger, dirpath string, snap walpb.Snapshot) (*WAL, error) { … } func openAtIndex(lg *zap.Logger, dirpath string, snap walpb.Snapshot, write bool) (*WAL, error) { … } func selectWALFiles(lg *zap.Logger, dirpath string, snap walpb.Snapshot) ([]string, int, error) { … } func openWALFiles(lg *zap.Logger, dirpath string, names []string, nameIndex int, write bool) ([]fileutil.FileReader, []*fileutil.LockedFile, func() error, error) { … } // ReadAll reads out records of the current WAL. // If opened in write mode, it must read out all records until EOF. Or an error // will be returned. // If opened in read mode, it will try to read all records if possible. // If it cannot read out the expected snap, it will return ErrSnapshotNotFound. // If loaded snap doesn't match with the expected one, it will return // all the records and error ErrSnapshotMismatch. // TODO: detect not-last-snap error. // TODO: maybe loose the checking of match. // After ReadAll, the WAL will be ready for appending new records. // // ReadAll suppresses WAL entries that got overridden (i.e. a newer entry with the same index // exists in the log). Such a situation can happen in cases described in figure 7. of the // RAFT paper (http://web.stanford.edu/~ouster/cgi-bin/papers/raft-atc14.pdf). // // ReadAll may return uncommitted yet entries, that are subject to be overriden. // Do not apply entries that have index > state.commit, as they are subject to change. func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.Entry, err error) { … } // ValidSnapshotEntries returns all the valid snapshot entries in the wal logs in the given directory. // Snapshot entries are valid if their index is less than or equal to the most recent committed hardstate. func ValidSnapshotEntries(lg *zap.Logger, walDir string) ([]walpb.Snapshot, error) { … } // Verify reads through the given WAL and verifies that it is not corrupted. // It creates a new decoder to read through the records of the given WAL. // It does not conflict with any open WAL, but it is recommended not to // call this function after opening the WAL for writing. // If it cannot read out the expected snap, it will return ErrSnapshotNotFound. // If the loaded snap doesn't match with the expected one, it will // return error ErrSnapshotMismatch. func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) (*raftpb.HardState, error) { … } // cut closes current file written and creates a new one ready to append. // cut first creates a temp wal file and writes necessary headers into it. // Then cut atomically rename temp wal file to a wal file. func (w *WAL) cut() error { … } func (w *WAL) sync() error { … } func (w *WAL) Sync() error { … } // ReleaseLockTo releases the locks, which has smaller index than the given index // except the largest one among them. // For example, if WAL is holding lock 1,2,3,4,5,6, ReleaseLockTo(4) will release // lock 1,2 but keep 3. ReleaseLockTo(5) will release 1,2,3 but keep 4. func (w *WAL) ReleaseLockTo(index uint64) error { … } // Close closes the current WAL file and directory. func (w *WAL) Close() error { … } func (w *WAL) saveEntry(e *raftpb.Entry) error { … } func (w *WAL) saveState(s *raftpb.HardState) error { … } func (w *WAL) Save(st raftpb.HardState, ents []raftpb.Entry) error { … } func (w *WAL) SaveSnapshot(e walpb.Snapshot) error { … } func (w *WAL) saveCrc(prevCrc uint32) error { … } func (w *WAL) tail() *fileutil.LockedFile { … } func (w *WAL) seq() uint64 { … } func closeAll(lg *zap.Logger, rcs ...io.ReadCloser) error { … }