|
| 1 | +#include "fdv1_adapter_synchronizer.hpp" |
| 2 | + |
| 3 | +#include <utility> |
| 4 | + |
| 5 | +namespace launchdarkly::server_side::data_systems { |
| 6 | + |
| 7 | +using data_interfaces::FDv2SourceResult; |
| 8 | + |
| 9 | +// ----- State ----- |
| 10 | + |
| 11 | +bool FDv1AdapterSynchronizer::State::TryStart() { |
| 12 | + std::lock_guard lock(mutex_); |
| 13 | + if (started_ || closed_) { |
| 14 | + return false; |
| 15 | + } |
| 16 | + started_ = true; |
| 17 | + return true; |
| 18 | +} |
| 19 | + |
| 20 | +bool FDv1AdapterSynchronizer::State::MarkClosed() { |
| 21 | + std::lock_guard lock(mutex_); |
| 22 | + closed_ = true; |
| 23 | + return started_; |
| 24 | +} |
| 25 | + |
| 26 | +async::Future<FDv2SourceResult> FDv1AdapterSynchronizer::State::GetNext() { |
| 27 | + std::lock_guard lock(mutex_); |
| 28 | + if (!result_queue_.empty()) { |
| 29 | + auto result = std::move(result_queue_.front()); |
| 30 | + result_queue_.pop_front(); |
| 31 | + return async::MakeFuture(std::move(result)); |
| 32 | + } |
| 33 | + return pending_promise_.emplace().GetFuture(); |
| 34 | +} |
| 35 | + |
| 36 | +void FDv1AdapterSynchronizer::State::ResolvePendingAsShutdown() { |
| 37 | + std::optional<async::Promise<FDv2SourceResult>> promise; |
| 38 | + { |
| 39 | + std::lock_guard lock(mutex_); |
| 40 | + if (pending_promise_) { |
| 41 | + promise = std::move(pending_promise_); |
| 42 | + pending_promise_.reset(); |
| 43 | + } |
| 44 | + } |
| 45 | + if (promise) { |
| 46 | + promise->Resolve(FDv2SourceResult{FDv2SourceResult::Shutdown{}}); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void FDv1AdapterSynchronizer::State::Notify(FDv2SourceResult result) { |
| 51 | + std::optional<async::Promise<FDv2SourceResult>> promise; |
| 52 | + { |
| 53 | + std::lock_guard lock(mutex_); |
| 54 | + if (closed_) { |
| 55 | + return; |
| 56 | + } |
| 57 | + if (pending_promise_) { |
| 58 | + promise = std::move(pending_promise_); |
| 59 | + pending_promise_.reset(); |
| 60 | + } else { |
| 61 | + result_queue_.push_back(std::move(result)); |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + // Resolve outside the lock — Promise::Resolve may invoke inline |
| 66 | + // continuations that could call back into Notify or GetNext. |
| 67 | + promise->Resolve(std::move(result)); |
| 68 | +} |
| 69 | + |
| 70 | +// ----- ConvertingDestination ----- |
| 71 | + |
| 72 | +FDv1AdapterSynchronizer::ConvertingDestination::ConvertingDestination( |
| 73 | + std::weak_ptr<State> state) |
| 74 | + : state_(std::move(state)) {} |
| 75 | + |
| 76 | +void FDv1AdapterSynchronizer::ConvertingDestination::Init( |
| 77 | + data_model::SDKDataSet data_set) { |
| 78 | + auto state = state_.lock(); |
| 79 | + if (!state) { |
| 80 | + return; |
| 81 | + } |
| 82 | + data_interfaces::ChangeSetData changes; |
| 83 | + changes.reserve(data_set.flags.size() + data_set.segments.size()); |
| 84 | + for (auto& [key, flag] : data_set.flags) { |
| 85 | + changes.push_back({key, std::move(flag)}); |
| 86 | + } |
| 87 | + for (auto& [key, segment] : data_set.segments) { |
| 88 | + changes.push_back({key, std::move(segment)}); |
| 89 | + } |
| 90 | + state->Notify(FDv2SourceResult{FDv2SourceResult::ChangeSet{ |
| 91 | + data_model::ChangeSet<data_interfaces::ChangeSetData>{ |
| 92 | + data_model::ChangeSetType::kFull, std::move(changes), |
| 93 | + data_model::Selector{}}}}); |
| 94 | +} |
| 95 | + |
| 96 | +void FDv1AdapterSynchronizer::ConvertingDestination::Upsert( |
| 97 | + std::string const& key, |
| 98 | + data_model::FlagDescriptor flag) { |
| 99 | + auto state = state_.lock(); |
| 100 | + if (!state) { |
| 101 | + return; |
| 102 | + } |
| 103 | + data_interfaces::ChangeSetData changes; |
| 104 | + changes.push_back({key, std::move(flag)}); |
| 105 | + state->Notify(FDv2SourceResult{FDv2SourceResult::ChangeSet{ |
| 106 | + data_model::ChangeSet<data_interfaces::ChangeSetData>{ |
| 107 | + data_model::ChangeSetType::kPartial, std::move(changes), |
| 108 | + data_model::Selector{}}}}); |
| 109 | +} |
| 110 | + |
| 111 | +void FDv1AdapterSynchronizer::ConvertingDestination::Upsert( |
| 112 | + std::string const& key, |
| 113 | + data_model::SegmentDescriptor segment) { |
| 114 | + auto state = state_.lock(); |
| 115 | + if (!state) { |
| 116 | + return; |
| 117 | + } |
| 118 | + data_interfaces::ChangeSetData changes; |
| 119 | + changes.push_back({key, std::move(segment)}); |
| 120 | + state->Notify(FDv2SourceResult{FDv2SourceResult::ChangeSet{ |
| 121 | + data_model::ChangeSet<data_interfaces::ChangeSetData>{ |
| 122 | + data_model::ChangeSetType::kPartial, std::move(changes), |
| 123 | + data_model::Selector{}}}}); |
| 124 | +} |
| 125 | + |
| 126 | +std::string const& FDv1AdapterSynchronizer::ConvertingDestination::Identity() |
| 127 | + const { |
| 128 | + static std::string const identity = "FDv1 adapter destination"; |
| 129 | + return identity; |
| 130 | +} |
| 131 | + |
| 132 | +// ----- FDv1AdapterSynchronizer ----- |
| 133 | + |
| 134 | +FDv1AdapterSynchronizer::FDv1AdapterSynchronizer( |
| 135 | + std::unique_ptr<data_interfaces::IDataSynchronizer> fdv1_source) |
| 136 | + : state_(std::make_shared<State>()), |
| 137 | + destination_(std::make_unique<ConvertingDestination>(state_)), |
| 138 | + fdv1_source_(std::move(fdv1_source)) {} |
| 139 | + |
| 140 | +FDv1AdapterSynchronizer::~FDv1AdapterSynchronizer() { |
| 141 | + Close(); |
| 142 | +} |
| 143 | + |
| 144 | +async::Future<FDv2SourceResult> FDv1AdapterSynchronizer::Next( |
| 145 | + data_model::Selector /*selector*/) { |
| 146 | + auto closed = close_promise_.GetFuture(); |
| 147 | + if (closed.IsFinished()) { |
| 148 | + return async::MakeFuture( |
| 149 | + FDv2SourceResult{FDv2SourceResult::Shutdown{}}); |
| 150 | + } |
| 151 | + if (state_->TryStart()) { |
| 152 | + fdv1_source_->StartAsync(destination_.get(), |
| 153 | + /*bootstrap_data=*/nullptr); |
| 154 | + } |
| 155 | + auto result_future = state_->GetNext(); |
| 156 | + if (result_future.IsFinished()) { |
| 157 | + return result_future; |
| 158 | + } |
| 159 | + return async::WhenAny(closed, result_future) |
| 160 | + .Then( |
| 161 | + [state = state_, result_future](std::size_t const& idx) mutable |
| 162 | + -> async::Future<FDv2SourceResult> { |
| 163 | + if (idx == 0) { |
| 164 | + state->ResolvePendingAsShutdown(); |
| 165 | + return async::MakeFuture( |
| 166 | + FDv2SourceResult{FDv2SourceResult::Shutdown{}}); |
| 167 | + } |
| 168 | + return result_future; |
| 169 | + }, |
| 170 | + async::kInlineExecutor); |
| 171 | +} |
| 172 | + |
| 173 | +void FDv1AdapterSynchronizer::Close() { |
| 174 | + if (!close_promise_.Resolve(std::monostate{})) { |
| 175 | + return; |
| 176 | + } |
| 177 | + if (state_->MarkClosed()) { |
| 178 | + fdv1_source_->ShutdownAsync([] {}); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +std::string const& FDv1AdapterSynchronizer::Identity() const { |
| 183 | + static std::string const identity = "FDv1 fallback adapter"; |
| 184 | + return identity; |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace launchdarkly::server_side::data_systems |
0 commit comments