fix(sync): stop MariaDB connection leak and cut per-cycle sync waste
- ConnectionManager reuses a stale-but-healthy pool instead of dialing a new one every 30s, and closes every pool it replaces (one client held ~30 connections). Pool capped at 2 connections, idle released after 1 min. - Background sync every 1 min with ±30% jitter, scheduled from cycle start; users-status online threshold = interval × 1.3. One heartbeat per cycle. - Partnumber books: read headers only, fetch partnumbers_json per book when new/incomplete; empty book counts as complete; an incomplete book is re-pulled once per process (server book lists BOT_AGIBOT_G2_PERF missing from qt_partnumber_book_items). - NeedSync in one UNION ALL query; SyncQtSettings skips unchanged rewrite. - Migration 035: server-maintained server_updated_at on qt_configurations and qt_projects. Clients pull only changed rows after one full pull per process; servers without 035 and old clients keep full pulls. - Migrator: fresh GORM statement per verify query and history insert; files with two verify checks failed to record. ADL: bible-local/decisions/2026-10-26-mariadb-single-pool.md, bible-local/decisions/2026-10-26-incremental-config-project-pull.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a08270b79
commit
44a09d4420
20 files changed
+901
-75
No files matched your search
@@ -0,0 +1,52 @@
|
||||
# Decision: incremental pull of configurations and projects via server_updated_at
|
||||
|
||||
**Date:** 2026-10-26
|
||||
**Status:** active
|
||||
|
||||
## Context
|
||||
|
||||
Opening the configurations or projects list (`GET /api/configs`, `/api/projects`,
|
||||
`/api/projects/:uuid/configs`, on-demand with a 30s cooldown) and the manual sync ran
|
||||
`ImportConfigurationsToLocal` / `ImportProjectsToLocal`, which read **every** server row
|
||||
and rewrote it locally. Measured on production (2026-10-26): configurations 22.2s
|
||||
(4158 rows, ~3.2 MB, 4078 rewritten for one actual change), projects 4.6s (1692 rows),
|
||||
holding a pool connection the whole time. `qt_configurations` had no change column;
|
||||
`qt_projects.updated_at` is set by clients (`SaveProjectPreservingUpdatedAt`), so a change
|
||||
made offline and pushed later carries an old timestamp and cannot drive a filter.
|
||||
|
||||
## Decision
|
||||
|
||||
- Server migration `035_add_server_updated_at.sql` adds
|
||||
`server_updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`
|
||||
plus index `(server_updated_at, id)` to `qt_configurations` and `qt_projects`. MariaDB
|
||||
maintains it; no client writes it and it is **not** in `models.Configuration` /
|
||||
`models.Project`, so writes from any client version (GORM `Create`/`Save`, raw
|
||||
`UPDATE` with column lists) keep working and bump the marker on every real change.
|
||||
- Client (`internal/services/sync/pull_cursor.go`):
|
||||
- if the column is missing (server not migrated yet) every pull is full, exactly as
|
||||
before; presence is re-checked until found, then cached for the process;
|
||||
- the first pull of each table per process is full (restores rows lost locally once per
|
||||
app start), later pulls read `server_updated_at >= cursor - 120s` in one query;
|
||||
- the cursor is the server's `NOW(6)` read *before* the pull, stored in `app_settings`
|
||||
(`server_pull_cursor:<table>`) together with `host:port/database`; a cursor from a
|
||||
different server is ignored;
|
||||
- the per-row merge (skip `pending`, keep local deactivation sticky, keep `line`) is
|
||||
unchanged; only the set of rows read differs.
|
||||
- Old clients keep doing full pulls and are unaffected.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Idle pulls go from ~27s / 3.2 MB to one indexed range query.
|
||||
- Hard deletes on the server are not propagated — same as the full pull before.
|
||||
- Changes applied by InnoDB `ON UPDATE CASCADE` (e.g. a `qt_projects.uuid` change
|
||||
cascading to `qt_configurations.project_uuid`) do not bump `server_updated_at`; they are
|
||||
picked up by the next full pull (app start).
|
||||
- Cursor comparison happens in the server session time zone. Production is `SYSTEM`/MSK
|
||||
(no DST); on a server with DST, rows changed around the autumn transition could be
|
||||
missed until the next app start.
|
||||
- Applying 035 adds the column and builds an index on both tables (~4k and ~2k rows on
|
||||
production, seconds); `qfs -migrate` is run once by the developer as usual.
|
||||
- Tests: `internal/services/sync/incremental_pull_mariadb_test.go` (needs
|
||||
`QF_TEST_MARIADB_DSN`, a disposable MariaDB with the production table DDL; verified on
|
||||
MariaDB 11.8, same as production), migrator fix in
|
||||
`internal/models/sql_migration_verify_test.go` (`two checks pass`).
|
||||
@@ -0,0 +1,54 @@
|
||||
# Decision: one MariaDB pool per client — reuse, never orphan
|
||||
|
||||
**Date:** 2026-10-26
|
||||
**Status:** active
|
||||
|
||||
## Context
|
||||
|
||||
A single client held ~30 MariaDB connections during sync. `ConnectionManager.GetDB`
|
||||
treated a pool older than `pingInterval` (30s) as unusable and dialed a brand-new
|
||||
`*gorm.DB` without closing the previous one. Sync calls `getDB()` per step, so every
|
||||
~30s a new pool appeared while the old one kept its idle connections until
|
||||
`ConnMaxLifetime` (5 min): ~10 orphaned pools × 2 idle + up to 10 active ≈ 30. Each
|
||||
orphaned `*sql.DB` also leaked its opener goroutine. `TryConnect`, the background
|
||||
ping-failure path and a failed ping inside `dial` dropped pools the same way.
|
||||
|
||||
## Decision
|
||||
|
||||
- `GetDB` pings the existing pool when it is merely stale and returns it; it dials only
|
||||
when there is no pool or the ping fails.
|
||||
- Whenever the manager replaces or drops a pool (`GetDB`, `TryConnect`,
|
||||
`checkOnlineNow`, `MarkOffline`, `Disconnect`, failed ping in `dial`) the old
|
||||
`*sql.DB` is closed.
|
||||
- Pool limits: `MaxOpenConns=2`, `MaxIdleConns=2`, `ConnMaxLifetime=5m`,
|
||||
`ConnMaxIdleTime=1m` — one client holds at most 2 connections, idle ones are released
|
||||
within a minute. Sync is sequential (one query at a time); the second connection lets
|
||||
UI server queries (active viewers, users status) run alongside a long sync step.
|
||||
- Background sync interval (`backgroundSyncInterval` in `cmd/qfs/main.go`) is 1 minute
|
||||
with ±30% random jitter (`sync.WorkerJitter`), so clients do not hit MariaDB in
|
||||
lockstep. The next cycle is scheduled from the *start* of the previous one, so
|
||||
heartbeats (`qt_client_schema_state.last_checked_at`) are at most 1.3 × interval apart
|
||||
while a cycle is shorter than that.
|
||||
- `/api/sync/users-status` online threshold = interval × (1 + `WorkerJitter`).
|
||||
- `PullPartnumberBooks` reads only book headers (`id, version, created_at, is_active`)
|
||||
each cycle; `partnumbers_json` (~5 MB for all books) is fetched per book only when the
|
||||
book is new or its local items are incomplete. An empty book counts as complete. An
|
||||
incomplete book is re-pulled at most once per process per version: a server book can
|
||||
list partnumbers missing from `qt_partnumber_book_items` (seen: `BOT_AGIBOT_G2_PERF`
|
||||
in `PNBOOK-2026-10-21-001`), which a re-pull cannot fix; this is logged as `WARN` with
|
||||
the missing partnumbers.
|
||||
- A worker cycle sends one heartbeat: the worker calls `EnsureReadinessForSync` once and
|
||||
then the unexported `pushPendingChanges` (the exported `PushPendingChanges` still checks
|
||||
readiness itself for the manual-sync handlers).
|
||||
- `NeedSync` asks for the latest active pricelist of all three sources in one
|
||||
`UNION ALL` query (`GetLatestActiveIDsBySources`): 0.71s → 0.24s.
|
||||
- `SyncQtSettings` skips the local `DELETE` + `INSERT` when the server rows equal the cache.
|
||||
- Measured against the production server: a cold sync took 171s (pricelists 105s,
|
||||
partnumber books 38s, configurations 23s) and is bound by server reads, not pool
|
||||
size. A warm cycle takes ~1.4s (was ~2.5s plus a 5 MB download).
|
||||
|
||||
## Consequences
|
||||
|
||||
- Code that keeps a `*gorm.DB` across a reconnect gets `sql: database is closed`; sync
|
||||
steps must call `getDB()` per operation instead of caching the handle.
|
||||
- Tests: `internal/db/connection_test.go`, `internal/services/sync/worker_jitter_test.go`.
|
||||
Reference in new issue
Block a user