• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "online_sync_table.h"
17 
18 #include "device_profile_errors.h"
19 #include "device_profile_log.h"
20 #include "device_profile_utils.h"
21 #include "dp_device_manager.h"
22 #include "sync_coordinator.h"
23 
24 namespace OHOS {
25 namespace DeviceProfile {
26 using namespace OHOS::DistributedKv;
27 
28 namespace {
29 const std::string TAG = "OnlineSyncTable";
30 
31 const std::string APP_ID = "distributed_device_profile_service";
32 const std::string STORE_ID = "online_sync_storage";
33 constexpr int32_t MAX_RETRY_SYNC_TIMES = 30;
34 constexpr int32_t INTERVAL_RETRY_SYNC_MS = 100;
35 }
36 
OnlineSyncTable()37 OnlineSyncTable::OnlineSyncTable() : DeviceProfileStorage(APP_ID, STORE_ID)
38 {
39 }
40 
Init()41 void OnlineSyncTable::Init()
42 {
43     HILOGD("called");
44     Options options = {
45         .createIfMissing = true,
46         .encrypt = true,
47         .autoSync = true,
48         .kvStoreType = KvStoreType::SINGLE_VERSION,
49         .area = 1,
50         .baseDir = "/data/service/el1/public/database/distributed_device_profile_service"
51     };
52     // clean the IMMEDIATE_SYNC_ON_CHANGE
53     options.policies = {};
54     SyncPolicy syncPolicy {
55         .type = PolicyType::IMMEDIATE_SYNC_ON_ONLINE
56     };
57     options.policies.emplace_back(syncPolicy);
58     SetOptions(options);
59     DeviceProfileStorage::Init();
60     int32_t errCode = DeviceProfileStorage::RegisterSyncCallback(shared_from_this());
61     if (errCode != ERR_OK) {
62         HILOGE("register sync callback failed, errCode = %{public}d", errCode);
63     }
64 }
65 
RegisterSyncCallback(const std::shared_ptr<KvStoreSyncCallback> & syncCb)66 int32_t OnlineSyncTable::RegisterSyncCallback(const std::shared_ptr<KvStoreSyncCallback>& syncCb)
67 {
68     HILOGI("called");
69     if (syncCb == nullptr) {
70         return ERR_DP_INVALID_PARAMS;
71     }
72     std::lock_guard<std::mutex> autoLock(tableLock_);
73     syncCallback_ = syncCb;
74     return ERR_OK;
75 }
76 
UnRegisterSyncCallback()77 int32_t  OnlineSyncTable::UnRegisterSyncCallback()
78 {
79     HILOGI("called");
80     std::lock_guard<std::mutex> autoLock(tableLock_);
81     syncCallback_ = nullptr;
82     return ERR_OK;
83 }
84 
SyncDeviceProfile(const std::vector<std::string> & deviceIds,SyncMode syncMode)85 int32_t OnlineSyncTable::SyncDeviceProfile(const std::vector<std::string>& deviceIds,
86     SyncMode syncMode)
87 {
88     HILOGI("called");
89     auto syncTask = [this, deviceIds = std::move(deviceIds), syncMode]() {
90         HILOGI("start sync task");
91         retrySyncTimes_ = 0;
92         int32_t errCode = DeviceProfileStorage::SyncDeviceProfile(deviceIds, syncMode);
93         if (errCode != ERR_OK) {
94             SyncCoordinator::GetInstance().ReleaseSync();
95             HILOGE("sync errCode = %{public}d", errCode);
96         }
97     };
98     if (!SyncCoordinator::GetInstance().DispatchSyncTask(syncTask)) {
99         HILOGE("post online sync task failed");
100         return ERR_DP_POST_TASK_FAILED;
101     }
102     return ERR_OK;
103 }
104 
SyncCompleted(const std::map<std::string,Status> & results)105 void OnlineSyncTable::SyncCompleted(const std::map<std::string, Status>& results)
106 {
107     if (!SyncCoordinator::GetInstance().IsOnlineSync()) {
108         HILOGI("manual sync callback");
109         NotifySyncCompleted(results);
110         return;
111     }
112 
113     HILOGI("online sync callback");
114     std::vector<std::string> failedDeviceIds;
115     for (const auto& [deviceId, result] : results) {
116         HILOGI("deviceId = %{public}s, result = %{public}d",
117             DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str(), result);
118         if (result != Status::SUCCESS) {
119             std::string networkId;
120             if (!DpDeviceManager::GetInstance().TransformDeviceId(deviceId, networkId,
121                 DeviceIdType::NETWORKID)) {
122                 HILOGE("transform to networkid failed");
123                 continue;
124             }
125             failedDeviceIds.emplace_back(std::move(networkId));
126         }
127     }
128 
129     HILOGI("retry times = %{public}d", retrySyncTimes_.load());
130     if ((retrySyncTimes_++ < MAX_RETRY_SYNC_TIMES) && !failedDeviceIds.empty()) {
131         auto retrySyncTask = [this, deviceIds = std::move(failedDeviceIds)]() {
132             HILOGI("retrying sync...");
133             DeviceProfileStorage::SyncDeviceProfile(deviceIds, SyncMode::PUSH);
134         };
135         if (SyncCoordinator::GetInstance().DispatchSyncTask(retrySyncTask,
136             INTERVAL_RETRY_SYNC_MS)) {
137             return;
138         } else {
139             HILOGE("post online sync retry task failed");
140         }
141     }
142     // notify and release when there is no retry
143     NotifySyncCompleted(results);
144     SyncCoordinator::GetInstance().ReleaseSync();
145 }
146 
NotifySyncCompleted(const std::map<std::string,Status> & results)147 void OnlineSyncTable::NotifySyncCompleted(const std::map<std::string, Status>& results)
148 {
149     std::lock_guard<std::mutex> autoLock(tableLock_);
150     if (syncCallback_ != nullptr) {
151         HILOGI("notify sync callback");
152         syncCallback_->SyncCompleted(results);
153     }
154 }
155 } // namespace DeviceProfile
156 } // namespace OHOS