Search in sources :

Example 1 with UnavailableShardsException

use of org.opensearch.action.UnavailableShardsException in project OpenSearch by opensearch-project.

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();
    primary.perform(request, ActionListener.wrap(this::handlePrimaryResult, this::finishAsFailed));
}
Also used : ShardId(org.opensearch.index.shard.ShardId) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 2 with UnavailableShardsException

use of org.opensearch.action.UnavailableShardsException in project OpenSearch by opensearch-project.

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").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<AcknowledgedResponse>() {

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

                @Override
                public void run() {
                    try {
                        // recreate that index
                        client().prepareIndex("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").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().value, all.getHits().getTotalHits().value);
    logger.info("total: {}", expected.getHits().getTotalHits().value);
}
Also used : AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) CountDownLatch(java.util.concurrent.CountDownLatch) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) SearchResponse(org.opensearch.action.search.SearchResponse) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 3 with UnavailableShardsException

use of org.opensearch.action.UnavailableShardsException in project OpenSearch by opensearch-project.

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").setId("1").setSource(source("1", "test"), XContentType.JSON).execute().actionGet();
    try {
        client().prepareIndex("test").setId("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").setId("1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(2).setTimeout(timeValueSeconds(1)).execute().actionGet();
    try {
        client().prepareIndex("test").setId("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").setId("1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(ActiveShardCount.ALL).setTimeout(timeValueSeconds(1)).execute().actionGet();
}
Also used : UnavailableShardsException(org.opensearch.action.UnavailableShardsException) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse)

Example 4 with UnavailableShardsException

use of org.opensearch.action.UnavailableShardsException in project OpenSearch by opensearch-project.

the class SimpleDataNodesIT method testIndexingBeforeAndAfterDataNodesStart.

public void testIndexingBeforeAndAfterDataNodesStart() {
    internalCluster().startNode(nonDataNode());
    client().admin().indices().create(createIndexRequest("test").waitForActiveShards(ActiveShardCount.NONE)).actionGet();
    try {
        client().index(Requests.indexRequest("test").id("1").source(SOURCE, XContentType.JSON).timeout(timeValueSeconds(1))).actionGet();
        fail("no allocation should happen");
    } catch (UnavailableShardsException e) {
    // all is well
    }
    internalCluster().startNode(nonDataNode());
    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").id("1").source(SOURCE, 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(dataNode());
    assertThat(client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("3").setLocal(true).execute().actionGet().isTimedOut(), equalTo(false));
    IndexResponse indexResponse = client().index(Requests.indexRequest("test").id("1").source(SOURCE, XContentType.JSON)).actionGet();
    assertThat(indexResponse.getId(), equalTo("1"));
}
Also used : UnavailableShardsException(org.opensearch.action.UnavailableShardsException) IndexResponse(org.opensearch.action.index.IndexResponse)

Example 5 with UnavailableShardsException

use of org.opensearch.action.UnavailableShardsException in project OpenSearch by opensearch-project.

the class BroadcastReplicationTests method testNotStartedPrimary.

public void testNotStartedPrimary() throws InterruptedException, ExecutionException {
    final String index = "test";
    setState(clusterService, state(index, randomBoolean(), randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.UNASSIGNED, ShardRoutingState.UNASSIGNED));
    logger.debug("--> using initial state:\n{}", clusterService.state());
    PlainActionFuture<BroadcastResponse> response = PlainActionFuture.newFuture();
    broadcastReplicationAction.execute(new DummyBroadcastRequest(index), response);
    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.opensearch.index.shard.ShardId) ActionListener(org.opensearch.action.ActionListener) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse)

Aggregations

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