Skip to content

Commit

Permalink
removes unnecessary Logger from files
Browse files Browse the repository at this point in the history
  • Loading branch information
gabru-md committed Jan 21, 2020
1 parent a6c3079 commit 7b98dce
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 76 deletions.
39 changes: 16 additions & 23 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type DB struct {
valueDirGuard *directoryLockGuard

closers closers
logger Logger
mt *skl.Skiplist // Our latest (actively written) in-memory table
imm []*skl.Skiplist // Add here only AFTER pushing to flushChan.
opt Options
Expand Down Expand Up @@ -108,7 +107,7 @@ func (db *DB) replayFunction() func(Entry, valuePointer) error {

toLSM := func(nk []byte, vs y.ValueStruct) {
for err := db.ensureRoomForWrite(); err != nil; err = db.ensureRoomForWrite() {
db.logger.Debugf("Replay: Making room for writes")
db.opt.Debugf("Replay: Making room for writes")
time.Sleep(10 * time.Millisecond)
}
db.mt.Put(nk, vs)
Expand All @@ -117,7 +116,7 @@ func (db *DB) replayFunction() func(Entry, valuePointer) error {
first := true
return func(e Entry, vp valuePointer) error { // Function for replaying.
if first {
db.logger.Debugf("First key=%q\n", e.Key)
db.opt.Debugf("First key=%q\n", e.Key)
}
first = false
db.orc.Lock()
Expand Down Expand Up @@ -272,18 +271,12 @@ func Open(opt Options) (db *DB, err error) {
}
}()

logger := NilLogger
if opt.Logging {
logger = defaultLogger
}

db = &DB{
imm: make([]*skl.Skiplist, 0, opt.NumMemtables),
flushChan: make(chan flushTask, opt.NumMemtables),
writeCh: make(chan *request, kvWriteChCapacity),
opt: opt,
manifest: manifestFile,
logger: logger,
dirLockGuard: dirLockGuard,
valueDirGuard: valueDirLockGuard,
orc: newOracle(opt),
Expand Down Expand Up @@ -404,7 +397,7 @@ func (db *DB) Close() error {
}

func (db *DB) close() (err error) {
db.logger.Debugf("Closing database")
db.opt.Debugf("Closing database")

atomic.StoreInt32(&db.blockWrites, 1)

Expand Down Expand Up @@ -432,7 +425,7 @@ func (db *DB) close() (err error) {
// trying to push stuff into the memtable. This will also resolve the value
// offset problem: as we push into memtable, we update value offsets there.
if !db.mt.Empty() {
db.logger.Debugf("Flushing memtable")
db.opt.Debugf("Flushing memtable")
for {
pushedFlushTask := func() bool {
db.Lock()
Expand All @@ -442,7 +435,7 @@ func (db *DB) close() (err error) {
case db.flushChan <- flushTask{mt: db.mt, vptr: db.vhead}:
db.imm = append(db.imm, db.mt) // Flusher will attempt to remove this from s.imm.
db.mt = nil // Will segfault if we try writing!
db.logger.Debugf("pushed to flush chan\n")
db.opt.Debugf("pushed to flush chan\n")
return true
default:
// If we fail to push, we need to unlock and wait for a short while.
Expand Down Expand Up @@ -478,7 +471,7 @@ func (db *DB) close() (err error) {
if lcErr := db.lc.close(); err == nil {
err = errors.Wrap(lcErr, "DB.Close")
}
db.logger.Debugf("Waiting for closer")
db.opt.Debugf("Waiting for closer")
db.closers.updateSize.SignalAndWait()
db.orc.Stop()
db.blockCache.Close()
Expand Down Expand Up @@ -680,16 +673,16 @@ func (db *DB) writeRequests(reqs []*request) error {
r.Wg.Done()
}
}
db.logger.Debugf("writeRequests called. Writing to value log")
db.opt.Debugf("writeRequests called. Writing to value log")
err := db.vlog.write(reqs)
if err != nil {
done(err)
return err
}

db.logger.Debugf("Sending updates to subscribers")
db.opt.Debugf("Sending updates to subscribers")
db.pub.sendUpdates(reqs)
db.logger.Debugf("Writing to memtable")
db.opt.Debugf("Writing to memtable")
var count int
for _, b := range reqs {
if len(b.Entries) == 0 {
Expand All @@ -700,7 +693,7 @@ func (db *DB) writeRequests(reqs []*request) error {
for err = db.ensureRoomForWrite(); err == errNoRoom; err = db.ensureRoomForWrite() {
i++
if i%100 == 0 {
db.logger.Debugf("Making room for writes")
db.opt.Debugf("Making room for writes")
}
// We need to poll a bit because both hasRoomForWrite and the flusher need access to s.imm.
// When flushChan is full and you are blocked there, and the flusher is trying to update s.imm,
Expand All @@ -718,7 +711,7 @@ func (db *DB) writeRequests(reqs []*request) error {
db.updateHead(b.Ptrs)
}
done(nil)
db.logger.Debugf("%d entries written", count)
db.opt.Debugf("%d entries written", count)
return nil
}

Expand Down Expand Up @@ -932,7 +925,7 @@ func (db *DB) handleFlushTask(ft flushTask) error {

// Store badger head even if vptr is zero, need it for readTs
db.opt.Debugf("Storing value log head: %+v\n", ft.vptr)
db.logger.Debugf("Storing offset: %+v\n", ft.vptr)
db.opt.Debugf("Storing offset: %+v\n", ft.vptr)
val := ft.vptr.Encode()

// Pick the max commit ts, so in case of crash, our read ts would be higher than all the
Expand Down Expand Up @@ -969,17 +962,17 @@ func (db *DB) handleFlushTask(ft flushTask) error {
go func() { dirSyncCh <- db.syncDir(db.opt.Dir) }()

if _, err = fd.Write(tableData); err != nil {
db.logger.Errorf("ERROR while writing to level 0: %v", err)
db.opt.Errorf("ERROR while writing to level 0: %v", err)
return err
}

if dirSyncErr := <-dirSyncCh; dirSyncErr != nil {
// Do dir sync as best effort. No need to return due to an error there.
db.logger.Errorf("ERROR while syncing level directory: %v", dirSyncErr)
db.opt.Errorf("ERROR while syncing level directory: %v", dirSyncErr)
}
tbl, err := table.OpenTable(fd, bopts)
if err != nil {
db.logger.Debugf("ERROR while opening table: %v", err)
db.opt.Debugf("ERROR while opening table: %v", err)
return err
}
// We own a ref on tbl.
Expand Down Expand Up @@ -1061,7 +1054,7 @@ func (db *DB) calculateSize() {
return nil
})
if err != nil {
db.logger.Debugf("Got error while calculating total size of directory: %s", dir)
db.opt.Debugf("Got error while calculating total size of directory: %s", dir)
}
return lsmSize, vlogSize
}
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/badger v1.6.0 h1:DshxFxZWXUcO0xX476VJC07Xsr6ZCBVRHKZ93Oh7Evo=
github.com/dgraph-io/ristretto v0.0.2-0.20200115201040-8f368f2f2ab3 h1:MQLRM35Pp0yAyBYksjbj1nZI/w6eyRY/mWoM1sFf4kU=
github.com/dgraph-io/ristretto v0.0.2-0.20200115201040-8f368f2f2ab3/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
Expand Down
12 changes: 5 additions & 7 deletions levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (

type levelsController struct {
nextFileID uint64 // Atomic
logger Logger

// The following are initialized once and const.
levels []*levelHandler
Expand All @@ -62,7 +61,7 @@ func revertToManifest(kv *DB, mf *Manifest, idMap map[uint64]struct{}) error {
// 2. Delete files that shouldn't exist.
for id := range idMap {
if _, ok := mf.Tables[id]; !ok {
kv.logger.Debugf("Table file %d not referenced in MANIFEST\n", id)
kv.opt.Debugf("Table file %d not referenced in MANIFEST\n", id)
filename := table.NewFilename(id, kv.opt.Dir)
if err := os.Remove(filename); err != nil {
return y.Wrapf(err, "While removing table %d", id)
Expand All @@ -77,7 +76,6 @@ func newLevelsController(db *DB, mf *Manifest) (*levelsController, error) {
y.AssertTrue(db.opt.NumLevelZeroTablesStall > db.opt.NumLevelZeroTables)
s := &levelsController{
kv: db,
logger: db.logger,
levels: make([]*levelHandler, db.opt.MaxLevels),
}
s.cstatus.levels = make([]*levelCompactStatus, db.opt.MaxLevels)
Expand Down Expand Up @@ -926,10 +924,10 @@ func (s *levelsController) addLevel0Table(t *table.Table) error {
// Stall. Make sure all levels are healthy before we unstall.
var timeStart time.Time
{
s.logger.Debugf("STALLED STALLED STALLED: %v\n", time.Since(s.lastUnstalled))
s.kv.opt.Debugf("STALLED STALLED STALLED: %v\n", time.Since(s.lastUnstalled))
s.cstatus.RLock()
for i := 0; i < s.kv.opt.MaxLevels; i++ {
s.logger.Debugf("level=%d. Status=%s Size=%d\n",
s.kv.opt.Debugf("level=%d. Status=%s Size=%d\n",
i, s.cstatus.levels[i].debug(), s.levels[i].getTotalSize())
}
s.cstatus.RUnlock()
Expand All @@ -947,12 +945,12 @@ func (s *levelsController) addLevel0Table(t *table.Table) error {
time.Sleep(10 * time.Millisecond)
if i%100 == 0 {
prios := s.pickCompactLevels()
s.logger.Debugf("Waiting to add level 0 table. Compaction priorities: %+v\n", prios)
s.kv.opt.Debugf("Waiting to add level 0 table. Compaction priorities: %+v\n", prios)
i = 0
}
}
{
s.logger.Debugf("UNSTALLED UNSTALLED UNSTALLED: %v\n", time.Since(timeStart))
s.kv.opt.Debugf("UNSTALLED UNSTALLED UNSTALLED: %v\n", time.Since(timeStart))
s.lastUnstalled = time.Now()
}
}
Expand Down
1 change: 0 additions & 1 deletion levels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ func TestL0Stall(t *testing.T) {
}

opt := DefaultOptions("")
opt.Logging = false
// Disable all compactions.
opt.NumCompactors = 0
// Number of level zero tables.
Expand Down
16 changes: 4 additions & 12 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type defaultLog struct {

var defaultLogger = &defaultLog{Logger: log.New(os.Stderr, "badger ", log.LstdFlags)}

func DefaultLogger() *defaultLog {
return defaultLogger
}

func (l *defaultLog) Errorf(f string, v ...interface{}) {
l.Printf("ERROR: "+f, v...)
}
Expand All @@ -83,15 +87,3 @@ func (l *defaultLog) Infof(f string, v ...interface{}) {
func (l *defaultLog) Debugf(f string, v ...interface{}) {
l.Printf("DEBUG: "+f, v...)
}

var NilLogger Logger = nilLogger{}

type nilLogger struct{}

func (nill nilLogger) Debugf(f string, v ...interface{}) {}

func (nill nilLogger) Errorf(f string, v ...interface{}) {}

func (nill nilLogger) Infof(f string, v ...interface{}) {}

func (nill nilLogger) Warningf(f string, v ...interface{}) {}
12 changes: 0 additions & 12 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type Options struct {
Truncate bool
Logger Logger
Compression options.CompressionType
Logging bool
InMemory bool

// Fine tuning options.
Expand Down Expand Up @@ -147,7 +146,6 @@ func DefaultOptions(path string) Options {
Truncate: false,
Logger: defaultLogger,
LogRotatesToFlush: 2,
Logging: true,
EncryptionKey: []byte{},
EncryptionKeyRotationDuration: 10 * 24 * time.Hour, // Default 10 days.
}
Expand Down Expand Up @@ -283,16 +281,6 @@ func (opt Options) WithLogger(val Logger) Options {
return opt
}

// WithLogging returns a new Options value with Logging set to the given value.
//
// Logging provides a way to enable or disable Logger logging.
//
// The default value of Logging is true.
func (opt Options) WithLogging(enabled bool) Options {
opt.Logging = 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, opt.Logging)
orc.txnMark.Init(orc.closer, opt.Logging)
orc.readMark.Init(orc.closer, opt)
orc.txnMark.Init(orc.closer, opt)
return orc
}

Expand Down
14 changes: 5 additions & 9 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,6 @@ type lfDiscardStats struct {

type valueLog struct {
dirPath string
logger Logger

// guards our view of which files exist, which to be deleted, how many active iterators
filesLock sync.RWMutex
Expand Down Expand Up @@ -1064,10 +1063,7 @@ func (vlog *valueLog) open(db *DB, ptr valuePointer, replayFn logEntry) error {
return nil
}
vlog.dirPath = vlog.opt.ValueDir
vlog.logger = NilLogger
if vlog.opt.Logging {
vlog.logger = defaultLogger
}

vlog.garbageCh = make(chan struct{}, 1) // Only allow one GC at a time.
vlog.lfDiscardStats = &lfDiscardStats{
m: make(map[uint32]int64),
Expand Down Expand Up @@ -1212,7 +1208,7 @@ func (vlog *valueLog) Close() error {
// close flushDiscardStats.
vlog.lfDiscardStats.closer.SignalAndWait()

vlog.logger.Debugf("Stopping garbage collection of values.")
vlog.opt.Debugf("Stopping garbage collection of values.")

var err error
for id, f := range vlog.filesMap {
Expand Down Expand Up @@ -1361,15 +1357,15 @@ func (vlog *valueLog) write(reqs []*request) error {
if buf.Len() == 0 {
return nil
}
vlog.logger.Debugf("Flushing buffer of size %d to vlog", buf.Len())
vlog.opt.Debugf("Flushing buffer of size %d to vlog", buf.Len())
n, err := curlf.fd.Write(buf.Bytes())
if err != nil {
return errors.Wrapf(err, "Unable to write to value log file: %q", curlf.path)
}
buf.Reset()
y.NumWrites.Add(1)
y.NumBytesWritten.Add(int64(n))
vlog.logger.Debugf("Done")
vlog.opt.Debugf("Done")
atomic.AddUint32(&vlog.writableLogOffset, uint32(n))
atomic.StoreUint32(&curlf.size, vlog.writableLogOffset)
return nil
Expand Down Expand Up @@ -1692,7 +1688,7 @@ func (vlog *valueLog) doRunGC(lf *logFile, discardRatio float64, tr trace.Trace)
// This is still the active entry. This would need to be rewritten.

} else {
vlog.logger.Debugf("Reason=%+v\n", r)
vlog.opt.Debugf("Reason=%+v\n", r)
buf, lf, err := vlog.readValueBytes(vp, s)
// we need to decide, whether to unlock the lock file immediately based on the
// loading mode. getUnlockCallback will take care of it.
Expand Down
Loading

0 comments on commit 7b98dce

Please sign in to comment.