matanper opened a new pull request, #71237:
URL: https://github.com/apache/doris/pull/71237
### What problem does this PR solve?
Issue Number: close #68903
Related PR: None
Problem Summary:
Every `ObjStorageFileWriter` submits its parts to the one
`S3FileUploadThreadPool` (`obj_storage_file_buffer.cpp`,
`submit_upload_buffer`) and its non-blocking close to the one
`NonBlockCloseThreadPool` (`ObjStorageFileWriter::close`). Load segment files
and compaction / schema change output share both pools, with no priority and no
bound on queued parts. When many compactions run, their parts and closes queue
ahead of load closes, and every queued part keeps an `s3_write_buffer_size`
buffer alive. Load commit latency and BE memory then grow together (see the
issue for the production numbers).
#### Change
- `FileWriterOptions::background_write` marks a file as the output of a
background task. `RowsetWriterContext::get_file_writer_options` sets it for
`TYPE_COMPACTION` and `TYPE_SCHEMA_CHANGE` (segment and inverted index files,
so vertical compaction and index output are covered too), and
`DeleteBitmapFileWriter` sets it for bitmaps stored without a load txn.
- A background `ObjStorageFileWriter` uploads its parts on
`CompactionS3FileUploadThreadPool` and runs its async close on
`CompactionNonBlockCloseThreadPool`. Both pools are needed: with only a
separate close pool, load parts still queue behind compaction parts; with only
a separate upload pool, compaction closes can still hold every close thread.
- `UploadBufferInflightLimiter` bounds the background parts in flight per BE
(`compaction_s3_upload_max_inflight_parts`, default 256). A writer takes a
permit right before it submits a part, and the part buffer holds it until the
buffer is destroyed. The bound therefore covers buffers that are queued, being
uploaded, and being copied into the file cache (with
`enable_flush_file_cache_async` the writer is notified before that copy ends,
but the buffer is still allocated). A writer at the bound waits before it
submits its next part. The permit is released exactly once on success, upload
failure, cancellation and a rejected submit, because all of them end with the
buffer being destroyed.
- Load writers are unchanged: same pools, no permits.
- Tools and unit tests that create only the load pools keep working: a
background writer uses the load pools when the compaction pools do not exist.
New BE configs:
| config | default | |
|---|---|---|
| `enable_separate_compaction_s3_upload_pool` | `true` (mutable) | read when
a writer is created |
| `compaction_s3_upload_thread_num` | 64 | max threads of
`CompactionS3FileUploadThreadPool` |
| `compaction_nonblock_close_thread_num` | 64 | max threads of
`CompactionNonBlockCloseThreadPool` |
| `compaction_s3_upload_max_inflight_parts` | 256 (mutable) | `<= 0` means
no bound; a raised value reaches waiting writers within a second |
Both new pools have `min_threads = 0`, so a BE that does no background
object storage writes keeps no extra threads.
New bvars: `s3_compaction_upload_inflight_parts`,
`s3_compaction_upload_waiting_writers`, `s3_compaction_upload_permit_wait_us`,
`s3_file_writer_compaction_async_close_queuing`,
`s3_file_writer_compaction_async_close_processing`.
`s3_file_writer_async_close_queuing/processing` now count load closes only.
#### What this does not do
- The bound counts submitted parts. The buffer a writer is still filling
(one per open background file) is outside it, as is every load buffer.
Load-side memory is #68970.
- The pools reserve threads. The S3 client, connections, bandwidth and
provider rate limits stay shared.
- Small files of a cloud schema change still go through the shared packed
file, which `PackedFileManager` uploads as a load write. Compaction already
sets `allow_packed_file = false`.
- The HDFS writer still uses the shared close pool.
#### Benchmark
A mock object store where every `PutObject` / `UploadPart` takes 100 ms and
`CompleteMultipartUpload` 20 ms, 5 MB parts, pools of 64 threads (the default
sizes). Compaction writers each write files of 16 parts in a loop; 16 load
writers each write a 1 MB file, close it, sleep 20 ms, in a loop. 10 s warm-up,
30 s measured, ASAN build, c7i.8xlarge. "Before" is the same binary with
`enable_separate_compaction_s3_upload_pool=false`, which is the behavior of
master (shared pools, no permits). Load close time is from the first append to
the end of `close()`. Each cell is two runs.
| compaction writers | | load close p50 | load close p99 | load files/s |
peak live part buffers | compaction files/s |
|---|---|---|---|---|---|---|
| 128 | before | 5019 / 5031 ms | 5238 / 5247 ms | 3.1 / 3.1 | 2021 / 2023
(10.1 GB) | 38.3 / 38.3 |
| 128 | after | 102 / 102 ms | 105 / 105 ms | 130.7 / 130.7 | 396 / 397 (2.0
GB) | 38.7 / 39.0 |
| 128 | after, no bound (`max_inflight_parts=0`) | 102 / 102 ms | 104 / 104
ms | 130.9 / 130.9 | 2020 / 2021 (10.1 GB) | 38.5 / 38.6 |
| 32 | before | 753 / 752 ms | 775 / 778 ms | 20.7 / 20.8 | 491 / 490 (2.5
GB) | 37.2 / 37.2 |
| 32 | after | 102 / 102 ms | 105 / 105 ms | 130.7 / 130.7 | 292 / 291 (1.5
GB) | 38.6 / 38.5 |
The pools remove the load latency (a load file costs one 100 ms PUT again);
the bound removes the memory (256 parts in flight + one filling buffer per open
file). Compaction throughput is the same: it is limited by 64 upload threads in
both cases.
This is a scheduling benchmark on a mock store, not a cluster measurement.
It shows the queueing effect in isolation; it says nothing about S3 throttling
or network limits.
<details>
<summary>Benchmark source (not part of the PR; appended to
obj_storage_file_writer_test.cpp)</summary>
```cpp
// Benchmark for apache/doris#68903. Not part of the PR: appended to
// be/test/io/fs/obj_storage_file_writer_test.cpp before the closing
namespace and run with
// --gtest_also_run_disabled_tests
--gtest_filter='*DISABLED_bench_load_close_under_compaction*'
//
// Mixed workload on a mock object store where every PutObject / UploadPart
takes
// BENCH_PART_MS and every CompleteMultipartUpload BENCH_COMPLETE_MS:
// - BENCH_COMPACTIONS threads, each writing BENCH_COMPACTION_PARTS full
parts per file
// (background_write), close(true) + close(), in a loop;
// - BENCH_LOADERS threads, each writing one BENCH_LOAD_BYTES file,
close(true) + close(),
// then sleeping BENCH_LOAD_GAP_MS, in a loop.
// Pools have the production default sizes (64 upload, 64 close, and 64 + 64
for compaction).
namespace io {
extern bvar::Adder<uint64_t> s3_file_buffer_allocated;
}
namespace {
int64_t bench_env(const char* name, int64_t def) {
const char* v = std::getenv(name);
return v == nullptr ? def : std::atoll(v);
}
class LatencyMockClient : public io::SimpleMockObjStorageClient {
public:
ObjStorageUploadResult create_multipart_upload(const ObjStoragePath&)
override {
return {.resp = ObjStorageResponse::OK(), .upload_id = "id", .etag =
"etag"};
}
ObjStorageResponse put_object(const ObjStoragePath&, std::string_view)
override {
std::this_thread::sleep_for(part_latency);
++parts;
return ObjStorageResponse::OK();
}
ObjStorageUploadResult upload_part(const ObjStoragePath&, const
std::string&, std::string_view,
int) override {
std::this_thread::sleep_for(part_latency);
++parts;
return {.resp = ObjStorageResponse::OK(), .upload_id = "id", .etag =
"etag"};
}
ObjStorageResponse complete_multipart_upload(
const ObjStoragePath&, const std::string&,
const std::vector<ObjStorageCompletedPart>&) override {
std::this_thread::sleep_for(complete_latency);
return ObjStorageResponse::OK();
}
std::chrono::milliseconds part_latency {100};
std::chrono::milliseconds complete_latency {20};
std::atomic<int64_t> parts {0};
};
} // namespace
TEST_F(S3FileWriterPoolTest, DISABLED_bench_load_close_under_compaction) {
const int64_t compactions = bench_env("BENCH_COMPACTIONS", 128);
const int64_t compaction_parts = bench_env("BENCH_COMPACTION_PARTS", 16);
const int64_t loaders = bench_env("BENCH_LOADERS", 16);
const int64_t load_bytes = bench_env("BENCH_LOAD_BYTES", 1 << 20);
const int64_t load_gap_ms = bench_env("BENCH_LOAD_GAP_MS", 20);
const int64_t seconds = bench_env("BENCH_SECONDS", 30);
const bool separate = bench_env("BENCH_SEPARATE", 1) != 0;
const int64_t max_inflight = bench_env("BENCH_MAX_INFLIGHT", 256);
config::enable_separate_compaction_s3_upload_pool = separate;
config::compaction_s3_upload_max_inflight_parts = max_inflight;
config::enable_s3_object_check_after_upload = false; // the mock keeps
no objects
auto* env = ExecEnv::GetInstance();
env->_obj_storage_upload_thread_pool = build_pool("BenchLoadUpload", 64);
env->_non_block_close_thread_pool = build_pool("BenchLoadClose", 64);
install_compaction_pools(64, 64);
auto client = std::make_shared<LatencyMockClient>();
client->part_latency =
std::chrono::milliseconds(bench_env("BENCH_PART_MS", 100));
client->complete_latency =
std::chrono::milliseconds(bench_env("BENCH_COMPLETE_MS", 20));
const std::string part(config::s3_write_buffer_size, 'b');
const std::string load_data(load_bytes, 'l');
std::atomic<bool> stop {false};
std::atomic<bool> measuring {false};
std::atomic<int64_t> compaction_files {0};
std::atomic<int64_t> errors {0};
std::atomic<int64_t> peak_buffers {0};
std::mutex latency_mutex;
std::vector<int64_t> load_close_us;
std::vector<std::thread> threads;
for (int64_t c = 0; c < compactions; ++c) {
threads.emplace_back([&, c] {
for (int64_t n = 0; !stop; ++n) {
auto writer = make_writer(fmt::format("bench_c_{}_{}.dat",
c, n), true, client);
bool good = true;
for (int64_t i = 0; i < compaction_parts; ++i) {
good &= writer->append(part).ok();
}
good &= writer->close(true).ok();
good &= writer->close().ok();
errors += !good;
compaction_files += measuring.load();
}
});
}
for (int64_t l = 0; l < loaders; ++l) {
threads.emplace_back([&, l] {
std::vector<int64_t> local;
for (int64_t n = 0; !stop; ++n) {
auto writer = make_writer(fmt::format("bench_l_{}_{}.dat",
l, n), false, client);
auto start = std::chrono::steady_clock::now();
bool good = writer->append(load_data).ok();
good &= writer->close(true).ok();
good &= writer->close().ok();
errors += !good;
if (measuring) {
local.push_back(std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now()
- start)
.count());
}
std::this_thread::sleep_for(std::chrono::milliseconds(load_gap_ms));
}
std::lock_guard lock(latency_mutex);
load_close_us.insert(load_close_us.end(), local.begin(),
local.end());
});
}
std::thread sampler([&] {
while (!stop) {
if (measuring) {
auto v =
static_cast<int64_t>(io::s3_file_buffer_allocated.get_value());
auto p = peak_buffers.load();
while (v > p && !peak_buffers.compare_exchange_weak(p, v)) {
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
});
// Warm up until the compaction backlog is in its steady state, then
measure.
std::this_thread::sleep_for(std::chrono::seconds(bench_env("BENCH_WARMUP_SECONDS",
10)));
auto parts_before = client->parts.load();
measuring = true;
std::this_thread::sleep_for(std::chrono::seconds(seconds));
measuring = false;
auto parts_after = client->parts.load();
stop = true;
for (auto& t : threads) {
t.join();
}
sampler.join();
std::sort(load_close_us.begin(), load_close_us.end());
auto pct = [&](double p) {
return load_close_us.empty()
? 0.0
:
static_cast<double>(load_close_us[static_cast<size_t>(
p *
static_cast<double>(load_close_us.size() - 1))]) /
1000.0;
};
double buffer_mb = static_cast<double>(config::s3_write_buffer_size) /
(1 << 20);
std::cout << fmt::format(
"BENCH separate={} max_inflight={} compactions={}
parts_per_file={} "
"loaders={} part_ms={} | load_files={}
load_files_per_s={:.1f} "
"load_close_ms p50={:.0f} p90={:.0f} p99={:.0f}
max={:.0f} | "
"peak_live_buffers={} peak_buffer_mb={:.0f} | "
"compaction_files_per_s={:.1f}
all_parts_per_s={:.0f} errors={}",
separate, max_inflight, compactions,
compaction_parts, loaders,
client->part_latency.count(), load_close_us.size(),
static_cast<double>(load_close_us.size()) /
static_cast<double>(seconds),
pct(0.5), pct(0.9), pct(0.99), pct(1.0),
peak_buffers.load(),
static_cast<double>(peak_buffers.load()) *
buffer_mb,
static_cast<double>(compaction_files.load()) /
static_cast<double>(seconds),
static_cast<double>(parts_after - parts_before) /
static_cast<double>(seconds),
errors.load())
<< std::endl;
EXPECT_EQ(errors.load(), 0);
}
```
</details>
### Release note
Compaction and schema change S3 writes use their own upload and close thread
pools and a bound on in-flight parts, so they no longer delay load writes. New
BE configs: `enable_separate_compaction_s3_upload_pool`,
`compaction_s3_upload_thread_num`, `compaction_nonblock_close_thread_num`,
`compaction_s3_upload_max_inflight_parts`.
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [ ] Regression test
- [x] Unit Test
- [x] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
New `S3FileWriterPoolTest` (7 cases), with load and compaction pools of 4
threads each so a test can hold every thread:
- multipart and small-file (PutObject) writes of load and background
writers run on the expected upload and close pools; with the feature off a
background writer is an ordinary writer;
- a background writer uses the load pools when the compaction pools are
absent;
- `RowsetWriterContext` marks compaction and schema change segment and
index files, not load files, also with
`enable_file_cache_write_index_file_only`;
- the limiter blocks at the limit, wakes on a release and on a raised
limit, and a moved permit is released once;
- 4 writers x 4 parts with a limit of 2 never have more than 2 parts in
flight;
- permits come back after an upload failure and after the pool rejects a
part (the writers submit more parts than there are permits);
- 8 background closes with hung uploads hold all 4 upload threads and all
4 close threads (4 more closes queued): a multipart load file still closes with
the compaction pools, and does not within 2 s with the pools shared.
`run-be-ut.sh --run --filter='S3FileWriterTest.*:S3FileWriterPoolTest.*'`
(ASAN): 22 tests passed. The existing `S3FileWriterTest` cases depend on a file
cache created by other suites, so this filtered run used `enable_file_cache =
false` in the local `be.conf`.
Manual test: the benchmark above.
- Behavior changed:
- [ ] No.
- [x] Yes. Compaction and schema change S3 writes run on two new thread
pools, and such a writer waits when 256 of its BE's background parts are in
flight. `enable_separate_compaction_s3_upload_pool=false` restores the old
behavior.
- Does this need documentation?
- [ ] No.
- [x] Yes. The new BE configs; a doc PR will follow.
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]