Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Replace LevelDB with BadgerDB #2141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
75 changes: 18 additions & 57 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,28 @@ module github.com/ethersphere/swarm
go 1.13

require (
bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898
github.com/Azure/azure-pipeline-go v0.0.0-20180607212504-7571e8eb0876 // indirect
github.com/Azure/azure-storage-blob-go v0.0.0-20180712005634-eaae161d9d5e
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.13 // indirect
github.com/Microsoft/hcsshim v0.8.6 // indirect
bazil.org/fuse v0.0.0-20200117225306-7b5117fecadc
github.com/VividCortex/ewma v1.1.1 // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/containerd/containerd v1.2.7 // indirect
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v0.7.3-0.20190806133308-ecdb0b22393b
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/ethereum/go-ethereum v1.9.2
github.com/ethersphere/go-sw3 v0.2.1
github.com/fatih/color v1.7.0 // indirect
github.com/dgraph-io/badger v1.6.0
github.com/ethereum/go-ethereum v1.9.12
github.com/ethersphere/go-sw3 v0.2.3
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc
github.com/go-kit/kit v0.9.0 // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/googleapis/gnostic v0.0.0-20190624222214-25d8b0b66985 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/hashicorp/golang-lru v0.5.3
github.com/json-iterator/go v1.1.7 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.2
github.com/mattn/go-isatty v0.0.8
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/naoina/toml v0.0.0-20170918210437-9fafd6967416
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4
github.com/mattn/go-colorable v0.1.6
github.com/mattn/go-isatty v0.0.12
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/opentracing/opentracing-go v1.1.0
github.com/pborman/uuid v0.0.0-20170112150404-1b00554d8222
github.com/peterh/liner v0.0.0-20190123174540-a2c9a5303de7 // indirect
github.com/rnsdomains/rns-go-lib v0.0.0-20191114120302-3505575b0b8f
github.com/robertkrimen/otto v0.0.0-20170205013659-6a77b7cbc37d // indirect
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00
github.com/syndtr/goleveldb v0.0.0-20190318030020-c3a204f8e965
github.com/pborman/uuid v1.2.0
github.com/rnsdomains/rns-go-lib v0.0.0-20200116145323-7d17f149733e
github.com/rs/cors v1.7.0
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d
github.com/tilinna/clock v1.0.2
github.com/uber-go/atomic v1.4.0 // indirect
github.com/uber/jaeger-client-go v0.0.0-20180607151842-f7e0d4744fa6
github.com/uber/jaeger-lib v0.0.0-20180615202729-a51202d6f4a7 // indirect
github.com/uber/jaeger-client-go v2.22.1+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
github.com/vbauerster/mpb v3.4.0+incompatible
go.uber.org/atomic v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/grpc v1.22.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
go.uber.org/atomic v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df
golang.org/x/net v0.0.0-20200320220750-118fecf932d8
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
gopkg.in/urfave/cli.v1 v1.20.0
gotest.tools v2.2.0+incompatible // indirect
k8s.io/api v0.0.0-20190703205437-39734b2a72fe
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76
k8s.io/client-go v0.0.0-20190706005506-4ed54556a14a
)
377 changes: 159 additions & 218 deletions go.sum

Large diffs are not rendered by default.

38 changes: 28 additions & 10 deletions p2p/protocols/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
"encoding/binary"
"time"

"github.com/dgraph-io/badger"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"

"github.com/syndtr/goleveldb/leveldb"
)

//AccountMetrics abstracts away the metrics DB and
Expand Down Expand Up @@ -51,7 +50,7 @@ func (am *AccountingMetrics) Close() {
type reporter struct {
reg metrics.Registry //the registry for these metrics (independent of other metrics)
interval time.Duration //duration at which the reporter will persist metrics
db *leveldb.DB //the actual DB
db *badger.DB //the actual DB
quit chan struct{} //quit the reporter loop
done chan struct{} //signal that reporter loop is done
}
Expand All @@ -62,8 +61,10 @@ func NewAccountingMetrics(r metrics.Registry, d time.Duration, path string) *Acc
var val = make([]byte, 8)
var err error

//Create the LevelDB
db, err := leveldb.OpenFile(path, nil)
//Create the badger db
opt := badger.DefaultOptions(path)
opt.Logger = nil
db, err := badger.Open(opt)
if err != nil {
log.Error(err.Error())
return nil
Expand All @@ -84,7 +85,19 @@ func NewAccountingMetrics(r metrics.Registry, d time.Duration, path string) *Acc
}
//iterate the map and get the values
for key, metric := range metricsMap {
val, err = db.Get([]byte(key), nil)
var value []byte
err = db.View(func(txn *badger.Txn) (err error) {
item, err := txn.Get([]byte(key))
if err != nil {
return err
}
return item.Value(func(val []byte) error {
value = make([]byte, len(val))
copy(value, val)
metrics.GetOrRegisterCounter("DB.get", nil).Inc(1)
return nil
})
})
//until the first time a value is being written,
//this will return an error.
//it could be beneficial though to log errors later,
Expand Down Expand Up @@ -143,8 +156,8 @@ func (r *reporter) run() {

//send the metrics to the DB
func (r *reporter) save() error {
//create a LevelDB Batch
batch := leveldb.Batch{}
//create a badger transaction
batch := r.db.NewTransaction(true)
//for each metric in the registry (which is independent)...
r.reg.Each(func(name string, i interface{}) {
metric, ok := i.(metrics.Counter)
Expand All @@ -155,8 +168,13 @@ func (r *reporter) save() error {
byteVal := make([]byte, 8)
binary.BigEndian.PutUint64(byteVal, uint64(ms.Count()))
//...and save the value to the DB
batch.Put([]byte(name), byteVal)
batch.Set([]byte(name), byteVal)
}
})
return r.db.Write(&batch, nil)
err := batch.Commit()
if err != nil {
return err
}
batch.Discard()
return nil
}
Loading