Skip to content

Commit

Permalink
feat: add index corruption detection and auto rebuild
Browse files Browse the repository at this point in the history
1. Add indexCorrupted flag to track index corruption state
2. Throw LuceneException in UpdateIndexHandler when index is corrupted
3. Add auto rebuild mechanism in TaskManager when index corruption detected
4. Add isIndexCorrupted() and setIndexCorrupted() methods to IndexTask
5. Improve error handling and logging messages

Log:
  • Loading branch information
Johnson-zs authored and deepin-bot[bot] committed Dec 2, 2024
1 parent a2ca012 commit 7a58c1a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
22 changes: 21 additions & 1 deletion src/services/textindex/task/indextask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

#include "progressnotifier.h"

#include <LuceneException.h>

#include <QDebug>
#include <QThread>
#include <QCoreApplication>

SERVICETEXTINDEX_USE_NAMESPACE
using namespace Lucene;

IndexTask::IndexTask(Type type, const QString &path, TaskHandler handler, QObject *parent)
: QObject(parent), m_type(type), m_path(path), m_handler(handler)
Expand Down Expand Up @@ -80,13 +83,30 @@ IndexTask::Status IndexTask::status() const
return m_status;
}

bool IndexTask::isIndexCorrupted() const
{
return indexCorrupted;
}

void IndexTask::setIndexCorrupted(bool corrupted)
{
indexCorrupted = corrupted;
}

void IndexTask::doTask()
{
fmInfo() << "Processing task for path:" << m_path;

bool success = false;
if (m_handler) {
success = m_handler(m_path, m_state);
try {
setIndexCorrupted(false);
success = m_handler(m_path, m_state);
} catch (const LuceneException &) {
// 捕获到 Lucene 异常,说明索引损坏
setIndexCorrupted(true);
success = false;
}
} else {
fmWarning() << "No task handler provided";
}
Expand Down
4 changes: 4 additions & 0 deletions src/services/textindex/task/indextask.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class IndexTask : public QObject
Type taskType() const;
Status status() const;

bool isIndexCorrupted() const;
void setIndexCorrupted(bool corrupted);

Q_SIGNALS:
void progressChanged(SERVICETEXTINDEX_NAMESPACE::IndexTask::Type type, qint64 count);
void finished(SERVICETEXTINDEX_NAMESPACE::IndexTask::Type type, bool success);
Expand All @@ -57,6 +60,7 @@ class IndexTask : public QObject
Status m_status { Status::NotStarted };
TaskState m_state;
TaskHandler m_handler;
bool indexCorrupted { false };
};

SERVICETEXTINDEX_END_NAMESPACE
Expand Down
9 changes: 6 additions & 3 deletions src/services/textindex/task/taskhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,15 @@ TaskHandler TaskHandlers::UpdateIndexHandler()
writer->optimize();
return true;
} catch (const LuceneException &e) {
fmWarning() << "Update index failed with Lucene exception:"
// Lucene异常表示索引损坏
fmWarning() << "Update index failed with Lucene exception, needs rebuild:"
<< QString::fromStdWString(e.getError());
throw; // 重新抛出异常,让 IndexTask 捕获并处理
} catch (const std::exception &e) {
// 其他异常不需要重建
fmWarning() << "Update index failed with exception:" << e.what();
}

return false;
};
}
Expand Down Expand Up @@ -402,7 +405,7 @@ TaskHandler TaskHandlers::RemoveIndexHandler()

// 将路径列表字符串转换为QStringList
QStringList paths = pathList.split("|", Qt::SkipEmptyParts);

ProgressReporter reporter;
for (const QString &path : paths) {
if (!running.isRunning())
Expand Down
24 changes: 14 additions & 10 deletions src/services/textindex/task/taskmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,21 @@ void TaskManager::onTaskFinished(IndexTask::Type type, bool success)
QString taskPath = currentTask->taskPath();

if (!success && type == IndexTask::Type::Update) {
fmWarning() << "Update task failed for path:" << taskPath << ", trying to rebuild index";

// 清理损坏的索引
clearIndexDirectory();

// 启动新的创建任务
cleanupTask(); // 清理当前失败的任务
if (startTask(IndexTask::Type::Create, taskPath)) {
return; // 新任务已启动,等待其完成
// 检查是否是由于索引损坏导致的失败
if (currentTask->isIndexCorrupted()) {
fmWarning() << "Update task failed due to index corruption for path:" << taskPath << ", trying to rebuild index";

// 清理损坏的索引
clearIndexDirectory();

// 启动新的创建任务
cleanupTask(); // 清理当前失败的任务
if (startTask(IndexTask::Type::Create, taskPath)) {
return; // 新任务已启动,等待其完成
}
} else {
fmInfo() << "Update task failed but index is not corrupted, skipping rebuild for path:" << taskPath;
}
// 如果重建也失败了,继续向下执行发出失败信号
}

fmInfo() << "Task" << typeToString(type) << "for path" << taskPath
Expand Down

0 comments on commit 7a58c1a

Please sign in to comment.