Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <launchdarkly/async/timer.hpp>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/json.hpp>
#include <boost/url/parse.hpp>
#include <boost/url/url.hpp>
Expand Down Expand Up @@ -36,9 +37,9 @@

FDv2StreamingSynchronizer::State::State(
Logger logger,
boost::asio::any_io_executor const& executor,

Check warning on line 40 in libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp:40:5 [modernize-pass-by-value]

pass by value and use std::move
std::string streaming_base_url,
config::built::HttpProperties const& http_properties,

Check warning on line 42 in libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp:42:5 [modernize-pass-by-value]

pass by value and use std::move
std::optional<std::string> filter_key,
std::chrono::milliseconds initial_reconnect_delay)
: logger_(std::move(logger)),
Expand All @@ -50,7 +51,7 @@

void FDv2StreamingSynchronizer::State::EnsureStarted(
data_model::Selector const& selector,
std::shared_ptr<State> self) {

Check warning on line 54 in libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp:54:28 [performance-unnecessary-value-param]

the parameter 'self' is copied for each invocation but only used as a const reference; consider making it a const reference
{
std::lock_guard lock(mutex_);
latest_selector_ = selector;
Expand Down Expand Up @@ -134,10 +135,10 @@
});
client_builder.logger([weak](std::string msg) {
if (auto s = weak.lock()) {
LD_LOG(s->logger_, LogLevel::kDebug) << msg;

Check warning on line 138 in libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp:138:53 [performance-unnecessary-value-param]

parameter 'msg' is passed by value and only copied once; consider moving it to avoid unnecessary copies
}
});
client_builder.errors([weak](sse::Error error) {

Check warning on line 141 in libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp:141:45 [performance-unnecessary-value-param]

the parameter 'error' is copied for each invocation but only used as a const reference; consider making it a const reference
if (auto s = weak.lock()) {
s->OnError(error);
}
Expand Down Expand Up @@ -202,11 +203,28 @@
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<ErrorInfo::StatusCodeType>(headers.result_int()),
std::move(msg))}});
}
}

void FDv2StreamingSynchronizer::State::OnEvent(sse::Event const& event) {

Check warning on line 227 in libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/fdv2/streaming_synchronizer.cpp:227:40 [readability-function-cognitive-complexity]

function 'OnEvent' has cognitive complexity of 29 (threshold 25)
if (!FDv2ProtocolHandler::IsKnownEvent(event.type())) {
return;
}
Expand Down
50 changes: 50 additions & 0 deletions libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@

Logger MakeNullLogger() {
struct NullBackend : ILogBackend {
bool Enabled(LogLevel) noexcept override { return false; }

Check warning on line 81 in libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp:81:30 [readability-named-parameter]

all parameters should be named in a function
void Write(LogLevel, std::string) noexcept override {}

Check warning on line 82 in libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp:82:28 [readability-named-parameter]

all parameters should be named in a function
};
return Logger{std::make_shared<NullBackend>()};
}

class IoContextRunner {

Check warning on line 87 in libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp:87:7 [cppcoreguidelines-special-member-functions]

class 'IoContextRunner' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator
public:
IoContextRunner() : work_guard_(boost::asio::make_work_guard(ioc_)) {
thread_ = std::thread([this] { ioc_.run(); });
Expand Down Expand Up @@ -518,7 +518,7 @@
ASSERT_NE(cs, nullptr);

// Only the fresh put should be present; the abandoned put was discarded.
ASSERT_EQ(cs->change_set.data.size(), 1u);

Check warning on line 521 in libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/fdv2_streaming_synchronizer_test.cpp:521:43 [readability-uppercase-literal-suffix]

integer literal has suffix 'u', which is not uppercase
EXPECT_EQ(cs->change_set.data[0].key, "fresh");
}

Expand Down Expand Up @@ -825,6 +825,56 @@
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<FDv2SourceResult::Interrupted>(&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;
Expand Down
Loading