|
2 | 2 | #include "bucketing.hpp" |
3 | 3 | #include "operators.hpp" |
4 | 4 |
|
| 5 | +#include "../data_components/big_segments/big_segment_store_wrapper.hpp" |
| 6 | + |
| 7 | +#include <optional> |
| 8 | +#include <string> |
| 9 | +#include <utility> |
| 10 | + |
5 | 11 | namespace launchdarkly::server_side::evaluation { |
6 | 12 |
|
7 | 13 | using namespace data_model; |
8 | 14 |
|
| 15 | +namespace { |
| 16 | + |
| 17 | +// Maps the wrapper's internal status to the public reason status. |
| 18 | +enum EvaluationReason::BigSegmentsStatus ToBigSegmentsStatus( |
| 19 | + data_components::BigSegmentsStatus status) { |
| 20 | + switch (status) { |
| 21 | + case data_components::BigSegmentsStatus::kHealthy: |
| 22 | + return EvaluationReason::BigSegmentsStatus::kHealthy; |
| 23 | + case data_components::BigSegmentsStatus::kStale: |
| 24 | + return EvaluationReason::BigSegmentsStatus::kStale; |
| 25 | + case data_components::BigSegmentsStatus::kStoreError: |
| 26 | + return EvaluationReason::BigSegmentsStatus::kStoreError; |
| 27 | + case data_components::BigSegmentsStatus::kNotConfigured: |
| 28 | + return EvaluationReason::BigSegmentsStatus::kNotConfigured; |
| 29 | + } |
| 30 | + return EvaluationReason::BigSegmentsStatus::kHealthy; |
| 31 | +} |
| 32 | + |
| 33 | +std::string MakeBigSegmentRef(Segment const& segment) { |
| 34 | + return segment.key + ".g" + std::to_string(*segment.generation); |
| 35 | +} |
| 36 | + |
| 37 | +// Evaluates membership in an unbounded (Big) segment. Returns true/false for a |
| 38 | +// definite match/non-match, or std::nullopt when the membership has no entry |
| 39 | +// for this segment and evaluation should fall through to the segment's rules. |
| 40 | +std::optional<bool> MatchBigSegment(Segment const& segment, |
| 41 | + Context const& context, |
| 42 | + EvaluationStack& stack) { |
| 43 | + if (!segment.generation) { |
| 44 | + // Without a generation the segment ref can't be formed. |
| 45 | + stack.RecordBigSegmentsStatus( |
| 46 | + EvaluationReason::BigSegmentsStatus::kNotConfigured); |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + // An absent or empty unboundedContextKind defaults to "user". |
| 51 | + ContextKind const kind = (segment.unboundedContextKind && |
| 52 | + !segment.unboundedContextKind->t.empty()) |
| 53 | + ? *segment.unboundedContextKind |
| 54 | + : ContextKind{"user"}; |
| 55 | + Value const& context_key = context.Get(kind, "key"); |
| 56 | + if (!context_key.IsString()) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + std::string const& key = context_key.AsString(); |
| 60 | + |
| 61 | + if (stack.DidStoreError(key)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + integrations::Membership const* membership = stack.FindMembership(key); |
| 66 | + if (!membership) { |
| 67 | + auto* store = stack.BigSegmentStore(); |
| 68 | + if (!store) { |
| 69 | + stack.RecordBigSegmentsStatus( |
| 70 | + EvaluationReason::BigSegmentsStatus::kNotConfigured); |
| 71 | + return false; |
| 72 | + } |
| 73 | + auto result = store->GetMembership(key); |
| 74 | + auto const status = ToBigSegmentsStatus(result.status); |
| 75 | + stack.RecordBigSegmentsStatus(status); |
| 76 | + if (status == EvaluationReason::BigSegmentsStatus::kStoreError) { |
| 77 | + stack.RecordStoreError(key); |
| 78 | + return false; |
| 79 | + } |
| 80 | + stack.StoreMembership(key, std::move(result.membership)); |
| 81 | + membership = stack.FindMembership(key); |
| 82 | + } |
| 83 | + |
| 84 | + return membership->CheckMembership(MakeBigSegmentRef(segment)); |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace |
| 88 | + |
9 | 89 | bool MaybeNegate(Clause const& clause, bool value) { |
10 | 90 | if (clause.negate) { |
11 | 91 | return !value; |
@@ -161,16 +241,19 @@ tl::expected<bool, Error> Contains(Segment const& segment, |
161 | 241 | } |
162 | 242 |
|
163 | 243 | if (segment.unbounded) { |
164 | | - // TODO(sc209881): set big segment status to NOT_CONFIGURED. |
165 | | - return false; |
166 | | - } |
167 | | - |
168 | | - if (IsTargeted(context, segment.included, segment.includedContexts)) { |
169 | | - return true; |
170 | | - } |
| 244 | + if (auto match = MatchBigSegment(segment, context, stack)) { |
| 245 | + return *match; |
| 246 | + } |
| 247 | + // Big segments don't use the regular include/exclude target lists; a |
| 248 | + // membership miss falls through directly to the segment's rules. |
| 249 | + } else { |
| 250 | + if (IsTargeted(context, segment.included, segment.includedContexts)) { |
| 251 | + return true; |
| 252 | + } |
171 | 253 |
|
172 | | - if (IsTargeted(context, segment.excluded, segment.excludedContexts)) { |
173 | | - return false; |
| 254 | + if (IsTargeted(context, segment.excluded, segment.excludedContexts)) { |
| 255 | + return false; |
| 256 | + } |
174 | 257 | } |
175 | 258 |
|
176 | 259 | for (auto const& rule : segment.rules) { |
|
0 commit comments