From 7ac49162c62d42f0c328e305a23b2cdd5640d572 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 31 Oct 2023 16:06:55 +0100 Subject: [PATCH] lib/storage: follow-up for 29cebd82fbe7470b323c4f51cbea63fffa28629b Use atomic.CompareAndSwapUint32() instead of atomic.LoadUint32() followed by atomic.StoreUint32(). This makes the code more clear. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5159 --- lib/storage/storage.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 5c4ebfd71..2e48db3d6 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -648,13 +648,12 @@ func (s *Storage) startFreeDiskSpaceWatcher() { f := func() { freeSpaceBytes := fs.MustGetFreeSpace(s.path) if freeSpaceBytes < freeDiskSpaceLimitBytes { - if atomic.LoadUint32(&s.isReadOnly) == 0 { + // Switch the storage to readonly mode if there is no enough free space left at s.path + if atomic.CompareAndSwapUint32(&s.isReadOnly, 0, 1) { // log notification only on state change logger.Warnf("switching the storage at %s to read-only mode, since it has less than -storage.minFreeDiskSpaceBytes=%d of free space: %d bytes left", s.path, freeDiskSpaceLimitBytes, freeSpaceBytes) } - // Switch the storage to readonly mode if there is no enough free space left at s.path - atomic.StoreUint32(&s.isReadOnly, 1) return } if atomic.CompareAndSwapUint32(&s.isReadOnly, 1, 0) {