Skip to content

fix(db): additional cross-DB and node traffic edge cases (migration scan + node reset time)#5045

Open
rqzbeh wants to merge 3 commits into
MHSanaei:mainfrom
rqzbeh:fix/db-compat-additional-issues
Open

fix(db): additional cross-DB and node traffic edge cases (migration scan + node reset time)#5045
rqzbeh wants to merge 3 commits into
MHSanaei:mainfrom
rqzbeh:fix/db-compat-additional-issues

Conversation

@rqzbeh
Copy link
Copy Markdown

@rqzbeh rqzbeh commented Jun 7, 2026

Additional database compatibility and node-specific edge cases found during audit of legacy SQLite assumptions vs Postgres + node features.

Fixes in this PR (clean base on mhsanaei/main, separate from the main node-traffic-sync PG+SQLite chain)

  1. ExternalProxy migration (MigrationRequirements):

    • StreamSettings was scanned as []byte. Changed to string (natural for the text column on both DBs).
    • json.Unmarshal now does []byte(ep.StreamSettings).
    • Prevents potential scan differences or issues when old multi-domain data is migrated on Postgres installs.
  2. Node mirror inbound creation and config sync (setRemoteTrafficLocked in node traffic merge):

    • When discovering or updating mirrored node inbounds from node snapshots, now also copies LastTrafficResetTime.
    • Previously this was not synced from the node' s reported inbound (only TrafficReset string).
    • Improves correctness for grace periods / traffic reset logic when combining traffics from nodes, or when using the public API to update trafficReset / perform resets on node-attached inbounds.
    • Affects accuracy of up/down accumulation and inGrace decisions for node-owned inbounds.

These are specific to:

  • Creating mirrored inbounds on central from node reports (during SetRemoteTraffic / snapshot processing).
  • Combining node traffics (the merge path that also handles resets).
  • Using API to manage nodes/inbounds (the update path can affect reset times and trigger re-syncs).
  • Postgres vs SQLite differences in migration code.

Does not include the previous commits (tag cleanup, sub fallback JSON, enable merge, etc.) — those are on the parallel branch/fix/pg-node-traffic-sync-api-update.

Hunted during review of node traffic combining, inbound creation on/push to nodes, and API node/inbound paths for other raw SQL, JSON, type, or reset-related assumptions.

rqzbeh added 3 commits June 7, 2026 13:37
- ExternalProxy migration: change StreamSettings scan from []byte
  to string (text column on both SQLite and Postgres). Use []byte()
  for json.Unmarshal. Avoids potential scan/encoding differences
  in migration of old multi-domain data on PG.

- Node mirror inbound creation and updates in SetRemoteTraffic:
  now copy LastTrafficResetTime from the node's reported snapshot.
  Previously, resets done on the node (or via API that affects
  node-owned inbounds) would not update the grace period tracking
  on the central mirror. This improves traffic reset + node traffic
  combining accuracy when using the public API to manage node
  inbounds or when nodes perform resets.

These are independent additional issues around node traffic
combining, creating mirrored node inbounds from snapshots,
and migration code that can affect Postgres (or mixed) setups
after API changes or node operations. They do not depend on the
previous enable-merge or tag/sub fixes.

Base: upstream/main (separate PR).
… tag cleanup)

- In NodeService.GetAll (used for node list/stats): the load of client_traffics
  for node inbound IDs used a direct "IN ?" with all IDs. On SQLite this
  can hit the bind var limit ("too many SQL variables") when there are
  many nodes/inbounds. Chunked using the existing chunkInts + sqliteMaxVars
  (same pattern as other large IN queries in the package). This is a
  specific scale issue for "node" setups on SQLite (PG is fine with large IN).

- Tag cleanup raw in MigrationRequirements (always runs at startup):
  was using SQLite-only INSTR. Fixed to use position() on PG (same as
  the previous tag fix on the main branch). Prevents startup crash on PG
  after node/inbound API changes that leave old tags.

These are additional specific cases around node traffic/stats combining,
node inbound counts, and startup migrations that can affect Postgres
users or large SQLite node deployments. They are independent of the
enable/traffic core fixes and the prior additional ones.

Added to the clean additional-issues branch for the separate PR.
…r + chunk gone deletes)

- Full dialect-safe client enable merge in setRemoteTrafficLocked:
  - Added ClientTrafficEnableMergeExpr() helper (PG CASE with ::boolean
    casts to avoid type errors; SQLite numeric for affinity).
  - Updated GreatestExpr with ::bigint casts on PG.
  - Switched the merge UPDATE from "enable AND ?" to the helper.
  This completes the node traffic sync safety for the "only node can
  disable" logic across DBs (core of the original symptom after API
  inbound updates on nodes).

- Chunked the NodeClientTraffic delete for "goneEmails" (when a node's
  snapshot no longer includes clients previously attached to a mirrored
  inbound). The "email IN ?" could exceed SQLite bind limit for nodes
  with many clients (after API deletes, bulk ops, or structural changes).
  Uses chunkStrings + sqliteMaxVars (consistent with the node stats chunk
  we added earlier).

These are direct extensions of node traffic combining, mirrored inbound
lifecycle, and API-driven changes that affect client_traffics / NodeClientTraffic
for nodes. Stayed on the clean 5045 branch as requested.

Pushed to update MHSanaei#5045
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.

1 participant