mirror of
https://github.com/versity/versitygw.git
synced 2026-09-25 17:34:29 +00:00
Problem:
- `posix.New` calls `os.Chdir(rootdir)` and uses cwd-relative paths for
every bucket and object. That is the cheapest way to address files, but
the cwd is process-wide: embedding the gateway (`embedgw`) silently moves
the host program's cwd. In particular, Go unit tests that embed the
gateway can no longer read their test data files by relative path.
Change:
- New `PosixOpts.AbsolutePaths`. When set, `New` leaves the working
directory alone and builds every path from the absolute root; a relative
`VersioningDir`/`SideCarDir` is then resolved against the working
directory rather than the root. The default is unchanged: chdir and
relative paths.
- All bucket and object paths go through new `BucketPath`/`ObjectPath`,
which return the name as-is by default and prefix the root with
`AbsolutePaths`. An absolute "bucket" (the versioning directory
substitution) is passed through unchanged.
- `tmpfile` records the bucket directory path so `link()` and its fallbacks
use the same addressing; `ListBuckets` reads the root through the same
helper.
- `meta.XattrMeta` needs the same root with `AbsolutePaths`. New
`meta.RootDirSetter` interface; `posix.New` calls `WithRootDir` on
storers that implement it in that mode. A zero `XattrMeta` keeps
resolving against the cwd. `SideCar`/`NoMeta` unchanged. A type that
embeds `XattrMeta` inherits a `WithRootDir` that returns a bare
`XattrMeta`, so it needs its own (documented on `RootDirSetter`).
- `DeleteObject` (directory object), `ListParts`, and `UploadPartCopy`
passed filesystem paths where the metadata API expects bucket/object
names; they now pass names, so the sidecar layout is unchanged in both
modes.
- Windows `handleParentDirError` walks up until `filepath.Dir` is a fixed
point, which works for relative and absolute paths.
- scoutfs used cwd-relative bucket/object paths in `CreateBucket`,
`GetObject`, `HeadObject`, `RestoreObject` and the glacier walk; they now
go through `BucketPath`/`ObjectPath`. `scoutfs.New` resolves `rootdir`
before `posix.New` so a relative root no longer reopens `rootdir/rootdir`
after the chdir.
- `isBucketValid` unconditionally rejects names that do not denote a single
entry under the root: `""`, `.`, `..`, names containing a path separator,
and absolute paths. `XattrMeta` rejects `""`, `.` and `..` likewise.
With relative paths `os.Stat("")` and `os.RemoveAll(".")` failed by
accident; with absolute paths they would act on the root directory itself
(reachable with strict bucket names disabled, or via the admin
`change-bucket-owner` endpoint which does not validate `bucket`).
- scoutfs had its own `isBucketValid` whose `validateBucketName` flag was
never set, so it accepted everything. It now delegates to the new exported
`Posix.IsBucketValid`.
- `UploadPartCopy` did not validate the copy source's bucket name (unlike
`CopyObject`); it does now.
- `New` opens the root after validating the versioning and sidecar
directories, so those error paths no longer leak the root handle. The
chdir still happens first, so a relative directory resolves against the
root as before.
Tests:
- New `TestDefaultModeChangesWorkingDirectory` documents the default.
- New `TestRootDirIndependentOfWorkingDirectory`: `AbsolutePaths` with a
relative root from an unrelated cwd, checks cwd is untouched and that
put/get/list/delete, copy, multipart upload with checksums and part copy,
directory-object delete, and invalid bucket names behave correctly under
the root, for both metadata storers.
- New `TestVersioningDirIndependentOfWorkingDirectory`: same setup with a
relative versioning directory; versions land there and not under the
root or cwd.
- New `TestXattrMetaPath` covers cwd-relative and root resolution, absolute
pass-through and the rejected names.
- New `BenchmarkPosix*` benchmarks (small-object head/get/put/list, both
storers, both path modes). The default mode matches `main` within noise
on both Linux and macOS. `AbsolutePaths` costs about 0.2µs (Linux) to
0.4µs (macOS) per path lookup; on Linux (arm64 VM, overlayfs) that is
+2-3% on PutObject and +10-27% on the metadata-heavy small-object
HeadObject/GetObject/ListObjectsV2 with xattr metadata, which is why it
is opt-in.
181 lines
5.2 KiB
Go
181 lines
5.2 KiB
Go
// Copyright 2026 Versity Software
|
|
// This file is licensed under the Apache License, Version 2.0
|
|
// (the "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package posix
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
|
"github.com/versity/versitygw/backend/meta"
|
|
"github.com/versity/versitygw/s3response"
|
|
)
|
|
|
|
// Metadata-heavy object operations on small objects, where filesystem path
|
|
// resolution and metadata lookups dominate rather than data transfer. Run
|
|
// with -bench 'Posix' to compare path resolution strategies across branches.
|
|
|
|
func benchPosix(b *testing.B, mkMeta func(*testing.B) (meta.MetadataStorer, PosixOpts)) (*Posix, context.Context) {
|
|
b.Helper()
|
|
// New chdirs into the root in default mode; run from a scratch directory
|
|
// so the cwd is restored when the benchmark ends.
|
|
b.Chdir(b.TempDir())
|
|
storer, opts := mkMeta(b)
|
|
p, err := New(b.TempDir(), storer, opts)
|
|
if err != nil {
|
|
b.Fatalf("new posix: %v", err)
|
|
}
|
|
ctx := context.Background()
|
|
bucket := "bucket"
|
|
err = p.CreateBucket(ctx, &s3.CreateBucketInput{
|
|
Bucket: &bucket,
|
|
CreateBucketConfiguration: &types.CreateBucketConfiguration{},
|
|
}, []byte{})
|
|
if err != nil {
|
|
b.Fatalf("create bucket: %v", err)
|
|
}
|
|
return p, ctx
|
|
}
|
|
|
|
// benchMetaModes returns a constructor per metadata storer and path mode:
|
|
// "xattr"/"sidecar" use the default chdir-relative paths, "xattr-abs" and
|
|
// "sidecar-abs" set PosixOpts.AbsolutePaths.
|
|
func benchMetaModes(b *testing.B) map[string]func(*testing.B) (meta.MetadataStorer, PosixOpts) {
|
|
modes := map[string]func(*testing.B) (meta.MetadataStorer, PosixOpts){}
|
|
for _, abs := range []bool{false, true} {
|
|
suffix := ""
|
|
if abs {
|
|
suffix = "-abs"
|
|
}
|
|
modes["xattr"+suffix] = func(b *testing.B) (meta.MetadataStorer, PosixOpts) {
|
|
return meta.XattrMeta{}, PosixOpts{NewDirPerm: 0755, AbsolutePaths: abs}
|
|
}
|
|
modes["sidecar"+suffix] = func(b *testing.B) (meta.MetadataStorer, PosixOpts) {
|
|
dir := b.TempDir()
|
|
sc, err := meta.NewSideCar(dir)
|
|
if err != nil {
|
|
b.Fatalf("new sidecar: %v", err)
|
|
}
|
|
return sc, PosixOpts{NewDirPerm: 0755, SideCarDir: dir, AbsolutePaths: abs}
|
|
}
|
|
}
|
|
return modes
|
|
}
|
|
|
|
func benchPut(b *testing.B, p *Posix, ctx context.Context, key, body string) {
|
|
b.Helper()
|
|
bucket := "bucket"
|
|
_, err := p.PutObject(ctx, s3response.PutObjectInput{
|
|
Bucket: &bucket,
|
|
Key: &key,
|
|
Body: strings.NewReader(body),
|
|
ContentLength: aws.Int64(int64(len(body))),
|
|
})
|
|
if err != nil {
|
|
b.Fatalf("put %q: %v", key, err)
|
|
}
|
|
}
|
|
|
|
func BenchmarkPosixHeadObject(b *testing.B) {
|
|
for name, mkMeta := range benchMetaModes(b) {
|
|
b.Run(name, func(b *testing.B) {
|
|
p, ctx := benchPosix(b, mkMeta)
|
|
bucket, key := "bucket", "dir/sub/object"
|
|
benchPut(b, p, ctx, key, "hello")
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := p.HeadObject(ctx, &s3.HeadObjectInput{Bucket: &bucket, Key: &key}); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkPosixGetObject(b *testing.B) {
|
|
for name, mkMeta := range benchMetaModes(b) {
|
|
b.Run(name, func(b *testing.B) {
|
|
p, ctx := benchPosix(b, mkMeta)
|
|
bucket, key := "bucket", "dir/sub/object"
|
|
benchPut(b, p, ctx, key, "hello")
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
out, err := p.GetObject(ctx, &s3.GetObjectInput{Bucket: &bucket, Key: &key})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
if _, err := io.Copy(io.Discard, out.Body); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
out.Body.Close()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkPosixPutObject(b *testing.B) {
|
|
for name, mkMeta := range benchMetaModes(b) {
|
|
b.Run(name, func(b *testing.B) {
|
|
p, ctx := benchPosix(b, mkMeta)
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchPut(b, p, ctx, fmt.Sprintf("dir/sub/object-%d", i%64), "hello")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkPosixListObjectsV2(b *testing.B) {
|
|
for name, mkMeta := range benchMetaModes(b) {
|
|
b.Run(name, func(b *testing.B) {
|
|
p, ctx := benchPosix(b, mkMeta)
|
|
bucket := "bucket"
|
|
for i := 0; i < 100; i++ {
|
|
benchPut(b, p, ctx, fmt.Sprintf("dir/object-%03d", i), "hello")
|
|
}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
res, err := p.ListObjectsV2(ctx, &s3.ListObjectsV2Input{Bucket: &bucket, MaxKeys: aws.Int32(1000), StartAfter: aws.String("")})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
if len(res.Contents) != 100 {
|
|
b.Fatalf("listed %d objects", len(res.Contents))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkPosixHeadBucket(b *testing.B) {
|
|
for name, mkMeta := range benchMetaModes(b) {
|
|
b.Run(name, func(b *testing.B) {
|
|
p, ctx := benchPosix(b, mkMeta)
|
|
bucket := "bucket"
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := p.HeadBucket(ctx, &s3.HeadBucketInput{Bucket: &bucket}); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|