lib/storage: follow-up for 29cebd82fb

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
This commit is contained in:
Aliaksandr Valialkin 2023-10-31 16:06:55 +01:00
parent 8c3bac8f40
commit c22b63af04
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -663,13 +663,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) {