| 
 | 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 | + * 2.0; you may not use this file except in compliance with the Elastic License  | 
 | 5 | + * 2.0.  | 
 | 6 | + */  | 
 | 7 | + | 
 | 8 | +package org.elasticsearch.xpack.enrich;  | 
 | 9 | + | 
 | 10 | +import org.elasticsearch.action.index.IndexRequest;  | 
 | 11 | +import org.elasticsearch.action.ingest.SimulateDocumentBaseResult;  | 
 | 12 | +import org.elasticsearch.action.ingest.SimulatePipelineRequest;  | 
 | 13 | +import org.elasticsearch.action.support.WriteRequest;  | 
 | 14 | +import org.elasticsearch.cluster.service.ClusterService;  | 
 | 15 | +import org.elasticsearch.common.bytes.BytesArray;  | 
 | 16 | +import org.elasticsearch.common.bytes.BytesReference;  | 
 | 17 | +import org.elasticsearch.common.bytes.ReleasableBytesReference;  | 
 | 18 | +import org.elasticsearch.common.settings.Settings;  | 
 | 19 | +import org.elasticsearch.core.Strings;  | 
 | 20 | +import org.elasticsearch.ingest.common.IngestCommonPlugin;  | 
 | 21 | +import org.elasticsearch.plugins.Plugin;  | 
 | 22 | +import org.elasticsearch.reindex.ReindexPlugin;  | 
 | 23 | +import org.elasticsearch.test.ESSingleNodeTestCase;  | 
 | 24 | +import org.elasticsearch.xcontent.XContentType;  | 
 | 25 | +import org.elasticsearch.xpack.core.XPackSettings;  | 
 | 26 | +import org.elasticsearch.xpack.core.enrich.EnrichPolicy;  | 
 | 27 | +import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;  | 
 | 28 | +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;  | 
 | 29 | + | 
 | 30 | +import java.util.Collection;  | 
 | 31 | +import java.util.List;  | 
 | 32 | +import java.util.concurrent.atomic.AtomicBoolean;  | 
 | 33 | + | 
 | 34 | +import static org.elasticsearch.xpack.enrich.AbstractEnrichTestCase.createSourceIndices;  | 
 | 35 | +import static org.hamcrest.Matchers.equalTo;  | 
 | 36 | +import static org.hamcrest.Matchers.nullValue;  | 
 | 37 | + | 
 | 38 | +public class EnrichPolicyChangeIT extends ESSingleNodeTestCase {  | 
 | 39 | + | 
 | 40 | +    @Override  | 
 | 41 | +    protected Collection<Class<? extends Plugin>> getPlugins() {  | 
 | 42 | +        return List.of(LocalStateEnrich.class, ReindexPlugin.class, IngestCommonPlugin.class);  | 
 | 43 | +    }  | 
 | 44 | + | 
 | 45 | +    @Override  | 
 | 46 | +    protected Settings nodeSettings() {  | 
 | 47 | +        return Settings.builder()  | 
 | 48 | +            // TODO Change this to run with security enabled  | 
 | 49 | +            // https://github.com/elastic/elasticsearch/issues/75940  | 
 | 50 | +            .put(XPackSettings.SECURITY_ENABLED.getKey(), false)  | 
 | 51 | +            .build();  | 
 | 52 | +    }  | 
 | 53 | + | 
 | 54 | +    private final String policyName = "device-enrich-policy";  | 
 | 55 | +    private final String sourceIndexName = "devices-idx";  | 
 | 56 | + | 
 | 57 | +    public void testEnrichCacheValuesCannotBeCorrupted() throws Exception {  | 
 | 58 | +        // create and store the enrich policy  | 
 | 59 | +        final EnrichPolicy enrichPolicy = new EnrichPolicy(  | 
 | 60 | +            EnrichPolicy.MATCH_TYPE,  | 
 | 61 | +            null,  | 
 | 62 | +            List.of(sourceIndexName),  | 
 | 63 | +            "host.ip",  | 
 | 64 | +            List.of("device.name", "host.ip")  | 
 | 65 | +        );  | 
 | 66 | + | 
 | 67 | +        // create the source index  | 
 | 68 | +        createSourceIndices(client(), enrichPolicy);  | 
 | 69 | + | 
 | 70 | +        // add a single document to the enrich index  | 
 | 71 | +        setEnrichDeviceName("some.device." + randomAlphaOfLength(10));  | 
 | 72 | + | 
 | 73 | +        // store the enrich policy  | 
 | 74 | +        var putPolicyRequest = new PutEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, policyName, enrichPolicy);  | 
 | 75 | +        client().execute(PutEnrichPolicyAction.INSTANCE, putPolicyRequest).actionGet();  | 
 | 76 | + | 
 | 77 | +        // execute the policy once  | 
 | 78 | +        executeEnrichPolicy();  | 
 | 79 | + | 
 | 80 | +        // add a low priority cluster state applier to increase the odds of a race occurring between  | 
 | 81 | +        // the cluster state *appliers* having been run (this adjusts the enrich index pointer) and the  | 
 | 82 | +        // cluster state *listeners* having been run (which adjusts the alias and therefore the search results)  | 
 | 83 | +        final var clusterService = node().injector().getInstance(ClusterService.class);  | 
 | 84 | +        clusterService.addLowPriorityApplier((event) -> safeSleep(10));  | 
 | 85 | + | 
 | 86 | +        // kick off some threads that just bang on _simulate in the background  | 
 | 87 | +        final var finished = new AtomicBoolean(false);  | 
 | 88 | +        for (int i = 0; i < 5; i++) {  | 
 | 89 | +            new Thread(() -> {  | 
 | 90 | +                while (finished.get() == false) {  | 
 | 91 | +                    simulatePipeline();  | 
 | 92 | +                }  | 
 | 93 | +            }).start();  | 
 | 94 | +        }  | 
 | 95 | + | 
 | 96 | +        try {  | 
 | 97 | +            for (int i = 0; i < randomIntBetween(10, 100); i++) {  | 
 | 98 | +                final String deviceName = "some.device." + randomAlphaOfLength(10);  | 
 | 99 | + | 
 | 100 | +                // add a single document to the enrich index  | 
 | 101 | +                setEnrichDeviceName(deviceName);  | 
 | 102 | + | 
 | 103 | +                // execute the policy  | 
 | 104 | +                executeEnrichPolicy();  | 
 | 105 | + | 
 | 106 | +                // simulate the pipeline and confirm that we see the expected result  | 
 | 107 | +                assertBusy(() -> {  | 
 | 108 | +                    SimulateDocumentBaseResult result = simulatePipeline();  | 
 | 109 | +                    assertThat(result.getFailure(), nullValue());  | 
 | 110 | +                    assertThat(result.getIngestDocument().getFieldValue("device.name", String.class), equalTo(deviceName));  | 
 | 111 | +                });  | 
 | 112 | +            }  | 
 | 113 | +        } finally {  | 
 | 114 | +            // we're finished, so those threads can all quit now  | 
 | 115 | +            finished.set(true);  | 
 | 116 | +        }  | 
 | 117 | +    }  | 
 | 118 | + | 
 | 119 | +    private SimulateDocumentBaseResult simulatePipeline() {  | 
 | 120 | +        final SimulatePipelineRequest simulatePipelineRequest = jsonSimulatePipelineRequest("""  | 
 | 121 | +            {  | 
 | 122 | +              "pipeline": {  | 
 | 123 | +                "processors": [  | 
 | 124 | +                  {  | 
 | 125 | +                    "enrich": {  | 
 | 126 | +                      "policy_name": "device-enrich-policy",  | 
 | 127 | +                      "field": "host.ip",  | 
 | 128 | +                      "target_field": "_tmp.device"  | 
 | 129 | +                    }  | 
 | 130 | +                  },  | 
 | 131 | +                  {  | 
 | 132 | +                    "rename" : {  | 
 | 133 | +                      "field" : "_tmp.device.device.name",  | 
 | 134 | +                      "target_field" : "device.name"  | 
 | 135 | +                    }  | 
 | 136 | +                  }  | 
 | 137 | +                ]  | 
 | 138 | +              },  | 
 | 139 | +              "docs": [  | 
 | 140 | +                {  | 
 | 141 | +                  "_source": {  | 
 | 142 | +                    "host": {  | 
 | 143 | +                      "ip": "10.151.80.8"  | 
 | 144 | +                    }  | 
 | 145 | +                  }  | 
 | 146 | +                }  | 
 | 147 | +              ]  | 
 | 148 | +            }  | 
 | 149 | +            """);  | 
 | 150 | +        final var response = clusterAdmin().simulatePipeline(simulatePipelineRequest).actionGet();  | 
 | 151 | +        return (SimulateDocumentBaseResult) response.getResults().get(0);  | 
 | 152 | +    }  | 
 | 153 | + | 
 | 154 | +    private void setEnrichDeviceName(final String deviceName) {  | 
 | 155 | +        final IndexRequest indexRequest = new IndexRequest(sourceIndexName);  | 
 | 156 | +        indexRequest.id("1"); // there's only one document, and we keep overwriting it  | 
 | 157 | +        indexRequest.source(Strings.format("""  | 
 | 158 | +            {  | 
 | 159 | +              "host": {  | 
 | 160 | +                "ip": "10.151.80.8"  | 
 | 161 | +              },  | 
 | 162 | +              "device": {  | 
 | 163 | +                "name": "%s"  | 
 | 164 | +              }  | 
 | 165 | +            }  | 
 | 166 | +            """, deviceName), XContentType.JSON);  | 
 | 167 | +        indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);  | 
 | 168 | +        client().index(indexRequest).actionGet();  | 
 | 169 | +    }  | 
 | 170 | + | 
 | 171 | +    private void executeEnrichPolicy() {  | 
 | 172 | +        final ExecuteEnrichPolicyAction.Request executePolicyRequest = new ExecuteEnrichPolicyAction.Request(  | 
 | 173 | +            TEST_REQUEST_TIMEOUT,  | 
 | 174 | +            policyName  | 
 | 175 | +        );  | 
 | 176 | +        client().execute(ExecuteEnrichPolicyAction.INSTANCE, executePolicyRequest).actionGet();  | 
 | 177 | +    }  | 
 | 178 | + | 
 | 179 | +    private static SimulatePipelineRequest jsonSimulatePipelineRequest(String jsonString) {  | 
 | 180 | +        return jsonSimulatePipelineRequest(new BytesArray(jsonString));  | 
 | 181 | +    }  | 
 | 182 | + | 
 | 183 | +    private static SimulatePipelineRequest jsonSimulatePipelineRequest(BytesReference jsonBytes) {  | 
 | 184 | +        return new SimulatePipelineRequest(ReleasableBytesReference.wrap(jsonBytes), XContentType.JSON);  | 
 | 185 | +    }  | 
 | 186 | + | 
 | 187 | +}  | 
0 commit comments