mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-08-21 14:46:58 +00:00
* ec: add EC bitrot checksum protobuf EcBitrotProtection/EcShardChecksums/ChecksumAlgorithm sidecar messages, copy_ecsum_file and unsafe_ignore_sidecar fields, and a CHECKSUM scrub mode. * ec: bitrot checksum sidecar format, validation, and per-volume load Per-shard CRC32C block checksums in an optional <base>.ecsum sidecar with a self-integrity header; validation, rolling builder, backfill primitive, and EcVolume load on mount + removal on destroy. * ec: capture per-shard checksums at encode; verify-and-exclude on rebuild WriteEcFilesWithContext returns the protection computed inline during encoding. generateMissingEcFiles verifies present inputs against the sidecar, excludes corrupt ones, regenerates in place, and re-verifies; fail-closed unless unsafe_ignore_sidecar, removing all generated outputs on failure. * ec: read-only checksum scrub with Reed-Solomon arbiter ChecksumScrub verifies each local shard against the sidecar and reconstructs flagged shards from the clean shards so stale-sidecar false positives are not reported. Wired to the gRPC CHECKSUM mode and ec.scrub -mode checksum. * ec: server-side bitrot sidecar write, copy, cleanup, and opportunistic backfill Write .ecsum at fresh encode; propagate it with copy_ecsum_file (tolerant); remove it on full delete and decode; rebuild honors unsafe_ignore_sidecar and opportunistically backfills a sidecar when all shards are reachable. * ec: volume server bitrot config flags -ec.bitrotChecksum (default on) and -ec.bitrotBlockSizeMB (default 16). * fix(ec_bitrot): bound -ec.bitrotBlockSizeMB before the int64 multiply Validate the MiB value is in [1, 1024] before multiplying by 1 MiB, so a huge flag value cannot overflow int64 and slip past the power-of-two check, and a block size cannot collapse a sidecar to a few oversized blocks. * fix(ec_bitrot): distribute the .ecsum sidecar from the worker encode path The worker EC encode wrote the generation-0 sidecar locally but never added it to shardFiles, so DistributeEcShards never shipped it and the distributed holders came up unprotected. Append it to shardFiles and map the ecsum shard type to its extension in the sender so it travels with the shards. * fix(ec_bitrot): remove orphaned sidecars when the generation is gone Gate sidecar removal on existingShardCount==0 alone rather than also requiring a stray .ecx. A sidecar whose shards have all been deleted is orphaned and must be removed even when no .ecx remains, or it leaks. .ecx/.ecj/.vif removal stays gated on hasEcxFile as before. * fix(ec_bitrot): do not fold checksum blocks scanned into TotalFiles ChecksumScrub's first return is blocks scanned, not files. Discard it so the scrub response's TotalFiles (a needle/file count) is not inflated by the block count for CHECKSUM mode. * test(ec_bitrot): clean up generated .ecsum sidecars in removeGeneratedFiles * fix(ec_bitrot): reject an oversized sidecar payload before the uint32 cast The header stores payload_len as a uint32; bound the payload before the conversion so a pathological manifest cannot truncate the length field and corrupt the sidecar. A real manifest is a few KB, so this never trips. * fix(ec_bitrot): cap -ec.bitrotBlockSizeMB at 64 MiB The block size becomes the per-shard scratch buffer the scrub/backfill path allocates, so an over-large value (e.g. 1 GiB) is a memory hazard per concurrent scrub worker. Lower the upper bound from 1024 to 64 MiB. * fix(ec_bitrot): add -ecUnsafeIgnoreSidecar to weed tool fix -ecx The -ecx recovery path reconstructs missing shards via RebuildEcFilesWithContext, which fails closed on a malformed/stale .ecsum. Without an override flag an operator could not complete the rebuild without manually deleting the sidecar. Expose -ecUnsafeIgnoreSidecar (default false) and thread it through. * fix(ec_bitrot): bound sidecar payload with a direct int constant; drop readFull Guard len(payload) against a plain int constant (1 GiB) before the allocation instead of a uint64 MaxUint32 compare, so the allocation-size value is provably bounded (clears the CodeQL overflow alert) and the math import is no longer needed. Inline os.File.ReadAt with io.EOF handling in verifyShardFileBlocks and remove the now-redundant readFull helper (os.File.ReadAt fills the slice or errors). * test(ec_bitrot): use slices.Contains instead of a hand-rolled containsU32 * refactor(ec): fold the EcFiles WithContext variants into the base functions RebuildEcFiles now takes the *ECContext directly (nil => derive from .vif as before) and WriteEcFiles takes it too (nil => default), removing the parallel RebuildEcFilesWithContext / WriteEcFilesWithContext names. Callers that had an explicit context drop the WithContext suffix; the default-context callers pass nil. No behavior change. * refactor(ec): pass BackgroundECContext instead of nil to Write/RebuildEcFiles Add a non-nil BackgroundECContext placeholder (analogous to context.Background()) and have callers with no specific layout pass it instead of a nil *ECContext. WriteEcFiles resolves a zero/background context to the default ratio and RebuildEcFiles resolves it from the .vif, so behavior is unchanged. * fix(ec_bitrot): make BackgroundECContext a func; RebuildEcFiles fails closed on bad .vif - BackgroundECContext is now a function returning a fresh *ECContext, so callers cannot mutate a shared singleton or race on it (and it mirrors context.Background, which is also a function). - RebuildEcFiles now propagates the MaybeLoadVolumeInfo error: a present-but- unreadable .vif fails closed instead of silently rebuilding with the default ratio (which would corrupt a custom-ratio volume). Pass an explicit ctx to override.
225 lines
8.5 KiB
Go
225 lines
8.5 KiB
Go
package erasure_coding
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
// TestEcConsistency_WritesBetweenEncodeAndEcx reproduces a race condition that
|
|
// existed in VolumeEcShardsGenerate before the fix in this PR.
|
|
//
|
|
// Previously, the order was:
|
|
// 1. WriteEcFiles(baseFileName, ecCtx) — EC shards from .dat
|
|
// 2. WriteSortedFileFromIdx(v.IndexFileName(), ".ecx") — .ecx from .idx
|
|
//
|
|
// If a write appended data to .dat/.idx between steps 1 and 2, the .ecx would
|
|
// have entries pointing to data that doesn't exist in the EC shards.
|
|
//
|
|
// The fix reverses the order (write .ecx first, then generate EC shards), so
|
|
// that .ecx is always a subset of what the EC shards contain.
|
|
//
|
|
// This test simulates the old buggy sequence to validate that the problem is real.
|
|
func TestEcConsistency_WritesBetweenEncodeAndEcx(t *testing.T) {
|
|
dir := t.TempDir()
|
|
baseFileName := dir + "/consistency"
|
|
|
|
ctx := NewDefaultECContext("", 0)
|
|
|
|
// Phase 1: Create initial .dat and .idx with known data
|
|
datSize := int64(largeBlockSize*DataShardsCount + smallBlockSize*DataShardsCount*3) // 1 large row + 3 small rows
|
|
originalData := make([]byte, datSize)
|
|
rand.Read(originalData)
|
|
|
|
err := os.WriteFile(baseFileName+".dat", originalData, 0644)
|
|
require.NoError(t, err)
|
|
|
|
// Create a minimal .idx with one entry pointing to the data
|
|
createTestIdx(t, baseFileName+".idx", []idxEntry{
|
|
{id: 1, offset: 0, size: types.Size(datSize)},
|
|
})
|
|
|
|
// Phase 2: EC encode — generates .ec00-.ec13 from current .dat
|
|
_, err = generateEcFiles(baseFileName, int(smallBlockSize), largeBlockSize, smallBlockSize, ctx)
|
|
require.NoError(t, err, "EC encoding")
|
|
|
|
// Phase 3: SIMULATE a write between EC encoding and .ecx generation
|
|
// (reproducing the old buggy order where .ecx was generated after EC shards)
|
|
extraData := make([]byte, 5000)
|
|
rand.Read(extraData)
|
|
|
|
f, err := os.OpenFile(baseFileName+".dat", os.O_WRONLY|os.O_APPEND, 0644)
|
|
require.NoError(t, err)
|
|
_, err = f.Write(extraData)
|
|
require.NoError(t, err)
|
|
f.Close()
|
|
|
|
// Update .idx with the new entry
|
|
createTestIdx(t, baseFileName+".idx", []idxEntry{
|
|
{id: 1, offset: 0, size: types.Size(datSize)},
|
|
{id: 2, offset: datSize, size: types.Size(len(extraData))},
|
|
})
|
|
|
|
// Phase 4: Generate .ecx from the UPDATED .idx (as the old buggy code did)
|
|
err = WriteSortedFileFromIdx(baseFileName, ".ecx")
|
|
require.NoError(t, err, "WriteSortedFileFromIdx")
|
|
|
|
// Phase 5: Now try to read needle 2 via EC shards — it should fail
|
|
// because the EC shards were generated from the OLD .dat (without the extra data)
|
|
ecFiles, err := openEcFiles(baseFileName, true, ctx)
|
|
require.NoError(t, err)
|
|
defer closeEcFiles(ecFiles)
|
|
|
|
ecStat, err := ecFiles[0].Stat()
|
|
require.NoError(t, err)
|
|
shardSize := ecStat.Size()
|
|
|
|
// Read needle 2 (the one added after EC encoding) using LocateData.
|
|
// Use shardSize-1 to simulate the ecdFileSize fallback path used by
|
|
// LocateEcShardNeedleInterval when datFileSize is unavailable.
|
|
actualSize := needle.GetActualSize(types.Size(len(extraData)), needle.Version3)
|
|
intervals := LocateData(largeBlockSize, smallBlockSize, shardSize-1, datSize, types.Size(actualSize))
|
|
|
|
t.Logf("Trying to read needle 2 at offset %d size %d from EC shards (shardSize=%d)", datSize, actualSize, shardSize)
|
|
t.Logf("Intervals: %+v", intervals)
|
|
|
|
// Try to read — this will either fail with an error (offset out of bounds)
|
|
// or return garbage data (the padded zeros from EC encoding)
|
|
ecData, readErr := assembleFromIntervalsAllowError(ecFiles, intervals, largeBlockSize, smallBlockSize)
|
|
|
|
if readErr != nil {
|
|
t.Logf("CONFIRMED: Read error for needle written after EC encoding: %v", readErr)
|
|
} else {
|
|
// If we got data, it should be zeros (padding) or garbage, not the actual extraData
|
|
isAllZeros := true
|
|
for _, b := range ecData {
|
|
if b != 0 {
|
|
isAllZeros = false
|
|
break
|
|
}
|
|
}
|
|
if isAllZeros {
|
|
t.Logf("CONFIRMED: Read returned zero-padded data (EC shards don't have the needle)")
|
|
} else if !bytes.Equal(ecData[:len(extraData)], extraData) {
|
|
t.Logf("CONFIRMED: Read returned wrong data (EC shards don't have the needle)")
|
|
} else {
|
|
t.Error("UNEXPECTED: Read returned correct data — needle should NOT be in EC shards")
|
|
}
|
|
}
|
|
|
|
// Phase 6: Verify a small read from the original data still works.
|
|
// Use the correct shardDatSize (from the original datSize, not the modified one)
|
|
// to avoid the fallback heuristic issues.
|
|
shardDatSize := datSize / int64(DataShardsCount)
|
|
readSize := types.Size(smallBlockSize)
|
|
intervals1 := LocateData(largeBlockSize, smallBlockSize, shardDatSize, 0, readSize)
|
|
ecData1, err := assembleFromIntervalsAllowError(ecFiles, intervals1, largeBlockSize, smallBlockSize)
|
|
require.NoError(t, err, "reading original data from EC shards")
|
|
|
|
assert.True(t, bytes.Equal(originalData[:readSize], ecData1),
|
|
"Original data at offset 0 should match EC shard data")
|
|
t.Logf("Original data reads correctly from EC shards")
|
|
}
|
|
|
|
// TestEcConsistency_ExactLargeRowEncoding verifies that generateEcFiles correctly
|
|
// encodes a .dat file whose size is exactly one large row (DataShardsCount *
|
|
// largeBlockSize), producing shards of exactly largeBlockSize each, and that
|
|
// every chunk of the encoded data can be read back correctly via LocateData.
|
|
func TestEcConsistency_ExactLargeRowEncoding(t *testing.T) {
|
|
dir := t.TempDir()
|
|
baseFileName := dir + "/exact"
|
|
ctx := NewDefaultECContext("", 0)
|
|
|
|
datSize := int64(largeBlockSize * DataShardsCount) // exactly 1 large row
|
|
data := make([]byte, datSize)
|
|
rand.Read(data)
|
|
err := os.WriteFile(baseFileName+".dat", data, 0644)
|
|
require.NoError(t, err)
|
|
|
|
// EC encode
|
|
_, err = generateEcFiles(baseFileName, int(smallBlockSize), largeBlockSize, smallBlockSize, ctx)
|
|
require.NoError(t, err)
|
|
|
|
// Check shard sizes — each shard should be exactly largeBlockSize
|
|
ecFiles, err := openEcFiles(baseFileName, true, ctx)
|
|
require.NoError(t, err)
|
|
defer closeEcFiles(ecFiles)
|
|
|
|
for i := 0; i < ctx.DataShards; i++ {
|
|
stat, err := ecFiles[i].Stat()
|
|
require.NoError(t, err, "stat shard %d", i)
|
|
assert.Equal(t, int64(largeBlockSize), stat.Size(),
|
|
"data shard %d should be exactly largeBlockSize", i)
|
|
}
|
|
|
|
// Verify data reads correctly at every smallBlockSize offset via LocateData
|
|
shardDatSize := datSize / int64(ctx.DataShards)
|
|
readSize := types.Size(smallBlockSize)
|
|
for offset := int64(0); offset+int64(readSize) <= datSize; offset += int64(readSize) {
|
|
intervals := LocateData(largeBlockSize, smallBlockSize, shardDatSize, offset, readSize)
|
|
ecData, err := assembleFromIntervalsAllowError(ecFiles, intervals, largeBlockSize, smallBlockSize)
|
|
require.NoError(t, err, "reading at offset %d", offset)
|
|
expected := data[offset : offset+int64(readSize)]
|
|
assert.True(t, bytes.Equal(expected, ecData),
|
|
"data mismatch at offset %d", offset)
|
|
}
|
|
}
|
|
|
|
type idxEntry struct {
|
|
id types.NeedleId
|
|
offset int64
|
|
size types.Size
|
|
}
|
|
|
|
func createTestIdx(t *testing.T, filename string, entries []idxEntry) {
|
|
t.Helper()
|
|
f, err := os.Create(filename)
|
|
require.NoError(t, err)
|
|
defer f.Close()
|
|
|
|
buf := make([]byte, types.NeedleMapEntrySize)
|
|
for _, e := range entries {
|
|
types.NeedleIdToBytes(buf[:types.NeedleIdSize], e.id)
|
|
types.OffsetToBytes(buf[types.NeedleIdSize:types.NeedleIdSize+types.OffsetSize], types.ToOffset(e.offset))
|
|
types.SizeToBytes(buf[types.NeedleIdSize+types.OffsetSize:], e.size)
|
|
_, err := f.Write(buf)
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
func assembleFromIntervalsAllowError(ecFiles []*os.File, intervals []Interval, large, small int64) ([]byte, error) {
|
|
var data []byte
|
|
for _, interval := range intervals {
|
|
shardId, shardOffset := interval.ToShardIdAndOffset(large, small)
|
|
if int(shardId) >= len(ecFiles) {
|
|
return nil, fmt.Errorf("shard %d out of range (have %d files)", shardId, len(ecFiles))
|
|
}
|
|
stat, err := ecFiles[shardId].Stat()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("stat shard %d: %v", shardId, err)
|
|
}
|
|
if shardOffset+int64(interval.Size) > stat.Size() {
|
|
return nil, fmt.Errorf("read past end of shard %d: offset %d + size %d > fileSize %d",
|
|
shardId, shardOffset, interval.Size, stat.Size())
|
|
}
|
|
chunk := make([]byte, interval.Size)
|
|
n, err := ecFiles[shardId].ReadAt(chunk, shardOffset)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read shard %d offset %d: %v", shardId, shardOffset, err)
|
|
}
|
|
if n != int(interval.Size) {
|
|
return nil, fmt.Errorf("short read shard %d: got %d want %d", shardId, n, interval.Size)
|
|
}
|
|
data = append(data, chunk...)
|
|
}
|
|
return data, nil
|
|
}
|