From 541cb26582ec1e593727657d0696327a24c2a715 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Fri, 27 Jun 2025 17:04:27 +0800 Subject: [PATCH 1/5] fix db corruption Signed-off-by: wenjinchao Change-Id: I999a964bdfe6e4b4f990372388ffc9e02304d647 --- .../distributeddataservice/service/test/BUILD.gn | 6 ++++++ .../service/udmf/store/runtime_store.cpp | 5 +++++ .../service/udmf/store/runtime_store.h | 3 +++ .../service/udmf/store/store_cache.cpp | 14 ++++++++++++++ .../service/udmf/store/store_cache.h | 1 + .../service/udmf/udmf_service_impl.cpp | 14 ++++++++++++++ 6 files changed, 43 insertions(+) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index e49bc9089..f4446f4d8 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1290,6 +1290,12 @@ ohos_unittest("DumpHelperTest") { } ohos_unittest("UdmfServiceImplTest") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "${datamgr_service_path}/cfi_blocklist.txt" + } module_out_path = module_output_path sources = [ "${data_service_path}/framework/feature/feature_system.cpp", diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.cpp b/services/distributeddataservice/service/udmf/store/runtime_store.cpp index d13e1adec..bd1a27e68 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.cpp +++ b/services/distributeddataservice/service/udmf/store/runtime_store.cpp @@ -433,6 +433,11 @@ bool RuntimeStore::Init() delegate = nbDelegate; status = dbStatus; }); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGE("GetKvStore fail, database corrupted, status: %{public}d.", static_cast(status)); + delegateManager_->DeleteKvStore(storeId_); + return false; + } if (status != DBStatus::OK) { ZLOGE("GetKvStore fail, status: %{public}d.", static_cast(status)); return false; diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.h b/services/distributeddataservice/service/udmf/store/runtime_store.h index ef9c37e36..cc0c13499 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.h +++ b/services/distributeddataservice/service/udmf/store/runtime_store.h @@ -16,6 +16,7 @@ #ifndef UDMF_RUNTIMESTORE_H #define UDMF_RUNTIMESTORE_H +#include "error_code.h" #include "store.h" #include "kv_store_delegate_manager.h" #include "metadata/store_meta_data.h" @@ -65,6 +66,8 @@ private: void NotifySyncProcss(const DevSyncProcessMap &processMap, ProcessCallback callback, const DevNameMap &deviceNameMap); Status PutSummary(const UnifiedData &data, std::vector &entries); + Status DeleteKvDb(); + }; } // namespace UDMF } // namespace OHOS diff --git a/services/distributeddataservice/service/udmf/store/store_cache.cpp b/services/distributeddataservice/service/udmf/store/store_cache.cpp index 545b549a1..14c7a3ac3 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.cpp +++ b/services/distributeddataservice/service/udmf/store/store_cache.cpp @@ -94,6 +94,20 @@ void StoreCache::CloseStores() stores_.Clear(); } +void StoreCache::RemoveStore(std::string intention) +{ + ZLOGI("RemoveStore, intention:%{public}s", intention.c_str()); + int foregroundUserId = 0; + bool ret = DistributedData::AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId); + if (!ret) { + ZLOGE("QueryForegroundUserId failed."); + return; + } + std::string key = intention; + key.append(std::to_string(foregroundUserId)); + stores_.Erase(key); +} + bool StoreCache::IsValidIntention(const std::string &intention) { return UnifiedDataUtils::GetIntentionByString(intention) != UD_INTENTION_BUTT; diff --git a/services/distributeddataservice/service/udmf/store/store_cache.h b/services/distributeddataservice/service/udmf/store/store_cache.h index 5d3918d2d..b71481dbe 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.h +++ b/services/distributeddataservice/service/udmf/store/store_cache.h @@ -28,6 +28,7 @@ public: static StoreCache &GetInstance(); void SetThreadPool(std::shared_ptr executors); void CloseStores(); + void RemoveStore(std::string intention); private: StoreCache() {} diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index a3ea7ce7e..ec80f18a6 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -158,6 +158,7 @@ int32_t UdmfServiceImpl::SaveData(CustomOption &option, UnifiedData &unifiedData if (store->Put(unifiedData) != E_OK) { ZLOGE("Put unified data failed:%{public}s", intention.c_str()); + StoreCache::GetInstance().RemoveStore(intention); return E_DB_ERROR; } key = unifiedData.GetRuntime()->key.GetUnifiedKey(); @@ -223,6 +224,7 @@ int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &uni int32_t res = store->Get(query.key, unifiedData); if (res != E_OK) { ZLOGE("Get data failed,res:%{public}d,key:%{public}s", res, query.key.c_str()); + StoreCache::GetInstance().RemoveStore(key.intention); return res; } @@ -420,6 +422,7 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi int32_t res = store->Get(query.key, data); if (res != E_OK) { ZLOGE("Get data failed:%{public}s", key.intention.c_str()); + StoreCache::GetInstance().RemoveStore(key.intention); return res; } if (data.IsEmpty()) { @@ -439,6 +442,7 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi PreProcessUtils::SetRecordUid(unifiedData); if (store->Update(unifiedData) != E_OK) { ZLOGE("Unified data update failed:%{public}s", key.intention.c_str()); + StoreCache::GetInstance().RemoveStore(key.intention); return E_DB_ERROR; } return E_OK; @@ -487,6 +491,7 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vectorDeleteBatch(deleteKeys) != E_OK) { ZLOGE("Remove data failed."); + StoreCache::GetInstance().RemoveStore(key.intention); return E_DB_ERROR; } return E_OK; @@ -509,6 +514,7 @@ int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary) if (store->GetSummary(key, summary) != E_OK) { ZLOGE("Store get summary failed:%{public}s", key.intention.c_str()); + StoreCache::GetInstance().RemoveStore(key.intention); return E_DB_ERROR; } return E_OK; @@ -553,12 +559,14 @@ int32_t UdmfServiceImpl::AddPrivilege(const QueryOption &query, Privilege &privi } if (res != E_OK) { ZLOGE("Get runtime failed, res:%{public}d, key:%{public}s.", res, query.key.c_str()); + StoreCache::GetInstance().RemoveStore(key.intention); return res; } runtime.privileges.emplace_back(privilege); res = store->PutRuntime(query.key, runtime); if (res != E_OK) { ZLOGE("Update runtime failed, res:%{public}d, key:%{public}s", res, query.key.c_str()); + StoreCache::GetInstance().RemoveStore(key.intention); } return res; } @@ -670,6 +678,7 @@ int32_t UdmfServiceImpl::IsRemoteData(const QueryOption &query, bool &result) auto res = store->GetRuntime(query.key, runtime); if (res != E_OK) { ZLOGE("Get runtime failed, res:%{public}d, key:%{public}s.", res, query.key.c_str()); + StoreCache::GetInstance().RemoveStore(key.intention); return E_DB_ERROR; } @@ -709,6 +718,7 @@ int32_t UdmfServiceImpl::SetAppShareOption(const std::string &intention, int32_t if (store->PutLocal(std::to_string(accessTokenIDEx), ShareOptionsUtil::GetEnumStr(shareOption)) != E_OK) { ZLOGE("Store get unifiedData failed:%{public}d", shareOption); + StoreCache::GetInstance().RemoveStore(intention); return E_DB_ERROR; } return E_OK; @@ -730,6 +740,7 @@ int32_t UdmfServiceImpl::GetAppShareOption(const std::string &intention, int32_t int32_t ret = store->GetLocal(std::to_string(accessTokenIDEx), appShareOption); if (ret != E_OK) { ZLOGW("GetLocal failed:%{public}s", intention.c_str()); + StoreCache::GetInstance().RemoveStore(intention); return ret; } ZLOGI("GetLocal ok intention:%{public}s,appShareOption:%{public}s", intention.c_str(), appShareOption.c_str()); @@ -759,6 +770,7 @@ int32_t UdmfServiceImpl::RemoveAppShareOption(const std::string &intention) UnifiedData unifiedData; if (store->DeleteLocal(std::to_string(accessTokenIDEx)) != E_OK) { ZLOGE("Store DeleteLocal failed:%{public}s", intention.c_str()); + StoreCache::GetInstance().RemoveStore(intention); return E_DB_ERROR; } return E_OK; @@ -806,6 +818,7 @@ int32_t UdmfServiceImpl::QueryDataCommon( } if (store->GetBatchData(dataPrefix, dataSet) != E_OK) { ZLOGE("Get dataSet failed, dataPrefix: %{public}s.", dataPrefix.c_str()); + StoreCache::GetInstance().RemoveStore(intention); return E_DB_ERROR; } return E_OK; @@ -1092,6 +1105,7 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptrPutSummary(udkey, summary) != E_OK) { ZLOGE("Put summary failed:%{public}s", key.c_str()); + StoreCache::GetInstance().RemoveStore(UD_INTENTION_MAP.at(UD_INTENTION_DRAG)); return E_DB_ERROR; } return E_OK; -- Gitee From df864f804e730e12dbc6d8a2c30177543180dddc Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Sat, 28 Jun 2025 14:54:28 +0800 Subject: [PATCH 2/5] add Signed-off-by: wenjinchao Change-Id: Ic979f1f8ffa0d4e366660bc913ca9ecc168f0337 --- .../service/udmf/store/runtime_store.cpp | 9 ++++ .../service/udmf/store/runtime_store.h | 3 +- .../service/udmf/store/store_cache.cpp | 48 ++++++++++++------- .../service/udmf/store/store_cache.h | 4 +- .../service/udmf/udmf_service_impl.cpp | 28 +++++------ 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.cpp b/services/distributeddataservice/service/udmf/store/runtime_store.cpp index bd1a27e68..7af5e6dbe 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.cpp +++ b/services/distributeddataservice/service/udmf/store/runtime_store.cpp @@ -584,5 +584,14 @@ Status RuntimeStore::DeleteEntries(const std::vector &keys) return E_OK; } +Status RuntimeStore::DeleteKvDb() +{ + DBStatus status = delegateManager_->DeleteKvStore(storeId_); + if (status != DBStatus::OK) { + ZLOGE("DeleteKvStore failed, status: %{public}d.", status); + return E_DB_ERROR; + } + return E_OK; +} } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.h b/services/distributeddataservice/service/udmf/store/runtime_store.h index cc0c13499..572084e0e 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.h +++ b/services/distributeddataservice/service/udmf/store/runtime_store.h @@ -16,7 +16,6 @@ #ifndef UDMF_RUNTIMESTORE_H #define UDMF_RUNTIMESTORE_H -#include "error_code.h" #include "store.h" #include "kv_store_delegate_manager.h" #include "metadata/store_meta_data.h" @@ -47,6 +46,7 @@ public: Status PutSummary(UnifiedKey &key, const Summary &summary) override; void Close() override; bool Init() override; + Status DeleteKvDb(); private: static constexpr const char *DATA_PREFIX = "udmf://"; @@ -66,7 +66,6 @@ private: void NotifySyncProcss(const DevSyncProcessMap &processMap, ProcessCallback callback, const DevNameMap &deviceNameMap); Status PutSummary(const UnifiedData &data, std::vector &entries); - Status DeleteKvDb(); }; } // namespace UDMF diff --git a/services/distributeddataservice/service/udmf/store/store_cache.cpp b/services/distributeddataservice/service/udmf/store/store_cache.cpp index 14c7a3ac3..e55036fb6 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.cpp +++ b/services/distributeddataservice/service/udmf/store/store_cache.cpp @@ -12,6 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include +#include +#include #define LOG_TAG "StoreCache" #include "store_cache.h" @@ -38,25 +41,27 @@ std::shared_ptr StoreCache::GetStore(std::string intention) } std::string key = intention; key.append(std::to_string(foregroundUserId)); - - stores_.Compute(key, [&store, intention](const auto &key, std::shared_ptr &storePtr) -> bool { - if (storePtr != nullptr) { - store = storePtr; - return true; - } - - if (IsValidIntention(intention)) { - storePtr = std::make_shared(intention); - if (!storePtr->Init()) { - ZLOGE("Init runtime store failed."); - return false; + { + std::shared_lock storeLock(storeMutex_); + stores_.Compute(key, [&store, intention](const auto &key, std::shared_ptr &storePtr) -> bool { + if (storePtr != nullptr) { + store = storePtr; + return true; } - store = storePtr; - return true; - } - return false; - }); + if (IsValidIntention(intention)) { + storePtr = std::make_shared(intention); + if (!storePtr->Init()) { + ZLOGE("Init runtime store failed."); + return false; + } + store = storePtr; + return true; + } + return false; + }); + } + std::unique_lock lock(taskMutex_); if (taskId_ == ExecutorPool::INVALID_TASK_ID && executorPool_ != nullptr) { taskId_ = executorPool_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&StoreCache::GarbageCollect, this)); @@ -94,9 +99,16 @@ void StoreCache::CloseStores() stores_.Clear(); } -void StoreCache::RemoveStore(std::string intention) +void StoreCache::RemoveStore(std::string intention, std::shared_ptr store) { + std::unique_lock lock(storeMutex_); ZLOGI("RemoveStore, intention:%{public}s", intention.c_str()); + if (store == nullptr) { + ZLOGE("store is nullptr"); + return; + } + store->Close(); + (static_cast(store.get()))->DeleteKvDb(); int foregroundUserId = 0; bool ret = DistributedData::AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId); if (!ret) { diff --git a/services/distributeddataservice/service/udmf/store/store_cache.h b/services/distributeddataservice/service/udmf/store/store_cache.h index b71481dbe..30f8092ae 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.h +++ b/services/distributeddataservice/service/udmf/store/store_cache.h @@ -19,6 +19,7 @@ #include "concurrent_map.h" #include "executor_pool.h" #include "store.h" +#include namespace OHOS { namespace UDMF { @@ -28,7 +29,7 @@ public: static StoreCache &GetInstance(); void SetThreadPool(std::shared_ptr executors); void CloseStores(); - void RemoveStore(std::string intention); + void RemoveStore(std::string intention, std::shared_ptr store); private: StoreCache() {} @@ -43,6 +44,7 @@ private: ConcurrentMap> stores_; std::mutex taskMutex_; + std::shared_mutex storeMutex_; ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID; static constexpr int64_t INTERVAL = 1; // 1 min diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index ec80f18a6..d52db8a77 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -158,7 +158,7 @@ int32_t UdmfServiceImpl::SaveData(CustomOption &option, UnifiedData &unifiedData if (store->Put(unifiedData) != E_OK) { ZLOGE("Put unified data failed:%{public}s", intention.c_str()); - StoreCache::GetInstance().RemoveStore(intention); + StoreCache::GetInstance().RemoveStore(intention, store); return E_DB_ERROR; } key = unifiedData.GetRuntime()->key.GetUnifiedKey(); @@ -224,7 +224,7 @@ int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &uni int32_t res = store->Get(query.key, unifiedData); if (res != E_OK) { ZLOGE("Get data failed,res:%{public}d,key:%{public}s", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); return res; } @@ -422,7 +422,7 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi int32_t res = store->Get(query.key, data); if (res != E_OK) { ZLOGE("Get data failed:%{public}s", key.intention.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); return res; } if (data.IsEmpty()) { @@ -442,7 +442,7 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi PreProcessUtils::SetRecordUid(unifiedData); if (store->Update(unifiedData) != E_OK) { ZLOGE("Unified data update failed:%{public}s", key.intention.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); return E_DB_ERROR; } return E_OK; @@ -491,7 +491,7 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vectorDeleteBatch(deleteKeys) != E_OK) { ZLOGE("Remove data failed."); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); return E_DB_ERROR; } return E_OK; @@ -514,7 +514,7 @@ int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary) if (store->GetSummary(key, summary) != E_OK) { ZLOGE("Store get summary failed:%{public}s", key.intention.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); return E_DB_ERROR; } return E_OK; @@ -559,14 +559,14 @@ int32_t UdmfServiceImpl::AddPrivilege(const QueryOption &query, Privilege &privi } if (res != E_OK) { ZLOGE("Get runtime failed, res:%{public}d, key:%{public}s.", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); return res; } runtime.privileges.emplace_back(privilege); res = store->PutRuntime(query.key, runtime); if (res != E_OK) { ZLOGE("Update runtime failed, res:%{public}d, key:%{public}s", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); } return res; } @@ -678,7 +678,7 @@ int32_t UdmfServiceImpl::IsRemoteData(const QueryOption &query, bool &result) auto res = store->GetRuntime(query.key, runtime); if (res != E_OK) { ZLOGE("Get runtime failed, res:%{public}d, key:%{public}s.", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention); + StoreCache::GetInstance().RemoveStore(key.intention, store); return E_DB_ERROR; } @@ -718,7 +718,7 @@ int32_t UdmfServiceImpl::SetAppShareOption(const std::string &intention, int32_t if (store->PutLocal(std::to_string(accessTokenIDEx), ShareOptionsUtil::GetEnumStr(shareOption)) != E_OK) { ZLOGE("Store get unifiedData failed:%{public}d", shareOption); - StoreCache::GetInstance().RemoveStore(intention); + StoreCache::GetInstance().RemoveStore(intention, store); return E_DB_ERROR; } return E_OK; @@ -740,7 +740,7 @@ int32_t UdmfServiceImpl::GetAppShareOption(const std::string &intention, int32_t int32_t ret = store->GetLocal(std::to_string(accessTokenIDEx), appShareOption); if (ret != E_OK) { ZLOGW("GetLocal failed:%{public}s", intention.c_str()); - StoreCache::GetInstance().RemoveStore(intention); + StoreCache::GetInstance().RemoveStore(intention, store); return ret; } ZLOGI("GetLocal ok intention:%{public}s,appShareOption:%{public}s", intention.c_str(), appShareOption.c_str()); @@ -770,7 +770,7 @@ int32_t UdmfServiceImpl::RemoveAppShareOption(const std::string &intention) UnifiedData unifiedData; if (store->DeleteLocal(std::to_string(accessTokenIDEx)) != E_OK) { ZLOGE("Store DeleteLocal failed:%{public}s", intention.c_str()); - StoreCache::GetInstance().RemoveStore(intention); + StoreCache::GetInstance().RemoveStore(intention, store); return E_DB_ERROR; } return E_OK; @@ -818,7 +818,7 @@ int32_t UdmfServiceImpl::QueryDataCommon( } if (store->GetBatchData(dataPrefix, dataSet) != E_OK) { ZLOGE("Get dataSet failed, dataPrefix: %{public}s.", dataPrefix.c_str()); - StoreCache::GetInstance().RemoveStore(intention); + StoreCache::GetInstance().RemoveStore(intention, store); return E_DB_ERROR; } return E_OK; @@ -1105,7 +1105,7 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptrPutSummary(udkey, summary) != E_OK) { ZLOGE("Put summary failed:%{public}s", key.c_str()); - StoreCache::GetInstance().RemoveStore(UD_INTENTION_MAP.at(UD_INTENTION_DRAG)); + StoreCache::GetInstance().RemoveStore(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), store); return E_DB_ERROR; } return E_OK; -- Gitee From ba86e536a80d04228d7c829a3cf856992ddda937 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Sat, 28 Jun 2025 16:42:11 +0800 Subject: [PATCH 3/5] add Signed-off-by: wenjinchao Change-Id: Ibf256ec9bed425e3d4dce4b154c1f692e19ecd02 --- .../service/udmf/store/runtime_store.cpp | 42 +++++++- .../service/udmf/store/store_cache.cpp | 11 +-- .../service/udmf/store/store_cache.h | 2 +- .../service/udmf/udmf_service_impl.cpp | 97 +++++++++++++------ 4 files changed, 110 insertions(+), 42 deletions(-) diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.cpp b/services/distributeddataservice/service/udmf/store/runtime_store.cpp index 7af5e6dbe..a24f47d9a 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.cpp +++ b/services/distributeddataservice/service/udmf/store/runtime_store.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "error_code.h" #define LOG_TAG "RuntimeStore" #include "runtime_store.h" @@ -60,6 +61,10 @@ Status RuntimeStore::PutLocal(const std::string &key, const std::string &value) auto status = kvStore_->PutLocal(keyBytes, valueBytes); if (status != DBStatus::OK) { ZLOGE("KvStore PutLocal failed, status: %{public}d.", status); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } return E_OK; @@ -73,6 +78,10 @@ Status RuntimeStore::GetLocal(const std::string &key, std::string &value) DBStatus status = kvStore_->GetLocal(keyBytes, valueBytes); if (status != DBStatus::OK && status != DBStatus::NOT_FOUND) { ZLOGE("GetLocal entry failed, key: %{public}s.", key.c_str()); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } if (valueBytes.empty()) { @@ -91,6 +100,10 @@ Status RuntimeStore::DeleteLocal(const std::string &key) DBStatus status = kvStore_->DeleteLocal(keyBytes); if (status != DBStatus::OK && status != DBStatus::NOT_FOUND) { ZLOGE("DeleteLocal failed, key: %{public}s.", key.c_str()); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } return E_OK; @@ -204,6 +217,10 @@ Status RuntimeStore::PutRuntime(const std::string &key, const Runtime &runtime) auto res = kvStore_->Put({key.begin(), key.end()}, value); if (res != OK) { ZLOGE("Put failed, key:%{public}s, status:%{public}d", key.c_str(), res); + if (res == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } return E_OK; @@ -220,6 +237,10 @@ Status RuntimeStore::GetRuntime(const std::string &key, Runtime &runtime) } if (res != OK || value.empty()) { ZLOGE("Get failed, key: %{public}s, status:%{public}d", key.c_str(), res); + if (res == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } auto status = DataHandler::UnmarshalEntries(value, runtime, TAG::TAG_RUNTIME); @@ -435,7 +456,9 @@ bool RuntimeStore::Init() }); if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { ZLOGE("GetKvStore fail, database corrupted, status: %{public}d.", static_cast(status)); - delegateManager_->DeleteKvStore(storeId_); + if (DeleteKvDb() != E_OK) { + ZLOGE("DeleteKvStore fail, status: %{public}d.", static_cast(status)); + } return false; } if (status != DBStatus::OK) { @@ -559,6 +582,10 @@ Status RuntimeStore::GetEntries(const std::string &dataPrefix, std::vectorGetEntries(dbQuery, entries); if (status != DBStatus::OK && status != DBStatus::NOT_FOUND) { ZLOGE("KvStore getEntries failed, status: %{public}d.", static_cast(status)); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } return E_OK; @@ -569,6 +596,10 @@ Status RuntimeStore::PutEntries(const std::vector &entries) DBStatus status = kvStore_->PutBatch(entries); if (status != DBStatus::OK) { ZLOGE("putBatch failed, status: %{public}d.", status); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } return E_OK; @@ -579,6 +610,10 @@ Status RuntimeStore::DeleteEntries(const std::vector &keys) DBStatus status = kvStore_->DeleteBatch(keys); if (status != DBStatus::OK) { ZLOGE("deleteBatch failed, status: %{public}d.", status); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + ZLOGW("Kv database corrupted"); + DeleteKvDb(); + } return E_DB_ERROR; } return E_OK; @@ -586,6 +621,11 @@ Status RuntimeStore::DeleteEntries(const std::vector &keys) Status RuntimeStore::DeleteKvDb() { + ZLOGI("Start to delete kvdb."); + if (kvStore_ == nullptr || delegateManager_->CloseKvStore(kvStore_.get()) != DBStatus::OK) { + ZLOGE("Kv store is null or CloseKvStore failed."); + return E_DB_ERROR; + } DBStatus status = delegateManager_->DeleteKvStore(storeId_); if (status != DBStatus::OK) { ZLOGE("DeleteKvStore failed, status: %{public}d.", status); diff --git a/services/distributeddataservice/service/udmf/store/store_cache.cpp b/services/distributeddataservice/service/udmf/store/store_cache.cpp index e55036fb6..448b96317 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.cpp +++ b/services/distributeddataservice/service/udmf/store/store_cache.cpp @@ -99,19 +99,12 @@ void StoreCache::CloseStores() stores_.Clear(); } -void StoreCache::RemoveStore(std::string intention, std::shared_ptr store) +void StoreCache::RemoveStore(std::string intention) { std::unique_lock lock(storeMutex_); ZLOGI("RemoveStore, intention:%{public}s", intention.c_str()); - if (store == nullptr) { - ZLOGE("store is nullptr"); - return; - } - store->Close(); - (static_cast(store.get()))->DeleteKvDb(); int foregroundUserId = 0; - bool ret = DistributedData::AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId); - if (!ret) { + if (!DistributedData::AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId)) { ZLOGE("QueryForegroundUserId failed."); return; } diff --git a/services/distributeddataservice/service/udmf/store/store_cache.h b/services/distributeddataservice/service/udmf/store/store_cache.h index 30f8092ae..a9a9a0e79 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.h +++ b/services/distributeddataservice/service/udmf/store/store_cache.h @@ -29,7 +29,7 @@ public: static StoreCache &GetInstance(); void SetThreadPool(std::shared_ptr executors); void CloseStores(); - void RemoveStore(std::string intention, std::shared_ptr store); + void RemoveStore(std::string intention); private: StoreCache() {} diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index d52db8a77..3d34d6a5b 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "error_code.h" #define LOG_TAG "UdmfServiceImpl" #include "udmf_service_impl.h" @@ -155,10 +156,12 @@ int32_t UdmfServiceImpl::SaveData(CustomOption &option, UnifiedData &unifiedData ZLOGE("Get store failed:%{public}s", intention.c_str()); return E_DB_ERROR; } - - if (store->Put(unifiedData) != E_OK) { - ZLOGE("Put unified data failed:%{public}s", intention.c_str()); - StoreCache::GetInstance().RemoveStore(intention, store); + auto status = store->Put(unifiedData); + if (status != E_OK) { + ZLOGE("Put unified data failed:%{public}s, status:%{public}d", intention.c_str(), status); + if (status == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(intention); + } return E_DB_ERROR; } key = unifiedData.GetRuntime()->key.GetUnifiedKey(); @@ -224,7 +227,9 @@ int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &uni int32_t res = store->Get(query.key, unifiedData); if (res != E_OK) { ZLOGE("Get data failed,res:%{public}d,key:%{public}s", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (res == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } return res; } @@ -422,7 +427,9 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi int32_t res = store->Get(query.key, data); if (res != E_OK) { ZLOGE("Get data failed:%{public}s", key.intention.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (res == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } return res; } if (data.IsEmpty()) { @@ -440,9 +447,12 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi runtime->lastModifiedTime = PreProcessUtils::GetTimestamp(); unifiedData.SetRuntime(*runtime); PreProcessUtils::SetRecordUid(unifiedData); - if (store->Update(unifiedData) != E_OK) { + res = store->Update(unifiedData); + if (res != E_OK) { ZLOGE("Unified data update failed:%{public}s", key.intention.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (res == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } return E_DB_ERROR; } return E_OK; @@ -489,9 +499,12 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vectorDeleteBatch(deleteKeys) != E_OK) { + status = store->DeleteBatch(deleteKeys); + if (status != E_OK) { ZLOGE("Remove data failed."); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (status == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } return E_DB_ERROR; } return E_OK; @@ -511,10 +524,12 @@ int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary) ZLOGE("Get store failed:%{public}s", key.intention.c_str()); return E_DB_ERROR; } - - if (store->GetSummary(key, summary) != E_OK) { + auto status = store->GetSummary(key, summary); + if (status != E_OK) { ZLOGE("Store get summary failed:%{public}s", key.intention.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (status == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } return E_DB_ERROR; } return E_OK; @@ -559,16 +574,21 @@ int32_t UdmfServiceImpl::AddPrivilege(const QueryOption &query, Privilege &privi } if (res != E_OK) { ZLOGE("Get runtime failed, res:%{public}d, key:%{public}s.", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (res == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } return res; } runtime.privileges.emplace_back(privilege); res = store->PutRuntime(query.key, runtime); if (res != E_OK) { ZLOGE("Update runtime failed, res:%{public}d, key:%{public}s", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (res == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } + return res; } - return res; + return E_OK; } int32_t UdmfServiceImpl::Sync(const QueryOption &query, const std::vector &devices) @@ -678,7 +698,9 @@ int32_t UdmfServiceImpl::IsRemoteData(const QueryOption &query, bool &result) auto res = store->GetRuntime(query.key, runtime); if (res != E_OK) { ZLOGE("Get runtime failed, res:%{public}d, key:%{public}s.", res, query.key.c_str()); - StoreCache::GetInstance().RemoveStore(key.intention, store); + if (res == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(key.intention); + } return E_DB_ERROR; } @@ -715,10 +737,12 @@ int32_t UdmfServiceImpl::SetAppShareOption(const std::string &intention, int32_t ZLOGE("SetAppShareOption failed,shareOption already set:%{public}s", shareOptionTmp.c_str()); return E_SETTINGS_EXISTED; } - - if (store->PutLocal(std::to_string(accessTokenIDEx), ShareOptionsUtil::GetEnumStr(shareOption)) != E_OK) { - ZLOGE("Store get unifiedData failed:%{public}d", shareOption); - StoreCache::GetInstance().RemoveStore(intention, store); + auto status = store->PutLocal(std::to_string(accessTokenIDEx), ShareOptionsUtil::GetEnumStr(shareOption)); + if (status != E_OK) { + ZLOGE("Store get unifiedData failed:%{public}d", status); + if (status == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(intention); + } return E_DB_ERROR; } return E_OK; @@ -740,7 +764,9 @@ int32_t UdmfServiceImpl::GetAppShareOption(const std::string &intention, int32_t int32_t ret = store->GetLocal(std::to_string(accessTokenIDEx), appShareOption); if (ret != E_OK) { ZLOGW("GetLocal failed:%{public}s", intention.c_str()); - StoreCache::GetInstance().RemoveStore(intention, store); + if (ret == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(intention); + } return ret; } ZLOGI("GetLocal ok intention:%{public}s,appShareOption:%{public}s", intention.c_str(), appShareOption.c_str()); @@ -768,9 +794,12 @@ int32_t UdmfServiceImpl::RemoveAppShareOption(const std::string &intention) } UnifiedData unifiedData; - if (store->DeleteLocal(std::to_string(accessTokenIDEx)) != E_OK) { - ZLOGE("Store DeleteLocal failed:%{public}s", intention.c_str()); - StoreCache::GetInstance().RemoveStore(intention, store); + auto status = store->DeleteLocal(std::to_string(accessTokenIDEx)); + if (status != E_OK) { + ZLOGE("Store DeleteLocal failed:%{public}s, status:%{public}d", intention.c_str(), status); + if (status == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(intention); + } return E_DB_ERROR; } return E_OK; @@ -816,9 +845,12 @@ int32_t UdmfServiceImpl::QueryDataCommon( ZLOGE("Get store failed:%{public}s", intention.c_str()); return E_DB_ERROR; } - if (store->GetBatchData(dataPrefix, dataSet) != E_OK) { - ZLOGE("Get dataSet failed, dataPrefix: %{public}s.", dataPrefix.c_str()); - StoreCache::GetInstance().RemoveStore(intention, store); + auto status = store->GetBatchData(dataPrefix, dataSet); + if (status != E_OK) { + ZLOGE("Get dataSet failed, dataPrefix: %{public}s, status:%{public}d.", dataPrefix.c_str(), status); + if (status == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(intention); + } return E_DB_ERROR; } return E_OK; @@ -1103,9 +1135,12 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptrPutSummary(udkey, summary) != E_OK) { - ZLOGE("Put summary failed:%{public}s", key.c_str()); - StoreCache::GetInstance().RemoveStore(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), store); + auto status = store->PutSummary(udkey, summary); + if (status != E_OK) { + ZLOGE("Put summary failed:%{public}s, status:%{public}d", key.c_str(), status); + if (status == E_DB_ERROR) { + StoreCache::GetInstance().RemoveStore(UD_INTENTION_MAP.at(UD_INTENTION_DRAG)); + } return E_DB_ERROR; } return E_OK; -- Gitee From fc9bea7284bf00850c9e3ca5c705bd8244e53653 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Sat, 28 Jun 2025 16:46:26 +0800 Subject: [PATCH 4/5] add Signed-off-by: wenjinchao Change-Id: I57cb5e2aca517ed8642f8928be57ccc17fc4f06a --- .../service/udmf/store/store_cache.cpp | 34 ++++++++----------- .../service/udmf/store/store_cache.h | 1 - 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/services/distributeddataservice/service/udmf/store/store_cache.cpp b/services/distributeddataservice/service/udmf/store/store_cache.cpp index 448b96317..73104baac 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.cpp +++ b/services/distributeddataservice/service/udmf/store/store_cache.cpp @@ -41,26 +41,23 @@ std::shared_ptr StoreCache::GetStore(std::string intention) } std::string key = intention; key.append(std::to_string(foregroundUserId)); - { - std::shared_lock storeLock(storeMutex_); - stores_.Compute(key, [&store, intention](const auto &key, std::shared_ptr &storePtr) -> bool { - if (storePtr != nullptr) { - store = storePtr; - return true; - } + stores_.Compute(key, [&store, intention](const auto &key, std::shared_ptr &storePtr) -> bool { + if (storePtr != nullptr) { + store = storePtr; + return true; + } - if (IsValidIntention(intention)) { - storePtr = std::make_shared(intention); - if (!storePtr->Init()) { - ZLOGE("Init runtime store failed."); - return false; - } - store = storePtr; - return true; + if (IsValidIntention(intention)) { + storePtr = std::make_shared(intention); + if (!storePtr->Init()) { + ZLOGE("Init runtime store failed."); + return false; } - return false; - }); - } + store = storePtr; + return true; + } + return false; + }); std::unique_lock lock(taskMutex_); if (taskId_ == ExecutorPool::INVALID_TASK_ID && executorPool_ != nullptr) { @@ -101,7 +98,6 @@ void StoreCache::CloseStores() void StoreCache::RemoveStore(std::string intention) { - std::unique_lock lock(storeMutex_); ZLOGI("RemoveStore, intention:%{public}s", intention.c_str()); int foregroundUserId = 0; if (!DistributedData::AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId)) { diff --git a/services/distributeddataservice/service/udmf/store/store_cache.h b/services/distributeddataservice/service/udmf/store/store_cache.h index a9a9a0e79..9c25d8254 100644 --- a/services/distributeddataservice/service/udmf/store/store_cache.h +++ b/services/distributeddataservice/service/udmf/store/store_cache.h @@ -44,7 +44,6 @@ private: ConcurrentMap> stores_; std::mutex taskMutex_; - std::shared_mutex storeMutex_; ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID; static constexpr int64_t INTERVAL = 1; // 1 min -- Gitee From edfe4aec6cd15a6be4181bddf7975faf55e0494a Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Sat, 28 Jun 2025 17:21:28 +0800 Subject: [PATCH 5/5] add Signed-off-by: wenjinchao Change-Id: I2f715859bc80ce4658671e20bcd36289e458bd15 --- .../service/udmf/store/runtime_store.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.cpp b/services/distributeddataservice/service/udmf/store/runtime_store.cpp index a24f47d9a..fa2a861a5 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.cpp +++ b/services/distributeddataservice/service/udmf/store/runtime_store.cpp @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "error_code.h" #define LOG_TAG "RuntimeStore" #include "runtime_store.h" @@ -456,7 +455,7 @@ bool RuntimeStore::Init() }); if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { ZLOGE("GetKvStore fail, database corrupted, status: %{public}d.", static_cast(status)); - if (DeleteKvDb() != E_OK) { + if (delegateManager_->DeleteKvStore(storeId_) != DBStatus::OK) { ZLOGE("DeleteKvStore fail, status: %{public}d.", static_cast(status)); } return false; -- Gitee