Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 101 additions & 16 deletions app/control/account/backends/local.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""SQLite account repository (WAL mode, single-process default backend)."""
"""SQLite account repository (WAL-preferred, single-process default backend)."""

import asyncio
import json
Expand Down Expand Up @@ -37,28 +37,96 @@ class LocalAccountRepository:
def __init__(self, db_path: Path) -> None:
self._path = Path(db_path)
self._lock = asyncio.Lock()
self._prefer_wal = True
self._fallback_journal_mode = "DELETE"

# ------------------------------------------------------------------
# Internal helpers
# ------------------------------------------------------------------

def _connect(self) -> sqlite3.Connection:
conn = sqlite3.connect(self._path, check_same_thread=False)
conn.row_factory = sqlite3.Row
try:
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
except sqlite3.OperationalError:
# WAL 模式在 NFS / 某些 Docker bind mount 文件系统上不支持,
# 静默 fallback 到默认 DELETE journal mode,功能不受影响。
pass
conn.execute("PRAGMA busy_timeout=5000")
conn.execute("PRAGMA foreign_keys=ON")
def _connect(self, *, no_lock: bool = False) -> sqlite3.Connection:
def _open() -> sqlite3.Connection:
if no_lock:
# 最后兜底才启用 nolock=1:它绕过 SQLite 文件锁,只用于 issue #31 这类
# Docker bind mount 在所有 journal 模式下首次建库都报 disk I/O error 的环境。
conn = sqlite3.connect(
self._no_lock_uri(),
uri=True,
check_same_thread=False,
)
else:
conn = sqlite3.connect(self._path, check_same_thread=False)
conn.row_factory = sqlite3.Row
return conn

conn = _open()
if self._prefer_wal:
try:
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
except sqlite3.OperationalError:
# WAL 模式在 NFS / 某些 Docker bind mount 文件系统上不支持,
# 失败后的连接可能继续报 disk I/O error,重开后回退到 DELETE。
self._prefer_wal = False
conn.close()
conn = _open()
self._apply_fallback_journal(conn)
else:
self._apply_fallback_journal(conn)
self._apply_connection_pragmas(conn)
return conn

def _no_lock_uri(self) -> str:
return f"{self._path.resolve().as_uri()}?mode=rwc&nolock=1"

@staticmethod
def _apply_connection_pragmas(conn: sqlite3.Connection) -> None:
try:
conn.execute("PRAGMA busy_timeout=5000")
conn.execute("PRAGMA foreign_keys=ON")
except Exception:
conn.close()
raise

def _apply_fallback_journal(self, conn: sqlite3.Connection) -> None:
try:
conn.execute(f"PRAGMA journal_mode={self._fallback_journal_mode}")
except Exception:
conn.close()
raise

def _init_sync(self) -> None:
self._path.parent.mkdir(parents=True, exist_ok=True)
with closing(self._connect()) as conn:
# 部分 Docker bind mount 会通过 journal PRAGMA,却在首次建表写入时
# 才报 disk I/O error;初始化语句幂等,因此只在此阶段逐级降级重试。
no_lock = False
for _ in range(4):
try:
self._init_schema_once(no_lock=no_lock)
return
except sqlite3.OperationalError as exc:
if not self._is_disk_io_error(exc):
raise
if self._prefer_wal:
self._prefer_wal = False
self._fallback_journal_mode = "DELETE"
continue
if self._fallback_journal_mode == "DELETE":
self._fallback_journal_mode = "MEMORY"
continue
if not no_lock:
# MEMORY journal 仍失败时,剩余高概率是宿主文件锁/挂载语义异常;
# no-lock 有并发风险,因此只在初始化幂等 SQL 的最后一次重试启用。
no_lock = True
continue
raise

@staticmethod
def _is_disk_io_error(exc: sqlite3.OperationalError) -> bool:
return "disk i/o error" in str(exc).lower()

def _init_schema_once(self, *, no_lock: bool = False) -> None:
with closing(self._connect(no_lock=no_lock)) as conn:
conn.executescript(f"""
CREATE TABLE IF NOT EXISTS {_META} (
key TEXT PRIMARY KEY,
Expand Down Expand Up @@ -98,12 +166,29 @@ def _init_sync(self) -> None:
ON {_TBL} (pool, status);
CREATE INDEX IF NOT EXISTS idx_acc_deleted
ON {_TBL} (deleted_at) WHERE deleted_at IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_acc_live_updated
ON {_TBL} (updated_at DESC) WHERE deleted_at IS NULL;
""")
self._ensure_column_sync(conn, "quota_grok_4_3", "TEXT NOT NULL DEFAULT '{}'")
self._ensure_column_sync(conn, "quota_console", "TEXT NOT NULL DEFAULT '{}'")
conn.commit()
self._create_optional_indexes(conn)

def _create_optional_indexes(self, conn: sqlite3.Connection) -> None:
# 账号列表性能索引不是启动所需的核心 schema。issue #31 的 Docker
# bind mount 反馈显示,核心建表之外的额外索引可能触发 disk I/O error;
# 因此核心 schema 先提交,性能索引失败时允许降级为慢查询而不是启动失败。
try:
conn.execute(
f"CREATE INDEX IF NOT EXISTS idx_acc_live_updated "
f"ON {_TBL} (updated_at DESC) WHERE deleted_at IS NULL"
)
conn.commit()
except sqlite3.OperationalError as exc:
if not self._is_disk_io_error(exc):
raise
try:
conn.rollback()
except sqlite3.Error:
pass

@staticmethod
def _ensure_column_sync(conn: sqlite3.Connection, name: str, ddl: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_account_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_defaults_define_deleted_account_cleanup(self):
self.assertIn("deleted_retention_days = 7", text)
self.assertIn('run_at = "03:30"', text)
self.assertIn("batch_size = 5000", text)
self.assertIn("vacuum = true", text)
self.assertIn("vacuum = false", text)

def test_admin_config_schema_exposes_deleted_account_cleanup(self):
html = (_ROOT / "app/statics/admin/config.html").read_text(encoding="utf-8")
Expand Down
5 changes: 4 additions & 1 deletion tests/test_admin_batch_review_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ async def test_batch_refresh_filters_non_manageable_explicit_tokens(self):
body = orjson.loads(response.body)
self.assertEqual(repo.requested_tokens, ["active-token", "disabled-token"])
self.assertEqual(refresh_svc.refreshed_tokens, ["active-token"])
self.assertEqual(body["summary"], {"total": 1, "ok": 1, "fail": 0})
self.assertEqual(
body["summary"],
{"total": 1, "ok": 1, "fail": 0, "expired": 0, "transient": 0},
)

async def test_batch_refresh_rejects_only_non_manageable_explicit_tokens(self):
repo = _Repo()
Expand Down
Loading
Loading