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

feat(event-processor): add C binding for context keys cache capacity #346

Merged
merged 1 commit into from
Dec 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ class EventsBuilder {
*/
EventsBuilder& PrivateAttribute(AttributeReference attribute);

/**
* @brief Specifies the number of unique context keys that can be remembered
* by the index event generation logic before needing to evict keys from
* memory in LRU order.
*
* After reaching capacity, it's possible
* that a previously-indexed context may cause generation of a redundant
* index event.
*
* @param capacity Maximum unique context keys to remember.
* @return Reference to this builder.
*/
EventsBuilder& ContextKeysCapacity(std::size_t capacity);

/**
* Builds Events configuration, if the configuration is valid.
* @return Events config, or error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class Events final {
[[nodiscard]] std::size_t FlushWorkers() const;

/**
* Max number of unique context keys to hold in LRU cache used for context
* deduplication when generating index events.
* Number of unique contexts to remember when deduplicating index
* events.
* @return Max, or std::nullopt if not applicable.
*/
[[nodiscard]] std::optional<std::size_t> ContextKeysCacheCapacity() const;
Expand Down
7 changes: 7 additions & 0 deletions libs/common/src/config/events_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ EventsBuilder<SDK>& EventsBuilder<SDK>::Capacity(std::size_t capacity) {
return *this;
}

template <typename SDK>
EventsBuilder<SDK>& EventsBuilder<SDK>::ContextKeysCapacity(
std::size_t capacity) {
config_.context_keys_cache_capacity_ = capacity;
return *this;
}

template <typename SDK>
EventsBuilder<SDK>& EventsBuilder<SDK>::FlushInterval(
std::chrono::milliseconds interval) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @file */

Check notice on line 1 in libs/server-sdk/include/launchdarkly/server_side/bindings/c/config/builder.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on libs/server-sdk/include/launchdarkly/server_side/bindings/c/config/builder.h

File libs/server-sdk/include/launchdarkly/server_side/bindings/c/config/builder.h does not conform to Custom style guidelines. (lines 119)
// NOLINTBEGIN modernize-use-using

#pragma once
Expand Down Expand Up @@ -107,6 +107,22 @@
LD_EXPORT(void)
LDServerConfigBuilder_Events_Enabled(LDServerConfigBuilder b, bool enabled);

/**
* Specifies the number of unique context keys that can be remembered
* by the index event generation logic before needing to evict keys from
* memory in LRU order.
*
* After reaching capacity, it's possible
* that a previously-indexed context may cause generation of a redundant
* index event.
* @param b Server config builder. Must not be NULL.
* @param context_keys_capacity Maximum unique context keys to remember. The default
* is 1000.
*/
LD_EXPORT(void)
LDServerConfigBuilder_Events_ContextKeysCapacity(LDServerConfigBuilder b,
size_t context_keys_capacity);

/**
* Sets the capacity of the event processor. When more events are generated
* within the processor's flush interval than this value, events will be
Expand Down
10 changes: 9 additions & 1 deletion libs/server-sdk/src/bindings/c/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ LDServerConfigBuilder_Events_Capacity(LDServerConfigBuilder b,
TO_BUILDER(b)->Events().Capacity(capacity);
}

LD_EXPORT(void)
LDServerConfigBuilder_Events_ContextKeysCapacity(LDServerConfigBuilder b,
size_t context_keys_capacity) {
LD_ASSERT_NOT_NULL(b);

TO_BUILDER(b)->Events().ContextKeysCapacity(context_keys_capacity);
}

LD_EXPORT(void)
LDServerConfigBuilder_Events_FlushIntervalMs(LDServerConfigBuilder b,
unsigned int milliseconds) {
Expand Down Expand Up @@ -225,7 +233,7 @@ LD_EXPORT(LDServerDataSourcePollBuilder) LDServerDataSourcePollBuilder_New() {

LD_EXPORT(void)
LDServerDataSourcePollBuilder_IntervalS(LDServerDataSourcePollBuilder b,
unsigned int seconds) {
unsigned int seconds) {
LD_ASSERT_NOT_NULL(b);

TO_POLL_BUILDER(b)->PollInterval(std::chrono::seconds{seconds});
Expand Down
27 changes: 27 additions & 0 deletions libs/server-sdk/tests/server_c_bindings_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
#include <boost/json/parse.hpp>

#include <chrono>
#include <launchdarkly/server_side/config/config.hpp>

TEST(ClientBindings, MinimalInstantiation) {
LDServerConfigBuilder cfg_builder = LDServerConfigBuilder_New("sdk-123");

LDServerConfig config;

Check warning on line 17 in libs/server-sdk/tests/server_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:17:20 [cppcoreguidelines-init-variables]

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

Expand All @@ -37,7 +38,7 @@
LDServerConfigBuilder cfg_builder = LDServerConfigBuilder_New("sdk-123");
LDServerConfigBuilder_Offline(cfg_builder, true);

LDServerConfig config;

Check warning on line 41 in libs/server-sdk/tests/server_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:41:20 [cppcoreguidelines-init-variables]

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

Expand All @@ -46,14 +47,14 @@
struct LDServerDataSourceStatusListener listener {};
LDServerDataSourceStatusListener_Init(&listener);

listener.UserData = const_cast<char*>("Potato");

Check warning on line 50 in libs/server-sdk/tests/server_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:50:25 [cppcoreguidelines-pro-type-const-cast]

do not use const_cast
listener.StatusChanged = StatusListenerFunction;

LDListenerConnection connection =
LDServerSDK_DataSourceStatus_OnStatusChange(sdk, listener);

bool success = false;
LDServerSDK_Start(sdk, 3000, &success);

Check warning on line 57 in libs/server-sdk/tests/server_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:57:28 [cppcoreguidelines-avoid-magic-numbers

3000 is a magic number; consider replacing it with a named constant
EXPECT_TRUE(success);

LDListenerConnection_Disconnect(connection);
Expand All @@ -66,7 +67,7 @@
LDServerConfigBuilder cfg_builder = LDServerConfigBuilder_New("sdk-123");
LDServerConfigBuilder_Offline(cfg_builder, true);

LDServerConfig config;

Check warning on line 70 in libs/server-sdk/tests/server_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:70:20 [cppcoreguidelines-init-variables]

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

Expand All @@ -78,7 +79,7 @@
LDServerDataSourceStatus_GetState(status_1));

bool success = false;
LDServerSDK_Start(sdk, 3000, &success);

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

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:82:28 [cppcoreguidelines-avoid-magic-numbers

3000 is a magic number; consider replacing it with a named constant

LDServerDataSourceStatus status_2 =
LDServerSDK_DataSourceStatus_Status(sdk);
Expand All @@ -100,9 +101,9 @@
DataSourceStatus status(
DataSourceStatus::DataSourceState::kValid,
std::chrono::time_point<std::chrono::system_clock>{
std::chrono::seconds{200}},

Check warning on line 104 in libs/server-sdk/tests/server_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:104:34 [cppcoreguidelines-avoid-magic-numbers

200 is a magic number; consider replacing it with a named constant
DataSourceStatus::ErrorInfo(
DataSourceStatus::ErrorInfo::ErrorKind::kErrorResponse, 404,

Check warning on line 106 in libs/server-sdk/tests/server_c_bindings_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/server-sdk/tests/server_c_bindings_test.cpp:106:69 [cppcoreguidelines-avoid-magic-numbers

404 is a magic number; consider replacing it with a named constant
"Not found",
std::chrono::time_point<std::chrono::system_clock>{
std::chrono::seconds{100}}));
Expand Down Expand Up @@ -186,3 +187,29 @@
LDServerSDK_Free(sdk);
LDContext_Free(context);
}

TEST(ClientBindings, CanSetEventConfigurationSuccessfully) {
LDServerConfigBuilder cfg_builder = LDServerConfigBuilder_New("sdk-123");

LDServerConfigBuilder_Events_Enabled(cfg_builder, false);
LDServerConfigBuilder_Events_Capacity(cfg_builder, 100);
LDServerConfigBuilder_Events_ContextKeysCapacity(cfg_builder, 100);
LDServerConfigBuilder_Events_PrivateAttribute(cfg_builder, "email");
LDServerConfigBuilder_Events_AllAttributesPrivate(cfg_builder, true);

LDServerConfig config;
LDStatus status = LDServerConfigBuilder_Build(cfg_builder, &config);
ASSERT_TRUE(LDStatus_Ok(status));

launchdarkly::server_side::Config const* c =
reinterpret_cast<launchdarkly::server_side::Config*>(config);

ASSERT_EQ(c->Events().Capacity(), 100);
ASSERT_EQ(c->Events().ContextKeysCacheCapacity(), 100);
ASSERT_EQ(c->Events().PrivateAttributes(),
launchdarkly::AttributeReference::SetType{"email"});
ASSERT_TRUE(c->Events().AllAttributesPrivate());
ASSERT_FALSE(c->Events().Enabled());

LDServerConfig_Free(config);
}
Loading