Skip to content

Commit dfa40e6

Browse files
authored
Add usage stats for frozen indices (#44286)
Adds usage stats for frozen indices of the form: "frozen_indices" : { "available" : true, "enabled" : true, "indices_count" : 0 }
1 parent c251450 commit dfa40e6

File tree

9 files changed

+229
-3
lines changed

9 files changed

+229
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.elasticsearch.xpack.core.dataframe.transforms.TimeSyncConfig;
5353
import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction;
5454
import org.elasticsearch.xpack.core.flattened.FlattenedFeatureSetUsage;
55+
import org.elasticsearch.xpack.core.frozen.FrozenIndicesFeatureSetUsage;
5556
import org.elasticsearch.xpack.core.graph.GraphFeatureSetUsage;
5657
import org.elasticsearch.xpack.core.graph.action.GraphExploreAction;
5758
import org.elasticsearch.xpack.core.indexlifecycle.AllocateAction;
@@ -491,7 +492,9 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
491492
// Vectors
492493
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VECTORS, VectorsFeatureSetUsage::new),
493494
// Voting Only Node
494-
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VOTING_ONLY, VotingOnlyNodeFeatureSetUsage::new)
495+
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VOTING_ONLY, VotingOnlyNodeFeatureSetUsage::new),
496+
// Frozen indices
497+
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.FROZEN_INDICES, FrozenIndicesFeatureSetUsage::new)
495498
);
496499
}
497500

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public final class XPackField {
4343
public static final String VECTORS = "vectors";
4444
/** Name constant for the voting-only-node feature. */
4545
public static final String VOTING_ONLY = "voting_only";
46+
/** Name constant for the frozen index feature. */
47+
public static final String FROZEN_INDICES = "frozen_indices";
4648

4749
private XPackField() {}
4850

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
import org.elasticsearch.xpack.core.action.TransportXPackUsageAction;
6464
import org.elasticsearch.xpack.core.action.XPackInfoAction;
6565
import org.elasticsearch.xpack.core.action.XPackUsageAction;
66+
import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction;
6667
import org.elasticsearch.xpack.core.action.XPackUsageResponse;
68+
import org.elasticsearch.xpack.core.frozen.FrozenIndicesUsageTransportAction;
6769
import org.elasticsearch.xpack.core.ml.MlMetadata;
6870
import org.elasticsearch.xpack.core.rest.action.RestFreezeIndexAction;
6971
import org.elasticsearch.xpack.core.rest.action.RestReloadAnalyzersAction;
@@ -251,6 +253,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
251253
actions.add(new ActionHandler<>(XPackUsageAction.INSTANCE, getUsageAction()));
252254
actions.add(new ActionHandler<>(TransportFreezeIndexAction.FreezeIndexAction.INSTANCE,
253255
TransportFreezeIndexAction.class));
256+
actions.add(new ActionHandler<>(XPackUsageFeatureAction.FROZEN_INDICES, FrozenIndicesUsageTransportAction.class));
254257
actions.addAll(licensing.getActions());
255258
actions.add(new ActionHandler<>(ReloadAnalyzerAction.INSTANCE, TransportReloadAnalyzersAction.class));
256259
return actions;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ public class XPackUsageFeatureAction extends StreamableResponseActionType<XPackU
3636
public static final XPackUsageFeatureAction FLATTENED = new XPackUsageFeatureAction(XPackField.FLATTENED);
3737
public static final XPackUsageFeatureAction VECTORS = new XPackUsageFeatureAction(XPackField.VECTORS);
3838
public static final XPackUsageFeatureAction VOTING_ONLY = new XPackUsageFeatureAction(XPackField.VOTING_ONLY);
39+
public static final XPackUsageFeatureAction FROZEN_INDICES = new XPackUsageFeatureAction(XPackField.FROZEN_INDICES);
3940

4041
public static final List<XPackUsageFeatureAction> ALL = Arrays.asList(
4142
SECURITY, MONITORING, WATCHER, GRAPH, MACHINE_LEARNING, LOGSTASH, SQL, ROLLUP, INDEX_LIFECYCLE, CCR, DATA_FRAME, FLATTENED,
42-
VECTORS, VOTING_ONLY
43+
VECTORS, VOTING_ONLY, FROZEN_INDICES
4344
);
4445

4546
private XPackUsageFeatureAction(String name) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
package org.elasticsearch.xpack.core.frozen;
7+
8+
import org.elasticsearch.common.io.stream.StreamInput;
9+
import org.elasticsearch.common.io.stream.StreamOutput;
10+
import org.elasticsearch.common.xcontent.XContentBuilder;
11+
import org.elasticsearch.xpack.core.XPackFeatureSet;
12+
import org.elasticsearch.xpack.core.XPackField;
13+
14+
import java.io.IOException;
15+
import java.util.Objects;
16+
17+
public class FrozenIndicesFeatureSetUsage extends XPackFeatureSet.Usage {
18+
19+
private final int numberOfFrozenIndices;
20+
21+
public FrozenIndicesFeatureSetUsage(StreamInput input) throws IOException {
22+
super(input);
23+
numberOfFrozenIndices = input.readVInt();
24+
}
25+
26+
@Override
27+
public void writeTo(StreamOutput out) throws IOException {
28+
super.writeTo(out);
29+
out.writeVInt(numberOfFrozenIndices);
30+
}
31+
32+
public FrozenIndicesFeatureSetUsage(boolean available, boolean enabled, int numberOfFrozenIndices) {
33+
super(XPackField.FROZEN_INDICES, available, enabled);
34+
this.numberOfFrozenIndices = numberOfFrozenIndices;
35+
}
36+
37+
@Override
38+
protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
39+
super.innerXContent(builder, params);
40+
builder.field("indices_count", numberOfFrozenIndices);
41+
}
42+
43+
public int getNumberOfFrozenIndices() {
44+
return numberOfFrozenIndices;
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return Objects.hash(available, enabled, numberOfFrozenIndices);
50+
}
51+
52+
@Override
53+
public boolean equals(Object obj) {
54+
if (obj == null) {
55+
return false;
56+
}
57+
if (getClass() != obj.getClass()) {
58+
return false;
59+
}
60+
FrozenIndicesFeatureSetUsage other = (FrozenIndicesFeatureSetUsage) obj;
61+
return Objects.equals(available, other.available) &&
62+
Objects.equals(enabled, other.enabled) &&
63+
Objects.equals(numberOfFrozenIndices, other.numberOfFrozenIndices);
64+
}
65+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
package org.elasticsearch.xpack.core.frozen;
7+
8+
import org.elasticsearch.action.ActionListener;
9+
import org.elasticsearch.action.support.ActionFilters;
10+
import org.elasticsearch.cluster.ClusterState;
11+
import org.elasticsearch.cluster.metadata.IndexMetaData;
12+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
13+
import org.elasticsearch.cluster.service.ClusterService;
14+
import org.elasticsearch.common.inject.Inject;
15+
import org.elasticsearch.index.engine.FrozenEngine;
16+
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
17+
import org.elasticsearch.tasks.Task;
18+
import org.elasticsearch.threadpool.ThreadPool;
19+
import org.elasticsearch.transport.TransportService;
20+
import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction;
21+
import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse;
22+
import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction;
23+
24+
public class FrozenIndicesUsageTransportAction extends XPackUsageFeatureTransportAction {
25+
26+
@Inject
27+
public FrozenIndicesUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool,
28+
ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) {
29+
super(XPackUsageFeatureAction.FROZEN_INDICES.name(), transportService, clusterService, threadPool, actionFilters,
30+
indexNameExpressionResolver);
31+
}
32+
33+
@Override
34+
protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state,
35+
ActionListener<XPackUsageFeatureResponse> listener) {
36+
int numFrozenIndices = 0;
37+
for (IndexMetaData indexMetaData : state.metaData()) {
38+
if (FrozenEngine.INDEX_FROZEN.get(indexMetaData.getSettings())) {
39+
numFrozenIndices++;
40+
}
41+
}
42+
listener.onResponse(new XPackUsageFeatureResponse(new FrozenIndicesFeatureSetUsage(true, true, numFrozenIndices)));
43+
}
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
package org.elasticsearch.xpack.core.frozen;
7+
8+
import org.elasticsearch.common.io.stream.Writeable;
9+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
10+
11+
import java.io.IOException;
12+
13+
public class FrozenIndicesFeatureSetUsageTests extends AbstractWireSerializingTestCase<FrozenIndicesFeatureSetUsage> {
14+
15+
@Override
16+
protected FrozenIndicesFeatureSetUsage createTestInstance() {
17+
boolean available = randomBoolean();
18+
boolean enabled = randomBoolean();
19+
return new FrozenIndicesFeatureSetUsage(available, enabled, randomIntBetween(0, 100000));
20+
}
21+
22+
@Override
23+
protected FrozenIndicesFeatureSetUsage mutateInstance(FrozenIndicesFeatureSetUsage instance) throws IOException {
24+
boolean available = instance.available();
25+
boolean enabled = instance.enabled();
26+
int numFrozenIndices = instance.getNumberOfFrozenIndices();
27+
switch (between(0, 2)) {
28+
case 0:
29+
available = available == false;
30+
break;
31+
case 1:
32+
enabled = enabled == false;
33+
break;
34+
case 2:
35+
numFrozenIndices = randomValueOtherThan(numFrozenIndices, () -> randomIntBetween(0, 100000));
36+
break;
37+
default:
38+
throw new AssertionError("Illegal randomisation branch");
39+
}
40+
return new FrozenIndicesFeatureSetUsage(available, enabled, numFrozenIndices);
41+
}
42+
43+
@Override
44+
protected Writeable.Reader<FrozenIndicesFeatureSetUsage> instanceReader() {
45+
return FrozenIndicesFeatureSetUsage::new;
46+
}
47+
48+
}

x-pack/plugin/src/test/resources/rest-api-spec/test/indices.freeze/20_stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ setup:
5151

5252
# unfreeze index
5353
- do:
54-
indices.freeze:
54+
indices.unfreeze:
5555
index: test
5656
- is_true: acknowledged
5757

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
setup:
3+
- do:
4+
indices.create:
5+
index: test
6+
- do:
7+
cluster.health:
8+
wait_for_no_initializing_shards: true
9+
10+
---
11+
"Usage stats on frozen indices":
12+
- skip:
13+
version: " - 7.9.99"
14+
reason: "frozen indices have usage stats starting in version 8.0.0"
15+
16+
- do:
17+
index:
18+
index: test
19+
id: 1
20+
body: { "foo": "bar" }
21+
22+
- do:
23+
index:
24+
index: test
25+
id: 2
26+
body: { "foo": "bar" }
27+
28+
- do:
29+
index:
30+
index: test
31+
id: 3
32+
body: { "foo": "bar" }
33+
34+
- do: {xpack.usage: {}}
35+
- match: { frozen_indices.available: true }
36+
- match: { frozen_indices.enabled: true }
37+
- match: { frozen_indices.indices_count: 0 }
38+
39+
# freeze index
40+
- do:
41+
indices.freeze:
42+
index: test
43+
- is_true: acknowledged
44+
45+
46+
- do: {xpack.usage: {}}
47+
- match: { frozen_indices.available: true }
48+
- match: { frozen_indices.enabled: true }
49+
- match: { frozen_indices.indices_count: 1 }
50+
51+
# unfreeze index
52+
- do:
53+
indices.unfreeze:
54+
index: test
55+
- is_true: acknowledged
56+
57+
- do: {xpack.usage: {}}
58+
- match: { frozen_indices.available: true }
59+
- match: { frozen_indices.enabled: true }
60+
- match: { frozen_indices.indices_count: 0 }

0 commit comments

Comments
 (0)