From df619bdff09fba07f9a6177675430f6af6dfff97 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 14 Apr 2023 20:09:56 -0700 Subject: [PATCH] all: consistently use fs.MustClose() for closing lock files --- lib/mergeset/table.go | 5 ++--- lib/mergeset/table_test.go | 22 ---------------------- lib/persistentqueue/persistentqueue.go | 6 ++---- lib/storage/storage.go | 5 ++--- lib/storage/storage_test.go | 20 -------------------- lib/storage/table.go | 5 ++--- lib/storage/table_test.go | 25 ------------------------- 7 files changed, 8 insertions(+), 80 deletions(-) diff --git a/lib/mergeset/table.go b/lib/mergeset/table.go index 81a1da24e..a2ca7f216 100644 --- a/lib/mergeset/table.go +++ b/lib/mergeset/table.go @@ -426,9 +426,8 @@ func (tb *Table) MustClose() { } // Release flockF - if err := tb.flockF.Close(); err != nil { - logger.Panicf("FATAL:cannot close %q: %s", tb.flockF.Name(), err) - } + fs.MustClose(tb.flockF) + tb.flockF = nil } // Path returns the path to tb on the filesystem. diff --git a/lib/mergeset/table_test.go b/lib/mergeset/table_test.go index d9154786d..6f0db9121 100644 --- a/lib/mergeset/table_test.go +++ b/lib/mergeset/table_test.go @@ -39,28 +39,6 @@ func TestTableOpenClose(t *testing.T) { } } -func TestTableOpenMultipleTimes(t *testing.T) { - const path = "TestTableOpenMultipleTimes" - defer func() { - _ = os.RemoveAll(path) - }() - - var isReadOnly uint32 - tb1, err := OpenTable(path, nil, nil, &isReadOnly) - if err != nil { - t.Fatalf("cannot open table: %s", err) - } - defer tb1.MustClose() - - for i := 0; i < 4; i++ { - tb2, err := OpenTable(path, nil, nil, &isReadOnly) - if err == nil { - tb2.MustClose() - t.Fatalf("expecting non-nil error when opening already opened table") - } - } -} - func TestTableAddItemsSerial(t *testing.T) { r := rand.New(rand.NewSource(1)) const path = "TestTableAddItemsSerial" diff --git a/lib/persistentqueue/persistentqueue.go b/lib/persistentqueue/persistentqueue.go index 27874dde8..9ae6b0b63 100644 --- a/lib/persistentqueue/persistentqueue.go +++ b/lib/persistentqueue/persistentqueue.go @@ -174,7 +174,7 @@ func tryOpeningQueue(path, name string, chunkFileSize, maxBlockSize, maxPendingB mustCloseFlockF := true defer func() { if mustCloseFlockF { - _ = q.flockF.Close() + fs.MustClose(q.flockF) } }() @@ -332,9 +332,7 @@ func (q *queue) MustClose() { } // Close flockF - if err := q.flockF.Close(); err != nil { - logger.Panicf("FATAL: cannot close flock file: %s", err) - } + fs.MustClose(q.flockF) q.flockF = nil } diff --git a/lib/storage/storage.go b/lib/storage/storage.go index e1a4e415f..38e78ec99 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -799,9 +799,8 @@ func (s *Storage) MustClose() { s.mustSaveNextDayMetricIDs(nextDayMetricIDs) // Release lock file. - if err := s.flockF.Close(); err != nil { - logger.Panicf("FATAL: cannot close lock file %q: %s", s.flockF.Name(), err) - } + fs.MustClose(s.flockF) + s.flockF = nil // Stop series limiters. if sl := s.hourlySeriesLimiter; sl != nil { diff --git a/lib/storage/storage_test.go b/lib/storage/storage_test.go index 1835f176a..cf5cc8b9e 100644 --- a/lib/storage/storage_test.go +++ b/lib/storage/storage_test.go @@ -433,26 +433,6 @@ func TestStorageOpenClose(t *testing.T) { } } -func TestStorageOpenMultipleTimes(t *testing.T) { - path := "TestStorageOpenMultipleTimes" - s1, err := OpenStorage(path, -1, 0, 0) - if err != nil { - t.Fatalf("cannot open storage the first time: %s", err) - } - - for i := 0; i < 10; i++ { - s2, err := OpenStorage(path, -1, 0, 0) - if err == nil { - s2.MustClose() - t.Fatalf("expecting non-nil error when opening already opened storage") - } - } - s1.MustClose() - if err := os.RemoveAll(path); err != nil { - t.Fatalf("cannot remove %q: %s", path, err) - } -} - func TestStorageRandTimestamps(t *testing.T) { path := "TestStorageRandTimestamps" retentionMsecs := int64(10 * msecsPerMonth) diff --git a/lib/storage/table.go b/lib/storage/table.go index cd78ae68d..4e3b9c05c 100644 --- a/lib/storage/table.go +++ b/lib/storage/table.go @@ -202,9 +202,8 @@ func (tb *table) MustClose() { } // Release exclusive lock on the table. - if err := tb.flockF.Close(); err != nil { - logger.Panicf("FATAL: cannot release lock on %q: %s", tb.flockF.Name(), err) - } + fs.MustClose(tb.flockF) + tb.flockF = nil } // flushPendingRows flushes all the pending raw rows, so they become visible to search. diff --git a/lib/storage/table_test.go b/lib/storage/table_test.go index 08bf2ba75..e6cdc044a 100644 --- a/lib/storage/table_test.go +++ b/lib/storage/table_test.go @@ -36,28 +36,3 @@ func TestTableOpenClose(t *testing.T) { tb.MustClose() } } - -func TestTableOpenMultipleTimes(t *testing.T) { - const path = "TestTableOpenMultipleTimes" - const retentionMsecs = 123 * msecsPerMonth - - defer func() { - _ = os.RemoveAll(path) - }() - - strg := newTestStorage() - strg.retentionMsecs = retentionMsecs - tb1, err := openTable(path, strg) - if err != nil { - t.Fatalf("cannot open table the first time: %s", err) - } - defer tb1.MustClose() - - for i := 0; i < 10; i++ { - tb2, err := openTable(path, strg) - if err == nil { - tb2.MustClose() - t.Fatalf("expecting non-nil error when opening already opened table") - } - } -}