rocksdb/util
Jay Huh f22557886e Fix Compaction Stats (#13071)
Summary:
Compaction stats code is not so straightforward to understand. Here's a bit of context for this PR and why this change was made.

- **CompactionStats (compaction_stats_.stats):** Internal stats about the compaction used for logging and public metrics.
- **CompactionJobStats (compaction_job_stats_)**: The public stats at job level. It's part of Compaction event listener and included in the CompactionResult.
- **CompactionOutputsStats**: output stats only. resides in CompactionOutputs. It gets aggregated toward the CompactionStats (internal stats).

The internal stats, `compaction_stats_.stats`, has the output information recorded from the compaction iterator, but it does not have any input information (input records, input output files) until `UpdateCompactionStats()` gets called. We cannot simply call `UpdateCompactionStats()` to fill in the input information in the remote compaction (which is a subcompaction of the primary host's compaction) because the `compaction->inputs()` have the full list of input files and `UpdateCompactionStats()` takes the entire list of records in all files. `num_input_records` gets double-counted if multiple sub-compactions are submitted to the remote worker.

The job level stats (in the case of remote compaction, it's subcompaction level stat), `compaction_job_stats_`, has the correct input records, but has no output information. We can use `UpdateCompactionJobStats(compaction_stats_.stats)` to set the output information (num_output_records, num_output_files, etc.) from the `compaction_stats_.stats`, but it also sets all other fields including the input information which sets all back to 0.

Therefore, we are overriding `UpdateCompactionJobStats()` in remote worker only to update job level stats, `compaction_job_stats_`, with output information of the internal stats.

Baiscally, we are merging the aggregated output info from the internal stats and aggregated input info from the compaction job stats.

In this PR we are also fixing how we are setting `is_remote_compaction` in CompactionJobStats.
- OnCompactionBegin event, if options.compaction_service is set, `is_remote_compaction=true` for all compactions except for trivial moves
- OnCompactionCompleted event, if any of the sub_compactions were done remotely, compaction level stats's `is_remote_compaction` will be true

Other minor changes
- num_output_records is already available in CompactionJobStats. No need to store separately in CompactionResult.
- total_bytes is not needed.
- Renamed `SubcompactionState::AggregateCompactionStats()` to `SubcompactionState::AggregateCompactionOutputStats()` to make it clear that it's only aggregating output stats.
- Renamed `SetTotalBytes()` to `AddBytesWritten()` to make it more clear that it's adding total written bytes from the compaction output.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/13071

Test Plan:
Unit Tests added and updated
```
./compaction_service_test
```

Reviewed By: anand1976

Differential Revision: D64479657

Pulled By: jaykorean

fbshipit-source-id: a7a776a00dc718abae95d856b661bcbafd3b0ed5
2024-10-16 19:20:37 -07:00
..
aligned_buffer.h
aligned_storage.h
async_file_reader.cc
async_file_reader.h
atomic.h
autovector.h
autovector_test.cc
bloom_impl.h
bloom_test.cc
build_version.cc.in
cast_util.h
channel.h
cleanable.cc
coding.cc
coding.h
coding_lean.h
coding_test.cc
compaction_job_stats_impl.cc
comparator.cc
compression.cc
compression.h
compression_context_cache.cc
compression_context_cache.h
concurrent_task_limiter_impl.cc
concurrent_task_limiter_impl.h
core_local.h
coro_utils.h
crc32c.cc
crc32c.h
crc32c_arm64.cc
crc32c_arm64.h
crc32c_ppc.c
crc32c_ppc.h
crc32c_ppc_asm.S
crc32c_ppc_constants.h
crc32c_test.cc
data_structure.cc
defer.h
defer_test.cc
distributed_mutex.h
duplicate_detector.h
dynamic_bloom.cc
dynamic_bloom.h
dynamic_bloom_test.cc
fastrange.h
file_checksum_helper.cc
file_checksum_helper.h
file_reader_writer_test.cc
filelock_test.cc
filter_bench.cc
gflags_compat.h
hash.cc
hash.h
hash128.h
hash_containers.h
hash_map.h
hash_test.cc
heap.h
heap_test.cc
kv_map.h
log_write_bench.cc
math.h
math128.h
murmurhash.cc
murmurhash.h
mutexlock.h
overload.h
ppc-opcode.h
random.cc
random.h
random_test.cc
rate_limiter.cc
rate_limiter_impl.h
rate_limiter_test.cc
repeatable_thread.h
repeatable_thread_test.cc
ribbon_alg.h
ribbon_config.cc
ribbon_config.h
ribbon_impl.h
ribbon_test.cc
set_comparator.h
single_thread_executor.h
slice.cc
slice_test.cc
slice_transform_test.cc
status.cc
stderr_logger.cc
stderr_logger.h
stop_watch.h
string_util.cc
string_util.h
string_util_test.cc
thread_guard.h
thread_list_test.cc
thread_local.cc
thread_local.h
thread_local_test.cc
thread_operation.h
threadpool_imp.cc
threadpool_imp.h
timer.h
timer_queue.h
timer_queue_test.cc
timer_test.cc
udt_util.cc
udt_util.h
udt_util_test.cc
user_comparator_wrapper.h
vector_iterator.h
work_queue.h
work_queue_test.cc
write_batch_util.cc
write_batch_util.h
xxhash.cc
xxhash.h
xxph3.h