From 4ba2035c81ab427efbb4b7236ebf83686bce23ab Mon Sep 17 00:00:00 2001 From: zqq Date: Thu, 24 Feb 2022 18:55:37 +0800 Subject: [PATCH 01/37] fixbug that multi ver change timestamp twice in statemachine Signed-off-by: zqq --- .../syncer/src/multi_ver_sync_state_machine.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp index 3f2890cfb..8e7fe90a5 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp @@ -472,16 +472,10 @@ int MultiVerSyncStateMachine::OneCommitSyncFinish() // Due to time sync error, commit timestamp may bigger than currentLocalTime, we need to fix the timestamp TimeOffset timefixOffset = (commit.timestamp < currentLocalTime) ? 0 : (commit.timestamp - static_cast(currentLocalTime)); - ChangeEntriesTimeStamp(entries, outOffset, timefixOffset); LOGD("MultiVerSyncStateMachine::OneCommitSyncFinish src=%s, timefixOffset = %lld", STR_MASK(context_->GetDeviceId()), timefixOffset); commit.timestamp -= static_cast(timefixOffset); - for (MultiVerKvEntry *entry : entries) { - TimeStamp timeStamp; - entry->GetTimestamp(timeStamp); - timeStamp = timeStamp - outOffset - timefixOffset; - entry->SetTimestamp(timeStamp); - } + ChangeEntriesTimeStamp(entries, outOffset, timefixOffset); PerformanceAnalysis *performance = PerformanceAnalysis::GetInstance(); if (performance != nullptr) { performance->StepTimeRecordStart(MV_TEST_RECORDS::RECORD_PUT_COMMIT_DATA); -- Gitee From 1dd11bfc4f91a215c852667b9d4b048ca9cd945c Mon Sep 17 00:00:00 2001 From: lianhuix Date: Fri, 18 Feb 2022 18:06:40 +0800 Subject: [PATCH 02/37] Fix code reviews Signed-off-by: lianhuix --- .../relational/relational_schema_object.h | 17 +-- .../relational/relational_schema_object.cpp | 101 +----------------- .../src/relational_sync_able_storage.cpp | 14 +++ .../storage/src/sqlite/sqlite_utils.cpp | 39 ++----- ...ributeddb_storage_subscribe_query_test.cpp | 4 + 5 files changed, 32 insertions(+), 143 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h index fa8a5d182..3d7aa3380 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h @@ -63,23 +63,16 @@ public: bool GetAutoIncrement() const; const std::string &GetCreateTableSql() const; const std::map &GetFields() const; // - const std::vector &GetUniqueDefine() const; const std::map &GetIndexDefine() const; const FieldName &GetPrimaryKey() const; - const std::vector &GetTriggers() const; - const std::string &GetDevId() const; void SetTableName(const std::string &tableName); void SetAutoIncrement(bool autoInc); void SetCreateTableSql(std::string sql); // set 'autoInc_' flag when set sql void AddField(const FieldInfo &field); - void AddUniqueDefine(const CompositeFields &uniqueDefine); - void SetUniqueDefines(const std::vector &uniqueDefines); void AddIndexDefine(const std::string &indexName, const CompositeFields &indexDefine); void SetPrimaryKey(const FieldName &fieldName); // not support composite index now - void AddTrigger(const std::string &triggerName); std::string ToTableInfoString() const; - void SetDevId(const std::string &devId); int CompareWithTable(const TableInfo &inTableInfo) const; std::map GetSchemaDefine() const; @@ -89,23 +82,18 @@ public: private: void AddFieldDefineString(std::string &attrStr) const; - void AddUniqueDefineString(std::string &attrStr) const; void AddIndexDefineString(std::string &attrStr) const; int CompareWithTableFields(const std::map &inTableFields) const; int CompareWithTableIndex(const std::map &inTableIndex) const; std::string tableName_; - std::string devId_; bool autoInc_ = false; // only 'INTEGER PRIMARY KEY' could be defined as 'AUTOINCREMENT' std::string sql_; std::map fields_; FieldName primaryKey_; - std::vector uniqueDefines_; std::map indexDefines_; - std::vector triggers_; - JsonObject ToJsonObject() const; - mutable std::vector fieldInfos_; + mutable std::vector fieldNames_; }; class RelationalSyncOpinion { @@ -169,14 +157,13 @@ private: int ParseRelationalSchema(const JsonObject &inJsonObject); int ParseCheckSchemaType(const JsonObject &inJsonObject); - int ParseCheckSchemaVersionMode(const JsonObject &inJsonObject); + int ParseCheckSchemaVersion(const JsonObject &inJsonObject); int ParseCheckSchemaTableDefine(const JsonObject &inJsonObject); int ParseCheckTableInfo(const JsonObject &inJsonObject); int ParseCheckTableName(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTableDefine(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTableFieldInfo(const JsonObject &inJsonObject, const FieldPath &path, FieldInfo &table); int ParseCheckTableAutoInc(const JsonObject &inJsonObject, TableInfo &resultTable); - int ParseCheckTableUnique(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable); diff --git a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp index 099d0c899..a217787bf 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp @@ -202,21 +202,6 @@ void TableInfo::AddField(const FieldInfo &field) fields_[field.GetFieldName()] = field; } -const std::vector &TableInfo::GetUniqueDefine() const -{ - return uniqueDefines_; -} - -void TableInfo::AddUniqueDefine(const CompositeFields &uniqueDefine) -{ - uniqueDefines_.push_back(uniqueDefine); -} - -void TableInfo::SetUniqueDefines(const std::vector &uniqueDefines) -{ - uniqueDefines_ = uniqueDefines; -} - const std::map &TableInfo::GetIndexDefine() const { return indexDefines_; @@ -237,16 +222,6 @@ void TableInfo::SetPrimaryKey(const FieldName &fieldName) primaryKey_ = fieldName; } -void TableInfo::AddTrigger(const std::string &triggerName) -{ - triggers_.push_back(triggerName); -} - -const std::vector &TableInfo::GetTriggers() const -{ - return triggers_; -} - void TableInfo::AddFieldDefineString(std::string &attrStr) const { if (fields_.empty()) { @@ -262,28 +237,6 @@ void TableInfo::AddFieldDefineString(std::string &attrStr) const attrStr += "},"; } -void TableInfo::AddUniqueDefineString(std::string &attrStr) const -{ - if (uniqueDefines_.empty()) { - return; - } - attrStr += R"("UNIQUE": [)"; - for (auto itUniqueDefine = uniqueDefines_.begin(); itUniqueDefine != uniqueDefines_.end(); ++itUniqueDefine) { - attrStr += "[\""; - for (auto itField = (*itUniqueDefine).begin(); itField != (*itUniqueDefine).end(); ++itField) { - attrStr += *itField; - if (itField != (*itUniqueDefine).end() - 1) { - attrStr += "\",\""; - } - } - attrStr += "\"]"; - if (itUniqueDefine != uniqueDefines_.end() - 1) { - attrStr += ","; - } - } - attrStr += "],"; -} - void TableInfo::AddIndexDefineString(std::string &attrStr) const { if (indexDefines_.empty()) { @@ -306,16 +259,6 @@ void TableInfo::AddIndexDefineString(std::string &attrStr) const attrStr += "}"; } -const std::string &TableInfo::GetDevId() const -{ - return devId_; -} - -void TableInfo::SetDevId(const std::string &devId) -{ - devId_ = devId; -} - int TableInfo::CompareWithTable(const TableInfo &inTableInfo) const { if (tableName_ != inTableInfo.GetTableName()) { @@ -408,26 +351,6 @@ namespace { } } -JsonObject TableInfo::ToJsonObject() const -{ - FieldValue jsonField; - JsonObject tableJson; - jsonField.stringValue = tableName_; - tableJson.InsertField(FieldPath { "NAME" }, FieldType::LEAF_FIELD_STRING, jsonField); - jsonField.boolValue = autoInc_; - tableJson.InsertField(FieldPath { "AUTOINCREMENT" }, FieldType::LEAF_FIELD_BOOL, jsonField); - jsonField.stringValue = primaryKey_; - tableJson.InsertField(FieldPath { "PRIMARY_KEY" }, FieldType::LEAF_FIELD_STRING, jsonField); - for (const auto &it : fields_) { - jsonField.stringValue = it.second.ToAttributeString(); - tableJson.InsertField(FieldPath { "DEFINE", it.first }, FieldType::LEAF_FIELD_STRING, jsonField); - } - for (const auto &it : uniqueDefines_) { - jsonField.stringValue = VectorJoin(it, ','); - } - return tableJson; -} - std::string TableInfo::ToTableInfoString() const { std::string attrStr; @@ -440,7 +363,6 @@ std::string TableInfo::ToTableInfoString() const } else { attrStr += "false,"; } - AddUniqueDefineString(attrStr); if (!primaryKey_.empty()) { attrStr += R"("PRIMARY_KEY": ")" + primaryKey_ + "\""; } @@ -813,7 +735,7 @@ int GetMemberFromJsonObject(const JsonObject &inJsonObject, const std::string &f int RelationalSchemaObject::ParseRelationalSchema(const JsonObject &inJsonObject) { - int errCode = ParseCheckSchemaVersionMode(inJsonObject); + int errCode = ParseCheckSchemaVersion(inJsonObject); if (errCode != E_OK) { return errCode; } @@ -824,7 +746,7 @@ int RelationalSchemaObject::ParseRelationalSchema(const JsonObject &inJsonObject return ParseCheckSchemaTableDefine(inJsonObject); } -int RelationalSchemaObject::ParseCheckSchemaVersionMode(const JsonObject &inJsonObject) +int RelationalSchemaObject::ParseCheckSchemaVersion(const JsonObject &inJsonObject) { FieldValue fieldValue; int errCode = GetMemberFromJsonObject(inJsonObject, SchemaConstant::KEYWORD_SCHEMA_VERSION, @@ -902,10 +824,6 @@ int RelationalSchemaObject::ParseCheckTableInfo(const JsonObject &inJsonObject) if (errCode != E_OK) { return errCode; } - errCode = ParseCheckTableUnique(inJsonObject, resultTable); - if (errCode != E_OK) { - return errCode; - } errCode = ParseCheckTablePrimaryKey(inJsonObject, resultTable); if (errCode != E_OK) { return errCode; @@ -1008,21 +926,6 @@ int RelationalSchemaObject::ParseCheckTableAutoInc(const JsonObject &inJsonObjec return E_OK; } -int RelationalSchemaObject::ParseCheckTableUnique(const JsonObject &inJsonObject, TableInfo &resultTable) -{ - if (!inJsonObject.IsFieldPathExist(FieldPath {"UNIQUE"})) { // UNIQUE is not necessary - return E_OK; - } - std::vector uniqueArray; - int errCode = inJsonObject.GetArrayContentOfStringOrStringArray(FieldPath {"UNIQUE"}, uniqueArray); - if (errCode != E_OK) { - LOGE("[RelationalSchema][Parse] Get unique array failed: %d.", errCode); - return -E_SCHEMA_PARSE_FAIL; - } - resultTable.SetUniqueDefines(uniqueArray); - return E_OK; -} - int RelationalSchemaObject::ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable) { FieldValue fieldValue; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp index cf5b70167..02a32fab1 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp @@ -478,6 +478,14 @@ int RelationalSyncAbleStorage::CreateDistributedDeviceTable(const std::string &d if (handle == nullptr) { return errCode; } + + errCode = handle->StartTransaction(TransactType::IMMEDIATE); + if (errCode != E_OK) { + LOGE("Start transaction failed:%d", errCode); + ReleaseHandle(handle); + return errCode; + } + for (const auto &[table, strategy] : syncStrategy.GetStrategies()) { if (!strategy.permitSync) { continue; @@ -490,6 +498,12 @@ int RelationalSyncAbleStorage::CreateDistributedDeviceTable(const std::string &d } } + if (errCode == E_OK) { + errCode = handle->Commit(); + } else { + (void)handle->Rollback(); + } + ReleaseHandle(handle); return errCode; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index 5a77599c9..f66cc46a9 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -608,10 +608,6 @@ int AnalysisSchemaSqlAndTrigger(sqlite3 *db, const std::string &tableName, Table std::string createTableSql; (void) SQLiteUtils::GetColumnTextValue(statement, 4, createTableSql); // 4 means create table sql table.SetCreateTableSql(createTableSql); - } else if (type == "trigger") { - std::string triggerName; - (void) SQLiteUtils::GetColumnTextValue(statement, 1, triggerName); // 1 means trigger name - table.AddTrigger(triggerName); } } else { LOGE("[AnalysisSchema] Step for the analysis create table sql and trigger failed:%d", err); @@ -623,8 +619,7 @@ int AnalysisSchemaSqlAndTrigger(sqlite3 *db, const std::string &tableName, Table return errCode; } -int GetSchemaIndexList(sqlite3 *db, const std::string &tableName, std::vector &indexList, - std::vector &uniqueList) +int GetSchemaIndexList(sqlite3 *db, const std::string &tableName, std::vector &indexList) { std::string sql = "pragma index_list(" + tableName + ")"; sqlite3_stmt *statement = nullptr; @@ -644,9 +639,7 @@ int GetSchemaIndexList(sqlite3 *db, const std::string &tableName, std::vector indexList; - std::vector uniqueList; - int errCode = GetSchemaIndexList(db, tableName, indexList, uniqueList); + int errCode = GetSchemaIndexList(db, tableName, indexList); if (errCode != E_OK) { LOGE("[AnalysisSchema] get schema index list failed."); return errCode; @@ -706,17 +698,6 @@ int AnalysisSchemaIndexAndUnique(sqlite3 *db, const std::string &tableName, Tabl } table.AddIndexDefine(indexName, indexDefine); } - - for (const auto &uniqueName : uniqueList) { - CompositeFields uniqueDefine; - errCode = AnalysisSchemaIndexDefine(db, uniqueName, uniqueDefine); - if (errCode != E_OK) { - LOGE("[AnalysisSchema] analysis schema unique columns failed."); - return errCode; - } - table.AddUniqueDefine(uniqueDefine); - } - return E_OK; } @@ -795,7 +776,7 @@ int SQLiteUtils::AnalysisSchema(sqlite3 *db, const std::string &tableName, Table return errCode; } - errCode = AnalysisSchemaIndexAndUnique(db, tableName, table); + errCode = AnalysisSchemaIndex(db, tableName, table); if (errCode != E_OK) { LOGE("[AnalysisSchema] Analysis index failed."); return errCode; @@ -1338,7 +1319,7 @@ int SQLiteUtils::CreateRelationalLogTable(sqlite3 *db, const std::string &oriTab } namespace { -std::string GetInsertTrigger(const TableInfo table) +std::string GetInsertTrigger(const TableInfo &table) { std::string insertTrigger = "CREATE TRIGGER IF NOT EXISTS "; insertTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_INSERT AFTER INSERT \n"; @@ -1347,27 +1328,27 @@ std::string GetInsertTrigger(const TableInfo table) insertTrigger += "\t INSERT OR REPLACE INTO "; insertTrigger += DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log"; insertTrigger += " (data_key, device, ori_device, timestamp, wtimestamp, flag, hash_key)"; - insertTrigger += " VALUES (new.rowid, '" + table.GetDevId() + "', '" + table.GetDevId() + "',"; + insertTrigger += " VALUES (new.rowid, '', '',"; insertTrigger += " get_sys_time(0), get_sys_time(0), 0x02,"; insertTrigger += " calc_hash(new." + table.GetPrimaryKey() + "));\n"; insertTrigger += "END;"; return insertTrigger; } -std::string GetUpdateTrigger(const TableInfo table) +std::string GetUpdateTrigger(const TableInfo &table) { std::string updateTrigger = "CREATE TRIGGER IF NOT EXISTS "; updateTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_UPDATE AFTER UPDATE \n"; updateTrigger += "ON " + table.GetTableName() + "\n"; updateTrigger += "BEGIN\n"; updateTrigger += "\t UPDATE " + DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log"; - updateTrigger += " SET timestamp=get_sys_time(0), device='" + table.GetDevId() + "', flag=0x22"; + updateTrigger += " SET timestamp=get_sys_time(0), device='', flag=0x22"; updateTrigger += " where hash_key=calc_hash(old." + table.GetPrimaryKey() + ") and flag&0x02=0x02;\n"; updateTrigger += "END;"; return updateTrigger; } -std::string GetDeleteTrigger(const TableInfo table) +std::string GetDeleteTrigger(const TableInfo &table) { std::string deleteTrigger = "CREATE TRIGGER IF NOT EXISTS "; deleteTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_DELETE BEFORE DELETE \n"; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_subscribe_query_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_subscribe_query_test.cpp index 80318eef5..c44b497b6 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_subscribe_query_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_storage_subscribe_query_test.cpp @@ -133,6 +133,7 @@ void CreateAndGetStore(const std::string &storeId, const std::string &schemaStri ASSERT_NE(store, nullptr); } +#ifndef OMIT_FLATBUFFER std::string FbfFileToSchemaString(const std::string &fileName) { std::string filePath = g_resourceDir + "fbs_files_for_ut/" + fileName; @@ -152,6 +153,7 @@ std::string FbfFileToSchemaString(const std::string &fileName) LOGE("[FbfFileToSchemaString] read file failed path : %s", filePath.c_str()); return ""; } +#endif void CheckDataNumByKey(const std::string &storeId, const Key& key, size_t expSize) { @@ -273,6 +275,7 @@ HWTEST_F(DistributedDBStorageSubscribeQueryTest, CheckAndInitQueryCondition001, KvDBManager::ReleaseDatabaseConnection(conn); } +#ifndef OMIT_FLATBUFFER /** * @tc.name: CheckAndInitQueryCondition002 * @tc.desc: Check the condition always illegal with flatbuffer schema @@ -309,6 +312,7 @@ HWTEST_F(DistributedDBStorageSubscribeQueryTest, CheckAndInitQueryCondition002, RefObject::KillAndDecObjRef(store); KvDBManager::ReleaseDatabaseConnection(conn); } +#endif /** * @tc.name: CheckAndInitQueryCondition003 -- Gitee From 9c7bb762f5a167640c264374663484ebaefbe5d4 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Fri, 25 Feb 2022 10:17:56 +0800 Subject: [PATCH 03/37] Fix reviewBot. Signed-off-by: lianhuix --- .../common/include/auto_launch.h | 2 +- .../common/include/{schema.h => ischema.h} | 0 .../relational/relational_schema_object.h | 2 +- .../common/include/schema_object.h | 2 +- .../distributeddb/common/src/auto_launch.cpp | 28 +++++++++---------- .../distributeddb/common/src/data_value.cpp | 8 +++--- .../common/src/flatbuffer_schema.cpp | 2 +- .../relational/relational_schema_object.cpp | 1 - .../common/src/schema_object.cpp | 6 ++-- .../relational_store_sqlite_ext.cpp | 4 +-- .../distributeddb/syncer/src/ability_sync.h | 2 +- .../syncer/src/single_ver_sync_task_context.h | 2 +- 12 files changed, 30 insertions(+), 29 deletions(-) rename services/distributeddataservice/libs/distributeddb/common/include/{schema.h => ischema.h} (100%) diff --git a/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h b/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h index 9d61867b4..fa8f10c16 100755 --- a/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h @@ -53,7 +53,7 @@ struct AutoLaunchItem { KvStoreNbConflictNotifier conflictNotifier; void *conn = nullptr; KvDBObserverHandle *observerHandle = nullptr; - bool isWriteOpenNotifiered = false; + bool isWriteOpenNotified = false; AutoLaunchItemState state = AutoLaunchItemState::UN_INITIAL; bool isDisable = false; bool inObserver = false; diff --git a/services/distributeddataservice/libs/distributeddb/common/include/schema.h b/services/distributeddataservice/libs/distributeddb/common/include/ischema.h similarity index 100% rename from services/distributeddataservice/libs/distributeddb/common/include/schema.h rename to services/distributeddataservice/libs/distributeddb/common/include/ischema.h diff --git a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h index 3d7aa3380..a6c3a4b7a 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h @@ -19,7 +19,7 @@ #include "data_value.h" #include "json_object.h" #include "parcel.h" -#include "schema.h" +#include "ischema.h" namespace DistributedDB { using CompositeFields = std::vector; diff --git a/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h b/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h index 2049882e1..9411c1864 100755 --- a/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h @@ -24,7 +24,7 @@ #include "db_types.h" #include "macro_utils.h" #include "relational_schema_object.h" -#include "schema.h" +#include "ischema.h" #include "value_object.h" namespace DistributedDB { diff --git a/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp b/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp index c2f7138ae..0c1277b48 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp @@ -384,9 +384,9 @@ void AutoLaunch::ObserverFunc(const KvDBCommitNotifyData ¬ifyData, const std: } autoLaunchItemMap_[identifier][userId].inObserver = true; autoLaunchItem.observer = autoLaunchItemMap_[identifier][userId].observer; - autoLaunchItem.isWriteOpenNotifiered = autoLaunchItemMap_[identifier][userId].isWriteOpenNotifiered; + autoLaunchItem.isWriteOpenNotified = autoLaunchItemMap_[identifier][userId].isWriteOpenNotified; autoLaunchItem.notifier = autoLaunchItemMap_[identifier][userId].notifier; - + std::shared_ptr properties = std::static_pointer_cast(autoLaunchItemMap_[identifier][userId].propertiesPtr); appId = properties->GetStringProp(KvDBProperties::APP_ID, ""); @@ -397,12 +397,12 @@ void AutoLaunch::ObserverFunc(const KvDBCommitNotifyData ¬ifyData, const std: KvStoreChangedDataImpl data(¬ifyData); (autoLaunchItem.observer)->OnChange(data); } - LOGI("[AutoLaunch] in observer autoLaunchItem.isWriteOpenNotifiered:%d", autoLaunchItem.isWriteOpenNotifiered); + LOGI("[AutoLaunch] in observer autoLaunchItem.isWriteOpenNotified:%d", autoLaunchItem.isWriteOpenNotified); - if (!autoLaunchItem.isWriteOpenNotifiered && autoLaunchItem.notifier != nullptr) { + if (!autoLaunchItem.isWriteOpenNotified && autoLaunchItem.notifier != nullptr) { { std::lock_guard autoLock(dataLock_); - autoLaunchItemMap_[identifier][userId].isWriteOpenNotifiered = true; + autoLaunchItemMap_[identifier][userId].isWriteOpenNotified = true; } AutoLaunchNotifier notifier = autoLaunchItem.notifier; int retCode = RuntimeContext::GetInstance()->ScheduleTask([notifier, userId, appId, storeId] { @@ -464,7 +464,7 @@ int AutoLaunch::DisableKvStoreAutoLaunch(const std::string &normalIdentifier, co cv_.notify_all(); return errCode; } - if (autoLaunchItem.isWriteOpenNotifiered && autoLaunchItem.notifier) { + if (autoLaunchItem.isWriteOpenNotified && autoLaunchItem.notifier) { RuntimeContext::GetInstance()->ScheduleTask([autoLaunchItem] { CloseNotifier(autoLaunchItem); }); } LOGI("[AutoLaunch] DisableKvStoreAutoLaunch ok"); @@ -528,11 +528,11 @@ void AutoLaunch::ConnectionLifeCycleCallbackTask(const std::string &identifier, std::lock_guard lock(dataLock_); autoLaunchItemMap_[identifier][userId].state = AutoLaunchItemState::IDLE; autoLaunchItemMap_[identifier][userId].conn = nullptr; - autoLaunchItemMap_[identifier][userId].isWriteOpenNotifiered = false; + autoLaunchItemMap_[identifier][userId].isWriteOpenNotified = false; cv_.notify_all(); LOGI("[AutoLaunch] ConnectionLifeCycleCallback notify_all"); } - if (autoLaunchItem.isWriteOpenNotifiered) { + if (autoLaunchItem.isWriteOpenNotified) { CloseNotifier(autoLaunchItem); } } @@ -670,7 +670,7 @@ void AutoLaunch::UpdateGlobalMap(std::map autoLock(dataLock_); autoLaunchItemMap_[identifier][userId].conn = autoLaunchItem.conn; autoLaunchItemMap_[identifier][userId].observerHandle = autoLaunchItem.observerHandle; - autoLaunchItemMap_[identifier][userId].isWriteOpenNotifiered = false; + autoLaunchItemMap_[identifier][userId].isWriteOpenNotified = false; autoLaunchItemMap_[identifier][userId].state = AutoLaunchItemState::IDLE; cv_.notify_all(); LOGI("[AutoLaunch] ReceiveUnknownIdentifierCallBackTask conn opened set state IDLE"); @@ -854,7 +854,7 @@ void AutoLaunch::AutoLaunchExtTask(const std::string identifier, const std::stri std::lock_guard autoLock(extLock_); extItemMap_[identifier][userId].conn = autoLaunchItem.conn; extItemMap_[identifier][userId].observerHandle = autoLaunchItem.observerHandle; - extItemMap_[identifier][userId].isWriteOpenNotifiered = false; + extItemMap_[identifier][userId].isWriteOpenNotified = false; LOGI("[AutoLaunch] AutoLaunchExtTask ok"); } @@ -881,9 +881,9 @@ void AutoLaunch::ExtObserverFunc(const KvDBCommitNotifyData ¬ifyData, const s { std::lock_guard autoLock(extLock_); if (extItemMap_.count(identifier) != 0 && extItemMap_[identifier].count(userId) != 0 && - !extItemMap_[identifier][userId].isWriteOpenNotifiered && + !extItemMap_[identifier][userId].isWriteOpenNotified && autoLaunchItem.notifier != nullptr) { - extItemMap_[identifier][userId].isWriteOpenNotifiered = true; + extItemMap_[identifier][userId].isWriteOpenNotified = true; notifier = autoLaunchItem.notifier; } else { return; @@ -929,7 +929,7 @@ void AutoLaunch::ExtConnectionLifeCycleCallbackTask(const std::string &identifie } LOGI("[AutoLaunch] ExtConnectionLifeCycleCallbackTask do CloseConnection"); TryCloseConnection(autoLaunchItem); // do nothing if failed - if (autoLaunchItem.isWriteOpenNotifiered) { + if (autoLaunchItem.isWriteOpenNotified) { CloseNotifier(autoLaunchItem); } } diff --git a/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp b/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp index ffd6fc7b6..ac12ae963 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp @@ -41,7 +41,7 @@ Blob::Blob(Blob &&blob) : ptr_(blob.ptr_), size_(blob.size_) Blob &Blob::operator=(Blob &&blob) noexcept { - if (this != &blob) { + if (&blob != this) { delete[] ptr_; ptr_ = blob.ptr_; size_ = blob.size_; @@ -104,7 +104,7 @@ DataValue::DataValue(DataValue &&dataValue) noexcept DataValue &DataValue::operator=(const DataValue &dataValue) { - if (this == &dataValue) { + if (&dataValue == this) { return *this; } ResetValue(); @@ -131,7 +131,7 @@ DataValue &DataValue::operator=(const DataValue &dataValue) DataValue &DataValue::operator=(DataValue &&dataValue) noexcept { - if (this == &dataValue) { + if (&dataValue == this) { return *this; } ResetValue(); @@ -299,7 +299,7 @@ int DataValue::GetBlob(Blob &outVal) const int DataValue::SetText(const std::string &val) { - return SetText(reinterpret_cast(val.c_str()), val.length()); + return SetText(reinterpret_cast(val.c_str()), val.length()); } int DataValue::SetText(const uint8_t *val, uint32_t length) diff --git a/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp b/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp index d1eb269e4..732f386d1 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp @@ -55,7 +55,7 @@ bool SchemaObject::FlatBufferSchema::IsFlatBufferSchema(const std::string &inOri LOGE("[FBSchema][Is] OriSchema empty."); return false; } - if (inOriginal.size() >= SchemaConstant::SCHEMA_STRING_SIZE_LIMIT * 2) { + if (inOriginal.size() >= SchemaConstant::SCHEMA_STRING_SIZE_LIMIT * 2) { // 2 :Maximum base64 encode size multiple // Base64 encode will not exceed 2 times original binary LOGE("[FBSchema][Is] OriSchemaSize=%zu too large even after base64 encode.", inOriginal.size()); return false; diff --git a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp index a217787bf..5ab27e045 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp @@ -505,7 +505,6 @@ RelationalSyncOpinion RelationalSchemaObject::MakeLocalSyncOpinion(const Relatio { SchemaType localType = localSchema.GetSchemaType(); SchemaType remoteType = ReadSchemaType(remoteSchemaType); - if (remoteType == SchemaType::UNRECOGNIZED) { LOGW("[RelationalSchema][opinion] Remote schema type %d is unrecognized.", remoteSchemaType); return {}; diff --git a/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp index 8dc67aa17..25b538deb 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp @@ -718,7 +718,8 @@ int SchemaObject::ParseCheckSchemaIndexes(const JsonObject& inJsonObject) // The type of SCHEMA_INDEXES field has been checked in CheckMetaFieldCountAndType to be an array // If not all members of the array are string type or string-array, this call will return error std::vector> oriIndexArray; - int errCode = inJsonObject.GetArrayContentOfStringOrStringArray(FieldPath{SchemaConstant::KEYWORD_SCHEMA_INDEXES}, oriIndexArray); + int errCode = inJsonObject.GetArrayContentOfStringOrStringArray(FieldPath{SchemaConstant::KEYWORD_SCHEMA_INDEXES}, + oriIndexArray); if (errCode != E_OK) { LOGE("[Schema][ParseIndex] GetArrayContent Fail, errCode=%d.", errCode); return -E_SCHEMA_PARSE_FAIL; @@ -745,7 +746,8 @@ int SchemaObject::ParseCheckSchemaSkipSize(const JsonObject& inJsonObject) } // The type of SCHEMA_SKIPSIZE field has been checked in CheckMetaFieldCountAndType to be an INTEGER FieldValue skipSizeValue; - int errCode = inJsonObject.GetFieldValueByFieldPath(FieldPath{SchemaConstant::KEYWORD_SCHEMA_SKIPSIZE}, skipSizeValue); + int errCode = inJsonObject.GetFieldValueByFieldPath(FieldPath{SchemaConstant::KEYWORD_SCHEMA_SKIPSIZE}, + skipSizeValue); if (errCode != E_OK) { return -E_INTERNAL_ERROR; } diff --git a/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp b/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp index b22eb7479..d66f4b93d 100644 --- a/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp +++ b/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp @@ -132,10 +132,10 @@ public: } } - static TimeStamp GetTime(TimeOffset TimeOffset) + static TimeStamp GetTime(TimeOffset timeOffset) { TimeStamp currentSysTime = GetSysCurrentTime(); - TimeStamp currentLocalTime = currentSysTime + TimeOffset; + TimeStamp currentLocalTime = currentSysTime + timeOffset; std::lock_guard lock(lastLocalTimeLock_); if (currentLocalTime <= lastLocalTime_ || currentLocalTime > MAX_VALID_TIME) { lastLocalTime_++; diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h index 956f5d0c5..10cf53d54 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h @@ -25,7 +25,7 @@ #include "isync_task_context.h" #include "parcel.h" #ifdef RELATIONAL_STORE -#include "schema.h" +#include "ischema.h" #endif namespace DistributedDB { diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h index 303ba4a40..1f60d7659 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h @@ -23,7 +23,7 @@ #include "db_ability.h" #include "query_sync_object.h" -#include "schema.h" +#include "ischema.h" #include "single_ver_kvdb_sync_interface.h" #include "single_ver_sync_target.h" #include "subscribe_manager.h" -- Gitee From 302cffacf68be22b49568f397f5496bda31100aa Mon Sep 17 00:00:00 2001 From: zqq Date: Thu, 24 Feb 2022 16:17:53 +0800 Subject: [PATCH 04/37] fix codedex Signed-off-by: zqq --- .../src/single_ver_sync_state_machine.cpp | 68 ++++++++++--------- .../src/single_ver_sync_state_machine.h | 10 +-- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index c92ca4f48..aef40cbe5 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -32,6 +32,8 @@ #include "single_ver_data_sync_utils.h" namespace DistributedDB { +using Event = SingleVerSyncStateMachine::Event; +using State = SingleVerSyncStateMachine::State; namespace { // used for state switch table const int CURRENT_STATE_INDEX = 0; @@ -41,54 +43,54 @@ namespace { // drop v1 and v2 table by one optimize, dataSend mode in all version go with slide window mode. // State switch table v3, has three columns, CurrentState, Event, and OutSate const std::vector> STATE_SWITCH_TABLE_V3 = { - {IDLE, START_SYNC_EVENT, TIME_SYNC}, + {State::IDLE, Event::START_SYNC_EVENT, State::TIME_SYNC}, // In TIME_SYNC state - {TIME_SYNC, TIME_SYNC_FINISHED_EVENT, ABILITY_SYNC}, - {TIME_SYNC, TIME_OUT_EVENT, SYNC_TIME_OUT}, - {TIME_SYNC, INNER_ERR_EVENT, INNER_ERR}, + {State::TIME_SYNC, Event::TIME_SYNC_FINISHED_EVENT, State::ABILITY_SYNC}, + {State::TIME_SYNC, Event::TIME_OUT_EVENT, State::SYNC_TIME_OUT}, + {State::TIME_SYNC, Event::INNER_ERR_EVENT, State::INNER_ERR}, // In ABILITY_SYNC state, compare version num and schema - {ABILITY_SYNC, VERSION_NOT_SUPPOR_EVENT, INNER_ERR}, - {ABILITY_SYNC, ABILITY_SYNC_FINISHED_EVENT, START_INITIACTIVE_DATA_SYNC}, - {ABILITY_SYNC, TIME_OUT_EVENT, SYNC_TIME_OUT}, - {ABILITY_SYNC, INNER_ERR_EVENT, INNER_ERR}, - {ABILITY_SYNC, CONTROL_CMD_EVENT, SYNC_CONTROL_CMD}, + {State::ABILITY_SYNC, Event::VERSION_NOT_SUPPOR_EVENT, State::INNER_ERR}, + {State::ABILITY_SYNC, Event::ABILITY_SYNC_FINISHED_EVENT, State::START_INITIACTIVE_DATA_SYNC}, + {State::ABILITY_SYNC, Event::TIME_OUT_EVENT, State::SYNC_TIME_OUT}, + {State::ABILITY_SYNC, Event::INNER_ERR_EVENT, State::INNER_ERR}, + {State::ABILITY_SYNC, Event::CONTROL_CMD_EVENT, State::SYNC_CONTROL_CMD}, // In START_INITIACTIVE_DATA_SYNC state, send a sync request, and send first packt of data sync - {START_INITIACTIVE_DATA_SYNC, NEED_ABILITY_SYNC_EVENT, ABILITY_SYNC}, - {START_INITIACTIVE_DATA_SYNC, TIME_OUT_EVENT, SYNC_TIME_OUT}, - {START_INITIACTIVE_DATA_SYNC, INNER_ERR_EVENT, INNER_ERR}, - {START_INITIACTIVE_DATA_SYNC, SEND_FINISHED_EVENT, START_PASSIVE_DATA_SYNC}, - {START_INITIACTIVE_DATA_SYNC, RE_SEND_DATA_EVENT, START_INITIACTIVE_DATA_SYNC}, + {State::START_INITIACTIVE_DATA_SYNC, Event::NEED_ABILITY_SYNC_EVENT, State::ABILITY_SYNC}, + {State::START_INITIACTIVE_DATA_SYNC, Event::TIME_OUT_EVENT, State::SYNC_TIME_OUT}, + {State::START_INITIACTIVE_DATA_SYNC, Event::INNER_ERR_EVENT, State::INNER_ERR}, + {State::START_INITIACTIVE_DATA_SYNC, Event::SEND_FINISHED_EVENT, State::START_PASSIVE_DATA_SYNC}, + {State::START_INITIACTIVE_DATA_SYNC, Event::RE_SEND_DATA_EVENT, State::START_INITIACTIVE_DATA_SYNC}, // In START_PASSIVE_DATA_SYNC state, do response pull request, and send first packt of data sync - {START_PASSIVE_DATA_SYNC, SEND_FINISHED_EVENT, START_PASSIVE_DATA_SYNC}, - {START_PASSIVE_DATA_SYNC, RESPONSE_TASK_FINISHED_EVENT, WAIT_FOR_RECEIVE_DATA_FINISH}, - {START_PASSIVE_DATA_SYNC, TIME_OUT_EVENT, SYNC_TIME_OUT}, - {START_PASSIVE_DATA_SYNC, INNER_ERR_EVENT, INNER_ERR}, - {START_PASSIVE_DATA_SYNC, NEED_ABILITY_SYNC_EVENT, ABILITY_SYNC}, - {START_PASSIVE_DATA_SYNC, RE_SEND_DATA_EVENT, START_PASSIVE_DATA_SYNC}, + {State::START_PASSIVE_DATA_SYNC, Event::SEND_FINISHED_EVENT, State::START_PASSIVE_DATA_SYNC}, + {State::START_PASSIVE_DATA_SYNC, Event::RESPONSE_TASK_FINISHED_EVENT, State::WAIT_FOR_RECEIVE_DATA_FINISH}, + {State::START_PASSIVE_DATA_SYNC, Event::TIME_OUT_EVENT, State::SYNC_TIME_OUT}, + {State::START_PASSIVE_DATA_SYNC, Event::INNER_ERR_EVENT, State::INNER_ERR}, + {State::START_PASSIVE_DATA_SYNC, Event::NEED_ABILITY_SYNC_EVENT, State::ABILITY_SYNC}, + {State::START_PASSIVE_DATA_SYNC, Event::RE_SEND_DATA_EVENT, State::START_PASSIVE_DATA_SYNC}, // In WAIT_FOR_RECEIVE_DATA_FINISH, - {WAIT_FOR_RECEIVE_DATA_FINISH, RECV_FINISHED_EVENT, SYNC_TASK_FINISHED}, - {WAIT_FOR_RECEIVE_DATA_FINISH, START_PULL_RESPONSE_EVENT, START_PASSIVE_DATA_SYNC}, - {WAIT_FOR_RECEIVE_DATA_FINISH, TIME_OUT_EVENT, SYNC_TIME_OUT}, - {WAIT_FOR_RECEIVE_DATA_FINISH, INNER_ERR_EVENT, INNER_ERR}, - {WAIT_FOR_RECEIVE_DATA_FINISH, NEED_ABILITY_SYNC_EVENT, ABILITY_SYNC}, + {State::WAIT_FOR_RECEIVE_DATA_FINISH, Event::RECV_FINISHED_EVENT, State::SYNC_TASK_FINISHED}, + {State::WAIT_FOR_RECEIVE_DATA_FINISH, Event::START_PULL_RESPONSE_EVENT, State::START_PASSIVE_DATA_SYNC}, + {State::WAIT_FOR_RECEIVE_DATA_FINISH, Event::TIME_OUT_EVENT, State::SYNC_TIME_OUT}, + {State::WAIT_FOR_RECEIVE_DATA_FINISH, Event::INNER_ERR_EVENT, State::INNER_ERR}, + {State::WAIT_FOR_RECEIVE_DATA_FINISH, Event::NEED_ABILITY_SYNC_EVENT, State::ABILITY_SYNC}, - {SYNC_CONTROL_CMD, SEND_FINISHED_EVENT, SYNC_TASK_FINISHED}, - {SYNC_CONTROL_CMD, TIME_OUT_EVENT, SYNC_TIME_OUT}, - {SYNC_CONTROL_CMD, INNER_ERR_EVENT, INNER_ERR}, - {SYNC_CONTROL_CMD, NEED_ABILITY_SYNC_EVENT, ABILITY_SYNC}, + {State::SYNC_CONTROL_CMD, Event::SEND_FINISHED_EVENT, State::SYNC_TASK_FINISHED}, + {State::SYNC_CONTROL_CMD, Event::TIME_OUT_EVENT, State::SYNC_TIME_OUT}, + {State::SYNC_CONTROL_CMD, Event::INNER_ERR_EVENT, State::INNER_ERR}, + {State::SYNC_CONTROL_CMD, Event::NEED_ABILITY_SYNC_EVENT, State::ABILITY_SYNC}, // In SYNC_TASK_FINISHED, - {SYNC_TASK_FINISHED, ALL_TASK_FINISHED_EVENT, IDLE}, - {SYNC_TASK_FINISHED, START_SYNC_EVENT, TIME_SYNC}, + {State::SYNC_TASK_FINISHED, Event::ALL_TASK_FINISHED_EVENT, State::IDLE}, + {State::SYNC_TASK_FINISHED, Event::START_SYNC_EVENT, State::TIME_SYNC}, // SYNC_TIME_OUT and INNE_ERR state, just do some exception resolve - {SYNC_TIME_OUT, ANY_EVENT, SYNC_TASK_FINISHED}, - {INNER_ERR, ANY_EVENT, SYNC_TASK_FINISHED}, + {State::SYNC_TIME_OUT, Event::ANY_EVENT, State::SYNC_TASK_FINISHED}, + {State::INNER_ERR, Event::ANY_EVENT, State::SYNC_TASK_FINISHED}, }; } diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h index 44aca53fd..6fcc994bd 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h @@ -32,7 +32,9 @@ #include "time_helper.h" namespace DistributedDB { -namespace { +using stateMappingHandler = std::function; +class SingleVerSyncStateMachine : public SyncStateMachine { +public: enum State { IDLE = 0, TIME_SYNC, @@ -66,12 +68,6 @@ namespace { CONTROL_CMD_EVENT, ANY_EVENT }; -} - -using stateMappingHandler = std::function; -class SingleVerSyncStateMachine : public SyncStateMachine { -public: - SingleVerSyncStateMachine(); ~SingleVerSyncStateMachine() override; -- Gitee From 3638a7f881d6353a56b10207fc6559fbf94195d2 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Fri, 25 Feb 2022 11:00:27 +0800 Subject: [PATCH 05/37] Add empty check for parse relational schema string Signed-off-by: lianhuix --- .../common/src/relational/relational_schema_object.cpp | 4 ++-- .../common/distributeddb_relational_schema_object_test.cpp | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp index 5ab27e045..1cb680295 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp @@ -610,8 +610,8 @@ int RelationalSchemaObject::ParseFromSchemaString(const std::string &inSchemaStr return -E_NOT_PERMIT; } - if (inSchemaString.size() > SchemaConstant::SCHEMA_STRING_SIZE_LIMIT) { - LOGE("[RelationalSchema][Parse] SchemaSize=%zu Too Large.", inSchemaString.size()); + if (inSchemaString.empty() || inSchemaString.size() > SchemaConstant::SCHEMA_STRING_SIZE_LIMIT) { + LOGE("[RelationalSchema][Parse] SchemaSize=%zu is invalid.", inSchemaString.size()); return -E_INVALID_ARGS; } JsonObject schemaObj; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp index 6a7ac034a..f7367d0ae 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp @@ -312,6 +312,9 @@ HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalSchemaParseTest003, TABLE_DEFINE_STR_KEY_INVALID + "}"; errCode = schemaObj.ParseFromSchemaString(GenerateFromTableStr("[" + invalidTableStr05 + "]")); EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); + + errCode = schemaObj.ParseFromSchemaString(""); + EXPECT_EQ(errCode, -E_INVALID_ARGS); } /** -- Gitee From 442089e7ecb1ca4c1779ab9869e868052f6e3561 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Fri, 25 Feb 2022 14:25:11 +0800 Subject: [PATCH 06/37] Fix compile error Signed-off-by: lianhuix --- .../include/relational/relational_schema_object.h | 2 +- .../src/relational/relational_schema_object.cpp | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h index a6c3a4b7a..9c97134dc 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h @@ -93,7 +93,7 @@ private: std::map fields_; FieldName primaryKey_; std::map indexDefines_; - mutable std::vector fieldNames_; + mutable std::vector fieldInfos_; }; class RelationalSyncOpinion { diff --git a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp index 1cb680295..228011e1f 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp @@ -338,19 +338,6 @@ int TableInfo::CompareWithTableIndex(const std::map Date: Mon, 28 Feb 2022 10:54:19 +0800 Subject: [PATCH 07/37] Fix codecheck Signed-off-by: lianhuix --- .../common/src/flatbuffer_schema.cpp | 2 +- .../common/src/query_expression.cpp | 3 +- .../distributeddb/common/src/schema_utils.cpp | 3 +- .../common/distributeddb_auto_launch_test.cpp | 34 +++++++++---------- .../common/distributeddb_schema_unit_test.cpp | 1 - .../common/distributeddb_tools_unit_test.cpp | 32 ++++++++--------- .../common/distributeddb_tools_unit_test.h | 2 -- .../common/common/evloop_timer_unit_test.cpp | 18 +++++----- 8 files changed, 45 insertions(+), 50 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp b/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp index 732f386d1..ae197fb64 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/flatbuffer_schema.cpp @@ -72,7 +72,7 @@ bool SchemaObject::FlatBufferSchema::IsFlatBufferSchema(const std::string &inOri // A macro check pointer get from flatbuffer that won't be nullptr(required field) in fact after verified by flatbuffer #define CHECK_NULL_UNLIKELY_RETURN_ERROR(pointer) \ - if (pointer == nullptr) { \ + if ((pointer) == nullptr) { \ return -E_INTERNAL_ERROR; \ } diff --git a/services/distributeddataservice/libs/distributeddb/common/src/query_expression.cpp b/services/distributeddataservice/libs/distributeddb/common/src/query_expression.cpp index 26f5ee403..6de2914ea 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/query_expression.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/query_expression.cpp @@ -18,8 +18,7 @@ #include "db_errno.h" namespace DistributedDB { -namespace -{ +namespace { const int MAX_OPR_TIMES = 256; } // namespace diff --git a/services/distributeddataservice/libs/distributeddb/common/src/schema_utils.cpp b/services/distributeddataservice/libs/distributeddb/common/src/schema_utils.cpp index dc18309d5..802d9f6ad 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/schema_utils.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/schema_utils.cpp @@ -23,8 +23,7 @@ #include "schema_constant.h" namespace DistributedDB { -namespace -{ +namespace { bool IsLegalFieldCharacter(char character) { return (std::isalnum(character) || character == '_'); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp index 21c729bed..64da6511c 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp @@ -564,22 +564,22 @@ HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch006, TestSize.Level3) std::mutex cvLock; std::condition_variable cv; bool threadIsWorking = true; - thread thread([&cvLock, &cv, &threadIsWorking](){ - LabelType label(g_identifierA.begin(), g_identifierA.end()); - for (int i = 0; i < TEST_ONLINE_CNT; i++) { - g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, true); - std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME)); - g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false); - std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME)); - g_communicatorAggregator->RunCommunicatorLackCallback(label); - std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME)); - LOGD("AutoLaunch006 thread i:%d", i); - } - std::unique_lock lock(cvLock); - threadIsWorking = false; - cv.notify_one(); - }); - thread.detach(); + thread aggregatorThread([&cvLock, &cv, &threadIsWorking]() { + LabelType label(g_identifierA.begin(), g_identifierA.end()); + for (int i = 0; i < TEST_ONLINE_CNT; i++) { + g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, true); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME)); + g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME)); + g_communicatorAggregator->RunCommunicatorLackCallback(label); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SHORT_TIME)); + LOGD("AutoLaunch006 thread i:%d", i); + } + std::unique_lock lock(cvLock); + threadIsWorking = false; + cv.notify_one(); + }); + aggregatorThread.detach(); AutoLaunchOption option; option.notifier = nullptr; option.observer = observer; @@ -596,7 +596,7 @@ HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch006, TestSize.Level3) LOGD("AutoLaunch006 disable i:%d", i); } std::unique_lock lock(cvLock); - cv.wait(lock, [&threadIsWorking]{return threadIsWorking == false;}); + cv.wait(lock, [&threadIsWorking] { return threadIsWorking == false; }); delete observer; g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_schema_unit_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_schema_unit_test.cpp index 3284cb843..7f9a88c1b 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_schema_unit_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_schema_unit_test.cpp @@ -410,7 +410,6 @@ HWTEST_F(DistributedDBSchemalTest, CheckFieldName002, TestSize.Level1) for (auto &iter : preData) { EXPECT_EQ(SchemaUtils::CheckFieldName(iter), -E_SCHEMA_PARSE_FAIL); } - } /** diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.cpp index 68a2711d1..cb4d16398 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.cpp @@ -760,12 +760,12 @@ DBStatus DistributedDBToolsUnitTest::SyncTest(KvStoreNbDelegate* delegate, }, query, false); std::unique_lock lock(syncLock_); - syncCondVar_.wait(lock, [callStatus, &statuses](){ - if (callStatus != OK) { - return true; - } - return !statuses.empty(); - }); + syncCondVar_.wait(lock, [callStatus, &statuses]() { + if (callStatus != OK) { + return true; + } + return !statuses.empty(); + }); return callStatus; } @@ -782,16 +782,16 @@ DBStatus DistributedDBToolsUnitTest::SyncTest(KvStoreNbDelegate* delegate, }, wait); if (!wait) { std::unique_lock lock(syncLock_); - syncCondVar_.wait(lock, [callStatus, &statuses](){ - if (callStatus != OK) { - return true; - } - if (statuses.size() != 0) { - return true; - } - return false; - }); - } + syncCondVar_.wait(lock, [callStatus, &statuses]() { + if (callStatus != OK) { + return true; + } + if (statuses.size() != 0) { + return true; + } + return false; + }); + } return callStatus; } diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.h b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.h index d87bdcc5b..02686a85e 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.h +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_tools_unit_test.h @@ -45,8 +45,6 @@ #include "sync_types.h" #include "store_types.h" namespace DistributedDBUnitTest { -static const int DIR_PERMISSION = 0750; - struct DatabaseInfo { std::string appId{}; std::string userId{}; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/evloop_timer_unit_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/evloop_timer_unit_test.cpp index 038ddbb95..67810662b 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/evloop_timer_unit_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/evloop_timer_unit_test.cpp @@ -116,7 +116,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest001, TestSize.Level0 * @tc.expected: step2. destroy successfully. */ bool finalized = false; - loop->OnLastRef([&finalized](){ finalized = true; }); + loop->OnLastRef([&finalized]() { finalized = true; }); loop->DecObjRef(loop); loop = nullptr; EXPECT_EQ(finalized, true); @@ -140,7 +140,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest002, TestSize.Level1 */ std::atomic running(false); EventTime delta = 0; - std::thread loopThread([&running, &delta](){ + std::thread loopThread([&running, &delta]() { running = true; EventTime start = TimerTester::GetCurrentTime(); g_loop->Run(); @@ -180,7 +180,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest003, TestSize.Level0 bool finalized = false; errCode = timer->SetAction([](EventsMask revents) -> int { return E_OK; - }, [&finalized](){ + }, [&finalized]() { finalized = true; }); EXPECT_EQ(errCode, E_OK); @@ -206,7 +206,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest004, TestSize.Level1 * @tc.expected: step1. start successfully. */ std::atomic running(false); - std::thread loopThread([&running](){ + std::thread loopThread([&running]() { running = true; g_loop->Run(); }); @@ -258,7 +258,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest005, TestSize.Level1 * @tc.expected: step1. start successfully. */ std::atomic running(false); - std::thread loopThread([&running](){ + std::thread loopThread([&running]() { running = true; g_loop->Run(); }); @@ -282,7 +282,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest005, TestSize.Level1 [&counter](EventsMask revents) -> int { ++counter; return E_OK; - }, [&finalize](){ finalize = true; }); + }, [&finalize]() { finalize = true; }); EXPECT_EQ(errCode, E_OK); errCode = g_loop->Add(timer); EXPECT_EQ(errCode, E_OK); @@ -317,7 +317,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest006, TestSize.Level1 * @tc.expected: step1. start successfully. */ std::atomic running(false); - std::thread loopThread([&running](){ + std::thread loopThread([&running]() { running = true; g_loop->Run(); }); @@ -338,7 +338,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest006, TestSize.Level1 std::atomic counter(0); std::atomic finalize(false); errCode = timer->SetAction([&counter](EventsMask revents) -> int { ++counter; return -E_STALE; }, - [&finalize](){ finalize = true; }); + [&finalize]() { finalize = true; }); EXPECT_EQ(errCode, E_OK); errCode = g_loop->Add(timer); EXPECT_EQ(errCode, E_OK); @@ -373,7 +373,7 @@ HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest007, TestSize.Level2 * @tc.expected: step1. start successfully. */ std::atomic running(false); - std::thread loopThread([&running](){ + std::thread loopThread([&running]() { running = true; g_loop->Run(); }); -- Gitee From 465997b4627c9053e5d66914fea22fcb76762b79 Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 28 Feb 2022 15:06:13 +0800 Subject: [PATCH 08/37] fix code check Signed-off-by: zqq --- .../communicator/include/combine_status.h | 1 - .../src/communicator_aggregator.cpp | 2 +- .../distributeddb_communicator_common.h | 2 +- .../distributeddb_communicator_deep_test.cpp | 6 ++-- ...buteddb_communicator_send_receive_test.cpp | 30 +++++++++---------- .../distributeddb_communicator_test.cpp | 6 ++-- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/communicator/include/combine_status.h b/services/distributeddataservice/libs/distributeddb/communicator/include/combine_status.h index 017243e3a..931f84ce0 100644 --- a/services/distributeddataservice/libs/distributeddb/communicator/include/combine_status.h +++ b/services/distributeddataservice/libs/distributeddb/communicator/include/combine_status.h @@ -17,7 +17,6 @@ #define COMBINE_STATUS_H #include -#include #include namespace DistributedDB { diff --git a/services/distributeddataservice/libs/distributeddb/communicator/src/communicator_aggregator.cpp b/services/distributeddataservice/libs/distributeddb/communicator/src/communicator_aggregator.cpp index 019153c6a..5614b31a9 100755 --- a/services/distributeddataservice/libs/distributeddb/communicator/src/communicator_aggregator.cpp +++ b/services/distributeddataservice/libs/distributeddb/communicator/src/communicator_aggregator.cpp @@ -417,7 +417,7 @@ int CommunicatorAggregator::RetryUntilTimeout(SendTask &inTask, uint32_t timeout int errCode = scheduler_.AddSendTaskIntoSchedule(inTask, inPrio); if (errCode != E_OK) { bool notTimeout = true; - auto retryFunc = [this, inPrio, &inTask]()->bool{ + auto retryFunc = [this, inPrio, &inTask]()->bool { if (this->shutdown_) { delete inTask.buffer; inTask.buffer = nullptr; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_common.h b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_common.h index da8823946..35e5b048c 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_common.h +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_common.h @@ -141,7 +141,7 @@ DistributedDB::Message *BuildUnRegedTinyMessage(); #define ASSERT_NOT_NULL_AND_ACTIVATE(communicator) \ { \ ASSERT_NE(communicator, nullptr); \ - communicator->Activate(); \ + (communicator)->Activate(); \ } #endif // DISTRIBUTEDDB_COMMUNICATOR_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp index c7447567c..bcafac2d8 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp @@ -140,7 +140,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, WaitAndRetrySend001, TestSize.Level2 { // Preset Message *msgForBB = nullptr; - g_commBB->RegOnMessageCallback([&msgForBB](const std::string &srcTarget, Message *inMsg){ + g_commBB->RegOnMessageCallback([&msgForBB](const std::string &srcTarget, Message *inMsg) { msgForBB = inMsg; }, nullptr); @@ -312,7 +312,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, Fragment001, TestSize.Level2) { // Preset Message *recvMsgForBB = nullptr; - g_commBB->RegOnMessageCallback([&recvMsgForBB](const std::string &srcTarget, Message *inMsg){ + g_commBB->RegOnMessageCallback([&recvMsgForBB](const std::string &srcTarget, Message *inMsg) { recvMsgForBB = inMsg; }, nullptr); @@ -367,7 +367,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, Fragment002, TestSize.Level2) { // Preset Message *recvMsgForCC = nullptr; - g_commCC->RegOnMessageCallback([&recvMsgForCC](const std::string &srcTarget, Message *inMsg){ + g_commCC->RegOnMessageCallback([&recvMsgForCC](const std::string &srcTarget, Message *inMsg) { recvMsgForCC = inMsg; }, nullptr); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp index 204e08876..7ddb34706 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp @@ -134,15 +134,15 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendAndReceive001, TestSize.L Message *recvMsgForBA = nullptr; string srcTargetForBB; Message *recvMsgForBB = nullptr; - g_commAA->RegOnMessageCallback([&srcTargetForAA, &recvMsgForAA](const std::string &srcTarget, Message *inMsg){ + g_commAA->RegOnMessageCallback([&srcTargetForAA, &recvMsgForAA](const std::string &srcTarget, Message *inMsg) { srcTargetForAA = srcTarget; recvMsgForAA = inMsg; }, nullptr); - g_commBA->RegOnMessageCallback([&srcTargetForBA, &recvMsgForBA](const std::string &srcTarget, Message *inMsg){ + g_commBA->RegOnMessageCallback([&srcTargetForBA, &recvMsgForBA](const std::string &srcTarget, Message *inMsg) { srcTargetForBA = srcTarget; recvMsgForBA = inMsg; }, nullptr); - g_commBB->RegOnMessageCallback([&srcTargetForBB, &recvMsgForBB](const std::string &srcTarget, Message *inMsg){ + g_commBB->RegOnMessageCallback([&srcTargetForBB, &recvMsgForBB](const std::string &srcTarget, Message *inMsg) { srcTargetForBB = srcTarget; recvMsgForBB = inMsg; }, nullptr); @@ -261,8 +261,8 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl001, TestSize. // Preset int countForBA = 0; int countForBB = 0; - g_commBA->RegOnSendableCallback([&countForBA](){countForBA++;}, nullptr); - g_commBB->RegOnSendableCallback([&countForBB](){countForBB++;}, nullptr); + g_commBA->RegOnSendableCallback([&countForBA](){ countForBA++; }, nullptr); + g_commBB->RegOnSendableCallback([&countForBB](){ countForBB++; }, nullptr); /** * @tc.steps: step1. connect device A with device B @@ -325,8 +325,8 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl002, TestSize. // Preset int cntForBA = 0; int cntForBB = 0; - g_commBA->RegOnSendableCallback([&cntForBA](){cntForBA++;}, nullptr); - g_commBB->RegOnSendableCallback([&cntForBB](){cntForBB++;}, nullptr); + g_commBA->RegOnSendableCallback([&cntForBA](){ cntForBA++; }, nullptr); + g_commBB->RegOnSendableCallback([&cntForBB](){ cntForBB++; }, nullptr); /** * @tc.steps: step1. connect device A with device B @@ -347,7 +347,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl002, TestSize. */ int sendCount = 0; int sendFailCount = 0; - std::thread sendThread([&sendCount, &sendFailCount](){ + std::thread sendThread([&sendCount, &sendFailCount]() { while (sendCount < SEND_COUNT_GOAL) { Message *msgForBA = BuildRegedHugeMessage(); ASSERT_NE(msgForBA, nullptr); @@ -391,8 +391,8 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl003, TestSize. // Preset int cntsForBA = 0; int cntsForBB = 0; - g_commBA->RegOnSendableCallback([&cntsForBA](){cntsForBA++;}, nullptr); - g_commBB->RegOnSendableCallback([&cntsForBB](){cntsForBB++;}, nullptr); + g_commBA->RegOnSendableCallback([&cntsForBA](){ cntsForBA++; }, nullptr); + g_commBB->RegOnSendableCallback([&cntsForBB](){ cntsForBB++; }, nullptr); /** * @tc.steps: step1. connect device A with device B @@ -413,7 +413,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl003, TestSize. */ int sendCnt = 0; int sendFailCnt = 0; - std::thread sendThread([&sendCnt, &sendFailCnt](){ + std::thread sendThread([&sendCnt, &sendFailCnt]() { while (sendCnt < SEND_COUNT_GOAL) { Message *msgForBA = BuildRegedHugeMessage(); ASSERT_NE(msgForBA, nullptr); @@ -456,7 +456,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, ReceiveCheck001, TestSize.Lev { // Preset int recvCount = 0; - g_commAA->RegOnMessageCallback([&recvCount](const std::string &srcTarget, Message *inMsg){ + g_commAA->RegOnMessageCallback([&recvCount](const std::string &srcTarget, Message *inMsg) { recvCount++; if (inMsg != nullptr) { delete inMsg; @@ -517,7 +517,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, ReceiveCheck002, TestSize.Lev { // Preset int recvCount = 0; - g_commAA->RegOnMessageCallback([&recvCount](const std::string &srcTarget, Message *inMsg){ + g_commAA->RegOnMessageCallback([&recvCount](const std::string &srcTarget, Message *inMsg) { recvCount++; if (inMsg != nullptr) { delete inMsg; @@ -714,11 +714,11 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendAndReceiveWithExtendHead0 string srcTargetForBA; Message *recvMsgForBA = nullptr; TimeSync::RegisterTransformFunc(); - g_commAA->RegOnMessageCallback([&srcTargetForAA, &recvMsgForAA](const std::string &srcTarget, Message *inMsg){ + g_commAA->RegOnMessageCallback([&srcTargetForAA, &recvMsgForAA](const std::string &srcTarget, Message *inMsg) { srcTargetForAA = srcTarget; recvMsgForAA = inMsg; }, nullptr); - g_commBA->RegOnMessageCallback([&srcTargetForBA, &recvMsgForBA](const std::string &srcTarget, Message *inMsg){ + g_commBA->RegOnMessageCallback([&srcTargetForBA, &recvMsgForBA](const std::string &srcTarget, Message *inMsg) { srcTargetForBA = srcTarget; recvMsgForBA = inMsg; }, nullptr); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp index 8407d3384..ef5851c51 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp @@ -175,7 +175,7 @@ HWTEST_F(DistributedDBCommunicatorTest, OnlineAndOffline001, TestSize.Level1) ICommunicator *commAA = g_envDeviceA.commAggrHandle->AllocCommunicator(LABEL_A, errorNo); ASSERT_NOT_NULL_AND_ACTIVATE(commAA); OnOfflineDevice onlineForAA; - commAA->RegOnConnectCallback([&onlineForAA](const std::string &target, bool isConnect){ + commAA->RegOnConnectCallback([&onlineForAA](const std::string &target, bool isConnect) { HandleConnectChange(onlineForAA, target, isConnect);}, nullptr); EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast(0)); @@ -193,7 +193,7 @@ HWTEST_F(DistributedDBCommunicatorTest, OnlineAndOffline001, TestSize.Level1) ICommunicator *commBB = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_B, errorNo); ASSERT_NOT_NULL_AND_ACTIVATE(commBB); OnOfflineDevice onlineForBB; - commBB->RegOnConnectCallback([&onlineForBB](const std::string &target, bool isConnect){ + commBB->RegOnConnectCallback([&onlineForBB](const std::string &target, bool isConnect) { HandleConnectChange(onlineForBB, target, isConnect);}, nullptr); EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast(0)); EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast(0)); @@ -213,7 +213,7 @@ HWTEST_F(DistributedDBCommunicatorTest, OnlineAndOffline001, TestSize.Level1) ICommunicator *commBA = g_envDeviceB.commAggrHandle->AllocCommunicator(LABEL_A, errorNo); ASSERT_NOT_NULL_AND_ACTIVATE(commBA); OnOfflineDevice onlineForBA; - commBA->RegOnConnectCallback([&onlineForBA](const std::string &target, bool isConnect){ + commBA->RegOnConnectCallback([&onlineForBA](const std::string &target, bool isConnect) { HandleConnectChange(onlineForBA, target, isConnect);}, nullptr); EXPECT_EQ(onlineForAA.onlineDevices.size(), static_cast(0)); EXPECT_EQ(onlineForBB.onlineDevices.size(), static_cast(0)); -- Gitee From 3a3b50444ec7f238c6c4d7a9b96f5e33b8c1cb1a Mon Sep 17 00:00:00 2001 From: lianhuix Date: Wed, 2 Mar 2022 09:59:28 +0800 Subject: [PATCH 09/37] Fix memcheck issue, Set aggregator to nullptr Signed-off-by: lianhuix --- .../distributeddb_interfaces_relational_sync_test.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp index 704741e11..6ca71a9a4 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp @@ -57,7 +57,7 @@ namespace { class DistributedDBInterfacesRelationalSyncTest : public testing::Test { public: static void SetUpTestCase(void); - static void TearDownTestCase(void) {}; + static void TearDownTestCase(void); void SetUp() override ; void TearDown() override; protected: @@ -76,6 +76,11 @@ void DistributedDBInterfacesRelationalSyncTest::SetUpTestCase(void) RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator); } +void DistributedDBInterfacesRelationalSyncTest::TearDownTestCase(void) +{ + RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr); +} + void DistributedDBInterfacesRelationalSyncTest::SetUp() { DistributedDBToolsUnitTest::PrintTestCaseInfo(); -- Gitee From d9c214a1254ebbb237453f87e5887c1814f74a12 Mon Sep 17 00:00:00 2001 From: lidwchn Date: Tue, 1 Mar 2022 17:49:51 +0800 Subject: [PATCH 10/37] Fix code check. Signed-off-by: lidwchn --- .../common/src/platform_specific.cpp | 5 +++- .../storage/include/db_properties.h | 1 - .../storage/src/default_factory.h | 2 +- .../src/generic_single_ver_kv_entry.cpp | 25 +++++++------------ .../storage/src/generic_single_ver_kv_entry.h | 9 +++++-- .../src/multiver/multi_ver_natural_store.cpp | 6 ++--- .../sqlite_single_ver_natural_store.cpp | 4 +-- .../sqlite_single_ver_storage_engine.cpp | 2 +- .../sqlite_single_ver_storage_executor.cpp | 2 +- .../storage/src/sqlite/sqlite_utils.cpp | 24 +++++++++++------- .../storage/src/storage_engine.cpp | 13 +++++----- .../distributeddb_multi_ver_vacuum_test.cpp | 4 +-- 12 files changed, 52 insertions(+), 45 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp b/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp index 264842e87..f7866cbe2 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp @@ -138,7 +138,10 @@ int GetRealPath(const std::string &inOriPath, std::string &outRealPath) if (realPath == nullptr) { return -E_OUT_OF_MEMORY; } - (void)memset_s(realPath, MAX_PATH_LENGTH + 1, 0, MAX_PATH_LENGTH + 1); + if (memset_s(realPath, MAX_PATH_LENGTH + 1, 0, MAX_PATH_LENGTH + 1) != EOK) { + return -E_SECUREC_ERROR; + } + if (realpath(inOriPath.c_str(), realPath) == nullptr) { LOGE("[RealPath] Get realpath for inOriPath fail:%d.", errno); delete []realPath; diff --git a/services/distributeddataservice/libs/distributeddb/storage/include/db_properties.h b/services/distributeddataservice/libs/distributeddb/storage/include/db_properties.h index 32b4d5772..6b7be4a0b 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/include/db_properties.h +++ b/services/distributeddataservice/libs/distributeddb/storage/include/db_properties.h @@ -17,7 +17,6 @@ #include #include -#include namespace DistributedDB { class DBProperties { diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/default_factory.h b/services/distributeddataservice/libs/distributeddb/storage/src/default_factory.h index fa3b4f73c..975a00679 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/default_factory.h +++ b/services/distributeddataservice/libs/distributeddb/storage/src/default_factory.h @@ -22,7 +22,7 @@ namespace DistributedDB { class DefaultFactory final : public IKvDBFactory { public: - DefaultFactory() {} + DefaultFactory() noexcept {} ~DefaultFactory() override {} DISABLE_COPY_ASSIGN_MOVE(DefaultFactory); diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp index b1359bf7c..7b0a11155 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp @@ -22,14 +22,6 @@ #include "version.h" namespace DistributedDB { -namespace { - enum OperType { - SERIALIZE, - DESERIALIZE, - CAL_LEN, - }; -} // namespace - GenericSingleVerKvEntry::GenericSingleVerKvEntry() { } @@ -126,7 +118,7 @@ int GenericSingleVerKvEntry::SerializeData(Parcel &parcel, uint32_t targetVersio if (errCode != E_OK) { return errCode; } - errCode = AdaptToVersion(SERIALIZE, targetVersion, parcel, len); + errCode = AdaptToVersion(OperType::SERIALIZE, targetVersion, parcel, len); if (errCode != E_OK) { return errCode; } @@ -160,7 +152,7 @@ int GenericSingleVerKvEntry::SerializeDatas(const std::vector INT32_MAX) || (errCode != E_OK)) { return 0; } @@ -193,7 +185,7 @@ int GenericSingleVerKvEntry::DeSerializeData(Parcel &parcel) if (parcel.IsError()) { return 0; } - int errCode = AdaptToVersion(DESERIALIZE, version, parcel, len); + int errCode = AdaptToVersion(OperType::DESERIALIZE, version, parcel, len); if (errCode != E_OK) { len = 0; } @@ -231,17 +223,18 @@ END: return len; } -int GenericSingleVerKvEntry::AdaptToVersion(int operType, uint32_t targetVersion, Parcel &parcel, uint64_t &datalen) +int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVersion, Parcel &parcel, + uint64_t &datalen) { if (targetVersion < SOFTWARE_VERSION_EARLIEST || targetVersion > SOFTWARE_VERSION_CURRENT) { return -E_VERSION_NOT_SUPPORT; } int errCode = E_OK; switch (operType) { - case SERIALIZE: + case OperType::SERIALIZE: errCode = SerializeDataByVersion(targetVersion, parcel); break; - case DESERIALIZE: + case OperType::DESERIALIZE: errCode = DeSerializeByVersion(targetVersion, parcel, datalen); break; default: @@ -251,13 +244,13 @@ int GenericSingleVerKvEntry::AdaptToVersion(int operType, uint32_t targetVersion return errCode; } -int GenericSingleVerKvEntry::AdaptToVersion(int 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 == CAL_LEN) { + if (operType == OperType::CAL_LEN) { return CalLenByVersion(targetVersion, datalen); } else { LOGE("Unknown upgrade serialize oper!"); diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h b/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h index 67c036e17..1bf10395b 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h +++ b/services/distributeddataservice/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h @@ -84,8 +84,13 @@ public: static int DeSerializeCompressedDatas(std::vector &kvEntries, Parcel &parcel); private: - int AdaptToVersion(int operType, uint32_t targetVersion, Parcel &parcel, uint64_t &datalen); - int AdaptToVersion(int operType, uint32_t targetVersion, uint64_t &datalen); + enum class OperType { + SERIALIZE, + 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 SerializeDataByVersion(uint32_t targetVersion, Parcel &parcel) const; int SerializeDataByFirstVersion(Parcel &parcel) const; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/multiver/multi_ver_natural_store.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/multiver/multi_ver_natural_store.cpp index 49e082efb..5ab0f971c 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/multiver/multi_ver_natural_store.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/multiver/multi_ver_natural_store.cpp @@ -902,7 +902,7 @@ int MultiVerNaturalStore::CompareVerDataAndLog(IKvDBMultiVerTransaction *transac return errCode; } - // compare the version; + // compare the version if (commitHead->GetCommitVersion() < maxLocalVersion) { LOGD("Delete entries"); errCode = transaction->ClearEntriesByVersion(maxLocalVersion); @@ -934,7 +934,7 @@ MultiVerStorageExecutor *MultiVerNaturalStore::GetHandle(bool isWrite, int &errC } if (isWrite && !isTrimming) { - // stop the trimming; + // stop the trimming shadowTrimmer_.Pause(GetStringIdentifier()); } StorageExecutor *handle = nullptr; @@ -946,7 +946,7 @@ MultiVerStorageExecutor *MultiVerNaturalStore::GetHandle(bool isWrite, int &errC if (handle == nullptr) { if (isWrite && !isTrimming) { - // restart the trimming; + // restart the trimming shadowTrimmer_.Continue(GetStringIdentifier(), false); } } else { diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp index 9856f72fe..c1433fcb5 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp @@ -1346,7 +1346,7 @@ int SQLiteSingleVerNaturalStore::Rekey(const CipherPassword &passwd) std::unique_ptr operation; - // stop the syncer; + // stop the syncer int errCode = storageEngine_->TryToDisable(false, OperatePerm::REKEY_MONOPOLIZE_PERM); if (errCode != E_OK) { return errCode; @@ -1434,7 +1434,7 @@ int SQLiteSingleVerNaturalStore::Import(const std::string &filePath, const Ciphe localDev.resize(0); } - // stop the syncer; + // stop the syncer errCode = storageEngine_->TryToDisable(false, OperatePerm::IMPORT_MONOPOLIZE_PERM); if (errCode != E_OK) { return errCode; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp index 8ae53a1ac..92e7386bb 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp @@ -880,7 +880,7 @@ int SQLiteSingleVerStorageEngine::CreateNewExecutor(bool isWrite, StorageExecuto dbHandle = nullptr; return -E_OUT_OF_MEMORY; } - if (isAttachMeta == true) { + if (isAttachMeta) { SQLiteSingleVerStorageExecutor *singleVerHandle = static_cast(handle); singleVerHandle->SetAttachMetaMode(isAttachMeta); } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp index dd637aaed..ae9970dd0 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.cpp @@ -1530,7 +1530,7 @@ void SQLiteSingleVerStorageExecutor::PutConflictData(const DataItem &itemPut, co } if (dataStatus.preStatus == DataStatus::NOEXISTED || - ((dataStatus.preStatus == DataStatus::DELETED) && (dataStatus.isDeleted == true))) { + ((dataStatus.preStatus == DataStatus::DELETED) && dataStatus.isDeleted)) { return; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index f66cc46a9..b5ab499d9 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -86,6 +86,20 @@ namespace { LOGE("[SQLite] Error[%d], sys[%d]", err, errno); } } + + // statement must not be null + std::string GetColString(sqlite3_stmt *statement, int nCol) + { + std::string rowString; + for (int i = 0; i < nCol; i++) { + if (sqlite3_column_name(statement, i) != nullptr) { + rowString += sqlite3_column_name(statement, i); + } + int blankFill = (i + 1) * 16 - rowString.size(); // each column width 16 + rowString.append(static_cast((blankFill > 0) ? blankFill : 0), ' '); + } + return rowString; + } } namespace TriggerMode { @@ -1888,15 +1902,7 @@ int SQLiteUtils::ExplainPlan(sqlite3 *db, const std::string &execSql, bool isQue nCol = std::min(nCol, 8); // Read 8 column at most if (isFirst) { - std::string rowString; - for (int i = 0; i < nCol; i++) { - if (sqlite3_column_name(statement, i) != nullptr) { - rowString += sqlite3_column_name(statement, i); - } - int blankFill = (i + 1) * 16 - rowString.size(); // each column width 16 - rowString.append(static_cast((blankFill > 0) ? blankFill : 0), ' '); - } - LOGD("#### %s", rowString.c_str()); + LOGD("#### %s", GetColString(statement, nCol).c_str()); isFirst = false; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/storage_engine.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/storage_engine.cpp index 674bb975f..a45b782b9 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/storage_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/storage_engine.cpp @@ -396,12 +396,13 @@ StorageExecutor *StorageEngine::FetchStorageExecutor(bool isWrite, std::listbool{ + stepOne = RepeatCheckAsyncResult([&databaseA]()->bool { return databaseA.IsTransactionOccupied() == true; }, 5, 100); // 5 times, 100 ms EXPECT_EQ(stepOne, true); @@ -582,7 +582,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskWriteHandleOccupy001, TestSi EXPECT_EQ(errCode, E_OK); bool stepThree = CheckVacuumTaskStatus(vacuum, DB_IDENTITY_A, VacuumTaskStatus::RUN_NING); EXPECT_EQ(stepThree, true); - stepThree = RepeatCheckAsyncResult([&databaseA]()->bool{ + stepThree = RepeatCheckAsyncResult([&databaseA]()->bool { return databaseA.IsTransactionOccupied() == true; }, 5, 100); // 5 times, 100 ms EXPECT_EQ(stepThree, true); -- Gitee From 84c54c840b9df720fdeab57534d375f0d3c85f85 Mon Sep 17 00:00:00 2001 From: lidwchn Date: Fri, 25 Feb 2022 15:06:02 +0800 Subject: [PATCH 11/37] Fix issue Signed-off-by: lidwchn --- .../distributeddb/common/include/data_value.h | 4 --- .../common/include/db_constant.h | 2 ++ .../distributeddb/common/src/data_value.cpp | 26 ----------------- .../storage/src/data_transformer.cpp | 19 ------------ .../storage/src/sqlite/query_object.cpp | 2 +- .../storage/src/sqlite/query_sync_object.cpp | 2 +- ...single_ver_relational_storage_executor.cpp | 9 ++---- ...stributeddb_interfaces_data_value_test.cpp | 29 +------------------ .../distributeddb_data_transformer_test.cpp | 6 ---- ...ributeddb_relational_ver_p2p_sync_test.cpp | 16 +--------- 10 files changed, 8 insertions(+), 107 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/include/data_value.h b/services/distributeddataservice/libs/distributeddb/common/include/data_value.h index 336a6257c..f355f7f55 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/data_value.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/data_value.h @@ -25,7 +25,6 @@ namespace DistributedDB { enum class StorageType { STORAGE_TYPE_NONE = 0, STORAGE_TYPE_NULL, - STORAGE_TYPE_BOOL, STORAGE_TYPE_INTEGER, STORAGE_TYPE_REAL, STORAGE_TYPE_TEXT, @@ -63,7 +62,6 @@ public: // move constructor DataValue(DataValue &&dataValue) noexcept; DataValue &operator=(DataValue &&dataValue) noexcept; - DataValue &operator=(bool boolVal); DataValue &operator=(int64_t intVal); DataValue &operator=(double doubleVal); DataValue &operator=(const Blob &blob); @@ -75,7 +73,6 @@ public: bool operator!=(const DataValue &dataValue) const; StorageType GetType() const; - int GetBool(bool &outVal) const; int GetInt64(int64_t &outVal) const; int GetDouble(double &outVal) const; int GetBlob(Blob *&outVal) const; @@ -92,7 +89,6 @@ private: StorageType type_ = StorageType::STORAGE_TYPE_NULL; union { void* zeroMem; - bool bValue; Blob* blobPtr; double dValue; int64_t iValue; diff --git a/services/distributeddataservice/libs/distributeddb/common/include/db_constant.h b/services/distributeddataservice/libs/distributeddb/common/include/db_constant.h index 2356e34dd..a94051de6 100755 --- a/services/distributeddataservice/libs/distributeddb/common/include/db_constant.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/db_constant.h @@ -29,6 +29,8 @@ public: static constexpr size_t MAX_DATA_DIR_LENGTH = 512; + static constexpr size_t MAX_INKEYS_SIZE = 128; + static constexpr int DB_TYPE_LOCAL = 1; static constexpr int DB_TYPE_MULTI_VER = 2; static constexpr int DB_TYPE_SINGLE_VER = 3; diff --git a/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp b/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp index ac12ae963..81929ae34 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/data_value.cpp @@ -109,9 +109,6 @@ DataValue &DataValue::operator=(const DataValue &dataValue) } ResetValue(); switch (dataValue.type_) { - case StorageType::STORAGE_TYPE_BOOL: - (void)dataValue.GetBool(this->value_.bValue); - break; case StorageType::STORAGE_TYPE_INTEGER: (void)dataValue.GetInt64(this->value_.iValue); break; @@ -148,14 +145,6 @@ DataValue &DataValue::operator=(DataValue &&dataValue) noexcept return *this; } -DataValue &DataValue::operator=(bool boolVal) -{ - ResetValue(); - type_ = StorageType::STORAGE_TYPE_BOOL; - value_.bValue = boolVal; - return *this; -} - DataValue &DataValue::operator=(int64_t intVal) { ResetValue(); @@ -203,8 +192,6 @@ bool DataValue::operator==(const DataValue &dataValue) const return false; } switch (type_) { - case StorageType::STORAGE_TYPE_BOOL: - return dataValue.value_.bValue == value_.bValue; case StorageType::STORAGE_TYPE_INTEGER: return dataValue.value_.iValue == value_.iValue; case StorageType::STORAGE_TYPE_REAL: @@ -230,15 +217,6 @@ bool DataValue::operator!=(const DataValue &dataValue) const return !(*this == dataValue); } -int DataValue::GetBool(bool &outVal) const -{ - if (type_ != StorageType::STORAGE_TYPE_BOOL) { - return -E_NOT_SUPPORT; - } - outVal = value_.bValue; - return E_OK; -} - int DataValue::GetDouble(double &outVal) const { if (type_ != StorageType::STORAGE_TYPE_REAL) { @@ -352,7 +330,6 @@ void DataValue::ResetValue() value_.blobPtr = nullptr; break; case StorageType::STORAGE_TYPE_NULL: - case StorageType::STORAGE_TYPE_BOOL: case StorageType::STORAGE_TYPE_INTEGER: case StorageType::STORAGE_TYPE_REAL: default: @@ -375,9 +352,6 @@ std::string DataValue::ToString() const case StorageType::STORAGE_TYPE_NULL: res = "null"; break; - case StorageType::STORAGE_TYPE_BOOL: - res = std::to_string(value_.bValue); - break; case StorageType::STORAGE_TYPE_INTEGER: res = std::to_string(value_.iValue); break; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp index 657d5a8b4..09799d112 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp @@ -101,7 +101,6 @@ uint32_t DataTransformer::CalDataValueLength(const DataValue &dataValue) { static std::map lengthMap = { { StorageType::STORAGE_TYPE_NULL, Parcel::GetUInt32Len()}, - { StorageType::STORAGE_TYPE_BOOL, Parcel::GetBoolLen()}, { StorageType::STORAGE_TYPE_INTEGER, Parcel::GetInt64Len()}, { StorageType::STORAGE_TYPE_REAL, Parcel::GetDoubleLen()} }; @@ -160,24 +159,6 @@ int DeSerializeNullValue(DataValue &dataValue, Parcel &parcel) return E_OK; } -int SerializeBoolValue(const DataValue &dataValue, Parcel &parcel) -{ - bool val = false; - (void)dataValue.GetBool(val); - return parcel.WriteBool(val); -} - -int DeSerializeBoolValue(DataValue &dataValue, Parcel &parcel) -{ - bool val = false; - (void)parcel.ReadBool(val); - if (parcel.IsError()) { - return -E_PARSE_FAIL; - } - dataValue = val; - return E_OK; -} - int SerializeIntValue(const DataValue &dataValue, Parcel &parcel) { int64_t val = 0; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_object.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_object.cpp index ba7e8cff2..bc77775f8 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_object.cpp @@ -415,7 +415,7 @@ int QueryObject::CheckInKeys() const LOGE("Inkeys cannot be empty."); return -E_INVALID_ARGS; } - if (keys_.size() > DBConstant::MAX_BATCH_SIZE) { + if (keys_.size() > DBConstant::MAX_INKEYS_SIZE) { LOGE("Inkeys cannot be over 128."); return -E_MAX_LIMITS; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp index 603f71362..29a7d6412 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp @@ -246,7 +246,7 @@ int DeSerializeVersion1Data(uint32_t version, Parcel &parcel, std::string &table } uint32_t keysSize = 0; (void)parcel.ReadUInt32(keysSize); - if (keysSize > DBConstant::MAX_BATCH_SIZE) { + if (keysSize > DBConstant::MAX_INKEYS_SIZE) { return -E_PARSE_FAIL; } for (uint32_t i = 0; i < keysSize; ++i) { diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp index ad66f016a..fee33d432 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp @@ -389,13 +389,6 @@ static int BindDataValueByType(sqlite3_stmt *statement, const std::optional g_checkFuncList = { - &DataValueDefaultNullCheck, &DataValueBoolCheck, &DataValueInt64Check, + &DataValueDefaultNullCheck, &DataValueInt64Check, &DataValueDoubleCheck, &DataValueStringCheck, &DataValueBlobCheck }; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_data_transformer_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_data_transformer_test.cpp index 5c9b4d5d8..6955116d6 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_data_transformer_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_data_transformer_test.cpp @@ -28,11 +28,6 @@ void SetNull(DataValue &dataValue) dataValue.ResetValue(); } -void SetBool(DataValue &dataValue) -{ - dataValue = false; -} - void SetInt64(DataValue &dataValue) { dataValue = INT64_MAX; @@ -57,7 +52,6 @@ void SetBlob(DataValue &dataValue) std::map g_typeMapFunction = { {StorageType::STORAGE_TYPE_NULL, &SetNull}, - {StorageType::STORAGE_TYPE_BOOL, &SetBool}, {StorageType::STORAGE_TYPE_INTEGER, &SetInt64}, {StorageType::STORAGE_TYPE_REAL, &SetDouble}, {StorageType::STORAGE_TYPE_TEXT, &SetText}, diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index 69693babb..7d77ee16f 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp @@ -91,7 +91,6 @@ namespace { { static std::map typeMap = { {StorageType::STORAGE_TYPE_INTEGER, "INT"}, - {StorageType::STORAGE_TYPE_BOOL, "BOOL"}, {StorageType::STORAGE_TYPE_REAL, "DOUBLE"}, {StorageType::STORAGE_TYPE_TEXT, "TEXT"}, {StorageType::STORAGE_TYPE_BLOB, "BLOB"} @@ -166,13 +165,6 @@ namespace { void BindValue(const DataValue &item, sqlite3_stmt *stmt, int col) { switch (item.GetType()) { - case StorageType::STORAGE_TYPE_BOOL: { - bool boolData = false; - (void)item.GetBool(boolData); - EXPECT_EQ(sqlite3_bind_int(stmt, col, boolData), SQLITE_OK); - break; - } - case StorageType::STORAGE_TYPE_INTEGER: { int64_t intData = 0; (void)item.GetInt64(intData); @@ -238,11 +230,6 @@ namespace { dataValue.ResetValue(); } - void SetBool(DataValue &dataValue) - { - dataValue = false; - } - void SetInt64(DataValue &dataValue) { dataValue = INT64_MAX; @@ -269,7 +256,6 @@ namespace { { static std::map typeMapFunction = { {StorageType::STORAGE_TYPE_NULL, &SetNull}, - {StorageType::STORAGE_TYPE_BOOL, &SetBool}, {StorageType::STORAGE_TYPE_INTEGER, &SetInt64}, {StorageType::STORAGE_TYPE_REAL, &SetDouble}, {StorageType::STORAGE_TYPE_TEXT, &SetText}, @@ -1002,7 +988,7 @@ HWTEST_F(DistributedDBRelationalVerP2PSyncTest, AbilitySync003, TestSize.Level1) * @tc.steps: step2. create table and insert data */ PrepareEnvironment(dataMap, schema, schema, {g_deviceB}); - + /** * @tc.steps: step3. change local table to (BOOL, INTEGER, REAL, TEXT, BLOB) * @tc.expected: sync fail -- Gitee From 9810581d5cdfcc047cbc45680fbaa0c5f39cc4d0 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Fri, 25 Feb 2022 10:15:21 +0800 Subject: [PATCH 12/37] sync callback should be called while userChange Signed-off-by: zwtmichael --- .../interfaces/include/store_types.h | 1 + .../storage/src/sync_able_engine.cpp | 2 +- .../storage/src/sync_able_kvdb.cpp | 2 +- .../distributeddb/syncer/include/isyncer.h | 2 +- .../syncer/include/syncer_proxy.h | 2 +- .../syncer/src/generic_syncer.cpp | 45 ++++- .../distributeddb/syncer/src/generic_syncer.h | 7 +- .../syncer/src/sync_operation.cpp | 21 ++ .../distributeddb/syncer/src/sync_operation.h | 6 +- .../distributeddb/syncer/src/syncer_proxy.cpp | 4 +- ...stributeddb_single_ver_multi_user_test.cpp | 183 ++++++++++++++++++ 11 files changed, 258 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/interfaces/include/store_types.h b/services/distributeddataservice/libs/distributeddb/interfaces/include/store_types.h index 163315177..2fb719492 100755 --- a/services/distributeddataservice/libs/distributeddb/interfaces/include/store_types.h +++ b/services/distributeddataservice/libs/distributeddb/interfaces/include/store_types.h @@ -61,6 +61,7 @@ enum DBStatus { DISTRIBUTED_SCHEMA_CHANGED, // the schema was changed MODE_MISMATCH, NOT_ACTIVE, + USER_CHANGED, }; struct KvStoreConfig { diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp index 654425829..a435982f6 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp @@ -114,7 +114,7 @@ void SyncAbleEngine::StartSyncer() void SyncAbleEngine::StopSyncer() { if (started_) { - syncer_.Close(); + syncer_.Close(false); started_ = false; } } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp index 2daf61d32..f4d16dd92 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -197,7 +197,7 @@ void SyncAbleKvDB::StopSyncer(bool isClosed) void SyncAbleKvDB::StopSyncerWithNoLock(bool isClosed) { ReSetSyncModuleActive(); - syncer_.Close(); + syncer_.Close(isClosed); if (started_) { started_ = false; } diff --git a/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h b/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h index ea0f7d45c..7cd036b24 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h @@ -48,7 +48,7 @@ public: } // Close - virtual int Close() = 0; + virtual int Close(bool isClosed) = 0; // Sync function. // param devices: The device id list. diff --git a/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h b/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h index 64ebfd05a..4e1ece88e 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h @@ -33,7 +33,7 @@ public: int Initialize(ISyncInterface *syncInterface, bool isNeedActive) override; // Close the syncer - int Close() override; + int Close(bool isClosed) override; // Sync function. // param devices: The device id list. diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp index 56c577a9c..21dc6dcbf 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp @@ -122,7 +122,7 @@ int GenericSyncer::Initialize(ISyncInterface *syncInterface, bool isNeedActive) return E_OK; } -int GenericSyncer::Close() +int GenericSyncer::Close(bool isClosed) { { std::lock_guard lock(syncerLock_); @@ -139,7 +139,7 @@ int GenericSyncer::Close() } closing_ = true; } - ClearSyncOperations(); + ClearSyncOperations(isClosed); if (syncEngine_ != nullptr) { syncEngine_->Close(); LOGD("[Syncer] Close SyncEngine!"); @@ -425,14 +425,43 @@ bool GenericSyncer::IsValidDevices(const std::vector &devices) cons return true; } -void GenericSyncer::ClearSyncOperations() +void GenericSyncer::ClearSyncOperations(bool isClosed) { - std::lock_guard lock(operationMapLock_); - for (auto &iter : syncOperationMap_) { - RefObject::KillAndDecObjRef(iter.second); - iter.second = nullptr; + std::vector syncOperation; + { + std::lock_guard lock(operationMapLock_); + for (auto &item : syncOperationMap_) { + bool isBlockSync = item.second->IsBlockSync(); + if (isBlockSync || !isClosed) { + int status = (!isClosed) ? SyncOperation::OP_USER_CHANGED : SyncOperation::OP_FAILED; + item.second->SetUnfinishedDevStatus(status); + RefObject::IncObjRef(item.second); + syncOperation.push_back(item.second); + } + } + } + for (auto &operation : syncOperation) { + // block sync operation or userChange will trigger remove sync operation + // caller won't blocked for block sync + // caller won't blocked for userChange operation no mater it is block or non-block sync + TriggerSyncFinished(operation); + RefObject::DecObjRef(operation); + } + { + std::lock_guard lock(operationMapLock_); + for (auto &iter : syncOperationMap_) { + RefObject::KillAndDecObjRef(iter.second); + iter.second = nullptr; + } + syncOperationMap_.clear(); + } +} + +void GenericSyncer::TriggerSyncFinished(SyncOperation *operation) +{ + if(operation != nullptr && operation->CheckIsAllFinished()) { + operation->Finished(); } - syncOperationMap_.clear(); } void GenericSyncer::OnSyncFinished(int syncId) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h index 2d0808406..2f282e298 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h @@ -38,7 +38,7 @@ public: int Initialize(ISyncInterface *syncInterface, bool isNeedActive) override; // Close - int Close() override; + int Close(bool isClosed) override; // Sync function. // param devices: The device id list. @@ -139,7 +139,10 @@ protected: bool IsValidDevices(const std::vector &devices) const; // Used Clear all SyncOperations. - void ClearSyncOperations(); + // isClosed is false while userChanged + void ClearSyncOperations(bool isClosed); + + void TriggerSyncFinished(SyncOperation *operation); // Callback when the special sync finished. void OnSyncFinished(int syncId); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.cpp index 8bb39a2e9..cc688a17e 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.cpp @@ -99,6 +99,26 @@ void SyncOperation::SetStatus(const std::string &deviceId, int status) } } +void SyncOperation::SetUnfinishedDevStatus(int status) +{ + LOGD("[SyncOperation] SetUnfinishedDevStatus status %d", status); + AutoLock lockGuard(this); + if (IsKilled()) { + LOGE("[SyncOperation] SetUnfinishedDevStatus failed, the SyncOperation has been killed!"); + return; + } + if (isFinished_) { + LOGI("[SyncOperation] SetUnfinishedDevStatus already finished"); + return; + } + for (auto &item : statuses_) { + if (item.second >= OP_FINISHED_ALL) { + continue; + } + item.second = status; + } +} + int SyncOperation::GetStatus(const std::string &deviceId) const { AutoLock lockGuard(this); @@ -288,6 +308,7 @@ const std::map &SyncOperation::DBStatusTransMap() { static_cast(OP_MAX_LIMITS), OVER_MAX_LIMITS }, { static_cast(OP_SCHEMA_CHANGED), DISTRIBUTED_SCHEMA_CHANGED }, { static_cast(OP_INVALID_ARGS), INVALID_ARGS }, + { static_cast(OP_USER_CHANGED), USER_CHANGED}, }; return transMap; } diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.h b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.h index 46510a772..5b5152e5f 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_operation.h @@ -53,7 +53,8 @@ public: OP_INTERCEPT_DATA_FAIL, OP_MAX_LIMITS, OP_SCHEMA_CHANGED, - OP_INVALID_ARGS + OP_INVALID_ARGS, + OP_USER_CHANGED }; using UserCallback = std::function)>; @@ -77,6 +78,9 @@ public: // Set the sync status, running or finished void SetStatus(const std::string &deviceId, int status); + // Set the unfinished devices sync status, running or finished + void SetUnfinishedDevStatus(int status); + // Set the identifier, used in SyncOperation::Finished void SetIdentifier(const std::vector &identifier); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp index fbe0cb5be..012c57c66 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp @@ -46,12 +46,12 @@ int SyncerProxy::Initialize(ISyncInterface *syncInterface, bool isNeedActive) return syncer_->Initialize(syncInterface, isNeedActive); } -int SyncerProxy::Close() +int SyncerProxy::Close(bool isClosed) { if (syncer_ == nullptr) { return -E_NOT_INIT; } - return syncer_->Close(); + return syncer_->Close(isClosed); } int SyncerProxy::Sync(const std::vector &devices, int mode, diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_multi_user_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_multi_user_test.cpp index 4e8f7c454..a0276873a 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_multi_user_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_multi_user_test.cpp @@ -37,6 +37,7 @@ namespace { const std::string DEVICE_B = "deviceB"; const std::string DEVICE_C = "deviceC"; const int WAIT_TIME = 1000; // 1000ms + const int WAIT_3_SECONDS = 3000; KvStoreDelegateManager g_mgr1(APP_ID, USER_ID_1); KvStoreDelegateManager g_mgr2(APP_ID, USER_ID_2); @@ -624,4 +625,186 @@ HWTEST_F(DistributedDBSingleVerMultiUserTest, MultiUser007, TestSize.Level0) std::this_thread::sleep_for(std::chrono::milliseconds(1)); EXPECT_TRUE(g_kvDelegatePtr1->Rekey(passwd) == OK); CloseStore(); +} + +/** + * @tc.name: MultiUser008 + * @tc.desc: test NotifyUserChanged and block sync concurrently + * @tc.type: FUNC + * @tc.require: AR000E8S2T + * @tc.author: zhuwentao + */ +HWTEST_F(DistributedDBSingleVerMultiUserTest, MultiUser008, TestSize.Level0) +{ + /** + * @tc.steps: step1. set SyncActivationCheckCallback and only userId1 can active + */ + g_mgr1.SetSyncActivationCheckCallback(g_syncActivationCheckCallback2); + /** + * @tc.steps: step2. openstore1 in dual tuple sync mode and openstore2 in normal sync mode + * @tc.expected: step2. only user2 sync mode is active + */ + + OpenStore1(true); + OpenStore2(true); + /** + * @tc.steps: step3. set SyncActivationCheckCallback and only userId2 can active + */ + g_mgr1.SetSyncActivationCheckCallback(g_syncActivationCheckCallback1); + + /** + * @tc.steps: step4. call NotifyUserChanged and block sync db concurrently + * @tc.expected: step4. return OK + */ + CipherPassword passwd; + thread subThread([&]() { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + EXPECT_TRUE(KvStoreDelegateManager::NotifyUserChanged() == OK); + }); + subThread.detach(); + /** + * @tc.steps: step5. deviceA call sync and wait + * @tc.expected: step5. sync should return OK. + */ + std::map result; + std::vector devices; + devices.push_back(g_deviceB->GetDeviceId()); + DBStatus status = g_tool.SyncTest(g_kvDelegatePtr1, devices, SYNC_MODE_PUSH_ONLY, result, true); + ASSERT_TRUE(status == OK); + + /** + * @tc.expected: step6. onComplete should be called, and status is USER_CHANGED + */ + ASSERT_TRUE(result.size() == devices.size()); + for (const auto &pair : result) { + LOGD("dev %s, status %d", pair.first.c_str(), pair.second); + EXPECT_TRUE(pair.second == USER_CHANGED); + } + CloseStore(); +} + +/** + * @tc.name: MultiUser009 + * @tc.desc: test NotifyUserChanged and non-block sync concurrently + * @tc.type: FUNC + * @tc.require: AR000E8S2T + * @tc.author: zhuwentao + */ +HWTEST_F(DistributedDBSingleVerMultiUserTest, MultiUser009, TestSize.Level0) +{ + /** + * @tc.steps: step1. set SyncActivationCheckCallback and only userId1 can active + */ + g_mgr1.SetSyncActivationCheckCallback(g_syncActivationCheckCallback2); + /** + * @tc.steps: step2. openstore1 in dual tuple sync mode and openstore2 in normal sync mode + * @tc.expected: step2. only user2 sync mode is active + */ + + OpenStore1(true); + OpenStore2(true); + /** + * @tc.steps: step3. set SyncActivationCheckCallback and only userId2 can active + */ + g_mgr1.SetSyncActivationCheckCallback(g_syncActivationCheckCallback1); + + /** + * @tc.steps: step4. call NotifyUserChanged and block sync db concurrently + * @tc.expected: step4. return OK + */ + CipherPassword passwd; + thread subThread([&]() { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + EXPECT_TRUE(KvStoreDelegateManager::NotifyUserChanged() == OK); + }); + subThread.detach(); + /** + * @tc.steps: step5. deviceA call sync and wait + * @tc.expected: step5. sync should return OK. + */ + std::map result; + std::vector devices; + devices.push_back(g_deviceB->GetDeviceId()); + DBStatus status = g_tool.SyncTest(g_kvDelegatePtr1, devices, SYNC_MODE_PUSH_ONLY, result, false); + ASSERT_TRUE(status == OK); + + /** + * @tc.expected: step6. onComplete should be called, and status is USER_CHANGED + */ + ASSERT_TRUE(result.size() == devices.size()); + for (const auto &pair : result) { + LOGD("dev %s, status %d", pair.first.c_str(), pair.second); + EXPECT_TRUE(pair.second == USER_CHANGED); + } + CloseStore(); +} + +/** + * @tc.name: MultiUser010 + * @tc.desc: test NotifyUserChanged and non-block sync with multi devices concurrently + * @tc.type: FUNC + * @tc.require: AR000E8S2T + * @tc.author: zhuwentao + */ +HWTEST_F(DistributedDBSingleVerMultiUserTest, MultiUser010, TestSize.Level3) +{ + /** + * @tc.steps: step1. set SyncActivationCheckCallback and only userId1 can active + */ + g_mgr1.SetSyncActivationCheckCallback(g_syncActivationCheckCallback2); + /** + * @tc.steps: step2. openstore1 and openstore2 in dual tuple sync mode + * @tc.expected: step2. only userId1 sync mode is active + */ + + OpenStore1(true); + OpenStore2(true); + /** + * @tc.steps: step3. set SyncActivationCheckCallback and only userId2 can active + */ + g_mgr1.SetSyncActivationCheckCallback(g_syncActivationCheckCallback1); + /** + * @tc.steps: step4. deviceA put {k1, v1} + */ + Key key = {'1'}; + Value value = {'1'}; + ASSERT_TRUE(g_kvDelegatePtr1->Put(key, value) == OK); + + /** + * @tc.steps: step5. deviceB set sava data dely 5s + */ + g_deviceC->SetSaveDataDelayTime(WAIT_3_SECONDS); + /** + * @tc.steps: step6. call NotifyUserChanged and block sync db concurrently + * @tc.expected: step6. return OK + */ + CipherPassword passwd; + thread subThread([&]() { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + EXPECT_TRUE(KvStoreDelegateManager::NotifyUserChanged() == OK); + }); + subThread.detach(); + /** + * @tc.steps: step7. deviceA call sync and wait + * @tc.expected: step7. sync should return OK. + */ + std::map result; + std::vector devices = {g_deviceB->GetDeviceId(), g_deviceC->GetDeviceId()}; + DBStatus status = g_tool.SyncTest(g_kvDelegatePtr1, devices, SYNC_MODE_PUSH_ONLY, result, false); + ASSERT_TRUE(status == OK); + + /** + * @tc.expected: step8. onComplete should be called, and status is USER_CHANGED + */ + ASSERT_TRUE(result.size() == devices.size()); + for (const auto &pair : result) { + LOGD("dev %s, status %d", pair.first.c_str(), pair.second); + if (pair.first == g_deviceB->GetDeviceId()) { + EXPECT_TRUE(pair.second == OK); + } else { + EXPECT_TRUE(pair.second == USER_CHANGED); + } + } + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_3_SECONDS)); + CloseStore(); } \ No newline at end of file -- Gitee From 071f557504bfa68a411e6fcb177e1973897ee072 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Wed, 2 Mar 2022 14:44:37 +0800 Subject: [PATCH 13/37] should reset equalIdentifierMap_ when call SetequalIdentifier Signed-off-by: zwtmichael --- .../libs/distributeddb/syncer/src/sync_engine.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp index f86e19637..baff89461 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp @@ -771,6 +771,7 @@ void SyncEngine::SetEqualIdentifier() void SyncEngine::SetEqualIdentifierMap(const std::string &identifier, const std::vector &targets) { + equalIdentifierMap_.clear(); for (auto &device : targets) { equalIdentifierMap_[device] = identifier; } -- Gitee From c4baae1645ef0a6b745ad263a6bb2bf28d94f8c4 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Wed, 2 Mar 2022 18:02:20 +0800 Subject: [PATCH 14/37] relation sync keep the same with kv while block sync Signed-off-by: zwtmichael --- .../libs/distributeddb/storage/src/sync_able_engine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp index a435982f6..55d346353 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_engine.cpp @@ -114,7 +114,7 @@ void SyncAbleEngine::StartSyncer() void SyncAbleEngine::StopSyncer() { if (started_) { - syncer_.Close(false); + syncer_.Close(true); started_ = false; } } -- Gitee From 0d5ac8a5f80daf666955d72de848e2c36c949526 Mon Sep 17 00:00:00 2001 From: unknown <347801860@qq.com> Date: Tue, 1 Mar 2022 16:50:14 +0800 Subject: [PATCH 15/37] SetEqualIdentifier optimize Signed-off-by: unknown <347801860@qq.com> --- .../syncer/src/communicator_proxy.cpp | 33 +++++++++++-------- .../syncer/src/communicator_proxy.h | 6 ++-- .../distributeddb/syncer/src/sync_engine.cpp | 10 ++++-- .../distributeddb_communicator_proxy_test.cpp | 3 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp index 25cc48bb1..3eef85f24 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp @@ -30,7 +30,7 @@ CommunicatorProxy::~CommunicatorProxy() std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - RefObject::DecObjRef(devCommMap_[iter.first]); + RefObject::DecObjRef(devCommMap_[iter.first].second); } devCommMap_.clear(); } @@ -43,7 +43,7 @@ int CommunicatorProxy::RegOnMessageCallback(const OnMessageCallback &onMessage, std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - (void) devCommMap_[iter.first]->RegOnMessageCallback(onMessage, inOper); + (void) devCommMap_[iter.first].second->RegOnMessageCallback(onMessage, inOper); } return E_OK; } @@ -56,7 +56,7 @@ int CommunicatorProxy::RegOnConnectCallback(const OnConnectCallback &onConnect, std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - (void) devCommMap_[iter.first]->RegOnConnectCallback(onConnect, inOper); + (void) devCommMap_[iter.first].second->RegOnConnectCallback(onConnect, inOper); } return E_OK; @@ -70,7 +70,7 @@ int CommunicatorProxy::RegOnSendableCallback(const std::function &on std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - (void) devCommMap_[iter.first]->RegOnSendableCallback(onSendable, inOper); + (void) devCommMap_[iter.first].second->RegOnSendableCallback(onSendable, inOper); } return E_OK; @@ -87,8 +87,8 @@ void CommunicatorProxy::Activate() { std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - tempMap[iter.first] = devCommMap_[iter.first]; - RefObject::IncObjRef(devCommMap_[iter.first]); + tempMap[iter.first] = devCommMap_[iter.first].second; + RefObject::IncObjRef(devCommMap_[iter.first].second); } } @@ -112,7 +112,7 @@ uint32_t CommunicatorProxy::GetCommunicatorMtuSize(const std::string &target) co { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(target) != 0) { - targetCommunicator = devCommMap_.at(target); + targetCommunicator = devCommMap_.at(target).second; RefObject::IncObjRef(targetCommunicator); } } @@ -143,7 +143,7 @@ uint32_t CommunicatorProxy::GetTimeout(const std::string &target) const { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(target) != 0) { - targetCommunicator = devCommMap_.at(target); + targetCommunicator = devCommMap_.at(target).second; RefObject::IncObjRef(targetCommunicator); } } @@ -176,7 +176,7 @@ int CommunicatorProxy::GetRemoteCommunicatorVersion(const std::string &target, u { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(target) != 0) { - targetCommunicator = devCommMap_.at(target); + targetCommunicator = devCommMap_.at(target).second; RefObject::IncObjRef(targetCommunicator); } } @@ -205,7 +205,7 @@ int CommunicatorProxy::SendMessage(const std::string &dstTarget, const Message * { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(dstTarget) != 0) { - targetCommunicator = devCommMap_[dstTarget]; + targetCommunicator = devCommMap_[dstTarget].second; RefObject::IncObjRef(targetCommunicator); } } @@ -229,17 +229,22 @@ void CommunicatorProxy::SetMainCommunicator(ICommunicator *communicator) RefObject::IncObjRef(mainComm_); } -void CommunicatorProxy::SetEqualCommunicator(ICommunicator *communicator, const std::vector &targets) +void CommunicatorProxy::SetEqualCommunicator(ICommunicator *communicator, const std::string &identifier, + const std::vector &targets) { std::lock_guard lock(devCommMapLock_); // Clear offline target for (auto dev = devCommMap_.begin(); dev != devCommMap_.end();) { + if (identifier != dev->second.first) { + dev++; + continue; + } auto iter = std::find_if(targets.begin(), targets.end(), [&dev](const std::string &target) { return target == dev->first; }); if (iter == targets.end()) { - RefObject::DecObjRef(devCommMap_[dev->first]); + RefObject::DecObjRef(devCommMap_[dev->first].second); dev = devCommMap_.erase(dev); continue; } @@ -250,10 +255,10 @@ void CommunicatorProxy::SetEqualCommunicator(ICommunicator *communicator, const for (const auto &target : targets) { if (devCommMap_.count(target) != 0) { // change the identifier and dev relation - RefObject::DecObjRef(devCommMap_[target]); + RefObject::DecObjRef(devCommMap_[target].second); } RefObject::IncObjRef(communicator); - devCommMap_[target] = communicator; + devCommMap_[target] = {identifier, communicator}; } } } // namespace DistributedDB \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h index a106a7e29..055bffcb5 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h @@ -50,12 +50,14 @@ public: void SetMainCommunicator(ICommunicator *communicator); // Set an equal communicator for this database, After this called, send msg to the target will use this communicator - void SetEqualCommunicator(ICommunicator *communicator, const std::vector &targets); + void SetEqualCommunicator(ICommunicator *communicator, const std::string &identifier, + const std::vector &targets); private: ICommunicator *mainComm_; mutable std::mutex devCommMapLock_; - std::map devCommMap_; + // key: device value: + std::map> devCommMap_; }; } // namespace DistributedDB #endif // COMMUNICATOR_PROXY_H diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp index baff89461..722a111dc 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp @@ -749,7 +749,7 @@ int SyncEngine::SetEqualIdentifier(const std::string &identifier, const std::vec LOGI("[SyncEngine] set equal identifier=%s, original=%s, targetDevices=%s", DBCommon::TransferStringToHex(identifier).c_str(), label_.c_str(), targetDevices.substr(0, targetDevices.size() - 1).c_str()); - communicatorProxy_->SetEqualCommunicator(communicator, targets); + communicatorProxy_->SetEqualCommunicator(communicator, identifier, targets); communicator->Activate(); return E_OK; } @@ -771,7 +771,13 @@ void SyncEngine::SetEqualIdentifier() void SyncEngine::SetEqualIdentifierMap(const std::string &identifier, const std::vector &targets) { - equalIdentifierMap_.clear(); + for (auto iter = equalIdentifierMap_.begin(); iter != equalIdentifierMap_.end();) { + if (identifier == iter->second) { + iter = equalIdentifierMap_.erase(iter); + continue; + } + iter++; + } for (auto &device : targets) { equalIdentifierMap_[device] = identifier; } diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp index b3590ba98..8f0df0575 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp @@ -104,12 +104,13 @@ void DistributedDBCommunicatorProxyTest::SetUp(void) */ KvStoreNbDelegate::Option option; g_mgr.GetKvStore(STORE_ID, option, g_kvDelegateCallback); + std::string identifier = g_mgr.GetKvStoreIdentifier(USER_ID, APP_ID, STORE_ID); ASSERT_TRUE(g_kvDelegateStatus == OK); ASSERT_TRUE(g_kvDelegatePtr != nullptr); commProxy_ = new (std::nothrow) CommunicatorProxy(); ASSERT_TRUE(commProxy_ != nullptr); commProxy_->SetMainCommunicator(&mainComm_); - commProxy_->SetEqualCommunicator(&extComm_, { DEVICE_C }); + commProxy_->SetEqualCommunicator(&extComm_, identifier, { DEVICE_C }); } void DistributedDBCommunicatorProxyTest::TearDown(void) -- Gitee From fbb33ce388a89501bd9a24209b825cf70afdf393 Mon Sep 17 00:00:00 2001 From: lidwchn Date: Wed, 2 Mar 2022 17:12:55 +0800 Subject: [PATCH 16/37] Fix reviewbot. Signed-off-by: lidwchn --- .../common/src/platform_specific.cpp | 1 + .../storage/src/sqlite/query_sync_object.cpp | 15 +++++---------- .../sqlite_single_relational_storage_engine.cpp | 6 ++---- ...ite_single_ver_relational_storage_executor.cpp | 7 ++----- .../storage/src/sqlite/sqlite_utils.cpp | 4 ++-- .../distributeddb/storage/src/sync_able_kvdb.cpp | 2 +- 6 files changed, 13 insertions(+), 22 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp b/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp index f7866cbe2..608c4525b 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp @@ -139,6 +139,7 @@ int GetRealPath(const std::string &inOriPath, std::string &outRealPath) return -E_OUT_OF_MEMORY; } if (memset_s(realPath, MAX_PATH_LENGTH + 1, 0, MAX_PATH_LENGTH + 1) != EOK) { + delete []realPath; return -E_SECUREC_ERROR; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp index 29a7d6412..cd4492dc3 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/query_sync_object.cpp @@ -140,8 +140,7 @@ std::string QuerySyncObject::GetIdentify() const len += isTableNameSpecified_ ? Parcel::GetStringLen(tableName_) : 0; for (const auto &key : keys_) { len += Parcel::GetVectorCharLen(key); - } - // QUERY_SYNC_OBJECT_VERSION_1 end. + } // QUERY_SYNC_OBJECT_VERSION_1 end. std::vector buff(len, 0); // It will affect the hash result, the default value cannot be modified Parcel parcel(buff.data(), len); @@ -168,8 +167,7 @@ std::string QuerySyncObject::GetIdentify() const } for (const auto &key : keys_) { (void)parcel.WriteVectorChar(key); - } - // QUERY_SYNC_OBJECT_VERSION_1 end. + } // QUERY_SYNC_OBJECT_VERSION_1 end. if (parcel.IsError()) { return std::string(); @@ -225,8 +223,7 @@ int QuerySyncObject::SerializeData(Parcel &parcel, uint32_t softWareVersion) for (const auto &key : keys_) { (void)parcel.WriteVectorChar(key); } - } - // QUERY_SYNC_OBJECT_VERSION_1 end. + } // QUERY_SYNC_OBJECT_VERSION_1 end. if (parcel.IsError()) { // parcel almost success return -E_INVALID_ARGS; @@ -298,8 +295,7 @@ int QuerySyncObject::DeSerializeData(Parcel &parcel, QuerySyncObject &queryObj) int errCode = DeSerializeVersion1Data(context.version, parcel, tableName, keys); if (errCode != E_OK) { return errCode; - } - // QUERY_SYNC_OBJECT_VERSION_1 end. + } // QUERY_SYNC_OBJECT_VERSION_1 end. if (parcel.IsError()) { // almost success return -E_INVALID_ARGS; @@ -342,8 +338,7 @@ uint32_t QuerySyncObject::CalculateLen() const len += Parcel::GetUInt32Len(); // size of keys_ for (const auto &key : keys_) { len += Parcel::GetVectorCharLen(key); - } - // QUERY_SYNC_OBJECT_VERSION_1 end. + } // QUERY_SYNC_OBJECT_VERSION_1 end. len = Parcel::GetEightByteAlign(len); if (len > INT32_MAX) { diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp index 0a3de8d6c..3040d91e9 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp @@ -17,6 +17,7 @@ #include "db_common.h" #include "db_errno.h" +#include "res_finalizer.h" #include "sqlite_single_ver_relational_storage_executor.h" @@ -156,10 +157,10 @@ int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::strin if (handle == nullptr) { return errCode; } + ResFinalizer finalizer([&handle, this] { this->ReleaseExecutor(handle); }); errCode = handle->StartTransaction(TransactType::IMMEDIATE); if (errCode != E_OK) { - ReleaseExecutor(handle); return errCode; } @@ -168,7 +169,6 @@ int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::strin if (errCode != E_OK) { LOGE("create distributed table failed. %d", errCode); (void)handle->Rollback(); - ReleaseExecutor(handle); return errCode; } @@ -177,7 +177,6 @@ int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::strin if (errCode != E_OK) { LOGE("Save schema to meta table for create distributed table failed. %d", errCode); (void)handle->Rollback(); - ReleaseExecutor(handle); return errCode; } @@ -186,7 +185,6 @@ int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::strin schema_ = tmpSchema; schemaChanged = true; } - ReleaseExecutor(handle); return errCode; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp index fee33d432..4ac87ec80 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp @@ -662,11 +662,11 @@ int SQLiteSingleVerRelationalStorageExecutor::PrepareForSavingData(const QueryOb int SQLiteSingleVerRelationalStorageExecutor::SaveSyncLog(sqlite3_stmt *statement, sqlite3_stmt *queryStmt, const DataItem &dataItem, TimeStamp &maxTimestamp, int64_t rowid) { - int errCode = SQLiteUtils::BindBlobToStatement(queryStmt, 1, dataItem.hashKey); + int errCode = SQLiteUtils::BindBlobToStatement(queryStmt, 1, dataItem.hashKey); // 1 means hashkey index. if (errCode != E_OK) { return errCode; } - errCode = SQLiteUtils::BindTextToStatement(queryStmt, 2, dataItem.dev); + errCode = SQLiteUtils::BindTextToStatement(queryStmt, 2, dataItem.dev); // 2 means device index. if (errCode != E_OK) { return errCode; } @@ -1092,9 +1092,6 @@ int SQLiteSingleVerRelationalStorageExecutor::GetSyncDataByQuery(std::vector(password.GetData()), - password.GetSize(), SQLITE_TRANSIENT); // 2nd para is password. + errCode = sqlite3_bind_blob(statement, 2, static_cast(password.GetData()), // 2 means password index. + password.GetSize(), SQLITE_TRANSIENT); if (errCode != SQLITE_OK) { LOGE("Bind the attached key failed:%d", errCode); errCode = SQLiteUtils::MapSQLiteErrno(errCode); diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp index f4d16dd92..fe4592a2e 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -180,7 +180,7 @@ void SyncAbleKvDB::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActi userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( std::bind(&SyncAbleKvDB::ChangeUserListerner, this), UserChangeMonitor::USER_ACTIVE_TO_NON_ACTIVE_EVENT); } else if (isSyncDualTupleMode && (userChangeListerner_ == nullptr)) { - EventType event = started_? + EventType event = started_ ? UserChangeMonitor::USER_ACTIVE_EVENT : UserChangeMonitor::USER_NON_ACTIVE_EVENT; userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( std::bind(&SyncAbleKvDB::UserChangeHandle, this), event); -- Gitee From f81f4f9262554f866697682bc043be81da16e52b Mon Sep 17 00:00:00 2001 From: lidwchn Date: Thu, 3 Mar 2022 14:15:04 +0800 Subject: [PATCH 17/37] Fix compile error. Signed-off-by: lidwchn --- .../libs/distributeddb/storage/src/data_transformer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp index 09799d112..1804808b4 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp @@ -262,7 +262,7 @@ int DeSerializeTextValue(DataValue &dataValue, Parcel &parcel) int SerializeDataValue(const DataValue &dataValue, Parcel &parcel) { static const std::function funcs[] = { - SerializeNullValue, SerializeBoolValue, SerializeIntValue, + SerializeNullValue, SerializeIntValue, SerializeDoubleValue, SerializeTextValue, SerializeBlobValue, }; StorageType type = dataValue.GetType(); @@ -277,7 +277,7 @@ int SerializeDataValue(const DataValue &dataValue, Parcel &parcel) int DeserializeDataValue(DataValue &dataValue, Parcel &parcel) { static const std::function funcs[] = { - DeSerializeNullValue, DeSerializeBoolValue, DeSerializeIntValue, + DeSerializeNullValue, DeSerializeIntValue, DeSerializeDoubleValue, DeSerializeTextValue, DeSerializeBlobValue, }; uint32_t type = 0; -- Gitee From 7fb62ab28ecc7a8ce525413773bea7cbdd4d6410 Mon Sep 17 00:00:00 2001 From: zqq Date: Tue, 1 Mar 2022 17:04:02 +0800 Subject: [PATCH 18/37] offline should not clear the ability sync res it will cause sync fail with schema_missmatch Signed-off-by: zqq --- .../distributeddb/syncer/src/isync_engine.h | 2 +- .../syncer/src/isync_task_context.h | 2 ++ ...ingle_ver_relational_sync_task_context.cpp | 5 +++ .../single_ver_relational_sync_task_context.h | 2 ++ .../src/single_ver_relational_syncer.cpp | 2 +- .../distributeddb/syncer/src/sync_engine.cpp | 6 ++-- .../distributeddb/syncer/src/sync_engine.h | 2 +- .../syncer/src/sync_task_context.cpp | 5 +++ .../syncer/src/sync_task_context.h | 3 ++ .../distributeddb_mock_sync_module_test.cpp | 36 ++++++++++++++++++- 10 files changed, 57 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/isync_engine.h b/services/distributeddataservice/libs/distributeddb/syncer/src/isync_engine.h index 3bc748449..007fb1315 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/isync_engine.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/isync_engine.h @@ -78,7 +78,7 @@ public: // Check if the Sync Engine is active, some times synchronization is not allowed virtual bool IsEngineActive() const = 0; - virtual void ResetAbilitySync() = 0; + virtual void SchemaChange() = 0; protected: virtual ~ISyncEngine() {}; diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/isync_task_context.h b/services/distributeddataservice/libs/distributeddb/syncer/src/isync_task_context.h index a2477a14d..7577cce3d 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/isync_task_context.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/isync_task_context.h @@ -172,6 +172,8 @@ public: virtual bool IsCurrentSyncTaskCanBeSkipped() const = 0; virtual void SetIsNeedResetAbilitySync(bool isNeedReset) = 0; + + virtual void SchemaChange() = 0; protected: virtual ~ISyncTaskContext() {}; }; diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp index 4403fea36..4303a21d1 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp @@ -70,6 +70,11 @@ void SingleVerRelationalSyncTaskContext::SetIsNeedResetAbilitySync(bool isNeedRe if (isNeedResetAbilitySync_) { SetIsSchemaSync(false); } +} + +void SingleVerRelationalSyncTaskContext::SchemaChange() +{ + SetIsNeedResetAbilitySync(true); std::lock_guard autoLock(syncStrategyMutex_); relationalSyncStrategy_ = {}; } diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.h b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.h index 3334ad90a..0146c42bf 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.h @@ -35,6 +35,8 @@ public: SyncStrategy GetSyncStrategy(QuerySyncObject &querySyncObject) const override; void SetIsNeedResetAbilitySync(bool isNeedReset) override; + + void SchemaChange() override; protected: ~SingleVerRelationalSyncTaskContext() override; void CopyTargetData(const ISyncTarget *target, const TaskParam &taskParam) override; diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_syncer.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_syncer.cpp index aa274c100..3b40c6627 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_syncer.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_syncer.cpp @@ -156,7 +156,7 @@ void SingleVerRelationalSyncer::LocalDataChanged(int notifyEvent) void SingleVerRelationalSyncer::SchemaChangeCallback() { if (syncEngine_ != nullptr) { - syncEngine_->ResetAbilitySync(); + syncEngine_->SchemaChange(); } } diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp index baff89461..a058e944a 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp @@ -1001,7 +1001,7 @@ bool SyncEngine::IsEngineActive() const return isActive_; } -void SyncEngine::ResetAbilitySync() +void SyncEngine::SchemaChange() { std::lock_guard lock(contextMapLock_); for (auto &enrty : syncTaskContextMap_) { @@ -1010,9 +1010,7 @@ void SyncEngine::ResetAbilitySync() continue; } // IncRef for SyncEngine to make sure context is valid, to avoid a big lock - RefObject::IncObjRef(context); - context->SetIsNeedResetAbilitySync(true); - RefObject::DecObjRef(context); + context->SchemaChange(); } } } // namespace DistributedDB diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.h b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.h index 280c7cc21..f942302f3 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.h @@ -107,7 +107,7 @@ public: bool IsEngineActive() const override; - void ResetAbilitySync() override; + void SchemaChange() override; protected: // Create a context diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.cpp index 123310d0c..d714dd952 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.cpp @@ -696,4 +696,9 @@ bool SyncTaskContext::IsCurrentSyncTaskCanBeSkipped() const void SyncTaskContext::ResetLastPushTaskStatus() { } + +void SyncTaskContext::SchemaChange() +{ + SetIsNeedResetAbilitySync(true); +} } // namespace DistributedDB diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.h b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.h index e7ed88459..a1df05992 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_task_context.h @@ -200,8 +200,11 @@ public: virtual void ResetLastPushTaskStatus(); bool GetIsNeedResetAbilitySync() const; + void SetIsNeedResetAbilitySync(bool isNeedReset) override; + void SchemaChange() override; + protected: const static int KILL_WAIT_SECONDS = INT32_MAX; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index f292d7878..8d29b05fb 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -23,6 +23,7 @@ #include "mock_single_ver_data_sync.h" #include "mock_single_ver_state_machine.h" #include "mock_sync_task_context.h" +#include "single_ver_relational_sync_task_context.h" #include "virtual_single_ver_sync_db_Interface.h" #ifdef DATA_SYNC_CHECK_003 #include "virtual_relational_ver_sync_db_interface.h" @@ -392,7 +393,7 @@ HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync002, TestSize.Level1) packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); message->SetCopiedObject(packet); /** - * @tc.steps: step1. set syncDBInterface busy for save data return -E_BUSY + * @tc.steps: step2. set syncDBInterface busy for save data return -E_BUSY */ syncDBInterface.SetBusy(true); SyncStrategy mockStrategy = {true, false, false}; @@ -400,4 +401,37 @@ HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync002, TestSize.Level1) EXPECT_EQ(abilitySync.AckRecv(message, &syncTaskContext), -E_BUSY); delete message; EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); +} + +/** + * @tc.name: AbilitySync002 + * @tc.desc: Test abilitySync when offline. + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync003, TestSize.Level1) +{ + /** + * @tc.steps: step1. set table TEST is permitSync + */ + SingleVerRelationalSyncTaskContext *context = new (std::nothrow) SingleVerRelationalSyncTaskContext(); + ASSERT_NE(context, nullptr); + RelationalSyncStrategy strategy; + const std::string tableName = "TEST"; + strategy.AddSyncStrategy(tableName, {true, true, true}); + context->SetRelationalSyncStrategy(strategy); + QuerySyncObject query; + query.SetTableName(tableName); + /** + * @tc.steps: step2. set table is need reset ability sync but it still permit sync + */ + context->SetIsNeedResetAbilitySync(true); + EXPECT_EQ(context->GetSyncStrategy(query).permitSync, true); + /** + * @tc.steps: step3. set table is schema change now it dont permit sync + */ + context->SchemaChange(); + EXPECT_EQ(context->GetSyncStrategy(query).permitSync, false); + RefObject::KillAndDecObjRef(context); } \ No newline at end of file -- Gitee From df6b8865415e426e1be3f942067b794d51431be9 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Thu, 3 Mar 2022 19:22:51 +0800 Subject: [PATCH 19/37] Fix code check Signed-off-by: lianhuix --- .../libs/distributeddb/syncer/src/generic_syncer.cpp | 2 +- .../communicator/distributeddb_communicator_deep_test.cpp | 2 +- .../common/storage/distributeddb_multi_ver_vacuum_test.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp index 21dc6dcbf..07e969805 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp @@ -459,7 +459,7 @@ void GenericSyncer::ClearSyncOperations(bool isClosed) void GenericSyncer::TriggerSyncFinished(SyncOperation *operation) { - if(operation != nullptr && operation->CheckIsAllFinished()) { + if (operation != nullptr && operation->CheckIsAllFinished()) { operation->Finished(); } } diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp index bcafac2d8..f95cee345 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp @@ -432,7 +432,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, Fragment002, TestSize.Level2) HWTEST_F(DistributedDBCommunicatorDeepTest, Fragment003, TestSize.Level3) { // Preset - std::atomic count{0}; + std::atomic count {0}; OnMessageCallback callback = [&count](const std::string &srcTarget, Message *inMsg) { delete inMsg; inMsg = nullptr; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp index b0d34f19c..219ae01c5 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp @@ -96,7 +96,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskNormalStatusSwitch001, TestS { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseA(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseA(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskA for databaseA @@ -493,7 +493,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskConcurrentStatusSwitch001, T { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseC(DbScale{1, 1, 1, 1}, 1000); // 1 For Scale, 1000 For TimeCost, 11s in Total + MultiVerVacuumExecutorStub databaseC(DbScale {1, 1, 1, 1}, 1000); // 1 For Scale, 1000 For TimeCost, 11s in Total /** * @tc.steps: step1. launch dbTaskC for databaseC, databaseC is timecost -- Gitee From dba1684ceda415b803406d59aeb765c64353efbf Mon Sep 17 00:00:00 2001 From: lianhuix Date: Fri, 4 Mar 2022 10:38:12 +0800 Subject: [PATCH 20/37] Fix memory leaks in UT Signed-off-by: lianhuix --- .../distributeddb_interfaces_relational_test.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp index 7f9d9be34..c0d4e798b 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp @@ -679,6 +679,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest0 ASSERT_NE(db, nullptr); EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); DBStatus status = OK; for (int i = 0; i < 1000; i++) { @@ -703,10 +704,12 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest0 ASSERT_NE(db, nullptr); EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); std::queue delegateQueue; - std::mutex queuelock; - default_random_engine e; + std::mutex queueLock; + std::random_device rd; + default_random_engine e(rd()); uniform_int_distribution u(0, 9); std::thread openStoreThread([&, this]() { @@ -717,7 +720,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest0 EXPECT_EQ(status, OK); ASSERT_NE(delegate, nullptr); { - std::lock_guard lock(queuelock); + std::lock_guard lock(queueLock); delegateQueue.push(delegate); } LOGD("++++< open store delegate: %d", i); @@ -728,7 +731,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest0 while (cnt < 1000) { RelationalStoreDelegate *delegate = nullptr; { - std::lock_guard lock(queuelock); + std::lock_guard lock(queueLock); if (delegateQueue.empty()) { std::this_thread::sleep_for(std::chrono::microseconds(100)); continue; -- Gitee From 5852636b3d873a74b28167156ff691e7ebd86c33 Mon Sep 17 00:00:00 2001 From: zqq Date: Fri, 4 Mar 2022 14:31:39 +0800 Subject: [PATCH 21/37] close db in test case Signed-off-by: zqq --- .../libs/distributeddb/syncer/src/time_helper.cpp | 2 +- .../syncer/distributeddb_relational_ver_p2p_sync_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/time_helper.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/time_helper.cpp index 963ee284c..a0374f7be 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/time_helper.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/time_helper.cpp @@ -72,7 +72,7 @@ int TimeHelper::Initialize(const ISyncInterface *inStorage, std::shared_ptr(maxItemTime - currentSysTime + MS_TO_100_NS); // 1ms + localTimeOffset = static_cast(maxItemTime - currentSysTime + MS_TO_100_NS); // 1ms int errCode = SaveLocalTimeOffset(localTimeOffset); if (errCode != E_OK) { LOGE("[TimeHelper] save local time offset faield,err=%d", errCode); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index 7d77ee16f..542de2511 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp @@ -1005,7 +1005,7 @@ HWTEST_F(DistributedDBRelationalVerP2PSyncTest, AbilitySync003, TestSize.Level1) ASSERT_NE(db, nullptr); std::string alterSql = "ALTER TABLE " + g_tableName + " ADD COLUMN NEW_COLUMN TEXT DEFAULT 'DEFAULT_TEXT'"; EXPECT_EQ(sqlite3_exec(db, alterSql.c_str(), nullptr, nullptr, nullptr), SQLITE_OK); - + EXPECT_EQ(sqlite3_close(db), SQLITE_OK); EXPECT_EQ(g_rdbDelegatePtr->CreateDistributedTable(g_tableName), OK); }); -- Gitee From 4115ea5247d0c4948218502cb4c802bcc8456d96 Mon Sep 17 00:00:00 2001 From: wbq_sky Date: Sat, 5 Mar 2022 11:17:02 +0800 Subject: [PATCH 22/37] fix the codecheck Signed-off-by: wbq_sky --- .../common/src/schema_object.cpp | 2 +- .../distributeddb_communicator_deep_test.cpp | 2 +- .../distributeddb_multi_ver_vacuum_test.cpp | 20 +++++++++---------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp index 25b538deb..082f428c4 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp @@ -746,7 +746,7 @@ int SchemaObject::ParseCheckSchemaSkipSize(const JsonObject& inJsonObject) } // The type of SCHEMA_SKIPSIZE field has been checked in CheckMetaFieldCountAndType to be an INTEGER FieldValue skipSizeValue; - int errCode = inJsonObject.GetFieldValueByFieldPath(FieldPath{SchemaConstant::KEYWORD_SCHEMA_SKIPSIZE}, + int errCode = inJsonObject.GetFieldValueByFieldPath(FieldPath {SchemaConstant::KEYWORD_SCHEMA_SKIPSIZE}, skipSizeValue); if (errCode != E_OK) { return -E_INTERNAL_ERROR; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp index f95cee345..74b71a6f1 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp @@ -512,7 +512,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, ReliableOnline001, TestSize.Level2) { // Preset ClearPreviousTestCaseInfluence(); - std::atomic count{0}; + std::atomic count {0}; OnConnectCallback callback = [&count](const std::string &target, bool isConnect) { if (isConnect) { count.fetch_add(1, std::memory_order_seq_cst); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp index 219ae01c5..4d14a4b24 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_multi_ver_vacuum_test.cpp @@ -163,7 +163,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskNormalStatusSwitch002, TestS { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseB(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseB(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskB for databaseB, then wait for some time @@ -230,7 +230,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskNormalStatusSwitch003, TestS { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseC(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseC(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskC for databaseC, then wait for some time @@ -296,7 +296,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskNormalStatusSwitch004, TestS { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseA(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseA(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskA for databaseA, then wait for some time @@ -354,7 +354,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskAbnormalStatusSwitch001, Tes { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseB(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseB(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskB for databaseB @@ -549,7 +549,7 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, SingleTaskWriteHandleOccupy001, TestSi { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseA(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseA(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskA for databaseA @@ -609,8 +609,8 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, MultipleTaskNormalStatusSwitch001, Tes { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseA(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total - MultiVerVacuumExecutorStub databaseB(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseA(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseB(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskA for databaseA and dbTaskB for databaseB @@ -680,9 +680,9 @@ HWTEST_F(DistributedDBMultiVerVacuumTest, MultipleTaskNormalStatusSwitch002, Tes { // Preset MultiVerVacuum vacuum; - MultiVerVacuumExecutorStub databaseA(DbScale{1, 1, 1, 1}, 30); // 1 For Scale, 30 For TimeCost, 330ms in Total - MultiVerVacuumExecutorStub databaseB(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total - MultiVerVacuumExecutorStub databaseC(DbScale{1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseA(DbScale {1, 1, 1, 1}, 30); // 1 For Scale, 30 For TimeCost, 330ms in Total + MultiVerVacuumExecutorStub databaseB(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total + MultiVerVacuumExecutorStub databaseC(DbScale {1, 1, 2, 2}, 100); // 1, 2 For Scale, 100 For TimeCost, 1.7s in Total /** * @tc.steps: step1. launch dbTaskA,B,C for databaseA,B,C and wait dbTaskA,B FINISH -- Gitee From 4ea2e1aa065eb292909823029dc52b1860a00568 Mon Sep 17 00:00:00 2001 From: unknown <347801860@qq.com> Date: Tue, 1 Mar 2022 16:50:14 +0800 Subject: [PATCH 23/37] SetEqualIdentifier optimize Signed-off-by: zwtmichael --- .../syncer/src/communicator_proxy.cpp | 33 +++++++++++-------- .../syncer/src/communicator_proxy.h | 6 ++-- .../distributeddb/syncer/src/sync_engine.cpp | 10 ++++-- .../distributeddb_communicator_proxy_test.cpp | 3 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp index 25cc48bb1..3eef85f24 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.cpp @@ -30,7 +30,7 @@ CommunicatorProxy::~CommunicatorProxy() std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - RefObject::DecObjRef(devCommMap_[iter.first]); + RefObject::DecObjRef(devCommMap_[iter.first].second); } devCommMap_.clear(); } @@ -43,7 +43,7 @@ int CommunicatorProxy::RegOnMessageCallback(const OnMessageCallback &onMessage, std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - (void) devCommMap_[iter.first]->RegOnMessageCallback(onMessage, inOper); + (void) devCommMap_[iter.first].second->RegOnMessageCallback(onMessage, inOper); } return E_OK; } @@ -56,7 +56,7 @@ int CommunicatorProxy::RegOnConnectCallback(const OnConnectCallback &onConnect, std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - (void) devCommMap_[iter.first]->RegOnConnectCallback(onConnect, inOper); + (void) devCommMap_[iter.first].second->RegOnConnectCallback(onConnect, inOper); } return E_OK; @@ -70,7 +70,7 @@ int CommunicatorProxy::RegOnSendableCallback(const std::function &on std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - (void) devCommMap_[iter.first]->RegOnSendableCallback(onSendable, inOper); + (void) devCommMap_[iter.first].second->RegOnSendableCallback(onSendable, inOper); } return E_OK; @@ -87,8 +87,8 @@ void CommunicatorProxy::Activate() { std::lock_guard lock(devCommMapLock_); for (const auto &iter : devCommMap_) { - tempMap[iter.first] = devCommMap_[iter.first]; - RefObject::IncObjRef(devCommMap_[iter.first]); + tempMap[iter.first] = devCommMap_[iter.first].second; + RefObject::IncObjRef(devCommMap_[iter.first].second); } } @@ -112,7 +112,7 @@ uint32_t CommunicatorProxy::GetCommunicatorMtuSize(const std::string &target) co { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(target) != 0) { - targetCommunicator = devCommMap_.at(target); + targetCommunicator = devCommMap_.at(target).second; RefObject::IncObjRef(targetCommunicator); } } @@ -143,7 +143,7 @@ uint32_t CommunicatorProxy::GetTimeout(const std::string &target) const { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(target) != 0) { - targetCommunicator = devCommMap_.at(target); + targetCommunicator = devCommMap_.at(target).second; RefObject::IncObjRef(targetCommunicator); } } @@ -176,7 +176,7 @@ int CommunicatorProxy::GetRemoteCommunicatorVersion(const std::string &target, u { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(target) != 0) { - targetCommunicator = devCommMap_.at(target); + targetCommunicator = devCommMap_.at(target).second; RefObject::IncObjRef(targetCommunicator); } } @@ -205,7 +205,7 @@ int CommunicatorProxy::SendMessage(const std::string &dstTarget, const Message * { std::lock_guard lock(devCommMapLock_); if (devCommMap_.count(dstTarget) != 0) { - targetCommunicator = devCommMap_[dstTarget]; + targetCommunicator = devCommMap_[dstTarget].second; RefObject::IncObjRef(targetCommunicator); } } @@ -229,17 +229,22 @@ void CommunicatorProxy::SetMainCommunicator(ICommunicator *communicator) RefObject::IncObjRef(mainComm_); } -void CommunicatorProxy::SetEqualCommunicator(ICommunicator *communicator, const std::vector &targets) +void CommunicatorProxy::SetEqualCommunicator(ICommunicator *communicator, const std::string &identifier, + const std::vector &targets) { std::lock_guard lock(devCommMapLock_); // Clear offline target for (auto dev = devCommMap_.begin(); dev != devCommMap_.end();) { + if (identifier != dev->second.first) { + dev++; + continue; + } auto iter = std::find_if(targets.begin(), targets.end(), [&dev](const std::string &target) { return target == dev->first; }); if (iter == targets.end()) { - RefObject::DecObjRef(devCommMap_[dev->first]); + RefObject::DecObjRef(devCommMap_[dev->first].second); dev = devCommMap_.erase(dev); continue; } @@ -250,10 +255,10 @@ void CommunicatorProxy::SetEqualCommunicator(ICommunicator *communicator, const for (const auto &target : targets) { if (devCommMap_.count(target) != 0) { // change the identifier and dev relation - RefObject::DecObjRef(devCommMap_[target]); + RefObject::DecObjRef(devCommMap_[target].second); } RefObject::IncObjRef(communicator); - devCommMap_[target] = communicator; + devCommMap_[target] = {identifier, communicator}; } } } // namespace DistributedDB \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h index a106a7e29..055bffcb5 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/communicator_proxy.h @@ -50,12 +50,14 @@ public: void SetMainCommunicator(ICommunicator *communicator); // Set an equal communicator for this database, After this called, send msg to the target will use this communicator - void SetEqualCommunicator(ICommunicator *communicator, const std::vector &targets); + void SetEqualCommunicator(ICommunicator *communicator, const std::string &identifier, + const std::vector &targets); private: ICommunicator *mainComm_; mutable std::mutex devCommMapLock_; - std::map devCommMap_; + // key: device value: + std::map> devCommMap_; }; } // namespace DistributedDB #endif // COMMUNICATOR_PROXY_H diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp index a058e944a..f2bbb1501 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_engine.cpp @@ -749,7 +749,7 @@ int SyncEngine::SetEqualIdentifier(const std::string &identifier, const std::vec LOGI("[SyncEngine] set equal identifier=%s, original=%s, targetDevices=%s", DBCommon::TransferStringToHex(identifier).c_str(), label_.c_str(), targetDevices.substr(0, targetDevices.size() - 1).c_str()); - communicatorProxy_->SetEqualCommunicator(communicator, targets); + communicatorProxy_->SetEqualCommunicator(communicator, identifier, targets); communicator->Activate(); return E_OK; } @@ -771,7 +771,13 @@ void SyncEngine::SetEqualIdentifier() void SyncEngine::SetEqualIdentifierMap(const std::string &identifier, const std::vector &targets) { - equalIdentifierMap_.clear(); + for (auto iter = equalIdentifierMap_.begin(); iter != equalIdentifierMap_.end();) { + if (identifier == iter->second) { + iter = equalIdentifierMap_.erase(iter); + continue; + } + iter++; + } for (auto &device : targets) { equalIdentifierMap_[device] = identifier; } diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp index b3590ba98..8f0df0575 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp @@ -104,12 +104,13 @@ void DistributedDBCommunicatorProxyTest::SetUp(void) */ KvStoreNbDelegate::Option option; g_mgr.GetKvStore(STORE_ID, option, g_kvDelegateCallback); + std::string identifier = g_mgr.GetKvStoreIdentifier(USER_ID, APP_ID, STORE_ID); ASSERT_TRUE(g_kvDelegateStatus == OK); ASSERT_TRUE(g_kvDelegatePtr != nullptr); commProxy_ = new (std::nothrow) CommunicatorProxy(); ASSERT_TRUE(commProxy_ != nullptr); commProxy_->SetMainCommunicator(&mainComm_); - commProxy_->SetEqualCommunicator(&extComm_, { DEVICE_C }); + commProxy_->SetEqualCommunicator(&extComm_, identifier, { DEVICE_C }); } void DistributedDBCommunicatorProxyTest::TearDown(void) -- Gitee From 4155e2ff13a125c7122aa938abdbb1cd3214b438 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Fri, 4 Mar 2022 17:31:05 +0800 Subject: [PATCH 24/37] modify error code Signed-off-by: zwtmichael --- .../interfaces/src/kv_store_errno.cpp | 2 +- .../distributeddb/storage/src/sync_able_kvdb.cpp | 14 ++++++++------ .../distributeddb/storage/src/sync_able_kvdb.h | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/interfaces/src/kv_store_errno.cpp b/services/distributeddataservice/libs/distributeddb/interfaces/src/kv_store_errno.cpp index a8aa97a48..77384a55e 100755 --- a/services/distributeddataservice/libs/distributeddb/interfaces/src/kv_store_errno.cpp +++ b/services/distributeddataservice/libs/distributeddb/interfaces/src/kv_store_errno.cpp @@ -56,7 +56,7 @@ namespace { { -E_DISTRIBUTED_SCHEMA_NOT_FOUND, DISTRIBUTED_SCHEMA_NOT_FOUND}, { -E_DISTRIBUTED_SCHEMA_CHANGED, DISTRIBUTED_SCHEMA_CHANGED}, { -E_MODE_MISMATCH, MODE_MISMATCH}, - { -E_NOT_INIT, NOT_ACTIVE}, + { -E_NO_NEED_ACTIVE, NOT_ACTIVE}, }; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp index fe4592a2e..1ea84ce14 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -81,9 +81,9 @@ void SyncAbleKvDB::Close() int SyncAbleKvDB::Sync(const ISyncer::SyncParma &parma) { if (!started_) { - StartSyncer(); + int errCode = StartSyncer(); if (!started_) { - return -E_NOT_INIT; + return errCode; } } return syncer_.Sync(parma); @@ -149,19 +149,20 @@ void SyncAbleKvDB::ReSetSyncModuleActive() } // Start syncer -void SyncAbleKvDB::StartSyncer(bool isCheckSyncActive, bool isNeedActive) +int SyncAbleKvDB::StartSyncer(bool isCheckSyncActive, bool isNeedActive) { std::unique_lock lock(syncerOperateLock_); - StartSyncerWithNoLock(isCheckSyncActive, isNeedActive); + int errCode = StartSyncerWithNoLock(isCheckSyncActive, isNeedActive); closed_ = false; + return errCode; } -void SyncAbleKvDB::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive) +int SyncAbleKvDB::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive) { IKvDBSyncInterface *syncInterface = GetSyncInterface(); if (syncInterface == nullptr) { LOGF("KvDB got null sync interface."); - return; + return -E_INVALID_ARGS; } if (!isCheckSyncActive) { SetSyncModuleActive(); @@ -185,6 +186,7 @@ void SyncAbleKvDB::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActi userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( std::bind(&SyncAbleKvDB::UserChangeHandle, this), event); } + return errCode; } // Stop syncer diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h index 14b0bcd75..cba41238f 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h @@ -92,9 +92,9 @@ protected: void ReSetSyncModuleActive(); // Start syncer - void StartSyncer(bool isCheckSyncActive = false, bool isNeedActive = true); + int StartSyncer(bool isCheckSyncActive = false, bool isNeedActive = true); - void StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive); + int StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive); // Stop syncer void StopSyncer(bool isClosed = false); -- Gitee From 0d105ddaf983ddff4de37601c000fa52b7b228aa Mon Sep 17 00:00:00 2001 From: zqq Date: Sat, 5 Mar 2022 11:16:47 +0800 Subject: [PATCH 25/37] add testcase for ability sync Signed-off-by: zqq --- ...ributeddb_relational_ver_p2p_sync_test.cpp | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp index 542de2511..e052edbc4 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_ver_p2p_sync_test.cpp @@ -390,7 +390,7 @@ namespace { } void PrepareBasicTable(const std::string &tableName, std::vector &fieldInfoList, - std::vector remoteDeviceVec) + std::vector &remoteDeviceVec, bool createDistributedTable = true) { sqlite3 *db = nullptr; EXPECT_EQ(GetDB(db), SQLITE_OK); @@ -406,8 +406,9 @@ namespace { for (auto &dev : remoteDeviceVec) { dev->SetTableInfo(tableInfo); } - - EXPECT_EQ(g_rdbDelegatePtr->CreateDistributedTable(tableName), OK); + if (createDistributedTable) { + EXPECT_EQ(g_rdbDelegatePtr->CreateDistributedTable(tableName), OK); + } sqlite3_close(db); } @@ -421,9 +422,10 @@ namespace { } void PrepareVirtualEnvironment(std::map &dataMap, const std::string &tableName, - std::vector &fieldInfoList, std::vector remoteDeviceVec) + std::vector &fieldInfoList, std::vector remoteDeviceVec, + bool createDistributedTable = true) { - PrepareBasicTable(tableName, fieldInfoList, remoteDeviceVec); + PrepareBasicTable(tableName, fieldInfoList, remoteDeviceVec, createDistributedTable); GenerateValue(dataMap, fieldInfoList); VirtualRowData virtualRowData; for (const auto &item : dataMap) { @@ -434,9 +436,9 @@ namespace { } void PrepareVirtualEnvironment(std::map &dataMap, - std::vector remoteDeviceVec) + std::vector remoteDeviceVec, bool createDistributedTable = true) { - PrepareVirtualEnvironment(dataMap, g_tableName, g_fieldInfoList, remoteDeviceVec); + PrepareVirtualEnvironment(dataMap, g_tableName, g_fieldInfoList, remoteDeviceVec, createDistributedTable); } void CheckData(std::map &targetMap, const std::string &tableName, @@ -1014,6 +1016,29 @@ HWTEST_F(DistributedDBRelationalVerP2PSyncTest, AbilitySync003, TestSize.Level1) g_communicatorAggregator->RegOnDispatch(nullptr); } +/** +* @tc.name: Ability Sync 004 +* @tc.desc: Test ability sync failed when one device hasn't distributed table. +* @tc.type: FUNC +* @tc.require: AR000GK58N +* @tc.author: zhangqiquan +*/ +HWTEST_F(DistributedDBRelationalVerP2PSyncTest, AbilitySync004, TestSize.Level1) +{ + std::map dataMap; + PrepareVirtualEnvironment(dataMap, {g_deviceB}, false); + + Query query = Query::Select(g_tableName); + int res = DB_ERROR; + auto callBack = [&res](std::map resMap) { + if (resMap.find("real_device") != resMap.end()) { + res = resMap["real_device"]; + } + }; + EXPECT_EQ(g_deviceB->GenericVirtualDevice::Sync(DistributedDB::SYNC_MODE_PULL_ONLY, query, callBack, true), E_OK); + EXPECT_EQ(res, static_cast(SyncOperation::Status::OP_SCHEMA_INCOMPATIBLE)); +} + /** * @tc.name: WaterMark 001 * @tc.desc: Test sync success after erase waterMark. -- Gitee From 60b7fd1d2639d1f443ebd05ba7f1c5521c86d541 Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 7 Mar 2022 09:13:29 +0800 Subject: [PATCH 26/37] fix bug that sync use error lastQueryTime Signed-off-by: zqq --- .../libs/distributeddb/syncer/src/single_ver_data_sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_data_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_data_sync.cpp index 185253f0a..12bdd398d 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_data_sync.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_data_sync.cpp @@ -338,7 +338,7 @@ int SingleVerDataSync::GetUnsyncData(SingleVerSyncTaskContext *context, std::vec GetLocalDeleteSyncWaterMark(context, deletedStartMark); TimeStamp lastQueryTimeStamp = 0; errCode = metadata_->GetLastQueryTime(context->GetQuerySyncId(), - context->GetDeleteSyncId(), lastQueryTimeStamp); + context->GetDeviceId(), lastQueryTimeStamp); if (errCode == E_OK) { QuerySyncObject queryObj = context->GetQuery(); errCode = storage_->GetSyncData(queryObj, -- Gitee From 068aa1e197fde1f260661eab968c4d87e09f2b15 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Thu, 3 Mar 2022 16:12:07 +0800 Subject: [PATCH 27/37] Fix code reviews Signed-off-by: lianhuix --- .../common/interfaces/process_system_api_adapter_impl.cpp | 1 - .../include/distributeddb_schema_test_tools.h | 8 ++++---- .../common/distributeddb/src/distributed_test_tools.cpp | 5 +++-- .../src/distributeddb_nb_autolaunch_test.cpp | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/process_system_api_adapter_impl.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/process_system_api_adapter_impl.cpp index 8b451b370..821801545 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/process_system_api_adapter_impl.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/interfaces/process_system_api_adapter_impl.cpp @@ -72,7 +72,6 @@ DBStatus ProcessSystemApiAdapterImpl::SetSecurityOption(const std::string &fileP if (dirPtr == nullptr) { LOGD("set path secOpt![%s] [%d] [%d]", filePath.c_str(), option.securityFlag, option.securityLabel); pathSecOptDic_[filePath] = option; - closedir(dirPtr); return OK; } diff --git a/services/distributeddataservice/test/common/distributeddb/include/distributeddb_schema_test_tools.h b/services/distributeddataservice/test/common/distributeddb/include/distributeddb_schema_test_tools.h index 696dfbefe..4ee1bcb1c 100755 --- a/services/distributeddataservice/test/common/distributeddb/include/distributeddb_schema_test_tools.h +++ b/services/distributeddataservice/test/common/distributeddb/include/distributeddb_schema_test_tools.h @@ -258,10 +258,10 @@ public: ~DistributedDBSchemaTestTools() {} // Delete the copy and assign constructors - DistributedDBSchemaTestTools(const DistributedDBSchemaTestTools &DistributedDBSchemaTestTools) = delete; - DistributedDBSchemaTestTools& operator=(const DistributedDBSchemaTestTools &DistributedDBSchemaTestTools) = delete; - DistributedDBSchemaTestTools(DistributedDBSchemaTestTools &&DistributedDBSchemaTestTools) = delete; - DistributedDBSchemaTestTools& operator=(DistributedDBSchemaTestTools &&DistributedDBSchemaTestTools) = delete; + DistributedDBSchemaTestTools(const DistributedDBSchemaTestTools &testTool) = delete; + DistributedDBSchemaTestTools& operator=(const DistributedDBSchemaTestTools &testTool) = delete; + DistributedDBSchemaTestTools(DistributedDBSchemaTestTools &&testTool) = delete; + DistributedDBSchemaTestTools& operator=(DistributedDBSchemaTestTools &&testTool) = delete; static DistributedDB::Entry GenerateFixedLenJsonSchemaRecord(const unsigned long serialNo, const EntrySize &entrySize, const uint8_t keyFilledChr, const uint8_t valueFilledChr); static std::vector GenerateFixedJsonSchemaRecords( diff --git a/services/distributeddataservice/test/common/distributeddb/src/distributed_test_tools.cpp b/services/distributeddataservice/test/common/distributeddb/src/distributed_test_tools.cpp index f65e79823..64f7a3005 100755 --- a/services/distributeddataservice/test/common/distributeddb/src/distributed_test_tools.cpp +++ b/services/distributeddataservice/test/common/distributeddb/src/distributed_test_tools.cpp @@ -414,6 +414,7 @@ void CopyFile(const string &srcFile, const string &destFile) FILE *pOut = fopen(destFile.c_str(), "w"); if (pOut == nullptr) { perror("pOut"); + fclose(pIn); return; } @@ -435,6 +436,8 @@ void CopyDir(const string &srcDir, const string &destDir, const int authRight) MST_LOG("[CopyDir] SetDir(%s) failed(%d)!", destFullDir.c_str(), errno); return; } + } else { + closedir(dpDest); } string path = srcFullDir; if (srcFullDir.back() != '/') { @@ -446,7 +449,6 @@ void CopyDir(const string &srcDir, const string &destDir, const int authRight) DIR *dpSrc = opendir(path.c_str()); if (dpSrc == nullptr) { - closedir(dpDest); MST_LOG("[CopyDir] please make sure srcDir(%s) is valid.", srcDir.c_str()); return; } @@ -466,7 +468,6 @@ void CopyDir(const string &srcDir, const string &destDir, const int authRight) } } closedir(dpSrc); - closedir(dpDest); MST_LOG("[CopyDir] copy file from %s to %s successfully.", srcDir.c_str(), destDir.c_str()); } diff --git a/services/distributeddataservice/test/moduletest/common/distributeddb/src/distributeddb_nb_autolaunch_test.cpp b/services/distributeddataservice/test/moduletest/common/distributeddb/src/distributeddb_nb_autolaunch_test.cpp index 7b5901e5f..a3cc731d9 100644 --- a/services/distributeddataservice/test/moduletest/common/distributeddb/src/distributeddb_nb_autolaunch_test.cpp +++ b/services/distributeddataservice/test/moduletest/common/distributeddb/src/distributeddb_nb_autolaunch_test.cpp @@ -646,7 +646,7 @@ HWTEST_F(DistributeddbNbAutolaunchTest, ClosedSyncPressure001, TestSize.Level0) * @tc.name: SyncCommErr 001 * @tc.desc: Test Sync return Code, when Communicator get some error. * @tc.type: FUNC - * @tc.require: DTS2020052001231 + * @tc.require: * @tc.author: xushaohua */ HWTEST_F(DistributeddbNbAutolaunchTest, SyncCommErr001, TestSize.Level1) -- Gitee From c010be8197b27113b9a7aacdf491e3d59442d4cb Mon Sep 17 00:00:00 2001 From: lianhuix Date: Sat, 5 Mar 2022 16:34:01 +0800 Subject: [PATCH 28/37] Fix code reviews Signed-off-by: lianhuix --- .../distributeddb/common/include/db_ability.h | 2 +- .../distributeddb/common/include/ischema.h | 8 ++++--- .../distributeddb/common/src/auto_launch.cpp | 2 +- .../distributeddb/common/src/db_ability.cpp | 2 +- .../relational/relational_store_instance.h | 2 +- .../relational/relational_sync_able_storage.h | 2 +- .../storage/src/data_transformer.cpp | 21 ++++++------------- .../storage/src/data_transformer.h | 8 +++---- .../storage/src/relational_store_instance.cpp | 5 +++-- .../src/relational_sync_able_storage.cpp | 2 +- .../sqlite_relational_store_connection.cpp | 7 ------- .../sqlite_relational_store_connection.h | 1 - ...single_ver_relational_storage_executor.cpp | 5 ++--- .../storage/src/sqlite/sqlite_utils.cpp | 8 +++---- 14 files changed, 30 insertions(+), 45 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/include/db_ability.h b/services/distributeddataservice/libs/distributeddb/common/include/db_ability.h index 75466cfec..16c5abcb5 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/db_ability.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/db_ability.h @@ -45,7 +45,7 @@ public: uint32_t GetAbilityBitsLen() const; - uint8_t GetAbilityItem(const AbilityItem abilityType) const; + uint8_t GetAbilityItem(const AbilityItem &abilityType) const; int SetAbilityItem(const AbilityItem &abilityType, uint8_t data); private: diff --git a/services/distributeddataservice/libs/distributeddb/common/include/ischema.h b/services/distributeddataservice/libs/distributeddb/common/include/ischema.h index dffc432ba..32f69fee4 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/ischema.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/ischema.h @@ -13,11 +13,13 @@ * limitations under the License. */ -#ifndef SCHEMA_H -#define SCHEMA_H +#ifndef I_SCHEMA_H +#define I_SCHEMA_H #include +#include "db_types.h" + namespace DistributedDB { // SchemaType::NONE represent for KV database which do not have schema. Only invalid SchemaObject is NONE type. // Enum value must not be changed except SchemaType::UNRECOGNIZED. @@ -69,4 +71,4 @@ public: virtual std::string ToSchemaString() const = 0; }; } -#endif // SCHEMA_H \ No newline at end of file +#endif // I_SCHEMA_H \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp b/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp index 0c1277b48..7b4440fa0 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/auto_launch.cpp @@ -522,7 +522,7 @@ void AutoLaunch::ConnectionLifeCycleCallbackTask(const std::string &identifier, autoLaunchItem = autoLaunchItemMap_[identifier][userId]; } LOGI("[AutoLaunch] ConnectionLifeCycleCallbackTask do CloseConnection"); - TryCloseConnection(autoLaunchItem); // do onthing if failed + TryCloseConnection(autoLaunchItem); // do nothing if failed LOGI("[AutoLaunch] ConnectionLifeCycleCallback do CloseConnection finished"); { std::lock_guard lock(dataLock_); diff --git a/services/distributeddataservice/libs/distributeddb/common/src/db_ability.cpp b/services/distributeddataservice/libs/distributeddb/common/src/db_ability.cpp index 673191fdc..87d0ca7eb 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/db_ability.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/db_ability.cpp @@ -125,7 +125,7 @@ uint32_t DbAbility::GetAbilityBitsLen() const return dbAbility_.size(); } -uint8_t DbAbility::GetAbilityItem(const AbilityItem abilityType) const +uint8_t DbAbility::GetAbilityItem(const AbilityItem &abilityType) const { uint8_t data = 0; auto iter = dbAbilityItemSet_.find(abilityType); diff --git a/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_instance.h b/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_instance.h index 0bdc2aa93..5d9733a56 100644 --- a/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_instance.h +++ b/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_store_instance.h @@ -42,7 +42,7 @@ private: IRelationalStore *OpenDatabase(const RelationalDBProperties &properties, int &errCode); void RemoveKvDBFromCache(const RelationalDBProperties &properties); - void SaveKvDBToCache(IRelationalStore *store, const RelationalDBProperties &properties); + void SaveRelationalDBToCache(IRelationalStore *store, const RelationalDBProperties &properties); void EnterDBOpenCloseProcess(const std::string &identifier); void ExitDBOpenCloseProcess(const std::string &identifier); diff --git a/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h b/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h index 66fec6748..7cc831745 100644 --- a/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h +++ b/services/distributeddataservice/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h @@ -130,7 +130,7 @@ private: // data SQLiteSingleRelationalStorageEngine *storageEngine_ = nullptr; TimeStamp currentMaxTimeStamp_ = 0; - KvDBProperties properties; + KvDBProperties properties_; mutable std::mutex maxTimeStampMutex_; std::function onSchemaChanged_; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp index 1804808b4..8e05736e9 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.cpp @@ -47,10 +47,10 @@ int DataTransformer::TransformDataItem(const std::vector &dataItems, return E_OK; } std::vector indexMapping; - ReduceMapping(remoteFieldInfo, localFieldInfo, indexMapping); + ReduceMapping(remoteFieldInfo, localFieldInfo); for (const DataItem &dataItem : dataItems) { OptRowDataWithLog dataWithLog; - int errCode = DeSerializeDataItem(dataItem, dataWithLog, remoteFieldInfo, indexMapping); + int errCode = DeSerializeDataItem(dataItem, dataWithLog, remoteFieldInfo); if (errCode != E_OK) { return errCode; } @@ -77,11 +77,10 @@ int DataTransformer::SerializeDataItem(const RowDataWithLog &data, } int DataTransformer::DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data, - const std::vector &remoteFieldInfo, std::vector &indexMapping) + const std::vector &remoteFieldInfo) { - int errCode; if ((dataItem.flag & DataItem::DELETE_FLAG) == 0) { - errCode = DeSerializeValue(dataItem.value, data.optionalData, remoteFieldInfo, indexMapping); + int errCode = DeSerializeValue(dataItem.value, data.optionalData, remoteFieldInfo); if (errCode != E_OK) { return errCode; } @@ -126,20 +125,13 @@ uint32_t DataTransformer::CalDataValueLength(const DataValue &dataValue) } void DataTransformer::ReduceMapping(const std::vector &remoteFieldInfo, - const std::vector &localFieldInfo, std::vector &indexMapping) + const std::vector &localFieldInfo) { std::map fieldMap; for (int i = 0; i < static_cast(remoteFieldInfo.size()); ++i) { const auto &fieldInfo = remoteFieldInfo[i]; fieldMap[fieldInfo.GetFieldName()] = i; } - for (const auto &fieldInfo : localFieldInfo) { - if (fieldMap.find(fieldInfo.GetFieldName()) == fieldMap.end()) { - indexMapping.push_back(-E_NOT_FOUND); - continue; - } - indexMapping.push_back(fieldMap[fieldInfo.GetFieldName()]); - } } namespace { @@ -322,9 +314,8 @@ int DataTransformer::SerializeValue(Value &value, const RowData &rowData, const } int DataTransformer::DeSerializeValue(const Value &value, OptRowData &optionalData, - const std::vector &remoteFieldInfo, std::vector &indexMapping) + const std::vector &remoteFieldInfo) { - (void)indexMapping; Parcel parcel(const_cast(value.data()), value.size()); uint64_t fieldCount = 0; (void)parcel.ReadUInt64(fieldCount); diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.h b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.h index 4d190e39d..92774ce5b 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.h +++ b/services/distributeddataservice/libs/distributeddb/storage/src/data_transformer.h @@ -27,7 +27,7 @@ using RowData = std::vector; using OptRowData = std::vector; struct LogInfo { - int dataKey = -1; + int64_t dataKey = -1; std::string device; std::string originDev; TimeStamp timestamp = 0; @@ -66,14 +66,14 @@ public: static int SerializeDataItem(const RowDataWithLog &data, const std::vector &fieldInfo, DataItem &dataItem); static int DeSerializeDataItem(const DataItem &dataItem, OptRowDataWithLog &data, - const std::vector &remoteFieldInfo, std::vector &indexMapping); + const std::vector &remoteFieldInfo); static void ReduceMapping(const std::vector &remoteFieldInfo, - const std::vector &localFieldInfo, std::vector &indexMapping); + const std::vector &localFieldInfo); private: static int SerializeValue(Value &value, const RowData &rowData, const std::vector &fieldInfoList); static int DeSerializeValue(const Value &value, OptRowData &optionalData, - const std::vector &remoteFieldInfo, std::vector &indexMapping); + const std::vector &remoteFieldInfo); static uint32_t CalDataValueLength(const DataValue &dataValue); }; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/relational_store_instance.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/relational_store_instance.cpp index de1dedeb3..cb44f6fbb 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/relational_store_instance.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/relational_store_instance.cpp @@ -103,7 +103,7 @@ void RelationalStoreInstance::RemoveKvDBFromCache(const RelationalDBProperties & dbs_.erase(identifier); } -void RelationalStoreInstance::SaveKvDBToCache(IRelationalStore *store, const RelationalDBProperties &properties) +void RelationalStoreInstance::SaveRelationalDBToCache(IRelationalStore *store, const RelationalDBProperties &properties) { if (store == nullptr) { return; @@ -122,6 +122,7 @@ IRelationalStore *RelationalStoreInstance::OpenDatabase(const RelationalDBProper { auto db = new (std::nothrow) SQLiteRelationalStore(); if (db == nullptr) { + errCode = -E_OUT_OF_MEMORY; LOGE("Failed to get relational store! err:%d", errCode); return nullptr; } @@ -138,7 +139,7 @@ IRelationalStore *RelationalStoreInstance::OpenDatabase(const RelationalDBProper return nullptr; } - SaveKvDBToCache(db, properties); + SaveRelationalDBToCache(db, properties); return db; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp index 02a32fab1..c62c09057 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp @@ -206,7 +206,7 @@ int RelationalSyncAbleStorage::GetAllMetaKeys(std::vector &keys) const const KvDBProperties &RelationalSyncAbleStorage::GetDbProperties() const { - return properties; + return properties_; } static int GetKvEntriesByDataItems(std::vector &entries, std::vector &dataItems) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp index ff9965c39..be2cc3b9e 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp @@ -95,7 +95,6 @@ int SQLiteRelationalStoreConnection::StartTransaction() LOGD("[RelationalConnection] Start transaction finish."); writeHandle_ = handle; - transactingFlag_.store(true); return E_OK; } @@ -110,9 +109,6 @@ int SQLiteRelationalStoreConnection::Commit() int errCode = writeHandle_->Commit(); ReleaseExecutor(writeHandle_); - if (errCode == E_OK) { - transactingFlag_.store(false); - } LOGD("connection commit transaction!"); return errCode; } @@ -128,9 +124,6 @@ int SQLiteRelationalStoreConnection::RollBack() int errCode = writeHandle_->Rollback(); ReleaseExecutor(writeHandle_); - if (errCode == E_OK) { - transactingFlag_.store(false); - } LOGI("connection rollback transaction!"); return errCode; } diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h index 6094af022..13b2dbd26 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h @@ -60,7 +60,6 @@ private: SQLiteSingleVerRelationalStorageExecutor *writeHandle_ = nullptr; mutable std::mutex transactionMutex_; // used for transaction - std::atomic transactingFlag_; }; } // namespace DistributedDB #endif diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp index 4ac87ec80..76867db2f 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp @@ -431,7 +431,7 @@ static int BindDataValueByType(sqlite3_stmt *statement, const std::optional dev; int errCode = SQLiteUtils::GetColumnBlobValue(logStatement, 1, dev); // 1 means dev index @@ -748,9 +748,8 @@ int SQLiteSingleVerRelationalStorageExecutor::SaveSyncDataItem(const DataItem &d return DeleteSyncDataItem(dataItem, rmDataStmt); } - std::vector indexMapping; OptRowDataWithLog data; - int errCode = DataTransformer::DeSerializeDataItem(dataItem, data, fieldInfos, indexMapping); + int errCode = DataTransformer::DeSerializeDataItem(dataItem, data, fieldInfos); if (errCode != E_OK) { LOGE("[RelationalStorageExecutor] DeSerialize dataItem failed! errCode = [%d]", errCode); return errCode; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index 5f0b8576a..368018710 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -594,7 +594,7 @@ int SQLiteUtils::CheckIntegrity(sqlite3 *db, const std::string &sql) namespace { // anonymous namespace for schema analysis int AnalysisSchemaSqlAndTrigger(sqlite3 *db, const std::string &tableName, TableInfo &table) { - std::string sql = "select type, name, tbl_name, rootpage, sql from sqlite_master where tbl_name = ?"; + std::string sql = "select type, sql from sqlite_master where tbl_name = ?"; sqlite3_stmt *statement = nullptr; int errCode = SQLiteUtils::GetStatement(db, sql, statement); if (errCode != E_OK) { @@ -620,7 +620,7 @@ int AnalysisSchemaSqlAndTrigger(sqlite3 *db, const std::string &tableName, Table (void) SQLiteUtils::GetColumnTextValue(statement, 0, type); if (type == "table") { std::string createTableSql; - (void) SQLiteUtils::GetColumnTextValue(statement, 4, createTableSql); // 4 means create table sql + (void) SQLiteUtils::GetColumnTextValue(statement, 1, createTableSql); // 1 means create table sql table.SetCreateTableSql(createTableSql); } } else { @@ -929,13 +929,13 @@ int SQLiteUtils::GetJournalMode(sqlite3 *db, std::string &mode) std::string sql = "PRAGMA journal_mode;"; sqlite3_stmt *statement = nullptr; - int errCode = sqlite3_prepare(db, sql.c_str(), -1, &statement, nullptr); + int errCode = SQLiteUtils::GetStatement(db, sql, statement); if (errCode != SQLITE_OK || statement == nullptr) { errCode = SQLiteUtils::MapSQLiteErrno(errCode); return errCode; } - if (sqlite3_step(statement) == SQLITE_ROW) { + if (SQLiteUtils::StepWithRetry(statement) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { errCode = SQLiteUtils::GetColumnTextValue(statement, 0, mode); } else { LOGE("[SqlUtil][GetJournal] Get db journal_mode failed."); -- Gitee From c1e7a75071bc055537424b4ab2fde15210031419 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Tue, 1 Mar 2022 21:23:33 +0800 Subject: [PATCH 29/37] Separate schema negotiate. Signed-off-by: lianhuix --- .../libs/distributeddb/BUILD.gn | 1 + .../distributeddb/common/include/ischema.h | 13 - .../relational/relational_schema_object.h | 29 --- .../common/include/schema_negotiate.h | 65 +++++ .../common/include/schema_object.h | 8 - .../relational/relational_schema_object.cpp | 196 -------------- .../common/src/schema_negotiate.cpp | 244 ++++++++++++++++++ .../common/src/schema_object.cpp | 76 ------ .../include/relational_db_sync_interface.h | 1 + .../src/relational_sync_able_storage.cpp | 2 +- .../distributeddb/syncer/src/ability_sync.cpp | 18 +- .../distributeddb/syncer/src/ability_sync.h | 1 + ...ingle_ver_relational_sync_task_context.cpp | 6 +- .../syncer/src/single_ver_sync_task_context.h | 2 +- .../libs/distributeddb/test/BUILD.gn | 1 + ...ibuteddb_relational_schema_object_test.cpp | 49 ++-- 16 files changed, 354 insertions(+), 358 deletions(-) create mode 100644 services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h create mode 100644 services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp diff --git a/services/distributeddataservice/libs/distributeddb/BUILD.gn b/services/distributeddataservice/libs/distributeddb/BUILD.gn index c363fd7c7..ed3a74a38 100755 --- a/services/distributeddataservice/libs/distributeddb/BUILD.gn +++ b/services/distributeddataservice/libs/distributeddb/BUILD.gn @@ -93,6 +93,7 @@ ohos_shared_library("distributeddb") { "common/src/runtime_context.cpp", "common/src/runtime_context_impl.cpp", "common/src/schema_constant.cpp", + "common/src/schema_negotiate.cpp", "common/src/schema_object.cpp", "common/src/schema_utils.cpp", "common/src/semaphore_utils.cpp", diff --git a/services/distributeddataservice/libs/distributeddb/common/include/ischema.h b/services/distributeddataservice/libs/distributeddb/common/include/ischema.h index 32f69fee4..f4413b25c 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/ischema.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/ischema.h @@ -39,19 +39,6 @@ inline SchemaType ReadSchemaType(uint8_t inType) return static_cast(inType); } -struct SyncOpinion { - bool permitSync = false; - bool requirePeerConvert = false; - bool checkOnReceive = false; -}; - -struct SyncStrategy { - bool permitSync = false; - bool convertOnSend = false; - bool convertOnReceive = false; - bool checkOnReceive = false; -}; - struct SchemaAttribute { FieldType type = FieldType::LEAF_FIELD_NULL; bool isIndexable = false; diff --git a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h index 9c97134dc..884dcd4ce 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/relational/relational_schema_object.h @@ -96,40 +96,11 @@ private: mutable std::vector fieldInfos_; }; -class RelationalSyncOpinion { -public: - uint32_t CalculateParcelLen(uint32_t softWareVersion) const; - int SerializeData(Parcel &parcel, uint32_t softWareVersion) const; - static int DeserializeData(Parcel &parcel, RelationalSyncOpinion &opinion); - SyncOpinion GetTableOpinion(const std::string& tableName) const; - const std::map &GetOpinions() const; - void AddSyncOpinion(const std::string &tableName, const SyncOpinion &opinion); -private: - std::map opinions_; - static const uint32_t SYNC_OPINION_VERSION = 1; -}; - -class RelationalSyncStrategy { -public: - SyncStrategy GetTableStrategy(const std::string &tableName) const; - void AddSyncStrategy(const std::string &tableName, const SyncStrategy &strategy); - const std::map &GetStrategies() const; -private: - std::map strategies_; -}; - class RelationalSchemaObject : public ISchema { public: RelationalSchemaObject() = default; ~RelationalSchemaObject() override = default; - static RelationalSyncOpinion MakeLocalSyncOpinion(const RelationalSchemaObject &localSchema, - const std::string &remoteSchema, uint8_t remoteSchemaType); - - // The remoteOpinion.checkOnReceive is ignored - static RelationalSyncStrategy ConcludeSyncStrategy(const RelationalSyncOpinion &localOpinion, - const RelationalSyncOpinion &remoteOpinion); - bool IsSchemaValid() const override; SchemaType GetSchemaType() const override; diff --git a/services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h b/services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h new file mode 100644 index 000000000..302171bf6 --- /dev/null +++ b/services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h @@ -0,0 +1,65 @@ +/* + * 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 SCHEMA_NEGOTIATE_H +#define SCHEMA_NEGOTIATE_H + +#include "schema_object.h" +#include "relational_schema_object.h" + +namespace DistributedDB { + +struct SyncOpinion { + bool permitSync = false; + bool requirePeerConvert = false; + bool checkOnReceive = false; +}; + +struct SyncStrategy { + bool permitSync = false; + bool convertOnSend = false; + bool convertOnReceive = false; + bool checkOnReceive = false; +}; + +using RelationalSyncOpinion = std::map; +using RelationalSyncStrategy = std::map; + +class SchemaNegotiate { +public: + // The remoteSchemaType may beyond local SchemaType definition + static SyncOpinion MakeLocalSyncOpinion(const SchemaObject &localSchema, const std::string &remoteSchema, + uint8_t remoteSchemaType); + + // The remoteOpinion.checkOnReceive is ignored + static SyncStrategy ConcludeSyncStrategy(const SyncOpinion &localOpinion, const SyncOpinion &remoteOpinion); + + static RelationalSyncOpinion MakeLocalSyncOpinion(const RelationalSchemaObject &localSchema, + const std::string &remoteSchema, uint8_t remoteSchemaType); + + // The remoteOpinion.checkOnReceive is ignored + static RelationalSyncStrategy ConcludeSyncStrategy(const RelationalSyncOpinion &localOpinion, + const RelationalSyncOpinion &remoteOpinion); + + static uint32_t CalculateParcelLen(const RelationalSyncOpinion &opinions, uint32_t softWareVersion); + static int SerializeData(const RelationalSyncOpinion &opinions, Parcel &parcel, uint32_t softWareVersion); + static int DeserializeData(Parcel &parcel, RelationalSyncOpinion &opinion); + +private: + SchemaNegotiate() = default; + ~SchemaNegotiate() = default; +}; +} + +#endif // SCHEMA_NEGOTIATE_H \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h b/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h index 9411c1864..b788def45 100755 --- a/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/schema_object.h @@ -45,13 +45,6 @@ public: static std::string GenerateExtractSQL(SchemaType inSchemaType, const FieldPath &inFieldpath, FieldType inFieldType, uint32_t skipSize, const std::string &accessStr = ""); - // The remoteSchemaType may beyond local SchemaType definition - static SyncOpinion MakeLocalSyncOpinion(const SchemaObject &localSchema, const std::string &remoteSchema, - uint8_t remoteSchemaType); - - // The remoteOpinion.checkOnReceive is ignored - static SyncStrategy ConcludeSyncStrategy(const SyncOpinion &localOpinion, const SyncOpinion &remoteOpinion); - // Support default constructor, copy constructor and copy assignment SchemaObject(); ~SchemaObject() = default; @@ -215,4 +208,3 @@ private: } // namespace DistributedDB #endif // SCHEMA_OBJECT_H - diff --git a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp index 228011e1f..c087a3a53 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/relational/relational_schema_object.cpp @@ -380,202 +380,6 @@ namespace { const std::string MAGIC = "relational_opinion"; } -uint32_t RelationalSyncOpinion::CalculateParcelLen(uint32_t softWareVersion) const -{ - uint64_t len = Parcel::GetStringLen(MAGIC); - len += Parcel::GetUInt32Len(); - len += Parcel::GetUInt32Len(); - len = Parcel::GetEightByteAlign(len); - for (const auto &it : opinions_) { - len += Parcel::GetStringLen(it.first); - len += Parcel::GetUInt32Len(); - len += Parcel::GetUInt32Len(); - len = Parcel::GetEightByteAlign(len); - } - if (len > UINT32_MAX) { - return 0; - } - return static_cast(len); -} - -int RelationalSyncOpinion::SerializeData(Parcel &parcel, uint32_t softWareVersion) const -{ - (void)parcel.WriteString(MAGIC); - (void)parcel.WriteUInt32(SYNC_OPINION_VERSION); - (void)parcel.WriteUInt32(static_cast(opinions_.size())); - (void)parcel.EightByteAlign(); - for (const auto &it : opinions_) { - (void)parcel.WriteString(it.first); - (void)parcel.WriteUInt32(it.second.permitSync); - (void)parcel.WriteUInt32(it.second.requirePeerConvert); - (void)parcel.EightByteAlign(); - } - return parcel.IsError() ? -E_INVALID_ARGS : E_OK; -} - -int RelationalSyncOpinion::DeserializeData(Parcel &parcel, RelationalSyncOpinion &opinion) -{ - if (!parcel.IsContinueRead()) { - return E_OK; - } - std::string magicStr; - (void)parcel.ReadString(magicStr); - if (magicStr != MAGIC) { - LOGE("Deserialize sync opinion failed while read MAGIC string [%s]", magicStr.c_str()); - return -E_INVALID_ARGS; - } - uint32_t version; - (void)parcel.ReadUInt32(version); - if (version != SYNC_OPINION_VERSION) { - LOGE("Not support sync opinion version: %u", version); - return -E_NOT_SUPPORT; - } - uint32_t opinionSize; - (void)parcel.ReadUInt32(opinionSize); - (void)parcel.EightByteAlign(); - for (uint32_t i = 0; i < opinionSize; i++) { - std::string tableName; - SyncOpinion tableOpinion; - (void)parcel.ReadString(tableName); - uint32_t permitSync; - (void)parcel.ReadUInt32(permitSync); - tableOpinion.permitSync = static_cast(permitSync); - uint32_t requirePeerConvert; - (void)parcel.ReadUInt32(requirePeerConvert); - tableOpinion.requirePeerConvert = static_cast(requirePeerConvert); - (void)parcel.EightByteAlign(); - opinion.AddSyncOpinion(tableName, tableOpinion); - } - return parcel.IsError() ? -E_INVALID_ARGS : E_OK; -} - -SyncOpinion RelationalSyncOpinion::GetTableOpinion(const std::string &tableName) const -{ - auto it = opinions_.find(tableName); - if (it == opinions_.end()) { - return {}; - } - return it->second; -} - -const std::map &RelationalSyncOpinion::GetOpinions() const -{ - return opinions_; -} - -void RelationalSyncOpinion::AddSyncOpinion(const std::string &tableName, const SyncOpinion &opinion) -{ - opinions_[tableName] = opinion; -} - -SyncStrategy RelationalSyncStrategy::GetTableStrategy(const std::string &tableName) const -{ - auto it = strategies_.find(tableName); - if (it == strategies_.end()) { - return {}; - } - return it->second; -} - -void RelationalSyncStrategy::AddSyncStrategy(const std::string &tableName, const SyncStrategy &strategy) -{ - strategies_[tableName] = strategy; -} - -const std::map &RelationalSyncStrategy::GetStrategies() const -{ - return strategies_; -} - -RelationalSyncOpinion RelationalSchemaObject::MakeLocalSyncOpinion(const RelationalSchemaObject &localSchema, - const std::string &remoteSchema, uint8_t remoteSchemaType) -{ - SchemaType localType = localSchema.GetSchemaType(); - SchemaType remoteType = ReadSchemaType(remoteSchemaType); - if (remoteType == SchemaType::UNRECOGNIZED) { - LOGW("[RelationalSchema][opinion] Remote schema type %d is unrecognized.", remoteSchemaType); - return {}; - } - - if (remoteType != SchemaType::RELATIVE) { - LOGW("[RelationalSchema][opinion] Not support sync with schema type: local-type=[%s] remote-type=[%s]", - SchemaUtils::SchemaTypeString(localType).c_str(), SchemaUtils::SchemaTypeString(remoteType).c_str()); - return {}; - } - - if (!localSchema.IsSchemaValid()) { - LOGW("[RelationalSchema][opinion] Local schema is not valid"); - return {}; - } - - RelationalSchemaObject remoteSchemaObj; - int errCode = remoteSchemaObj.ParseFromSchemaString(remoteSchema); - if (errCode != E_OK) { - LOGW("[RelationalSchema][opinion] Parse remote schema failed %d, remote schema type %s", errCode, - SchemaUtils::SchemaTypeString(remoteType).c_str()); - return {}; - } - - RelationalSyncOpinion opinion; - for (const auto &it : localSchema.GetTables()) { - if (remoteSchemaObj.GetTable(it.first).GetTableName() != it.first) { - LOGW("[RelationalSchema][opinion] Table was missing in remote schema"); - continue; - } - // remote table is compatible(equal or upgrade) based on local table, permit sync and don't need check - errCode = it.second.CompareWithTable(remoteSchemaObj.GetTable(it.first)); - if (errCode != -E_RELATIONAL_TABLE_INCOMPATIBLE) { - opinion.AddSyncOpinion(it.first, {true, false, false}); - continue; - } - // local table is compatible upgrade based on remote table, permit sync and need check - errCode = remoteSchemaObj.GetTable(it.first).CompareWithTable(it.second); - if (errCode != -E_RELATIONAL_TABLE_INCOMPATIBLE) { - opinion.AddSyncOpinion(it.first, {true, false, true}); - continue; - } - // local table is incompatible with remote table mutually, don't permit sync and need check - LOGW("[RelationalSchema][opinion] Local table is incompatible with remote table mutually."); - opinion.AddSyncOpinion(it.first, {false, true, true}); - } - - return opinion; -} - -RelationalSyncStrategy RelationalSchemaObject::ConcludeSyncStrategy(const RelationalSyncOpinion &localOpinion, - const RelationalSyncOpinion &remoteOpinion) -{ - RelationalSyncStrategy syncStrategy; - for (const auto &itLocal : localOpinion.GetOpinions()) { - if (remoteOpinion.GetOpinions().find(itLocal.first) == remoteOpinion.GetOpinions().end()) { - LOGW("[RelationalSchema][Strategy] Table opinion is not found from remote."); - continue; - } - - SyncStrategy strategy; - SyncOpinion localTableOpinion = itLocal.second; - SyncOpinion remoteTableOpinion = remoteOpinion.GetTableOpinion(itLocal.first); - - strategy.permitSync = (localTableOpinion.permitSync || remoteTableOpinion.permitSync); - - bool convertConflict = (localTableOpinion.requirePeerConvert && remoteTableOpinion.requirePeerConvert); - if (convertConflict) { - // Should not both need peer convert - strategy.permitSync = false; - } - // No peer convert required means local side has convert capability, convert locally takes precedence - strategy.convertOnSend = (!localTableOpinion.requirePeerConvert); - strategy.convertOnReceive = remoteTableOpinion.requirePeerConvert; - - strategy.checkOnReceive = localTableOpinion.checkOnReceive; - LOGI("[RelationalSchema][Strategy] PermitSync=%d, SendConvert=%d, ReceiveConvert=%d, ReceiveCheck=%d.", - strategy.permitSync, strategy.convertOnSend, strategy.convertOnReceive, strategy.checkOnReceive); - syncStrategy.AddSyncStrategy(itLocal.first, strategy); - } - - return syncStrategy; -} - bool RelationalSchemaObject::IsSchemaValid() const { return isValid_; diff --git a/services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp b/services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp new file mode 100644 index 000000000..564ef31cc --- /dev/null +++ b/services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp @@ -0,0 +1,244 @@ +/* + * 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. + */ +#include "schema_negotiate.h" + +#include "log_print.h" +#include "schema_utils.h" + +namespace DistributedDB { +// Some principle in current version describe below. (Relative-type will be introduced in future but not involved now) +// 1. PermitSync: Be false may because schemaType-unrecognized, schemaType-different, schema-unparsable, +// schemaVersion-unrecognized, schema-incompatible, and so on. +// 2. RequirePeerConvert: Be true normally when permitSync false, for future possible sync and convert(by remote). +// 3. checkOnReceive: Be false when local is KV-DB, or when local is not KV-DB only if schema type equal as well as +// define equal or remote is the upgradation of local. +SyncOpinion SchemaNegotiate::MakeLocalSyncOpinion(const SchemaObject &localSchema, const std::string &remoteSchema, + uint8_t remoteSchemaType) +{ + SchemaType localType = localSchema.GetSchemaType(); // An invalid schemaObject will return SchemaType::NONE + SchemaType remoteType = ReadSchemaType(remoteSchemaType); + // Logic below only be correct in current version, should be redesigned if new type added in the future + // 1. If remote-type unrecognized(Include Relative-type), Do not permit sync. + if (remoteType == SchemaType::UNRECOGNIZED) { + LOGE("[Schema][Opinion] Remote-type=%u unrecognized.", remoteSchemaType); + return SyncOpinion{false, true, true}; + } + // 2. If local-type is KV(Here remote-type is within recognized), Always permit sync. + if (localType == SchemaType::NONE) { + LOGI("[Schema][Opinion] Local-type KV."); + return SyncOpinion{true, false, false}; + } + // 3. If remote-type is KV(Here local-type can only be JSON or FLATBUFFER), Always permit sync but need check. + if (remoteType == SchemaType::NONE) { + LOGI("[Schema][Opinion] Remote-type KV."); + return SyncOpinion{true, false, true}; + } + // 4. If local-type differ with remote-type(Here both type can only be JSON or FLATBUFFER), Do not permit sync. + if (localType != remoteType) { + LOGE("[Schema][Opinion] Local-type=%s differ remote-type=%s.", SchemaUtils::SchemaTypeString(localType).c_str(), + SchemaUtils::SchemaTypeString(remoteType).c_str()); + return SyncOpinion{false, true, true}; + } + // 5. If schema parse fail, Do not permit sync. + SchemaObject remoteSchemaObj; + int errCode = remoteSchemaObj.ParseFromSchemaString(remoteSchema); + if (errCode != E_OK) { + LOGE("[Schema][Opinion] Parse remote-schema fail, errCode=%d, remote-type=%s.", errCode, + SchemaUtils::SchemaTypeString(remoteType).c_str()); + return SyncOpinion{false, true, true}; + } + // 6. If remote-schema is not incompatible based on local-schema(SchemaDefine Equal), Permit sync and don't check. + errCode = localSchema.CompareAgainstSchemaObject(remoteSchemaObj); + if (errCode != -E_SCHEMA_UNEQUAL_INCOMPATIBLE) { + return SyncOpinion{true, false, false}; + } + // 7. If local-schema is not incompatible based on remote-schema(Can only be COMPATIBLE_UPGRADE), Sync and check. + errCode = remoteSchemaObj.CompareAgainstSchemaObject(localSchema); + if (errCode != -E_SCHEMA_UNEQUAL_INCOMPATIBLE) { + return SyncOpinion{true, false, true}; + } + // 8. Local-schema incompatible with remote-schema mutually. + LOGE("[Schema][Opinion] Local-schema incompatible with remote-schema mutually."); + return SyncOpinion{false, true, true}; +} + +SyncStrategy SchemaNegotiate::ConcludeSyncStrategy(const SyncOpinion &localOpinion, const SyncOpinion &remoteOpinion) +{ + SyncStrategy outStrategy; + // Any side permit sync, the final conclusion is permit sync. + outStrategy.permitSync = (localOpinion.permitSync || remoteOpinion.permitSync); + bool convertConflict = (localOpinion.requirePeerConvert && remoteOpinion.requirePeerConvert); + if (convertConflict) { + outStrategy.permitSync = false; + } + // Responsible for conversion on send now that local do not require remote to do conversion + outStrategy.convertOnSend = (!localOpinion.requirePeerConvert); + // Responsible for conversion on receive since remote will not do conversion on send and require local to convert + outStrategy.convertOnReceive = remoteOpinion.requirePeerConvert; + // Only depend on local opinion + outStrategy.checkOnReceive = localOpinion.checkOnReceive; + LOGI("[Schema][Strategy] PermitSync=%d, SendConvert=%d, ReceiveConvert=%d, ReceiveCheck=%d.", + outStrategy.permitSync, outStrategy.convertOnSend, outStrategy.convertOnReceive, outStrategy.checkOnReceive); + return outStrategy; +} + +RelationalSyncOpinion SchemaNegotiate::MakeLocalSyncOpinion(const RelationalSchemaObject &localSchema, + const std::string &remoteSchema, uint8_t remoteSchemaType) +{ + SchemaType localType = localSchema.GetSchemaType(); + SchemaType remoteType = ReadSchemaType(remoteSchemaType); + if (remoteType == SchemaType::UNRECOGNIZED) { + LOGW("[RelationalSchema][opinion] Remote schema type %d is unrecognized.", remoteSchemaType); + return {}; + } + + if (remoteType != SchemaType::RELATIVE) { + LOGW("[RelationalSchema][opinion] Not support sync with schema type: local-type=[%s] remote-type=[%s]", + SchemaUtils::SchemaTypeString(localType).c_str(), SchemaUtils::SchemaTypeString(remoteType).c_str()); + return {}; + } + + if (!localSchema.IsSchemaValid()) { + LOGW("[RelationalSchema][opinion] Local schema is not valid"); + return {}; + } + + RelationalSchemaObject remoteSchemaObj; + int errCode = remoteSchemaObj.ParseFromSchemaString(remoteSchema); + if (errCode != E_OK) { + LOGW("[RelationalSchema][opinion] Parse remote schema failed %d, remote schema type %s", errCode, + SchemaUtils::SchemaTypeString(remoteType).c_str()); + return {}; + } + + RelationalSyncOpinion opinion; + for (const auto &it : localSchema.GetTables()) { + if (remoteSchemaObj.GetTable(it.first).GetTableName() != it.first) { + LOGW("[RelationalSchema][opinion] Table was missing in remote schema"); + continue; + } + // remote table is compatible(equal or upgrade) based on local table, permit sync and don't need check + errCode = it.second.CompareWithTable(remoteSchemaObj.GetTable(it.first)); + if (errCode != -E_RELATIONAL_TABLE_INCOMPATIBLE) { + opinion[it.first] = {true, false, false}; + continue; + } + // local table is compatible upgrade based on remote table, permit sync and need check + errCode = remoteSchemaObj.GetTable(it.first).CompareWithTable(it.second); + if (errCode != -E_RELATIONAL_TABLE_INCOMPATIBLE) { + opinion[it.first] = {true, false, true}; + continue; + } + // local table is incompatible with remote table mutually, don't permit sync and need check + LOGW("[RelationalSchema][opinion] Local table is incompatible with remote table mutually."); + opinion[it.first] = {false, true, true}; + } + + return opinion; +} + +RelationalSyncStrategy SchemaNegotiate::ConcludeSyncStrategy(const RelationalSyncOpinion &localOpinion, + const RelationalSyncOpinion &remoteOpinion) +{ + RelationalSyncStrategy syncStrategy; + for (const auto &itLocal : localOpinion) { + if (remoteOpinion.find(itLocal.first) == remoteOpinion.end()) { + LOGW("[RelationalSchema][Strategy] Table opinion is not found from remote."); + continue; + } + SyncOpinion localTableOpinion = itLocal.second; + SyncOpinion remoteTableOpinion = remoteOpinion.at(itLocal.first); + syncStrategy[itLocal.first] = ConcludeSyncStrategy(localTableOpinion, remoteTableOpinion); + } + + return syncStrategy; +} + +namespace { + const std::string MAGIC = "relational_opinion"; + const uint32_t SYNC_OPINION_VERSION = 1; +} // namespace + + +uint32_t SchemaNegotiate::CalculateParcelLen(const RelationalSyncOpinion &opinions, uint32_t softWareVersion) +{ + uint64_t len = Parcel::GetStringLen(MAGIC); + len += Parcel::GetUInt32Len(); + len += Parcel::GetUInt32Len(); + len = Parcel::GetEightByteAlign(len); + for (const auto &it : opinions) { + len += Parcel::GetStringLen(it.first); + len += Parcel::GetUInt32Len(); + len += Parcel::GetUInt32Len(); + len = Parcel::GetEightByteAlign(len); + } + if (len > UINT32_MAX) { + return 0; + } + return static_cast(len); +} + +int SchemaNegotiate::SerializeData(const RelationalSyncOpinion &opinions, Parcel &parcel, uint32_t softWareVersion) +{ + (void)parcel.WriteString(MAGIC); + (void)parcel.WriteUInt32(SYNC_OPINION_VERSION); + (void)parcel.WriteUInt32(static_cast(opinions.size())); + (void)parcel.EightByteAlign(); + for (const auto &it : opinions) { + (void)parcel.WriteString(it.first); + (void)parcel.WriteUInt32(it.second.permitSync); + (void)parcel.WriteUInt32(it.second.requirePeerConvert); + (void)parcel.EightByteAlign(); + } + return parcel.IsError() ? -E_INVALID_ARGS : E_OK; +} + +int SchemaNegotiate::DeserializeData(Parcel &parcel, RelationalSyncOpinion &opinion) +{ + if (!parcel.IsContinueRead()) { + return E_OK; + } + std::string magicStr; + (void)parcel.ReadString(magicStr); + if (magicStr != MAGIC) { + LOGE("Deserialize sync opinion failed while read MAGIC string [%s]", magicStr.c_str()); + return -E_INVALID_ARGS; + } + uint32_t version; + (void)parcel.ReadUInt32(version); + if (version != SYNC_OPINION_VERSION) { + LOGE("Not support sync opinion version: %u", version); + return -E_NOT_SUPPORT; + } + uint32_t opinionSize; + (void)parcel.ReadUInt32(opinionSize); + (void)parcel.EightByteAlign(); + for (uint32_t i = 0; i < opinionSize; i++) { + std::string tableName; + SyncOpinion tableOpinion; + (void)parcel.ReadString(tableName); + uint32_t permitSync; + (void)parcel.ReadUInt32(permitSync); + tableOpinion.permitSync = static_cast(permitSync); + uint32_t requirePeerConvert; + (void)parcel.ReadUInt32(requirePeerConvert); + tableOpinion.requirePeerConvert = static_cast(requirePeerConvert); + (void)parcel.EightByteAlign(); + opinion[tableName] = tableOpinion; + } + return parcel.IsError() ? -E_INVALID_ARGS : E_OK; +} + +} \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp index 082f428c4..07565295a 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp @@ -81,82 +81,6 @@ std::string SchemaObject::GenerateExtractSQL(SchemaType inSchemaType, const Fiel return resultSql; } -// Some principle in current version describe below. (Relative-type will be introduced in future but not involved now) -// 1. PermitSync: Be false may because schemaType-unrecognized, schemaType-different, schema-unparsable, -// schemaVersion-unrecognized, schema-incompatible, and so on. -// 2. RequirePeerConvert: Be true normally when permitSync false, for future possible sync and convert(by remote). -// 3. checkOnReceive: Be false when local is KV-DB, or when local is not KV-DB only if schema type equal as well as -// define equal or remote is the upgradation of local. -SyncOpinion SchemaObject::MakeLocalSyncOpinion(const SchemaObject &localSchema, const std::string &remoteSchema, - uint8_t remoteSchemaType) -{ - SchemaType localType = localSchema.GetSchemaType(); // An invalid schemaObject will return SchemaType::NONE - SchemaType remoteType = ReadSchemaType(remoteSchemaType); - // Logic below only be correct in current version, should be redesigned if new type added in the future - // 1. If remote-type unrecognized(Include Relative-type), Do not permit sync. - if (remoteType == SchemaType::UNRECOGNIZED) { - LOGE("[Schema][Opinion] Remote-type=%u unrecognized.", remoteSchemaType); - return SyncOpinion{false, true, true}; - } - // 2. If local-type is KV(Here remote-type is within recognized), Always permit sync. - if (localType == SchemaType::NONE) { - LOGI("[Schema][Opinion] Local-type KV."); - return SyncOpinion{true, false, false}; - } - // 3. If remote-type is KV(Here local-type can only be JSON or FLATBUFFER), Always permit sync but need check. - if (remoteType == SchemaType::NONE) { - LOGI("[Schema][Opinion] Remote-type KV."); - return SyncOpinion{true, false, true}; - } - // 4. If local-type differ with remote-type(Here both type can only be JSON or FLATBUFFER), Do not permit sync. - if (localType != remoteType) { - LOGE("[Schema][Opinion] Local-type=%s differ remote-type=%s.", SchemaUtils::SchemaTypeString(localType).c_str(), - SchemaUtils::SchemaTypeString(remoteType).c_str()); - return SyncOpinion{false, true, true}; - } - // 5. If schema parse fail, Do not permit sync. - SchemaObject remoteSchemaObj; - int errCode = remoteSchemaObj.ParseFromSchemaString(remoteSchema); - if (errCode != E_OK) { - LOGE("[Schema][Opinion] Parse remote-schema fail, errCode=%d, remote-type=%s.", errCode, - SchemaUtils::SchemaTypeString(remoteType).c_str()); - return SyncOpinion{false, true, true}; - } - // 6. If remote-schema is not incompatible based on local-schema(SchemaDefine Equal), Permit sync and don't check. - errCode = localSchema.CompareAgainstSchemaObject(remoteSchemaObj); - if (errCode != -E_SCHEMA_UNEQUAL_INCOMPATIBLE) { - return SyncOpinion{true, false, false}; - } - // 7. If local-schema is not incompatible based on remote-schema(Can only be COMPATIBLE_UPGRADE), Sync and check. - errCode = remoteSchemaObj.CompareAgainstSchemaObject(localSchema); - if (errCode != -E_SCHEMA_UNEQUAL_INCOMPATIBLE) { - return SyncOpinion{true, false, true}; - } - // 8. Local-schema incompatible with remote-schema mutually. - LOGE("[Schema][Opinion] Local-schema incompatible with remote-schema mutually."); - return SyncOpinion{false, true, true}; -} - -SyncStrategy SchemaObject::ConcludeSyncStrategy(const SyncOpinion &localOpinion, const SyncOpinion &remoteOpinion) -{ - SyncStrategy outStrategy; - // Any side permit sync, the final conclusion is permit sync. - outStrategy.permitSync = (localOpinion.permitSync || remoteOpinion.permitSync); - bool convertConflict = (localOpinion.requirePeerConvert && remoteOpinion.requirePeerConvert); - if (convertConflict) { - outStrategy.permitSync = false; - } - // Responsible for conversion on send now that local do not require remote to do conversion - outStrategy.convertOnSend = (!localOpinion.requirePeerConvert); - // Responsible for conversion on receive since remote will not do conversion on send and require local to convert - outStrategy.convertOnReceive = remoteOpinion.requirePeerConvert; - // Only depend on local opinion - outStrategy.checkOnReceive = localOpinion.checkOnReceive; - LOGI("[Schema][Strategy] PermitSync=%d, SendConvert=%d, ReceiveConvert=%d, ReceiveCheck=%d.", - outStrategy.permitSync, outStrategy.convertOnSend, outStrategy.convertOnReceive, outStrategy.checkOnReceive); - return outStrategy; -} - SchemaObject::SchemaObject() : flatbufferSchema_(*this) {}; SchemaObject::SchemaObject(const SchemaObject &other) diff --git a/services/distributeddataservice/libs/distributeddb/storage/include/relational_db_sync_interface.h b/services/distributeddataservice/libs/distributeddb/storage/include/relational_db_sync_interface.h index 81e8fb3d8..4f82e6e31 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/include/relational_db_sync_interface.h +++ b/services/distributeddataservice/libs/distributeddb/storage/include/relational_db_sync_interface.h @@ -20,6 +20,7 @@ #include "relational_schema_object.h" #include "single_ver_kv_entry.h" #include "sync_generic_interface.h" +#include "schema_negotiate.h" namespace DistributedDB { class RelationalDBSyncInterface : public SyncGenericInterface { diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp index c62c09057..ad35b6504 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/relational_sync_able_storage.cpp @@ -486,7 +486,7 @@ int RelationalSyncAbleStorage::CreateDistributedDeviceTable(const std::string &d return errCode; } - for (const auto &[table, strategy] : syncStrategy.GetStrategies()) { + for (const auto &[table, strategy] : syncStrategy) { if (!strategy.permitSync) { continue; } diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp index d8389548d..8b621bc25 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp @@ -297,7 +297,7 @@ uint32_t AbilitySyncAckPacket::CalculateLen() const len += Parcel::GetUInt32Len(); // requirePeerConvert_ len += Parcel::GetUInt64Len(); // dbCreateTime_ len += DbAbility::CalculateLen(dbAbility_); // dbAbility_ - len += relationalSyncOpinion_.CalculateParcelLen(softwareVersion_); + len += SchemaNegotiate::CalculateParcelLen(relationalSyncOpinion_, softwareVersion_); if (len > INT32_MAX) { LOGE("[AbilitySyncAckPacket][CalculateLen] err len:%llu", len); return 0; @@ -709,7 +709,7 @@ int AbilitySync::AckPacketSerialization(uint8_t *buffer, uint32_t length, const if (parcel.IsError() || errCode != E_OK) { return -E_PARSE_FAIL; } - errCode = packet->GetRelationalSyncOpinion().SerializeData(parcel, SOFTWARE_VERSION_CURRENT); + errCode = SchemaNegotiate::SerializeData(packet->GetRelationalSyncOpinion(), parcel, SOFTWARE_VERSION_CURRENT); if (parcel.IsError() || errCode != E_OK) { return -E_PARSE_FAIL; } @@ -826,7 +826,7 @@ int AbilitySync::AckPacketDeSerializationTailPart(Parcel &parcel, AbilitySyncAck } packet->SetDbAbility(remoteDbAbility); RelationalSyncOpinion relationalSyncOpinion; - errCode = RelationalSyncOpinion::DeserializeData(parcel, relationalSyncOpinion); + errCode = SchemaNegotiate::DeserializeData(parcel, relationalSyncOpinion); if (errCode != E_OK) { LOGE("[AbilitySync] ack packet DeSerializ RelationalSyncOpinion failed."); return errCode; @@ -1125,7 +1125,7 @@ SyncOpinion AbilitySync::MakeKvSyncOpnion(const AbilitySyncRequestPacket *packet { uint8_t remoteSchemaType = packet->GetSchemaType(); SchemaObject localSchema = (static_cast(storageInterface_))->GetSchemaInfo(); - SyncOpinion localSyncOpinion = SchemaObject::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); + SyncOpinion localSyncOpinion = SchemaNegotiate::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); return localSyncOpinion; } @@ -1134,7 +1134,7 @@ RelationalSyncOpinion AbilitySync::MakeRelationSyncOpnion(const AbilitySyncReque { uint8_t remoteSchemaType = packet->GetSchemaType(); RelationalSchemaObject localSchema = (static_cast(storageInterface_))->GetSchemaInfo(); - return RelationalSchemaObject::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); + return SchemaNegotiate::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); } void AbilitySync::HandleKvAckSchemaParam(const AbilitySyncAckPacket *recvPacket, @@ -1146,8 +1146,8 @@ void AbilitySync::HandleKvAckSchemaParam(const AbilitySyncAckPacket *recvPacket, bool requirePeerConvert = static_cast(recvPacket->GetRequirePeerConvert()); SyncOpinion remoteOpinion = {permitSync, requirePeerConvert, true}; SchemaObject localSchema = (static_cast(storageInterface_))->GetSchemaInfo(); - SyncOpinion syncOpinion = SchemaObject::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); - SyncStrategy localStrategy = SchemaObject::ConcludeSyncStrategy(syncOpinion, remoteOpinion); + SyncOpinion syncOpinion = SchemaNegotiate::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); + SyncStrategy localStrategy = SchemaNegotiate::ConcludeSyncStrategy(syncOpinion, remoteOpinion); SetAbilityAckSyncOpinionInfo(sendPacket, syncOpinion); (static_cast(context))->SetSyncStrategy(localStrategy); } @@ -1158,8 +1158,8 @@ int AbilitySync::HandleRelationAckSchemaParam(const AbilitySyncAckPacket *recvPa std::string remoteSchema = recvPacket->GetSchema(); uint8_t remoteSchemaType = recvPacket->GetSchemaType(); auto localSchema = (static_cast(storageInterface_))->GetSchemaInfo(); - auto localOpinion = RelationalSchemaObject::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); - auto localStrategy = RelationalSchemaObject::ConcludeSyncStrategy(localOpinion, + auto localOpinion = SchemaNegotiate::MakeLocalSyncOpinion(localSchema, remoteSchema, remoteSchemaType); + auto localStrategy = SchemaNegotiate::ConcludeSyncStrategy(localOpinion, recvPacket->GetRelationalSyncOpinion()); (static_cast(context))->SetRelationalSyncStrategy(localStrategy); int errCode = (static_cast(storageInterface_))-> diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h index 10cf53d54..4e2f4d968 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.h @@ -27,6 +27,7 @@ #ifdef RELATIONAL_STORE #include "ischema.h" #endif +#include "schema_negotiate.h" namespace DistributedDB { class AbilitySyncRequestPacket { diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp index 4303a21d1..ef556385f 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp @@ -61,7 +61,11 @@ void SingleVerRelationalSyncTaskContext::SetRelationalSyncStrategy(RelationalSyn SyncStrategy SingleVerRelationalSyncTaskContext::GetSyncStrategy(QuerySyncObject &querySyncObject) const { std::lock_guard autoLock(syncStrategyMutex_); - return relationalSyncStrategy_.GetTableStrategy(querySyncObject.GetRelationTableName()); + auto it = relationalSyncStrategy_.find(querySyncObject.GetRelationTableName()); + if (it == relationalSyncStrategy_.end()) { + return {}; + } + return it->second; } void SingleVerRelationalSyncTaskContext::SetIsNeedResetAbilitySync(bool isNeedReset) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h index 1f60d7659..f0619a410 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/single_ver_sync_task_context.h @@ -23,7 +23,7 @@ #include "db_ability.h" #include "query_sync_object.h" -#include "ischema.h" +#include "schema_negotiate.h" #include "single_ver_kvdb_sync_interface.h" #include "single_ver_sync_target.h" #include "subscribe_manager.h" diff --git a/services/distributeddataservice/libs/distributeddb/test/BUILD.gn b/services/distributeddataservice/libs/distributeddb/test/BUILD.gn index c29b5c53a..7185d447e 100755 --- a/services/distributeddataservice/libs/distributeddb/test/BUILD.gn +++ b/services/distributeddataservice/libs/distributeddb/test/BUILD.gn @@ -96,6 +96,7 @@ ohos_source_set("src_file") { "../common/src/runtime_context.cpp", "../common/src/runtime_context_impl.cpp", "../common/src/schema_constant.cpp", + "../common/src/schema_negotiate.cpp", "../common/src/schema_object.cpp", "../common/src/schema_utils.cpp", "../common/src/semaphore_utils.cpp", diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp index f7367d0ae..3393ec54b 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp @@ -23,6 +23,7 @@ #include "relational_schema_object.h" #include "schema_utils.h" #include "schema_constant.h" +#include "schema_negotiate.h" using namespace std; using namespace testing::ext; @@ -330,11 +331,11 @@ HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalSchemaCompareTest001 int errCode = schemaObj.ParseFromSchemaString(NORMAL_SCHEMA); EXPECT_EQ(errCode, E_OK); - RelationalSyncOpinion opinion = RelationalSchemaObject::MakeLocalSyncOpinion(schemaObj, NORMAL_SCHEMA, + RelationalSyncOpinion opinion = SchemaNegotiate::MakeLocalSyncOpinion(schemaObj, NORMAL_SCHEMA, static_cast(SchemaType::RELATIVE)); - EXPECT_EQ(opinion.GetTableOpinion("FIRST").permitSync, true); - EXPECT_EQ(opinion.GetTableOpinion("FIRST").checkOnReceive, false); - EXPECT_EQ(opinion.GetTableOpinion("FIRST").requirePeerConvert, false); + EXPECT_EQ(opinion.at("FIRST").permitSync, true); + EXPECT_EQ(opinion.at("FIRST").checkOnReceive, false); + EXPECT_EQ(opinion.at("FIRST").requirePeerConvert, false); } /** @@ -379,24 +380,24 @@ HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalSchemaOpinionTest001 { const uint32_t softWareVersion = 106; RelationalSyncOpinion opinion; - opinion.AddSyncOpinion("table_1", SyncOpinion {true, false, false}); - opinion.AddSyncOpinion("table_2", SyncOpinion {false, true, false}); - opinion.AddSyncOpinion("table_3", SyncOpinion {false, false, true}); + opinion["table_1"] = SyncOpinion {true, false, false}; + opinion["table_2"] = SyncOpinion {false, true, false}; + opinion["table_3"] = SyncOpinion {false, false, true}; - uint32_t len = opinion.CalculateParcelLen(softWareVersion); + uint32_t len = SchemaNegotiate::CalculateParcelLen(opinion, softWareVersion); std::vector buff(len, 0); Parcel writeParcel(buff.data(), len); - int errCode = opinion.SerializeData(writeParcel, softWareVersion); + int errCode = SchemaNegotiate::SerializeData(opinion, writeParcel, softWareVersion); EXPECT_EQ(errCode, E_OK); Parcel readParcel(buff.data(), len); RelationalSyncOpinion opinionRecv; - errCode = RelationalSyncOpinion::DeserializeData(readParcel, opinionRecv); + errCode = SchemaNegotiate::DeserializeData(readParcel, opinionRecv); EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(opinion.GetOpinions().size(), opinionRecv.GetOpinions().size()); - for (const auto &it : opinion.GetOpinions()) { - SyncOpinion tableOpinionRecv = opinionRecv.GetTableOpinion(it.first); + EXPECT_EQ(opinion.size(), opinionRecv.size()); + for (const auto &it : opinion) { + SyncOpinion tableOpinionRecv = opinionRecv.at(it.first); EXPECT_EQ(it.second.permitSync, tableOpinionRecv.permitSync); EXPECT_EQ(it.second.requirePeerConvert, tableOpinionRecv.requirePeerConvert); } @@ -412,19 +413,19 @@ HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalSchemaOpinionTest001 HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalSchemaNegotiateTest001, TestSize.Level1) { RelationalSyncOpinion localOpinion; - localOpinion.AddSyncOpinion("table_1", SyncOpinion {true, false, false}); - localOpinion.AddSyncOpinion("table_2", SyncOpinion {false, true, false}); - localOpinion.AddSyncOpinion("table_3", SyncOpinion {false, false, true}); + localOpinion["table_1"] = SyncOpinion {true, false, false}; + localOpinion["table_2"] = SyncOpinion {false, true, false}; + localOpinion["table_3"] = SyncOpinion {false, false, true}; RelationalSyncOpinion remoteOpinion; - remoteOpinion.AddSyncOpinion("table_2", SyncOpinion {true, false, false}); - remoteOpinion.AddSyncOpinion("table_3", SyncOpinion {false, true, false}); - remoteOpinion.AddSyncOpinion("table_4", SyncOpinion {false, false, true}); - RelationalSyncStrategy strategy = RelationalSchemaObject::ConcludeSyncStrategy(localOpinion, remoteOpinion); - - EXPECT_EQ(strategy.GetStrategies().size(), 2); - EXPECT_EQ(strategy.GetTableStrategy("table_2").permitSync, true); - EXPECT_EQ(strategy.GetTableStrategy("table_3").permitSync, false); + remoteOpinion["table_2"] = SyncOpinion {true, false, false}; + remoteOpinion["table_3"] = SyncOpinion {false, true, false}; + remoteOpinion["table_4"] = SyncOpinion {false, false, true}; + RelationalSyncStrategy strategy = SchemaNegotiate::ConcludeSyncStrategy(localOpinion, remoteOpinion); + + EXPECT_EQ(strategy.size(), 2u); + EXPECT_EQ(strategy.at("table_2").permitSync, true); + EXPECT_EQ(strategy.at("table_3").permitSync, false); } /** -- Gitee From f1a868a175da06386edd3a9205faa0bcc23a7c4a Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 7 Mar 2022 10:50:22 +0800 Subject: [PATCH 30/37] Fix compile error Signed-off-by: lianhuix --- .../common/syncer/distributeddb_mock_sync_module_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index 8d29b05fb..972b6d764 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -419,7 +419,7 @@ HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync003, TestSize.Level1) ASSERT_NE(context, nullptr); RelationalSyncStrategy strategy; const std::string tableName = "TEST"; - strategy.AddSyncStrategy(tableName, {true, true, true}); + strategy[tableName] = {true, true, true}; context->SetRelationalSyncStrategy(strategy); QuerySyncObject query; query.SetTableName(tableName); -- Gitee From 9a988cd2ac0b455364606431dff5c19dd9af0b8b Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 7 Mar 2022 11:39:46 +0800 Subject: [PATCH 31/37] Fix code review Signed-off-by: lianhuix --- .../distributeddb/storage/src/sqlite/sqlite_utils.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index 368018710..179a5db43 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -930,20 +930,19 @@ int SQLiteUtils::GetJournalMode(sqlite3 *db, std::string &mode) std::string sql = "PRAGMA journal_mode;"; sqlite3_stmt *statement = nullptr; int errCode = SQLiteUtils::GetStatement(db, sql, statement); - if (errCode != SQLITE_OK || statement == nullptr) { - errCode = SQLiteUtils::MapSQLiteErrno(errCode); + if (errCode != E_OK || statement == nullptr) { return errCode; } - if (SQLiteUtils::StepWithRetry(statement) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + errCode = SQLiteUtils::StepWithRetry(statement); + if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { errCode = SQLiteUtils::GetColumnTextValue(statement, 0, mode); } else { LOGE("[SqlUtil][GetJournal] Get db journal_mode failed."); - errCode = SQLiteUtils::MapSQLiteErrno(SQLITE_ERROR); } SQLiteUtils::ResetStatement(statement, true, errCode); - return E_OK; + return errCode; } int SQLiteUtils::SetUserVer(const OpenDbProperties &properties, int version) -- Gitee From 1f0f10976170dabbdf3f8dc1ad6823f7ddefd4b6 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 7 Mar 2022 15:27:05 +0800 Subject: [PATCH 32/37] Fix reviewBot Signed-off-by: lianhuix --- .../common/include/json_object.h | 4 +-- .../distributeddb/common/src/json_object.cpp | 29 +++++++++++-------- .../common/src/platform_specific.cpp | 4 +-- .../common/src/time_tick_monitor.cpp | 4 +-- .../distributeddb_json_precheck_unit_test.cpp | 12 ++++++-- 5 files changed, 32 insertions(+), 21 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/include/json_object.h b/services/distributeddataservice/libs/distributeddb/common/include/json_object.h index ddad4bb79..3d79c31ac 100755 --- a/services/distributeddataservice/libs/distributeddb/common/include/json_object.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/json_object.h @@ -34,8 +34,8 @@ public: static uint32_t SetMaxNestDepth(uint32_t nestDepth); // Calculate nest depth when json string is legal or estimate depth by legal part from illegal json. - static uint32_t CalculateNestDepth(const std::string &inString); - static uint32_t CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd); + static uint32_t CalculateNestDepth(const std::string &inString, int &errCode); + static uint32_t CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd, int &errCode); // Support default constructor, copy constructor and copy assignment JsonObject() = default; diff --git a/services/distributeddataservice/libs/distributeddb/common/src/json_object.cpp b/services/distributeddataservice/libs/distributeddb/common/src/json_object.cpp index 595dde63a..9cfd5b8db 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/json_object.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/json_object.cpp @@ -41,17 +41,18 @@ uint32_t JsonObject::SetMaxNestDepth(uint32_t nestDepth) return preValue; } -uint32_t JsonObject::CalculateNestDepth(const std::string &inString) +uint32_t JsonObject::CalculateNestDepth(const std::string &inString, int &errCode) { auto begin = reinterpret_cast(inString.c_str()); auto end = begin + inString.size(); - return CalculateNestDepth(begin, end); + return CalculateNestDepth(begin, end, errCode); } -uint32_t JsonObject::CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd) +uint32_t JsonObject::CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd, int &errCode) { if (dataBegin == nullptr || dataEnd == nullptr || dataBegin >= dataEnd) { - return -E_INVALID_ARGS; + errCode = -E_INVALID_ARGS; + return maxNestDepth_ + 1; // return a invalid depth } bool isInString = false; uint32_t maxDepth = 0; @@ -111,9 +112,11 @@ int JsonObject::Parse(const std::string &inString) LOGE("[Json][Parse] Already Valid."); return -E_NOT_PERMIT; } - uint32_t nestDepth = CalculateNestDepth(inString); - if (nestDepth > maxNestDepth_) { - LOGE("[Json][Parse] Json nest depth=%u exceed max allowed=%u.", nestDepth, maxNestDepth_); + int errCode = E_OK; + uint32_t nestDepth = CalculateNestDepth(inString, errCode); + if (errCode != E_OK || nestDepth > maxNestDepth_) { + LOGE("[Json][Parse] Json calculate nest depth failed %d, depth=%u exceed max allowed=%u.", errCode, nestDepth, + maxNestDepth_); return -E_JSON_PARSE_FAIL; } #ifdef JSONCPP_USE_BUILDER @@ -165,9 +168,11 @@ int JsonObject::Parse(const uint8_t *dataBegin, const uint8_t *dataEnd) if (dataBegin == nullptr || dataEnd == nullptr || dataBegin >= dataEnd) { return -E_INVALID_ARGS; } - uint32_t nestDepth = CalculateNestDepth(dataBegin, dataEnd); - if (nestDepth > maxNestDepth_) { - LOGE("[Json][Parse] Json nest depth=%u exceed max allowed=%u.", nestDepth, maxNestDepth_); + int errCode = E_OK; + uint32_t nestDepth = CalculateNestDepth(dataBegin, dataEnd, errCode); + if (errCode != E_OK || nestDepth > maxNestDepth_) { + LOGE("[Json][Parse] Json calculate nest depth failed %d, depth=%u exceed max allowed=%u.", errCode, nestDepth, + maxNestDepth_); return -E_JSON_PARSE_FAIL; } #ifdef JSONCPP_USE_BUILDER @@ -784,12 +789,12 @@ uint32_t JsonObject::SetMaxNestDepth(uint32_t nestDepth) return 0; } -uint32_t JsonObject::CalculateNestDepth(const std::string &inString) +uint32_t JsonObject::CalculateNestDepth(const std::string &inString, int &errCode) { return 0; } -uint32_t JsonObject::CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd) +uint32_t JsonObject::CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd, int &errCode) { return 0; } diff --git a/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp b/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp index 608c4525b..01f32ea65 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp @@ -78,7 +78,7 @@ int CalFileSize(const std::string &fileUrl, uint64_t &size) return errCode; } - size = fileStat.st_size; + size = static_cast(fileStat.st_size); return E_OK; } @@ -221,7 +221,7 @@ static int GetFilePathAttr(const std::string &topPath, const std::string &relati if (isNeedAllPath) { file.fileName = fileAbsName; } - file.fileLen = fileStat.st_size; + file.fileLen = static_cast(fileStat.st_size); files.push_back(file); if (file.fileType == PATH) { errCode = GetFilePathAttr(fileAbsName, relativePath + fileDirInfo->d_name + "/", files, isNeedAllPath); diff --git a/services/distributeddataservice/libs/distributeddb/common/src/time_tick_monitor.cpp b/services/distributeddataservice/libs/distributeddb/common/src/time_tick_monitor.cpp index ff8df05c2..82a8a606a 100755 --- a/services/distributeddataservice/libs/distributeddb/common/src/time_tick_monitor.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/time_tick_monitor.cpp @@ -116,8 +116,8 @@ int TimeTickMonitor::TimeTick(TimerId timerId) uint64_t monotonicTime = GetMonotonicTime(); uint64_t systemTime = GetSysCurrentTime(); - int64_t monotonicOffset = monotonicTime - lastMonotonicTime_; - int64_t systemOffset = systemTime - lastSystemTime_; + int64_t monotonicOffset = static_cast(monotonicTime - lastMonotonicTime_); + int64_t systemOffset = static_cast(systemTime - lastSystemTime_); lastMonotonicTime_ = monotonicTime; lastSystemTime_ = systemTime; int64_t changedOffset = systemOffset - monotonicOffset; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp index 835a27d50..ffae09ca3 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp @@ -101,7 +101,9 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseValidString001, TestSize.Level1 * @tc.steps: step1. Check legal json string with nesting depth of 12. * @tc.expected: step1. return value = 12. */ - int stepOne = JsonObject::CalculateNestDepth(JSON_STRING1); + int errCode = E_OK; + int stepOne = JsonObject::CalculateNestDepth(JSON_STRING1, errCode); + EXPECT_TRUE(errCode == E_OK); EXPECT_TRUE(stepOne == STRING1_DEPTH); /** @@ -126,7 +128,9 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseValidString002, TestSize.Level1 * @tc.steps: step1. Check legal json string with nesting depth of 6. * @tc.expected: step1. return value = 6. */ - int stepOne = JsonObject::CalculateNestDepth(JSON_STRING3); + int errCode = E_OK; + int stepOne = JsonObject::CalculateNestDepth(JSON_STRING3, errCode); + EXPECT_TRUE(errCode == E_OK); EXPECT_TRUE(stepOne == STRING3_DEPTH); /** @@ -187,7 +191,9 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseInvalidString003, TestSize.Leve * @tc.steps: step1. Detect illegal json string with nesting depth greater than 10. * @tc.expected: step1. return value > 10. */ - int stepOne = JsonObject::CalculateNestDepth(JSON_STRING5); + int errCode = E_OK; + int stepOne = JsonObject::CalculateNestDepth(JSON_STRING5, errCode); + EXPECT_TRUE(errCode == E_OK); EXPECT_TRUE(stepOne > MAX_DEPTH_FOR_TEST); /** -- Gitee From 06a1aeaacb8d525361cb54a179cd4a3647e936d2 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 7 Mar 2022 20:00:06 +0800 Subject: [PATCH 33/37] Fix errcode log format Signed-off-by: lianhuix --- .../sqlite/sqlite_single_ver_relational_storage_executor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp index 76867db2f..7d85f0743 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp @@ -1184,7 +1184,7 @@ int SQLiteSingleVerRelationalStorageExecutor::CheckAndCleanDistributedTable(cons } missingTables.emplace_back(tableName); } else if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - LOGE("Check distributed table failed. %s", errCode); + LOGE("Check distributed table failed. %d", errCode); break; } errCode = E_OK; // Check result ok for distributed table is still exists -- Gitee From 3ff27b0938d6548556ae1fc5b8e29fa0e0d0a4c2 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 7 Mar 2022 21:32:20 +0800 Subject: [PATCH 34/37] Fix code reviews Signed-off-by: lianhuix --- .../common/include/auto_launch.h | 47 +++++++++---------- .../common/include/schema_negotiate.h | 5 +- .../common/src/schema_negotiate.cpp | 5 +- .../distributeddb/syncer/src/ability_sync.cpp | 4 +- .../common/distributeddb_auto_launch_test.cpp | 2 +- ...ibuteddb_relational_schema_object_test.cpp | 5 +- 6 files changed, 32 insertions(+), 36 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h b/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h index fa8f10c16..ea898c47c 100755 --- a/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/auto_launch.h @@ -64,6 +64,9 @@ struct AutoLaunchItem { class AutoLaunch { public: + static int GetAutoLaunchProperties(const AutoLaunchParam ¶m, const DBType &openType, + std::shared_ptr &propertiesPtr); + AutoLaunch() = default; virtual ~AutoLaunch(); @@ -82,21 +85,33 @@ public: void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback, DBType type); - static int GetAutoLaunchProperties(const AutoLaunchParam ¶m, const DBType &openType, - std::shared_ptr &propertiesPtr); - protected: + static int OpenOneConnection(AutoLaunchItem &autoLaunchItem); + + // we will return errCode, if errCode != E_OK + static int CloseConnectionStrict(AutoLaunchItem &autoLaunchItem); + + static void CloseNotifier(const AutoLaunchItem &autoLaunchItem); + + static int SetConflictNotifier(AutoLaunchItem &autoLaunchItem); + + static int GetAutoLaunchKVProperties(const AutoLaunchParam ¶m, + const std::shared_ptr &propertiesPtr); + + static int GetAutoLaunchRelationProperties(const AutoLaunchParam ¶m, + const std::shared_ptr &propertiesPtr); + + static int OpenKvConnection(AutoLaunchItem &autoLaunchItem); + + static int OpenRelationalConnection(AutoLaunchItem &autoLaunchItem); + + static int PragmaAutoSync(AutoLaunchItem &autoLaunchItem); int EnableKvStoreAutoLaunchParmCheck(AutoLaunchItem &autoLaunchItem, const std::string &normalIdentifier, const std::string &dualTupleIdentifier, bool isDualTupleMode); int GetKVConnectionInEnable(AutoLaunchItem &autoLaunchItem, const std::string &identifier); - static int OpenOneConnection(AutoLaunchItem &autoLaunchItem); - - // we will return errCode, if errCode != E_OK - static int CloseConnectionStrict(AutoLaunchItem &autoLaunchItem); - // before ReleaseDatabaseConnection, if errCode != E_OK, we not return, we try close more virtual void TryCloseConnection(AutoLaunchItem &autoLaunchItem); @@ -120,8 +135,6 @@ protected: void ReceiveUnknownIdentifierCallBackTask(const std::string &identifier, const std::string userId); - static void CloseNotifier(const AutoLaunchItem &autoLaunchItem); - void ConnectionLifeCycleCallback(const std::string &identifier, const std::string &userId); void OnlineCallBack(const std::string &device, bool isConnect); @@ -139,25 +152,11 @@ protected: void ExtConnectionLifeCycleCallbackTask(const std::string &identifier, const std::string &userId); - static int SetConflictNotifier(AutoLaunchItem &autoLaunchItem); - int ExtAutoLaunchRequestCallBack(const std::string &identifier, AutoLaunchParam ¶m, DBType &openType); - static int GetAutoLaunchKVProperties(const AutoLaunchParam ¶m, - const std::shared_ptr &propertiesPtr); - - static int GetAutoLaunchRelationProperties(const AutoLaunchParam ¶m, - const std::shared_ptr &propertiesPtr); - - static int OpenKvConnection(AutoLaunchItem &autoLaunchItem); - - static int OpenRelationalConnection(AutoLaunchItem &autoLaunchItem); - int RegisterLifeCycleCallback(AutoLaunchItem &autoLaunchItem, const std::string &identifier, bool isExt); - static int PragmaAutoSync(AutoLaunchItem &autoLaunchItem); - void TryCloseKvConnection(AutoLaunchItem &autoLaunchItem); void TryCloseRelationConnection(AutoLaunchItem &autoLaunchItem); diff --git a/services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h b/services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h index 302171bf6..cddb6d6c8 100644 --- a/services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h +++ b/services/distributeddataservice/libs/distributeddb/common/include/schema_negotiate.h @@ -19,7 +19,6 @@ #include "relational_schema_object.h" namespace DistributedDB { - struct SyncOpinion { bool permitSync = false; bool requirePeerConvert = false; @@ -52,8 +51,8 @@ public: static RelationalSyncStrategy ConcludeSyncStrategy(const RelationalSyncOpinion &localOpinion, const RelationalSyncOpinion &remoteOpinion); - static uint32_t CalculateParcelLen(const RelationalSyncOpinion &opinions, uint32_t softWareVersion); - static int SerializeData(const RelationalSyncOpinion &opinions, Parcel &parcel, uint32_t softWareVersion); + static uint32_t CalculateParcelLen(const RelationalSyncOpinion &opinions); + static int SerializeData(const RelationalSyncOpinion &opinions, Parcel &parcel); static int DeserializeData(Parcel &parcel, RelationalSyncOpinion &opinion); private: diff --git a/services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp b/services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp index 564ef31cc..3bd78a2e4 100644 --- a/services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp +++ b/services/distributeddataservice/libs/distributeddb/common/src/schema_negotiate.cpp @@ -172,7 +172,7 @@ namespace { } // namespace -uint32_t SchemaNegotiate::CalculateParcelLen(const RelationalSyncOpinion &opinions, uint32_t softWareVersion) +uint32_t SchemaNegotiate::CalculateParcelLen(const RelationalSyncOpinion &opinions) { uint64_t len = Parcel::GetStringLen(MAGIC); len += Parcel::GetUInt32Len(); @@ -190,7 +190,7 @@ uint32_t SchemaNegotiate::CalculateParcelLen(const RelationalSyncOpinion &opinio return static_cast(len); } -int SchemaNegotiate::SerializeData(const RelationalSyncOpinion &opinions, Parcel &parcel, uint32_t softWareVersion) +int SchemaNegotiate::SerializeData(const RelationalSyncOpinion &opinions, Parcel &parcel) { (void)parcel.WriteString(MAGIC); (void)parcel.WriteUInt32(SYNC_OPINION_VERSION); @@ -240,5 +240,4 @@ int SchemaNegotiate::DeserializeData(Parcel &parcel, RelationalSyncOpinion &opin } return parcel.IsError() ? -E_INVALID_ARGS : E_OK; } - } \ No newline at end of file diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp index 8b621bc25..bce417a8e 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/ability_sync.cpp @@ -297,7 +297,7 @@ uint32_t AbilitySyncAckPacket::CalculateLen() const len += Parcel::GetUInt32Len(); // requirePeerConvert_ len += Parcel::GetUInt64Len(); // dbCreateTime_ len += DbAbility::CalculateLen(dbAbility_); // dbAbility_ - len += SchemaNegotiate::CalculateParcelLen(relationalSyncOpinion_, softwareVersion_); + len += SchemaNegotiate::CalculateParcelLen(relationalSyncOpinion_); if (len > INT32_MAX) { LOGE("[AbilitySyncAckPacket][CalculateLen] err len:%llu", len); return 0; @@ -709,7 +709,7 @@ int AbilitySync::AckPacketSerialization(uint8_t *buffer, uint32_t length, const if (parcel.IsError() || errCode != E_OK) { return -E_PARSE_FAIL; } - errCode = SchemaNegotiate::SerializeData(packet->GetRelationalSyncOpinion(), parcel, SOFTWARE_VERSION_CURRENT); + errCode = SchemaNegotiate::SerializeData(packet->GetRelationalSyncOpinion(), parcel); if (parcel.IsError() || errCode != E_OK) { return -E_PARSE_FAIL; } diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp index 64da6511c..2a355eb7e 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_auto_launch_test.cpp @@ -596,7 +596,7 @@ HWTEST_F(DistributedDBAutoLaunchUnitTest, AutoLaunch006, TestSize.Level3) LOGD("AutoLaunch006 disable i:%d", i); } std::unique_lock lock(cvLock); - cv.wait(lock, [&threadIsWorking] { return threadIsWorking == false; }); + cv.wait(lock, [&threadIsWorking] { return !threadIsWorking; }); delete observer; g_communicatorAggregator->RunOnConnectCallback(REMOTE_DEVICE_ID, false); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp index 3393ec54b..c26c2943c 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp @@ -378,16 +378,15 @@ HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalTableCompareTest001, */ HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalSchemaOpinionTest001, TestSize.Level1) { - const uint32_t softWareVersion = 106; RelationalSyncOpinion opinion; opinion["table_1"] = SyncOpinion {true, false, false}; opinion["table_2"] = SyncOpinion {false, true, false}; opinion["table_3"] = SyncOpinion {false, false, true}; - uint32_t len = SchemaNegotiate::CalculateParcelLen(opinion, softWareVersion); + uint32_t len = SchemaNegotiate::CalculateParcelLen(opinion); std::vector buff(len, 0); Parcel writeParcel(buff.data(), len); - int errCode = SchemaNegotiate::SerializeData(opinion, writeParcel, softWareVersion); + int errCode = SchemaNegotiate::SerializeData(opinion, writeParcel); EXPECT_EQ(errCode, E_OK); Parcel readParcel(buff.data(), len); -- Gitee From 1e86721bbc192d2578b401aa53ae93e91e999e69 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 7 Mar 2022 21:32:20 +0800 Subject: [PATCH 35/37] Fix code reviews Signed-off-by: lianhuix --- .../libs/distributeddb/storage/src/sync_able_kvdb.cpp | 10 +++++----- .../libs/distributeddb/storage/src/sync_able_kvdb.h | 4 ++-- .../libs/distributeddb/syncer/include/isyncer.h | 2 +- .../libs/distributeddb/syncer/include/syncer_proxy.h | 2 +- .../libs/distributeddb/syncer/src/generic_syncer.cpp | 10 +++++----- .../libs/distributeddb/syncer/src/generic_syncer.h | 6 +++--- .../libs/distributeddb/syncer/src/syncer_proxy.cpp | 4 ++-- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp index 1ea84ce14..ee568c06f 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -190,20 +190,20 @@ int SyncAbleKvDB::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActiv } // Stop syncer -void SyncAbleKvDB::StopSyncer(bool isClosed) +void SyncAbleKvDB::StopSyncer(bool isClosedOperation) { std::unique_lock lock(syncerOperateLock_); - StopSyncerWithNoLock(isClosed); + StopSyncerWithNoLock(isClosedOperation); } -void SyncAbleKvDB::StopSyncerWithNoLock(bool isClosed) +void SyncAbleKvDB::StopSyncerWithNoLock(bool isClosedOperation) { ReSetSyncModuleActive(); - syncer_.Close(isClosed); + syncer_.Close(isClosedOperation); if (started_) { started_ = false; } - closed_ = isClosed; + closed_ = isClosedOperation; if (userChangeListerner_ != nullptr) { userChangeListerner_->Drop(false); userChangeListerner_ = nullptr; diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h index cba41238f..06912bafe 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.h @@ -97,9 +97,9 @@ protected: int StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive); // Stop syncer - void StopSyncer(bool isClosed = false); + void StopSyncer(bool isClosedOperation = false); - void StopSyncerWithNoLock(bool isClosed = false); + void StopSyncerWithNoLock(bool isClosedOperation = false); void UserChangeHandle(); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h b/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h index 7cd036b24..fd3c3aba0 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/include/isyncer.h @@ -48,7 +48,7 @@ public: } // Close - virtual int Close(bool isClosed) = 0; + virtual int Close(bool isClosedOperation) = 0; // Sync function. // param devices: The device id list. diff --git a/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h b/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h index 4e1ece88e..7bf90555d 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/include/syncer_proxy.h @@ -33,7 +33,7 @@ public: int Initialize(ISyncInterface *syncInterface, bool isNeedActive) override; // Close the syncer - int Close(bool isClosed) override; + int Close(bool isClosedOperation) override; // Sync function. // param devices: The device id list. diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp index 07e969805..1b36af62c 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.cpp @@ -122,7 +122,7 @@ int GenericSyncer::Initialize(ISyncInterface *syncInterface, bool isNeedActive) return E_OK; } -int GenericSyncer::Close(bool isClosed) +int GenericSyncer::Close(bool isClosedOperation) { { std::lock_guard lock(syncerLock_); @@ -139,7 +139,7 @@ int GenericSyncer::Close(bool isClosed) } closing_ = true; } - ClearSyncOperations(isClosed); + ClearSyncOperations(isClosedOperation); if (syncEngine_ != nullptr) { syncEngine_->Close(); LOGD("[Syncer] Close SyncEngine!"); @@ -425,15 +425,15 @@ bool GenericSyncer::IsValidDevices(const std::vector &devices) cons return true; } -void GenericSyncer::ClearSyncOperations(bool isClosed) +void GenericSyncer::ClearSyncOperations(bool isClosedOperation) { std::vector syncOperation; { std::lock_guard lock(operationMapLock_); for (auto &item : syncOperationMap_) { bool isBlockSync = item.second->IsBlockSync(); - if (isBlockSync || !isClosed) { - int status = (!isClosed) ? SyncOperation::OP_USER_CHANGED : SyncOperation::OP_FAILED; + if (isBlockSync || !isClosedOperation) { + int status = (!isClosedOperation) ? SyncOperation::OP_USER_CHANGED : SyncOperation::OP_FAILED; item.second->SetUnfinishedDevStatus(status); RefObject::IncObjRef(item.second); syncOperation.push_back(item.second); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h index 2f282e298..1077497a4 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/generic_syncer.h @@ -38,7 +38,7 @@ public: int Initialize(ISyncInterface *syncInterface, bool isNeedActive) override; // Close - int Close(bool isClosed) override; + int Close(bool isClosedOperation) override; // Sync function. // param devices: The device id list. @@ -139,8 +139,8 @@ protected: bool IsValidDevices(const std::vector &devices) const; // Used Clear all SyncOperations. - // isClosed is false while userChanged - void ClearSyncOperations(bool isClosed); + // isClosedOperation is false while userChanged + void ClearSyncOperations(bool isClosedOperation); void TriggerSyncFinished(SyncOperation *operation); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp index 012c57c66..71a9fbe24 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/syncer_proxy.cpp @@ -46,12 +46,12 @@ int SyncerProxy::Initialize(ISyncInterface *syncInterface, bool isNeedActive) return syncer_->Initialize(syncInterface, isNeedActive); } -int SyncerProxy::Close(bool isClosed) +int SyncerProxy::Close(bool isClosedOperation) { if (syncer_ == nullptr) { return -E_NOT_INIT; } - return syncer_->Close(isClosed); + return syncer_->Close(isClosedOperation); } int SyncerProxy::Sync(const std::vector &devices, int mode, -- Gitee From a06e029fdb68a73702727a92475b4ae7f56b890c Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Mon, 7 Mar 2022 15:29:06 +0800 Subject: [PATCH 36/37] fix review comments Signed-off-by: zwtmichael --- .../communicator/include/icommunicator.h | 8 ++++--- .../storage/src/sync_able_kvdb.cpp | 2 +- .../syncer/src/commit_history_sync.cpp | 2 +- .../syncer/src/device_manager.cpp | 2 +- .../syncer/src/multi_ver_data_sync.cpp | 2 +- .../distributeddb/syncer/src/sync_types.h | 1 - .../distributeddb_communicator_deep_test.cpp | 8 +++---- ...buteddb_communicator_send_receive_test.cpp | 22 +++++++++---------- .../distributeddb_communicator_test.cpp | 6 ++--- .../distributeddb_communicator_proxy_test.cpp | 2 +- .../syncer/distributeddb_time_sync_test.cpp | 4 ++-- 11 files changed, 30 insertions(+), 29 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/communicator/include/icommunicator.h b/services/distributeddataservice/libs/distributeddb/communicator/include/icommunicator.h index 9d23e31c1..84fbed366 100644 --- a/services/distributeddataservice/libs/distributeddb/communicator/include/icommunicator.h +++ b/services/distributeddataservice/libs/distributeddb/communicator/include/icommunicator.h @@ -26,11 +26,13 @@ namespace DistributedDB { // inMsg is heap memory, its ownership transfers by calling OnMessageCallback using OnMessageCallback = std::function; +constexpr uint32_t SEND_TIME_OUT = 3000; // 3s + struct SendConfig { - bool nonBlock; - uint32_t timeout; - ExtendInfo paramInfo; + bool nonBlock = false; bool isNeedExtendHead = false; + uint32_t timeout = SEND_TIME_OUT; + ExtendInfo paramInfo; }; class ICommunicator : public virtual RefObject { diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp index ee568c06f..f833394ec 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -181,7 +181,7 @@ int SyncAbleKvDB::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActiv userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( std::bind(&SyncAbleKvDB::ChangeUserListerner, this), UserChangeMonitor::USER_ACTIVE_TO_NON_ACTIVE_EVENT); } else if (isSyncDualTupleMode && (userChangeListerner_ == nullptr)) { - EventType event = started_ ? + EventType event = isNeedActive ? UserChangeMonitor::USER_ACTIVE_EVENT : UserChangeMonitor::USER_NON_ACTIVE_EVENT; userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( std::bind(&SyncAbleKvDB::UserChangeHandle, this), event); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/commit_history_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/commit_history_sync.cpp index b8c0ad86e..7ea01a854 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/commit_history_sync.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/commit_history_sync.cpp @@ -536,7 +536,7 @@ bool CommitHistorySync::IsPacketValid(const Message *inMsg, uint16_t messageType int CommitHistorySync::Send(const DeviceID &deviceId, const Message *inMsg) { - SendConfig conf = {false, SEND_TIME_OUT}; + SendConfig conf = {false, false, SEND_TIME_OUT}; int errCode = communicateHandle_->SendMessage(deviceId, inMsg, conf); if (errCode != E_OK) { LOGE("CommitHistorySync::Send ERR! err = %d", errCode); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/device_manager.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/device_manager.cpp index 46ee72b12..50057c3ff 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/device_manager.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/device_manager.cpp @@ -138,7 +138,7 @@ int DeviceManager::SendLocalDataChanged() } msg->SetMessageId(LOCAL_DATA_CHANGED); msg->SetTarget(deviceId); - SendConfig conf = {false, SEND_TIME_OUT}; + SendConfig conf = {false, false, SEND_TIME_OUT}; int errCode = communicator_->SendMessage(deviceId, msg, conf); if (errCode != E_OK) { LOGE("[DeviceManager] SendLocalDataChanged to dev %s{private} failed. err %d", diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_data_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_data_sync.cpp index b08fe0da9..49c8cac8a 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_data_sync.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/multi_ver_data_sync.cpp @@ -576,7 +576,7 @@ bool MultiVerDataSync::IsCommitExisted(const MultiVerCommitNode &commit) int MultiVerDataSync::Send(const DeviceID &deviceId, const Message *inMsg) { - SendConfig conf = {false, SEND_TIME_OUT}; + SendConfig conf = {false, false, SEND_TIME_OUT}; int errCode = communicateHandle_->SendMessage(deviceId, inMsg, conf); if (errCode != E_OK) { LOGE("MultiVerDataSync::Send ERR! ERR = %d", errCode); diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_types.h b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_types.h index ab36e40b2..3af5663de 100644 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/sync_types.h +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/sync_types.h @@ -75,7 +75,6 @@ struct InternalSyncParma { QuerySyncObject syncQuery; }; -constexpr uint32_t SEND_TIME_OUT = 3000; // 3s constexpr int NOT_SURPPORT_SEC_CLASSIFICATION = 0xff; constexpr uint8_t QUERY_SYNC_MODE_BASE = SyncModeType::QUERY_PUSH; constexpr int AUTO_RETRY_TIMES = 3; diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp index 74b71a6f1..5bf315556 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_deep_test.cpp @@ -161,7 +161,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, WaitAndRetrySend001, TestSize.Level2 */ Message *msgForAB = BuildRegedTinyMessage(); ASSERT_NE(msgForAB, nullptr); - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; int errCode = g_commAB->SendMessage(DEVICE_NAME_B, msgForAB, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Wait 100 ms @@ -328,7 +328,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, Fragment001, TestSize.Level2) const uint32_t dataLength = 13 * 1024 * 1024; // 13 MB, 1024 is scale Message *sendMsgForAB = BuildRegedGiantMessage(dataLength); ASSERT_NE(sendMsgForAB, nullptr); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; int errCode = g_commAB->SendMessage(DEVICE_NAME_B, sendMsgForAB, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(2600)); // Wait 2600 ms to make sure send done @@ -389,7 +389,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, Fragment002, TestSize.Level2) uint32_t dataLength = 13 * 1024 * 1024; // 13 MB, 1024 is scale Message *sendMsgForBC = BuildRegedGiantMessage(dataLength); ASSERT_NE(sendMsgForBC, nullptr); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; int errCode = g_commBC->SendMessage(DEVICE_NAME_C, sendMsgForBC, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(2600)); // Wait 2600 ms to make sure send done @@ -460,7 +460,7 @@ HWTEST_F(DistributedDBCommunicatorDeepTest, Fragment003, TestSize.Level3) uint32_t dataLength = 23 * 1024 * 1024; // 23 MB, 1024 is scale Message *sendMsgForAB = BuildRegedGiantMessage(dataLength); ASSERT_NE(sendMsgForAB, nullptr); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; int errCode = g_commAB->SendMessage(DEVICE_NAME_B, sendMsgForAB, conf); EXPECT_EQ(errCode, E_OK); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp index 7ddb34706..ea0d11a23 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_send_receive_test.cpp @@ -158,7 +158,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendAndReceive001, TestSize.L */ Message *msgForAA = BuildRegedTinyMessage(); ASSERT_NE(msgForAA, nullptr); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; int errCode = g_commAA->SendMessage(DEVICE_NAME_B, msgForAA, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(200)); // sleep 200 ms @@ -209,7 +209,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendAndReceive002, TestSize.L */ Message *msgForAA = BuildRegedOverSizeMessage(); ASSERT_NE(msgForAA, nullptr); - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; int errCode = g_commAA->SendMessage(DEVICE_NAME_B, msgForAA, conf); EXPECT_NE(errCode, E_OK); delete msgForAA; @@ -239,7 +239,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendAndReceive003, TestSize.L */ Message *msgForAA = BuildUnRegedTinyMessage(); ASSERT_NE(msgForAA, nullptr); - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; int errCode = g_commAA->SendMessage(DEVICE_NAME_B, msgForAA, conf); EXPECT_NE(errCode, E_OK); delete msgForAA; @@ -286,7 +286,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl001, TestSize. while (true) { Message *msgForBA = BuildRegedHugeMessage(); ASSERT_NE(msgForBA, nullptr); - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; int errCode = g_commBA->SendMessage(DEVICE_NAME_A, msgForBA, conf); if (errCode == E_OK) { sendCount++; @@ -351,7 +351,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl002, TestSize. while (sendCount < SEND_COUNT_GOAL) { Message *msgForBA = BuildRegedHugeMessage(); ASSERT_NE(msgForBA, nullptr); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; int errCode = g_commBA->SendMessage(DEVICE_NAME_A, msgForBA, conf); if (errCode != E_OK) { delete msgForBA; @@ -417,7 +417,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendFlowControl003, TestSize. while (sendCnt < SEND_COUNT_GOAL) { Message *msgForBA = BuildRegedHugeMessage(); ASSERT_NE(msgForBA, nullptr); - SendConfig conf = {false, 100}; + SendConfig conf = {false, false, 100}; int errCode = g_commBA->SendMessage(DEVICE_NAME_A, msgForBA, conf); // 100 ms timeout if (errCode != E_OK) { delete msgForBA; @@ -471,7 +471,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, ReceiveCheck001, TestSize.Lev */ g_envDeviceB.adapterHandle->SimulateSendBitErrorInMagicField(true, 0xFFFF); Message *msgForBA = BuildRegedTinyMessage(); - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; int errCode = g_commBA->SendMessage(DEVICE_NAME_A, msgForBA, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); @@ -532,7 +532,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, ReceiveCheck002, TestSize.Lev */ g_envDeviceB.adapterHandle->SimulateSendBitErrorInPacketLenField(true, 0xFFFF); Message *msgForBA = BuildRegedTinyMessage(); - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; int errCode = g_commBA->SendMessage(DEVICE_NAME_A, msgForBA, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); @@ -593,7 +593,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendResultNotify001, TestSize */ Message *msgForAA = BuildRegedTinyMessage(); ASSERT_NE(msgForAA, nullptr); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; int errCode = g_commAA->SendMessage(DEVICE_NAME_B, msgForAA, conf, sendResultNotifier); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms @@ -653,7 +653,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, MessageFeedback001, TestSize. */ Message *msgForBB = BuildRegedTinyMessage(); ASSERT_NE(msgForBB, nullptr); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; int errCode = g_commBB->SendMessage(DEVICE_NAME_A, msgForBB, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms @@ -734,7 +734,7 @@ HWTEST_F(DistributedDBCommunicatorSendReceiveTest, SendAndReceiveWithExtendHead0 */ Message *msgForAA = BuildAppLayerFrameMessage(); ASSERT_NE(msgForAA, nullptr); - SendConfig conf = {false, 0, {"appId", "storeId", "userId", "deviceB"}, true}; + SendConfig conf = {false, true, 0, {"appId", "storeId", "userId", "deviceB"}}; int errCode = g_commAA->SendMessage(DEVICE_NAME_B, msgForAA, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(200)); // sleep 200 ms diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp index ef5851c51..ab7e1b016 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp @@ -467,7 +467,7 @@ HWTEST_F(DistributedDBCommunicatorTest, ReportCommunicatorNotFound001, TestSize. ASSERT_NOT_NULL_AND_ACTIVATE(commAA); Message *msgForAA = BuildRegedTinyMessage(); ASSERT_NE(msgForAA, nullptr); - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; errCode = commAA->SendMessage(DEVICE_NAME_B, msgForAA, conf); EXPECT_EQ(errCode, E_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep 100 ms @@ -500,7 +500,7 @@ HWTEST_F(DistributedDBCommunicatorTest, ReportCommunicatorNotFound001, TestSize. Message *msgFor##src##label = BuildRegedTinyMessage(); \ ASSERT_NE(msgFor##src##label, nullptr); \ msgFor##src##label->SetSessionId(session); \ - SendConfig conf = {true, 0}; \ + SendConfig conf = {true, false, 0}; \ errCode = comm##src##label->SendMessage(DEVICE_NAME_##dst, msgFor##src##label, conf); \ EXPECT_EQ(errCode, E_OK); \ } @@ -509,7 +509,7 @@ HWTEST_F(DistributedDBCommunicatorTest, ReportCommunicatorNotFound001, TestSize. { \ Message *msgFor##src##label = BuildRegedGiantMessage(size); \ ASSERT_NE(msgFor##src##label, nullptr); \ - SendConfig conf = {false, 0}; \ + SendConfig conf = {false, false, 0}; \ errCode = comm##src##label->SendMessage(DEVICE_NAME_##dst, msgFor##src##label, conf); \ EXPECT_EQ(errCode, E_OK); \ } diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp index 8f0df0575..609a9e00c 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_communicator_proxy_test.cpp @@ -310,7 +310,7 @@ HWTEST_F(DistributedDBCommunicatorProxyTest, SendMessage001, TestSize.Level1) * @tc.steps: step1. Call SendMessage from CommProxy with param DEVICE_B. * @tc.expected: step1. MainComm's SendMessage willed called and return E_OK. */ - SendConfig conf = {true, 0}; + SendConfig conf = {true, false, 0}; EXPECT_CALL(mainComm_, SendMessage(DEVICE_B, _, _, _)).WillOnce(Return(E_OK)); EXPECT_EQ(commProxy_->SendMessage(DEVICE_B, nullptr, conf, nullptr), E_OK); diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_time_sync_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_time_sync_test.cpp index 42bec439f..293d95c04 100755 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_time_sync_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer/distributeddb_time_sync_test.cpp @@ -346,7 +346,7 @@ HWTEST_F(DistributedDBTimeSyncTest, InvalidMessgeTest001, TestSize.Level0) */ msg->SetMessageId(TIME_SYNC_MESSAGE); msg->SetMessageType(TYPE_REQUEST); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; errCode = g_virtualCommunicator->SendMessage(DEVICE_B, msg, conf); EXPECT_TRUE(errCode == -E_INVALID_ARGS); @@ -417,7 +417,7 @@ HWTEST_F(DistributedDBTimeSyncTest, InvalidMessgeTest002, TestSize.Level0) */ msg->SetMessageId(TIME_SYNC_MESSAGE); msg->SetMessageType(TYPE_RESPONSE); - SendConfig conf = {false, 0}; + SendConfig conf = {false, false, 0}; errCode = g_virtualCommunicator->SendMessage(DEVICE_A, msg, conf); EXPECT_TRUE(errCode == -E_INVALID_ARGS); -- Gitee From f30c796ac9eb7e05afc0fff3e894aff6a093ce9f Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Tue, 8 Mar 2022 20:27:34 +0800 Subject: [PATCH 37/37] fix reviewBot Signed-off-by: zwtmichael --- .../libs/distributeddb/syncer/src/value_slice_sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/value_slice_sync.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/value_slice_sync.cpp index 7582cd3d9..f6e9ecce9 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/value_slice_sync.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/value_slice_sync.cpp @@ -521,7 +521,7 @@ int ValueSliceSync::GetValidValueSliceHashNode(MultiVerSyncTaskContext *context, int ValueSliceSync::Send(const DeviceID &deviceId, const Message *inMsg) { - SendConfig conf = {false, SEND_TIME_OUT}; + SendConfig conf = {false, false, SEND_TIME_OUT}; int errCode = communicateHandle_->SendMessage(deviceId, inMsg, conf); if (errCode != E_OK) { LOGE("ValueSliceSync::Send ERR! err = %d", errCode); -- Gitee