• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
20 #include <list>
21 #include "types.h"
22 #include "kv_scheduler.h"
23 #include "kv_store_nb_delegate.h"
24 
25 namespace OHOS {
26 namespace DistributedKv {
27 class KvStoreSyncManager {
28 public:
29     static constexpr uint32_t SYNC_DEFAULT_DELAY_MS = 1000;
30     static constexpr uint32_t SYNC_MIN_DELAY_MS = 100;
31     static constexpr uint32_t SYNC_MAX_DELAY_MS = 1000 * 3600 * 24;  // 24hours
32     static constexpr uint32_t SYNC_RETRY_MAX_COUNT = 3;
GetInstance()33     static KvStoreSyncManager *GetInstance()
34     {
35         static KvStoreSyncManager syncManager;
36         return &syncManager;
37     }
38     using TimePoint = std::chrono::system_clock::time_point;
39     using SyncEnd = std::function<void(const std::map<std::string, DistributedDB::DBStatus> &)>;
40     using SyncFunc = std::function<Status(const SyncEnd &)>;
41 
42     struct KvSyncOperation {
43         uintptr_t syncId{ 0 };
44         uint32_t opSeq{ 0 };
45         uint32_t delayMs{ 0 };
46         SyncFunc syncFunc{};
47         SyncEnd  syncEnd{};
48         TimePoint beginTime{};
49     };
50     using OpPred = std::function<bool(KvSyncOperation &)>;
51     Status AddSyncOperation(uintptr_t syncId, uint32_t delayMs, const SyncFunc &syncFunc,
52         const SyncEnd &syncEnd);
53     Status RemoveSyncOperation(uintptr_t syncId);
54 private:
55     KvStoreSyncManager();
56     ~KvStoreSyncManager();
57 
58     uint32_t GetExpireTimeRange(uint32_t delayMs) const;
59     uint32_t DoRemoveSyncingOp(OpPred pred, std::list<KvSyncOperation> &syncingOps);
60     Status RemoveSyncingOp(uint32_t opSeq, std::list<KvSyncOperation> &syncingOps);
61     void AddTimer(const TimePoint &expireTime);
62     bool GetTimeoutSyncOps(const TimePoint &time, std::list<KvSyncOperation> &syncOps);
63     void DoCheckSyncingTimeout(std::list<KvSyncOperation> &syncingOps);
64     void Schedule(const TimePoint &expireTime);
65 
66     static constexpr uint32_t SYNCING_TIMEOUT_MS = 5000;
67     static constexpr uint32_t REALTIME_PRIOR_SYNCING_MS = 300;
68     static constexpr uint32_t DELAY_TIME_RANGE_DIVISOR = 4;
69 
70     mutable std::mutex syncOpsMutex_{};
71     std::list<KvSyncOperation> realtimeSyncingOps_{};
72     std::list<KvSyncOperation> delaySyncingOps_{};
73     std::multimap<TimePoint, KvSyncOperation> scheduleSyncOps_{};
74 
75     KvScheduler syncScheduler_{};
76     TimePoint nextScheduleTime_{};
77     std::atomic_uint32_t syncOpSeq_{ 0 };
78 };
79 }  // namespace DistributedKv
80 }  // namespace OHOS
81 
82 #endif  // KVSTORE_SYNC_MANAGER_H
83