Skip to content

Commit

Permalink
Add xpack usage stats for the spatial plugin (#61946) (#65391)
Browse files Browse the repository at this point in the history
This commit leverages the analytics plugin's aggregation usage
framework and adopts it to the spatial plugin.

This is just a skeleton, as there are no aggregations to
be tracked just yet, but future ones will be introduced and
their usage should be tracked here.
  • Loading branch information
talevy authored Nov 24, 2020
1 parent dcec746 commit 34f4afd
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.xcontent.ContextParser;
import org.elasticsearch.xpack.core.analytics.EnumCounters;
import org.elasticsearch.xpack.core.common.stats.EnumCounters;
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xpack.core.analytics.EnumCounters;
import org.elasticsearch.xpack.core.common.stats.EnumCounters;
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction;

import static org.hamcrest.Matchers.equalTo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.analytics.EnumCounters;
import org.elasticsearch.xpack.core.common.stats.EnumCounters;

import java.io.IOException;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

package org.elasticsearch.xpack.core.analytics;
package org.elasticsearch.xpack.core.common.stats;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,49 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.XPackField;
import org.elasticsearch.xpack.core.spatial.action.SpatialStatsAction;

import java.io.IOException;
import java.util.Objects;

public class SpatialFeatureSetUsage extends XPackFeatureSet.Usage {

public SpatialFeatureSetUsage(boolean available, boolean enabled) {
private final SpatialStatsAction.Response statsResponse;

public SpatialFeatureSetUsage(boolean available, boolean enabled, SpatialStatsAction.Response statsResponse) {
super(XPackField.SPATIAL, available, enabled);
this.statsResponse = statsResponse;
}

public SpatialFeatureSetUsage(StreamInput input) throws IOException {
super(input);
if (input.getVersion().onOrAfter(Version.V_7_11_0)) {
this.statsResponse = new SpatialStatsAction.Response(input);
} else {
this.statsResponse = null;
}
}

@Override
public Version getMinimalSupportedVersion() {
return Version.V_7_4_0;
}

SpatialStatsAction.Response statsResponse() {
return statsResponse;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (out.getVersion().onOrAfter(Version.V_7_11_0)) {
this.statsResponse.writeTo(out);
}
}

@Override
public int hashCode() {
return Objects.hash(available, enabled);
return Objects.hash(available, enabled, statsResponse);
}

@Override
Expand All @@ -49,7 +65,8 @@ public boolean equals(Object obj) {
return false;
}
SpatialFeatureSetUsage other = (SpatialFeatureSetUsage) obj;
return Objects.equals(available, other.available) &&
Objects.equals(enabled, other.enabled);
return Objects.equals(available, other.available)
&& Objects.equals(enabled, other.enabled)
&& Objects.equals(statsResponse, other.statsResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.core.spatial.action;

import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.FailedNodeException;
import org.elasticsearch.action.support.nodes.BaseNodeRequest;
import org.elasticsearch.action.support.nodes.BaseNodeResponse;
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
import org.elasticsearch.action.support.nodes.BaseNodesResponse;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.common.stats.EnumCounters;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

public class SpatialStatsAction extends ActionType<SpatialStatsAction.Response> {
public static final SpatialStatsAction INSTANCE = new SpatialStatsAction();
public static final String NAME = "cluster:monitor/xpack/spatial/stats";

private SpatialStatsAction() {
super(NAME, Response::new);
}

/**
* Items to track. Serialized by ordinals. Append only, don't remove or change order of items in this list.
*/
public enum Item {
}

public static class Request extends BaseNodesRequest<Request> implements ToXContentObject {

public Request() {
super((String[]) null);
}

public Request(StreamInput in) throws IOException {
super(in);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.endObject();
return builder;
}

@Override
public int hashCode() {
// Nothing to hash atm, so just use the action name
return Objects.hashCode(NAME);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
return true;
}
}

public static class NodeRequest extends BaseNodeRequest {
public NodeRequest(StreamInput in) throws IOException {
super(in);
}

public NodeRequest(Request request) {

}
}

public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable, ToXContentObject {
public Response(StreamInput in) throws IOException {
super(in);
}

public Response(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) {
super(clusterName, nodes, failures);
}

@Override
protected List<NodeResponse> readNodesFrom(StreamInput in) throws IOException {
return in.readList(NodeResponse::new);
}

@Override
protected void writeNodesTo(StreamOutput out, List<NodeResponse> nodes) throws IOException {
out.writeList(nodes);
}

public EnumCounters<Item> getStats() {
List<EnumCounters<Item>> countersPerNode = getNodes()
.stream()
.map(SpatialStatsAction.NodeResponse::getStats)
.collect(Collectors.toList());
return EnumCounters.merge(Item.class, countersPerNode);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
EnumCounters<Item> stats = getStats();
builder.startObject("stats");
for (Item item : Item.values()) {
builder.field(item.name().toLowerCase(Locale.ROOT) + "_usage", stats.get(item));
}
builder.endObject();
return builder;
}

@Override
public int hashCode() {
return Objects.hash(getStats());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response other = (Response) o;
return Objects.equals(getStats(), other.getStats());
}
}

public static class NodeResponse extends BaseNodeResponse {
private final EnumCounters<Item> counters;

public NodeResponse(DiscoveryNode node, EnumCounters<Item> counters) {
super(node);
this.counters = counters;
}

public NodeResponse(StreamInput in) throws IOException {
super(in);
counters = new EnumCounters<>(in, Item.class);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
counters.writeTo(out);
}

public EnumCounters<Item> getStats() {
return counters;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NodeResponse that = (NodeResponse) o;
return counters.equals(that.counters) &&
getNode().equals(that.getNode());
}

@Override
public int hashCode() {
return Objects.hash(counters, getNode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

package org.elasticsearch.analytics;
package org.elasticsearch.xpack.core.common.stats;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractWireTestCase;
import org.elasticsearch.xpack.core.analytics.EnumCounters;

import java.io.IOException;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,36 @@
*/
package org.elasticsearch.xpack.core.spatial;

import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xpack.core.common.stats.EnumCounters;
import org.elasticsearch.xpack.core.spatial.action.SpatialStatsAction;

import java.io.IOException;
import java.net.InetAddress;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

public class SpatialFeatureSetUsageTests extends AbstractWireSerializingTestCase<SpatialFeatureSetUsage> {

@Override
protected SpatialFeatureSetUsage createTestInstance() {
boolean available = randomBoolean();
boolean enabled = randomBoolean();
return new SpatialFeatureSetUsage(available, enabled);
SpatialStatsAction.Response statsResponse = randomStatsResponse();
return new SpatialFeatureSetUsage(available, enabled, statsResponse);
}

@Override
protected SpatialFeatureSetUsage mutateInstance(SpatialFeatureSetUsage instance) throws IOException {
boolean available = instance.available();
boolean enabled = instance.enabled();
SpatialStatsAction.Response statsResponse = instance.statsResponse();
switch (between(0, 1)) {
case 0:
available = available == false;
Expand All @@ -33,12 +45,19 @@ protected SpatialFeatureSetUsage mutateInstance(SpatialFeatureSetUsage instance)
default:
throw new AssertionError("Illegal randomisation branch");
}
return new SpatialFeatureSetUsage(available, enabled);
return new SpatialFeatureSetUsage(available, enabled, statsResponse);
}

@Override
protected Writeable.Reader<SpatialFeatureSetUsage> instanceReader() {
return SpatialFeatureSetUsage::new;
}

public static SpatialStatsAction.Response randomStatsResponse() {
DiscoveryNode node = new DiscoveryNode("_node_id",
new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT);
EnumCounters<SpatialStatsAction.Item> counters = new EnumCounters<>(SpatialStatsAction.Item.class);
SpatialStatsAction.NodeResponse nodeResponse = new SpatialStatsAction.NodeResponse(node, counters);
return new SpatialStatsAction.Response(new ClusterName("cluster_name"), singletonList(nodeResponse), emptyList());
}
}
1 change: 1 addition & 0 deletions x-pack/plugin/spatial/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
restResources {
restApi {
includeCore '_common', 'bulk', 'indices', 'index', 'search'
includeXpack 'xpack'
}
restTests {
includeCore 'geo_shape'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
package org.elasticsearch.xpack.spatial;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.XPackField;
import org.elasticsearch.xpack.core.spatial.SpatialFeatureSetUsage;
import org.elasticsearch.xpack.core.spatial.action.SpatialStatsAction;

import java.util.Map;

public class SpatialFeatureSet implements XPackFeatureSet {

private final XPackLicenseState licenseState;
private Client client;

@Inject
public SpatialFeatureSet(Settings settings, @Nullable XPackLicenseState licenseState) {
public SpatialFeatureSet(@Nullable XPackLicenseState licenseState, Client client) {
this.licenseState = licenseState;
this.client = client;
}

@Override
Expand All @@ -46,6 +50,9 @@ public Map<String, Object> nativeCodeInfo() {

@Override
public void usage(ActionListener<XPackFeatureSet.Usage> listener) {
listener.onResponse(new SpatialFeatureSetUsage(available(), enabled()));
SpatialStatsAction.Request request = new SpatialStatsAction.Request();
client.execute(SpatialStatsAction.INSTANCE, request,
ActionListener.wrap(r -> listener.onResponse(new SpatialFeatureSetUsage(available(), enabled(), r)),
listener::onFailure));
}
}
Loading

0 comments on commit 34f4afd

Please sign in to comment.