mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +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>
208 lines
8.6 KiB
Go
208 lines
8.6 KiB
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
var ErrInsufficientSpace = fmt.Errorf("insufficient free space")
|
|
|
|
// CheckCompactVolume reports the garbage ratio and the read-only causes a
|
|
// sweep may still compact through.
|
|
func (s *Store) CheckCompactVolume(volumeId needle.VolumeId) (garbageRatio float64, diskSpaceLow, canDeleteMark bool, err error) {
|
|
if v := s.findVolume(volumeId); v != nil {
|
|
glog.V(3).Infof("volume %d garbage level: %f", volumeId, v.garbageLevel())
|
|
// diskSpaceLow only counts when it is the sole read-only cause — an
|
|
// operator mark or I/O quarantine still shields the volume.
|
|
_, noWriteOrDelete, noWriteCanDelete, isLow := v.ReadOnlyReasons()
|
|
// A readonlyCanDelete mark keeps taking deletes, so compacting their
|
|
// space away stays within it; a mark that refuses deletes or an I/O
|
|
// quarantine still wins. A remote-tiered volume carries the same flag
|
|
// but cannot be compacted in place.
|
|
canDeleteMark = noWriteCanDelete && !noWriteOrDelete && !v.HasRemoteFile()
|
|
return v.garbageLevel(), isLow && !noWriteOrDelete && !noWriteCanDelete, canDeleteMark, nil
|
|
}
|
|
return 0, false, false, fmt.Errorf("volume id %d is not found during check compact: %w", volumeId, ErrVolumeNotFound)
|
|
}
|
|
|
|
func (s *Store) CompactVolume(vid needle.VolumeId, preallocate int64, compactionBytePerSecond int64, progressFn ProgressFunc) error {
|
|
if v := s.findVolume(vid); v != nil {
|
|
if err := ensureCompactVolumeSpace(v, preallocate); err != nil {
|
|
return err
|
|
}
|
|
return v.CompactByIndex(&CompactOptions{
|
|
PreallocateBytes: preallocate,
|
|
MaxBytesPerSecond: compactionBytePerSecond,
|
|
ProgressCallback: progressFn,
|
|
})
|
|
}
|
|
return fmt.Errorf("volume id %d is not found during compact: %w", vid, ErrVolumeNotFound)
|
|
}
|
|
|
|
func (s *Store) CommitCompactVolume(vid needle.VolumeId) (bool, int64, error) {
|
|
if s.isStopping.Load() {
|
|
return false, 0, fmt.Errorf("volume id %d skips compact because volume is stopping", vid)
|
|
}
|
|
if v := s.findVolume(vid); v != nil {
|
|
isReadOnly := v.IsReadOnly()
|
|
err := v.CommitCompact()
|
|
var volumeSize int64 = 0
|
|
if err == nil && v.DataBackend != nil {
|
|
volumeSize, _, _ = v.DataBackend.GetStat()
|
|
}
|
|
return isReadOnly, volumeSize, err
|
|
}
|
|
return false, 0, fmt.Errorf("volume id %d is not found during commit compact: %w", vid, ErrVolumeNotFound)
|
|
}
|
|
|
|
func (s *Store) CommitCleanupVolume(vid needle.VolumeId) error {
|
|
if v := s.findVolume(vid); v != nil {
|
|
return v.cleanupCompact()
|
|
}
|
|
return fmt.Errorf("volume id %d is not found during cleaning up: %w", vid, ErrVolumeNotFound)
|
|
}
|
|
|
|
// compactionDiskFree reports the free bytes on the disk holding dir, and
|
|
// compactionSameFilesystem whether two directories draw on the same pool.
|
|
// Both are variables so a test can stand in for a full or a split disk.
|
|
var (
|
|
compactionDiskFree = func(dir string) uint64 {
|
|
return stats.NewDiskStatus(dir).Free
|
|
}
|
|
compactionSameFilesystem = sameFilesystem
|
|
)
|
|
|
|
// compactionSpaceNeeded estimates what CompactByIndex will write for v: a new
|
|
// .dat holding the live needles with their on-disk framing behind a superblock
|
|
// (or preallocate, when that is larger, since the file is preallocated to it),
|
|
// and a rebuilt index with one entry per live needle. The volume's current size
|
|
// is the wrong yardstick: the more garbage a volume holds, the less its
|
|
// compaction writes, and a store that filled up until its volumes went
|
|
// read-only is exactly where the all-garbage volumes must still compact to
|
|
// give the space back (issue #11516). Neither estimate exceeds the current file.
|
|
func compactionSpaceNeeded(v *Volume, preallocate int64) (dataBytes, indexBytes int64) {
|
|
datSize, idxSize, _ := v.FileStat()
|
|
dataBytes, indexBytes = int64(datSize), int64(idxSize)
|
|
|
|
liveCount := int64(v.FileCount()) - int64(v.DeletedCount())
|
|
liveContent := int64(v.ContentSize()) - int64(v.DeletedSize())
|
|
// A .sdx converted back to .idx carries no deleted sizes (see
|
|
// garbageLevel), and counters that disagree mean the metric is off;
|
|
// either way the whole volume stays the estimate.
|
|
deletedSizeKnown := v.DeletedCount() == 0 || v.DeletedSize() > 0
|
|
if deletedSizeKnown && liveCount >= 0 && liveContent >= 0 {
|
|
// GetActualSize(0) is the framing of an empty needle; another
|
|
// padding unit covers the worst case for any other size.
|
|
perNeedle := needle.GetActualSize(0, v.Version()) + types.NeedlePaddingSize
|
|
// Counters rebuilt from an index file (LevelDB and sorted maps) go
|
|
// through a Bloom filter with a 0.1% false positive rate that can
|
|
// count a live needle as deleted. A few percent of headroom covers
|
|
// that many times over.
|
|
if estimate := withHeadroom(super_block.SuperBlockSize + liveContent + liveCount*perNeedle); estimate < dataBytes {
|
|
dataBytes = estimate
|
|
}
|
|
if estimate := withHeadroom(liveCount * types.NeedleMapEntrySize); estimate < indexBytes {
|
|
indexBytes = estimate
|
|
}
|
|
}
|
|
if preallocate > dataBytes {
|
|
dataBytes = preallocate
|
|
}
|
|
return dataBytes, indexBytes
|
|
}
|
|
|
|
func withHeadroom(estimate int64) int64 {
|
|
return estimate + estimate/16
|
|
}
|
|
|
|
func ensureCompactVolumeSpace(v *Volume, preallocate int64) error {
|
|
dataBytes, indexBytes := compactionSpaceNeeded(v, preallocate)
|
|
volumeSize, indexSize, _ := v.FileStat()
|
|
check := func(dir string, needed int64) error {
|
|
free := compactionDiskFree(dir)
|
|
if int64(free) < needed {
|
|
return fmt.Errorf("insufficient free space for compaction in %s: need %d bytes (data: %d, index: %d, current volume: %d, current index: %d), but only %d bytes available: %w",
|
|
dir, needed, dataBytes, indexBytes, volumeSize, indexSize, free, ErrInsufficientSpace)
|
|
}
|
|
glog.V(1).Infof("volume %d compaction space check in %s: data=%d, index=%d, current volume=%d, space_needed=%d, free_space=%d",
|
|
v.Id, dir, dataBytes, indexBytes, volumeSize, needed, free)
|
|
return nil
|
|
}
|
|
// The new .dat lands next to the old one and the new .idx next to the old
|
|
// index. When the index directory is on another filesystem each disk
|
|
// answers for its own share; two directories on one filesystem draw on
|
|
// the same free space and must cover the sum.
|
|
if v.dirIdx == "" || v.dirIdx == v.dir {
|
|
return check(v.dir, dataBytes+indexBytes)
|
|
}
|
|
if !compactionSameFilesystem(v.dir, v.dirIdx) {
|
|
if err := check(v.dir, dataBytes); err != nil {
|
|
return err
|
|
}
|
|
return check(v.dirIdx, indexBytes)
|
|
}
|
|
// Same filesystem as far as the identity check can tell. The index
|
|
// directory is still asked for its own share, because a mount point the
|
|
// check cannot see (a volume mounted under one drive letter on Windows)
|
|
// would otherwise go unchecked.
|
|
if err := check(v.dir, dataBytes+indexBytes); err != nil {
|
|
return err
|
|
}
|
|
return check(v.dirIdx, indexBytes)
|
|
}
|
|
|
|
func (s *Store) CompactVolumeFiles(vid needle.VolumeId, collection string, location *DiskLocation, needleMapKind NeedleMapKind, ldbTimeout int64, preallocate int64, compactionBytePerSecond int64) (err error) {
|
|
if location == nil {
|
|
return fmt.Errorf("volume %d compaction location is nil", vid)
|
|
}
|
|
|
|
tempVolume, err := loadVolumeWithoutWorker(location.Directory, location.IdxDirectory, collection, vid, needleMapKind, ldbTimeout)
|
|
if err != nil {
|
|
return fmt.Errorf("load volume %d for offline compaction: %w", vid, err)
|
|
}
|
|
tempVolume.location = location
|
|
|
|
defer func() {
|
|
if tempVolume.tmpNm != nil {
|
|
tempVolume.tmpNm.Close()
|
|
tempVolume.tmpNm = nil
|
|
}
|
|
tempVolume.doClose()
|
|
}()
|
|
|
|
if err := ensureCompactVolumeSpace(tempVolume, preallocate); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := tempVolume.CompactByIndex(&CompactOptions{
|
|
PreallocateBytes: preallocate,
|
|
MaxBytesPerSecond: compactionBytePerSecond,
|
|
}); err != nil {
|
|
if cleanupErr := tempVolume.cleanupCompact(); cleanupErr != nil {
|
|
return fmt.Errorf("compact volume %d: %v (cleanup failed: %v)", vid, err, cleanupErr)
|
|
}
|
|
return fmt.Errorf("compact volume %d: %w", vid, err)
|
|
}
|
|
|
|
if err := tempVolume.CommitCompact(); err != nil {
|
|
// A failed commit may have swapped only one of .dat/.idx; reconcile
|
|
// rolls a decided swap forward or removes orphan temp files before
|
|
// this volume can mount with a mismatched pair.
|
|
if reconcileErr := tempVolume.reconcileCompactState(); reconcileErr != nil {
|
|
return fmt.Errorf("commit compact volume %d: %v (reconcile failed: %v)", vid, err, reconcileErr)
|
|
}
|
|
if cleanupErr := tempVolume.cleanupCompact(); cleanupErr != nil {
|
|
return fmt.Errorf("commit compact volume %d: %v (cleanup failed: %v)", vid, err, cleanupErr)
|
|
}
|
|
return fmt.Errorf("commit compact volume %d: %w", vid, err)
|
|
}
|
|
|
|
return nil
|
|
}
|