mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-31 09:20:50 +01:00
* volume server: add idx_content to VolumeIncrementalCopyResponse A remote-tier delete is an index-only tombstone row with no .dat record, so it cannot reach an incremental copy through the .dat stream. Carrying the .idx tail verbatim lets the backup reproduce those rows at their exact positions. * backup: carry index-only tombstones through incremental copies A remote-tier delete leaves an offset-0 tombstone in .idx with no .dat record. The copy only streamed .dat bytes and regenerated .idx from them, so the tombstone never reached the backup: the deleted needle stayed live and every later incremental diverged and rebuilt again. backupCursor and ResumeByLastEntry now accept an index-only tombstone as a boundary, matched by key and size since there is no record to read a cookie from. VolumeIncrementalCopy appends the .idx tail after the .dat tail, and IncrementalBackup writes it at the index boundary, truncates, and loads the new rows — so tombstones land at the exact positions the replica has them. * seaweed-volume: mirror index-only tombstone handling in incremental copy resume_by_last_entry accepts an offset-0 tombstone boundary matched on key and size, skips its missing cookie check, and resumes .dat streaming at the next entry that has a record — index-only rows ride the new idx_content stream instead of reporting divergence. The server captures the .idx bound before the .dat one (a row is published only after its bytes) and streams the tail after the data. * backup: keep incremental copy bounds and streams consistent Review fixes: * Bound capture moved after the resume reads and reads .dat/.idx sizes together (Go FileStat, Rust file_stat under one lock), so every row resume saw is inside the index bound and each row's record is always inside the data bound. * A compaction commit changes CompactionRevision: the server now rejects the copy when the revision moved between resume, file opens, or the end of the stream, so a backup never mixes revisions. The .idx is opened up front to pin the inode. * An index tail that ends before the captured bound is an error, not a silent success. * IncrementalBackup rolls the .idx tail and .dat bytes back to the pre-copy boundary when the stream fails, so a partial row cannot wedge backupCursor and unapplied rows cannot pass for current ones. * The index tail is replayed through a small indexTailReceiver interface, covering the leveldb needle maps too, not just the in-memory map. * backup: bound the resume scan to the captured index size The resume and the stream bounds are now captured in one order: revision, then the .dat/.idx pair, then the scan. ResumeByLastEntry and resume_by_last_entry take the index bound and scan no further, so a row appended while the copy is being set up can never enter the index stream without its .dat bytes. The index-tail replay stays on the in-memory map only: the leveldb map's DoOffsetLoading writes to the .cpldb compaction database, not the live one, so it cannot serve a live tail replay; other maps keep regenerating the index from .dat as before. * seaweed-volume: guard incremental copy total against a located offset past .dat A corrupt index can place the resume offset beyond the captured .dat size; has_data already refuses the reader, but total still evaluates dat_size - s and underflows. * backup: bound the incremental .dat tail by index-covered records A record staged but not yet indexed can sit inside the captured .dat bound while its row stays outside the index bound — the fsync gap between staging and publishing is not covered by one stat. Streaming those bytes leaves them unindexed on the backup, and the next run appends the record again. Bound the .dat tail by the last record the streamed rows cover on both producers, and cover the bound walk with tests. * backup: cap the .dat stream at the captured bound and roll back replay failures sendFileContent could read past stopOffset on its last chunk, so a needle appended after FileStat arrived without its index row and the next copy appended at the wrong position. Each read is now limited to the remaining captured bytes. A failed truncateIndex or DoOffsetLoading after a successful stream left the index tail on disk unapplied, which a later backupCursor could mistake for applied rows; both now roll the tail back to the pre-copy boundary like the stream-error path. * backup: bound the incremental .dat tail by the maximum covered offset A key-sorted .idx ends on the highest-key row, which need not be the record physically last in .dat, so the last-row bound could cut off live records. Take the end of the highest-offset row inside the bound instead, and cover the case with a sorted-idx test in Go and Rust. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * backup: restore .dat and the needle map when index replay fails A failed DoOffsetLoading left the received .dat bytes in place and could leave partial mappings for rows the index rollback just removed, so the next copy appended the same records again or read stale entries. Truncate .dat back to the copy boundary and restore the touched mappings and metrics from a pre-replay snapshot. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * backup: cover no index rows with the superblock bound When no row inside the index bound names a record — an empty index or a bound of index-only tombstones — the covered end is the superblock, not the captured .dat size, so staged bytes are never streamed without a covering row. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * seaweed-volume: apply the covered bound even when the index streams no rows The bound was gated on idx_end > idx_start, so an empty captured index skipped it entirely and a staged .dat tail still shipped. Gate on the stream-index predicate instead, matching Go's streamIndex check. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * backup: propagate index coverage read errors * backup: rebuild the needle map from the truncated index on replay failure * seaweed-volume: propagate index coverage read errors --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
249 lines
8.2 KiB
Go
249 lines
8.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"os"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/idx"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
func TestBinarySearch(t *testing.T) {
|
|
var testInput []int
|
|
testInput = []int{-1, 0, 3, 5, 9, 12}
|
|
|
|
if 3 != binarySearchForLargerThanTarget(testInput, 4) {
|
|
t.Errorf("failed to find target %d", 4)
|
|
}
|
|
if 3 != binarySearchForLargerThanTarget(testInput, 3) {
|
|
t.Errorf("failed to find target %d", 3)
|
|
}
|
|
if 6 != binarySearchForLargerThanTarget(testInput, 12) {
|
|
t.Errorf("failed to find target %d", 12)
|
|
}
|
|
if 1 != binarySearchForLargerThanTarget(testInput, -1) {
|
|
t.Errorf("failed to find target %d", -1)
|
|
}
|
|
if 0 != binarySearchForLargerThanTarget(testInput, -2) {
|
|
t.Errorf("failed to find target %d", -2)
|
|
}
|
|
|
|
}
|
|
|
|
func binarySearchForLargerThanTarget(nums []int, target int) int {
|
|
l := 0
|
|
h := len(nums)
|
|
for l < h {
|
|
m := (l + h) / 2
|
|
if nums[m] <= target {
|
|
l = m + 1
|
|
} else {
|
|
h = m
|
|
}
|
|
}
|
|
return l
|
|
}
|
|
|
|
func TestResumeByLastEntry(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
|
|
if err != nil {
|
|
t.Fatalf("volume creation: %v", err)
|
|
}
|
|
defer v.Close()
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
if _, _, _, err := v.writeNeedle2(newRandomNeedle(uint64(i)), true, false, false); err != nil {
|
|
t.Fatalf("write needle %d: %v", i, err)
|
|
}
|
|
}
|
|
|
|
entryAt := func(m int64) (key types.NeedleId, cookie uint32, size types.Size) {
|
|
key, offset, size, err := v.readIndexEntry(m)
|
|
if err != nil {
|
|
t.Fatalf("readIndexEntry %d: %v", m, err)
|
|
}
|
|
n, err := v.readNeedleAt(offset)
|
|
if err != nil {
|
|
t.Fatalf("readNeedleAt %d: %v", m, err)
|
|
}
|
|
return key, uint32(n.Cookie), size
|
|
}
|
|
|
|
prefixHash := func(count uint64) uint64 {
|
|
h, err := v.hashIndexEntries(count)
|
|
if err != nil {
|
|
t.Fatalf("hashIndexEntries %d: %v", count, err)
|
|
}
|
|
return h
|
|
}
|
|
|
|
// a copy whose tail is the replica's tail is already up to date
|
|
key, cookie, size := entryAt(4)
|
|
if _, upToDate, err := v.ResumeByLastEntry(5, key, cookie, size, prefixHash(5), v.IndexFileSize()); err != nil || !upToDate {
|
|
t.Fatalf("tail cursor: upToDate=%v err=%v", upToDate, err)
|
|
}
|
|
|
|
// a copy ending at an earlier record resumes from the next entry
|
|
key, cookie, size = entryAt(2)
|
|
offset, upToDate, err := v.ResumeByLastEntry(3, key, cookie, size, prefixHash(3), v.IndexFileSize())
|
|
if err != nil || upToDate {
|
|
t.Fatalf("mid cursor: upToDate=%v err=%v", upToDate, err)
|
|
}
|
|
if want, _ := v.readOffsetFromIndex(3); offset != want {
|
|
t.Fatalf("resume offset %v, want %v", offset, want)
|
|
}
|
|
|
|
// a copy ahead of the replica diverged
|
|
if _, _, err = v.ResumeByLastEntry(6, key, cookie, size, prefixHash(3), v.IndexFileSize()); err != ErrIncrementalCopyDiverged {
|
|
t.Fatalf("ahead cursor: %v", err)
|
|
}
|
|
|
|
// a copy whose tail record or earlier records differ diverged
|
|
key, cookie, size = entryAt(4)
|
|
if _, _, err = v.ResumeByLastEntry(5, key+1, cookie, size, prefixHash(5), v.IndexFileSize()); err != ErrIncrementalCopyDiverged {
|
|
t.Fatalf("key mismatch: %v", err)
|
|
}
|
|
if _, _, err = v.ResumeByLastEntry(5, key, cookie+1, size, prefixHash(5), v.IndexFileSize()); err != ErrIncrementalCopyDiverged {
|
|
t.Fatalf("cookie mismatch: %v", err)
|
|
}
|
|
if _, _, err = v.ResumeByLastEntry(5, key, cookie, size+1, prefixHash(5), v.IndexFileSize()); err != ErrIncrementalCopyDiverged {
|
|
t.Fatalf("size mismatch: %v", err)
|
|
}
|
|
if _, _, err = v.ResumeByLastEntry(5, key, cookie, size, prefixHash(5)+1, v.IndexFileSize()); err != ErrIncrementalCopyDiverged {
|
|
t.Fatalf("prefix hash mismatch: %v", err)
|
|
}
|
|
|
|
// a remote-tier tombstone has no .dat record; a copy ending on it is a
|
|
// valid boundary identified by key and size alone
|
|
deletedKey, _, _ := entryAt(3)
|
|
if err := v.nm.Delete(deletedKey, types.ToOffset(0)); err != nil {
|
|
t.Fatalf("zero-offset tombstone: %v", err)
|
|
}
|
|
if _, upToDate, err := v.ResumeByLastEntry(6, deletedKey, 0, types.TombstoneFileSize, prefixHash(6), v.IndexFileSize()); err != nil || !upToDate {
|
|
t.Fatalf("tombstone tail cursor: upToDate=%v err=%v", upToDate, err)
|
|
}
|
|
|
|
// index-only rows are skipped when locating the .dat resume offset
|
|
if _, _, _, err := v.writeNeedle2(newRandomNeedle(6), true, false, false); err != nil {
|
|
t.Fatalf("write needle 6: %v", err)
|
|
}
|
|
key, cookie, size = entryAt(4)
|
|
offset, upToDate, err = v.ResumeByLastEntry(5, key, cookie, size, prefixHash(5), v.IndexFileSize())
|
|
if err != nil || upToDate {
|
|
t.Fatalf("cursor before tombstone: upToDate=%v err=%v", upToDate, err)
|
|
}
|
|
if want, _ := v.readOffsetFromIndex(6); offset != want {
|
|
t.Fatalf("resume offset %v, want %v", offset, want)
|
|
}
|
|
|
|
// a copy ahead of only tombstones resumes with no .dat left to send
|
|
if err := v.nm.Delete(deletedKey+1, types.ToOffset(0)); err != nil {
|
|
t.Fatalf("second zero-offset tombstone: %v", err)
|
|
}
|
|
key, cookie, size = entryAt(6)
|
|
offset, upToDate, err = v.ResumeByLastEntry(7, key, cookie, size, prefixHash(7), v.IndexFileSize())
|
|
if err != nil || upToDate {
|
|
t.Fatalf("cursor before trailing tombstone: upToDate=%v err=%v", upToDate, err)
|
|
}
|
|
if !offset.IsZero() {
|
|
t.Fatalf("resume offset %v, want zero", offset)
|
|
}
|
|
}
|
|
|
|
func TestDatSizeCoveredByIndex(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
|
|
if err != nil {
|
|
t.Fatalf("volume creation: %v", err)
|
|
}
|
|
defer v.Close()
|
|
|
|
for i := 1; i <= 3; i++ {
|
|
if _, _, _, err := v.writeNeedle2(newRandomNeedle(uint64(i)), true, false, false); err != nil {
|
|
t.Fatalf("write needle %d: %v", i, err)
|
|
}
|
|
}
|
|
datSize, idxSize, _ := v.FileStat()
|
|
|
|
covered, err := v.DatSizeCoveredByIndex(int64(idxSize))
|
|
if err != nil || covered != int64(datSize) {
|
|
t.Fatalf("covered %d, want %d, err %v", covered, datSize, err)
|
|
}
|
|
|
|
// a bound partway through covers only up to that row's record end,
|
|
// which is where the next record begins
|
|
next, err := v.readOffsetFromIndex(2)
|
|
if err != nil {
|
|
t.Fatalf("read row 2: %v", err)
|
|
}
|
|
covered, err = v.DatSizeCoveredByIndex(2 * types.NeedleMapEntrySize)
|
|
if err != nil || covered != next.ToActualOffset() {
|
|
t.Fatalf("mid bound covered %d, want %d, err %v", covered, next.ToActualOffset(), err)
|
|
}
|
|
|
|
// index-only tombstones past the bound still resolve to the last record
|
|
key, _, _, _, err := v.locateLastAppendEntry()
|
|
if err != nil {
|
|
t.Fatalf("locate last entry: %v", err)
|
|
}
|
|
if err := v.nm.Delete(key, types.ToOffset(0)); err != nil {
|
|
t.Fatalf("zero-offset tombstone: %v", err)
|
|
}
|
|
covered, err = v.DatSizeCoveredByIndex(int64(v.IndexFileSize()))
|
|
if err != nil || covered != int64(datSize) {
|
|
t.Fatalf("covered %d after tombstone, want %d, err %v", covered, datSize, err)
|
|
}
|
|
}
|
|
|
|
func TestDatSizeCoveredByIndexKeySorted(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
|
|
if err != nil {
|
|
t.Fatalf("volume creation: %v", err)
|
|
}
|
|
defer v.Close()
|
|
|
|
// write keys out of order so a key-sorted .idx ends on a mid-file row
|
|
for _, id := range []uint64{2, 3, 1} {
|
|
if _, _, _, err := v.writeNeedle2(newRandomNeedle(id), true, false, false); err != nil {
|
|
t.Fatalf("write needle %d: %v", id, err)
|
|
}
|
|
}
|
|
datSize, idxSize, _ := v.FileStat()
|
|
|
|
// rewrite the .idx sorted by key, as rebuilds do
|
|
indexFile := v.FileName(".idx")
|
|
content, err := os.ReadFile(indexFile)
|
|
if err != nil {
|
|
t.Fatalf("read idx: %v", err)
|
|
}
|
|
var rows [][]byte
|
|
for i := 0; i+types.NeedleMapEntrySize <= len(content); i += types.NeedleMapEntrySize {
|
|
rows = append(rows, content[i:i+types.NeedleMapEntrySize])
|
|
}
|
|
sort.Slice(rows, func(i, j int) bool {
|
|
keyI, _, _ := idx.IdxFileEntry(rows[i])
|
|
keyJ, _, _ := idx.IdxFileEntry(rows[j])
|
|
return keyI < keyJ
|
|
})
|
|
var sorted []byte
|
|
for _, row := range rows {
|
|
sorted = append(sorted, row...)
|
|
}
|
|
if err := os.WriteFile(indexFile, sorted, 0644); err != nil {
|
|
t.Fatalf("rewrite idx: %v", err)
|
|
}
|
|
|
|
covered, err := v.DatSizeCoveredByIndex(int64(idxSize))
|
|
if err != nil || covered != int64(datSize) {
|
|
t.Fatalf("key-sorted covered %d, want %d, err %v", covered, datSize, err)
|
|
}
|
|
}
|