diff --git a/frameworks/libs/distributeddb/common/include/data_value.h b/frameworks/libs/distributeddb/common/include/data_value.h index 27085aa7468f210d222f1651c1370d868016b46c..25a0ae4b714b7f390fd23fd9ad34e9a8b0fe0ce4 100644 --- a/frameworks/libs/distributeddb/common/include/data_value.h +++ b/frameworks/libs/distributeddb/common/include/data_value.h @@ -75,7 +75,6 @@ public: int SetText(const uint8_t *val, uint32_t length); int GetText(std::string &outVal) const; void ResetValue(); - int GetBlobLength(uint32_t &length) const; std::string ToString() const; private: diff --git a/frameworks/libs/distributeddb/common/include/db_errno.h b/frameworks/libs/distributeddb/common/include/db_errno.h index 6df071c3d5b80ab4041ad2ce7747100372679d30..13a1098cc42bb0be2f621e2ca2ea7c388aa3a787 100644 --- a/frameworks/libs/distributeddb/common/include/db_errno.h +++ b/frameworks/libs/distributeddb/common/include/db_errno.h @@ -16,8 +16,6 @@ #ifndef DISTRIBUTEDDB_ERRNO_H #define DISTRIBUTEDDB_ERRNO_H -#include - namespace DistributedDB { constexpr int E_OK = 0; constexpr int E_BASE = 1000; // different from the other errno. diff --git a/frameworks/libs/distributeddb/common/include/performance_analysis.h b/frameworks/libs/distributeddb/common/include/performance_analysis.h index 1b0da052c1d39ad4eb4c324fd32d3d5b6a776b73..51a84dfe9215d5c2761f90a6324a985634d704cc 100644 --- a/frameworks/libs/distributeddb/common/include/performance_analysis.h +++ b/frameworks/libs/distributeddb/common/include/performance_analysis.h @@ -74,6 +74,8 @@ public: static PerformanceAnalysis *GetInstance(int stepNum = 20); + void Initialization(); + void TimeRecordStart(); void TimeRecordEnd(); @@ -88,7 +90,7 @@ public: void ClosePerformanceAnalysis(); - void SetFileNumber(const std::string &FileID); + void SetFileName(const std::string &fileName); private: @@ -117,7 +119,7 @@ private: bool isOpen_; std::ofstream outFile; int fileNumber_; - std::string fileID_; + std::string fileName_; }; } // namespace DistributedDB #endif diff --git a/frameworks/libs/distributeddb/common/include/platform_specific.h b/frameworks/libs/distributeddb/common/include/platform_specific.h index 10d91822f80e8a3641a9daa9f6969e88261b96e2..6573bcf4402e6c20213e518fdc1fa3250cdf667b 100644 --- a/frameworks/libs/distributeddb/common/include/platform_specific.h +++ b/frameworks/libs/distributeddb/common/include/platform_specific.h @@ -34,8 +34,8 @@ enum FileType { struct FileAttr { std::string fileName; - FileType fileType; - uint64_t fileLen; + FileType fileType = FileType::FILE; + uint64_t fileLen = 0; }; // Shield the representation method of file handles on different platforms diff --git a/frameworks/libs/distributeddb/common/src/auto_launch.cpp b/frameworks/libs/distributeddb/common/src/auto_launch.cpp index 388840b1256d0bff9da7ad144804d23d2e368b0c..5e599fde4c121e94e676b251cc519acccecdb6c6 100644 --- a/frameworks/libs/distributeddb/common/src/auto_launch.cpp +++ b/frameworks/libs/distributeddb/common/src/auto_launch.cpp @@ -435,7 +435,6 @@ int AutoLaunch::DisableKvStoreAutoLaunch(const std::string &normalIdentifier, co void AutoLaunch::GetAutoLaunchSyncDevices(const std::string &identifier, std::vector &devices) const { devices.clear(); - devices.shrink_to_fit(); std::lock_guard autoLock(dataLock_); if (autoLaunchItemMap_.count(identifier) == 0) { LOGD("[AutoLaunch] GetSyncDevices identifier is not exist!"); @@ -444,6 +443,7 @@ void AutoLaunch::GetAutoLaunchSyncDevices(const std::string &identifier, std::ve for (const auto &device : onlineDevices_) { devices.push_back(device); } + devices.shrink_to_fit(); } void AutoLaunch::CloseNotifier(const AutoLaunchItem &autoLaunchItem) diff --git a/frameworks/libs/distributeddb/common/src/data_value.cpp b/frameworks/libs/distributeddb/common/src/data_value.cpp index 8c8fd72916c81aee3e6738c6a18651b084c7ef47..d5f7ae772e7813598fc009213252cf111852aebd 100644 --- a/frameworks/libs/distributeddb/common/src/data_value.cpp +++ b/frameworks/libs/distributeddb/common/src/data_value.cpp @@ -319,15 +319,6 @@ StorageType DataValue::GetType() const return type_; } -int DataValue::GetBlobLength(uint32_t &length) const -{ - if (type_ != StorageType::STORAGE_TYPE_BLOB && type_ != StorageType::STORAGE_TYPE_TEXT) { - return -E_NOT_SUPPORT; - } - length = value_.blobPtr->GetSize(); - return E_OK; -} - void DataValue::ResetValue() { switch (type_) { diff --git a/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp b/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp index 7f4ba262ad71134f3ea07eb9dcfe24282b44fd8a..31e6de630661914c6af510b948b772c4f4785160 100644 --- a/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp +++ b/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp @@ -763,12 +763,12 @@ int CompareFieldCount(bool isRoot, uint32_t selfCount, uint32_t otherCount) { if (isRoot) { if (otherCount < selfCount) { - LOGW("[FBSchema][CompareRoot] RootFieldSize: %" PRu32 " vs %" PRu32, selfCount, otherCount); + LOGW("[FBSchema][CompareRoot] RootFieldSize: %" PRIu32 " vs %" PRIu32, selfCount, otherCount); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } else { if (selfCount != otherCount) { - LOGW("[FBSchema][CompareRoot] StructFieldSize: %" PRu32 " vs %" PRu32, selfCount, otherCount); + LOGW("[FBSchema][CompareRoot] StructFieldSize: %" PRIu32 " vs %" PRIu32, selfCount, otherCount); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -821,7 +821,7 @@ int CompareFieldInfo(const reflection::Field &selfField, const reflection::Field auto selfElementType = selfType->element(); auto otherElementType = otherType->element(); if (selfElementType != otherElementType) { - LOGE("[FBSchema][CompareField] ElementType diff:%" PRu32 " vs %" PRu32, selfElementType, otherElementType); + LOGE("[FBSchema][CompareField] ElementType diff:%" PRIu32 " vs %" PRIu32, selfElementType, otherElementType); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } diff --git a/frameworks/libs/distributeddb/common/src/json_object.cpp b/frameworks/libs/distributeddb/common/src/json_object.cpp index 96346909e5081b0dea8bc0c00d22ef0e1562b4b3..06f497aefc339fba45d81a18a49e32ba30651fcd 100644 --- a/frameworks/libs/distributeddb/common/src/json_object.cpp +++ b/frameworks/libs/distributeddb/common/src/json_object.cpp @@ -327,7 +327,7 @@ int JsonObject::GetSubFieldPath(const FieldPath &inPath, std::set &ou std::vector subFields = valueNode.getMemberNames(); for (const auto &eachSubField : subFields) { FieldPath eachSubPath = inPath; - eachSubPath.push_back(eachSubField); + eachSubPath.emplace_back(eachSubField); outSubPath.insert(eachSubPath); } return E_OK; diff --git a/frameworks/libs/distributeddb/common/src/performance_analysis.cpp b/frameworks/libs/distributeddb/common/src/performance_analysis.cpp index 94207bf3067e745c01fb5968713d78265568c21c..75e11259c98c5eeec81997b041aef69dc3252397 100644 --- a/frameworks/libs/distributeddb/common/src/performance_analysis.cpp +++ b/frameworks/libs/distributeddb/common/src/performance_analysis.cpp @@ -30,16 +30,20 @@ const std::string PerformanceAnalysis::DEFAULT_FILE_NAME = "default00"; PerformanceAnalysis *PerformanceAnalysis::GetInstance(int stepNum) { static PerformanceAnalysis inst(stepNum); + inst.Initialization(); return &inst; } PerformanceAnalysis::PerformanceAnalysis(uint32_t inStepNum) : isOpen_(false) { - if (inStepNum == 0) { - stepNum_ = 0; - } stepNum_ = inStepNum; + fileNumber_ = 0; + fileName_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); +} + +void PerformanceAnalysis::Initialization() +{ counts_.resize(stepNum_); timeRecordData_.timeInfo.resize(stepNum_); stepTimeRecordInfo_.resize(stepNum_); @@ -51,8 +55,6 @@ PerformanceAnalysis::PerformanceAnalysis(uint32_t inStepNum) for (auto iter = counts_.begin(); iter != counts_.end(); ++iter) { *iter = 0; } - fileNumber_ = 0; - fileID_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); } PerformanceAnalysis::~PerformanceAnalysis() {}; @@ -190,7 +192,7 @@ std::string PerformanceAnalysis::GetStatistics() void PerformanceAnalysis::OutStatistics() { - std::string addrStatistics = STATISTICAL_DATA_FILE_NAME_HEADER + fileID_ + CSV_FILE_EXTENSION; + std::string addrStatistics = STATISTICAL_DATA_FILE_NAME_HEADER + fileName_ + CSV_FILE_EXTENSION; outFile.open(addrStatistics, std::ios_base::app); if (!outFile.is_open()) { return; @@ -220,11 +222,11 @@ void PerformanceAnalysis::Clear() iter.min = ULLONG_MAX; iter.average = 0; } - fileID_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); + fileName_ = std::string(DEFAULT_FILE_NAME) + std::to_string(fileNumber_); } -void PerformanceAnalysis::SetFileNumber(const std::string &FileID) +void PerformanceAnalysis::SetFileName(const std::string &fileName) { - fileID_ = FileID; + fileName_ = fileName; } } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/common/src/platform_specific.cpp b/frameworks/libs/distributeddb/common/src/platform_specific.cpp index 6f8155c7c86b596b9127f64526d90d5604fb4367..efc9840f7b04b455a55f6fd62aea63210422ffe0 100644 --- a/frameworks/libs/distributeddb/common/src/platform_specific.cpp +++ b/frameworks/libs/distributeddb/common/src/platform_specific.cpp @@ -313,6 +313,11 @@ int OpenFile(const std::string &fileName, FileHandle &handle) int CloseFile(FileHandle &handle) { + if (handle.handle == -1) { + LOGI("[CloseFile] file handle is invalid!"); + return E_OK; + } + if (close(handle.handle) != 0) { LOGE("close file failed, errno:%d", errno); return -E_SYSTEM_API_FAIL; @@ -333,12 +338,7 @@ int FileLock(const FileHandle &handle, bool isBlock) int FileUnlock(FileHandle &handle) { - if (handle.handle == -1) { - LOGI("[FileUnlock] file handle is invalid!"); - return E_OK; - } - - return CloseFile(handle); + return E_OK;; } #else namespace { @@ -589,6 +589,10 @@ int OpenFile(const std::string &fileName, FileHandle &handle) int CloseFile(FileHandle &handle) { + if (handle.handle == -1) { + LOGI("[CloseFile] file handle is invalid!"); + return E_OK; + } if (close(handle.handle) != 0) { LOGE("close file failed, errno:%d", errno); return -E_SYSTEM_API_FAIL; @@ -636,7 +640,7 @@ int FileUnlock(FileHandle &handle) LOGE("Unlock file failed. errno:%d", errno); return -E_SYSTEM_API_FAIL; } - return CloseFile(handle); + return E_OK; } #endif } // namespace OS diff --git a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp index ed2510b220766ddbc27c19af3159220eeed8659d..0fba976541a6131bf950fa75df458c615ef5cf8d 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp +++ b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp @@ -538,11 +538,16 @@ int RuntimeContextImpl::GetSecurityOption(const std::string &filePath, SecurityO bool RuntimeContextImpl::CheckDeviceSecurityAbility(const std::string &devId, const SecurityOption &option) const { - std::lock_guard autoLock(systemApiAdapterLock_); - if (systemApiAdapter_ == nullptr) { - return true; + std::shared_ptr tempSystemApiAdapter = nullptr; + { + std::lock_guard autoLock(systemApiAdapterLock_); + if (systemApiAdapter_ == nullptr) { + return true; + } + tempSystemApiAdapter = systemApiAdapter_; } - return systemApiAdapter_->CheckDeviceSecurityAbility(devId, option); + + return tempSystemApiAdapter->CheckDeviceSecurityAbility(devId, option); } int RuntimeContextImpl::SetProcessSystemApiAdapter(const std::shared_ptr &adapter) diff --git a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp index c3b3df30e9000313d9f0f210cd8dad6e65a2b904..7c9151c9fc2d80e9abca2fd8d3b492f1ae4f2fe3 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp @@ -15,6 +15,8 @@ #include "communicator_aggregator.h" +#include + #include "hash.h" #include "communicator.h" #include "communicator_linker.h" diff --git a/frameworks/libs/distributeddb/storage/include/iconnection.h b/frameworks/libs/distributeddb/storage/include/iconnection.h index dbe63aac573ea529b0606f1537b5dd1e77d16bf6..c5ac3d96a2fd6770d2248f6ead8e314410b096b0 100644 --- a/frameworks/libs/distributeddb/storage/include/iconnection.h +++ b/frameworks/libs/distributeddb/storage/include/iconnection.h @@ -37,4 +37,4 @@ protected: }; } // namespace DistributedDB -#endif // I_KV_DB_CONNECTION_H +#endif // I_CONNECTION_H diff --git a/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h b/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h index 717cc0ed4363f9fa50b2d0045a58ad7bc74b7b0a..5c036ac30c1a774dcf605c0c21b435cc942c1d53 100644 --- a/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h +++ b/frameworks/libs/distributeddb/storage/include/storage_engine_manager.h @@ -41,7 +41,7 @@ private: StorageEngineManager(); ~StorageEngineManager(); - // Get a StorageEngineManager instance, Singleton mode + // Get a StorageEngineManager instance, Singleton mode static StorageEngineManager *GetInstance(); int RegisterLockStatusListener(); diff --git a/frameworks/libs/distributeddb/storage/src/data_transformer.cpp b/frameworks/libs/distributeddb/storage/src/data_transformer.cpp index 0eac2a7384427ac744e3d436427bd454645c276a..264609c8ca53d4a8a6a8e6c94866326e0281f9cd 100644 --- a/frameworks/libs/distributeddb/storage/src/data_transformer.cpp +++ b/frameworks/libs/distributeddb/storage/src/data_transformer.cpp @@ -114,11 +114,14 @@ uint32_t DataTransformer::CalDataValueLength(const DataValue &dataValue) uint32_t length = 0; switch (dataValue.GetType()) { case StorageType::STORAGE_TYPE_BLOB: - case StorageType::STORAGE_TYPE_TEXT: - (void)dataValue.GetBlobLength(length); + case StorageType::STORAGE_TYPE_TEXT: { + Blob blob; + (void)dataValue.GetBlob(blob); + length = blob.GetSize(); length = Parcel::GetEightByteAlign(length); length += Parcel::GetUInt32Len(); // record data length break; + } default: break; } diff --git a/frameworks/libs/distributeddb/storage/src/default_factory.h b/frameworks/libs/distributeddb/storage/src/default_factory.h index 975a00679db774a778bfa0f504085b6ba216185c..b86dcf0162f6b78b266468fa4661d3ddddfa482e 100644 --- a/frameworks/libs/distributeddb/storage/src/default_factory.h +++ b/frameworks/libs/distributeddb/storage/src/default_factory.h @@ -39,7 +39,7 @@ public: IKvDBCommitStorage *CreateMultiVerCommitStorage(int &errCode) override; #endif private: - // Create the a local kv db + // Create a local kv db IKvDB *CreateLocalKvDB(int &errCode); #ifndef OMIT_MULTI_VER diff --git a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp index a8be5246de7d946cf2366c3e73b741dbe6fda5ef..48ec008934e0b6a9a5f4e67b87ccf0817d1bd0e7 100644 --- a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp +++ b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp @@ -243,14 +243,14 @@ int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVe return errCode; } -int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &datalen) +int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &dataLen) { if (targetVersion < SOFTWARE_VERSION_EARLIEST || targetVersion > SOFTWARE_VERSION_CURRENT) { return -E_VERSION_NOT_SUPPORT; } if (operType == OperType::CAL_LEN) { - return CalLenByVersion(targetVersion, datalen); + return CalLenByVersion(targetVersion, dataLen); } else { LOGE("Unknown upgrade serialize oper!"); return -E_UPGRADE_FAILED; diff --git a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h index 8f6a430455c784feecf3d1d79056bf5ea8e8698e..f4fcd95e86e56f0889358873d784da53b7e55c1e 100644 --- a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h +++ b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h @@ -89,8 +89,8 @@ private: DESERIALIZE, CAL_LEN, }; - int AdaptToVersion(OperType operType, uint32_t targetVersion, Parcel &parcel, uint64_t &datalen); - int AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &datalen); + int AdaptToVersion(OperType operType, uint32_t targetVersion, Parcel &parcel, uint64_t &dataLen); + int AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &dataLen); int SerializeDataByVersion(uint32_t targetVersion, Parcel &parcel) const; int SerializeDataByFirstVersion(Parcel &parcel) const; diff --git a/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h b/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h index c6e7aeef883ae0370af0048d564f580388c6f29c..cef320763bec46ae7d2b4dbd50000cb8173298f4 100644 --- a/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h +++ b/frameworks/libs/distributeddb/storage/src/ikvdb_commit_storage.h @@ -57,7 +57,7 @@ public: virtual int ImportDatabase(const Property &property, const std::string &dir, const CipherPassword &passwd) = 0; virtual int StartVacuum() = 0; virtual int CancelVacuum() = 0; - virtual int FinishlVacuum() = 0; + virtual int FinishVacuum() = 0; virtual int GetAllCommitsInTree(std::list &commits) const = 0; }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp b/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp index 0c4ceb20c09031789f2beb3302d5497ac28748db..4c7c46867f02d20d86008471a3726ae89ff4eb67 100644 --- a/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp +++ b/frameworks/libs/distributeddb/storage/src/kvdb_manager.cpp @@ -242,6 +242,11 @@ int KvDBManager::UnlockDB(const KvDBProperties &kvDBProp) if (errCode != E_OK) { return errCode; } + errCode = OS::CloseFile(locks_[identifierDir]); + LOGI("DB closed! errCode = [%d]", errCode); + if (errCode != E_OK) { + return errCode; + } locks_.erase(identifierDir); return E_OK; } @@ -446,7 +451,7 @@ int KvDBManager::CalculateKvStoreSize(const KvDBProperties &properties, uint64_t totalSize = totalSize + dbSize; } // This represent Db file size(Unit is byte), It is small than max size(max uint64_t represent 2^64B) - if (totalSize != 0ull) { + if (totalSize != 0ULL) { size = totalSize; return E_OK; } diff --git a/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp b/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp index 0cedc59e6c021317c1bf0df10b02c9abb4361573..b947542a22b95eaf544ef678436b40e136669d57 100644 --- a/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp +++ b/frameworks/libs/distributeddb/storage/src/kvdb_utils.cpp @@ -28,7 +28,6 @@ void KvDBUtils::GetStoreDirectory(std::string &directory, const std::string &ide directory += "/"; } directory += identifierName; - return; } int KvDBUtils::RemoveKvDB(const std::string &dirAll, const std::string &dirStoreOnly, const std::string &dbName) diff --git a/frameworks/libs/distributeddb/storage/src/kvdb_utils.h b/frameworks/libs/distributeddb/storage/src/kvdb_utils.h index b55ac8dac228f448f4f0cd2e13619dd06cd28852..795a99a6aa5c67de12428b6f102227ba028f1295 100644 --- a/frameworks/libs/distributeddb/storage/src/kvdb_utils.h +++ b/frameworks/libs/distributeddb/storage/src/kvdb_utils.h @@ -35,4 +35,4 @@ public: static int RemoveKvDB(const std::string &dirAll, const std::string &dirStoreOnly, const std::string &dbName); }; } // namespace DistributedDB -#endif // LOCAL_KVDB_H \ No newline at end of file +#endif // KVDB_UTILLS_H \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp index 02133d50847d9ed6feaafc1a31daf1fd98a512ae..c32c7aa554196ea7074c76a66622ebe7c915d26a 100644 --- a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp +++ b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.cpp @@ -237,7 +237,7 @@ int MultiVerNaturalStoreCommitStorage::CancelVacuum() return commitStorageDBConnection_->RollBack(); } -int MultiVerNaturalStoreCommitStorage::FinishlVacuum() +int MultiVerNaturalStoreCommitStorage::FinishVacuum() { if (commitStorageDBConnection_ == nullptr) { LOGE("commitStorage Connection not existed!"); diff --git a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h index 3a310de4f40efa9886a0fe83bee8ecd7c564bc23..791a1607dec9019e71c9a0018ec3f2d0da31671c 100644 --- a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h +++ b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_natural_store_commit_storage.h @@ -74,7 +74,7 @@ public: int CancelVacuum() override; - int FinishlVacuum() override; + int FinishVacuum() override; int GetAllCommitsInTree(std::list &commits) const override; diff --git a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp index 0b25aae51ca31c5b7857022c59fd0007ada570ca..d11d6140ae0d4a481ef316a3d48f2ee4f3ef0616 100644 --- a/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp +++ b/frameworks/libs/distributeddb/storage/src/multiver/multi_ver_storage_executor.cpp @@ -826,7 +826,7 @@ int MultiVerStorageExecutor::CommitAllDbTransaction() } // start commit history transaction - errCode = commitStorage_->FinishlVacuum(); + errCode = commitStorage_->FinishVacuum(); if (errCode != E_OK) { LOGE("Finish commitStorage transaction failed:%d", errCode); goto END; diff --git a/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp b/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp index f940084132cf2aae35d0ef96234e96f66da12472..e832f58e481e0c2b223cbde5de72ebd57a5ee655 100644 --- a/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp +++ b/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp @@ -366,7 +366,7 @@ int DatabaseOper::RemoveFile(const std::string &fileName) return E_OK; } - if (OS::RemoveFile(fileName.c_str()) != E_OK) { + if (OS::RemoveFile(fileName) != E_OK) { LOGE("Remove file failed:%d", errno); return -E_REMOVE_FILE; } diff --git a/frameworks/libs/distributeddb/storage/src/package_file.cpp b/frameworks/libs/distributeddb/storage/src/package_file.cpp index fb5b6a7e6c78b5ea6d4cc8b37247295586f2c0a5..fe8b33e14d0bdfcb642434b4c7111ddff2622200 100644 --- a/frameworks/libs/distributeddb/storage/src/package_file.cpp +++ b/frameworks/libs/distributeddb/storage/src/package_file.cpp @@ -60,10 +60,9 @@ static void Clear(ofstream &target, string targetFile) if (target.is_open()) { target.close(); } - if (OS::RemoveFile(targetFile.c_str()) != E_OK) { + if (OS::RemoveFile(targetFile) != E_OK) { LOGE("Remove file failed."); } - return; } static int GetChecksum(const string &file, vector &result) diff --git a/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp index 76b7fffcddf6912759d7ae771bff2534af830f1f..a3c4bb09691f157f8fbcb955743cd3355333781e 100644 --- a/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational_store_connection.cpp @@ -15,7 +15,6 @@ #ifdef RELATIONAL_STORE #include "relational_store_connection.h" #include "db_errno.h" -#include "sqlite_single_ver_relational_storage_executor.h" namespace DistributedDB { RelationalStoreConnection::RelationalStoreConnection() : isExclusive_(false) diff --git a/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp b/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp index fd269ecebe1bc8aacd736e34344ef1632db63b37..363b308908db13b5963a091ba68bab1152e74d85 100644 --- a/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational_store_instance.cpp @@ -15,9 +15,6 @@ #ifdef RELATIONAL_STORE #include "relational_store_instance.h" -#include -#include - #include "db_common.h" #include "db_errno.h" #include "sqlite_relational_store.h" @@ -185,6 +182,12 @@ int CheckCompatibility(const RelationalDBProperties &prop, const RelationalDBPro LOGE("Failed to check cipher args."); return -E_INVALID_PASSWD_OR_CORRUPTED_DB; } + + if (prop.GetBoolProp(DBProperties::SYNC_DUAL_TUPLE_MODE, false) != + existedProp.GetBoolProp(DBProperties::SYNC_DUAL_TUPLE_MODE, false)) { + LOGE("Failed to check dual tuple sync mode for rdb"); + return -E_MODE_MISMATCH; + } return E_OK; } } diff --git a/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp b/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp index c97449bcbe5a200e8b874dbfb8219cd82e5199ea..c9b5f3eb1844387508c9433e3f40f13751ab2b5c 100644 --- a/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational_sync_able_storage.cpp @@ -672,7 +672,7 @@ int RelationalSyncAbleStorage::CheckAndInitQueryCondition(QueryObject &query) co query.SetSchema(schema); int errCode = E_OK; - auto *handle = GetHandle(false, errCode); + auto *handle = GetHandle(true, errCode); if (handle == nullptr) { return errCode; } diff --git a/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp b/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp index 5709380850fa2ac9aade50bbaaed85a025924bf2..3433156cf551726f293143a9e8894940b4fb8854 100644 --- a/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp +++ b/frameworks/libs/distributeddb/storage/src/result_entries_window.cpp @@ -68,10 +68,10 @@ int ResultEntriesWindow::GetCurrentPosition() const bool ResultEntriesWindow::MoveToPosition(int position) { - if ((rawCursor_ == nullptr && buffer_.size() == 0) || (position < 0 || position >= totalCount_)) { + if ((rawCursor_ == nullptr && buffer_.empty()) || (position < 0 || position >= totalCount_)) { return false; } - if (buffer_.size() == 0) { + if (buffer_.empty()) { if (SetCursor(0, position) != E_OK) { return false; } @@ -107,13 +107,13 @@ bool ResultEntriesWindow::MoveToPosition(int position) int ResultEntriesWindow::GetEntry(Entry &entry) const { - if (rawCursor_ == nullptr && buffer_.size() == 0) { + if (rawCursor_ == nullptr && buffer_.empty()) { return -E_NOT_INIT; } if (totalCount_ == 0) { return -E_NOT_FOUND; } - if (buffer_.size() == 0) { + if (buffer_.empty()) { int errCode = LoadData(0, currentPosition_); if (errCode != E_OK) { return errCode; @@ -132,7 +132,6 @@ void ResultEntriesWindow::ResetWindow() } begin_ = 0; currentPosition_ = 0; - return; } int ResultEntriesWindow::SetCursor(int begin, int target) diff --git a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp index d4148e27001ec60a053007f60252f36e30e86976..db3c984fa495d4924b57b21a0c99ed2e7c931321 100644 --- a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp +++ b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp @@ -45,7 +45,6 @@ const std::list SingleVerNaturalStoreCommitNotifyData::GetCom void SingleVerNaturalStoreCommitNotifyData::SetFilterKey(const Key &key) { keyFilter_ = key; - return; } bool SingleVerNaturalStoreCommitNotifyData::IsChangedDataEmpty() const diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp index c7816daa92a03a584c267d5178ad0beeb46d88df..83036994d93597b33e8ec4e064f5a1d1a68d9354 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_local_kvdb.cpp @@ -167,11 +167,6 @@ int SQLiteLocalKvDB::RunExportLogic(CipherType type, const CipherPassword &passw } errCode = SQLiteUtils::ExportDatabase(db, type, passwd, newDbName); - if (errCode != E_OK) { - goto END; - } - -END: (void)sqlite3_close_v2(db); db = nullptr; return errCode; @@ -393,9 +388,8 @@ int SQLiteLocalKvDB::CheckVersionAndUpgradeIfNeed(const OpenDbProperties &openPr errCode = SQLiteUtils::SetUserVer(openProp, LOCAL_STORE_VERSION_CURRENT); if (errCode != E_OK) { LOGE("[SqlLocalDb][CheckUpgrade] SetUserVer fail, errCode=%d.", errCode); - return errCode; } - return E_OK; + return errCode; } DEFINE_OBJECT_TAG_FACILITIES(SQLiteLocalKvDB) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp index 41b3e208e37f06ebb64adb3b7611fd07349d25e4..31ca06386eb6a7d5fc7532340915e840438834ec 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp @@ -1730,11 +1730,7 @@ int SQLiteSingleVerNaturalStore::RemoveKvDB(const KvDBProperties &properties) if (errCode != E_OK) { return errCode; } - errCode = DBCommon::RemoveAllFilesOfDirectory(storeOnlyDir, true); - if (errCode != E_OK) { - return errCode; - } - return errCode; + return DBCommon::RemoveAllFilesOfDirectory(storeOnlyDir, true); } int SQLiteSingleVerNaturalStore::GetKvDBSize(const KvDBProperties &properties, uint64_t &size) const @@ -2157,7 +2153,7 @@ int SQLiteSingleVerNaturalStore::CheckAndInitQueryCondition(QueryObject &query) query.SetSchema(localSchema); int errCode = E_OK; - SQLiteSingleVerStorageExecutor *handle = GetHandle(false, errCode); + SQLiteSingleVerStorageExecutor *handle = GetHandle(true, errCode); if (handle == nullptr) { return errCode; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp index 63d10262046b8cdf0c43ba90875044bdc005b2dc..3501c1f4ba89237775f4f1e044331b19d56b65bc 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp @@ -1103,7 +1103,7 @@ END: } void SQLiteSingleVerStorageExecutor::PutIntoCommittedData(const DataItem &itemPut, const DataItem &itemGet, - const DataOperStatus &status, const Key &hashKey, SingleVerNaturalStoreCommitNotifyData *committedData) + const DataOperStatus &status, SingleVerNaturalStoreCommitNotifyData *committedData) { if (committedData == nullptr) { return; @@ -1363,7 +1363,7 @@ int SQLiteSingleVerStorageExecutor::SaveSyncDataItem(DataItem &dataItem, const D std::string origDev = GetOriginDevName(dataItem, notify.getData.origDev); errCode = SaveSyncDataToDatabase(dataItem, notify.hashKey, origDev, deviceInfo.deviceName, isUpdate); if (errCode == E_OK) { - PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, notify.hashKey, committedData); + PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, committedData); maxStamp = std::max(dataItem.timestamp, maxStamp); } else { LOGE("Save sync data to db failed:%d", errCode); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h index 92eafb275e51a9bfeba575b84b87fc2d69c7ae55..64ebe43fca0fc468d61ca11a14f21d9af2a91d36 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h @@ -263,7 +263,7 @@ private: }; void PutIntoCommittedData(const DataItem &itemPut, const DataItem &itemGet, const DataOperStatus &status, - const Key &hashKey, SingleVerNaturalStoreCommitNotifyData *committedData); + SingleVerNaturalStoreCommitNotifyData *committedData); static int BindSavedSyncData(sqlite3_stmt *statement, const DataItem &dataItem, const Key &hashKey, const SyncDataDevices &devices, bool isUpdate); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp index f236a4fdb81a30aede5d1944f2a9d61f2a5ff3b0..8df1cfc29ab009c4957525fabfa676e667b8f56e 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp @@ -773,7 +773,7 @@ int SQLiteSingleVerStorageExecutor::PutIntoConflictAndCommitForMigrateCache(Data return ResetForMigrateCacheData(); } - PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, notify.hashKey, notify.committedData); + PutIntoCommittedData(dataItem, notify.getData, notify.dataStatus, notify.committedData); return ResetForMigrateCacheData(); } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp index 3ab1bd85d32ca8885c25e123d41ad72dc3eed2a4..061d9b89ffacf9ea8cc459e958fd622fc3da60df 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_subscribe.cpp @@ -150,7 +150,7 @@ int SQLiteSingleVerStorageExecutor::AddSubscribeTrigger(QueryObject &query, cons { if (executorState_ == ExecutorState::CACHEDB || executorState_ == ExecutorState::CACHE_ATTACH_MAIN) { LOGE("Not support add subscribe in cache db."); - return -E_NOT_SUPPORT; + return -E_EKEYREVOKED; } int errCode = E_OK; SqliteQueryHelper helper = query.GetQueryHelper(errCode); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp index b7ed3b1fc438e5798b8a4837641efdd0e2cad3ed..4d004150816a77f5a1a4e1ee0f1bb35026b8af84 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.cpp @@ -120,7 +120,7 @@ EngineState SQLiteStorageEngine::GetEngineState() const void SQLiteStorageEngine::SetEngineState(EngineState state) { LOGD("[SQLiteStorageEngine::SetEngineState] Engine State : [%d]", state); - engineState_ = state; // Current usage logically can guarante no concurrency + engineState_ = state; // Current usage logically can guarantee no concurrency } const OpenDbProperties &SQLiteStorageEngine::GetOpenOption() const @@ -171,7 +171,7 @@ int SQLiteStorageEngine::CheckEngineOption(const KvDBProperties &kvDBProp) const securityOpt.securityFlag = kvDBProp.GetSecFlag(); } - int conflictReslovePolicy = kvDBProp.GetIntProp(KvDBProperties::CONFLICT_RESOLVE_POLICY, DEFAULT_LAST_WIN); + int conflictResolvePolicy = kvDBProp.GetIntProp(KvDBProperties::CONFLICT_RESOLVE_POLICY, DEFAULT_LAST_WIN); bool createDirByStoreIdOnly = kvDBProp.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false); if (kvDBProp.GetSchemaConstRef().IsSchemaValid() == option_.schema.empty()) { @@ -194,7 +194,7 @@ int SQLiteStorageEngine::CheckEngineOption(const KvDBProperties &kvDBProp) const if (isMemDb == false && option_.createDirByStoreIdOnly == createDirByStoreIdOnly && option_.securityOpt == securityOpt && - option_.conflictReslovePolicy == conflictReslovePolicy) { + option_.conflictReslovePolicy == conflictResolvePolicy) { return E_OK; } return -E_INVALID_ARGS; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h index 068448da680f5725a2d0bf3ff21b8e2d771f705b..bed316466340fc8bb9fcbbe3c233ea897b66f86d 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h @@ -13,14 +13,14 @@ * limitations under the License. */ -#ifndef SQLITE_KVDB_HANDLE_POOL_H -#define SQLITE_KVDB_HANDLE_POOL_H +#ifndef SQLITE_STORAGE_ENGINE_H +#define SQLITE_STORAGE_ENGINE_H #include #include "macro_utils.h" -#include "storage_engine.h" #include "sqlite_utils.h" +#include "storage_engine.h" namespace DistributedDB { class SQLiteStorageEngine : public StorageEngine { @@ -71,4 +71,4 @@ protected: OpenDbProperties option_; }; } // namespace DistributedDB -#endif // SQLITE_DB_HANDLE_H +#endif // SQLITE_STORAGE_ENGINE_H diff --git a/frameworks/libs/distributeddb/storage/src/storage_engine.cpp b/frameworks/libs/distributeddb/storage/src/storage_engine.cpp index 73727a8d9b4faae50477bd3d9903d31e4df13176..afc52c469528d839bd6cdfa68d272e8fe355026f 100644 --- a/frameworks/libs/distributeddb/storage/src/storage_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/storage_engine.cpp @@ -55,7 +55,7 @@ int StorageEngine::Init() if (engineAttr_.minReadNum == 0 && engineAttr_.minWriteNum == 0) { errCode = CreateNewExecutor(true, handle); if (errCode != E_OK) { - goto ERROR; + goto END; } if (handle != nullptr) { @@ -68,7 +68,7 @@ int StorageEngine::Init() handle = nullptr; errCode = CreateNewExecutor(true, handle); if (errCode != E_OK) { - goto ERROR; + goto END; } AddStorageExecutor(handle); } @@ -77,13 +77,13 @@ int StorageEngine::Init() handle = nullptr; errCode = CreateNewExecutor(false, handle); if (errCode != E_OK) { - goto ERROR; + goto END; } AddStorageExecutor(handle); } isInitialized_ = true; -ERROR: +END: if (errCode != E_OK) { // Assumed file system has classification function, can only get one write handle if (errCode == -E_EKEYREVOKED && !writeIdleList_.empty()) { @@ -329,7 +329,6 @@ void StorageEngine::SetNotifiedCallback(const std::function lock(notifyMutex_); commitNotifyFunc_ = callback; - return; } void StorageEngine::SetConnectionFlag(bool isExisted) diff --git a/frameworks/libs/distributeddb/storage/src/storage_executor.h b/frameworks/libs/distributeddb/storage/src/storage_executor.h index 9645cef7a61b010c1240c2b5ed8dbfef8a41c3d1..224ee2b684329391e286a8914136fd3386484d13 100644 --- a/frameworks/libs/distributeddb/storage/src/storage_executor.h +++ b/frameworks/libs/distributeddb/storage/src/storage_executor.h @@ -23,7 +23,7 @@ enum EngineState { INVALID = -1, // default value, representative database is not generated CACHEDB, ATTACHING, // main db and cache db attach together - MIGRATING, // begine to Migrate data + MIGRATING, // began to Migrate data MAINDB, ENGINE_BUSY, // In order to change handle during the migration process, it is temporarily unavailable }; diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp index ca10da6dba362090e65b2c659c4f3e7a3e2fe2d7..d55ccfb2a4104f522d451b8d0a12cfe4f4ebe0a9 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb_connection.cpp @@ -65,7 +65,7 @@ void SyncAbleKvDBConnection::InitPragmaFunc() {PRAGMA_PERFORMANCE_ANALYSIS_CLOSE, [](void *parameter, int &errCode) { PerformanceAnalysis::GetInstance()->ClosePerformanceAnalysis(); }}, {PRAGMA_PERFORMANCE_ANALYSIS_SET_REPORTFILENAME, [](void *parameter, int &errCode) { - PerformanceAnalysis::GetInstance()->SetFileNumber(*(static_cast(parameter))); }}, + PerformanceAnalysis::GetInstance()->SetFileName(*(static_cast(parameter))); }}, {PRAGMA_GET_QUEUED_SYNC_SIZE, [this](void *parameter, int &errCode) { errCode = GetQueuedSyncSize(static_cast(parameter)); }}, {PRAGMA_SET_QUEUED_SYNC_LIMIT, [this](void *parameter, int &errCode) { diff --git a/frameworks/libs/distributeddb/syncer/include/isyncer.h b/frameworks/libs/distributeddb/syncer/include/isyncer.h index 400ad99fdec4e40f88ab058d9aa5509f3949ad37..15ea8ae4c76e9392390fd78488f6efc6fbb717ad 100644 --- a/frameworks/libs/distributeddb/syncer/include/isyncer.h +++ b/frameworks/libs/distributeddb/syncer/include/isyncer.h @@ -18,8 +18,9 @@ #define I_SYNCER_H #include -#include #include +#include +#include #include "distributeddb/result_set.h" #include "isync_interface.h" diff --git a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h index a867eee72dd5117039c8162978f6459c13560ab0..90f3bc6009ce48de1bc239236955f38fc20d2f47 100644 --- a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h @@ -63,6 +63,9 @@ public: // check if need trigger query auto sync and get query from inMsg virtual bool IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyncObject &query) = 0; + // Notify machine is closing, should release some lock + virtual void NotifyClosing() = 0; + // start a timer to ResetWatchDog when get data and send notify ack if need virtual void StartFeedDogForGetData(uint32_t sessionId) = 0; diff --git a/frameworks/libs/distributeddb/syncer/src/isync_task_context.h b/frameworks/libs/distributeddb/syncer/src/isync_task_context.h index 3c4146e429ebb52c7912e93316b24a822716b306..d14caa4ae98e6006767bc12ebce0e5e5f043d6dc 100644 --- a/frameworks/libs/distributeddb/syncer/src/isync_task_context.h +++ b/frameworks/libs/distributeddb/syncer/src/isync_task_context.h @@ -149,7 +149,7 @@ public: virtual uint32_t GetRemoteSoftwareVersion() const = 0; // Get the remount software version id, when called GetRemoteSoftwareVersion this id will be increase. - // Used to check if the version num is is overdue + // Used to check if the version num is overdue virtual uint64_t GetRemoteSoftwareVersionId() const = 0; // Judge if the communicator is normal diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp index 291518d1f412107711b7293b604f5191234c253a..b4fd0d634deca19861d5b08bda53c474b3ac58c4 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp @@ -325,7 +325,7 @@ int SingleVerDataSync::GetUnsyncData(SingleVerSyncTaskContext *context, std::vec SyncType curType = (context->IsQuerySync()) ? SyncType::QUERY_SYNC_TYPE : SyncType::MANUAL_FULL_SYNC_TYPE; GetLocalWaterMark(curType, context->GetQuerySyncId(), context, startMark); WaterMark endMark = MAX_TIMESTAMP; - if ((endMark == 0) || (startMark > endMark)) { + if ((startMark > endMark)) { return E_OK; } ContinueToken token = nullptr; @@ -833,8 +833,8 @@ int SingleVerDataSync::PullResponseStart(SingleVerSyncTaskContext *context) return errCode; } -void SingleVerDataSync::UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, SyncTimeRange &dataTime, - const SingleVerSyncTaskContext *context, UpdateWaterMark isUpdateWaterMark) +void SingleVerDataSync::UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, + const SyncTimeRange &dataTime, const SingleVerSyncTaskContext *context, UpdateWaterMark isUpdateWaterMark) { WaterMark tmpPeerWatermark = dataTime.endTime; WaterMark tmpPeerDeletedWatermark = dataTime.deleteEndTime; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h index 4e9343aece32f3d8adf78156db81dabb8398b361..4b212023154617926a7ed999bef653330bba78e8 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h @@ -180,7 +180,7 @@ protected: int SendDataPacket(SyncType syncType, const DataRequestPacket *packet, SingleVerSyncTaskContext *context); - void UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, SyncTimeRange &dataTime, + void UpdateQueryPeerWaterMark(SyncType syncType, const std::string &queryId, const SyncTimeRange &dataTime, const SingleVerSyncTaskContext *context, UpdateWaterMark isUpdateWaterMark); void UpdatePeerWaterMark(SyncType syncType, const std::string &queryId, const SingleVerSyncTaskContext *context, diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp index 2827ef3758a52876d8ef91388e83bf89e9f1e98c..51e2b47b604816c55b28d8d1c718eb3624e30496 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.cpp @@ -257,7 +257,7 @@ void SingleVerDataSyncUtils::FillControlRequestPacket(ControlRequestPacket *pack uint32_t version = std::min(context->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT); uint32_t flag = 0; if (context->GetMode() == SyncModeType::SUBSCRIBE_QUERY && context->IsAutoSubscribe()) { - flag = flag | SubscribeRequest::IS_AUTO_SUBSCRIBE; + flag = SubscribeRequest::IS_AUTO_SUBSCRIBE; } packet->SetPacketHead(E_OK, version, GetControlCmdType(context->GetMode()), flag); packet->SetQuery(context->GetQuery()); @@ -298,7 +298,7 @@ bool SingleVerDataSyncUtils::IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyn uint32_t controlCmdType = packet->GetcontrolCmdType(); if (controlCmdType == ControlCmdType::SUBSCRIBE_QUERY_CMD && inMsg->GetMessageType() == TYPE_REQUEST) { const SubscribeRequest *subPacket = inMsg->GetObject(); - if (packet == nullptr) { + if (subPacket == nullptr) { return false; } query = subPacket->GetQuery(); @@ -398,7 +398,7 @@ SyncTimeRange SingleVerDataSyncUtils::GetQuerySyncDataTimeRange(const std::vecto return dataTimeRange; } -SyncTimeRange SingleVerDataSyncUtils::ReviseLocalMark(SyncType syncType, SyncTimeRange &dataTimeRange, +SyncTimeRange SingleVerDataSyncUtils::ReviseLocalMark(SyncType syncType, const SyncTimeRange &dataTimeRange, UpdateWaterMark updateMark) { SyncTimeRange tmpDataTime = dataTimeRange; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h index 09ff9780c55d029d9d311eb48936e2e84967b5ad..e42472a6075092a1ff3e52244fd1fcf3d6c626e6 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h @@ -79,7 +79,8 @@ public: static SyncTimeRange GetQuerySyncDataTimeRange(const std::vector &inData, WaterMark localMark, WaterMark deleteLocalMark, UpdateWaterMark &isUpdate); - static SyncTimeRange ReviseLocalMark(SyncType syncType, SyncTimeRange &dataTimeRange, UpdateWaterMark updateMark); + static SyncTimeRange ReviseLocalMark(SyncType syncType, const SyncTimeRange &dataTimeRange, + UpdateWaterMark updateMark); static SyncTimeRange GetRecvDataTimeRange(SyncType syncType, const std::vector &data, UpdateWaterMark &isUpdate); diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp index c9abb08a4d78dccd2e231cd0dbb52dc5274acbbf..8164e1ccb0bab29b75452ccf5ba371ef9de03375 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_kv_syncer.cpp @@ -83,19 +83,19 @@ void SingleVerKVSyncer::LocalDataChanged(int notifyEvent) return; } triggerSyncTask_ = false; + std::vector devices; + GetOnlineDevices(devices); + if (devices.empty()) { + LOGI("[Syncer] LocalDataChanged no online devices, Label=%s", label_.c_str()); + triggerSyncTask_ = true; + return; + } RefObject::IncObjRef(syncEngine_); // To avoid many task were produced and waiting in the queue. For example, put value in a loop. // It will consume thread pool resources, so other task will delay until these task finish. // In extreme situation, 10 thread run the localDataChanged task and 1 task waiting in queue. - int errCode = RuntimeContext::GetInstance()->ScheduleTask([this] { + int errCode = RuntimeContext::GetInstance()->ScheduleTask([this, devices] { triggerSyncTask_ = true; - std::vector devices; - GetOnlineDevices(devices); - if (devices.empty()) { - LOGI("[Syncer] LocalDataChanged no online devices, Label=%s", label_.c_str()); - RefObject::DecObjRef(syncEngine_); - return; - } if (!TryFullSync(devices)) { TriggerSubQuerySync(devices); } diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp index 3f85cb9edbbdca1ad7eeee131b960483d6013b98..13988c72c7dde204a21a3dad3ee4f7ac9f033df1 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_engine.cpp @@ -103,8 +103,8 @@ int SingleVerSyncEngine::SubscribeTimeOut(TimerId id) LOGI("no need to trigger auto subscribe"); return E_OK; } - for (auto &item : allSyncQueries) { - for (auto &query : item.second) { + for (const auto &item : allSyncQueries) { + for (const auto &query : item.second) { InternalSyncParma param; GetSubscribeSyncParam(item.first, query, param); queryAutoSyncCallback_(param); diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index 1ad51ce9f52b8adcaf5a9839ea9f09584ade823d..27de0c7a7dc2edb857a900a96d48550882be776c 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -298,7 +298,7 @@ void SingleVerSyncStateMachine::CommErrAbort(uint32_t sessionId) { std::lock_guard lock(stateMachineLock_); uint32_t requestSessionId = context_->GetRequestSessionId(); - if ((sessionId != 0) && ((sessionId != requestSessionId) || (requestSessionId == 0))) { + if ((sessionId != 0) && ((requestSessionId == 0) || (sessionId != requestSessionId))) { return; } context_->SetCommNormal(false); @@ -645,34 +645,39 @@ int SingleVerSyncStateMachine::HandleDataRequestRecv(const Message *inMsg) performance->StepTimeRecordStart(PT_TEST_RECORDS::RECORD_DATA_REQUEST_RECV_TO_SEND_ACK); } DecRefCountOfFeedDogTimer(SyncDirectionFlag::RECEIVE); + + // RequestRecv will save data, it may cost a long time. + // So we need to send save data notify to keep remote alive. + bool isNeedStop = StartSaveDataNotify(inMsg->GetSessionId(), inMsg->GetSequenceId(), inMsg->GetMessageId()); { std::lock_guard lockWatchDog(stateMachineLock_); if (IsNeedResetWatchdog(inMsg)) { (void)ResetWatchDog(); } } - - // RequestRecv will save data, it may cost a long time. - // So we need to send save data notify to keep remote alive. - bool isNeedStop = StartSaveDataNotify(inMsg->GetSessionId(), inMsg->GetSequenceId(), inMsg->GetMessageId()); WaterMark pullEndWaterkark = 0; errCode = dataSync_->DataRequestRecv(context_, inMsg, pullEndWaterkark); if (performance != nullptr) { performance->StepTimeRecordEnd(PT_TEST_RECORDS::RECORD_DATA_REQUEST_RECV_TO_SEND_ACK); } - if (isNeedStop) { - StopSaveDataNotify(); - } // only higher than 102 version receive this errCode here. // while both RequestSessionId is not equal,but get this errCode;slwr would seem to handle first secquencid. // so while receive the same secquencid after abiitysync it wouldn't handle. if (errCode == -E_NEED_ABILITY_SYNC) { + if (isNeedStop) { + StopSaveDataNotify(); + } return errCode; } - std::lock_guard lock(stateMachineLock_); - DataRecvErrCodeHandle(inMsg->GetSessionId(), errCode); - if (pullEndWaterkark > 0) { - AddPullResponseTarget(inMsg, pullEndWaterkark); + { + std::lock_guard lock(stateMachineLock_); + DataRecvErrCodeHandle(inMsg->GetSessionId(), errCode); + if (pullEndWaterkark > 0) { + AddPullResponseTarget(inMsg, pullEndWaterkark); + } + } + if (isNeedStop) { + StopSaveDataNotify(); } return E_OK; } @@ -1062,7 +1067,7 @@ bool SingleVerSyncStateMachine::IsNeedResetWatchdog(const Message *inMsg) const return false; } -Event SingleVerSyncStateMachine::TransforTimeOutErrCodeToEvent() +Event SingleVerSyncStateMachine::TransforTimeOutErrCodeToEvent() const { if (syncContext_->IsSyncTaskNeedRetry() && (syncContext_->GetRetryTime() < syncContext_->GetSyncRetryTimes())) { return Event::WAIT_TIME_OUT_EVENT; @@ -1252,4 +1257,11 @@ void SingleVerSyncStateMachine::InnerErrorAbort(uint32_t sessionId) SyncStep(); } } + +void SingleVerSyncStateMachine::NotifyClosing() +{ + if (timeSync_ != nullptr) { + timeSync_->Close(); + } +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h index 18654f9caa4abcbad61d30aa5e656758b03b0843..59df3bccfcb75d6c7eae30675847a4142327fdef 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h @@ -97,6 +97,8 @@ public: uint64_t &outValue); void InnerErrorAbort(uint32_t sessionId) override; + + void NotifyClosing() override; protected: // Step the SingleVerSyncStateMachine void SyncStep() override; @@ -199,7 +201,7 @@ private: bool IsNeedResetWatchdog(const Message *inMsg) const; - Event TransforTimeOutErrCodeToEvent(); + Event TransforTimeOutErrCodeToEvent() const; bool AbilityMsgSessionIdCheck(const Message *inMsg); diff --git a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp index f0facc99739be521fef358a6589ad2432946009b..c1de6980657f566f258d853c55fc55f78f356593 100644 --- a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp +++ b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp @@ -97,7 +97,7 @@ void SubscribeManager::DeleteRemoteSubscribeQuery(const std::string &device, con } void SubscribeManager::PutLocalUnFiniedSubQueries(const std::string &device, - std::vector &subscribeQueries) + const std::vector &subscribeQueries) { LOGI("[SubscribeManager] put local unfinished subscribe queries, nums=%zu", subscribeQueries.size()); std::unique_lock lockGuard(localSubscribeMapLock_); diff --git a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h index 42ca070c54b65850421b2d62c5ce5bc16d6283c4..87a38556c2e8a4e814e1815a457534ddafeb2f85 100644 --- a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h +++ b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.h @@ -63,7 +63,7 @@ public: void DeleteRemoteSubscribeQuery(const std::string &device, const QuerySyncObject &query); // put subscribe queries into unfinished map when remote db online - void PutLocalUnFiniedSubQueries(const std::string &device, std::vector &subscribeQueries); + void PutLocalUnFiniedSubQueries(const std::string &device, const std::vector &subscribeQueries); // get all device unFinished subscribe queries which triggered by auto subscribe and need retry subscribe void GetAllUnFinishSubQueries(std::map> &allSyncQueries) const; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp index d4685183809ee330551bbe2aa39e7a8e96580bf8..8dae7991b4e97b7c23a5f671f48602623fff3c5c 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.cpp @@ -113,12 +113,6 @@ int SyncEngine::Close() UnRegCommunicatorsCallback(); StopAutoSubscribeTimer(); - std::unique_lock closeLock(execTaskCountLock_); - bool isTimeout = execTaskCv_.wait_for(closeLock, std::chrono::milliseconds(DBConstant::MIN_TIMEOUT), - [this]() { return execTaskCount_ == 0; }); - if (!isTimeout) { - LOGD("SyncEngine Close with executing task!"); - } // Clear SyncContexts { std::unique_lock lock(contextMapLock_); @@ -133,6 +127,7 @@ int SyncEngine::Close() syncTaskContextMap_.clear(); } + WaitingExecTaskExist(); ReleaseCommunicators(); std::lock_guard msgLock(queueLock_); while (!msgQueue_.empty()) { @@ -508,6 +503,7 @@ int SyncEngine::MessageReciveCallbackInner(const std::string &targetDev, Message DecExecTaskCount(); return -E_BUSY; } + RefObject::DecObjRef(executor); int msgSize = 0; if (!IsSkipCalculateLen(inMsg)) { msgSize = GetMsgSize(inMsg); @@ -633,9 +629,16 @@ ISyncTaskContext *SyncEngine::GetSyncTaskContext(const std::string &deviceId, in syncTaskContextMap_.insert(std::pair(deviceId, context)); // IncRef for SyncEngine to make sure SyncEngine is valid when context access RefObject::IncObjRef(this); - context->OnLastRef([this, deviceId]() { + auto storage = syncInterface_; + if (storage != nullptr) { + storage->IncRefCount(); + } + context->OnLastRef([this, deviceId, storage]() { LOGD("[SyncEngine] SyncTaskContext for id %s finalized", STR_MASK(deviceId)); RefObject::DecObjRef(this); + if (storage != nullptr) { + storage->DecRefCount(); + } }); context->RegOnSyncTask(std::bind(&SyncEngine::ExecSyncTask, this, context)); return context; @@ -762,7 +765,7 @@ void SyncEngine::SetEqualIdentifier() equalIdentifier[item.second].push_back(item.first); } } - for (auto &item : equalIdentifier) { + for (const auto &item : equalIdentifier) { SetEqualIdentifier(item.first, item.second); } } @@ -776,7 +779,7 @@ void SyncEngine::SetEqualIdentifierMap(const std::string &identifier, const std: } iter++; } - for (auto &device : targets) { + for (const auto &device : targets) { equalIdentifierMap_[device] = identifier; } } @@ -832,7 +835,7 @@ void SyncEngine::GetRemoteSubscribeQueries(const std::string &device, std::vecto subManager_->GetRemoteSubscribeQueries(device, subscribeQueries); } -void SyncEngine::PutUnfiniedSubQueries(const std::string &device, std::vector &subscribeQueries) +void SyncEngine::PutUnfiniedSubQueries(const std::string &device, const std::vector &subscribeQueries) { subManager_->PutLocalUnFiniedSubQueries(device, subscribeQueries); } @@ -989,7 +992,7 @@ bool SyncEngine::IsEngineActive() const void SyncEngine::SchemaChange() { std::lock_guard lock(contextMapLock_); - for (auto &entry : syncTaskContextMap_) { + for (const auto &entry : syncTaskContextMap_) { auto context = entry.second; if (context->IsKilled()) { continue; @@ -1039,6 +1042,7 @@ int SyncEngine::RemoteQuery(const std::string &device, const RemoteCondition &co { RemoteExecutor *executor = GetAndIncRemoteExector(); if (!isActive_ || executor == nullptr) { + RefObject::DecObjRef(executor); return -E_BUSY; // db is closing just return } int errCode = executor->RemoteQuery(device, condition, timeout, connectionId, result); @@ -1050,6 +1054,7 @@ void SyncEngine::NotifyConnectionClosed(uint64_t connectionId) { RemoteExecutor *executor = GetAndIncRemoteExector(); if (!isActive_ || executor == nullptr) { + RefObject::DecObjRef(executor); return; // db is closing just return } executor->NotifyConnectionClosed(connectionId); @@ -1060,6 +1065,7 @@ void SyncEngine::NotifyUserChange() { RemoteExecutor *executor = GetAndIncRemoteExector(); if (!isActive_ || executor == nullptr) { + RefObject::DecObjRef(executor); return; // db is closing just return } executor->NotifyUserChange(); @@ -1105,7 +1111,7 @@ void SyncEngine::AbortMachineIfNeed(uint32_t syncId) ISyncTaskContext *abortContext = nullptr; { std::lock_guard lock(contextMapLock_); - for (auto &entry : syncTaskContextMap_) { + for (const auto &entry : syncTaskContextMap_) { auto context = entry.second; if (context->IsKilled()) { continue; @@ -1123,4 +1129,14 @@ void SyncEngine::AbortMachineIfNeed(uint32_t syncId) RefObject::DecObjRef(abortContext); } } + +void SyncEngine::WaitingExecTaskExist() +{ + std::unique_lock closeLock(execTaskCountLock_); + bool isTimeout = execTaskCv_.wait_for(closeLock, std::chrono::milliseconds(DBConstant::MIN_TIMEOUT), + [this]() { return execTaskCount_ == 0; }); + if (!isTimeout) { + LOGD("SyncEngine Close with executing task!"); + } +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.h b/frameworks/libs/distributeddb/syncer/src/sync_engine.h index 3de46f37d9c34bef3138d159fee2968855e7a24e..3c529f6a87e8891961862b466802b3c13de913ce 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.h @@ -93,7 +93,7 @@ public: void GetRemoteSubscribeQueries(const std::string &device, std::vector &subscribeQueries); - void PutUnfiniedSubQueries(const std::string &device, std::vector &subscribeQueries); + void PutUnfiniedSubQueries(const std::string &device, const std::vector &subscribeQueries); void GetAllUnFinishSubQueries(std::map> &allSyncQueries); @@ -206,6 +206,8 @@ private: int GetLocalDeviceId(std::string &deviceId); + void WaitingExecTaskExist(); + ICommunicator *communicator_; DeviceManager *deviceManager_; std::function onRemoteDataChanged_; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index a8d1a649d747e4094e4333393b7f77787f08374f..666332660cae632767bfee6a0dff82c54cca7825 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -31,6 +31,8 @@ SyncStateMachine::SyncStateMachine() currentSyncProctolVersion_(SINGLE_VER_SYNC_PROCTOL_V3), saveDataNotifyTimerId_(0), saveDataNotifyCount_(0), + waitingResetLockBySaveData_(false), + saveDataNotifyRefCount_(0), getDataNotifyTimerId_(0), getDataNotifyCount_(0) { @@ -223,10 +225,11 @@ void SyncStateMachine::StopWatchDog() bool SyncStateMachine::StartSaveDataNotify(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) { std::lock_guard lockGuard(saveDataNotifyLock_); + saveDataNotifyRefCount_++; if (saveDataNotifyTimerId_ > 0) { saveDataNotifyCount_ = 0; LOGW("[SyncStateMachine][SaveDataNotify] timer has been started!"); - return false; + return true; } // Incref to make sure context still alive before timer stopped. @@ -249,6 +252,7 @@ bool SyncStateMachine::StartSaveDataNotify(uint32_t sessionId, uint32_t sequence saveDataNotifyTimerId_); if (errCode != E_OK) { LOGW("[SyncStateMachine][SaveDataNotify] start timer failed err %d !", errCode); + saveDataNotifyRefCount_--; return false; } return true; @@ -266,9 +270,14 @@ void SyncStateMachine::StopSaveDataNotifyNoLock() LOGI("[SyncStateMachine][SaveDataNotify] timer is not started!"); return; } + saveDataNotifyRefCount_--; + if (saveDataNotifyRefCount_ > 0) { + return; + } RuntimeContext::GetInstance()->RemoveTimer(saveDataNotifyTimerId_); saveDataNotifyTimerId_ = 0; saveDataNotifyCount_ = 0; + saveDataNotifyRefCount_ = 0; } bool SyncStateMachine::StartFeedDogForSync(uint32_t time, SyncDirectionFlag flag) @@ -375,17 +384,26 @@ void SyncStateMachine::DecRefCountOfFeedDogTimer(SyncDirectionFlag flag) void SyncStateMachine::DoSaveDataNotify(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) { + // we send notify packet at first, because it will cost a lot of time to get machine lock { - std::lock_guard lock(stateMachineLock_); - (void)ResetWatchDog(); + std::lock_guard innerLock(saveDataNotifyLock_); + if (saveDataNotifyCount_ >= MAX_DATA_NOTIFY_COUNT) { + StopSaveDataNotifyNoLock(); + return; + } + SendNotifyPacket(sessionId, sequenceId, inMsgId); + saveDataNotifyCount_++; + if (waitingResetLockBySaveData_) { + return; + } + waitingResetLockBySaveData_ = true; } - std::lock_guard innerLock(saveDataNotifyLock_); - if (saveDataNotifyCount_ >= MAX_DATA_NOTIFY_COUNT) { - StopSaveDataNotifyNoLock(); - return; + std::lock_guard lock(stateMachineLock_); + { + std::lock_guard innerLock(saveDataNotifyLock_); + waitingResetLockBySaveData_ = false; } - SendNotifyPacket(sessionId, sequenceId, inMsgId); - saveDataNotifyCount_++; + (void)ResetWatchDog(); } void SyncStateMachine::DoFeedDogForSync(SyncDirectionFlag flag) @@ -408,6 +426,11 @@ void SyncStateMachine::InnerErrorAbort(uint32_t sessionId) (void) sessionId; } +void SyncStateMachine::NotifyClosing() +{ + // do nothing +} + void SyncStateMachine::StartFeedDogForGetData(uint32_t sessionId) { std::lock_guard lockGuard(getDataNotifyLock_); diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index 4b8cdccbd1a7bcfff026901633fedf239e03e1df..477dc13d24a0ff438fca8c8b9e9e19573948e262 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -77,6 +77,9 @@ public: // start a timer to ResetWatchDog when get data and stop send notify ack if need void StopFeedDogForGetData() override; + + // Notify machine is closing, should release some lock + void NotifyClosing() override; protected: // SyncOperation is timeout, step to timeout state @@ -167,6 +170,8 @@ protected: std::mutex saveDataNotifyLock_; TimerId saveDataNotifyTimerId_; uint8_t saveDataNotifyCount_; + bool waitingResetLockBySaveData_; + int32_t saveDataNotifyRefCount_; std::mutex getDataNotifyLock_; TimerId getDataNotifyTimerId_; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp index 01481034db348d15aed3828f6ec7d08bd684d628..dcbf754068a5d46470449dccb7dead7e324873dc 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp @@ -536,15 +536,28 @@ void SyncTaskContext::CommErrHandlerFuncInner(int errCode, uint32_t sessionId) int SyncTaskContext::TimeOut(TimerId id) { - int errCode = E_OK; if (!timeOutCallback_) { - return errCode; + return E_OK; } - if (IncUsedCount() == E_OK) { - errCode = timeOutCallback_(id); + int errCode = IncUsedCount(); + if (errCode != E_OK) { + LOGW("[SyncTaskContext][TimeOut] IncUsedCount failed! errCode=", errCode); + // if return is not E_OK, the timer will be removed + // we removed timer when context call StopTimer + return E_OK; + } + IncObjRef(this); + errCode = RuntimeContext::GetInstance()->ScheduleTask([this, id]() { + timeOutCallback_(id); + SafeExit(); + DecObjRef(this); + }); + if (errCode != E_OK) { + LOGW("[SyncTaskContext][Timeout] Trigger Timeout Async Failed! TimerId=" PRIu64 " errCode=%d", id, errCode); SafeExit(); + DecObjRef(this); } - return errCode; + return E_OK; } void SyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam &taskParam) @@ -591,6 +604,7 @@ void SyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam void SyncTaskContext::KillWait() { StopTimer(); + stateMachine_->NotifyClosing(); UnlockObj(); stateMachine_->AbortImmediately(); LockObj(); diff --git a/frameworks/libs/distributeddb/syncer/src/time_sync.cpp b/frameworks/libs/distributeddb/syncer/src/time_sync.cpp index 9d7f57bbbcf5720e5dacb5d8e0f77a2cab5b4940..bcc8f28eb75460c62a0e03bebf1f569625b273aa 100644 --- a/frameworks/libs/distributeddb/syncer/src/time_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/time_sync.cpp @@ -117,7 +117,8 @@ TimeSync::TimeSync() isAckReceived_(false), timeChangedListener_(nullptr), timeDriverLockCount_(0), - isOnline_(true) + isOnline_(true), + closed_(false) { } @@ -149,7 +150,7 @@ int TimeSync::RegisterTransformFunc() return MessageTransform::RegTransformFunction(TIME_SYNC_MESSAGE, func); } -int TimeSync::Initialize(ICommunicator *communicator, std::shared_ptr &metadata, +int TimeSync::Initialize(ICommunicator *communicator, const std::shared_ptr &metadata, const ISyncInterface *storage, const DeviceID &deviceId) { if ((communicator == nullptr) || (storage == nullptr) || (metadata == nullptr)) { @@ -504,7 +505,7 @@ int TimeSync::GetTimeOffset(TimeOffset &outOffset, uint32_t timeout, uint32_t se TimeHelper::GetSysCurrentTime(), errCode, timeout); std::unique_lock lock(cvLock_); if (errCode != E_OK || !conditionVar_.wait_for(lock, std::chrono::milliseconds(timeout), - [this](){ return this->isAckReceived_ == true; })) { + [this](){ return this->isAckReceived_ || this->closed_; })) { LOGD("TimeSync::GetTimeOffset, retryTime_ = %d", retryTime_); retryTime_++; if (retryTime_ < MAX_RETRY_TIME) { @@ -516,6 +517,9 @@ int TimeSync::GetTimeOffset(TimeOffset &outOffset, uint32_t timeout, uint32_t se return -E_TIMEOUT; } } + if (IsClosed()) { + return -E_BUSY; + } retryTime_ = 0; metadata_->GetTimeOffset(deviceId_, outOffset); return E_OK; @@ -560,4 +564,19 @@ void TimeSync::ResetTimer() LOGW("[TimeSync] Reset TimeSync timer failed err :%d", errCode); } } + +void TimeSync::Close() +{ + { + std::lock_guard lock(cvLock_); + closed_ = true; + } + conditionVar_.notify_all(); +} + +bool TimeSync::IsClosed() +{ + std::lock_guard lock(cvLock_); + return closed_ ; +} } // namespace DistributedDB \ No newline at end of file diff --git a/frameworks/libs/distributeddb/syncer/src/time_sync.h b/frameworks/libs/distributeddb/syncer/src/time_sync.h index 5aaa8a115c37585fceb3eb20cfc3e24f1c350271..263faac16778ab3c22839667fae592b76e937340 100644 --- a/frameworks/libs/distributeddb/syncer/src/time_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/time_sync.h @@ -71,7 +71,7 @@ public: static int DeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg); // register to communicator - int Initialize(ICommunicator *communicator, std::shared_ptr &metadata, + int Initialize(ICommunicator *communicator, const std::shared_ptr &metadata, const ISyncInterface *storage, const DeviceID &deviceId); int SyncStart(const CommErrHandler &handler = nullptr, uint32_t sessionId = 0); // send timesync request @@ -87,6 +87,8 @@ public: void SetOnline(bool isOnline); + void Close(); + // Used in send msg, as execution is asynchronous, should use this function to handle result. static void CommErrHandlerFunc(int errCode, TimeSync *timeSync); @@ -107,6 +109,8 @@ private: void ResetTimer(); + bool IsClosed(); + ICommunicator *communicateHandle_; std::shared_ptr metadata_; std::unique_ptr timeHelper_; @@ -123,6 +127,7 @@ private: std::mutex timeDriverLock_; int timeDriverLockCount_; bool isOnline_; + bool closed_; static std::mutex timeSyncSetLock_; static std::set timeSyncSet_; }; diff --git a/frameworks/libs/distributeddb/test/BUILD.gn b/frameworks/libs/distributeddb/test/BUILD.gn index d65fed7052392a6a7259e1c7e0d1867b21ca7525..5d8a48d35e6d9cf53fbe59f161ed5028d7b59aa8 100644 --- a/frameworks/libs/distributeddb/test/BUILD.gn +++ b/frameworks/libs/distributeddb/test/BUILD.gn @@ -658,7 +658,7 @@ distributeddb_unittest("DistributedDBCommunicatorProxyTest") { } distributeddb_unittest("DistributedDBSingleVerP2PSubscribeSyncTest") { - sources = [ "unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp" ] + sources = [ "unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp" ] } distributeddb_unittest("DistributedDBMockSyncModuleTest") { diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp index 00b4da375331df1d331bd0c43ab112c449797d2e..873eea992b39183f49a728d2516eb142bec45d82 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp @@ -121,7 +121,9 @@ HWTEST_F(DistributedDBCommonTest, SameProcessReLockFile, TestSize.Level1) // unlock EXPECT_EQ(OS::FileUnlock(fd), E_OK); - EXPECT_EQ(OS::FileUnlock(fd2), E_OK); // unlock success will close fd + EXPECT_EQ(OS::CloseFile(fd), E_OK); + EXPECT_EQ(OS::FileUnlock(fd2), E_OK); + EXPECT_EQ(OS::CloseFile(fd2), E_OK); } /** @@ -138,7 +140,9 @@ HWTEST_F(DistributedDBCommonTest, SameProcessReUnLockFile, TestSize.Level1) OS::FileHandle fd; EXPECT_EQ(OS::OpenFile(g_testDir + "/normalmode", fd), E_OK); EXPECT_EQ(OS::FileUnlock(fd), E_OK); - EXPECT_EQ(OS::FileUnlock(fd), E_OK); // unlock success will close fd + EXPECT_EQ(OS::CloseFile(fd), E_OK); + EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); EXPECT_EQ(OS::FileLock(fd, true), -E_SYSTEM_API_FAIL); EXPECT_EQ(OS::FileLock(fd, true), -E_SYSTEM_API_FAIL); @@ -152,7 +156,9 @@ HWTEST_F(DistributedDBCommonTest, SameProcessReUnLockFile, TestSize.Level1) EXPECT_EQ(OS::FileLock(fd, false), E_OK); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); } /** @@ -263,6 +269,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessLockFileBlocked, TestSize.Level1) EXPECT_EQ(OS::FileLock(ChildFd, true), E_OK); createStepFlag(1); EXPECT_EQ(OS::FileUnlock(ChildFd), E_OK); + EXPECT_EQ(OS::CloseFile(ChildFd), E_OK); LOGI("child process finish!"); exit(0); } else { @@ -273,6 +280,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessLockFileBlocked, TestSize.Level1) } ASSERT_FALSE(waitForStep(1, 10)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); ASSERT_TRUE(waitForStep(1, 10)); } } @@ -319,6 +327,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessGetDBBlocked, TestSize.Level1) // Prevent the child process from not being completed, the main process ends to clean up resources EXPECT_TRUE(waitForStep(2, 1000)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); } /** @@ -365,6 +374,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessDeleteDBBlocked, TestSize.Level1) // Prevent the child process from not being completed, the main process ends to clean up resources EXPECT_TRUE(waitForStep(2, 1000)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); g_mgr.CloseKvStore(g_kvNbDelegatePtr); } @@ -409,6 +419,7 @@ HWTEST_F(DistributedDBCommonTest, DiffProcessGetDBBlocked001, TestSize.Level1) ASSERT_TRUE(waitForStep(1, 100)); EXPECT_EQ(OS::FileUnlock(fd), E_OK); + EXPECT_EQ(OS::CloseFile(fd), E_OK); ASSERT_TRUE(waitForStep(2, 100)); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp index c0dd063026e1c17546fe5d6fef2abe2f6c4ba9e4..9587a2c46f6ab921b5447d8bc032f170378bf413 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_schema_database_upgrade_test.cpp @@ -96,7 +96,7 @@ namespace { } return oriSchemaStr; } - bool SchemaChecker(const std::string schema) + bool SchemaChecker(const std::string &schema) { SchemaObject schemaObj; return (schemaObj.ParseFromSchemaString(schema) == E_OK); diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp index 9f20c2eeb6b98497491a2d4bd97305b67e8a43dd..8ce1573800271b010b53e08598d525b8eed5edf5 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_encrypted_db_test.cpp @@ -86,6 +86,16 @@ void ExecSqlAndAssertOK(sqlite3 *db, const std::string &sql) { ASSERT_EQ(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr), SQLITE_OK); } + +void FreeEntires(std::vector &entries) +{ + for (auto *&it : entries) { + if (it != nullptr) { + delete it; + it = nullptr; + } + } +} } class DistributedDBRelationalEncryptedDbTest : public testing::Test { @@ -254,7 +264,7 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithPasswdInSpli std::vector entries; EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK); EXPECT_EQ(entries.size(), 3u); - + FreeEntires(entries); sqlite3_close(db); RefObject::DecObjRef(g_store); } @@ -356,7 +366,7 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, OpenEncryptedDBWithCustomizedIt std::vector entries; EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK); EXPECT_EQ(entries.size(), 3u); - + FreeEntires(entries); sqlite3_close(db); RefObject::DecObjRef(g_store); } @@ -432,7 +442,7 @@ HWTEST_F(DistributedDBRelationalEncryptedDbTest, RekeyAfterOpenStore_001, TestSi store = GetRelationalStore(); ASSERT_NE(store, nullptr); EXPECT_EQ(store->GetSyncData(query, SyncTimeRange {}, DataSizeSpecInfo {}, token, entries), E_OK); - + FreeEntires(entries); sqlite3_close(db); RefObject::DecObjRef(g_store); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index da6c3175cd97ca66b5bd8a6b16173c35c327a2e2..19abfe73a5063d292dd8f8dea8c1753076279739 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -25,6 +25,7 @@ #include "message.h" #include "mock_auto_launch.h" #include "mock_communicator.h" +#include "mock_kv_sync_interface.h" #include "mock_meta_data.h" #include "mock_remote_executor.h" #include "mock_single_ver_data_sync.h" @@ -45,6 +46,50 @@ using namespace testing; using namespace DistributedDB; using namespace DistributedDBUnitTest; +class TestKvDb { +public: + ~TestKvDb() + { + LOGI("~TestKvDb"); + } + void Initialize(ISyncInterface *syncInterface) + { + syncer_.Initialize(syncInterface, true); + syncer_.EnableAutoSync(true); + } + void LocalChange() + { + syncer_.LocalDataChanged(SQLITE_GENERAL_NS_PUT_EVENT); + } + void Close() + { + syncer_.Close(true); + } +private: + SyncerProxy syncer_; +}; + +class TestInterface: public TestKvDb, public VirtualSingleVerSyncDBInterface { +public: + TestInterface() {} + ~TestInterface() + { + TestKvDb::Close(); + } + void Initialize() + { + TestKvDb::Initialize(this); + } + void TestLocalChange() + { + TestKvDb::LocalChange(); + } + void TestSetIdentifier(std::vector &identifier) + { + VirtualSingleVerSyncDBInterface::SetIdentifier(identifier); + } +}; + namespace { const uint32_t MESSAGE_COUNT = 10u; const uint32_t EXECUTE_COUNT = 2u; @@ -376,6 +421,32 @@ HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck011, TestSize.Level1) EXPECT_EQ(syncTaskContext.IsCommNormal(), false); } +/** + * @tc.name: StateMachineCheck012 + * @tc.desc: Verify Ability LastNotify AckReceive callback. + * @tc.type: FUNC + * @tc.require: AR000DR9K4 + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck012, TestSize.Level1) +{ + MockSingleVerStateMachine stateMachine; + MockSyncTaskContext syncTaskContext; + MockCommunicator communicator; + VirtualSingleVerSyncDBInterface dbSyncInterface; + Init(stateMachine, syncTaskContext, communicator, dbSyncInterface); + EXPECT_CALL(stateMachine, SwitchStateAndStep(_)).WillOnce(Return()); + DistributedDB::Message msg(ABILITY_SYNC_MESSAGE); + msg.SetMessageType(TYPE_NOTIFY); + AbilitySyncAckPacket packet; + packet.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); + packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); + packet.SetAckCode(-E_BUSY); + msg.SetCopiedObject(packet); + EXPECT_EQ(stateMachine.ReceiveMessageCallback(&msg), E_OK); + EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); +} + /** * @tc.name: StateMachineCheck013 * @tc.desc: test kill syncTaskContext. @@ -409,6 +480,20 @@ HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck013, TestSize.Level1) tokenPtr = nullptr; } +/** + * @tc.name: StateMachineCheck014 + * @tc.desc: test machine stop save notify without start. + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck014, TestSize.Level1) +{ + MockSingleVerStateMachine stateMachine; + stateMachine.CallStopSaveDataNotify(); + EXPECT_EQ(stateMachine.GetSaveDataNotifyRefCount(), 0); +} + /** * @tc.name: DataSyncCheck001 * @tc.desc: Test dataSync recv error ack. @@ -767,6 +852,40 @@ HWTEST_F(DistributedDBMockSyncModuleTest, SyncLifeTest001, TestSize.Level3) delete syncDBInterface; } +/** + * @tc.name: SyncLifeTest003 + * @tc.desc: Test syncer localdatachange when store is destructor + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, SyncLifeTest003, TestSize.Level3) +{ + VirtualCommunicatorAggregator *virtualCommunicatorAggregator = new VirtualCommunicatorAggregator(); + RuntimeContext::GetInstance()->SetCommunicatorAggregator(virtualCommunicatorAggregator); + TestInterface *syncDBInterface = new TestInterface(); + const std::string DEVICE_B = "deviceB"; + std::string userId = "userId_0"; + std::string storeId = "storeId_0"; + std::string appId = "appId_0"; + std::string identifier = KvStoreDelegateManager::GetKvStoreIdentifier(userId, appId, storeId); + std::vector identifierVec(identifier.begin(), identifier.end()); + syncDBInterface->TestSetIdentifier(identifierVec); + syncDBInterface->Initialize(); + virtualCommunicatorAggregator->OnlineDevice(DEVICE_B); + std::thread WriteThread([&syncDBInterface] { + syncDBInterface->TestLocalChange(); + }); + std::thread deleteThread([&syncDBInterface] { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + delete syncDBInterface; + }); + deleteThread.join(); + WriteThread.join(); + std::this_thread::sleep_for(std::chrono::seconds(5)); + RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr); +} + /** * @tc.name: MessageScheduleTest001 * @tc.desc: Test MessageSchedule stop timer when no message. @@ -801,9 +920,14 @@ HWTEST_F(DistributedDBMockSyncModuleTest, MessageScheduleTest001, TestSize.Level HWTEST_F(DistributedDBMockSyncModuleTest, SyncEngineTest001, TestSize.Level1) { std::unique_ptr enginePtr = std::make_unique(); - EXPECT_CALL(*enginePtr, CreateSyncTaskContext()).WillRepeatedly(Return(nullptr)); + EXPECT_CALL(*enginePtr, CreateSyncTaskContext()) + .WillRepeatedly(Return(new (std::nothrow) SingleVerKvSyncTaskContext())); VirtualCommunicatorAggregator *virtualCommunicatorAggregator = new VirtualCommunicatorAggregator(); - VirtualSingleVerSyncDBInterface syncDBInterface; + MockKvSyncInterface syncDBInterface; + EXPECT_CALL(syncDBInterface, IncRefCount()).WillOnce(Return()); + EXPECT_CALL(syncDBInterface, DecRefCount()).WillRepeatedly(Return()); + std::vector identifier(COMM_LABEL_LENGTH, 1u); + syncDBInterface.SetIdentifier(identifier); std::shared_ptr metaData = std::make_shared(); ASSERT_NE(virtualCommunicatorAggregator, nullptr); RuntimeContext::GetInstance()->SetCommunicatorAggregator(virtualCommunicatorAggregator); @@ -817,10 +941,13 @@ HWTEST_F(DistributedDBMockSyncModuleTest, SyncEngineTest001, TestSize.Level1) } for (int count = 0; count < 100; count++) { // loop 100 times auto *message = new(std::nothrow) DistributedDB::Message(); + message->SetMessageId(LOCAL_DATA_CHANGED); + message->SetErrorNo(E_FEEDBACK_UNKNOWN_MESSAGE); communicator->CallbackOnMessage("src", message); } }); - std::thread thread2([&enginePtr]() { + std::thread thread2([&]() { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); enginePtr->Close(); }); thread1.join(); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp index 70d3ffbcd2af86d0aef10dfcec00985654e753de..f4cce093ad714720a840e3658c8a4aad89ac6593 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp @@ -770,7 +770,7 @@ static bool IsCommitHistorySyncRequestPacketEqual(const CommitHistorySyncRequest std::map commitMapB; inPacketA.GetCommitMap(commitMapA); inPacketB.GetCommitMap(commitMapB); - for (auto &entry : commitMapA) { + for (const auto &entry : commitMapA) { if (commitMapB.count(entry.first) == 0) { return false; } @@ -778,7 +778,7 @@ static bool IsCommitHistorySyncRequestPacketEqual(const CommitHistorySyncRequest return false; } } - for (auto &entry : commitMapB) { + for (const auto &entry : commitMapB) { if (commitMapA.count(entry.first) == 0) { return false; } @@ -897,7 +897,7 @@ static bool IsCommitHistorySyncAckPacketEqual(const CommitHistorySyncAckPacket & return false; } int count = 0; - for (auto &entry : commitVecA) { + for (const auto &entry : commitVecA) { if (!IsMultiVerCommitEqual(entry, commitVecB[count++])) { return false; } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp index c15b38ba390fcb66154702247ce98b75d8e9a0b5..cdaf305c83b723f9a9a7e3fb5689166dd7544cae 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp @@ -126,6 +126,7 @@ namespace { dev->SetLocalFieldInfo(fieldInfoList); dev->SetTableInfo(tableInfo); } + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } void PrepareData(const std::string &tableName, const std::string &dbPath) @@ -133,6 +134,7 @@ namespace { sqlite3 *db = nullptr; EXPECT_EQ(GetDB(db, dbPath), SQLITE_OK); EXPECT_EQ(InsertValue(db, tableName), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } void OpenStore1(bool syncDualTupleMode = true) @@ -394,6 +396,7 @@ namespace { } EXPECT_EQ(rowCount, 1); sqlite3_finalize(statement); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } } @@ -823,6 +826,7 @@ HWTEST_F(DistributedDBRelationalMultiUserTest, RdbMultiUser008, TestSize.Level1) */ OpenStore1(true); OpenStore2(true); + PrepareEnvironment(g_tableName, g_storePath1, g_rdbDelegatePtr1); /** * @tc.steps: step3. user1 call remote query @@ -887,4 +891,25 @@ HWTEST_F(DistributedDBRelationalMultiUserTest, RdbMultiUser010, TestSize.Level1) Query query = Query::Select(g_tableName); EXPECT_EQ(g_rdbDelegatePtr1->Sync({DEVICE_B}, SYNC_MODE_PUSH_ONLY, query, nullptr, true), OK); CloseStore(); +} + +/** + * @tc.name: multi user 011 + * @tc.desc: test use different option to open store for rdb + * @tc.type: FUNC + * @tc.require: AR000GK58G + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBRelationalMultiUserTest, RdbMultiUser011, TestSize.Level1) +{ + for (int i = 0; i < 2; i++) { + bool syncDualTupleMode = i / 2; + OpenStore1(syncDualTupleMode); + RelationalStoreDelegate::Option option = { g_observer }; + option.syncDualTupleMode = !syncDualTupleMode; + RelationalStoreDelegate *rdbDeletegatePtr = nullptr; + EXPECT_EQ(g_mgr1.OpenStore(g_storePath1, STORE_ID_1, option, rdbDeletegatePtr), MODE_MISMATCH); + EXPECT_EQ(rdbDeletegatePtr, nullptr); + CloseStore(); + } } \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index 9c49cac73959152fd4ec71a526d71f7b43038a60..cbc305229bf454b9ba7b4a7fd14439f06e0b491a 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp @@ -145,7 +145,7 @@ namespace { return sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr); } - int CreateTable(sqlite3 *db, std::vector &fieldInfoList, const std::string &tableName) + int CreateTable(sqlite3 *db, const std::vector &fieldInfoList, const std::string &tableName) { std::string sql = "CREATE TABLE " + tableName + "("; int index = 0; @@ -398,7 +398,7 @@ namespace { } void GetSyncDataStep(std::map &dataMap, sqlite3_stmt *statement, - std::vector fieldInfoList) + std::vector &fieldInfoList) { int columnCount = sqlite3_column_count(statement); ASSERT_EQ(static_cast(columnCount), fieldInfoList.size()); @@ -410,7 +410,7 @@ namespace { } void GetSyncData(sqlite3 *db, std::map &dataMap, const std::string &tableName, - std::vector fieldInfoList) + std::vector &fieldInfoList) { sqlite3_stmt *statement = nullptr; EXPECT_EQ(PrepareSelect(db, statement, GetDeviceTableName(tableName)), SQLITE_OK); @@ -434,7 +434,7 @@ namespace { } void PrepareBasicTable(const std::string &tableName, std::vector &fieldInfoList, - std::vector &remoteDeviceVec, bool createDistributedTable = true) + const std::vector &remoteDeviceVec, bool createDistributedTable = true) { sqlite3 *db = nullptr; EXPECT_EQ(GetDB(db), SQLITE_OK); @@ -466,7 +466,7 @@ namespace { } void PrepareVirtualEnvironment(std::map &dataMap, const std::string &tableName, - std::vector &fieldInfoList, std::vector remoteDeviceVec, + std::vector &fieldInfoList, const std::vector remoteDeviceVec, bool createDistributedTable = true) { PrepareBasicTable(tableName, fieldInfoList, remoteDeviceVec, createDistributedTable); @@ -480,13 +480,13 @@ namespace { } void PrepareVirtualEnvironment(std::map &dataMap, - std::vector remoteDeviceVec, bool createDistributedTable = true) + const std::vector remoteDeviceVec, bool createDistributedTable = true) { PrepareVirtualEnvironment(dataMap, g_tableName, g_fieldInfoList, remoteDeviceVec, createDistributedTable); } - void CheckData(std::map &targetMap, const std::string &tableName, - std::vector fieldInfoList) + void CheckData(const std::map &targetMap, const std::string &tableName, + std::vector &fieldInfoList) { std::map dataMap; sqlite3 *db = nullptr; @@ -500,7 +500,7 @@ namespace { } } - void CheckData(std::map &targetMap) + void CheckData(const std::map &targetMap) { CheckData(targetMap, g_tableName, g_fieldInfoList); } @@ -576,7 +576,7 @@ namespace { void PrepareEnvironment(std::map &dataMap, std::vector &localFieldInfoList, std::vector &remoteFieldInfoList, - std::vector remoteDeviceVec) + const std::vector remoteDeviceVec) { PrepareEnvironment(dataMap, g_tableName, localFieldInfoList, remoteFieldInfoList, remoteDeviceVec); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp index 9df55d1bb65c8d80ba25eaa61aa9549aa421e17e..f1778d10a06873aa88d1c76715e2fd7bc23063a9 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_msg_schedule_test.cpp @@ -432,8 +432,8 @@ HWTEST_F(DistributedDBSingleVerMsgScheduleTest, MsgSchedule007, TestSize.Level0) msgSchedule.ScheduleInfoHandle(isNeedHandle, false, msg); delete msg; } - Message *msg = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); - ASSERT_TRUE(msg == nullptr); + Message *msg2 = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); + ASSERT_TRUE(msg2 == nullptr); /** * @tc.steps: step2. put msg seq1_packet1, seq2_packet2 * @tc.expected: get nullptr @@ -448,9 +448,9 @@ HWTEST_F(DistributedDBSingleVerMsgScheduleTest, MsgSchedule007, TestSize.Level0) isNeedHandle = true; isNeedContinue = true; for (uint32_t i = 1; i <= 3; i++) { - Message *msg = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); + Message *msg3 = msgSchedule.MoveNextMsg(context, isNeedHandle, isNeedContinue); EXPECT_EQ(isNeedContinue, true); - ASSERT_TRUE(msg == nullptr); + ASSERT_TRUE(msg3 == nullptr); } RefObject::KillAndDecObjRef(context); context = nullptr; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp index cdf06110bd488c406bb5a67eed39661e51510654..112dc2f227098ddc7cbeed13ae8c8acb5270b6f5 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_query_sync_test.cpp @@ -344,9 +344,11 @@ HWTEST_F(DistributedDBSingleVerP2PQuerySyncTest, NormalSync003, TestSize.Level1) Value value = {'1'}; const int dataSize = 10; status = g_kvDelegatePtr->Put(key, value); + ASSERT_TRUE(status == OK); Key key2 = {'2'}; Value value2 = {'2'}; status = g_kvDelegatePtr->Put(key2, value2); + ASSERT_TRUE(status == OK); /** * @tc.steps: step2. deviceB put {b0, v0} - {b9, v9}, {c, v} @@ -1448,6 +1450,7 @@ HWTEST_F(DistributedDBSingleVerP2PQuerySyncTest, AllPredicateQuerySync001, TestS key.push_back(i); key2.push_back(i); status = g_schemaKvDelegatePtr->Put(key, value); + ASSERT_TRUE(status == OK); status = g_schemaKvDelegatePtr->Put(key2, value2); ASSERT_TRUE(status == OK); key.pop_back(); @@ -1545,6 +1548,7 @@ HWTEST_F(DistributedDBSingleVerP2PQuerySyncTest, AllPredicateQuerySync003, TestS key.push_back(i); key2.push_back(i); status = g_schemaKvDelegatePtr->Put(key, value); + ASSERT_TRUE(status == OK); status = g_schemaKvDelegatePtr->Put(key2, value2); ASSERT_TRUE(status == OK); key.pop_back(); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp similarity index 98% rename from frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp rename to frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp index a7ce0829ad300c9a5c505b8655aa42fa20fb74b8..fcf58d5200f03e27bea2ed751b36813722a54c63 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subsribe_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_subscribe_sync_test.cpp @@ -511,9 +511,9 @@ HWTEST_F(DistributedDBSingleVerP2PSubscribeSyncTest, subscribeManager003, TestSi */ LOGI("============step 3============"); for (int i = 0; i < 8; i++) { - QuerySyncObject querySyncObj(Query::Select().PrefixKey({'a', static_cast('a' + i)})); - ASSERT_TRUE(subManager.ReserveLocalSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); - ASSERT_TRUE(subManager.ActiveLocalSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); + QuerySyncObject querySyncObj2(Query::Select().PrefixKey({'a', static_cast('a' + i)})); + ASSERT_TRUE(subManager.ReserveLocalSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); + ASSERT_TRUE(subManager.ActiveLocalSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); } QuerySyncObject querySyncObj1(Query::Select().PrefixKey({'a', static_cast('a' + 8)})); ASSERT_TRUE(subManager.ReserveLocalSubscribeQuery(device + std::to_string(8), querySyncObj1) != E_OK); @@ -562,9 +562,9 @@ HWTEST_F(DistributedDBSingleVerP2PSubscribeSyncTest, subscribeManager004, TestSi */ LOGI("============step 3============"); for (int i = 0; i < 8; i++) { - QuerySyncObject querySyncObj(Query::Select().PrefixKey({'a', static_cast('a' + i)})); - ASSERT_TRUE(subManager.ReserveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); - ASSERT_TRUE(subManager.ActiveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj) == E_OK); + QuerySyncObject querySyncObj2(Query::Select().PrefixKey({'a', static_cast('a' + i)})); + ASSERT_TRUE(subManager.ReserveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); + ASSERT_TRUE(subManager.ActiveRemoteSubscribeQuery(device + std::to_string(i), querySyncObj2) == E_OK); } QuerySyncObject querySyncObj1(Query::Select().PrefixKey({'a', static_cast('a' + 8)})); ASSERT_TRUE(subManager.ReserveRemoteSubscribeQuery(device + std::to_string(8), querySyncObj1) != E_OK); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp index 44a5353779d26b556dbd3d14af8ff75f0d3ab40b..5f468630015a4a0f4f52910303702a067ed83e35 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp @@ -42,6 +42,7 @@ namespace { const int THREE_HUNDRED = 300; const int WAIT_30_SECONDS = 30000; const int WAIT_40_SECONDS = 40000; + const int TIMEOUT_6_SECONDS = 6000; KvStoreDelegateManager g_mgr(APP_ID, USER_ID); KvStoreConfig g_config; @@ -592,7 +593,7 @@ void SyncWithQuery(vector &devices, const Query &query, const DBSta SyncWithQuery(devices, query, DistributedDB::SYNC_MODE_PUSH_ONLY, targetStatus); } -void SyncWithDeviceOffline(vector &devices, Key &key, Query &query) +void SyncWithDeviceOffline(vector &devices, Key &key, const Query &query) { Value value = {'2'}; ASSERT_TRUE(g_kvDelegatePtr->Put(key, value) == OK); @@ -659,7 +660,6 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, AckSessionCheck001, TestSize.Le * @tc.steps: step1. deviceB sync to deviceA just for timeSync and abilitySync * @tc.expected: step1. should return OK. */ - std::map result; ASSERT_TRUE(g_deviceB->Sync(SYNC_MODE_PUSH_ONLY, true) == OK); /** @@ -1333,6 +1333,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify001, TestSize.Leve * @tc.steps: step1. deviceB set get data delay 40s */ g_deviceB->DelayGetSyncData(WAIT_40_SECONDS); + g_communicatorAggregator->SetTimeout(DEVICE_A, TIMEOUT_6_SECONDS); /** * @tc.steps: step2. deviceA call sync and wait @@ -1372,4 +1373,56 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify001, TestSize.Leve EXPECT_EQ(virtualRes.size(), devices.size()); EXPECT_EQ(virtualRes[DEVICE_A], static_cast(SyncOperation::OP_FINISHED_ALL)); g_deviceB->DelayGetSyncData(0); +} + +/** + * @tc.name: GetDataNotify002 + * @tc.desc: Test GetDataNotify function, two device sync each other at same time + * @tc.type: FUNC + * @tc.require: AR000D4876 + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify002, TestSize.Level3) +{ + ASSERT_NE(g_kvDelegatePtr, nullptr); + DBStatus status = OK; + std::vector devices; + devices.push_back(g_deviceB->GetDeviceId()); + const std::string DEVICE_A = "real_device"; + + /** + * @tc.steps: step1. deviceA sync first to finish time sync and ability sync + */ + std::map result; + status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result, true); + EXPECT_EQ(status, OK); + EXPECT_EQ(result.size(), devices.size()); + EXPECT_EQ(result[DEVICE_B], OK); + /** + * @tc.steps: step2. deviceB set get data delay 30s + */ + g_deviceB->DelayGetSyncData(WAIT_30_SECONDS); + + /** + * @tc.steps: step3. deviceB call sync and wait + */ + std::thread asyncThread([]() { + std::map virtualRes; + Query query = Query::Select(); + g_deviceB->Sync(SYNC_MODE_PUSH_ONLY, query, [&virtualRes](std::map resMap) { + virtualRes = std::move(resMap); + }, true); + }); + + /** + * @tc.steps: step4. deviceA call sync and wait + * @tc.expected: step4. sync should return OK. because notify timer trigger (30s - 1s)/2s => 15times + */ + std::this_thread::sleep_for(std::chrono::seconds(1)); + status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result, true); + EXPECT_EQ(status, OK); + EXPECT_EQ(result.size(), devices.size()); + EXPECT_EQ(result[DEVICE_B], OK); + asyncThread.join(); + std::this_thread::sleep_for(std::chrono::seconds(TEN_SECONDS)); } \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp index 464cc355d7376648839be23550c7196b343df635..71bc92db6fdc2cef5eb6336b84b9addd16736d7a 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_test.cpp @@ -2302,11 +2302,12 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SaveDataNotify001, TestSize.Level3) */ g_deviceB->SetSaveDataDelayTime(WAIT_30_SECONDS); status = g_kvDelegatePtr->Put(key, value); - + ASSERT_TRUE(status == OK); /** * @tc.steps: step3. deviceA call sync and wait * @tc.expected: step3. sync should return OK. onComplete should be called, deviceB sync success. */ + result.clear(); status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result); ASSERT_TRUE(status == OK); ASSERT_TRUE(result.size() == devices.size()); @@ -2317,11 +2318,12 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SaveDataNotify001, TestSize.Level3) */ g_deviceB->SetSaveDataDelayTime(WAIT_36_SECONDS); status = g_kvDelegatePtr->Put(key, value); - + ASSERT_TRUE(status == OK); /** * @tc.steps: step5. deviceA call sync and wait * @tc.expected: step5. sync should return OK. onComplete should be called, deviceB sync TIME_OUT. */ + result.clear(); status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PUSH_ONLY, result); ASSERT_TRUE(status == OK); ASSERT_TRUE(result.size() == devices.size()); @@ -2412,7 +2414,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SametimeSync002, TestSize.Level3) } }); std::map result; - auto callback = [&result](std::map map) { + auto callback = [&result](std::map &map) { result = map; }; Query query = Query::Select().PrefixKey({'k', '1'}); @@ -2431,7 +2433,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SametimeSync002, TestSize.Level3) }); std::thread subThread([&devices] { std::map result; - auto callback = [&result](std::map map) { + auto callback = [&result](std::map &map) { result = map; }; Query query = Query::Select().PrefixKey({'k', '2'}); @@ -2447,7 +2449,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, SametimeSync002, TestSize.Level3) std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::map virtualResult; g_deviceB->Sync(DistributedDB::SYNC_MODE_PULL_ONLY, query, - [&virtualResult](std::map map) { + [&virtualResult](std::map &map) { virtualResult = map; }, true); EXPECT_TRUE(status == OK); @@ -2472,7 +2474,7 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, DatabaseOnlineCallback001, TestSize. */ std::string targetDev = "DEVICE_X"; bool isCheckOk = false; - auto databaseStatusNotifyCallback = [targetDev, &isCheckOk] (std::string userId, + auto databaseStatusNotifyCallback = [targetDev, &isCheckOk] (std::string userId,2304 std::string appId, std::string storeId, const std::string deviceId, bool onlineStatus) -> void { if (userId == USER_ID && appId == APP_ID && storeId == STORE_ID && deviceId == targetDev && onlineStatus == true) { @@ -2580,6 +2582,56 @@ HWTEST_F(DistributedDBSingleVerP2PSyncTest, CloseSync001, TestSize.Level3) EXPECT_EQ(g_deviceB->GetData(key, actualValue), -E_NOT_FOUND); } +/** + * @tc.name: CloseSync002 + * @tc.desc: Test 1 delegate close when in time sync + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBSingleVerP2PSyncTest, CloseSync002, TestSize.Level3) +{ + /** + * @tc.steps: step1. invalid time sync packet from A + */ + g_communicatorAggregator->RegOnDispatch([](const std::string &target, DistributedDB::Message *msg) { + ASSERT_NE(msg, nullptr); + if (target == DEVICE_B && msg->GetMessageId() == TIME_SYNC_MESSAGE && msg->GetMessageType() == TYPE_REQUEST) { + msg->SetMessageId(INVALID_MESSAGE_ID); + LOGD("Message is invalid"); + } + }); + Timestamp currentTime; + (void)OS::GetCurrentSysTimeInMicrosecond(currentTime); + g_deviceB->PutData({'k'}, {'v'}, currentTime, 0); + + /** + * @tc.steps: step2. B PUSH to A and A close after 1s + * @tc.expected: step2. A closing time cost letter than 4s + */ + std::thread closingThread([]() { + std::this_thread::sleep_for(std::chrono::seconds(1)); + LOGD("Begin Close"); + Timestamp beginTime; + (void)OS::GetCurrentSysTimeInMicrosecond(beginTime); + ASSERT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK); + Timestamp endTime; + (void)OS::GetCurrentSysTimeInMicrosecond(endTime); + EXPECT_LE(static_cast(endTime - beginTime), 4 * 1000 * 1000); // waiting 4 * 1000 * 1000 us + LOGD("End Close"); + }); + EXPECT_EQ(g_deviceB->Sync(DistributedDB::SYNC_MODE_PUSH_ONLY, true), E_OK); + closingThread.join(); + + /** + * @tc.steps: step3. remove db + * @tc.expected: step3. remove ok + */ + g_kvDelegatePtr = nullptr; + DBStatus status = g_mgr.DeleteKvStore(STORE_ID); + LOGD("delete kv store status %d", status); + ASSERT_TRUE(status == OK); +} /** * @tc.name: OrderbyWriteTimeSync001 diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h index af89a85aea477859a44b8ae9387d478ed6b1cebe..6236d010a937b51cfb8c05a785f0d9ec8aa14dc5 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_auto_launch.h @@ -22,7 +22,7 @@ namespace DistributedDB { class MockAutoLaunch : public AutoLaunch { public: - void SetAutoLaunchItem(const std::string &identify, const std::string &userId, AutoLaunchItem &item) + void SetAutoLaunchItem(const std::string &identify, const std::string &userId, const AutoLaunchItem &item) { std::lock_guard autoLock(extLock_); extItemMap_[identify][userId] = item; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h new file mode 100644 index 0000000000000000000000000000000000000000..1aacb5351f755d586dde6bf98b8df5682f0bc3ff --- /dev/null +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_kv_sync_interface.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_KV_SYNC_INTERFACE_H +#define MOCK_KV_SYNC_INTERFACE_H + +#include +#include "virtual_single_ver_sync_db_Interface.h" + +namespace DistributedDB { +class MockKvSyncInterface : public VirtualSingleVerSyncDBInterface { +public: + MOCK_METHOD0(IncRefCount, void(void)); + MOCK_METHOD0(DecRefCount, void(void)); +}; +} // namespace DistributedDB +#endif // #define MOCK_KV_SYNC_INTERFACE_H \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h index e48aac1f101779357bb979905624abcb35ba1a8f..9b7fbb37030b23cfe038134862349d2a57c125f0 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_single_ver_state_machine.h @@ -67,6 +67,11 @@ public: SingleVerSyncStateMachine::ResponsePullError(errCode, ignoreInnerErr); } + int32_t GetSaveDataNotifyRefCount() + { + return saveDataNotifyRefCount_; + } + MOCK_METHOD1(SwitchStateAndStep, void(uint8_t)); MOCK_METHOD0(PrepareNextSyncTask, int(void)); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp index 0f8bfe3d7c4df6af72b249f6b4cc12973ee9e352..d6a625f42b5446c5384824eb0e431532e87c3aaa 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.cpp @@ -124,7 +124,7 @@ uint32_t VirtualCommunicator::GetCommunicatorMtuSize(const std::string &target) uint32_t VirtualCommunicator::GetTimeout() const { - return 5 * 1000; // 5 * 1000ms + return timeout_; } uint32_t VirtualCommunicator::GetTimeout(const std::string &target) const @@ -132,6 +132,11 @@ uint32_t VirtualCommunicator::GetTimeout(const std::string &target) const return GetTimeout(); } +void VirtualCommunicator::SetTimeout(uint32_t timeout) +{ + timeout_ = timeout; +} + int VirtualCommunicator::GetLocalIdentity(std::string &outTarget) const { outTarget = deviceId_; @@ -202,10 +207,8 @@ int VirtualCommunicator::TranslateMsg(const Message *inMsg, Message *&outMsg) } outMsg = ProtocolProto::ToMessage(buffer, errCode); - if (errCode != E_OK) { - delete buffer; - buffer = nullptr; - } + delete buffer; + buffer = nullptr; return errCode; } } // namespace DistributedDB \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h index 7cde89e498145d7ec640188f415b30465ab05761..f202d1dad60b8c803240c504da624f7c6cbc74a6 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator.h @@ -49,6 +49,8 @@ public: uint32_t GetTimeout() const override; uint32_t GetTimeout(const std::string &target) const override; + void SetTimeout(uint32_t timeout); + int GetLocalIdentity(std::string &outTarget) const override; int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config) override; @@ -97,6 +99,8 @@ private: std::mutex onAggregatorLock_; VirtualCommunicatorAggregator *communicatorAggregator_; + + uint32_t timeout_ = 5 * 1000; // 5 * 1000ms }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp index 2131336581026d60f122ef8efaf39b78fd673d8d..7e40b97c0bd716882010664fa9cc54e11c910ebb 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp @@ -143,7 +143,7 @@ ICommunicator *VirtualCommunicatorAggregator::AllocCommunicator(const std::strin } { std::lock_guard lock(communicatorsLock_); - communicators_.insert(std::pair(deviceId, communicator)); + communicators_.insert(std::pair(deviceId, communicator)); } OnlineDevice(deviceId); return communicator; @@ -250,4 +250,12 @@ void VirtualCommunicatorAggregator::SetCurrentUserId(const std::string &userId) { userId_ = userId; } + +void VirtualCommunicatorAggregator::SetTimeout(const std::string &deviceId, uint32_t timeout) +{ + std::lock_guard lock(communicatorsLock_); + if (communicators_.find(deviceId) != communicators_.end()) { + communicators_[deviceId]->SetTimeout(timeout); + } +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h index 22db8b586c7f60bce98f337dda3621bca0501baf..c1848a2cac9aae93f46d01890b30bc1c9742a384 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h @@ -70,6 +70,8 @@ public: void SetCurrentUserId(const std::string &userId); + void SetTimeout(const std::string &deviceId, uint32_t timeout); + ~VirtualCommunicatorAggregator() {}; VirtualCommunicatorAggregator() {}; @@ -77,7 +79,7 @@ private: void CallSendEnd(int errCode, const OnSendEnd &onEnd); mutable std::mutex communicatorsLock_; - std::map communicators_; + std::map communicators_; std::string remoteDeviceId_ = "real_device"; std::mutex blockLock_; std::condition_variable conditionVar_; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp index afda4562fedb4ec727905a5487b2df2244db2af5..5ec215d783fa9e7d84513d4f077ba76ddb7145c2 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp @@ -25,7 +25,7 @@ namespace { int GetEntriesFromItems(std::vector &entries, const std::vector &dataItems) { int errCode = E_OK; - for (auto &item : dataItems) { + for (const auto &item : dataItems) { auto entry = new (std::nothrow) GenericSingleVerKvEntry(); if (entry == nullptr) { LOGE("Create entry failed."); @@ -274,7 +274,7 @@ int VirtualRelationalVerSyncDBInterface::DeleteMetaDataByPrefixKey(const Key &ke int VirtualRelationalVerSyncDBInterface::GetAllMetaKeys(std::vector &keys) const { - for (auto &iter : metadata_) { + for (const auto &iter : metadata_) { keys.push_back(iter.first); } LOGD("GetAllMetaKeys size %zu", keys.size()); @@ -364,7 +364,7 @@ void VirtualRelationalVerSyncDBInterface::SetPermitCreateDistributedTable(bool p permitCreateDistributedTable_ = permitCreateDistributedTable; } -void ObjectData::PutDataValue(const std::string &fieldName, const DataValue &value) +void ObjectData::PutDataValue(const std::string &fieldName, const DataValue &value) const { fieldData[fieldName] = value; } diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h index e42cba6be4faa482cd9010aea750c69a917cca4c..a7ca8f479b25a76808da79a77a67248f6614beeb 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.h @@ -24,7 +24,7 @@ namespace DistributedDB { struct ObjectData { public: - void PutDataValue(const std::string &fieldName, const DataValue &value); + void PutDataValue(const std::string &fieldName, const DataValue &value) const; int GetDataValue(const std::string &fieldName, DataValue &value) const; private: mutable std::map fieldData; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp index b826d4845cbf454d11337cf3b7b176c62ee97cc6..259868e48723deb6b7ec9259b5a96a430c2a0b70 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp @@ -31,7 +31,7 @@ namespace { int GetEntriesFromItems(std::vector &entries, const std::vector &dataItems) { int errCode = E_OK; - for (auto &item : dataItems) { + for (const auto &item : dataItems) { auto entry = new (std::nothrow) GenericSingleVerKvEntry(); if (entry == nullptr) { LOGE("Create entry failed.");