-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Adding ActionRequestLazyBuilder implementation of RequestBuilder #104927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
masseyke
merged 11 commits into
elastic:main
from
masseyke:adding-ActionRequestLazyBuilder
Jan 30, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c2b32df
Adding ActionRequestLazyBuilder implementation of RequestBuilder
masseyke c2239ec
Update docs/changelog/104927.yaml
masseyke 16945e8
Adding a temporary javadoc fix
masseyke 0a5cbc5
Merge branch 'adding-ActionRequestLazyBuilder' of github.com:masseyke…
masseyke 11cf930
removing code that is out of scope for this PR
masseyke f775713
Merge branch 'main' into adding-ActionRequestLazyBuilder
masseyke cc53db7
Getting rid of apply() and ManagedActionRequestLazyBuilder
masseyke 11072db
getting rid of unnecessary methods
masseyke 6ac3c28
code review feedback
masseyke f98e89e
fixing vairiables
masseyke ca07936
fixing dumb mistake
masseyke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 104927 | ||
| summary: Adding `ActionRequestLazyBuilder` implementation of `RequestBuilder` | ||
| area: Ingest Node | ||
| type: enhancement | ||
| issues: [] |
61 changes: 61 additions & 0 deletions
61
server/src/main/java/org/elasticsearch/action/ActionRequestLazyBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.action; | ||
|
|
||
| import org.elasticsearch.client.internal.ElasticsearchClient; | ||
| import org.elasticsearch.core.TimeValue; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * This class is similar to ActionRequestBuilder, except that it does not build the request until the request() method is called. | ||
| * @param <Request> | ||
| * @param <Response> | ||
| */ | ||
| public abstract class ActionRequestLazyBuilder<Request extends ActionRequest, Response extends ActionResponse> | ||
| implements | ||
| RequestBuilder<Request, Response> { | ||
|
|
||
| protected final ActionType<Response> action; | ||
| protected final ElasticsearchClient client; | ||
|
|
||
| protected ActionRequestLazyBuilder(ElasticsearchClient client, ActionType<Response> action) { | ||
| Objects.requireNonNull(action, "action must not be null"); | ||
| this.action = action; | ||
| this.client = client; | ||
| } | ||
|
|
||
| /** | ||
| * This method creates the request. The caller of this method is responsible for calling Request#decRef. | ||
| * @return A newly-built Request, fully initialized by this builder. | ||
| */ | ||
| public abstract Request request(); | ||
|
|
||
| public ActionFuture<Response> execute() { | ||
| return client.execute(action, request()); | ||
| } | ||
|
|
||
| /** | ||
| * Short version of execute().actionGet(). | ||
| */ | ||
| public Response get() { | ||
| return execute().actionGet(); | ||
| } | ||
|
|
||
| /** | ||
| * Short version of execute().actionGet(). | ||
| */ | ||
| public Response get(TimeValue timeout) { | ||
| return execute().actionGet(timeout); | ||
| } | ||
|
|
||
| public void execute(ActionListener<Response> listener) { | ||
| client.execute(action, request(), listener); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/src/test/java/org/elasticsearch/action/bulk/BulkRequestBuilderTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.action.bulk; | ||
|
|
||
| import org.elasticsearch.action.index.IndexRequest; | ||
| import org.elasticsearch.action.index.IndexRequestBuilder; | ||
| import org.elasticsearch.action.support.WriteRequest; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| public class BulkRequestBuilderTests extends ESTestCase { | ||
|
|
||
| public void testValidation() { | ||
| BulkRequestBuilder bulkRequestBuilder = new BulkRequestBuilder(null, null); | ||
| bulkRequestBuilder.add(new IndexRequestBuilder(null, randomAlphaOfLength(10))); | ||
| bulkRequestBuilder.add(new IndexRequest()); | ||
| expectThrows(IllegalStateException.class, bulkRequestBuilder::request); | ||
|
|
||
| bulkRequestBuilder = new BulkRequestBuilder(null, null); | ||
| bulkRequestBuilder.add(new IndexRequestBuilder(null, randomAlphaOfLength(10))); | ||
| bulkRequestBuilder.setTimeout(randomTimeValue()); | ||
| bulkRequestBuilder.setTimeout(TimeValue.timeValueSeconds(randomIntBetween(1, 30))); | ||
| expectThrows(IllegalStateException.class, bulkRequestBuilder::request); | ||
|
|
||
| bulkRequestBuilder = new BulkRequestBuilder(null, null); | ||
| bulkRequestBuilder.add(new IndexRequestBuilder(null, randomAlphaOfLength(10))); | ||
| bulkRequestBuilder.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values()).getValue()); | ||
| bulkRequestBuilder.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())); | ||
| expectThrows(IllegalStateException.class, bulkRequestBuilder::request); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low value contribution, but let the record show I was here: maybe these two
!= nullchecks could be switch so they're symmetrical with the previous ones for the timeout handling.