|
16 | 16 | import org.elasticsearch.action.admin.indices.create.CreateIndexAction;
|
17 | 17 | import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
18 | 18 | import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
| 19 | +import org.elasticsearch.action.admin.indices.delete.DeleteIndexAction; |
| 20 | +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; |
19 | 21 | import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeAction;
|
20 | 22 | import org.elasticsearch.action.admin.indices.get.GetIndexAction;
|
21 | 23 | import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
|
@@ -2090,6 +2092,88 @@ protected <Request extends ActionRequest, Response extends ActionResponse> void
|
2090 | 2092 | assertThat(exception.get().getMessage(), containsString("cancelled policy execution [test1], status ["));
|
2091 | 2093 | }
|
2092 | 2094 |
|
| 2095 | + public void testRunnerValidatesIndexIntegrity() throws Exception { |
| 2096 | + final String sourceIndex = "source-index"; |
| 2097 | + IndexResponse indexRequest = client().index( |
| 2098 | + new IndexRequest().index(sourceIndex) |
| 2099 | + .id("id") |
| 2100 | + .source( |
| 2101 | + "{" |
| 2102 | + + "\"field1\":\"value1\"," |
| 2103 | + + "\"field2\":2," |
| 2104 | + + "\"field3\":\"ignored\"," |
| 2105 | + + "\"field4\":\"ignored\"," |
| 2106 | + + "\"field5\":\"value5\"" |
| 2107 | + + "}", |
| 2108 | + XContentType.JSON |
| 2109 | + ) |
| 2110 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 2111 | + ).actionGet(); |
| 2112 | + assertEquals(RestStatus.CREATED, indexRequest.status()); |
| 2113 | + |
| 2114 | + SearchResponse sourceSearchResponse = client().search( |
| 2115 | + new SearchRequest(sourceIndex).source(SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery())) |
| 2116 | + ).actionGet(); |
| 2117 | + assertThat(sourceSearchResponse.getHits().getTotalHits().value, equalTo(1L)); |
| 2118 | + Map<String, Object> sourceDocMap = sourceSearchResponse.getHits().getAt(0).getSourceAsMap(); |
| 2119 | + assertNotNull(sourceDocMap); |
| 2120 | + assertThat(sourceDocMap.get("field1"), is(equalTo("value1"))); |
| 2121 | + assertThat(sourceDocMap.get("field2"), is(equalTo(2))); |
| 2122 | + assertThat(sourceDocMap.get("field3"), is(equalTo("ignored"))); |
| 2123 | + assertThat(sourceDocMap.get("field4"), is(equalTo("ignored"))); |
| 2124 | + assertThat(sourceDocMap.get("field5"), is(equalTo("value5"))); |
| 2125 | + |
| 2126 | + List<String> enrichFields = Arrays.asList("field2", "field5"); |
| 2127 | + EnrichPolicy policy = new EnrichPolicy(EnrichPolicy.MATCH_TYPE, null, singletonList(sourceIndex), "field1", enrichFields); |
| 2128 | + String policyName = "test1"; |
| 2129 | + |
| 2130 | + final long createTime = randomNonNegativeLong(); |
| 2131 | + String createdEnrichIndex = ".enrich-test1-" + createTime; |
| 2132 | + final AtomicReference<Exception> exception = new AtomicReference<>(); |
| 2133 | + final CountDownLatch latch = new CountDownLatch(1); |
| 2134 | + ActionListener<ExecuteEnrichPolicyStatus> listener = createTestListener(latch, exception::set); |
| 2135 | + |
| 2136 | + // Wrap the client so that when we receive the reindex action, we delete the index then resume operation. This mimics an invalid |
| 2137 | + // state for the resulting index. |
| 2138 | + Client client = new FilterClient(client()) { |
| 2139 | + @Override |
| 2140 | + protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute( |
| 2141 | + ActionType<Response> action, |
| 2142 | + Request request, |
| 2143 | + ActionListener<Response> listener |
| 2144 | + ) { |
| 2145 | + if (action.equals(EnrichReindexAction.INSTANCE)) { |
| 2146 | + super.doExecute( |
| 2147 | + DeleteIndexAction.INSTANCE, |
| 2148 | + new DeleteIndexRequest(createdEnrichIndex), |
| 2149 | + listener.delegateFailure((delegate, response) -> { |
| 2150 | + if (response.isAcknowledged()) { |
| 2151 | + super.doExecute(action, request, delegate); |
| 2152 | + } else { |
| 2153 | + fail("Enrich index should have been deleted but was not"); |
| 2154 | + delegate.onFailure(new ElasticsearchException("Could not delete enrich index - cleaning up")); |
| 2155 | + } |
| 2156 | + }) |
| 2157 | + ); |
| 2158 | + } else { |
| 2159 | + super.doExecute(action, request, listener); |
| 2160 | + } |
| 2161 | + } |
| 2162 | + }; |
| 2163 | + EnrichPolicyRunner enrichPolicyRunner = createPolicyRunner(client, policyName, policy, listener, createdEnrichIndex); |
| 2164 | + |
| 2165 | + logger.info("Starting policy run"); |
| 2166 | + enrichPolicyRunner.run(); |
| 2167 | + latch.await(); |
| 2168 | + Exception runnerException = exception.get(); |
| 2169 | + if (runnerException == null) { |
| 2170 | + fail("Expected the runner to fail when the underlying index was deleted during policy execution!"); |
| 2171 | + } |
| 2172 | + assertThat(runnerException, is(instanceOf(ElasticsearchException.class))); |
| 2173 | + assertThat(runnerException.getMessage(), containsString("Could not verify enrich index")); |
| 2174 | + assertThat(runnerException.getMessage(), containsString("mapping meta field missing")); |
| 2175 | + } |
| 2176 | + |
2093 | 2177 | private EnrichPolicyRunner createPolicyRunner(
|
2094 | 2178 | String policyName,
|
2095 | 2179 | EnrichPolicy policy,
|
|
0 commit comments