Skip to content

Cache the federation-exchanged token - #660

Open
cathleeny wants to merge 5 commits into
mainfrom
cathleeny/federation-token-cache
Open

cathleeny wants to merge 5 commits into
mainfrom
cathleeny/federation-token-cache

Conversation

@cathleeny

@cathleeny cathleeny commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Found while reviewing #656: with enableTokenFederation, FederationProvider performed an RFC 8693 token exchange at /oidc/v1/token on every getToken() call. The Thrift path calls this per HTTP request (and the kernel's token provider per request too), so each request paid a network exchange plus retries.

What changes

  • The exchanged token is cached and reused while the subject token is unchanged and the exchanged token is not near expiry (same 5-minute refresh threshold as CachedTokenProvider). A changed subject token or a near-expiry exchanged token triggers a re-exchange.
  • When the exchange fails, the original (external) token is cached for up to 5 minutes and the exchange is re-attempted at most that often, instead of on every request. The Python driver also caches the fallback, but until the token expires. returnOriginalTokenOnFailure: false still throws immediately and caches nothing.
  • Concurrent callers share one in-flight exchange, and a mid-flight subject change can neither clear the newer exchange nor evict its cache entry.
  • When the exchange response has no expires_in, the token now falls back to its JWT exp claim, then to a 1-hour default lifetime, so the cache is always bounded.

Tests

  • 22 FederationProvider unit tests: one exchange per subject, re-exchange on subject change or near expiry, concurrent coalescing, mid-flight subject swap, fallback caching with re-attempt after the 5-minute interval or a subject change, returnOriginalTokenOnFailure: false throws without caching, exp and default TTL fallbacks.
  • npm run build, npm run lint, npm run prettier and npm run test (1904 unit tests) pass. No e2e or live tests run.

This pull request and its description were written by Isaac.

FederationProvider performed an RFC 8693 token exchange on every
getToken() call, which the Thrift path issues per HTTP request. Cache
the exchanged token and reuse it while the subject token is unchanged
and the exchanged token is not within the 5-minute refresh threshold
shared with CachedTokenProvider. Concurrent callers share one in-flight
exchange, and failures or fallback results are never cached, so the
next call re-attempts.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
@cathleeny
cathleeny marked this pull request as ready for review December 3, 2026 19:48

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Medium · 1 Nit

Looks good, with 2 low concerns. The caching, coalescing and no-caching-on-failure behavior is correct and well tested. The remaining gaps are stale finally cleanup when the subject token rotates mid-exchange, and no test for the JWT exp fallback.

Other findings

  • ⚪ Nit — The JWT exp fallback when expires_in is missing has no test. Only the non-JWT 1-hour default is covered. Add a test with a JWT access_token that has exp and no expires_in, and assert expiresAt equals exp * 1000. (Found by Claude Opus 5.5 · confirmed by the judge)

A late-settling exchange for an older subject cleared the shared
in-flight reference even after a newer subject's exchange had replaced
it, causing duplicate exchanges, and could evict the newer subject's
cached entry. Track each exchange's own promise and only clear the
shared fields when it is still the tracked exchange; write the cache
only while the exchange is still tracked for its subject.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

When returnOriginalTokenOnFailure is enabled and the exchange fails,
cache the original token for the subject and re-attempt the exchange at
most once per 5 minutes (or when the subject token changes or nears
expiry), matching the Python driver, instead of failing on every call.
A successful exchange replaces the fallback cache; errors with
returnOriginalTokenOnFailure disabled still throw and are not cached.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 2 Low

Looks good, with one low-severity test gap. Caching, request coalescing and the stale-settlement guards are correct, and the federation provider wraps CachedTokenProvider, so subject token strings stay stable between refreshes.

This overview is Claude Opus 5.5's initial take; GPT-6.1 Sol added findings it doesn't mention — see the findings below.

Resolve the newer subject's exchange first so a missing cache-write
guard lets the stale exchange evict its entry, and add a test that
settles the older exchange first to cover the in-flight reference
clear guard. Both tests fail when their guard is removed.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Low

Looks good, no blocking issues. The caching, fallback interval, coalescing and stale-write guards are correct for the CachedTokenProvider → FederationProvider wiring in DBSQLClient.wrapTokenProvider, and the unit tests cover each path.

This overview is Claude Opus 5.5's initial take; GPT-6.1 Sol added findings it doesn't mention — see the findings below.

This branch was previously deployed

1 inactive deployment
azure-prod — 7bb635a1 Deployed Dec 3, 2026 by cathleeny via e2e-test (26.12.1, 26_12_1) #2307
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