▶️ Run queries in the background and retrieve partial results along the way https://opensearch.org/docs/latest/search-plugins/async/index/
  • Java 99.7%
  • Shell 0.3%
Find a file
Darshit Chanpura bd71cd1028
Run system index operations as the plugin subject (#878)
* Add PluginClient for running actions as the plugin subject

Wraps the node client and routes doExecute through the subject assigned to the
plugin, so operations on the plugin's own system index do not depend on the
privileges of whoever triggered them.

Kept as a local class until the shared implementation in common-utils is
published, after which this becomes an import change.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Run system index operations as the plugin subject

The plugin persists, reads, updates and expires asynchronous search responses in
.opendistro-asynchronous-search-response, which it registers as a system index.
Those operations now go through a client bound to the subject assigned by
IdentityAwarePlugin, so they do not depend on the privileges of whoever happened
to trigger them, and the response persistence and the local handling of a get or
delete run in the caller's context.

The search a user submits is unaffected. It is executed by the submit action
against the user's own indices and continues to run as that user.

AsynchronousSearchManagementService keeps its stash. It wraps a node to node
transport request for the plugin's own cleanup handler rather than a client
action, so a plugin client cannot express it. The delete by query it eventually
triggers on the receiving node is a client call and does run as the subject.

Adds plugin-additional-permissions.yml for the three actions that security
classifies as cluster permissions and so does not cover with the grant a plugin
has on its own system indices: the task cancellation behind a delete, and the
scroll and scroll clear that delete by query drives internally.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Align PluginClient with the shared client design

Restores the caller's context for the listener with
ContextPreservingActionListener. The previous form restored it with
runBefore and never put the responding thread's own context back, so the
caller's context was left on the responding thread and response headers
the action added were dropped.

Removes the catch around runAs. Core already reports validation and filter
failures through the listener, and the synchronous failures that escape
are ones the node client throws to every caller. Catching them changed
that contract for this one client and notified the listener a second time
when the listener itself threw.

Types the subject as PluginSubject, assigns it once and rejects null or a
different second subject, refuses getRemoteClusterClient because a plugin
subject has no meaning on another cluster, and makes the class final.

Ports the tests of the shared client, with a fake subject that stashes and
restores around its body as the real subjects do.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Refresh before reading a stored response when the GET is not realtime

Workaround until the security plugin keeps GETs realtime for a plugin
reading its own system index.

getResponse reads a response from .opendistro-asynchronous-search-response
right after storeResponse indexed it and the active context was freed. Now
that the GET runs as the plugin subject, the security plugin evaluates it
and turns realtime off because the index is a system index, so the GET can
miss the document until the next refresh and the caller gets a 404. Under
the stashed context the request was not evaluated and stayed realtime.

Setting refresh on the GetRequest makes core refresh the shard before a
non-realtime GET. Core ignores the flag on a realtime GET, so without the
security plugin, or once the security plugin keeps it realtime, nothing
changes.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Describe the GET refresh workaround as the code stands

Rewords the comment on the refresh in getResponse so it states why the GET
is not realtime rather than what changed, and renames its test after what
it checks. Part of the same workaround as the previous commit.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Test the plugin subject wiring and document assignSubject

Adds a test that builds the plugin's components, assigns a subject and
checks that the persistence service runs its actions as that subject, so
dropping the assignment or handing the service the plain client fails a
unit test rather than only an integration test with the security plugin.

Extends the PluginClient tests to cover a listener completed inside runAs,
that a rejected second subject does not replace the first, and that the
action never runs without a subject.

Adds Javadoc to assignSubject and shortens the comment in the routing
action to describe the code as it is.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* State the cost of the GET refresh workaround

With the security plugin installed every GET of a stored response refreshes one shard first. The comment now says so instead of only noting that the flag does nothing while realtime holds. Part of the same workaround.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Share the fake plugin subject between tests

PluginClientTests and AsynchronousSearchPluginTests each declared the same subject that stashes and marks the context. Both now use one RecordingSubject test helper, and the plugin test builds its cluster service before the try block.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Use the shared PluginClient from common-utils

PluginClient now extends org.opensearch.commons.client.PluginClient and keeps
only the constructor the plugin calls. The subject handling, the caller
context restore, the remote cluster refusal and the action wrapping it used
to copy are removed, because the base class provides them with the same
code, the same checks and the same messages. Its package and name stay, so
no call site changes.

PluginClientTests is removed because it only tested the copied client, and
the same tests now live in common-utils. AsynchronousSearchPluginTests still
checks that the subject from assignSubject reaches the client the
persistence service uses.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* Drop the refresh before reading a stored response

getResponse set refresh(true) on its GET because the security plugin's
legacy evaluator turned realtime off for any GET on a system index once
the GET ran as the plugin subject, and a response stored a moment ago
was then not found. The security plugin now keeps realtime for a plugin
subject's GET when every index it names is one of the plugin's own
registered system indices and none of them is protected or the security
index. This GET names only the response index, which the plugin
registers, so it stays realtime and the refresh is no longer needed.

The unit test now checks that the GET asks for realtime, names only the
response index, that this index is one the plugin registers, and that it
does not ask for a refresh.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

* State the protected index condition in the realtime GET comment

The security plugin keeps the GET realtime only when none of the indices
it names is protected, so the comment says so. The test drops a comment
that repeated its assertion messages.

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>

---------

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
2026-12-01 09:41:26 -08:00
.github Add SPDX license header checker and missing headers (#864) 2026-12-01 09:27:41 -08:00
.idea/copyright Enabled automated license header checks 2021-06-12 12:59:25 +05:30
build-tools Update to Gradle 9.1 (#773) 2025-10-13 11:37:59 -07:00
formatter Configure spotless for asynchronous-search (#669) 2024-11-13 08:21:46 -08:00
gradle Guard eclipse() to spotless tasks and keep P2 mirror on pinned version (asynchronous-search) (#867) 2026-09-23 16:25:35 -04:00
release-notes Add release notes for 3.10.1 (#895) (#897) 2026-11-23 22:00:54 -05:00
scripts Uses custom plugin to publish zips to maven (#150) 2022-06-15 11:56:04 +05:30
src Run system index operations as the plugin subject (#878) 2026-12-01 09:41:26 -08:00
.codecov.yml Configure spotless for asynchronous-search (#669) 2024-11-13 08:21:46 -08:00
.gitignore Bump bwc version -> 2.15 (#574) 2024-05-17 11:42:28 -07:00
.licenserc.json Add SPDX license header checker and missing headers (#864) 2026-12-01 09:27:41 -08:00
build.gradle Increment version to 3.11.0-SNAPSHOT (#888) 2026-11-20 14:01:30 -05:00
CODE_OF_CONDUCT.md minor changes in OSS files 2021-04-16 16:11:59 +05:30
CONTRIBUTING.md Correct copyright notices to reflect Copyright OpenSearch Contributors (#61) 2021-11-10 02:56:24 +05:30
DEVELOPER_GUIDE.md Add CI Check to ensure links in documentations resolve correctly (#717) 2025-04-22 11:07:45 -07:00
gradlew Update to Gradle 9.2 and use JDK 25 for GHA (#792) 2025-11-24 10:52:19 -08:00
gradlew.bat Update to Gradle 9.2 and use JDK 25 for GHA (#792) 2025-11-24 10:52:19 -08:00
LICENSE Initial commit 2020-10-27 00:48:38 -07:00
MAINTAINERS.md Remove the trailing blank line from MAINTAINERS.md (#883) 2026-11-03 15:30:52 -06:00
NOTICE Correct copyright notices to reflect Copyright OpenSearch Contributors (#61) 2021-11-10 02:56:24 +05:30
README.md Configure spotless for asynchronous-search (#669) 2024-11-13 08:21:46 -08:00
RELEASING.md Configure spotless for asynchronous-search (#669) 2024-11-13 08:21:46 -08:00
SECURITY.md Updating security reachout email (#571) 2024-05-21 09:17:49 -04:00
settings.gradle Add ci.opensearch.org/m2/ mirror for plugin resolution (asynchronous-search) (#851) (#853) 2026-07-31 17:42:41 -04:00

Test and Build Workflow codecov

Asynchronous search

Asynchronous search makes it possible for users to run such queries without worrying about the query timing out. These queries run in the background, and users can track the progress, and retrieve partial results as they become available.

The asynchronous search plugin supports the below operations

1. Submit asynchronous search

POST /_plugins/_asynchronous_search?wait_for_completion_timeout=500ms&keep_on_completion=true&keep_alive=3d
{  "aggs": {
    "city": {
      "terms": {
        "field": "city", "size": 50
      }
    }
  }
}

2. Retrieve asynchronous search results

GET /_plugins/_asynchronous_search/FjdITFhYbC1zVFdHVVV1MUd3UkxkMFEFMjQ1MzYUWHRrZjhuWUJXdFhxMmlCSW5HTE8BMQ==?keep_alive=3d

3. Delete an asynchronous search

DELETE /_plugins/_asynchronous_search/FjdITFhYbC1zVFdHVVV1MUd3UkxkMFEFMjQ1MzYUWHRrZjhuWUJXdFhxMmlCSW5HTE8BMQ==

4. Stats for asynchronous search

GET /_plugins/_asynchronous_search/stats

Tunable Settings

  1. plugins.asynchronous_search.max_search_running_time : Maximum running time for the search beyond which the search would be terminated
  2. plugins.asynchronous_search.node_concurrent_running_searches : Concurrent searches running per coordinator node
  3. plugins.asynchronous_search.max_keep_alive : Maximum keep alive for search which dictates how long the search is allowed to be present in the cluster
  4. plugins.asynchronous_search.max_wait_for_completion_timeout : Maximum keep on completion to block for the search response
  5. plugins.asynchronous_search.persist_search_failures : Persist asynchronous search result ending with search failure in system index

Setup

  1. Check out this package from version control.
  2. Launch Intellij IDEA, choose Import Project, and select the settings.gradle file in the root of this package.
  3. To build from the command line, set JAVA_HOME to point to a JDK >= 8 before running ./gradlew.
  • Unix System

    1. export JAVA_HOME=jdk-install-dir: Replace jdk-install-dir with the JAVA_HOME directory of your system.
    2. export PATH=$JAVA_HOME/bin:$PATH
  • Windows System

    1. Find My Computers from file directory, right click and select properties.
    2. Select the Advanced tab, select Environment variables.
    3. Edit JAVA_HOME to path of where JDK software is installed.

Build

The project in this package uses the Gradle build system. Gradle comes with excellent documentation that should be your first stop when trying to figure out how to operate or modify the build.

Building from the command line

  • ./gradlew build builds and tests project.
  • ./gradlew run launches a single node cluster with the asynchronous search plugin installed.
  • ./gradlew run -PnumNodes=3 launches a multi-node cluster with the asynchronous search plugin installed.

Integration tests

  • ./gradlew integTest launches a single node cluster with the asynchronous search plugin installed and runs all integ tests.
  • ./gradlew integTest -PnumNodes=3 launches a multi-node cluster with the asynchronous search plugin installed and runs all integ tests.
  • ./gradlew integTest -Dtests.class=*AsynchronousSearchRestIT runs a single integ class
  • ./gradlew integTest -Dtests.class=*AsynchronousSearchRestIT -Dtests.method="testSubmitWithRetainedResponse" runs a single integ test method (remember to quote the test method name if it contains spaces)
  • ./gradlew integTestRemote -Dtests.rest.cluster=localhost:9200 -Dtests.cluster=localhost:9200 -Dtests.clustername="docker-cluster" -Dhttps=true -Duser=admin -Dpassword=admin launches integration tests against a local cluster and run tests with security./gradlew integTestRemote -Dtests.rest.cluster=localhost:9200 -Dtests.cluster=localhost:9200 -Dtests.clustername="docker-cluster" -Dhttps=true -Duser=admin -Dpassword=<admin-password> launches integration tests against a local cluster and run tests with security

Backwards compatibility tests

Note that while all backwards compatibility tests support the -PcustomDistributionDownloadType=bundle flag for testing with on the distribution bundle level, only changes previously published to the bundled distribution will be captured by these tests.

  • ./gradlew asynSearchCluster#mixedClusterTask -Dtests.security.manager=false launches a cluster of three nodes of bwc version of OpenSearch with async search plugin and tests backwards compatibility by performing rolling upgrade of one node with the current version of OpenSearch with async search plugin.
  • ./gradlew asynSearchCluster#rollingUpgradeClusterTask -Dtests.security.manager=false launches a cluster with three nodes of bwc version of OpenSearch with async search plugin and tests backwards compatibility by performing rolling upgrade of all nodes with the current version of OpenSearch with async search plugin.
  • ./gradlew asynSearchCluster#fullRestartClusterTask -Dtests.security.manager=false launches a cluster with three nodes of bwc version of OpenSearch with async search plugin and tests backwards compatibility by performing a full restart on the cluster upgrading all the nodes with the current version of OpenSearch with async search plugin.
  • ./gradlew bwcTestSuite -Dtests.security.manager=false runs all the above bwc tests combined.

When launching a cluster using one of the above commands, logs are placed in build/testclusters/<node-identifier>/logs. Though the logs are tied to the console, in practices it's best to check the actual log file.

Debugging

Sometimes it is useful to attach a debugger to either the OpenSearch cluster or the integ tests to see what's going on. When running unit tests, hit Debug from the IDE's gutter to debug the tests. For the OpenSearch cluster or the integ tests, first, make sure start a debugger listening on port 5005.

To debug the server code, run:

./gradlew :integTest -Dcluster.debug # to start a cluster with debugger and run integ tests

OR

./gradlew run --debug-jvm # to just start a cluster that can be debugged

The OpenSearch server JVM will connect to a debugger attached to localhost:5005.

The IDE needs to listen for the remote JVM. If using Intellij you must set your debug configuration to "Listen to remote JVM" and make sure "Auto Restart" is checked. You must start your debugger to listen for remote JVM before running the commands.

To debug code running in an integration test (which exercises the server from a separate JVM), first, setup a remote debugger listening on port 8000, and then run:

./gradlew :integTest -Dtest.debug

The test runner JVM will connect to a debugger attached to localhost:8000 before running the tests.

Additionally, it is possible to attach one debugger to the cluster JVM and another debugger to the test runner. First, make sure one debugger is listening on port 5005 and the other is listening on port 8000. Then, run:

./gradlew :integTest -Dtest.debug -Dcluster.debug

Getting Help

If you find a bug, or have a feature request, please don't hesitate to open an issue in this repository.

For more information, see the project website and technical documentation. If you need help and are unsure where to open an issue, try the OpenSearch Forum.

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache v2.0 License.

Copyright OpenSearch Contributors. See NOTICE for details.