mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
* filer: fold chunk lists for scoped commits A filer serving a keep-only scoped token never folded chunk lists, and refused any new chunk for an entry holding manifests, so scoped clients kept their lists flat. On a store with a value size limit, such as FoundationDB, a scoped file stopped fitting at about 1000 chunks. The filer now folds scoped commits and reuses stored manifests as it does for unscoped ones. Folding runs after the retained-data check, so the only manifests a scoped commit stores are ones the filer wrote: - a scoped request may keep a manifest stored at the target, sent as stored; any other manifest is refused, with or without a proof, and a stored manifest cannot be kept as a data chunk; - the chunks held by the stored manifests count as stored at the target and join the identity map, so they may be retained flat but not under another spelling of their file id; - those chunks are read under the handler's path lock, not at admission; admission still refuses a manifest the target does not store and leaves the checks that depend on the held chunks to the locked check. A manifest that cannot be read refuses the write as Unavailable. An update rechecking inside its store transaction reuses the chunks it already read, so it does no volume I/O there. Appends to a folded entry are allowed under the same rules: an append keeps every stored chunk, and the locked check refuses aliases of the chunks the stored manifests hold. * mount: explain why a scoped mount keeps chunk lists flat A filer serving a scoped token now folds scoped commits itself and keeps only the manifests it wrote. The mount's flat rule is unchanged; only the reason given for it was out of date. * filer: test a scoped truncate through a stored manifest * filer: refuse manifest chunks in a scoped append An append moves each chunk it carries to the end of the file. A manifest cannot be moved that way: the chunks it holds keep their own offsets, so the appended range would read no data and the manifest's bytes would still read at their old place. Before this series a scoped request could carry no manifest at all; now that a scoped commit may keep a stored manifest, an append still takes data chunks only. * filer: read only the stored manifests a scoped commit drops The locked retained-data check read every stored manifest whenever a scoped request had chunks, so each flush of a folded file and each append cost one volume read per manifest under the path lock. The unscoped path reads only the manifests a commit drops. Read the same way for a keep-only scope: only the stored manifests the request does not keep unchanged, and none for an append, which keeps every stored chunk. A kept manifest keeps its chunks referenced, and garbage comparisons match chunk identities, so another spelling of one of its chunks cannot get the needle deleted; such a chunk now needs its own proof like any new chunk. Truncating through a manifest and listing its chunks flat still work, since both drop it. * filer: find the dropped manifests of a scoped commit in one pass Index the manifests the request keeps by file id instead of scanning the request for each stored manifest, which compared and cloned chunks quadratically in the number of manifests. * filer: rewrap the scopeManifestReader comment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
305 lines
11 KiB
Go
305 lines
11 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func TestScopeRetainsData(t *testing.T) {
|
|
const storedFileID = "5,0c00000055"
|
|
cases := []struct {
|
|
name string
|
|
stored *filer_pb.Entry
|
|
requested *filer_pb.Entry
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "unchanged inline content",
|
|
stored: &filer_pb.Entry{Content: []byte("stored")},
|
|
requested: &filer_pb.Entry{Content: []byte("stored")},
|
|
},
|
|
{
|
|
name: "changed inline content",
|
|
stored: &filer_pb.Entry{Content: []byte("stored")},
|
|
requested: &filer_pb.Entry{Content: []byte("new")},
|
|
},
|
|
{
|
|
name: "cleared inline content",
|
|
stored: &filer_pb.Entry{Content: []byte("stored")},
|
|
requested: &filer_pb.Entry{},
|
|
},
|
|
{
|
|
name: "unchanged chunk",
|
|
stored: entryWith("stored", storedFileID),
|
|
requested: entryWith("stored", storedFileID),
|
|
},
|
|
{
|
|
name: "new chunk",
|
|
stored: entryWith("stored", storedFileID),
|
|
requested: entryWith("stored", "5,0c00000056"),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "cleared chunks",
|
|
stored: entryWith("stored", storedFileID),
|
|
requested: &filer_pb.Entry{},
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := scopeRetainsData(tc.stored, tc.requested, false, nil, nil)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Fatalf("scopeRetainsData err=%v, wantErr=%v", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type scopeRetentionCase struct {
|
|
name string
|
|
initial *filer_pb.Entry
|
|
replacement *filer_pb.Entry
|
|
staleErr bool
|
|
}
|
|
|
|
func scopeRetentionCases() []scopeRetentionCase {
|
|
return []scopeRetentionCase{
|
|
{
|
|
name: "inline",
|
|
initial: &filer_pb.Entry{Name: "retained", Content: []byte("A")},
|
|
replacement: &filer_pb.Entry{Name: "retained", Content: []byte("B")},
|
|
},
|
|
{
|
|
name: "chunks",
|
|
initial: entryWith("retained", "5,0c00000055"),
|
|
replacement: entryWith("retained", "5,0c00000056"),
|
|
staleErr: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
func newScopeRetentionServer(t *testing.T, store *renameTestStore) (*FilerServer, *filerGrpcScope) {
|
|
t.Helper()
|
|
fs := &FilerServer{
|
|
filer: newRenameTestFiler(t, store),
|
|
option: &FilerOption{},
|
|
entryLockTable: util.NewLockTable[util.FullPath](),
|
|
}
|
|
scope := newFilerGrpcScope(scopeConfig{
|
|
signingKey: func() security.SigningKey { return security.SigningKey(scopeTestKey) },
|
|
}, fs)
|
|
return fs, scope
|
|
}
|
|
|
|
func scopedRetentionContext() context.Context {
|
|
return metadata.NewIncomingContext(context.Background(), metadata.Pairs(
|
|
"authorization",
|
|
"Bearer "+scopedToken("retention", time.Hour, []string{aliceWS}, nil),
|
|
))
|
|
}
|
|
|
|
func retentionEntryDataEqual(got *filer.Entry, want *filer_pb.Entry) bool {
|
|
if got == nil || want == nil {
|
|
return got == nil && want == nil
|
|
}
|
|
// The store may populate the parsed Fid cache from FileId. Compare the
|
|
// retained reference and layout, allowing that derived representation.
|
|
normalized := func(chunks []*filer_pb.FileChunk) *filer_pb.Entry {
|
|
e := proto.Clone(&filer_pb.Entry{Chunks: chunks}).(*filer_pb.Entry)
|
|
for _, chunk := range e.Chunks {
|
|
chunk.FileId = chunk.GetFileIdString()
|
|
chunk.Fid = nil
|
|
}
|
|
return e
|
|
}
|
|
return bytes.Equal(got.Content, want.Content) && proto.Equal(normalized(got.GetChunks()), normalized(want.GetChunks()))
|
|
}
|
|
|
|
func TestScopeRetentionRechecksCreateAndUpdateUnderPathLock(t *testing.T) {
|
|
for _, method := range []string{"CreateEntry", "UpdateEntry"} {
|
|
for _, tc := range scopeRetentionCases() {
|
|
t.Run(method+"/stale "+tc.name, func(t *testing.T) {
|
|
store := newRenameTestStore()
|
|
path := scopeJoin(aliceWS, tc.initial.GetName())
|
|
store.entries[path] = filer.FromPbEntry(aliceWS, proto.Clone(tc.initial).(*filer_pb.Entry))
|
|
fs, scope := newScopeRetentionServer(t, store)
|
|
var req any
|
|
if method == "CreateEntry" {
|
|
req = &filer_pb.CreateEntryRequest{Directory: aliceWS, SkipCheckParentDirectory: true, Entry: proto.Clone(tc.initial).(*filer_pb.Entry)}
|
|
} else {
|
|
req = &filer_pb.UpdateEntryRequest{Directory: aliceWS, Entry: proto.Clone(tc.initial).(*filer_pb.Entry)}
|
|
}
|
|
|
|
called := false
|
|
resp, err := scope.unary(scopedRetentionContext(), req, &grpc.UnaryServerInfo{
|
|
FullMethod: scopeFilerMethodPrefix + method,
|
|
}, func(handlerCtx context.Context, handlerReq any) (any, error) {
|
|
called = true
|
|
if updateErr := store.UpdateEntry(context.Background(), filer.FromPbEntry(aliceWS, proto.Clone(tc.replacement).(*filer_pb.Entry))); updateErr != nil {
|
|
t.Fatalf("replace stored entry: %v", updateErr)
|
|
}
|
|
switch method {
|
|
case "CreateEntry":
|
|
return fs.CreateEntry(handlerCtx, handlerReq.(*filer_pb.CreateEntryRequest))
|
|
case "UpdateEntry":
|
|
return fs.UpdateEntry(handlerCtx, handlerReq.(*filer_pb.UpdateEntryRequest))
|
|
default:
|
|
t.Fatalf("unexpected method %q", method)
|
|
return nil, nil
|
|
}
|
|
})
|
|
if tc.staleErr {
|
|
if status.Code(err) != codes.PermissionDenied || !called {
|
|
t.Fatalf("stale %s err=%v called=%v, want handler recheck PermissionDenied", method, err, called)
|
|
}
|
|
} else {
|
|
if err != nil || !called {
|
|
t.Fatalf("stale %s err=%v called=%v, want inline write admitted", method, err, called)
|
|
}
|
|
if method == "CreateEntry" {
|
|
createResp, ok := resp.(*filer_pb.CreateEntryResponse)
|
|
if !ok || createResp == nil || createResp.GetError() != "" || createResp.GetErrorCode() != filer_pb.FilerError_OK {
|
|
t.Fatalf("stale inline CreateEntry response = %#v, want empty error and FilerError_OK", resp)
|
|
}
|
|
}
|
|
}
|
|
got, findErr := store.FindEntry(context.Background(), util.FullPath(path))
|
|
if findErr != nil {
|
|
t.Fatalf("find replacement: %v", findErr)
|
|
}
|
|
want := tc.replacement
|
|
if !tc.staleErr {
|
|
want = tc.initial
|
|
}
|
|
if !retentionEntryDataEqual(got, want) {
|
|
t.Fatalf("stale %s stored content=%q chunks=%v, want content=%q chunks=%v", method, got.Content, got.GetChunks(), want.Content, want.GetChunks())
|
|
}
|
|
})
|
|
|
|
t.Run(method+"/unchanged "+tc.name, func(t *testing.T) {
|
|
store := newRenameTestStore()
|
|
path := scopeJoin(aliceWS, tc.initial.GetName())
|
|
store.entries[path] = filer.FromPbEntry(aliceWS, proto.Clone(tc.initial).(*filer_pb.Entry))
|
|
fs, scope := newScopeRetentionServer(t, store)
|
|
var req any
|
|
if method == "CreateEntry" {
|
|
req = &filer_pb.CreateEntryRequest{Directory: aliceWS, SkipCheckParentDirectory: true, Entry: proto.Clone(tc.initial).(*filer_pb.Entry)}
|
|
} else {
|
|
req = &filer_pb.UpdateEntryRequest{Directory: aliceWS, Entry: proto.Clone(tc.initial).(*filer_pb.Entry)}
|
|
}
|
|
|
|
called := false
|
|
resp, err := scope.unary(scopedRetentionContext(), req, &grpc.UnaryServerInfo{
|
|
FullMethod: scopeFilerMethodPrefix + method,
|
|
}, func(handlerCtx context.Context, handlerReq any) (any, error) {
|
|
called = true
|
|
switch method {
|
|
case "CreateEntry":
|
|
return fs.CreateEntry(handlerCtx, handlerReq.(*filer_pb.CreateEntryRequest))
|
|
case "UpdateEntry":
|
|
return fs.UpdateEntry(handlerCtx, handlerReq.(*filer_pb.UpdateEntryRequest))
|
|
default:
|
|
t.Fatalf("unexpected method %q", method)
|
|
return nil, nil
|
|
}
|
|
})
|
|
if err != nil || !called {
|
|
t.Fatalf("unchanged %s err=%v called=%v, want successful handler", method, err, called)
|
|
}
|
|
if method == "CreateEntry" {
|
|
createResp, ok := resp.(*filer_pb.CreateEntryResponse)
|
|
if !ok || createResp == nil || createResp.GetError() != "" || createResp.GetErrorCode() != filer_pb.FilerError_OK {
|
|
t.Fatalf("unchanged CreateEntry response = %#v, want empty error and FilerError_OK", resp)
|
|
}
|
|
}
|
|
got, findErr := store.FindEntry(context.Background(), util.FullPath(path))
|
|
if findErr != nil {
|
|
t.Fatalf("find retained entry: %v", findErr)
|
|
}
|
|
if !retentionEntryDataEqual(got, tc.initial) {
|
|
t.Fatalf("successful unchanged %s changed retained data: got content=%q chunks=%v, want content=%q chunks=%v", method, got.Content, got.GetChunks(), tc.initial.Content, tc.initial.GetChunks())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestScopeKeepsHardLink(t *testing.T) {
|
|
const storedFileID = "5,0c00000055"
|
|
id := []byte("shared-hard-link")
|
|
linked := &filer_pb.Entry{HardLinkId: id, HardLinkCounter: 2, Chunks: []*filer_pb.FileChunk{{FileId: storedFileID}}}
|
|
cases := []struct {
|
|
name string
|
|
stored *filer_pb.Entry
|
|
requested *filer_pb.Entry
|
|
wantErr bool
|
|
}{
|
|
{name: "unlinked entry", stored: entryWith("f", storedFileID), requested: entryWith("f", storedFileID)},
|
|
{name: "same link and count", stored: linked, requested: &filer_pb.Entry{HardLinkId: id, HardLinkCounter: 2, Chunks: linked.Chunks}},
|
|
{name: "lowered count", stored: linked, requested: &filer_pb.Entry{HardLinkId: id, HardLinkCounter: 1, Chunks: linked.Chunks}, wantErr: true},
|
|
{name: "lowered count without chunks", stored: linked, requested: &filer_pb.Entry{HardLinkId: id, HardLinkCounter: 1}, wantErr: true},
|
|
{name: "chunks detached from the link", stored: linked, requested: entryWith("f", storedFileID), wantErr: true},
|
|
{name: "link dropped with the data", stored: linked, requested: &filer_pb.Entry{}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := scopeKeepsHardLink(tc.stored, tc.requested)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Fatalf("scopeKeepsHardLink err=%v, wantErr=%v", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestScopeUpdateCannotFreeAnotherNamesHardLinkData(t *testing.T) {
|
|
const storedFileID = "5,0c00000055"
|
|
id := []byte("shared-hard-link")
|
|
for _, tc := range []struct {
|
|
name string
|
|
entry *filer_pb.Entry
|
|
}{
|
|
{name: "lowered link count", entry: &filer_pb.Entry{Name: "f", HardLinkId: id, HardLinkCounter: 1, Chunks: []*filer_pb.FileChunk{{FileId: storedFileID}}}},
|
|
{name: "chunks detached from the link", entry: entryWith("f", storedFileID)},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
store := newRenameTestStore()
|
|
store.kv = make(map[string][]byte)
|
|
fs, scope := newScopeRetentionServer(t, store)
|
|
for _, p := range []string{aliceWS + "/f", "/bob/g"} {
|
|
e := &filer.Entry{FullPath: util.FullPath(p), HardLinkId: id, HardLinkCounter: 2, Attr: filer.Attr{Mode: 0644}, Chunks: []*filer_pb.FileChunk{{FileId: storedFileID}}}
|
|
if err := fs.filer.Store.InsertEntry(context.Background(), e); err != nil {
|
|
t.Fatalf("insert %s: %v", p, err)
|
|
}
|
|
}
|
|
req := &filer_pb.UpdateEntryRequest{Directory: aliceWS, Entry: tc.entry}
|
|
_, err := scope.unary(scopedRetentionContext(), req, &grpc.UnaryServerInfo{
|
|
FullMethod: scopeFilerMethodPrefix + "UpdateEntry",
|
|
}, func(handlerCtx context.Context, handlerReq any) (any, error) {
|
|
return fs.UpdateEntry(handlerCtx, handlerReq.(*filer_pb.UpdateEntryRequest))
|
|
})
|
|
if status.Code(err) != codes.PermissionDenied {
|
|
t.Fatalf("UpdateEntry err=%v, want PermissionDenied", err)
|
|
}
|
|
got, findErr := fs.filer.FindEntry(context.Background(), util.FullPath(aliceWS+"/f"))
|
|
if findErr != nil {
|
|
t.Fatalf("find %s/f: %v", aliceWS, findErr)
|
|
}
|
|
if !bytes.Equal(got.HardLinkId, id) || got.HardLinkCounter != 2 {
|
|
t.Fatalf("%s/f link=%x count=%d, want %x and 2", aliceWS, got.HardLinkId, got.HardLinkCounter, id)
|
|
}
|
|
})
|
|
}
|
|
}
|