From 280154bc7e8890457cad49efdbb0afb2e3b0eda4 Mon Sep 17 00:00:00 2001 From: illybyy Date: Mon, 7 Mar 2022 22:09:24 +0800 Subject: [PATCH] increase retry count and do error process while empty device Signed-off-by: illybyy --- .../app/src/kvstore_data_service.cpp | 66 +++++++++++++------ .../app/src/kvstore_data_service.h | 2 + 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 7733dfee8..684d1160b 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -186,26 +186,16 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGI("begin."); - if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType() - || options.kvStoreType == KvStoreType::MULTI_VERSION) { - ZLOGE("invalid argument type."); - return Status::INVALID_ARGUMENT; - } - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); KvStoreParam param; - param.bundleName = appId.appId; - param.storeId = storeId.storeId; - const int32_t uid = IPCSkeleton::GetCallingUid(); - param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, uid); - if (param.trueAppId.empty()) { - ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), uid); - return Status::PERMISSION_DENIED; + Status status = FillStoreParam(options, appId, storeId, param); + if (status != Status::SUCCESS) { + callback(nullptr); + return status; } - param.userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid); SecretKeyPara keyPara; - Status status = KvStoreDataService::GetSecretKey(options, param, keyPara); + status = KvStoreDataService::GetSecretKey(options, param, keyPara); if (status != Status::SUCCESS) { callback(nullptr); return status; @@ -223,14 +213,21 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId it = result.first; } sptr store; - param.status = (it->second).GetKvStore(options, param.bundleName, param.storeId, uid, keyPara.secretKey, store); + param.status = + (it->second).GetKvStore(options, param.bundleName, param.storeId, param.uid, keyPara.secretKey, store); if (keyPara.outdated) { KvStoreMetaManager::GetInstance().ReKey(param.userId, param.bundleName, param.storeId, KvStoreAppManager::ConvertPathType(param.uid, param.bundleName, options.securityLevel), store); } if (param.status == Status::SUCCESS) { + status = UpdateMetaData(options, param, keyPara.metaKey, it->second); + if (status != Status::SUCCESS) { + ZLOGE("failed to write meta"); + callback(nullptr); + return status; + } callback(std::move(store)); - return UpdateMetaData(options, param, keyPara.metaKey, it->second); + return status; } param.status = GetSingleKvStoreFailDo(options, param, keyPara, it->second, store); @@ -238,6 +235,28 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId return param.status; } +Status KvStoreDataService::FillStoreParam( + const Options &options, const AppId &appId, const StoreId &storeId, KvStoreParam ¶m) +{ + if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType() + || options.kvStoreType == KvStoreType::MULTI_VERSION) { + ZLOGE("invalid argument type."); + return Status::INVALID_ARGUMENT; + } + param.bundleName = appId.appId; + param.storeId = storeId.storeId; + param.uid = IPCSkeleton::GetCallingUid(); + param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, param.uid); + ZLOGI("%{public}s, %{public}s", param.trueAppId.c_str(), param.bundleName.c_str()); + if (param.trueAppId.empty()) { + ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), param.uid); + return PERMISSION_DENIED; + } + + param.userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(param.uid); + return SUCCESS; +} + Status KvStoreDataService::GetSecretKey(const Options &options, const KvStoreParam &kvParas, SecretKeyPara &secretKeyParas) { @@ -323,12 +342,17 @@ Status KvStoreDataService::RecoverSecretKey(const Status &alreadyCreated, bool & Status KvStoreDataService::UpdateMetaData(const Options &options, const KvStoreParam &kvParas, const std::vector &metaKey, KvStoreUserManager &kvStoreUserManager) { + auto localDeviceId = DeviceKvStoreImpl::GetLocalDeviceId(); + if (localDeviceId.empty()) { + ZLOGE("failed to get local device id"); + return Status::ERROR; + } KvStoreMetaData metaData; metaData.appId = kvParas.trueAppId; metaData.appType = "harmony"; metaData.bundleName = kvParas.bundleName; metaData.deviceAccountId = kvParas.userId; - metaData.deviceId = DeviceKvStoreImpl::GetLocalDeviceId(); + metaData.deviceId = localDeviceId; metaData.isAutoSync = options.autoSync; metaData.isBackup = options.backup; metaData.isEncrypt = options.encrypt; @@ -816,12 +840,12 @@ void KvStoreDataService::OnStart() void KvStoreDataService::StartService() { static constexpr int32_t RETRY_TIMES = 10; - static constexpr int32_t RETRY_INTERVAL = 500; // unit is ms + static constexpr int32_t RETRY_INTERVAL = 500 * 1000; // unit is ms for (BlockInteger retry(RETRY_INTERVAL); retry < RETRY_TIMES; ++retry) { if (!DeviceKvStoreImpl::GetLocalDeviceId().empty()) { break; } - ZLOGE("GetLocalDeviceId failed, reties: %{public}d", static_cast(retry)); + ZLOGE("GetLocalDeviceId failed, retry count:%{public}d", static_cast(retry)); } // register this to ServiceManager. @@ -960,7 +984,7 @@ bool KvStoreDataService::CheckPermissions(const std::string &userId, const std:: return true; } bool ret = PermissionValidator::CheckSyncPermission(userId, appId, metaData.uid); - ZLOGD("checking sync permission ret:%d.", ret); + ZLOGD("checking sync permission ret:%{public}d.", ret); return ret; } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index f62715443..766935805 100755 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -167,6 +167,8 @@ private: bool CheckOptions(const Options &options, const std::vector &metaKey) const; void CreateRdbService(); + static Status FillStoreParam( + const Options &options, const AppId &appId, const StoreId &storeId, KvStoreParam ¶m); static constexpr int TEN_SEC = 10; -- Gitee