-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Add the FDv2 cache initializer and per-context freshness to the client #613
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
4
commits into
bklimt/SDK-3032/client-fdv2-orchestrator
from
bklimt/SDK-3033/client-fdv2-cache-initializer
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b547414
feat: Initialize client FDv2 from the local cache and track freshness
beekld 191860a
refactor: Rename the FDv2 freshness accessors to ReadFreshness and Ge…
beekld 95183e7
docs: Tighten comments in the FDv2 cache initializer and its tests
beekld 60b04ea
fix: Report FDv2 freshness only while the context's flags are cached
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
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
60 changes: 60 additions & 0 deletions
60
libs/client-sdk/src/data_sources/fdv2/cache_initializer.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,60 @@ | ||
| #include "cache_initializer.hpp" | ||
|
|
||
| #include <utility> | ||
|
|
||
| namespace launchdarkly::client_side::data_sources { | ||
|
|
||
| static char const* const kIdentity = "FDv2 cache initializer"; | ||
|
|
||
| FDv2CacheInitializer::FDv2CacheInitializer(flag_manager::FlagPersistence* cache, | ||
| Context context, | ||
| Logger const& logger) | ||
| : cache_(cache), context_(std::move(context)), logger_(logger) {} | ||
|
|
||
| async::Future<FDv2SourceResult> FDv2CacheInitializer::Run() { | ||
| auto data = cache_->ReadCached(context_); | ||
| if (!data) { | ||
| LD_LOG(logger_, LogLevel::kDebug) | ||
| << kIdentity << ": no cached data for this context"; | ||
| // A miss leaves the data set unchanged so initialization can continue. | ||
| return async::MakeFuture(FDv2SourceResult{FDv2SourceResult::ChangeSet{ | ||
| FlagChangeSet{data_model::ChangeSetType::kNone, | ||
| {}, | ||
| data_model::Selector{}}}}); | ||
| } | ||
|
|
||
| LD_LOG(logger_, LogLevel::kDebug) | ||
| << kIdentity << ": loaded " << data->size() | ||
| << " flags for this context"; | ||
|
|
||
| FlagChangeSetData changes; | ||
| changes.reserve(data->size()); | ||
| for (auto& [key, item] : *data) { | ||
| changes.push_back(FlagChange{key, std::move(item)}); | ||
| } | ||
|
|
||
| return async::MakeFuture(FDv2SourceResult{FDv2SourceResult::ChangeSet{ | ||
| FlagChangeSet{data_model::ChangeSetType::kFull, std::move(changes), | ||
| data_model::Selector{}}}}); | ||
| } | ||
|
|
||
| void FDv2CacheInitializer::Close() { | ||
| // Run() completes on the calling thread, so there is nothing to cancel. | ||
| } | ||
|
|
||
| std::string const& FDv2CacheInitializer::Identity() const { | ||
| static std::string const identity = kIdentity; | ||
| return identity; | ||
| } | ||
|
|
||
| FDv2CacheInitializerFactory::FDv2CacheInitializerFactory( | ||
| flag_manager::FlagPersistence* cache, | ||
| Context context, | ||
| Logger const& logger) | ||
| : cache_(cache), context_(std::move(context)), logger_(logger) {} | ||
|
|
||
| std::unique_ptr<IFDv2Initializer> FDv2CacheInitializerFactory::Build() { | ||
| return std::make_unique<FDv2CacheInitializer>(cache_, context_, logger_); | ||
| } | ||
|
|
||
| } // namespace launchdarkly::client_side::data_sources | ||
64 changes: 64 additions & 0 deletions
64
libs/client-sdk/src/data_sources/fdv2/cache_initializer.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,64 @@ | ||
| #pragma once | ||
|
|
||
| #include "ifdv2_initializer.hpp" | ||
| #include "ifdv2_initializer_factory.hpp" | ||
|
|
||
| #include "../../flag_manager/flag_persistence.hpp" | ||
|
|
||
| #include <launchdarkly/async/promise.hpp> | ||
| #include <launchdarkly/context.hpp> | ||
| #include <launchdarkly/logging/logger.hpp> | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace launchdarkly::client_side::data_sources { | ||
|
|
||
| /** | ||
| * Loads flag data the SDK persisted for this context on a previous run, so | ||
| * that evaluation can begin before the network answers. | ||
| */ | ||
| class FDv2CacheInitializer final : public IFDv2Initializer { | ||
| public: | ||
| /** | ||
| * @param cache Local cache to read. Non-owning. Must outlive this object. | ||
| * @param context The evaluation context to load data for. | ||
| * @param logger Receives diagnostic logging. | ||
| */ | ||
| FDv2CacheInitializer(flag_manager::FlagPersistence* cache, | ||
| Context context, | ||
| Logger const& logger); | ||
|
|
||
| async::Future<FDv2SourceResult> Run() override; | ||
|
|
||
| void Close() override; | ||
|
|
||
| [[nodiscard]] std::string const& Identity() const override; | ||
|
|
||
| private: | ||
| flag_manager::FlagPersistence* const cache_; | ||
| Context const context_; | ||
| Logger logger_; | ||
| }; | ||
|
|
||
| /** | ||
| * Builds fresh FDv2CacheInitializer instances on demand. | ||
| * | ||
| * Thread-safe: Build() may be called from any thread. | ||
| */ | ||
| class FDv2CacheInitializerFactory final : public IFDv2InitializerFactory { | ||
| public: | ||
| FDv2CacheInitializerFactory(flag_manager::FlagPersistence* cache, | ||
| Context context, | ||
| Logger const& logger); | ||
|
|
||
| std::unique_ptr<IFDv2Initializer> Build() override; | ||
|
|
||
| [[nodiscard]] bool IsFromCache() const override { return true; } | ||
|
|
||
| private: | ||
| flag_manager::FlagPersistence* const cache_; | ||
| Context const context_; | ||
| Logger logger_; | ||
| }; | ||
|
|
||
| } // namespace launchdarkly::client_side::data_sources |
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
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
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
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.
Freshness outlives overwritten flag data
Medium Severity
ReadFreshnesstreats any cached flags for the context'sCanonicalKeyas proof the stored timestamp still describes that data. Flag data is keyed only by that key, while freshness is keyed by a hash of the full context, so a later payload for the same key with different attributes overwrites the flags and leaves the earlier timestamp in place.ReadFreshnessthen reports the old time for the new data, which can make a poll wait on evaluations that were never confirmed for this attribute set.Additional Locations (2)
libs/client-sdk/src/flag_manager/flag_persistence.cpp#L117-L123libs/client-sdk/src/flag_manager/flag_persistence.cpp#L66-L71Reviewed by Cursor Bugbot for commit fc7d735. Configure here.