Merged
fix(a4p): 冷启动并发消费时 SQLite 报 readonly #398
zhangzjAdd on 6 days ago
fix(a4p): 冷启动并发消费时 SQLite 报 readonly #398
Merged
Total: 1 files changed+47-12
| @@ -44,6 +44,19 @@ class SQLiteIntentTokenUsageStore: | |||
| 44 | self.path = Path(configured_path.strip()) | 44 | self.path = Path(configured_path.strip()) |
| 45 | self.timeout_seconds = float(timeout_seconds) | 45 | self.timeout_seconds = float(timeout_seconds) |
| 46 | 46 | ||
| 47 | + _SCHEMA_SQL = """ | ||
| 48 | + CREATE TABLE IF NOT EXISTS intent_token_usage ( | ||
| 49 | + token_id TEXT PRIMARY KEY, | ||
| 50 | + executions_used INTEGER NOT NULL, | ||
| 51 | + executions_limit INTEGER NOT NULL, | ||
| 52 | + expire_at_epoch INTEGER NOT NULL | ||
| 53 | + ) | ||
| 54 | + """ | ||
| 55 | + | ||
| 56 | + # Creating the database file concurrently makes SQLite hand a read-only handle | ||
| 57 | + # to one of the connections; a single short retry is enough to recover. | ||
| 58 | + _WRITE_RETRY_ATTEMPTS = 2 | ||
| 59 | + | ||
| 47 | def consume( | 60 | def consume( |
| 48 | self, | 61 | self, |
| 49 | *, | 62 | *, |
| @@ -59,6 +72,31 @@ class SQLiteIntentTokenUsageStore: | |||
| 59 | if isinstance(expire_at_epoch, bool) or not isinstance(expire_at_epoch, int) or expire_at_epoch <= 0: | 72 | if isinstance(expire_at_epoch, bool) or not isinstance(expire_at_epoch, int) or expire_at_epoch <= 0: |
| 60 | raise ValueError("expire_at_epoch must be a positive integer") | 73 | raise ValueError("expire_at_epoch must be a positive integer") |
| 61 | 74 | ||
| 75 | + for attempt in range(self._WRITE_RETRY_ATTEMPTS): | ||
| 76 | + try: | ||
| 77 | + return self._consume_once( | ||
| 78 | + normalized_token_id=normalized_token_id, | ||
| 79 | + max_executions=max_executions, | ||
| 80 | + expire_at_epoch=expire_at_epoch, | ||
| 81 | + ) | ||
| 82 | + except sqlite3.OperationalError as exc: | ||
| 83 | + is_last_attempt = attempt + 1 >= self._WRITE_RETRY_ATTEMPTS | ||
| 84 | + if is_last_attempt or "readonly" not in str(exc).lower(): | ||
| 85 | + raise IntentTokenUsageStoreError("Intent token usage store unavailable") from exc | ||
| 86 | + time.sleep(0.01) | ||
| 87 | + except (OSError, sqlite3.Error) as exc: | ||
| 88 | + raise IntentTokenUsageStoreError("Intent token usage store unavailable") from exc | ||
| 89 | + | ||
| 90 | + # Unreachable: the loop either returns or raises. | ||
| 91 | + raise IntentTokenUsageStoreError("Intent token usage store unavailable") | ||
| 92 | + | ||
| 93 | + def _consume_once( | ||
| 94 | + self, | ||
| 95 | + *, | ||
| 96 | + normalized_token_id: str, | ||
| 97 | + max_executions: int, | ||
| 98 | + expire_at_epoch: int, | ||
| 99 | + ) -> tuple[bool, int]: | ||
| 62 | connection: sqlite3.Connection | None = None | 100 | connection: sqlite3.Connection | None = None |
| 63 | try: | 101 | try: |
| 64 | self.path.parent.mkdir(parents=True, exist_ok=True) | 102 | self.path.parent.mkdir(parents=True, exist_ok=True) |
| @@ -68,17 +106,12 @@ class SQLiteIntentTokenUsageStore: | |||
| 68 | isolation_level=None, | 106 | isolation_level=None, |
| 69 | ) | 107 | ) |
| 70 | connection.execute(f"PRAGMA busy_timeout = {int(self.timeout_seconds * 1000)}") | 108 | connection.execute(f"PRAGMA busy_timeout = {int(self.timeout_seconds * 1000)}") |
| 109 | + # Make sure the schema exists *before* opening the write transaction. | ||
| 110 | + # Previously "BEGIN IMMEDIATE" ran first, so when two connections raced | ||
| 111 | + # to create a brand-new database file one of them ended up with | ||
| 112 | + # "attempt to write a readonly database". | ||
| 113 | + connection.execute(self._SCHEMA_SQL) | ||
| 71 | connection.execute("BEGIN IMMEDIATE") | 114 | connection.execute("BEGIN IMMEDIATE") |
| 72 | - connection.execute( | ||
| 73 | - """ | ||
| 74 | - CREATE TABLE IF NOT EXISTS intent_token_usage ( | ||
| 75 | - token_id TEXT PRIMARY KEY, | ||
| 76 | - executions_used INTEGER NOT NULL, | ||
| 77 | - executions_limit INTEGER NOT NULL, | ||
| 78 | - expire_at_epoch INTEGER NOT NULL | ||
| 79 | - ) | ||
| 80 | - """ | ||
| 81 | - ) | ||
| 82 | connection.execute( | 115 | connection.execute( |
| 83 | "DELETE FROM intent_token_usage WHERE expire_at_epoch <= ?", | 116 | "DELETE FROM intent_token_usage WHERE expire_at_epoch <= ?", |
| 84 | (int(time.time()),), | 117 | (int(time.time()),), |
| @@ -133,10 +166,12 @@ class SQLiteIntentTokenUsageStore: | |||
| 133 | if connection is not None and connection.in_transaction: | 166 | if connection is not None and connection.in_transaction: |
| 134 | connection.rollback() | 167 | connection.rollback() |
| 135 | raise | 168 | raise |
| 136 | - except (OSError, sqlite3.Error) as exc: | 169 | + except (OSError, sqlite3.Error): |
| 137 | if connection is not None and connection.in_transaction: | 170 | if connection is not None and connection.in_transaction: |
| 138 | connection.rollback() | 171 | connection.rollback() |
| 139 | - raise IntentTokenUsageStoreError("Intent token usage store unavailable") from exc | 172 | + # Bubble up so consume() can decide whether a readonly failure is |
| 173 | + # retryable and wrap the rest as "store unavailable". | ||
| 174 | + raise | ||
| 140 | finally: | 175 | finally: |
| 141 | if connection is not None: | 176 | if connection is not None: |
| 142 | connection.close() | 177 | connection.close() |