-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ActionRequestLazyBuilder implementation of RequestBuilder (#10…
…4927) This introduces a second implementation of RequestBuilder (#104778). As opposed to ActionRequestBuilder, ActionRequestLazyBuilder does not create its request until the request() method is called, and does not hold onto that request (so each call to request() gets a new request instance). This PR also updates BulkRequestBuilder to inherit from ActionRequestLazyBuilder as an example of its use.
- Loading branch information
Showing
4 changed files
with
234 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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); | ||
} | ||
} |