Skip to content

Stop MetricsDB.update() from re-inserting rows it just updated - #882

Merged
EdwardKaravakis merged 1 commit into
masterfrom
fix-metric-collector-duplicate-insert
Nov 24, 2026
Merged

EdwardKaravakis merged 1 commit into
masterfrom
fix-metric-collector-duplicate-insert

Conversation

@EdwardKaravakis

Copy link
Copy Markdown
Member

Summary

  • MetricsDB.update() in metric_collector.py does a batch UPDATE via executemanySQL, then falls back to inserting rows that weren't updated. Since executemanySQL only returns one aggregate rowcount for the whole batch (not per-row results), the fallback loop re-attempts an INSERT for every row in the batch whenever any row is new - including rows that were just successfully updated a moment earlier in the same cycle.
  • Those re-inserts hit the unique constraint on (computingSite, gshare, metric) and get logged as a full ORA-00001 error, every time this happens - in production this is most cycles, since the live site/gshare set changes over time. This accounts for 830 of this week's ~2900 DBProxy errors (the single largest non-deadlock contributor), confirmed via the Kibana error-signature report.
  • Fix: make the insert a guarded INSERT ... SELECT ... WHERE NOT EXISTS, the same pattern DBProxy.backfill_pilot_attributes already uses for an identical race - re-inserting an already-present row becomes a harmless no-op instead of a constraint violation, instead of trying to track exactly which rows need it (not possible with the current executemanySQL primitive without a bigger rework).
  • Also adds the same Postgres JSON-cast guard backfill_pilot_attributes needs (CAST(:x AS JSON) for Postgres, since a text bind isn't implicitly cast to a JSON column's type in INSERT ... SELECT the way it is in INSERT ... VALUES). This daemon isn't enabled on any Postgres deployment today (confirmed on DOMA: panda_server.cfg has [daemon] enable = False), but the ATLAS_PANDA. schema prefix already gets rewritten transparently for any backend by WrappedCursor.change_schema(), so this would silently break the moment someone enables daemons on a Postgres cluster without the guard.

Test plan

  • python -m py_compile on the changed file.
  • isort --check-only clean.
  • flake8/black could not be run in this environment (missing/broken local install) - worth a CI check before merging.

executemanySQL only returns one aggregate rowcount for the whole batch
UPDATE, not per-row results, so when any row in a cycle is new, the
fallback loop re-attempts an INSERT for every row in the batch - not
just the ones that weren't actually updated. Rows that just succeeded
the UPDATE hit the unique constraint on (computingSite, gshare, metric)
and get logged as a full ORA-00001 error every single cycle this
happens, which in production is most cycles given the site/gshare set
changes over time.

Makes the insert a guarded INSERT ... SELECT ... WHERE NOT EXISTS,
matching the pattern DBProxy.backfill_pilot_attributes already uses for
the same kind of race - re-inserting an already-present row becomes a
harmless no-op instead of a constraint violation. Also adds the same
Postgres JSON-cast guard that function needs, even though this daemon
isn't enabled on any Postgres deployment today - the schema name itself
already gets rewritten transparently for any backend by WrappedCursor's
change_schema(), so this would silently break the moment someone
enables it there without the guard.
@EdwardKaravakis
EdwardKaravakis merged commit 6ffa77c into master Nov 24, 2026
1 check passed
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