zhiqiang-hhhh opened a new pull request, #62398:
URL: https://github.com/apache/doris/pull/62398

   ## Summary
   
   Build index tasks previously reused the schema change memory limit 
(`schema_change_mem_limit_frac`), which was statically divided by 
`alter_tablet_worker_count`. This caused two problems:
   
   1. **Single build index task was under-provisioned** — it shared the budget 
with ALTER TABLE and was divided by a worker count that didn't even match the 
actual build index worker pool (`alter_index_worker_count`).
   2. **No real-time memory pressure awareness** — under heavy system load, 
build index tasks still claimed the same amount of memory, increasing OOM risk 
during concurrent execution.
   
   This PR introduces an **independent, dynamic memory limit strategy** for 
build index tasks, optimized for the common case (single task, give it maximum 
memory) while protecting against OOM during rare concurrent execution.
   
   ## New Strategy
   
   Each build index task gets up to `soft_mem_limit × 
build_index_mem_limit_frac` (default 60%), **no division by task count**. The 
actual limit is then constrained by two mechanisms:
   
   ### 1. Remaining Memory Cap
   
   ```
   limit = min(total_budget, soft_mem_limit - process_memory_usage)
   ```
   
   When the first task is already consuming memory, subsequent tasks naturally 
see less remaining memory. If the remaining memory is below `min_limit` 
(default 2GB), the task will likely fail due to the mem tracker — this is the 
intended behavior to prevent OOM.
   
   ### 2. Watermark-based Throttling
   
   | System Memory Usage | Effect |
   |---|---|
   | < 75% of soft_mem_limit | Full budget |
   | ≥ 75% (low watermark) | Budget halved |
   | ≥ 85% (high watermark) | Budget reduced to `min_limit` only |
   
   ### Configuration (all mutable at runtime)
   
   | Config | Default | Description |
   |---|---|---|
   | `build_index_mem_limit_frac` | 0.6 | Total budget as fraction of 
soft_mem_limit |
   | `build_index_min_memory_per_task_bytes` | 2GB | Floor per task, prevents 
starvation |
   | `build_index_memory_high_watermark_pct` | 85 | Aggressive throttling 
threshold |
   | `build_index_memory_low_watermark_pct` | 75 | Moderate throttling 
threshold |
   
   ## Key Mechanism Change: Mem Tracker Creation Timing
   
   The `MemTrackerLimiter` for build index tasks was previously created in the 
**constructor** of `EngineIndexChangeTask` / `EngineCloudIndexChangeTask`. This 
is problematic because tasks may be queued for a long time before execution, 
and the memory budget calculated at construction time may be stale.
   
   Now the mem tracker is created at the beginning of `execute()`, with 
RAII-based task counting (`Defer`) to ensure correctness:
   
   ```cpp
   Status EngineIndexChangeTask::execute() {
       _engine.notify_build_index_task_begin();
       Defer defer {[this]() { _engine.notify_build_index_task_end(); }};
       _mem_tracker = MemTrackerLimiter::create_shared(
               MemTrackerLimiter::Type::SCHEMA_CHANGE,
               ...,
               _engine.memory_limitation_bytes_for_build_index());
       // ... actual work
   }
   ```
   
   ## Key Code Changes
   
   - **`config.h/cpp`** — 4 new mutable configs (`DECLARE_m*/DEFINE_m*`)
   - **`storage_engine.h/cpp`** — `memory_limitation_bytes_for_build_index()` 
with remaining-memory cap + watermark throttling; 
`notify_build_index_task_{begin,end}()` for task counting; 
`_running_build_index_tasks` atomic counter
   - **`engine_index_change_task.cpp`** — Moved mem tracker from constructor to 
`execute()`, switched to new API
   - **`engine_cloud_index_change_task.cpp`** — Same changes for cloud path
   - **`build_index_memory_limit_test.cpp`** — 7 unit tests covering: task 
counting, full budget, remaining memory cap, min floor, high/low watermark 
throttling, config mutability
   
   ## Test Plan
   
   - [x] BE unit test: `BuildIndexMemoryLimitTest.*` (7 tests, all passed)
   - [ ] Regression test
   - [ ] Manual test with concurrent build index


-- 
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