Skip to content

[Failure Store] Expose failure store lifecycle information via the GET data stream API #126668

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

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.elasticsearch.action.datastreams.GetDataStreamAction;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.cluster.metadata.DataStream;
import org.elasticsearch.cluster.metadata.DataStreamFailureStore;
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.set.Sets;
Expand All @@ -23,7 +25,10 @@
import org.elasticsearch.rest.action.RestToXContentListener;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.elasticsearch.rest.RestRequest.Method.GET;

Expand All @@ -45,6 +50,10 @@ public class RestGetDataStreamsAction extends BaseRestHandler {
)
)
);
private static final Set<String> CAPABILITIES = Stream.of(
DataStreamLifecycle.EFFECTIVE_RETENTION_REST_API_CAPABILITY,
DataStream.isFailureStoreFeatureFlagEnabled() ? DataStreamFailureStore.FAILURES_LIFECYCLE_API_CAPABILITY : null
).filter(Objects::nonNull).collect(Collectors.toSet());

@Override
public String getName() {
Expand Down Expand Up @@ -79,7 +88,7 @@ public boolean allowSystemIndexAccessByDefault() {

@Override
public Set<String> supportedCapabilities() {
return Set.of(DataStreamLifecycle.EFFECTIVE_RETENTION_REST_API_CAPABILITY);
return CAPABILITIES;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.elasticsearch.cluster.metadata.DataStream.getDefaultBackingIndexName;
import static org.elasticsearch.cluster.metadata.DataStream.getDefaultFailureStoreName;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

public class GetDataStreamsResponseTests extends ESTestCase {
Expand Down Expand Up @@ -136,16 +137,16 @@ public void testResponseIlmAndDataStreamLifecycleRepresentation() throws Excepti

if (DataStream.isFailureStoreFeatureFlagEnabled()) {
var failureStore = (Map<String, Object>) dataStreamMap.get(DataStream.FAILURE_STORE_FIELD.getPreferredName());
List<Object> failureStoresRepresentation = (List<Object>) failureStore.get(DataStream.INDICES_FIELD.getPreferredName());
Map<String, Object> failureStoreRepresentation = (Map<String, Object>) failureStoresRepresentation.get(0);
assertThat(failureStoreRepresentation.get("index_name"), is(failureStoreIndex.getName()));
assertThat(failureStoreRepresentation.get(Response.DataStreamInfo.PREFER_ILM.getPreferredName()), is(false));
List<Object> failureIndices = (List<Object>) failureStore.get(DataStream.INDICES_FIELD.getPreferredName());
Map<String, Object> failureIndexRepresentation = (Map<String, Object>) failureIndices.get(0);
assertThat(failureIndexRepresentation.get("index_name"), is(failureStoreIndex.getName()));
assertThat(failureIndexRepresentation.get(Response.DataStreamInfo.PREFER_ILM.getPreferredName()), nullValue());
assertThat(
failureStoreRepresentation.get(Response.DataStreamInfo.ILM_POLICY_FIELD.getPreferredName()),
failureIndexRepresentation.get(Response.DataStreamInfo.ILM_POLICY_FIELD.getPreferredName()),
is(nullValue())
);
assertThat(
failureStoreRepresentation.get(Response.DataStreamInfo.MANAGED_BY.getPreferredName()),
failureIndexRepresentation.get(Response.DataStreamInfo.MANAGED_BY.getPreferredName()),
is(ManagedBy.LIFECYCLE.displayValue)
);
}
Expand Down Expand Up @@ -230,21 +231,86 @@ public void testResponseIlmAndDataStreamLifecycleRepresentation() throws Excepti

if (DataStream.isFailureStoreFeatureFlagEnabled()) {
var failureStore = (Map<String, Object>) dataStreamMap.get(DataStream.FAILURE_STORE_FIELD.getPreferredName());
List<Object> failureStoresRepresentation = (List<Object>) failureStore.get(DataStream.INDICES_FIELD.getPreferredName());
Map<String, Object> failureStoreRepresentation = (Map<String, Object>) failureStoresRepresentation.get(0);
assertThat(failureStoreRepresentation.get("index_name"), is(failureStoreIndex.getName()));
assertThat(failureStoreRepresentation.get(Response.DataStreamInfo.PREFER_ILM.getPreferredName()), is(false));
List<Object> failureIndices = (List<Object>) failureStore.get(DataStream.INDICES_FIELD.getPreferredName());
Map<String, Object> failureIndexRepresentation = (Map<String, Object>) failureIndices.get(0);
assertThat(failureIndexRepresentation.get("index_name"), is(failureStoreIndex.getName()));
assertThat(failureIndexRepresentation.get(Response.DataStreamInfo.PREFER_ILM.getPreferredName()), nullValue());
assertThat(
failureStoreRepresentation.get(Response.DataStreamInfo.ILM_POLICY_FIELD.getPreferredName()),
failureIndexRepresentation.get(Response.DataStreamInfo.ILM_POLICY_FIELD.getPreferredName()),
is(nullValue())
);
assertThat(
failureStoreRepresentation.get(Response.DataStreamInfo.MANAGED_BY.getPreferredName()),
failureIndexRepresentation.get(Response.DataStreamInfo.MANAGED_BY.getPreferredName()),
is(ManagedBy.UNMANAGED.displayValue)
);
}
}
}

{
// one failure index that have ILM policy
DataStream logs = DataStream.builder("logs", indices)
.setGeneration(3)
.setAllowCustomRouting(true)
.setIndexMode(IndexMode.STANDARD)
.setLifecycle(DataStreamLifecycle.DEFAULT_DATA_LIFECYCLE)
.setDataStreamOptions(DataStreamOptions.FAILURE_STORE_ENABLED)
.setFailureIndices(DataStream.DataStreamIndices.failureIndicesBuilder(failureStores).build())
.build();

String ilmPolicyName = "rollover-30days";
Map<Index, Response.IndexProperties> indexSettingsValues = Map.of(
firstGenerationIndex,
new Response.IndexProperties(true, ilmPolicyName, ManagedBy.ILM, null),
secondGenerationIndex,
new Response.IndexProperties(false, ilmPolicyName, ManagedBy.LIFECYCLE, null),
writeIndex,
new Response.IndexProperties(true, null, ManagedBy.LIFECYCLE, null),
failureStoreIndex,
new Response.IndexProperties(randomBoolean(), ilmPolicyName, ManagedBy.LIFECYCLE, null)
);

Response.DataStreamInfo dataStreamInfo = new Response.DataStreamInfo(
logs,
true,
ClusterHealthStatus.GREEN,
"index-template",
null,
null,
indexSettingsValues,
false,
null,
null
);
Response response = new Response(List.of(dataStreamInfo));
XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
response.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);

BytesReference bytes = BytesReference.bytes(contentBuilder);
try (XContentParser parser = createParser(JsonXContent.jsonXContent, bytes)) {
Map<String, Object> map = parser.map();
List<Object> dataStreams = (List<Object>) map.get(Response.DATA_STREAMS_FIELD.getPreferredName());
assertThat(dataStreams.size(), is(1));
Map<String, Object> dataStreamMap = (Map<String, Object>) dataStreams.get(0);
assertThat(dataStreamMap.get(DataStream.NAME_FIELD.getPreferredName()), is(dataStreamName));

if (DataStream.isFailureStoreFeatureFlagEnabled()) {
var failureStore = (Map<String, Object>) dataStreamMap.get(DataStream.FAILURE_STORE_FIELD.getPreferredName());
List<Object> failureIndices = (List<Object>) failureStore.get(DataStream.INDICES_FIELD.getPreferredName());
Map<String, Object> failureIndexRepresentation = (Map<String, Object>) failureIndices.get(0);
assertThat(failureIndexRepresentation.get("index_name"), is(failureStoreIndex.getName()));
assertThat(failureIndexRepresentation.get(Response.DataStreamInfo.PREFER_ILM.getPreferredName()), notNullValue());
assertThat(
failureIndexRepresentation.get(Response.DataStreamInfo.ILM_POLICY_FIELD.getPreferredName()),
is(ilmPolicyName)
);
assertThat(
failureIndexRepresentation.get(Response.DataStreamInfo.MANAGED_BY.getPreferredName()),
is(ManagedBy.LIFECYCLE.displayValue)
);
}
}
}
}

public void testManagedByDisplayValuesDontAccidentalyChange() {
Expand Down
Loading