-
Notifications
You must be signed in to change notification settings - Fork 4
refactor: implement fdv2 polling initializer / synchronizer #519
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
bf945b3
refactor: add Apply method to MemoryStore for transactional change sets
beekld cc59635
fix an unneeded copy, and warn on missing enum case
beekld 82d09d2
add an ApplyResult with the changed keys
beekld 259d8e9
mark the apply result as nodiscard for now
beekld f5b82bf
simplify tests
beekld 7826357
simplify, since FDv2 doesnt require version checking in memory store
beekld 651a780
refactor: define new Synchronizer and Initializer interfaces for FDv2
beekld fe37f55
move selector source into Next
beekld c470763
Merge branch 'main' into beeklimt/SDK-2096
beekld ed22857
distinguish between timeouts and errors
beekld f0013da
refactor: implement fdv2 polling initializer / synchronizer
beekld 29a94c8
update for upstream change
beekld 9e75b47
Merge branch 'main' into beeklimt/SDK-2096
beekld c8e1c7f
Merge branch 'beeklimt/SDK-2096' into beeklimt/SDK-2097
beekld 0996cca
refactor: fix an error type that could be better
beekld 92489a6
refactor polling synchronizer to make it more threadsafe
beekld 071a4eb
update asio_requester to use new style
beekld f1b3f06
add comments
beekld 37fb4c0
fix missing kInactive handling
beekld 07810cd
refactor error types to preserve more info
beekld 55e4d8c
refactor shared code into a separate file
beekld b8a334c
refactor FDv2ProtocolHandler::HandleEvent to be clearer
beekld 6e5b1d4
clang format
beekld 1e2d438
handle closed first in network response
beekld 01cc26b
handle url parsing failure
beekld 7a60c2a
minor cleanup and add tests
beekld c8e736f
clang-format
beekld 19da904
Merge branch 'main' into beeklimt/SDK-2097
beekld 48989a4
rewrite everything to use promises.
beekld 45befda
fix: make it safe to delete the synchronizer at any time
beekld cd0914d
refactor: reorder and clean up code
beekld 9d08671
docs: fix some comments
beekld 20e3f96
refactor: some simple optimizations
beekld d37dc5d
refactor: more cleanup
beekld 3378ed1
fix: close the synchronizer on destruction
beekld 66e3936
fix: minor feedback fix
beekld e9afed0
docs: fix some comments
beekld 112c78e
refactor: use generic http requester interface
beekld 4b02b67
refactor: break up HandleFDv2PollResponse into separate functions
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
62 changes: 62 additions & 0 deletions
62
libs/internal/include/launchdarkly/fdv2_protocol_handler.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,62 @@ | ||
| #pragma once | ||
|
|
||
| #include <launchdarkly/data_model/fdv2_change.hpp> | ||
| #include <launchdarkly/serialization/json_fdv2_events.hpp> | ||
|
|
||
| #include <boost/json/value.hpp> | ||
|
|
||
| #include <string_view> | ||
| #include <variant> | ||
| #include <vector> | ||
|
|
||
| namespace launchdarkly { | ||
|
|
||
| /** | ||
| * Protocol state machine for the FDv2 wire format. | ||
| * | ||
| * Accumulates put-object and delete-object events between a server-intent | ||
| * and payload-transferred event, then emits a complete FDv2ChangeSet. | ||
| * | ||
| * Shared between the polling and streaming synchronizers. | ||
| */ | ||
| class FDv2ProtocolHandler { | ||
| public: | ||
| /** | ||
| * Result of handling a single FDv2 event: | ||
| * - monostate: no output yet (accumulating, heartbeat, or unknown event) | ||
| * - FDv2ChangeSet: complete changeset ready to apply | ||
| * - FDv2Error: server reported an error; discard partial data | ||
| * - Goodbye: server is closing; caller should rotate sources | ||
| */ | ||
| using Result = std::variant<std::monostate, | ||
| data_model::FDv2ChangeSet, | ||
| FDv2Error, | ||
| Goodbye>; | ||
|
|
||
| /** | ||
| * Process one FDv2 event. | ||
| * | ||
| * @param event_type The event type string (e.g. "server-intent", | ||
| * "put-object", "payload-transferred"). | ||
| * @param data The parsed JSON value for the event's data field. | ||
| * @return A Result indicating what (if anything) the caller | ||
| * should act on. | ||
| */ | ||
| Result HandleEvent(std::string_view event_type, | ||
| boost::json::value const& data); | ||
|
|
||
| /** | ||
| * Reset accumulated state. Call on reconnect before processing new events. | ||
| */ | ||
| void Reset(); | ||
|
|
||
| FDv2ProtocolHandler() = default; | ||
|
|
||
| private: | ||
| enum class State { kInactive, kFull, kPartial }; | ||
|
|
||
| State state_ = State::kInactive; | ||
| std::vector<data_model::FDv2Change> changes_; | ||
| }; | ||
|
|
||
| } // namespace launchdarkly |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| #include <launchdarkly/fdv2_protocol_handler.hpp> | ||
|
|
||
| #include <launchdarkly/data_model/flag.hpp> | ||
| #include <launchdarkly/data_model/item_descriptor.hpp> | ||
| #include <launchdarkly/data_model/segment.hpp> | ||
| #include <launchdarkly/serialization/json_flag.hpp> | ||
| #include <launchdarkly/serialization/json_segment.hpp> | ||
|
|
||
| #include <boost/json.hpp> | ||
| #include <tl/expected.hpp> | ||
|
|
||
| namespace launchdarkly { | ||
|
|
||
| static char const* const kServerIntent = "server-intent"; | ||
| static char const* const kPutObject = "put-object"; | ||
| static char const* const kDeleteObject = "delete-object"; | ||
| static char const* const kPayloadTransferred = "payload-transferred"; | ||
| static char const* const kError = "error"; | ||
| static char const* const kGoodbye = "goodbye"; | ||
|
|
||
| // Returns the parsed FDv2Change on success, nullopt for unknown kinds (which | ||
| // should be silently skipped for forward-compatibility), or an error string if | ||
| // a known kind fails to deserialize. | ||
| static tl::expected<std::optional<data_model::FDv2Change>, std::string> | ||
| ParsePut(PutObject const& put) { | ||
| if (put.kind == "flag") { | ||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<data_model::Flag>, JsonError>>( | ||
| put.object); | ||
| // One bad flag aborts the entire transfer so the store is never | ||
| // left in a partially-updated state. | ||
| if (!result) { | ||
| return tl::make_unexpected("could not deserialize flag '" + | ||
| put.key + "'"); | ||
| } | ||
| if (!result->has_value()) { | ||
| return tl::make_unexpected("flag '" + put.key + "' object was null"); | ||
| } | ||
| return data_model::FDv2Change{ | ||
| put.key, | ||
| data_model::ItemDescriptor<data_model::Flag>{std::move(**result)}}; | ||
| } | ||
| if (put.kind == "segment") { | ||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<data_model::Segment>, JsonError>>( | ||
| put.object); | ||
| // One bad segment aborts the entire transfer so the store is never | ||
| // left in a partially-updated state. | ||
| if (!result) { | ||
| return tl::make_unexpected("could not deserialize segment '" + | ||
| put.key + "'"); | ||
| } | ||
| if (!result->has_value()) { | ||
| return tl::make_unexpected("segment '" + put.key + | ||
| "' object was null"); | ||
| } | ||
| return data_model::FDv2Change{ | ||
| put.key, | ||
| data_model::ItemDescriptor<data_model::Segment>{ | ||
| std::move(**result)}}; | ||
| } | ||
| // Silently skip unknown kinds for forward-compatibility. | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| static data_model::FDv2Change MakeDeleteChange(DeleteObject const& del) { | ||
| if (del.kind == "flag") { | ||
| return data_model::FDv2Change{ | ||
| del.key, | ||
| data_model::ItemDescriptor<data_model::Flag>{ | ||
| data_model::Tombstone{static_cast<uint64_t>(del.version)}}}; | ||
| } | ||
| return data_model::FDv2Change{ | ||
| del.key, | ||
| data_model::ItemDescriptor<data_model::Segment>{ | ||
| data_model::Tombstone{static_cast<uint64_t>(del.version)}}}; | ||
| } | ||
|
beekld marked this conversation as resolved.
|
||
|
|
||
| FDv2ProtocolHandler::Result FDv2ProtocolHandler::HandleEvent( | ||
| std::string_view event_type, | ||
| boost::json::value const& data) { | ||
| if (event_type == kServerIntent) { | ||
|
kinyoklion marked this conversation as resolved.
|
||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<ServerIntent>, JsonError>>(data); | ||
| if (!result) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, "could not deserialize server-intent"}; | ||
| } | ||
| if (!result->has_value()) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, "server-intent data was null"}; | ||
| } | ||
| auto const& intent = **result; | ||
| if (intent.payloads.empty()) { | ||
| return std::monostate{}; | ||
| } | ||
| auto const& code = intent.payloads[0].intent_code; | ||
| changes_.clear(); | ||
| if (code == IntentCode::kTransferFull) { | ||
| state_ = State::kFull; | ||
| } else if (code == IntentCode::kTransferChanges) { | ||
| state_ = State::kPartial; | ||
| } else { | ||
| // kNone or kUnknown: emit an empty changeset immediately. | ||
| state_ = State::kInactive; | ||
| return data_model::FDv2ChangeSet{data_model::FDv2ChangeSet::Type::kNone, | ||
| {}, | ||
| data_model::Selector{}}; | ||
| } | ||
| return std::monostate{}; | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| if (event_type == kPutObject) { | ||
| if (state_ == State::kInactive) { | ||
| return std::monostate{}; | ||
| } | ||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<PutObject>, JsonError>>(data); | ||
| if (!result) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, "could not deserialize put-object"}; | ||
| } | ||
| if (!result->has_value()) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, "put-object data was null"}; | ||
| } | ||
| auto change = ParsePut(**result); | ||
| if (!change) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, std::move(change.error())}; | ||
| } | ||
| if (*change) { | ||
| changes_.push_back(std::move(**change)); | ||
| } | ||
| return std::monostate{}; | ||
| } | ||
|
|
||
| if (event_type == kDeleteObject) { | ||
| if (state_ == State::kInactive) { | ||
| return std::monostate{}; | ||
| } | ||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<DeleteObject>, JsonError>>(data); | ||
| if (!result) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, "could not deserialize delete-object"}; | ||
|
beekld marked this conversation as resolved.
Outdated
|
||
| } | ||
| if (!result->has_value()) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, "delete-object data was null"}; | ||
| } | ||
| auto const& del = **result; | ||
| // Silently skip unknown kinds for forward-compatibility. | ||
| if (del.kind != "flag" && del.kind != "segment") { | ||
| return std::monostate{}; | ||
| } | ||
| changes_.push_back(MakeDeleteChange(del)); | ||
| return std::monostate{}; | ||
| } | ||
|
|
||
| if (event_type == kPayloadTransferred) { | ||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<PayloadTransferred>, JsonError>>(data); | ||
| if (!result) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, | ||
| "could not deserialize payload-transferred"}; | ||
| } | ||
| if (!result->has_value()) { | ||
| Reset(); | ||
| return FDv2Error{std::nullopt, "payload-transferred data was null"}; | ||
| } | ||
| auto const& transferred = **result; | ||
| auto type = (state_ == State::kPartial) | ||
| ? data_model::FDv2ChangeSet::Type::kPartial | ||
| : data_model::FDv2ChangeSet::Type::kFull; | ||
| data_model::FDv2ChangeSet changeset{ | ||
| type, | ||
| std::move(changes_), | ||
| data_model::Selector{data_model::Selector::State{ | ||
| transferred.version, transferred.state}}}; | ||
| Reset(); | ||
| return changeset; | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| if (event_type == kError) { | ||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<FDv2Error>, JsonError>>(data); | ||
| Reset(); | ||
| if (!result) { | ||
| return FDv2Error{std::nullopt, "could not deserialize error event"}; | ||
| } | ||
| if (!result->has_value()) { | ||
| return FDv2Error{std::nullopt, "error event data was null"}; | ||
| } | ||
| return **result; | ||
| } | ||
|
|
||
| if (event_type == kGoodbye) { | ||
| auto result = boost::json::value_to< | ||
| tl::expected<std::optional<Goodbye>, JsonError>>(data); | ||
| if (!result) { | ||
| return Goodbye{std::nullopt}; | ||
| } | ||
| if (!result->has_value()) { | ||
| return Goodbye{std::nullopt}; | ||
| } | ||
| return **result; | ||
| } | ||
|
|
||
| // heartbeat and unrecognized events: no-op. | ||
| return std::monostate{}; | ||
| } | ||
|
|
||
| void FDv2ProtocolHandler::Reset() { | ||
| state_ = State::kInactive; | ||
| changes_.clear(); | ||
| } | ||
|
|
||
| } // namespace launchdarkly | ||
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.
This ends up with maybe a slightly different layer responsibility, but I think it is likely fine. I think Java returns it, and then it is discarded a layer up, but we aren't acting on it, so it doesn't really make a difference.
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.
Actually I am a bit concerned that we may be losing granularity of data source reporting. Though maybe we don't have that concern at the moment.
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.
I'm not sure what exactly you're asking here. The Java version seems to have a distinction between parsing and "translation" that isn't there in C++. This function doesn't return raw JSON, so there's nothing to be returned in this case.
I believe, technically, our spec says that this case should silently ignore the uknown kind, whereas the Java implementation logs a warning. I followed the spec, but I could add a warning here, if you'd prefer.
This is the only branch that returns
std::nullopt, so callers could still technically distinguish this case, but I'm not sure what else we would do about it.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.
So, the thing I am trying to get at here is that the protocol handler here knows about the types, where I expect 1 layer up from here to know the types. Or for this to take a mapper type interface.
The reason being is that theoretically a client and server can use the same protocol handler, but they get different model types.
Server has
flagandsegment, and client-side has aflag-eval. The protocol itself should be agnostic to these types theoretically. Then you report "hey we got a change to kind: potato", and the data source layer can be like "I don't support potato" and then discard it.In the Java version it just remains as some kind of JsonElement until another layer knows how to handle it.
Another option though is a mapper. Where the handler takes a mapper, and then says "map this type for me" and if the mapper says it cannot handle it, then we also know to discard it.
Mapper approach:
In regards to the log, I do tend to prefer a log, but one could argue about it being debug, or just once, or something. But that isn't the main concern. This layer knowing about the specific types is.
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.
I am making a note to handle this in a follow-up PR. I'm just worried about this PR getting too big and out of hand.