-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Avoid stale enrich results after policy execution #133752
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 5 commits
e00c7a1
1c3e2d7
f3c53bb
cff6ab8
c242bab
253425b
66d00b9
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 133752 | ||
summary: Fix enrich fails to update when source changes | ||
area: Ingest Node | ||
type: bug | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.enrich; | ||
|
||
import org.elasticsearch.action.get.GetRequest; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.core.Strings; | ||
import org.elasticsearch.ingest.common.IngestCommonPlugin; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.reindex.ReindexPlugin; | ||
import org.elasticsearch.test.ESSingleNodeTestCase; | ||
import org.elasticsearch.xcontent.XContentType; | ||
import org.elasticsearch.xpack.core.XPackSettings; | ||
import org.elasticsearch.xpack.core.enrich.EnrichPolicy; | ||
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; | ||
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.xpack.enrich.AbstractEnrichTestCase.createSourceIndices; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class EnrichSourceDataChangeIT extends ESSingleNodeTestCase { | ||
|
||
@Override | ||
protected Collection<Class<? extends Plugin>> getPlugins() { | ||
return List.of(LocalStateEnrich.class, ReindexPlugin.class, IngestCommonPlugin.class); | ||
} | ||
|
||
@Override | ||
protected Settings nodeSettings() { | ||
return Settings.builder() | ||
// TODO Change this to run with security enabled | ||
// https://github.com/elastic/elasticsearch/issues/75940 | ||
Comment on lines
+42
to
+43
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. Should this new test be updated to run with security? If not, we should make sure to update #75940 with a check list item for this test 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. 👍, that's fair -- I'll add this one to the checklist for now, but separately take on the task of thinking about making these tests run with security. (edit: done, and promises made publicly #75940 (comment)) |
||
.put(XPackSettings.SECURITY_ENABLED.getKey(), false) | ||
.build(); | ||
} | ||
|
||
private final String policyName = "device-enrich-policy"; | ||
private final String sourceIndexName = "devices-idx"; | ||
|
||
public void testChangesToTheSourceIndexTakeEffectOnPolicyExecution() throws Exception { | ||
// create and store the enrich policy | ||
final var enrichPolicy = new EnrichPolicy( | ||
EnrichPolicy.MATCH_TYPE, | ||
null, | ||
List.of(sourceIndexName), | ||
"host.ip", | ||
List.of("device.name", "host.ip") | ||
); | ||
|
||
// create the source index | ||
createSourceIndices(client(), enrichPolicy); | ||
|
||
final String initialDeviceName = "some.device." + randomAlphaOfLength(10); | ||
|
||
// add a single document to the enrich index | ||
setEnrichDeviceName(initialDeviceName); | ||
|
||
// store the enrich policy and execute it | ||
var putPolicyRequest = new PutEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, policyName, enrichPolicy); | ||
client().execute(PutEnrichPolicyAction.INSTANCE, putPolicyRequest).actionGet(); | ||
executeEnrichPolicy(); | ||
|
||
// create an honest to goodness pipeline for repeated executions (we're not running any _simulate requests here) | ||
final String pipelineName = "my-pipeline"; | ||
putJsonPipeline(pipelineName, """ | ||
{ | ||
"processors": [ | ||
{ | ||
"enrich": { | ||
"policy_name": "device-enrich-policy", | ||
"field": "host.ip", | ||
"target_field": "_tmp.device" | ||
} | ||
}, | ||
{ | ||
"rename" : { | ||
"field" : "_tmp.device.device.name", | ||
"target_field" : "device.name" | ||
} | ||
}, | ||
{ | ||
"remove" : { | ||
"field" : "_tmp" | ||
} | ||
} | ||
] | ||
}"""); | ||
|
||
{ | ||
final var indexRequest = new IndexRequest(sourceIndexName); | ||
indexRequest.id("1"); | ||
indexRequest.setPipeline("my-pipeline"); | ||
indexRequest.source(""" | ||
{ | ||
"host": { | ||
"ip": "10.151.80.8" | ||
} | ||
} | ||
""", XContentType.JSON); | ||
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
client().index(indexRequest).actionGet(); | ||
|
||
final var response = client().get(new GetRequest(sourceIndexName).id("1")).actionGet(); | ||
assertThat(response.getSource().get("device"), equalTo(Map.of("name", initialDeviceName))); | ||
} | ||
|
||
// add different document to the enrich index | ||
final String changedDeviceName = "some.device." + randomAlphaOfLength(10); | ||
setEnrichDeviceName(changedDeviceName); | ||
|
||
// execute the policy to pick up the change | ||
executeEnrichPolicy(); | ||
|
||
// it can take a moment for the execution to take effect, so assertBusy | ||
assertBusy(() -> { | ||
final var indexRequest = new IndexRequest(sourceIndexName); | ||
indexRequest.id("2"); | ||
indexRequest.setPipeline("my-pipeline"); | ||
indexRequest.source(""" | ||
{ | ||
"host": { | ||
"ip": "10.151.80.8" | ||
} | ||
} | ||
""", XContentType.JSON); | ||
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
client().index(indexRequest).actionGet(); | ||
|
||
final var response = client().get(new GetRequest(sourceIndexName).id("2")).actionGet(); | ||
assertThat(response.getSource().get("device"), equalTo(Map.of("name", changedDeviceName))); | ||
}); | ||
} | ||
|
||
private void setEnrichDeviceName(final String deviceName) { | ||
final var indexRequest = new IndexRequest(sourceIndexName); | ||
indexRequest.id("1"); // there's only one document, and we keep overwriting it | ||
indexRequest.source(Strings.format(""" | ||
{ | ||
"host": { | ||
"ip": "10.151.80.8" | ||
}, | ||
"device": { | ||
"name": "%s" | ||
} | ||
} | ||
""", deviceName), XContentType.JSON); | ||
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
client().index(indexRequest).actionGet(); | ||
} | ||
|
||
private void executeEnrichPolicy() { | ||
final var executePolicyRequest = new ExecuteEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, policyName); | ||
client().execute(ExecuteEnrichPolicyAction.INSTANCE, executePolicyRequest).actionGet(); | ||
} | ||
|
||
} |
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.
Nit: should we rephrase this to make it easier to understand and find for users? Perhaps something like:
Avoid stale enrich cache results after policy is re-ran
?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.
👍, 253425b