Skip to content

Commit 89446a7

Browse files
authored
Fix BWC issues for x_pack/usage (#55181)
Fixes several BWC issues in x_pack/usage discovered after introduction of the bwc test. Relates to #54847
1 parent cc09e24 commit 89446a7

File tree

14 files changed

+351
-55
lines changed

14 files changed

+351
-55
lines changed

x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/action/AnalyticsUsageTransportAction.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121
import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse;
2222
import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction;
2323
import org.elasticsearch.xpack.core.analytics.AnalyticsFeatureSetUsage;
24-
import org.elasticsearch.xpack.core.analytics.EnumCounters;
2524
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction;
2625

2726
import java.util.Collections;
28-
import java.util.List;
29-
import java.util.stream.Collectors;
3027

3128
public class AnalyticsUsageTransportAction extends XPackUsageFeatureTransportAction {
3229
private final XPackLicenseState licenseState;
@@ -50,20 +47,12 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat
5047
AnalyticsStatsAction.Request statsRequest = new AnalyticsStatsAction.Request();
5148
statsRequest.setParentTask(clusterService.localNode().getId(), task.getId());
5249
client.execute(AnalyticsStatsAction.INSTANCE, statsRequest, ActionListener.wrap(r ->
53-
listener.onResponse(new XPackUsageFeatureResponse(usageFeatureResponse(true, true, r))),
50+
listener.onResponse(new XPackUsageFeatureResponse(new AnalyticsFeatureSetUsage(true, true, r))),
5451
listener::onFailure));
5552
} else {
56-
AnalyticsFeatureSetUsage usage = new AnalyticsFeatureSetUsage(false, true, Collections.emptyMap());
53+
AnalyticsFeatureSetUsage usage = new AnalyticsFeatureSetUsage(false, true,
54+
new AnalyticsStatsAction.Response(state.getClusterName(), Collections.emptyList(), Collections.emptyList()));
5755
listener.onResponse(new XPackUsageFeatureResponse(usage));
5856
}
5957
}
60-
61-
static AnalyticsFeatureSetUsage usageFeatureResponse(boolean available, boolean enabled, AnalyticsStatsAction.Response r) {
62-
List<EnumCounters<AnalyticsStatsAction.Item>> countersPerNode = r.getNodes()
63-
.stream()
64-
.map(AnalyticsStatsAction.NodeResponse::getStats)
65-
.collect(Collectors.toList());
66-
EnumCounters<AnalyticsStatsAction.Item> mergedCounters = EnumCounters.merge(AnalyticsStatsAction.Item.class, countersPerNode);
67-
return new AnalyticsFeatureSetUsage(available, enabled, mergedCounters.toMap());
68-
}
6958
}

x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/action/AnalyticsInfoTransportActionTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.action.support.PlainActionFuture;
1111
import org.elasticsearch.client.Client;
1212
import org.elasticsearch.cluster.ClusterName;
13+
import org.elasticsearch.cluster.ClusterState;
1314
import org.elasticsearch.cluster.node.DiscoveryNode;
1415
import org.elasticsearch.cluster.service.ClusterService;
1516
import org.elasticsearch.common.io.stream.BytesStreamOutput;
@@ -42,6 +43,7 @@ public class AnalyticsInfoTransportActionTests extends ESTestCase {
4243
private Task task;
4344
private ClusterService clusterService;
4445
private ClusterName clusterName;
46+
private ClusterState clusterState;
4547

4648
@Before
4749
public void init() {
@@ -52,7 +54,9 @@ public void init() {
5254
DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
5355
when(discoveryNode.getId()).thenReturn(randomAlphaOfLength(10));
5456
when(clusterService.localNode()).thenReturn(discoveryNode);
55-
clusterName = mock(ClusterName.class);
57+
clusterName = new ClusterName(randomAlphaOfLength(10));
58+
clusterState = mock(ClusterState.class);
59+
when(clusterState.getClusterName()).thenReturn(clusterName);
5660
}
5761

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

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

x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/action/TransportAnalyticsStatsActionTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static java.util.Collections.emptyList;
3535
import static java.util.stream.Collectors.toList;
3636
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
37-
import static org.elasticsearch.xpack.analytics.action.AnalyticsUsageTransportAction.usageFeatureResponse;
3837
import static org.hamcrest.Matchers.equalTo;
3938
import static org.mockito.Mockito.mock;
4039
import static org.mockito.Mockito.when;
@@ -83,7 +82,7 @@ private ObjectPath run(AnalyticsUsage... nodeUsages) throws IOException {
8382
AnalyticsStatsAction.Response response = new AnalyticsStatsAction.Response(
8483
new ClusterName("cluster_name"), nodeResponses, emptyList());
8584

86-
AnalyticsFeatureSetUsage usage = usageFeatureResponse(true, true, response);
85+
AnalyticsFeatureSetUsage usage = new AnalyticsFeatureSetUsage(true, true, response);
8786
try (XContentBuilder builder = jsonBuilder()) {
8887
usage.toXContent(builder, ToXContent.EMPTY_PARAMS);
8988
return ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder));

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import org.elasticsearch.xpack.core.analytics.AnalyticsFeatureSetUsage;
3737
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata;
3838
import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction;
39+
import org.elasticsearch.xpack.core.enrich.EnrichFeatureSetUsage;
3940
import org.elasticsearch.xpack.core.eql.EqlFeatureSetUsage;
41+
import org.elasticsearch.xpack.core.flattened.FlattenedFeatureSetUsage;
4042
import org.elasticsearch.xpack.core.frozen.FrozenIndicesFeatureSetUsage;
4143
import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction;
4244
import org.elasticsearch.xpack.core.graph.GraphFeatureSetUsage;
@@ -484,6 +486,8 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
484486
new NamedWriteableRegistry.Entry(Task.Status.class, TransformField.TASK_NAME, TransformState::new),
485487
new NamedWriteableRegistry.Entry(PersistentTaskState.class, TransformField.TASK_NAME, TransformState::new),
486488
new NamedWriteableRegistry.Entry(SyncConfig.class, TransformField.TIME_BASED_SYNC.getPreferredName(), TimeSyncConfig::new),
489+
// Flattened for backward compatibility with 7.x
490+
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.FLATTENED, FlattenedFeatureSetUsage::new),
487491
// Vectors
488492
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VECTORS, VectorsFeatureSetUsage::new),
489493
// Voting Only Node
@@ -493,7 +497,9 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
493497
// Spatial
494498
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SPATIAL, SpatialFeatureSetUsage::new),
495499
// Analytics
496-
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ANALYTICS, AnalyticsFeatureSetUsage::new)
500+
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ANALYTICS, AnalyticsFeatureSetUsage::new),
501+
// Enrich
502+
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ENRICH, EnrichFeatureSetUsage::new)
497503
);
498504
}
499505

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackField.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ public final class XPackField {
4141
public static final String CCR = "ccr";
4242
/** Name constant for the transform feature. */
4343
public static final String TRANSFORM = "transform";
44-
/** Name constant for flattened fields. */
44+
/** Name constant for flattened fields.
45+
*
46+
* @deprecated used for Backward Compatibility with 7.x only
47+
*/
48+
@Deprecated
49+
public static final String FLATTENED = "flattened";
4550
/** Name constant for the vectors feature. */
4651
public static final String VECTORS = "vectors";
4752
/** Name constant for the voting-only-node feature. */

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageFeatureAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class XPackUsageFeatureAction extends ActionType<XPackUsageFeatureRespons
4040
public static final XPackUsageFeatureAction FROZEN_INDICES = new XPackUsageFeatureAction(XPackField.FROZEN_INDICES);
4141
public static final XPackUsageFeatureAction SPATIAL = new XPackUsageFeatureAction(XPackField.SPATIAL);
4242
public static final XPackUsageFeatureAction ANALYTICS = new XPackUsageFeatureAction(XPackField.ANALYTICS);
43+
public static final XPackUsageFeatureAction ENRICH = new XPackUsageFeatureAction(XPackField.ENRICH);
4344

4445
public static final List<XPackUsageFeatureAction> ALL = Arrays.asList(
4546
SECURITY, MONITORING, WATCHER, GRAPH, MACHINE_LEARNING, LOGSTASH, EQL, SQL, ROLLUP, INDEX_LIFECYCLE, SNAPSHOT_LIFECYCLE, CCR,

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/analytics/AnalyticsFeatureSetUsage.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,46 @@
1212
import org.elasticsearch.common.xcontent.XContentBuilder;
1313
import org.elasticsearch.xpack.core.XPackFeatureSet;
1414
import org.elasticsearch.xpack.core.XPackField;
15+
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction;
1516

1617
import java.io.IOException;
17-
import java.util.Collections;
18-
import java.util.Map;
1918
import java.util.Objects;
2019

2120
public class AnalyticsFeatureSetUsage extends XPackFeatureSet.Usage {
2221

23-
private final Map<String, Object> stats;
22+
private final AnalyticsStatsAction.Response response;
2423

25-
public AnalyticsFeatureSetUsage(boolean available, boolean enabled, Map<String, Object> stats) {
24+
public AnalyticsFeatureSetUsage(boolean available, boolean enabled, AnalyticsStatsAction.Response response) {
2625
super(XPackField.ANALYTICS, available, enabled);
27-
this.stats = stats;
26+
this.response = response;
2827
}
2928

3029
public AnalyticsFeatureSetUsage(StreamInput input) throws IOException {
3130
super(input);
32-
if (input.getVersion().onOrAfter(Version.V_7_8_0)) {
33-
stats = input.readMap();
34-
} else {
35-
stats = Collections.emptyMap();
36-
}
31+
this.response = new AnalyticsStatsAction.Response(input);
3732
}
3833

3934
@Override
40-
public Version getMinimalSupportedVersion() {
41-
return Version.V_7_4_0;
35+
public int hashCode() {
36+
return Objects.hash(available, enabled, response);
4237
}
4338

4439
@Override
45-
public void writeTo(StreamOutput out) throws IOException {
46-
super.writeTo(out);
47-
if (out.getVersion().onOrAfter(Version.V_7_8_0)) {
48-
out.writeMap(stats);
40+
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
41+
super.innerXContent(builder, params);
42+
if (response != null) {
43+
response.toXContent(builder, params);
4944
}
5045
}
5146

47+
public Version getMinimalSupportedVersion() {
48+
return Version.V_7_4_0;
49+
}
50+
5251
@Override
53-
public int hashCode() {
54-
return Objects.hash(available, enabled, stats);
52+
public void writeTo(StreamOutput out) throws IOException {
53+
super.writeTo(out);
54+
response.writeTo(out);
5555
}
5656

5757
@Override
@@ -63,20 +63,8 @@ public boolean equals(Object obj) {
6363
return false;
6464
}
6565
AnalyticsFeatureSetUsage other = (AnalyticsFeatureSetUsage) obj;
66-
return Objects.equals(available, other.available) &&
67-
Objects.equals(enabled, other.enabled) &&
68-
Objects.equals(stats, other.stats);
69-
}
70-
71-
@Override
72-
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
73-
super.innerXContent(builder, params);
74-
if (enabled) {
75-
builder.startObject("stats");
76-
for (Map.Entry<String, Object> entry : stats.entrySet()) {
77-
builder.field(entry.getKey() + "_usage", entry.getValue());
78-
}
79-
builder.endObject();
80-
}
66+
return Objects.equals(available, other.available)
67+
&& Objects.equals(enabled, other.enabled)
68+
&& Objects.equals(response, other.response);
8169
}
8270
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/analytics/action/AnalyticsStatsAction.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import java.io.IOException;
2525
import java.util.List;
26+
import java.util.Locale;
2627
import java.util.Objects;
28+
import java.util.stream.Collectors;
2729

2830
public class AnalyticsStatsAction extends ActionType<AnalyticsStatsAction.Response> {
2931
public static final AnalyticsStatsAction INSTANCE = new AnalyticsStatsAction();
@@ -89,7 +91,7 @@ public NodeRequest(Request request) {
8991
}
9092
}
9193

92-
public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable {
94+
public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable, ToXContentObject {
9395
public Response(StreamInput in) throws IOException {
9496
super(in);
9597
}
@@ -107,6 +109,25 @@ protected List<NodeResponse> readNodesFrom(StreamInput in) throws IOException {
107109
protected void writeNodesTo(StreamOutput out, List<NodeResponse> nodes) throws IOException {
108110
out.writeList(nodes);
109111
}
112+
113+
public EnumCounters<Item> getStats() {
114+
List<EnumCounters<Item>> countersPerNode = getNodes()
115+
.stream()
116+
.map(AnalyticsStatsAction.NodeResponse::getStats)
117+
.collect(Collectors.toList());
118+
return EnumCounters.merge(Item.class, countersPerNode);
119+
}
120+
121+
@Override
122+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
123+
EnumCounters<Item> stats = getStats();
124+
builder.startObject("stats");
125+
for (Item item : Item.values()) {
126+
builder.field(item.name().toLowerCase(Locale.ROOT) + "_usage", stats.get(item));
127+
}
128+
builder.endObject();
129+
return builder;
130+
}
110131
}
111132

112133
public static class NodeResponse extends BaseNodeResponse {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.enrich;
8+
9+
import org.elasticsearch.Version;
10+
import org.elasticsearch.common.io.stream.StreamInput;
11+
import org.elasticsearch.xpack.core.XPackFeatureSet;
12+
import org.elasticsearch.xpack.core.XPackField;
13+
14+
import java.io.IOException;
15+
16+
public class EnrichFeatureSetUsage extends XPackFeatureSet.Usage {
17+
18+
public EnrichFeatureSetUsage(boolean available, boolean enabled) {
19+
super(XPackField.ENRICH, available, enabled);
20+
}
21+
22+
public EnrichFeatureSetUsage(StreamInput input) throws IOException {
23+
super(input);
24+
}
25+
26+
@Override
27+
public Version getMinimalSupportedVersion() {
28+
return Version.V_7_5_0;
29+
}
30+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.flattened;
8+
9+
import org.elasticsearch.Version;
10+
import org.elasticsearch.common.io.stream.StreamInput;
11+
import org.elasticsearch.common.io.stream.StreamOutput;
12+
import org.elasticsearch.common.xcontent.XContentBuilder;
13+
import org.elasticsearch.xpack.core.XPackFeatureSet;
14+
import org.elasticsearch.xpack.core.XPackField;
15+
16+
import java.io.IOException;
17+
import java.util.Objects;
18+
19+
/**
20+
* @deprecated used for backward compatibility with 7.x only
21+
*/
22+
@Deprecated
23+
public class FlattenedFeatureSetUsage extends XPackFeatureSet.Usage {
24+
private final int fieldCount;
25+
26+
public FlattenedFeatureSetUsage(StreamInput input) throws IOException {
27+
super(input);
28+
this.fieldCount = input.getVersion().onOrAfter(Version.V_7_6_0) ? input.readInt() : 0;
29+
}
30+
31+
public FlattenedFeatureSetUsage(boolean available, boolean enabled, int fieldCount) {
32+
super(XPackField.FLATTENED, available, enabled);
33+
this.fieldCount = fieldCount;
34+
}
35+
36+
int fieldCount() {
37+
return fieldCount;
38+
}
39+
40+
@Override
41+
public Version getMinimalSupportedVersion() {
42+
return Version.V_7_3_0;
43+
}
44+
45+
@Override
46+
public void writeTo(StreamOutput out) throws IOException {
47+
super.writeTo(out);
48+
if (out.getVersion().onOrAfter(Version.V_7_6_0)) {
49+
out.writeInt(fieldCount);
50+
}
51+
}
52+
53+
@Override
54+
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
55+
super.innerXContent(builder, params);
56+
builder.field("field_count", fieldCount);
57+
}
58+
59+
@Override
60+
public boolean equals(Object o) {
61+
if (this == o) return true;
62+
if (o == null || getClass() != o.getClass()) return false;
63+
FlattenedFeatureSetUsage that = (FlattenedFeatureSetUsage) o;
64+
return available == that.available && enabled == that.enabled && fieldCount == that.fieldCount;
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hash(available, enabled, fieldCount);
70+
}
71+
}

0 commit comments

Comments
 (0)