From 4420ae094e98ce4eb54203107cb2837059a35b1a Mon Sep 17 00:00:00 2001 From: Mary Gouseti Date: Thu, 8 Aug 2024 12:27:51 +0300 Subject: [PATCH] Convert data stream global retention to record (#111704) In this refactoring PR we convert the global retention class to a record. --- .../GetDataStreamsTransportActionTests.java | 4 +- .../metadata/DataStreamGlobalRetention.java | 39 +------------------ .../cluster/metadata/DataStreamLifecycle.java | 10 ++--- ...ataStreamGlobalRetentionProviderTests.java | 4 +- .../DataStreamGlobalRetentionTests.java | 4 +- .../metadata/DataStreamLifecycleTests.java | 2 +- ...amLifecycleWithRetentionWarningsTests.java | 2 +- .../cluster/metadata/DataStreamTests.java | 3 +- 8 files changed, 16 insertions(+), 52 deletions(-) diff --git a/modules/data-streams/src/test/java/org/elasticsearch/datastreams/action/GetDataStreamsTransportActionTests.java b/modules/data-streams/src/test/java/org/elasticsearch/datastreams/action/GetDataStreamsTransportActionTests.java index 8b6a9b1bddbc5..cd3f862a51ddf 100644 --- a/modules/data-streams/src/test/java/org/elasticsearch/datastreams/action/GetDataStreamsTransportActionTests.java +++ b/modules/data-streams/src/test/java/org/elasticsearch/datastreams/action/GetDataStreamsTransportActionTests.java @@ -344,12 +344,12 @@ public void testPassingGlobalRetention() { new DataStreamFactoryRetention() { @Override public TimeValue getMaxRetention() { - return globalRetention.getMaxRetention(); + return globalRetention.maxRetention(); } @Override public TimeValue getDefaultRetention() { - return globalRetention.getDefaultRetention(); + return globalRetention.defaultRetention(); } @Override diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetention.java b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetention.java index 00947456d1970..c74daa22cc137 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetention.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetention.java @@ -14,36 +14,24 @@ import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.features.NodeFeature; -import org.elasticsearch.xcontent.ParseField; import java.io.IOException; -import java.util.Objects; /** * A cluster state entry that contains global retention settings that are configurable by the user. These settings include: * - default retention, applied on any data stream managed by DSL that does not have an explicit retention defined * - max retention, applied on every data stream managed by DSL */ -public final class DataStreamGlobalRetention implements Writeable { +public record DataStreamGlobalRetention(@Nullable TimeValue defaultRetention, @Nullable TimeValue maxRetention) implements Writeable { public static final String TYPE = "data-stream-global-retention"; public static final NodeFeature GLOBAL_RETENTION = new NodeFeature("data_stream.lifecycle.global_retention"); - - public static final ParseField DEFAULT_RETENTION_FIELD = new ParseField("default_retention"); - public static final ParseField MAX_RETENTION_FIELD = new ParseField("max_retention"); - - public static final DataStreamGlobalRetention EMPTY = new DataStreamGlobalRetention(null, null); public static final TimeValue MIN_RETENTION_VALUE = TimeValue.timeValueSeconds(10); - @Nullable - private final TimeValue defaultRetention; - @Nullable - private final TimeValue maxRetention; - /** * @param defaultRetention the default retention or null if it's undefined - * @param maxRetention the max retention or null if it's undefined + * @param maxRetention the max retention or null if it's undefined * @throws IllegalArgumentException when the default retention is greater than the max retention. */ public DataStreamGlobalRetention(TimeValue defaultRetention, TimeValue maxRetention) { @@ -77,29 +65,6 @@ public void writeTo(StreamOutput out) throws IOException { out.writeOptionalTimeValue(maxRetention); } - @Nullable - public TimeValue getDefaultRetention() { - return defaultRetention; - } - - @Nullable - public TimeValue getMaxRetention() { - return maxRetention; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - DataStreamGlobalRetention that = (DataStreamGlobalRetention) o; - return Objects.equals(defaultRetention, that.defaultRetention) && Objects.equals(maxRetention, that.maxRetention); - } - - @Override - public int hashCode() { - return Objects.hash(defaultRetention, maxRetention); - } - @Override public String toString() { return "DataStreamGlobalRetention{" diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamLifecycle.java b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamLifecycle.java index 3fb5e92cb3359..de9d615022975 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamLifecycle.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStreamLifecycle.java @@ -174,12 +174,12 @@ public Tuple getEffectiveDataRetentionWithSource(@Nu return Tuple.tuple(dataStreamRetention, RetentionSource.DATA_STREAM_CONFIGURATION); } if (dataStreamRetention == null) { - return globalRetention.getDefaultRetention() != null - ? Tuple.tuple(globalRetention.getDefaultRetention(), RetentionSource.DEFAULT_GLOBAL_RETENTION) - : Tuple.tuple(globalRetention.getMaxRetention(), RetentionSource.MAX_GLOBAL_RETENTION); + return globalRetention.defaultRetention() != null + ? Tuple.tuple(globalRetention.defaultRetention(), RetentionSource.DEFAULT_GLOBAL_RETENTION) + : Tuple.tuple(globalRetention.maxRetention(), RetentionSource.MAX_GLOBAL_RETENTION); } - if (globalRetention.getMaxRetention() != null && globalRetention.getMaxRetention().getMillis() < dataStreamRetention.getMillis()) { - return Tuple.tuple(globalRetention.getMaxRetention(), RetentionSource.MAX_GLOBAL_RETENTION); + if (globalRetention.maxRetention() != null && globalRetention.maxRetention().getMillis() < dataStreamRetention.getMillis()) { + return Tuple.tuple(globalRetention.maxRetention(), RetentionSource.MAX_GLOBAL_RETENTION); } else { return Tuple.tuple(dataStreamRetention, RetentionSource.DATA_STREAM_CONFIGURATION); } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionProviderTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionProviderTests.java index ccf586455f687..f22664ea5b7d0 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionProviderTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionProviderTests.java @@ -23,8 +23,8 @@ public void testOnlyFactoryRetentionFallback() { DataStreamGlobalRetentionProvider resolver = new DataStreamGlobalRetentionProvider(factoryRetention); DataStreamGlobalRetention globalRetention = resolver.provide(); assertThat(globalRetention, notNullValue()); - assertThat(globalRetention.getDefaultRetention(), equalTo(factoryRetention.getDefaultRetention())); - assertThat(globalRetention.getMaxRetention(), equalTo(factoryRetention.getMaxRetention())); + assertThat(globalRetention.defaultRetention(), equalTo(factoryRetention.getDefaultRetention())); + assertThat(globalRetention.maxRetention(), equalTo(factoryRetention.getMaxRetention())); } private static DataStreamFactoryRetention randomNonEmptyFactoryRetention() { diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionTests.java index 11cbfaf71355b..54b365689db8b 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamGlobalRetentionTests.java @@ -26,8 +26,8 @@ protected DataStreamGlobalRetention createTestInstance() { @Override protected DataStreamGlobalRetention mutateInstance(DataStreamGlobalRetention instance) { - var defaultRetention = instance.getDefaultRetention(); - var maxRetention = instance.getMaxRetention(); + var defaultRetention = instance.defaultRetention(); + var maxRetention = instance.maxRetention(); switch (randomInt(1)) { case 0 -> { defaultRetention = randomValueOtherThan( diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleTests.java index 01d77d6eba2be..50ab76ed794d8 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleTests.java @@ -135,7 +135,7 @@ public void testXContentSerializationWithRolloverAndEffectiveRetention() throws } else { assertThat(serialized, containsString("data_retention")); } - boolean globalRetentionIsNotNull = globalRetention.getDefaultRetention() != null || globalRetention.getMaxRetention() != null; + boolean globalRetentionIsNotNull = globalRetention.defaultRetention() != null || globalRetention.maxRetention() != null; boolean configuredLifeCycleIsNotNull = lifecycle.getDataRetention() != null && lifecycle.getDataRetention().value() != null; if (lifecycle.isEnabled() && (globalRetentionIsNotNull || configuredLifeCycleIsNotNull)) { assertThat(serialized, containsString("effective_retention")); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleWithRetentionWarningsTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleWithRetentionWarningsTests.java index 960c76769b9d4..acfe2b4f847c4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleWithRetentionWarningsTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamLifecycleWithRetentionWarningsTests.java @@ -92,7 +92,7 @@ public void testDefaultRetentionHeaderWarning() { responseHeaders.get("Warning").get(0), containsString( "Not providing a retention is not allowed for this project. The default retention of [" - + globalRetention.getDefaultRetention().getStringRep() + + globalRetention.defaultRetention().getStringRep() + "] will be applied." ) ); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java index 0568f61e0dfd3..b1113677c82c4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java @@ -1900,8 +1900,7 @@ public void testXContentSerializationWithRolloverAndEffectiveRetention() throws } // We check that even if there was no retention provided by the user, the global retention applies assertThat(serialized, not(containsString("data_retention"))); - if (dataStream.isSystem() == false - && (globalRetention.getDefaultRetention() != null || globalRetention.getMaxRetention() != null)) { + if (dataStream.isSystem() == false && (globalRetention.defaultRetention() != null || globalRetention.maxRetention() != null)) { assertThat(serialized, containsString("effective_retention")); } else { assertThat(serialized, not(containsString("effective_retention")));