|
18 | 18 | */ |
19 | 19 | package org.elasticsearch.indices.state; |
20 | 20 |
|
21 | | -import org.elasticsearch.ElasticsearchWrapperException; |
| 21 | +import org.elasticsearch.ExceptionsHelper; |
22 | 22 | import org.elasticsearch.action.ActionRequestValidationException; |
23 | | -import org.elasticsearch.action.bulk.BulkItemResponse; |
24 | | -import org.elasticsearch.action.bulk.BulkRequestBuilder; |
25 | | -import org.elasticsearch.action.bulk.BulkResponse; |
26 | | -import org.elasticsearch.action.index.IndexRequest; |
27 | | -import org.elasticsearch.action.index.IndexResponse; |
28 | 23 | import org.elasticsearch.action.support.ActiveShardCount; |
29 | 24 | import org.elasticsearch.action.support.IndicesOptions; |
30 | 25 | import org.elasticsearch.cluster.ClusterState; |
|
34 | 29 | import org.elasticsearch.cluster.routing.ShardRouting; |
35 | 30 | import org.elasticsearch.common.settings.Settings; |
36 | 31 | import org.elasticsearch.index.IndexNotFoundException; |
37 | | -import org.elasticsearch.index.engine.VersionConflictEngineException; |
38 | 32 | import org.elasticsearch.indices.IndexClosedException; |
39 | | -import org.elasticsearch.rest.RestStatus; |
| 33 | +import org.elasticsearch.test.BackgroundIndexer; |
40 | 34 | import org.elasticsearch.test.ESIntegTestCase; |
41 | 35 |
|
42 | 36 | import java.util.ArrayList; |
43 | 37 | import java.util.List; |
44 | 38 | import java.util.Locale; |
45 | 39 | import java.util.concurrent.CountDownLatch; |
46 | | -import java.util.concurrent.atomic.AtomicBoolean; |
47 | | -import java.util.concurrent.atomic.AtomicInteger; |
48 | 40 | import java.util.stream.IntStream; |
49 | 41 |
|
50 | 42 | import static java.util.stream.Collectors.toList; |
@@ -102,7 +94,7 @@ public void testCloseIndex() throws Exception { |
102 | 94 | assertIndexIsClosed(indexName); |
103 | 95 |
|
104 | 96 | assertAcked(client().admin().indices().prepareOpen(indexName)); |
105 | | - assertHitCount(client().prepareSearch(indexName).setSize(0).setFetchSource(false).get(), nbDocs); |
| 97 | + assertHitCount(client().prepareSearch(indexName).setSize(0).get(), nbDocs); |
106 | 98 | } |
107 | 99 |
|
108 | 100 | public void testCloseAlreadyClosedIndex() throws Exception { |
@@ -170,90 +162,26 @@ public void testCloseWhileIndexingDocuments() throws Exception { |
170 | 162 | final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
171 | 163 | createIndex(indexName); |
172 | 164 |
|
173 | | - final int nbDocs = randomIntBetween(10, 50); |
174 | | - final AtomicInteger acknowledgedDocs = new AtomicInteger(nbDocs); |
175 | | - indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, nbDocs) |
176 | | - .mapToObj(i -> client().prepareIndex(indexName, "_doc", String.valueOf(i)).setSource("num", i)).collect(toList())); |
177 | | - |
178 | | - final CountDownLatch startIndexing = new CountDownLatch(1); |
179 | | - final CountDownLatch startClosing = new CountDownLatch(1); |
180 | | - final AtomicBoolean runIndexing = new AtomicBoolean(true); |
181 | | - |
182 | | - final Thread[] threads = new Thread[randomIntBetween(2, 5)]; |
183 | | - for (int i = 0; i < threads.length; i++) { |
184 | | - threads[i] = new Thread(() -> { |
185 | | - try { |
186 | | - startIndexing.await(); |
187 | | - } catch (InterruptedException e) { |
188 | | - throw new AssertionError(e); |
189 | | - } |
190 | | - while (runIndexing.get()) { |
191 | | - switch (randomIntBetween(0, 2)) { |
192 | | - case 0: // Single doc indexing |
193 | | - { |
194 | | - try { |
195 | | - IndexResponse response = client().prepareIndex(indexName, "_doc").setSource("num", randomInt()).get(); |
196 | | - if (response.status() == RestStatus.CREATED) { |
197 | | - acknowledgedDocs.incrementAndGet(); |
198 | | - } |
199 | | - } catch (Exception e) { |
200 | | - assertException(e, indexName); |
201 | | - } |
202 | | - break; |
203 | | - } |
204 | | - case 1: // Bulk docs indexing |
205 | | - { |
206 | | - BulkRequestBuilder request = client().prepareBulk(indexName, "_doc"); |
207 | | - for (int j = 0; j < randomIntBetween(1, 10); j++) { |
208 | | - request.add(new IndexRequest().source("num", randomInt())); |
209 | | - } |
210 | | - |
211 | | - BulkResponse response = request.get(); |
212 | | - startClosing.countDown(); |
213 | | - for (BulkItemResponse itemResponse : response) { |
214 | | - if (itemResponse.isFailed() == false) { |
215 | | - acknowledgedDocs.incrementAndGet(); |
216 | | - } else { |
217 | | - assertException(itemResponse.getFailure().getCause(), indexName); |
218 | | - } |
219 | | - } |
220 | | - break; |
221 | | - } |
222 | | - case 2: // Single doc update |
223 | | - { |
224 | | - try { |
225 | | - int docId = randomIntBetween(0, nbDocs - 1); |
226 | | - client().prepareUpdate(indexName, "_doc", String.valueOf(docId)).setDoc("num", randomInt()).get(); |
227 | | - } catch (VersionConflictEngineException e) { |
228 | | - // it's ok to ignore |
229 | | - } catch (Exception e) { |
230 | | - assertException(e, indexName); |
231 | | - } |
232 | | - break; |
233 | | - } |
234 | | - default: |
235 | | - throw new AssertionError("Illegal randomisation branch"); |
236 | | - } |
237 | | - } |
238 | | - }); |
239 | | - threads[i].start(); |
240 | | - } |
| 165 | + int nbDocs = 0; |
| 166 | + try (BackgroundIndexer indexer = new BackgroundIndexer(indexName, "_doc", client())) { |
| 167 | + indexer.setAssertNoFailuresOnStop(false); |
241 | 168 |
|
242 | | - try { |
243 | | - startIndexing.countDown(); |
244 | | - startClosing.await(); |
| 169 | + waitForDocs(randomIntBetween(10, 50), indexer); |
245 | 170 | assertAcked(client().admin().indices().prepareClose(indexName)); |
246 | | - } finally { |
247 | | - runIndexing.set(false); |
248 | | - } |
| 171 | + indexer.stop(); |
| 172 | + nbDocs += indexer.totalIndexedDocs(); |
249 | 173 |
|
250 | | - for (Thread thread : threads) { |
251 | | - thread.join(); |
| 174 | + final Throwable[] failures = indexer.getFailures(); |
| 175 | + if (failures != null) { |
| 176 | + for (Throwable failure : failures) { |
| 177 | + assertException(failure, indexName); |
| 178 | + } |
| 179 | + } |
252 | 180 | } |
253 | 181 |
|
254 | 182 | assertIndexIsClosed(indexName); |
255 | 183 | assertAcked(client().admin().indices().prepareOpen(indexName)); |
256 | | - assertHitCount(client().prepareSearch(indexName).setSize(0).setFetchSource(false).get(), acknowledgedDocs.get()); |
| 184 | + assertHitCount(client().prepareSearch(indexName).setSize(0).get(), nbDocs); |
257 | 185 | } |
258 | 186 |
|
259 | 187 | public void testCloseWhileDeletingIndices() throws Exception { |
@@ -317,27 +245,29 @@ static void assertIndexIsClosed(final String indexName) { |
317 | 245 | assertThat(clusterState.blocks().hasIndexBlock(indexName, MetaDataIndexStateService.INDEX_CLOSED_BLOCK), is(true)); |
318 | 246 | } |
319 | 247 |
|
320 | | - static void assertException(final Exception exception, final String indexName) { |
321 | | - if (exception instanceof ElasticsearchWrapperException) { |
322 | | - if (exception.getCause() != null && exception.getCause() instanceof Exception) { |
323 | | - assertException((Exception) exception.getCause(), indexName); |
324 | | - return; |
325 | | - } |
326 | | - } |
327 | | - if (exception instanceof ClusterBlockException) { |
328 | | - ClusterBlockException clusterBlockException = (ClusterBlockException) exception; |
| 248 | + static void assertIndexIsOpened(final String indexName) { |
| 249 | + final ClusterState clusterState = client().admin().cluster().prepareState().get().getState(); |
| 250 | + assertThat(clusterState.metaData().indices().get(indexName).getState(), is(IndexMetaData.State.OPEN)); |
| 251 | + assertThat(clusterState.routingTable().index(indexName), notNullValue()); |
| 252 | + assertThat(clusterState.blocks().hasIndexBlock(indexName, MetaDataIndexStateService.INDEX_CLOSED_BLOCK), is(false)); |
| 253 | + } |
| 254 | + |
| 255 | + static void assertException(final Throwable throwable, final String indexName) { |
| 256 | + final Throwable t = ExceptionsHelper.unwrapCause(throwable); |
| 257 | + if (t instanceof ClusterBlockException) { |
| 258 | + ClusterBlockException clusterBlockException = (ClusterBlockException) t; |
329 | 259 | assertThat(clusterBlockException.blocks(), hasSize(1)); |
330 | 260 | assertThat(clusterBlockException.blocks(), hasItem(MetaDataIndexStateService.INDEX_CLOSED_BLOCK)); |
331 | | - } else if (exception instanceof IndexClosedException) { |
332 | | - IndexClosedException indexClosedException = (IndexClosedException) exception; |
| 261 | + } else if (t instanceof IndexClosedException) { |
| 262 | + IndexClosedException indexClosedException = (IndexClosedException) t; |
333 | 263 | assertThat(indexClosedException.getIndex(), notNullValue()); |
334 | 264 | assertThat(indexClosedException.getIndex().getName(), equalTo(indexName)); |
335 | | - } else if (exception instanceof IndexNotFoundException) { |
336 | | - IndexNotFoundException indexNotFoundException = (IndexNotFoundException) exception; |
| 265 | + } else if (t instanceof IndexNotFoundException) { |
| 266 | + IndexNotFoundException indexNotFoundException = (IndexNotFoundException) t; |
337 | 267 | assertThat(indexNotFoundException.getIndex(), notNullValue()); |
338 | 268 | assertThat(indexNotFoundException.getIndex().getName(), equalTo(indexName)); |
339 | 269 | } else { |
340 | | - fail("Unexpected exception: " + exception); |
| 270 | + fail("Unexpected exception: " + t); |
341 | 271 | } |
342 | 272 | } |
343 | 273 | } |
0 commit comments