-
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
3
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
3 commits
Select commit
Hold shift + click to select a range
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
148 changes: 148 additions & 0 deletions
148
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,148 @@ | ||
| #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(); | ||
| if (item.empty()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| std::vector<std::string> included; | ||
| std::vector<std::string> excluded; | ||
|
|
||
| if (auto const it = item.find(kBigSegmentsIncludedAttribute); | ||
| it != item.end()) { | ||
| for (auto const& ref : it->second.GetSS()) { | ||
| included.emplace_back(ref); | ||
| } | ||
| } | ||
| if (auto const it = item.find(kBigSegmentsExcludedAttribute); | ||
| it != item.end()) { | ||
| for (auto const& ref : it->second.GetSS()) { | ||
| excluded.emplace_back(ref); | ||
| } | ||
| } | ||
|
|
||
| return Membership::FromSegmentRefs(included, excluded); | ||
| } | ||
|
|
||
| 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()) { | ||
| return tl::make_unexpected( | ||
| "DynamoDB Big Segments metadata row missing 'synchronizedOn'"); | ||
| } | ||
|
|
||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unvalidated
GetSS()silently drops membership data on type mismatchMedium Severity
The
GetSS()calls on theincludedandexcludedattributes are not validated for type correctness. AWS SDK'sGetSS()silently returns an empty vector when the attribute is a non-SS type (S, N, BOOL, etc.), just asGetS()returns""for non-String types. Since DynamoDB enforces that SS attributes must have at least one element, an empty result fromGetSS()when the attribute key exists in the item indicates a type mismatch. TheGetN()call forsynchronizedOnat line 131 correctly validates for empty, but theseGetSS()calls do not, causing silent membership data loss instead of surfacing an error.Triggered by learned rule: DynamoDB source: validate AttributeValue type before using GetS()
Reviewed by Cursor Bugbot for commit a3dfc38. Configure here.