-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add expiration tracker. #188
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
662b0b2
feat: Add persistent core interface.
kinyoklion 7bcf2ea
feat: Add expiration tracker.
kinyoklion 17c9dc1
feat: Add expiration tracker.
kinyoklion 27bd749
Constructor/destructor/move/assign.
kinyoklion 4ba9f57
Merge branch 'rlamb/add-persistent-core-interface' into rlamb/add-exp…
kinyoklion 02d8de1
Add public specifiers.
kinyoklion f0e4a44
Merge branch 'rlamb/add-persistent-core-interface' into rlamb/add-exp…
kinyoklion a1b7a44
Add extra blank line
kinyoklion 0aeb6e8
Format
kinyoklion 740a58d
Merge branch 'server-side' into rlamb/add-expiration-tracker
kinyoklion 9c8903d
PR feedback.
kinyoklion 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
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #include "dependency_tracker.hpp" | ||
| #include "tagged_data.hpp" | ||
|
|
||
| #include <type_traits> | ||
|
|
||
|
|
||
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
152 changes: 152 additions & 0 deletions
152
libs/server-sdk/src/data_store/persistent/expiration_tracker.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,152 @@ | ||
| #include "expiration_tracker.hpp" | ||
|
|
||
| namespace launchdarkly::server_side::data_store::persistent { | ||
|
|
||
| void ExpirationTracker::Add(std::string const& key, | ||
| ExpirationTracker::TimePoint expiration) { | ||
| unscoped_.insert({key, expiration}); | ||
| } | ||
|
|
||
| void ExpirationTracker::Remove(std::string const& key) { | ||
| unscoped_.erase(key); | ||
| } | ||
|
|
||
| ExpirationTracker::TrackState ExpirationTracker::State( | ||
| std::string const& key, | ||
| ExpirationTracker::TimePoint current_time) const { | ||
| auto item = unscoped_.find(key); | ||
| if (item != unscoped_.end()) { | ||
| return State(item->second, current_time); | ||
| } | ||
|
|
||
| return ExpirationTracker::TrackState::kNotTracked; | ||
| } | ||
|
|
||
| void ExpirationTracker::Add(data_store::DataKind kind, | ||
| std::string const& key, | ||
| ExpirationTracker::TimePoint expiration) { | ||
| scoped_.Set(kind, key, expiration); | ||
| } | ||
|
|
||
| void ExpirationTracker::Remove(data_store::DataKind kind, | ||
| std::string const& key) { | ||
| scoped_.Remove(kind, key); | ||
| } | ||
|
|
||
| ExpirationTracker::TrackState ExpirationTracker::State( | ||
| data_store::DataKind kind, | ||
| std::string const& key, | ||
| ExpirationTracker::TimePoint current_time) const { | ||
| auto expiration = scoped_.Get(kind, key); | ||
| if (expiration.has_value()) { | ||
| return State(expiration.value(), current_time); | ||
| } | ||
| return ExpirationTracker::TrackState::kNotTracked; | ||
| } | ||
|
|
||
| void ExpirationTracker::Clear() { | ||
| scoped_.Clear(); | ||
| unscoped_.clear(); | ||
| } | ||
| std::vector<std::pair<std::optional<DataKind>, std::string>> | ||
| ExpirationTracker::Prune(ExpirationTracker::TimePoint current_time) { | ||
| std::vector<std::pair<std::optional<DataKind>, std::string>> pruned; | ||
|
|
||
| // Determine everything to be pruned. | ||
| for (auto const& item : unscoped_) { | ||
| if (State(item.second, current_time) == | ||
| ExpirationTracker::TrackState::kStale) { | ||
kinyoklion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pruned.emplace_back(std::nullopt, item.first); | ||
| } | ||
| } | ||
| for (auto const& scope : scoped_) { | ||
| for (auto const& item : scope.Data()) { | ||
| if (State(item.second, current_time) == | ||
| ExpirationTracker::TrackState::kStale) { | ||
| pruned.emplace_back(scope.Kind(), item.first); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Do the actual prune. | ||
| for (auto const& item : pruned) { | ||
| if (item.first.has_value()) { | ||
| scoped_.Remove(item.first.value(), item.second); | ||
| } else { | ||
| unscoped_.erase(item.second); | ||
| } | ||
| } | ||
| return pruned; | ||
| } | ||
| ExpirationTracker::TrackState ExpirationTracker::State( | ||
| ExpirationTracker::TimePoint expiration, | ||
| ExpirationTracker::TimePoint current_time) { | ||
| if (expiration > current_time) { | ||
| return ExpirationTracker::TrackState::kFresh; | ||
| } | ||
| return ExpirationTracker::TrackState::kStale; | ||
| } | ||
|
|
||
| void ExpirationTracker::ScopedTtls::Set( | ||
| DataKind kind, | ||
| std::string const& key, | ||
| ExpirationTracker::TimePoint expiration) { | ||
| data_[static_cast<std::underlying_type_t<DataKind>>(kind)].Data().insert( | ||
| {key, expiration}); | ||
| } | ||
|
|
||
| void ExpirationTracker::ScopedTtls::Remove(DataKind kind, | ||
| std::string const& key) { | ||
| data_[static_cast<std::underlying_type_t<DataKind>>(kind)].Data().erase( | ||
| key); | ||
| } | ||
|
|
||
| void ExpirationTracker::ScopedTtls::Clear() { | ||
| for (auto& scope : data_) { | ||
| scope.Data().clear(); | ||
| } | ||
| } | ||
|
|
||
| std::optional<ExpirationTracker::TimePoint> ExpirationTracker::ScopedTtls::Get( | ||
| DataKind kind, | ||
| std::string const& key) const { | ||
| auto const& scope = | ||
| data_[static_cast<std::underlying_type_t<DataKind>>(kind)]; | ||
| auto found = scope.Data().find(key); | ||
| if (found != scope.Data().end()) { | ||
| return found->second; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
| ExpirationTracker::ScopedTtls::ScopedTtls() | ||
| : data_{ | ||
| TaggedData<TtlMap>(DataKind::kFlag), | ||
| TaggedData<TtlMap>(DataKind::kSegment), | ||
| } {} | ||
|
|
||
| std::array<TaggedData<ExpirationTracker::TtlMap>, 2>::iterator | ||
| ExpirationTracker::ScopedTtls::begin() { | ||
| return data_.begin(); | ||
| } | ||
|
|
||
| std::array<TaggedData<ExpirationTracker::TtlMap>, 2>::iterator | ||
| ExpirationTracker::ScopedTtls::end() { | ||
| return data_.end(); | ||
| } | ||
|
|
||
| std::ostream& operator<<(std::ostream& out, | ||
| ExpirationTracker::TrackState const& state) { | ||
| switch (state) { | ||
| case ExpirationTracker::TrackState::kFresh: | ||
| out << "FRESH"; | ||
| break; | ||
| case ExpirationTracker::TrackState::kStale: | ||
| out << "STALE"; | ||
| break; | ||
| case ExpirationTracker::TrackState::kNotTracked: | ||
| out << "NOT_TRACKED"; | ||
| break; | ||
| } | ||
| return out; | ||
| } | ||
| } // namespace launchdarkly::server_side::data_store::persistent | ||
144 changes: 144 additions & 0 deletions
144
libs/server-sdk/src/data_store/persistent/expiration_tracker.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,144 @@ | ||
| #pragma once | ||
|
|
||
| #include <chrono> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| #include <launchdarkly/connection.hpp> | ||
|
|
||
| #include "../../data_store/data_kind.hpp" | ||
| #include "../tagged_data.hpp" | ||
|
|
||
| namespace launchdarkly::server_side::data_store::persistent { | ||
|
|
||
| class ExpirationTracker { | ||
| public: | ||
| using TimePoint = std::chrono::time_point<std::chrono::steady_clock>; | ||
|
|
||
| /** | ||
| * The state of the key in the tracker. | ||
| */ | ||
| enum class TrackState { | ||
| /** | ||
| * The key is tracked and the key expiration is in the future. | ||
| */ | ||
| kFresh, | ||
| /** | ||
| * The key is tracked and the expiration is either now or in the past. | ||
| */ | ||
| kStale, | ||
| /** | ||
| * The key is not being tracked. | ||
| */ | ||
| kNotTracked | ||
| }; | ||
|
|
||
| /** | ||
| * Add an unscoped key to the tracker. | ||
| * | ||
| * @param key The key to track. | ||
| * @param expiration The time that the key expires. | ||
| * used. | ||
| */ | ||
| void Add(std::string const& key, TimePoint expiration); | ||
|
|
||
| /** | ||
| * Remove an unscoped key from the tracker. | ||
| * | ||
| * @param key The key to stop tracking. | ||
| */ | ||
| void Remove(std::string const& key); | ||
|
|
||
| /** | ||
| * Check the state of an unscoped key. | ||
| * | ||
| * @param key The key to check. | ||
| * @param current_time The current time. | ||
| * @return The state of the key. | ||
| */ | ||
| TrackState State(std::string const& key, TimePoint current_time) const; | ||
|
|
||
| /** | ||
| * Add a scoped key to the tracker. Will use the specified TTL for the kind. | ||
| * | ||
| * @param kind The scope (kind) of the key. | ||
| * @param key The key to track. | ||
| * @param expiration The time that the key expires. | ||
| */ | ||
| void Add(data_store::DataKind kind, | ||
| std::string const& key, | ||
| TimePoint expiration); | ||
|
|
||
| /** | ||
| * Remove a scoped key from the tracker. | ||
| * | ||
| * @param kind The scope (kind) of the key. | ||
| * @param key The key to stop tracking. | ||
| */ | ||
| void Remove(data_store::DataKind kind, std::string const& key); | ||
|
|
||
| /** | ||
| * Check the state of a scoped key. | ||
| * | ||
| * @param kind The scope (kind) of the key. | ||
| * @param key The key to check. | ||
| * @return The state of the key. | ||
| */ | ||
| TrackState State(data_store::DataKind kind, | ||
| std::string const& key, | ||
| TimePoint current_time) const; | ||
|
|
||
| /** | ||
| * Stop tracking all keys. | ||
| */ | ||
| void Clear(); | ||
|
|
||
| /** | ||
| * Prune expired keys from the tracker. | ||
| * @param current_time The current time. | ||
| * @return A list of all the kinds and associated keys that expired. | ||
| * Unscoped keys will have std::nullopt as the kind. | ||
| */ | ||
| std::vector<std::pair<std::optional<DataKind>, std::string>> Prune( | ||
| TimePoint current_time); | ||
|
|
||
| private: | ||
| using TtlMap = std::unordered_map<std::string, TimePoint>; | ||
|
|
||
| TtlMap unscoped_; | ||
|
|
||
| static ExpirationTracker::TrackState State( | ||
| ExpirationTracker::TimePoint expiration, | ||
| ExpirationTracker::TimePoint current_time); | ||
|
|
||
| class ScopedTtls { | ||
| public: | ||
| ScopedTtls(); | ||
|
|
||
| using DataType = | ||
| std::array<TaggedData<TtlMap>, | ||
| static_cast<std::underlying_type_t<DataKind>>( | ||
| DataKind::kKindCount)>; | ||
| void Set(DataKind kind, std::string const& key, TimePoint expiration); | ||
| void Remove(DataKind kind, std::string const& key); | ||
| std::optional<TimePoint> Get(DataKind kind, | ||
| std::string const& key) const; | ||
| void Clear(); | ||
|
|
||
| [[nodiscard]] typename DataType::iterator begin(); | ||
|
|
||
| [[nodiscard]] typename DataType::iterator end(); | ||
|
|
||
| private: | ||
| DataType data_; | ||
| }; | ||
|
|
||
| ScopedTtls scoped_; | ||
| }; | ||
|
|
||
| std::ostream& operator<<(std::ostream& out, | ||
| ExpirationTracker::TrackState const& state); | ||
|
|
||
| } // namespace launchdarkly::server_side::data_store::persistent |
3 changes: 3 additions & 0 deletions
3
libs/server-sdk/src/data_store/persistent/persistent_data_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,3 @@ | ||
| #include "persistent_data_store.hpp" | ||
|
|
||
| namespace launchdarkly::server_side::data_store::persistent {} |
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.
Moved this to its own file so it could be re-used.