-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e00c7a1
Add a failing test
joegallo 1c3e2d7
Mark these parameters final
joegallo f3c53bb
Capture the project id, not project metadata
joegallo cff6ab8
Put this project reference in a dedicated scope
joegallo c242bab
Update docs/changelog/133752.yaml
joegallo 253425b
Update changelog
joegallo 66d00b9
Merge branch 'main' into enrich-source-change-bug
joegallo 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: 133752 | ||
summary: Avoid stale enrich results after policy execution | ||
area: Ingest Node | ||
type: bug | ||
issues: [] |
167 changes: 167 additions & 0 deletions
167
...src/internalClusterTest/java/org/elasticsearch/xpack/enrich/EnrichSourceDataChangeIT.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,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 | ||
.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(); | ||
} | ||
|
||
} |
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
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.
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
Uh oh!
There was an error while loading. Please reload this page.
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.
👍, 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))