mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
* filer, mount: keep a hard-linked file to one link id and count its names on the filer A hard link's bookkeeping was done by the client. To link a file a mount reads its entry and sends two writes, an update of the source and a create of the new name, both with a link id and a count of the file's names. Two clients that link one file at the same moment each worked from the entry as it was before the other's writes: - With no link id on the entry yet, each made one up. The second update moved the source to its id, the store took the name off the first, and the first client's new name was left alone on it: one file's chunks under two link ids. - With a link id there, both read the same count and sent it plus one. The record counted one name fewer than the file had. Any write through a name carried the client's copy of the count and replaced the record the same way. Either way the delete of the last counted name deleted the record and the chunks while another name still read them. The filer's own store now counts the names of a hard-linked file itself (countLink): one more when the name written did not hold the link before, the same when it did or when a rename moves it there (WithMovedLink), one for a record that starts. The count sent is not used, and the entry gets the count stored, so the response and the event carry it. The lookup of the name, the count, the record and the name are stored as one change of the record (changeLinkRecord), which DeleteHardLink goes through as well: under a lock per link id, and in a transaction where the store refuses a commit over a change made since it began, run again when it does. A wrapper around a copy of some entries, such as a mount's metadata cache, sees only some of the names and stores the count it is given. UpdateEntry refuses to give a name that holds a hard link another link id that no file has yet (refuseSecondHardLink). The mount sends a file's first link's update as a unary RPC, which keeps that refusal apart from an I/O failure, reads the entry again and links under the id the file has by now. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * filer: take a replicated update's link id without refusing it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: look a path-specific name up outside the record's transaction Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: check the leveldb open and close it in the hard link test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: send a refused first link again from the entry as stored Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep a first link's refusal over the next filer's error Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: count a hard link's names next to the clients' count instead of in its place The filer's count replaced the one clients send, which lost two things that count did by being worked out ahead: - A link's first write, the update of the source, carries the count with the new name in it already. While the second write is on its way that kept a delete of the source from being taken for the delete of the file's last name (lastLinkDropped goes by the count it read), and the chunks from being deleted under the name about to come. Counted only when it arrived, the new name was stored with chunks that were gone: 104 of 300 rounds of a mount linking a file while another deletes it. - A write of a name the store refused had counted the name already, and sent again counted it once more. The filer now keeps its count of the names in a key of its own next to the record (linkNames), written once the name is stored, and the record gets whichever is higher: the count the client sent or the names counted. It is never lower than what the client alone would have left, and never lower than the names the file has. A record from before has its count recorded as the names when it is first written again. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * mount: send a refused first link again with the file's inline content The second attempt builds the new name's create from the entry as the filer has it by now: attributes, chunks and extended attributes. A file the filer keeps inside its entry has its bytes in Content, and the create is what the filer writes the link's shared record from, so without them the record lost the file's content. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * filer: store a path-specific name before the record's lock is let go A second create of the same new name on this filer took the lock between the first one's count and its name write, missed the name, and counted it again: the file's record and chunks then outlived its last name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep a link's names key out of the keys of a directory's entries The key began with the link's random id. One that began as the root's entry keys do in a store keyed by path put the names' 4 bytes in the root's listing, and it failed to decode: listing / and the etcd scan under fsck failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: record a hard link's name before it is stored, and take it off if it is not The names counted for a hard link were recorded once the name was stored. On a store without transactions a failure of that write left the name stored and not counted, and the write sent again found the name there and did not count it either: the names recorded stayed one below the names stored, the side a file is deleted under a name from. They are recorded first now, inside the change of the record that counted them, and a name the store then does not take is counted off again (storeCountedName). A failure halfway leaves them one above the names stored, and the file leaked. A name in a path-specific store is recorded with the record as well, and counted off in a change of its own. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * filer: keep a hard link's name counted when a failed write stored it A write whose deadline passes after the store took the name returns an error, and counting the name off then left the names one below the names stored. A later write with the count from before the link lowered the record to them, and the file was deleted under a name it still had. Read the name back first, and keep it counted where that cannot be told. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: carry the stored RemoteEntry into the name a retried first link adds The retry rebuilds the new name from the entry the file has once another client linked it, but kept the RemoteEntry of the entry first read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
338 lines
12 KiB
Go
338 lines
12 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// A write that replaces one name of a hard-linked file unlinks that name and
|
|
// nothing else: the file's other names keep reading its chunks. These tests
|
|
// write over /dir/dst.txt, one of the links set up by hardLinkedTarget, whose
|
|
// file's only chunk is 3,0c03; with two links /dir/other.txt is the second.
|
|
|
|
const replacedChunk, writtenChunk = "3,0c03", "4,0d04"
|
|
|
|
func fileEntryWithChunk(path string, inode uint64, fileId string) *filer.Entry {
|
|
entry := newFileEntry(path, inode)
|
|
entry.Chunks = []*filer_pb.FileChunk{{FileId: fileId, Offset: 0, Size: 4, ModifiedTsNs: 1}}
|
|
return entry
|
|
}
|
|
|
|
func queuedDeletions(f *filer.Filer) []string {
|
|
var ids []string
|
|
for i := 0; i < 2; i++ {
|
|
f.FileIdDeletionQueue.Consume(func(batch []string) { ids = append(ids, batch...) })
|
|
}
|
|
sort.Strings(ids)
|
|
return ids
|
|
}
|
|
|
|
// hardLinkedTarget puts /dir/dst.txt in the store as one of links hard links
|
|
// to a file whose only chunk is 3,0c03; with two links /dir/other.txt is the
|
|
// second.
|
|
func hardLinkedTarget(t *testing.T, store *renameTestStore, links int32) filer.HardLinkId {
|
|
t.Helper()
|
|
id := filer.HardLinkId("hl-chunks-test-01")
|
|
shared := fileEntryWithChunk("/dir/dst.txt", 303, replacedChunk)
|
|
shared.HardLinkId, shared.HardLinkCounter = id, links
|
|
blob, err := shared.EncodeAttributesAndChunks()
|
|
if err != nil {
|
|
t.Fatalf("encode hard link: %v", err)
|
|
}
|
|
store.kv = map[string][]byte{string(id): blob}
|
|
for _, p := range []string{"/dir/dst.txt", "/dir/other.txt"}[:min(int(links), 2)] {
|
|
link := newFileEntry(p, 303)
|
|
link.HardLinkId, link.HardLinkCounter = id, links
|
|
store.entries[p] = link
|
|
}
|
|
return id
|
|
}
|
|
|
|
func hardLinkChunkServer(t *testing.T, links int32) (*FilerServer, *filer.Filer, *renameTestStore, filer.HardLinkId) {
|
|
t.Helper()
|
|
store := newRenameTestStore()
|
|
store.entries["/dir"] = newDirectoryEntry("/dir", 100)
|
|
id := hardLinkedTarget(t, store, links)
|
|
swapNotificationQueue(t, &captureQueue{})
|
|
f := newRenameTestFiler(t, store)
|
|
return &FilerServer{filer: f, option: &FilerOption{}, entryLockTable: util.NewLockTable[util.FullPath]()}, f, store, id
|
|
}
|
|
|
|
func written(fileId string, tsNs int64) *filer_pb.FileChunk {
|
|
return &filer_pb.FileChunk{FileId: fileId, Offset: 0, Size: 4, ModifiedTsNs: tsNs}
|
|
}
|
|
|
|
func dstEntry(hardLinkId filer.HardLinkId, links int32, chunks ...*filer_pb.FileChunk) *filer_pb.Entry {
|
|
return &filer_pb.Entry{
|
|
Name: "dst.txt",
|
|
Attributes: &filer_pb.FuseAttributes{Mtime: 1700000100, FileMode: 0644, Inode: 404, FileSize: 4},
|
|
Chunks: chunks,
|
|
HardLinkId: hardLinkId,
|
|
HardLinkCounter: links,
|
|
}
|
|
}
|
|
|
|
// checkDeletions compares the distinct chunks queued for deletion: a chunk
|
|
// both the old entry's diff and the request's covered chunks name is queued
|
|
// twice, which deletes it once.
|
|
func checkDeletions(t *testing.T, f *filer.Filer, want ...string) {
|
|
t.Helper()
|
|
got := slices.Compact(queuedDeletions(f))
|
|
if len(got) != len(want) {
|
|
t.Fatalf("queued chunk deletions = %v, want %v", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("queued chunk deletions = %v, want %v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// checkOtherLink checks that /dir/other.txt, the surviving link, reads
|
|
// wantChunk with wantLinks links.
|
|
func checkOtherLink(t *testing.T, f *filer.Filer, wantChunk string, wantLinks int32) {
|
|
t.Helper()
|
|
other, err := f.FindEntry(context.Background(), "/dir/other.txt")
|
|
if err != nil {
|
|
t.Fatalf("find the other link: %v", err)
|
|
}
|
|
if other.HardLinkCounter != wantLinks {
|
|
t.Fatalf("other link count = %d, want %d", other.HardLinkCounter, wantLinks)
|
|
}
|
|
if chunks := other.GetChunks(); len(chunks) != 1 || chunks[0].GetFileIdString() != wantChunk {
|
|
t.Fatalf("other link chunks = %v, want %s", chunks, wantChunk)
|
|
}
|
|
}
|
|
|
|
func createDst(t *testing.T, server *FilerServer, entry *filer_pb.Entry) {
|
|
t.Helper()
|
|
resp, err := server.CreateEntry(context.Background(), &filer_pb.CreateEntryRequest{
|
|
Directory: "/dir", Entry: entry, SkipCheckParentDirectory: true,
|
|
})
|
|
if err != nil || resp.Error != "" {
|
|
t.Fatalf("CreateEntry: %v %q", err, resp.GetError())
|
|
}
|
|
}
|
|
|
|
func updateDst(t *testing.T, server *FilerServer, entry *filer_pb.Entry) {
|
|
t.Helper()
|
|
if _, err := server.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{Directory: "/dir", Entry: entry}); err != nil {
|
|
t.Fatalf("UpdateEntry: %v", err)
|
|
}
|
|
}
|
|
|
|
// A create over the path (an S3 PUT, a client that does not know the path is
|
|
// a hard link) replaces dst.txt with a new file.
|
|
func TestCreateEntryOverHardLinkKeepsSharedChunks(t *testing.T) {
|
|
server, f, store, id := hardLinkChunkServer(t, 2)
|
|
createDst(t, server, dstEntry(nil, 0, written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f)
|
|
checkOtherLink(t, f, replacedChunk, 1)
|
|
if _, found := store.kv[string(id)]; !found {
|
|
t.Fatal("hard link record deleted while a link remains")
|
|
}
|
|
dst, err := f.FindEntry(context.Background(), "/dir/dst.txt")
|
|
if err != nil {
|
|
t.Fatalf("find dst: %v", err)
|
|
}
|
|
if len(dst.HardLinkId) != 0 || len(dst.GetChunks()) != 1 || dst.GetChunks()[0].GetFileIdString() != writtenChunk {
|
|
t.Fatalf("dst = link %x chunks %v, want a plain file with %s", dst.HardLinkId, dst.GetChunks(), writtenChunk)
|
|
}
|
|
}
|
|
|
|
func TestCreateEntryOverLastHardLinkDeletesItsChunks(t *testing.T) {
|
|
server, f, store, id := hardLinkChunkServer(t, 1)
|
|
createDst(t, server, dstEntry(nil, 0, written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f, replacedChunk)
|
|
if _, found := store.kv[string(id)]; found {
|
|
t.Fatal("hard link record kept after its last link was replaced")
|
|
}
|
|
}
|
|
|
|
// A create whose chunk list still carries the file's chunk under a newer one
|
|
// (a client writing over the range from a stale view): the covered chunk is
|
|
// the other link's data.
|
|
func TestCreateEntryOverHardLinkKeepsSharedChunkItCovers(t *testing.T) {
|
|
server, f, _, _ := hardLinkChunkServer(t, 2)
|
|
createDst(t, server, dstEntry(nil, 0, written(replacedChunk, 1), written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f)
|
|
checkOtherLink(t, f, replacedChunk, 1)
|
|
}
|
|
|
|
// The lookup of dst.txt fails to read the shared record, so the replaced
|
|
// entry is the name's own row, which has no chunks.
|
|
func TestCreateEntryOverHardLinkKeepsSharedChunkItCoversAfterFailedLookup(t *testing.T) {
|
|
server, f, store, _ := hardLinkChunkServer(t, 2)
|
|
store.kvGetErrs = 1
|
|
createDst(t, server, dstEntry(nil, 0, written(replacedChunk, 1), written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f)
|
|
checkOtherLink(t, f, replacedChunk, 1)
|
|
}
|
|
|
|
func TestCreateEntryOverLastHardLinkDeletesChunkItCovers(t *testing.T) {
|
|
server, f, _, _ := hardLinkChunkServer(t, 1)
|
|
createDst(t, server, dstEntry(nil, 0, written(replacedChunk, 1), written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f, replacedChunk)
|
|
}
|
|
|
|
// An update that no longer carries the hard link (a client with a stale view
|
|
// of dst.txt) drops this name's link like a create over it.
|
|
func TestUpdateEntryDroppingHardLinkKeepsSharedChunks(t *testing.T) {
|
|
server, f, store, id := hardLinkChunkServer(t, 2)
|
|
updateDst(t, server, dstEntry(nil, 0, written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f)
|
|
checkOtherLink(t, f, replacedChunk, 1)
|
|
if _, found := store.kv[string(id)]; !found {
|
|
t.Fatal("hard link record deleted while a link remains")
|
|
}
|
|
}
|
|
|
|
func TestUpdateEntryDroppingLastHardLinkDeletesItsChunks(t *testing.T) {
|
|
server, f, _, _ := hardLinkChunkServer(t, 1)
|
|
updateDst(t, server, dstEntry(nil, 0, written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f, replacedChunk)
|
|
}
|
|
|
|
// Writing the file through one of its names changes the file for every name,
|
|
// so the chunk the write replaced is garbage.
|
|
func TestUpdateEntryThroughHardLinkDeletesReplacedChunk(t *testing.T) {
|
|
server, f, _, id := hardLinkChunkServer(t, 2)
|
|
updateDst(t, server, dstEntry(id, 2, written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f, replacedChunk)
|
|
checkOtherLink(t, f, writtenChunk, 2)
|
|
}
|
|
|
|
func renameSrcOverHardLinkedDst(t *testing.T, server *FilerServer, store *renameTestStore) {
|
|
t.Helper()
|
|
store.entries["/dir/src.txt"] = fileEntryWithChunk("/dir/src.txt", 101, "1,0a01")
|
|
if _, err := server.AtomicRenameEntry(context.Background(), &filer_pb.AtomicRenameEntryRequest{
|
|
OldDirectory: "/dir", OldName: "src.txt", NewDirectory: "/dir", NewName: "dst.txt",
|
|
}); err != nil {
|
|
t.Fatalf("AtomicRenameEntry: %v", err)
|
|
}
|
|
dst, err := server.filer.FindEntry(context.Background(), "/dir/dst.txt")
|
|
if err != nil {
|
|
t.Fatalf("find dst: %v", err)
|
|
}
|
|
if chunks := dst.GetChunks(); len(chunks) != 1 || chunks[0].GetFileIdString() != "1,0a01" {
|
|
t.Fatalf("dst chunks = %v, want the renamed file's 1,0a01", chunks)
|
|
}
|
|
}
|
|
|
|
// Saving a file by writing a temporary one and renaming it over the old
|
|
// name (Python's .pyc writes, checkpoint savers) must not take the data of
|
|
// another hard link to the old file with it.
|
|
func TestAtomicRenameEntryOverHardLinkKeepsSharedChunks(t *testing.T) {
|
|
server, f, store, id := hardLinkChunkServer(t, 2)
|
|
renameSrcOverHardLinkedDst(t, server, store)
|
|
|
|
checkDeletions(t, f)
|
|
checkOtherLink(t, f, replacedChunk, 1)
|
|
if _, found := store.kv[string(id)]; !found {
|
|
t.Fatal("hard link record deleted while a link remains")
|
|
}
|
|
}
|
|
|
|
func TestAtomicRenameEntryOverLastHardLinkDeletesItsChunks(t *testing.T) {
|
|
server, f, store, id := hardLinkChunkServer(t, 1)
|
|
renameSrcOverHardLinkedDst(t, server, store)
|
|
|
|
checkDeletions(t, f, replacedChunk)
|
|
if _, found := store.kv[string(id)]; found {
|
|
t.Fatal("hard link record kept after its last link was replaced")
|
|
}
|
|
}
|
|
|
|
// Two clients link a file at the same moment, each from an entry that had no
|
|
// link yet, so each gives the name a link id of its own. The file has the
|
|
// first one's when the second arrives. Taken, the second would move the name
|
|
// to its id and leave the first client's new name alone on the other: one
|
|
// file's chunks under two link ids, each deleting them under the other with
|
|
// its last name. The update is refused, nothing changes, and the client links
|
|
// under the id the file has.
|
|
func TestUpdateEntryRefusesAFileASecondLinkId(t *testing.T) {
|
|
server, f, store, id := hardLinkChunkServer(t, 2)
|
|
second := filer.HardLinkId("hl-chunks-test-02")
|
|
|
|
_, err := server.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
|
|
Directory: "/dir", Entry: dstEntry(second, 2, written(replacedChunk, 1)),
|
|
})
|
|
if status.Code(err) != codes.FailedPrecondition {
|
|
t.Fatalf("update with a second link id: %v, want FailedPrecondition", err)
|
|
}
|
|
checkDeletions(t, f)
|
|
checkOtherLink(t, f, replacedChunk, 2)
|
|
dst, err := f.FindEntry(context.Background(), "/dir/dst.txt")
|
|
if err != nil {
|
|
t.Fatalf("find dst: %v", err)
|
|
}
|
|
if string(dst.HardLinkId) != string(id) {
|
|
t.Fatalf("dst has link id %q, want the file's %q", dst.HardLinkId, id)
|
|
}
|
|
if _, found := store.kv[string(second)]; found {
|
|
t.Fatal("the refused link id got a record")
|
|
}
|
|
|
|
updateDst(t, server, dstEntry(id, 3, written(replacedChunk, 1)))
|
|
checkDeletions(t, f)
|
|
}
|
|
|
|
// filer.sync sends the other cluster's link id without its record: taken.
|
|
func TestUpdateEntryFromOtherClusterTakesItsLinkId(t *testing.T) {
|
|
server, f, _, _ := hardLinkChunkServer(t, 2)
|
|
other := filer.HardLinkId("hl-chunks-test-02")
|
|
|
|
if _, err := server.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
|
|
Directory: "/dir", Entry: dstEntry(other, 2, written(replacedChunk, 1)), IsFromOtherCluster: true,
|
|
}); err != nil {
|
|
t.Fatalf("replicated update with another link id: %v", err)
|
|
}
|
|
dst, err := f.FindEntry(context.Background(), "/dir/dst.txt")
|
|
if err != nil {
|
|
t.Fatalf("find dst: %v", err)
|
|
}
|
|
if string(dst.HardLinkId) != string(other) {
|
|
t.Fatalf("dst has link id %q, want the replicated %q", dst.HardLinkId, other)
|
|
}
|
|
}
|
|
|
|
// A link id that has a record is another file's, not a second one for this
|
|
// file: an update that carries it puts that file at the name, which leaves
|
|
// the old file as a create over the name does.
|
|
func TestUpdateEntryWithAnotherFilesLinkIdReplacesTheName(t *testing.T) {
|
|
server, f, store, id := hardLinkChunkServer(t, 2)
|
|
another := filer.HardLinkId("hl-chunks-test-03")
|
|
shared := fileEntryWithChunk("/dir/third.txt", 505, writtenChunk)
|
|
shared.HardLinkId, shared.HardLinkCounter = another, 1
|
|
blob, err := shared.EncodeAttributesAndChunks()
|
|
if err != nil {
|
|
t.Fatalf("encode hard link: %v", err)
|
|
}
|
|
store.kv[string(another)] = blob
|
|
store.entries["/dir/third.txt"] = shared
|
|
|
|
updateDst(t, server, dstEntry(another, 2, written(writtenChunk, 2)))
|
|
|
|
checkDeletions(t, f)
|
|
checkOtherLink(t, f, replacedChunk, 1)
|
|
if _, found := store.kv[string(id)]; !found {
|
|
t.Fatal("hard link record deleted while a link remains")
|
|
}
|
|
}
|