lib/logstorage: eliminate data race when clearing s.ptwHot after deleting the corresponding partition

The previous code could result in the following data race:
1. The s.ptwHot partition is marked to be deleted
2. ptw.decRef() is called on it
3. ptw.pt is set to nil
4. s.ptwHot.pt is accessed from concurrent goroutine, which leads to panic.

The change clears s.ptwHot under s.partitionsLock in order to prevent from the data race.

This is a follow-up for 8d50032dd6

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4895
This commit is contained in:
Aliaksandr Valialkin 2023-08-29 11:04:11 +02:00
parent e8db78eaa4
commit 317a273c6d
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -317,6 +317,9 @@ func (s *Storage) watchRetention() {
break
}
ptwsToDelete = append(ptwsToDelete, ptw)
if ptw == s.ptwHot {
s.ptwHot = nil
}
}
for i := range ptwsToDelete {
s.partitions[i] = nil
@ -329,9 +332,6 @@ func (s *Storage) watchRetention() {
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.decRef()
if s.ptwHot != nil && s.ptwHot.pt == ptw.pt {
s.ptwHot = nil
}
}
select {