Skip to content

fix(notification): Enable WAL mode and optimize SQLite configuration …#1468

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
re2zero:bugfix
Mar 3, 2026
Merged

fix(notification): Enable WAL mode and optimize SQLite configuration …#1468
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
re2zero:bugfix

Conversation

@re2zero
Copy link
Contributor

@re2zero re2zero commented Mar 3, 2026

…for notification performance

Configure SQLite WAL mode, NORMAL sync level and 10MB cache to improve notification processing performance, resolving blocking issues during high-frequency notification updates.

Log: 启用WAL模式和优化SQLite配置,提升通知系统并发性能,解决高频通知更新时的阻塞问题。
PMS: BUG-338965
Influence: 写操作不再阻塞读操作,读不阻塞写,消除通知刷新卡顿.

…for notification performance

Configure SQLite WAL mode, NORMAL sync level and 10MB cache to improve notification processing performance, resolving blocking issues during high-frequency notification updates.

Log: 启用WAL模式和优化SQLite配置,提升通知系统并发性能,解决高频通知更新时的阻塞问题。
PMS: BUG-338965
Influence: 写操作不再阻塞读操作,读不阻塞写,消除通知刷新卡顿.
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @re2zero, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要对 SQLite 数据库连接进行了一些性能优化配置。以下是对这段代码的审查意见,包括语法逻辑、代码质量、代码性能和代码安全四个方面:

1. 语法逻辑

  • PRAGMA read_uncommitted=0 的逻辑矛盾

    • 问题:代码注释写着 Enable read_uncommitted for better concurrency with WAL(为了更好的并发性启用 read_uncommitted),但实际执行的 SQL 是 PRAGMA read_uncommitted=0。在 SQLite 中,0 表示 FALSE(即禁用脏读),1 表示 TRUE(即启用脏读)。
    • 建议:如果确实需要启用脏读以减少读取阻塞(在 WAL 模式下通常不需要,因为 WAL 本身读写不冲突),应改为 PRAGMA read_uncommitted=1。但考虑到注释中提到是为了配合 WAL 模式,实际上 WAL 模式本身已经实现了读写不阻塞,因此显式设置 read_uncommitted 通常是不必要的,甚至可能因为读到未提交的数据导致逻辑错误。建议删除该行代码及注释
  • 版权年份更新

    • 观察SPDX-FileCopyrightText 从 2024 更新到了 2024 - 2026。
    • 建议:虽然语法正确,但通常版权年份应覆盖实际开发和维护的年份。如果是预设未来年份,需确保符合公司规范。

2. 代码质量

  • 错误处理

    • 问题:代码对每个 PRAGMA 设置失败都打印了 qWarning,但依然继续执行并返回 true。这意味着如果关键的性能优化(如 WAL 模式)设置失败,程序将在未优化的模式下运行,且调用者 open() 无法感知这一点。
    • 建议
      • 如果 journal_mode=WAL 是系统运行的关键依赖(例如为了解决高并发下的死锁问题),建议在设置失败时返回 false,或者在日志中记录更严重的错误等级,以便运维人员排查。
      • 可以将 PRAGMA 的执行封装为一个辅助函数,减少重复代码。
  • 代码可读性与魔法数字

    • 问题PRAGMA cache_size=-10000 使用了负数,这在 SQLite 中表示 KB(正数表示页数)。虽然注释解释了是 10MB,但直接看数字不够直观。
    • 建议:使用常量或宏定义,例如 const int kCacheSizeInKB = -10240;,并添加注释说明负数单位的含义。

3. 代码性能

  • PRAGMA optimize 的使用时机

    • 问题PRAGMA optimize 是一个耗时操作,它会分析数据库并生成优化统计信息。在每次 open() 连接时都执行可能会导致启动延迟。
    • 建议PRAGMA optimize 通常建议在应用程序关闭时或定期维护任务(如每日一次)中执行,而不是在每次建立连接时执行。如果这是高频调用的连接池场景,建议移除该行;如果是单例长连接,影响较小,但也需考虑启动耗时。
  • Cache Size 设置

    • 评价:将缓存设置为 10MB (-10000) 对于通知类数据库(通常数据量不大)是一个合理的设置,能有效减少磁盘 I/O。

4. 代码安全

  • PRAGMA synchronous=NORMAL 的安全性权衡
    • 分析:将同步模式从默认的 FULL 改为 NORMAL,意味着在发生系统崩溃(如断电)时,数据库可能会损坏,或者丢失最后的事务。但对于“通知”类数据(非关键金融数据),这种丢失通常是可以接受的,以此换取显著的写入性能提升。
    • 建议:确认业务场景允许极小概率的数据丢失。如果是,则该设置是合理的。

改进后的代码建议

// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "dbaccessor.h"

// 定义常量,避免魔法数字
static const int kCacheSizeInKB = -10240; // -10240KB = -10MB

bool DBAccessor::open(const QString &dataPath)
{
    // ... 前面的检查代码保持不变 ...

    if (!QSqlDatabase::contains(m_connectionName)) {
        // ... 数据库添加逻辑 ...
    }

    m_connection = QSqlDatabase::database(m_connectionName);
    
    // === Performance Optimization: WAL Mode and Sync Level ===
    QSqlQuery query(m_connection);

    // WAL mode: Reads don't block writes, writes don't block reads
    // 这是一个关键设置,失败应记录严重错误
    if (!query.exec("PRAGMA journal_mode=WAL")) {
        qCritical(notifyLog) << "Failed to set WAL mode:" << query.lastError().text();
        // 根据业务需求决定是否返回 false
        // return false; 
    }

    // Lower sync level: NORMAL balances performance and safety.
    // 崩溃时可能丢失最近的事务,但能显著提升性能。
    if (!query.exec("PRAGMA synchronous=NORMAL")) {
        qWarning(notifyLog) << "Failed to set synchronous:" << query.lastError().text();
    }

    // Increase cache size: 10MB for better performance
    if (!query.exec(QString("PRAGMA cache_size=%1").arg(kCacheSizeInKB))) {
        qWarning(notifyLog) << "Failed to set cache_size:" << query.lastError().text();
    }

    // 移除 PRAGMA optimize,避免每次打开连接时的性能开销
    // 移除 PRAGMA read_uncommitted,因为 WAL 模式下读写已不阻塞,且脏读不安全

    return true;
}

总结修改点:

  1. 删除了 PRAGMA read_uncommitted:修正了逻辑错误,并利用 WAL 模式本身的特性。
  2. 删除了 PRAGMA optimize:避免连接时的性能损耗。
  3. 引入常量 kCacheSizeInKB:提高代码可读性。
  4. 错误处理增强:对 WAL 模式设置失败建议使用 qCritical
  5. 版权年份:建议根据实际情况确认是否需要修改为 2026。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, re2zero

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@re2zero
Copy link
Contributor Author

re2zero commented Mar 3, 2026

/merge

@deepin-bot
Copy link

deepin-bot bot commented Mar 3, 2026

This pr cannot be merged! (status: unstable)

@re2zero
Copy link
Contributor Author

re2zero commented Mar 3, 2026

/forcemerge

@deepin-bot
Copy link

deepin-bot bot commented Mar 3, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 4f5f603 into linuxdeepin:master Mar 3, 2026
11 of 12 checks passed
@re2zero re2zero deleted the bugfix branch March 3, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants