mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
Files, a directory longer than a listing page, renames, hard and symbolic links, tree removal and a bucket made through the mount, checked against the rows, then read back by a second mount with an empty metadata cache. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
236 lines
8.1 KiB
Go
236 lines
8.1 KiB
Go
//go:build linux
|
|
|
|
package postgres_flat
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// mount mounts the filer root with a metadata cache of its own, so a new
|
|
// mount reads everything back from the store.
|
|
func (c *cluster) mount(name string) string {
|
|
c.t.Helper()
|
|
if _, err := os.Stat("/dev/fuse"); err != nil {
|
|
c.t.Skip("no /dev/fuse")
|
|
}
|
|
dir := filepath.Join(c.baseDir, name)
|
|
cacheDir := filepath.Join(c.baseDir, name+"-cache")
|
|
require.NoError(c.t, os.MkdirAll(dir, 0o755))
|
|
require.NoError(c.t, os.MkdirAll(cacheDir, 0o755))
|
|
cmd := c.start(name, "mount", "-filer="+c.filerAddr, "-dir="+dir, "-filer.path=/", "-cacheDir="+cacheDir, "-allowOthers=false")
|
|
c.t.Cleanup(func() {
|
|
_ = exec.Command("fusermount3", "-u", dir).Run()
|
|
_ = exec.Command("fusermount", "-u", dir).Run()
|
|
})
|
|
|
|
var parent syscall.Stat_t
|
|
require.NoError(c.t, syscall.Stat(c.baseDir, &parent))
|
|
probe := filepath.Join(dir, ".probe-"+name)
|
|
deadline := time.Now().Add(90 * time.Second)
|
|
for {
|
|
var st syscall.Stat_t
|
|
if syscall.Stat(dir, &st) == nil && st.Dev != parent.Dev {
|
|
if err := os.WriteFile(probe, []byte("probe"), 0o644); err == nil {
|
|
require.NoError(c.t, os.Remove(probe))
|
|
return dir
|
|
}
|
|
}
|
|
if cmd.ProcessState != nil || time.Now().After(deadline) {
|
|
c.t.Fatalf("mount %s not ready:\n%s", name, c.logTail(name))
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func unmount(t *testing.T, dir string) {
|
|
t.Helper()
|
|
out, err := exec.Command("fusermount3", "-u", dir).CombinedOutput()
|
|
if err != nil {
|
|
out, err = exec.Command("fusermount", "-u", dir).CombinedOutput()
|
|
}
|
|
require.NoError(t, err, "unmount %s: %s", dir, out)
|
|
}
|
|
|
|
// tree lists every file under root with its content, and every directory
|
|
// with a trailing slash.
|
|
func tree(t *testing.T, root string) map[string]string {
|
|
t.Helper()
|
|
got := map[string]string{}
|
|
require.NoError(t, filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil || p == root {
|
|
return err
|
|
}
|
|
rel, _ := filepath.Rel(root, p)
|
|
switch {
|
|
case d.IsDir():
|
|
got[rel+"/"] = ""
|
|
case d.Type()&fs.ModeSymlink != 0:
|
|
target, err := os.Readlink(p)
|
|
got[rel+" -> "+target] = ""
|
|
return err
|
|
default:
|
|
data, err := os.ReadFile(p)
|
|
got[rel] = string(data)
|
|
return err
|
|
}
|
|
return nil
|
|
}))
|
|
return got
|
|
}
|
|
|
|
func TestPosix(t *testing.T) {
|
|
c := startCluster(t)
|
|
mnt := c.mount("mount")
|
|
root := filepath.Join(mnt, "posix")
|
|
write := func(t *testing.T, rel, body string) {
|
|
t.Helper()
|
|
p := filepath.Join(root, rel)
|
|
require.NoError(t, os.MkdirAll(filepath.Dir(p), 0o755))
|
|
require.NoError(t, os.WriteFile(p, []byte(body), 0o644))
|
|
}
|
|
read := func(t *testing.T, rel string) string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(filepath.Join(root, rel))
|
|
require.NoError(t, err)
|
|
return string(data)
|
|
}
|
|
kvRows := func() int { return c.count(`SELECT count(*) FROM filemeta_kv`) }
|
|
|
|
t.Run("files and directories are rows keyed by their path", func(t *testing.T) {
|
|
write(t, "a/b/f.txt", "f")
|
|
write(t, "a/g.txt", "g")
|
|
require.Equal(t, []string{"/posix", "/posix/a", "/posix/a/b", "/posix/a/b/f.txt", "/posix/a/g.txt"}, c.paths("filemeta", "/posix"))
|
|
require.NoError(t, os.WriteFile(filepath.Join(root, "a/g.txt"), []byte("g2"), 0o644))
|
|
require.Equal(t, "g2", read(t, "a/g.txt"))
|
|
require.NoError(t, os.Chmod(filepath.Join(root, "a/g.txt"), 0o600))
|
|
st, err := os.Stat(filepath.Join(root, "a/g.txt"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, os.FileMode(0o600), st.Mode().Perm())
|
|
})
|
|
|
|
t.Run("a directory longer than a listing page", func(t *testing.T) {
|
|
require.NoError(t, os.Mkdir(filepath.Join(root, "many"), 0o755))
|
|
var want []string
|
|
for i := 0; i < filer.PaginationSize+100; i++ {
|
|
name := fmt.Sprintf("f%05d", i)
|
|
want = append(want, name)
|
|
f, err := os.Create(filepath.Join(root, "many", name))
|
|
require.NoError(t, err)
|
|
require.NoError(t, f.Close())
|
|
}
|
|
entries, err := os.ReadDir(filepath.Join(root, "many"))
|
|
require.NoError(t, err)
|
|
var got []string
|
|
for _, e := range entries {
|
|
got = append(got, e.Name())
|
|
}
|
|
require.Equal(t, want, got)
|
|
require.Equal(t, want, c.filerList("/posix/many", 100))
|
|
require.Equal(t, len(want), c.count(`SELECT count(*) FROM filemeta WHERE directory = '/posix/many'`))
|
|
})
|
|
|
|
t.Run("rename moves rows", func(t *testing.T) {
|
|
write(t, "mv/src/one.txt", "one")
|
|
write(t, "mv/src/sub/two.txt", "two")
|
|
require.NoError(t, os.Rename(filepath.Join(root, "mv/src/one.txt"), filepath.Join(root, "mv/uno.txt")))
|
|
require.Equal(t, "one", read(t, "mv/uno.txt"))
|
|
require.NoError(t, os.Rename(filepath.Join(root, "mv/src"), filepath.Join(root, "mv/dst")))
|
|
require.Equal(t, "two", read(t, "mv/dst/sub/two.txt"))
|
|
require.Equal(t, []string{"/posix/mv", "/posix/mv/dst", "/posix/mv/dst/sub", "/posix/mv/dst/sub/two.txt", "/posix/mv/uno.txt"}, c.paths("filemeta", "/posix/mv"))
|
|
})
|
|
|
|
t.Run("hard links keep their record in the key-value table", func(t *testing.T) {
|
|
before := kvRows()
|
|
write(t, "links/orig.txt", "linked")
|
|
require.NoError(t, os.Link(filepath.Join(root, "links/orig.txt"), filepath.Join(root, "links/hard.txt")))
|
|
require.Greater(t, kvRows(), before)
|
|
require.Equal(t, "linked", read(t, "links/hard.txt"))
|
|
st, err := os.Stat(filepath.Join(root, "links/hard.txt"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(2), uint64(st.Sys().(*syscall.Stat_t).Nlink))
|
|
require.NoError(t, os.Remove(filepath.Join(root, "links/orig.txt")))
|
|
require.Equal(t, "linked", read(t, "links/hard.txt"))
|
|
|
|
require.NoError(t, os.Symlink("hard.txt", filepath.Join(root, "links/soft")))
|
|
target, err := os.Readlink(filepath.Join(root, "links/soft"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, "hard.txt", target)
|
|
require.Equal(t, "linked", read(t, "links/soft"))
|
|
})
|
|
|
|
t.Run("remove a tree", func(t *testing.T) {
|
|
write(t, "gone/x/y/z.txt", "z")
|
|
write(t, "gone/w.txt", "w")
|
|
write(t, "gone2/kept.txt", "kept")
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "gone")))
|
|
require.Empty(t, c.paths("filemeta", "/posix/gone/"))
|
|
require.Equal(t, []string{"/posix/gone2", "/posix/gone2/kept.txt"}, c.paths("filemeta", "/posix/gone"))
|
|
})
|
|
|
|
t.Run("bucket made through the mount", func(t *testing.T) {
|
|
bucket := "pgflat-posix"
|
|
bucketDir := filepath.Join(mnt, "buckets", bucket)
|
|
require.NoError(t, os.Mkdir(bucketDir, 0o755))
|
|
eventually(t, "the bucket has a table", func() bool { return c.tableExists(bucket) })
|
|
require.NoError(t, os.MkdirAll(filepath.Join(bucketDir, "k"), 0o755))
|
|
require.NoError(t, os.WriteFile(filepath.Join(bucketDir, "k/v.txt"), []byte("from mount"), 0o644))
|
|
require.Equal(t, []string{"/k", "/k/v.txt"}, c.paths(bucket, "/"))
|
|
|
|
client := c.s3Client()
|
|
ctx := context.Background()
|
|
out, err := client.GetObject(ctx, &s3.GetObjectInput{Bucket: aws.String(bucket), Key: aws.String("k/v.txt")})
|
|
require.NoError(t, err)
|
|
data, err := io.ReadAll(out.Body)
|
|
out.Body.Close()
|
|
require.NoError(t, err)
|
|
require.Equal(t, "from mount", string(data))
|
|
|
|
_, err = client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String("k/s3.txt"), Body: strings.NewReader("from s3")})
|
|
require.NoError(t, err)
|
|
eventually(t, "the mount sees the S3 object", func() bool {
|
|
data, err := os.ReadFile(filepath.Join(bucketDir, "k/s3.txt"))
|
|
return err == nil && string(data) == "from s3"
|
|
})
|
|
|
|
require.NoError(t, os.RemoveAll(bucketDir))
|
|
eventually(t, "the bucket table is dropped", func() bool { return !c.tableExists(bucket) })
|
|
})
|
|
|
|
t.Run("a new mount reads the same tree from the store", func(t *testing.T) {
|
|
want := tree(t, root)
|
|
require.Contains(t, want, "mv/dst/sub/two.txt")
|
|
require.Contains(t, want, "links/soft -> hard.txt")
|
|
require.Contains(t, want, fmt.Sprintf("many/f%05d", filer.PaginationSize+99))
|
|
unmount(t, mnt)
|
|
fresh := c.mount("remount")
|
|
got := tree(t, filepath.Join(fresh, "posix"))
|
|
require.Equal(t, keys(want), keys(got))
|
|
require.Equal(t, want, got)
|
|
})
|
|
}
|
|
|
|
func keys(m map[string]string) []string {
|
|
var ks []string
|
|
for k := range m {
|
|
ks = append(ks, k)
|
|
}
|
|
sort.Strings(ks)
|
|
return ks
|
|
}
|