Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: client init state should not regress when data source interrupted #441

Merged
merged 5 commits into from
Oct 1, 2024
Merged
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
47 changes: 28 additions & 19 deletions libs/client-sdk/src/client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,28 @@
run_thread_ = std::move(std::thread([&]() { ioc_.run(); }));
}

// Was an attempt made to initialize the data source, and did that attempt
// succeed? The data source being connected, or not being connected due to
// offline mode, both represent successful terminal states.
// Returns true if the SDK can be considered initialized. We have defined
// explicit configuration of offline mode as initialized.
//
// When online, we're initialized if we've obtained a payload and are healthy
// (kValid) or obtained a payload and are unhealthy (kInterrupted).
//
// The purpose of this concept is to enable:
//
// (1) Resolving the StartAsync() promise. Once the SDK is no longer
// initializing, this promise needs to indicate if the process was successful
// or not.
//
// (2) Providing a getter for (1), if the user didn't check the promise or
// otherwise need to poll the state. That's the Initialized() method.
//
// (3) As a diagnostic during evaluation, to log a message warning that a
// cached (if persistence is being used) or default (if not) value will be
// returned because the SDK is not yet initialized.
static bool IsInitializedSuccessfully(DataSourceStatus::DataSourceState state) {
return (state == DataSourceStatus::DataSourceState::kValid ||
state == DataSourceStatus::DataSourceState::kSetOffline);
}

// Was any attempt made to initialize the data source (with a successful or
// permanent failure outcome?)
static bool IsInitialized(DataSourceStatus::DataSourceState state) {
return IsInitializedSuccessfully(state) ||
(state == DataSourceStatus::DataSourceState::kShutdown);
state == DataSourceStatus::DataSourceState::kSetOffline ||
state == DataSourceStatus::DataSourceState::kInterrupted);
}

std::future<bool> ClientImpl::IdentifyAsync(Context context) {
Expand All @@ -155,7 +164,7 @@
event_processor_->SendAsync(events::IdentifyEventParams{
std::chrono::system_clock::now(), std::move(context)});

return StartAsyncInternal(IsInitializedSuccessfully);
return StartAsyncInternal();
}

void ClientImpl::RestartDataSource() {
Expand All @@ -169,16 +178,16 @@
data_source_->ShutdownAsync(start_op);
}

std::future<bool> ClientImpl::StartAsyncInternal(
std::function<bool(DataSourceStatus::DataSourceState)> result_predicate) {
std::future<bool> ClientImpl::StartAsyncInternal() {
auto init_promise = std::make_shared<std::promise<bool>>();
auto init_future = init_promise->get_future();

status_manager_.OnDataSourceStatusChangeEx(
[result = std::move(result_predicate),
init_promise](data_sources::DataSourceStatus const& status) {
if (auto const state = status.State(); IsInitialized(state)) {
init_promise->set_value(result(status.State()));
[init_promise](data_sources::DataSourceStatus const& status) {
if (auto const state = status.State();
state != DataSourceStatus::DataSourceState::kInitializing) {
init_promise->set_value(
IsInitializedSuccessfully(status.State()));
return true; /* delete this change listener since the desired
state was reached */
}
Expand All @@ -191,7 +200,7 @@
}

std::future<bool> ClientImpl::StartAsync() {
return StartAsyncInternal(IsInitializedSuccessfully);
return StartAsyncInternal();
}

bool ClientImpl::Initialized() const {
Expand All @@ -202,7 +211,7 @@
std::unordered_map<Client::FlagKey, Value> result;
for (auto& [key, descriptor] : flag_manager_.Store().GetAll()) {
if (descriptor->item) {
result.try_emplace(key, descriptor->item->Detail().Value());

Check warning on line 214 in libs/client-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/src/client_impl.cpp:214:37 [bugprone-unchecked-optional-access]

unchecked access to optional value
}
}
return result;
Expand Down Expand Up @@ -237,7 +246,7 @@
}

template <typename T>
EvaluationDetail<T> ClientImpl::VariationInternal(FlagKey const& key,

Check warning on line 249 in libs/client-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/src/client_impl.cpp:249:51 [bugprone-easily-swappable-parameters]

2 adjacent parameters of 'VariationInternal' of convertible types are easily swapped by mistake
Value default_value,
bool check_type,
bool detailed) {
Expand Down Expand Up @@ -293,7 +302,7 @@

LD_ASSERT(desc->item);

auto const& flag = *(desc->item);

Check warning on line 305 in libs/client-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/src/client_impl.cpp:305:25 [bugprone-unchecked-optional-access]

unchecked access to optional value
auto const& detail = flag.Detail();

if (check_type && default_value.Type() != Value::Type::kNull &&
Expand Down
5 changes: 1 addition & 4 deletions libs/client-sdk/src/client_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace launchdarkly::client_side {
class ClientImpl : public IClient {
public:
ClientImpl(Config config, Context context, std::string const& version);

Check warning on line 31 in libs/client-sdk/src/client_impl.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/src/client_impl.hpp:31:5 [readability-inconsistent-declaration-parameter-name]

function 'launchdarkly::client_side::ClientImpl::ClientImpl' has a definition with different parameter names

ClientImpl(ClientImpl&&) = delete;
ClientImpl(ClientImpl const&) = delete;
Expand Down Expand Up @@ -84,7 +84,7 @@

flag_manager::IFlagNotifier& FlagNotifier() override;

~ClientImpl();

Check warning on line 87 in libs/client-sdk/src/client_impl.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/src/client_impl.hpp:87:5 [cppcoreguidelines-explicit-virtual-functions

annotate this function with 'override' or (rarely) 'final'

std::future<bool> StartAsync() override;

Expand All @@ -99,7 +99,7 @@
std::optional<double> metric_value);

template <typename F>
auto ReadContextSynchronized(F fn) const

Check warning on line 102 in libs/client-sdk/src/client_impl.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/src/client_impl.hpp:102:36 [readability-identifier-length]

parameter name 'fn' is too short, expected at least 3 characters
-> std::invoke_result_t<F, Context const&> {
std::shared_lock lock(context_mutex_);
return fn(context_);
Expand All @@ -109,10 +109,7 @@

void RestartDataSource();

std::future<bool> StartAsyncInternal(
std::function<bool(data_sources::DataSourceStatus::DataSourceState)>
predicate);

std::future<bool> StartAsyncInternal();
Config config_;
Logger logger_;

Expand Down
4 changes: 4 additions & 0 deletions libs/client-sdk/tests/client_c_bindings_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
TEST(ClientBindings, MinimalInstantiation) {
LDClientConfigBuilder cfg_builder = LDClientConfigBuilder_New("sdk-123");

LDClientConfig config;

Check warning on line 17 in libs/client-sdk/tests/client_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/tests/client_c_bindings_test.cpp:17:20 [cppcoreguidelines-init-variables]

variable 'config' is not initialized
LDStatus status = LDClientConfigBuilder_Build(cfg_builder, &config);
ASSERT_TRUE(LDStatus_Ok(status));

Expand All @@ -33,7 +33,7 @@
}

void FlagListenerFunction(char const* flag_key,
LDValue new_value,

Check warning on line 36 in libs/client-sdk/tests/client_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/client-sdk/tests/client_c_bindings_test.cpp:36:27 [bugprone-easily-swappable-parameters]

2 adjacent parameters of 'FlagListenerFunction' of similar type ('LDValue') are easily swapped by mistake
LDValue old_value,
bool deleted,
void* user_data) {}
Expand Down Expand Up @@ -148,6 +148,8 @@
bool success = false;
LDClientSDK_Start(sdk, 3000, &success);

EXPECT_TRUE(success);

LDDataSourceStatus status_2 = LDClientSDK_DataSourceStatus_Status(sdk);
EXPECT_EQ(LD_DATASOURCESTATUS_STATE_OFFLINE,
LDDataSourceStatus_GetState(status_2));
Expand All @@ -156,6 +158,8 @@

EXPECT_NE(0, LDDataSourceStatus_StateSince(status_2));

EXPECT_TRUE(LDClientSDK_Initialized(sdk));

LDDataSourceStatus_Free(status_1);
LDDataSourceStatus_Free(status_2);
LDClientSDK_Free(sdk);
Expand Down
Loading