Skip to content

Commit 1f9673a

Browse files
author
JojiiOfficial
committed
improve ns load
1 parent 2b2bcc4 commit 1f9673a

File tree

4 files changed

+80
-33
lines changed

4 files changed

+80
-33
lines changed

dmfs/data.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dmfs
22

33
import (
44
"os"
5+
"time"
56

67
"github.com/DataManager-Go/libdatamanager"
78
libdm "github.com/DataManager-Go/libdatamanager"
@@ -16,8 +17,10 @@ type dataStruct struct {
1617
config *dmConfig.Config
1718
libdm *libdatamanager.LibDM
1819

19-
userAttributes *libdm.UserAttributeDataResponse
20-
gid, uid uint32
20+
userAttributes *libdm.UserAttributeDataResponse
21+
lastUserAttrLoad int64
22+
23+
gid, uid uint32
2124
}
2225

2326
func initData() {
@@ -27,10 +30,17 @@ func initData() {
2730

2831
// load user attributes (namespaces, groups)
2932
func (data *dataStruct) loadUserAttributes() error {
30-
var err error
31-
data.userAttributes, err = data.libdm.GetUserAttributeData()
32-
if err != nil {
33-
return err
33+
// TODO make cachhe time configureable or even create
34+
// a genuius way to calculate a reasonable cachetime
35+
36+
if time.Now().Unix()-10 > data.lastUserAttrLoad {
37+
var err error
38+
data.userAttributes, err = data.libdm.GetUserAttributeData()
39+
if err != nil {
40+
return err
41+
}
42+
43+
data.lastUserAttrLoad = time.Now().Unix()
3444
}
3545

3646
return nil

dmfs/dmfsRoot.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,81 @@ import (
77
"syscall"
88
"time"
99

10+
"github.com/DataManager-Go/libdatamanager"
1011
"github.com/hanwen/go-fuse/v2/fs"
12+
"github.com/hanwen/go-fuse/v2/fuse"
1113
)
1214

1315
type rootNode struct {
1416
fs.Inode
17+
18+
nsMap map[string][]string
1519
}
1620

1721
// implement the interfaces
18-
var _ = (fs.NodeOnAdder)((*rootNode)(nil))
22+
var _ = (fs.NodeReaddirer)((*rootNode)(nil))
1923
var _ = (fs.NodeRenamer)((*rootNode)(nil))
2024
var _ = (fs.NodeRmdirer)((*rootNode)(nil))
25+
var _ = (fs.NodeLookuper)((*rootNode)(nil))
2126

2227
// OnAdd is called on mounting the file system. Use it to populate
2328
// the file system tree.
24-
func (root *rootNode) OnAdd(ctx context.Context) {
25-
root.debug("Init files")
29+
func (root *rootNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
30+
r := make([]fuse.DirEntry, 0)
2631

2732
err := data.loadUserAttributes()
2833
if err != nil {
2934
log.Fatal(err)
30-
return
35+
return nil, syscall.EPERM
36+
}
37+
38+
if root.nsMap == nil {
39+
root.nsMap = make(map[string][]string, 0)
3140
}
3241

3342
// Loop Namespaces and add childs in as folders
3443
for _, namespace := range data.userAttributes.Namespace {
3544
nsName := removeNSName(namespace.Name)
3645

37-
// reuse child
38-
nsp := root.Inode.GetChild(nsName)
46+
_, has := root.nsMap[namespace.Name]
47+
if !has {
48+
root.nsMap[namespace.Name] = namespace.Groups
49+
}
3950

40-
// Create namespace folder
41-
if nsp == nil {
42-
nsp = root.Inode.NewInode(ctx, &namespaceNode{
43-
namespace: nsName,
44-
groups: namespace.Groups,
45-
}, fs.StableAttr{
46-
Mode: syscall.S_IFDIR,
47-
})
51+
r = append(r, fuse.DirEntry{
52+
Name: nsName,
53+
Mode: syscall.S_IFDIR,
54+
})
55+
}
4856

49-
root.AddChild(nsName, nsp, true)
50-
}
57+
return fs.NewListDirStream(r), 0
58+
}
59+
60+
func (root *rootNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
61+
fullNS := addNSName(name, data.mounter.Libdm.Config)
62+
63+
// Get cached namespaceInfo from map
64+
val, has := root.nsMap[fullNS]
65+
if !has {
66+
return nil, syscall.ENOENT
67+
}
68+
69+
// Try to reuse child
70+
child := root.GetChild(name)
71+
72+
// Create new child if not found
73+
if child == nil {
74+
child = root.NewInode(ctx, &namespaceNode{
75+
nsInfo: &libdatamanager.Namespaceinfo{
76+
Name: fullNS,
77+
Groups: val,
78+
},
79+
}, fs.StableAttr{
80+
Mode: syscall.S_IFDIR,
81+
})
5182
}
5283

53-
root.debug("Init files success")
84+
return child, 0
5485
}
5586

5687
// Delete Namespace if virtual file was unlinked

dmfs/fsNamespace.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import (
66
"syscall"
77
"time"
88

9-
"github.com/DataManager-Go/libdatamanager"
9+
libdm "github.com/DataManager-Go/libdatamanager"
1010
"github.com/hanwen/go-fuse/v2/fs"
1111
"github.com/hanwen/go-fuse/v2/fuse"
1212
)
1313

1414
type namespaceNode struct {
1515
fs.Inode
1616

17-
namespace string
18-
groups []string
17+
nsInfo *libdm.Namespaceinfo
1918
}
2019

2120
var _ = (fs.NodeOnAdder)((*namespaceNode)(nil))
@@ -26,17 +25,17 @@ var _ = (fs.NodeRenamer)((*namespaceNode)(nil))
2625
func (nsNode *namespaceNode) OnAdd(ctx context.Context) {
2726
// Use a no_group folder for files
2827
// not associated to a groud
29-
if len(nsNode.groups) == 0 {
30-
nsNode.groups = []string{"no_group"}
28+
if len(nsNode.nsInfo.Groups) == 0 {
29+
nsNode.nsInfo.Groups = []string{"no_group"}
3130
}
3231

3332
// Add groups to namespace
34-
for _, group := range nsNode.groups {
33+
for _, group := range nsNode.nsInfo.Groups {
3534
gp := nsNode.GetChild(group)
3635
if gp == nil {
3736
gp = nsNode.NewInode(ctx, &groupInode{
3837
group: group,
39-
namespace: nsNode.namespace,
38+
namespace: nsNode.nsInfo.Name,
4039
}, fs.StableAttr{
4140
Mode: syscall.S_IFDIR,
4241
})
@@ -56,7 +55,7 @@ func (nsNode *namespaceNode) Rmdir(ctx context.Context, name string) syscall.Err
5655
}
5756

5857
// Do http delete request
59-
_, err := data.libdm.DeleteAttribute(libdatamanager.GroupAttribute, nsNode.namespace, name)
58+
_, err := data.libdm.DeleteAttribute(libdm.GroupAttribute, nsNode.nsInfo.Name, name)
6059
if err != nil {
6160
fmt.Println(err)
6261
return syscall.ENOENT
@@ -72,7 +71,7 @@ func (nsNode *namespaceNode) Rename(ctx context.Context, name string, newParent
7271
return 0
7372
}
7473

75-
_, err := data.libdm.UpdateAttribute(libdatamanager.GroupAttribute, nsNode.namespace, name, newName)
74+
_, err := data.libdm.UpdateAttribute(libdm.GroupAttribute, nsNode.nsInfo.Name, name, newName)
7675
if err != nil {
7776
return syscall.ENOENT
7877
}
@@ -84,7 +83,7 @@ func (nsNode *namespaceNode) Rename(ctx context.Context, name string, newParent
8483
func (nsNode *namespaceNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
8584
node := nsNode.NewInode(ctx, &groupInode{
8685
group: name,
87-
namespace: nsNode.namespace,
86+
namespace: nsNode.nsInfo.Name,
8887
}, fs.StableAttr{
8988
Mode: syscall.S_IFDIR,
9089
})

dmfs/mount.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dmfs
33
import (
44
"fmt"
55
"log"
6+
"math/rand"
67
"os"
78
"os/signal"
89
"path/filepath"
@@ -36,6 +37,8 @@ func (mounter *Mounter) Mount() {
3637
log.Fatal(err)
3738
}
3839

40+
rand.Seed(time.Now().Unix())
41+
3942
// Test server availability
4043
if !mounter.testServer() {
4144
return
@@ -122,13 +125,17 @@ func (mounter *Mounter) testServer() bool {
122125

123126
// Get the mountoptions for the mount operation
124127
func (mounter *Mounter) getMountOptions() *fs.Options {
128+
sec := time.Second
129+
125130
return &fs.Options{
126131
MountOptions: fuse.MountOptions{
127132
Debug: mounter.DebugFS,
128133
AllowOther: false,
129134
FsName: "Datamanager mount",
130135
Name: "dmanager",
131136
},
137+
EntryTimeout: &sec,
138+
AttrTimeout: &sec,
132139
}
133140
}
134141

0 commit comments

Comments
 (0)