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

View File

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