From 21c6e1a2999ea6330e3fbcc72c0d68b3f267f3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E4=BF=8A?= Date: Thu, 10 Oct 2024 10:29:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=88=A4=E6=96=AD=E6=A0=87=E8=AE=B0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=A4=E6=96=AD=E6=89=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邓俊 --- .../dfx/include/rdb_fault_hiview_reporter.h | 1 - .../dfx/src/rdb_fault_hiview_reporter.cpp | 44 ++++----------- .../mock/src/rdb_fault_hiview_reporter.cpp | 4 -- frameworks/native/rdb/src/connection_pool.cpp | 2 +- frameworks/native/rdb/src/rd_statement.cpp | 5 +- frameworks/native/rdb/src/rdb_helper.cpp | 8 ++- frameworks/native/rdb/src/rdb_store_impl.cpp | 6 ++- .../native/rdb/src/rdb_store_manager.cpp | 53 ++++++++++++++----- .../native/rdb/src/sqlite_statement.cpp | 5 +- 9 files changed, 69 insertions(+), 59 deletions(-) diff --git a/frameworks/native/dfx/include/rdb_fault_hiview_reporter.h b/frameworks/native/dfx/include/rdb_fault_hiview_reporter.h index 8edf42a77..2b5b8227c 100644 --- a/frameworks/native/dfx/include/rdb_fault_hiview_reporter.h +++ b/frameworks/native/dfx/include/rdb_fault_hiview_reporter.h @@ -62,7 +62,6 @@ private: static void DeleteCorruptedFlag(const std::string &dbPath); static std::string GetTimeWithMilliseconds(time_t sec, int64_t nsec); static std::string GetBundleName(const RdbCorruptedEvent &eventInfo); - static bool HandTimeout(const std::string &dbPath); static Connection::Collector collector_; }; } // namespace OHOS::NativeRdb diff --git a/frameworks/native/dfx/src/rdb_fault_hiview_reporter.cpp b/frameworks/native/dfx/src/rdb_fault_hiview_reporter.cpp index 25261fd82..a1659a069 100644 --- a/frameworks/native/dfx/src/rdb_fault_hiview_reporter.cpp +++ b/frameworks/native/dfx/src/rdb_fault_hiview_reporter.cpp @@ -44,24 +44,17 @@ static constexpr int MAX_TIME_BUF_LEN = 32; static constexpr int MILLISECONDS_LEN = 3; static constexpr int NANO_TO_MILLI = 1000000; static constexpr int MILLI_PRE_SEC = 1000; -static constexpr int TIME_OUT_MINUTES = 10; -static std::unordered_map dbfileLastTimeStatistical_; -static std::mutex dbfilesMutex_; Connection::Collector RdbFaultHiViewReporter::collector_ = nullptr; void RdbFaultHiViewReporter::ReportFault(const RdbCorruptedEvent &eventInfo) { - RdbCorruptedEvent eventInfoAppend = eventInfo; - if (HandTimeout(eventInfo.path)) { - for (auto &[name, debugInfo] : eventInfo.debugInfos) { - eventInfoAppend.appendix += "\n" + name + " :" + GetFileStatInfo(debugInfo); - } - LOG_WARN("database corrupted store:%{public}s, errCode:%{public}d, append:%{public}s", - SqliteUtils::Anonymous(eventInfo.storeName).c_str(), - eventInfo.errorCode, - eventInfoAppend.appendix.c_str()); - } if (IsReportCorruptedFault(eventInfo.path)) { + RdbCorruptedEvent eventInfoAppend = eventInfo; + eventInfoAppend.appendix += Format(eventInfoAppend.debugInfos, ""); + LOG_WARN("corrupted %{public}s errCode:0x%{public}x [%{public}s]", + SqliteUtils::Anonymous(eventInfoAppend.storeName).c_str(), + eventInfoAppend.errorCode, + eventInfoAppend.appendix.c_str()); Report(eventInfoAppend); CreateCorruptedFlag(eventInfo.path); } @@ -69,11 +62,12 @@ void RdbFaultHiViewReporter::ReportFault(const RdbCorruptedEvent &eventInfo) void RdbFaultHiViewReporter::ReportRestore(const RdbCorruptedEvent &eventInfo) { - RdbCorruptedEvent eventInfoAppend = eventInfo; - for (auto &[name, debugInfo] : eventInfo.debugInfos) { - eventInfoAppend.appendix += "\n" + name + " :" + GetFileStatInfo(debugInfo); + if (IsReportCorruptedFault(eventInfo.path)) { + return; } - LOG_INFO("database restore store:%{public}s, errCode:%{public}d, append:%{public}s", + RdbCorruptedEvent eventInfoAppend = eventInfo; + eventInfoAppend.appendix += Format(eventInfoAppend.debugInfos, ""); + LOG_INFO("restored %{public}s errCode:0x%{public}x [%{public}s]", SqliteUtils::Anonymous(eventInfo.storeName).c_str(), eventInfo.errorCode, eventInfoAppend.appendix.c_str()); Report(eventInfoAppend); DeleteCorruptedFlag(eventInfo.path); @@ -173,22 +167,6 @@ std::string RdbFaultHiViewReporter::GetTimeWithMilliseconds(time_t sec, int64_t return oss.str(); } -bool RdbFaultHiViewReporter::HandTimeout(const std::string &dbPath) -{ - std::lock_guard lock(dbfilesMutex_); - auto now = std::chrono::steady_clock::now(); - if (dbfileLastTimeStatistical_.find(dbPath) != dbfileLastTimeStatistical_.end()) { - auto lastTime = dbfileLastTimeStatistical_[dbPath]; - auto duration = std::chrono::duration_cast(now - lastTime); - if (duration.count() < TIME_OUT_MINUTES) { - return false; - } - } - LOG_DEBUG("dbPath %{public}s Last Time is update", SqliteUtils::Anonymous(dbPath).c_str()); - dbfileLastTimeStatistical_[dbPath] = now; - return true; -} - RdbCorruptedEvent RdbFaultHiViewReporter::Create(const RdbStoreConfig &config, int32_t errCode, const std::string &appendix) { diff --git a/frameworks/native/rdb/mock/src/rdb_fault_hiview_reporter.cpp b/frameworks/native/rdb/mock/src/rdb_fault_hiview_reporter.cpp index cf8d93ae7..9bd197f8e 100644 --- a/frameworks/native/rdb/mock/src/rdb_fault_hiview_reporter.cpp +++ b/frameworks/native/rdb/mock/src/rdb_fault_hiview_reporter.cpp @@ -83,10 +83,6 @@ std::string RdbFaultHiViewReporter::GetBundleName(const RdbCorruptedEvent &event return ""; } -bool RdbFaultHiViewReporter::HandTimeout(const std::string &dbPath) -{ - return false; -} std::string RdbFaultHiViewReporter::Format(const std::map &debugs, const std::string &header) { (void)debugs; diff --git a/frameworks/native/rdb/src/connection_pool.cpp b/frameworks/native/rdb/src/connection_pool.cpp index 907394c12..19cb70f29 100644 --- a/frameworks/native/rdb/src/connection_pool.cpp +++ b/frameworks/native/rdb/src/connection_pool.cpp @@ -91,7 +91,7 @@ std::pair> ConnPool::HandleDataCorr static_cast(rebuiltType), SqliteUtils::Anonymous(storeConfig.GetName()).c_str(), storeConfig.IsEncrypt(), errCode, errno); } else { - RdbFaultHiViewReporter::ReportRestore(RdbFaultHiViewReporter::Create(storeConfig, E_OK)); + Reportor::ReportRestore(Reportor::Create(storeConfig, E_OK, "RestoreType:Rebuild")); } return result; diff --git a/frameworks/native/rdb/src/rd_statement.cpp b/frameworks/native/rdb/src/rd_statement.cpp index f15baba6e..f6b4c4003 100644 --- a/frameworks/native/rdb/src/rd_statement.cpp +++ b/frameworks/native/rdb/src/rd_statement.cpp @@ -32,6 +32,7 @@ namespace OHOS { namespace NativeRdb { using namespace OHOS::Rdb; +using Reportor = RdbFaultHiViewReporter; RdStatement::RdStatement() { } @@ -128,7 +129,7 @@ int RdStatement::Prepare(GRD_DB *db, const std::string &newSql) int ret = RdUtils::RdSqlPrepare(db, newSql.c_str(), newSql.length(), &tmpStmt, nullptr); if (ret != E_OK) { if (ret == E_SQLITE_CORRUPT && config_ != nullptr) { - RdbFaultHiViewReporter::ReportFault(RdbFaultHiViewReporter::Create(*config_, ret)); + Reportor::ReportFault(Reportor::Create(*config_, ret)); } if (tmpStmt != nullptr) { (void)RdUtils::RdSqlFinalize(tmpStmt); @@ -296,7 +297,7 @@ int32_t RdStatement::Step() } int ret = RdUtils::RdSqlStep(stmtHandle_); if (ret == E_SQLITE_CORRUPT && config_ != nullptr) { - RdbFaultHiViewReporter::ReportFault(RdbFaultHiViewReporter::Create(*config_, ret)); + Reportor::ReportFault(Reportor::Create(*config_, ret)); } stepCnt_++; return ret; diff --git a/frameworks/native/rdb/src/rdb_helper.cpp b/frameworks/native/rdb/src/rdb_helper.cpp index 62170e410..55f34bef6 100644 --- a/frameworks/native/rdb/src/rdb_helper.cpp +++ b/frameworks/native/rdb/src/rdb_helper.cpp @@ -17,6 +17,7 @@ #include "logger.h" #include "rdb_errno.h" +#include "rdb_fault_hiview_reporter.h" #include "rdb_security_manager.h" #include "rdb_store_manager.h" #include "rdb_trace.h" @@ -30,6 +31,7 @@ namespace OHOS { namespace NativeRdb { using namespace OHOS::Rdb; +using Reportor = RdbFaultHiViewReporter; std::shared_ptr RdbHelper::GetRdbStore( const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode) @@ -81,12 +83,13 @@ int RdbHelper::DeleteRdbStore(const std::string &dbFileName) LOG_ERROR("Store to delete doesn't exist, path %{public}s", SqliteUtils::Anonymous(dbFileName).c_str()); return E_OK; // not not exist } - + RdbStoreConfig config(dbFileName); + Reportor::ReportRestore(Reportor::Create(config, E_OK, "RestoreType:Restore")); + RdbStoreManager::GetInstance().Delete(dbFileName); RdbSecurityManager::GetInstance().DelAllKeyFiles(dbFileName); DeleteRdbStore(SqliteUtils::GetSlavePath(dbFileName)); - RdbStoreConfig config(dbFileName); config.SetDBType(DB_SQLITE); int errCodeSqlite = Connection::Delete(config); @@ -110,6 +113,7 @@ int RdbHelper::DeleteRdbStore(const RdbStoreConfig &config) LOG_ERROR("not exist, path %{public}s", SqliteUtils::Anonymous(dbFile).c_str()); return E_OK; // not not exist } + Reportor::ReportRestore(Reportor::Create(config, E_OK, "RestoreType:Restore")); RdbStoreManager::GetInstance().Delete(dbFile); Connection::Delete(config); RdbSecurityManager::GetInstance().DelAllKeyFiles(dbFile); diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index ea805d0e6..a5a06c757 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -69,6 +69,7 @@ using namespace OHOS::Rdb; using namespace std::chrono; using SqlStatistic = DistributedRdb::SqlStatistic; using RdbNotifyConfig = DistributedRdb::RdbNotifyConfig; +using Reportor = RdbFaultHiViewReporter; #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) using RdbMgr = DistributedRdb::RdbManagerImpl; #endif @@ -1580,6 +1581,9 @@ int RdbStoreImpl::Commit() } auto [errCode, statement] = GetStatement(sqlStr); if (statement == nullptr) { + if (errCode == E_DATABASE_BUSY || errCode == E_SQLITE_BUSY || E_SQLITE_LOCKED) { + Reportor::Report(Reportor::Create(config_, E_DATABASE_BUSY, "ErrorType: Busy")); + } LOG_ERROR("id: %{public}zu, storeName: %{public}s, statement error", transactionId, SqliteUtils::Anonymous(name_).c_str()); return E_DATABASE_BUSY; @@ -1809,7 +1813,7 @@ int RdbStoreImpl::Restore(const std::string &backupPath, const std::vector RdbStoreManager::GetRdbStore(const RdbStoreConfig &config, - int &errCode, int version, RdbOpenCallback &openCallback) +std::shared_ptr RdbStoreManager::GetStoreFromCache(const RdbStoreConfig &config, const std::string &path) +{ + if (storeCache_.find(path) != storeCache_.end()) { + std::shared_ptr rdbStore = storeCache_[path].lock(); + if (rdbStore != nullptr && rdbStore->GetConfig() == config) { + return rdbStore; + } + // TOD reconfigure store should be repeated this + storeCache_.erase(path); + // If rdbStore is not null, it means that the config has changed. + if (rdbStore != nullptr) { + LOG_INFO("app[%{public}s:%{public}s] path[%{public}s]" + " cfg[%{public}d,%{public}d,%{public}d,%{public}d,%{public}d,%{public}d,%{public}d,%{public}s]" + " %{public}s", + config.GetBundleName().c_str(), config.GetModuleName().c_str(), SqliteUtils::Anonymous(path).c_str(), + config.GetDBType(), config.GetHaMode(), config.IsEncrypt(), config.GetArea(), + config.GetSecurityLevel(), config.GetRoleType(), config.IsReadOnly(), config.GetCustomDir().c_str(), + RdbFaultHiViewReporter::FormatBrief( + Connection::Collect(config), SqliteUtils::Anonymous(config.GetName())) + .c_str()); + } + } + return nullptr; +} + +std::shared_ptr RdbStoreManager::GetRdbStore( + const RdbStoreConfig &config, int &errCode, int version, RdbOpenCallback &openCallback) { if (config.IsVector() && config.GetStorageMode() == StorageMode::MODE_MEMORY) { LOG_ERROR("GetRdbStore type not support memory mode."); @@ -67,17 +94,17 @@ std::shared_ptr RdbStoreManager::GetRdbStore(const RdbStoreConfig &con // TOD this lock should only work on storeCache_, add one more lock for connectionpool std::lock_guard lock(mutex_); auto path = config.GetRoleType() == VISITOR ? config.GetVisitorDir() : config.GetPath(); - bundleName_ = config.GetBundleName(); - if (storeCache_.find(path) != storeCache_.end()) { - std::shared_ptr rdbStore = storeCache_[path].lock(); - if (rdbStore != nullptr && rdbStore->GetConfig() == config) { - return rdbStore; + auto pool = TaskExecutor::GetInstance().GetExecutor(); + pool->Schedule(std::chrono::seconds(RETRY_INTERVAL), [path, config, this]() { + if (IsConfigInvalidChanged(path, config)) { + Reportor::Report(Reportor::Create(config, E_CONFIG_INVALID_CHANGE, "ErrorType:Encrypt diff")); } - // TOD reconfigure store should be repeated this - storeCache_.erase(path); + }); + std::shared_ptr rdbStore = GetStoreFromCache(config, path); + if (rdbStore != nullptr) { + return rdbStore; } - - std::shared_ptr rdbStore = std::make_shared(config, errCode); + rdbStore = std::make_shared(config, errCode); if (errCode != E_OK) { LOG_ERROR("RdbStoreManager GetRdbStore fail to open RdbStore as memory issue, rc=%{public}d", errCode); return nullptr; @@ -98,8 +125,8 @@ std::shared_ptr RdbStoreManager::GetRdbStore(const RdbStoreConfig &con errCode = ProcessOpenCallback(*rdbStore, config, version, openCallback); if (errCode != E_OK) { LOG_ERROR("fail, storeName:%{public}s path:%{public}s ProcessOpenCallback errCode:%{public}d", - SqliteUtils::Anonymous(config.GetName()).c_str(), - SqliteUtils::Anonymous(config.GetPath()).c_str(), errCode); + SqliteUtils::Anonymous(config.GetName()).c_str(), SqliteUtils::Anonymous(config.GetPath()).c_str(), + errCode); return nullptr; } } diff --git a/frameworks/native/rdb/src/sqlite_statement.cpp b/frameworks/native/rdb/src/sqlite_statement.cpp index 85bd9beb0..710ff0db6 100644 --- a/frameworks/native/rdb/src/sqlite_statement.cpp +++ b/frameworks/native/rdb/src/sqlite_statement.cpp @@ -43,6 +43,7 @@ namespace NativeRdb { using namespace OHOS::Rdb; using namespace std::chrono; using SqlStatistic = DistributedRdb::SqlStatistic; +using Reportor = RdbFaultHiViewReporter; // Setting Data Precision constexpr SqliteStatement::Action SqliteStatement::ACTIONS[ValueObject::TYPE_MAX]; SqliteStatement::SqliteStatement() : readOnly_(false), columnCount_(0), numParameters_(0), stmt_(nullptr), sql_("") @@ -78,7 +79,7 @@ int SqliteStatement::Prepare(sqlite3 *dbHandle, const std::string &newSql) int ret = SQLiteError::ErrNo(errCode); if (config_ != nullptr && (errCode == SQLITE_CORRUPT || (errCode == SQLITE_NOTADB && config_->GetIter() != 0))) { - RdbFaultHiViewReporter::ReportFault(RdbFaultHiViewReporter::Create(*config_, ret)); + Reportor::ReportFault(Reportor::Create(*config_, ret)); } PrintInfoForDbError(ret, newSql); return ret; @@ -288,7 +289,7 @@ int SqliteStatement::InnerStep() auto errCode = sqlite3_step(stmt_); int ret = SQLiteError::ErrNo(errCode); if (config_ != nullptr && (errCode == SQLITE_CORRUPT || (errCode == SQLITE_NOTADB && config_->GetIter() != 0))) { - RdbFaultHiViewReporter::ReportFault(RdbFaultHiViewReporter::Create(*config_, ret)); + Reportor::ReportFault(Reportor::Create(*config_, ret)); } PrintInfoForDbError(ret, sql_); return ret;