Skip to content

[Server] Check shared parents once per list in ResultFilter - #2105

Merged
yili-db merged 2 commits into
unitycatalog:mainfrom
s-maling-celonis:server/list-filter-authz-batch
Nov 5, 2026
Merged

yili-db merged 2 commits into
unitycatalog:mainfrom
s-maling-celonis:server/list-filter-authz-batch

Conversation

@s-maling-celonis

@s-maling-celonis s-maling-celonis commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description of changes

List endpoints filtered by @ResponseAuthorizeFilter get slow as the policy table grows. Listing a schema with a few hundred tables can take several seconds per page, and almost all of that time is spent in ResultFilter.filter.

Why:

  • Each check scans the whole policy table. JCasbinAuthorizer calls jCasbin enforce, which checks every casbin_rule row against the matcher on each call.
  • The shared checks repeat for every item. ResultFilter evaluated the full expression once per item. For GET_TABLE, used by listTables, that means up to 7 authorizer calls per table: the metastore, catalog and schema checks (USE_CATALOG twice) plus the table's own authorizeAny. Only that last one depends on the item.
  • Denied items were evaluated twice. After the first deny-path refresh in a request, every later deny was covered by that refresh, so refreshAuthorizations returned true straight away and the item was evaluated a second time.

This PR adds UnityAccessEvaluator.batch(principal, expression, nonResourceValues, operationStartNanos), which returns a Batch:

  • It parses the expression once.
  • It remembers each authorize/authorizeAny/authorizeAll decision, keyed by principal, resource type and ID, and privileges. The remembered decisions are cleared once, when the single refresh runs.
  • It refreshes policy at most once: the first deny refreshes, forgets the remembered decisions and re-evaluates. Later denies are final, because that refresh already covers the operation start. Without this, every later deny would clear the remembered decisions, and pages with many denied items would gain little.

ResultFilter.filter uses one batch per call. Single-object evaluate(...) goes through a one-item batch, so its behavior is unchanged.

Behavior notes:

  • A grant or revoke made while a page is being filtered is seen only by checks the batch has not made yet; it fully applies from the next request.
  • The first deny uses up the one refresh attempt. If the refresh returns false (disabled, failed probe, interrupt) or throws, that deny and every later one in the page are kept without retrying.

Results

Measured locally: 100-table page, 5,000 policy rows, JCasbinAuthorizer on H2, AuthorizeExpressions.GET_TABLE, best of 5. The timing harness is not part of this PR.

Principal Per-item Batched
Catalog owner 57 ms 1 ms
Reader (USE_CATALOG + USE_SCHEMA, SELECT on half the tables) 250 ms 91 ms

Readers still need at least one table-level check per item, and each one still scans every policy row. Making JCasbinAuthorizer.authorize an indexed lookup would fix that, but it is a separate change.

Tests

New tests in UnityAccessEvaluatorTest and ResultFilterTest:

  • batchChecksSharedParentsOncePerList: the metastore, catalog and schema checks run once for a 100-table list.
  • batchRefreshesAtMostOncePerList: the refresh runs at most once per list, whether it returns true, returns false, or throws.
  • listFilteringKeepsEachItemsOwnDecision: with alternating table grants, ResultFilter.filter keeps exactly the readable tables, so one item's decision does not leak into another's.

🤖 Generated with Claude Code

Each JCasbinAuthorizer check scans every casbin_rule row, and the list
expressions re-ran the metastore, catalog and schema checks for every
item. A Batch now remembers each authorizer decision for one list
operation, and refreshes policy at most once per batch instead of
re-evaluating every denied item after the first refresh.

Signed-off-by: Simon Maling <[email protected]>
@s-maling-celonis
s-maling-celonis marked this pull request as ready for review November 3, 2026 10:48
@yili-db

yili-db commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI Assisted] Nice speedup, and the cache key looks right: function, principal, resource type and ID, and privileges, so one item's decision can't leak into another's. Every refresh failure keeps the deny, and single-object evaluate behaves as before. No blockers from me; comments inline.

On the bot's note that removing decisions.clear() fails no test: I think evaluateAllowsAfterSuccessfulRefresh already covers it (it stubs thenReturn(false, true), which would stay denied without the clear), so only the multi-item latch cases look untested.

*
* <p>On deny, refreshes policy if needed, forgets the remembered decisions, and re-evaluates.
* That happens at most once per batch: one refresh already brings the policy up to the start of
* the operation, so later denies in the same batch are final. If refresh is disabled or throws,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI Assisted] Can we describe this as per-decision memoization that is cleared once, when the single refresh runs? The batch(...) doc above says a grant or revoke mid-batch "is not seen by it", but cache misses still read the live authorizer.

Also, refreshAuthorizations returns false for more than "disabled" (checkAndReloadAfter returns false after a failed probe or an interrupt), and refreshAttempted is latched before the call, so any false or throw makes every later deny in the batch final. That's the right call (otherwise each denied item could wait on a probe), but the doc should say it. Same wording fix in the PR description.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, done in 8e03a64. The batch(...) doc now says the batch remembers each decision and clears them only once, when its single refresh runs, so a mid-batch grant or revoke is seen by checks it hasn't made yet. Batch.evaluate now says the first deny uses up the one refresh attempt whatever the outcome: if the refresh returns false (disabled, failed probe, interrupt) or throws, that deny and every later one are kept without retrying. The PR description's behavior notes say the same.

String function, UUID principal, Resource resource, List<Privileges> actions) {}

/** One expression evaluated for one principal against many resources; see {@link #batch}. */
public final class Batch {

@yili-db yili-db Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI Assisted] Have you considered a request-scoped UnityCatalogAuthorizer wrapper, like CachedUnityCatalogAuthorizer, instead of Batch, created in beforeRequest? It would cache authorize*, throw on writes and list*, and have refreshAuthorizations refresh once per operation start and then clear the cache. UnityAccessEvaluator would stay almost unchanged, and the cache would be testable without SpEL. The costs are the throwing methods and parsing per item (unless we cache parsed expressions).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I looked into it, and I'd like to keep Batch for this PR:

  • Same speedup. Both approaches make the same number of authorizer calls and refresh at most once, so the wrapper doesn't make filtering faster.
  • Clearer lifetime. Batch is a local variable in ResultFilter.filter, so its cached decisions can't outlive the list. A wrapper on the request context lives for the whole request, and anything async that holds on to it would see stale decisions.
  • Smaller change. The wrapper needs the evaluator to get its authorizer per request instead of at construction. It also needs a cache of parsed expressions, a choice about what to do with the other authorizer methods, and a second meaning for refreshAuthorizations returning false.
  • No caller needs request-wide caching yet. Each list handler filters once per request.

You're right that the wrapper separates concerns better and makes the cache testable without SpEL. If a second caller for request-wide caching shows up, I'm happy to do it as a follow-up.

* @param nonResourceValues Map of parameter names to their values (from @AuthorizeKey)
* @param operationStartNanos {@link System#nanoTime()} from the start of this operation
*/
public Batch batch(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI Assisted] Can batch(...) and Batch be package-private? The only caller is ResultFilter in the same package, and Batch is stateful and not thread-safe, so I'd rather not make it public API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 8e03a64: batch(...), Batch and Batch.evaluate are package-private.

}

@Test
void batchRefreshesAtMostOncePerList() throws Exception {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI Assisted] Can we parameterize this over refresh returning true, returning false, and throwing? Then assert one refreshAuthorizations call in each case. The existing false/throw tests use a single item, so a change that retried the refresh for each later deny would still pass them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 8e03a64. batchRefreshesAtMostOncePerList now runs for a refresh that returns true, returns false and throws, and asserts one refreshAuthorizations call for three denied tables in each case. The first table is checked twice only when the refresh returns true.

* <p>To check one expression against many resources (list filtering), use {@link #batch}: a {@link
* Batch} remembers each authorizer decision, so checks on the shared parents (metastore, catalog,
* schema) run once per list instead of once per item. That matters because each {@code
* JCasbinAuthorizer} check scans every policy row.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI Assisted] nit: this goes stale once JCasbinAuthorizer gets an indexed lookup. Can we drop it, or say "authorizer checks can be expensive"? Same for the "Unbatched, ... 7 authorizer calls per table" comment in the test, which describes the old code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 8e03a64: the class doc now says "authorizer checks can be expensive", and the "Unbatched ... 7 authorizer calls per table" test comment is replaced by one describing what the test checks.

}

@Test
void listFilteringKeepsEachItemsOwnDecision() throws Exception {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI Assisted] nit: this tests ResultFilter.filter. Could it go in a new ResultFilterTest?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 8e03a64: listFilteringKeepsEachItemsOwnDecision moved to a new ResultFilterTest. I left the older listFilteringPreservesItemResourceType where it is to keep this PR's scope tight.

Describe the Batch cache as cleared once when its single refresh runs,
and say that a false or throwing refresh makes every later deny final.
Make batch(...) and Batch package-private, since ResultFilter is the
only caller. Run the refresh-once test for a refresh that returns true,
returns false and throws, and move the ResultFilter test to its own
ResultFilterTest.

Signed-off-by: Simon Maling <[email protected]>
@s-maling-celonis

Copy link
Copy Markdown
Contributor Author

Thanks for checking evaluateAllowsAfterSuccessfulRefresh; agreed it covers the clear, and the parameterized test now covers the multi-item latch cases.

@yili-db

yili-db commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db
yili-db merged commit 4277d12 into unitycatalog:main Nov 5, 2026
20 checks passed
timothyw553 pushed a commit that referenced this pull request Nov 6, 2026
**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [ ] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

`main`'s unit tests fail on
`UnityAccessEvaluatorTest.batchChecksSharedParentsOncePerList`, on H2,
PostgreSQL and MySQL. Every open PR's "All tests" job fails with it,
because CI tests each PR merged with `main`.

#2105 added the test on a base that did not have #2086 yet. #2086
changed `GET_TABLE`'s parent checks from `authorize(OWNER)` to
`authorizeAny(OWNER, READ_METADATA)`, so on `main` the test fails with:

```
Wanted:  authorize(<principal>, metastore, <id>, OWNER)
Actual:  authorizeAny(<principal>, metastore, <id>, OWNER, READ METADATA)
```

The test now verifies the checks that `GET_TABLE` makes on `main`. It
still checks what #2105 meant it to check: each shared parent check runs
once for the whole list, and only the table's own check runs per item.
Test-only change.

Tested: `build/sbt "server/testOnly
io.unitycatalog.server.auth.decorator.UnityAccessEvaluatorTest"`, 37/37
pass. Before the change, the test fails on clean `main` (6e752d4).

This pull request and its description were written by Isaac.

Signed-off-by: Shubham Bansal <[email protected]>
Co-authored-by: Isaac <[email protected]>
@yili-db yili-db mentioned this pull request Nov 6, 2026
4 tasks done
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.

2 participants