mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
* filer: read a missing path once when a rename or a delete looks it up A rename looks its target up with FindEntryForOverwrite, and DeleteEntryMetaAndData looks up the path it deletes the same way. Inside a store transaction an ordinary lookup hides an expired S3 entry and leaves its row for the commit. A write over the path still owes that row its chunks and its hard link, so the store was read again whenever the lookup came back with nothing. It comes back with nothing far more often because the store holds nothing at the path, which is what a rename to a new name finds. The second read then repeats the first. Filer.findEntry already tells whether its answer is what the store holds at the path, so the store is read again only for a row the lookup found and kept from its caller. Around 20 renames of a file to a new name on leveldb2 the store counter shows 7 finds per rename before and 6 after. * filer: shorten the overwrite-lookup comments Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
220 lines
8.7 KiB
Go
220 lines
8.7 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// What one write reads the store for. A create looked the path up to
|
|
// decide what it makes of it; the store wrapper used to look it up again to
|
|
// learn the hard link the name left behind. The write now takes that from the
|
|
// lookup it already has, so a plain file write reads the path once.
|
|
|
|
func fileAt(p string) *Entry {
|
|
return &Entry{
|
|
FullPath: util.FullPath(p),
|
|
Attr: Attr{Mode: 0o644, Mtime: time.Unix(1700000000, 0), Crtime: time.Unix(1700000000, 0)},
|
|
Chunks: []*filer_pb.FileChunk{{FileId: "5,0c050a"}},
|
|
}
|
|
}
|
|
|
|
// writeCountingFiler is a filer over its own store, the way Filer.SetStore
|
|
// builds one: the wrapper counts the names of a hard-linked file.
|
|
func writeCountingFiler(t *testing.T) (*Filer, *stubFilerStore) {
|
|
t.Helper()
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
f.Store.(*FilerStoreWrapper).countsLinks = true
|
|
return f, store
|
|
}
|
|
|
|
func TestCreateNewFileReadsThePathOnce(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
ctx := context.Background()
|
|
|
|
require.NoError(t, f.CreateEntry(ctx, fileAt("/d/new"), nil, false, false, nil, true, 255))
|
|
|
|
require.Equal(t, 1, store.findCount(), "a create that found the path empty must not read it again in the write")
|
|
}
|
|
|
|
func TestOverwriteFileReadsThePathOnce(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
ctx := context.Background()
|
|
require.NoError(t, f.CreateEntry(ctx, fileAt("/d/f"), nil, false, false, nil, true, 255))
|
|
|
|
store.resetFindCount()
|
|
replacement := fileAt("/d/f")
|
|
replacement.Chunks = []*filer_pb.FileChunk{{FileId: "6,0c060a"}}
|
|
require.NoError(t, f.CreateEntry(ctx, replacement, nil, false, false, nil, true, 255))
|
|
|
|
require.Equal(t, 1, store.findCount(), "replacing a plain file reads the path once")
|
|
|
|
stored, err := f.Store.FindEntry(ctx, util.FullPath("/d/f"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, "6,0c060a", stored.GetChunks()[0].FileId, "the lookup still decides what the write stores")
|
|
}
|
|
|
|
// The read the wrapper keeps for this case is the one that decides what the
|
|
// replaced name carried. A write that has a link to drop will not take a
|
|
// caller's word for it, so it reads the path — and the link still goes.
|
|
func TestReplacingALinkedNameReadsAndDropsTheLink(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
ctx := context.Background()
|
|
wrapper := f.Store.(*FilerStoreWrapper)
|
|
|
|
id := linkedFile(t, store, "/d/x", "/d/y")
|
|
checkNames(t, wrapper, 2, "/d/x", "/d/y")
|
|
|
|
store.resetFindCount()
|
|
require.NoError(t, f.CreateEntry(ctx, fileAt("/d/x"), nil, false, false, nil, true, 255))
|
|
|
|
require.Equal(t, 2, store.findCount(), "dropping a link is decided from the store, not from a row read earlier")
|
|
|
|
// one name is gone, so the file counts one name left
|
|
shared := &Entry{}
|
|
value, err := store.KvGet(ctx, id)
|
|
require.NoError(t, err)
|
|
require.NoError(t, shared.DecodeAttributesAndChunks(value))
|
|
require.Equal(t, int32(1), shared.HardLinkCounter, "the dropped name's link was not taken off")
|
|
|
|
// The write brought no link of its own, so it counted no names: the count
|
|
// in the record is what the file's remaining name reads.
|
|
require.Equal(t, int32(-1), namesCounted(t, store, id))
|
|
|
|
// the other name still reads the file, and its chunks are still there
|
|
checkNames(t, wrapper, 1, "/d/y")
|
|
stored, err := wrapper.FindEntry(ctx, util.FullPath("/d/y"))
|
|
require.NoError(t, err)
|
|
require.Len(t, stored.GetChunks(), 1)
|
|
}
|
|
|
|
// Two filers overwrite one name of a linked file. The first takes the link off;
|
|
// the second must not take it off again from a row it read before the first
|
|
// wrote: a second drop deletes the record, and the file's other name loses its
|
|
// chunks. This is the reason a write with a link at stake reads the path itself.
|
|
func TestStaleLinkedRowIsNotTrusted(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
wrapper := countingWrapper(store)
|
|
ctx := context.Background()
|
|
|
|
id := linkedFile(t, store, "/d/x", "/d/y")
|
|
|
|
// what this filer read, before the other one got there
|
|
stale := store.entries["/d/x"].ShallowClone()
|
|
require.NotEmpty(t, stale.HardLinkId)
|
|
|
|
// the other filer's write replaces the name with a plain file
|
|
require.NoError(t, wrapper.UpdateEntry(ctx, fileAt("/d/x")))
|
|
after := &Entry{}
|
|
value, err := store.KvGet(ctx, id)
|
|
require.NoError(t, err)
|
|
require.NoError(t, after.DecodeAttributesAndChunks(value))
|
|
require.Equal(t, int32(1), after.HardLinkCounter)
|
|
|
|
// this filer's write, a moment later, with a row that is no longer there
|
|
require.NoError(t, wrapper.UpdateEntryReplaces(ctx, fileAt("/d/x"), stale))
|
|
|
|
value, err = store.KvGet(ctx, id)
|
|
require.NoError(t, err, "the record is gone: the link was dropped twice")
|
|
twice := &Entry{}
|
|
require.NoError(t, twice.DecodeAttributesAndChunks(value))
|
|
require.Equal(t, int32(1), twice.HardLinkCounter, "the link was dropped twice")
|
|
|
|
// /d/y still reads the file
|
|
checkNames(t, wrapper, 1, "/d/y")
|
|
}
|
|
|
|
// A file with a hard link is still read by the write: its record may change
|
|
// under a change the store refused and sent again, so each run judges the name
|
|
// as the store holds it then. This pins the exception, so it is a choice and
|
|
// not an oversight.
|
|
func TestWritingALinkedFileStillReadsThePath(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
ctx := context.Background()
|
|
|
|
source := fileAt("/d/a")
|
|
source.HardLinkId = NewHardLinkId()
|
|
source.HardLinkCounter = 1
|
|
require.NoError(t, f.CreateEntry(ctx, source, nil, false, false, nil, true, 255))
|
|
|
|
store.resetFindCount()
|
|
again := source.ShallowClone()
|
|
again.Attr.Mtime = time.Unix(1700000002, 0)
|
|
require.NoError(t, f.CreateEntry(ctx, again, nil, false, false, nil, true, 255))
|
|
|
|
require.Equal(t, 2, store.findCount(), "a linked name is judged from the store, not from the caller's read")
|
|
}
|
|
|
|
// A directory is read twice by a create: once before taking the lock that
|
|
// keeps two mkdirs of one name apart, once again under it. The write itself
|
|
// adds no third read — a directory has no link of its own to leave behind.
|
|
func TestCreateDirectoryReadsThePathTwiceAtMost(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
ctx := context.Background()
|
|
|
|
dir := &Entry{FullPath: util.FullPath("/d2"), Attr: Attr{Mode: os.ModeDir | 0755}}
|
|
require.NoError(t, f.CreateEntry(ctx, dir, nil, false, false, nil, true, 255))
|
|
|
|
require.Equal(t, 2, store.findCount(), "the create's recheck under its lock, and nothing from the write")
|
|
}
|
|
|
|
// An insert whose lookup found the path empty takes the write's shortcut only
|
|
// for an entry with no link of its own: a linked entry has its name counted
|
|
// inside the change of its record, which has to see whether another filer
|
|
// stored that name meanwhile, or the count runs one above the names.
|
|
func TestNewLinkedNameStillReadsThePath(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
ctx := context.Background()
|
|
|
|
entry := fileAt("/d/linked")
|
|
entry.HardLinkId = NewHardLinkId()
|
|
entry.HardLinkCounter = 1
|
|
require.NoError(t, f.CreateEntry(ctx, entry, nil, false, false, nil, true, 255))
|
|
|
|
require.Equal(t, 2, store.findCount(), "a linked name is counted from the store, not from the create's lookup")
|
|
}
|
|
|
|
// A rename to a new name finds nothing at the path; the store is read once.
|
|
func TestLookupForOverwriteReadsAMissingPathOnce(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
txCtx, err := f.BeginTransaction(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
entry, err := f.FindEntryForOverwrite(txCtx, util.FullPath("/d/none"))
|
|
|
|
require.ErrorIs(t, err, filer_pb.ErrNotFound)
|
|
require.Nil(t, entry)
|
|
require.Equal(t, 1, store.findCount(), "the store said the path is empty; asking again repeats the read")
|
|
}
|
|
|
|
// The expired S3 row a transaction keeps still comes back from the second read.
|
|
func TestLookupForOverwriteReturnsAnExpiredRowTheTransactionKeeps(t *testing.T) {
|
|
f, store := writeCountingFiler(t)
|
|
expired := fileAt("/d/expired")
|
|
expired.TtlSec = 1
|
|
expired.Mtime = time.Now().Add(-time.Hour)
|
|
expired.Extended = map[string][]byte{s3_constants.SeaweedFSExpiresS3: []byte("true")}
|
|
store.entries["/d/expired"] = expired
|
|
txCtx, err := f.BeginTransaction(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
_, err = f.FindEntry(txCtx, util.FullPath("/d/expired"))
|
|
require.ErrorIs(t, err, filer_pb.ErrNotFound, "an ordinary lookup does not see an expired entry")
|
|
|
|
store.resetFindCount()
|
|
entry, err := f.FindEntryForOverwrite(txCtx, util.FullPath("/d/expired"))
|
|
|
|
require.NoError(t, err)
|
|
require.NotNil(t, entry)
|
|
require.Equal(t, "5,0c050a", entry.GetChunks()[0].FileId, "a write over the path owes the expired entry its chunks")
|
|
require.Equal(t, 2, store.findCount())
|
|
require.Contains(t, store.entries, "/d/expired", "the transaction keeps the row")
|
|
}
|