Skip to content

Commit f152c9b

Browse files
author
zhangyueqin
committed
[SDK] BatchSpanProcessor: wait for a full batch instead of draining partial batches
- Change the worker wait predicate from '!buffer_.empty()' to 'buffer_.size() >= max_export_batch_size_'. - Export() now only drains the entire buffer when a force flush is pending or the processor is shutting down; on normal wakeups it exports at most one batch of max_export_batch_size spans. This prevents the processor from waking up and draining partial trailing batches every time a span arrives, reducing CPU usage and gRPC request count while preserving ForceFlush/Shutdown drain semantics.
1 parent 57253cd commit f152c9b

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

‎sdk/src/trace/batch_span_processor.cc‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void BatchSpanProcessor::DoBackgroundWork()
194194
// Since `Export()` calls `NotifyCompletion()` which takes `force_flush_cv_m`,
195195
// holding `cv_m` while calling `Export()` can lead to a ABBA deadlock.
196196
{
197-
// Wait for `timeout` milliseconds.
197+
// Wait for `timeout` milliseconds, or until a full batch is available.
198198
std::unique_lock<std::mutex> lk(synchronization_data_->cv_m);
199199
synchronization_data_->cv.wait_for(lk, timeout, [this] {
200200
if (synchronization_data_->is_force_wakeup_background_worker.load(
@@ -203,7 +203,7 @@ void BatchSpanProcessor::DoBackgroundWork()
203203
return true;
204204
}
205205

206-
return !buffer_.empty();
206+
return buffer_.size() >= max_export_batch_size_;
207207
});
208208
synchronization_data_->is_force_wakeup_background_worker.store(false,
209209
std::memory_order_release);
@@ -248,13 +248,16 @@ void BatchSpanProcessor::Export()
248248
}
249249
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
250250

251+
std::uint64_t notify_force_flush =
252+
synchronization_data_->force_flush_pending_sequence.load(std::memory_order_acquire);
253+
bool should_drain = notify_force_flush != 0 ||
254+
synchronization_data_->is_shutdown.load(std::memory_order_acquire);
255+
251256
do
252257
{
253258
std::vector<std::unique_ptr<Recordable>> spans_arr;
254259
size_t num_records_to_export{};
255-
std::uint64_t notify_force_flush =
256-
synchronization_data_->force_flush_pending_sequence.load(std::memory_order_acquire);
257-
if (notify_force_flush)
260+
if (should_drain)
258261
{
259262
num_records_to_export = buffer_.size();
260263
}
@@ -285,7 +288,7 @@ void BatchSpanProcessor::Export()
285288

286289
exporter_->Export(nostd::span<std::unique_ptr<Recordable>>(spans_arr.data(), spans_arr.size()));
287290
NotifyCompletion(notify_force_flush, exporter_, synchronization_data_);
288-
} while (true);
291+
} while (should_drain);
289292

290293
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
291294
if (worker_thread_instrumentation_ != nullptr)

0 commit comments

Comments
 (0)