Skip to content

Commit

Permalink
Add EventLogging option (dgraph-io#1035)
Browse files Browse the repository at this point in the history
This introduces an option to disable or enable trace.EventLog logging of
events.  For backward compatibility this defaults to true.

The reason for making this configurable is that under some workloads the
cost of event logging is significant and so can cause performance
issues.

Fixes dgraph-io#938
  • Loading branch information
mjgarton authored and Ibrahim Jarif committed Sep 18, 2019
1 parent c6681c1 commit 75c6a44
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
7 changes: 6 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,18 @@ func Open(opt Options) (db *DB, err error) {
}
}()

elog := y.NoEventLog
if opt.EventLogging {
elog = trace.NewEventLog("Badger", "DB")
}

db = &DB{
imm: make([]*skl.Skiplist, 0, opt.NumMemtables),
flushChan: make(chan flushTask, opt.NumMemtables),
writeCh: make(chan *request, kvWriteChCapacity),
opt: opt,
manifest: manifestFile,
elog: trace.NewEventLog("Badger", "DB"),
elog: elog,
dirLockGuard: dirLockGuard,
valueDirGuard: valueDirLockGuard,
orc: newOracle(opt),
Expand Down
12 changes: 12 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Options struct {
ReadOnly bool
Truncate bool
Logger Logger
EventLogging bool

// Fine tuning options.

Expand Down Expand Up @@ -116,6 +117,7 @@ func DefaultOptions(path string) Options {
ValueThreshold: 32,
Truncate: false,
Logger: defaultLogger,
EventLogging: true,
LogRotatesToFlush: 2,
}
}
Expand Down Expand Up @@ -239,6 +241,16 @@ func (opt Options) WithLogger(val Logger) Options {
return opt
}

// WithEventLogging returns a new Options value with EventLogging set to the given value.
//
// EventLogging provides a way to enable or disable trace.EventLog logging.
//
// The default value of EventLogging is true.
func (opt Options) WithEventLogging(enabled bool) Options {
opt.EventLogging = enabled
return opt
}

// WithMaxTableSize returns a new Options value with MaxTableSize set to the given value.
//
// MaxTableSize sets the maximum size in bytes for each LSM table or file.
Expand Down
4 changes: 2 additions & 2 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func newOracle(opt Options) *oracle {
txnMark: &y.WaterMark{Name: "badger.TxnTimestamp"},
closer: y.NewCloser(2),
}
orc.readMark.Init(orc.closer)
orc.txnMark.Init(orc.closer)
orc.readMark.Init(orc.closer, opt.EventLogging)
orc.txnMark.Init(orc.closer, opt.EventLogging)
return orc
}

Expand Down
5 changes: 4 additions & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,10 @@ func (vlog *valueLog) open(db *DB, ptr valuePointer, replayFn logEntry) error {
vlog.opt = opt
vlog.dirPath = opt.ValueDir
vlog.db = db
vlog.elog = trace.NewEventLog("Badger", "Valuelog")
vlog.elog = y.NoEventLog
if opt.EventLogging {
vlog.elog = trace.NewEventLog("Badger", "Valuelog")
}
vlog.garbageCh = make(chan struct{}, 1) // Only allow one GC at a time.
vlog.lfDiscardStats = &lfDiscardStats{m: make(map[uint32]int64)}
if err := vlog.populateFilesMap(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func TestValueGC4(t *testing.T) {
err = kv.vlog.Close()
require.NoError(t, err)

err = kv.vlog.open(kv, valuePointer{Fid: 2}, kv.replayFunction())
err = kv.vlog.open(kv, valuePointer{Fid: 2}, kv.replayFunction(), true)
require.NoError(t, err)

for i := 0; i < 8; i++ {
Expand Down Expand Up @@ -629,7 +629,7 @@ func TestPartialAppendToValueLog(t *testing.T) {
// Replay value log from beginning, badger head is past k2.
require.NoError(t, kv.vlog.Close())
require.NoError(t,
kv.vlog.open(kv, valuePointer{Fid: 0}, kv.replayFunction()))
kv.vlog.open(kv, valuePointer{Fid: 0}, kv.replayFunction(), true))
require.NoError(t, kv.Close())
}

Expand Down
31 changes: 31 additions & 0 deletions y/event_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package y

import "golang.org/x/net/trace"

var (
NoEventLog trace.EventLog = nilEventLog{}
)

type nilEventLog struct{}

func (nel nilEventLog) Printf(format string, a ...interface{}) {}

func (nel nilEventLog) Errorf(format string, a ...interface{}) {}

func (nel nilEventLog) Finish() {}
8 changes: 6 additions & 2 deletions y/watermark.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ type WaterMark struct {
}

// Init initializes a WaterMark struct. MUST be called before using it.
func (w *WaterMark) Init(closer *Closer) {
func (w *WaterMark) Init(closer *Closer, eventLogging bool) {
w.markCh = make(chan mark, 100)
w.elog = trace.NewEventLog("Watermark", w.Name)
if eventLogging {
w.elog = trace.NewEventLog("Watermark", w.Name)
} else {
w.elog = NoEventLog
}
go w.process(closer)
}

Expand Down

0 comments on commit 75c6a44

Please sign in to comment.