-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add IBigSegmentStore interface + Redis and DynamoDB stores #536
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
Open
beekld
wants to merge
10
commits into
main
Choose a base branch
from
beeklimt/SDK-2363
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fc17043
feat: add IBigSegmentStore interface + Redis and DynamoDB stores
beekld 2d92bc7
refactor: drop trailing colon from redis key namespaces; tighten test…
beekld a3dfc38
fix: inline Membership methods so dynamodb/redis dylibs can link
beekld 79af82e
fix: reject non-STRING_SET included/excluded in dynamodb big-segments
beekld 0082f6e
refactor: redis big-segments returns empty Membership instead of nullopt
beekld b99bf0c
refactor: drop std::optional from IBigSegmentStore::GetMembershipResult
beekld 44af937
fix: redis big-segments handle empty prefix and catch unexpected exce…
beekld 44c8a54
test: dynamodb big-segments empty-prefix smoke tests
beekld 8453937
refactor: cache redis big-segments key prefixes; drop unused prefix_ …
beekld b0ab11c
fix: dynamodb big-segments treats absent synchronizedOn as never-sync…
beekld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...rce/include/launchdarkly/server_side/integrations/dynamodb/dynamodb_big_segment_store.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** @file dynamodb_big_segment_store.hpp | ||
| * @brief Server-Side DynamoDB Big Segments Store | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <launchdarkly/server_side/integrations/big_segments/ibig_segment_store.hpp> | ||
| #include <launchdarkly/server_side/integrations/dynamodb/options.hpp> | ||
|
|
||
| #include <tl/expected.hpp> | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| namespace Aws::DynamoDB { | ||
| class DynamoDBClient; | ||
| } | ||
|
|
||
| namespace launchdarkly::server_side::integrations { | ||
|
|
||
| /** | ||
| * @brief DynamoDBBigSegmentStore is a Big Segments persistent store backed by | ||
| * Amazon DynamoDB. | ||
| * | ||
| * Call DynamoDBBigSegmentStore::Create to obtain a new instance, then pass it | ||
| * to the SDK via the Big Segments config builder. | ||
| * | ||
| * The DynamoDB table must already exist and follow the LaunchDarkly schema: | ||
| * a String partition key named `namespace` and a String sort key named `key`. | ||
| * The same table can be shared with @ref DynamoDBDataSource — Big Segments | ||
| * rows occupy their own partition-key values and do not conflict with | ||
| * flag/segment rows. The LaunchDarkly Relay Proxy is responsible for | ||
| * populating Big Segments data in this table; this class only reads from it. | ||
| * | ||
| * This implementation is backed by the AWS SDK for C++. | ||
| */ | ||
| class DynamoDBBigSegmentStore final : public IBigSegmentStore { | ||
| public: | ||
| /** | ||
| * @brief Creates a new DynamoDBBigSegmentStore, or returns an error if | ||
| * construction failed. | ||
| * | ||
| * @param table_name Name of the DynamoDB table to read from. The table | ||
| * must already exist; this class does not create it. | ||
| * | ||
| * @param prefix Optional namespace prefix. When non-empty, Big Segments | ||
| * rows live under partition keys `<prefix>:big_segments_user` and | ||
| * `<prefix>:big_segments_metadata`. This allows multiple LaunchDarkly | ||
| * environments to share a single table. | ||
| * | ||
| * @param options Optional AWS DynamoDB client configuration. See | ||
| * @ref DynamoDBClientOptions. When defaulted, the AWS SDK resolves | ||
| * region, endpoint, and credentials from the standard provider chain | ||
| * (environment variables, shared config files, instance metadata). | ||
| * | ||
| * @return A DynamoDBBigSegmentStore, or an error if construction failed. | ||
| */ | ||
| static tl::expected<std::unique_ptr<DynamoDBBigSegmentStore>, std::string> | ||
| Create(std::string table_name, | ||
| std::string prefix, | ||
| DynamoDBClientOptions options = {}); | ||
|
|
||
| [[nodiscard]] GetMembershipResult GetMembership( | ||
| std::string const& context_hash) const override; | ||
| [[nodiscard]] GetMetadataResult GetMetadata() const override; | ||
|
|
||
| ~DynamoDBBigSegmentStore() override; | ||
|
|
||
| private: | ||
| DynamoDBBigSegmentStore( | ||
| std::unique_ptr<Aws::DynamoDB::DynamoDBClient> client, | ||
| std::string table_name, | ||
| std::string prefix); | ||
|
|
||
| std::unique_ptr<Aws::DynamoDB::DynamoDBClient> client_; | ||
| std::string const table_name_; | ||
| std::string const prefix_; | ||
| std::string const user_namespace_; | ||
| std::string const metadata_namespace_; | ||
| }; | ||
|
|
||
| } // namespace launchdarkly::server_side::integrations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
libs/server-sdk-dynamodb-source/src/dynamodb_big_segment_store.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| #include <launchdarkly/server_side/integrations/dynamodb/dynamodb_big_segment_store.hpp> | ||
|
|
||
| #include "aws_sdk_guard.hpp" | ||
| #include "client_factory.hpp" | ||
| #include "dynamodb_attributes.hpp" | ||
| #include "prefix.hpp" | ||
|
|
||
| #include <aws/core/utils/Outcome.h> | ||
| #include <aws/dynamodb/DynamoDBClient.h> | ||
| #include <aws/dynamodb/model/AttributeValue.h> | ||
| #include <aws/dynamodb/model/GetItemRequest.h> | ||
|
|
||
| #include <cerrno> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <exception> | ||
| #include <utility> | ||
|
|
||
| namespace launchdarkly::server_side::integrations { | ||
|
|
||
| namespace { | ||
|
|
||
| using detail::kBigSegmentsExcludedAttribute; | ||
| using detail::kBigSegmentsIncludedAttribute; | ||
| using detail::kBigSegmentsMetadataNamespace; | ||
| using detail::kBigSegmentsSyncTimeAttribute; | ||
| using detail::kBigSegmentsUserNamespace; | ||
| using detail::kPartitionKey; | ||
| using detail::kSortKey; | ||
| using detail::PrefixedNamespace; | ||
|
|
||
| } // namespace | ||
|
|
||
| tl::expected<std::unique_ptr<DynamoDBBigSegmentStore>, std::string> | ||
| DynamoDBBigSegmentStore::Create(std::string table_name, | ||
| std::string prefix, | ||
| DynamoDBClientOptions options) { | ||
| try { | ||
| detail::AwsSdkGuard::Ensure(); | ||
| auto maybe_client = detail::BuildDynamoDBClient(options); | ||
| if (!maybe_client) { | ||
| return tl::make_unexpected(std::move(maybe_client.error())); | ||
| } | ||
| return std::unique_ptr<DynamoDBBigSegmentStore>( | ||
| new DynamoDBBigSegmentStore(std::move(*maybe_client), | ||
| std::move(table_name), | ||
| std::move(prefix))); | ||
| } catch (std::exception const& e) { | ||
| return tl::make_unexpected(e.what()); | ||
| } | ||
| } | ||
|
|
||
| DynamoDBBigSegmentStore::DynamoDBBigSegmentStore( | ||
| std::unique_ptr<Aws::DynamoDB::DynamoDBClient> client, | ||
| std::string table_name, | ||
| std::string prefix) | ||
| : client_(std::move(client)), | ||
| table_name_(std::move(table_name)), | ||
| prefix_(std::move(prefix)), | ||
| user_namespace_(PrefixedNamespace(prefix_, kBigSegmentsUserNamespace)), | ||
| metadata_namespace_( | ||
| PrefixedNamespace(prefix_, kBigSegmentsMetadataNamespace)) {} | ||
|
|
||
| DynamoDBBigSegmentStore::~DynamoDBBigSegmentStore() = default; | ||
|
|
||
| IBigSegmentStore::GetMembershipResult DynamoDBBigSegmentStore::GetMembership( | ||
| std::string const& context_hash) const { | ||
| Aws::DynamoDB::Model::GetItemRequest request; | ||
| request.SetTableName(table_name_); | ||
| request.SetConsistentRead(true); | ||
| request.AddKey(kPartitionKey, | ||
| Aws::DynamoDB::Model::AttributeValue{user_namespace_}); | ||
| request.AddKey(kSortKey, | ||
| Aws::DynamoDB::Model::AttributeValue{context_hash}); | ||
|
|
||
| auto outcome = client_->GetItem(request); | ||
| if (!outcome.IsSuccess()) { | ||
| return tl::make_unexpected(outcome.GetError().GetMessage()); | ||
| } | ||
|
|
||
| auto const& item = outcome.GetResult().GetItem(); | ||
|
|
||
| std::vector<std::string> included; | ||
| std::vector<std::string> excluded; | ||
|
|
||
| // GetSS() silently returns an empty vector if the attribute is not | ||
| // actually a String Set, so check the type explicitly before reading. | ||
| if (auto const it = item.find(kBigSegmentsIncludedAttribute); | ||
| it != item.end()) { | ||
| if (it->second.GetType() != | ||
| Aws::DynamoDB::Model::ValueType::STRING_SET) { | ||
| return tl::make_unexpected( | ||
| std::string("DynamoDB Big Segments '") + | ||
| kBigSegmentsIncludedAttribute + | ||
| "' is not of type STRING_SET"); | ||
| } | ||
| for (auto const& ref : it->second.GetSS()) { | ||
| included.emplace_back(ref); | ||
| } | ||
| } | ||
| if (auto const it = item.find(kBigSegmentsExcludedAttribute); | ||
| it != item.end()) { | ||
| if (it->second.GetType() != | ||
| Aws::DynamoDB::Model::ValueType::STRING_SET) { | ||
| return tl::make_unexpected( | ||
| std::string("DynamoDB Big Segments '") + | ||
| kBigSegmentsExcludedAttribute + | ||
| "' is not of type STRING_SET"); | ||
| } | ||
| for (auto const& ref : it->second.GetSS()) { | ||
| excluded.emplace_back(ref); | ||
| } | ||
| } | ||
|
|
||
| return Membership::FromSegmentRefs(included, excluded); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| IBigSegmentStore::GetMetadataResult DynamoDBBigSegmentStore::GetMetadata() | ||
| const { | ||
| Aws::DynamoDB::Model::GetItemRequest request; | ||
| request.SetTableName(table_name_); | ||
| request.SetConsistentRead(true); | ||
| request.AddKey(kPartitionKey, | ||
| Aws::DynamoDB::Model::AttributeValue{metadata_namespace_}); | ||
| request.AddKey(kSortKey, | ||
| Aws::DynamoDB::Model::AttributeValue{metadata_namespace_}); | ||
|
|
||
| auto outcome = client_->GetItem(request); | ||
| if (!outcome.IsSuccess()) { | ||
| return tl::make_unexpected(outcome.GetError().GetMessage()); | ||
| } | ||
|
|
||
| auto const& item = outcome.GetResult().GetItem(); | ||
| if (item.empty()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| auto const it = item.find(kBigSegmentsSyncTimeAttribute); | ||
| if (it == item.end()) { | ||
| // "absent" sync time is treated as never synchronized rather than | ||
| // an error; the wrapper marks the store stale based on the | ||
| // resulting nullopt. | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| auto const& raw = it->second.GetN(); | ||
| if (raw.empty()) { | ||
| return tl::make_unexpected( | ||
| "DynamoDB Big Segments 'synchronizedOn' is empty or not type N"); | ||
| } | ||
|
|
||
| errno = 0; | ||
| char* end = nullptr; | ||
| long long const parsed = std::strtoll(raw.c_str(), &end, 10); | ||
| if (errno != 0 || end == raw.c_str() || *end != '\0') { | ||
| return tl::make_unexpected( | ||
| "DynamoDB Big Segments 'synchronizedOn' is not a valid integer"); | ||
| } | ||
|
|
||
| return StoreMetadata{std::chrono::milliseconds{parsed}}; | ||
| } | ||
|
|
||
| } // namespace launchdarkly::server_side::integrations | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.