-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add wait for completion for Enrich policy execution #47886
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
Changes from 4 commits
7ac5aa9
1934ef6
2f2e2a5
b8e6de8
90e6fdb
66457cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
*/ | ||
package org.elasticsearch.xpack.enrich; | ||
|
||
import org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskAction; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskRequest; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse; | ||
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; | ||
import org.elasticsearch.action.bulk.BulkItemResponse; | ||
import org.elasticsearch.action.bulk.BulkRequest; | ||
|
@@ -23,6 +26,7 @@ | |
import org.elasticsearch.xpack.core.enrich.EnrichPolicy; | ||
import org.elasticsearch.xpack.core.enrich.action.EnrichStatsAction; | ||
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; | ||
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyStatus; | ||
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; | ||
|
||
import java.util.Collection; | ||
|
@@ -37,7 +41,9 @@ | |
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class BasicEnrichTests extends ESSingleNodeTestCase { | ||
|
||
|
@@ -208,6 +214,58 @@ public void testMultiplePolicies() { | |
} | ||
} | ||
|
||
public void testAsyncTaskExecute() throws Exception { | ||
String policyName = "async-policy"; | ||
String sourceIndexName = "async-policy-source"; | ||
|
||
{ | ||
IndexRequest indexRequest = new IndexRequest(sourceIndexName); | ||
indexRequest.source("key", "key", "value", "val1"); | ||
client().index(indexRequest).actionGet(); | ||
client().admin().indices().refresh(new RefreshRequest(sourceIndexName)).actionGet(); | ||
} | ||
|
||
EnrichPolicy enrichPolicy = | ||
new EnrichPolicy(EnrichPolicy.MATCH_TYPE, null, List.of(sourceIndexName), "key", List.of("value")); | ||
PutEnrichPolicyAction.Request request = new PutEnrichPolicyAction.Request(policyName, enrichPolicy); | ||
client().execute(PutEnrichPolicyAction.INSTANCE, request).actionGet(); | ||
ExecuteEnrichPolicyAction.Response executeResponse = client() | ||
.execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(policyName).setWaitForCompletion(false)) | ||
.actionGet(); | ||
|
||
assertThat(executeResponse.getStatus(), is(nullValue())); | ||
assertThat(executeResponse.getTaskId(), is(not(nullValue()))); | ||
GetTaskRequest getPolicyTaskRequest = new GetTaskRequest().setTaskId(executeResponse.getTaskId()).setWaitForCompletion(true); | ||
GetTaskResponse taskResponse = client().execute(GetTaskAction.INSTANCE, getPolicyTaskRequest).actionGet(); | ||
assertThat(((ExecuteEnrichPolicyStatus) taskResponse.getTask().getTask().getStatus()).getPhase(), | ||
is(ExecuteEnrichPolicyStatus.PolicyPhases.COMPLETE)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would place this inside an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the assertBusy line. In the code the task request has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But in this test If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah nevermind my comment... I missed that So the assertBusy(...) doesn't make sense and can be removed. |
||
|
||
String pipelineName = "test-pipeline"; | ||
String pipelineBody = "{\"processors\": [{\"enrich\": {\"policy_name\":\"" + policyName + | ||
"\", \"field\": \"key\", \"target_field\": \"target\"}}]}"; | ||
PutPipelineRequest putPipelineRequest = new PutPipelineRequest(pipelineName, new BytesArray(pipelineBody), XContentType.JSON); | ||
client().admin().cluster().putPipeline(putPipelineRequest).actionGet(); | ||
|
||
BulkRequest bulkRequest = new BulkRequest("my-index"); | ||
int numTestDocs = randomIntBetween(3, 10); | ||
for (int i = 0; i < numTestDocs; i++) { | ||
IndexRequest indexRequest = new IndexRequest("my-index"); | ||
indexRequest.id(Integer.toString(i)); | ||
indexRequest.setPipeline(pipelineName); | ||
indexRequest.source(Map.of("key", "key")); | ||
bulkRequest.add(indexRequest); | ||
} | ||
BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet(); | ||
assertThat("Expected no failure, but " + bulkResponse.buildFailureMessage(), bulkResponse.hasFailures(), is(false)); | ||
|
||
for (int i = 0; i < numTestDocs; i++) { | ||
GetResponse getResponse = client().get(new GetRequest("my-index", Integer.toString(i))).actionGet(); | ||
Map<String, Object> source = getResponse.getSourceAsMap(); | ||
assertThat(source.size(), equalTo(2)); | ||
assertThat(source.get("target"), equalTo(List.of(Map.of("key", "key", "value", "val1")))); | ||
} | ||
} | ||
|
||
private List<String> createSourceMatchIndex(int numKeys, int numDocsPerKey) { | ||
Set<String> keys = new HashSet<>(); | ||
for (int id = 0; id < numKeys; id++) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.