mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
* vacuum: compact copies held read-only by a readonlyCanDelete mark A volume marked with volume.mark -readonlyCanDelete takes no new writes but keeps taking deletes, so it can drain instead of holding a volume slot (#10688). The automatic vacuum never reclaims those deletes: it skips every read-only copy unless low disk space is the only cause, and a mark clears that exception (#11519). The space comes back only once the last needle is deleted (deleteEmpty) or through volume.vacuum -volumeId, one volume at a time, so a volume that never drains completely keeps its garbage. VacuumVolumeCheck now also reports can_delete_mark: the copy is read-only through a readonlyCanDelete mark and nothing that refuses deletes (no plain read-only mark, no I/O quarantine, not tiered to remote storage). The sweep compacts such a copy like a writable one, except when the master pushed the mark itself to quarantine a duplicate volume id. The mark survives the compaction, so the copy stays read-only, keeps taking deletes and stays out of the writables. A read-only volume reserves no headroom, so the space it gives back becomes free volume slots. The Rust volume server reports the same field. In volume.vacuum, the help text and the list of read-only volumes the sweep skips leave out copies that still take deletes. For the unnamed collection, which the shell vacuums volume by volume, such volumes are left to the sweep: an explicit request would also compact a quarantined copy. An older volume server does not set the field and an older master ignores it, so a mixed cluster keeps skipping these copies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: say which marked volumes volume.vacuum leaves to the sweep For the unnamed collection, volume.vacuum sends one request per volume and skips a volume held by a readonlyCanDelete mark, while the "skipped by the sweep" hint leaves such volumes out because the sweep compacts them. With a filter matching only those volumes, the command returned without a request and without a word. It now lists the marked volumes over the threshold that it leaves to the automatic sweep. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * storage: drop the compacted needle map a read-only commit leaves unused CommitCompact reloads a read-only volume into a sorted map, so the compacted map built for the reload is never consumed: it stays in v.tmpNm with every needle of the volume, and with a leveldb index its .cpldb stays on disk. The next compaction reopens that .cpldb as is, so needles deleted between the two compactions come back in the leveldb map with stale offsets once the volume is writable again, and reads of them fail with EOF instead of not found. The sweep now compacts readonlyCanDelete volumes, so this path is no longer limited to an explicit volume.vacuum -volumeId. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * storage: do not tie the canDelete compaction test to random needle sizes newRandomNeedle sizes its data at random, so three deleted needles out of six left under 30% garbage about once in twenty runs and failed the precondition. The test only needs the deletes to leave some garbage. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: say the sweep skips quarantined copies in the left-to-sweep note The note named unnamed-collection volumes as left to the automatic sweep, but the sweep skips a copy the master holds for a duplicate volume id under the same mark, and the topology does not show that quarantine. The note now says so. It also describes the copies by what the topology shows, read-only but still taking deletes, since a copy read-only only for low disk space looks the same when its probe fails. 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>
348 lines
13 KiB
Go
348 lines
13 KiB
Go
package storage
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
// newVolumeWithGarbage writes live+deleted needles and deletes the first
|
|
// deleted ones, so the volume carries that much garbage on disk.
|
|
func newVolumeWithGarbage(t *testing.T, live, deleted int) *Volume {
|
|
t.Helper()
|
|
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("NewVolume: %v", err)
|
|
}
|
|
t.Cleanup(v.Close)
|
|
for i := 1; i <= live+deleted; i++ {
|
|
if _, _, _, err := v.writeNeedle2(newRandomNeedle(uint64(i)), true, false, false); err != nil {
|
|
t.Fatalf("write needle %d: %v", i, err)
|
|
}
|
|
}
|
|
for i := 1; i <= deleted; i++ {
|
|
if _, err := v.deleteNeedle2(newEmptyNeedle(uint64(i)), false); err != nil {
|
|
t.Fatalf("delete needle %d: %v", i, err)
|
|
}
|
|
}
|
|
return v
|
|
}
|
|
|
|
func stubCompactionDiskFree(t *testing.T, free uint64) {
|
|
t.Helper()
|
|
prev := compactionDiskFree
|
|
compactionDiskFree = func(string) uint64 { return free }
|
|
t.Cleanup(func() { compactionDiskFree = prev })
|
|
}
|
|
|
|
func TestCompactionSpaceNeeded_CountsLiveBytesNotVolumeSize(t *testing.T) {
|
|
v := newVolumeWithGarbage(t, 20, 2000)
|
|
datSize, idxSize, _ := v.FileStat()
|
|
liveContent := int64(v.ContentSize() - v.DeletedSize())
|
|
|
|
dataBytes, indexBytes := compactionSpaceNeeded(v, 0)
|
|
|
|
if dataBytes < liveContent+super_block.SuperBlockSize {
|
|
t.Fatalf("data estimate %d does not cover the %d live content bytes plus the superblock", dataBytes, liveContent)
|
|
}
|
|
if indexBytes < 20*types.NeedleMapEntrySize {
|
|
t.Fatalf("index estimate %d does not cover 20 live entries", indexBytes)
|
|
}
|
|
if dataBytes+indexBytes >= int64(datSize+idxSize) {
|
|
t.Fatalf("space needed %d is not below the current volume size %d: a mostly-garbage volume must not require its own size to compact", dataBytes+indexBytes, datSize+idxSize)
|
|
}
|
|
}
|
|
|
|
func TestCompactionSpaceNeeded_AllGarbageNeedsAlmostNothing(t *testing.T) {
|
|
v := newVolumeWithGarbage(t, 0, 2000)
|
|
datSize, _, _ := v.FileStat()
|
|
|
|
dataBytes, indexBytes := compactionSpaceNeeded(v, 0)
|
|
|
|
if needed := dataBytes + indexBytes; needed > int64(datSize)/10 {
|
|
t.Fatalf("an all-garbage volume of %d bytes still asks for %d bytes", datSize, needed)
|
|
}
|
|
}
|
|
|
|
func TestCompactionSpaceNeeded_NeverAboveCurrentVolume(t *testing.T) {
|
|
// Nothing deleted: the estimate may not exceed what is already on disk.
|
|
v := newVolumeWithGarbage(t, 200, 0)
|
|
datSize, idxSize, _ := v.FileStat()
|
|
|
|
dataBytes, indexBytes := compactionSpaceNeeded(v, 0)
|
|
|
|
if dataBytes > int64(datSize) || indexBytes > int64(idxSize) {
|
|
t.Fatalf("estimate data=%d index=%d exceeds the current files data=%d index=%d", dataBytes, indexBytes, datSize, idxSize)
|
|
}
|
|
}
|
|
|
|
func TestCompactionSpaceNeeded_PreallocateWinsForDataOnly(t *testing.T) {
|
|
// The new .dat is preallocated to this size; the rebuilt index is a
|
|
// separate file and still needs its own room.
|
|
v := newVolumeWithGarbage(t, 5, 5)
|
|
const preallocate = int64(1) << 30
|
|
|
|
dataBytes, indexBytes := compactionSpaceNeeded(v, preallocate)
|
|
|
|
if dataBytes != preallocate {
|
|
t.Fatalf("data estimate %d, want the preallocate size %d", dataBytes, preallocate)
|
|
}
|
|
if indexBytes < 5*types.NeedleMapEntrySize {
|
|
t.Fatalf("index estimate %d does not cover 5 live entries", indexBytes)
|
|
}
|
|
}
|
|
|
|
func TestEnsureCompactVolumeSpace_FullDiskWithGarbage(t *testing.T) {
|
|
// The disk-full case from #11516: free space is far below the volume's
|
|
// size, but well above what compacting its live needles will write.
|
|
v := newVolumeWithGarbage(t, 20, 2000)
|
|
datSize, idxSize, _ := v.FileStat()
|
|
dataBytes, indexBytes := compactionSpaceNeeded(v, 0)
|
|
needed := dataBytes + indexBytes
|
|
if uint64(needed) >= datSize+idxSize {
|
|
t.Fatalf("test setup: estimate %d is not below volume size %d", needed, datSize+idxSize)
|
|
}
|
|
|
|
stubCompactionDiskFree(t, uint64(needed))
|
|
if err := ensureCompactVolumeSpace(v, 0); err != nil {
|
|
t.Fatalf("free %d covers the estimate %d, volume is %d: unexpected %v", needed, needed, datSize+idxSize, err)
|
|
}
|
|
|
|
stubCompactionDiskFree(t, uint64(needed)-1)
|
|
err := ensureCompactVolumeSpace(v, 0)
|
|
if !errors.Is(err, ErrInsufficientSpace) {
|
|
t.Fatalf("free %d below the estimate %d: got %v, want ErrInsufficientSpace", needed-1, needed, err)
|
|
}
|
|
}
|
|
|
|
func TestEnsureCompactVolumeSpace_SeparateIndexDisk(t *testing.T) {
|
|
dataDir, idxDir := t.TempDir(), t.TempDir()
|
|
v, err := NewVolume(dataDir, idxDir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, needle.GetCurrentVersion(), 0, 0)
|
|
if err != nil {
|
|
t.Fatalf("NewVolume: %v", err)
|
|
}
|
|
t.Cleanup(v.Close)
|
|
for i := 1; i <= 50; i++ {
|
|
if _, _, _, err := v.writeNeedle2(newRandomNeedle(uint64(i)), true, false, false); err != nil {
|
|
t.Fatalf("write needle %d: %v", i, err)
|
|
}
|
|
}
|
|
dataBytes, indexBytes := compactionSpaceNeeded(v, 0)
|
|
|
|
free := map[string]uint64{dataDir: uint64(dataBytes), idxDir: uint64(indexBytes)}
|
|
prevFree, prevSame := compactionDiskFree, compactionSameFilesystem
|
|
compactionDiskFree = func(dir string) uint64 { return free[dir] }
|
|
compactionSameFilesystem = func(string, string) bool { return false }
|
|
t.Cleanup(func() { compactionDiskFree, compactionSameFilesystem = prevFree, prevSame })
|
|
|
|
if err := ensureCompactVolumeSpace(v, 0); err != nil {
|
|
t.Fatalf("each disk covers its own share: unexpected %v", err)
|
|
}
|
|
// Plenty of room on the data disk cannot make up for a full index disk.
|
|
free[dataDir] = uint64(dataBytes) * 10
|
|
free[idxDir] = uint64(indexBytes) - 1
|
|
if err := ensureCompactVolumeSpace(v, 0); !errors.Is(err, ErrInsufficientSpace) {
|
|
t.Fatalf("full index disk: got %v, want ErrInsufficientSpace", err)
|
|
}
|
|
|
|
// Two directories on one filesystem draw on the same free space, so the
|
|
// data and the index estimates must be covered together.
|
|
compactionSameFilesystem = func(string, string) bool { return true }
|
|
free[dataDir] = uint64(dataBytes+indexBytes) - 1
|
|
free[idxDir] = uint64(dataBytes+indexBytes) - 1
|
|
if err := ensureCompactVolumeSpace(v, 0); !errors.Is(err, ErrInsufficientSpace) {
|
|
t.Fatalf("shared filesystem short of the sum: got %v, want ErrInsufficientSpace", err)
|
|
}
|
|
free[dataDir] = uint64(dataBytes + indexBytes)
|
|
free[idxDir] = uint64(dataBytes + indexBytes)
|
|
if err := ensureCompactVolumeSpace(v, 0); err != nil {
|
|
t.Fatalf("shared filesystem covering the sum: unexpected %v", err)
|
|
}
|
|
// A mount point the identity check cannot see: the index directory
|
|
// reports its own, smaller pool and must still be checked.
|
|
free[idxDir] = uint64(indexBytes) - 1
|
|
if err := ensureCompactVolumeSpace(v, 0); !errors.Is(err, ErrInsufficientSpace) {
|
|
t.Fatalf("index mount point short of the index: got %v, want ErrInsufficientSpace", err)
|
|
}
|
|
}
|
|
|
|
func TestSameFilesystem(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if !sameFilesystem(dir, dir) {
|
|
t.Fatal("a directory is on its own filesystem")
|
|
}
|
|
if !sameFilesystem(dir, filepath.Join(dir, "missing")) {
|
|
t.Fatal("an unreadable path must be treated as shared, so the check asks for the sum")
|
|
}
|
|
}
|
|
|
|
// The estimate may not fall below the current .dat size: an all-live
|
|
// volume's compacted copy is byte-for-byte its current one.
|
|
func TestCompactionSpaceNeededCoversNeedleFraming(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 <= 100; i++ {
|
|
if _, _, _, err := v.writeNeedle2(newRandomNeedle(uint64(i)), true, false, false); err != nil {
|
|
t.Fatalf("write needle %d: %v", i, err)
|
|
}
|
|
}
|
|
datSize, _, _ := v.FileStat()
|
|
|
|
dataBytes, _ := compactionSpaceNeeded(v, 0)
|
|
if dataBytes < int64(datSize) {
|
|
t.Fatalf("estimate %d below .dat size %d for an all-live volume: missing per-needle framing", dataBytes, datSize)
|
|
}
|
|
}
|
|
|
|
// disk_space_low is only reported when low space is the sole read-only cause.
|
|
func TestCheckCompactVolumeDiskLowSoleCauseOnly(t *testing.T) {
|
|
dir := t.TempDir()
|
|
store := newSingleDirStore(t, dir)
|
|
defer store.Close()
|
|
const vid = needle.VolumeId(7)
|
|
require.NoError(t, store.AddVolume(vid, "", NeedleMapInMemory, "000", "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0))
|
|
|
|
_, low, _, err := store.CheckCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
require.False(t, low)
|
|
|
|
store.Locations[0].isDiskSpaceLow.Store(true)
|
|
_, low, _, err = store.CheckCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
require.True(t, low)
|
|
|
|
require.NoError(t, store.MarkVolumeReadonly(vid, false, false))
|
|
_, low, _, err = store.CheckCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
require.False(t, low)
|
|
}
|
|
|
|
// can_delete_mark is reported for a readonlyCanDelete mark with nothing that
|
|
// refuses deletes: low disk space does not hide it, a plain read-only mark or
|
|
// an I/O quarantine does.
|
|
func TestCheckCompactVolumeCanDeleteMark(t *testing.T) {
|
|
dir := t.TempDir()
|
|
store := newSingleDirStore(t, dir)
|
|
defer store.Close()
|
|
const vid = needle.VolumeId(8)
|
|
require.NoError(t, store.AddVolume(vid, "", NeedleMapInMemory, "000", "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0))
|
|
check := func() (low, mark bool) {
|
|
t.Helper()
|
|
_, low, mark, err := store.CheckCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
return low, mark
|
|
}
|
|
|
|
_, mark := check()
|
|
require.False(t, mark, "a writable volume carries no mark")
|
|
|
|
require.NoError(t, store.MarkVolumeReadonly(vid, true, false))
|
|
_, mark = check()
|
|
require.True(t, mark)
|
|
|
|
store.Locations[0].isDiskSpaceLow.Store(true)
|
|
low, mark := check()
|
|
require.False(t, low, "the mark is a read-only cause besides low disk space")
|
|
require.True(t, mark, "low disk space does not hide the mark")
|
|
store.Locations[0].isDiskSpaceLow.Store(false)
|
|
|
|
require.NoError(t, store.MarkVolumeReadonly(vid, false, false))
|
|
_, mark = check()
|
|
require.False(t, mark, "a plain read-only mark refuses deletes")
|
|
|
|
require.NoError(t, store.MarkVolumeReadonly(vid, true, false))
|
|
store.GetVolume(vid).markIoUnavailable(errors.New("injected"))
|
|
_, mark = check()
|
|
require.False(t, mark, "an I/O quarantine wins over the mark")
|
|
}
|
|
|
|
// Compacting a readonlyCanDelete volume keeps the mark: writes stay refused,
|
|
// deletes keep landing, and a persisted mark survives a restart.
|
|
func TestCompactKeepsReadonlyCanDeleteMark(t *testing.T) {
|
|
for _, persist := range []bool{false, true} {
|
|
name := "memory"
|
|
if persist {
|
|
name = "persisted"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
store := newSingleDirStore(t, dir)
|
|
const vid = needle.VolumeId(13)
|
|
require.NoError(t, store.AddVolume(vid, "", NeedleMapInMemory, "000", "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0))
|
|
var needles []*needle.Needle
|
|
for i := uint64(1); i <= 6; i++ {
|
|
n := newRandomNeedle(i)
|
|
_, err := store.WriteVolumeNeedle(vid, n, true, false)
|
|
require.NoError(t, err)
|
|
needles = append(needles, n)
|
|
}
|
|
require.NoError(t, store.MarkVolumeReadonly(vid, true, persist))
|
|
for _, n := range needles[:3] {
|
|
_, err := store.DeleteVolumeNeedle(vid, n, false)
|
|
require.NoError(t, err)
|
|
}
|
|
ratio, _, mark, err := store.CheckCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
require.True(t, mark)
|
|
require.Greater(t, ratio, 0.0, "precondition: the deletes left garbage")
|
|
|
|
require.NoError(t, store.CompactVolume(vid, 0, 0, nil))
|
|
readOnly, _, err := store.CommitCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
require.True(t, readOnly)
|
|
|
|
ratio, _, mark, err = store.CheckCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
require.True(t, mark, "the compaction must keep the mark")
|
|
require.Zero(t, ratio, "the compaction reclaimed the deletes")
|
|
_, noWriteOrDelete, noWriteCanDelete, _ := store.GetVolume(vid).ReadOnlyReasons()
|
|
require.True(t, noWriteCanDelete)
|
|
require.False(t, noWriteOrDelete)
|
|
|
|
_, err = store.WriteVolumeNeedle(vid, newRandomNeedle(7), true, false)
|
|
require.Error(t, err, "writes stay refused")
|
|
_, err = store.DeleteVolumeNeedle(vid, needles[3], false)
|
|
require.NoError(t, err, "deletes still land")
|
|
ratio, _, _, err = store.CheckCompactVolume(vid)
|
|
require.NoError(t, err)
|
|
require.Greater(t, ratio, 0.0, "the new delete is garbage again")
|
|
for _, n := range needles[4:] {
|
|
read := newEmptyNeedle(uint64(n.Id))
|
|
_, err = store.ReadVolumeNeedle(vid, read, nil, nil)
|
|
require.NoError(t, err)
|
|
require.Equal(t, n.Data, read.Data)
|
|
}
|
|
store.Close()
|
|
|
|
store2 := newSingleDirStore(t, dir)
|
|
defer store2.Close()
|
|
_, noWriteOrDelete, noWriteCanDelete, _ = store2.GetVolume(vid).ReadOnlyReasons()
|
|
require.Equal(t, persist, noWriteCanDelete, "only a persisted mark survives a restart")
|
|
require.False(t, noWriteOrDelete)
|
|
for i, n := range needles[3:] {
|
|
read := newEmptyNeedle(uint64(n.Id))
|
|
_, err = store2.ReadVolumeNeedle(vid, read, nil, nil)
|
|
if i == 0 {
|
|
require.Error(t, err, "the delete after the compaction stays deleted")
|
|
continue
|
|
}
|
|
require.NoError(t, err)
|
|
require.Equal(t, n.Data, read.Data)
|
|
}
|
|
})
|
|
}
|
|
}
|