Skip to content

Commit

Permalink
Merge pull request #1943 from kubernetes-sigs/refine-GetRemoteServerF…
Browse files Browse the repository at this point in the history
…romTarget-cache-1.28

[release-1.28] fix: refine GetRemoteServerFromTarget on Windows with cache optimization
  • Loading branch information
andyzhangx authored Jun 23, 2024
2 parents 6044ab7 + 7153b3f commit da2c089
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/os/smb/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)
Expand Down

0 comments on commit da2c089

Please sign in to comment.