]> git.hungrycats.org Git - bees/commitdiff
roots: do not queue the same extent twice for scanning
authorZygo Blaxell <bees@furryterror.org>
Tue, 1 Sep 2026 04:18:04 +0000 (00:18 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:05 +0000 (15:00 -0400)
A task report from a healthy run showed 22 bytenrs carrying more than
one distinct scan Task, some three deep and waiting over twenty
minutes:

  Task ID 19464  32M_2889dba000_12M      2x  1473.09s  wait
  Task ID 27833  32M_2889dba000_20.121M  1x  1244.94s  wait
  Task ID 29193  32M_2889dba000_20.121M  1x  1211.26s  wait

Successive crawl cycles re-derive overlapping extent sets, so an extent
that has not finished scanning is queued again on the next pass.  The
duplicate cannot make progress -- it parks on the same Exclusion the
first Task holds -- but it is not harmless: each one holds a crawl
progress hold, and a crawler only reports itself idle once
m_state.begin() catches up to m_state.end().  Duplicates that can never
run therefore keep the crawler permanently non-idle, which is one way
loop.exit-when-finished fails to fire on an otherwise finished
filesystem.

Claim the bytenr before creating the Task and drop the extent if it is
already claimed, releasing the claim when the Task and its closure
copies are destroyed.  crawl_extent_dup counts the drops.

Note the differing sizes at one bytenr above: the two Tasks were built
from different views of what extent lives at that address, so this also
stops two disagreeing descriptions of one extent being scanned as if
they were separate work.

Assisted-by: Claude-Code:claude-opus-5
src/bees-roots.cc

index adb3ae137d2395a06cfe4911ec1ed4a61e132fd0..05f3f7aeaaaa79b8168ec28a34e76988ea74819f 100644 (file)
@@ -539,6 +539,20 @@ BeesScanModeRecent::next_transid()
 /// each other; each tier has its own BeesCrawl position and Task.
 class BeesScanModeExtent : public BeesScanMode {
        mutex m_mutex;
+       /// Bytenrs of extents already queued for scanning and not yet
+       /// finished.  Successive crawl cycles re-derive overlapping extent
+       /// sets, so without this the same extent is queued again while the
+       /// previous Task is still parked on its Exclusion: the duplicates
+       /// cannot run, they hold crawl progress holds while they wait, and
+       /// they keep the crawler from ever reporting itself idle.
+       mutex m_inflight_mutex;
+       set<uint64_t> m_inflight;
+public:
+       /// Claim @p bytenr for scanning.  False if already queued.
+       bool inflight_insert(uint64_t bytenr);
+       /// Release a claim taken by inflight_insert().
+       void inflight_erase(uint64_t bytenr);
+private:
        mutex m_insert_root_mutex;
 
        /// Size range [m_min_size, m_max_size] for one extent size tier.
@@ -1015,6 +1029,20 @@ BeesScanModeExtent::build_chunk_map() const
        return cm;
 }
 
+bool
+BeesScanModeExtent::inflight_insert(const uint64_t bytenr)
+{
+       unique_lock<mutex> lock(m_inflight_mutex);
+       return m_inflight.insert(bytenr).second;
+}
+
+void
+BeesScanModeExtent::inflight_erase(const uint64_t bytenr)
+{
+       unique_lock<mutex> lock(m_inflight_mutex);
+       m_inflight.erase(bytenr);
+}
+
 shared_ptr<const BeesScanModeExtent::ChunkMap>
 BeesScanModeExtent::chunk_map(const bool force)
 {
@@ -1381,10 +1409,26 @@ BeesScanModeExtent::SizeTier::find_next_extent()
                // every scan method, not only the planner path.
                const auto incrementer = make_shared<ScannerByteCounters::Incrementer>(counters, created_length);
                if (m_method == 1) {
+                       // Successive crawl cycles re-derive overlapping extent sets, so
+                       // the same extent arrives here again while the Task from a
+                       // previous cycle is still parked on its Exclusion.  Queueing it
+                       // twice cannot make it run any sooner -- the duplicate parks on
+                       // the same lock -- but it does hold a second crawl progress
+                       // hold, which is what keeps a crawler from ever reporting
+                       // itself idle.  Claim the bytenr, and drop the extent if
+                       // someone already has it.
+                       if (!m_bsme->inflight_insert(this_bytenr)) {
+                               BEESCOUNT(crawl_extent_dup);
+                               continue;
+                       }
+                       const auto bsme = m_bsme;
+                       const auto inflight = shared_ptr<void>(nullptr, [bsme, this_bytenr](void *) {
+                               bsme->inflight_erase(this_bytenr);
+                       });
                        ostringstream oss;
                        oss << m_name << "_" << hex << this_bytenr << dec << "_" << pretty(this_length);
                        const auto pstate = make_shared<PlannerState>();
-                       Task scan_next_task(oss.str(), [sft, bti, hold_state, find_next_task, pstate, incrementer]() {
+                       Task scan_next_task(oss.str(), [sft, bti, hold_state, find_next_task, pstate, incrementer, inflight]() {
                                const bool restart_extent = sft->scan_one_extent_item(bti, pstate);
                                if (restart_extent) {
                                        Task::current_task().run();