raftbadger

package module
v0.0.0-...-f874ad7 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 11 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

raft-badger

raft-badger implements LogStore and StableStore Interface of hashicorp/raft.

raft-badger in hascorp/raft

links

hashicorp api

hashicorp raft.LogStore

raft-badger implements LogStore interface of hashicorp/raft.

type LogStore interface {
	// FirstIndex returns the first index written. 0 for no entries.
	FirstIndex() (uint64, error)

	// LastIndex returns the last index written. 0 for no entries.
	LastIndex() (uint64, error)

	// GetLog gets a log entry at a given index.
	GetLog(index uint64, log *Log) error

	// StoreLog stores a log entry.
	StoreLog(log *Log) error

	// StoreLogs stores multiple log entries.
	StoreLogs(logs []*Log) error

	// DeleteRange deletes a range of log entries. The range is inclusive.
	DeleteRange(min, max uint64) error
}
hashicorp raft.StableStore

raft-badger implements StableStore interface of hashicorp/raft.

type StableStore interface {
	Set(key []byte, val []byte) error

	// Get returns the value for key, or an empty byte slice if key was not found.
	Get(key []byte) ([]byte, error)

	SetUint64(key []byte, val uint64) error

	// GetUint64 returns the uint64 value for key, or 0 if key was not found.
	GetUint64(key []byte) (uint64, error)
}

installation

go get -u github.com/rfyiamcool/raft-badger

performance

StoreLogs

store logs qps = 10w per second

goos: darwin
pkg: github.com/rfyiamcool/raft-badger
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkStoreLogs
BenchmarkStoreLogs-12    	   99162	     11767 ns/op	    5032 B/op	      67 allocs/op

GetLog

get log qps = 30w per second

pkg: github.com/rfyiamcool/raft-badger
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkGetLog
BenchmarkGetLog-12    	  307989	      3362 ns/op	    1717 B/op	      41 allocs/op

usage with hashicorp-raft

import (
	"os"
	"fmt"

	"github.com/hashicorp/raft"
	"github.com/dgraph-io/badger/v3/options"

	raftbadger "github.com/rfyiamcool/raft-badger"
)

func main() {
	var (
		logStore raft.LogStore
		stableStore raft.StableStore
	)

	cfg = raftbadger.Config{
		DataPath: "/tmp/raft",
		Compression: "zstd", // zstd, snappy
		DisableLogger: true,
	}

	opts := badger.DefaultOptions(cfg.DataPath)
	badgerDB, err := raftbadger.New(cfg, &opts)
	if err != nil {
		fmt.Println("fail to create new badger sotrage, err: %s", err.Error())
		os.Exit(1)
	}

	logStore = badgerDB
	stableStore = badgerDB

	r, err := raft.NewRaft(config, (*fsm)(s), logStore, stableStore, snapshots, transport)
	...
}

raft-badger usage

example code in example/main.go

package main

import (
	"fmt"

	"github.com/hashicorp/raft"
	raftbadger "github.com/rfyiamcool/raft-badger"
)

func main() {
	cfg := raftbadger.Config{
		DataPath: "/tmp/raft",
	}
	store, err := raftbadger.New(cfg, nil)
	if err != nil {
		panic(fmt.Sprintf("failed to create raft badger storage, err: %s", err.Error()))
	}

	// test dropall api
	err = store.DropAll()
	checkError(err)

	// test set api
	err = store.Set([]byte("blog"), []byte("xiaorui.cc"))
	checkError(err)

	// test get api
	value, err := store.Get([]byte("blog"))
	checkError(err)
	fmt.Println("value is ", string(value))

	// test SetUint64 api
	err = store.SetUint64([]byte("index"), 111)
	checkError(err)

	// test GetUint64 api
	index, err := store.GetUint64([]byte("index"))
	checkError(err)
	fmt.Println("index is ", index)

	var logs []*raft.Log
	for i := 0; i < 20; i++ {
		logs = append(logs, &raft.Log{
			Index: uint64(i),
			Term:  10,
			Type:  0,
			Data:  []byte("a"),
		})
	}

	// test StoreLogs api
	err = store.StoreLogs(logs)
	checkError(err)

	for i := 0; i < 10; i++ {
		logptr := new(raft.Log)
		err = store.GetLog(uint64(i), logptr)
		checkError(err)
		fmt.Printf("the index of the No.%v log is %v\n", i, logptr.Index)
	}

	err = store.DeleteRange(0, 10)
	checkError(err)

	for i := 0; i < 10; i++ {
		logptr := new(raft.Log)
		err = store.GetLog(uint64(i), logptr)
		if err != nil {
			fmt.Printf("not found the No.%v log\n", i)
		}
		if err == nil {
			panic(fmt.Sprintf("found No.%v log, but tne log is deleted \n", i))
		}
	}

	logptr := new(raft.Log)
	err = store.GetLog(11, logptr)
	checkError(err)
	fmt.Printf("get No.11 log, index is %v\n", logptr.Index)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// not found the key
	ErrNotFoundKey        = raft.ErrLogNotFound
	ErrNotFoundFirstIndex = errors.New("not found first index")
	ErrNotFoundLastIndex  = errors.New("not found last index")
)

Functions

This section is empty.

Types

type Storage

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

Storage

func New

func New(log *slog.Logger, path string) (*Storage, error)

New badger storage object with config and badger options.

func (*Storage) Close

func (s *Storage) Close() error

Close the storage connection

func (*Storage) Delete

func (s *Storage) Delete(key []byte) error

Delete kv of the key

func (*Storage) DeleteFiles

func (s *Storage) DeleteFiles()

DeleteFiles delete badgerDB files

func (*Storage) DeleteRange

func (s *Storage) DeleteRange(min, max uint64) error

DeleteRange is used to delete logs within a given range.

func (*Storage) DropAll

func (s *Storage) DropAll() error

DropAll drop all kvs in the badger db.

func (*Storage) FirstIndex

func (s *Storage) FirstIndex() (uint64, error)

FirstIndex get the first index from the Raft log.

func (*Storage) Get

func (s *Storage) Get(key []byte) ([]byte, error)

Get is used to get value by key

func (*Storage) GetDB

func (s *Storage) GetDB() *badger.DB

GetDB return badger database instance.

func (*Storage) GetLog

func (s *Storage) GetLog(idx uint64, log *raft.Log) error

GetLog is used to get a log from Badger by a given index.

func (*Storage) GetUint64

func (s *Storage) GetUint64(key []byte) (uint64, error)

GetUint64 returns uint64 value of key

func (*Storage) LastIndex

func (s *Storage) LastIndex() (uint64, error)

LastIndex get the last index from the Raft log.

func (*Storage) Set

func (s *Storage) Set(key, val []byte) error

Set is used to set kv

func (*Storage) SetUint64

func (s *Storage) SetUint64(key []byte, val uint64) error

SetUint64 key and val

func (*Storage) StoreLog

func (s *Storage) StoreLog(log *raft.Log) error

StoreLog is used to store a single raft log

func (*Storage) StoreLogs

func (s *Storage) StoreLogs(logs []*raft.Log) error

StoreLogs is used to store a set of raft logs

func (*Storage) Sync

func (s *Storage) Sync() error

Sync db to disk

Jump to

Keyboard shortcuts

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