Skip to content

Commit

Permalink
Convert data stream global retention to record (elastic#111704)
Browse files Browse the repository at this point in the history
In this refactoring PR we convert the global retention class to a
record.
  • Loading branch information
gmarouli authored Aug 8, 2024
1 parent 681e9c1 commit 4420ae0
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public Tuple<TimeValue, RetentionSource> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down

0 comments on commit 4420ae0

Please sign in to comment.