Skip to content

Commit da2c089

Browse files
authored
Merge pull request #1943 from kubernetes-sigs/refine-GetRemoteServerFromTarget-cache-1.28
[release-1.28] fix: refine GetRemoteServerFromTarget on Windows with cache optimization
2 parents 6044ab7 + 7153b3f commit da2c089

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pkg/os/smb/smb.go

+7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ import (
2121
"os"
2222
"path/filepath"
2323
"strings"
24+
"sync"
2425

2526
"k8s.io/klog/v2"
2627
"sigs.k8s.io/azurefile-csi-driver/pkg/util"
2728
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
2829
)
2930

31+
var getRemoteServerFromTargetMutex = &sync.Mutex{}
32+
3033
func IsSmbMapped(remotePath string) (bool, error) {
3134
cmdLine := `$(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath -ErrorAction Stop).Status`
3235
cmdEnv := fmt.Sprintf("smbremotepath=%s", remotePath)
@@ -93,6 +96,10 @@ func RemoveSmbGlobalMapping(remotePath string) error {
9396

9497
// GetRemoteServerFromTarget- gets the remote server path given a mount point, the function is recursive until it find the remote server or errors out
9598
func GetRemoteServerFromTarget(mount string, volStatsCache azcache.Resource) (string, error) {
99+
// use mutex to allow more cache hit
100+
getRemoteServerFromTargetMutex.Lock()
101+
defer getRemoteServerFromTargetMutex.Unlock()
102+
96103
cache, err := volStatsCache.Get(mount, azcache.CacheReadTypeDefault)
97104
if err != nil {
98105
return "", err

pkg/util/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
MaxPathLengthWindows = 260
3030
)
3131

32-
var mutex = &sync.Mutex{}
32+
var powershellCmdMutex = &sync.Mutex{}
3333

3434
// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB
3535
// in the unit of Bytes
@@ -68,8 +68,8 @@ func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 {
6868

6969
func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
7070
// only one powershell command can be executed at a time to avoid OOM
71-
mutex.Lock()
72-
defer mutex.Unlock()
71+
powershellCmdMutex.Lock()
72+
defer powershellCmdMutex.Unlock()
7373

7474
cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command)
7575
cmd.Env = append(os.Environ(), envs...)

0 commit comments

Comments
 (0)