Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2018-2019 Nokia. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 22 | #include <chrono> |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 23 | #include <sstream> |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 24 | #include <sdl/asyncstorage.hpp> |
| 25 | #include <sdl/backenderror.hpp> |
| 26 | #include <sdl/errorqueries.hpp> |
| 27 | #include <sdl/invalidnamespace.hpp> |
| 28 | #include <sdl/notconnected.hpp> |
| 29 | #include <sdl/operationinterrupted.hpp> |
| 30 | #include <sdl/rejectedbybackend.hpp> |
| 31 | #include <sdl/rejectedbysdl.hpp> |
| 32 | #include "private/redis/asyncredisstorage.hpp" |
| 33 | #include "private/syncstorageimpl.hpp" |
| 34 | #include "private/system.hpp" |
| 35 | |
| 36 | using namespace shareddatalayer; |
| 37 | |
| 38 | namespace |
| 39 | { |
| 40 | void throwExceptionForErrorCode[[ noreturn ]](const std::error_code& ec) |
| 41 | { |
| 42 | if (ec == shareddatalayer::Error::BACKEND_FAILURE) |
| 43 | throw BackendError(ec.message()); |
| 44 | else if (ec == shareddatalayer::Error::NOT_CONNECTED) |
| 45 | throw NotConnected(ec.message()); |
| 46 | else if (ec == shareddatalayer::Error::OPERATION_INTERRUPTED) |
| 47 | throw OperationInterrupted(ec.message()); |
| 48 | else if (ec == shareddatalayer::Error::REJECTED_BY_BACKEND) |
| 49 | throw RejectedByBackend(ec.message()); |
| 50 | else if (ec == AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE) |
| 51 | throw InvalidNamespace(ec.message()); |
| 52 | else if (ec == shareddatalayer::Error::REJECTED_BY_SDL) |
| 53 | throw RejectedBySdl(ec.message()); |
| 54 | |
| 55 | std::ostringstream os; |
| 56 | os << "No corresponding SDL exception found for error code: " << ec.category().name() << " " << ec.value(); |
| 57 | throw std::range_error(os.str()); |
| 58 | } |
| 59 | } |
| 60 | |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 61 | /* TODO: This synchronous API implementation could probably be refactored to be boost::asio based |
| 62 | * instead of current (bit error prone) poll based implementation. |
| 63 | */ |
| 64 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 65 | SyncStorageImpl::SyncStorageImpl(std::unique_ptr<AsyncStorage> asyncStorage): |
| 66 | SyncStorageImpl(std::move(asyncStorage), System::getSystem()) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | SyncStorageImpl::SyncStorageImpl(std::unique_ptr<AsyncStorage> pAsyncStorage, |
| 71 | System& system): |
| 72 | asyncStorage(std::move(pAsyncStorage)), |
| 73 | system(system), |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 74 | localStatus(false), |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 75 | synced(false), |
| 76 | isReady(false), |
| 77 | events{ asyncStorage->fd(), POLLIN, 0 }, |
| 78 | operationTimeout(std::chrono::steady_clock::duration::zero()) |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 79 | { |
| 80 | } |
| 81 | |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 82 | void SyncStorageImpl::waitReadyAck(const std::error_code& error) |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 83 | { |
| 84 | isReady = true; |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 85 | localError = error; |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 86 | } |
| 87 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 88 | void SyncStorageImpl::modifyAck(const std::error_code& error) |
| 89 | { |
| 90 | synced = true; |
| 91 | localError = error; |
| 92 | } |
| 93 | |
| 94 | void SyncStorageImpl::modifyIfAck(const std::error_code& error, bool status) |
| 95 | { |
| 96 | synced = true; |
| 97 | localError = error; |
| 98 | localStatus = status; |
| 99 | } |
| 100 | |
| 101 | void SyncStorageImpl::getAck(const std::error_code& error, const DataMap& dataMap) |
| 102 | { |
| 103 | synced = true; |
| 104 | localError = error; |
| 105 | localMap = dataMap; |
| 106 | } |
| 107 | |
| 108 | void SyncStorageImpl::findKeysAck(const std::error_code& error, const Keys& keys) |
| 109 | { |
| 110 | synced = true; |
| 111 | localError = error; |
| 112 | localKeys = keys; |
| 113 | } |
| 114 | |
| 115 | void SyncStorageImpl::verifyBackendResponse() |
| 116 | { |
| 117 | if(localError) |
| 118 | throwExceptionForErrorCode(localError); |
| 119 | } |
| 120 | |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 121 | void SyncStorageImpl::waitForOperationCallback() |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 122 | { |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 123 | while(!synced) |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 124 | pollAndHandleEvents(NO_TIMEOUT); |
| 125 | } |
| 126 | |
| 127 | void SyncStorageImpl::pollAndHandleEvents(int timeout_ms) |
| 128 | { |
| 129 | if (system.poll(&events, 1, timeout_ms) > 0 && (events.revents & POLLIN)) |
| 130 | asyncStorage->handleEvents(); |
| 131 | } |
| 132 | |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 133 | void SyncStorageImpl::waitForReadinessCheckCallback(const std::chrono::steady_clock::duration& timeout) |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 134 | { |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 135 | if (timeout == std::chrono::steady_clock::duration::zero()) |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 136 | { |
| 137 | while (!isReady) |
| 138 | pollAndHandleEvents(NO_TIMEOUT); |
| 139 | } |
| 140 | else |
| 141 | { |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 142 | auto timeout_ms(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()); |
| 143 | auto pollTimeout_ms(timeout_ms / 10); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 144 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 145 | while(!isReady && (std::chrono::steady_clock::now() - start < std::chrono::milliseconds(timeout_ms))) |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 146 | pollAndHandleEvents(pollTimeout_ms); |
| 147 | } |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void SyncStorageImpl::waitSdlToBeReady(const Namespace& ns) |
| 151 | { |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 152 | waitSdlToBeReady(ns, operationTimeout); |
| 153 | } |
| 154 | |
| 155 | void SyncStorageImpl::waitSdlToBeReady(const Namespace& ns, const std::chrono::steady_clock::duration& timeout) |
| 156 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 157 | isReady = false; |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 158 | asyncStorage->waitReadyAsync(ns, |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 159 | std::bind(&shareddatalayer::SyncStorageImpl::waitReadyAck, |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 160 | this, |
Timo Tietavainen | d565df6 | 2021-08-11 07:33:30 +0300 | [diff] [blame] | 161 | std::placeholders::_1)); |
| 162 | waitForReadinessCheckCallback(timeout); |
| 163 | } |
| 164 | |
| 165 | void SyncStorageImpl::waitReady(const Namespace& ns, const std::chrono::steady_clock::duration& timeout) |
| 166 | { |
| 167 | waitSdlToBeReady(ns, timeout); |
| 168 | if(!isReady) |
| 169 | throw RejectedBySdl("Timeout, SDL service not ready for the '" + ns + "' namespace"); |
| 170 | verifyBackendResponse(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void SyncStorageImpl::set(const Namespace& ns, const DataMap& dataMap) |
| 174 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 175 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 176 | waitSdlToBeReady(ns); |
| 177 | synced = false; |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 178 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 179 | asyncStorage->setAsync(ns, |
| 180 | dataMap, |
| 181 | std::bind(&shareddatalayer::SyncStorageImpl::modifyAck, |
| 182 | this, |
| 183 | std::placeholders::_1)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 184 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 185 | verifyBackendResponse(); |
| 186 | } |
| 187 | |
| 188 | bool SyncStorageImpl::setIf(const Namespace& ns, const Key& key, const Data& oldData, const Data& newData) |
| 189 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 190 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 191 | waitSdlToBeReady(ns); |
| 192 | synced = false; |
| 193 | asyncStorage->setIfAsync(ns, |
| 194 | key, |
| 195 | oldData, |
| 196 | newData, |
| 197 | std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck, |
| 198 | this, |
| 199 | std::placeholders::_1, |
| 200 | std::placeholders::_2)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 201 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 202 | verifyBackendResponse(); |
| 203 | return localStatus; |
| 204 | } |
| 205 | |
| 206 | bool SyncStorageImpl::setIfNotExists(const Namespace& ns, const Key& key, const Data& data) |
| 207 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 208 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 209 | waitSdlToBeReady(ns); |
| 210 | synced = false; |
| 211 | asyncStorage->setIfNotExistsAsync(ns, |
| 212 | key, |
| 213 | data, |
| 214 | std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck, |
| 215 | this, |
| 216 | std::placeholders::_1, |
| 217 | std::placeholders::_2)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 218 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 219 | verifyBackendResponse(); |
| 220 | return localStatus; |
| 221 | } |
| 222 | |
| 223 | SyncStorageImpl::DataMap SyncStorageImpl::get(const Namespace& ns, const Keys& keys) |
| 224 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 225 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 226 | waitSdlToBeReady(ns); |
| 227 | synced = false; |
| 228 | asyncStorage->getAsync(ns, |
| 229 | keys, |
| 230 | std::bind(&shareddatalayer::SyncStorageImpl::getAck, |
| 231 | this, |
| 232 | std::placeholders::_1, |
| 233 | std::placeholders::_2)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 234 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 235 | verifyBackendResponse(); |
| 236 | return localMap; |
| 237 | } |
| 238 | |
| 239 | void SyncStorageImpl::remove(const Namespace& ns, const Keys& keys) |
| 240 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 241 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 242 | waitSdlToBeReady(ns); |
| 243 | synced = false; |
| 244 | asyncStorage->removeAsync(ns, |
| 245 | keys, |
| 246 | std::bind(&shareddatalayer::SyncStorageImpl::modifyAck, |
| 247 | this, |
| 248 | std::placeholders::_1)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 249 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 250 | verifyBackendResponse(); |
| 251 | } |
| 252 | |
| 253 | bool SyncStorageImpl::removeIf(const Namespace& ns, const Key& key, const Data& data) |
| 254 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 255 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 256 | waitSdlToBeReady(ns); |
| 257 | synced = false; |
| 258 | asyncStorage->removeIfAsync(ns, |
| 259 | key, |
| 260 | data, |
| 261 | std::bind(&shareddatalayer::SyncStorageImpl::modifyIfAck, |
| 262 | this, |
| 263 | std::placeholders::_1, |
| 264 | std::placeholders::_2)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 265 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 266 | verifyBackendResponse(); |
| 267 | return localStatus; |
| 268 | } |
| 269 | |
| 270 | SyncStorageImpl::Keys SyncStorageImpl::findKeys(const Namespace& ns, const std::string& keyPrefix) |
| 271 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 272 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 273 | waitSdlToBeReady(ns); |
| 274 | synced = false; |
| 275 | asyncStorage->findKeysAsync(ns, |
| 276 | keyPrefix, |
| 277 | std::bind(&shareddatalayer::SyncStorageImpl::findKeysAck, |
| 278 | this, |
| 279 | std::placeholders::_1, |
| 280 | std::placeholders::_2)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 281 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 282 | verifyBackendResponse(); |
| 283 | return localKeys; |
| 284 | } |
| 285 | |
Petri Ovaska | 63869e1 | 2021-09-17 11:54:21 +0300 | [diff] [blame] | 286 | SyncStorageImpl::Keys SyncStorageImpl::listKeys(const Namespace& ns, const std::string& pattern) |
| 287 | { |
| 288 | handlePendingEvents(); |
| 289 | waitSdlToBeReady(ns); |
| 290 | synced = false; |
| 291 | asyncStorage->listKeys(ns, |
| 292 | pattern, |
| 293 | std::bind(&shareddatalayer::SyncStorageImpl::findKeysAck, |
| 294 | this, |
| 295 | std::placeholders::_1, |
| 296 | std::placeholders::_2)); |
| 297 | waitForOperationCallback(); |
| 298 | verifyBackendResponse(); |
| 299 | return localKeys; |
| 300 | } |
| 301 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 302 | void SyncStorageImpl::removeAll(const Namespace& ns) |
| 303 | { |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 304 | handlePendingEvents(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 305 | waitSdlToBeReady(ns); |
| 306 | synced = false; |
| 307 | asyncStorage->removeAllAsync(ns, |
| 308 | std::bind(&shareddatalayer::SyncStorageImpl::modifyAck, |
| 309 | this, |
| 310 | std::placeholders::_1)); |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 311 | waitForOperationCallback(); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 312 | verifyBackendResponse(); |
| 313 | } |
Timo Tietavainen | faf9fc7 | 2021-08-05 11:46:07 +0300 | [diff] [blame] | 314 | |
| 315 | void SyncStorageImpl::handlePendingEvents() |
| 316 | { |
| 317 | int pollRetVal = system.poll(&events, 1, 0); |
| 318 | |
| 319 | while (pollRetVal > 0 && events.revents & POLLIN) |
| 320 | { |
| 321 | asyncStorage->handleEvents(); |
| 322 | pollRetVal = system.poll(&events, 1, 0); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void SyncStorageImpl::setOperationTimeout(const std::chrono::steady_clock::duration& timeout) |
| 327 | { |
| 328 | operationTimeout = timeout; |
| 329 | } |