Skip to content

Commit 1d4bf6c

Browse files
committed
[BUG] Stop OTLP ForceFlush returning on the first notification and overrunning its deadline
OtlpGrpcClient::ForceFlush left its wait loop on any notification without re-reading the completion counter, so one finished request reported the whole flush as complete. The loop condition is the only place that counter is read, and the break skipped it. Drop the break, let the loop condition decide, and return that condition instead of the leftover duration. Both clients waited for the configured export timeout on every iteration, so ForceFlush could block well past the deadline it was given. Bound each wait by the caller's remaining time as well. OtlpHttpClient::Shutdown calls ForceFlush(1ms) in a loop, so shutdown cost a multiple of the export timeout. The wait stays bounded and the condition is re-read on every wakeup, which is what covers a notification lost between the check and the wait. ForceFlushReturnsWithinTheCallerDeadline holds a request open with the nosend HTTP client and asserts the call returns inside its own deadline rather than the client timeout. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 89729e1 commit 1d4bf6c

4 files changed

Lines changed: 90 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Increment the:
3333
[#4334](https://github.com/open-telemetry/opentelemetry-cpp/pull/4334)
3434
* [BUG] Stop reading past a `nostd::string_view` that is not NUL terminated
3535
[#4346](https://github.com/open-telemetry/opentelemetry-cpp/pull/4346)
36+
* [BUG] Stop OTLP `ForceFlush` returning on the first notification and
37+
overrunning the caller's deadline
38+
[#4357](https://github.com/open-telemetry/opentelemetry-cpp/pull/4357)
3639

3740
* [OTLP/HTTP] Honor `Retry-After` response header when retrying exports,
3841
supporting both delay-seconds and HTTP-date formats per RFC 7231 §7.1.3.

‎exporters/otlp/src/otlp_grpc_client.cc‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -757,20 +757,20 @@ bool OtlpGrpcClient::ForceFlush(
757757
while (timeout_steady > std::chrono::steady_clock::duration::zero() &&
758758
request_counter > async_data_->finished_request_counter.load(std::memory_order_acquire))
759759
{
760-
// When changes of running_sessions_ and notify_one/notify_all happen between predicate
761-
// checking and waiting, we should not wait forever.We should cleanup gc sessions here as soon
762-
// as possible to call FinishSession() and cleanup resources.
760+
// A completion can land between the loop condition and the wait, so the wait is bounded and the
761+
// condition re-read on every wakeup. Never wait past the caller's deadline.
762+
const std::chrono::steady_clock::duration wait_interval =
763+
(std::min)(std::chrono::duration_cast<std::chrono::steady_clock::duration>(
764+
async_data_->export_timeout),
765+
timeout_steady);
766+
763767
std::chrono::steady_clock::time_point start_timepoint = std::chrono::steady_clock::now();
764-
if (std::cv_status::timeout !=
765-
async_data_->session_waker.wait_for(lock, async_data_->export_timeout))
766-
{
767-
break;
768-
}
768+
async_data_->session_waker.wait_for(lock, wait_interval);
769769

770770
timeout_steady -= std::chrono::steady_clock::now() - start_timepoint;
771771
}
772772

773-
return timeout_steady > std::chrono::steady_clock::duration::zero();
773+
return request_counter <= async_data_->finished_request_counter.load(std::memory_order_acquire);
774774
#else
775775
return true;
776776
#endif

‎exporters/otlp/src/otlp_http_client.cc‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include <algorithm>
45
#include <atomic>
56
#include <chrono>
67
#include <condition_variable>
@@ -857,8 +858,12 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept
857858
// When changes of running_sessions_ and notify_one/notify_all happen between predicate
858859
// checking and waiting, we should not wait forever.We should cleanup gc sessions here as soon
859860
// as possible to call FinishSession() and cleanup resources.
861+
const std::chrono::steady_clock::duration wait_interval = (std::min)(
862+
std::chrono::duration_cast<std::chrono::steady_clock::duration>(options_.timeout),
863+
timeout_steady);
864+
860865
std::chrono::steady_clock::time_point start_timepoint = std::chrono::steady_clock::now();
861-
if (std::cv_status::timeout == session_waker_.wait_for(lock, options_.timeout))
866+
if (std::cv_status::timeout == session_waker_.wait_for(lock, wait_interval))
862867
{
863868
cleanupGCSessions();
864869
}
@@ -870,7 +875,17 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept
870875
timeout_steady -= std::chrono::steady_clock::now() - start_timepoint;
871876
}
872877

873-
return timeout_steady > std::chrono::steady_clock::duration::zero();
878+
// What the wait was for, rather than what is left of the deadline. A session that finishes
879+
// between the last check and the last wait takes its notification with it, and the flush it was
880+
// holding up has happened.
881+
{
882+
std::lock_guard<std::recursive_mutex> guard{session_manager_lock_};
883+
if (running_sessions_.empty())
884+
{
885+
return true;
886+
}
887+
}
888+
return finished_session_counter_.load(std::memory_order_acquire) >= wait_counter;
874889
}
875890

876891
bool OtlpHttpClient::Shutdown(std::chrono::microseconds timeout) noexcept

‎exporters/otlp/test/otlp_http_exporter_custom_client_test.cc‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@
44
#ifndef OPENTELEMETRY_STL_VERSION
55

66
# include <chrono>
7+
# include <memory>
8+
# include <utility>
79

810
# include "opentelemetry/exporters/otlp/otlp_http_client.h"
911
# include "opentelemetry/exporters/otlp/otlp_http_exporter.h"
1012
# include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
1113
# include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
1214
# include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h"
15+
16+
# include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
17+
18+
# include <google/protobuf/arena.h>
19+
# include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h"
20+
21+
# include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
22+
23+
# include "opentelemetry/ext/http/client/http_client.h"
1324
# include "opentelemetry/ext/http/client/http_client_factory.h"
25+
# include "opentelemetry/sdk/common/exporter_utils.h"
1426
# include "opentelemetry/sdk/trace/batch_span_processor.h"
1527
# include "opentelemetry/sdk/trace/batch_span_processor_options.h"
1628
# include "opentelemetry/sdk/trace/tracer_provider.h"
@@ -33,12 +45,13 @@ namespace http_client = opentelemetry::ext::http::client;
3345
using NosendHttpClientFactory =
3446
opentelemetry::test_common::ext::http::client::nosend::HttpClientFactoryNosend;
3547

36-
static OtlpHttpClientOptions MakeOtlpHttpClientOptions()
48+
static OtlpHttpClientOptions MakeOtlpHttpClientOptions(
49+
std::chrono::system_clock::duration timeout = std::chrono::system_clock::duration::zero())
3750
{
3851
std::shared_ptr<opentelemetry::sdk::common::ThreadInstrumentation> not_instrumented;
3952
OtlpHttpExporterOptions options;
4053
options.console_debug = true;
41-
options.timeout = std::chrono::system_clock::duration::zero();
54+
options.timeout = timeout;
4255
options.retry_policy_max_attempts = 0U;
4356
options.retry_policy_initial_backoff = std::chrono::duration<float>::zero();
4457
options.retry_policy_max_backoff = std::chrono::duration<float>::zero();
@@ -145,6 +158,52 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ExportCallsSendRequest)
145158
provider->ForceFlush();
146159
}
147160

161+
TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReturnsWithinTheCallerDeadline)
162+
{
163+
auto client = http_client::HttpClientTestFactory::Create();
164+
auto no_send_client = std::static_pointer_cast<http_client::nosend::HttpClient>(client);
165+
auto session = std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_);
166+
167+
// Hold the request open, so ForceFlush has something to wait for.
168+
std::shared_ptr<opentelemetry::ext::http::client::EventHandler> pending;
169+
EXPECT_CALL(*session, SendRequest)
170+
.WillRepeatedly(
171+
[&pending](std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) {
172+
pending = std::move(callback);
173+
});
174+
175+
// A client timeout far longer than the deadline ForceFlush is given below.
176+
OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client);
177+
178+
auto arena = std::make_unique<google::protobuf::Arena>();
179+
auto *request =
180+
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceRequest>(
181+
arena.get());
182+
auto *response =
183+
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceResponse>(
184+
arena.get());
185+
186+
// A non-zero request budget keeps the export asynchronous, so it returns while the session runs.
187+
otlp_client.Export(
188+
*request, std::move(arena), response,
189+
[](opentelemetry::sdk::common::ExportResult, google::protobuf::Message *) { return true; },
190+
1);
191+
ASSERT_NE(pending, nullptr);
192+
193+
const auto started = std::chrono::steady_clock::now();
194+
const bool flushed = otlp_client.ForceFlush(std::chrono::milliseconds{50});
195+
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
196+
std::chrono::steady_clock::now() - started)
197+
.count();
198+
199+
EXPECT_FALSE(flushed);
200+
EXPECT_LT(elapsed, 1000) << "waited on the client timeout rather than the one it was given";
201+
202+
// Complete the export, or the client destructor waits on it.
203+
http_client::nosend::Response sent;
204+
sent.Finish(*pending);
205+
}
206+
148207
} // namespace otlp
149208
} // namespace exporter
150209
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)