Search in sources :

Example 36 with UnassignedInfo

use of org.elasticsearch.cluster.routing.UnassignedInfo in project crate by crate.

the class GatewayIndexStateIT method testRecoverMissingAnalyzer.

/**
 * This test really tests worst case scenario where we have a missing analyzer setting.
 * In that case we now have the ability to check the index on local recovery from disk
 * if it is sane and if we can successfully create an IndexService.
 * This also includes plugins etc.
 */
@Test
public void testRecoverMissingAnalyzer() throws Exception {
    logger.info("--> starting one node");
    internalCluster().startNode();
    var tableName = getFqn("test");
    prepareCreate(tableName).setSettings(Settings.builder().put("index.analysis.analyzer.test.tokenizer", "standard").put("index.number_of_shards", "1")).addMapping("default", "{\n" + "    \"default\": {\n" + "      \"properties\": {\n" + "        \"field1\": {\n" + "          \"type\": \"text\",\n" + "          \"analyzer\": \"test\"\n" + "        }\n" + "      }\n" + "    }\n" + "  }}", XContentType.JSON).get();
    logger.info("--> indexing a simple document");
    execute("insert into test (field1) values ('value one')");
    execute("refresh table test");
    logger.info("--> waiting for green status");
    if (usually()) {
        ensureYellow();
    } else {
        internalCluster().startNode();
        client().admin().cluster().health(Requests.clusterHealthRequest().waitForGreenStatus().waitForEvents(Priority.LANGUID).waitForNoRelocatingShards(true).waitForNodes("2")).actionGet(REQUEST_TIMEOUT);
    }
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    final IndexMetadata metadata = state.getMetadata().index(tableName);
    final IndexMetadata.Builder brokenMeta = IndexMetadata.builder(metadata).settings(metadata.getSettings().filter((s) -> "index.analysis.analyzer.test.tokenizer".equals(s) == false));
    restartNodesOnBrokenClusterState(ClusterState.builder(state).metadata(Metadata.builder(state.getMetadata()).put(brokenMeta)));
    // check that the cluster does not keep reallocating shards
    assertBusy(() -> {
        final RoutingTable routingTable = client().admin().cluster().prepareState().execute().actionGet(REQUEST_TIMEOUT).getState().routingTable();
        final IndexRoutingTable indexRoutingTable = routingTable.index(tableName);
        assertNotNull(indexRoutingTable);
        for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) {
            assertTrue(shardRoutingTable.primaryShard().unassigned());
            assertEquals(UnassignedInfo.AllocationStatus.DECIDERS_NO, shardRoutingTable.primaryShard().unassignedInfo().getLastAllocationStatus());
            assertThat(shardRoutingTable.primaryShard().unassignedInfo().getNumFailedAllocations(), greaterThan(0));
        }
    }, 60, TimeUnit.SECONDS);
    execute("alter table test close");
    // try to open it with the broken setting - fail again!
    assertThrowsMatches(() -> execute("alter table test open"), isSQLError(is("Failed to verify index " + metadata.getIndex().getName()), INTERNAL_ERROR, INTERNAL_SERVER_ERROR, 5000));
}
Also used : REQUEST_TIMEOUT(io.crate.testing.SQLTransportExecutor.REQUEST_TIMEOUT) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) Scope(org.elasticsearch.test.ESIntegTestCase.Scope) Locale(java.util.Locale) Map(java.util.Map) SQLTransportExecutor(io.crate.testing.SQLTransportExecutor) SQLOperations(io.crate.action.sql.SQLOperations) Priority(org.elasticsearch.common.Priority) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) SETTING_CLUSTER_MAX_SHARDS_PER_NODE(org.elasticsearch.indices.ShardLimitValidator.SETTING_CLUSTER_MAX_SHARDS_PER_NODE) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) INTERNAL_ERROR(io.crate.protocols.postgres.PGErrorStatus.INTERNAL_ERROR) XContentType(org.elasticsearch.common.xcontent.XContentType) INTERNAL_SERVER_ERROR(io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR) SQLIntegrationTestCase(io.crate.integrationtests.SQLIntegrationTestCase) IndexGraveyard(org.elasticsearch.cluster.metadata.IndexGraveyard) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) Function(java.util.function.Function) Asserts.assertThrowsMatches(io.crate.testing.Asserts.assertThrowsMatches) Metadata(org.elasticsearch.cluster.metadata.Metadata) Node(org.elasticsearch.node.Node) Requests(org.elasticsearch.client.Requests) ClusterScope(org.elasticsearch.test.ESIntegTestCase.ClusterScope) Client(org.elasticsearch.client.Client) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) Test(org.junit.Test) IOException(java.io.IOException) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) TimeUnit(java.util.concurrent.TimeUnit) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) SQLErrorMatcher.isSQLError(io.crate.testing.SQLErrorMatcher.isSQLError) PostgresNetty(io.crate.protocols.postgres.PostgresNetty) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) LogManager(org.apache.logging.log4j.LogManager) ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Test(org.junit.Test)

Example 37 with UnassignedInfo

use of org.elasticsearch.cluster.routing.UnassignedInfo in project elasticsearch by elastic.

the class AllocationService method deassociateDeadNodes.

private void deassociateDeadNodes(RoutingAllocation allocation) {
    for (Iterator<RoutingNode> it = allocation.routingNodes().mutableIterator(); it.hasNext(); ) {
        RoutingNode node = it.next();
        if (allocation.nodes().getDataNodes().containsKey(node.nodeId())) {
            // its a live node, continue
            continue;
        }
        // now, go over all the shards routing on the node, and fail them
        for (ShardRouting shardRouting : node.copyShards()) {
            final IndexMetaData indexMetaData = allocation.metaData().getIndexSafe(shardRouting.index());
            boolean delayed = INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.get(indexMetaData.getSettings()).nanos() > 0;
            UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.NODE_LEFT, "node_left[" + node.nodeId() + "]", null, 0, allocation.getCurrentNanoTime(), System.currentTimeMillis(), delayed, AllocationStatus.NO_ATTEMPT);
            allocation.routingNodes().failShard(logger, shardRouting, unassignedInfo, indexMetaData, allocation.changes());
        }
        // its a dead node, remove it, note, its important to remove it *after* we apply failed shard
        // since it relies on the fact that the RoutingNode exists in the list of nodes
        it.remove();
    }
}
Also used : RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 38 with UnassignedInfo

use of org.elasticsearch.cluster.routing.UnassignedInfo in project elasticsearch by elastic.

the class AllocationService method removeDelayMarkers.

/**
     * Removes delay markers from unassigned shards based on current time stamp.
     */
private void removeDelayMarkers(RoutingAllocation allocation) {
    final RoutingNodes.UnassignedShards.UnassignedIterator unassignedIterator = allocation.routingNodes().unassigned().iterator();
    final MetaData metaData = allocation.metaData();
    while (unassignedIterator.hasNext()) {
        ShardRouting shardRouting = unassignedIterator.next();
        UnassignedInfo unassignedInfo = shardRouting.unassignedInfo();
        if (unassignedInfo.isDelayed()) {
            final long newComputedLeftDelayNanos = unassignedInfo.getRemainingDelay(allocation.getCurrentNanoTime(), metaData.getIndexSafe(shardRouting.index()).getSettings());
            if (newComputedLeftDelayNanos == 0) {
                unassignedIterator.updateUnassigned(new UnassignedInfo(unassignedInfo.getReason(), unassignedInfo.getMessage(), unassignedInfo.getFailure(), unassignedInfo.getNumFailedAllocations(), unassignedInfo.getUnassignedTimeInNanos(), unassignedInfo.getUnassignedTimeInMillis(), false, unassignedInfo.getLastAllocationStatus()), shardRouting.recoverySource(), allocation.changes());
            }
        }
    }
}
Also used : UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 39 with UnassignedInfo

use of org.elasticsearch.cluster.routing.UnassignedInfo in project elasticsearch by elastic.

the class PriorityComparatorTests method testPreferNewIndices.

public void testPreferNewIndices() {
    RoutingNodes.UnassignedShards shards = new RoutingNodes.UnassignedShards(mock(RoutingNodes.class));
    List<ShardRouting> shardRoutings = Arrays.asList(TestShardRouting.newShardRouting("oldest", 0, null, null, randomBoolean(), ShardRoutingState.UNASSIGNED, new UnassignedInfo(randomFrom(UnassignedInfo.Reason.values()), "foobar")), TestShardRouting.newShardRouting("newest", 0, null, null, randomBoolean(), ShardRoutingState.UNASSIGNED, new UnassignedInfo(randomFrom(UnassignedInfo.Reason.values()), "foobar")));
    Collections.shuffle(shardRoutings, random());
    for (ShardRouting routing : shardRoutings) {
        shards.add(routing);
    }
    shards.sort(new PriorityComparator() {

        @Override
        protected Settings getIndexSettings(Index index) {
            if ("oldest".equals(index.getName())) {
                return Settings.builder().put(IndexMetaData.SETTING_CREATION_DATE, 10).put(IndexMetaData.SETTING_PRIORITY, 1).build();
            } else if ("newest".equals(index.getName())) {
                return Settings.builder().put(IndexMetaData.SETTING_CREATION_DATE, 100).put(IndexMetaData.SETTING_PRIORITY, 1).build();
            }
            return Settings.EMPTY;
        }
    });
    RoutingNodes.UnassignedShards.UnassignedIterator iterator = shards.iterator();
    ShardRouting next = iterator.next();
    assertEquals("newest", next.getIndexName());
    next = iterator.next();
    assertEquals("oldest", next.getIndexName());
    assertFalse(iterator.hasNext());
}
Also used : RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Index(org.elasticsearch.index.Index) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) Settings(org.elasticsearch.common.settings.Settings)

Example 40 with UnassignedInfo

use of org.elasticsearch.cluster.routing.UnassignedInfo in project elasticsearch by elastic.

the class PriorityComparatorTests method testPriorityComparatorSort.

public void testPriorityComparatorSort() {
    RoutingNodes.UnassignedShards shards = new RoutingNodes.UnassignedShards(mock(RoutingNodes.class));
    int numIndices = randomIntBetween(3, 99);
    IndexMeta[] indices = new IndexMeta[numIndices];
    final Map<String, IndexMeta> map = new HashMap<>();
    for (int i = 0; i < indices.length; i++) {
        if (frequently()) {
            indices[i] = new IndexMeta("idx_2015_04_" + String.format(Locale.ROOT, "%02d", i), randomIntBetween(1, 1000), randomIntBetween(1, 10000));
        } else {
            // sometimes just use defaults
            indices[i] = new IndexMeta("idx_2015_04_" + String.format(Locale.ROOT, "%02d", i));
        }
        map.put(indices[i].name, indices[i]);
    }
    int numShards = randomIntBetween(10, 100);
    for (int i = 0; i < numShards; i++) {
        IndexMeta indexMeta = randomFrom(indices);
        shards.add(TestShardRouting.newShardRouting(indexMeta.name, randomIntBetween(1, 5), null, null, randomBoolean(), ShardRoutingState.UNASSIGNED, new UnassignedInfo(randomFrom(UnassignedInfo.Reason.values()), "foobar")));
    }
    shards.sort(new PriorityComparator() {

        @Override
        protected Settings getIndexSettings(Index index) {
            IndexMeta indexMeta = map.get(index.getName());
            return indexMeta.settings;
        }
    });
    ShardRouting previous = null;
    for (ShardRouting routing : shards) {
        if (previous != null) {
            IndexMeta prevMeta = map.get(previous.getIndexName());
            IndexMeta currentMeta = map.get(routing.getIndexName());
            if (prevMeta.priority == currentMeta.priority) {
                if (prevMeta.creationDate == currentMeta.creationDate) {
                    if (prevMeta.name.equals(currentMeta.name) == false) {
                        assertTrue("indexName mismatch, expected:" + currentMeta.name + " after " + prevMeta.name + " " + prevMeta.name.compareTo(currentMeta.name), prevMeta.name.compareTo(currentMeta.name) > 0);
                    }
                } else {
                    assertTrue("creationDate mismatch, expected:" + currentMeta.creationDate + " after " + prevMeta.creationDate, prevMeta.creationDate > currentMeta.creationDate);
                }
            } else {
                assertTrue("priority mismatch, expected:" + currentMeta.priority + " after " + prevMeta.priority, prevMeta.priority > currentMeta.priority);
            }
        }
        previous = routing;
    }
}
Also used : RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) HashMap(java.util.HashMap) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Index(org.elasticsearch.index.Index) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)68 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)45 ShardId (org.elasticsearch.index.shard.ShardId)36 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)30 ClusterState (org.elasticsearch.cluster.ClusterState)20 Index (org.elasticsearch.index.Index)19 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)18 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)18 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)18 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)18 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)17 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)16 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)16 Matchers.containsString (org.hamcrest.Matchers.containsString)16 ShardRoutingState (org.elasticsearch.cluster.routing.ShardRoutingState)14 AllocateUnassignedDecision (org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision)12 NodeAllocationResult (org.elasticsearch.cluster.routing.allocation.NodeAllocationResult)11 Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)11 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)11 MetaData (org.elasticsearch.cluster.metadata.MetaData)10