use our logger in autofile/group

This commit is contained in:
Anton Kaliaev 2018-11-06 13:12:12 +01:00
parent 46d32af055
commit d178ea9eaf
3 changed files with 23 additions and 5 deletions

View File

@ -13,6 +13,7 @@ import (
amino "github.com/tendermint/go-amino" amino "github.com/tendermint/go-amino"
auto "github.com/tendermint/tendermint/libs/autofile" auto "github.com/tendermint/tendermint/libs/autofile"
cmn "github.com/tendermint/tendermint/libs/common" cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time" tmtime "github.com/tendermint/tendermint/types/time"
) )
@ -95,6 +96,11 @@ func (wal *baseWAL) Group() *auto.Group {
return wal.group return wal.group
} }
func (wal *baseWAL) SetLogger(l log.Logger) {
wal.BaseService.Logger = l
wal.group.SetLogger(l)
}
func (wal *baseWAL) OnStart() error { func (wal *baseWAL) OnStart() error {
size, err := wal.group.Head.Size() size, err := wal.group.Head.Size()
if err != nil { if err != nil {

View File

@ -83,6 +83,8 @@ func OpenAutoFile(path string) (*AutoFile, error) {
return af, nil return af, nil
} }
// Close shuts down the closing goroutine, SIGHUP handler and closes the
// AutoFile.
func (af *AutoFile) Close() error { func (af *AutoFile) Close() error {
af.closeTicker.Stop() af.closeTicker.Stop()
close(af.closeTickerStopc) close(af.closeTickerStopc)
@ -116,6 +118,10 @@ func (af *AutoFile) closeFile() (err error) {
return file.Close() return file.Close()
} }
// Write writes len(b) bytes to the AutoFile. It returns the number of bytes
// written and an error, if any. Write returns a non-nil error when n !=
// len(b).
// Opens AutoFile if needed.
func (af *AutoFile) Write(b []byte) (n int, err error) { func (af *AutoFile) Write(b []byte) (n int, err error) {
af.mtx.Lock() af.mtx.Lock()
defer af.mtx.Unlock() defer af.mtx.Unlock()
@ -130,6 +136,10 @@ func (af *AutoFile) Write(b []byte) (n int, err error) {
return return
} }
// Sync commits the current contents of the file to stable storage. Typically,
// this means flushing the file system's in-memory copy of recently written
// data to disk.
// Opens AutoFile if needed.
func (af *AutoFile) Sync() error { func (af *AutoFile) Sync() error {
af.mtx.Lock() af.mtx.Lock()
defer af.mtx.Unlock() defer af.mtx.Unlock()
@ -158,6 +168,9 @@ func (af *AutoFile) openFile() error {
return nil return nil
} }
// Size returns the size of the AutoFile. It returns -1 and an error if fails
// get stats or open file.
// Opens AutoFile if needed.
func (af *AutoFile) Size() (int64, error) { func (af *AutoFile) Size() (int64, error) {
af.mtx.Lock() af.mtx.Lock()
defer af.mtx.Unlock() defer af.mtx.Unlock()
@ -171,10 +184,10 @@ func (af *AutoFile) Size() (int64, error) {
return -1, err return -1, err
} }
} }
stat, err := af.file.Stat() stat, err := af.file.Stat()
if err != nil { if err != nil {
return -1, err return -1, err
} }
return stat.Size(), nil return stat.Size(), nil
} }

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -253,18 +252,18 @@ func (g *Group) checkTotalSizeLimit() {
} }
if index == gInfo.MaxIndex { if index == gInfo.MaxIndex {
// Special degenerate case, just do nothing. // Special degenerate case, just do nothing.
log.Println("WARNING: Group's head " + g.Head.Path + "may grow without bound") g.Logger.Info("Group's head may grow without bound", "head", g.Head.Path)
return return
} }
pathToRemove := filePathForIndex(g.Head.Path, index, gInfo.MaxIndex) pathToRemove := filePathForIndex(g.Head.Path, index, gInfo.MaxIndex)
fileInfo, err := os.Stat(pathToRemove) fileInfo, err := os.Stat(pathToRemove)
if err != nil { if err != nil {
log.Println("WARNING: Failed to fetch info for file @" + pathToRemove) g.Logger.Error("Failed to fetch info for file", "file", pathToRemove)
continue continue
} }
err = os.Remove(pathToRemove) err = os.Remove(pathToRemove)
if err != nil { if err != nil {
log.Println(err) g.Logger.Error("Failed to remove path", "path", pathToRemove)
return return
} }
totalSize -= fileInfo.Size() totalSize -= fileInfo.Size()