8000 Improve retrieval of large read-only resources via admin API by martin-kanis · Pull Request #52034 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Improve retrieval of large read-only resources via admin API - #52034

Merged
pedroigor merged 1 commit into
keycloak:mainfrom
martin-kanis:52033
Sep 8, 2026
Merged

pedroigor merged 1 commit into
keycloak:mainfrom
martin-kanis:52033

Conversation

@martin-kanis
@martin-kanis martin-kanis commented Aug 25, 2026 •
Copy link
Copy Markdown
Member

Closes #52033

Copilot AI balanced review requested due to automatic review settings August 25, 2026 13:39
@martin-kanis
martin-kanis requested a review from a team as a code owner August 25, 2026 13:39
Copilot AI 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.

Pull request overview

Optimizes large Admin API user searches by suppressing automatic JPA flushes during read-only retrieval.

Changes:

  • Adds a session-scoped JPA read-only flag.
  • Applies commit-only flush mode to queries when enabled.
  • Enables this mode for Admin API user searches.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
UsersResource.java Enables read-only JPA behavior for user searches.
EntityManagerProxy.java Applies commit-only query flush mode.
EntityManagerProxyTest.java Updates the proxy factory invocation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@martin-kanis
martin-kanis requested a review from pedroigor August 26, 2026 10:43
Copilot AI 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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Copilot AI 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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

server-spi-private/src/main/java/org/keycloak/connections/jpa/support/EntityManagerProxy.java:105

  • The existing EntityManagerProxyTest only checks close tracking and passes a null session, so the new read-only flush behavior is untested. Add cases with read-only and writable sessions that verify FlushModeType.COMMIT is applied only to queries created while read-only.
            if ((batched || readOnly) && result instanceof Query query) {

Comment on lines +164 to +167
boolean previous = readOnly;
readOnly = true;
try {
return supplier.get().onClose(() -> readOnly = previous);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There are no nested streams here. If there is a need in the future (I don't see a valid usecase to nest these read-only wrappers) it can be handled then.

* Executes the given supplier in read-only mode. The read-only flag is set before invoking the
* supplier and cleared when the returned stream is closed, making it safe for lazily consumed streams.
*/
<T> Stream<T> runAsReadOnly(Supplier<Stream<T>> supplier);
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.

Works for me. But if you think we can take the opportunity and have a more generic method that returns instead of a Stream<T>, I think it will be best to support methods that return objects other than a stream.

Copilot AI review requested due to automatic review settings August 31, 2026 11:43
Copilot AI 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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Suppressed comments (1)

server-spi-private/src/main/java/org/keycloak/connections/jpa/support/EntityManagerProxy.java:105

  • The existing EntityManagerProxyTest only adapts the factory call, so the new read-only branch is untested. Add a unit test with a read-only session and an EntityManager returning a mock Query that verifies setFlushMode(COMMIT), plus a non-read-only case to guard the default behavior.
            if ((batched || readOnly) && result instanceof Query query) {

Closes keycloak#52033

Signed-off-by: Martin Kanis <mkanis@ibm.com>
Copilot AI 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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Suppressed comments (2)

server-spi-private/src/main/java/org/keycloak/connections/jpa/support/EntityManagerProxy.java:105

  • The updated proxy test passes a null session and only checks closure, leaving this new read-only branch untested. Add cases with a session and returned Query that verify FlushModeType.COMMIT is applied when read-only and is not applied when read-only is false.
        boolean readOnly = session != null && session.isReadOnly();
        try {
            flushInBatchIfEnabled(method);
            Object result = method.invoke(em, args);
            if ((batched || readOnly) && result instanceof Query query) {

services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java:170

  • The new session-state contract has no tests, so regressions in the immediate-return, lazy-stream, close, and exceptional-supplier paths would go unnoticed. Add focused DefaultKeycloakSession tests that assert the flag is active only for the intended scope and is restored afterward.
    public <T> T runAsReadOnly(Supplier<T> supplier) {
        boolean previous = readOnly;
        readOnly = true;
        try {
            T result = supplier.get();
            if (result instanceof Stream<?> stream) {
                return (T) stream.onClose(() -> readOnly = previous);

@keycloak-github-bot keycloak-github-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.

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.LevelOfAssuranceFlowTest#testWithOTPAndRecoveryCodesAtLevel2

Keycloak CI - Forms IT (chrome)

org.openqa.selenium.WebDriverException: 
unknown error: unhandled inspector error: {"code":-32000,"message":"Node with given id does not belong to the document"}
  (Session info: chrome=151.0.7922.173)
Build info: version: '4.46.0', revision: 'df5a634 df5a6341cbb5f8f06b836bd595752775627c80dc'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.17.0-1022-azure', java.version: '25.0.4.1'
...

Report flaky test

@keycloak-github-bot keycloak-github-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.

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.LevelOfAssuranceFlowTest#testWithOTPAndRecoveryCodesAtLevel2

Keycloak CI - Forms IT (chrome)

org.openqa.selenium.WebDriverException: 
unknown error: unhandled inspector error: {"code":-32000,"message":"Node with given id does not belong to the document"}
  (Session info: chrome=151.0.7922.173)
Build info: version: '4.46.0', revision: 'df5a634 df5a6341cbb5f8f06b836bd595752775627c80dc'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.17.0-1022-azure', java.version: '25.0.4.1'
...

Report flaky test

@pedroigor
pedroigor merged commit 68ff311 into keycloak:main Sep 8, 2026
151 of 153 checks passed
BD63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve retrieval of large read-only resources via admin API

4 participants

0