!2455 优化系统事件序列的存储及读取逻辑

Merge pull request !2455 from honghecun/master
This commit is contained in:
openharmony_ci 2024-11-19 14:07:50 +00:00 committed by Gitee
commit 6f647cd4c0
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 50 additions and 15 deletions

View File

@ -21,6 +21,9 @@
namespace OHOS {
namespace HiviewDFX {
namespace EventStore {
constexpr char SEQ_PERSISTS_FILE_NAME[] = "event_sequence";
constexpr char SEQ_PERSISTS_BACKUP_FILE_NAME[] = "event_sequence_backup";
class SysEventSequenceManager {
public:
static SysEventSequenceManager& GetInstance();

View File

@ -26,7 +26,6 @@ namespace HiviewDFX {
namespace EventStore {
namespace {
DEFINE_LOG_TAG("HiView-SysEventSeqMgr");
constexpr char SEQ_PERSISTS_FILE_NAME[] = "event_sequence";
bool SaveStringToFile(const std::string& filePath, const std::string& content)
{
@ -44,6 +43,28 @@ bool SaveStringToFile(const std::string& filePath, const std::string& content)
file.close();
return ret;
}
std::string GetSequenceBackupFile()
{
return EventStore::SysEventDao::GetDatabaseDir() + SEQ_PERSISTS_BACKUP_FILE_NAME;
}
void WriteEventSeqToFile(int64_t seq, const std::string& file)
{
std::string content(std::to_string(seq));
if (!SaveStringToFile(file, content)) {
HIVIEW_LOGE("failed to write sequence %{public}s to %{public}s.", content.c_str(), file.c_str());
}
}
void ReadEventSeqFromFile(int64_t& seq, const std::string& file)
{
std::string content;
if (!FileUtil::LoadStringFromFile(file, content)) {
HIVIEW_LOGE("failed to read sequence value from %{public}s.", file.c_str());
}
seq = static_cast<int64_t>(strtoll(content.c_str(), nullptr, 0));
}
}
SysEventSequenceManager& SysEventSequenceManager::GetInstance()
@ -75,23 +96,26 @@ int64_t SysEventSequenceManager::GetSequence()
void SysEventSequenceManager::WriteSeqToFile(int64_t seq)
{
std::string seqFile(GetSequenceFile());
std::string content(std::to_string(seq));
if (!SaveStringToFile(seqFile, content)) {
HIVIEW_LOGE("failed to write sequence %{public}s.", content.c_str());
}
WriteEventSeqToFile(seq, GetSequenceFile());
WriteEventSeqToFile(seq, GetSequenceBackupFile());
}
void SysEventSequenceManager::ReadSeqFromFile(int64_t& seq)
{
std::string content;
std::string seqFile = GetSequenceFile();
if (!FileUtil::LoadStringFromFile(seqFile, content)) {
HIVIEW_LOGE("failed to read sequence value from %{public}s.", seqFile.c_str());
ReadEventSeqFromFile(seq, GetSequenceFile());
int64_t seqBackup = 0;
ReadEventSeqFromFile(seqBackup, GetSequenceBackupFile());
if (seq == seqBackup) {
HIVIEW_LOGI("succeed to read event sequence, value is %{public}" PRId64 ".", seq);
return;
}
seq = static_cast<int64_t>(strtoll(content.c_str(), nullptr, 0));
HIVIEW_LOGI("read sequence successful, value is %{public}" PRId64 ".", seq);
HIVIEW_LOGW("seq[%{public}" PRId64 "] is different with backup seq[%{public}" PRId64 "].", seq, seqBackup);
if (seq > seqBackup) {
WriteEventSeqToFile(seq, GetSequenceBackupFile());
} else {
seq = seqBackup;
WriteEventSeqToFile(seq, GetSequenceFile());
}
}
std::string SysEventSequenceManager::GetSequenceFile() const

View File

@ -18,6 +18,7 @@
#include "file_util.h"
#include "hiview_logger.h"
#include "sys_event_database.h"
#include "sys_event_sequence_mgr.h"
namespace OHOS::HiviewDFX {
namespace EventStore {
@ -112,7 +113,7 @@ bool SysEventBackup::CheckBackupFile()
FileUtil::RemoveFile(backupTmpFile_);
FileUtil::RemoveFile(backupBakFile_);
std::string seqFilePath(SysEventDatabase::GetInstance().GetDatabaseDir() + "event_sequence");
std::string seqFilePath(SysEventDatabase::GetInstance().GetDatabaseDir() + SEQ_PERSISTS_FILE_NAME);
if (FileUtil::FileExists(seqFilePath)) {
HIVIEW_LOGI("seq id file exists, no need restore.");
return false;

View File

@ -25,6 +25,7 @@
#include "hiview_zip_util.h"
#include "string_util.h"
#include "sys_event_dao.h"
#include "sys_event_sequence_mgr.h"
#include "sys_event_repeat_guard.h"
namespace OHOS {
@ -143,7 +144,7 @@ bool SysEventDatabase::Backup(const std::string& zipFilePath)
HIVIEW_LOGI("no event files exist.");
return false;
}
std::string seqFilePath(dbDir + "event_sequence");
std::string seqFilePath(dbDir + SEQ_PERSISTS_FILE_NAME);
if (!FileUtil::FileExists(seqFilePath)) {
HIVIEW_LOGW("seq id file not exist.");
return false;
@ -155,6 +156,12 @@ bool SysEventDatabase::Backup(const std::string& zipFilePath)
return false;
}
std::string seqBackupFilePath(dbDir + SEQ_PERSISTS_BACKUP_FILE_NAME);
if (int32_t ret = zipUnit.AddFileInZip(seqBackupFilePath, ZipFileLevel::KEEP_NONE_PARENT_PATH); ret != 0) {
HIVIEW_LOGW("zip seq id backup file failed, ret: %{public}d.", ret);
return false;
}
const std::string faultType(std::to_string(HiSysEvent::EventType::FAULT));
for (const auto& domainDir : domainDirs) {
std::vector<std::string> eventFiles;
@ -184,7 +191,7 @@ bool SysEventDatabase::Restore(const std::string& zipFilePath, const std::string
return false;
}
std::string seqFilePath(restoreDir + "event_sequence");
std::string seqFilePath(restoreDir + SEQ_PERSISTS_FILE_NAME);
if (!FileUtil::FileExists(seqFilePath)) {
HIVIEW_LOGW("seq id file not exist in zip file.");
return false;