Skip to content

Commit

Permalink
The get aliases api should not return entries for data streams with n…
Browse files Browse the repository at this point in the history
…o aliases (#73225)

Backport #72953 to 7.x branch.

The get alias api should take into account the aliases parameter when
returning aliases that refer to data streams and don't return entries
for data streams that don't have any aliases pointing to it.

Relates to #66163
  • Loading branch information
martijnvg authored May 19, 2021
1 parent 3627275 commit aac787a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
3 changes: 0 additions & 3 deletions docs/reference/indices/aliases.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,6 @@ The get index alias API returns:
[source,console-result]
----
{
"logs-my_app-default": {
"aliases": {}
},
"logs-nginx.access-prod": {
"aliases": {
"logs": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ protected void masterOperation(GetAliasesRequest request, ClusterState state, Ac
final SystemIndexAccessLevel systemIndexAccessLevel = indexNameExpressionResolver.getSystemIndexAccessLevel();
ImmutableOpenMap<String, List<AliasMetadata>> aliases = state.metadata().findAliases(request, concreteIndices);
listener.onResponse(new GetAliasesResponse(postProcess(request, concreteIndices, aliases, state,
systemIndexAccessLevel, threadPool.getThreadContext(), systemIndices), postProcess(request, state)));
systemIndexAccessLevel, threadPool.getThreadContext(), systemIndices),
postProcess(indexNameExpressionResolver, request, state)));
}

/**
Expand Down Expand Up @@ -106,17 +107,19 @@ static ImmutableOpenMap<String, List<AliasMetadata>> postProcess(GetAliasesReque
return finalResponse;
}

Map<String, List<DataStreamAlias>> postProcess(GetAliasesRequest request, ClusterState state) {
static Map<String, List<DataStreamAlias>> postProcess(IndexNameExpressionResolver resolver, GetAliasesRequest request,
ClusterState state) {
Map<String, List<DataStreamAlias>> result = new HashMap<>();
boolean noAliasesSpecified = request.getOriginalAliases() == null || request.getOriginalAliases().length == 0;
List<String> requestedDataStreams =
indexNameExpressionResolver.dataStreamNames(state, request.indicesOptions(), request.indices());
List<String> requestedDataStreams = resolver.dataStreamNames(state, request.indicesOptions(), request.indices());
for (String requestedDataStream : requestedDataStreams) {
List<DataStreamAlias> aliases = state.metadata().dataStreamAliases().values().stream()
.filter(alias -> alias.getDataStreams().contains(requestedDataStream))
.filter(alias -> noAliasesSpecified || Regex.simpleMatch(request.aliases(), alias.getName()))
.collect(Collectors.toList());
result.put(requestedDataStream, aliases);
if (aliases.isEmpty() == false) {
result.put(requestedDataStream, aliases);
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ public Builder put(DataStream dataStream) {
return this;
}

public boolean put(String name, String dataStream) {
public boolean put(String aliasName, String dataStream) {
Map<String, DataStream> existingDataStream =
Optional.ofNullable((DataStreamMetadata) this.customs.get(DataStreamMetadata.TYPE))
.map(dsmd -> new HashMap<>(dsmd.dataStreams()))
Expand All @@ -1311,21 +1311,21 @@ public boolean put(String name, String dataStream) {
.orElse(new HashMap<>());

if (existingDataStream.containsKey(dataStream) == false) {
throw new IllegalArgumentException("alias [" + name + "] refers to a non existing data stream [" + dataStream + "]");
throw new IllegalArgumentException("alias [" + aliasName + "] refers to a non existing data stream [" + dataStream + "]");
}

DataStreamAlias alias = dataStreamAliases.get(name);
DataStreamAlias alias = dataStreamAliases.get(aliasName);
if (alias == null) {
alias = new DataStreamAlias(name, Collections.singletonList(dataStream));
alias = new DataStreamAlias(aliasName, Collections.singletonList(dataStream));
} else {
Set<String> dataStreams = new HashSet<>(alias.getDataStreams());
boolean added = dataStreams.add(dataStream);
if (added == false) {
return false;
}
alias = new DataStreamAlias(name, dataStreams);
alias = new DataStreamAlias(aliasName, dataStreams);
}
dataStreamAliases.put(name, alias);
dataStreamAliases.put(aliasName, alias);

this.customs.put(DataStreamMetadata.TYPE, new DataStreamMetadata(existingDataStream, dataStreamAliases));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.DataStreamAlias;
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.indices.EmptySystemIndices;
import org.elasticsearch.indices.SystemIndexDescriptor;
import org.elasticsearch.indices.SystemIndices;
import org.elasticsearch.indices.SystemIndices.SystemIndexAccessLevel;
import org.elasticsearch.indices.TestIndexNameExpressionResolver;
import org.elasticsearch.test.ESTestCase;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;

public class TransportGetAliasesActionTests extends ESTestCase {
Expand Down Expand Up @@ -184,6 +193,38 @@ public void testDeprecationWarningEmittedWhenRequestingNonExistingAliasInSystemP
"access to system indices and their aliases will not be allowed");
}

public void testPostProcessDataStreamAliases() {
IndexNameExpressionResolver resolver = TestIndexNameExpressionResolver.newInstance();
List<Tuple<String, Integer>> tuples =
Arrays.asList(new Tuple<>("logs-foo", 1), new Tuple<>("logs-bar", 1), new Tuple<>("logs-baz", 1));
ClusterState clusterState = DataStreamTestHelper.getClusterStateWithDataStreams(tuples, Collections.emptyList());
Metadata.Builder builder = Metadata.builder(clusterState.metadata());
builder.put("logs", "logs-foo");
builder.put("logs", "logs-bar");
builder.put("secret", "logs-bar");
clusterState = ClusterState.builder(clusterState).metadata(builder).build();

// return all all data streams with aliases
GetAliasesRequest getAliasesRequest = new GetAliasesRequest();
Map<String, List<DataStreamAlias>> result = TransportGetAliasesAction.postProcess(resolver, getAliasesRequest, clusterState);
assertThat(result.keySet(), containsInAnyOrder("logs-foo", "logs-bar"));
assertThat(result.get("logs-foo"), contains(new DataStreamAlias("logs", Arrays.asList("logs-bar", "logs-foo"))));
assertThat(result.get("logs-bar"), containsInAnyOrder(new DataStreamAlias("logs", Arrays.asList("logs-bar", "logs-foo")),
new DataStreamAlias("secret", Collections.singletonList("logs-bar"))));

// filter by alias name
getAliasesRequest = new GetAliasesRequest("secret");
result = TransportGetAliasesAction.postProcess(resolver, getAliasesRequest, clusterState);
assertThat(result.keySet(), containsInAnyOrder("logs-bar"));
assertThat(result.get("logs-bar"), contains(new DataStreamAlias("secret", Collections.singletonList("logs-bar"))));

// filter by data stream:
getAliasesRequest = new GetAliasesRequest().indices("logs-foo");
result = TransportGetAliasesAction.postProcess(resolver, getAliasesRequest, clusterState);
assertThat(result.keySet(), containsInAnyOrder("logs-foo"));
assertThat(result.get("logs-foo"), contains(new DataStreamAlias("logs", Arrays.asList("logs-bar", "logs-foo"))));
}

public ClusterState systemIndexTestClusterState() {
return ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ public void testDataStreamAliases() throws Exception {

getAliasesRequest = new Request("GET", "/_aliases");
getAliasesResponse = entityAsMap(client().performRequest(getAliasesRequest));
assertEquals(emptyMap(), XContentMapValues.extractValue("logs-myapp1.aliases", getAliasesResponse));
assertEquals(emptyMap(), XContentMapValues.extractValue("logs-myapp2.aliases", getAliasesResponse));
assertEquals(emptyMap(), getAliasesResponse);
expectThrows(ResponseException.class, () -> client().performRequest(new Request("GET", "/logs/_search")));

// Add logs-* -> logs
Expand All @@ -157,8 +156,7 @@ public void testDataStreamAliases() throws Exception {

getAliasesRequest = new Request("GET", "/_aliases");
getAliasesResponse = entityAsMap(client().performRequest(getAliasesRequest));
assertEquals(emptyMap(), XContentMapValues.extractValue("logs-myapp1.aliases", getAliasesResponse));
assertEquals(emptyMap(), XContentMapValues.extractValue("logs-myapp2.aliases", getAliasesResponse));
assertEquals(emptyMap(), getAliasesResponse);
expectThrows(ResponseException.class, () -> client().performRequest(new Request("GET", "/logs/_search")));
}

Expand Down Expand Up @@ -227,20 +225,17 @@ public void testGetAliasApiFilterByDataStreamAlias() throws Exception {
response = client().performRequest(new Request("GET", "/_alias/emea"));
assertOK(response);
getAliasesResponse = entityAsMap(response);
assertThat(getAliasesResponse.size(), equalTo(2)); // Adjust to equalTo(1) when #72953 is merged
assertThat(getAliasesResponse.size(), equalTo(1));
assertEquals(singletonMap("emea", emptyMap()), XContentMapValues.extractValue("logs-emea.aliases", getAliasesResponse));
assertEquals(emptyMap(), XContentMapValues.extractValue("logs-nasa.aliases", getAliasesResponse)); // Remove when #72953 is merged

ResponseException exception =
expectThrows(ResponseException.class, () -> client().performRequest(new Request("GET", "/_alias/wrong_name")));
response = exception.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(404));
getAliasesResponse = entityAsMap(response);
assertThat(getAliasesResponse.size(), equalTo(4)); // Adjust to equalTo(2) when #72953 is merged
assertThat(getAliasesResponse.size(), equalTo(2));
assertEquals("alias [wrong_name] missing", getAliasesResponse.get("error"));
assertEquals(404, getAliasesResponse.get("status"));
assertEquals(emptyMap(), XContentMapValues.extractValue("logs-emea.aliases", getAliasesResponse)); // Remove when #72953 is merged
assertEquals(emptyMap(), XContentMapValues.extractValue("logs-nasa.aliases", getAliasesResponse)); // Remove when #72953 is merged
}

}

0 comments on commit aac787a

Please sign in to comment.