add lock in volume manager reconciler to avoid data race

Signed-off-by: Paco Xu <paco.xu@daocloud.io>
This commit is contained in:
Paco Xu 2023-03-17 21:00:20 +08:00
parent 7afcfe1826
commit 5134520a3b
2 changed files with 11 additions and 4 deletions

View File

@ -18,6 +18,7 @@ package reconciler
import (
"fmt"
"sync"
"time"
v1 "k8s.io/api/core/v1"
@ -139,10 +140,12 @@ type reconciler struct {
volumePluginMgr *volumepkg.VolumePluginMgr
skippedDuringReconstruction map[v1.UniqueVolumeName]*globalVolumeInfo
kubeletPodsDir string
timeOfLastSync time.Time
volumesFailedReconstruction []podVolume
volumesNeedDevicePath []v1.UniqueVolumeName
volumesNeedReportedInUse []v1.UniqueVolumeName
// lock protects timeOfLastSync for updating and checking
timeOfLastSyncLock sync.Mutex
timeOfLastSync time.Time
volumesFailedReconstruction []podVolume
volumesNeedDevicePath []v1.UniqueVolumeName
volumesNeedReportedInUse []v1.UniqueVolumeName
}
func (rc *reconciler) Run(stopCh <-chan struct{}) {

View File

@ -72,10 +72,14 @@ type globalVolumeInfo struct {
}
func (rc *reconciler) updateLastSyncTime() {
rc.timeOfLastSyncLock.Lock()
defer rc.timeOfLastSyncLock.Unlock()
rc.timeOfLastSync = time.Now()
}
func (rc *reconciler) StatesHasBeenSynced() bool {
rc.timeOfLastSyncLock.Lock()
defer rc.timeOfLastSyncLock.Unlock()
return !rc.timeOfLastSync.IsZero()
}