Skip to content

Commit 1d9cb96

Browse files
committed
Merge branch '524-deduplicate-mount-entries' into 'master'
fix: eliminate the duplicate mount entries in /proc/mounts (#524) Closes #524 See merge request postgres-ai/database-lab!1063
2 parents 06ac987 + 096f087 commit 1d9cb96

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

engine/internal/retrieval/engine/postgres/logical/logical.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func isAlreadyMounted(mounts []mount.Mount, dir string) bool {
2828
dir = strings.Trim(dir, "/")
2929

3030
for _, mountPoint := range mounts {
31-
if strings.Trim(mountPoint.Source, "/") == dir {
31+
if strings.Trim(mountPoint.Source, "/") == dir || strings.Trim(mountPoint.Target, "/") == dir {
3232
return true
3333
}
3434
}

engine/internal/retrieval/engine/postgres/logical/logical_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ func TestIsAlreadyMounted(t *testing.T) {
3838
dumpLocation: "/var/lib/dblab/new_pool/dump",
3939
expectedResult: false,
4040
},
41+
{
42+
source: []mount.Mount{{Source: "/host/path/dump", Target: "/var/lib/dblab/pool/dump"}},
43+
dumpLocation: "/var/lib/dblab/pool/dump",
44+
expectedResult: true,
45+
},
46+
{
47+
source: []mount.Mount{{Source: "/host/path/dump", Target: "/var/lib/dblab/pool/dump/"}},
48+
dumpLocation: "/var/lib/dblab/pool/dump",
49+
expectedResult: true,
50+
},
4151
}
4252

4353
for _, tc := range testCases {

engine/internal/retrieval/engine/postgres/tools/tools.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func AddVolumesToHostConfig(ctx context.Context, docker *client.Client, hostConf
183183
// GetMountsFromMountPoints creates a list of mounts.
184184
func GetMountsFromMountPoints(dataDir string, mountPoints []types.MountPoint) []mount.Mount {
185185
mounts := make([]mount.Mount, 0, len(mountPoints))
186+
seen := make(map[string]struct{})
186187

187188
for _, mountPoint := range mountPoints {
188189
// Rewrite mounting to data directory.
@@ -192,6 +193,18 @@ func GetMountsFromMountPoints(dataDir string, mountPoints []types.MountPoint) []
192193
mountPoint.Destination = dataDir
193194
}
194195

196+
// Deduplicate mounts by normalizing paths and checking both source and target.
197+
normalizedSource := strings.Trim(mountPoint.Source, "/")
198+
normalizedTarget := strings.Trim(mountPoint.Destination, "/")
199+
mountKey := normalizedSource + "|" + normalizedTarget
200+
201+
if _, ok := seen[mountKey]; ok {
202+
log.Dbg("skipping duplicate mount", mountPoint.Source, "to", mountPoint.Destination)
203+
continue
204+
}
205+
206+
seen[mountKey] = struct{}{}
207+
195208
mounts = append(mounts, mount.Mount{
196209
Type: mountPoint.Type,
197210
Source: mountPoint.Source,

engine/internal/retrieval/engine/postgres/tools/tools_test.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ func TestIfDirectoryEmpty(t *testing.T) {
3737

3838
func TestGetMountsFromMountPoints(t *testing.T) {
3939
testCases := []struct {
40+
name string
4041
dataDir string
4142
mountPoints []types.MountPoint
4243
expectedPoints []mount.Mount
4344
}{
4445
{
46+
name: "simple mount without transformation",
4547
dataDir: "/var/lib/dblab/clones/dblab_clone_6000/data",
4648
mountPoints: []types.MountPoint{{
4749
Source: "/var/lib/pgsql/data",
@@ -56,8 +58,8 @@ func TestGetMountsFromMountPoints(t *testing.T) {
5658
},
5759
}},
5860
},
59-
6061
{
62+
name: "mount with path transformation",
6163
dataDir: "/var/lib/dblab/clones/dblab_clone_6000/data",
6264
mountPoints: []types.MountPoint{{
6365
Source: "/var/lib/postgresql",
@@ -72,10 +74,44 @@ func TestGetMountsFromMountPoints(t *testing.T) {
7274
},
7375
}},
7476
},
77+
{
78+
name: "deduplicate identical mounts",
79+
dataDir: "/var/lib/dblab/data",
80+
mountPoints: []types.MountPoint{
81+
{Source: "/host/dump", Destination: "/var/lib/dblab/dump"},
82+
{Source: "/host/dump", Destination: "/var/lib/dblab/dump"},
83+
},
84+
expectedPoints: []mount.Mount{{
85+
Source: "/host/dump",
86+
Target: "/var/lib/dblab/dump",
87+
ReadOnly: true,
88+
BindOptions: &mount.BindOptions{
89+
Propagation: "",
90+
},
91+
}},
92+
},
93+
{
94+
name: "deduplicate mounts with trailing slashes",
95+
dataDir: "/var/lib/dblab/data",
96+
mountPoints: []types.MountPoint{
97+
{Source: "/host/dump/", Destination: "/var/lib/dblab/dump"},
98+
{Source: "/host/dump", Destination: "/var/lib/dblab/dump/"},
99+
},
100+
expectedPoints: []mount.Mount{{
101+
Source: "/host/dump/",
102+
Target: "/var/lib/dblab/dump",
103+
ReadOnly: true,
104+
BindOptions: &mount.BindOptions{
105+
Propagation: "",
106+
},
107+
}},
108+
},
75109
}
76110

77111
for _, tc := range testCases {
78-
mounts := GetMountsFromMountPoints(tc.dataDir, tc.mountPoints)
79-
assert.Equal(t, tc.expectedPoints, mounts)
112+
t.Run(tc.name, func(t *testing.T) {
113+
mounts := GetMountsFromMountPoints(tc.dataDir, tc.mountPoints)
114+
assert.Equal(t, tc.expectedPoints, mounts)
115+
})
80116
}
81117
}

engine/test/_cleanup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -euxo pipefail
33

44
DLE_TEST_MOUNT_DIR="/var/lib/test/dblab_mount"
55
DLE_TEST_POOL_NAME="test_dblab_pool"
6-
TMP_DATA_DIR="/tmp/dle_test/logical_generic"
6+
TMP_DATA_DIR="/tmp/dle_test"
77
ZFS_FILE="$(pwd)/zfs_file"
88

99
# Stop and remove test Docker containers

0 commit comments

Comments
 (0)