From 92e098012ab3985b62e6716fea76bd57df38c9fe Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 23 Feb 2024 23:04:38 +0200 Subject: [PATCH] lib/logstorage: consistently use atomic.* type for refCount and mustDrop fields in datadb and storage structs in the same way as it is used in lib/storage See ea9e2b19a5fa8aecc25c9da10fad8f4c1c58df38 and a204fd69f173f6e76d9f684d176d20f2fdca010c --- lib/logstorage/datadb.go | 16 ++++++++-------- lib/logstorage/storage.go | 18 +++++++++--------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/logstorage/datadb.go b/lib/logstorage/datadb.go index a96eb58ce2..0cf47d962c 100644 --- a/lib/logstorage/datadb.go +++ b/lib/logstorage/datadb.go @@ -92,10 +92,10 @@ type partWrapper struct { // refCount is the number of references to p. // // When the number of references reaches zero, then p is closed. - refCount int32 + refCount atomic.Int32 // The flag, which is set when the part must be deleted after refCount reaches zero. - mustBeDeleted uint32 + mustDrop atomic.Bool // p is an opened part p *part @@ -111,18 +111,18 @@ type partWrapper struct { } func (pw *partWrapper) incRef() { - atomic.AddInt32(&pw.refCount, 1) + pw.refCount.Add(1) } func (pw *partWrapper) decRef() { - n := atomic.AddInt32(&pw.refCount, -1) + n := pw.refCount.Add(-1) if n > 0 { return } deletePath := "" if pw.mp == nil { - if atomic.LoadUint32(&pw.mustBeDeleted) != 0 { + if pw.mustDrop.Load() { deletePath = pw.p.path } } else { @@ -767,7 +767,7 @@ func (ddb *datadb) swapSrcWithDstParts(pws []*partWrapper, pwNew *partWrapper, d // Mark old parts as must be deleted and decrement reference count, // so they are eventually closed and deleted. for _, pw := range pws { - atomic.StoreUint32(&pw.mustBeDeleted, 1) + pw.mustDrop.Store(true) pw.decRef() } } @@ -917,8 +917,8 @@ func mustCloseDatadb(ddb *datadb) { // close file parts for _, pw := range ddb.fileParts { pw.decRef() - if pw.refCount != 0 { - logger.Panicf("BUG: ther are %d references to filePart", pw.refCount) + if n := pw.refCount.Load(); n != 0 { + logger.Panicf("BUG: ther are %d references to filePart", n) } } ddb.fileParts = nil diff --git a/lib/logstorage/storage.go b/lib/logstorage/storage.go index 5701110036..4fd40df774 100644 --- a/lib/logstorage/storage.go +++ b/lib/logstorage/storage.go @@ -142,10 +142,10 @@ type Storage struct { type partitionWrapper struct { // refCount is the number of active references to p. // When it reaches zero, then the p is closed. - refCount int32 + refCount atomic.Int32 // The flag, which is set when the partition must be deleted after refCount reaches zero. - mustBeDeleted uint32 + mustDrop atomic.Bool // day is the day for the partition in the unix timestamp divided by the number of seconds in the day. day int64 @@ -164,17 +164,17 @@ func newPartitionWrapper(pt *partition, day int64) *partitionWrapper { } func (ptw *partitionWrapper) incRef() { - atomic.AddInt32(&ptw.refCount, 1) + ptw.refCount.Add(1) } func (ptw *partitionWrapper) decRef() { - n := atomic.AddInt32(&ptw.refCount, -1) + n := ptw.refCount.Add(-1) if n > 0 { return } deletePath := "" - if atomic.LoadUint32(&ptw.mustBeDeleted) != 0 { + if ptw.mustDrop.Load() { deletePath = ptw.pt.path } @@ -293,7 +293,7 @@ func MustOpenStorage(path string, cfg *StorageConfig) *Storage { break } logger.Infof("the partition %s is scheduled to be deleted because it is outside the -futureRetention=%dd", ptw.pt.path, durationToDays(s.futureRetention)) - atomic.StoreUint32(&ptw.mustBeDeleted, 1) + ptw.mustDrop.Store(true) ptw.decRef() j-- } @@ -348,7 +348,7 @@ func (s *Storage) watchRetention() { for _, ptw := range ptwsToDelete { logger.Infof("the partition %s is scheduled to be deleted because it is outside the -retentionPeriod=%dd", ptw.pt.path, durationToDays(s.retention)) - atomic.StoreUint32(&ptw.mustBeDeleted, 1) + ptw.mustDrop.Store(true) ptw.decRef() } @@ -379,8 +379,8 @@ func (s *Storage) MustClose() { // Close partitions for _, pw := range s.partitions { pw.decRef() - if pw.refCount != 0 { - logger.Panicf("BUG: there are %d users of partition", pw.refCount) + if n := pw.refCount.Load(); n != 0 { + logger.Panicf("BUG: there are %d users of partition", n) } } s.partitions = nil