Skip to content

Commit

Permalink
Merge pull request #80 from aalda/rocksdb
Browse files Browse the repository at this point in the history
Use rocksdb as default store
  • Loading branch information
aalda authored Mar 14, 2019
2 parents cdaf43e + 0fbf825 commit 2951295
Show file tree
Hide file tree
Showing 45 changed files with 2,530 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ tests/cloud/aws/outputs.tf
tests/cloud/aws/results
tests/cloud/aws/to_upload/rendered
tests/results
c-deps/libs

# Documents
*.pdf
TODO.md
Expand Down
11 changes: 11 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[submodule "github.com/facebook/rocksdb"]
path = c-deps/rocksdb
url = https://github.com/facebook/rocksdb.git

[submodule "github.com/google/snappy"]
path = c-deps/snappy
url = https://github.com/google/snappy.git

[submodule "github.com/jemalloc/jemalloc"]
path = c-deps/jemalloc
url = https://github.com/jemalloc/jemalloc.git
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ and verifing events. This example runs a standalone server. You can add any
source of ordered events like logs, ledgers, etc...

### Standalone example
- Download the software and its dependencies
- Download the software and its dependencies (including rocksdb)
```
go get -v -u -d github.com/bbva/qed/...
$ git clone https://github.com/bbva/qed $GOPATH/src/github.com/bbva/qed
$ cd $GOPATH/src/github.com/bbva/qed
$ git submodule update --init --recursive
$ cd c-deps
$ ./builddeps.sh
$ cd ..
$ CGO_LDFLAGS_ALLOW='.*' go build
```
- Start the standalone server

Expand Down
12 changes: 4 additions & 8 deletions api/apihttp/apihttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/protocol"
"github.com/bbva/qed/raftwal"
"github.com/bbva/qed/storage/badger"
"github.com/bbva/qed/testutils/rand"
storage_utils "github.com/bbva/qed/testutils/storage"
assert "github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -372,18 +372,14 @@ func newNodeBench(b *testing.B, id int) (*raftwal.RaftBalloon, func()) {
badgerPath := fmt.Sprintf("/var/tmp/raft-test/node%d/badger", id)

os.MkdirAll(badgerPath, os.FileMode(0755))
badger, err := badger.NewBadgerStore(badgerPath)
assert.NoError(b, err)
rocks, closeF := storage_utils.OpenRocksDBStore(b, badgerPath)

raftPath := fmt.Sprintf("/var/tmp/raft-test/node%d/raft", id)
os.MkdirAll(raftPath, os.FileMode(0755))
r, err := raftwal.NewRaftBalloon(raftPath, ":8301", fmt.Sprintf("%d", id), badger, make(chan *protocol.Snapshot))
r, err := raftwal.NewRaftBalloon(raftPath, ":8301", fmt.Sprintf("%d", id), rocks, make(chan *protocol.Snapshot))
assert.NoError(b, err)

return r, func() {
fmt.Println("Removing node folder")
os.RemoveAll(fmt.Sprintf("/var/tmp/raft-test/node%d", id))
}
return r, closeF

}
func BenchmarkApiAdd(b *testing.B) {
Expand Down
35 changes: 34 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ variables:
GOPATH: '$(system.defaultWorkingDirectory)/gopath' # Go workspace path
modulePath: '$(GOPATH)/src/github.com/$(build.repository.name)' # Path to the module's code
GO111MODULE: on
CGO_LDFLAGS_ALLOW: '.*'

steps:
- script: |
Expand All @@ -36,11 +37,43 @@ steps:
openssl x509 -req -days 365 -sha256 -in $HOME/.ssh/server.csr -CA $HOME/.ssh/ca.crt -CAkey $HOME/.ssh/ca.key -CAcreateserial -out $HOME/.ssh/server.crt -extfile <(echo subjectAltName = IP:127.0.0.1)
displayName: 'Generate certificates'

- script: |
git submodule update --init --recursive
cd c-deps
./builddeps.sh
cd ..
workingDirectory: '$(modulePath)'
displayName: 'Build rocksdb'

- script: |
GO111MODULE=auto go get github.com/jstemmer/go-junit-report
GO111MODULE=auto go get github.com/axw/gocov/gocov
GO111MODULE=auto go get github.com/AlekSi/gocov-xml
GO111MODULE=auto go get gopkg.in/matm/v1/gocov-html
workingDirectory: '$(modulePath)'
displayName: 'Download code coverage tools'

- script: |
go version
go mod download
go test -v -coverprofile=coverage.txt -covermode=atomic ./...
go test -v -coverprofile=coverage.txt -covermode=count ./... 2>&1 | tee coverage.out
cat coverage.out | go-junit-report > report.xml
go vet -composites=false ./...
gocov convert coverage.txt > coverage.json
gocov-xml < coverage.json > coverage.xml
mkdir coverage
gocov-html < coverage.json > coverage/index.html
workingDirectory: '$(modulePath)'
displayName: 'Get dependencies, then build'

- task: PublishTestResults@2
inputs:
testRunner: JUnit
testResultsFiles: $(System.DefaultWorkingDirectory)/**/report.xml
failOnStandardError: 'true'

- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(System.DefaultWorkingDirectory)/**/coverage.xml
reportDirectory: $(System.DefaultWorkingDirectory)/**/coverage
54 changes: 49 additions & 5 deletions balloon/balloon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestConsistencyProofVerify(t *testing.T) {
func TestAddQueryAndVerify(t *testing.T) {
log.SetLogger("TestCacheWarmingUp", log.SILENT)

store, closeF := storage_utils.OpenBadgerStore(t, "/var/tmp/balloon.test.1")
store, closeF := storage_utils.OpenRocksDBStore(t, "/var/tmp/balloon.test.1")
defer closeF()

// start balloon
Expand All @@ -212,7 +212,7 @@ func TestCacheWarmingUp(t *testing.T) {

log.SetLogger("TestCacheWarmingUp", log.SILENT)

store, closeF := storage_utils.OpenBadgerStore(t, "/var/tmp/ballon_test.db")
store, closeF := storage_utils.OpenRocksDBStore(t, "/var/tmp/ballon_test.db")
defer closeF()

// start balloon
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestCacheWarmingUp(t *testing.T) {
func TestTamperAndVerify(t *testing.T) {
log.SetLogger("TestTamperAndVerify", log.SILENT)

store, closeF := storage_utils.OpenBadgerStore(t, "/var/tmp/balloon.test.2")
store, closeF := storage_utils.OpenRocksDBStore(t, "/var/tmp/balloon.test.2")
defer closeF()

b, err := NewBalloon(store, hashing.NewSha256Hasher)
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestTamperAndVerify(t *testing.T) {
func TestDeleteAndVerify(t *testing.T) {
log.SetLogger("TestDeleteAndVerify", log.SILENT)

store, closeF := storage_utils.OpenBadgerStore(t, "/var/tmp/balloon.test.3")
store, closeF := storage_utils.OpenRocksDBStore(t, "/var/tmp/balloon.test.3")
defer closeF()

b, err := NewBalloon(store, hashing.NewSha256Hasher)
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestDeleteAndVerify(t *testing.T) {
func TestGenIncrementalAndVerify(t *testing.T) {
log.SetLogger("TestDeleteAndVerify", log.SILENT)

store, closeF := storage_utils.OpenBadgerStore(t, "/var/tmp/balloon.test.3")
store, closeF := storage_utils.OpenRocksDBStore(t, "/var/tmp/balloon.test.3")
defer closeF()

b, err := NewBalloon(store, hashing.NewSha256Hasher)
Expand Down Expand Up @@ -384,3 +384,47 @@ func BenchmarkQueryBadger(b *testing.B) {
}

}
func BenchmarkAddRocksDB(b *testing.B) {

log.SetLogger("BenchmarkAddRocksDB", log.SILENT)

store, closeF := storage_utils.OpenRocksDBStore(b, "/var/tmp/balloon_bench.db")
defer closeF()

balloon, err := NewBalloon(store, hashing.NewSha256Hasher)
require.NoError(b, err)

b.ResetTimer()
b.N = 100000
for i := 0; i < b.N; i++ {
event := rand.Bytes(128)
_, mutations, _ := balloon.Add(event)
store.Mutate(mutations)
}

}

func BenchmarkQueryRocksDB(b *testing.B) {
var events [][]byte
log.SetLogger("BenchmarkQueryRocksDB", log.SILENT)

store, closeF := storage_utils.OpenRocksDBStore(b, "/var/tmp/ballon_bench.db")
defer closeF()

balloon, err := NewBalloon(store, hashing.NewSha256Hasher)
require.NoError(b, err)

b.N = 100000
for i := 0; i < b.N; i++ {
event := rand.Bytes(128)
events = append(events, event)
_, mutations, _ := balloon.Add(event)
store.Mutate(mutations)
}

b.ResetTimer()
for i, e := range events {
balloon.QueryMembership(e, uint64(i))
}

}
2 changes: 1 addition & 1 deletion balloon/history/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func BenchmarkAdd(b *testing.B) {

log.SetLogger("BenchmarkAdd", log.SILENT)

store, closeF := storage_utils.OpenBadgerStore(b, "/var/tmp/history_tree_test.db")
store, closeF := storage_utils.OpenRocksDBStore(b, "/var/tmp/history_tree_test.db")
defer closeF()

tree := NewHistoryTree(hashing.NewSha256Hasher, store, 300)
Expand Down
2 changes: 1 addition & 1 deletion balloon/hyper/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func BenchmarkAdd(b *testing.B) {

log.SetLogger("BenchmarkAdd", log.SILENT)

store, closeF := storage_utils.OpenBadgerStore(b, "/var/tmp/hyper_tree_test.db")
store, closeF := storage_utils.OpenRocksDBStore(b, "/var/tmp/hyper_tree_test.db")
defer closeF()

hasher := hashing.NewSha256Hasher()
Expand Down
47 changes: 47 additions & 0 deletions c-deps/builddeps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

set -e

BASE=$(pwd)
LIBS="$BASE/libs"
mkdir -p $LIBS

# build jemalloc
if [ ! -f $LIBS/libjemalloc.so.2 ]; then
cd jemalloc
bash autogen.sh
make -j8
cp lib/libjemalloc.a ../libs
cd ../libs
fi

cd $BASE

# build snappy shared lib
if [ ! -f $LIBS/libsnappy.so.1.1.7 ]; then
cd snappy
mkdir -p build
cd build
cmake ../
# sed -i.bak s/BUILD_SHARED_LIBS:BOOL=OFF/BUILD_SHARED_LIBS:BOOL=ON/g CMakeCache.txt
make -j8

cp libsnappy.a ../../libs/
cp snappy-stubs-public.h ../
cd ../../libs/
fi

cd $BASE

# build rocksdb shared with those libraries
cd rocksdb
mkdir -p build
cd build

cmake -DWITH_GFLAGS=OFF -DPORTABLE=ON \
-DWITH_SNAPPY=ON -DSNAPPY_LIBRARIES="$LIBS/libsnappy.a" -DSNAPPY_INCLUDE_DIR="$BASE/snappy" \
-DWITH_JEMALLOC=ON -DJEMALLOC_LIBRARIES="$LIBS/libjemalloc.a" -DJEMALLOC_INCLUDE_DIR="$BASE/jemalloc/include" \
-DCMAKE_BUILD_TYPE=Release -DUSE_RTTI=1 ../
make -j8 rocksdb

cp librocksdb.a ../../libs
11 changes: 11 additions & 0 deletions c-deps/gitsubmodules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e

git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
while read path_key path
do
url_key=$(echo $path_key | sed 's/\.path/.url/')
url=$(git config -f .gitmodules --get "$url_key")
git submodule add $url $path
done
1 change: 1 addition & 0 deletions c-deps/jemalloc
Submodule jemalloc added at 61efbd
1 change: 1 addition & 0 deletions c-deps/rocksdb
Submodule rocksdb added at 641fae
1 change: 1 addition & 0 deletions c-deps/snappy
Submodule snappy added at b02bfa
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/coocood/freecache v1.1.0
github.com/dgraph-io/badger v1.5.4
github.com/golang/protobuf v1.2.0
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c
github.com/hashicorp/go-msgpack v0.5.3
github.com/hashicorp/logutils v1.0.0
Expand Down
8 changes: 4 additions & 4 deletions raftwal/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,20 @@ func (fsm *BalloonFSM) Snapshot() (raft.FSMSnapshot, error) {
fsm.restoreMu.Lock()
defer fsm.restoreMu.Unlock()

version, err := fsm.store.GetLastVersion()
id, err := fsm.store.Snapshot()
if err != nil {
return nil, err
}
log.Debugf("Generating snapshot until version: %d (balloon version %d)", version, fsm.balloon.Version())
log.Debugf("Generating snapshot until version: %d (balloon version %d)", id, fsm.balloon.Version())

// Copy the node metadata.
meta, err := json.Marshal(fsm.meta)
if err != nil {
log.Debugf("failed to encode meta for snapshot: %s", err.Error())
return nil, err
}

return &fsmSnapshot{lastVersion: version, store: fsm.store, meta: meta}, nil
// change lastVersion by checkpoint structure
return &fsmSnapshot{id: id, store: fsm.store, meta: meta}, nil
}

// Restore restores the node to a previous state.
Expand Down
8 changes: 4 additions & 4 deletions raftwal/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import (
)

type fsmSnapshot struct {
lastVersion uint64
store storage.ManagedStore
meta []byte
id uint64
store storage.ManagedStore
meta []byte
}

// Persist writes the snapshot to the given sink.
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
log.Debug("Persisting snapshot...")
err := func() error {
if err := f.store.Backup(sink, f.lastVersion); err != nil {
if err := f.store.Backup(sink, f.id); err != nil {
return err
}
return sink.Close()
Expand Down
Loading

0 comments on commit 2951295

Please sign in to comment.