Search in sources :

Example 1 with Flag

use of org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag in project elasticsearch by elastic.

the class IndexStatsIT method testAllFlags.

public void testAllFlags() throws Exception {
    // rely on 1 replica for this tests
    createIndex("test1");
    createIndex("test2");
    ensureGreen();
    client().prepareIndex("test1", "type1", Integer.toString(1)).setSource("field", "value").execute().actionGet();
    client().prepareIndex("test1", "type2", Integer.toString(1)).setSource("field", "value").execute().actionGet();
    client().prepareIndex("test2", "type", Integer.toString(1)).setSource("field", "value").execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    IndicesStatsRequestBuilder builder = client().admin().indices().prepareStats();
    Flag[] values = CommonStatsFlags.Flag.values();
    for (Flag flag : values) {
        set(flag, builder, false);
    }
    IndicesStatsResponse stats = builder.execute().actionGet();
    for (Flag flag : values) {
        if (flag == Flag.Suggest) {
            // suggest flag is unused
            continue;
        }
        assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
        assertThat(isSet(flag, stats.getTotal()), equalTo(false));
    }
    for (Flag flag : values) {
        set(flag, builder, true);
    }
    stats = builder.execute().actionGet();
    for (Flag flag : values) {
        assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
        assertThat(isSet(flag, stats.getTotal()), equalTo(true));
    }
    Random random = random();
    EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
    for (Flag flag : values) {
        if (random.nextBoolean()) {
            flags.add(flag);
        }
    }
    for (Flag flag : values) {
        // clear all
        set(flag, builder, false);
    }
    for (Flag flag : flags) {
        // set the flags
        set(flag, builder, true);
    }
    stats = builder.execute().actionGet();
    for (Flag flag : flags) {
        // check the flags
        assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
        assertThat(isSet(flag, stats.getTotal()), equalTo(true));
    }
    for (Flag flag : EnumSet.complementOf(flags)) {
        // check the complement
        if (flag == Flag.Suggest) {
            // suggest flag is unused
            continue;
        }
        assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
        assertThat(isSet(flag, stats.getTotal()), equalTo(false));
    }
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) IndicesStatsRequestBuilder(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder) Random(java.util.Random) Flag(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag)

Example 2 with Flag

use of org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag in project elasticsearch by elastic.

the class IndicesService method stats.

public NodeIndicesStats stats(boolean includePrevious, CommonStatsFlags flags) {
    CommonStats oldStats = new CommonStats(flags);
    if (includePrevious) {
        Flag[] setFlags = flags.getFlags();
        for (Flag flag : setFlags) {
            switch(flag) {
                case Get:
                    oldStats.get.add(oldShardsStats.getStats);
                    break;
                case Indexing:
                    oldStats.indexing.add(oldShardsStats.indexingStats);
                    break;
                case Search:
                    oldStats.search.add(oldShardsStats.searchStats);
                    break;
                case Merge:
                    oldStats.merge.add(oldShardsStats.mergeStats);
                    break;
                case Refresh:
                    oldStats.refresh.add(oldShardsStats.refreshStats);
                    break;
                case Recovery:
                    oldStats.recoveryStats.add(oldShardsStats.recoveryStats);
                    break;
                case Flush:
                    oldStats.flush.add(oldShardsStats.flushStats);
                    break;
            }
        }
    }
    Map<Index, List<IndexShardStats>> statsByShard = new HashMap<>();
    for (IndexService indexService : this) {
        for (IndexShard indexShard : indexService) {
            try {
                if (indexShard.routingEntry() == null) {
                    continue;
                }
                IndexShardStats indexShardStats = new IndexShardStats(indexShard.shardId(), new ShardStats[] { new ShardStats(indexShard.routingEntry(), indexShard.shardPath(), new CommonStats(indicesQueryCache, indexShard, flags), indexShard.commitStats(), indexShard.seqNoStats()) });
                if (!statsByShard.containsKey(indexService.index())) {
                    statsByShard.put(indexService.index(), arrayAsArrayList(indexShardStats));
                } else {
                    statsByShard.get(indexService.index()).add(indexShardStats);
                }
            } catch (IllegalIndexShardStateException e) {
                // we can safely ignore illegal state on ones that are closing for example
                logger.trace((Supplier<?>) () -> new ParameterizedMessage("{} ignoring shard stats", indexShard.shardId()), e);
            }
        }
    }
    return new NodeIndicesStats(oldStats, statsByShard);
}
Also used : IndexShardStats(org.elasticsearch.action.admin.indices.stats.IndexShardStats) ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) HashMap(java.util.HashMap) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) Index(org.elasticsearch.index.Index) IndexShardStats(org.elasticsearch.action.admin.indices.stats.IndexShardStats) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) Flag(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag) CommonStats(org.elasticsearch.action.admin.indices.stats.CommonStats) CollectionUtils.arrayAsArrayList(org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 3 with Flag

use of org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag in project elasticsearch by elastic.

the class IndexStatsIT method testEncodeDecodeCommonStats.

public void testEncodeDecodeCommonStats() throws IOException {
    CommonStatsFlags flags = new CommonStatsFlags();
    Flag[] values = CommonStatsFlags.Flag.values();
    assertThat(flags.anySet(), equalTo(true));
    for (Flag flag : values) {
        flags.set(flag, false);
    }
    assertThat(flags.anySet(), equalTo(false));
    for (Flag flag : values) {
        flags.set(flag, true);
    }
    assertThat(flags.anySet(), equalTo(true));
    Random random = random();
    flags.set(values[random.nextInt(values.length)], false);
    assertThat(flags.anySet(), equalTo(true));
    {
        BytesStreamOutput out = new BytesStreamOutput();
        flags.writeTo(out);
        out.close();
        BytesReference bytes = out.bytes();
        CommonStatsFlags readStats = new CommonStatsFlags(bytes.streamInput());
        for (Flag flag : values) {
            assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
        }
    }
    {
        for (Flag flag : values) {
            flags.set(flag, random.nextBoolean());
        }
        BytesStreamOutput out = new BytesStreamOutput();
        flags.writeTo(out);
        out.close();
        BytesReference bytes = out.bytes();
        CommonStatsFlags readStats = new CommonStatsFlags(bytes.streamInput());
        for (Flag flag : values) {
            assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Random(java.util.Random) CommonStatsFlags(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags) Flag(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

Flag (org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag)3 Random (java.util.Random)2 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Supplier (java.util.function.Supplier)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 CommonStats (org.elasticsearch.action.admin.indices.stats.CommonStats)1 CommonStatsFlags (org.elasticsearch.action.admin.indices.stats.CommonStatsFlags)1 IndexShardStats (org.elasticsearch.action.admin.indices.stats.IndexShardStats)1 IndicesStatsRequestBuilder (org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder)1 IndicesStatsResponse (org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse)1 ShardStats (org.elasticsearch.action.admin.indices.stats.ShardStats)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 CollectionUtils.arrayAsArrayList (org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList)1 Index (org.elasticsearch.index.Index)1 IndexService (org.elasticsearch.index.IndexService)1 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)1