github-actions[bot] commented on code in PR #61833:
URL: https://github.com/apache/doris/pull/61833#discussion_r3097971264


##########
be/src/cloud/cloud_tablet_hotspot.cpp:
##########
@@ -184,6 +316,103 @@ uint64_t HotspotCounter::qpw() {
     return week_history_counter + cur_counter.load();
 }
 
+bool TabletHotspot::is_gc_eligible(const HotspotCounter& counter, 
SystemTimePoint now) {
+    const auto week_window = 
std::chrono::seconds((HotspotCounter::week_counters_size + 1) *
+                                                  
HotspotCounter::time_interval);
+    return counter.last_access_time < now - week_window &&
+           counter.cur_counter.load(std::memory_order_relaxed) == 0 &&
+           counter.day_history_counter.load(std::memory_order_relaxed) == 0 &&
+           counter.week_history_counter.load(std::memory_order_relaxed) == 0;
+}
+
+TabletHotspot::MaintenanceStats 
TabletHotspot::run_maintenance_once(SystemTimePoint now) {
+    MaintenanceStats stats;
+    auto gc_elapsed = SteadyClock::duration::zero();
+
+    for (size_t slot_idx = 0; slot_idx < s_slot_size; ++slot_idx) {
+        auto& slot = _tablets_hotspot[slot_idx];
+        std::vector<HotspotCounterPtr> counters;
+        size_t slot_size_before = 0;
+        size_t slot_bucket_count_before = 0;
+
+        {
+            std::lock_guard lock(slot.mtx);
+            slot_size_before = slot.map.size();
+            slot_bucket_count_before = slot.map.bucket_count();
+            stats.total_counters_before_gc += slot_size_before;
+            if (slot_size_before > 0) {
+                ++stats.non_empty_slots_before_gc;
+            }
+            stats.max_slot_size_before_gc =
+                    std::max<uint64_t>(stats.max_slot_size_before_gc, 
slot_size_before);
+            stats.sum_bucket_count_before_gc += slot_bucket_count_before;
+            counters.reserve(slot_size_before);
+            for (auto& [_, counter] : slot.map) {
+                counters.push_back(counter);
+            }
+        }
+        stats.copied_counters += counters.size();
+        std::for_each(counters.begin(), counters.end(),
+                      [](HotspotCounterPtr& counter) { 
counter->make_dot_point(); });
+
+        size_t erased_count = 0;
+        bool compacted = false;
+        size_t slot_size_after = 0;
+        size_t slot_bucket_count_after = 0;
+        auto gc_start = SteadyClock::now();
+        {
+            std::lock_guard lock(slot.mtx);
+            for (auto iter = slot.map.begin(); iter != slot.map.end();) {
+                if (is_gc_eligible(*iter->second, now)) {
+                    iter = slot.map.erase(iter);
+                    ++erased_count;
+                } else {
+                    ++iter;
+                }

Review Comment:
   Rebuilding the shard inside this `slot.mtx` critical section will block 
`TabletHotspot::count()` for every tablet hashing into the same slot. `count()` 
sits on the cloud scan preparation path (`parallel_scanner_builder.cpp`, 
`olap_scanner.cpp`, `olap_scan_operator.cpp`), so in the exact sparse-shard 
scenario this PR is targeting, an hourly GC round can turn into visible 
query-latency spikes. Please avoid doing the full rehash under the shard mutex 
and keep the critical section to the minimum state swap/erase work.



##########
be/src/cloud/cloud_tablet_hotspot.cpp:
##########
@@ -184,6 +316,103 @@ uint64_t HotspotCounter::qpw() {
     return week_history_counter + cur_counter.load();
 }
 
+bool TabletHotspot::is_gc_eligible(const HotspotCounter& counter, 
SystemTimePoint now) {
+    const auto week_window = 
std::chrono::seconds((HotspotCounter::week_counters_size + 1) *
+                                                  
HotspotCounter::time_interval);
+    return counter.last_access_time < now - week_window &&
+           counter.cur_counter.load(std::memory_order_relaxed) == 0 &&
+           counter.day_history_counter.load(std::memory_order_relaxed) == 0 &&
+           counter.week_history_counter.load(std::memory_order_relaxed) == 0;
+}
+
+TabletHotspot::MaintenanceStats 
TabletHotspot::run_maintenance_once(SystemTimePoint now) {
+    MaintenanceStats stats;
+    auto gc_elapsed = SteadyClock::duration::zero();
+
+    for (size_t slot_idx = 0; slot_idx < s_slot_size; ++slot_idx) {
+        auto& slot = _tablets_hotspot[slot_idx];
+        std::vector<HotspotCounterPtr> counters;
+        size_t slot_size_before = 0;
+        size_t slot_bucket_count_before = 0;
+
+        {
+            std::lock_guard lock(slot.mtx);
+            slot_size_before = slot.map.size();
+            slot_bucket_count_before = slot.map.bucket_count();
+            stats.total_counters_before_gc += slot_size_before;
+            if (slot_size_before > 0) {
+                ++stats.non_empty_slots_before_gc;
+            }
+            stats.max_slot_size_before_gc =
+                    std::max<uint64_t>(stats.max_slot_size_before_gc, 
slot_size_before);
+            stats.sum_bucket_count_before_gc += slot_bucket_count_before;
+            counters.reserve(slot_size_before);
+            for (auto& [_, counter] : slot.map) {
+                counters.push_back(counter);
+            }
+        }
+        stats.copied_counters += counters.size();
+        std::for_each(counters.begin(), counters.end(),
+                      [](HotspotCounterPtr& counter) { 
counter->make_dot_point(); });
+
+        size_t erased_count = 0;
+        bool compacted = false;
+        size_t slot_size_after = 0;
+        size_t slot_bucket_count_after = 0;
+        auto gc_start = SteadyClock::now();
+        {
+            std::lock_guard lock(slot.mtx);
+            for (auto iter = slot.map.begin(); iter != slot.map.end();) {
+                if (is_gc_eligible(*iter->second, now)) {
+                    iter = slot.map.erase(iter);
+                    ++erased_count;
+                } else {
+                    ++iter;
+                }
+            }
+

Review Comment:
   This new compaction branch allocates a second `unordered_map` for all 
surviving entries, but there is no `try_reserve()`/fallback handling and 
`make_dot_point()` has no catch boundary above it. When a hotspot shard is 
already large enough to need compaction, `reserve()`/`emplace()` here can fail 
under memory pressure; an uncaught allocation exception on `_counter_thread` 
will call `std::terminate` and take down the BE. Please make this path 
fail-safe (reserve with rollback/fallback, or catch and skip compaction/log 
instead of letting the background thread abort the process).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to