Search in sources :

Example 16 with ShardOperationFailedException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.ShardOperationFailedException in project elasticsearch by elastic.

the class IndexStatsIT method testConcurrentIndexingAndStatsRequests.

/**
     * Test that we can safely concurrently index and get stats. This test was inspired by a serialization issue that arose due to a race
     * getting doc stats during heavy indexing. The race could lead to deleted docs being negative which would then be serialized as a
     * variable-length long. Since serialization of negative longs using a variable-length format was unsupported
     * ({@link org.elasticsearch.common.io.stream.StreamOutput#writeVLong(long)}), the stream would become corrupted. Here, we want to test
     * that we can continue to get stats while indexing.
     */
public void testConcurrentIndexingAndStatsRequests() throws BrokenBarrierException, InterruptedException, ExecutionException {
    final AtomicInteger idGenerator = new AtomicInteger();
    final int numberOfIndexingThreads = Runtime.getRuntime().availableProcessors();
    final int numberOfStatsThreads = 4 * numberOfIndexingThreads;
    final CyclicBarrier barrier = new CyclicBarrier(1 + numberOfIndexingThreads + numberOfStatsThreads);
    final AtomicBoolean stop = new AtomicBoolean();
    final List<Thread> threads = new ArrayList<>(numberOfIndexingThreads + numberOfIndexingThreads);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean failed = new AtomicBoolean();
    final AtomicReference<List<ShardOperationFailedException>> shardFailures = new AtomicReference<>(new CopyOnWriteArrayList<>());
    final AtomicReference<List<Exception>> executionFailures = new AtomicReference<>(new CopyOnWriteArrayList<>());
    // increasing the number of shards increases the number of chances any one stats request will hit a race
    final CreateIndexRequest createIndexRequest = new CreateIndexRequest("test", Settings.builder().put("index.number_of_shards", 10).build());
    client().admin().indices().create(createIndexRequest).get();
    // start threads that will index concurrently with stats requests
    for (int i = 0; i < numberOfIndexingThreads; i++) {
        final Thread thread = new Thread(() -> {
            try {
                barrier.await();
            } catch (final BrokenBarrierException | InterruptedException e) {
                failed.set(true);
                executionFailures.get().add(e);
                latch.countDown();
            }
            while (!stop.get()) {
                final String id = Integer.toString(idGenerator.incrementAndGet());
                final IndexResponse response = client().prepareIndex("test", "type", id).setSource("{}", XContentType.JSON).get();
                assertThat(response.getResult(), equalTo(DocWriteResponse.Result.CREATED));
            }
        });
        thread.setName("indexing-" + i);
        threads.add(thread);
        thread.start();
    }
    // start threads that will get stats concurrently with indexing
    for (int i = 0; i < numberOfStatsThreads; i++) {
        final Thread thread = new Thread(() -> {
            try {
                barrier.await();
            } catch (final BrokenBarrierException | InterruptedException e) {
                failed.set(true);
                executionFailures.get().add(e);
                latch.countDown();
            }
            final IndicesStatsRequest request = new IndicesStatsRequest();
            request.all();
            request.indices(new String[0]);
            while (!stop.get()) {
                try {
                    final IndicesStatsResponse response = client().admin().indices().stats(request).get();
                    if (response.getFailedShards() > 0) {
                        failed.set(true);
                        shardFailures.get().addAll(Arrays.asList(response.getShardFailures()));
                        latch.countDown();
                    }
                } catch (final ExecutionException | InterruptedException e) {
                    failed.set(true);
                    executionFailures.get().add(e);
                    latch.countDown();
                }
            }
        });
        thread.setName("stats-" + i);
        threads.add(thread);
        thread.start();
    }
    // release the hounds
    barrier.await();
    // wait for a failure, or for fifteen seconds to elapse
    latch.await(15, TimeUnit.SECONDS);
    // stop all threads and wait for them to complete
    stop.set(true);
    for (final Thread thread : threads) {
        thread.join();
    }
    assertThat(shardFailures.get(), emptyCollectionOf(ShardOperationFailedException.class));
    assertThat(executionFailures.get(), emptyCollectionOf(Exception.class));
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) ExecutionException(java.util.concurrent.ExecutionException) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexResponse(org.elasticsearch.action.index.IndexResponse) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesStatsRequest(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest)

Example 17 with ShardOperationFailedException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.ShardOperationFailedException in project crate by crate.

the class FailedShardsException method genMessage.

private static String genMessage(ShardOperationFailedException[] shardFailures) {
    StringBuilder sb;
    if (shardFailures.length == 1) {
        sb = new StringBuilder("query failed on shard ");
    } else {
        sb = new StringBuilder("query failed on shards ");
    }
    List<String> errors = new ArrayList<>(shardFailures.length);
    String table = null;
    for (ShardOperationFailedException shardFailure : shardFailures) {
        if (shardFailure == null) {
            continue;
        }
        errors.add(shardFailure.shardId() + " ( " + shardFailure.reason() + " )");
        table = shardFailure.index();
    }
    if (errors.isEmpty() && table == null) {
        return "query failed on unknown shard / table";
    }
    sb.append(Joiner.on(", ").join(errors));
    if (table != null) {
        sb.append(" of table ").append(table);
    }
    return sb.toString();
}
Also used : ArrayList(java.util.ArrayList) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException)

Example 18 with ShardOperationFailedException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.ShardOperationFailedException in project molgenis by molgenis.

the class ClientFacade method refreshIndexes.

private void refreshIndexes(List<Index> indexes) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Refreshing index(es) '{}' ...", toString(indexes));
    }
    String[] indexNames = toIndexNames(indexes);
    RefreshRequestBuilder refreshRequest = client.admin().indices().prepareRefresh(indexNames);
    RefreshResponse refreshResponse;
    try {
        refreshResponse = refreshRequest.get();
    } catch (ResourceNotFoundException e) {
        LOG.debug("", e);
        throw new UnknownIndexException(toIndexNames(indexes));
    } catch (ElasticsearchException e) {
        LOG.error("", e);
        throw new IndexException(format("Error refreshing index(es) '%s'.", toString(indexes)));
    }
    if (refreshResponse.getFailedShards() > 0) {
        LOG.error(stream(refreshResponse.getShardFailures()).map(ShardOperationFailedException::toString).collect(joining("\n")));
        throw new IndexException(format("Error refreshing index(es) '%s'.", toString(indexes)));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Refreshed index(es) '{}'", toString(indexes));
    }
}
Also used : RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) RefreshRequestBuilder(org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder)

Aggregations

ShardOperationFailedException (org.elasticsearch.action.ShardOperationFailedException)17 ArrayList (java.util.ArrayList)9 List (java.util.List)5 DefaultShardOperationFailedException (org.elasticsearch.action.support.DefaultShardOperationFailedException)4 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 FlushResponse (org.elasticsearch.action.admin.indices.flush.FlushResponse)2 StoreStatus.readStoreStatus (org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse.StoreStatus.readStoreStatus)2 BroadcastShardOperationFailedException (org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException)2 ImmutableOpenIntMap (org.elasticsearch.common.collect.ImmutableOpenIntMap)2 Maps (com.google.common.collect.Maps)1 Named (com.google.inject.name.Named)1 CrateUnitTest (io.crate.test.integration.CrateUnitTest)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1