org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestElasticsearchIndexManager.java    From Raigad with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunIndexManagement_NotActionable_NoRetentionPeriod() throws Exception {
    String serializedIndexMetadata = "[{\"retentionType\": \"yearly\", \"indexName\": \"nf_errors_log\"}]";
    when(config.getIndexMetadata()).thenReturn(serializedIndexMetadata);

    Map<String, IndexStats> indexStats = new HashMap<>();
    indexStats.put("nf_errors_log2018", new IndexStats("nf_errors_log2018", new ShardStats[]{}));

    IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class);
    when(indicesStatsResponse.getIndices()).thenReturn(indexStats);

    doReturn(indicesStatsResponse).when(elasticsearchIndexManager).getIndicesStatsResponse(elasticsearchClient);

    elasticsearchIndexManager.runIndexManagement();

    verify(elasticsearchIndexManager, times(0)).checkIndexRetention(any(Client.class), anySet(), any(IndexMetadata.class), any(DateTime.class));
    verify(elasticsearchIndexManager, times(0)).preCreateIndex(any(Client.class), any(IndexMetadata.class), any(DateTime.class));
}
 
Example #2
Source File: TestElasticsearchIndexManager.java    From Raigad with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunIndexManagement_NotActionable_NoIndex() throws Exception {
    String serializedIndexMetadata = "[{\"retentionType\": \"yearly\", \"retentionPeriod\": 20}]";
    when(config.getIndexMetadata()).thenReturn(serializedIndexMetadata);

    Map<String, IndexStats> indexStats = new HashMap<>();
    indexStats.put("nf_errors_log2018", new IndexStats("nf_errors_log2018", new ShardStats[]{}));

    IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class);
    when(indicesStatsResponse.getIndices()).thenReturn(indexStats);

    doReturn(indicesStatsResponse).when(elasticsearchIndexManager).getIndicesStatsResponse(elasticsearchClient);

    elasticsearchIndexManager.runIndexManagement();

    verify(elasticsearchIndexManager, times(0)).checkIndexRetention(any(Client.class), anySet(), any(IndexMetadata.class), any(DateTime.class));
    verify(elasticsearchIndexManager, times(0)).preCreateIndex(any(Client.class), any(IndexMetadata.class), any(DateTime.class));
}
 
Example #3
Source File: TestElasticsearchIndexManager.java    From Raigad with Apache License 2.0 6 votes vote down vote up
@Test
public void testCheckIndexRetention_Overlapping() throws Exception {
    String serializedIndexMetadata = "[{\"preCreate\": false, \"retentionType\": \"hourly\", \"retentionPeriod\": 2, \"indexName\": \"nf_errors_log\"}," +
            "{\"preCreate\": false, \"retentionType\": \"yearly\", \"retentionPeriod\": 3, \"indexName\": \"nf_errors_log201712\"}]";
    List<IndexMetadata> indexMetadataList = IndexUtils.parseIndexMetadata(serializedIndexMetadata);

    Map<String, IndexStats> indexStats = new HashMap<>();
    indexStats.put("nf_errors_log2017121110", new IndexStats("nf_errors_log2017121110", new ShardStats[]{}));
    indexStats.put("nf_errors_log2017121111", new IndexStats("nf_errors_log2017121111", new ShardStats[]{}));
    indexStats.put("nf_errors_log2017121112", new IndexStats("nf_errors_log2017121112", new ShardStats[]{}));
    indexStats.put("nf_errors_log2017121113", new IndexStats("nf_errors_log2017121113", new ShardStats[]{}));
    indexStats.put("nf_errors_log2017121114", new IndexStats("nf_errors_log2017121114", new ShardStats[]{}));

    IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class);
    when(indicesStatsResponse.getIndices()).thenReturn(indexStats);

    doReturn(indicesStatsResponse).when(elasticsearchIndexManager).getIndicesStatsResponse(elasticsearchClient);

    elasticsearchIndexManager.runIndexManagement(elasticsearchClient, indexMetadataList, new DateTime("2017-12-11T13:30Z"));

    verify(elasticsearchIndexManager, times(2)).checkIndexRetention(any(Client.class), anySet(), any(IndexMetadata.class), any(DateTime.class));
}
 
Example #4
Source File: ElasticsearchIndexManager.java    From Raigad with Apache License 2.0 6 votes vote down vote up
void preCreateIndex(Client client, IndexMetadata indexMetadata, DateTime dateTime) throws UnsupportedAutoIndexException {
    logger.info("Pre-creating indices for {}*", indexMetadata.getIndexNamePattern());

    IndicesStatsResponse indicesStatsResponse = getIndicesStatsResponse(client);
    Map<String, IndexStats> indexStatsMap = indicesStatsResponse.getIndices();

    if (indexStatsMap == null || indexStatsMap.isEmpty()) {
        logger.info("No existing indices, no need to pre-create");
        return;
    }

    indexStatsMap.keySet().stream()
        .filter(indexName -> indexMetadata.getIndexNameFilter().filter(indexName))
        .findFirst()
        .ifPresent(indexName -> {
            try {
                createIndex(client, indexMetadata.getIndexNameToPreCreate(dateTime));
            } catch (UnsupportedAutoIndexException e) {
                logger.error("Invalid index metadata: " + indexMetadata.toString(), e);
            }
        });
}
 
Example #5
Source File: RestShardsAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void doRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    clusterStateRequest.clear().nodes(true).metaData(true).routingTable(true).indices(indices);
    client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {
        @Override
        public void processResponse(final ClusterStateResponse clusterStateResponse) {
            IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest(indices);
            indicesStatsRequest.all();
            client.admin().indices().stats(indicesStatsRequest, new RestResponseListener<IndicesStatsResponse>(channel) {
                @Override
                public RestResponse buildResponse(IndicesStatsResponse indicesStatsResponse) throws Exception {
                    return RestTable.buildResponse(buildTable(request, clusterStateResponse, indicesStatsResponse), channel);
                }
            });
        }
    });
}
 
Example #6
Source File: ElasticsearchQueryStore.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
@Timed
public void cleanup(Set<String> tables) {
    List<String> indicesToDelete = new ArrayList<>();
    try {
        IndicesStatsResponse response = connection.getClient()
                .admin()
                .indices()
                .prepareStats()
                .execute()
                .actionGet();
        Set<String> currentIndices = response.getIndices()
                .keySet();
        indicesToDelete = getIndicesToDelete(tables, currentIndices);
        deleteIndices(indicesToDelete);
    } catch (Exception e) {
        throw FoxtrotExceptions.createDataCleanupException(String.format("Index Deletion Failed indexes - %s", indicesToDelete), e);
    }
}
 
Example #7
Source File: ElasticsearchTransactionRepository.java    From servicecomb-pack with Apache License 2.0 6 votes vote down vote up
@Override
public List<GlobalTransaction> getSlowGlobalTransactionsTopN(int n) {
  // ElasticsearchTemplate.prepareScroll() does not add sorting https://jira.spring.io/browse/DATAES-457
  ObjectMapper jsonMapper = new ObjectMapper();
  List<GlobalTransaction> globalTransactions = new ArrayList();
  IndicesStatsResponse indicesStatsResponse = this.template.getClient().admin().indices().prepareStats(INDEX_NAME).get();
  if(indicesStatsResponse.getIndices().get(INDEX_NAME).getTotal().docs.getCount()>0){
    SearchResponse response = this.template.getClient().prepareSearch(INDEX_NAME)
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setQuery(QueryBuilders.matchAllQuery())
        .addSort(SortBuilders.fieldSort("durationTime").order(SortOrder.DESC))
        .setFrom(0).setSize(n).setExplain(true)
        .get();
    response.getHits().forEach(hit -> {
      try {
        GlobalTransactionDocument dto = jsonMapper
            .readValue(hit.getSourceAsString(), GlobalTransactionDocument.class);
        globalTransactions.add(dto);
      } catch (Exception e) {
        LOG.error(e.getMessage(), e);
      }
    });
  }
  return globalTransactions;
}
 
Example #8
Source File: PrometheusMetricsCollector.java    From elasticsearch-prometheus-exporter with Apache License 2.0 6 votes vote down vote up
private void updatePerIndexMetrics(ClusterHealthResponse chr, IndicesStatsResponse isr) {

        if (chr != null && isr != null) {
            for (Map.Entry<String, IndexStats> entry : isr.getIndices().entrySet()) {
                String indexName = entry.getKey();
                ClusterIndexHealth cih = chr.getIndices().get(indexName);
                catalog.setClusterGauge("index_status", cih.getStatus().value(), indexName);
                catalog.setClusterGauge("index_replicas_number", cih.getNumberOfReplicas(), indexName);
                catalog.setClusterGauge("index_shards_number", cih.getActiveShards(), "active", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getNumberOfShards(), "shards", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getActivePrimaryShards(), "active_primary", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getInitializingShards(), "initializing", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getRelocatingShards(), "relocating", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getUnassignedShards(), "unassigned", indexName);
                IndexStats indexStats = entry.getValue();
                updatePerIndexContextMetrics(indexName, "total", indexStats.getTotal());
                updatePerIndexContextMetrics(indexName, "primaries", indexStats.getPrimaries());
            }
        }
    }
 
Example #9
Source File: PrometheusMetricsCollector.java    From elasticsearch-prometheus-exporter with Apache License 2.0 6 votes vote down vote up
public void updateMetrics(ClusterHealthResponse clusterHealthResponse, NodeStats nodeStats,
                          IndicesStatsResponse indicesStats, ClusterStatsData clusterStatsData) {
    Summary.Timer timer = catalog.startSummaryTimer("metrics_generate_time_seconds");

    updateClusterMetrics(clusterHealthResponse);
    updateNodeMetrics(nodeStats);
    updateIndicesMetrics(nodeStats.getIndices());
    if (isPrometheusIndices) {
        updatePerIndexMetrics(clusterHealthResponse, indicesStats);
    }
    updateTransportMetrics(nodeStats.getTransport());
    updateHTTPMetrics(nodeStats.getHttp());
    updateThreadPoolMetrics(nodeStats.getThreadPool());
    updateIngestMetrics(nodeStats.getIngestStats());
    updateCircuitBreakersMetrics(nodeStats.getBreaker());
    updateScriptMetrics(nodeStats.getScriptStats());
    updateProcessMetrics(nodeStats.getProcess());
    updateJVMMetrics(nodeStats.getJvm());
    updateOsMetrics(nodeStats.getOs());
    updateFsMetrics(nodeStats.getFs());
    if (isPrometheusClusterSettings) {
        updateESSettings(clusterStatsData);
    }

    timer.observeDuration();
}
 
Example #10
Source File: ElasticsearchQueryStoreTest.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndicesStats() throws FoxtrotException, ExecutionException, InterruptedException {
    List<Document> documents = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        documents.add(createDummyDocument());
    }
    doReturn(documents).when(dataStore)
            .saveAll(any(Table.class), anyListOf(Document.class));

    queryStore.save(TestUtils.TEST_TABLE_NAME, documents);
    elasticsearchConnection.refresh(ElasticsearchUtils.getIndices(TestUtils.TEST_TABLE_NAME));
    IndicesStatsResponse clusterHealth = queryStore.getIndicesStats();
    assertEquals(10, clusterHealth.getPrimaries()
            .getDocs()
            .getCount());
    assertNotEquals(0, clusterHealth.getTotal()
            .getStore()
            .getSizeInBytes());
    assertNotEquals(0, clusterHealth.getPrimaries()
            .getStore()
            .getSizeInBytes());
}
 
Example #11
Source File: ESConnector.java    From Siamese with GNU General Public License v3.0 6 votes vote down vote up
public long getIndicesStats(String indexName) {
        IndicesStatsResponse indicesStatsResponse = client.admin().indices().prepareStats(indexName)
                .all()
                .execute().actionGet();

        XContentBuilder builder = null;
        try {
            builder = XContentFactory.jsonBuilder();
            builder.startObject();
            indicesStatsResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
            builder.endObject();
            String jsonResponse = builder.prettyPrint().string();

            JsonParser jsonParser = new JsonParser(); // from import com.google.gson.JsonParser;
            Long docCount = jsonParser.parse(jsonResponse)
                    .getAsJsonObject().get("_all")
                    .getAsJsonObject().get("primaries")
                    .getAsJsonObject().get("docs")
                    .getAsJsonObject().get("count").getAsLong();
//            System.out.println(docCount);
            return docCount;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }
 
Example #12
Source File: EsEntityIndexImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
private long getIndexSize(){
    long indexSize = 0L;
    final String indexName = indexLocationStrategy.getIndexInitialName();
    try {
        final IndicesStatsResponse statsResponse = esProvider.getClient()
            .admin()
            .indices()
            .prepareStats(indexName)
            .all()
            .execute()
            .actionGet();
        final CommonStats indexStats = statsResponse.getIndex(indexName).getTotal();
        indexSize = indexStats.getStore().getSizeInBytes();
    } catch (IndexMissingException e) {
        // if for some reason the index size does not exist,
        // log an error and we can assume size is 0 as it doesn't exist
        logger.error("Unable to get size for index {} due to IndexMissingException for app {}",
            indexName, indexLocationStrategy.getApplicationScope().getApplication().getUuid());
    }
    return indexSize;
}
 
Example #13
Source File: TransportNodePrometheusMetricsAction.java    From elasticsearch-prometheus-exporter with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(IndicesStatsResponse response) {
    indicesStatsResponse = response;
    if (isPrometheusClusterSettings) {
        client.admin().cluster().state(clusterStateRequest, clusterStateResponseActionListener);
    } else {
        gatherRequests();
    }
}
 
Example #14
Source File: TestElasticsearchIndexManager.java    From Raigad with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunIndexManagement() throws Exception {
    String serializedIndexMetadata = "[{\"retentionType\": \"yearly\", \"retentionPeriod\": 3, \"indexName\": \"nf_errors_log\"}]";
    when(config.getIndexMetadata()).thenReturn(serializedIndexMetadata);

    Map<String, IndexStats> indexStats = new HashMap<>();
    indexStats.put("nf_errors_log2018", new IndexStats("nf_errors_log2018", new ShardStats[]{}));
    indexStats.put("nf_errors_log2017", new IndexStats("nf_errors_log2017", new ShardStats[]{}));
    indexStats.put("nf_errors_log2016", new IndexStats("nf_errors_log2016", new ShardStats[]{}));
    indexStats.put("nf_errors_log2015", new IndexStats("nf_errors_log2015", new ShardStats[]{}));
    indexStats.put("nf_errors_log2014", new IndexStats("nf_errors_log2014", new ShardStats[]{}));
    indexStats.put("nf_errors_log2013", new IndexStats("nf_errors_log2013", new ShardStats[]{}));
    indexStats.put("nf_errors_log2012", new IndexStats("nf_errors_log2012", new ShardStats[]{}));

    IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class);
    when(indicesStatsResponse.getIndices()).thenReturn(indexStats);

    doReturn(indicesStatsResponse).when(elasticsearchIndexManager).getIndicesStatsResponse(elasticsearchClient);

    elasticsearchIndexManager.runIndexManagement();

    verify(elasticsearchIndexManager, times(1)).checkIndexRetention(any(Client.class), anySet(), any(IndexMetadata.class), any(DateTime.class));

    verify(elasticsearchIndexManager, times(1)).deleteIndices(any(Client.class), eq("nf_errors_log2012"), eq(AUTO_CREATE_INDEX_TIMEOUT));
    verify(elasticsearchIndexManager, times(1)).deleteIndices(any(Client.class), eq("nf_errors_log2013"), eq(AUTO_CREATE_INDEX_TIMEOUT));

    verify(elasticsearchIndexManager, times(0)).preCreateIndex(any(Client.class), any(IndexMetadata.class), any(DateTime.class));
}
 
Example #15
Source File: ElasticsearchIndexManager.java    From Raigad with Apache License 2.0 5 votes vote down vote up
void runIndexManagement(Client esTransportClient, List<IndexMetadata> indexMetadataList, DateTime dateTime) {
    // Find all the indices
    IndicesStatsResponse indicesStatsResponse = getIndicesStatsResponse(esTransportClient);
    Map<String, IndexStats> indexStatsMap = indicesStatsResponse.getIndices();

    if (indexStatsMap == null || indexStatsMap.isEmpty()) {
        logger.info("Cluster is empty, no indices found");
        return;
    }

    for (IndexMetadata indexMetadata : indexMetadataList) {
        if (!indexMetadata.isActionable()) {
            logger.warn(String.format("Index metadata %s is not actionable, skipping", indexMetadata));
            continue;
        }

        try {
            checkIndexRetention(esTransportClient, indexStatsMap.keySet(), indexMetadata, dateTime);

            if (indexMetadata.isPreCreate()) {
                preCreateIndex(esTransportClient, indexMetadata, dateTime);
            }
        } catch (Exception e) {
            logger.error("Caught an exception while building index metadata information from configuration property", e);
            return;
        }
    }
}
 
Example #16
Source File: ElasticsearchQueryStore.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
@Override
public IndicesStatsResponse getIndicesStats() throws ExecutionException, InterruptedException {
    return connection.getClient()
            .admin()
            .indices()
            .prepareStats(ElasticsearchUtils.getAllIndicesPattern())
            .clear()
            .setDocs(true)
            .setStore(true)
            .execute()
            .get();
}
 
Example #17
Source File: NodePrometheusMetricsResponse.java    From elasticsearch-prometheus-exporter with Apache License 2.0 5 votes vote down vote up
public NodePrometheusMetricsResponse(ClusterHealthResponse clusterHealth, NodeStats nodesStats,
                                     @Nullable IndicesStatsResponse indicesStats,
                                     @Nullable ClusterStateResponse clusterStateResponse,
                                     Settings settings,
                                     ClusterSettings clusterSettings) {
    this.clusterHealth = clusterHealth;
    this.nodeStats = nodesStats;
    this.indicesStats = indicesStats;
    if (clusterStateResponse != null) {
        this.clusterStatsData = new ClusterStatsData(clusterStateResponse, settings, clusterSettings);
    }
}
 
Example #18
Source File: TransportNodePrometheusMetricsAction.java    From elasticsearch-prometheus-exporter with Apache License 2.0 5 votes vote down vote up
protected NodePrometheusMetricsResponse buildResponse(ClusterHealthResponse clusterHealth,
                                                      NodesStatsResponse nodesStats,
                                                      @Nullable IndicesStatsResponse indicesStats,
                                                      @Nullable ClusterStateResponse clusterStateResponse) {
    NodePrometheusMetricsResponse response = new NodePrometheusMetricsResponse(clusterHealth,
            nodesStats.getNodes().get(0), indicesStats, clusterStateResponse,
            settings, clusterSettings);
    if (logger.isTraceEnabled()) {
        logger.trace("Return response: [{}]", response);
    }
    return response;
}
 
Example #19
Source File: InternalClusterInfoService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the latest indices stats, calling the listener when complete
 * @return a latch that can be used to wait for the indices stats to complete if desired
 */
protected CountDownLatch updateIndicesStats(final ActionListener<IndicesStatsResponse> listener) {
    final CountDownLatch latch = new CountDownLatch(1);
    final IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
    indicesStatsRequest.clear();
    indicesStatsRequest.store(true);

    transportIndicesStatsAction.execute(indicesStatsRequest, new LatchedActionListener<>(listener, latch));
    return latch;
}
 
Example #20
Source File: StatsRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(IndicesStatsRequest request, IndicesStatsResponse response, XContentBuilder builder) throws IOException {
    builder.startObject();
    builder.field(Fields.OK, true);
    buildBroadcastShardsHeader(builder, response);
    response.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    return builder;
}
 
Example #21
Source File: InternalClusterInfoService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the latest indices stats, calling the listener when complete
 * @return a latch that can be used to wait for the indices stats to complete if desired
 */
private CountDownLatch updateIndicesStats(final ActionListener<IndicesStatsResponse> listener) {
    final CountDownLatch latch = new CountDownLatch(1);
    final IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
    indicesStatsRequest.clear();
    indicesStatsRequest.store(true);

    client.admin().indices().stats(indicesStatsRequest, new LatchedActionListener<>(listener, latch));
    return latch;
}
 
Example #22
Source File: TransportResizeAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected void masterOperation(final ResizeRequest resizeRequest, final ClusterState state,
                               final ActionListener<ResizeResponse> listener) {

    // there is no need to fetch docs stats for split but we keep it simple and do it anyway for simplicity of the code
    final String sourceIndex = resizeRequest.getSourceIndex();
    final String targetIndex = resizeRequest.getTargetIndexRequest().index();
    client.admin().indices().prepareStats(sourceIndex).clear().setDocs(true).execute(new ActionListener<>() {
        @Override
        public void onResponse(IndicesStatsResponse indicesStatsResponse) {
            CreateIndexClusterStateUpdateRequest updateRequest = prepareCreateIndexRequest(resizeRequest, state,
                (i) -> {
                    IndexShardStats shard = indicesStatsResponse.getIndex(sourceIndex).getIndexShards().get(i);
                    return shard == null ? null : shard.getPrimary().getDocs();
                }, sourceIndex, targetIndex);
            createIndexService.createIndex(
                updateRequest,
                ActionListener.wrap(response ->
                        listener.onResponse(new ResizeResponse(response.isAcknowledged(), response.isShardsAcknowledged(),
                                updateRequest.index())), listener::onFailure
                )
            );
        }

        @Override
        public void onFailure(Exception e) {
            listener.onFailure(e);
        }
    });

}
 
Example #23
Source File: IndexUtils.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the number of documents in an index.
 *
 * @deprecated
 *
 * @param indexName Name of the index
 * @return The number of documents in an index. 0 is returned if the index does not exist. -1 is returned if the
 * request fails.
 */
@Deprecated
public Long getNumberOfDocumentsInIndex(String indexName) {
    if (!clusterService.state().getRoutingTable().hasIndex(indexName)) {
        return 0L;
    }
    IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
    Optional<IndicesStatsResponse> response = clientUtil.timedRequest(indicesStatsRequest, logger, client.admin().indices()::stats);
    return response.map(r -> r.getIndex(indexName).getPrimaries().docs.getCount()).orElse(-1L);
}
 
Example #24
Source File: MasterDisruptionIT.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testMappingNewFieldsTimeoutDoesntAffectCheckpoints() throws Exception {
    InternalTestCluster internalCluster = internalCluster();
    internalCluster.startNodes(3,
                               Settings.builder()
                                   .put(MappingUpdatedAction.INDICES_MAPPING_DYNAMIC_TIMEOUT_SETTING.getKey(),
                                        "1ms")
                                   .build()
    );
    ensureStableCluster(3);

    logger.info("creating table t with 1 shards and 1 replica");
    execute("create table t (id int primary key, x object(dynamic)) clustered into 1 shards with " +
            "(number_of_replicas = 1, \"routing.allocation.exclude._name\" = '" + internalCluster().getMasterName()
            + "', \"write.wait_for_active_shards\" = 1)");
    ensureGreen();
    execute("insert into t values (?, ?)", new Object[]{1, Map.of("first field", "first value")});

    ServiceDisruptionScheme disruption = new BlockMasterServiceOnMaster(random());
    setDisruptionScheme(disruption);

    disruption.startDisrupting();

    try {
        execute("insert into t values (?, ?), (?, ?), (?, ?)",
                new Object[]{
                    2, Map.of("2nd field", "2nd value"),
                    3, Map.of("3rd field", "3rd value"),
                    4, Map.of("4th field", "4th value"),
                });
    } catch (Exception e) {
        // failure is acceptable
    }

    disruption.stopDisrupting();

    String indexName = toIndexName(sqlExecutor.getCurrentSchema(), "t", null);
    assertBusy(() -> {
        IndicesStatsResponse stats = client().admin().indices().prepareStats(indexName).clear().get();
        for (ShardStats shardStats : stats.getShards()) {
            assertThat(shardStats.getShardRouting().toString(),
                       shardStats.getSeqNoStats().getGlobalCheckpoint(), equalTo(shardStats.getSeqNoStats().getLocalCheckpoint()));
        }
    });
}
 
Example #25
Source File: StatsRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<IndicesStatsResponse> doExecute(IndicesStatsRequest request) {
    return client.admin().indices().stats(request);
}
 
Example #26
Source File: AbstractClient.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<IndicesStatsResponse> stats(final IndicesStatsRequest request) {
    return execute(IndicesStatsAction.INSTANCE, request);
}
 
Example #27
Source File: AbstractClient.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void stats(final IndicesStatsRequest request, final ActionListener<IndicesStatsResponse> listener) {
    execute(IndicesStatsAction.INSTANCE, request, listener);
}
 
Example #28
Source File: FieldDataTermsQueryTest.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
private QueryCacheStats getQueryCacheStats(String index) {
  IndicesStatsResponse statsResponse = client().admin().indices().prepareStats(index).setQueryCache(true).setRefresh(true).get();
  return statsResponse.getIndex(index).getTotal().getQueryCache();
}
 
Example #29
Source File: Elasticsearch5SearchIndexTest.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private long getRefreshCount() {
    IndicesStatsResponse resp = getSearchIndex().getClient().admin().indices().prepareStats().get();
    return resp.getTotal().getRefresh().getTotal();
}
 
Example #30
Source File: Elasticsearch7SearchIndexTest.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private long getRefreshCount() {
    IndicesStatsResponse resp = getSearchIndex().getClient().admin().indices().prepareStats().get();
    return resp.getTotal().getRefresh().getTotal();
}