Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/116128.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 116128
summary: Add num docs and size to logsdb telemetry
area: Logs
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

package org.elasticsearch.monitor.metrics;

import org.elasticsearch.action.admin.cluster.node.stats.IndexModeStatsActionType;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.mapper.OnScriptError;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.indices.IndicesService;
Expand Down Expand Up @@ -329,6 +332,10 @@ public void testIndicesMetrics() {
equalTo(0L)
)
);

verifyStatsPerIndexMode(
Map.of(IndexMode.STANDARD, numStandardDocs, IndexMode.LOGSDB, numLogsdbDocs, IndexMode.TIME_SERIES, numTimeSeriesDocs)
);
}

void collectThenAssertMetrics(TestTelemetryPlugin telemetry, int times, Map<String, Matcher<Long>> matchers) {
Expand Down Expand Up @@ -434,6 +441,16 @@ int populateLogsdbIndices(long numIndices) {
return totalDocs;
}

private void verifyStatsPerIndexMode(Map<IndexMode, Long> expectedDocs) {
var nodes = clusterService().state().nodes().stream().toArray(DiscoveryNode[]::new);
var request = new IndexModeStatsActionType.StatsRequest(nodes);
var resp = client().execute(IndexModeStatsActionType.TYPE, request).actionGet();
var stats = resp.stats();
for (Map.Entry<IndexMode, Long> e : expectedDocs.entrySet()) {
assertThat(stats.get(e.getKey()).numDocs(), equalTo(e.getValue()));
}
}

private Map<String, Object> parseMapping(String mapping) throws IOException {
try (XContentParser parser = createParser(JsonXContent.jsonXContent, mapping)) {
return parser.map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ static TransportVersion def(int id) {
public static final TransportVersion QUERY_RULES_RETRIEVER = def(8_782_00_0);
public static final TransportVersion ESQL_CCS_EXEC_INFO_WITH_FAILURES = def(8_783_00_0);
public static final TransportVersion LOGSDB_TELEMETRY = def(8_784_00_0);
public static final TransportVersion LOGSDB_TELEMETRY_STATS = def(8_785_00_0);

/*
* STOP! READ THIS FIRST! No, really,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.elasticsearch.action.admin.cluster.node.shutdown.PrevalidateNodeRemovalAction;
import org.elasticsearch.action.admin.cluster.node.shutdown.TransportPrevalidateNodeRemovalAction;
import org.elasticsearch.action.admin.cluster.node.shutdown.TransportPrevalidateShardPathAction;
import org.elasticsearch.action.admin.cluster.node.stats.IndexModeStatsActionType;
import org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction;
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.TransportCancelTasksAction;
import org.elasticsearch.action.admin.cluster.node.tasks.get.TransportGetTaskAction;
Expand Down Expand Up @@ -628,6 +629,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void reg
actions.register(TransportNodesFeaturesAction.TYPE, TransportNodesFeaturesAction.class);
actions.register(RemoteClusterNodesAction.TYPE, RemoteClusterNodesAction.TransportAction.class);
actions.register(TransportNodesStatsAction.TYPE, TransportNodesStatsAction.class);
actions.register(IndexModeStatsActionType.TYPE, IndexModeStatsActionType.TransportAction.class);
actions.register(TransportNodesUsageAction.TYPE, TransportNodesUsageAction.class);
actions.register(TransportNodesHotThreadsAction.TYPE, TransportNodesHotThreadsAction.class);
actions.register(TransportListTasksAction.TYPE, TransportListTasksAction.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.action.admin.cluster.node.stats;

import org.apache.lucene.store.AlreadyClosedException;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.FailedNodeException;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.nodes.BaseNodeResponse;
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
import org.elasticsearch.action.support.nodes.BaseNodesResponse;
import org.elasticsearch.action.support.nodes.TransportNodesAction;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.index.shard.IllegalIndexShardStateException;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.injection.guice.Inject;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.transport.TransportService;

import java.io.IOException;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

public final class IndexModeStatsActionType extends ActionType<IndexModeStatsActionType.StatsResponse> {
public static final IndexModeStatsActionType TYPE = new IndexModeStatsActionType();

private IndexModeStatsActionType() {
super("cluster:monitor/nodes/index_mode_stats");
}

public static class StatsRequest extends BaseNodesRequest {
public StatsRequest(String[] nodesIds) {
super(nodesIds);
}

public StatsRequest(DiscoveryNode... concreteNodes) {
super(concreteNodes);
}
}

public static class IndexStats implements Writeable {
private long numDocs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make fields final?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fields need to be non-final for the add method, which combines stats across shards and nodes.

private long sizeInBytes;

public IndexStats() {}

IndexStats(StreamInput in) throws IOException {
this.numDocs = in.readVLong();
this.sizeInBytes = in.readVLong();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(numDocs);
out.writeVLong(sizeInBytes);
}

public long numDocs() {
return numDocs;
}

public long sizeInBytes() {
return sizeInBytes;
}

public void add(IndexStats other) {
this.numDocs += other.numDocs;
this.sizeInBytes += other.sizeInBytes;
}

@Override
public String toString() {
return "IndexStats{" + "numDocs=" + numDocs + ", sizeInBytes=" + sizeInBytes + '}';
}
}

public static class StatsResponse extends BaseNodesResponse<NodeResponse> {
StatsResponse(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) {
super(clusterName, nodes, failures);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
assert false : "must be local";
throw new UnsupportedOperationException("must be local");
}

@Override
protected List<NodeResponse> readNodesFrom(StreamInput in) throws IOException {
assert false : "must be local";
throw new UnsupportedOperationException("must be local");
}

@Override
protected void writeNodesTo(StreamOutput out, List<NodeResponse> nodes) throws IOException {
assert false : "must be local";
throw new UnsupportedOperationException("must be local");
}

public Map<IndexMode, IndexStats> stats() {
final Map<IndexMode, IndexStats> stats = new EnumMap<>(IndexMode.class);
for (IndexMode mode : IndexMode.values()) {
stats.put(mode, new IndexStats());
}
for (NodeResponse node : getNodes()) {
for (Map.Entry<IndexMode, IndexStats> e : node.stats.entrySet()) {
stats.get(e.getKey()).add(e.getValue());
}
}
return stats;
}
}

public static class NodeRequest extends TransportRequest {
NodeRequest() {

}

NodeRequest(StreamInput in) throws IOException {
super(in);
}
}

public static class NodeResponse extends BaseNodeResponse {
private final Map<IndexMode, IndexStats> stats;

NodeResponse(DiscoveryNode node, Map<IndexMode, IndexStats> stats) {
super(node);
this.stats = stats;
}

NodeResponse(StreamInput in, DiscoveryNode node) throws IOException {
super(in, node);
stats = in.readMap(IndexMode::readFrom, IndexStats::new);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeMap(stats, (o, m) -> IndexMode.writeTo(m, o), (o, s) -> s.writeTo(o));
}
}

public static class TransportAction extends TransportNodesAction<StatsRequest, StatsResponse, NodeRequest, NodeResponse, Void> {
private final ClusterService clusterService;
private final IndicesService indicesService;

@Inject
public TransportAction(
ClusterService clusterService,
TransportService transportService,
IndicesService indicesService,
ActionFilters actionFilters
) {
super(
TYPE.name(),
clusterService,
transportService,
actionFilters,
NodeRequest::new,
transportService.getThreadPool().executor(ThreadPool.Names.MANAGEMENT)
);
this.clusterService = clusterService;
this.indicesService = indicesService;
}

@Override
protected StatsResponse newResponse(StatsRequest request, List<NodeResponse> nodeResponses, List<FailedNodeException> failures) {
return new StatsResponse(ClusterName.DEFAULT, nodeResponses, failures);
}

@Override
protected NodeRequest newNodeRequest(StatsRequest request) {
return new NodeRequest();
}

@Override
protected NodeResponse newNodeResponse(StreamInput in, DiscoveryNode node) throws IOException {
return new NodeResponse(in, node);
}

@Override
protected NodeResponse nodeOperation(NodeRequest request, Task task) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think there is an easy way to reuse the logic in IndicesStatsCache#internalGetIndicesStats()? I think it is the same logic.

I suspect we don't want the caching behaviour that IndicesMetrics has (because this should be invoked often), otherwise we could think of reusing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ I pushed 650c7b7

final Map<IndexMode, IndexStats> stats = new EnumMap<>(IndexMode.class);
for (IndexMode mode : IndexMode.values()) {
stats.put(mode, new IndexStats());
}
for (IndexService indexService : indicesService) {
for (IndexShard indexShard : indexService) {
if (indexShard.isSystem()) {
continue; // skip system indices
}
final ShardRouting shardRouting = indexShard.routingEntry();
final IndexMode indexMode = indexShard.indexSettings().getMode();
final IndexStats indexStats = stats.get(indexMode);
try {
if (shardRouting.primary() && shardRouting.recoverySource() == null) {
final DocsStats docStats = indexShard.docStats();
indexStats.numDocs += docStats.getCount();
indexStats.sizeInBytes += docStats.getTotalSizeInBytes();
}
} catch (IllegalIndexShardStateException | AlreadyClosedException ignored) {
// ignored
}
}
}
return new NodeResponse(clusterService.localNode(), stats);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
*/
public class XPackFeatures implements FeatureSpecification {
public static final NodeFeature LOGSDB_TELEMETRY = new NodeFeature("logsdb_telemetry");
public static final NodeFeature LOGSDB_TELMETRY_STATS = new NodeFeature("logsdb_telemetry_stats");

@Override
public Set<NodeFeature> getFeatures() {
return Set.of(
NodesDataTiersUsageTransportAction.LOCALLY_PRECALCULATED_STATS_FEATURE, // Added in 8.12
License.INDEPENDENT_TRIAL_VERSION_FEATURE, // 8.14.0
LOGSDB_TELEMETRY
LOGSDB_TELEMETRY,
LOGSDB_TELMETRY_STATS
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,46 @@
public final class LogsDBFeatureSetUsage extends XPackFeatureUsage {
private final int indicesCount;
private final int indicesWithSyntheticSource;
private final long numDocs;
private final long sizeInBytes;

public LogsDBFeatureSetUsage(StreamInput input) throws IOException {
super(input);
indicesCount = input.readVInt();
indicesWithSyntheticSource = input.readVInt();
if (input.getTransportVersion().onOrAfter(TransportVersions.LOGSDB_TELEMETRY_STATS)) {
numDocs = input.readVLong();
sizeInBytes = input.readVLong();
} else {
numDocs = 0;
sizeInBytes = 0;
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(indicesCount);
out.writeVInt(indicesWithSyntheticSource);
if (out.getTransportVersion().onOrAfter(TransportVersions.LOGSDB_TELEMETRY_STATS)) {
out.writeVLong(numDocs);
out.writeVLong(sizeInBytes);
}
}

public LogsDBFeatureSetUsage(boolean available, boolean enabled, int indicesCount, int indicesWithSyntheticSource) {
public LogsDBFeatureSetUsage(
boolean available,
boolean enabled,
int indicesCount,
int indicesWithSyntheticSource,
long numDocs,
long sizeInBytes
) {
super(XPackField.LOGSDB, available, enabled);
this.indicesCount = indicesCount;
this.indicesWithSyntheticSource = indicesWithSyntheticSource;
this.numDocs = numDocs;
this.sizeInBytes = sizeInBytes;
}

@Override
Expand All @@ -50,11 +72,13 @@ protected void innerXContent(XContentBuilder builder, Params params) throws IOEx
super.innerXContent(builder, params);
builder.field("indices_count", indicesCount);
builder.field("indices_with_synthetic_source", indicesWithSyntheticSource);
builder.field("num_docs", numDocs);
builder.field("size_in_bytes", sizeInBytes);
}

@Override
public int hashCode() {
return Objects.hash(available, enabled, indicesCount, indicesWithSyntheticSource);
return Objects.hash(available, enabled, indicesCount, indicesWithSyntheticSource, numDocs, sizeInBytes);
}

@Override
Expand All @@ -69,6 +93,8 @@ public boolean equals(Object obj) {
return Objects.equals(available, other.available)
&& Objects.equals(enabled, other.enabled)
&& Objects.equals(indicesCount, other.indicesCount)
&& Objects.equals(indicesWithSyntheticSource, other.indicesWithSyntheticSource);
&& Objects.equals(indicesWithSyntheticSource, other.indicesWithSyntheticSource)
&& Objects.equals(numDocs, other.numDocs)
&& Objects.equals(sizeInBytes, other.sizeInBytes);
}
}
Loading