Skip to content

Fixes 33641: scope the JDBI test decorators on Predicate<StatementContext> - #33642

Merged
harshach merged 8 commits into
mainfrom
harshach/it-decorators-predicate-scope-v2
Sep 27, 2026
Merged

harshach merged 8 commits into
mainfrom
harshach/it-decorators-predicate-scope-v2

Conversation

@harshach

@harshach harshach commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Describe your changes:

Fixes #33641. First in the merge order for #32946 — it unblocks #33344, #33355 and #33362.

#33539 landed the three JDBI test decorators on main. Three PRs in the epic still add their own
copies of two of them, so all three now fail to merge:

CONFLICT (add/add) …/it/util/SqlQueryCounter.java   #33344, #33355
CONFLICT (add/add) …/it/util/SqlFailureProbe.java   #33362

Their copies are not gratuitous. They scope on Predicate<StatementContext> where the merged
version scopes on a BooleanSupplier, and #33344 and #33355 already call the three-argument
forRequests and deadlockOnce. Deleting their copies in favour of main's would break those call
sites. This makes main's copy the one they already call, so the dedup on rebase is a deletion
rather than a rewrite.

It also settles an open review point on #33539. The review there raised that forRequests was
too broad — it matches any in-flight request, so a concurrent request running a matching statement
can consume the injection meant for the test. I resolved it then by removing
SqlFailureProbe.forRequests, because RequestLatencyContext carries no test identity and a
BooleanSupplier gives the caller nothing to discriminate on. A Predicate<StatementContext> does.
So forRequests returns — now requiring the caller's predicate rather than matching any request.

Injection stays in the probe rather than moving into the counter: a counter that also throws is two
jobs in one class, and the probe's one-shot claim is already a compareAndSet (whereas #33344's
counter holds that state in plain mutable fields written from whichever thread ran the statement).

Correction (was wrong in the first revision of this description). I originally added
SqlFailureProbe.deadlockOnce and claimed it made #33344's call site a one-line swap. Both halves
were wrong, as @manerow caught in review:

  • It would have silently disarmed the test. deadlockOnce built its probe on the calling thread,
    while the Fixes 32946: Pin entity repository lifecycle and cache contracts (1/7) #33344 factory it replaces is request-scoped. Its only call site drives a PATCH over
    REST, so the statement runs on a Jetty worker — the probe would never fire and deadlock-replay
    would pass without exercising a replay. It is replaced by deadlock(), a
    Supplier<RuntimeException> that composes with either scope, so the scope can no longer be picked
    wrongly by default.
  • It is not a one-line swap. That call site counts as well as injects
    (assertEquals(2, writes.count(), …)), and no factory here does both — the migration nests a
    counter around a probe. A test pins that they compose: the probe sits outermost while both are
    open, closing it hands the logger back to the counter rather than the application, and the chain
    unwinds to the original.

SqlQueryCounter.afterFirst is also still not provided, so #33344 cannot delete its copy outright —
it has one call site and runs an action without failing anything, which does not fit a class named
FailureProbe. Happy to add it under a better name if a reviewer would rather it were shared.

Type of change:

  • Improvement

High-level design:

Test infrastructure only; no production code. The scope type widens from BooleanSupplier to
Predicate<StatementContext> on both classes, the existing no-argument forRequests is preserved as
the permissive default, and a new overload takes the caller's predicate. TransactionCounter is
untouched.

Tests:

Use cases covered

  • A REST-driven test can scope a SQL count or an injected failure to the request under test rather
    than to any request in flight.
  • A test needing the deadlock shape DeadlockRetry replays gets it from the probe.

Unit tests

21, all green. Two are new and both were verified to fail without the code that satisfies them:

  • Dropping the predicate gate → a matching statement the predicate rejects must not be counted ==> expected: <false> but was: <true>
  • Replacing the CAS with a check-then-act → both threads were past the guard at once … expected: <1> but was: <2>

The concurrency test is deterministic on purpose — a CyclicBarrier(2) inside the scope predicate
holds both threads within the check-then-set window. Racing threads and hoping for a collision
passed against the unsynchronised version on every run.

Backend integration tests

Not applicable — this is integration-test infrastructure. SqlTestDecoratorsIT on main continues
to cover the Jdbi wiring end to end.

Ingestion integration tests

Not applicable.

Playwright (UI) tests

Not applicable.

Manual testing performed

  1. mvn -pl openmetadata-integration-tests test -Dtest='SqlQueryCounterTest,SqlFailureProbeTest,TransactionCounterTest' — 16 tests, BUILD SUCCESS.
  2. Both RED checks above, each reverted afterwards.
  3. mvn spotless:apply -pl openmetadata-integration-tests — clean.

UI screen recording / screenshots:

Not applicable.

Checklist:

  • I have read the CONTRIBUTING document.
  • My PR title is Fixes <issue-number>: <short explanation>
  • My PR is linked to a GitHub issue via Fixes #33641.
  • I have commented on my code, particularly in hard-to-understand areas.
  • For JSON Schema changes: not applicable.
  • For UI changes: not applicable.
  • I have added tests and listed them above.

🤖 Generated with Claude Code

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no actionable defects or outstanding previous findings remain.

Findings

  1. P2 Request scoping lacks coverage ▶
Summary

This PR improves JDBI test decorators so REST-driven tests can scope counters and failure probes with Predicate<StatementContext>, adds a reusable deadlock-shaped failure supplier, and verifies decorator composition through observable SQL behavior.

  • Preserves calling-thread scoping while adding request-context and statement-predicate scoping.
  • Uses an atomic one-shot claim for injected failures.
  • Adds coverage for request-context composition, caller-predicate rejection, deadlock shape, concurrency, and nested counter/probe behavior.
  • The previous request-scoping coverage gap is fixed, and the resolved internal-wiring test was replaced with an observable integration test.

Reviews (6) · Last reviewed commit: "Move the composition test to the IT, whe..."

Three PRs in the #32946 stack each carry their own copy of these decorators and
now collide with the versions #33539 landed on main:

  CONFLICT (add/add) SqlQueryCounter.java   #33344, #33355
  CONFLICT (add/add) SqlFailureProbe.java   #33362

Their copies are not gratuitous - they scope on Predicate<StatementContext>
rather than the BooleanSupplier that merged, and #33344 and #33355 already call
the three-argument forRequests and deadlockOnce. This makes main's copy the one
they call, so the dedup on rebase is a deletion rather than a rewrite.

Predicate<StatementContext> is also the better answer to the review on #33539
that forRequests was too broad. A fragment alone cannot tell the request under
test apart from a concurrent one running the same SQL; the caller needs a look
at each candidate statement. SqlFailureProbe.forRequests therefore returns, but
requiring the caller's predicate rather than matching any in-flight request.

SqlFailureProbe.deadlockOnce covers #33344's SqlQueryCounter.deadlockOnce so
EntityRepositoryGoldenIT becomes a one-line swap. Injection stays in the probe
rather than moving into the counter: a counter that also throws is two jobs in
one class, and the probe's one-shot claim is already a compareAndSet.

16 unit tests, both guards verified to fail without the code that satisfies
them - dropping the predicate gate gives "a matching statement the predicate
rejects must not be counted", and replacing the CAS with a check-then-act gives
"both threads were past the guard at once ... expected: <1> but was: <2>".

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR checks passed

The linked issue has a description and all required Shipping project fields set. Thanks!

@github-actions github-actions Bot added backend safe to test Add this label to run secure Github workflows on PRs labels Sep 19, 2026
Review feedback, and correct: forRequests was called by nothing but itself. The
unit tests injected predicates through the package-private constructor and
SqlTestDecoratorsIT only used the calling-thread ones, so the composed scope -
"a request is in flight AND the caller's predicate accepts" - was the one line
in these classes with no coverage. It is also the line where a regression is
silent: get it wrong and every REST-driven statement is rejected, or every one
is accepted, and a test supplying its own predicate would never notice.

RequestLatencyContext.startRequest and clearContext are public statics, so the
composition is testable without a server. Four tests, two per class: a matching
statement outside any request is not counted or claimed, the same statement
inside one is, and a caller predicate that rejects still wins while a request
is in flight.

Each half is independently guarded. Dropping the request check fails with "no
request in flight, so there is nothing to attribute the statement to"; dropping
the caller predicate fails with "a request is in flight but the caller's
predicate rejects this statement". Verified both ways.

20 unit tests total.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@mohityadav766
mohityadav766 added this pull request to the merge queue Sep 21, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚦 Removed from the merge queue — failed_checks (2026-09-21T20:30:50Z)

Blocked the queue: integration-tests-postgres-opensearch

@manerow manerow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Test-only change, no production code, and nothing on main regresses: only the package-private constructors changed signature, and every existing caller (SqlTestDecoratorsIT, MultiTeamUserFanOutIT) goes through the public constructors or the 2-arg forRequests, both preserved.

Verified locally: mvn -pl openmetadata-integration-tests test -Dtest='SqlQueryCounterTest,SqlFailureProbeTest,TransactionCounterTest' gives 20 tests, 0 failures. Also checked that deadlockOnce raises the shape DeadlockRetry.isDeadlock actually matches (it walks the cause chain for SQLSTATE 40001 or vendor code 1213), and that calling RequestLatencyContext.startRequest from a unit test is safe (thread-local plus a timer on globalRegistry, no wiring needed, and both new tests clear it in a finally). CI is green across all lanes.

One follow-up that does not block this merge, about the claim that SqlFailureProbe.deadlockOnce makes #33344's call site a one-line swap:

  • Scope differs. #33344's deadlockOnce is request-scoped (RequestLatencyContext.getContext() != null); this one goes through the 3-arg constructor, so it is calling-thread-scoped. Its only known call site, EntityRepositoryGoldenIT:428, drives a PATCH over REST, so the statement runs on a Jetty thread and the probe would never fire. The test would go green without testing the replay. Request-scoping it, or taking the scope as a parameter, or at least saying "on the calling thread" in the javadoc (it currently only says "after the first matching statement") would close that.
  • That call site also does double duty: it injects and asserts assertEquals(2, writes.count(), ...). SqlFailureProbe has no count(), so the swap needs more than one line.
  • SqlQueryCounter.afterFirst(jdbi, fragment, Runnable) is used by #33344 at EntityCacheRecoveryIT:77 and is not added here, so that PR still cannot delete its copy outright.

Happy for this to land as is and for the deadlockOnce scope to be settled either here or when #33344 rebases.

@harshach
harshach added this pull request to the merge queue Sep 22, 2026
harshach and others added 2 commits September 22, 2026 07:21
Review follow-up from @manerow, and the trap is real. deadlockOnce built its
probe through the calling-thread constructor, while the #33344 factory it was
meant to replace is request-scoped. Its only call site, EntityRepositoryGoldenIT,
drives a PATCH over REST, so the statement runs on a Jetty worker: a
calling-thread probe never fires, and the replay test goes green without ever
exercising the replay. Silent false pass, in the one place whose job is to prove
a deadlock is retried.

A convenience that has to guess a scope will keep guessing wrong, so there is no
longer one. deadlock() returns the failure shape DeadlockRetry replays and
composes with either scope, which makes the mistake impossible to make by
accident:

  new SqlFailureProbe(jdbi, fragment, deadlock())                      // this thread
  SqlFailureProbe.forRequests(jdbi, fragment, deadlock(), mine::match) // a request

Two claims in this PR were also wrong and are corrected in the description. The
#33344 call site counts as well as injects - assertEquals(2, writes.count()) -
and no factory here does both, so the swap is not one line; it nests a counter
around a probe. And SqlQueryCounter.afterFirst is still not provided, so #33344
cannot delete its copy outright either. A new test pins the nesting: the probe
sits outermost while both are open, closing it hands the logger back to the
counter rather than the application, and the chain unwinds to the original.

21 unit tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…-scope-v2' into harshach/it-decorators-predicate-scope-v2
@harshach

Copy link
Copy Markdown
Collaborator Author

Thanks — all three land, and the first one is worse than a nit, so I've fixed it rather than
deferring it to the rebase.

The scope mismatch is a silent false pass

Confirmed exactly as you describe. #33344's factory is
forRequests(jdbi, fragment, ignored -> true, true, null) — request-scoped. Mine went through the
three-argument constructor, so calling-thread-scoped. Its only call site drives a PATCH over REST:

try (var writes = SqlQueryCounter.deadlockOnce(Entity.getJdbi(), "update " + table)) {
  patch(path(), "description", "replayed");
  assertEquals(2, writes.count(), "One rolled-back write and one committed replay");
}

The statement runs on a Jetty worker, so a calling-thread probe never fires and
deadlock-replay goes green without exercising a replay — in the one test whose job is to prove a
deadlock is retried.

So there is no longer a convenience that guesses a scope. deadlockOnce is replaced by
deadlock(), a Supplier<RuntimeException> that composes with either:

new SqlFailureProbe(jdbi, fragment, deadlock())                       // this thread
SqlFailureProbe.forRequests(jdbi, fragment, deadlock(), mine::match)  // a request

Taking the scope as a parameter would have worked too, but a factory that cannot pick a scope at all
seemed better than one that can pick the wrong one.

Your other two points were claims of mine that were simply wrong

Not a one-line swap. That call site counts and injects, and no factory here does both — the
swap nests a counter around a probe. I've added a test pinning that they compose: the probe sits
outermost while both are open, closing it hands the logger back to the counter rather than to the
application, and the chain unwinds to the original. (Asserted on the chain rather than a count,
because driving both ends needs a real StatementContext, which is final.)

afterFirst is still missing, so #33344 cannot delete its copy outright — correct. I left it out
deliberately: it runs an action without failing anything and doesn't belong in a class called
FailureProbe. It has one call site (EntityCacheRecoveryIT:77), so I'd keep it local to that test
rather than name it wrongly here — but that's a judgement call, and if you'd rather it were shared
I'll add it under a better name.

I've corrected both claims in the PR description.

Where the fix is

#33642 was already in the merge queue when I pushed, so the branch is locked and I did not dequeue an
approved, green PR. The change is on harshach/it-decorators-deadlock-scope and I'll open it as a
follow-up the moment this merges. Nothing on main calls deadlockOnce, so the trap has no reach
until #33344 rebases — which is after this in the merge order, and the follow-up will be in by then.

@harshach
harshach removed this pull request from the merge queue due to a manual request Sep 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚦 Removed from the merge queue — manual (2026-09-22T14:27:14Z)

The entry left the queue before it was built, so no checks ran against it.

@harshach

Copy link
Copy Markdown
Collaborator Author

Folded the deadlock() fix into this PR rather than deferring it — dequeued, pushed, re-opening for
your re-approval since the diff changed after your review. Sorry for the queue churn.

2e98042c238 now contains:

  • deadlockOnce removed, replaced by deadlock() — a Supplier<RuntimeException> that composes
    with either scope, so nothing can pick the wrong one by default:

    new SqlFailureProbe(jdbi, fragment, deadlock())                       // this thread
    SqlFailureProbe.forRequests(jdbi, fragment, deadlock(), mine::match)  // a request
  • A test pinning the counter/probe nesting, which is the actual migration for Fixes 32946: Pin entity repository lifecycle and cache contracts (1/7) #33344's call site
    now that no single factory both counts and injects: the probe sits outermost while both are open,
    closing it hands the logger back to the counter rather than the application, and the chain unwinds
    to the original.

  • The PR description corrected. Both of my claims you flagged — "one-line swap" and the implied
    scope equivalence — were wrong, and the description now says so rather than quietly dropping them.

afterFirst is still deliberately absent, for the reason above: one call site, and it runs an action
without failing anything, so it does not fit a class called FailureProbe. That one is a judgement
call and I'll happily add it under a better name if you'd rather it were shared than duplicated.

21 unit tests green. CI re-running.

Review feedback, and correct: assertSame on the SqlLogger chain is wiring, not
behaviour, and passes even if delegation stops invoking the inner decorator -
the one failure it needed to catch.

I had justified it by saying a real StatementContext could not be built here.
That was wrong twice over. StatementContext is not final; it has a private
constructor that reflection can reach. But the signature is not stable: this
module resolves jdbi3-core 3.37.1, whose constructor takes (ConfigRegistry,
ExtensionMethod), while 3.47.0 takes a third Type argument. A unit test built on
that would break on a minor dependency bump.

So the test moves to SqlTestDecoratorsIT, where real statements produce real
contexts and no reflection is needed. It now asserts what one statement does to
both decorators: the outer probe fires, the inner counter still sees the same
statement, and once the probe is spent the counter keeps counting through it.
The unit-level wiring assertion is deleted rather than kept alongside.

20 unit tests; the composition case runs in the IT lane.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@harshach
harshach enabled auto-merge September 22, 2026 14:46
harshach added a commit that referenced this pull request Sep 22, 2026
#33539 landed SqlQueryCounter on main, and this PR carried its own, so the two
collided add/add. This PR's copy is dropped in favour of main's.

That is a pure dedup rather than a compromise: TableMetadataReadIT only calls
the two-argument forRequests and the public constructor, both of which main's
copy has with the same semantics - calling-thread scope for the constructor,
"a request is in flight" for forRequests. Nothing here used the three-argument
forRequests, deadlockOnce or afterFirst that the dropped copy also carried, so
this does not depend on #33642.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@gitar-bot

gitar-bot Bot commented Sep 26, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

🟡 Medium risk · Test decorators now scope SQL counting and failure injection per statement.

Refactors JDBI test decorators to scope on Predicate<StatementContext> instead of BooleanSupplier, enabling REST-driven tests to distinguish SQL statements from concurrent requests. Adds request-aware predicate composition to SqlQueryCounter and SqlFailureProbe, a one-shot deadlock failure helper, and comprehensive test coverage including concurrent claim atomicity. No issues found.

Review coverage

📋 Rules No rules evaluated

🧪 Functional validation Not enabled · Set up

🤖 Auto-approval Not enabled · Set up

Options

Display: compact → Counting what did not apply, without listing it.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@harshach
harshach added this pull request to the merge queue Sep 26, 2026
Merged via the queue into main with commit b1a39c3 Sep 27, 2026
125 of 127 checks passed
@harshach
harshach deleted the harshach/it-decorators-predicate-scope-v2 branch September 27, 2026 01:18

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Three epic PRs conflict with the merged JDBI test decorators; main's copy lacks the scope they call

3 participants