Skip to content

Commit

Permalink
refactor(CSI-250): remove mountMap ref for NFS
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyberezansky committed Sep 11, 2024
1 parent 2cc3eda commit 806b508
Showing 1 changed file with 50 additions and 76 deletions.
126 changes: 50 additions & 76 deletions pkg/wekafs/nfsmounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import (
"github.com/rs/zerolog/log"
"github.com/wekafs/csi-wekafs/pkg/wekafs/apiclient"
"k8s.io/mount-utils"
"sync"
"time"
)

type nfsMounter struct {
mountMap mountsMap
lock sync.Mutex
kMounter mount.Interface
debugPath string
selinuxSupport *bool
Expand All @@ -30,7 +27,7 @@ func newNfsMounter(driver *WekaFsDriver) *nfsMounter {
log.Debug().Msg("SELinux support is forced")
selinuxSupport = &[]bool{true}[0]
}
mounter := &nfsMounter{mountMap: mountsMap{}, debugPath: driver.debugPath, selinuxSupport: selinuxSupport}
mounter := &nfsMounter{debugPath: driver.debugPath, selinuxSupport: selinuxSupport}
mounter.gc = initInnerPathVolumeGc(mounter)
mounter.schedulePeriodicMountGc()
mounter.interfaceGroupName = &driver.config.interfaceGroupName
Expand All @@ -40,28 +37,20 @@ func newNfsMounter(driver *WekaFsDriver) *nfsMounter {
}

func (m *nfsMounter) NewMount(fsName string, options MountOptions) AnyMount {
m.lock.Lock()
if m.kMounter == nil {
m.kMounter = mount.New("")
}
if _, ok := m.mountMap[fsName]; !ok {
m.mountMap[fsName] = mountsMapPerFs{}
uniqueId := getStringSha1AsB32(fsName + ":" + options.String())
wMount := &nfsMount{
kMounter: m.kMounter,
fsName: fsName,
debugPath: m.debugPath,
mountPoint: "/run/weka-fs-mounts/" + getAsciiPart(fsName, 64) + "-" + uniqueId,
mountOptions: options,
interfaceGroupName: m.interfaceGroupName,
clientGroupName: m.clientGroupName,
}
if _, ok := m.mountMap[fsName][options.AsMapKey()]; !ok {
uniqueId := getStringSha1AsB32(fsName + ":" + options.String())
wMount := &nfsMount{
kMounter: m.kMounter,
fsName: fsName,
debugPath: m.debugPath,
mountPoint: "/run/weka-fs-mounts/" + getAsciiPart(fsName, 64) + "-" + uniqueId,
mountOptions: options,
interfaceGroupName: m.interfaceGroupName,
clientGroupName: m.clientGroupName,
}
m.mountMap[fsName][options.AsMapKey()] = wMount
}
m.lock.Unlock()
return m.mountMap[fsName][options.AsMapKey()]
return wMount
}

func (m *nfsMounter) getSelinuxStatus(ctx context.Context) bool {
Expand Down Expand Up @@ -97,68 +86,53 @@ func (m *nfsMounter) Mount(ctx context.Context, fs string, apiClient *apiclient.
func (m *nfsMounter) unmountWithOptions(ctx context.Context, fsName string, options MountOptions) error {
opts := options
options.setSelinux(m.getSelinuxStatus(ctx), MountProtocolNfs)
options = options.AsNfs()
mnt := m.NewMount(fsName, options)

log.Ctx(ctx).Trace().Strs("mount_options", opts.Strings()).Str("filesystem", fsName).Msg("Received an unmount request")
if mnt, ok := m.mountMap[fsName][options.AsMapKey()]; ok {
err := mnt.decRef(ctx)
if err == nil {
if m.mountMap[fsName][options.AsMapKey()].getRefCount() <= 0 {
log.Ctx(ctx).Trace().Str("filesystem", fsName).Strs("mount_options", options.Strings()).Msg("This is a last use of this mount, removing from map")
delete(m.mountMap[fsName], options.String())
}
if len(m.mountMap[fsName]) == 0 {
log.Ctx(ctx).Trace().Str("filesystem", fsName).Msg("No more mounts to filesystem, removing from map")
delete(m.mountMap, fsName)
}
}
return err

} else {
log.Ctx(ctx).Warn().Msg("Attempted to access mount point which is not known to the system")
return nil
}
return mnt.decRef(ctx)
}

func (m *nfsMounter) LogActiveMounts() {
if len(m.mountMap) > 0 {
count := 0
for fsName := range m.mountMap {
for mnt := range m.mountMap[fsName] {
mapEntry := m.mountMap[fsName][mnt]
if mapEntry.getRefCount() > 0 {
log.Trace().Str("filesystem", fsName).Int("refcount", mapEntry.getRefCount()).Strs("mount_options", mapEntry.getMountOptions().Strings()).Msg("Mount is active")
count++
} else {
log.Trace().Str("filesystem", fsName).Int("refcount", mapEntry.getRefCount()).Strs("mount_options", mapEntry.getMountOptions().Strings()).Msg("Mount is not active")
}

}
}
log.Debug().Int("total", len(m.mountMap)).Int("active", count).Msg("Periodic checkup on mount map")
}
//if len(m.mountMap) > 0 {
// count := 0
// for fsName := range m.mountMap {
// for mnt := range m.mountMap[fsName] {
// mapEntry := m.mountMap[fsName][mnt]
// if mapEntry.getRefCount() > 0 {
// log.Trace().Str("filesystem", fsName).Int("refcount", mapEntry.getRefCount()).Strs("mount_options", mapEntry.getMountOptions().Strings()).Msg("Mount is active")
// count++
// } else {
// log.Trace().Str("filesystem", fsName).Int("refcount", mapEntry.getRefCount()).Strs("mount_options", mapEntry.getMountOptions().Strings()).Msg("Mount is not active")
// }
//
// }
// }
// log.Debug().Int("total", len(m.mountMap)).Int("active", count).Msg("Periodic checkup on mount map")
//}
}

func (m *nfsMounter) gcInactiveMounts() {
if len(m.mountMap) > 0 {
for fsName := range m.mountMap {
for uniqueId, wekaMount := range m.mountMap[fsName] {
if wekaMount.getRefCount() == 0 {
if wekaMount.getLastUsed().Before(time.Now().Add(-inactiveMountGcPeriod)) {
m.lock.Lock()
if wekaMount.getRefCount() == 0 {
log.Trace().Str("filesystem", fsName).Strs("mount_options", wekaMount.getMountOptions().Strings()).
Time("last_used", wekaMount.getLastUsed()).Msg("Removing stale mount from map")
delete(m.mountMap[fsName], uniqueId)
}
m.lock.Unlock()
}
}
}
if len(m.mountMap[fsName]) == 0 {
delete(m.mountMap, fsName)
}
}
}
//if len(m.mountMap) > 0 {
// for fsName := range m.mountMap {
// for uniqueId, wekaMount := range m.mountMap[fsName] {
// if wekaMount.getRefCount() == 0 {
// if wekaMount.getLastUsed().Before(time.Now().Add(-inactiveMountGcPeriod)) {
// m.lock.Lock()
// if wekaMount.getRefCount() == 0 {
// log.Trace().Str("filesystem", fsName).Strs("mount_options", wekaMount.getMountOptions().Strings()).
// Time("last_used", wekaMount.getLastUsed()).Msg("Removing stale mount from map")
// delete(m.mountMap[fsName], uniqueId)
// }
// m.lock.Unlock()
// }
// }
// }
// if len(m.mountMap[fsName]) == 0 {
// delete(m.mountMap, fsName)
// }
// }
//}
}

func (m *nfsMounter) schedulePeriodicMountGc() {
Expand Down

0 comments on commit 806b508

Please sign in to comment.