-
Couldn't load subscription status.
- Fork 25.6k
Add recover_failure_document processor to restore failurestore docs to original form #133360
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 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
07e7139
Add remediate processor to remediate failure docs.
seanzatzdev 3b0f998
add version to tests
seanzatzdev 1f3e4ec
nit
seanzatzdev a48d5f5
nit fixes
seanzatzdev 09fdfb8
nit
seanzatzdev c410390
Add yaml rest tests
seanzatzdev b493955
Merge branch 'elastic:main' into remediate-processor
seanzatzdev 901c3e3
Add docs
seanzatzdev d548b89
Update docs/changelog/133360.yaml
seanzatzdev 819f67f
Merge branch 'main' into remediate-processor
seanzatzdev 497382e
Merge branch 'main' into remediate-processor
seanzatzdev ed25287
rename to recover_failure_document processor
seanzatzdev 7baf17f
Add pre_recovery ingest metadata field to store original doc info
seanzatzdev 663eadc
Update docs
seanzatzdev edf6758
Merge branch 'main' into remediate-processor
seanzatzdev 8e9f58a
clean up the code a bit
seanzatzdev 4a92135
Merge branch 'main' into remediate-processor
seanzatzdev 7a8daf0
Merge branch 'main' into remediate-processor
seanzatzdev 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: 133360 | ||
| summary: Add remediate processor to remediate failurestore docs | ||
| area: Ingest Node | ||
| type: feature | ||
| issues: [] |
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
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,146 @@ | ||
| --- | ||
| navigation_title: "Remediate" | ||
| mapped_pages: | ||
| - https://www.elastic.co/guide/en/elasticsearch/reference/current/remediate-processor.html | ||
| --- | ||
|
|
||
| # Remediate processor [remediate-processor] | ||
|
|
||
| Remediates documents that have been stored in a data stream's failure store by restoring them to their original format. This processor is designed to work with documents that failed during ingestion and were automatically stored in the failure store with additional error metadata and document structure wrapping. | ||
|
|
||
| The remediate processor performs the following operations: | ||
|
|
||
| * Extracts the original document source from the `document.source` field | ||
| * Restores the original index name from `document.index` to the document metadata | ||
| * Restores the original routing value from `document.routing` to the document metadata (if present) | ||
| * Removes the failure-related fields (`error` and `document`) from the document | ||
| * Places all original source fields back at the root level of the document | ||
|
|
||
| $$$remediate-options$$$ | ||
|
|
||
| | Name | Required | Default | Description | | ||
| | --- | --- | --- | --- | | ||
| | `description` | no | - | Description of the processor. Useful for describing the purpose of the processor or its configuration. | | ||
| | `if` | no | - | Conditionally execute the processor. See [Conditionally run a processor](docs-content://manage-data/ingest/transform-enrich/ingest-pipelines.md#conditionally-run-processor). | | ||
| | `ignore_failure` | no | `false` | Ignore failures for the processor. See [Handling pipeline failures](docs-content://manage-data/ingest/transform-enrich/ingest-pipelines.md#handling-pipeline-failures). | | ||
| | `on_failure` | no | - | Handle failures for the processor. See [Handling pipeline failures](docs-content://manage-data/ingest/transform-enrich/ingest-pipelines.md#handling-pipeline-failures). | | ||
| | `tag` | no | - | Identifier for the processor. Useful for debugging and metrics. | | ||
|
|
||
| ## Examples [remediate-processor-ex] | ||
|
|
||
| ```console | ||
| POST _ingest/pipeline/_simulate | ||
| { | ||
| "pipeline": { | ||
| "processors": [ | ||
| { | ||
| "remediate": {} | ||
| } | ||
| ] | ||
| }, | ||
| "docs": [ | ||
| { | ||
| "_index": ".fs-my-datastream-ingest-2025.05.09-000001", | ||
| "_id": "HnTJs5YBwrYNjPmaFcri", | ||
| "_score": 1, | ||
| "_source": { | ||
| "@timestamp": "2025-05-09T06:41:24.775Z", | ||
| "document": { | ||
| "index": "my-datastream-ingest", | ||
| "source": { | ||
| "@timestamp": "2025-04-21T00:00:00Z", | ||
| "counter_name": "test" | ||
| } | ||
| }, | ||
| "error": { | ||
| "type": "illegal_argument_exception", | ||
| "message": "field [counter] not present as part of path [counter]", | ||
| "stack_trace": "j.l.IllegalArgumentException: field [counter] not present as part of path [counter] at o.e.i.IngestDocument.getFieldValue(IngestDocument.java: 202 at o.e.i.c.SetProcessor.execute(SetProcessor.java: 86) 14 more", | ||
| "pipeline_trace": [ | ||
| "complicated-processor" | ||
| ], | ||
| "pipeline": "complicated-processor", | ||
| "processor_type": "set", | ||
| "processor_tag": "copy to new counter again" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
| Which produces the following response: | ||
| ```console | ||
| { | ||
| "docs": [ | ||
| { | ||
| "doc": { | ||
| "_index": "my-datastream-ingest", | ||
| "_version": "-3", | ||
| "_id": "HnTJs5YBwrYNjPmaFcri", | ||
| "_source": { | ||
| "@timestamp": "2025-04-21T00:00:00Z", | ||
| "counter_name": "test" | ||
| }, | ||
| "_ingest": { | ||
| "timestamp": "2025-08-25T23:33:19.183295Z" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| Documents which do not match the failure store document format result in errors: | ||
| ```console | ||
| POST _ingest/pipeline/_simulate | ||
|
|
||
| { | ||
| "pipeline": { | ||
| "processors": [ | ||
| { | ||
| "remediate": {} | ||
| } | ||
| ] | ||
| }, | ||
| "docs": [ | ||
| { | ||
| "_index": ".fs-my-datastream-ingest-2025.05.09-000001", | ||
| "_id": "HnTJs5YBwrYNjPmaFcri", | ||
| "_score": 1, | ||
| "_source": { | ||
| "@timestamp": "2025-05-09T06:41:24.775Z", | ||
| "error": { | ||
| "type": "illegal_argument_exception", | ||
| "message": "field [counter] not present as part of path [counter]", | ||
| "stack_trace": "j.l.IllegalArgumentException: field [counter] not present as part of path [counter] at o.e.i.IngestDocument.getFieldValue(IngestDocument.java: 202 at o.e.i.c.SetProcessor.execute(SetProcessor.java: 86) 14 more", | ||
| "pipeline_trace": [ | ||
| "complicated-processor" | ||
| ], | ||
| "pipeline": "complicated-processor", | ||
| "processor_type": "set", | ||
| "processor_tag": "copy to new counter again" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
| Which produces the following response: | ||
| ```console | ||
| { | ||
| "docs": [ | ||
| { | ||
| "error": { | ||
| "root_cause": [ | ||
| { | ||
| "type": "illegal_argument_exception", | ||
| "reason": "field [document] not present as part of path [document]" | ||
| } | ||
| ], | ||
| "type": "illegal_argument_exception", | ||
| "reason": "field [document] not present as part of path [document]" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` |
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
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
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
90 changes: 90 additions & 0 deletions
90
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RemediateProcessor.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,90 @@ | ||
| /* | ||
| * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.ingest.common; | ||
|
|
||
| import org.elasticsearch.cluster.metadata.ProjectId; | ||
| import org.elasticsearch.ingest.AbstractProcessor; | ||
| import org.elasticsearch.ingest.IngestDocument; | ||
| import org.elasticsearch.ingest.Processor; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Processor that remediates a failure document by: | ||
| * - Copying the original index (and routing if present) from ctx.document.* into the document metadata | ||
| * - Extracting the original source from ctx.document.source | ||
| * - Removing the "error" and "document" fields from the root | ||
| * - Restoring the original source fields at the root of the document | ||
| */ | ||
| public final class RemediateProcessor extends AbstractProcessor { | ||
|
|
||
| public static final String TYPE = "remediate"; | ||
|
|
||
| RemediateProcessor(String tag, String description) { | ||
| super(tag, description); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public IngestDocument execute(IngestDocument document) throws Exception { | ||
| // Get the nested 'document' field, which holds the original document and metadata. | ||
| if (document.hasField("document") == false) { | ||
| throw new IllegalArgumentException("field [document] not present as part of path [document]"); | ||
| } | ||
| Map<String, Object> failedDocument = (Map<String, Object>) document.getFieldValue("document", Map.class); | ||
|
|
||
| // Copy the original index and routing back to the document's metadata. | ||
| String originalIndex = (String) failedDocument.get("index"); | ||
| if (originalIndex != null) { | ||
| document.setFieldValue("_index", originalIndex); | ||
| } | ||
|
|
||
| String originalRouting = (String) failedDocument.get("routing"); | ||
| if (originalRouting != null) { | ||
| document.setFieldValue("_routing", originalRouting); | ||
| } | ||
|
|
||
| // Get the original document's source. | ||
| Map<String, Object> originalSource = (Map<String, Object>) failedDocument.get("source"); | ||
| if (originalSource == null) { | ||
| throw new IllegalArgumentException("field [source] not present as part of path [document.source]"); | ||
| } | ||
|
|
||
| // Remove the 'error' and 'document' fields from the top-level document. | ||
seanzatzdev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| document.removeField("error"); | ||
| document.removeField("document"); | ||
|
|
||
| // Extract all fields from the original source back to the root of the document. | ||
| for (Map.Entry<String, Object> entry : originalSource.entrySet()) { | ||
| document.setFieldValue(entry.getKey(), entry.getValue()); | ||
| } | ||
seanzatzdev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Return the modified document. | ||
| return document; | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return TYPE; | ||
| } | ||
|
|
||
| public static final class Factory implements Processor.Factory { | ||
| @Override | ||
| public RemediateProcessor create( | ||
| Map<String, Processor.Factory> registry, | ||
| String processorTag, | ||
| String description, | ||
| Map<String, Object> config, | ||
| ProjectId projectId | ||
| ) throws Exception { | ||
| return new RemediateProcessor(processorTag, description); | ||
| } | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
...-common/src/test/java/org/elasticsearch/ingest/common/RemediateProcessorFactoryTests.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,51 @@ | ||
| /* | ||
| * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.ingest.common; | ||
|
|
||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.junit.Before; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class RemediateProcessorFactoryTests extends ESTestCase { | ||
|
|
||
| private RemediateProcessor.Factory factory; | ||
|
|
||
| @Before | ||
| public void init() { | ||
| factory = new RemediateProcessor.Factory(); | ||
| } | ||
|
|
||
| public void testCreate() throws Exception { | ||
| RemediateProcessor.Factory factory = new RemediateProcessor.Factory(); | ||
| Map<String, Object> config = new HashMap<>(); | ||
|
|
||
| String processorTag = randomAlphaOfLength(10); | ||
| RemediateProcessor processor = factory.create(null, processorTag, null, config, null); | ||
|
|
||
| assertThat(processor.getTag(), equalTo(processorTag)); | ||
| } | ||
|
|
||
| public void testCreateWithDescription() throws Exception { | ||
| RemediateProcessor.Factory factory = new RemediateProcessor.Factory(); | ||
| Map<String, Object> config = new HashMap<>(); | ||
|
|
||
| String processorTag = randomAlphaOfLength(10); | ||
| String description = randomAlphaOfLength(20); | ||
| RemediateProcessor processor = factory.create(null, processorTag, description, config, null); | ||
|
|
||
| assertThat(processor.getTag(), equalTo(processorTag)); | ||
| assertThat(processor.getDescription(), equalTo(description)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.