Files
seaweedfs/weed/shell/command_volume_fsck_missing_volume_test.go
T
c7fc40c76c filer, mount: read a manifest's chunks only within its span (#13048)
* filer, mount: read a manifest's chunks only within its span

Until #13042, a truncate through a manifest only shortened the manifest
chunk; the chunks it holds stayed whole. Reads resolve those chunks at
their own offsets and sizes, so once such a file grows over the cut again
without writing it, by an extending truncate or a write past the end, the
cut bytes come back where zeros belong. #13042 keeps new truncates from
leaving such manifests, but files a mount truncated between #12462 and
#13042 still have them.

Reproduced with weed mount on a local cluster: a mount from before #13042
appends 700 lines with one close() each, so the first 256 fold into a
manifest, and truncates the file to 100 lines; a mount from current master
then extends it past its old end with ftruncate. The extended range read
8,580 bytes of the cut lines instead of zeros, through that mount and
through the filer.

Reads now cut each manifest's chunks to the manifest's span:
ResolveChunkManifestForRead, used by NonOverlappingVisibleIntervals (filer
and S3 streaming, WebDAV, the mount's merge reads), the S3 ranged reads and
the mount's peer reads, and ChunkGroup for the mount's reads. A manifest
made by MaybeManifestize spans its chunks exactly, so only shortened ones
change. ResolveChunkManifest still returns the chunks whole: MinusChunks,
deletions and the other chunk-list comparisons need every chunk a manifest
references, or the chunks past the cut would never be deleted.

* sw-client: read a manifest's chunks only within its span

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* java client: read a manifest's chunks only within its span

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: drop PR numbers from the manifest span comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: fold ResolveChunkManifestForRead into ResolveChunkManifest

ResolveChunkManifest takes cutToSpan: the read paths pass true, the
deletion and chunk-list comparison paths pass false.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: cut manifests to their span when flattening an object's chunks

A full-object SSE GET, CopyObject and UploadPartCopy flatten the
source's manifests into a plain chunk list, which drops each manifest's
span, so the chunks a truncate left past a shortened manifest came back
as object data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: purge a volume.fsck path only for chunks within a manifest's span

A chunk past a shortened manifest's span is still referenced but never
read, so a missing volume under it loses nothing. The purge decision now
uses the span-cut chunks while the inventory keeps every reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: cut manifests to their span in fs.distribute_chunks

The entry is rewritten with the resolved chunks as a flat list or a new
manifest that spans them, so chunks past a shortened manifest's span
became readable again. The filer's update still deletes them, since it
compares against the old entry's uncut chunks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: verify only the chunks within a manifest's span in fs.verify

A chunk past a shortened manifest's span is never read, so a missing
needle under it failed the entry and, with -pruneEntries, deleted a file
whose readable bytes were intact.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: mark the chunks past a manifest's span instead of allowing the visible ones

The purge gate now skips only the resolved chunks that the span cut drops,
so chunks that come from outside the manifests keep their purge behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: skip the purge for a missing past-span chunk on a listed volume

volume.fsck -findMissingChunksInFiler queued a path for deletion when a
listed volume's index lacked any chunk the uncut inventory recorded,
including a chunk past a shortened manifest's span that no read reaches.
Record those chunks per path during the filer walk and let only a missing
chunk within the span queue the purge. The path is still reported.

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>
2026-11-27 18:51:15 -08:00

302 lines
12 KiB
Go

package shell
import (
"bytes"
"strings"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/wildcard"
)
// A selected volume can already be absent from the master when fsck is used to
// clean stale Filer references after physical volume deletion. Only that
// explicitly selected missing ID should enter the purge queue.
func TestVolumeFsckFindMissingFilerEntriesForDeletedSelectedVolume(t *testing.T) {
tempDir := t.TempDir()
chunk := func(volumeID uint32, fileKey uint64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{
Fid: &filer_pb.FileId{VolumeId: volumeID, FileKey: fileKey, Cookie: 1},
Size: 1,
}
}
stub := &stubFilerServer{entries: map[string]*filer_pb.Entry{
"/": {IsDirectory: true},
"/archive": {IsDirectory: true},
"/archive/old": {Chunks: []*filer_pb.FileChunk{chunk(99, 1), chunk(99, 4), chunk(100, 2), chunk(7, 5)}},
"/live": {Chunks: []*filer_pb.FileChunk{chunk(7, 3)}},
}}
verbose := false
findMissing := true
var output bytes.Buffer
c := &commandVolumeFsck{
tempFolder: tempDir,
verbose: &verbose,
findMissingChunksInFiler: &findMissing,
writer: &output,
env: startStubFiler(t, stub),
volumeIds: map[uint32]bool{99: true},
knownVolumeIds: map[uint32]bool{7: true},
}
currentVolumes := map[string]map[uint32]VInfo{"dn1": {7: {collection: "live"}}}
var purged []purgeItem
if err := c.collectFilerFileIdAndPaths(currentVolumes, true, func(item purgeItem) {
purged = append(purged, item)
}, 0, 0); err != nil {
t.Fatalf("collect filer references: %v", err)
}
if len(purged) != 1 || purged[0].path != util.FullPath("/archive/old") {
t.Fatalf("purged paths = %v, want only /archive/old for selected missing volume 99", purged)
}
// The same logical file also references a live volume. The scan preserves
// that reference while queuing the path only once for the missing chunks.
foundSurvivingChunk := false
if err := c.readFilerFileIdFile(7, func(_ types.NeedleId, itemPath util.FullPath, _ []byte) {
if itemPath == util.FullPath("/archive/old") {
foundSurvivingChunk = true
}
}); err != nil {
t.Fatalf("read surviving volume references: %v", err)
}
if !foundSurvivingChunk {
t.Fatal("the surviving chunk reference was not recorded for volume 7")
}
for _, line := range []string{
"99,100000001 /archive/old volume not found\n",
"99,400000001 /archive/old volume not found\n",
} {
if !strings.Contains(output.String(), line) {
t.Errorf("missing-volume report %q lacks %q", output.String(), line)
}
}
if strings.Contains(output.String(), "100,200000001") {
t.Errorf("missing-volume report %q includes unselected volume 100", output.String())
}
}
// Unknown IDs remain errors outside the reverse Filer scan, preserving typo detection.
func TestVolumeFsckSelectedVolumeIDValidation(t *testing.T) {
known := map[uint32]bool{7: true}
requested := map[uint32]bool{99: true, 100: true}
if _, err := validateSelectedVolumeIDs(requested, known, false); err == nil {
t.Fatal("normal fsck accepted volume IDs not present on the master")
}
missing, err := validateSelectedVolumeIDs(requested, known, true)
if err != nil {
t.Fatalf("reverse Filer scan rejected missing volume IDs: %v", err)
}
if len(missing) != 2 || missing[0] != 99 || missing[1] != 100 {
t.Fatalf("missing IDs = %v, want sorted [99 100]", missing)
}
}
// A missing volume has no trustworthy collection metadata; a collection
// restriction must fail closed instead of silently widening or narrowing scope.
func TestValidateMissingVolumeCollectionFilter(t *testing.T) {
missing := []uint32{12}
compile := func(filter string) *wildcard.CollectionMatcher {
matcher, err := wildcard.CompileCollectionMatcher(filter)
if err != nil {
t.Fatalf("compile %q: %v", filter, err)
}
return matcher
}
for _, filter := range []string{"", "*", "ALL_COLLECTIONS", "EACH_COLLECTION"} {
if err := validateMissingVolumeCollectionFilter(missing, compile(filter)); err != nil {
t.Errorf("filter %q: %v", filter, err)
}
}
if err := validateMissingVolumeCollectionFilter(missing, compile("photos")); err == nil || !strings.Contains(err.Error(), "collection is unknown") {
t.Fatalf("expected an unknown-collection error, got %v", err)
}
}
// Missing IDs require an extra acknowledgement only when metadata deletion
// is requested; read-only diagnosis and known-volume purges are unchanged.
func TestValidateMissingVolumePurgeConfirmation(t *testing.T) {
missing := []uint32{12}
for _, test := range []struct {
name string
missing []uint32
purgeAbsent bool
confirmed bool
wantError bool
}{
{name: "read-only scan", missing: missing},
{name: "purge known volumes only", purgeAbsent: true},
{name: "confirmed missing volume purge", missing: missing, purgeAbsent: true, confirmed: true},
{name: "unconfirmed missing volume purge", missing: missing, purgeAbsent: true, wantError: true},
} {
t.Run(test.name, func(t *testing.T) {
err := validateMissingVolumePurgeConfirmation(test.missing, test.purgeAbsent, test.confirmed)
if (err != nil) != test.wantError {
t.Fatalf("validateMissingVolumePurgeConfirmation() error = %v, wantError %v", err, test.wantError)
}
if err != nil && !strings.Contains(err.Error(), "confirmMissingVolumeDeletion") {
t.Fatalf("error %q does not name the required acknowledgement flag", err)
}
})
}
}
func TestVolumeFsckPurgesEntryWhoseManifestIsOnMissingVolume(t *testing.T) {
manifest := &filer_pb.FileChunk{
Fid: &filer_pb.FileId{VolumeId: 99, FileKey: 1, Cookie: 1},
Size: 1,
IsChunkManifest: true,
}
stub := &stubFilerServer{entries: map[string]*filer_pb.Entry{
"/": {IsDirectory: true},
"/big": {Chunks: []*filer_pb.FileChunk{manifest}},
"/live": {Chunks: []*filer_pb.FileChunk{{Fid: &filer_pb.FileId{VolumeId: 7, FileKey: 2, Cookie: 1}, Size: 1}}},
}}
verbose := false
findMissing := true
var output bytes.Buffer
c := &commandVolumeFsck{
tempFolder: t.TempDir(),
verbose: &verbose,
findMissingChunksInFiler: &findMissing,
writer: &output,
env: startStubFiler(t, stub),
volumeIds: map[uint32]bool{99: true},
knownVolumeIds: map[uint32]bool{7: true},
}
var purged []purgeItem
if err := c.collectFilerFileIdAndPaths(map[string]map[uint32]VInfo{"dn1": {7: {}}}, true, func(item purgeItem) {
purged = append(purged, item)
}, 0, 0); err != nil {
t.Fatalf("collect filer references: %v", err)
}
if !strings.Contains(output.String(), "ResolveChunkManifest failed for /big") {
t.Fatalf("manifest on the missing volume resolved unexpectedly: %q", output.String())
}
if len(purged) != 1 || purged[0].path != util.FullPath("/big") {
t.Fatalf("purged paths = %v, want /big, whose manifest is on missing volume 99", purged)
}
}
// An older truncate could shorten a manifest and leave its chunks whole. A
// chunk past the cut on a missing volume loses nothing a reader sees, so it
// must not purge the path; it is still reported as a reference.
func TestVolumeFsckPurgesOnlyForChunksWithinAManifestSpan(t *testing.T) {
chunk := func(volumeID uint32, fileKey uint64, offset int64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: volumeID, FileKey: fileKey, Cookie: 1}, Offset: offset, Size: 100}
}
manifest := func(fileKey uint64, size uint64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 7, FileKey: fileKey, Cookie: 1}, Size: size, IsChunkManifest: true}
}
shortened, whole := manifest(1, 100), manifest(4, 200)
stub := &stubFilerServer{
entries: map[string]*filer_pb.Entry{
"/": {IsDirectory: true},
"/short": {Chunks: []*filer_pb.FileChunk{shortened}},
"/lost": {Chunks: []*filer_pb.FileChunk{whole}},
},
volumeUrls: map[string]string{"7": startStubManifestVolume(t, map[*filer_pb.FileChunk][]*filer_pb.FileChunk{
shortened: {chunk(7, 2, 0), chunk(99, 3, 100)},
whole: {chunk(99, 5, 0), chunk(7, 6, 100)},
})},
}
verbose := false
findMissing := true
var output bytes.Buffer
c := &commandVolumeFsck{
tempFolder: t.TempDir(),
verbose: &verbose,
findMissingChunksInFiler: &findMissing,
writer: &output,
env: startStubFiler(t, stub),
volumeIds: map[uint32]bool{99: true},
knownVolumeIds: map[uint32]bool{7: true},
}
var purged []purgeItem
if err := c.collectFilerFileIdAndPaths(map[string]map[uint32]VInfo{"dn1": {7: {}}}, true, func(item purgeItem) {
purged = append(purged, item)
}, 0, 0); err != nil {
t.Fatalf("collect filer references: %v", err)
}
if len(purged) != 1 || purged[0].path != util.FullPath("/lost") {
t.Fatalf("purged paths = %v, want only /lost, whose visible chunk is on missing volume 99", purged)
}
if !strings.Contains(output.String(), "99,300000001 /short volume not found\n") {
t.Fatalf("missing-volume report %q lacks the chunk past /short's span", output.String())
}
}
func TestVolumeFsckListedVolumePurgesOnlyForChunksWithinAManifestSpan(t *testing.T) {
chunk := func(fileKey uint64, offset int64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 7, FileKey: fileKey, Cookie: 1}, Offset: offset, Size: 100}
}
manifest := func(fileKey uint64, size uint64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 7, FileKey: fileKey, Cookie: 1}, Size: size, IsChunkManifest: true}
}
shortened, whole, both := manifest(1, 100), manifest(4, 200), manifest(7, 100)
stub := &stubFilerServer{
entries: map[string]*filer_pb.Entry{
"/": {IsDirectory: true},
"/short": {Chunks: []*filer_pb.FileChunk{shortened}},
"/lost": {Chunks: []*filer_pb.FileChunk{whole}},
"/both": {Chunks: []*filer_pb.FileChunk{both}},
},
volumeUrls: map[string]string{"7": startStubManifestVolume(t, map[*filer_pb.FileChunk][]*filer_pb.FileChunk{
shortened: {chunk(2, 0), chunk(3, 100)},
whole: {chunk(5, 0), chunk(6, 100)},
both: {chunk(8, 0), chunk(9, 100)},
})},
}
tempDir := t.TempDir()
verbose := false
findMissing := true
var output bytes.Buffer
c := &commandVolumeFsck{
tempFolder: tempDir,
verbose: &verbose,
findMissingChunksInFiler: &findMissing,
writer: &output,
env: startStubFiler(t, stub),
volumeIds: map[uint32]bool{},
knownVolumeIds: map[uint32]bool{7: true},
}
var purged []purgeItem
queuePurge := func(item purgeItem) { purged = append(purged, item) }
if err := c.collectFilerFileIdAndPaths(map[string]map[uint32]VInfo{"dn1": {7: {}}}, false, queuePurge, 0, 0); err != nil {
t.Fatalf("collect filer references: %v", err)
}
// The index lacks 3, past /short's span, 5, within /lost's, and both
// 9, past /both's span, and /both's manifest 7, listed after it.
db := needle_map.NewMemDb()
for _, needleId := range []types.NeedleId{1, 2, 4, 6, 8} {
if err := db.Set(needleId, types.ToOffset(16), types.Size(1)); err != nil {
t.Fatalf("seed volume idx: %v", err)
}
}
if err := db.SaveToIdx(getVolumeFileIdFile(tempDir, "dn1", 7)); err != nil {
t.Fatalf("write volume idx: %v", err)
}
db.Close()
if err := c.oneVolumeFileIdsCheckOneVolume("dn1", 7, queuePurge); err != nil {
t.Fatalf("check volume: %v", err)
}
purgedPaths := make(map[util.FullPath]bool)
for _, item := range purged {
purgedPaths[item.path] = true
}
if len(purged) != 2 || !purgedPaths["/lost"] || !purgedPaths["/both"] {
t.Fatalf("purged paths = %v, want /lost and /both, whose visible chunks are missing", purged)
}
if !strings.Contains(output.String(), "/short\n") {
t.Fatalf("missing-chunk report %q lacks /short", output.String())
}
}