Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/97038.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 97038
summary: Limit the details field length we store for each SLM invocation
area: ILM+SLM
type: bug
issues:
- 96918
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.elasticsearch.xpack.core.slm;

import org.elasticsearch.cluster.SimpleDiffable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
Expand All @@ -31,6 +32,7 @@ public class SnapshotInvocationRecord implements SimpleDiffable<SnapshotInvocati
static final ParseField START_TIMESTAMP = new ParseField("start_time");
static final ParseField TIMESTAMP = new ParseField("time");
static final ParseField DETAILS = new ParseField("details");
static final int MAX_DETAILS_LENGTH = 1000;

private final String snapshotName;
private final Long snapshotStartTimestamp;
Expand All @@ -54,11 +56,16 @@ public static SnapshotInvocationRecord parse(XContentParser parser, String name)
return PARSER.apply(parser, name);
}

public SnapshotInvocationRecord(String snapshotName, Long snapshotStartTimestamp, long snapshotFinishTimestamp, String details) {
public SnapshotInvocationRecord(
String snapshotName,
Long snapshotStartTimestamp,
long snapshotFinishTimestamp,
@Nullable String details
) {
this.snapshotName = Objects.requireNonNull(snapshotName, "snapshot name must be provided");
this.snapshotStartTimestamp = snapshotStartTimestamp;
this.snapshotFinishTimestamp = snapshotFinishTimestamp;
this.details = details;
this.details = Strings.substring(details, 0, MAX_DETAILS_LENGTH);
}

public SnapshotInvocationRecord(StreamInput in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import java.io.IOException;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

public class SnapshotInvocationRecordTests extends AbstractXContentSerializingTestCase<SnapshotInvocationRecord> {

@Override
Expand Down Expand Up @@ -56,6 +59,42 @@ protected SnapshotInvocationRecord mutateInstance(SnapshotInvocationRecord insta
}
}

public void testDetailsFieldIsTruncated() {
{
// value larger than the max allowed
SnapshotInvocationRecord snapshotInvocationRecord = new SnapshotInvocationRecord(
randomAlphaOfLengthBetween(5, 10),
randomNonNegativeNullableLong(),
randomNonNegativeLong(),
randomAlphaOfLengthBetween(1300, 1500)
);
assertThat(snapshotInvocationRecord.getDetails().length(), is(SnapshotInvocationRecord.MAX_DETAILS_LENGTH));
}

{
// value lower than the max allowed
String details = randomAlphaOfLengthBetween(5, 500);
SnapshotInvocationRecord snapshotInvocationRecord = new SnapshotInvocationRecord(
randomAlphaOfLengthBetween(5, 10),
randomNonNegativeNullableLong(),
randomNonNegativeLong(),
details
);
assertThat(snapshotInvocationRecord.getDetails().length(), is(details.length()));
}

{
// null value, remains null
SnapshotInvocationRecord snapshotInvocationRecord = new SnapshotInvocationRecord(
randomAlphaOfLengthBetween(5, 10),
randomNonNegativeNullableLong(),
randomNonNegativeLong(),
null
);
assertThat(snapshotInvocationRecord.getDetails(), is(nullValue()));
}
}

public static SnapshotInvocationRecord randomSnapshotInvocationRecord() {
return new SnapshotInvocationRecord(
randomAlphaOfLengthBetween(5, 10),
Expand Down