Skip to content

Commit 4e02456

Browse files
committed
Refactor retention stats so we can reuse them for the failure store
1 parent e0f429a commit 4e02456

File tree

4 files changed

+69
-67
lines changed

4 files changed

+69
-67
lines changed

x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/action/DataStreamLifecycleUsageTransportActionIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class DataStreamLifecycleUsageTransportActionIT extends ESIntegTestCase {
5656
*/
5757
@Override
5858
protected Collection<Class<? extends Plugin>> nodePlugins() {
59-
return List.of(TestDateLifecycleUsagePlugin.class);
59+
return List.of(TestDataLifecycleUsagePlugin.class);
6060
}
6161

6262
@After
@@ -287,7 +287,7 @@ public void clusterStateProcessed(ClusterState oldState, ClusterState newState)
287287
/*
288288
* This plugin exposes the DataLifecycleUsageTransportAction.
289289
*/
290-
public static final class TestDateLifecycleUsagePlugin extends XPackClientPlugin {
290+
public static final class TestDataLifecycleUsagePlugin extends XPackClientPlugin {
291291
@Override
292292
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
293293
List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> actions = new ArrayList<>();

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

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.xpack.core.datastreams.DataStreamLifecycleFeatureSetUsage;
2727

2828
import java.util.Collection;
29-
import java.util.HashMap;
3029
import java.util.LongSummaryStatistics;
3130
import java.util.Map;
3231

@@ -102,11 +101,12 @@ public static DataStreamLifecycleFeatureSetUsage.LifecycleStats calculateStats(
102101
}
103102
}
104103
}
105-
Map<String, DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats> globalRetentionStats = getGlobalRetentionStats(
106-
globalRetention,
107-
dataStreamsWithDefaultRetention,
108-
dataStreamsWithMaxRetention
109-
);
104+
Map<String, DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats> globalRetentionStats =
105+
DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats.getGlobalRetentionStats(
106+
globalRetention,
107+
dataStreamsWithDefaultRetention,
108+
dataStreamsWithMaxRetention
109+
);
110110
return new DataStreamLifecycleFeatureSetUsage.LifecycleStats(
111111
dataStreamsWithLifecycles,
112112
DataStreamLifecycle.CLUSTER_LIFECYCLE_DEFAULT_ROLLOVER_SETTING.getDefault(null).equals(rolloverConfiguration),
@@ -115,31 +115,4 @@ public static DataStreamLifecycleFeatureSetUsage.LifecycleStats calculateStats(
115115
globalRetentionStats
116116
);
117117
}
118-
119-
private static Map<String, DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats> getGlobalRetentionStats(
120-
DataStreamGlobalRetention globalRetention,
121-
long dataStreamsWithDefaultRetention,
122-
long dataStreamsWithMaxRetention
123-
) {
124-
if (globalRetention == null) {
125-
return Map.of();
126-
}
127-
Map<String, DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats> globalRetentionStats = new HashMap<>();
128-
if (globalRetention.defaultRetention() != null) {
129-
globalRetentionStats.put(
130-
DataStreamLifecycleFeatureSetUsage.LifecycleStats.DEFAULT_RETENTION_FIELD_NAME,
131-
new DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats(
132-
dataStreamsWithDefaultRetention,
133-
globalRetention.defaultRetention()
134-
)
135-
);
136-
}
137-
if (globalRetention.maxRetention() != null) {
138-
globalRetentionStats.put(
139-
DataStreamLifecycleFeatureSetUsage.LifecycleStats.MAX_RETENTION_FIELD_NAME,
140-
new DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats(dataStreamsWithMaxRetention, globalRetention.maxRetention())
141-
);
142-
}
143-
return globalRetentionStats;
144-
}
145118
}

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

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.elasticsearch.TransportVersion;
1111
import org.elasticsearch.TransportVersions;
12+
import org.elasticsearch.cluster.metadata.DataStreamGlobalRetention;
1213
import org.elasticsearch.common.Strings;
1314
import org.elasticsearch.common.io.stream.StreamInput;
1415
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -20,6 +21,7 @@
2021
import org.elasticsearch.xpack.core.XPackField;
2122

2223
import java.io.IOException;
24+
import java.util.HashMap;
2325
import java.util.LongSummaryStatistics;
2426
import java.util.Map;
2527
import java.util.Objects;
@@ -183,41 +185,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
183185
builder.field("count", dataStreamsWithLifecyclesCount);
184186
builder.field("default_rollover_used", defaultRolloverUsed);
185187

186-
builder.startObject("data_retention");
187-
builder.field("configured_data_streams", dataRetentionStats.dataStreamCount());
188-
if (dataRetentionStats.dataStreamCount() > 0) {
189-
builder.field("minimum_millis", dataRetentionStats.minMillis);
190-
builder.field("maximum_millis", dataRetentionStats.maxMillis);
191-
builder.field("average_millis", dataRetentionStats.avgMillis);
192-
}
193-
builder.endObject();
194-
195-
builder.startObject("effective_retention");
196-
builder.field("retained_data_streams", effectiveRetentionStats.dataStreamCount());
197-
if (effectiveRetentionStats.dataStreamCount() > 0) {
198-
builder.field("minimum_millis", effectiveRetentionStats.minMillis);
199-
builder.field("maximum_millis", effectiveRetentionStats.maxMillis);
200-
builder.field("average_millis", effectiveRetentionStats.avgMillis);
201-
}
202-
builder.endObject();
188+
RetentionStats.toXContentFragment(builder, dataRetentionStats, false);
189+
RetentionStats.toXContentFragment(builder, effectiveRetentionStats, true);
203190

204191
builder.startObject("global_retention");
205-
globalRetentionStatsToXContent(builder, params, LifecycleStats.DEFAULT_RETENTION_FIELD_NAME);
206-
globalRetentionStatsToXContent(builder, params, LifecycleStats.MAX_RETENTION_FIELD_NAME);
192+
GlobalRetentionStats.toXContentFragment(
193+
builder,
194+
LifecycleStats.DEFAULT_RETENTION_FIELD_NAME,
195+
globalRetentionStats.get(LifecycleStats.DEFAULT_RETENTION_FIELD_NAME)
196+
);
197+
GlobalRetentionStats.toXContentFragment(
198+
builder,
199+
LifecycleStats.MAX_RETENTION_FIELD_NAME,
200+
globalRetentionStats.get(LifecycleStats.MAX_RETENTION_FIELD_NAME)
201+
);
207202
builder.endObject();
208203
return builder;
209204
}
210-
211-
private void globalRetentionStatsToXContent(XContentBuilder builder, Params params, String retentionType) throws IOException {
212-
builder.startObject(retentionType);
213-
GlobalRetentionStats stats = globalRetentionStats.get(retentionType);
214-
builder.field("defined", stats != null);
215-
if (stats != null) {
216-
builder.field("affected_data_streams", stats.dataStreamCount());
217-
builder.field("retention_millis", stats.retention());
218-
}
219-
builder.endObject();
220-
}
221205
}
222206

223207
public record RetentionStats(long dataStreamCount, Double avgMillis, Long minMillis, Long maxMillis) implements Writeable {
@@ -251,6 +235,17 @@ public void writeTo(StreamOutput out) throws IOException {
251235
out.writeVLong(maxMillis);
252236
}
253237
}
238+
239+
static void toXContentFragment(XContentBuilder builder, RetentionStats stats, boolean isEffectiveRetention) throws IOException {
240+
builder.startObject(isEffectiveRetention ? "effective_retention" : "data_retention");
241+
builder.field(isEffectiveRetention ? "retained_data_streams" : "configured_data_streams", stats.dataStreamCount());
242+
if (stats.dataStreamCount() > 0) {
243+
builder.field("minimum_millis", stats.minMillis);
244+
builder.field("maximum_millis", stats.maxMillis);
245+
builder.field("average_millis", stats.avgMillis);
246+
}
247+
builder.endObject();
248+
}
254249
}
255250

256251
public record GlobalRetentionStats(long dataStreamCount, long retention) implements Writeable {
@@ -263,10 +258,44 @@ public GlobalRetentionStats(StreamInput in) throws IOException {
263258
this(in.readVLong(), in.readVLong());
264259
}
265260

261+
public static Map<String, GlobalRetentionStats> getGlobalRetentionStats(
262+
DataStreamGlobalRetention globalRetention,
263+
long dataStreamsWithDefaultRetention,
264+
long dataStreamsWithMaxRetention
265+
) {
266+
if (globalRetention == null) {
267+
return Map.of();
268+
}
269+
Map<String, GlobalRetentionStats> globalRetentionStats = new HashMap<>();
270+
if (globalRetention.defaultRetention() != null) {
271+
globalRetentionStats.put(
272+
LifecycleStats.DEFAULT_RETENTION_FIELD_NAME,
273+
new GlobalRetentionStats(dataStreamsWithDefaultRetention, globalRetention.defaultRetention())
274+
);
275+
}
276+
if (globalRetention.maxRetention() != null) {
277+
globalRetentionStats.put(
278+
LifecycleStats.MAX_RETENTION_FIELD_NAME,
279+
new GlobalRetentionStats(dataStreamsWithMaxRetention, globalRetention.maxRetention())
280+
);
281+
}
282+
return globalRetentionStats;
283+
}
284+
266285
@Override
267286
public void writeTo(StreamOutput out) throws IOException {
268287
out.writeVLong(dataStreamCount);
269288
out.writeVLong(retention);
270289
}
290+
291+
static void toXContentFragment(XContentBuilder builder, String retentionType, GlobalRetentionStats stats) throws IOException {
292+
builder.startObject(retentionType);
293+
builder.field("defined", stats != null);
294+
if (stats != null) {
295+
builder.field("affected_data_streams", stats.dataStreamCount());
296+
builder.field("retention_millis", stats.retention());
297+
}
298+
builder.endObject();
299+
}
271300
}
272301
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/datastreams/DataStreamLifecycleFeatureSetUsageTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ protected DataStreamLifecycleFeatureSetUsage createTestInstance() {
4545
: DataStreamLifecycleFeatureSetUsage.DISABLED;
4646
}
4747

48-
private static DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats generateGlobalRetention() {
48+
static DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats generateGlobalRetention() {
4949
return new DataStreamLifecycleFeatureSetUsage.GlobalRetentionStats(randomNonNegativeLong(), randomNonNegativeLong());
5050
}
5151

52-
private static DataStreamLifecycleFeatureSetUsage.RetentionStats generateRetentionStats() {
52+
static DataStreamLifecycleFeatureSetUsage.RetentionStats generateRetentionStats() {
5353
return randomBoolean()
5454
? DataStreamLifecycleFeatureSetUsage.RetentionStats.NO_DATA
5555
: new DataStreamLifecycleFeatureSetUsage.RetentionStats(

0 commit comments

Comments
 (0)