Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New logging system #164

Merged
merged 7 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/apihttp/apihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,19 +565,19 @@ func GetReqSanitizer(w http.ResponseWriter, r *http.Request) (http.ResponseWrite

// LogHandler Logs the Http Status for a request into fileHandler and returns a
// httphandler function which is a wrapper to log the requests.
func LogHandler(handle http.Handler) http.HandlerFunc {
func LogHandler(handle http.Handler, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
start := time.Now()
writer := statusWriter{w, 0, 0}
handle.ServeHTTP(&writer, request)
latency := time.Now().Sub(start)

log.Debugf("Request: lat %d %+v", latency, request)
logger.Debugf("Request: lat %d %+v", latency, request)
if writer.status >= 400 && writer.status < 500 {
log.Infof("Bad Request: %d %+v", latency, request)
logger.Infof("Bad Request: %d %+v", latency, request)
}
if writer.status >= 500 {
log.Infof("Server error: %d %+v", latency, request)
logger.Infof("Server error: %d %+v", latency, request)
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions balloon/balloon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/bbva/qed/balloon/history"
"github.com/bbva/qed/balloon/hyper"
"github.com/bbva/qed/crypto/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/storage"
"github.com/bbva/qed/util"
)
Expand All @@ -46,22 +47,28 @@ type Balloon struct {
historyTree *history.HistoryTree
hyperTree *hyper.HyperTree
sync.RWMutex
log log.Logger
}

// NewBalloon function instanciates a balloon given a storage and a hasher function.
func NewBalloon(store storage.Store, hasherF func() hashing.Hasher) (*Balloon, error) {
return NewBalloonWithLogger(store, hasherF, log.L())
}

// NewBalloon function instanciates a balloon given a storage and a hasher function.
func NewBalloonWithLogger(store storage.Store, hasherF func() hashing.Hasher, logger log.Logger) (*Balloon, error) {

// create trees
historyTree := history.NewHistoryTree(hasherF, store, 300)
historyTree := history.NewHistoryTreeWithLogger(hasherF, store, 300, logger.Named("history"))
batchCache := hyper.NewBatchCache(hyper.DefaultBatchLevels)
hyperTree := hyper.NewHyperTree(hasherF, store, batchCache)
hyperTree := hyper.NewHyperTreeWithLogger(hasherF, store, batchCache, logger.Named("hyper"))

balloon := &Balloon{
version: 0,
hasherF: hasherF,
store: store,
historyTree: historyTree,
hyperTree: hyperTree,
log: logger,
}

// update version
Expand Down
31 changes: 2 additions & 29 deletions balloon/balloon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/bbva/qed/crypto/hashing"
"github.com/bbva/qed/log"
metrics_utils "github.com/bbva/qed/testutils/metrics"
"github.com/bbva/qed/testutils/rand"
storage_utils "github.com/bbva/qed/testutils/storage"
Expand All @@ -35,8 +34,6 @@ import (

func TestAdd(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

store, closeF := storage_utils.OpenBPlusTreeStore()
defer closeF()

Expand All @@ -61,8 +58,6 @@ func TestAdd(t *testing.T) {
}
func TestAddBulk(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

store, closeF := storage_utils.OpenBPlusTreeStore()
defer closeF()

Expand Down Expand Up @@ -102,8 +97,6 @@ func TestAddBulk(t *testing.T) {

func TestQueryMembershipConsistency(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

testCases := []struct {
key []byte
version uint64
Expand Down Expand Up @@ -148,8 +141,6 @@ func TestQueryMembershipConsistency(t *testing.T) {

func TestQueryMembership(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

testCases := []struct {
key []byte
exists bool
Expand Down Expand Up @@ -190,8 +181,6 @@ func TestQueryMembership(t *testing.T) {

func TestQueryConsistencyProof(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

testCases := []struct {
additions, start, end uint64
ok bool
Expand Down Expand Up @@ -237,8 +226,6 @@ func TestConsistencyProofVerify(t *testing.T) {

func TestAddQueryAndVerify(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

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

Expand All @@ -264,8 +251,6 @@ func TestAddQueryAndVerify(t *testing.T) {

func TestCacheWarmingUp(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

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

Expand Down Expand Up @@ -305,8 +290,6 @@ func TestCacheWarmingUp(t *testing.T) {

func TestGenIncrementalAndVerify(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

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

Expand Down Expand Up @@ -335,8 +318,6 @@ func TestGenIncrementalAndVerify(t *testing.T) {

func TestAddBulkAndQuery(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

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

Expand Down Expand Up @@ -371,8 +352,6 @@ func TestAddBulkAndQuery(t *testing.T) {

func TestAddAndQuery(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

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

Expand Down Expand Up @@ -403,8 +382,6 @@ func TestAddAndQuery(t *testing.T) {

func TestAddAndQueryConsistency(t *testing.T) {

log.SetLogger(t.Name(), log.SILENT)

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

Expand Down Expand Up @@ -439,8 +416,6 @@ func TestAddAndQueryConsistency(t *testing.T) {

func BenchmarkAddRocksDB(b *testing.B) {

log.SetLogger(b.Name(), log.SILENT)

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

Expand All @@ -465,8 +440,6 @@ func BenchmarkAddRocksDB(b *testing.B) {

func BenchmarkAddBulkRocksDB(b *testing.B) {

log.SetLogger(b.Name(), log.SILENT)

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

Expand Down Expand Up @@ -499,8 +472,8 @@ func BenchmarkAddBulkRocksDB(b *testing.B) {
}
}
func BenchmarkQueryRocksDB(b *testing.B) {

var events [][]byte
log.SetLogger(b.Name(), log.SILENT)

store, closeF := storage_utils.OpenRocksDBStore(b, "/var/tmp/ballon_bench.db")
defer closeF()
Expand All @@ -526,8 +499,8 @@ func BenchmarkQueryRocksDB(b *testing.B) {
}

func BenchmarkQueryRocksDBParallel(b *testing.B) {

var events [][]byte
log.SetLogger(b.Name(), log.SILENT)

store, closeF := storage_utils.OpenRocksDBStore(b, "/var/tmp/ballon_bench.db")
defer closeF()
Expand Down
5 changes: 0 additions & 5 deletions balloon/history/consistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package history
import (
"testing"

"github.com/bbva/qed/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -361,8 +360,6 @@ func TestPruneToCheckConsistency(t *testing.T) {

func BenchmarkPruneToFindConsistent(b *testing.B) {

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

b.ResetTimer()
for i := uint64(0); i < uint64(b.N); i++ {
pruned := pruneToFindConsistent(0, i)
Expand All @@ -373,8 +370,6 @@ func BenchmarkPruneToFindConsistent(b *testing.B) {

func BenchmarkPruneToCheckConsistency(b *testing.B) {

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

b.ResetTimer()
for i := uint64(0); i < uint64(b.N); i++ {
pruned := pruneToCheckConsistency(0, i)
Expand Down
3 changes: 0 additions & 3 deletions balloon/history/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

"github.com/bbva/qed/crypto/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/testutils/rand"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -129,8 +128,6 @@ func TestPruneToInsert(t *testing.T) {

func BenchmarkPruneToInsert(b *testing.B) {

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

b.ResetTimer()
for i := uint64(0); i < uint64(b.N); i++ {
pruned := pruneToInsert(i, rand.Bytes(32))
Expand Down
5 changes: 0 additions & 5 deletions balloon/history/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

"github.com/bbva/qed/crypto/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/util"
)

Expand Down Expand Up @@ -76,8 +75,6 @@ func NewMembershipProof(index, version uint64, auditPath AuditPath, hasher hashi
// Verify verifies a membership proof
func (p MembershipProof) Verify(eventDigest []byte, expectedRootHash hashing.Digest) (correct bool) {

log.Debugf("Verifying membership proof for index %d and version %d", p.Index, p.Version)

// build a visitable pruned tree and then visit it to recompute root hash
visitor := newComputeHashVisitor(p.hasher, p.AuditPath)
recomputed := pruneToVerify(p.Index, p.Version, eventDigest).Accept(visitor)
Expand All @@ -102,8 +99,6 @@ func NewIncrementalProof(start, end uint64, auditPath AuditPath, hasher hashing.

func (p IncrementalProof) Verify(startDigest, endDigest hashing.Digest) (correct bool) {

log.Debugf("Verifying incremental proof between versions %d and %d", p.StartVersion, p.EndVersion)

// build two visitable pruned trees and then visit them to recompute root hash
visitor := newComputeHashVisitor(p.hasher, p.AuditPath)
startRecomputed := pruneToVerifyIncrementalStart(p.StartVersion).Accept(visitor)
Expand Down
5 changes: 0 additions & 5 deletions balloon/history/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

"github.com/bbva/qed/crypto/hashing"
"github.com/bbva/qed/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -104,8 +103,6 @@ func TestParseAuditPath(t *testing.T) {

func TestVerifyMembershipProof(t *testing.T) {

log.SetLogger("TestVerifyMembershipProof", log.INFO)

testCases := []struct {
index, version uint64
auditPath AuditPath
Expand Down Expand Up @@ -329,8 +326,6 @@ func TestVerifyMembershipProof(t *testing.T) {

func TestVerifyIncrementalProof(t *testing.T) {

log.SetLogger("TestVerifyIncrementalProof", log.INFO)

testCases := []struct {
auditPath AuditPath
start uint64
Expand Down
3 changes: 0 additions & 3 deletions balloon/history/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package history
import (
"testing"

"github.com/bbva/qed/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -118,8 +117,6 @@ func TestPruneToFind(t *testing.T) {

func BenchmarkPruneToFind(b *testing.B) {

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

b.ResetTimer()
for i := uint64(0); i < uint64(b.N); i++ {
pruned := pruneToFind(i)
Expand Down
14 changes: 11 additions & 3 deletions balloon/history/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package history
import (
"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/crypto/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/storage"
)

Expand All @@ -30,9 +31,15 @@ type HistoryTree struct {
hasher hashing.Hasher
writeCache cache.ModifiableCache
readCache cache.Cache

log log.Logger
}

func NewHistoryTree(hasherF func() hashing.Hasher, store storage.Store, cacheSize uint16) *HistoryTree {
return NewHistoryTreeWithLogger(hasherF, store, cacheSize, log.L())
}

func NewHistoryTreeWithLogger(hasherF func() hashing.Hasher, store storage.Store, cacheSize uint16, logger log.Logger) *HistoryTree {

// create cache for Adding
writeCache := cache.NewLruReadThroughCache(storage.HistoryTable, store, cacheSize)
Expand All @@ -45,6 +52,7 @@ func NewHistoryTree(hasherF func() hashing.Hasher, store storage.Store, cacheSiz
hasher: hasherF(),
writeCache: writeCache,
readCache: readCache,
log: logger,
}
}

Expand All @@ -53,7 +61,7 @@ func NewHistoryTree(hasherF func() hashing.Hasher, store storage.Store, cacheSiz
// with the storage mutations to be done at balloon level.
func (t *HistoryTree) Add(eventDigest hashing.Digest, version uint64) (hashing.Digest, []*storage.Mutation, error) {

// log.Debugf("Adding new event digest %x with version %d", eventDigest, version)
// t.log.Tracef("Adding new event digest %x with version %d", eventDigest, version)
aalda marked this conversation as resolved.
Show resolved Hide resolved

// build a visitable pruned tree and then visit it to generate the root hash
visitor := newInsertVisitor(t.hasher, t.writeCache, storage.HistoryTable)
Expand Down Expand Up @@ -82,7 +90,7 @@ func (t *HistoryTree) AddBulk(eventDigests []hashing.Digest, initialVersion uint
// version. It builds an audit-path visitor to build the proof.
func (t *HistoryTree) ProveMembership(index, version uint64) (*MembershipProof, error) {

//log.Debugf("Proving membership for index %d with version %d", index, version)
//t.log.Tracef("Proving membership for index %d with version %d", index, version)

// build a visitable pruned tree and then visit it to collect the audit path
visitor := newAuditPathVisitor(t.hasherF(), t.readCache)
Expand All @@ -100,7 +108,7 @@ func (t *HistoryTree) ProveMembership(index, version uint64) (*MembershipProof,
// It builds an audit-path visitor to build the proof.
func (t *HistoryTree) ProveConsistency(start, end uint64) (*IncrementalProof, error) {

//log.Debugf("Proving consistency between versions %d and %d", start, end)
//t.log.Tracef("Proving consistency between versions %d and %d", start, end)

// build a visitable pruned tree and then visit it to collect the audit path
visitor := newAuditPathVisitor(t.hasherF(), t.readCache)
Expand Down
Loading