doris icon indicating copy to clipboard operation
doris copied to clipboard

[Bug] All spill files land on one disk when several spill disks are configured

Open zhuxiangyi opened this issue 3 months ago • 1 comments

Search before asking

  • [X] I had searched in the issues and found no similar issues.

Version

master

What's Wrong?

On a backend configured with several spill disks, all spill files created within any two-second window land on the same disk while the other spill disks stay idle. Spill throughput is therefore capped at one disk's bandwidth, and that disk reaches its capacity limit long before the others.

The disk is chosen deterministically. _get_stores_for_spill() sorts candidates by usage ascending:

// be/src/exec/spill/spill_file_manager.cpp
std::ranges::sort(stores_with_usage, [](auto&& a, auto&& b) { return a.second < b.second; });

and create_spill_file() always takes the front entry:

// Select the first available data dir (sorted by usage ascending)
SpillDataDir* data_dir = data_dirs.front();

Sorting by usage is the right intent, but the usage value it sorts on is stale. SpillDataDir::_get_disk_usage() is derived from _available_bytes:

double _get_disk_usage(int64_t incoming_data_size) const {
    return _disk_capacity_bytes == 0
                   ? 0
                   : (double)(_disk_capacity_bytes - _available_bytes + incoming_data_size) /
                             (double)_disk_capacity_bytes;
}

and _available_bytes is only refreshed by SpillDataDir::update_capacity(), which runs on the GC thread once per config::spill_gc_interval_ms (2s by default):

void SpillFileManager::_spill_gc_thread_callback() {
    while (!_stop_background_threads_latch.wait_for(
            std::chrono::milliseconds(config::spill_gc_interval_ms))) {
        gc(config::spill_gc_work_time_ms);
        for (auto& [path, dir] : _spill_store_map) {
            static_cast<void>(dir->update_capacity());
        }
    }
}

Note that _spill_data_bytes, which is updated on every write, does not feed into _get_disk_usage(). So nothing the manager hands out during those two seconds changes the ordering: every caller computes the same usage vector, sorts it the same way, and picks the same winner.

This is easy to hit in practice — a single spilling query creates one spill file per partition per pipeline task, so hundreds of create_spill_file() calls can fall inside one refresh window.

What You Expected?

Spill files should be spread across the spill disks that are effectively equally empty, so that all configured spill disks contribute bandwidth and fill at a comparable rate. A disk that is genuinely emptier than the rest should still be preferred.

How to Reproduce?

Configure a backend with several spill disks of similar free space, then run a query that spills heavily:

set enable_spill = true;
set enable_force_spill = true;

select l_orderkey, count(*) from lineitem group by l_orderkey;

Watch the per-disk spill_disk_data_size gauges (or the on-disk spill/ directories). The bytes concentrate on one disk instead of being spread across them.

Anything Else?

StarRocks solves the same problem by picking a random start index and walking the directory list from there (be/src/compute_env/spill/dir_manager.cpp). Randomizing only among the disks that a stale snapshot cannot tell apart keeps Doris's existing "prefer the emptiest disk" behaviour intact.

Are you willing to submit PR?

  • [X] Yes I am willing to submit a PR!

Code of Conduct

zhuxiangyi avatar Aug 24 '26 07:08 zhuxiangyi

Initial maintainer analysis

Triage: This is a valid BE spill-placement bug by code inspection, with potential performance and per-disk capacity impact. I checked current upstream master at 87a7f9c0be237fd180822bce4ca9077b9ac1e332; the reported selection path is still present. The issue currently has no labels; it should be classified as a bug and routed to the BE spill owners.

Verified facts

  • _get_stores_for_spill() filters candidates by storage medium and capacity, sorts them by _get_disk_usage(0), and create_spill_file() always selects data_dirs.front().
  • _get_disk_usage() uses only _disk_capacity_bytes and _available_bytes. Those values are refreshed by update_capacity() during initialization and then by the spill GC loop, whose default interval is 2,000 ms.
  • Successful writes update _spill_data_bytes immediately, but that counter is used only for the spill-data limit check and metrics; it does not change placement ordering.
  • Consequently, repeated file creations between capacity refreshes see the same ordering and select the same minimum-usage directory. This confirms the reported batching behavior; after a refresh, the whole batch may switch to another directory rather than being balanced within the interval.
  • The conclusion applies among spill paths with the same storage_medium. The current code intentionally uses eligible SSD paths first and considers HDD paths only when no SSD path is available, so mixed SSD/HDD paths are not expected to be balanced together.

PR guidance

The proposed direction is sound: retain the primary preference for genuinely emptier disks, but use a manager-owned random or round-robin tie-breaker among candidates whose effective usage is indistinguishable. A deterministic round-robin or injectable chooser would make the unit test non-flaky.

Two implementation details need care:

  1. Exact floating-point equality may not balance disks with slightly different free-space snapshots. The PR should define and test a small, documented equivalence tolerance while ensuring a clearly emptier disk still wins.
  2. Simply adding all of _spill_data_bytes to the filesystem-reported used bytes can double-count spill data already reflected by the last get_space_info() refresh, and it still cannot reserve space for a burst of files created before their first write. A tie-breaker/reservation at file-assignment time is therefore still needed.

There is also an adjacent concurrency issue to address in the PR: _disk_capacity_bytes and _available_bytes are documented as protected by SpillDataDir::_mutex, but _get_stores_for_spill() calls _get_disk_usage(0) after reach_capacity_limit(0) has released that mutex. The GC thread can update those fields concurrently. The capacity check and usage snapshot should be exposed through a lock-protected helper; any round-robin state used by concurrent creators must likewise be atomic or protected.

Recommended coverage:

  • Multiple same-medium, equally empty directories: a burst of file assignments before a capacity refresh must use more than one directory.
  • Slightly different usage inside the chosen equivalence tolerance, plus a clearly emptier directory outside it.
  • Capacity-limited directories remain excluded and SSD-before-HDD behavior remains unchanged.
  • Concurrent creators, preferably with deterministic assertions rather than a probabilistic distribution test.

Additional information

The code is sufficient to confirm the placement defect, so logs or a query profile are not blockers for triage. To quantify the real-world impact and validate the eventual fix, it would still help to attach the exact BE commit SHA, the spill path configuration including each path's storage medium and physical mount, spill_gc_interval_ms/spill_storage_limit, and a short per-path time series of spill_disk_avail_capacity and spill_disk_data_size. BE warnings about spill or disk capacity limits would also clarify whether the observed early failure is the filesystem flood-stage limit or the configured spill-data limit.

Given that the reporter is willing to submit a PR, the next step is a focused BE fix with the deterministic multi-directory tests above; a query profile is not required to start that work.

Breakwater-GitHub-Analysis-Slot: slot_38163ea4ce6a

Doris-Breakwater avatar Aug 24 '26 08:08 Doris-Breakwater