Skip to content

Commit

Permalink
Revert "log file unmap/close errors (ledgerwatch#1036)"
Browse files Browse the repository at this point in the history
This reverts commit dc1d96d.
  • Loading branch information
pratikspatil024 authored Aug 18, 2023
1 parent c787d61 commit 6135ed9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 51 deletions.
2 changes: 0 additions & 2 deletions common/dbg/leak_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/ledgerwatch/log/v3"
)

const FileCloseLogLevel = log.LvlTrace

// LeakDetector - use it to find which resource was created but not closed (leaked)
// periodically does print in logs resources which living longer than 1min with their creation stack trace
// For example db transactions can call Add/Del from Begin/Commit/Rollback methods
Expand Down
16 changes: 7 additions & 9 deletions compress/decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,14 @@ func (d *Decompressor) ModTime() time.Time {
return d.modTime
}

func (d *Decompressor) Close() {
if d.f != nil {
if err := mmap.Munmap(d.mmapHandle1, d.mmapHandle2); err != nil {
log.Log(dbg.FileCloseLogLevel, "unmap", "err", err, "file", d.FileName(), "stack", dbg.Stack())
}
if err := d.f.Close(); err != nil {
log.Log(dbg.FileCloseLogLevel, "close", "err", err, "file", d.FileName(), "stack", dbg.Stack())
}
d.f = nil
func (d *Decompressor) Close() error {
if err := mmap.Munmap(d.mmapHandle1, d.mmapHandle2); err != nil {
log.Trace("unmap", "err", err, "file", d.FileName())
}
if err := d.f.Close(); err != nil {
return err
}
return nil
}

func (d *Decompressor) FilePath() string { return d.filePath }
Expand Down
21 changes: 5 additions & 16 deletions kv/bitmapdb/fixed_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,18 @@ func OpenFixedSizeBitmaps(filePath string, bitsPerBitmap int) (*FixedSizeBitmaps

func (bm *FixedSizeBitmaps) FileName() string { return bm.fileName }
func (bm *FixedSizeBitmaps) FilePath() string { return bm.filePath }
func (bm *FixedSizeBitmaps) Close() {
func (bm *FixedSizeBitmaps) Close() error {
if bm.m != nil {
if err := bm.m.Unmap(); err != nil {
log.Trace("unmap", "err", err, "file", bm.FileName())
}
bm.m = nil
}
if bm.f != nil {
if err := bm.f.Close(); err != nil {
log.Trace("close", "err", err, "file", bm.FileName())
return err
}
bm.f = nil
}
return nil
}

func (bm *FixedSizeBitmaps) At(item uint64) (res []uint64, err error) {
Expand Down Expand Up @@ -220,18 +219,8 @@ func NewFixedSizeBitmapsWriter(indexFile string, bitsPerBitmap int, amount uint6
return idx, nil
}
func (w *FixedSizeBitmapsWriter) Close() {
if w.m != nil {
if err := w.m.Unmap(); err != nil {
log.Trace("unmap", "err", err, "file", w.f.Name())
}
w.m = nil
}
if w.f != nil {
if err := w.f.Close(); err != nil {
log.Trace("close", "err", err, "file", w.f.Name())
}
w.f = nil
}
_ = w.m.Unmap()
_ = w.f.Close()
}
func growFileToSize(f *os.File, size int) error {
pageSize := os.Getpagesize()
Expand Down
19 changes: 8 additions & 11 deletions recsplit/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"time"
"unsafe"

"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -175,19 +174,17 @@ func (idx *Index) BaseDataID() uint64 { return idx.baseDataID }
func (idx *Index) FilePath() string { return idx.filePath }
func (idx *Index) FileName() string { return idx.fileName }

func (idx *Index) Close() {
func (idx *Index) Close() error {
if idx == nil {
return
return nil
}
if idx.f != nil {
if err := mmap.Munmap(idx.mmapHandle1, idx.mmapHandle2); err != nil {
log.Log(dbg.FileCloseLogLevel, "unmap", "err", err, "file", idx.FileName(), "stack", dbg.Stack())
}
if err := idx.f.Close(); err != nil {
log.Log(dbg.FileCloseLogLevel, "close", "err", err, "file", idx.FileName(), "stack", dbg.Stack())
}
idx.f = nil
if err := mmap.Munmap(idx.mmapHandle1, idx.mmapHandle2); err != nil {
log.Trace("unmap", "err", err, "file", idx.FileName())
}
if err := idx.f.Close(); err != nil {
return err
}
return nil
}

func (idx *Index) skipBits(m uint16) int {
Expand Down
6 changes: 2 additions & 4 deletions state/btree_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/c2h5oh/datasize"
"github.com/edsrzf/mmap-go"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/common/background"
Expand Down Expand Up @@ -1090,11 +1089,10 @@ func (b *BtIndex) Close() {
}
if b.file != nil {
if err := b.m.Unmap(); err != nil {
log.Log(dbg.FileCloseLogLevel, "unmap", "err", err, "file", b.FileName(), "stack", dbg.Stack())
_ = err
}
b.m = nil
if err := b.file.Close(); err != nil {
log.Log(dbg.FileCloseLogLevel, "close", "err", err, "file", b.FileName(), "stack", dbg.Stack())
_ = err
}
b.file = nil
}
Expand Down
16 changes: 12 additions & 4 deletions state/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ func filesItemLess(i, j *filesItem) bool {
}
func (i *filesItem) closeFilesAndRemove() {
if i.decompressor != nil {
i.decompressor.Close()
if err := i.decompressor.Close(); err != nil {
log.Trace("close", "err", err, "file", i.decompressor.FileName())
}
// paranoic-mode on: don't delete frozen files
if !i.frozen {
if err := os.Remove(i.decompressor.FilePath()); err != nil {
Expand All @@ -94,7 +96,9 @@ func (i *filesItem) closeFilesAndRemove() {
i.decompressor = nil
}
if i.index != nil {
i.index.Close()
if err := i.index.Close(); err != nil {
log.Trace("close", "err", err, "file", i.index.FileName())
}
// paranoic-mode on: don't delete frozen files
if !i.frozen {
if err := os.Remove(i.index.FilePath()); err != nil {
Expand Down Expand Up @@ -376,11 +380,15 @@ func (d *Domain) closeWhatNotInList(fNames []string) {
})
for _, item := range toDelete {
if item.decompressor != nil {
item.decompressor.Close()
if err := item.decompressor.Close(); err != nil {
d.logger.Trace("close", "err", err, "file", item.decompressor.FileName())
}
item.decompressor = nil
}
if item.index != nil {
item.index.Close()
if err := item.index.Close(); err != nil {
d.logger.Trace("close", "err", err, "file", item.index.FileName())
}
item.index = nil
}
if item.bindex != nil {
Expand Down
8 changes: 6 additions & 2 deletions state/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,15 @@ func (h *History) closeWhatNotInList(fNames []string) {
})
for _, item := range toDelete {
if item.decompressor != nil {
item.decompressor.Close()
if err := item.decompressor.Close(); err != nil {
h.logger.Trace("close", "err", err, "file", item.index.FileName())
}
item.decompressor = nil
}
if item.index != nil {
item.index.Close()
if err := item.index.Close(); err != nil {
h.logger.Trace("close", "err", err, "file", item.index.FileName())
}
item.index = nil
}
h.files.Delete(item)
Expand Down
8 changes: 6 additions & 2 deletions state/inverted_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,15 @@ func (ii *InvertedIndex) closeWhatNotInList(fNames []string) {
})
for _, item := range toDelete {
if item.decompressor != nil {
item.decompressor.Close()
if err := item.decompressor.Close(); err != nil {
ii.logger.Trace("close", "err", err, "file", item.index.FileName())
}
item.decompressor = nil
}
if item.index != nil {
item.index.Close()
if err := item.index.Close(); err != nil {
ii.logger.Trace("close", "err", err, "file", item.index.FileName())
}
item.index = nil
}
ii.files.Delete(item)
Expand Down
4 changes: 3 additions & 1 deletion state/locality_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ func closeLocalityIndexFilesAndRemove(i *ctxLocalityIdx, logger log.Logger) {
i.file.src = nil
}
if i.bm != nil {
i.bm.Close()
if err := i.bm.Close(); err != nil {
logger.Trace("close", "err", err, "file", i.bm.FileName())
}
if err := os.Remove(i.bm.FilePath()); err != nil {
logger.Trace("os.Remove", "err", err, "file", i.bm.FileName())
}
Expand Down

0 comments on commit 6135ed9

Please sign in to comment.