Search in sources :

Example 6 with DocsStats

use of org.elasticsearch.index.shard.DocsStats in project elasticsearch by elastic.

the class TransportShrinkActionTests method testShrinkIndexSettings.

public void testShrinkIndexSettings() {
    String indexName = randomAsciiOfLength(10);
    // create one that won't fail
    ClusterState clusterState = ClusterState.builder(createClusterState(indexName, randomIntBetween(2, 10), 0, Settings.builder().put("index.blocks.write", true).build())).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    AllocationService service = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY, Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    // now we start the shard
    routingTable = service.applyStartedShards(clusterState, routingTable.index(indexName).shardsWithState(ShardRoutingState.INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    int numSourceShards = clusterState.metaData().index(indexName).getNumberOfShards();
    DocsStats stats = new DocsStats(randomIntBetween(0, (IndexWriter.MAX_DOCS) / numSourceShards), randomIntBetween(1, 1000));
    ShrinkRequest target = new ShrinkRequest("target", indexName);
    final ActiveShardCount activeShardCount = randomBoolean() ? ActiveShardCount.ALL : ActiveShardCount.ONE;
    target.setWaitForActiveShards(activeShardCount);
    CreateIndexClusterStateUpdateRequest request = TransportShrinkAction.prepareCreateIndexRequest(target, clusterState, (i) -> stats, new IndexNameExpressionResolver(Settings.EMPTY));
    assertNotNull(request.shrinkFrom());
    assertEquals(indexName, request.shrinkFrom().getName());
    assertEquals("1", request.settings().get("index.number_of_shards"));
    assertEquals("shrink_index", request.cause());
    assertEquals(request.waitForActiveShards(), activeShardCount);
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) CreateIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 7 with DocsStats

use of org.elasticsearch.index.shard.DocsStats in project elasticsearch by elastic.

the class RecoveryWhileUnderLoadIT method iterateAssertCount.

private void iterateAssertCount(final int numberOfShards, final int iterations, final Set<String> ids) throws Exception {
    final long numberOfDocs = ids.size();
    SearchResponse[] iterationResults = new SearchResponse[iterations];
    boolean error = false;
    for (int i = 0; i < iterations; i++) {
        SearchResponse searchResponse = client().prepareSearch().setSize((int) numberOfDocs).setQuery(matchAllQuery()).addSort("id", SortOrder.ASC).get();
        logSearchResponse(numberOfShards, numberOfDocs, i, searchResponse);
        iterationResults[i] = searchResponse;
        if (searchResponse.getHits().getTotalHits() != numberOfDocs) {
            error = true;
        }
    }
    if (error) {
        //Printing out shards and their doc count
        IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats().get();
        for (ShardStats shardStats : indicesStatsResponse.getShards()) {
            DocsStats docsStats = shardStats.getStats().docs;
            logger.info("shard [{}] - count {}, primary {}", shardStats.getShardRouting().id(), docsStats.getCount(), shardStats.getShardRouting().primary());
        }
        ClusterService clusterService = clusterService();
        final ClusterState state = clusterService.state();
        for (int shard = 0; shard < numberOfShards; shard++) {
            for (String id : ids) {
                ShardId docShard = clusterService.operationRouting().shardId(state, "test", id, null);
                if (docShard.id() == shard) {
                    for (ShardRouting shardRouting : state.routingTable().shardRoutingTable("test", shard)) {
                        GetResponse response = client().prepareGet("test", "type", id).setPreference("_only_nodes:" + shardRouting.currentNodeId()).get();
                        if (response.isExists()) {
                            logger.info("missing id [{}] on shard {}", id, shardRouting);
                        }
                    }
                }
            }
        }
        //if there was an error we try to wait and see if at some point it'll get fixed
        logger.info("--> trying to wait");
        assertTrue(awaitBusy(() -> {
            boolean errorOccurred = false;
            for (int i = 0; i < iterations; i++) {
                SearchResponse searchResponse = client().prepareSearch().setSize(0).setQuery(matchAllQuery()).get();
                if (searchResponse.getHits().getTotalHits() != numberOfDocs) {
                    errorOccurred = true;
                }
            }
            return !errorOccurred;
        }, 5, TimeUnit.MINUTES));
        assertEquals(numberOfDocs, ids.size());
    }
    //lets now make the test fail if it was supposed to fail
    for (int i = 0; i < iterations; i++) {
        assertHitCount(iterationResults[i], numberOfDocs);
    }
}
Also used : ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) ClusterState(org.elasticsearch.cluster.ClusterState) GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.service.ClusterService) DocsStats(org.elasticsearch.index.shard.DocsStats) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 8 with DocsStats

use of org.elasticsearch.index.shard.DocsStats in project graylog2-server by Graylog2.

the class Indices method numberOfMessages.

public long numberOfMessages(String indexName) throws IndexNotFoundException {
    final IndexStats index = indexStats(indexName);
    if (index == null) {
        throw new IndexNotFoundException("Couldn't find index " + indexName);
    }
    final DocsStats docsStats = index.getPrimaries().getDocs();
    return docsStats == null ? 0L : docsStats.getCount();
}
Also used : IndexNotFoundException(org.graylog2.indexer.IndexNotFoundException) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexStats(org.elasticsearch.action.admin.indices.stats.IndexStats)

Aggregations

DocsStats (org.elasticsearch.index.shard.DocsStats)8 CreateIndexClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest)3 ClusterState (org.elasticsearch.cluster.ClusterState)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3 Settings (org.elasticsearch.common.settings.Settings)3 ShardId (org.elasticsearch.index.shard.ShardId)3 ShardStats (org.elasticsearch.action.admin.indices.stats.ShardStats)2 ActiveShardCount (org.elasticsearch.action.support.ActiveShardCount)2 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)2 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)2 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)2 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)2 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)2 AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)2 MaxRetryAllocationDecider (org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider)2 QueryCacheStats (org.elasticsearch.index.cache.query.QueryCacheStats)2 RequestCacheStats (org.elasticsearch.index.cache.request.RequestCacheStats)2 SegmentsStats (org.elasticsearch.index.engine.SegmentsStats)2 FieldDataStats (org.elasticsearch.index.fielddata.FieldDataStats)2 FlushStats (org.elasticsearch.index.flush.FlushStats)2