diff --git a/contract-tests/server-contract-tests/test-suppressions-fdv2.txt b/contract-tests/server-contract-tests/test-suppressions-fdv2.txt index a56dbe377..32e7a8838 100644 --- a/contract-tests/server-contract-tests/test-suppressions-fdv2.txt +++ b/contract-tests/server-contract-tests/test-suppressions-fdv2.txt @@ -1,6 +1,3 @@ -# The SDK still retries streaming after HTTP 500 with an FDv1 directive and no fallback. -streaming/fdv2/FDv1 fallback directive/directive without FDv1 fallback configured halts the data system - # FDv2 public payload filter API is not exposed by the C++ SDK. streaming/requests/URL path is computed correctly/environment_filter_key="encoding_not_necessary"/base URI has no trailing slash/GET streaming/requests/URL path is computed correctly/environment_filter_key="encoding_not_necessary"/base URI has a trailing slash/GET diff --git a/libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp b/libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp index a8a6c4d00..7afc63d13 100644 --- a/libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp +++ b/libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -202,8 +203,25 @@ void FDv2StreamingSynchronizer::State::OnResponse( directive.ttl = *ttl; } } - std::lock_guard lock(mutex_); - latest_fdv1_fallback_ = directive; + { + std::lock_guard lock(mutex_); + latest_fdv1_fallback_ = directive; + } + + // The SSE client retries a server error response and does not report it. + // Report a server error response that has the directive now, so that the + // data system can engage the FDv1 fallback. + if (boost::beast::http::to_status_class(headers.result()) == + boost::beast::http::status_class::server_error) { + std::string msg = "HTTP status " + + std::to_string(headers.result_int()) + + " with an FDv1 fallback directive"; + LD_LOG(logger_, LogLevel::kWarn) << kIdentity << ": " << msg; + Notify(FDv2SourceResult{FDv2SourceResult::Interrupted{MakeError( + ErrorKind::kErrorResponse, + static_cast(headers.result_int()), + std::move(msg))}}); + } } void FDv2StreamingSynchronizer::State::OnEvent(sse::Event const& event) { diff --git a/libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp b/libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp index 7fd28185d..fd06c785e 100644 --- a/libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp +++ b/libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp @@ -825,6 +825,56 @@ TEST(FDv2StreamingSynchronizerTest, ErrorAfterDirectiveCarriesFlag) { EXPECT_TRUE(result->fdv1_fallback); } +TEST(FDv2StreamingSynchronizerTest, + ServerErrorWithDirectiveReturnsInterrupted) { + auto logger = MakeNullLogger(); + IoContextRunner runner; + + FDv2StreamingSynchronizer synchronizer( + runner.context().get_executor(), logger, "http://localhost", + MakeHttpProperties(), std::nullopt, 1s); + FDv2StreamingSynchronizerTestPeer::MarkStarted(synchronizer); + + // The server sends an error response with the directive. The SSE client + // retries the request and does not report an error. + boost::beast::http::response_header<> headers; + headers.result(503); + headers.set("X-LD-FD-Fallback", "true"); + FDv2StreamingSynchronizerTestPeer::OnResponse(synchronizer, headers); + + auto result = synchronizer.Next(data_model::Selector{}).WaitForResult(2s); + + // The response gives a result that has the directive. + ASSERT_TRUE(result.has_value()); + auto* interrupted = + std::get_if(&result->value); + ASSERT_NE(interrupted, nullptr); + EXPECT_EQ(interrupted->error.Kind(), + FDv2SourceResult::ErrorInfo::ErrorKind::kErrorResponse); + EXPECT_EQ(interrupted->error.StatusCode(), 503u); + EXPECT_TRUE(result->fdv1_fallback); +} + +TEST(FDv2StreamingSynchronizerTest, ServerErrorWithoutDirectiveGivesNoResult) { + auto logger = MakeNullLogger(); + IoContextRunner runner; + + FDv2StreamingSynchronizer synchronizer( + runner.context().get_executor(), logger, "http://localhost", + MakeHttpProperties(), std::nullopt, 1s); + FDv2StreamingSynchronizerTestPeer::MarkStarted(synchronizer); + + // The server sends an error response without the directive. + boost::beast::http::response_header<> headers; + headers.result(503); + FDv2StreamingSynchronizerTestPeer::OnResponse(synchronizer, headers); + + auto future = synchronizer.Next(data_model::Selector{}); + + // The SSE client retries the request, so the synchronizer has no result. + EXPECT_FALSE(future.IsFinished()); +} + TEST(FDv2StreamingSynchronizerTest, DirectiveWithTtlHeaderParsesValue) { auto logger = MakeNullLogger(); IoContextRunner runner;