From bd5b1636d09b7b4fb9beba951981b0c9bf385f59 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Fri, 23 Sep 2022 14:34:44 +0800 Subject: [PATCH 01/14] Fix analysis for autoinc constrant Signed-off-by: lianhuix --- .../common/src/relational/table_info.cpp | 2 +- ...uteddb_relational_schema_analysis_test.cpp | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/frameworks/libs/distributeddb/common/src/relational/table_info.cpp b/frameworks/libs/distributeddb/common/src/relational/table_info.cpp index 9c5841073ac..ca82ea7a610 100644 --- a/frameworks/libs/distributeddb/common/src/relational/table_info.cpp +++ b/frameworks/libs/distributeddb/common/src/relational/table_info.cpp @@ -217,7 +217,7 @@ void TableInfo::SetCreateTableSql(const std::string &sql) for (auto &c : sql_) { c = static_cast(std::toupper(c)); } - if (DBCommon::HasConstraint(DBCommon::TrimSpace(sql_), "AUTOINCREMENT", " ", " ,")) { + if (DBCommon::HasConstraint(DBCommon::TrimSpace(sql_), "AUTOINCREMENT", " ", " ,)")) { autoInc_ = true; } } diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_schema_analysis_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_schema_analysis_test.cpp index 23ad62a6048..216561c8754 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_schema_analysis_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_schema_analysis_test.cpp @@ -145,4 +145,35 @@ HWTEST_F(DistributedDBRelationalSchemaAnalysisTest, AnalysisTable003, TestSize.L TableInfo table = AnalysisTable("t1", NO_PRIMARI_KEY_SQL); EXPECT_EQ(table.GetPrimaryKey().size(), 1u); } + +/** + * @tc.name: AnalysisTable003 + * @tc.desc: Analysis with table which has no primary key + * @tc.type: FUNC + * @tc.require: AR000H2PKN + * @tc.author: xulianhui + */ +HWTEST_F(DistributedDBRelationalSchemaAnalysisTest, AnalysisTable004, TestSize.Level1) +{ + TableInfo table = AnalysisTable("t1", "create table t1(a integer primary key autoincrement, b integer);"); + EXPECT_EQ(table.GetPrimaryKey().size(), 1u); + EXPECT_EQ(table.GetAutoIncrement(), true); + table = AnalysisTable("t2", "create table t2(a integer primary key autoincrement , b integer);"); + EXPECT_EQ(table.GetPrimaryKey().size(), 1u); + EXPECT_EQ(table.GetAutoIncrement(), true); + table = AnalysisTable("t3", "create table t3(a integer, b integer primary key autoincrement);"); + EXPECT_EQ(table.GetPrimaryKey().size(), 1u); + EXPECT_EQ(table.GetAutoIncrement(), true); + table = AnalysisTable("t4", "create table t4(a integer, b integer primary key autoincrement );"); + EXPECT_EQ(table.GetPrimaryKey().size(), 1u); + EXPECT_EQ(table.GetAutoIncrement(), true); + + table = AnalysisTable("t5", "create table t5(a_autoincrement integer, b integer primary key);"); + EXPECT_EQ(table.GetPrimaryKey().size(), 1u); + EXPECT_EQ(table.GetAutoIncrement(), false); + + table = AnalysisTable("t6_autoincrement", "create table t6_autoincrement(a integer, b integer primary key);"); + EXPECT_EQ(table.GetPrimaryKey().size(), 1u); + EXPECT_EQ(table.GetAutoIncrement(), false); +} #endif // RELATIONAL_STORE \ No newline at end of file -- Gitee From e906a581d82714871be839566875621b05ba5839 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Sat, 17 Sep 2022 17:45:29 +0800 Subject: [PATCH 02/14] Fix sqlite execute sql with prepare-step Signed-off-by: lianhuix --- .../distributeddb/common/include/db_common.h | 2 + .../distributeddb/common/src/db_common.cpp | 8 +++ .../common/src/param_check_utils.cpp | 6 +- .../relational/relational_schema_object.cpp | 9 +++ .../collaboration_log_table_manager.cpp | 9 +-- .../sqlite/collaboration_log_table_manager.h | 2 +- .../src/sqlite/sqlite_log_table_manager.cpp | 35 +++++++---- .../src/sqlite/sqlite_log_table_manager.h | 2 +- .../sqlite/sqlite_multi_ver_data_storage.cpp | 8 ++- .../sqlite/sqlite_multi_ver_transaction.cpp | 12 +++- .../src/sqlite/sqlite_multi_ver_transaction.h | 2 + .../sqlite_single_ver_database_upgrader.cpp | 24 ++++++-- .../storage/src/sqlite/sqlite_utils.cpp | 58 +++++++++++-------- ...ibuteddb_relational_schema_object_test.cpp | 17 ++++++ ...stributeddb_interfaces_relational_test.cpp | 5 ++ 15 files changed, 143 insertions(+), 56 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/db_common.h b/frameworks/libs/distributeddb/common/include/db_common.h index 225fffcf77e..aca9546ebd1 100644 --- a/frameworks/libs/distributeddb/common/include/db_common.h +++ b/frameworks/libs/distributeddb/common/include/db_common.h @@ -67,6 +67,8 @@ public: const std::string &nextPattern); static bool IsSameCipher(CipherType srcType, CipherType inputType); + + static bool CheckIsAlnumAndUnderscore(const std::string &text); }; // Define short macro substitute for original long expression for convenience of using diff --git a/frameworks/libs/distributeddb/common/src/db_common.cpp b/frameworks/libs/distributeddb/common/src/db_common.cpp index a3459ec3f1c..9b51158ddca 100644 --- a/frameworks/libs/distributeddb/common/src/db_common.cpp +++ b/frameworks/libs/distributeddb/common/src/db_common.cpp @@ -395,4 +395,12 @@ bool DBCommon::IsSameCipher(CipherType srcType, CipherType inputType) } return false; } + +bool DBCommon::CheckIsAlnumAndUnderscore(const std::string &text) +{ + auto iter = std::find_if_not(text.begin(), text.end(), [](char c) { + return (std::isalnum(c) || c == '_'); + }); + return iter == text.end(); +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/common/src/param_check_utils.cpp b/frameworks/libs/distributeddb/common/src/param_check_utils.cpp index e7a94aac223..1cb41e1de5d 100644 --- a/frameworks/libs/distributeddb/common/src/param_check_utils.cpp +++ b/frameworks/libs/distributeddb/common/src/param_check_utils.cpp @@ -15,6 +15,7 @@ #include "param_check_utils.h" +#include "db_common.h" #include "db_errno.h" #include "platform_specific.h" #include "log_print.h" @@ -199,10 +200,7 @@ uint8_t ParamCheckUtils::GetValidCompressionRate(uint8_t compressionRate) bool ParamCheckUtils::CheckRelationalTableName(const std::string &tableName) { - auto iter = std::find_if_not(tableName.begin(), tableName.end(), [](char c) { - return (std::isalnum(c) || c == '_'); - }); - if (iter != tableName.end()) { + if (!DBCommon::CheckIsAlnumAndUnderscore(tableName)) { return false; } return tableName.compare(0, DBConstant::SYSTEM_TABLE_PREFIX.size(), DBConstant::SYSTEM_TABLE_PREFIX) != 0; diff --git a/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp b/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp index ba8b1c976e5..fa8936ca516 100644 --- a/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp +++ b/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp @@ -353,6 +353,10 @@ int RelationalSchemaObject::ParseCheckTableName(const JsonObject &inJsonObject, int errCode = GetMemberFromJsonObject(inJsonObject, "NAME", FieldType::LEAF_FIELD_STRING, true, fieldValue); if (errCode == E_OK) { + if (!DBCommon::CheckIsAlnumAndUnderscore(fieldValue.stringValue)) { + LOGE("[RelationalSchema][Parse] Invalid characters in table name, err=%d.", errCode); + return -E_SCHEMA_PARSE_FAIL; + } resultTable.SetTableName(fieldValue.stringValue); } return errCode; @@ -381,6 +385,11 @@ int RelationalSchemaObject::ParseCheckTableDefine(const JsonObject &inJsonObject return errCode; } + if (!DBCommon::CheckIsAlnumAndUnderscore(field.first[1])) { + LOGE("[RelationalSchema][Parse] Invalid characters in field name, err=%d.", errCode); + return -E_SCHEMA_PARSE_FAIL; + } + FieldInfo fieldInfo; fieldInfo.SetFieldName(field.first[1]); // 1 : table name element in path errCode = ParseCheckTableFieldInfo(fieldObj, field.first, fieldInfo); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp index 3aadddf38f4..09beed32c88 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp @@ -111,10 +111,11 @@ std::string CollaborationLogTableManager::GetPrimaryKeySql(const TableInfo &tabl return "PRIMARY KEY(hash_key)"; } -std::string CollaborationLogTableManager::GetIndexSql(const TableInfo &table) +void CollaborationLogTableManager::GetIndexSql(const TableInfo &table, std::vector &schema) { - return SqliteLogTableManager::GetIndexSql(table) + - "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX + "datakey_index ON " + GetLogTableName(table) + - "(data_key);"; + SqliteLogTableManager::GetIndexSql(table, schema); + std::string dataKeyIndex = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX + "datakey_index ON " + + GetLogTableName(table) + "(data_key);"; + schema.emplace_back(dataKeyIndex); } } \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.h b/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.h index dc3d3f5514c..c5bea74f1a4 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.h @@ -30,7 +30,7 @@ private: bool IsCollaborationWithoutKey(const TableInfo &table); std::string GetPrimaryKeySql(const TableInfo &table) override; - std::string GetIndexSql(const TableInfo &table) override; + void GetIndexSql(const TableInfo &table, std::vector &schema) override; std::string GetInsertTrigger(const TableInfo &table, const std::string &identity) override; std::string GetUpdateTrigger(const TableInfo &table, const std::string &identity) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp index 1325697b6ed..4badb1b3154 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp @@ -25,7 +25,7 @@ int SqliteLogTableManager::AddRelationalLogTableTrigger(sqlite3 *db, const Table for (const auto &sql : sqls) { int errCode = SQLiteUtils::ExecuteRawSQL(db, sql); if (errCode != E_OK) { - LOGE("[SQLite] execute create log trigger sql failed, errCode=%d", errCode); + LOGE("[LogTableManager] execute create log trigger sql failed, errCode=%d", errCode); return errCode; } } @@ -35,9 +35,9 @@ int SqliteLogTableManager::AddRelationalLogTableTrigger(sqlite3 *db, const Table int SqliteLogTableManager::CreateRelationalLogTable(sqlite3 *db, const TableInfo &table) { const std::string tableName = GetLogTableName(table); - std::string primaryKey = GetPrimaryKeySql(table) + ");"; + std::string primaryKey = GetPrimaryKeySql(table); - std::string sql = + std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + tableName + "(" \ "data_key INT NOT NULL," \ "device BLOB," \ @@ -46,17 +46,32 @@ int SqliteLogTableManager::CreateRelationalLogTable(sqlite3 *db, const TableInfo "wtimestamp INT NOT NULL," \ "flag INT NOT NULL," \ "hash_key BLOB NOT NULL," \ - + primaryKey; - sql += GetIndexSql(table); - return SQLiteUtils::ExecuteRawSQL(db, sql); + + primaryKey + ");"; + std::vector logTableSchema; + logTableSchema.emplace_back(createTableSql); + GetIndexSql(table, logTableSchema); + + for (const auto &sql : logTableSchema) { + int errCode = SQLiteUtils::ExecuteRawSQL(db, sql); + if (errCode != E_OK) { + LOGE("[LogTableManager] execute create log table schema failed, errCode=%d", errCode); + return errCode; + } + } + return E_OK; } -std::string SqliteLogTableManager::GetIndexSql(const TableInfo &table) +void SqliteLogTableManager::GetIndexSql(const TableInfo &table, std::vector &schema) { const std::string tableName = GetLogTableName(table); - return "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX + "time_flag_index ON " + tableName + - "(timestamp, flag);" \ - "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX + "hashkey_index ON " + tableName + "(hash_key);"; + + std::string indexTimestampFlag = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX + + "time_flag_index ON " + tableName + "(timestamp, flag);"; + schema.emplace_back(indexTimestampFlag); + + std::string indexHashkey = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX + + "hashkey_index ON " + tableName + "(hash_key);"; + schema.emplace_back(indexHashkey); } std::string SqliteLogTableManager::GetLogTableName(const TableInfo &table) const diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.h index d20c67513e9..d792bbe1956 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.h @@ -34,7 +34,7 @@ public: int CreateRelationalLogTable(sqlite3 *db, const TableInfo &table); protected: - virtual std::string GetIndexSql(const TableInfo &table); + virtual void GetIndexSql(const TableInfo &table, std::vector &schema); std::string GetLogTableName(const TableInfo &table) const; private: diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_data_storage.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_data_storage.cpp index 6255829e3fa..de3344c48af 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_data_storage.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_data_storage.cpp @@ -37,8 +37,10 @@ namespace { const std::string CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS version_data(key BLOB, value BLOB, oper_flag INTEGER, version INTEGER, " \ "timestamp INTEGER, ori_timestamp INTEGER, hash_key BLOB, " \ - "PRIMARY key(hash_key, version));" \ - "CREATE INDEX IF NOT EXISTS version_index ON version_data (version);" \ + "PRIMARY key(hash_key, version));"; + const std::string CREATE_TABLE_VERSION_INDEX_SQL = + "CREATE INDEX IF NOT EXISTS version_index ON version_data (version);"; + const std::string CREATE_TABLE_FLAG_INDEX_SQL = "CREATE INDEX IF NOT EXISTS flag_index ON version_data (oper_flag);"; const std::size_t MAX_READ_CONNECT_NUM = 16; @@ -95,6 +97,8 @@ int SQLiteMultiVerDataStorage::Open(const Property &property) DBConstant::MULTI_VER_DATA_STORE + DBConstant::SQLITE_DB_EXTENSION; std::vector tableVect; tableVect.push_back(CREATE_TABLE_SQL); + tableVect.push_back(CREATE_TABLE_VERSION_INDEX_SQL); + tableVect.push_back(CREATE_TABLE_FLAG_INDEX_SQL); OpenDbProperties option = {uri_, property.isNeedCreate, false, tableVect, property.cipherType, property.passwd}; sqlite3 *db = nullptr; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.cpp index 37bcfe7da8a..1d4ef5b16a0 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.cpp @@ -37,9 +37,13 @@ namespace DistributedDB { const std::string SQLiteMultiVerTransaction::CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS version_data(key BLOB, value BLOB, oper_flag INTEGER, version INTEGER, " \ "timestamp INTEGER, ori_timestamp INTEGER, hash_key BLOB, " \ - "PRIMARY key(hash_key, version));" \ - "CREATE INDEX IF NOT EXISTS version_index ON version_data (version);" \ - "CREATE INDEX IF NOT EXISTS flag_index ON version_data (oper_flag);"; + "PRIMARY key(hash_key, version));"; + + const std::string SQLiteMultiVerTransaction::CREATE_TABLE_VERSION_INDEX_SQL = + "CREATE INDEX IF NOT EXISTS version_index ON version_data (version);"; + + const std::string SQLiteMultiVerTransaction::CREATE_TABLE_FLAG_INDEX_SQL = + "CREATE INDEX IF NOT EXISTS flag_index ON version_data (oper_flag);"; const std::string SQLiteMultiVerTransaction::SELECT_ONE_SQL = "SELECT oper_flag, key, value FROM version_data WHERE hash_key=? AND (timestamp>? OR (timestamp=? AND rowid>=?)) " \ @@ -138,6 +142,8 @@ int SQLiteMultiVerTransaction::Initialize(const std::string &uri, { std::vector tableVect; tableVect.push_back(CREATE_TABLE_SQL); + tableVect.push_back(CREATE_TABLE_VERSION_INDEX_SQL); + tableVect.push_back(CREATE_TABLE_FLAG_INDEX_SQL); OpenDbProperties option = {uri, true, false, tableVect, type, passwd}; int errCode = SQLiteUtils::OpenDatabase(option, db_); if (errCode != E_OK) { diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.h index bee608f7869..26d88682b42 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_multi_ver_transaction.h @@ -165,6 +165,8 @@ private: int GetPrePutValues(const Version &versionInfo, Timestamp timestamp, std::vector &values) const; static const std::string CREATE_TABLE_SQL; + static const std::string CREATE_TABLE_VERSION_INDEX_SQL; + static const std::string CREATE_TABLE_FLAG_INDEX_SQL; static const std::string SELECT_ONE_SQL; // select the rowid static const std::string SELECT_BATCH_SQL; // select the rowid and the key static const std::string SELECT_HASH_ENTRY_SQL; // select the data according the hash key diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_database_upgrader.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_database_upgrader.cpp index f0ff27eed14..3dd7bc99124 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_database_upgrader.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_database_upgrader.cpp @@ -52,10 +52,16 @@ namespace { "key BLOB PRIMARY KEY NOT NULL," \ "value BLOB);"; - const std::string CREATE_SYNC_TABLE_INDEX_SQL = - "CREATE INDEX IF NOT EXISTS key_index ON sync_data (key, flag);" \ - "CREATE INDEX IF NOT EXISTS time_index ON sync_data (timestamp);" \ - "CREATE INDEX IF NOT EXISTS dev_index ON sync_data (device);" \ + const std::string CREATE_SYNC_TABLE_INDEX_SQL_KEY_INDEX = + "CREATE INDEX IF NOT EXISTS key_index ON sync_data (key, flag);"; + + const std::string CREATE_SYNC_TABLE_INDEX_SQL_TIME_INDEX = + "CREATE INDEX IF NOT EXISTS time_index ON sync_data (timestamp);"; + + const std::string CREATE_SYNC_TABLE_INDEX_SQL_DEV_INDEX = + "CREATE INDEX IF NOT EXISTS dev_index ON sync_data (device);"; + + const std::string CREATE_SYNC_TABLE_INDEX_SQL_LOCAL_HASHKEY_INDEX = "CREATE INDEX IF NOT EXISTS local_hashkey_index ON local_data (hash_key);"; const std::string DROP_META_TABLE_SQL = "DROP TABLE IF EXISTS main.meta_data;"; @@ -151,14 +157,20 @@ void SQLiteSingleVerDatabaseUpgrader::SetUpgradeSqls(int version, std::vector indexes; while (true) { errCode = SQLiteUtils::StepWithRetry(stmt, false); if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { std::string indexSql; (void)GetColumnTextValue(stmt, 0, indexSql); - sql += indexSql; + indexes.emplace_back(indexSql); continue; } if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) { @@ -1428,9 +1433,12 @@ int SQLiteUtils::CloneIndexes(sqlite3 *db, const std::string &oriTableName, cons if (errCode != E_OK) { return errCode; } - errCode = SQLiteUtils::ExecuteRawSQL(db, sql); - if (errCode != E_OK) { - LOGE("[SQLite] execute create table sql failed"); + + for (const auto &it : indexes) { + errCode = SQLiteUtils::ExecuteRawSQL(db, it); + if (errCode != E_OK) { + LOGE("[SQLite] execute clone index sql failed"); + } } return errCode; } diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp index 84f358559c1..8f943acff28 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_relational_schema_object_test.cpp @@ -213,6 +213,7 @@ namespace { const std::string TABLE_DEFINE_STR_NAME = R""("NAME": "FIRST",)""; const std::string TABLE_DEFINE_STR_NAME_INVALID = R"("NAME": 123,)"; + const std::string TABLE_DEFINE_STR_NAME_INVALID_CHARACTER = R"("NAME": "t1; --",)"; const std::string TABLE_DEFINE_STR_FIELDS = R""("DEFINE": { "field_name1": { "COLUMN_ID":1, @@ -233,6 +234,12 @@ namespace { "NOT_NULL": true, "DEFAULT": "abcd" }},)""; + const std::string TABLE_DEFINE_STR_FIELDS_INVALID_CHARACTER = R""("DEFINE": { + "1 = 1; --": { + "COLUMN_ID":1, + "NOT_NULL": true, + "DEFAULT": "abcd" + }},)""; const std::string TABLE_DEFINE_STR_KEY = R""("PRIMARY_KEY": "field_name1")""; const std::string TABLE_DEFINE_BOOL_KEY_INVALID = R""("PRIMARY_KEY": false)""; const std::string TABLE_DEFINE_BOOL_ARRAY_KEY_INVALID = R""("PRIMARY_KEY": [false, true, true])""; @@ -384,6 +391,16 @@ HWTEST_F(DistributedDBRelationalSchemaObjectTest, RelationalSchemaParseTest003, errCode = schemaObj.ParseFromSchemaString(GenerateFromTableStr("[" + invalidTableStr05 + "]")); EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); + std::string invalidTableStr07 = "{" + TABLE_DEFINE_STR_NAME_INVALID_CHARACTER + TABLE_DEFINE_STR_FIELDS + + TABLE_DEFINE_STR_KEY + "}"; + errCode = schemaObj.ParseFromSchemaString(GenerateFromTableStr("[" + invalidTableStr07 + "]")); + EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); + + std::string invalidTableStr08 = "{" + TABLE_DEFINE_STR_NAME + TABLE_DEFINE_STR_FIELDS_INVALID_CHARACTER + + TABLE_DEFINE_STR_KEY + "}"; + errCode = schemaObj.ParseFromSchemaString(GenerateFromTableStr("[" + invalidTableStr08 + "]")); + EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); + errCode = schemaObj.ParseFromSchemaString(""); EXPECT_EQ(errCode, -E_INVALID_ARGS); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp index 2b3c5162224..c9284cb0f15 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp @@ -73,6 +73,8 @@ namespace { const std::string INSERT_SYNC_DATA_SQL = "INSERT OR REPLACE INTO sync_data (key, timestamp, flag, hash_key) " "VALUES('KEY', 123456789, 1, 'HASH_KEY');"; + + const std::string INVALID_TABLE_FIELD_SQL = "create table if not exists t1 ('1 = 1; --' int primary key, b blob)"; } class DistributedDBInterfacesRelationalTest : public testing::Test { @@ -333,6 +335,9 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize EXPECT_EQ(delegate->CreateDistributedTable("Handle-J@^."), INVALID_ARGS); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, INVALID_TABLE_FIELD_SQL), SQLITE_OK); + EXPECT_EQ(delegate->CreateDistributedTable("t1"), NOT_SUPPORT); + /** * @tc.steps:step4. Close store * @tc.expected: step4. Return OK. -- Gitee From 5e7ab814133b91b2a6cb8e32361adcc156df6026 Mon Sep 17 00:00:00 2001 From: wbq_sky Date: Sat, 24 Sep 2022 18:00:06 +0800 Subject: [PATCH 03/14] fix the review issue Signed-off-by: wbq_sky --- .../distributeddb/common/include/db_common.h | 3 +- .../distributeddb/common/include/db_types.h | 2 +- .../common/include/endian_convert.h | 2 +- .../common/include/performance_analysis.h | 2 +- .../common/include/platform_specific.h | 2 +- .../distributeddb/common/include/ref_object.h | 9 +-- .../common/include/runtime_context.h | 2 +- .../common/include/schema_constant.h | 1 + .../common/include/schema_negotiate.h | 2 +- .../common/include/schema_object.h | 5 +- .../distributeddb/common/include/task_pool.h | 3 +- .../common/include/user_change_monitor.h | 3 +- .../common/src/flatbuffer_schema.cpp | 69 ++++++++----------- .../distributeddb/common/src/json_object.cpp | 19 +++-- .../common/src/param_check_utils.cpp | 4 +- .../libs/distributeddb/common/src/parcel.cpp | 10 +-- .../common/src/performance_analysis.cpp | 10 +-- .../common/src/platform_specific.cpp | 17 ++--- .../distributeddb/common/src/ref_object.cpp | 16 ++--- .../common/src/runtime_context.cpp | 2 +- .../common/src/runtime_context_impl.h | 10 +-- .../common/src/schema_negotiate.cpp | 2 +- .../common/src/schema_object.cpp | 66 ++++++++---------- .../distributeddb/common/src/schema_utils.cpp | 8 ++- .../distributeddb/common/src/task_pool_impl.h | 5 +- .../common/src/time_tick_monitor.h | 5 +- .../common/src/user_change_monitor.cpp | 3 +- .../interfaces/include/get_query_info.h | 2 +- .../storage/src/generic_kvdb.cpp | 13 ++-- ...e_ver_natural_store_commit_notify_data.cpp | 2 +- .../sqlite_single_ver_storage_engine.cpp | 4 +- .../sqlite_single_ver_storage_executor.h | 2 +- ...lite_single_ver_storage_executor_cache.cpp | 32 ++++----- 33 files changed, 162 insertions(+), 175 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/db_common.h b/frameworks/libs/distributeddb/common/include/db_common.h index aca9546ebd1..2b51e51e45d 100644 --- a/frameworks/libs/distributeddb/common/include/db_common.h +++ b/frameworks/libs/distributeddb/common/include/db_common.h @@ -18,9 +18,10 @@ #include #include + #include "db_types.h" -#include "store_types.h" #include "kvdb_properties.h" +#include "store_types.h" namespace DistributedDB { class DBCommon final { diff --git a/frameworks/libs/distributeddb/common/include/db_types.h b/frameworks/libs/distributeddb/common/include/db_types.h index c620dbe41ae..656beaacae8 100644 --- a/frameworks/libs/distributeddb/common/include/db_types.h +++ b/frameworks/libs/distributeddb/common/include/db_types.h @@ -21,8 +21,8 @@ #include #include -#include "types_export.h" #include "db_constant.h" +#include "types_export.h" namespace DistributedDB { using Timestamp = uint64_t; diff --git a/frameworks/libs/distributeddb/common/include/endian_convert.h b/frameworks/libs/distributeddb/common/include/endian_convert.h index 97060c201ea..c2e20756a90 100644 --- a/frameworks/libs/distributeddb/common/include/endian_convert.h +++ b/frameworks/libs/distributeddb/common/include/endian_convert.h @@ -16,8 +16,8 @@ #ifndef ENDIAN_CONVERT_H #define ENDIAN_CONVERT_H -#include #include +#include namespace DistributedDB { inline bool IsBigEndian() diff --git a/frameworks/libs/distributeddb/common/include/performance_analysis.h b/frameworks/libs/distributeddb/common/include/performance_analysis.h index 4a89f22f088..1b0da052c1d 100644 --- a/frameworks/libs/distributeddb/common/include/performance_analysis.h +++ b/frameworks/libs/distributeddb/common/include/performance_analysis.h @@ -16,8 +16,8 @@ #ifndef PERFORMANCE_ANALYSIS_H #define PERFORMANCE_ANALYSIS_H -#include #include +#include #include "db_types.h" diff --git a/frameworks/libs/distributeddb/common/include/platform_specific.h b/frameworks/libs/distributeddb/common/include/platform_specific.h index 5f9a8ce053a..10d91822f80 100644 --- a/frameworks/libs/distributeddb/common/include/platform_specific.h +++ b/frameworks/libs/distributeddb/common/include/platform_specific.h @@ -16,9 +16,9 @@ #ifndef PLATFORM_SPECIFIC_H #define PLATFORM_SPECIFIC_H -#include #include #include +#include namespace DistributedDB { #if (defined(OS_TYPE_WINDOWS)) || (defined(OS_TYPE_MAC)) diff --git a/frameworks/libs/distributeddb/common/include/ref_object.h b/frameworks/libs/distributeddb/common/include/ref_object.h index 7fce0ea1e73..9cf3a569c0a 100644 --- a/frameworks/libs/distributeddb/common/include/ref_object.h +++ b/frameworks/libs/distributeddb/common/include/ref_object.h @@ -17,10 +17,11 @@ #define KV_DB_REF_OBJECT_H #include -#include -#include -#include #include +#include +#include +#include + #include "macro_utils.h" namespace DistributedDB { @@ -36,7 +37,7 @@ public: private: DISABLE_COPY_ASSIGN_MOVE(AutoLock); const RefObject *refObj_; - bool IsLocked_; + bool isLocked_; }; RefObject(); diff --git a/frameworks/libs/distributeddb/common/include/runtime_context.h b/frameworks/libs/distributeddb/common/include/runtime_context.h index 63ab1c15f02..dc291b983b0 100644 --- a/frameworks/libs/distributeddb/common/include/runtime_context.h +++ b/frameworks/libs/distributeddb/common/include/runtime_context.h @@ -17,8 +17,8 @@ #define RUNTIME_CONTEXT_H #include -#include #include +#include #include "auto_launch.h" #include "auto_launch_export.h" diff --git a/frameworks/libs/distributeddb/common/include/schema_constant.h b/frameworks/libs/distributeddb/common/include/schema_constant.h index bbfe3b2d101..0fb0e6b75aa 100644 --- a/frameworks/libs/distributeddb/common/include/schema_constant.h +++ b/frameworks/libs/distributeddb/common/include/schema_constant.h @@ -16,6 +16,7 @@ #ifndef SCHEMA_CONSTANT_H #define SCHEMA_CONSTANT_H +#include #include // This header is supposed to be included only in source files. Do not include it in any header files. diff --git a/frameworks/libs/distributeddb/common/include/schema_negotiate.h b/frameworks/libs/distributeddb/common/include/schema_negotiate.h index d12304cb75c..9bb759211fb 100644 --- a/frameworks/libs/distributeddb/common/include/schema_negotiate.h +++ b/frameworks/libs/distributeddb/common/include/schema_negotiate.h @@ -15,8 +15,8 @@ #ifndef SCHEMA_NEGOTIATE_H #define SCHEMA_NEGOTIATE_H -#include "schema_object.h" #include "relational_schema_object.h" +#include "schema_object.h" namespace DistributedDB { struct SyncOpinion { diff --git a/frameworks/libs/distributeddb/common/include/schema_object.h b/frameworks/libs/distributeddb/common/include/schema_object.h index 2cf9091a678..e265565e445 100644 --- a/frameworks/libs/distributeddb/common/include/schema_object.h +++ b/frameworks/libs/distributeddb/common/include/schema_object.h @@ -16,15 +16,16 @@ #ifndef SCHEMA_OBJECT_H #define SCHEMA_OBJECT_H -#include #include +#include + #ifndef OMIT_FLATBUFFER #include #endif // OMIT_FLATBUFFER #include "db_types.h" +#include "ischema.h" #include "macro_utils.h" #include "relational_schema_object.h" -#include "ischema.h" #include "value_object.h" namespace DistributedDB { diff --git a/frameworks/libs/distributeddb/common/include/task_pool.h b/frameworks/libs/distributeddb/common/include/task_pool.h index fe5d8c8bc5a..39d400d4397 100644 --- a/frameworks/libs/distributeddb/common/include/task_pool.h +++ b/frameworks/libs/distributeddb/common/include/task_pool.h @@ -16,8 +16,9 @@ #ifndef TASK_POOL_H #define TASK_POOL_H -#include #include +#include + #include "macro_utils.h" namespace DistributedDB { diff --git a/frameworks/libs/distributeddb/common/include/user_change_monitor.h b/frameworks/libs/distributeddb/common/include/user_change_monitor.h index 6591a40c401..1f2837c3a8d 100644 --- a/frameworks/libs/distributeddb/common/include/user_change_monitor.h +++ b/frameworks/libs/distributeddb/common/include/user_change_monitor.h @@ -17,9 +17,10 @@ #define USER_CHANGE_MONITOR_H #include -#include "platform_specific.h" + #include "macro_utils.h" #include "notification_chain.h" +#include "platform_specific.h" #include "runtime_context.h" namespace DistributedDB { diff --git a/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp b/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp index 021b5ec7400..7f4ba262ad7 100644 --- a/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp +++ b/frameworks/libs/distributeddb/common/src/flatbuffer_schema.cpp @@ -14,12 +14,14 @@ */ #include "schema_object.h" -#include + #include -#include "schema_constant.h" -#include "schema_utils.h" +#include + #include "db_errno.h" #include "log_print.h" +#include "schema_constant.h" +#include "schema_utils.h" namespace DistributedDB { namespace { @@ -169,11 +171,7 @@ int SchemaObject::FlatBufferSchema::ParseFlatBufferSchema(const std::string &inD return errCode; } - errCode = ParseCheckIndexes(indexCollect); - if (errCode != E_OK) { - return errCode; - } - return E_OK; + return ParseCheckIndexes(indexCollect); } int SchemaObject::FlatBufferSchema::CompareFlatBufferDefine(const FlatBufferSchema &other) const @@ -196,7 +194,7 @@ int SchemaObject::FlatBufferSchema::CompareFlatBufferDefine(const FlatBufferSche std::string selfRootName = SchemaUtils::StripNameSpace(selfRootTableName->str()); std::string otherRootName = SchemaUtils::StripNameSpace(otherRootTableName->str()); if (selfRootName != otherRootName) { - LOGE("[FBSchema][Compare] RootName differ, self=%s, other=%s.", selfRootName.c_str(), otherRootName.c_str()); + LOGE("[FBSchema][Compare] RootName dif."); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } // We don't have to compare rootTableAttribute or index here, they are done by SchemaObject @@ -212,12 +210,12 @@ int CheckSizePrefixRawValue(const RawValue &inValue) return -E_INVALID_ARGS; } if (inValue.second <= SIZE_PREFIX_SIZE) { - LOGE("[FBSchema][CheckSizePreValue] ValueSize=%u too short.", inValue.second); + LOGW("[FBSchema][CheckSizePreValue] ValueSize too short:%" PRIu32, inValue.second); return -E_INVALID_ARGS; } auto realSize = flatbuffers::ReadScalar(inValue.first); if (realSize != inValue.second - SIZE_PREFIX_SIZE) { - LOGE("[FBSchema][CheckSizePreValue] RealSize=%u mismatch valueSize=(%u-4).", realSize, inValue.second); + LOGE("[FBSchema][CheckSizePreValue] RealSize=%" PRIu32 " mismatch %" PRIu32, realSize, inValue.second); return -E_INVALID_ARGS; } return E_OK; @@ -439,7 +437,7 @@ int SchemaObject::FlatBufferSchema::ExtractFlatBufferValue(RawString inPath, con // Currently we don't support nest-path auto fieldInfo = GetFieldInfoFromSchemaByPath(*schema, pathStr); if (fieldInfo == nullptr) { - LOGE("[FBSchema][Extract] FieldInfo of path=%s not found.", pathStr); + LOGW("[FBSchema][Extract] FieldInfo of path not found."); return -E_INTERNAL_ERROR; } // Begin extract, we have to minimal verify if we don't trust value from database @@ -532,19 +530,18 @@ int SchemaObject::FlatBufferSchema::ParseCheckRootTableDefine(const reflection:: CHECK_NULL_UNLIKELY_RETURN_ERROR(name); int errCode = SchemaUtils::CheckFieldName(name->str()); if (errCode != E_OK) { - LOGE("[FBSchema][ParseRootDefine] Invalid fieldName=%s, errCode=%d.", name->c_str(), errCode); + LOGE("[FBSchema][ParseRootDefine] Invalid fieldName, errCode=%d.", errCode); return -E_SCHEMA_PARSE_FAIL; } FieldPath path{name->str()}; if (owner_.schemaDefine_[ROOT_DEFINE_DEPTH].count(path) != 0) { // Unlikely - LOGE("[FBSchema][ParseRootDefine] FieldPath=%s already exist at root.", name->c_str()); + LOGE("[FBSchema][ParseRootDefine] FieldPath already exist at root."); return -E_SCHEMA_PARSE_FAIL; } errCode = ParseCheckFieldInfo(schema, *eachField, path, indexCollect); if (errCode != E_OK) { - LOGE("[FBSchema][ParseRootDefine] ParseFieldInfo errCode=%d, FieldPath=%s.", errCode, - SchemaUtils::FieldPathString(path).c_str()); + LOGE("[FBSchema][ParseRootDefine] ParseFieldInfo errCode=%d", errCode); return errCode; } } @@ -555,7 +552,7 @@ int SchemaObject::FlatBufferSchema::ParseCheckRootTableDefine(const reflection:: } } if (fieldPathCount > SchemaConstant::SCHEMA_FEILD_NAME_COUNT_MAX) { - LOGE("[FBSchema][ParseRootDefine] FieldPath count=%u exceed the limitation.", fieldPathCount); + LOGE("[FBSchema][ParseRootDefine] FieldPath count=%" PRIu32 " exceed the limitation.", fieldPathCount); return -E_SCHEMA_PARSE_FAIL; } return E_OK; @@ -691,7 +688,7 @@ int SchemaObject::FlatBufferSchema::ParseCheckStructDefine(const reflection::Sch CHECK_NULL_UNLIKELY_RETURN_ERROR(eachName); int errCode = SchemaUtils::CheckFieldName(eachName->str()); if (errCode != E_OK) { - LOGE("[FBSchema][ParseStruct] Invalid fieldName=%s, errCode=%d.", eachName->c_str(), errCode); + LOGE("[FBSchema][ParseStruct] Invalid fieldName, errCode=%d.", errCode); return -E_SCHEMA_PARSE_FAIL; } FieldPath eachPath = path; @@ -699,8 +696,7 @@ int SchemaObject::FlatBufferSchema::ParseCheckStructDefine(const reflection::Sch RawIndexInfos notUsed; errCode = ParseCheckFieldInfo(schema, *eachField, eachPath, notUsed); if (errCode != E_OK) { - LOGE("[FBSchema][ParseStruct] ParseFieldInfo errCode=%d, FieldPath=%s.", errCode, - SchemaUtils::FieldPathString(eachPath).c_str()); + LOGE("[FBSchema][ParseStruct] ParseFieldInfo errCode=%d.", errCode); return errCode; } } @@ -723,7 +719,7 @@ int SchemaObject::FlatBufferSchema::ParseCheckIndexes(const RawIndexInfos &index if (IsNotCompositeIndex(rawIndexStr)) { int errCode = owner_.ParseCheckEachIndexFromStringArray(indexStrArray); if (errCode != E_OK) { - LOGE("[FBSchema][ParseIndex] Create single-index=%s fail, errCode=%d.", entry.first.c_str(), errCode); + LOGE("[FBSchema][ParseIndex] Create single-index fail:%d.", errCode); return errCode; } description_ += ("INDEX=" + entry.first + ";"); @@ -744,8 +740,7 @@ int SchemaObject::FlatBufferSchema::ParseCheckIndexes(const RawIndexInfos &index } int errCode = owner_.ParseCheckEachIndexFromStringArray(indexStrArray); if (errCode != E_OK) { - LOGE("[FBSchema][ParseIndex] Create composite-index=%s, rawStr=%s fail, errCode=%d.", entry.first.c_str(), - rawIndexStr.c_str(), errCode); + LOGE("[FBSchema][ParseIndex] Create composite-index fail:%d.", errCode); return errCode; } description_ += ("INDEX=" + entry.first + ";"); @@ -768,12 +763,12 @@ int CompareFieldCount(bool isRoot, uint32_t selfCount, uint32_t otherCount) { if (isRoot) { if (otherCount < selfCount) { - LOGE("[FBSchema][CompareRoot] RootFieldSize: other=%u less than self=%u.", otherCount, selfCount); + LOGW("[FBSchema][CompareRoot] RootFieldSize: %" PRu32 " vs %" PRu32, selfCount, otherCount); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } else { if (selfCount != otherCount) { - LOGE("[FBSchema][CompareRoot] StructFieldSize: self=%u differ with other=%u.", selfCount, otherCount); + LOGW("[FBSchema][CompareRoot] StructFieldSize: %" PRu32 " vs %" PRu32, selfCount, otherCount); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -785,26 +780,23 @@ int CompareFieldInfoBesideType(const reflection::Field &selfField, const reflect { // Compare offset if (selfField.offset() != otherField.offset()) { - LOGE("[FBSchema][CompareField] Offset differ: self=%u, other=%u.", selfField.offset(), otherField.offset()); + LOGW("[FBSchema][CompareField] Offset diff"); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } // Compare default value if (selfField.default_integer() != otherField.default_integer()) { - LOGE("[FBSchema][CompareField] DefaultInteger differ: self=%lld, other=%lld.", - static_cast(selfField.default_integer()), static_cast(otherField.default_integer())); + LOGE("[FBSchema][CompareField] DefaultInteger diff"); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } // QUEER: for the same default_real value in fbs, flatbuffer will generate different value in binary ??? if (!IsDoubleNearlyEqual(selfField.default_real(), otherField.default_real())) { - LOGE("[FBSchema][CompareField] DefaultReal differ: self=%f, other=%f.", selfField.default_real(), - otherField.default_real()); + LOGE("[FBSchema][CompareField] DefaultReal diff"); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } // Ignore deprecated, Compare required if (IsRequiredSupportType(theType)) { if (selfField.required() != otherField.required()) { - LOGE("[FBSchema][CompareField] Require differ: self=%d, other=%d.", selfField.required(), - otherField.required()); + LOGE("[FBSchema][CompareField] Require diff"); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -821,7 +813,7 @@ int CompareFieldInfo(const reflection::Field &selfField, const reflection::Field auto selfBaseType = selfType->base_type(); auto otherBaseType = otherType->base_type(); if (selfBaseType != otherBaseType) { - LOGE("[FBSchema][CompareField] BaseType differ: self=%s, other=%s.", reflection::EnumNameBaseType(selfBaseType), + LOGE("[FBSchema][CompareField] BaseType diff:%s vs %s.", reflection::EnumNameBaseType(selfBaseType), reflection::EnumNameBaseType(otherBaseType)); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } @@ -829,7 +821,7 @@ int CompareFieldInfo(const reflection::Field &selfField, const reflection::Field auto selfElementType = selfType->element(); auto otherElementType = otherType->element(); if (selfElementType != otherElementType) { - LOGE("[FBSchema][CompareField] ElementType differ: self=%u, other=%u.", selfElementType, otherElementType); + LOGE("[FBSchema][CompareField] ElementType diff:%" PRu32 " vs %" PRu32, selfElementType, otherElementType); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -858,7 +850,7 @@ int CompareExtraField(const PairConstPointer &bothObject) continue; } if (eachOtherField->required()) { - LOGE("[FBSchema][CompareDefine] Extra field=%s should not be required.", otherName->c_str()); + LOGE("[FBSchema][CompareDefine] Extra field should not be required."); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -886,13 +878,13 @@ int SchemaObject::FlatBufferSchema::CompareTableOrStructDefine(const PairConstPo CHECK_NULL_UNLIKELY_RETURN_ERROR(selfName); auto correspondOtherField = otherFields->LookupByKey(selfName->c_str()); if (correspondOtherField == nullptr) { - LOGE("[FBSchema][CompareDefine] SelfField=%s not found in other.", selfName->c_str()); + LOGE("[FBSchema][CompareDefine] SelfField not found in other."); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } bool isStruct = false; errCode = CompareFieldInfo(*eachSelfField, *correspondOtherField, isStruct); if (IsNotEqualNotCompatible(errCode)) { - LOGE("[FBSchema][CompareDefine] Compare info of field=%s fail, errCode=%d.", selfName->c_str(), errCode); + LOGE("[FBSchema][CompareDefine] Compare info of field fail, errCode=%d.", errCode); return errCode; } if (isStruct) { @@ -941,8 +933,7 @@ int SchemaObject::FlatBufferSchema::CompareStruct(const PairConstPointerstr()); std::string otherName = SchemaUtils::StripNameSpace(otherStructName->str()); if (selfName != otherName) { - LOGE("[FBSchema][CompareStruct] The field is not of same struct type, self=%s, other=%s.", - selfName.c_str(), otherName.c_str()); + LOGE("[FBSchema][CompareStruct] The field is not of same struct type"); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } if (compared.count(selfName) != 0) { // This struct-type had already been compared, no need to do recurse again diff --git a/frameworks/libs/distributeddb/common/src/json_object.cpp b/frameworks/libs/distributeddb/common/src/json_object.cpp index 807798a6a49..860f0b3c611 100644 --- a/frameworks/libs/distributeddb/common/src/json_object.cpp +++ b/frameworks/libs/distributeddb/common/src/json_object.cpp @@ -14,9 +14,11 @@ */ #include "json_object.h" + +#include #include #include -#include + #include "db_errno.h" #include "log_print.h" @@ -116,8 +118,8 @@ int JsonObject::Parse(const std::string &inString) 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_); + LOGE("[Json][Parse] Json calculate nest depth failed %d, depth=%" PRIu32 " exceed max allowed:%" PRIu32, + errCode, nestDepth, maxNestDepth_); return -E_JSON_PARSE_FAIL; } #ifdef JSONCPP_USE_BUILDER @@ -173,8 +175,8 @@ int JsonObject::Parse(const uint8_t *dataBegin, const uint8_t *dataEnd) 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_); + LOGE("[Json][Parse] Json calculate nest depth failed %d, depth:%" PRIu32 " exceed max allowed:%" PRIu32, + errCode, nestDepth, maxNestDepth_); return -E_JSON_PARSE_FAIL; } #ifdef JSONCPP_USE_BUILDER @@ -436,7 +438,7 @@ int JsonObject::GetArrayContentOfStringOrStringArray(const FieldPath &inPath, } // If reach here, then something is not ok outContent.clear(); - LOGE("[Json][GetArrayContent] Not string or array or GetStringArray fail=%d at index=%u.", errCode, index); + LOGE("[Json][GetArrayContent] Not string or array fail=%d at index:%" PRIu32, errCode, index); return -E_NOT_SUPPORT; } return E_OK; @@ -451,10 +453,7 @@ bool InsertFieldCheckParameter(const FieldPath &inPath, FieldType inType, const return false; } // Infinite double not support - if (inType == FieldType::LEAF_FIELD_DOUBLE && !std::isfinite(inValue.doubleValue)) { - return false; - } - return true; + return !(inType == FieldType::LEAF_FIELD_DOUBLE && !std::isfinite(inValue.doubleValue)); } void LeafJsonNodeAppendValue(Json::Value &leafNode, FieldType inType, const FieldValue &inValue) diff --git a/frameworks/libs/distributeddb/common/src/param_check_utils.cpp b/frameworks/libs/distributeddb/common/src/param_check_utils.cpp index 1cb41e1de5d..2a1fe4d232a 100644 --- a/frameworks/libs/distributeddb/common/src/param_check_utils.cpp +++ b/frameworks/libs/distributeddb/common/src/param_check_utils.cpp @@ -17,8 +17,8 @@ #include "db_common.h" #include "db_errno.h" -#include "platform_specific.h" #include "log_print.h" +#include "platform_specific.h" namespace DistributedDB { bool ParamCheckUtils::CheckDataDir(const std::string &dataDir, std::string &canonicalDir) @@ -192,7 +192,7 @@ uint8_t ParamCheckUtils::GetValidCompressionRate(uint8_t compressionRate) { // Valid when between 1 and 100. When compressionRate is invalid, change it to default rate. if (compressionRate < 1 || compressionRate > DBConstant::DEFAULT_COMPTRESS_RATE) { - LOGD("Invalid compression rate:%u.", compressionRate); + LOGD("Invalid compression rate:%" PRIu8, compressionRate); compressionRate = DBConstant::DEFAULT_COMPTRESS_RATE; } return compressionRate; diff --git a/frameworks/libs/distributeddb/common/src/parcel.cpp b/frameworks/libs/distributeddb/common/src/parcel.cpp index 372a9bfc66f..815597416fa 100644 --- a/frameworks/libs/distributeddb/common/src/parcel.cpp +++ b/frameworks/libs/distributeddb/common/src/parcel.cpp @@ -17,12 +17,12 @@ #include +#include "db_constant.h" +#include "db_errno.h" #include "endian_convert.h" -#include "securec.h" -#include "macro_utils.h" #include "log_print.h" -#include "db_errno.h" -#include "db_constant.h" +#include "macro_utils.h" +#include "securec.h" namespace DistributedDB { Parcel::Parcel(uint8_t *inBuf, uint32_t len) @@ -392,7 +392,7 @@ int Parcel::WriteBlob(const char *buffer, uint32_t bufLen) uint32_t leftLen = static_cast(totalLen_ - parcelLen_); int errCode = memcpy_s(bufPtr_, leftLen, buffer, bufLen); if (errCode != EOK) { - LOGE("[WriteBlob] leftLen:%u, bufLen:%u", leftLen, bufLen); + LOGE("[WriteBlob] leftLen:%" PRIu32 ", bufLen:%" PRIu32, leftLen, bufLen); isError_ = true; return -E_SECUREC_ERROR; } diff --git a/frameworks/libs/distributeddb/common/src/performance_analysis.cpp b/frameworks/libs/distributeddb/common/src/performance_analysis.cpp index f0aa482b9d7..94207bf3067 100644 --- a/frameworks/libs/distributeddb/common/src/performance_analysis.cpp +++ b/frameworks/libs/distributeddb/common/src/performance_analysis.cpp @@ -17,10 +17,10 @@ #include #include "db_errno.h" -#include "macro_utils.h" -#include "time_helper.h" #include "log_print.h" +#include "macro_utils.h" #include "platform_specific.h" +#include "time_helper.h" namespace DistributedDB { const std::string PerformanceAnalysis::STATISTICAL_DATA_FILE_NAME_HEADER = "/data/log/statistic"; @@ -192,9 +192,11 @@ void PerformanceAnalysis::OutStatistics() { std::string addrStatistics = STATISTICAL_DATA_FILE_NAME_HEADER + fileID_ + CSV_FILE_EXTENSION; outFile.open(addrStatistics, std::ios_base::app); + if (!outFile.is_open()) { + return; + } // This part filters the zeros data - outFile << "stepNum" << "," << "maxTime(us)" << "," << "minTime(us)" << "," << "averageTime(us)" - << "," << "count" << "," << "\n"; + outFile << "stepNum, maxTime(us), minTime(us), averageTime(us), count,\n"; for (size_t i = 0; i < stepTimeRecordInfo_.size(); i++) { // output to performance file if (stepTimeRecordInfo_[i].max != 0) { outFile << i << "," << stepTimeRecordInfo_[i].max<< "," << stepTimeRecordInfo_[i].min diff --git a/frameworks/libs/distributeddb/common/src/platform_specific.cpp b/frameworks/libs/distributeddb/common/src/platform_specific.cpp index 93f33a3ae29..16df2d75745 100644 --- a/frameworks/libs/distributeddb/common/src/platform_specific.cpp +++ b/frameworks/libs/distributeddb/common/src/platform_specific.cpp @@ -18,23 +18,24 @@ #include #include #include -#include -#include -#include #include -#include -#include + #include +#include +#include +#include +#include +#include +#include #if defined OS_TYPE_WINDOWS -#include #include +#include #include #endif -#include -#include "securec.h" #include "db_errno.h" #include "log_print.h" +#include "securec.h" namespace DistributedDB { namespace OS { diff --git a/frameworks/libs/distributeddb/common/src/ref_object.cpp b/frameworks/libs/distributeddb/common/src/ref_object.cpp index a3cd10760d9..cc5442dc448 100644 --- a/frameworks/libs/distributeddb/common/src/ref_object.cpp +++ b/frameworks/libs/distributeddb/common/src/ref_object.cpp @@ -21,22 +21,22 @@ constexpr static int MAX_REF_COUNT = 1024; RefObject::AutoLock::AutoLock(const RefObject *obj, bool unlocked) : refObj_(obj), - IsLocked_(false) + isLocked_(false) { if (refObj_ != nullptr) { if (unlocked) { refObj_->LockObj(); } - IsLocked_ = true; + isLocked_ = true; } } void RefObject::AutoLock::Lock() { if (refObj_ != nullptr) { - if (!IsLocked_) { + if (!isLocked_) { refObj_->LockObj(); - IsLocked_ = true; + isLocked_ = true; } else { LOGE("RefObject-AutoLock: obj' acquires lock more than once."); } @@ -46,9 +46,9 @@ void RefObject::AutoLock::Lock() void RefObject::AutoLock::Unlock() { if (refObj_ != nullptr) { - if (IsLocked_) { + if (isLocked_) { refObj_->UnlockObj(); - IsLocked_ = false; + isLocked_ = false; } else { LOGE("RefObject-AutoLock: obj releases lock more than once."); } @@ -58,9 +58,9 @@ void RefObject::AutoLock::Unlock() RefObject::AutoLock::~AutoLock() { if (refObj_ != nullptr) { - if (IsLocked_) { + if (isLocked_) { refObj_->UnlockObj(); - IsLocked_ = false; + isLocked_ = false; } refObj_ = nullptr; } diff --git a/frameworks/libs/distributeddb/common/src/runtime_context.cpp b/frameworks/libs/distributeddb/common/src/runtime_context.cpp index 9acf1e19cd5..022ff75a83f 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context.cpp +++ b/frameworks/libs/distributeddb/common/src/runtime_context.cpp @@ -14,9 +14,9 @@ */ #include +#include "log_print.h" #include "runtime_context_impl.h" #include "version.h" -#include "log_print.h" namespace DistributedDB { RuntimeContext *RuntimeContext::GetInstance() diff --git a/frameworks/libs/distributeddb/common/src/runtime_context_impl.h b/frameworks/libs/distributeddb/common/src/runtime_context_impl.h index 60652baa801..5df535dd42c 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context_impl.h +++ b/frameworks/libs/distributeddb/common/src/runtime_context_impl.h @@ -16,18 +16,18 @@ #ifndef RUNTIME_CONTEXT_IMPL_H #define RUNTIME_CONTEXT_IMPL_H -#include #include +#include #include -#include "runtime_context.h" -#include "task_pool.h" +#include "auto_launch.h" #include "evloop/include/ievent.h" #include "evloop/include/ievent_loop.h" +#include "icommunicator_aggregator.h" #include "lock_status_observer.h" +#include "runtime_context.h" +#include "task_pool.h" #include "time_tick_monitor.h" -#include "icommunicator_aggregator.h" -#include "auto_launch.h" #include "user_change_monitor.h" namespace DistributedDB { diff --git a/frameworks/libs/distributeddb/common/src/schema_negotiate.cpp b/frameworks/libs/distributeddb/common/src/schema_negotiate.cpp index f183488e9cf..e1669b7f748 100644 --- a/frameworks/libs/distributeddb/common/src/schema_negotiate.cpp +++ b/frameworks/libs/distributeddb/common/src/schema_negotiate.cpp @@ -128,7 +128,7 @@ RelationalSyncOpinion SchemaNegotiate::MakeLocalSyncOpinion(const RelationalSche SchemaType localType = localSchema.GetSchemaType(); SchemaType remoteType = ReadSchemaType(remoteSchemaType); if (remoteType == SchemaType::UNRECOGNIZED) { - LOGW("[RelationalSchema][opinion] Remote schema type %d is unrecognized.", remoteSchemaType); + LOGW("[RelationalSchema][opinion] Remote schema type %" PRIu8 " is unrecognized.", remoteSchemaType); return {}; } diff --git a/frameworks/libs/distributeddb/common/src/schema_object.cpp b/frameworks/libs/distributeddb/common/src/schema_object.cpp index 003bfe0f1bc..46114c7c6ca 100644 --- a/frameworks/libs/distributeddb/common/src/schema_object.cpp +++ b/frameworks/libs/distributeddb/common/src/schema_object.cpp @@ -14,10 +14,10 @@ */ #include "schema_object.h" -#include "schema_utils.h" #include "db_errno.h" #include "log_print.h" #include "schema_constant.h" +#include "schema_utils.h" namespace DistributedDB { namespace { @@ -27,6 +27,7 @@ const std::string FLATBUFFER_EXTRACT_FUNC_NAME = "flatbuffer_extract_by_path"; // For Json-Schema, display its original content before parse. For FlatBuffer-Schema, only display its parsed content. void DisplaySchemaLineByLine(SchemaType inType, const std::string &inSchema) { +#ifdef TRACE_SCHEMA_INFO constexpr uint32_t lengthPerLine = 400; // 400 char per line constexpr uint32_t usualMaxLine = 25; // For normal schema, 25 line for 10k length is quite enough LOGD("[Schema][Display] IS %s, LENGTH=%zu.", SchemaUtils::SchemaTypeString(inType).c_str(), inSchema.size()); @@ -39,6 +40,7 @@ void DisplaySchemaLineByLine(SchemaType inType, const std::string &inSchema) std::string lineStr = inSchema.substr(line * lengthPerLine, lengthPerLine); LOGD("%s", lineStr.c_str()); } +#endif } } @@ -356,7 +358,7 @@ int SchemaObject::ExtractValue(ValueSource sourceType, RawString inPath, const R return -E_INVALID_ARGS; } if (inValue.second <= schemaSkipSize_) { - LOGE("[Schema][Extract] Value length=%u invalid, skipsize=%u.", inValue.second, schemaSkipSize_); + LOGE("[Schema][Extract] Value length=%" PRIu32 " invalid, skip:%" PRIu32, inValue.second, schemaSkipSize_); return -E_FLATBUFFER_VERIFY_FAIL; } @@ -383,7 +385,7 @@ int SchemaObject::ExtractValue(ValueSource sourceType, RawString inPath, const R // Currently do not try no sizePrefix, future may depend on sourceType int errCode = flatbufferSchema_.ExtractFlatBufferValue(inPath, rawValue, outExtract, false); if (errCode != E_OK) { - LOGE("[Schema][Extract] Fail, path=%s, srcType=%d.", inPath, static_cast(sourceType)); + LOGE("[Schema][Extract] Fail, srcType=%d.", static_cast(sourceType)); } delete tempCache; // delete nullptr is safe tempCache = nullptr; @@ -410,11 +412,7 @@ int SchemaObject::ParseJsonSchema(const JsonObject &inJsonObject) if (errCode != E_OK) { return errCode; } - errCode = ParseCheckSchemaSkipSize(inJsonObject); - if (errCode != E_OK) { - return errCode; - } - return E_OK; + return ParseCheckSchemaSkipSize(inJsonObject); } namespace { @@ -542,7 +540,7 @@ int SchemaObject::ParseCheckSchemaDefine(const JsonObject& inJsonObject) std::map subPathType; int errCode = inJsonObject.GetSubFieldPathAndType(nestPathCurDepth, subPathType); if (errCode != E_OK) { // Unlikely - LOGE("[Schema][ParseDefine] Internal Error: GetSubFieldPathAndType Fail, Depth=%u.", depth); + LOGE("[Schema][ParseDefine] Internal Error: GetSubFieldPathAndType Fail, Depth=%" PRIu32, depth); return -E_INTERNAL_ERROR; } fieldNameCount += subPathType.size(); @@ -551,8 +549,7 @@ int SchemaObject::ParseCheckSchemaDefine(const JsonObject& inJsonObject) SchemaAttribute attribute; errCode = CheckSchemaDefineItemDecideAttribute(inJsonObject, subField.first, subField.second, attribute); if (errCode != E_OK) { - LOGE("[Schema][ParseDefine] CheckSchemaDefineItemDecideAttribute Fail, Path=%s.", - SchemaUtils::FieldPathString(subField.first).c_str()); + LOGE("[Schema][ParseDefine] CheckSchemaDefineItemDecideAttribute Fail."); return -E_SCHEMA_PARSE_FAIL; } // If everything ok, insert this schema item into schema define @@ -561,8 +558,7 @@ int SchemaObject::ParseCheckSchemaDefine(const JsonObject& inJsonObject) // Deal with the nestpath and check depth limitation if (subField.second == FieldType::INTERNAL_FIELD_OBJECT) { if (depth == SchemaConstant::SCHEMA_FEILD_PATH_DEPTH_MAX - 1) { // Minus 1 to be the boundary - LOGE("[Schema][ParseDefine] Path=%s is INTERNAL_FIELD_OBJECT but reach schema depth limitation.", - SchemaUtils::FieldPathString(subField.first).c_str()); + LOGE("[Schema][ParseDefine] node is INTERNAL_FIELD_OBJECT but reach schema depth limitation."); return -E_SCHEMA_PARSE_FAIL; } nestPathCurDepth.insert(subField.first); @@ -575,7 +571,7 @@ int SchemaObject::ParseCheckSchemaDefine(const JsonObject& inJsonObject) } if (fieldNameCount > SchemaConstant::SCHEMA_FEILD_NAME_COUNT_MAX) { // Check Field Count Here - LOGE("[Schema][ParseDefine] FieldName count=%u exceed the limitation.", fieldNameCount); + LOGE("[Schema][ParseDefine] FieldName count=%" PRIu32 " exceed the limitation.", fieldNameCount); return -E_SCHEMA_PARSE_FAIL; } return E_OK; @@ -590,7 +586,7 @@ int SchemaObject::CheckSchemaDefineItemDecideAttribute(const JsonObject& inJsonO } int errCode = SchemaUtils::CheckFieldName(inPath.back()); if (errCode != E_OK) { - LOGE("[Schema][CheckItemDecideAttr] Invalid fieldName=%s, errCode=%d.", inPath.back().c_str(), errCode); + LOGE("[Schema][CheckItemDecideAttr] Invalid fieldName, errCode=%d.", errCode); return -E_SCHEMA_PARSE_FAIL; } if (inType == FieldType::LEAF_FIELD_STRING) { @@ -693,15 +689,15 @@ int SchemaObject::ParseCheckEachIndexFromStringArray(const std::vector SchemaConstant::SCHEMA_FEILD_PATH_DEPTH_MAX) { - LOGE("[Schema][ParseEachIndex] Root not indexable or path=%s depth exceed limit.", eachPathStr.c_str()); + LOGE("[Schema][ParseEachIndex] Root not indexable or path depth exceed limit."); return -E_SCHEMA_PARSE_FAIL; } if (indexPathSet.count(eachPath) != 0) { - LOGE("[Schema][ParseEachIndex] IndexPath=%s Duplicated.", eachPathStr.c_str()); + LOGE("[Schema][ParseEachIndex] IndexPath Duplicated."); return -E_SCHEMA_PARSE_FAIL; } indexPathVec.push_back(eachPath); @@ -713,7 +709,7 @@ int SchemaObject::ParseCheckEachIndexFromStringArray(const std::vector & uint32_t depth = eachPath.size() - 1; // minus 1 to change depth count from zero std::string eachPathStr = SchemaUtils::FieldPathString(eachPath); if (schemaDefine_.count(depth) == 0) { - LOGE("[Schema][CheckIndexable] No schema define of this depth, path=%s.", eachPathStr.c_str()); + LOGE("[Schema][CheckIndexable] No schema define of this depth."); return -E_SCHEMA_PARSE_FAIL; } if (schemaDefine_[depth].count(eachPath) == 0) { - LOGE("[Schema][CheckIndexable] No such path in schema define, path=%s.", eachPathStr.c_str()); + LOGE("[Schema][CheckIndexable] No such path in schema define."); return -E_SCHEMA_PARSE_FAIL; } if (!schemaDefine_[depth][eachPath].isIndexable) { - LOGE("[Schema][CheckIndexable] Path=%s is not indexable.", eachPathStr.c_str()); + LOGE("[Schema][CheckIndexable] Path is not indexable."); return -E_SCHEMA_PARSE_FAIL; } // Save this indexField to indexInfo @@ -827,15 +823,13 @@ int SchemaObject::CompareSchemaDefineByDepth(const SchemaDefine &oldDefine, cons // Looking for incompatible : new define should at least contain all field the old define hold for (auto &entry : oldDefine) { if (newDefine.count(entry.first) == 0) { - LOGE("[Schema][CompareDefineDepth] fieldpath=%s not found in new schema.", - SchemaUtils::FieldPathString(entry.first).c_str()); + LOGE("[Schema][CompareDefineDepth] fieldpath not found in new schema."); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } // SchemaAttribute require to be equal exactly int errCode = CompareSchemaAttribute(entry.second, newDefine.at(entry.first)); if (errCode != -E_SCHEMA_EQUAL_EXACTLY) { - LOGE("[Schema][CompareDefineDepth] Attribute mismatch at fieldpath=%s.", - SchemaUtils::FieldPathString(entry.first).c_str()); + LOGE("[Schema][CompareDefineDepth] Attribute mismatch at fieldpath."); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -845,9 +839,8 @@ int SchemaObject::CompareSchemaDefineByDepth(const SchemaDefine &oldDefine, cons continue; } if (!IsExtraFieldConformToCompatibility(entry.second)) { - LOGE("[Schema][CompareDefineDepth] ExtraField=%s, {notnull=%d, default=%d}, not conform compatibility.", - SchemaUtils::FieldPathString(entry.first).c_str(), entry.second.hasNotNullConstraint, - entry.second.hasDefaultValue); + LOGE("[Schema][CompareDefineDepth] ExtraField, {notnull=%d, default=%d}, not conform compatibility.", + entry.second.hasNotNullConstraint, entry.second.hasDefaultValue); return -E_SCHEMA_UNEQUAL_INCOMPATIBLE; } } @@ -966,12 +959,12 @@ int SchemaObject::CompareSchemaIndexes(const SchemaObject &newSchema, IndexDiffe // Find the increase and change index for (const auto &entry : newSchema.schemaIndexes_) { if (schemaIndexes_.count(entry.first) == 0) { - LOGD("[Schema][CompareIndex] Increase indexName=%s.", SchemaUtils::FieldPathString(entry.first).c_str()); + LOGD("[Schema][CompareIndex] Increase indexName."); indexDiffer.increase[entry.first] = entry.second; } else { // Both schema have same IndexName, Check whether indexInfo differs if (!IsIndexInfoExactlyEqual(entry.second, schemaIndexes_.at(entry.first))) { - LOGD("[Schema][CompareIndex] Change indexName=%s.", SchemaUtils::FieldPathString(entry.first).c_str()); + LOGD("[Schema][CompareIndex] Change indexName."); indexDiffer.change[entry.first] = entry.second; } } @@ -979,7 +972,7 @@ int SchemaObject::CompareSchemaIndexes(const SchemaObject &newSchema, IndexDiffe // Find the decrease index for (const auto &entry : schemaIndexes_) { if (newSchema.schemaIndexes_.count(entry.first) == 0) { - LOGD("[Schema][CompareIndex] Decrease indexName=%s.", SchemaUtils::FieldPathString(entry.first).c_str()); + LOGD("[Schema][CompareIndex] Decrease indexName."); indexDiffer.decrease.insert(entry.first); } } @@ -1098,7 +1091,7 @@ int SchemaObject::CheckValue(const ValueObject &inValue, std::set &la std::map subPathType; int errCode = inValue.GetSubFieldPathAndType(nestPathCurDepth, subPathType); // Value field of current depth if (errCode != E_OK && errCode != -E_INVALID_PATH) { // E_INVALID_PATH for path not exist - LOGE("[Schema][CheckValue] GetSubFieldPathAndType Fail=%d, Depth=%u.", errCode, depth); + LOGE("[Schema][CheckValue] GetSubFieldPathAndType Fail=%d, Depth=%" PRIu32, errCode, depth); return -E_VALUE_MISMATCH_FEILD_TYPE; } nestPathCurDepth.clear(); // Clear it for collecting new nestPath @@ -1119,8 +1112,8 @@ int SchemaObject::CheckValue(const ValueObject &inValue, std::set &la } errCode = CheckValueBySchemaItem(schemaItem, subPathType, lackingPaths); if (errCode != -E_VALUE_MATCH) { - LOGE("[Schema][CheckValue] Path=%s, schema{NotNull=%d,Default=%d,Type=%s}, Value{Type=%s}, errCode=%d.", - SchemaUtils::FieldPathString(schemaItem.first).c_str(), schemaItem.second.hasNotNullConstraint, + LOGE("[Schema][CheckValue] Schema{NotNull=%d,Default=%d,Type=%s}, Value{Type=%s}, errCode=%d.", + schemaItem.second.hasNotNullConstraint, schemaItem.second.hasDefaultValue, SchemaUtils::FieldTypeString(schemaItem.second.type).c_str(), ValueFieldType(subPathType, schemaItem.first).c_str(), errCode); return errCode; @@ -1144,8 +1137,7 @@ int SchemaObject::AmendValueIfNeed(ValueObject &inValue, const std::set -#include -#include + #include +#include +#include +#include + #include "db_errno.h" #include "log_print.h" #include "schema_constant.h" diff --git a/frameworks/libs/distributeddb/common/src/task_pool_impl.h b/frameworks/libs/distributeddb/common/src/task_pool_impl.h index 7e533a2d64e..97440afc4b3 100644 --- a/frameworks/libs/distributeddb/common/src/task_pool_impl.h +++ b/frameworks/libs/distributeddb/common/src/task_pool_impl.h @@ -16,11 +16,12 @@ #ifndef TASK_POOL_IMPL_H #define TASK_POOL_IMPL_H -#include -#include #include #include +#include #include +#include + #include "task_pool.h" #include "task_queue.h" diff --git a/frameworks/libs/distributeddb/common/src/time_tick_monitor.h b/frameworks/libs/distributeddb/common/src/time_tick_monitor.h index 0bb42c5d0c2..3a53f32aa31 100644 --- a/frameworks/libs/distributeddb/common/src/time_tick_monitor.h +++ b/frameworks/libs/distributeddb/common/src/time_tick_monitor.h @@ -16,10 +16,11 @@ #ifndef TIME_TICK_MONITOR_H #define TIME_TICK_MONITOR_H -#include "runtime_context.h" #include "db_types.h" -#include "platform_specific.h" #include "macro_utils.h" +#include "runtime_context.h" +#include "platform_specific.h" + namespace DistributedDB { class TimeTickMonitor final { diff --git a/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp b/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp index f670ce3182d..6f6166e19a7 100644 --- a/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp +++ b/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp @@ -77,7 +77,6 @@ NotificationChain::Listener *UserChangeMonitor::RegisterUserChangedListerner(con int UserChangeMonitor::PrepareNotifierChain() { - int errCode = E_OK; std::unique_lock lockGuard(userChangeMonitorLock_); if (userNotifier_ != nullptr) { return E_OK; @@ -86,7 +85,7 @@ int UserChangeMonitor::PrepareNotifierChain() if (userNotifier_ == nullptr) { return -E_OUT_OF_MEMORY; } - errCode = userNotifier_->RegisterEventType(USER_ACTIVE_EVENT); + int errCode = userNotifier_->RegisterEventType(USER_ACTIVE_EVENT); if (errCode != E_OK) { goto ERROR_HANDLE; } diff --git a/frameworks/libs/distributeddb/interfaces/include/get_query_info.h b/frameworks/libs/distributeddb/interfaces/include/get_query_info.h index 5adaf9d91b7..bb380290e5a 100644 --- a/frameworks/libs/distributeddb/interfaces/include/get_query_info.h +++ b/frameworks/libs/distributeddb/interfaces/include/get_query_info.h @@ -27,4 +27,4 @@ public: } }; } -#endif \ No newline at end of file +#endif // DISTRIBUTEDDB_GET_QUERY_INFO_H \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/src/generic_kvdb.cpp b/frameworks/libs/distributeddb/storage/src/generic_kvdb.cpp index 556a3fdc051..c1e681e5675 100644 --- a/frameworks/libs/distributeddb/storage/src/generic_kvdb.cpp +++ b/frameworks/libs/distributeddb/storage/src/generic_kvdb.cpp @@ -95,15 +95,14 @@ void GenericKvDB::ReleaseDBConnection(GenericKvDBConnection *connection) SetConnectionFlag(false); } - connectMutex_.lock(); if (connection != nullptr) { - connection->SetSafeDeleted(); - DelConnection(connection); - DecreaseConnectionCounter(); - connectMutex_.unlock(); + { + std::lock_guard lock(connectMutex_); + connection->SetSafeDeleted(); + DelConnection(connection); + DecreaseConnectionCounter(); + } DecObjRef(this); - } else { - connectMutex_.unlock(); } } diff --git a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp index 56d61937afc..d4148e27001 100644 --- a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp +++ b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_commit_notify_data.cpp @@ -139,7 +139,7 @@ const std::list SingleVerNaturalStoreCommitNotifyData::FilterEntriesByKey const std::list &entries, const Key &filterKey, int &errCode) { errCode = E_OK; - if (filterKey.size() == 0) { + if (filterKey.empty()) { return entries; } std::list filterEntries; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp index 3c3e075920b..a5b63634d2c 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.cpp @@ -298,7 +298,7 @@ int SQLiteSingleVerStorageEngine::AttachMainDbAndCacheDb(SQLiteSingleVerStorageE } uint64_t maxVersion = 0; - errCode = handle->GetMaxVersionIncacheDb(maxVersion); + errCode = handle->GetMaxVersionInCacheDb(maxVersion); if (errCode != E_OK || maxVersion < CACHE_RECORD_DEFAULT_VERSION) { maxVersion = CACHE_RECORD_DEFAULT_VERSION; } @@ -430,7 +430,7 @@ int SQLiteSingleVerStorageEngine::InitExecuteMigrate(SQLiteSingleVerStorageExecu // Has been attach, maybe ever crashed, need update version executorState_ == ExecutorState::CACHE_ATTACH_MAIN) { uint64_t maxVersion = 0; - errCode = handle->GetMaxVersionIncacheDb(maxVersion); + errCode = handle->GetMaxVersionInCacheDb(maxVersion); if (errCode != E_OK || maxVersion < CACHE_RECORD_DEFAULT_VERSION) { maxVersion = CACHE_RECORD_DEFAULT_VERSION; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h index ddc9154c151..92eafb275e5 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h @@ -206,7 +206,7 @@ public: std::vector &dataItems); int GetMinVersionCacheData(std::vector &dataItems, uint64_t &maxVerIncurCacheDb) const; - int GetMaxVersionIncacheDb(uint64_t &maxVersion) const; + int GetMaxVersionInCacheDb(uint64_t &maxVersion) const; int AttachMainDbAndCacheDb(CipherType type, const CipherPassword &passwd, const std::string &attachDbAbsPath, EngineState engineState); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp index 62e8f57afa7..f236a4fdb81 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_cache.cpp @@ -224,7 +224,7 @@ int SQLiteSingleVerStorageExecutor::AttachMainDbAndCacheDb(CipherType type, cons return errCode; } -int SQLiteSingleVerStorageExecutor::GetMaxVersionIncacheDb(uint64_t &maxVersion) const +int SQLiteSingleVerStorageExecutor::GetMaxVersionInCacheDb(uint64_t &maxVersion) const { sqlite3_stmt *statement = nullptr; std::string sql; @@ -238,24 +238,18 @@ int SQLiteSingleVerStorageExecutor::GetMaxVersionIncacheDb(uint64_t &maxVersion) int errCode = SQLiteUtils::GetStatement(dbHandle_, sql, statement); if (errCode != E_OK) { - LOGE("GetStatement fail when get max version in cache db! errCode = [%d]", errCode); - goto END; + LOGE("GetStatement fail when get max version in cache db"); + return CheckCorruptedStatus(errCode); } - do { - errCode = SQLiteUtils::StepWithRetry(statement, isMemDb_); - if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - maxVersion = static_cast(sqlite3_column_int64(statement, 0)); - } else if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) { - errCode = E_OK; - break; - } else { - LOGE("SQLite step failed:%d", errCode); - break; - } - } while (true); - -END: + errCode = SQLiteUtils::StepWithRetry(statement, isMemDb_); + if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + maxVersion = static_cast(sqlite3_column_int64(statement, 0)); + errCode = E_OK; + } else if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) { + maxVersion = 0; + errCode = E_OK; + } SQLiteUtils::ResetStatement(statement, true, errCode); return CheckCorruptedStatus(errCode); } @@ -274,7 +268,7 @@ int SQLiteSingleVerStorageExecutor::MigrateDataItem(DataItem &dataItem, NotifyMi } // after solving conflict, the item should not be saved into mainDB if (notify.dataStatus.isDefeated) { - LOGD("Data status is defeated:%d", errCode); + LOGD("Data status is defeated"); return errCode; } bool isUpdate = notify.dataStatus.preStatus != DataStatus::NOEXISTED; @@ -476,7 +470,7 @@ int SQLiteSingleVerStorageExecutor::VacuumLocalData() const int errCode = SQLiteUtils::ExecuteRawSQL(dbHandle_, sql); if (errCode != E_OK) { - LOGE("SQLite sync mode failed: %d", errCode); + LOGE("[SingleVerExe] vaccum local data failed: %d", errCode); } return CheckCorruptedStatus(errCode); -- Gitee From 8d135a2ae0b9a3e3c1f76870ee5b72d368bcdc4e Mon Sep 17 00:00:00 2001 From: wbq_sky Date: Mon, 26 Sep 2022 14:46:12 +0800 Subject: [PATCH 04/14] fix the review issues Signed-off-by: wbq_sky --- .../interfaces/src/intercepted_data_impl.cpp | 3 + .../src/generic_single_ver_kv_entry.cpp | 67 +++++++------------ .../storage/src/generic_single_ver_kv_entry.h | 4 +- 3 files changed, 30 insertions(+), 44 deletions(-) diff --git a/frameworks/libs/distributeddb/interfaces/src/intercepted_data_impl.cpp b/frameworks/libs/distributeddb/interfaces/src/intercepted_data_impl.cpp index 3bfb06d1571..97042fd5bf9 100644 --- a/frameworks/libs/distributeddb/interfaces/src/intercepted_data_impl.cpp +++ b/frameworks/libs/distributeddb/interfaces/src/intercepted_data_impl.cpp @@ -178,6 +178,9 @@ void InterceptedDataImpl::GetKvEntries() { for (size_t i = 0; i < dataItems_.size(); ++i) { const auto &kvEntry = dataItems_[i]; + if (kvEntry == nullptr) { + continue; + } if ((kvEntry->GetFlag() & DataItem::DELETE_FLAG) == 0) { // For deleted data, do not modify. kvEntries_.push_back({ kvEntry->GetKey(), kvEntry->GetValue() }); indexes_.push_back(i); diff --git a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp index f943964775a..a8be5246de7 100644 --- a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp +++ b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.cpp @@ -118,11 +118,7 @@ int GenericSingleVerKvEntry::SerializeData(Parcel &parcel, uint32_t targetVersio if (errCode != E_OK) { return errCode; } - errCode = AdaptToVersion(OperType::SERIALIZE, targetVersion, parcel, len); - if (errCode != E_OK) { - return errCode; - } - return errCode; + return AdaptToVersion(OperType::SERIALIZE, targetVersion, parcel, len); } int GenericSingleVerKvEntry::SerializeDatas(const std::vector &kvEntries, Parcel &parcel, @@ -194,30 +190,29 @@ int GenericSingleVerKvEntry::DeSerializeData(Parcel &parcel) int GenericSingleVerKvEntry::DeSerializeDatas(std::vector &kvEntries, Parcel &parcel) { - uint64_t len = 0; uint32_t size = 0; - len += parcel.ReadUInt32(size); + uint64_t len = parcel.ReadUInt32(size); if (size > DBConstant::MAX_NORMAL_PACK_ITEM_SIZE) { len = 0; - goto END; - } - parcel.EightByteAlign(); - len = BYTE_8_ALIGN(len); - for (uint32_t i = 0; i < size; i++) { - auto kvEntry = new (std::nothrow) GenericSingleVerKvEntry(); - if (kvEntry == nullptr) { - LOGE("Create kvEntry failed."); - len = 0; - goto END; - } - len += kvEntry->DeSerializeData(parcel); - kvEntries.push_back(kvEntry); - if (len > INT32_MAX || parcel.IsError()) { - len = 0; - goto END; + } else { + parcel.EightByteAlign(); + len = BYTE_8_ALIGN(len); + for (uint32_t i = 0; i < size; i++) { + auto kvEntry = new (std::nothrow) GenericSingleVerKvEntry(); + if (kvEntry == nullptr) { + LOGE("Create kvEntry failed."); + len = 0; + break; + } + len += kvEntry->DeSerializeData(parcel); + kvEntries.push_back(kvEntry); + if (len > INT32_MAX || parcel.IsError()) { + len = 0; + break; + } } } -END: + if (len == 0) { for (auto &kvEntry : kvEntries) { delete kvEntry; @@ -228,7 +223,7 @@ END: } int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVersion, Parcel &parcel, - uint64_t &datalen) + uint64_t &dataLen) { if (targetVersion < SOFTWARE_VERSION_EARLIEST || targetVersion > SOFTWARE_VERSION_CURRENT) { return -E_VERSION_NOT_SUPPORT; @@ -239,7 +234,7 @@ int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVe errCode = SerializeDataByVersion(targetVersion, parcel); break; case OperType::DESERIALIZE: - errCode = DeSerializeByVersion(targetVersion, parcel, datalen); + errCode = DeSerializeByVersion(targetVersion, parcel, dataLen); break; default: LOGE("Unknown upgrade serialize oper!"); @@ -264,22 +259,10 @@ int GenericSingleVerKvEntry::AdaptToVersion(OperType operType, uint32_t targetVe int GenericSingleVerKvEntry::SerializeDataByFirstVersion(Parcel &parcel) const { - int errCode = parcel.WriteVectorChar(dataItem_.key); - if (errCode != E_OK) { - return errCode; - } - errCode = parcel.WriteVectorChar(dataItem_.value); - if (errCode != E_OK) { - return errCode; - } - errCode = parcel.WriteUInt64(dataItem_.timestamp); - if (errCode != E_OK) { - return errCode; - } - errCode = parcel.WriteUInt64(dataItem_.flag); - if (errCode != E_OK) { - return errCode; - } + (void)parcel.WriteVectorChar(dataItem_.key); + (void)parcel.WriteVectorChar(dataItem_.value); + (void)parcel.WriteUInt64(dataItem_.timestamp); + (void)parcel.WriteUInt64(dataItem_.flag); return parcel.WriteString(dataItem_.origDev); } diff --git a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h index 8fa82b0061a..8f6a430455c 100644 --- a/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h +++ b/frameworks/libs/distributeddb/storage/src/generic_single_ver_kv_entry.h @@ -25,8 +25,8 @@ namespace DistributedDB { struct CompressInfo { - CompressAlgorithm compressAlgo; - uint32_t targetVersion; + CompressAlgorithm compressAlgo = CompressAlgorithm::ZLIB; + uint32_t targetVersion = 0; }; class GenericSingleVerKvEntry : public SingleVerKvEntry { -- Gitee From 26c067ab0b5228bc83cd592395a87f54736b4a0d Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 26 Sep 2022 20:17:51 +0800 Subject: [PATCH 05/14] optimization for get sync data Signed-off-by: lianhuix --- .../src/sqlite/sqlite_query_helper.cpp | 64 ++++++++++++++++++- .../storage/src/sqlite/sqlite_query_helper.h | 1 + 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp index b1f04273611..a3c312d48e7 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp @@ -962,6 +962,64 @@ int SqliteQueryHelper::GetRelationalSyncDataQuerySql(std::string &sql, bool hasS return errCode; } +namespace { +std::string GetRelationalSyncDataQueryHeader(const std::vector &fieldNames) +{ + std::string sql = "SELECT b.data_key," + "b.device," + "b.ori_device," + "b.timestamp as " + DBConstant::TIMESTAMP_ALIAS + "," + "b.wtimestamp," + "b.flag," + "b.hash_key,"; + if (fieldNames.empty()) { // For query check. If column count changed, can be discovered. + sql += "a.*"; + } else { + for (const auto &fieldName : fieldNames) { // For query data. + sql += "a." + fieldName + ","; + } + sql.pop_back(); + } + return sql; +} + +std::string GetRelationalLogTableQuerySql(const std::string &tableName) +{ + return "SELECT * FROM " + DBConstant::RELATIONAL_PREFIX + tableName + "_log WHERE " + + "(timestamp >= ? AND timestamp < ?) AND (flag & 3 = 2)"; +} +} + +int SqliteQueryHelper::GetRelationalSyncDataQuerySqlWithSubQuery(const std::vector &fieldNames, + std::string &sql) +{ + if (!isValid_) { + return -E_INVALID_QUERY_FORMAT; + } + + if (hasPrefixKey_) { + LOGE("For relational DB query, prefix key is not supported."); + return -E_NOT_SUPPORT; + } + sql = GetRelationalSyncDataQueryHeader(fieldNames); + sql += " FROM ("; + sql += "SELECT rowid,* FROM " + tableName_ + " AS a WHERE (1 = 1) "; + { + querySql_.clear(); // clear local query sql format + int errCode = ToQuerySyncSql(true, true); + if (errCode != E_OK) { + LOGE("To query sql fail! errCode[%d]", errCode); + return errCode; + } + sql += querySql_; + } + sql += ") AS a INNER JOIN ("; + sql += GetRelationalLogTableQuerySql(tableName_); + sql += ") AS b ON (a.rowid = b.data_key)"; + sql += " ORDER BY naturalbase_rdb_aux_timestamp;"; + return E_OK; +} + int SqliteQueryHelper::GetRelationalMissQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime, const std::vector &fieldNames, sqlite3_stmt *&statement) { @@ -986,13 +1044,15 @@ int SqliteQueryHelper::GetRelationalQueryStatement(sqlite3 *dbHandle, uint64_t b const std::vector &fieldNames, sqlite3_stmt *&statement) { bool hasSubQuery = false; + std::string sql; + int errCode = E_OK; if (hasLimit_ || hasOrderBy_) { hasSubQuery = true; // Need sub query. + errCode = GetRelationalSyncDataQuerySqlWithSubQuery(fieldNames, sql); } else { isNeedOrderbyKey_ = false; // Need order by timestamp. + errCode = GetRelationalSyncDataQuerySql(sql, hasSubQuery, fieldNames); } - std::string sql; - int errCode = GetRelationalSyncDataQuerySql(sql, hasSubQuery, fieldNames); if (errCode != E_OK) { LOGE("[Query] Get SQL fail!"); return -E_INVALID_QUERY_FORMAT; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h index 0b00bbbdd91..fc73a069fa6 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h @@ -97,6 +97,7 @@ public: int GetRelationalMissQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime, const std::vector &fieldNames, sqlite3_stmt *&statement); int GetRelationalSyncDataQuerySql(std::string &sql, bool hasSubQuery, const std::vector &fieldNames); + int GetRelationalSyncDataQuerySqlWithSubQuery(const std::vector &fieldNames, std::string &sql); int GetRelationalQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime, const std::vector &fieldNames, sqlite3_stmt *&statement); -- Gitee From 2ea07d0d9d8de515a987f889d5cdee876e00f125 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Thu, 29 Sep 2022 15:19:18 +0800 Subject: [PATCH 06/14] fix review issues Signed-off-by: zwtmichael --- .../common/include/runtime_context.h | 2 +- .../common/include/user_change_monitor.h | 2 +- .../common/src/notification_chain.cpp | 2 +- .../common/src/runtime_context_impl.cpp | 4 +-- .../common/src/runtime_context_impl.h | 3 +- .../common/src/user_change_monitor.cpp | 4 +-- .../include/kv_store_delegate_manager.h | 7 ++-- .../interfaces/include/store_types.h | 2 +- .../storage/src/sync_able_engine.cpp | 34 +++++++++---------- .../storage/src/sync_able_engine.h | 2 +- .../storage/src/sync_able_kvdb.cpp | 34 +++++++++---------- .../storage/src/sync_able_kvdb.h | 2 +- .../syncer/src/commit_history_sync.h | 2 +- .../distributeddb/syncer/src/device_manager.h | 2 +- .../src/single_ver_data_message_schedule.h | 2 +- .../syncer/src/single_ver_data_sync_utils.h | 3 +- .../src/single_ver_sync_state_machine.cpp | 2 +- .../src/single_ver_sync_task_context.cpp | 2 +- .../distributeddb/syncer/src/sync_engine.h | 1 - .../distributeddb/syncer/src/sync_operation.h | 4 +-- .../syncer/src/value_slice_sync.cpp | 6 ++-- 21 files changed, 61 insertions(+), 61 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/runtime_context.h b/frameworks/libs/distributeddb/common/include/runtime_context.h index dc291b983b0..e504950ba36 100644 --- a/frameworks/libs/distributeddb/common/include/runtime_context.h +++ b/frameworks/libs/distributeddb/common/include/runtime_context.h @@ -122,7 +122,7 @@ public: virtual bool IsSyncerNeedActive(const DBProperties &properties) const = 0; - virtual NotificationChain::Listener *RegisterUserChangedListerner(const UserChangedAction &action, + virtual NotificationChain::Listener *RegisterUserChangedListener(const UserChangedAction &action, EventType event) = 0; virtual int NotifyUserChanged() const = 0; diff --git a/frameworks/libs/distributeddb/common/include/user_change_monitor.h b/frameworks/libs/distributeddb/common/include/user_change_monitor.h index 1f2837c3a8d..6f55d9e18eb 100644 --- a/frameworks/libs/distributeddb/common/include/user_change_monitor.h +++ b/frameworks/libs/distributeddb/common/include/user_change_monitor.h @@ -38,7 +38,7 @@ public: void Stop(); // Register a user changed lister, it will be callback when user changed. - NotificationChain::Listener *RegisterUserChangedListerner(const UserChangedAction &action, EventType event, + NotificationChain::Listener *RegisterUserChangedListener(const UserChangedAction &action, EventType event, int &errCode); // Notify USER_CHANGE_EVENT. diff --git a/frameworks/libs/distributeddb/common/src/notification_chain.cpp b/frameworks/libs/distributeddb/common/src/notification_chain.cpp index 4b7daea5f42..237fcf9d38a 100644 --- a/frameworks/libs/distributeddb/common/src/notification_chain.cpp +++ b/frameworks/libs/distributeddb/common/src/notification_chain.cpp @@ -221,7 +221,7 @@ void NotificationChain::ListenerChain::ClearListeners() for (auto listener : tmpSet) { // Drop the ref 1 which increased in 'BackupListenerSet()', - // the origal 1 will be dropped when user call listener->Drop(); + // the original 1 will be dropped when user call listener->Drop(); listener->KillAndDecObjRef(listener); listener = nullptr; } diff --git a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp index f9cba285a11..ed2510b2207 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp +++ b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp @@ -642,7 +642,7 @@ bool RuntimeContextImpl::IsSyncerNeedActive(const DBProperties &properties) cons return true; } -NotificationChain::Listener *RuntimeContextImpl::RegisterUserChangedListerner(const UserChangedAction &action, +NotificationChain::Listener *RuntimeContextImpl::RegisterUserChangedListener(const UserChangedAction &action, EventType event) { int errCode; @@ -656,7 +656,7 @@ NotificationChain::Listener *RuntimeContextImpl::RegisterUserChangedListerner(co return nullptr; } } - NotificationChain::Listener *listener = userChangeMonitor_->RegisterUserChangedListerner(action, event, errCode); + NotificationChain::Listener *listener = userChangeMonitor_->RegisterUserChangedListener(action, event, errCode); if ((listener == nullptr) || (errCode != E_OK)) { LOGE("Register user status changed listener failed, err = %d", errCode); return nullptr; diff --git a/frameworks/libs/distributeddb/common/src/runtime_context_impl.h b/frameworks/libs/distributeddb/common/src/runtime_context_impl.h index 5df535dd42c..7ee478543f1 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context_impl.h +++ b/frameworks/libs/distributeddb/common/src/runtime_context_impl.h @@ -25,7 +25,6 @@ #include "evloop/include/ievent_loop.h" #include "icommunicator_aggregator.h" #include "lock_status_observer.h" -#include "runtime_context.h" #include "task_pool.h" #include "time_tick_monitor.h" #include "user_change_monitor.h" @@ -112,7 +111,7 @@ public: bool IsSyncerNeedActive(const DBProperties &properties) const override; // Register a user changed lister, it will be callback when user change. - NotificationChain::Listener *RegisterUserChangedListerner(const UserChangedAction &action, + NotificationChain::Listener *RegisterUserChangedListener(const UserChangedAction &action, EventType event) override; // Notify TIME_CHANGE_EVENT. int NotifyUserChanged() const override; diff --git a/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp b/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp index 6f6166e19a7..c7d67e541c0 100644 --- a/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp +++ b/frameworks/libs/distributeddb/common/src/user_change_monitor.cpp @@ -59,7 +59,7 @@ void UserChangeMonitor::Stop() isStarted_ = false; } -NotificationChain::Listener *UserChangeMonitor::RegisterUserChangedListerner(const UserChangedAction &action, +NotificationChain::Listener *UserChangeMonitor::RegisterUserChangedListener(const UserChangedAction &action, EventType event, int &errCode) { std::shared_lock lockGuard(userChangeMonitorLock_); @@ -71,7 +71,7 @@ NotificationChain::Listener *UserChangeMonitor::RegisterUserChangedListerner(con errCode = -E_NOT_INIT; return nullptr; } - LOGI("[UserChangeMonitor] RegisterUserChangedListerner event=%d", event); + LOGI("[UserChangeMonitor] RegisterUserChangedListener event=%d", event); return userNotifier_->RegisterListener(event, action, nullptr, errCode); } diff --git a/frameworks/libs/distributeddb/interfaces/include/kv_store_delegate_manager.h b/frameworks/libs/distributeddb/interfaces/include/kv_store_delegate_manager.h index 3ea9fe14743..70f1cc6ec37 100644 --- a/frameworks/libs/distributeddb/interfaces/include/kv_store_delegate_manager.h +++ b/frameworks/libs/distributeddb/interfaces/include/kv_store_delegate_manager.h @@ -21,14 +21,15 @@ #include #include + +#include "auto_launch_export.h" +#include "iprocess_communicator.h" +#include "iprocess_system_api_adapter.h" #ifndef OMIT_MULTI_VER #include "kv_store_delegate.h" #endif #include "kv_store_nb_delegate.h" #include "store_types.h" -#include "iprocess_communicator.h" -#include "iprocess_system_api_adapter.h" -#include "auto_launch_export.h" namespace DistributedDB { class KvStoreDelegateManager final { diff --git a/frameworks/libs/distributeddb/interfaces/include/store_types.h b/frameworks/libs/distributeddb/interfaces/include/store_types.h index 6c48b9303e4..fdc8b0a8cbd 100644 --- a/frameworks/libs/distributeddb/interfaces/include/store_types.h +++ b/frameworks/libs/distributeddb/interfaces/include/store_types.h @@ -17,8 +17,8 @@ #define KV_STORE_TYPE_H #include -#include #include +#include #include "types_export.h" diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp b/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp index 670aad89002..282e3e68865 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp @@ -31,14 +31,14 @@ SyncAbleEngine::SyncAbleEngine(ISyncInterface *store) isSyncModuleActiveCheck_(false), isSyncNeedActive_(true), store_(store), - userChangeListerner_(nullptr) + userChangeListener_(nullptr) {} SyncAbleEngine::~SyncAbleEngine() { - if (userChangeListerner_ != nullptr) { - userChangeListerner_->Drop(true); - userChangeListerner_ = nullptr; + if (userChangeListener_ != nullptr) { + userChangeListener_->Drop(true); + userChangeListener_ = nullptr; } } @@ -131,14 +131,14 @@ int SyncAbleEngine::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedAct } bool isSyncDualTupleMode = store_->GetDbProperties().GetBoolProp(DBProperties::SYNC_DUAL_TUPLE_MODE, false); - if (isSyncDualTupleMode && isCheckSyncActive && !isNeedActive && (userChangeListerner_ == nullptr)) { + if (isSyncDualTupleMode && isCheckSyncActive && !isNeedActive && (userChangeListener_ == nullptr)) { // active to non_active - userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( + userChangeListener_ = RuntimeContext::GetInstance()->RegisterUserChangedListener( std::bind(&SyncAbleEngine::ChangeUserListerner, this), UserChangeMonitor::USER_ACTIVE_TO_NON_ACTIVE_EVENT); - } else if (isSyncDualTupleMode && (userChangeListerner_ == nullptr)) { + } else if (isSyncDualTupleMode && (userChangeListener_ == nullptr)) { EventType event = isNeedActive ? UserChangeMonitor::USER_ACTIVE_EVENT : UserChangeMonitor::USER_NON_ACTIVE_EVENT; - userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( + userChangeListener_ = RuntimeContext::GetInstance()->RegisterUserChangedListener( std::bind(&SyncAbleEngine::UserChangeHandle, this), event); } return errCode; @@ -159,9 +159,9 @@ void SyncAbleEngine::StopSyncerWithNoLock(bool isClosedOperation) started_ = false; } closed_ = isClosedOperation; - if (userChangeListerner_ != nullptr) { - userChangeListerner_->Drop(false); - userChangeListerner_ = nullptr; + if (userChangeListener_ != nullptr) { + userChangeListener_->Drop(false); + userChangeListener_ = nullptr; } } @@ -187,13 +187,13 @@ void SyncAbleEngine::UserChangeHandle() void SyncAbleEngine::ChangeUserListerner() { - // only active to non_active call, put into USER_NON_ACTIVE_EVENT listerner from USER_ACTIVE_TO_NON_ACTIVE_EVENT - if (userChangeListerner_ != nullptr) { - userChangeListerner_->Drop(false); - userChangeListerner_ = nullptr; + // only active to non_active call, put into USER_NON_ACTIVE_EVENT listener from USER_ACTIVE_TO_NON_ACTIVE_EVENT + if (userChangeListener_ != nullptr) { + userChangeListener_->Drop(false); + userChangeListener_ = nullptr; } - if (userChangeListerner_ == nullptr) { - userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( + if (userChangeListener_ == nullptr) { + userChangeListener_ = RuntimeContext::GetInstance()->RegisterUserChangedListener( std::bind(&SyncAbleEngine::UserChangeHandle, this), UserChangeMonitor::USER_NON_ACTIVE_EVENT); } } diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_engine.h b/frameworks/libs/distributeddb/storage/src/sync_able_engine.h index d7b949c7d14..609a55d4c9d 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_engine.h +++ b/frameworks/libs/distributeddb/storage/src/sync_able_engine.h @@ -84,7 +84,7 @@ private: ISyncInterface *store_; mutable std::mutex syncerOperateLock_; - NotificationChain::Listener *userChangeListerner_; + NotificationChain::Listener *userChangeListener_; }; } // namespace DistributedDB #endif // SYNC_ABLE_ENGINE_H \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp index c4fbdb2af7e..fc55252d7d8 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -31,7 +31,7 @@ SyncAbleKvDB::SyncAbleKvDB() isSyncModuleActiveCheck_(false), isSyncNeedActive_(true), notifyChain_(nullptr), - userChangeListerner_(nullptr) + userChangeListener_(nullptr) {} SyncAbleKvDB::~SyncAbleKvDB() @@ -41,9 +41,9 @@ SyncAbleKvDB::~SyncAbleKvDB() KillAndDecObjRef(notifyChain_); notifyChain_ = nullptr; } - if (userChangeListerner_ != nullptr) { - userChangeListerner_->Drop(true); - userChangeListerner_ = nullptr; + if (userChangeListener_ != nullptr) { + userChangeListener_->Drop(true); + userChangeListener_ = nullptr; } } @@ -180,14 +180,14 @@ int SyncAbleKvDB::StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActiv } bool isSyncDualTupleMode = syncInterface->GetDbProperties().GetBoolProp(KvDBProperties::SYNC_DUAL_TUPLE_MODE, false); - if (isSyncDualTupleMode && isCheckSyncActive && !isNeedActive && (userChangeListerner_ == nullptr)) { + if (isSyncDualTupleMode && isCheckSyncActive && !isNeedActive && (userChangeListener_ == nullptr)) { // active to non_active - userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( + userChangeListener_ = RuntimeContext::GetInstance()->RegisterUserChangedListener( std::bind(&SyncAbleKvDB::ChangeUserListerner, this), UserChangeMonitor::USER_ACTIVE_TO_NON_ACTIVE_EVENT); - } else if (isSyncDualTupleMode && (userChangeListerner_ == nullptr)) { + } else if (isSyncDualTupleMode && (userChangeListener_ == nullptr)) { EventType event = isNeedActive ? UserChangeMonitor::USER_ACTIVE_EVENT : UserChangeMonitor::USER_NON_ACTIVE_EVENT; - userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( + userChangeListener_ = RuntimeContext::GetInstance()->RegisterUserChangedListener( std::bind(&SyncAbleKvDB::UserChangeHandle, this), event); } return errCode; @@ -208,9 +208,9 @@ void SyncAbleKvDB::StopSyncerWithNoLock(bool isClosedOperation) started_ = false; } closed_ = isClosedOperation; - if (userChangeListerner_ != nullptr) { - userChangeListerner_->Drop(false); - userChangeListerner_ = nullptr; + if (userChangeListener_ != nullptr) { + userChangeListener_->Drop(false); + userChangeListener_ = nullptr; } } @@ -241,13 +241,13 @@ void SyncAbleKvDB::UserChangeHandle() void SyncAbleKvDB::ChangeUserListerner() { - // only active to non_active call, put into USER_NON_ACTIVE_EVENT listerner from USER_ACTIVE_TO_NON_ACTIVE_EVENT - if (userChangeListerner_ != nullptr) { - userChangeListerner_->Drop(false); - userChangeListerner_ = nullptr; + // only active to non_active call, put into USER_NON_ACTIVE_EVENT listener from USER_ACTIVE_TO_NON_ACTIVE_EVENT + if (userChangeListener_ != nullptr) { + userChangeListener_->Drop(false); + userChangeListener_ = nullptr; } - if (userChangeListerner_ == nullptr) { - userChangeListerner_ = RuntimeContext::GetInstance()->RegisterUserChangedListerner( + if (userChangeListener_ == nullptr) { + userChangeListener_ = RuntimeContext::GetInstance()->RegisterUserChangedListener( std::bind(&SyncAbleKvDB::UserChangeHandle, this), UserChangeMonitor::USER_NON_ACTIVE_EVENT); } } diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h index 133a242c604..56e1289a99f 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h +++ b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h @@ -126,7 +126,7 @@ private: NotificationChain *notifyChain_; mutable std::mutex syncerOperateLock_; - NotificationChain::Listener *userChangeListerner_; + NotificationChain::Listener *userChangeListener_; static const EventType REMOTE_PUSH_FINISHED; }; diff --git a/frameworks/libs/distributeddb/syncer/src/commit_history_sync.h b/frameworks/libs/distributeddb/syncer/src/commit_history_sync.h index 5c7869fe5f3..644d68732a7 100644 --- a/frameworks/libs/distributeddb/syncer/src/commit_history_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/commit_history_sync.h @@ -17,8 +17,8 @@ #define COMMIT_HISTORY_SYNC_H #ifndef OMIT_MULTI_VER -#include #include +#include #include "icommunicator.h" #include "multi_ver_kvdb_sync_interface.h" diff --git a/frameworks/libs/distributeddb/syncer/src/device_manager.h b/frameworks/libs/distributeddb/syncer/src/device_manager.h index 5b638b1e20d..d9cbfaae5e5 100644 --- a/frameworks/libs/distributeddb/syncer/src/device_manager.h +++ b/frameworks/libs/distributeddb/syncer/src/device_manager.h @@ -16,8 +16,8 @@ #ifndef DEVICE_MANAGER_H #define DEVICE_MANAGER_H -#include #include +#include #include "icommunicator.h" diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_message_schedule.h b/frameworks/libs/distributeddb/syncer/src/single_ver_data_message_schedule.h index bc9a0937e6b..f8335b9d9d0 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_message_schedule.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_message_schedule.h @@ -15,8 +15,8 @@ #ifndef SINGLE_VER_DATA_MESSAGE_SCHEDULE_H #define SINGLE_VER_DATA_MESSAGE_SCHEDULE_H -#include #include +#include #include #include diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h index 8519bcf7fda..09ff9780c55 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync_utils.h @@ -14,9 +14,10 @@ */ #ifndef SINGLE_VER_DATA_SYNC_UTIL_H #define SINGLE_VER_DATA_SYNC_UTIL_H + +#include "message.h" #include "single_ver_data_packet.h" #include "single_ver_sync_task_context.h" -#include "message.h" namespace DistributedDB { class SingleVerDataSyncUtils { public: diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index 64118156290..fc1c9b8617f 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -412,7 +412,7 @@ Event SingleVerSyncStateMachine::DoPassiveDataSyncWithSlidingWindow() } int errCode = dataSync_->SyncStart(SyncModeType::RESPONSE_PULL, context_); if (errCode != E_OK) { - LOGW("[SingleVerSyncStateMachine][DoPassiveDataSyncWithSlidingWindow] response pull send failed[%d]", errCode); + LOGE("[SingleVerSyncStateMachine][DoPassiveDataSyncWithSlidingWindow] response pull send failed[%d]", errCode); return RESPONSE_TASK_FINISHED_EVENT; } return Event::WAIT_ACK_EVENT; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp index e4daa17e994..ea6dae5b92d 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp @@ -19,8 +19,8 @@ #include "db_common.h" #include "db_dfx_adapter.h" #include "db_errno.h" -#include "log_print.h" #include "isyncer.h" +#include "log_print.h" #include "single_ver_sync_state_machine.h" #include "single_ver_sync_target.h" #include "sync_types.h" diff --git a/frameworks/libs/distributeddb/syncer/src/sync_engine.h b/frameworks/libs/distributeddb/syncer/src/sync_engine.h index 178d4858665..a4fd7466477 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_engine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_engine.h @@ -29,7 +29,6 @@ #include "task_pool.h" namespace DistributedDB { -constexpr uint16_t NEW_SEND_TASK = 1; class SyncEngine : public ISyncEngine { public: diff --git a/frameworks/libs/distributeddb/syncer/src/sync_operation.h b/frameworks/libs/distributeddb/syncer/src/sync_operation.h index 040a2fcf0c5..bb4f02c9399 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_operation.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_operation.h @@ -17,10 +17,10 @@ #define SYNC_OPERATION_H #include -#include -#include #include #include +#include +#include #include "ikvdb_sync_interface.h" #include "notification_chain.h" diff --git a/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp b/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp index 6b19fa333dc..d9490cecb64 100644 --- a/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp @@ -16,12 +16,12 @@ #ifndef OMIT_MULTI_VER #include "value_slice_sync.h" -#include "parcel.h" +#include "db_constant.h" #include "log_print.h" -#include "sync_types.h" #include "message_transform.h" +#include "parcel.h" #include "performance_analysis.h" -#include "db_constant.h" +#include "sync_types.h" namespace DistributedDB { const int ValueSliceSync::MAX_VALUE_NODE_SIZE = 100000; -- Gitee From 68a00fc8601ad0a1a5550111b11799cd6aa4ff27 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Fri, 30 Sep 2022 10:33:37 +0800 Subject: [PATCH 07/14] fix secure issue Signed-off-by: zwtmichael --- .../common/include/relational/prepared_stmt.h | 2 +- .../common/include/user_change_monitor.h | 1 - .../libs/distributeddb/common/src/auto_launch.cpp | 13 ++++++------- .../common/src/relational/prepared_stmt.cpp | 4 ++-- .../src/relational/relational_row_data_set.cpp | 3 +++ .../communicator/src/communicator_aggregator.cpp | 11 ++++------- .../communicator/src/protocol_proto.cpp | 9 +++++++-- .../communicator/src/serial_buffer.cpp | 3 ++- .../interfaces/include/kv_store_observer.h | 2 +- .../interfaces/include/relational/store_observer.h | 2 +- .../distributeddb/storage/src/sync_able_engine.cpp | 2 +- .../distributeddb/storage/src/sync_able_kvdb.cpp | 2 +- .../libs/distributeddb/syncer/src/ability_sync.cpp | 2 -- .../libs/distributeddb/syncer/src/db_ability.cpp | 6 +----- .../distributeddb/syncer/src/generic_syncer.cpp | 9 ++++++++- .../syncer/src/single_ver_data_packet.cpp | 3 +++ .../syncer/src/single_ver_sync_state_machine.cpp | 6 ++++++ .../distributeddb/syncer/src/subscribe_manager.cpp | 14 +++++++------- .../libs/distributeddb/syncer/src/sync_operation.h | 3 --- .../libs/distributeddb/syncer/src/syncer_factory.h | 1 + .../distributeddb/syncer/src/value_slice_sync.cpp | 3 --- 21 files changed, 55 insertions(+), 46 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/relational/prepared_stmt.h b/frameworks/libs/distributeddb/common/include/relational/prepared_stmt.h index 5307b08faac..38eb2fe7cc1 100644 --- a/frameworks/libs/distributeddb/common/include/relational/prepared_stmt.h +++ b/frameworks/libs/distributeddb/common/include/relational/prepared_stmt.h @@ -38,7 +38,7 @@ public: bool IsValid() const; - int CalcLength() const; + uint32_t CalcLength() const; int Serialize(Parcel &parcel) const; int DeSerialize(Parcel &parcel); diff --git a/frameworks/libs/distributeddb/common/include/user_change_monitor.h b/frameworks/libs/distributeddb/common/include/user_change_monitor.h index 6f55d9e18eb..df6d18c9a2f 100644 --- a/frameworks/libs/distributeddb/common/include/user_change_monitor.h +++ b/frameworks/libs/distributeddb/common/include/user_change_monitor.h @@ -20,7 +20,6 @@ #include "macro_utils.h" #include "notification_chain.h" -#include "platform_specific.h" #include "runtime_context.h" namespace DistributedDB { diff --git a/frameworks/libs/distributeddb/common/src/auto_launch.cpp b/frameworks/libs/distributeddb/common/src/auto_launch.cpp index 23471b2d4d8..388840b1256 100644 --- a/frameworks/libs/distributeddb/common/src/auto_launch.cpp +++ b/frameworks/libs/distributeddb/common/src/auto_launch.cpp @@ -47,12 +47,11 @@ void AutoLaunch::SetCommunicatorAggregator(ICommunicatorAggregator *aggregator) LOGI("[AutoLaunch] SetCommunicatorAggregator communicatorAggregator_ is not nullptr"); errCode = communicatorAggregator_->RegOnConnectCallback(nullptr, nullptr); if (errCode != E_OK) { - LOGW("[AutoLaunch] communicatorAggregator_->RegOnConnectCallback(nullptr, nullptr), errCode:%d", errCode); + LOGW("[AutoLaunch] RegOnConnectCallback set nullptr failed, errCode:%d", errCode); } errCode = communicatorAggregator_->RegCommunicatorLackCallback(nullptr, nullptr); if (errCode != E_OK) { - LOGW("[AutoLaunch] communicatorAggregator_->RegCommunicatorLackCallback(nullptr, nullptr), errCode:%d", - errCode); + LOGW("[AutoLaunch] RegCommunicatorLackCallback set nullptr failed, errCode:%d", errCode); } } communicatorAggregator_ = aggregator; @@ -63,13 +62,13 @@ void AutoLaunch::SetCommunicatorAggregator(ICommunicatorAggregator *aggregator) errCode = aggregator->RegOnConnectCallback(std::bind(&AutoLaunch::OnlineCallBack, this, std::placeholders::_1, std::placeholders::_2), nullptr); if (errCode != E_OK) { - LOGW("[AutoLaunch] aggregator->RegOnConnectCallback errCode:%d", errCode); + LOGW("[AutoLaunch] RegOnConnectCallback errCode:%d", errCode); } errCode = aggregator->RegCommunicatorLackCallback( std::bind(&AutoLaunch::ReceiveUnknownIdentifierCallBack, this, std::placeholders::_1, std::placeholders::_2), nullptr); if (errCode != E_OK) { - LOGW("[AutoLaunch] aggregator->RegCommunicatorLackCallback errCode:%d", errCode); + LOGW("[AutoLaunch] RegCommunicatorLackCallback errCode:%d", errCode); } } @@ -606,7 +605,7 @@ void AutoLaunch::GetConnInDoOpenMap(std::mapScheduleTask([&sema, &iter, &items, this] { int ret = OpenOneConnection(iter.second); - LOGI("[AutoLaunch] GetConnInDoOpenMap GetOneConnection errCode:%d\n", ret); + LOGI("[AutoLaunch] GetConnInDoOpenMap GetOneConnection errCode:%d", ret); if (iter.second.conn == nullptr) { sema.SendSemaphore(); LOGI("[AutoLaunch] GetConnInDoOpenMap in open thread finish SendSemaphore"); @@ -661,7 +660,7 @@ void AutoLaunch::ReceiveUnknownIdentifierCallBackTask(const std::string &identif autoLaunchItem = autoLaunchItemMap_[identifier][userId]; } int errCode = OpenOneConnection(autoLaunchItem); - LOGI("[AutoLaunch] ReceiveUnknownIdentifierCallBack GetOneConnection errCode:%d\n", errCode); + LOGI("[AutoLaunch] ReceiveUnknownIdentifierCallBack GetOneConnection errCode:%d", errCode); if (autoLaunchItem.conn == nullptr) { std::lock_guard autoLock(dataLock_); autoLaunchItemMap_[identifier][userId].state = AutoLaunchItemState::IDLE; diff --git a/frameworks/libs/distributeddb/common/src/relational/prepared_stmt.cpp b/frameworks/libs/distributeddb/common/src/relational/prepared_stmt.cpp index 1735cf1ca18..03db2eb0f28 100644 --- a/frameworks/libs/distributeddb/common/src/relational/prepared_stmt.cpp +++ b/frameworks/libs/distributeddb/common/src/relational/prepared_stmt.cpp @@ -56,7 +56,7 @@ bool PreparedStmt::IsValid() const return opCode_ == ExecutorOperation::QUERY && !sql_.empty() && bindArgs_.size() <= DBConstant::MAX_SQL_ARGS_COUNT; } -int PreparedStmt::CalcLength() const +uint32_t PreparedStmt::CalcLength() const { uint32_t length = Parcel::GetIntLen() + // current version Parcel::GetIntLen() + // opcode_ @@ -65,7 +65,7 @@ int PreparedStmt::CalcLength() const for (const auto &bindArg : bindArgs_) { length += Parcel::GetStringLen(bindArg); // bindArgs_ } - return static_cast(Parcel::GetEightByteAlign(length)); + return Parcel::GetEightByteAlign(length); } // Before call this func. You should check if the object is valid. diff --git a/frameworks/libs/distributeddb/common/src/relational/relational_row_data_set.cpp b/frameworks/libs/distributeddb/common/src/relational/relational_row_data_set.cpp index 912f6cc76c2..e602066006e 100644 --- a/frameworks/libs/distributeddb/common/src/relational/relational_row_data_set.cpp +++ b/frameworks/libs/distributeddb/common/src/relational/relational_row_data_set.cpp @@ -149,6 +149,9 @@ int RelationalRowDataSet::Insert(RelationalRowData *rowData) if (rowData == nullptr) { return -E_INVALID_ARGS; } + if ((serialLength_ + static_cast(rowData->CalcLength())) > static_cast(INT32_MAX)) { + return -E_INVALID_ARGS; + } data_.push_back(rowData); serialLength_ += rowData->CalcLength(); return E_OK; diff --git a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp index 1ec0fa24a76..c3b3df30e90 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp @@ -14,17 +14,14 @@ */ #include "communicator_aggregator.h" -#include -#include -#include -#include + #include "hash.h" -#include "log_print.h" -#include "db_common.h" #include "communicator.h" +#include "communicator_linker.h" +#include "db_common.h" #include "endian_convert.h" +#include "log_print.h" #include "protocol_proto.h" -#include "communicator_linker.h" namespace DistributedDB { namespace { diff --git a/frameworks/libs/distributeddb/communicator/src/protocol_proto.cpp b/frameworks/libs/distributeddb/communicator/src/protocol_proto.cpp index 8d27be2af6c..ed79435483b 100644 --- a/frameworks/libs/distributeddb/communicator/src/protocol_proto.cpp +++ b/frameworks/libs/distributeddb/communicator/src/protocol_proto.cpp @@ -208,14 +208,19 @@ SerialBuffer *ProtocolProto::BuildLabelExchange(uint64_t inDistinctValue, uint64 { // Size of inLabels won't be too large. // The upper layer code(inside this communicator module) guarantee that size of each Label equals COMM_LABEL_LENGTH - uint32_t payloadLen = LABEL_VER_LEN + DISTINCT_VALUE_LEN + SEQUENCE_ID_LEN + COMM_LABEL_COUNT_LEN + + uint64_t payloadLen = LABEL_VER_LEN + DISTINCT_VALUE_LEN + SEQUENCE_ID_LEN + COMM_LABEL_COUNT_LEN + inLabels.size() * COMM_LABEL_LENGTH; + if (payloadLen > INT32_MAX) { + outErrorNo = -E_INVALID_ARGS; + return nullptr; + } SerialBuffer *buffer = new (std::nothrow) SerialBuffer(); if (buffer == nullptr) { outErrorNo = -E_OUT_OF_MEMORY; return nullptr; } - int errCode = buffer->AllocBufferByPayloadLength(payloadLen, GetCommLayerFrameHeaderLength()); + int errCode = buffer->AllocBufferByPayloadLength(static_cast(payloadLen), + GetCommLayerFrameHeaderLength()); if (errCode != E_OK) { LOGE("[Proto][BuildLabel] Alloc Fail, errCode=%d.", errCode); outErrorNo = errCode; diff --git a/frameworks/libs/distributeddb/communicator/src/serial_buffer.cpp b/frameworks/libs/distributeddb/communicator/src/serial_buffer.cpp index 4fbcd89af9f..ae75a154a46 100644 --- a/frameworks/libs/distributeddb/communicator/src/serial_buffer.cpp +++ b/frameworks/libs/distributeddb/communicator/src/serial_buffer.cpp @@ -44,7 +44,8 @@ uint32_t SerialBuffer::GetExtendHeadLength() const // In case buffer be directly send out, so padding is needed int SerialBuffer::AllocBufferByPayloadLength(uint32_t inPayloadLen, uint32_t inHeaderLen) { - if (oringinalBytes_ != nullptr || bytes_ != nullptr || externalBytes_ != nullptr) { + if (oringinalBytes_ != nullptr || bytes_ != nullptr || externalBytes_ != nullptr || + BYTE_8_ALIGN(static_cast(payloadLen_) + static_cast(headerLen_)) > INT32_MAX) { return -E_NOT_PERMIT; } diff --git a/frameworks/libs/distributeddb/interfaces/include/kv_store_observer.h b/frameworks/libs/distributeddb/interfaces/include/kv_store_observer.h index ea74c0c4b85..ab2c9a295dd 100644 --- a/frameworks/libs/distributeddb/interfaces/include/kv_store_observer.h +++ b/frameworks/libs/distributeddb/interfaces/include/kv_store_observer.h @@ -23,7 +23,7 @@ class KvStoreObserver { public: virtual ~KvStoreObserver() {} - // Databa change callback + // Data change callback virtual void OnChange(const KvStoreChangedData &data) = 0; }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/interfaces/include/relational/store_observer.h b/frameworks/libs/distributeddb/interfaces/include/relational/store_observer.h index 6072bbc6946..ea753a3ed89 100644 --- a/frameworks/libs/distributeddb/interfaces/include/relational/store_observer.h +++ b/frameworks/libs/distributeddb/interfaces/include/relational/store_observer.h @@ -23,7 +23,7 @@ class StoreObserver { public: virtual ~StoreObserver() {} - // Databa change callback + // Data change callback virtual void OnChange(const StoreChangedData &data) = 0; }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp b/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp index 282e3e68865..fc34669a02d 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp +++ b/frameworks/libs/distributeddb/storage/src/sync_able_engine.cpp @@ -178,7 +178,7 @@ void SyncAbleEngine::UserChangeHandle() isNeedChange = (isNeedActive != isSyncNeedActive_) ? true : false; // non_active to active or active to non_active if (isNeedChange) { - StopSyncerWithNoLock(); // will drop userChangeListerner; + StopSyncerWithNoLock(); // will drop userChangeListener isSyncModuleActiveCheck_ = true; isSyncNeedActive_ = isNeedActive; StartSyncerWithNoLock(true, isNeedActive); diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp index fc55252d7d8..21edf7885b8 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -232,7 +232,7 @@ void SyncAbleKvDB::UserChangeHandle() isNeedChange = (isNeedActive != isSyncNeedActive_) ? true : false; // non_active to active or active to non_active if (isNeedChange) { - StopSyncerWithNoLock(); // will drop userChangeListerner; + StopSyncerWithNoLock(); // will drop userChangeListener isSyncModuleActiveCheck_ = true; isSyncNeedActive_ = isNeedActive; StartSyncerWithNoLock(true, isNeedActive); diff --git a/frameworks/libs/distributeddb/syncer/src/ability_sync.cpp b/frameworks/libs/distributeddb/syncer/src/ability_sync.cpp index 38359d61177..b02926d7fa8 100644 --- a/frameworks/libs/distributeddb/syncer/src/ability_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/ability_sync.cpp @@ -617,7 +617,6 @@ int AbilitySync::Serialization(uint8_t *buffer, uint32_t length, const Message * case TYPE_REQUEST: return RequestPacketSerialization(buffer, length, inMsg); case TYPE_RESPONSE: - return AckPacketSerialization(buffer, length, inMsg); case TYPE_NOTIFY: return AckPacketSerialization(buffer, length, inMsg); default: @@ -635,7 +634,6 @@ int AbilitySync::DeSerialization(const uint8_t *buffer, uint32_t length, Message case TYPE_REQUEST: return RequestPacketDeSerialization(buffer, length, inMsg); case TYPE_RESPONSE: - return AckPacketDeSerialization(buffer, length, inMsg); case TYPE_NOTIFY: return AckPacketDeSerialization(buffer, length, inMsg); default: diff --git a/frameworks/libs/distributeddb/syncer/src/db_ability.cpp b/frameworks/libs/distributeddb/syncer/src/db_ability.cpp index 54631a3ccd3..9204f52d4e2 100644 --- a/frameworks/libs/distributeddb/syncer/src/db_ability.cpp +++ b/frameworks/libs/distributeddb/syncer/src/db_ability.cpp @@ -66,11 +66,7 @@ int DbAbility::Serialize(Parcel &parcel, const DbAbility &curAbility) uint64_t value = static_cast(abilityBuff[pos]) << innerBuffOffset; dstBuf[buffOffset] = dstBuf[buffOffset] | value; } - int errCode = parcel.WriteVector(dstBuf); - if (errCode != E_OK) { - return errCode; - } - return E_OK; + return parcel.WriteVector(dstBuf); } int DbAbility::DeSerialize(Parcel &parcel, DbAbility &curAbility) diff --git a/frameworks/libs/distributeddb/syncer/src/generic_syncer.cpp b/frameworks/libs/distributeddb/syncer/src/generic_syncer.cpp index 80ea17ad298..f4657850967 100644 --- a/frameworks/libs/distributeddb/syncer/src/generic_syncer.cpp +++ b/frameworks/libs/distributeddb/syncer/src/generic_syncer.cpp @@ -149,7 +149,7 @@ int GenericSyncer::Close(bool isClosedOperation) timeHelper_ = nullptr; metadata_ = nullptr; } - LOGW("[Syncer] Syncer[%s] don't need to close, because it has no been init", label_.c_str()); + LOGW("[Syncer] Syncer[%s] don't need to close, because it has not been init", label_.c_str()); return -E_NOT_INIT; } initialized_ = false; @@ -360,6 +360,10 @@ int GenericSyncer::InitMetaData(ISyncInterface *syncInterface) } metadata_ = std::make_shared(); + if (metadata_ == nullptr) { + LOGE("[Syncer] metadata make shared failed"); + return -E_OUT_OF_MEMORY; + } int errCode = metadata_->Initialize(syncInterface); if (errCode != E_OK) { LOGE("[Syncer] metadata Initializeate failed! err %d.", errCode); @@ -376,6 +380,9 @@ int GenericSyncer::InitTimeHelper(ISyncInterface *syncInterface) } timeHelper_ = std::make_shared(); + if (timeHelper_ == nullptr) { + return -E_OUT_OF_MEMORY; + } int errCode = timeHelper_->Initialize(syncInterface, metadata_); if (errCode != E_OK) { LOGE("[Syncer] TimeHelper init failed! err:%d.", errCode); diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_packet.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_data_packet.cpp index 135398cd3d0..64f85ba2551 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_packet.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_packet.cpp @@ -152,6 +152,9 @@ uint32_t DataRequestPacket::CalculateLen(uint32_t messageId) const { uint64_t totalLen = GenericSingleVerKvEntry::CalculateLens( IsCompressData() ? std::vector {} : data_, version_); // for data + if (totalLen == 0) { + return 0; + } totalLen += Parcel::GetUInt64Len(); // endWaterMark totalLen += Parcel::GetUInt64Len(); // localWaterMark totalLen += Parcel::GetUInt64Len(); // peerWaterMark diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index fc1c9b8617f..5295d2222e0 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -129,6 +129,12 @@ int SingleVerSyncStateMachine::Initialize(ISyncTaskContext *context, ISyncInterf timeSync_ = std::make_unique(); dataSync_ = std::make_shared(); abilitySync_ = std::make_unique(); + if ((timeSync_ == nullptr) || (dataSync_ == nullptr) || (abilitySync_ == nullptr)) { + timeSync_ = nullptr; + dataSync_ = nullptr; + abilitySync_ = nullptr; + return -E_OUT_OF_MEMORY; + } errCode = timeSync_->Initialize(communicator, metaData, syncInterface, context->GetDeviceId()); if (errCode != E_OK) { diff --git a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp index 40991b6ec88..f0facc99739 100644 --- a/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp +++ b/frameworks/libs/distributeddb/syncer/src/subscribe_manager.cpp @@ -77,7 +77,7 @@ int SubscribeManager::ActiveLocalSubscribeQuery(const std::string &device, const } if (unFinishedLocalAutoSubMap_.find(device) != unFinishedLocalAutoSubMap_.end() && unFinishedLocalAutoSubMap_[device].find(queryId) != unFinishedLocalAutoSubMap_[device].end()) { - unFinishedLocalAutoSubMap_[device].erase(queryId); + unFinishedLocalAutoSubMap_[device].erase(queryId); } return errCode; } @@ -147,12 +147,12 @@ void SubscribeManager::RemoveLocalSubscribeQuery(const std::string &device, cons RemoveSubscribeQuery(device, queryId, localSubscribeMap_, localSubscribeTotalMap_); if (unFinishedLocalAutoSubMap_.find(device) != unFinishedLocalAutoSubMap_.end() && unFinishedLocalAutoSubMap_[device].find(queryId) != unFinishedLocalAutoSubMap_[device].end()) { - unFinishedLocalAutoSubMap_[device].erase(queryId); - LOGI("[SubscribeManager] dev=%s,queryId=%s delete from UnFinishedMap", STR_MASK(device), STR_MASK(queryId)); - if (unFinishedLocalAutoSubMap_[device].size() == 0) { - LOGI("[SubscribeManager] dev=%s delete from unFinish map", STR_MASK(device)); - unFinishedLocalAutoSubMap_.erase(device); - } + unFinishedLocalAutoSubMap_[device].erase(queryId); + LOGI("[SubscribeManager] dev=%s,queryId=%s delete from UnFinishedMap", STR_MASK(device), STR_MASK(queryId)); + if (unFinishedLocalAutoSubMap_[device].size() == 0) { + LOGI("[SubscribeManager] dev=%s delete from unFinish map", STR_MASK(device)); + unFinishedLocalAutoSubMap_.erase(device); + } } } diff --git a/frameworks/libs/distributeddb/syncer/src/sync_operation.h b/frameworks/libs/distributeddb/syncer/src/sync_operation.h index bb4f02c9399..c10013d2aa5 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_operation.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_operation.h @@ -138,9 +138,6 @@ private: // called by destruction void Finalize(); - // Transfer sync mode from interface to inner - void TransferQuerySyncMode(); - // The device list const std::vector devices_; diff --git a/frameworks/libs/distributeddb/syncer/src/syncer_factory.h b/frameworks/libs/distributeddb/syncer/src/syncer_factory.h index 20e1c45e2bd..e235251d869 100644 --- a/frameworks/libs/distributeddb/syncer/src/syncer_factory.h +++ b/frameworks/libs/distributeddb/syncer/src/syncer_factory.h @@ -26,6 +26,7 @@ public: // Product a ISyncer for the given type // type can be : IKvDBSyncInterface::SYNC_SVD // IKvDBSyncInterface::SYNC_MVD + // IKvDBSyncInterface::SYNC_RELATION static std::shared_ptr GetSyncer(int type); }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp b/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp index d9490cecb64..62a822b4b7f 100644 --- a/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/value_slice_sync.cpp @@ -292,9 +292,6 @@ int ValueSliceSync::AckRecvCallback(const MultiVerSyncTaskContext *context, cons } LOGD("ValueSliceSync::AckRecvCallback PutValueSlice finished, src=%s{private}, errCode = %d", context->GetDeviceId().c_str(), errCode); - if (errCode != E_OK) { - return errCode; - } return errCode; } -- Gitee From b841a3b84ee20aade1273183b11fea9427c10b70 Mon Sep 17 00:00:00 2001 From: zqq Date: Sat, 17 Sep 2022 10:31:34 +0800 Subject: [PATCH 08/14] reset timer when get sync data Signed-off-by: zqq --- .../syncer/src/isync_state_machine.h | 6 ++ .../src/multi_ver_sync_state_machine.cpp | 2 +- .../syncer/src/multi_ver_sync_state_machine.h | 2 +- .../syncer/src/single_ver_data_sync.cpp | 2 + .../src/single_ver_sync_state_machine.cpp | 2 +- .../src/single_ver_sync_state_machine.h | 3 +- .../src/single_ver_sync_task_context.cpp | 10 +++ .../syncer/src/single_ver_sync_task_context.h | 3 + .../syncer/src/sync_state_machine.cpp | 80 ++++++++++++++++++- .../syncer/src/sync_state_machine.h | 19 ++++- 10 files changed, 120 insertions(+), 9 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h index 137f0351c75..a2dee92ccc1 100644 --- a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h @@ -59,6 +59,12 @@ public: // check if need trigger query auto sync and get query from inMsg virtual bool IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyncObject &query) = 0; + + // start a timer to ResetWatchDog when get data and send notify ack if need + virtual void StartFeedDogForGetData(uint32_t sessionId) = 0; + + // start a timer to ResetWatchDog when get data + virtual void StopFeedDogForGetData() = 0; }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp index a8819c57d1a..f36417c3839 100644 --- a/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.cpp @@ -303,7 +303,7 @@ int MultiVerSyncStateMachine::PrepareNextSyncTask() return StartSyncInner(); } -void MultiVerSyncStateMachine::SendSaveDataNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) +void MultiVerSyncStateMachine::SendNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) { (void)sessionId; (void)sequenceId; diff --git a/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.h index aa4a663111b..e8583a75b89 100644 --- a/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/multi_ver_sync_state_machine.h @@ -72,7 +72,7 @@ protected: int PrepareNextSyncTask() override; // Called by StartSaveDataNotifyTimer, used to send a save data notify packet - void SendSaveDataNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) override; + void SendNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) override; bool IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyncObject &query) override; diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp index c12967fce16..291518d1f41 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp @@ -289,7 +289,9 @@ int SingleVerDataSync::GetDataWithPerformanceRecord(SingleVerSyncTaskContext *co if (performance != nullptr) { performance->StepTimeRecordStart(PT_TEST_RECORDS::RECORD_READ_DATA); } + context->StartFeedDogForGetData(context->GetResponseSessionId()); int errCode = GetData(context, syncOutData.entries, packetSize); + context->StopFeedDogForGetData(); if (performance != nullptr) { performance->StepTimeRecordEnd(PT_TEST_RECORDS::RECORD_READ_DATA); } diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp index 5295d2222e0..1ad51ce9f52 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.cpp @@ -288,7 +288,7 @@ int SingleVerSyncStateMachine::PrepareNextSyncTask() return E_OK; } -void SingleVerSyncStateMachine::SendSaveDataNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) +void SingleVerSyncStateMachine::SendNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) { dataSync_->SendSaveDataNotifyPacket(context_, std::min(context_->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT), sessionId, sequenceId, inMsgId); diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h index d7fbb6f7feb..18654f9caa4 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_state_machine.h @@ -97,7 +97,6 @@ public: uint64_t &outValue); void InnerErrorAbort(uint32_t sessionId) override; - protected: // Step the SingleVerSyncStateMachine void SyncStep() override; @@ -123,7 +122,7 @@ protected: int PrepareNextSyncTask() override; // Called by StartSaveDataNotifyTimer, used to send a save data notify packet - void SendSaveDataNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) override; + void SendNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) override; int TimeMarkSyncRecv(const Message *inMsg); diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp index ea6dae5b92d..fd7f3f71b0d 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.cpp @@ -582,4 +582,14 @@ bool SingleVerSyncTaskContext::IsCurrentSyncTaskCanBeSkippedInner(const SyncOper } return false; } + +void SingleVerSyncTaskContext::StartFeedDogForGetData(uint32_t sessionId) +{ + stateMachine_->StartFeedDogForGetData(sessionId); +} + +void SingleVerSyncTaskContext::StopFeedDogForGetData() +{ + stateMachine_->StopFeedDogForGetData(); +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.h b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.h index e74b77c7a5b..6528c11a727 100644 --- a/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.h +++ b/frameworks/libs/distributeddb/syncer/src/single_ver_sync_task_context.h @@ -140,6 +140,9 @@ public: virtual std::string GetDeleteSyncId() const = 0; void SetCommNormal(bool isCommNormal); + + void StartFeedDogForGetData(uint32_t sessionId); + void StopFeedDogForGetData(); protected: ~SingleVerSyncTaskContext() override; void CopyTargetData(const ISyncTarget *target, const TaskParam &taskParam) override; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index daf71d2eb2b..bc35df060c3 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -375,11 +375,11 @@ void SyncStateMachine::DoSaveDataNotify(uint32_t sessionId, uint32_t sequenceId, (void)ResetWatchDog(); } std::lock_guard innerLock(saveDataNotifyLock_); - if (saveDataNotifyCount_ >= MAXT_SAVE_DATA_NOTIFY_COUNT) { + if (saveDataNotifyCount_ >= MAX_SAVE_DATA_NOTIFY_COUNT) { StopSaveDataNotifyNoLock(); return; } - SendSaveDataNotifyPacket(sessionId, sequenceId, inMsgId); + SendNotifyPacket(sessionId, sequenceId, inMsgId); saveDataNotifyCount_++; } @@ -402,4 +402,80 @@ void SyncStateMachine::InnerErrorAbort(uint32_t sessionId) // do nothing (void) sessionId; } + +void SyncStateMachine::StartFeedDogForGetData(uint32_t sessionId) +{ + std::lock_guard lockGuard(getDataNotifyLock_); + if (getDataNotifyTimerId_ > 0) { + getDataNotifyCount_ = 0; + LOGW("[SyncStateMachine][StartFeedDogForGetData] timer has been started!"); + } + + // Incref to make sure context still alive before timer stopped. + RefObject::IncObjRef(syncContext_); + int errCode = RuntimeContext::GetInstance()->SetTimer( + SAVE_DATA_NOTIFY_INTERVAL, + [this, sessionId](TimerId timerId) { + RefObject::IncObjRef(syncContext_); + int ret = RuntimeContext::GetInstance()->ScheduleTask([this, sessionId, timerId]() { + DoGetDataNotify(sessionId); + int getDataNotifyCount = 0; + { + std::lock_guard autoLock(getDataNotifyLock_); + getDataNotifyCount = getDataNotifyCount_; + } + if (getDataNotifyCount >= MAX_SAVE_DATA_NOTIFY_COUNT) { + StopFeedDogForGetDataInner(timerId); + } + RefObject::DecObjRef(syncContext_); + }); + if (ret != E_OK) { + LOGE("[SyncStateMachine] [StartFeedDogForGetData] ScheduleTask failed errCode %d", ret); + RefObject::DecObjRef(syncContext_); + } + return ret; + }, + [this]() { RefObject::DecObjRef(syncContext_); }, + getDataNotifyTimerId_); + if (errCode != E_OK) { + LOGW("[SyncStateMachine][StartFeedDogForGetData] start timer failed err %d !", errCode); + } +} + +void SyncStateMachine::StopFeedDogForGetData() +{ + TimerId timerId = 0; + { + std::lock_guard lockGuard(getDataNotifyLock_); + timerId = getDataNotifyTimerId_; + } + if (timerId == 0) { + return; + } + StopFeedDogForGetDataInner(timerId); +} + +void SyncStateMachine::DoGetDataNotify(uint32_t sessionId) +{ + (void)ResetWatchDog(); + std::lock_guard autoLock(getDataNotifyLock_); + if (getDataNotifyCount_ >= MAX_SAVE_DATA_NOTIFY_COUNT) { + return; + } + if (sessionId != 0) { + SendNotifyPacket(sessionId, 0, DATA_SYNC_MESSAGE); + } + getDataNotifyCount_++; +} + +void SyncStateMachine::StopFeedDogForGetDataInner(TimerId timerId) +{ + std::lock_guard lockGuard(getDataNotifyLock_); + if (getDataNotifyTimerId_ == 0 || getDataNotifyTimerId_ != timerId) { + return; + } + RuntimeContext::GetInstance()->RemoveTimer(timerId); + getDataNotifyTimerId_ = 0; + getDataNotifyCount_ = 0; +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index 0a73b0cc778..5e10235b7fe 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -68,6 +68,12 @@ public: // stop timer to ResetWatchDog when sync data one (key,value) size bigger than mtu void StopFeedDogForSync(SyncDirectionFlag flag) override; + + // start a timer to ResetWatchDog when get data and send notify ack if need + void StartFeedDogForGetData(uint32_t sessionId) override; + + // start a timer to ResetWatchDog when get data and stop send notify ack if need + void StopFeedDogForGetData() override; protected: // SyncOperation is timeout, step to timeout state @@ -98,7 +104,7 @@ protected: virtual int PrepareNextSyncTask() = 0; // Called by StartSaveDataNotifyTimer, Sub class should realize this function to send a heartbeet packet - virtual void SendSaveDataNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) = 0; + virtual void SendNotifyPacket(uint32_t sessionId, uint32_t sequenceId, uint32_t inMsgId) = 0; // Used to parse state table to switch machine state, this function must be called in stateMachineLock int SwitchMachineState(uint8_t event); @@ -136,6 +142,10 @@ protected: void DoFeedDogForSync(SyncDirectionFlag flag); + void DoGetDataNotify(uint32_t sessionId); + + void StopFeedDogForGetDataInner(TimerId timerId); + DISABLE_COPY_ASSIGN_MOVE(SyncStateMachine); ISyncTaskContext *syncContext_; @@ -149,12 +159,17 @@ protected: // For save data notify static const int SAVE_DATA_NOTIFY_INTERVAL = 2000; // 2s for save data notify - static const int MAXT_SAVE_DATA_NOTIFY_COUNT = 15; // only notify 15 times + static const int GET_DATA_NOTIFY_INTERVAL = 2000; // 2s for get data notify + static const int MAX_SAVE_DATA_NOTIFY_COUNT = 15; // only notify 15 times static const int SYNC_DIRECTION_NUM = 2; // send receive std::mutex saveDataNotifyLock_; TimerId saveDataNotifyTimerId_; uint8_t saveDataNotifyCount_; + std::mutex getDataNotifyLock_; + TimerId getDataNotifyTimerId_; + uint8_t getDataNotifyCount_; + // used for one (key,value) bigger than mtu size, in this case, send packet need more longger time std::mutex feedDogLock_[SYNC_DIRECTION_NUM]; WatchDogController watchDogController_[SYNC_DIRECTION_NUM] = {{0}, {0}}; -- Gitee From db2ba1f4dc2f92a833d2c53351e7833dea481a83 Mon Sep 17 00:00:00 2001 From: zqq Date: Sat, 17 Sep 2022 14:58:20 +0800 Subject: [PATCH 09/14] add ut for get data notify Signed-off-by: zqq --- ...buteddb_single_ver_p2p_sync_check_test.cpp | 61 +++++++++++++++++++ .../common/syncer/kv_virtual_device.cpp | 7 ++- .../common/syncer/kv_virtual_device.h | 1 + .../virtual_single_ver_sync_db_Interface.cpp | 7 +++ .../virtual_single_ver_sync_db_Interface.h | 3 + 5 files changed, 78 insertions(+), 1 deletion(-) diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp index 50e356d30ac..44a5353779d 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_single_ver_p2p_sync_check_test.cpp @@ -40,6 +40,8 @@ namespace { const int SLEEP_MILLISECONDS = 500; const int TEN_SECONDS = 10; const int THREE_HUNDRED = 300; + const int WAIT_30_SECONDS = 30000; + const int WAIT_40_SECONDS = 40000; KvStoreDelegateManager g_mgr(APP_ID, USER_ID); KvStoreConfig g_config; @@ -1311,4 +1313,63 @@ HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, QuerySyncMergeCheck004, TestSiz ASSERT_TRUE(status == OK); std::this_thread::sleep_for(std::chrono::seconds(TEN_SECONDS)); EXPECT_EQ(sendRequestCount, 0); +} + +/** + * @tc.name: GetDataNotify001 + * @tc.desc: Test GetDataNotify function, delay < 30s should sync ok, > 36 should timeout + * @tc.type: FUNC + * @tc.require: AR000D4876 + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBSingleVerP2PSyncCheckTest, GetDataNotify001, TestSize.Level3) +{ + ASSERT_NE(g_kvDelegatePtr, nullptr); + DBStatus status = OK; + std::vector devices; + devices.push_back(g_deviceB->GetDeviceId()); + const std::string DEVICE_A = "real_device"; + /** + * @tc.steps: step1. deviceB set get data delay 40s + */ + g_deviceB->DelayGetSyncData(WAIT_40_SECONDS); + + /** + * @tc.steps: step2. deviceA call sync and wait + * @tc.expected: step2. sync should return OK. onComplete should be called, deviceB sync TIME_OUT. + */ + std::map result; + std::map virtualRes; + status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PULL_ONLY, result, true); + EXPECT_EQ(status, OK); + EXPECT_EQ(result.size(), devices.size()); + EXPECT_EQ(result[DEVICE_B], TIME_OUT); + std::this_thread::sleep_for(std::chrono::seconds(TEN_SECONDS)); + Query query = Query::Select(); + g_deviceB->Sync(SYNC_MODE_PUSH_ONLY, query, [&virtualRes](std::map resMap) { + virtualRes = std::move(resMap); + }, true); + EXPECT_EQ(virtualRes.size(), devices.size()); + EXPECT_EQ(virtualRes[DEVICE_A], static_cast(SyncOperation::OP_TIMEOUT)); + std::this_thread::sleep_for(std::chrono::seconds(TEN_SECONDS)); + + /** + * @tc.steps: step3. deviceB set get data delay 30s + */ + g_deviceB->DelayGetSyncData(WAIT_30_SECONDS); + /** + * @tc.steps: step4. deviceA call sync and wait + * @tc.expected: step4. sync should return OK. onComplete should be called, deviceB sync OK. + */ + status = g_tool.SyncTest(g_kvDelegatePtr, devices, SYNC_MODE_PULL_ONLY, result, true); + EXPECT_EQ(status, OK); + EXPECT_EQ(result.size(), devices.size()); + EXPECT_EQ(result[DEVICE_B], OK); + std::this_thread::sleep_for(std::chrono::seconds(TEN_SECONDS)); + g_deviceB->Sync(SYNC_MODE_PUSH_ONLY, query, [&virtualRes](std::map resMap) { + virtualRes = std::move(resMap); + }, true); + EXPECT_EQ(virtualRes.size(), devices.size()); + EXPECT_EQ(virtualRes[DEVICE_A], static_cast(SyncOperation::OP_FINISHED_ALL)); + g_deviceB->DelayGetSyncData(0); } \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.cpp index 1cfde89e9e0..b272d6e19b4 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.cpp @@ -69,13 +69,18 @@ int KvVirtualDevice::Commit() return syncInterface->Commit(); } - void KvVirtualDevice::SetSaveDataDelayTime(uint64_t milliDelayTime) { VirtualSingleVerSyncDBInterface *syncInterface = static_cast(storage_); syncInterface->SetSaveDataDelayTime(milliDelayTime); } +void KvVirtualDevice::DelayGetSyncData(uint64_t milliDelayTime) +{ + VirtualSingleVerSyncDBInterface *syncInterface = static_cast(storage_); + syncInterface->DelayGetSyncData(milliDelayTime); +} + int KvVirtualDevice::Subscribe(QuerySyncObject query, bool wait, int id) { auto operation = new (std::nothrow) SyncOperation(id, {remoteDeviceId_}, SUBSCRIBE_QUERY, nullptr, wait); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.h index 220aabc4b41..b95e5402958 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/kv_virtual_device.h @@ -33,6 +33,7 @@ public: int StartTransaction(); int Commit(); void SetSaveDataDelayTime(uint64_t milliDelayTime); + void DelayGetSyncData(uint64_t milliDelayTime); int Subscribe(QuerySyncObject query, bool wait, int id); int UnSubscribe(QuerySyncObject query, bool wait, int id); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp index f4de94fe463..b826d4845cb 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.cpp @@ -218,6 +218,7 @@ int VirtualSingleVerSyncDBInterface::GetSyncDataNext(std::vector &dataItems, ContinueToken &continueStmtToken) const { + std::this_thread::sleep_for(std::chrono::milliseconds(getDataDelayTime_)); for (const auto &data : dbData_) { if (data.isLocal) { if (data.writeTimestamp >= begin && data.writeTimestamp < end) { @@ -318,6 +319,7 @@ int VirtualSingleVerSyncDBInterface::GetSyncData(QueryObject &query, const SyncT const DataSizeSpecInfo &dataSizeInfo, ContinueToken &continueStmtToken, std::vector &entries) const { + std::this_thread::sleep_for(std::chrono::milliseconds(getDataDelayTime_)); const auto &startKey = query.GetPrefixKey(); Key endKey = startKey; endKey.resize(DBConstant::MAX_KEY_SIZE, UCHAR_MAX); @@ -441,4 +443,9 @@ void VirtualSingleVerSyncDBInterface::SetDbProperties(KvDBProperties &kvDBProper { properties_ = kvDBProperties; } + +void VirtualSingleVerSyncDBInterface::DelayGetSyncData(uint32_t milliDelayTime) +{ + getDataDelayTime_ = milliDelayTime; +} } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.h index 43508e5b365..efb16751af2 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_single_ver_sync_db_Interface.h @@ -129,6 +129,8 @@ public: void SetIdentifier(std::vector &identifier); void SetDbProperties(KvDBProperties &kvDBProperties); + + void DelayGetSyncData(uint32_t milliDelayTime); private: int GetSyncData(Timestamp begin, Timestamp end, uint32_t blockSize, std::vector& dataItems, ContinueToken& continueStmtToken) const; @@ -150,6 +152,7 @@ private: std::mutex deviceDataLock_; std::map> deviceData_; std::vector identifier_; + uint64_t getDataDelayTime_ = 0; }; } // namespace DistributedDB -- Gitee From fb1f881d3ff3baa8faf4e13d6b8cda6b85e9f82f Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 19 Sep 2022 10:54:06 +0800 Subject: [PATCH 10/14] fix issue Signed-off-by: zqq --- .../syncer/src/sync_state_machine.cpp | 18 +++++++++--------- .../syncer/src/sync_state_machine.h | 7 +++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index bc35df060c3..49fa46c6c6f 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -227,7 +227,7 @@ bool SyncStateMachine::StartSaveDataNotify(uint32_t sessionId, uint32_t sequence // Incref to make sure context still alive before timer stopped. RefObject::IncObjRef(syncContext_); int errCode = RuntimeContext::GetInstance()->SetTimer( - SAVE_DATA_NOTIFY_INTERVAL, + DATA_NOTIFY_INTERVAL, [this, sessionId, sequenceId, inMsgId](TimerId timerId) { RefObject::IncObjRef(syncContext_); int ret = RuntimeContext::GetInstance()->ScheduleTask([this, sessionId, sequenceId, inMsgId]() { @@ -273,7 +273,7 @@ bool SyncStateMachine::StartFeedDogForSync(uint32_t time, SyncDirectionFlag flag return false; } - uint8_t cnt = GetFeedDogTimeout(time / SAVE_DATA_NOTIFY_INTERVAL); + uint8_t cnt = GetFeedDogTimeout(time / DATA_NOTIFY_INTERVAL); LOGI("[SyncStateMachine][feedDog] start cnt:%d, flag:%d", cnt, flag); std::lock_guard lockGuard(feedDogLock_[flag]); @@ -295,7 +295,7 @@ bool SyncStateMachine::StartFeedDogForSync(uint32_t time, SyncDirectionFlag flag RefObject::IncObjRef(syncContext_); watchDogController_[flag].feedDogUpperLimit = cnt; int errCode = RuntimeContext::GetInstance()->SetTimer( - SAVE_DATA_NOTIFY_INTERVAL, + DATA_NOTIFY_INTERVAL, [this, flag](TimerId timerId) { RefObject::IncObjRef(syncContext_); int ret = RuntimeContext::GetInstance()->ScheduleTask([this, flag]() { @@ -375,7 +375,7 @@ void SyncStateMachine::DoSaveDataNotify(uint32_t sessionId, uint32_t sequenceId, (void)ResetWatchDog(); } std::lock_guard innerLock(saveDataNotifyLock_); - if (saveDataNotifyCount_ >= MAX_SAVE_DATA_NOTIFY_COUNT) { + if (saveDataNotifyCount_ >= MAX_DATA_NOTIFY_COUNT) { StopSaveDataNotifyNoLock(); return; } @@ -414,17 +414,17 @@ void SyncStateMachine::StartFeedDogForGetData(uint32_t sessionId) // Incref to make sure context still alive before timer stopped. RefObject::IncObjRef(syncContext_); int errCode = RuntimeContext::GetInstance()->SetTimer( - SAVE_DATA_NOTIFY_INTERVAL, + DATA_NOTIFY_INTERVAL, [this, sessionId](TimerId timerId) { RefObject::IncObjRef(syncContext_); int ret = RuntimeContext::GetInstance()->ScheduleTask([this, sessionId, timerId]() { - DoGetDataNotify(sessionId); + DoGetAndSendDataNotify(sessionId); int getDataNotifyCount = 0; { std::lock_guard autoLock(getDataNotifyLock_); getDataNotifyCount = getDataNotifyCount_; } - if (getDataNotifyCount >= MAX_SAVE_DATA_NOTIFY_COUNT) { + if (getDataNotifyCount >= MAX_DATA_NOTIFY_COUNT) { StopFeedDogForGetDataInner(timerId); } RefObject::DecObjRef(syncContext_); @@ -455,11 +455,11 @@ void SyncStateMachine::StopFeedDogForGetData() StopFeedDogForGetDataInner(timerId); } -void SyncStateMachine::DoGetDataNotify(uint32_t sessionId) +void SyncStateMachine::DoGetAndSendDataNotify(uint32_t sessionId) { (void)ResetWatchDog(); std::lock_guard autoLock(getDataNotifyLock_); - if (getDataNotifyCount_ >= MAX_SAVE_DATA_NOTIFY_COUNT) { + if (getDataNotifyCount_ >= MAX_DATA_NOTIFY_COUNT) { return; } if (sessionId != 0) { diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index 5e10235b7fe..e1f6003bb92 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -142,7 +142,7 @@ protected: void DoFeedDogForSync(SyncDirectionFlag flag); - void DoGetDataNotify(uint32_t sessionId); + void DoGetAndSendDataNotify(uint32_t sessionId); void StopFeedDogForGetDataInner(TimerId timerId); @@ -158,9 +158,8 @@ protected: uint32_t currentSyncProctolVersion_; // For save data notify - static const int SAVE_DATA_NOTIFY_INTERVAL = 2000; // 2s for save data notify - static const int GET_DATA_NOTIFY_INTERVAL = 2000; // 2s for get data notify - static const int MAX_SAVE_DATA_NOTIFY_COUNT = 15; // only notify 15 times + static const int DATA_NOTIFY_INTERVAL = 2000; // 2s for save/get data notify + static const int MAX_DATA_NOTIFY_COUNT = 15; // only notify 15 times static const int SYNC_DIRECTION_NUM = 2; // send receive std::mutex saveDataNotifyLock_; TimerId saveDataNotifyTimerId_; -- Gitee From ad9697d095678b7bb959b28d79f7063d0713323f Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 19 Sep 2022 15:03:04 +0800 Subject: [PATCH 11/14] fix issue Signed-off-by: zqq --- .../libs/distributeddb/interfaces/include/store_types.h | 4 ++-- .../libs/distributeddb/syncer/src/sync_state_machine.cpp | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/frameworks/libs/distributeddb/interfaces/include/store_types.h b/frameworks/libs/distributeddb/interfaces/include/store_types.h index fdc8b0a8cbd..1fc0b12eddc 100644 --- a/frameworks/libs/distributeddb/interfaces/include/store_types.h +++ b/frameworks/libs/distributeddb/interfaces/include/store_types.h @@ -73,8 +73,8 @@ struct KvStoreConfig { enum PragmaCmd { AUTO_SYNC = 1, - SYNC_DEVICES = 2, - RM_DEVICE_DATA = 3, // remove the device data synced from remote by device name + SYNC_DEVICES = 2, // this cmd will be removed in the future, don't use it + RM_DEVICE_DATA = 3, // this cmd will be removed in the future, don't use it PERFORMANCE_ANALYSIS_GET_REPORT, PERFORMANCE_ANALYSIS_OPEN, PERFORMANCE_ANALYSIS_CLOSE, diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index 49fa46c6c6f..826b97d0d08 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -30,7 +30,9 @@ SyncStateMachine::SyncStateMachine() watchDogStarted_(false), currentSyncProctolVersion_(SINGLE_VER_SYNC_PROCTOL_V3), saveDataNotifyTimerId_(0), - saveDataNotifyCount_(0) + saveDataNotifyCount_(0), + getDataNotifyTimerId_(0), + getDataNotifyCount_(0) { } -- Gitee From 2e252b37c50ca661cd23106e3d07936460d844be Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 19 Sep 2022 10:00:38 +0800 Subject: [PATCH 12/14] sync code with gauss Signed-off-by: zqq --- .../syncer/src/remote_executor.cpp | 10 ++-- .../syncer/src/remote_executor.h | 10 ++-- .../syncer/src/remote_executor_packet.cpp | 11 +++++ .../syncer/src/remote_executor_packet.h | 4 ++ .../distributeddb_mock_sync_module_test.cpp | 49 +++++++++++++++++++ .../common/syncer/mock_remote_executor.h | 29 +++++++++++ 6 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h diff --git a/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp b/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp index b3f95652a98..d75be371fc0 100644 --- a/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp +++ b/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp @@ -143,12 +143,12 @@ void RemoteExecutor::Close() { closed_ = true; LOGD("[RemoteExecutor][Close] close enter"); + RemoveAllTask(-E_BUSY); + ClearInnerSource(); { std::unique_lock lock(msgQueueLock_); clearCV_.wait(lock, [this] { return workingThreadsCount_ == 0; }); } - RemoveAllTask(-E_BUSY); - ClearInnerSource(); LOGD("[RemoteExecutor][Close] close exist"); } @@ -301,7 +301,7 @@ int RemoteExecutor::ReceiveRemoteExecutorAck(const std::string &targetDev, Messa int ackCode = packget->GetAckCode(); uint32_t sessionId = inMsg->GetSessionId(); uint32_t sequenceId = inMsg->GetSequenceId(); - if (!IsPackgetValid(sessionId)) { + if (!IsPacketValid(sessionId)) { LOGD("[RemoteExecutor][ReceiveRemoteExecutorAck] receive unknown ack"); return -E_INVALID_ARGS; } @@ -760,7 +760,7 @@ int RemoteExecutor::FillRequestPacket(RemoteExecutorRequestPacket *packet, uint3 void RemoteExecutor::ReceiveMessageInner(const std::string &targetDev, Message *inMsg) { int errCode = E_OK; - if (inMsg->IsFeedbackError() && IsPackgetValid(inMsg->GetSessionId())) { + if (inMsg->IsFeedbackError() && IsPacketValid(inMsg->GetSessionId())) { DoFinished(inMsg->GetSessionId(), -inMsg->GetErrorNo()); delete inMsg; inMsg = nullptr; @@ -783,7 +783,7 @@ void RemoteExecutor::ReceiveMessageInner(const std::string &targetDev, Message * } } -bool RemoteExecutor::IsPackgetValid(uint32_t sessionId) +bool RemoteExecutor::IsPacketValid(uint32_t sessionId) { std::lock_guard autoLock(taskLock_); return taskMap_.find(sessionId) != taskMap_.end() && taskMap_[sessionId].status == Status::WORKING; diff --git a/frameworks/libs/distributeddb/syncer/src/remote_executor.h b/frameworks/libs/distributeddb/syncer/src/remote_executor.h index e4b39567638..05e113fddfb 100644 --- a/frameworks/libs/distributeddb/syncer/src/remote_executor.h +++ b/frameworks/libs/distributeddb/syncer/src/remote_executor.h @@ -71,6 +71,11 @@ public: void NotifyConnectionClosed(uint64_t connectionId); +protected: + virtual void ParseOneRequestMessage(const std::string &device, Message *inMsg); + + virtual bool IsPacketValid(uint32_t sessionId); + private: void ReceiveMessageInner(const std::string &targetDev, Message *inMsg); @@ -79,8 +84,6 @@ private: int ReceiveRemoteExecutorAck(const std::string &targetDev, Message *inMsg); - void ParseOneRequestMessage(const std::string &device, Message *inMsg); - int CheckPermissions(const std::string &device); int SendRemoteExecutorData(const std::string &device, const Message *inMsg); @@ -117,9 +120,8 @@ private: int FillRequestPacket(RemoteExecutorRequestPacket *packet, uint32_t sessionId, std::string &target); - bool IsPackgetValid(uint32_t sessionId); void ReceiveDataWithValidSession(const std::string &targetDev, uint32_t sessionId, uint32_t sequenceId, - const RemoteExecutorAckPacket *packget); + const RemoteExecutorAckPacket *packet); void RemoveTaskByDevice(const std::string &device, std::vector &removeList); void RemoveAllTask(int errCode); diff --git a/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp b/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp index a112583e341..6cc062591b1 100644 --- a/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp +++ b/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp @@ -155,6 +155,17 @@ int RemoteExecutorRequestPacket::DeSerialization(Parcel &parcel) return E_OK; } +RemoteExecutorRequestPacket* RemoteExecutorRequestPacket::Create() +{ + return new (std::nothrow) RemoteExecutorRequestPacket(); +} + +void RemoteExecutorRequestPacket::Release(RemoteExecutorRequestPacket *&packet) +{ + delete packet; + packet = nullptr; +} + RemoteExecutorAckPacket::RemoteExecutorAckPacket() { } diff --git a/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h b/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h index 2f35d858f5e..35b51972b67 100644 --- a/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h +++ b/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h @@ -55,6 +55,10 @@ public: int DeSerialization(Parcel &parcel) override; + static RemoteExecutorRequestPacket* Create(); + + static void Release(RemoteExecutorRequestPacket *&packet); + static const uint32_t REQUEST_PACKET_VERSION_V1 = SOFTWARE_VERSION_RELEASE_6_0; static const uint32_t REQUEST_PACKET_VERSION_V2 = SOFTWARE_VERSION_RELEASE_6_0 + 1; static const uint32_t REQUEST_PACKET_VERSION_CURRENT = REQUEST_PACKET_VERSION_V2; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index 2b2812ce820..4e2e15969f4 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -26,6 +26,7 @@ #include "mock_auto_launch.h" #include "mock_communicator.h" #include "mock_meta_data.h" +#include "mock_remote_executor.h" #include "mock_single_ver_data_sync.h" #include "mock_single_ver_state_machine.h" #include "mock_sync_engine.h" @@ -45,6 +46,8 @@ using namespace DistributedDB; using namespace DistributedDBUnitTest; namespace { +const uint32_t MESSAGE_COUNT = 10u; +const uint32_t EXECUTE_COUNT = 2u; void Init(MockSingleVerStateMachine &stateMachine, MockSyncTaskContext &syncTaskContext, MockCommunicator &communicator, VirtualSingleVerSyncDBInterface &dbSyncInterface) { @@ -62,6 +65,23 @@ void ChangeTime(int sec) settimeofday(&time, nullptr); } #endif + +int BuildRemoteQueryMsg(DistributedDB::Message *&message) +{ + auto packet = RemoteExecutorRequestPacket::Create(); + if (packet == nullptr) { + return -E_OUT_OF_MEMORY; + } + message = new (std::nothrow) DistributedDB::Message(static_cast(MessageId::REMOTE_EXECUTE_MESSAGE)); + if (message == nullptr) { + RemoteExecutorRequestPacket::Release(packet); + return -E_OUT_OF_MEMORY; + } + message->SetMessageType(TYPE_REQUEST); + packet->SetNeedResponse(); + message->SetExternalObject(packet); + return E_OK; +} } class DistributedDBMockSyncModuleTest : public testing::Test { @@ -878,6 +898,35 @@ HWTEST_F(DistributedDBMockSyncModuleTest, SingleVerKvEntryTest001, TestSize.Leve EXPECT_EQ(GenericSingleVerKvEntry::DeSerializeDatas(kvEntries, parcel), 0); } +/** +* @tc.name: mock remote query 001 +* @tc.desc: Test RemoteExecutor receive msg when closing +* @tc.type: FUNC +* @tc.require: AR000GK58G +* @tc.author: zhangqiquan +*/ +HWTEST_F(DistributedDBMockSyncModuleTest, MockRemoteQuery001, TestSize.Level3) +{ + MockRemoteExecutor *executor = new(std::nothrow) MockRemoteExecutor(); + ASSERT_NE(executor, nullptr); + uint32_t count = 0u; + EXPECT_CALL(*executor, ParseOneRequestMessage).WillRepeatedly( + [&count](const std::string &device, DistributedDB::Message *inMsg) { + std::this_thread::sleep_for(std::chrono::seconds(5)); // mock one msg execute 5 s + count++; + }); + EXPECT_CALL(*executor, IsPacketValid).WillRepeatedly(Return(true)); + for (uint32_t i = 0; i < MESSAGE_COUNT; i++) { + DistributedDB::Message *message = nullptr; + EXPECT_EQ(BuildRemoteQueryMsg(message), E_OK); + executor->ReceiveMessage("DEVICE", message); + } + std::this_thread::sleep_for(std::chrono::seconds(1)); + executor->Close(); + EXPECT_EQ(count, EXECUTE_COUNT); + RefObject::KillAndDecObjRef(executor); +} + /** * @tc.name: SyncTaskContextCheck001 * @tc.desc: test context check task can be skipped in push mode. diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h new file mode 100644 index 00000000000..e21f7f6f4f2 --- /dev/null +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MOCK_REMOTE_EXECUTOR_H +#define MOCK_REMOTE_EXECUTOR_H + +#include +#include "remote_executor.h" + +namespace DistributedDB { +class MockRemoteExecutor : public RemoteExecutor { +public: + MOCK_METHOD2(ParseOneRequestMessage, void(const std::string &, Message *)); + + MOCK_METHOD1(IsPacketValid, bool(uint32_t)); +}; +} // namespace DistributedDB +#endif // #define MOCK_META_DATA_H \ No newline at end of file -- Gitee From 2d5a22c2d72a56a06b2e61fac6f3772579577dad Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 19 Sep 2022 10:11:43 +0800 Subject: [PATCH 13/14] fix double free in remote executor Signed-off-by: zqq --- .../distributeddb/syncer/src/remote_executor.cpp | 5 +---- .../distributeddb/syncer/src/remote_executor.h | 3 ++- .../distributeddb_mock_sync_module_test.cpp | 15 +++++++++++++++ .../unittest/common/syncer/mock_remote_executor.h | 5 +++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp b/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp index d75be371fc0..90b5ad489c6 100644 --- a/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp +++ b/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp @@ -540,10 +540,7 @@ void RemoteExecutor::ResponseFailed(int errCode, uint32_t sessionId, uint32_t se } packet->SetAckCode(errCode); packet->SetLastAck(); - errCode = ResponseStart(packet, sessionId, sequenceId, device); - if (errCode != E_OK) { - delete packet; - } + (void) ResponseStart(packet, sessionId, sequenceId, device); } int RemoteExecutor::ResponseData(RelationalRowDataSet &&dataSet, uint32_t sessionId, uint32_t sequenceId, bool isLast, diff --git a/frameworks/libs/distributeddb/syncer/src/remote_executor.h b/frameworks/libs/distributeddb/syncer/src/remote_executor.h index 05e113fddfb..116151b5d54 100644 --- a/frameworks/libs/distributeddb/syncer/src/remote_executor.h +++ b/frameworks/libs/distributeddb/syncer/src/remote_executor.h @@ -76,6 +76,8 @@ protected: virtual bool IsPacketValid(uint32_t sessionId); + void ResponseFailed(int errCode, uint32_t sessionId, uint32_t sequenceId, const std::string &device); + private: void ReceiveMessageInner(const std::string &targetDev, Message *inMsg); @@ -101,7 +103,6 @@ private: int RequestStart(uint32_t sessionId); - void ResponseFailed(int errCode, uint32_t sessionId, uint32_t sequenceId, const std::string &device); int ResponseData(RelationalRowDataSet &&dataSet, uint32_t sessionId, uint32_t sequenceId, bool isLast, const std::string &device); int ResponseStart(RemoteExecutorAckPacket *packet, uint32_t sessionId, uint32_t sequenceId, diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index 4e2e15969f4..1d358fd8668 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -927,6 +927,21 @@ HWTEST_F(DistributedDBMockSyncModuleTest, MockRemoteQuery001, TestSize.Level3) RefObject::KillAndDecObjRef(executor); } +/** +* @tc.name: mock remote query 002 +* @tc.desc: Test RemoteExecutor response failed when closing +* @tc.type: FUNC +* @tc.require: AR000GK58G +* @tc.author: zhangqiquan +*/ +HWTEST_F(DistributedDBMockSyncModuleTest, MockRemoteQuery002, TestSize.Level3) +{ + MockRemoteExecutor *executor = new(std::nothrow) MockRemoteExecutor(); + ASSERT_NE(executor, nullptr); + executor->CallResponseFailed(0, 0, 0, "DEVICE"); + RefObject::KillAndDecObjRef(executor); +} + /** * @tc.name: SyncTaskContextCheck001 * @tc.desc: test context check task can be skipped in push mode. diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h index e21f7f6f4f2..bfab8c9d887 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_remote_executor.h @@ -24,6 +24,11 @@ public: MOCK_METHOD2(ParseOneRequestMessage, void(const std::string &, Message *)); MOCK_METHOD1(IsPacketValid, bool(uint32_t)); + + void CallResponseFailed(int errCode, uint32_t sessionId, uint32_t sequenceId, const std::string &device) + { + RemoteExecutor::ResponseFailed(errCode, sessionId, sequenceId, device); + } }; } // namespace DistributedDB #endif // #define MOCK_META_DATA_H \ No newline at end of file -- Gitee From 7574cf1ca5a06a402cdc2756e0c0e7f7639a6a24 Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 26 Sep 2022 17:17:22 +0800 Subject: [PATCH 14/14] fixbug when storage relase and machine still use storage Signed-off-by: zqq --- .../syncer/src/isync_state_machine.h | 3 ++ .../syncer/src/sync_state_machine.cpp | 15 ++++--- .../syncer/src/sync_state_machine.h | 3 ++ .../syncer/src/sync_task_context.cpp | 4 +- .../distributeddb_mock_sync_module_test.cpp | 41 +++++++++++++++++++ .../common/syncer/mock_sync_task_context.h | 15 +++++++ 6 files changed, 74 insertions(+), 7 deletions(-) diff --git a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h index a2dee92ccc1..a867eee72dd 100644 --- a/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/isync_state_machine.h @@ -45,6 +45,9 @@ public: // Force stop the state machine virtual void Abort() = 0; + // Force stop the state machine now + virtual void AbortImmediately() = 0; + // Force stop current task with sessionId virtual void InnerErrorAbort(uint32_t sessionId) = 0; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp index 826b97d0d08..a8d1a649d74 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.cpp @@ -108,12 +108,7 @@ void SyncStateMachine::Abort() { RefObject::IncObjRef(syncContext_); int errCode = RuntimeContext::GetInstance()->ScheduleTask([this]() { - { - std::lock_guard lock(this->stateMachineLock_); - this->AbortInner(); - StopWatchDog(); - currentState_ = 0; - } + this->AbortImmediately(); RefObject::DecObjRef(this->syncContext_); }); if (errCode != E_OK) { @@ -122,6 +117,14 @@ void SyncStateMachine::Abort() } } +void SyncStateMachine::AbortImmediately() +{ + std::lock_guard lock(stateMachineLock_); + AbortInner(); + StopWatchDog(); + currentState_ = 0; +} + int SyncStateMachine::SwitchMachineState(uint8_t event) { const std::vector &tables = GetStateSwitchTables(); diff --git a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h index e1f6003bb92..4b8cdccbd1a 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h +++ b/frameworks/libs/distributeddb/syncer/src/sync_state_machine.h @@ -58,6 +58,9 @@ public: // Force stop the state machine void Abort() override; + // Force stop the state machine now + void AbortImmediately() override; + // Force stop current task with sessionId void InnerErrorAbort(uint32_t sessionId) override; diff --git a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp index dc86ece6d1e..01481034db3 100644 --- a/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp +++ b/frameworks/libs/distributeddb/syncer/src/sync_task_context.cpp @@ -591,7 +591,9 @@ void SyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam void SyncTaskContext::KillWait() { StopTimer(); - stateMachine_->Abort(); + UnlockObj(); + stateMachine_->AbortImmediately(); + LockObj(); LOGW("[SyncTaskContext] Try to kill a context, now wait."); bool noDeadLock = WaitLockedUntil( safeKill_, diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index 1d358fd8668..c702113a5ba 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -56,6 +56,14 @@ void Init(MockSingleVerStateMachine &stateMachine, MockSyncTaskContext &syncTask (void)stateMachine.Initialize(&syncTaskContext, &dbSyncInterface, metadata, &communicator); } +void Init(MockSingleVerStateMachine &stateMachine, MockSyncTaskContext *syncTaskContext, + MockCommunicator &communicator, VirtualSingleVerSyncDBInterface *dbSyncInterface) +{ + std::shared_ptr metadata = std::make_shared(); + (void)syncTaskContext->Initialize("device", dbSyncInterface, metadata, &communicator); + (void)stateMachine.Initialize(syncTaskContext, dbSyncInterface, metadata, &communicator); +} + #ifdef RUN_AS_ROOT void ChangeTime(int sec) { @@ -368,6 +376,39 @@ HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck011, TestSize.Level1) EXPECT_EQ(syncTaskContext.IsCommNormal(), false); } +/** + * @tc.name: StateMachineCheck013 + * @tc.desc: test kill syncTaskContext. + * @tc.type: FUNC + * @tc.require: AR000CCPOM + * @tc.author: zhangqiquan + */ +HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck013, TestSize.Level1) +{ + MockSingleVerStateMachine stateMachine; + auto *syncTaskContext = new(std::nothrow) MockSyncTaskContext(); + auto *dbSyncInterface = new(std::nothrow) VirtualSingleVerSyncDBInterface(); + ASSERT_NE(syncTaskContext, nullptr); + EXPECT_NE(dbSyncInterface, nullptr); + if (dbSyncInterface == nullptr) { + RefObject::KillAndDecObjRef(syncTaskContext); + return; + } + MockCommunicator communicator; + Init(stateMachine, syncTaskContext, communicator, dbSyncInterface); + EXPECT_CALL(*syncTaskContext, Clear()).WillOnce(Return()); + syncTaskContext->RegForkGetDeviceIdFunc([]() { + std::this_thread::sleep_for(std::chrono::seconds(2)); // sleep 2s + }); + int token = 1; + int *tokenPtr = &token; + syncTaskContext->SetContinueToken(tokenPtr); + RefObject::KillAndDecObjRef(syncTaskContext); + delete dbSyncInterface; + std::this_thread::sleep_for(std::chrono::seconds(5)); // sleep 5s and wait for task exist + tokenPtr = nullptr; +} + /** * @tc.name: DataSyncCheck001 * @tc.desc: Test dataSync recv error ack. diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h index 0f736f54435..81ebf2d2c82 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/mock_sync_task_context.h @@ -64,6 +64,21 @@ public: { lastFullSyncTaskStatus_ = static_cast(lastFullSyncTaskStatus); } + + void RegForkGetDeviceIdFunc(const std::function &forkGetDeviceIdFunc) + { + forkGetDeviceIdFunc_ = forkGetDeviceIdFunc; + } + + std::string GetDeviceId() const override + { + if (forkGetDeviceIdFunc_) { + forkGetDeviceIdFunc_(); + } + return SingleVerKvSyncTaskContext::GetDeviceId(); + } +private: + std::function forkGetDeviceIdFunc_; }; } // namespace DistributedDB #endif // #define MOCK_SINGLE_VER_STATE_MACHINE_H \ No newline at end of file -- Gitee