mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* filer: take a hard link off only after the store dropped the name Deleting one name of a hard-linked file took the name off the file's link count (DeleteHardLink: the shared record read, its count less one written back, in writes of their own) before the store deleted the name. When the store then refused the delete (a TiKV write conflict, a database timeout), or the filer stopped in between, the name stayed with the count already one short. Each time the delete was sent again it took one more off: a client retrying, or rm run again after the error. At zero the record went, and that delete, having read a count of one, deleted the chunks. The file's other names were left with no record and with their chunks deleted. Without a retry, the next delete of another name of the file did the same. A name now comes off the count once the store no longer holds it (FilerStoreWrapper.dropLink). A refused delete leaves the name and the count as they were, and the delete sent again takes it off once. - DeleteEntry and DeleteOneEntry delete the name, then drop its link. - InsertEntry and UpdateEntry over a name of another hard-linked file write the new entry, then drop the replaced name's link (handleUpdateToHardLinks now returns it instead of dropping it). - A recursive delete drops its hard-linked children's links after DeleteFolderChildren deleted them, not while listing them. - A link that cannot be dropped after the delete or the write is logged and fails neither (a write failed before): the count stays one too high, and the record and the chunks outlive the file's last name instead of going before it. Inside a store transaction (a rename) the order changes nothing. Tests: filer_hardlink_delete_test.go, and through the DeleteEntry handler filer_grpc_server_hardlink_delete_test.go. With the old order the refused delete, write and recursive delete each take a link off, and the delete sent again queues chunk 3,0c03 for deletion while /dir/other.txt reads it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: delete a hard-linked name's chunks only once its file's record is gone DeleteEntryMetaAndData deleted the chunks of a hard-linked name when the link count it read before the delete was one or less. Another filer can link the file under a new name in between: the count misses that name, the record, which counts it, outlives the delete, and the chunks went while the new name read them. Now the count read before must leave no other name and the file's record must be gone after the delete (lastLinkDropped); a recursive delete already asks for the record. The count stays in: one name deleted at once through two filers is taken off twice, and the record goes while another name still holds the file. When both deletes read the count before either took a link off, the count still shows that name and the chunks stay. Where the two disagree the chunks leak rather than go. Tests: TestDeleteEntryKeepsChunksOfLinkAddedMeanwhile fails with the count alone, TestDeleteEntryTwiceAtOnceKeepsChunks with the record alone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: delete a recursive delete's hard-linked names one by one, as a single delete does A recursive delete listed a folder's hard-linked names, had DeleteFolderChildren delete them with the other children, and then took their links off and asked whether each record was gone. Found in review: - A store that deletes a folder's children in batches (TiKV) can fail after some of them. The names it deleted are gone, their links were never taken off, and the delete run again no longer lists them, so their counts stay high and the file's chunks are never freed. - Two filers deleting the same folder at once both take the link of each hard-linked name off. The second finds the record gone and deletes the chunks while a name outside the folder still reads them (as on master). - The listed entries, chunk lists included, were kept until the folder's children were deleted. A hard-linked child now goes as it is listed, the way a single delete takes a name (deleteLinkedName): its entry read again for the current count, the name deleted by the store, then its link taken off (dropLink), and its chunks queued only when it was the last link (lastLinkDropped: the count read left no other name and the record is gone). DeleteFolderChildren takes the other children. Names of one file in two folders of the tree still free the chunks once, with the last of them. The link of a hard-linked child now comes off whether or not the delete removes data, as it already did for a single delete. OnHardLinkIdsFunc and maybeDeleteHardLinks, which took links off only with the data, are gone. Tests: TestRecursiveDeleteFailingAfterItsLinkedChildTookItsLinkOff and TestRecursiveDeletesAtOnceKeepChunksOfANameOutside fail with the previous commit; the latter also fails when deleteLinkedName asks for the record alone. TestRecursiveDeleteOfEveryNameDeletesChunksOnce covers the names of one file in two folders of the tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: delete a recursive delete's last-link chunks as the link goes deleteLinkedName queued the chunks of a file whose last link it took off with the folder's other chunks, which are deleted only once the whole folder delete has succeeded. When a later child, a listing or DeleteFolderChildren failed, the delete returned first and those chunks stayed for good: their name and record were gone, so no retry could find them. Found in review. They are now deleted when the link goes, through DeleteChunks with the child's path, as a single delete deletes them. Test: TestRecursiveDeleteFailingAfterALastLinkWentDeletesItsChunks fails with the previous commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: free a swept hard-linked child's chunks only when its last link went The replay sweep freed a hard-linked child's chunks once the shared record was gone. One name deleted at the same time through another filer takes the link off twice, so the record goes while another name still reads the file. Use lastLinkDropped with the count read under the child's lock, as the single and recursive deletes do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: test store hands out a copy of an entry, as a real store does Returning the stored entry let a later lookup overwrite the link count an earlier lookup handed to its caller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: look a hard-linked name up again before taking its link off A delete read the name, and another filer deleted the same name and took its link off before this delete read the count. The count then already lacked the name, the second delete took the link off again, the record went with another name left, and the chunks were deleted. A hard-linked name is now deleted through DeleteEntry, which looks the name up after the count was read and skips the link when the name is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: report a failed lookup from a store delete instead of success DeleteEntry took any lookup error for a name already gone, so the delete reported success, the name stayed, and the caller sent a delete event. Only a missing name is a no-op now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
416 lines
17 KiB
Go
416 lines
17 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// A name of a hard-linked file comes off the file's link count only once the
|
|
// store no longer holds the name (dropLink). These tests have the store refuse
|
|
// the delete or the write, as TiKV does after write conflicts it gave up on,
|
|
// and run it again, as a client does after the error.
|
|
|
|
// storeRefusal is a store refusing a delete or a write, as TiKV does on a
|
|
// write conflict: the change is not made, and the client may send it again.
|
|
var storeRefusal = errors.New("write conflict { start_ts:463, conflict_start_ts:462, conflict_commit_ts:464 }")
|
|
|
|
const linkedChunk = "3,0c03"
|
|
|
|
// linkedFile puts in store a file with the given names: the hard link record
|
|
// counting them all and holding the file's one chunk, and a row for each name.
|
|
func linkedFile(t *testing.T, store *stubFilerStore, names ...util.FullPath) HardLinkId {
|
|
t.Helper()
|
|
id := NewHardLinkId()
|
|
shared := &Entry{
|
|
FullPath: names[0],
|
|
Attr: Attr{Mode: 0o644, Mtime: time.Unix(1700000000, 0)},
|
|
HardLinkId: id,
|
|
HardLinkCounter: int32(len(names)),
|
|
Chunks: []*filer_pb.FileChunk{{FileId: linkedChunk, Size: 4}},
|
|
}
|
|
store.kv[string(id)] = encodeLinkRecord(t, shared)
|
|
for _, name := range names {
|
|
row := shared.ShallowClone()
|
|
row.FullPath = name
|
|
store.entries[string(name)] = row
|
|
}
|
|
return id
|
|
}
|
|
|
|
func encodeLinkRecord(t *testing.T, shared *Entry) []byte {
|
|
t.Helper()
|
|
blob, err := shared.EncodeAttributesAndChunks()
|
|
require.NoError(t, err)
|
|
return blob
|
|
}
|
|
|
|
// setLinkCount rewrites the count in id's record, as a link or a delete
|
|
// through another filer does. Callers hold store.mu (kvGetHook).
|
|
func setLinkCount(t *testing.T, store *stubFilerStore, id HardLinkId, count int32) {
|
|
t.Helper()
|
|
shared := &Entry{}
|
|
require.NoError(t, shared.DecodeAttributesAndChunks(store.kv[string(id)]))
|
|
shared.HardLinkCounter = count
|
|
store.kv[string(id)] = encodeLinkRecord(t, shared)
|
|
}
|
|
|
|
// linkCount is the count in id's record, 0 once the record is gone.
|
|
func linkCount(t *testing.T, store *stubFilerStore, id HardLinkId) int32 {
|
|
t.Helper()
|
|
blob, err := store.KvGet(context.Background(), id)
|
|
if errors.Is(err, ErrKvNotFound) {
|
|
return 0
|
|
}
|
|
require.NoError(t, err)
|
|
shared := &Entry{}
|
|
require.NoError(t, shared.DecodeAttributesAndChunks(blob))
|
|
return shared.HardLinkCounter
|
|
}
|
|
|
|
// onSecondRecordRead runs f when the store reads id's record the second time:
|
|
// a delete reads it first in its lookup, then to take the link off.
|
|
func onSecondRecordRead(store *stubFilerStore, id HardLinkId, f func()) {
|
|
onRecordRead(store, id, 2, f)
|
|
}
|
|
|
|
// onRecordRead runs f when the store reads id's record the nth time.
|
|
func onRecordRead(store *stubFilerStore, id HardLinkId, nth int, f func()) {
|
|
reads := 0
|
|
store.kvGetHook = func(key []byte) {
|
|
if string(key) == string(id) {
|
|
if reads++; reads == nth {
|
|
f()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeleteRefusedByStoreKeepsHardLinkCount(t *testing.T) {
|
|
for _, del := range []struct {
|
|
name string
|
|
run func(*FilerStoreWrapper, context.Context, *Entry) error
|
|
}{
|
|
{"DeleteOneEntry", (*FilerStoreWrapper).DeleteOneEntry},
|
|
{"DeleteEntry", func(w *FilerStoreWrapper, ctx context.Context, e *Entry) error { return w.DeleteEntry(ctx, e.FullPath) }},
|
|
} {
|
|
t.Run(del.name, func(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
wrapper := NewFilerStoreWrapper(store)
|
|
ctx := context.Background()
|
|
a, err := wrapper.FindEntry(ctx, "/d/a")
|
|
require.NoError(t, err)
|
|
|
|
store.deleteErrByPath["/d/a"] = storeRefusal
|
|
require.ErrorIs(t, del.run(wrapper, ctx, a), storeRefusal)
|
|
require.Equal(t, int32(2), linkCount(t, store, id), "the refused delete took a link off")
|
|
|
|
delete(store.deleteErrByPath, "/d/a")
|
|
require.NoError(t, del.run(wrapper, ctx, a))
|
|
require.Equal(t, int32(1), linkCount(t, store, id))
|
|
b, err := wrapper.FindEntry(ctx, "/d/b")
|
|
require.NoError(t, err)
|
|
require.Equal(t, int32(1), b.HardLinkCounter)
|
|
})
|
|
}
|
|
}
|
|
|
|
// The same for a write of another file over one of the names.
|
|
func TestWriteOverHardLinkRefusedByStoreKeepsHardLinkCount(t *testing.T) {
|
|
for _, write := range []struct {
|
|
name string
|
|
refuse func(*stubFilerStore, error)
|
|
run func(*FilerStoreWrapper, context.Context, *Entry) error
|
|
}{
|
|
{"InsertEntry", func(s *stubFilerStore, err error) { s.insertErr = err }, (*FilerStoreWrapper).InsertEntry},
|
|
{"UpdateEntry", func(s *stubFilerStore, err error) { s.updateErr = err }, (*FilerStoreWrapper).UpdateEntry},
|
|
} {
|
|
t.Run(write.name, func(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
wrapper := NewFilerStoreWrapper(store)
|
|
ctx := context.Background()
|
|
another := func() *Entry {
|
|
return &Entry{FullPath: "/d/a", Attr: Attr{Mode: 0o644}, Chunks: []*filer_pb.FileChunk{{FileId: "4,0d04", Size: 4}}}
|
|
}
|
|
|
|
write.refuse(store, storeRefusal)
|
|
require.ErrorIs(t, write.run(wrapper, ctx, another()), storeRefusal)
|
|
require.Equal(t, int32(2), linkCount(t, store, id), "the refused write took a link off")
|
|
|
|
write.refuse(store, nil)
|
|
require.NoError(t, write.run(wrapper, ctx, another()))
|
|
require.Equal(t, int32(1), linkCount(t, store, id))
|
|
})
|
|
}
|
|
}
|
|
|
|
// A rename deletes the old name of a file it has stored under the new one
|
|
// (OP "MV"): the link went with the name, so the count stays.
|
|
func TestRenameDeleteKeepsHardLinkCount(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
wrapper := NewFilerStoreWrapper(store)
|
|
ctx := context.WithValue(context.Background(), "OP", "MV")
|
|
a, err := wrapper.FindEntry(ctx, "/d/a")
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, wrapper.DeleteOneEntry(ctx, a))
|
|
require.NoError(t, wrapper.DeleteEntry(ctx, "/d/b"))
|
|
require.Equal(t, int32(2), linkCount(t, store, id))
|
|
}
|
|
|
|
// When the link cannot be taken off after the delete, the name still goes and
|
|
// the count stays one too high: the record and the chunks outlive the file's
|
|
// last name rather than go before it.
|
|
func TestDeleteKeepsHardLinkItCannotTakeOff(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
wrapper := NewFilerStoreWrapper(store)
|
|
ctx := context.Background()
|
|
a, err := wrapper.FindEntry(ctx, "/d/a")
|
|
require.NoError(t, err)
|
|
|
|
store.setKvPutErr(errors.New("kv put: store unavailable"))
|
|
require.NoError(t, wrapper.DeleteOneEntry(ctx, a))
|
|
store.setKvPutErr(nil)
|
|
_, err = store.FindEntry(ctx, "/d/a")
|
|
require.ErrorIs(t, err, filer_pb.ErrNotFound)
|
|
require.Equal(t, int32(2), linkCount(t, store, id))
|
|
}
|
|
|
|
// A client sends again a delete the store refused, then deletes the file's
|
|
// other name. Only that last delete takes the chunks.
|
|
func TestDeleteEntryRefusedAndSentAgainKeepsChunksForOtherName(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
|
|
store.deleteErrByPath["/d/a"] = storeRefusal
|
|
require.ErrorContains(t, f.DeleteEntryMetaAndData(ctx, "/d/a", false, false, true, false, nil, time.Time{}), "write conflict")
|
|
delete(store.deleteErrByPath, "/d/a")
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/a", false, false, true, false, nil, time.Time{}))
|
|
|
|
require.Equal(t, 0, f.pendingDeletionCount(), "chunks deleted while /d/b still reads them")
|
|
b, err := f.FindEntry(ctx, "/d/b")
|
|
require.NoError(t, err)
|
|
require.Equal(t, int32(1), b.HardLinkCounter)
|
|
require.Equal(t, linkedChunk, b.GetChunks()[0].GetFileIdString())
|
|
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/b", false, false, true, false, nil, time.Time{}))
|
|
require.Equal(t, 1, f.pendingDeletionCount())
|
|
require.Equal(t, int32(0), linkCount(t, store, id))
|
|
}
|
|
|
|
// Another filer links the file under a new name after this delete read the
|
|
// count of its only name: the record outlives the delete, and so do the
|
|
// chunks, which the new name reads.
|
|
func TestDeleteEntryKeepsChunksOfLinkAddedMeanwhile(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
id := linkedFile(t, store, "/d/a")
|
|
onSecondRecordRead(store, id, func() {
|
|
setLinkCount(t, store, id, 2)
|
|
store.entries["/d/c"] = &Entry{FullPath: "/d/c", Attr: Attr{Mode: 0o644}, HardLinkId: id, HardLinkCounter: 2}
|
|
})
|
|
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/a", false, false, true, false, nil, time.Time{}))
|
|
store.kvGetHook = nil
|
|
require.Equal(t, 0, f.pendingDeletionCount(), "chunks deleted while /d/c reads them")
|
|
require.Equal(t, int32(1), linkCount(t, store, id))
|
|
}
|
|
|
|
// One name deleted twice at once through two filers: both read the file's two
|
|
// links before either took one off, so both take one off and the record goes
|
|
// while /d/b still holds the file. The count each delete read still shows
|
|
// /d/b, and the chunks stay.
|
|
func TestDeleteEntryTwiceAtOnceKeepsChunks(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
onSecondRecordRead(store, id, func() {
|
|
// the other delete of /d/a takes its link off first
|
|
setLinkCount(t, store, id, 1)
|
|
})
|
|
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/a", false, false, true, false, nil, time.Time{}))
|
|
store.kvGetHook = nil
|
|
require.Equal(t, 0, f.pendingDeletionCount(), "chunks deleted while /d/b still reads them")
|
|
}
|
|
|
|
// Another filer deletes /d/a, name and link, after this delete read the name
|
|
// and before it read the count: the count it reads already lacks /d/a, so its
|
|
// link must not come off again, or the record and chunks go with /d/b left.
|
|
func TestDeleteEntryOfANameGoneSinceItsLookupKeepsTheOtherLink(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
onRecordRead(store, id, 1, func() {
|
|
delete(store.entries, "/d/a")
|
|
setLinkCount(t, store, id, 1)
|
|
})
|
|
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/a", false, false, true, false, nil, time.Time{}))
|
|
store.kvGetHook = nil
|
|
require.Equal(t, 0, f.pendingDeletionCount(), "chunks deleted while /d/b still reads them")
|
|
require.Equal(t, int32(1), linkCount(t, store, id))
|
|
}
|
|
|
|
// The second lookup of a hard-linked name fails: the delete reports the error,
|
|
// and the name stays with its link.
|
|
func TestDeleteEntryReportsAFailedSecondLookup(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
id := linkedFile(t, store, "/d/a", "/d/b")
|
|
onRecordRead(store, id, 1, func() {
|
|
store.findErrByPath = map[string]error{"/d/a": storeRefusal}
|
|
})
|
|
|
|
require.Error(t, f.DeleteEntryMetaAndData(ctx, "/d/a", false, false, true, false, nil, time.Time{}))
|
|
store.kvGetHook = nil
|
|
require.Contains(t, store.entries, "/d/a")
|
|
require.Equal(t, int32(2), linkCount(t, store, id))
|
|
require.Equal(t, 0, f.pendingDeletionCount())
|
|
}
|
|
|
|
// onRecordReadAfterDelete runs f once, when the store reads id's record after
|
|
// name is gone from it: the delete that removed name has come to take its link
|
|
// off.
|
|
func onRecordReadAfterDelete(store *stubFilerStore, id HardLinkId, name util.FullPath, f func()) {
|
|
done := false
|
|
store.kvGetHook = func(key []byte) {
|
|
if _, kept := store.entries[string(name)]; string(key) == string(id) && !kept && !done {
|
|
done = true
|
|
f()
|
|
}
|
|
}
|
|
}
|
|
|
|
// A recursive delete takes a hard-linked child as a single delete takes it.
|
|
// Refused by the store, the name stays with its link; run again, the delete
|
|
// takes the link off once, and the chunks stay with /d/b.
|
|
func TestRecursiveDeleteRefusedByStoreKeepsHardLinkCount(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
store.entries["/d/sub"] = &Entry{FullPath: "/d/sub", Attr: Attr{Mode: os.ModeDir | 0o755}}
|
|
id := linkedFile(t, store, "/d/sub/a", "/d/b")
|
|
|
|
store.deleteErrByPath["/d/sub/a"] = storeRefusal
|
|
require.ErrorContains(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}), "write conflict")
|
|
require.Equal(t, int32(2), linkCount(t, store, id), "the refused delete took a link off")
|
|
|
|
delete(store.deleteErrByPath, "/d/sub/a")
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}))
|
|
require.Equal(t, int32(1), linkCount(t, store, id))
|
|
require.Equal(t, 0, f.pendingDeletionCount(), "chunks deleted while /d/b still reads them")
|
|
}
|
|
|
|
// The folder's other children are refused after its hard-linked child went:
|
|
// that child's link is off already, so once /d/b goes too the chunks go with
|
|
// it. A store that deletes a folder's children in batches can fail after some
|
|
// of them; a hard-linked name is never among those.
|
|
func TestRecursiveDeleteFailingAfterItsLinkedChildTookItsLinkOff(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
store.entries["/d/sub"] = &Entry{FullPath: "/d/sub", Attr: Attr{Mode: os.ModeDir | 0o755}}
|
|
store.entries["/d/sub/c"] = &Entry{FullPath: "/d/sub/c", Attr: Attr{Mode: 0o644}}
|
|
id := linkedFile(t, store, "/d/sub/a", "/d/b")
|
|
|
|
store.deleteFolderErr = storeRefusal
|
|
require.ErrorContains(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}), "write conflict")
|
|
require.Equal(t, int32(1), linkCount(t, store, id))
|
|
|
|
store.deleteFolderErr = nil
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}))
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/b", false, false, true, false, nil, time.Time{}))
|
|
require.Equal(t, int32(0), linkCount(t, store, id))
|
|
require.Equal(t, 1, f.pendingDeletionCount())
|
|
}
|
|
|
|
// The folder's other children are refused after the file's last link went
|
|
// with its hard-linked child. Nothing names those chunks any more, so they
|
|
// are deleted then, not with the folder's other chunks at the end.
|
|
func TestRecursiveDeleteFailingAfterALastLinkWentDeletesItsChunks(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
store.entries["/d/sub"] = &Entry{FullPath: "/d/sub", Attr: Attr{Mode: os.ModeDir | 0o755}}
|
|
store.entries["/d/sub/c"] = &Entry{FullPath: "/d/sub/c", Attr: Attr{Mode: 0o644}}
|
|
id := linkedFile(t, store, "/d/sub/a")
|
|
|
|
store.deleteFolderErr = storeRefusal
|
|
require.ErrorContains(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}), "write conflict")
|
|
require.Equal(t, int32(0), linkCount(t, store, id))
|
|
require.Equal(t, 1, f.pendingDeletionCount(), "the chunks of the file's last link were left behind")
|
|
}
|
|
|
|
// Every name of the file is in the deleted tree, in two folders: the second
|
|
// name takes the last link off, and the chunks go once.
|
|
func TestRecursiveDeleteOfEveryNameDeletesChunksOnce(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
for _, dir := range []util.FullPath{"/d/sub", "/d/sub/x", "/d/sub/y"} {
|
|
store.entries[string(dir)] = &Entry{FullPath: dir, Attr: Attr{Mode: os.ModeDir | 0o755}}
|
|
}
|
|
id := linkedFile(t, store, "/d/sub/x/a", "/d/sub/y/b")
|
|
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}))
|
|
require.Equal(t, int32(0), linkCount(t, store, id))
|
|
require.Equal(t, 1, f.pendingDeletionCount())
|
|
}
|
|
|
|
// Two filers delete /d/sub at once, and both read the count of /d/sub/a
|
|
// before either took its link off: both take it off, and the record goes
|
|
// while /d/b still holds the file. The count each delete read still shows
|
|
// /d/b, and the chunks stay.
|
|
func TestRecursiveDeletesAtOnceKeepChunksOfANameOutside(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
store.entries["/d/sub"] = &Entry{FullPath: "/d/sub", Attr: Attr{Mode: os.ModeDir | 0o755}}
|
|
id := linkedFile(t, store, "/d/sub/a", "/d/b")
|
|
onRecordReadAfterDelete(store, id, "/d/sub/a", func() {
|
|
// the other delete of /d/sub takes the link of /d/sub/a off first
|
|
setLinkCount(t, store, id, 1)
|
|
})
|
|
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}))
|
|
store.kvGetHook = nil
|
|
require.Equal(t, 0, f.pendingDeletionCount(), "chunks deleted while /d/b still reads them")
|
|
}
|
|
|
|
// The recursive delete's take on the race above: /d/sub/a goes through another
|
|
// filer between this delete's lookup of the name and its read of the count.
|
|
func TestRecursiveDeleteOfANameGoneSinceItsLookupKeepsTheOtherLink(t *testing.T) {
|
|
store := newStubFilerStore()
|
|
f := newTestFiler(t, store, NewFilerRemoteStorage())
|
|
ctx := context.Background()
|
|
store.entries["/d/sub"] = &Entry{FullPath: "/d/sub", Attr: Attr{Mode: os.ModeDir | 0o755}}
|
|
id := linkedFile(t, store, "/d/sub/a", "/d/b")
|
|
// the listing reads the record first, the lookup of /d/sub/a second
|
|
onRecordRead(store, id, 2, func() {
|
|
delete(store.entries, "/d/sub/a")
|
|
setLinkCount(t, store, id, 1)
|
|
})
|
|
|
|
require.NoError(t, f.DeleteEntryMetaAndData(ctx, "/d/sub", true, false, true, false, nil, time.Time{}))
|
|
store.kvGetHook = nil
|
|
require.Equal(t, 0, f.pendingDeletionCount(), "chunks deleted while /d/b still reads them")
|
|
require.Equal(t, int32(1), linkCount(t, store, id))
|
|
}
|