1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef KVSTORE_SYNC_MANAGER_H 17 #define KVSTORE_SYNC_MANAGER_H 18 19 #include <atomic> 20 #include <list> 21 #include <map> 22 23 #include "task_scheduler.h" 24 #include "kv_store_nb_delegate.h" 25 #include "types.h" 26 27 namespace OHOS { 28 namespace DistributedKv { 29 class API_EXPORT KvStoreSyncManager { 30 public: 31 static constexpr uint32_t SYNC_DEFAULT_DELAY_MS = 1000; 32 static constexpr uint32_t SYNC_MIN_DELAY_MS = 100; 33 static constexpr uint32_t SYNC_MAX_DELAY_MS = 1000 * 3600 * 24; // 24hours 34 static constexpr uint32_t SYNC_RETRY_MAX_COUNT = 3; GetInstance()35 static KvStoreSyncManager *GetInstance() 36 { 37 static KvStoreSyncManager syncManager; 38 return &syncManager; 39 } 40 using TimePoint = std::chrono::steady_clock::time_point; 41 using SyncEnd = std::function<void(const std::map<std::string, DistributedDB::DBStatus> &)>; 42 using SyncFunc = std::function<Status(const SyncEnd &)>; 43 44 struct KvSyncOperation { 45 uintptr_t syncId = 0; 46 uint32_t opSeq = 0; 47 uint32_t delayMs = 0; 48 SyncFunc syncFunc; 49 SyncEnd syncEnd; 50 TimePoint beginTime; 51 }; 52 using OpPred = std::function<bool(KvSyncOperation &)>; 53 Status AddSyncOperation(uintptr_t syncId, uint32_t delayMs, const SyncFunc &syncFunc, const SyncEnd &syncEnd); 54 Status RemoveSyncOperation(uintptr_t syncId); 55 56 private: 57 KvStoreSyncManager(); 58 ~KvStoreSyncManager(); 59 60 uint32_t GetExpireTimeRange(uint32_t delayMs) const; 61 uint32_t DoRemoveSyncingOp(OpPred pred, std::list<KvSyncOperation> &syncingOps); 62 Status RemoveSyncingOp(uint32_t opSeq, std::list<KvSyncOperation> &syncingOps); 63 void AddTimer(const TimePoint &expireTime); 64 bool GetTimeoutSyncOps(const TimePoint &time, std::list<KvSyncOperation> &syncOps); 65 void DoCheckSyncingTimeout(std::list<KvSyncOperation> &syncingOps); 66 void Schedule(const TimePoint &expireTime); 67 68 static constexpr uint32_t SYNCING_TIMEOUT_MS = 5000; 69 static constexpr uint32_t REALTIME_PRIOR_SYNCING_MS = 300; 70 static constexpr uint32_t DELAY_TIME_RANGE_DIVISOR = 4; 71 72 mutable std::mutex syncOpsMutex_; 73 std::list<KvSyncOperation> realtimeSyncingOps_; 74 std::list<KvSyncOperation> delaySyncingOps_; 75 std::multimap<TimePoint, KvSyncOperation> scheduleSyncOps_; 76 77 TaskScheduler syncScheduler_ { "sync_mgr" }; 78 TimePoint nextScheduleTime_; 79 std::atomic_uint32_t syncOpSeq_ = 0; 80 }; 81 } // namespace DistributedKv 82 } // namespace OHOS 83 #endif // KVSTORE_SYNC_MANAGER_H 84