raftbadgerdb

package module
v0.0.0-...-27c6605 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2020 License: MPL-2.0 Imports: 6 Imported by: 0

README

raft-badgerdb

A copy of raft-boltdb but with BadgerDB instead. BoltDB's buckets are replaced with using key prefixes in BadgerDB (this seems to be common). Reverse iterator is achieved with the suggestion here: https://github.com/dgraph-io/badger/issues/436#issuecomment-398904773

opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
opts.Reverse = true

prefix := dbLogs
it := txn.NewIterator(opts)
defer it.Close()
it.Rewind()
if it.ValidForPrefix(prefix) {
        k := it.Item().Key()
        ret = bytesToUint64(bytes.TrimPrefix(k, dbLogs))
        return nil
}
Bench

First generate results from this repo (and replace Badger with Bolt to be able to use benchcmp):

sevagh:raft-badgerdb $ go test -run=^a -bench=Bench* > raft-badgerdb-results.txt
sevagh:raft-badgerdb $ sed -i 's/Badger/Bolt/g' raft-badgerdb-results.txt

Then clone raft-boltdb and run those benchmarks:

sevagh:raft-boltdb $ go test -run=^a -bench=Bench* > raft-boltdb-results.txt
sevagh:raft-boltdb $ benchcmp ./raft-boltdb-results.txt ../raft-badgerdb/raft-badgerdb-results.txt
benchmark                            old ns/op     new ns/op     delta
BenchmarkBoltStore_FirstIndex-8      560           1432          +155.71%
BenchmarkBoltStore_LastIndex-8       508           1462          +187.80%
BenchmarkBoltStore_GetLog-8          1958          2657          +35.70%
BenchmarkBoltStore_StoreLog-8        24056         19920         -17.19%
BenchmarkBoltStore_StoreLogs-8       27322         49514         +81.22%
BenchmarkBoltStore_DeleteRange-8     26377         2265373       +8488.44%
BenchmarkBoltStore_Set-8             14993         22139         +47.66%
BenchmarkBoltStore_Get-8             766           1103          +43.99%
BenchmarkBoltStore_SetUint64-8       15952         15996         +0.28%
BenchmarkBoltStore_GetUint64-8       747           853           +14.19%

I wouldn't take the numbers seriously, given I may have made some suboptimal implementation choices.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// An error indicating a given key does not exist
	ErrKeyNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type BadgerStore

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

BadgerStore provides access to BadgerDB for Raft to store and retrieve log entries. It also provides key/value storage, and can be used as a LogStore and StableStore.

func New

func New(options badger.Options) (*BadgerStore, error)

New uses the supplied options to open the BadgerDB and prepare it for use as a raft backend.

func NewBadgerStore

func NewBadgerStore(path string) (*BadgerStore, error)

NewBadgerStore takes a file path and returns a connected Raft backend.

func (*BadgerStore) Close

func (b *BadgerStore) Close() error

Close is used to gracefully close the DB connection.

func (*BadgerStore) DeleteRange

func (b *BadgerStore) DeleteRange(min, max uint64) error

DeleteRange is used to delete logs within a given range inclusively.

func (*BadgerStore) FirstIndex

func (b *BadgerStore) FirstIndex() (uint64, error)

func (*BadgerStore) Get

func (b *BadgerStore) Get(k []byte) ([]byte, error)

Get is used to retrieve a value from the k/v store by key

func (*BadgerStore) GetLog

func (b *BadgerStore) GetLog(idx uint64, log *raft.Log) error

GetLog is used to retrieve a log from BadgerDB at a given index.

func (*BadgerStore) GetUint64

func (b *BadgerStore) GetUint64(key []byte) (uint64, error)

GetUint64 is like Get, but handles uint64 values

func (*BadgerStore) LastIndex

func (b *BadgerStore) LastIndex() (uint64, error)

LastIndex returns the last known index from the Raft log.

func (*BadgerStore) Set

func (b *BadgerStore) Set(k, v []byte) error

Set is used to set a key/value set outside of the raft log

func (*BadgerStore) SetUint64

func (b *BadgerStore) SetUint64(key []byte, val uint64) error

SetUint64 is like Set, but handles uint64 values

func (*BadgerStore) StoreLog

func (b *BadgerStore) StoreLog(log *raft.Log) error

StoreLog is used to store a single raft log

func (*BadgerStore) StoreLogs

func (b *BadgerStore) StoreLogs(logs []*raft.Log) error

StoreLogs is used to store a set of raft logs

func (*BadgerStore) Sync

func (b *BadgerStore) Sync() error

Sync performs an fsync on the database file handle. This is not necessary under normal operation unless NoSync is enabled, in which this forces the database file to sync against the disk.

Jump to

Keyboard shortcuts

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