|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | +package org.graylog.storage.opensearch3.stats; |
| 18 | + |
| 19 | + |
| 20 | +import org.graylog2.indexer.indices.stats.IndexStatistics; |
| 21 | +import org.graylog2.rest.models.system.indexer.responses.IndexStats.TimeAndTotalStats; |
| 22 | +import org.opensearch.client.opensearch._types.DocStats; |
| 23 | +import org.opensearch.client.opensearch._types.FlushStats; |
| 24 | +import org.opensearch.client.opensearch._types.GetStats; |
| 25 | +import org.opensearch.client.opensearch._types.IndexingStats; |
| 26 | +import org.opensearch.client.opensearch._types.MergesStats; |
| 27 | +import org.opensearch.client.opensearch._types.RefreshStats; |
| 28 | +import org.opensearch.client.opensearch._types.SearchStats; |
| 29 | +import org.opensearch.client.opensearch._types.SegmentsStats; |
| 30 | +import org.opensearch.client.opensearch._types.StoreStats; |
| 31 | +import org.opensearch.client.opensearch.indices.stats.IndexShardStats; |
| 32 | +import org.opensearch.client.opensearch.indices.stats.IndexStats; |
| 33 | +import org.opensearch.client.opensearch.indices.stats.IndicesStats; |
| 34 | +import org.opensearch.client.opensearch.indices.stats.ShardRouting; |
| 35 | + |
| 36 | +import java.util.Comparator; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Locale; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
| 41 | +import java.util.function.Function; |
| 42 | + |
| 43 | +public class IndexStatisticsBuilder { |
| 44 | + |
| 45 | + public IndexStatistics build(final String index, final IndicesStats indicesStats) { |
| 46 | + final IndexStats primaries = indicesStats.primaries(); |
| 47 | + final IndexStats total = indicesStats.total(); |
| 48 | + final Map<String, List<IndexShardStats>> shards = indicesStats.shards(); |
| 49 | + |
| 50 | + return IndexStatistics.create(index, buildIndexStats(primaries), buildIndexStats(total), buildShardRoutings(shards)); |
| 51 | + } |
| 52 | + |
| 53 | + private org.graylog2.rest.models.system.indexer.responses.IndexStats buildIndexStats(final IndexStats stats) { |
| 54 | + |
| 55 | + final Optional<DocStats> docs = Optional.ofNullable(stats.docs()); |
| 56 | + final long docsCount = docs.map(DocStats::count).orElse(0L); |
| 57 | + final long docsDeleted = docs.map(DocStats::deleted).orElse(0L); |
| 58 | + |
| 59 | + final SearchStats search = stats.search(); |
| 60 | + return org.graylog2.rest.models.system.indexer.responses.IndexStats.create( |
| 61 | + createTimeAndTotalStats(stats.flush(), FlushStats::total, FlushStats::totalTimeInMillis), |
| 62 | + createTimeAndTotalStats(stats.get(), GetStats::total, GetStats::timeInMillis), |
| 63 | + createTimeAndTotalStats(stats.indexing(), IndexingStats::indexTotal, IndexingStats::indexTimeInMillis), |
| 64 | + createTimeAndTotalStats(stats.merges(), MergesStats::total, MergesStats::totalTimeInMillis), |
| 65 | + createTimeAndTotalStats(stats.refresh(), RefreshStats::total, RefreshStats::totalTimeInMillis), |
| 66 | + createTimeAndTotalStats(search, SearchStats::queryTotal, SearchStats::queryTimeInMillis), |
| 67 | + createTimeAndTotalStats(search, SearchStats::fetchTotal, SearchStats::fetchTimeInMillis), |
| 68 | + Optional.ofNullable(search).map(SearchStats::openContexts).orElse(0L), |
| 69 | + Optional.ofNullable(stats.store()).map(StoreStats::sizeInBytes).orElse(0L), |
| 70 | + Optional.ofNullable(stats.segments()).map(SegmentsStats::count).orElse(0), |
| 71 | + org.graylog2.rest.models.system.indexer.responses.IndexStats.DocsStats.create(docsCount, docsDeleted) |
| 72 | + ); |
| 73 | + |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + private <T> TimeAndTotalStats createTimeAndTotalStats(final T stats, |
| 78 | + final Function<T, Long> totalMapper, |
| 79 | + final Function<T, Long> totalTimeInMillisMapper |
| 80 | + ) { |
| 81 | + return TimeAndTotalStats.create( |
| 82 | + stats != null ? totalMapper.apply(stats) : 0L, |
| 83 | + stats != null ? totalTimeInMillisMapper.apply(stats) / 1000L : 0L |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + private static List<org.graylog2.rest.models.system.indexer.responses.ShardRouting> buildShardRoutings(Map<String, List<IndexShardStats>> shardRoutings) { |
| 88 | + return shardRoutings.entrySet().stream() |
| 89 | + .map(entry -> { |
| 90 | + final int shardId = Integer.parseInt(entry.getKey()); |
| 91 | + final List<IndexShardStats> shards = entry.getValue(); |
| 92 | + |
| 93 | + return shards.stream().map(shard -> { |
| 94 | + final Optional<ShardRouting> routing = Optional.ofNullable(shard.routing()); |
| 95 | + final String state = routing.map(ShardRouting::state) |
| 96 | + .map(st -> st.jsonValue().toLowerCase(Locale.ENGLISH)) |
| 97 | + .orElse("unknown"); |
| 98 | + |
| 99 | + // Taken from org.elasticsearch.cluster.routing.ShardRouting |
| 100 | + final boolean active = "started".equals(state) || "relocating".equals(state); |
| 101 | + |
| 102 | + final boolean primary = routing.map(ShardRouting::primary).orElse(false); |
| 103 | + final String nodeId = routing.map(ShardRouting::node).orElse("Unknown"); |
| 104 | + |
| 105 | + // Node name and hostname should be filled when necessary (requiring an additional round trip to Elasticsearch) |
| 106 | + final String nodeName = null; |
| 107 | + final String nodeHostname = null; |
| 108 | + |
| 109 | + final String relocatingNode = routing.map(ShardRouting::relocatingNode).orElse(null); |
| 110 | + |
| 111 | + final org.graylog2.rest.models.system.indexer.responses.ShardRouting shardRouting = |
| 112 | + org.graylog2.rest.models.system.indexer.responses.ShardRouting.create( |
| 113 | + shardId, |
| 114 | + state, |
| 115 | + active, |
| 116 | + primary, |
| 117 | + nodeId, |
| 118 | + nodeName, |
| 119 | + nodeHostname, |
| 120 | + relocatingNode |
| 121 | + ); |
| 122 | + return shardRouting; |
| 123 | + }).toList(); |
| 124 | + }).flatMap(List::stream) |
| 125 | + .sorted(Comparator.comparing(org.graylog2.rest.models.system.indexer.responses.ShardRouting::id).thenComparing(org.graylog2.rest.models.system.indexer.responses.ShardRouting::nodeId)) |
| 126 | + .toList(); |
| 127 | + |
| 128 | + } |
| 129 | +} |
0 commit comments