Files
seaweedfs/weed/shell/command_fs_distribute_chunks_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

446 lines
16 KiB
Go

package shell
import (
"bytes"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// makeChunk builds a FileChunk with the given volume ID, file key, and byte offset.
func makeChunk(vid uint32, key uint64, offset int64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{
Fid: &filer_pb.FileId{VolumeId: vid, FileKey: key, Cookie: 1},
Offset: offset,
Size: 1024 * 1024 * 16,
}
}
// threeNodeTopology returns a minimal topology for 3 nodes with 3 volumes (one per node).
//
// node A owns vol 1, node B owns vol 2, node C owns vol 3
// no replication: each volume exists on exactly one node.
func threeNodeTopology() (volumeToOwner map[uint32]string, volumeNodesList map[uint32][]string) {
volumeToOwner = map[uint32]string{
1: "nodeA",
2: "nodeB",
3: "nodeC",
}
volumeNodesList = map[uint32][]string{
1: {"nodeA"},
2: {"nodeB"},
3: {"nodeC"},
}
return volumeToOwner, volumeNodesList
}
// ── shortName ────────────────────────────────────────────────────────────────
func TestShortName(t *testing.T) {
tests := []struct {
input string
want string
}{
{"nodeA.example.com", "nodeA"},
{"nodeA", "nodeA"},
{"10.0.0.1", "10"},
{"", ""},
}
for _, tt := range tests {
assert.Equal(t, tt.want, shortName(tt.input), "input=%q", tt.input)
}
}
// ── relevantNodes ────────────────────────────────────────────────────────────
func TestRelevantNodes(t *testing.T) {
nodeList := []string{"nodeA", "nodeB", "nodeC", "nodeD"}
activeSet := map[string]bool{"nodeA": true, "nodeB": true}
ownerCount := map[string]int{"nodeA": 3, "nodeC": 1} // nodeC has chunks but isn't active
got := relevantNodes(nodeList, activeSet, ownerCount)
// nodeA (active+chunks), nodeB (active), nodeC (inactive but has chunks)
// nodeD (inactive, no chunks) → excluded
assert.ElementsMatch(t, []string{"nodeA", "nodeB", "nodeC"}, got)
}
// ── computeOwnerTarget ───────────────────────────────────────────────────────
func TestComputeOwnerTarget_EvenDivision(t *testing.T) {
nodes := []string{"nodeA", "nodeB", "nodeC"}
activeSet := map[string]bool{"nodeA": true, "nodeB": true, "nodeC": true}
target := computeOwnerTarget(nodes, nodes, activeSet, 9, 3)
assert.Equal(t, 3, target["nodeA"])
assert.Equal(t, 3, target["nodeB"])
assert.Equal(t, 3, target["nodeC"])
}
func TestComputeOwnerTarget_UnevenDivision(t *testing.T) {
nodes := []string{"nodeA", "nodeB", "nodeC"}
activeSet := map[string]bool{"nodeA": true, "nodeB": true, "nodeC": true}
// 10 chunks / 3 nodes → 3,3,4 (remainder goes to first nodes)
target := computeOwnerTarget(nodes, nodes, activeSet, 10, 3)
total := target["nodeA"] + target["nodeB"] + target["nodeC"]
assert.Equal(t, 10, total)
for _, node := range nodes {
assert.True(t, target[node] == 3 || target[node] == 4,
"node %s has unexpected target %d", node, target[node])
}
}
func TestComputeOwnerTarget_InactiveNodesDrainToZero(t *testing.T) {
activeNodes := []string{"nodeA", "nodeB"}
allNodes := []string{"nodeA", "nodeB", "nodeC"}
activeSet := map[string]bool{"nodeA": true, "nodeB": true}
target := computeOwnerTarget(activeNodes, allNodes, activeSet, 4, 2)
assert.Equal(t, 0, target["nodeC"], "inactive node should have target 0")
assert.Equal(t, 2, target["nodeA"])
assert.Equal(t, 2, target["nodeB"])
}
// ── buildDistributionCounts ──────────────────────────────────────────────────
func TestBuildDistributionCounts(t *testing.T) {
volumeToOwner, volumeNodesList := threeNodeTopology()
// 4 chunks on vol1 (nodeA), 2 on vol2 (nodeB), 0 on nodeC
chunks := []*filer_pb.FileChunk{
makeChunk(1, 1, 0),
makeChunk(1, 2, 1),
makeChunk(1, 3, 2),
makeChunk(1, 4, 3),
makeChunk(2, 5, 4),
makeChunk(2, 6, 5),
}
chunkToNode, ownerCount, copiesCount, err := buildDistributionCounts(chunks, volumeToOwner, volumeNodesList)
require.NoError(t, err)
assert.Equal(t, 4, ownerCount["nodeA"])
assert.Equal(t, 2, ownerCount["nodeB"])
assert.Equal(t, 0, ownerCount["nodeC"])
// each chunk maps to its owner
assert.Equal(t, "nodeA", chunkToNode[0])
assert.Equal(t, "nodeB", chunkToNode[4])
// no replication → copies == owner counts
assert.Equal(t, 4, copiesCount["nodeA"])
assert.Equal(t, 2, copiesCount["nodeB"])
}
func TestBuildDistributionCounts_NilFidFallback(t *testing.T) {
volumeToOwner := map[uint32]string{3: "nodeA"}
volumeNodesList := map[uint32][]string{3: {"nodeA"}}
// chunk with nil Fid but a valid legacy FileId string "3,0123456789abcdef01"
chunks := []*filer_pb.FileChunk{
{Fid: nil, FileId: "3,0123456789abcdef01"},
}
_, ownerCount, _, err := buildDistributionCounts(chunks, volumeToOwner, volumeNodesList)
require.NoError(t, err)
assert.Equal(t, 1, ownerCount["nodeA"])
}
// ── selectActiveNodes ────────────────────────────────────────────────────────
func TestSelectActiveNodes_AllNodes(t *testing.T) {
nodeList := []string{"nodeA", "nodeB", "nodeC"}
ownerCount := map[string]int{"nodeA": 5, "nodeB": 3, "nodeC": 2}
active, activeSet := selectActiveNodes(nodeList, ownerCount, 0)
assert.Equal(t, nodeList, active)
assert.Len(t, activeSet, 3)
}
func TestSelectActiveNodes_LimitToTwo(t *testing.T) {
nodeList := []string{"nodeA", "nodeB", "nodeC"}
ownerCount := map[string]int{"nodeA": 5, "nodeB": 3, "nodeC": 0}
active, activeSet := selectActiveNodes(nodeList, ownerCount, 2)
assert.Len(t, active, 2)
// nodeA and nodeB have chunks and are the top-2 heaviest
assert.True(t, activeSet["nodeA"])
assert.True(t, activeSet["nodeB"])
assert.False(t, activeSet["nodeC"])
}
// ── planOwnerMoves ───────────────────────────────────────────────────────────
func TestPlanOwnerMoves_BasicBalance(t *testing.T) {
// nodeA has 5 chunks, nodeB/C have 0; target is 2,2,1 (or similar even split of 5)
ownerCount := map[string]int{"nodeA": 5, "nodeB": 0, "nodeC": 0}
ownerTarget := map[string]int{"nodeA": 1, "nodeB": 2, "nodeC": 2}
nodes := []string{"nodeA", "nodeB", "nodeC"}
chunkToNode := map[int]string{0: "nodeA", 1: "nodeA", 2: "nodeA", 3: "nodeA", 4: "nodeA"}
chunks := []*filer_pb.FileChunk{
makeChunk(1, 1, 0),
makeChunk(1, 2, 1),
makeChunk(1, 3, 2),
makeChunk(1, 4, 3),
makeChunk(1, 5, 4),
}
moves := planOwnerMoves(ownerCount, ownerTarget, chunkToNode, chunks, nodes)
// 4 chunks must leave nodeA
assert.Len(t, moves, 4)
for _, mv := range moves {
assert.Equal(t, "nodeA", mv.fromNode)
assert.NotEqual(t, "nodeA", mv.toNode)
}
}
func TestPlanOwnerMoves_AlreadyBalanced(t *testing.T) {
ownerCount := map[string]int{"nodeA": 2, "nodeB": 2, "nodeC": 2}
ownerTarget := map[string]int{"nodeA": 2, "nodeB": 2, "nodeC": 2}
nodes := []string{"nodeA", "nodeB", "nodeC"}
chunkToNode := map[int]string{0: "nodeA", 1: "nodeA", 2: "nodeB", 3: "nodeB", 4: "nodeC", 5: "nodeC"}
chunks := make([]*filer_pb.FileChunk, 6)
for i := range chunks {
chunks[i] = makeChunk(1, uint64(i+1), int64(i))
}
moves := planOwnerMoves(ownerCount, ownerTarget, chunkToNode, chunks, nodes)
assert.Empty(t, moves)
}
// ── planDistribution: primary ─────────────────────────────────────────────────
func TestPlanDistribution_Primary_BalancesChunks(t *testing.T) {
// 9 chunks, all on nodeA; 3 nodes; primary should balance to 3 each
volumeToOwner, volumeNodesList := threeNodeTopology()
chunks := make([]*filer_pb.FileChunk, 9)
for i := range chunks {
chunks[i] = makeChunk(1, uint64(i+1), int64(i)) // all on vol1 → nodeA
}
chunkToNode, ownerCount, copiesCount, err := buildDistributionCounts(chunks, volumeToOwner, volumeNodesList)
require.NoError(t, err)
nodeList := []string{"nodeA", "nodeB", "nodeC"}
activeNodeList, activeSet := selectActiveNodes(nodeList, ownerCount, 0)
totalChunks := len(chunks)
totalNodes := len(activeNodeList)
totalCopies := 0
for _, cnt := range copiesCount {
totalCopies += cnt
}
moves, ownerTarget := planDistribution("primary", &bytes.Buffer{},
activeNodeList, nodeList, activeSet,
ownerCount, copiesCount, chunkToNode, chunks, volumeNodesList,
totalChunks, totalNodes, totalCopies)
assert.Equal(t, 3, ownerTarget["nodeA"])
assert.Equal(t, 3, ownerTarget["nodeB"])
assert.Equal(t, 3, ownerTarget["nodeC"])
// 6 chunks must be moved off nodeA
assert.Len(t, moves, 6)
for _, mv := range moves {
assert.Equal(t, "nodeA", mv.fromNode)
}
}
func TestPlanDistribution_Primary_NoMovesWhenBalanced(t *testing.T) {
volumeToOwner, volumeNodesList := threeNodeTopology()
// 3 chunks evenly spread: one per volume/node
chunks := []*filer_pb.FileChunk{
makeChunk(1, 1, 0),
makeChunk(2, 2, 1),
makeChunk(3, 3, 2),
}
chunkToNode, ownerCount, copiesCount, err := buildDistributionCounts(chunks, volumeToOwner, volumeNodesList)
require.NoError(t, err)
nodeList := []string{"nodeA", "nodeB", "nodeC"}
activeNodeList, activeSet := selectActiveNodes(nodeList, ownerCount, 0)
totalCopies := 0
for _, cnt := range copiesCount {
totalCopies += cnt
}
moves, _ := planDistribution("primary", &bytes.Buffer{},
activeNodeList, nodeList, activeSet,
ownerCount, copiesCount, chunkToNode, chunks, volumeNodesList,
len(chunks), len(activeNodeList), totalCopies)
assert.Empty(t, moves)
}
// ── planDistribution: round-robin ─────────────────────────────────────────────
func TestPlanDistribution_RoundRobin_AssignsByOffset(t *testing.T) {
// 6 chunks all on nodeA; round-robin over 3 nodes should spread them A,B,C,A,B,C
volumeToOwner, volumeNodesList := threeNodeTopology()
chunks := []*filer_pb.FileChunk{
makeChunk(1, 1, 0),
makeChunk(1, 2, 1),
makeChunk(1, 3, 2),
makeChunk(1, 4, 3),
makeChunk(1, 5, 4),
makeChunk(1, 6, 5),
}
chunkToNode, ownerCount, copiesCount, err := buildDistributionCounts(chunks, volumeToOwner, volumeNodesList)
require.NoError(t, err)
nodeList := []string{"nodeA", "nodeB", "nodeC"}
activeNodeList, activeSet := selectActiveNodes(nodeList, ownerCount, 0)
totalCopies := 0
for _, cnt := range copiesCount {
totalCopies += cnt
}
var buf bytes.Buffer
moves, ownerTarget := planDistribution("round-robin", &buf,
activeNodeList, nodeList, activeSet,
ownerCount, copiesCount, chunkToNode, chunks, volumeNodesList,
len(chunks), len(activeNodeList), totalCopies)
// 2 chunks stay on nodeA (positions 0,3), 3 moves to nodeB and 3 to nodeC
assert.Equal(t, 2, ownerTarget["nodeA"])
assert.Equal(t, 2, ownerTarget["nodeB"])
assert.Equal(t, 2, ownerTarget["nodeC"])
// 4 chunks must move (nodeB and nodeC positions)
assert.Len(t, moves, 4)
// output should contain the round-robin preview
assert.Contains(t, buf.String(), "Round-robin assignment")
}
func TestPlanDistribution_RoundRobin_OffsetOrdering(t *testing.T) {
// chunks deliberately added in reverse offset order to verify sort
volumeToOwner := map[uint32]string{1: "nodeA", 2: "nodeB", 3: "nodeC"}
volumeNodesList := map[uint32][]string{1: {"nodeA"}, 2: {"nodeB"}, 3: {"nodeC"}}
// 3 chunks already balanced but in non-sequential order
chunks := []*filer_pb.FileChunk{
makeChunk(3, 3, 200), // highest offset → should be assigned last
makeChunk(1, 1, 0), // lowest offset → assigned to nodeList[0]
makeChunk(2, 2, 100),
}
chunkToNode, ownerCount, copiesCount, err := buildDistributionCounts(chunks, volumeToOwner, volumeNodesList)
require.NoError(t, err)
nodeList := []string{"nodeA", "nodeB", "nodeC"}
activeNodeList, activeSet := selectActiveNodes(nodeList, ownerCount, 0)
totalCopies := 0
for _, cnt := range copiesCount {
totalCopies += cnt
}
_, ownerTarget := planDistribution("round-robin", &bytes.Buffer{},
activeNodeList, nodeList, activeSet,
ownerCount, copiesCount, chunkToNode, chunks, volumeNodesList,
len(chunks), len(activeNodeList), totalCopies)
// every node gets exactly 1 chunk
assert.Equal(t, 1, ownerTarget["nodeA"])
assert.Equal(t, 1, ownerTarget["nodeB"])
assert.Equal(t, 1, ownerTarget["nodeC"])
}
// ── planDistribution: replica ─────────────────────────────────────────────────
func TestPlanDistribution_Replica_BalancesOwnerAndCopies(t *testing.T) {
// 2-node replica topology: each volume has copies on both nodeA and nodeB
volumeToOwner := map[uint32]string{
1: "nodeA", // vid%2=1 → nodeA
2: "nodeB", // vid%2=0 → nodeB (sorted: [nodeA,nodeB], idx 2%2=0 → nodeA... let's be explicit)
}
// explicit: vol1 primary=nodeA, vol2 primary=nodeB; both replicated on each other
volumeNodesList := map[uint32][]string{
1: {"nodeA", "nodeB"},
2: {"nodeA", "nodeB"},
}
// 6 chunks: 5 on vol1 (nodeA), 1 on vol2 (nodeB)
chunks := []*filer_pb.FileChunk{
makeChunk(1, 1, 0),
makeChunk(1, 2, 1),
makeChunk(1, 3, 2),
makeChunk(1, 4, 3),
makeChunk(1, 5, 4),
makeChunk(2, 6, 5),
}
chunkToNode, ownerCount, copiesCount, err := buildDistributionCounts(chunks, volumeToOwner, volumeNodesList)
require.NoError(t, err)
nodeList := []string{"nodeA", "nodeB"}
activeNodeList, activeSet := selectActiveNodes(nodeList, ownerCount, 0)
totalCopies := 0
for _, cnt := range copiesCount {
totalCopies += cnt
}
moves, ownerTarget := planDistribution("replica", &bytes.Buffer{},
activeNodeList, nodeList, activeSet,
ownerCount, copiesCount, chunkToNode, chunks, volumeNodesList,
len(chunks), len(activeNodeList), totalCopies)
// after balancing, nodeA should have 3 and nodeB should have 3
assert.Equal(t, 3, ownerTarget["nodeA"])
assert.Equal(t, 3, ownerTarget["nodeB"])
assert.NotEmpty(t, moves)
for _, mv := range moves {
assert.Equal(t, "nodeA", mv.fromNode)
assert.Equal(t, "nodeB", mv.toNode)
}
}
// ── printRedistributionPlan ──────────────────────────────────────────────────
func TestPrintRedistributionPlan_Output(t *testing.T) {
var buf bytes.Buffer
nodes := []string{"nodeA.local", "nodeB.local"}
ownerCount := map[string]int{"nodeA.local": 5, "nodeB.local": 1}
ownerTarget := map[string]int{"nodeA.local": 3, "nodeB.local": 3}
printRedistributionPlan(&buf, nodes, ownerCount, ownerTarget, 2)
out := buf.String()
assert.Contains(t, out, "2 chunks to move")
assert.Contains(t, out, "nodeA")
assert.Contains(t, out, "nodeB")
assert.Contains(t, out, "5 ->")
assert.Contains(t, out, "1 ->")
}
// The entry is rewritten with the resolved chunks, so a chunk past a
// shortened manifest's span must not come back with it.
func TestResolveEntryDataChunksCutsAShortenedManifest(t *testing.T) {
chunk := func(key uint64, offset int64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 7, FileKey: key, Cookie: 1}, Offset: offset, Size: 100}
}
shortened := &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 7, FileKey: 1, Cookie: 1}, Size: 150, IsChunkManifest: true}
env := startStubFiler(t, &stubFilerServer{volumeUrls: map[string]string{"7": startStubManifestVolume(t, map[*filer_pb.FileChunk][]*filer_pb.FileChunk{
shortened: {chunk(2, 0), chunk(3, 100), chunk(4, 200)},
})}})
dataChunks, hadManifest, err := resolveEntryDataChunks(env, &filer_pb.Entry{Chunks: []*filer_pb.FileChunk{shortened}})
require.NoError(t, err)
assert.True(t, hadManifest)
require.Len(t, dataChunks, 2)
assert.Equal(t, [2]int64{0, 100}, [2]int64{dataChunks[0].Offset, dataChunks[0].Offset + int64(dataChunks[0].Size)})
assert.Equal(t, [2]int64{100, 150}, [2]int64{dataChunks[1].Offset, dataChunks[1].Offset + int64(dataChunks[1].Size)})
}