From 6feba640dd0054e5ef71461c73c98f678a5f913f Mon Sep 17 00:00:00 2001 From: gecheng Date: Mon, 20 Jan 2025 15:57:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3kvdb=20crash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gecheng --- .../framework/include/store/general_store.h | 2 +- .../service/kvdb/kvdb_general_store.cpp | 42 +++++++++---------- .../service/kvdb/kvdb_general_store.h | 7 ++-- .../service/rdb/rdb_general_store.cpp | 2 +- .../service/rdb/rdb_general_store.h | 2 +- .../service/test/kvdb_general_store_test.cpp | 2 +- .../service/test/mock/general_store_mock.cpp | 2 +- .../service/test/mock/general_store_mock.h | 2 +- 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index a5f269326..ee5e85041 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -130,7 +130,7 @@ public: virtual void SetExecutor(std::shared_ptr executor) = 0; - virtual int32_t Bind(Database &database, const std::map &bindInfos, + virtual int32_t Bind(const Database &database, const std::map &bindInfos, const CloudConfig &config) = 0; virtual bool IsBound(uint32_t user) = 0; diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 3784e1547..0a5daafc1 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -213,13 +213,15 @@ KVDBGeneralStore::~KVDBGeneralStore() manager_.CloseKvStore(delegate_); delegate_ = nullptr; } - for (auto &bindInfo : bindInfos_) { - if (bindInfo.db_ != nullptr) { - bindInfo.db_->Close(); + { + std::unique_lock lock(bindMutex_); + for (auto &[userId, bindInfo] : bindInfos_) { + if (bindInfo.db_ != nullptr) { + bindInfo.db_->Close(); + } } + bindInfos_.clear(); } - bindInfos_.clear(); - dbClouds_.clear(); } int32_t KVDBGeneralStore::BindSnapshots(std::shared_ptr>> bindAssets) @@ -228,30 +230,26 @@ int32_t KVDBGeneralStore::BindSnapshots(std::shared_ptr &bindInfos, const CloudConfig &config) + const Database &database, const std::map &bindInfos, const CloudConfig &config) { if (bindInfos.empty()) { ZLOGW("No cloudDB!"); return GeneralError::E_OK; } std::map schemas; + std::map> dbClouds{}; auto dbSchema = GetDBSchema(database); - std::set users; - for (auto &[userId, bindInfo] : bindInfos) { - if (bindInfo.db_ == nullptr) { - return GeneralError::E_INVALID_ARGS; - } - users.insert(userId); - dbClouds_.insert({ std::to_string(userId), std::make_shared(bindInfo.db_, nullptr) }); - bindInfos_.insert(std::move(bindInfo)); - schemas.insert({ std::to_string(userId), dbSchema }); - } { - std::unique_lock lock(bindMutex_); - if (users_ == users) { - return GeneralError::E_OK; + std::unique_lock lock(bindMutex_); + for (auto &[userId, bindInfo] : bindInfos) { + if (bindInfo.db_ == nullptr) { + return GeneralError::E_INVALID_ARGS; + } + dbClouds.insert({ std::to_string(userId), + std::make_shared(bindInfo.db_, nullptr) }); + bindInfos_.insert(std::make_pair(userId, std::move(bindInfo))); + schemas.insert({ std::to_string(userId), dbSchema }); } - users_ = users; } DistributedDB::CloudSyncConfig dbConfig; dbConfig.maxUploadCount = config.maxNumber; @@ -261,7 +259,7 @@ int32_t KVDBGeneralStore::Bind( if (delegate_ == nullptr) { return GeneralError::E_ALREADY_CLOSED; } - delegate_->SetCloudDB(dbClouds_); + delegate_->SetCloudDB(std::move(dbClouds)); delegate_->SetCloudDbSchema(std::move(schemas)); delegate_->SetCloudSyncConfig(dbConfig); return GeneralError::E_OK; @@ -270,7 +268,7 @@ int32_t KVDBGeneralStore::Bind( bool KVDBGeneralStore::IsBound(uint32_t user) { std::shared_lock lock(bindMutex_); - return users_.find(user) != users_.end(); + return bindInfos_.find(user) != bindInfos_.end(); } int32_t KVDBGeneralStore::Close(bool isForce) diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 3931458aa..80e030f05 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -43,7 +43,8 @@ public: explicit KVDBGeneralStore(const StoreMetaData &meta); ~KVDBGeneralStore(); - int32_t Bind(Database &database, const std::map &bindInfos, const CloudConfig &config) override; + int32_t Bind(const Database &database, + const std::map &bindInfos, const CloudConfig &config) override; bool IsBound(uint32_t user) override; bool IsValid(); int32_t Execute(const std::string &table, const std::string &sql) override; @@ -124,10 +125,8 @@ private: ObserverProxy observer_; KvManager manager_; KvDelegate *delegate_ = nullptr; - std::map> dbClouds_{}; - std::set bindInfos_; std::shared_mutex bindMutex_; - std::set users_{}; + std::map bindInfos_{}; std::mutex mutex_; int32_t ref_ = 1; mutable std::shared_timed_mutex rwMutex_; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index daa2eb16c..7244ba7d2 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -231,7 +231,7 @@ int32_t RdbGeneralStore::BindSnapshots(std::shared_ptr &bindInfos, +int32_t RdbGeneralStore::Bind(const Database &database, const std::map &bindInfos, const CloudConfig &config) { if (bindInfos.empty()) { diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index ac3a93a07..8d2a40790 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -54,7 +54,7 @@ public: static void OnSyncFinish(const DistributedData::StoreInfo &storeInfo, uint32_t flag, uint32_t syncMode, uint32_t traceId); void SetExecutor(std::shared_ptr executor) override; - int32_t Bind(Database &database, const std::map &bindInfos, + int32_t Bind(const Database &database, const std::map &bindInfos, const CloudConfig &config) override; bool IsBound(uint32_t user) override; bool IsValid(); diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 9888340c9..8466ecce4 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -365,7 +365,7 @@ HWTEST_F(KVDBGeneralStoreTest, BindTest, TestSize.Level0) ret = store->Bind(database, bindInfos, config); EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); - store->users_.clear(); + store->bindInfos_.clear(); KvStoreNbDelegateMock mockDelegate; store->delegate_ = &mockDelegate; EXPECT_NE(store->delegate_, nullptr); diff --git a/services/distributeddataservice/service/test/mock/general_store_mock.cpp b/services/distributeddataservice/service/test/mock/general_store_mock.cpp index face394a3..e984f665a 100644 --- a/services/distributeddataservice/service/test/mock/general_store_mock.cpp +++ b/services/distributeddataservice/service/test/mock/general_store_mock.cpp @@ -15,7 +15,7 @@ #include "general_store_mock.h" namespace OHOS { namespace DistributedData { -int32_t GeneralStoreMock::Bind(Database &database, const std::map &bindInfos, +int32_t GeneralStoreMock::Bind(const Database &database, const std::map &bindInfos, const CloudConfig &config) { return 0; diff --git a/services/distributeddataservice/service/test/mock/general_store_mock.h b/services/distributeddataservice/service/test/mock/general_store_mock.h index 2b30f77ce..03fcb2eee 100644 --- a/services/distributeddataservice/service/test/mock/general_store_mock.h +++ b/services/distributeddataservice/service/test/mock/general_store_mock.h @@ -21,7 +21,7 @@ namespace OHOS { namespace DistributedData { class GeneralStoreMock : public GeneralStore { public: - int32_t Bind(Database &database, const std::map &bindInfos, + int32_t Bind(const Database &database, const std::map &bindInfos, const CloudConfig &config) override; bool IsBound(uint32_t user) override; int32_t Execute(const std::string &table, const std::string &sql) override; -- Gitee From df63a0759db5e951fdb029394ac2f92f03236a68 Mon Sep 17 00:00:00 2001 From: BrainL Date: Mon, 27 Jan 2025 17:28:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E6=96=87?= =?UTF-8?q?=E4=BB=B6idl=E5=8C=96=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: BrainL Change-Id: I316b5546d269a0005435d68a270df5e8a0de68b3 --- .../service/object/object_asset_loader.cpp | 8 +++++--- .../service/object/object_asset_loader.h | 4 ++-- .../service/object/object_data_listener.cpp | 12 +++++++----- .../service/object/object_data_listener.h | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/services/distributeddataservice/service/object/object_asset_loader.cpp b/services/distributeddataservice/service/object/object_asset_loader.cpp index c3d582e6a..6a49aec40 100644 --- a/services/distributeddataservice/service/object/object_asset_loader.cpp +++ b/services/distributeddataservice/service/object/object_asset_loader.cpp @@ -182,15 +182,17 @@ int32_t ObjectAssetLoader::PushAsset(int32_t userId, const sptr &asset return status; } -int32_t ObjectAssetsSendListener::OnSendResult(const sptr &assetObj, int32_t result) +int32_t ObjectAssetsSendListener::OnSendResult(const AssetObj &assetObj, int32_t result) { - if (assetObj == nullptr) { + AssetObj assetObjTemp = assetObj; + const sptr assetObjPtr = sptr(&assetObjTemp); + if (assetObjPtr == nullptr) { ZLOGE("OnSendResult error! status:%{public}d", result); ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::SAVE, ObjectStore::PUSH_ASSETS, ObjectStore::RADAR_FAILED, result, ObjectStore::FINISHED); return result; } - ZLOGI("OnSendResult, status:%{public}d, asset size:%{public}zu", result, assetObj->uris_.size()); + ZLOGI("OnSendResult, status:%{public}d, asset size:%{public}zu", result, assetObjPtr->uris_.size()); if (result == OBJECT_SUCCESS) { ObjectStore::RadarReporter::ReportStage(std::string(__FUNCTION__), ObjectStore::SAVE, ObjectStore::PUSH_ASSETS, ObjectStore::RADAR_SUCCESS); diff --git a/services/distributeddataservice/service/object/object_asset_loader.h b/services/distributeddataservice/service/object/object_asset_loader.h index 44d3f8ab0..5f90d574b 100644 --- a/services/distributeddataservice/service/object/object_asset_loader.h +++ b/services/distributeddataservice/service/object/object_asset_loader.h @@ -21,7 +21,7 @@ #include "store/general_value.h" #include "concurrent_map.h" #include -#include "asset/asset_send_callback_stub.h" +#include "asset_send_callback_stub.h" namespace OHOS { namespace DistributedObject { using AssetObj = Storage::DistributedFile::AssetObj; @@ -36,7 +36,7 @@ class ObjectAssetsSendListener : public Storage::DistributedFile::AssetSendCallb public: ObjectAssetsSendListener() =default; ~ObjectAssetsSendListener() = default; - int32_t OnSendResult(const sptr &assetObj, int32_t result) override; + int32_t OnSendResult(const AssetObj &assetObj, int32_t result) override; }; class ObjectAssetLoader { diff --git a/services/distributeddataservice/service/object/object_data_listener.cpp b/services/distributeddataservice/service/object/object_data_listener.cpp index 1c58fb493..e29cf565e 100644 --- a/services/distributeddataservice/service/object/object_data_listener.cpp +++ b/services/distributeddataservice/service/object/object_data_listener.cpp @@ -55,20 +55,22 @@ int32_t ObjectAssetsRecvListener::OnStart(const std::string &srcNetworkId, const return OBJECT_SUCCESS; } -int32_t ObjectAssetsRecvListener::OnFinished(const std::string &srcNetworkId, const sptr &assetObj, +int32_t ObjectAssetsRecvListener::OnFinished(const std::string &srcNetworkId, const AssetObj &assetObj, int32_t result) { - if (assetObj == nullptr) { + AssetObj assetObjTemp = assetObj; + const sptr assetObjPtr = sptr(&assetObjTemp); + if (assetObjPtr == nullptr) { ZLOGE("OnFinished error! status:%{public}d, srcNetworkId:%{public}s", result, DistributedData::Anonymous::Change(srcNetworkId).c_str()); ObjectStore::RadarReporter::ReportStageError(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, ObjectStore::ASSETS_RECV, ObjectStore::RADAR_FAILED, result); return result; } - auto objectKey = assetObj->dstBundleName_+assetObj->sessionId_; + auto objectKey = assetObjPtr->dstBundleName_+assetObjPtr->sessionId_; ZLOGI("OnFinished, status:%{public}d objectKey:%{public}s, asset size:%{public}zu", result, objectKey.c_str(), - assetObj->uris_.size()); - ObjectStoreManager::GetInstance()->NotifyAssetsReady(objectKey, assetObj->dstBundleName_, srcNetworkId); + assetObjPtr->uris_.size()); + ObjectStoreManager::GetInstance()->NotifyAssetsReady(objectKey, assetObjPtr->dstBundleName_, srcNetworkId); return OBJECT_SUCCESS; } } // namespace DistributedObject diff --git a/services/distributeddataservice/service/object/object_data_listener.h b/services/distributeddataservice/service/object/object_data_listener.h index 0ee568c60..a96ff0219 100644 --- a/services/distributeddataservice/service/object/object_data_listener.h +++ b/services/distributeddataservice/service/object/object_data_listener.h @@ -17,7 +17,7 @@ #define DISTRIBUTEDDATAMGR_OBJECT_DATA_LISTENER_H #include "kv_store_observer.h" -#include "asset/asset_recv_callback_stub.h" +#include "asset_recv_callback_stub.h" namespace OHOS { namespace DistributedObject { using AssetObj = Storage::DistributedFile::AssetObj; @@ -37,7 +37,7 @@ public: const std::string &dstNetworkId, const std::string &sessionId, const std::string &dstBundleName) override; int32_t OnFinished(const std::string &srcNetworkId, - const sptr &assetObj, int32_t result) override; + const AssetObj &assetObj, int32_t result) override; }; } // namespace DistributedObject } // namespace OHOS -- Gitee