Skip to content

Add age_in_millis to ILM Explain Response #128866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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/128866.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 128866
summary: Add `age_in_millis` to ILM Explain Response
area: ILM+SLM
type: enhancement
issues:
- 103659
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl
private static final ParseField PREVIOUS_STEP_INFO_FIELD = new ParseField("previous_step_info");
private static final ParseField PHASE_EXECUTION_INFO = new ParseField("phase_execution");
private static final ParseField AGE_FIELD = new ParseField("age");
private static final ParseField AGE_IN_MILLIS_FIELD = new ParseField("age_in_millis");
private static final ParseField TIME_SINCE_INDEX_CREATION_FIELD = new ParseField("time_since_index_creation");
private static final ParseField REPOSITORY_NAME = new ParseField("repository_name");
private static final ParseField SHRINK_INDEX_NAME = new ParseField("shrink_index_name");
Expand Down Expand Up @@ -121,6 +122,7 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl
return BytesReference.bytes(builder);
}, PREVIOUS_STEP_INFO_FIELD);
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), SKIP_NAME);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), AGE_IN_MILLIS_FIELD);
}

private final String index;
Expand Down Expand Up @@ -530,6 +532,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
lifecycleDate
);
builder.field(AGE_FIELD.getPreferredName(), getAge(nowSupplier).toHumanReadableString(2));
builder.field(AGE_IN_MILLIS_FIELD.getPreferredName(), getAge(nowSupplier).getMillis());
}
if (phase != null) {
builder.field(PHASE_FIELD.getPreferredName(), phase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
Expand Down Expand Up @@ -110,14 +114,16 @@ public void testInvalidStepDetails() {
assertThat(exception.getMessage(), containsString("=null"));
}

public void testIndexAges() {
public void testIndexAges() throws IOException {
IndexLifecycleExplainResponse unmanagedExplainResponse = randomUnmanagedIndexExplainResponse();
assertThat(unmanagedExplainResponse.getLifecycleDate(), is(nullValue()));
assertThat(unmanagedExplainResponse.getAge(System::currentTimeMillis), is(TimeValue.MINUS_ONE));

assertThat(unmanagedExplainResponse.getIndexCreationDate(), is(nullValue()));
assertThat(unmanagedExplainResponse.getTimeSinceIndexCreation(System::currentTimeMillis), is(nullValue()));

assertAgeInMillisXContentAbsentForUnmanagedResponse(unmanagedExplainResponse);

IndexLifecycleExplainResponse managedExplainResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(
"indexName",
12345L,
Expand Down Expand Up @@ -155,6 +161,46 @@ public void testIndexAges() {
is(equalTo(TimeValue.timeValueMillis(now - managedExplainResponse.getIndexCreationDate())))
);
assertThat(managedExplainResponse.getTimeSinceIndexCreation(() -> 0L), is(equalTo(TimeValue.ZERO)));

long expectedAgeInMillisForThisCase = Math.max(0L, now - managedExplainResponse.getLifecycleDate());
assertAgeInMillisXContent(managedExplainResponse, expectedAgeInMillisForThisCase, now);
}

protected void assertAgeInMillisXContent(
final IndexLifecycleExplainResponse managedExplainResponse,
final long expectedAgeInMillis,
final long now
) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
managedExplainResponse.nowSupplier = () -> now;
try (builder) {
managedExplainResponse.toXContent(builder, ToXContentObject.EMPTY_PARAMS);
}
final String json = Strings.toString(builder);

try (XContentParser parser = createParser(builder.contentType().xContent(), json)) {
Map<String, Object> parsedMap = parser.map();

assertThat(parsedMap, hasKey("age_in_millis"));
final long actualParsedAgeInMillis = ((Number) parsedMap.get("age_in_millis")).longValue();
assertThat(actualParsedAgeInMillis, equalTo((Number) expectedAgeInMillis));
}
}

protected void assertAgeInMillisXContentAbsentForUnmanagedResponse(final IndexLifecycleExplainResponse unmanagedExplainResponse)
throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
try (builder) {
unmanagedExplainResponse.toXContent(builder, ToXContentObject.EMPTY_PARAMS);
}
final String json = Strings.toString(builder);

try (XContentParser parser = createParser(builder.contentType().xContent(), json)) {
Map<String, Object> parsedMap = parser.map();

assertThat(parsedMap, not(hasKey("age_in_millis")));
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public void testExplainIndicesWildcard() throws Exception {
assertThat(explainIndexWithMissingPolicy.get("action"), is(nullValue()));
assertThat(explainIndexWithMissingPolicy.get("step"), is(ErrorStep.NAME));
assertThat(explainIndexWithMissingPolicy.get("age"), is(nullValue()));
assertThat(explainIndexWithMissingPolicy.get("age_in_millis"), is(nullValue()));
assertThat(explainIndexWithMissingPolicy.get("failed_step"), is(nullValue()));
Map<String, Object> stepInfo = (Map<String, Object>) explainIndexWithMissingPolicy.get("step_info");
assertThat(stepInfo, is(notNullValue()));
Expand Down Expand Up @@ -343,6 +344,7 @@ private void assertManagedIndex(Map<String, Object> explainIndexMap) {
assertThat(explainIndexMap.get("step"), is("complete"));
assertThat(explainIndexMap.get("phase_time_millis"), is(notNullValue()));
assertThat(explainIndexMap.get("age"), is(notNullValue()));
assertThat(explainIndexMap.get("age_in_millis"), is(notNullValue()));
assertThat(explainIndexMap.get("phase_execution"), is(notNullValue()));
assertThat(explainIndexMap.get("failed_step"), is(nullValue()));
assertThat(explainIndexMap.get("step_info"), is(nullValue()));
Expand Down
Loading