From 781bff24b57568d84d469ef6bf4289d413da50cc Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin <valyala@victoriametrics.com> Date: Fri, 20 Sep 2024 11:57:24 +0200 Subject: [PATCH] lib/storage: simplify indexDB.doExtDB() usage by removing the returned value Previously indexDB.doExtDB() was returning boolean value, which was indicating whether f callback was called. There is no need in returning this boolean value, since the f callback can determine on itself whether it was called. This simplifies the code a bit. While at it, document indexDB.doExtDB(). --- lib/storage/index_db.go | 18 ++++++++++-------- lib/storage/storage.go | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 56883dfa3e..fa49a64667 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -258,19 +258,20 @@ func (db *indexDB) UpdateMetrics(m *IndexDBMetrics) { }) } -func (db *indexDB) doExtDB(f func(extDB *indexDB)) bool { +// doExtDB calls f for non-nil db.extDB. +// +// f isn't called if db.extDB is nil. +func (db *indexDB) doExtDB(f func(extDB *indexDB)) { db.extDBLock.Lock() extDB := db.extDB if extDB != nil { extDB.incRef() } db.extDBLock.Unlock() - if extDB == nil { - return false + if extDB != nil { + f(extDB) + extDB.decRef() } - f(extDB) - extDB.decRef() - return true } // SetExtDB sets external db to search. @@ -1657,7 +1658,7 @@ func (db *indexDB) searchMetricNameWithCache(dst []byte, metricID uint64, accoun } // Try searching in the external indexDB. - if db.doExtDB(func(extDB *indexDB) { + db.doExtDB(func(extDB *indexDB) { is := extDB.getIndexSearch(accountID, projectID, noDeadline) dst, ok = is.searchMetricName(dst, metricID) extDB.putIndexSearch(is) @@ -1666,7 +1667,8 @@ func (db *indexDB) searchMetricNameWithCache(dst []byte, metricID uint64, accoun // since the filtering must be performed before calling this func. extDB.putMetricNameToCache(metricID, dst) } - }) && ok { + }) + if ok { return dst, true } diff --git a/lib/storage/storage.go b/lib/storage/storage.go index f58cc9d014..8d0ed4833c 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -404,11 +404,11 @@ func (s *Storage) CreateSnapshot() (string, error) { dirsToRemoveOnError = append(dirsToRemoveOnError, idbSnapshot) var err error - ok := idb.doExtDB(func(extDB *indexDB) { + idb.doExtDB(func(extDB *indexDB) { prevSnapshot := filepath.Join(idbSnapshot, extDB.name) err = extDB.tb.CreateSnapshotAt(prevSnapshot) }) - if ok && err != nil { + if err != nil { return "", fmt.Errorf("cannot create prev indexDB snapshot: %w", err) } dstIdbDir := filepath.Join(dstDir, indexdbDirname)