Skip to content

Commit

Permalink
Fix BWC issues for x_pack/usage (elastic#55181)
Browse files Browse the repository at this point in the history
Fixes several BWC issues in x_pack/usage discovered after introduction of the
bwc test.

Relates to elastic#54847
  • Loading branch information
imotov authored Apr 16, 2020
1 parent cc09e24 commit 89446a7
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction;
import org.elasticsearch.xpack.core.analytics.AnalyticsFeatureSetUsage;
import org.elasticsearch.xpack.core.analytics.EnumCounters;
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class AnalyticsUsageTransportAction extends XPackUsageFeatureTransportAction {
private final XPackLicenseState licenseState;
Expand All @@ -50,20 +47,12 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat
AnalyticsStatsAction.Request statsRequest = new AnalyticsStatsAction.Request();
statsRequest.setParentTask(clusterService.localNode().getId(), task.getId());
client.execute(AnalyticsStatsAction.INSTANCE, statsRequest, ActionListener.wrap(r ->
listener.onResponse(new XPackUsageFeatureResponse(usageFeatureResponse(true, true, r))),
listener.onResponse(new XPackUsageFeatureResponse(new AnalyticsFeatureSetUsage(true, true, r))),
listener::onFailure));
} else {
AnalyticsFeatureSetUsage usage = new AnalyticsFeatureSetUsage(false, true, Collections.emptyMap());
AnalyticsFeatureSetUsage usage = new AnalyticsFeatureSetUsage(false, true,
new AnalyticsStatsAction.Response(state.getClusterName(), Collections.emptyList(), Collections.emptyList()));
listener.onResponse(new XPackUsageFeatureResponse(usage));
}
}

static AnalyticsFeatureSetUsage usageFeatureResponse(boolean available, boolean enabled, AnalyticsStatsAction.Response r) {
List<EnumCounters<AnalyticsStatsAction.Item>> countersPerNode = r.getNodes()
.stream()
.map(AnalyticsStatsAction.NodeResponse::getStats)
.collect(Collectors.toList());
EnumCounters<AnalyticsStatsAction.Item> mergedCounters = EnumCounters.merge(AnalyticsStatsAction.Item.class, countersPerNode);
return new AnalyticsFeatureSetUsage(available, enabled, mergedCounters.toMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class AnalyticsInfoTransportActionTests extends ESTestCase {
private Task task;
private ClusterService clusterService;
private ClusterName clusterName;
private ClusterState clusterState;

@Before
public void init() {
Expand All @@ -52,7 +54,9 @@ public void init() {
DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
when(discoveryNode.getId()).thenReturn(randomAlphaOfLength(10));
when(clusterService.localNode()).thenReturn(discoveryNode);
clusterName = mock(ClusterName.class);
clusterName = new ClusterName(randomAlphaOfLength(10));
clusterState = mock(ClusterState.class);
when(clusterState.getClusterName()).thenReturn(clusterName);
}

public void testAvailable() throws Exception {
Expand All @@ -65,7 +69,7 @@ public void testAvailable() throws Exception {
AnalyticsUsageTransportAction usageAction = new AnalyticsUsageTransportAction(mock(TransportService.class), clusterService, null,
mock(ActionFilters.class), null, licenseState, client);
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
usageAction.masterOperation(task, null, null, future);
usageAction.masterOperation(task, null, clusterState, future);
XPackFeatureSet.Usage usage = future.get().getUsage();
assertThat(usage.available(), is(available));

Expand All @@ -90,7 +94,7 @@ public void testEnabled() throws Exception {
AnalyticsUsageTransportAction usageAction = new AnalyticsUsageTransportAction(mock(TransportService.class),
clusterService, null, mock(ActionFilters.class), null, licenseState, client);
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
usageAction.masterOperation(task, null, null, future);
usageAction.masterOperation(task, null, clusterState, future);
XPackFeatureSet.Usage usage = future.get().getUsage();
assertTrue(usage.enabled());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.analytics.action.AnalyticsUsageTransportAction.usageFeatureResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -83,7 +82,7 @@ private ObjectPath run(AnalyticsUsage... nodeUsages) throws IOException {
AnalyticsStatsAction.Response response = new AnalyticsStatsAction.Response(
new ClusterName("cluster_name"), nodeResponses, emptyList());

AnalyticsFeatureSetUsage usage = usageFeatureResponse(true, true, response);
AnalyticsFeatureSetUsage usage = new AnalyticsFeatureSetUsage(true, true, response);
try (XContentBuilder builder = jsonBuilder()) {
usage.toXContent(builder, ToXContent.EMPTY_PARAMS);
return ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import org.elasticsearch.xpack.core.analytics.AnalyticsFeatureSetUsage;
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata;
import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction;
import org.elasticsearch.xpack.core.enrich.EnrichFeatureSetUsage;
import org.elasticsearch.xpack.core.eql.EqlFeatureSetUsage;
import org.elasticsearch.xpack.core.flattened.FlattenedFeatureSetUsage;
import org.elasticsearch.xpack.core.frozen.FrozenIndicesFeatureSetUsage;
import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction;
import org.elasticsearch.xpack.core.graph.GraphFeatureSetUsage;
Expand Down Expand Up @@ -484,6 +486,8 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
new NamedWriteableRegistry.Entry(Task.Status.class, TransformField.TASK_NAME, TransformState::new),
new NamedWriteableRegistry.Entry(PersistentTaskState.class, TransformField.TASK_NAME, TransformState::new),
new NamedWriteableRegistry.Entry(SyncConfig.class, TransformField.TIME_BASED_SYNC.getPreferredName(), TimeSyncConfig::new),
// Flattened for backward compatibility with 7.x
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.FLATTENED, FlattenedFeatureSetUsage::new),
// Vectors
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VECTORS, VectorsFeatureSetUsage::new),
// Voting Only Node
Expand All @@ -493,7 +497,9 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
// Spatial
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SPATIAL, SpatialFeatureSetUsage::new),
// Analytics
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ANALYTICS, AnalyticsFeatureSetUsage::new)
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ANALYTICS, AnalyticsFeatureSetUsage::new),
// Enrich
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ENRICH, EnrichFeatureSetUsage::new)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public final class XPackField {
public static final String CCR = "ccr";
/** Name constant for the transform feature. */
public static final String TRANSFORM = "transform";
/** Name constant for flattened fields. */
/** Name constant for flattened fields.
*
* @deprecated used for Backward Compatibility with 7.x only
*/
@Deprecated
public static final String FLATTENED = "flattened";
/** Name constant for the vectors feature. */
public static final String VECTORS = "vectors";
/** Name constant for the voting-only-node feature. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class XPackUsageFeatureAction extends ActionType<XPackUsageFeatureRespons
public static final XPackUsageFeatureAction FROZEN_INDICES = new XPackUsageFeatureAction(XPackField.FROZEN_INDICES);
public static final XPackUsageFeatureAction SPATIAL = new XPackUsageFeatureAction(XPackField.SPATIAL);
public static final XPackUsageFeatureAction ANALYTICS = new XPackUsageFeatureAction(XPackField.ANALYTICS);
public static final XPackUsageFeatureAction ENRICH = new XPackUsageFeatureAction(XPackField.ENRICH);

public static final List<XPackUsageFeatureAction> ALL = Arrays.asList(
SECURITY, MONITORING, WATCHER, GRAPH, MACHINE_LEARNING, LOGSTASH, EQL, SQL, ROLLUP, INDEX_LIFECYCLE, SNAPSHOT_LIFECYCLE, CCR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,46 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.XPackField;
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction;

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

public class AnalyticsFeatureSetUsage extends XPackFeatureSet.Usage {

private final Map<String, Object> stats;
private final AnalyticsStatsAction.Response response;

public AnalyticsFeatureSetUsage(boolean available, boolean enabled, Map<String, Object> stats) {
public AnalyticsFeatureSetUsage(boolean available, boolean enabled, AnalyticsStatsAction.Response response) {
super(XPackField.ANALYTICS, available, enabled);
this.stats = stats;
this.response = response;
}

public AnalyticsFeatureSetUsage(StreamInput input) throws IOException {
super(input);
if (input.getVersion().onOrAfter(Version.V_7_8_0)) {
stats = input.readMap();
} else {
stats = Collections.emptyMap();
}
this.response = new AnalyticsStatsAction.Response(input);
}

@Override
public Version getMinimalSupportedVersion() {
return Version.V_7_4_0;
public int hashCode() {
return Objects.hash(available, enabled, response);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (out.getVersion().onOrAfter(Version.V_7_8_0)) {
out.writeMap(stats);
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
super.innerXContent(builder, params);
if (response != null) {
response.toXContent(builder, params);
}
}

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

@Override
public int hashCode() {
return Objects.hash(available, enabled, stats);
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
response.writeTo(out);
}

@Override
Expand All @@ -63,20 +63,8 @@ public boolean equals(Object obj) {
return false;
}
AnalyticsFeatureSetUsage other = (AnalyticsFeatureSetUsage) obj;
return Objects.equals(available, other.available) &&
Objects.equals(enabled, other.enabled) &&
Objects.equals(stats, other.stats);
}

@Override
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
super.innerXContent(builder, params);
if (enabled) {
builder.startObject("stats");
for (Map.Entry<String, Object> entry : stats.entrySet()) {
builder.field(entry.getKey() + "_usage", entry.getValue());
}
builder.endObject();
}
return Objects.equals(available, other.available)
&& Objects.equals(enabled, other.enabled)
&& Objects.equals(response, other.response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

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

public class AnalyticsStatsAction extends ActionType<AnalyticsStatsAction.Response> {
public static final AnalyticsStatsAction INSTANCE = new AnalyticsStatsAction();
Expand Down Expand Up @@ -89,7 +91,7 @@ public NodeRequest(Request request) {
}
}

public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable {
public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable, ToXContentObject {
public Response(StreamInput in) throws IOException {
super(in);
}
Expand All @@ -107,6 +109,25 @@ protected List<NodeResponse> readNodesFrom(StreamInput in) throws IOException {
protected void writeNodesTo(StreamOutput out, List<NodeResponse> nodes) throws IOException {
out.writeList(nodes);
}

public EnumCounters<Item> getStats() {
List<EnumCounters<Item>> countersPerNode = getNodes()
.stream()
.map(AnalyticsStatsAction.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;
}
}

public static class NodeResponse extends BaseNodeResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.enrich;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.XPackField;

import java.io.IOException;

public class EnrichFeatureSetUsage extends XPackFeatureSet.Usage {

public EnrichFeatureSetUsage(boolean available, boolean enabled) {
super(XPackField.ENRICH, available, enabled);
}

public EnrichFeatureSetUsage(StreamInput input) throws IOException {
super(input);
}

@Override
public Version getMinimalSupportedVersion() {
return Version.V_7_5_0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.flattened;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.XPackField;

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

/**
* @deprecated used for backward compatibility with 7.x only
*/
@Deprecated
public class FlattenedFeatureSetUsage extends XPackFeatureSet.Usage {
private final int fieldCount;

public FlattenedFeatureSetUsage(StreamInput input) throws IOException {
super(input);
this.fieldCount = input.getVersion().onOrAfter(Version.V_7_6_0) ? input.readInt() : 0;
}

public FlattenedFeatureSetUsage(boolean available, boolean enabled, int fieldCount) {
super(XPackField.FLATTENED, available, enabled);
this.fieldCount = fieldCount;
}

int fieldCount() {
return fieldCount;
}

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

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (out.getVersion().onOrAfter(Version.V_7_6_0)) {
out.writeInt(fieldCount);
}
}

@Override
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
super.innerXContent(builder, params);
builder.field("field_count", fieldCount);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FlattenedFeatureSetUsage that = (FlattenedFeatureSetUsage) o;
return available == that.available && enabled == that.enabled && fieldCount == that.fieldCount;
}

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

0 comments on commit 89446a7

Please sign in to comment.