Search in sources :

Example 1 with InternalClusterInfoService

use of org.elasticsearch.cluster.InternalClusterInfoService in project elasticsearch by elastic.

the class IndexShardIT method testExpectedShardSizeIsPresent.

public void testExpectedShardSizeIsPresent() throws InterruptedException {
    assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0));
    for (int i = 0; i < 50; i++) {
        client().prepareIndex("test", "test").setSource("{}", XContentType.JSON).get();
    }
    ensureGreen("test");
    InternalClusterInfoService clusterInfoService = (InternalClusterInfoService) getInstanceFromNode(ClusterInfoService.class);
    clusterInfoService.refresh();
    ClusterState state = getInstanceFromNode(ClusterService.class).state();
    Long test = clusterInfoService.getClusterInfo().getShardSize(state.getRoutingTable().index("test").getShards().get(0).primaryShard());
    assertNotNull(test);
    assertTrue(test > 0);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterService(org.elasticsearch.cluster.service.ClusterService) ClusterInfoService(org.elasticsearch.cluster.ClusterInfoService) InternalClusterInfoService(org.elasticsearch.cluster.InternalClusterInfoService) InternalClusterInfoService(org.elasticsearch.cluster.InternalClusterInfoService)

Example 2 with InternalClusterInfoService

use of org.elasticsearch.cluster.InternalClusterInfoService in project elasticsearch by elastic.

the class ShrinkIndexIT method testCreateShrinkIndexFails.

/**
     * Tests that we can manually recover from a failed allocation due to shards being moved away etc.
     */
public void testCreateShrinkIndexFails() throws Exception {
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("number_of_shards", randomIntBetween(2, 7)).put("number_of_replicas", 0)).get();
    for (int i = 0; i < 20; i++) {
        client().prepareIndex("source", randomFrom("t1", "t2", "t3")).setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes();
    assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2);
    DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class);
    String spareNode = discoveryNodes[0].getName();
    String mergeNode = discoveryNodes[1].getName();
    // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node
    // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due
    // to the require._name below.
    ensureGreen();
    // relocate all shards to one node such that we can merge it.
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode).put("index.blocks.write", true)).get();
    ensureGreen();
    // now merge source into a single shard index
    client().admin().indices().prepareShrinkIndex("source", "target").setWaitForActiveShards(ActiveShardCount.NONE).setSettings(Settings.builder().put("index.routing.allocation.exclude._name", // we manually exclude the merge node to forcefully fuck it up
    mergeNode).put("index.number_of_replicas", 0).put("index.allocation.max_retries", 1).build()).get();
    client().admin().cluster().prepareHealth("target").setWaitForEvents(Priority.LANGUID).get();
    // now we move all shards away from the merge node
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", spareNode).put("index.blocks.write", true)).get();
    ensureGreen("source");
    // erase the forcefully fuckup!
    client().admin().indices().prepareUpdateSettings("target").setSettings(Settings.builder().putNull("index.routing.allocation.exclude._name")).get();
    // wait until it fails
    assertBusy(() -> {
        ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get();
        RoutingTable routingTables = clusterStateResponse.getState().routingTable();
        assertTrue(routingTables.index("target").shard(0).getShards().get(0).unassigned());
        assertEquals(UnassignedInfo.Reason.ALLOCATION_FAILED, routingTables.index("target").shard(0).getShards().get(0).unassignedInfo().getReason());
        assertEquals(1, routingTables.index("target").shard(0).getShards().get(0).unassignedInfo().getNumFailedAllocations());
    });
    // now relocate them all to the right node
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode)).get();
    ensureGreen("source");
    final InternalClusterInfoService infoService = (InternalClusterInfoService) internalCluster().getInstance(ClusterInfoService.class, internalCluster().getMasterName());
    infoService.refresh();
    // kick off a retry and wait until it's done!
    ClusterRerouteResponse clusterRerouteResponse = client().admin().cluster().prepareReroute().setRetryFailed(true).get();
    long expectedShardSize = clusterRerouteResponse.getState().routingTable().index("target").shard(0).getShards().get(0).getExpectedShardSize();
    // we support the expected shard size in the allocator to sum up over the source index shards
    assertTrue("expected shard size must be set but wasn't: " + expectedShardSize, expectedShardSize > 0);
    ensureGreen();
    assertHitCount(client().prepareSearch("target").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) InternalClusterInfoService(org.elasticsearch.cluster.InternalClusterInfoService) ClusterInfoService(org.elasticsearch.cluster.ClusterInfoService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) InternalClusterInfoService(org.elasticsearch.cluster.InternalClusterInfoService) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Aggregations

ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)2 InternalClusterInfoService (org.elasticsearch.cluster.InternalClusterInfoService)2 ClusterRerouteResponse (org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1 ClusterService (org.elasticsearch.cluster.service.ClusterService)1 TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)1