Search in sources :

Example 1 with ShardLockObtainFailedException

use of org.elasticsearch.env.ShardLockObtainFailedException in project elasticsearch by elastic.

the class IndicesService method deleteShardStore.

/**
     * This method deletes the shard contents on disk for the given shard ID. This method will fail if the shard deleting
     * is prevented by {@link #canDeleteShardContent(ShardId, IndexSettings)}
     * of if the shards lock can not be acquired.
     *
     * On data nodes, if the deleted shard is the last shard folder in its index, the method will attempt to remove the index folder as well.
     *
     * @param reason the reason for the shard deletion
     * @param shardId the shards ID to delete
     * @param clusterState . This is required to access the indexes settings etc.
     * @throws IOException if an IOException occurs
     */
public void deleteShardStore(String reason, ShardId shardId, ClusterState clusterState) throws IOException, ShardLockObtainFailedException {
    final IndexMetaData metaData = clusterState.getMetaData().indices().get(shardId.getIndexName());
    final IndexSettings indexSettings = buildIndexSettings(metaData);
    ShardDeletionCheckResult shardDeletionCheckResult = canDeleteShardContent(shardId, indexSettings);
    if (shardDeletionCheckResult != ShardDeletionCheckResult.FOLDER_FOUND_CAN_DELETE) {
        throw new IllegalStateException("Can't delete shard " + shardId + " (cause: " + shardDeletionCheckResult + ")");
    }
    nodeEnv.deleteShardDirectorySafe(shardId, indexSettings);
    logger.debug("{} deleted shard reason [{}]", shardId, reason);
    if (// master nodes keep the index meta data, even if having no shards..
    clusterState.nodes().getLocalNode().isMasterNode() == false && canDeleteIndexContents(shardId.getIndex(), indexSettings)) {
        if (nodeEnv.findAllShardIds(shardId.getIndex()).isEmpty()) {
            try {
                // note that deleteIndexStore have more safety checks and may throw an exception if index was concurrently created.
                deleteIndexStore("no longer used", metaData, clusterState);
            } catch (Exception e) {
                // wrap the exception to indicate we already deleted the shard
                throw new ElasticsearchException("failed to delete unused index after deleting its last shard (" + shardId + ")", e);
            }
        } else {
            logger.trace("[{}] still has shard stores, leaving as is", shardId.getIndex());
        }
    }
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 2 with ShardLockObtainFailedException

use of org.elasticsearch.env.ShardLockObtainFailedException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testShardLockObtainFailedException.

public void testShardLockObtainFailedException() throws IOException {
    ShardId shardId = new ShardId("foo", "_na_", 1);
    ShardLockObtainFailedException orig = new ShardLockObtainFailedException(shardId, "boom");
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0, Version.CURRENT);
    if (version.before(Version.V_5_0_2)) {
        version = Version.V_5_0_2;
    }
    ShardLockObtainFailedException ex = serialize(orig, version);
    assertEquals(orig.getMessage(), ex.getMessage());
    assertEquals(orig.getShardId(), ex.getShardId());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException)

Example 3 with ShardLockObtainFailedException

use of org.elasticsearch.env.ShardLockObtainFailedException in project elasticsearch by elastic.

the class PrimaryShardAllocatorTests method testShardLockObtainFailedExceptionPreferOtherValidCopies.

/**
     * Tests that when one node returns a ShardLockObtainFailedException and another properly loads the store, it will
     * select the second node as target
     */
public void testShardLockObtainFailedExceptionPreferOtherValidCopies() {
    String allocId1 = randomAsciiOfLength(10);
    String allocId2 = randomAsciiOfLength(10);
    final RoutingAllocation allocation = routingAllocationWithOnePrimaryNoReplicas(yesAllocationDeciders(), CLUSTER_RECOVERED, allocId1, allocId2);
    ;
    testAllocator.addData(node1, allocId1, randomBoolean(), new ShardLockObtainFailedException(shardId, "test"));
    testAllocator.addData(node2, allocId2, randomBoolean(), null);
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodesChanged(), equalTo(true));
    assertThat(allocation.routingNodes().unassigned().ignored().isEmpty(), equalTo(true));
    assertThat(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(1));
    assertThat(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), equalTo(node2.getId()));
    // check that allocation id is reused
    assertThat(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).allocationId().getId(), equalTo(allocId2));
    assertClusterHealthStatus(allocation, ClusterHealthStatus.YELLOW);
}
Also used : RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException)

Example 4 with ShardLockObtainFailedException

use of org.elasticsearch.env.ShardLockObtainFailedException in project elasticsearch by elastic.

the class PrimaryShardAllocatorTests method testShardLockObtainFailedException.

/**
     * Tests that when the node returns a ShardLockObtainFailedException, it will be considered as a valid shard copy
     */
public void testShardLockObtainFailedException() {
    final RoutingAllocation allocation = routingAllocationWithOnePrimaryNoReplicas(yesAllocationDeciders(), CLUSTER_RECOVERED, "allocId1");
    testAllocator.addData(node1, "allocId1", randomBoolean(), new ShardLockObtainFailedException(shardId, "test"));
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodesChanged(), equalTo(true));
    assertThat(allocation.routingNodes().unassigned().ignored().isEmpty(), equalTo(true));
    assertThat(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(1));
    assertThat(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), equalTo(node1.getId()));
    // check that allocation id is reused
    assertThat(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).allocationId().getId(), equalTo("allocId1"));
    assertClusterHealthStatus(allocation, ClusterHealthStatus.YELLOW);
}
Also used : RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException)

Example 5 with ShardLockObtainFailedException

use of org.elasticsearch.env.ShardLockObtainFailedException in project crate by crate.

the class IndicesService method deleteShardStore.

/**
 * This method deletes the shard contents on disk for the given shard ID. This method will fail if the shard deleting
 * is prevented by {@link #canDeleteShardContent(ShardId, IndexSettings)}
 * of if the shards lock can not be acquired.
 *
 * On data nodes, if the deleted shard is the last shard folder in its index, the method will attempt to remove the index folder as well.
 *
 * @param reason the reason for the shard deletion
 * @param shardId the shards ID to delete
 * @param clusterState . This is required to access the indexes settings etc.
 * @throws IOException if an IOException occurs
 */
public void deleteShardStore(String reason, ShardId shardId, ClusterState clusterState) throws IOException, ShardLockObtainFailedException {
    final IndexMetadata metadata = clusterState.getMetadata().indices().get(shardId.getIndexName());
    final IndexSettings indexSettings = buildIndexSettings(metadata);
    ShardDeletionCheckResult shardDeletionCheckResult = canDeleteShardContent(shardId, indexSettings);
    if (shardDeletionCheckResult != ShardDeletionCheckResult.FOLDER_FOUND_CAN_DELETE) {
        throw new IllegalStateException("Can't delete shard " + shardId + " (cause: " + shardDeletionCheckResult + ")");
    }
    nodeEnv.deleteShardDirectorySafe(shardId, indexSettings);
    LOGGER.debug("{} deleted shard reason [{}]", shardId, reason);
    if (canDeleteIndexContents(shardId.getIndex(), indexSettings)) {
        if (nodeEnv.findAllShardIds(shardId.getIndex()).isEmpty()) {
            try {
                // note that deleteIndexStore have more safety checks and may throw an exception if index was concurrently created.
                deleteIndexStore("no longer used", metadata);
            } catch (Exception e) {
                // wrap the exception to indicate we already deleted the shard
                throw new ElasticsearchException("failed to delete unused index after deleting its last shard (" + shardId + ")", e);
            }
        } else {
            LOGGER.trace("[{}] still has shard stores, leaving as is", shardId.getIndex());
        }
    }
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) UncheckedIOException(java.io.UncheckedIOException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Aggregations

ShardLockObtainFailedException (org.elasticsearch.env.ShardLockObtainFailedException)14 ShardId (org.elasticsearch.index.shard.ShardId)7 IOException (java.io.IOException)6 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)4 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)3 IndexShardClosedException (org.elasticsearch.index.shard.IndexShardClosedException)3 ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)3 ShardPath (org.elasticsearch.index.shard.ShardPath)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)2 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)2 TimeValue (org.elasticsearch.common.unit.TimeValue)2 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 IndexSettings (org.elasticsearch.index.IndexSettings)2 TimeValue (io.crate.common.unit.TimeValue)1 Closeable (java.io.Closeable)1