|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.enrich; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.UncheckedIOException; |
| 10 | + |
| 11 | +import org.elasticsearch.Version; |
| 12 | +import org.elasticsearch.action.ActionListener; |
| 13 | +import org.elasticsearch.action.ingest.PutPipelineRequest; |
| 14 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 15 | +import org.elasticsearch.client.Client; |
| 16 | +import org.elasticsearch.cluster.ClusterState; |
| 17 | +import org.elasticsearch.common.bytes.BytesReference; |
| 18 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 19 | +import org.elasticsearch.common.xcontent.XContentType; |
| 20 | +import org.elasticsearch.ingest.IngestMetadata; |
| 21 | +import org.elasticsearch.ingest.PipelineConfiguration; |
| 22 | + |
| 23 | +/** |
| 24 | + * Manages the definitions and lifecycle of the ingest pipeline used by the reindex operation within the Enrich Policy execution. |
| 25 | + */ |
| 26 | +public class EnrichPolicyReindexPipeline { |
| 27 | + |
| 28 | + /** |
| 29 | + * The current version of the pipeline definition. Used in the pipeline's name to differentiate from breaking changes |
| 30 | + * (separate from product version). |
| 31 | + */ |
| 32 | + static final String CURRENT_PIPELINE_VERSION_NAME = "7"; |
| 33 | + |
| 34 | + /** |
| 35 | + * The last version of the distribution that updated the pipelines definition. |
| 36 | + * TODO: This should be the version of ES that Enrich first ships in, which likely doesn't exist yet. |
| 37 | + */ |
| 38 | + static final int ENRICH_PIPELINE_LAST_UPDATED_VERSION = Version.V_7_4_0.id; |
| 39 | + |
| 40 | + static String pipelineName() { |
| 41 | + return "enrich-policy-reindex-" + CURRENT_PIPELINE_VERSION_NAME; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Checks if the current version of the pipeline definition is installed in the cluster |
| 46 | + * @param clusterState The cluster state to check |
| 47 | + * @return true if a pipeline exists that is compatible with this version of Enrich, false otherwise |
| 48 | + */ |
| 49 | + static boolean exists(ClusterState clusterState) { |
| 50 | + final IngestMetadata ingestMetadata = clusterState.getMetaData().custom(IngestMetadata.TYPE); |
| 51 | + // we ensure that we both have the pipeline and its version represents the current (or later) version |
| 52 | + if (ingestMetadata != null) { |
| 53 | + final PipelineConfiguration pipeline = ingestMetadata.getPipelines().get(pipelineName()); |
| 54 | + if (pipeline != null) { |
| 55 | + Object version = pipeline.getConfigAsMap().get("version"); |
| 56 | + return version instanceof Number && ((Number) version).intValue() >= ENRICH_PIPELINE_LAST_UPDATED_VERSION; |
| 57 | + } |
| 58 | + } |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a pipeline with the current version's pipeline definition |
| 64 | + * @param client Client used to execute put pipeline |
| 65 | + * @param listener Callback used after pipeline has been created |
| 66 | + */ |
| 67 | + public static void create(Client client, ActionListener<AcknowledgedResponse> listener) { |
| 68 | + final BytesReference pipeline = BytesReference.bytes(currentEnrichPipelineDefinition(XContentType.JSON)); |
| 69 | + final PutPipelineRequest request = new PutPipelineRequest(pipelineName(), pipeline, XContentType.JSON); |
| 70 | + client.admin().cluster().putPipeline(request, listener); |
| 71 | + } |
| 72 | + |
| 73 | + private static XContentBuilder currentEnrichPipelineDefinition(XContentType xContentType) { |
| 74 | + try { |
| 75 | + return XContentBuilder.builder(xContentType.xContent()) |
| 76 | + .startObject() |
| 77 | + .field("description", "This pipeline sanitizes documents that will be stored in enrich indices for ingest lookup " + |
| 78 | + "purposes. It is an internal pipeline and should not be modified.") |
| 79 | + .field("version", ENRICH_PIPELINE_LAST_UPDATED_VERSION) |
| 80 | + .startArray("processors") |
| 81 | + .startObject() |
| 82 | + // remove the id from the document so that documents from multiple indices will always be unique. |
| 83 | + .startObject("remove") |
| 84 | + .field("field", "_id") |
| 85 | + .endObject() |
| 86 | + .endObject() |
| 87 | + .endArray() |
| 88 | + .endObject(); |
| 89 | + } catch (final IOException e) { |
| 90 | + throw new UncheckedIOException("Failed to create pipeline for enrich document sanitization", e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments