Search in sources :

Example 1 with UnavailableShardsException

use of org.elasticsearch.action.UnavailableShardsException in project elasticsearch by elastic.

the class CreateIndexIT method testCreateAndDeleteIndexConcurrently.

public void testCreateAndDeleteIndexConcurrently() throws InterruptedException {
    createIndex("test");
    final AtomicInteger indexVersion = new AtomicInteger(0);
    final Object indexVersionLock = new Object();
    final CountDownLatch latch = new CountDownLatch(1);
    int numDocs = randomIntBetween(1, 10);
    for (int i = 0; i < numDocs; i++) {
        client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).get();
    }
    synchronized (indexVersionLock) {
        // not necessarily needed here but for completeness we lock here too
        indexVersion.incrementAndGet();
    }
    client().admin().indices().prepareDelete("test").execute(new // this happens async!!!
    ActionListener<DeleteIndexResponse>() {

        @Override
        public void onResponse(DeleteIndexResponse deleteIndexResponse) {
            Thread thread = new Thread() {

                @Override
                public void run() {
                    try {
                        // recreate that index
                        client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).get();
                        synchronized (indexVersionLock) {
                            // we sync here since we have to ensure that all indexing operations below for a given ID are done before
                            // we increment the index version otherwise a doc that is in-flight could make it into an index that it
                            // was supposed to be deleted for and our assertion fail...
                            indexVersion.incrementAndGet();
                        }
                        // from here on all docs with index_version == 0|1 must be gone!!!! only 2 are ok;
                        assertAcked(client().admin().indices().prepareDelete("test").get());
                    } finally {
                        latch.countDown();
                    }
                }
            };
            thread.start();
        }

        @Override
        public void onFailure(Exception e) {
            throw new RuntimeException(e);
        }
    });
    numDocs = randomIntBetween(100, 200);
    for (int i = 0; i < numDocs; i++) {
        try {
            synchronized (indexVersionLock) {
                client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).setTimeout(TimeValue.timeValueSeconds(10)).get();
            }
        } catch (IndexNotFoundException inf) {
        // fine
        } catch (UnavailableShardsException ex) {
            assertEquals(ex.getCause().getClass(), IndexNotFoundException.class);
        // fine we run into a delete index while retrying
        }
    }
    latch.await();
    refresh();
    // we only really assert that we never reuse segments of old indices or anything like this here and that nothing fails with
    // crazy exceptions
    SearchResponse expected = client().prepareSearch("test").setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(new RangeQueryBuilder("index_version").from(indexVersion.get(), true)).get();
    SearchResponse all = client().prepareSearch("test").setIndicesOptions(IndicesOptions.lenientExpandOpen()).get();
    assertEquals(expected + " vs. " + all, expected.getHits().getTotalHits(), all.getHits().getTotalHits());
    logger.info("total: {}", expected.getHits().getTotalHits());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) SearchResponse(org.elasticsearch.action.search.SearchResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 2 with UnavailableShardsException

use of org.elasticsearch.action.UnavailableShardsException in project elasticsearch by elastic.

the class BroadcastReplicationTests method testNotStartedPrimary.

public void testNotStartedPrimary() throws InterruptedException, ExecutionException, IOException {
    final String index = "test";
    setState(clusterService, state(index, randomBoolean(), randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.UNASSIGNED, ShardRoutingState.UNASSIGNED));
    logger.debug("--> using initial state:\n{}", clusterService.state());
    Future<BroadcastResponse> response = (broadcastReplicationAction.execute(new DummyBroadcastRequest().indices(index)));
    for (Tuple<ShardId, ActionListener<ReplicationResponse>> shardRequests : broadcastReplicationAction.capturedShardRequests) {
        if (randomBoolean()) {
            shardRequests.v2().onFailure(new NoShardAvailableActionException(shardRequests.v1()));
        } else {
            shardRequests.v2().onFailure(new UnavailableShardsException(shardRequests.v1(), "test exception"));
        }
    }
    response.get();
    logger.info("total shards: {}, ", response.get().getTotalShards());
    // we expect no failures here because UnavailableShardsException does not count as failed
    assertBroadcastResponse(2, 0, 0, response.get(), null);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ActionListener(org.elasticsearch.action.ActionListener) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) BroadcastResponse(org.elasticsearch.action.support.broadcast.BroadcastResponse)

Example 3 with UnavailableShardsException

use of org.elasticsearch.action.UnavailableShardsException in project elasticsearch by elastic.

the class WaitActiveShardCountIT method testReplicationWaitsForActiveShardCount.

public void testReplicationWaitsForActiveShardCount() throws Exception {
    CreateIndexResponse createIndexResponse = prepareCreate("test", 1, Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 2)).get();
    assertAcked(createIndexResponse);
    // indexing, by default, will work (waiting for one shard copy only)
    client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).execute().actionGet();
    try {
        client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(// wait for 2 active shard copies
        2).setTimeout(timeValueMillis(100)).execute().actionGet();
        fail("can't index, does not enough active shard copies");
    } catch (UnavailableShardsException e) {
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
        assertThat(e.getMessage(), startsWith("[test][0] Not enough active copies to meet shard count of [2] (have 1, needed 2). Timeout: [100ms], request:"));
    // but really, all is well
    }
    allowNodes("test", 2);
    ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForActiveShards(2).setWaitForYellowStatus().execute().actionGet();
    logger.info("Done Cluster Health, status {}", clusterHealth.getStatus());
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
    // this should work, since we now have two
    client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(2).setTimeout(timeValueSeconds(1)).execute().actionGet();
    try {
        client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(ActiveShardCount.ALL).setTimeout(timeValueMillis(100)).execute().actionGet();
        fail("can't index, not enough active shard copies");
    } catch (UnavailableShardsException e) {
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
        assertThat(e.getMessage(), startsWith("[test][0] Not enough active copies to meet shard count of [" + ActiveShardCount.ALL + "] (have 2, needed 3). Timeout: [100ms], request:"));
    // but really, all is well
    }
    allowNodes("test", 3);
    clusterHealth = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForActiveShards(3).setWaitForGreenStatus().execute().actionGet();
    logger.info("Done Cluster Health, status {}", clusterHealth.getStatus());
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    // this should work, since we now have all shards started
    client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(ActiveShardCount.ALL).setTimeout(timeValueSeconds(1)).execute().actionGet();
}
Also used : UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 4 with UnavailableShardsException

use of org.elasticsearch.action.UnavailableShardsException in project elasticsearch by elastic.

the class SimpleDataNodesIT method testDataNodes.

public void testDataNodes() throws Exception {
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build());
    client().admin().indices().create(createIndexRequest("test").waitForActiveShards(ActiveShardCount.NONE)).actionGet();
    try {
        client().index(Requests.indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON).timeout(timeValueSeconds(1))).actionGet();
        fail("no allocation should happen");
    } catch (UnavailableShardsException e) {
    // all is well
    }
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build());
    assertThat(client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("2").setLocal(true).execute().actionGet().isTimedOut(), equalTo(false));
    // still no shard should be allocated
    try {
        client().index(Requests.indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON).timeout(timeValueSeconds(1))).actionGet();
        fail("no allocation should happen");
    } catch (UnavailableShardsException e) {
    // all is well
    }
    // now, start a node data, and see that it gets with shards
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true).build());
    assertThat(client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("3").setLocal(true).execute().actionGet().isTimedOut(), equalTo(false));
    IndexResponse indexResponse = client().index(Requests.indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
    assertThat(indexResponse.getId(), equalTo("1"));
    assertThat(indexResponse.getType(), equalTo("type1"));
}
Also used : UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 5 with UnavailableShardsException

use of org.elasticsearch.action.UnavailableShardsException in project elasticsearch by elastic.

the class ReplicationOperation method execute.

public void execute() throws Exception {
    final String activeShardCountFailure = checkActiveShardCount();
    final ShardRouting primaryRouting = primary.routingEntry();
    final ShardId primaryId = primaryRouting.shardId();
    if (activeShardCountFailure != null) {
        finishAsFailed(new UnavailableShardsException(primaryId, "{} Timeout: [{}], request: [{}]", activeShardCountFailure, request.timeout(), request));
        return;
    }
    totalShards.incrementAndGet();
    // increase by 1 until we finish all primary coordination
    pendingActions.incrementAndGet();
    primaryResult = primary.perform(request);
    primary.updateLocalCheckpointForShard(primaryRouting.allocationId().getId(), primary.localCheckpoint());
    final ReplicaRequest replicaRequest = primaryResult.replicaRequest();
    if (replicaRequest != null) {
        assert replicaRequest.primaryTerm() > 0 : "replicaRequest doesn't have a primary term";
        if (logger.isTraceEnabled()) {
            logger.trace("[{}] op [{}] completed on primary for request [{}]", primaryId, opType, request);
        }
        // we have to get a new state after successfully indexing into the primary in order to honour recovery semantics.
        // we have to make sure that every operation indexed into the primary after recovery start will also be replicated
        // to the recovery target. If we use an old cluster state, we may miss a relocation that has started since then.
        ClusterState clusterState = clusterStateSupplier.get();
        final List<ShardRouting> shards = getShards(primaryId, clusterState);
        Set<String> inSyncAllocationIds = getInSyncAllocationIds(primaryId, clusterState);
        markUnavailableShardsAsStale(replicaRequest, inSyncAllocationIds, shards);
        performOnReplicas(replicaRequest, shards);
    }
    // mark primary as successful
    successfulShards.incrementAndGet();
    decPendingAndFinishIfNeeded();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

UnavailableShardsException (org.elasticsearch.action.UnavailableShardsException)5 ShardId (org.elasticsearch.index.shard.ShardId)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ActionListener (org.elasticsearch.action.ActionListener)1 NoShardAvailableActionException (org.elasticsearch.action.NoShardAvailableActionException)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BroadcastResponse (org.elasticsearch.action.support.broadcast.BroadcastResponse)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)1 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)1 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)1