|
| 1 | +#include <launchdarkly/server_side/integrations/dynamodb/dynamodb_big_segment_store.hpp> |
| 2 | + |
| 3 | +#include "aws_sdk_guard.hpp" |
| 4 | +#include "client_factory.hpp" |
| 5 | +#include "dynamodb_attributes.hpp" |
| 6 | +#include "prefix.hpp" |
| 7 | + |
| 8 | +#include <aws/core/utils/Outcome.h> |
| 9 | +#include <aws/dynamodb/DynamoDBClient.h> |
| 10 | +#include <aws/dynamodb/model/AttributeValue.h> |
| 11 | +#include <aws/dynamodb/model/GetItemRequest.h> |
| 12 | + |
| 13 | +#include <cerrno> |
| 14 | +#include <cstdint> |
| 15 | +#include <cstdlib> |
| 16 | +#include <exception> |
| 17 | +#include <utility> |
| 18 | + |
| 19 | +namespace launchdarkly::server_side::integrations { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +using detail::kBigSegmentsExcludedAttribute; |
| 24 | +using detail::kBigSegmentsIncludedAttribute; |
| 25 | +using detail::kBigSegmentsMetadataNamespace; |
| 26 | +using detail::kBigSegmentsSyncTimeAttribute; |
| 27 | +using detail::kBigSegmentsUserNamespace; |
| 28 | +using detail::kPartitionKey; |
| 29 | +using detail::kSortKey; |
| 30 | +using detail::PrefixedNamespace; |
| 31 | + |
| 32 | +} // namespace |
| 33 | + |
| 34 | +tl::expected<std::unique_ptr<DynamoDBBigSegmentStore>, std::string> |
| 35 | +DynamoDBBigSegmentStore::Create(std::string table_name, |
| 36 | + std::string prefix, |
| 37 | + DynamoDBClientOptions options) { |
| 38 | + try { |
| 39 | + detail::AwsSdkGuard::Ensure(); |
| 40 | + auto maybe_client = detail::BuildDynamoDBClient(options); |
| 41 | + if (!maybe_client) { |
| 42 | + return tl::make_unexpected(std::move(maybe_client.error())); |
| 43 | + } |
| 44 | + return std::unique_ptr<DynamoDBBigSegmentStore>( |
| 45 | + new DynamoDBBigSegmentStore(std::move(*maybe_client), |
| 46 | + std::move(table_name), |
| 47 | + std::move(prefix))); |
| 48 | + } catch (std::exception const& e) { |
| 49 | + return tl::make_unexpected(e.what()); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +DynamoDBBigSegmentStore::DynamoDBBigSegmentStore( |
| 54 | + std::unique_ptr<Aws::DynamoDB::DynamoDBClient> client, |
| 55 | + std::string table_name, |
| 56 | + std::string prefix) |
| 57 | + : client_(std::move(client)), |
| 58 | + table_name_(std::move(table_name)), |
| 59 | + prefix_(std::move(prefix)), |
| 60 | + user_namespace_(PrefixedNamespace(prefix_, kBigSegmentsUserNamespace)), |
| 61 | + metadata_namespace_( |
| 62 | + PrefixedNamespace(prefix_, kBigSegmentsMetadataNamespace)) {} |
| 63 | + |
| 64 | +DynamoDBBigSegmentStore::~DynamoDBBigSegmentStore() = default; |
| 65 | + |
| 66 | +IBigSegmentStore::GetMembershipResult DynamoDBBigSegmentStore::GetMembership( |
| 67 | + std::string const& context_hash) const { |
| 68 | + Aws::DynamoDB::Model::GetItemRequest request; |
| 69 | + request.SetTableName(table_name_); |
| 70 | + request.SetConsistentRead(true); |
| 71 | + request.AddKey(kPartitionKey, |
| 72 | + Aws::DynamoDB::Model::AttributeValue{user_namespace_}); |
| 73 | + request.AddKey(kSortKey, |
| 74 | + Aws::DynamoDB::Model::AttributeValue{context_hash}); |
| 75 | + |
| 76 | + auto outcome = client_->GetItem(request); |
| 77 | + if (!outcome.IsSuccess()) { |
| 78 | + return tl::make_unexpected(outcome.GetError().GetMessage()); |
| 79 | + } |
| 80 | + |
| 81 | + auto const& item = outcome.GetResult().GetItem(); |
| 82 | + if (item.empty()) { |
| 83 | + return std::nullopt; |
| 84 | + } |
| 85 | + |
| 86 | + std::vector<std::string> included; |
| 87 | + std::vector<std::string> excluded; |
| 88 | + |
| 89 | + if (auto const it = item.find(kBigSegmentsIncludedAttribute); |
| 90 | + it != item.end()) { |
| 91 | + for (auto const& ref : it->second.GetSS()) { |
| 92 | + included.emplace_back(ref); |
| 93 | + } |
| 94 | + } |
| 95 | + if (auto const it = item.find(kBigSegmentsExcludedAttribute); |
| 96 | + it != item.end()) { |
| 97 | + for (auto const& ref : it->second.GetSS()) { |
| 98 | + excluded.emplace_back(ref); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return Membership::FromSegmentRefs(included, excluded); |
| 103 | +} |
| 104 | + |
| 105 | +IBigSegmentStore::GetMetadataResult DynamoDBBigSegmentStore::GetMetadata() |
| 106 | + const { |
| 107 | + Aws::DynamoDB::Model::GetItemRequest request; |
| 108 | + request.SetTableName(table_name_); |
| 109 | + request.SetConsistentRead(true); |
| 110 | + request.AddKey(kPartitionKey, |
| 111 | + Aws::DynamoDB::Model::AttributeValue{metadata_namespace_}); |
| 112 | + request.AddKey(kSortKey, |
| 113 | + Aws::DynamoDB::Model::AttributeValue{metadata_namespace_}); |
| 114 | + |
| 115 | + auto outcome = client_->GetItem(request); |
| 116 | + if (!outcome.IsSuccess()) { |
| 117 | + return tl::make_unexpected(outcome.GetError().GetMessage()); |
| 118 | + } |
| 119 | + |
| 120 | + auto const& item = outcome.GetResult().GetItem(); |
| 121 | + if (item.empty()) { |
| 122 | + return std::nullopt; |
| 123 | + } |
| 124 | + |
| 125 | + auto const it = item.find(kBigSegmentsSyncTimeAttribute); |
| 126 | + if (it == item.end()) { |
| 127 | + return tl::make_unexpected( |
| 128 | + "DynamoDB Big Segments metadata row missing 'synchronizedOn'"); |
| 129 | + } |
| 130 | + |
| 131 | + auto const& raw = it->second.GetN(); |
| 132 | + if (raw.empty()) { |
| 133 | + return tl::make_unexpected( |
| 134 | + "DynamoDB Big Segments 'synchronizedOn' is empty or not type N"); |
| 135 | + } |
| 136 | + |
| 137 | + errno = 0; |
| 138 | + char* end = nullptr; |
| 139 | + long long const parsed = std::strtoll(raw.c_str(), &end, 10); |
| 140 | + if (errno != 0 || end == raw.c_str() || *end != '\0') { |
| 141 | + return tl::make_unexpected( |
| 142 | + "DynamoDB Big Segments 'synchronizedOn' is not a valid integer"); |
| 143 | + } |
| 144 | + |
| 145 | + return StoreMetadata{std::chrono::milliseconds{parsed}}; |
| 146 | +} |
| 147 | + |
| 148 | +} // namespace launchdarkly::server_side::integrations |
0 commit comments