From 462ae6a405ea626523e5a0afe18c88723add3638 Mon Sep 17 00:00:00 2001 From: Sergey Berezansky Date: Tue, 3 Sep 2024 17:40:24 +0300 Subject: [PATCH 1/2] feat(CSI-247): implement InterfaceGroup.GetRandomIpAddress() --- pkg/wekafs/apiclient/interfacegroup.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/wekafs/apiclient/interfacegroup.go b/pkg/wekafs/apiclient/interfacegroup.go index c4c181ab..09b75e7d 100644 --- a/pkg/wekafs/apiclient/interfacegroup.go +++ b/pkg/wekafs/apiclient/interfacegroup.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/google/uuid" "github.com/rs/zerolog/log" + "k8s.io/apimachinery/pkg/util/rand" "k8s.io/helm/pkg/urlutil" "os" "sort" @@ -93,6 +94,20 @@ func (i *InterfaceGroup) GetIpAddress(ctx context.Context) (string, error) { return i.Ips[idx], nil } +func (i *InterfaceGroup) GetRandomIpAddress(ctx context.Context) (string, error) { + logger := log.Ctx(ctx) + if i == nil { + return "", errors.New("interface group is nil") + } + if len(i.Ips) == 0 { + return "", errors.New("no IP addresses found for interface group") + } + idx := rand.Intn(len(i.Ips)) + ip := i.Ips[idx] + logger.Debug().Str("ip", ip).Msg("Selected random IP address") + return ip, nil +} + func (a *ApiClient) GetInterfaceGroups(ctx context.Context, interfaceGroups *[]InterfaceGroup) error { ig := &InterfaceGroup{} @@ -191,5 +206,5 @@ func (a *ApiClient) GetNfsMountIp(ctx context.Context, interfaceGroupName *strin return "", errors.New("no IP addresses found for NFS interface group") } - return ig.GetIpAddress(ctx) + return ig.GetRandomIpAddress(ctx) } From 43c8d6ac80448dcef16cdb680242e613a08d412a Mon Sep 17 00:00:00 2001 From: Sergey Berezansky Date: Thu, 5 Sep 2024 09:51:04 +0300 Subject: [PATCH 2/2] feat(CSI-247): optimize NFS by utilizing multiple targets --- pkg/wekafs/nfsmount.go | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) diff --git a/pkg/wekafs/nfsmount.go b/pkg/wekafs/nfsmount.go index f14a35c6..25496263 100644 --- a/pkg/wekafs/nfsmount.go +++ b/pkg/wekafs/nfsmount.go @@ -10,15 +10,12 @@ import ( "os" "path/filepath" "strings" - "sync" "time" ) type nfsMount struct { fsName string mountPoint string - refCount int - lock sync.Mutex kMounter mount.Interface debugPath string mountOptions MountOptions @@ -33,7 +30,7 @@ func (m *nfsMount) getMountPoint() string { } func (m *nfsMount) getRefCount() int { - return m.refCount + return 0 } func (m *nfsMount) getMountOptions() MountOptions { @@ -53,44 +50,15 @@ func (m *nfsMount) isMounted() bool { } func (m *nfsMount) incRef(ctx context.Context, apiClient *apiclient.ApiClient) error { - logger := log.Ctx(ctx) - m.lock.Lock() - defer m.lock.Unlock() - if m.refCount < 0 { - logger.Error().Str("mount_point", m.mountPoint).Int("refcount", m.refCount).Msg("During incRef negative refcount encountered") - m.refCount = 0 // to make sure that we don't have negative refcount later - } - if m.refCount == 0 { - if err := m.doMount(ctx, apiClient, m.mountOptions); err != nil { - return err - } - } else if !m.isMounted() { - logger.Warn().Str("mount_point", m.mountPoint).Int("refcount", m.refCount).Msg("Mount not exists although should!") - if err := m.doMount(ctx, apiClient, m.mountOptions); err != nil { - return err - } - + if err := m.doMount(ctx, apiClient, m.mountOptions); err != nil { + return err } - m.refCount++ - logger.Trace().Int("refcount", m.refCount).Strs("mount_options", m.mountOptions.Strings()).Str("filesystem_name", m.fsName).Msg("RefCount increased") return nil } func (m *nfsMount) decRef(ctx context.Context) error { - logger := log.Ctx(ctx) - m.lock.Lock() - defer m.lock.Unlock() - m.refCount-- - m.lastUsed = time.Now() - logger.Trace().Int("refcount", m.refCount).Strs("mount_options", m.mountOptions.Strings()).Str("filesystem_name", m.fsName).Msg("RefCount decreased") - if m.refCount < 0 { - logger.Error().Int("refcount", m.refCount).Msg("During decRef negative refcount encountered") - m.refCount = 0 // to make sure that we don't have negative refcount later - } - if m.refCount == 0 { - if err := m.doUnmount(ctx); err != nil { - return err - } + if err := m.doUnmount(ctx); err != nil { + return err } return nil }