diff --git a/pkg/os/smb/smb.go b/pkg/os/smb/smb.go index 6ec6dd0338..3a540aab68 100644 --- a/pkg/os/smb/smb.go +++ b/pkg/os/smb/smb.go @@ -21,12 +21,15 @@ import ( "os" "path/filepath" "strings" + "sync" "k8s.io/klog/v2" "sigs.k8s.io/azurefile-csi-driver/pkg/util" azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache" ) +var getRemoteServerFromTargetMutex = &sync.Mutex{} + func IsSmbMapped(remotePath string) (bool, error) { cmdLine := `$(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath -ErrorAction Stop).Status` cmdEnv := fmt.Sprintf("smbremotepath=%s", remotePath) @@ -93,6 +96,10 @@ func RemoveSmbGlobalMapping(remotePath string) error { // GetRemoteServerFromTarget- gets the remote server path given a mount point, the function is recursive until it find the remote server or errors out func GetRemoteServerFromTarget(mount string, volStatsCache azcache.Resource) (string, error) { + // use mutex to allow more cache hit + getRemoteServerFromTargetMutex.Lock() + defer getRemoteServerFromTargetMutex.Unlock() + cache, err := volStatsCache.Get(mount, azcache.CacheReadTypeDefault) if err != nil { return "", err diff --git a/pkg/util/util.go b/pkg/util/util.go index 234b18e4cb..1f774930d8 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -29,7 +29,7 @@ const ( MaxPathLengthWindows = 260 ) -var mutex = &sync.Mutex{} +var powershellCmdMutex = &sync.Mutex{} // RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB // in the unit of Bytes @@ -68,8 +68,8 @@ func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 { func RunPowershellCmd(command string, envs ...string) ([]byte, error) { // only one powershell command can be executed at a time to avoid OOM - mutex.Lock() - defer mutex.Unlock() + powershellCmdMutex.Lock() + defer powershellCmdMutex.Unlock() cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command) cmd.Env = append(os.Environ(), envs...)