Skip to content

Commit

Permalink
Refactor IndexAbstraction to not use IndexMetadata
Browse files Browse the repository at this point in the history
Most users of an `IndexAbstraction` instance doesn't need to use the
`IndexMetadata` instances that `getIndices()` and `getWriteIndex()` returns.

Cluster state variables/parameters can be used in places that access to
`IndexMetadata` is required.

By changing the `getIndices()` and `getWriteIndex()` methods to return `Index`
instance, the indices lookup can be reused across different cluster states.
This should be possible in cases that don't change an index hidden status or
 open and closes indices or when adding / removing aliases, data streams or indices.

This change should allow for elastic#79004
  • Loading branch information
martijnvg committed Oct 13, 2021
1 parent 04e5823 commit f5dc38c
Show file tree
Hide file tree
Showing 45 changed files with 254 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.IndexAbstraction;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.TimeValue;
Expand Down Expand Up @@ -665,8 +666,9 @@ public void testSameAlias() {
assertThat(stopWatch.stop().lastTaskTime().millis(), lessThan(timeout.millis()));

logger.info("--> verify that filter was updated");
IndexAbstraction ia = internalCluster().clusterService().state().metadata().getIndicesLookup().get("alias1");
AliasMetadata aliasMetadata = AliasMetadata.getFirstAliasMetadata(ia);
Metadata metadata = internalCluster().clusterService().state().metadata();
IndexAbstraction ia = metadata.getIndicesLookup().get("alias1");
AliasMetadata aliasMetadata = AliasMetadata.getFirstAliasMetadata(metadata, ia);
assertThat(aliasMetadata.getFilter().toString(), equalTo("{\"term\":{\"name\":{\"value\":\"bar\",\"boost\":1.0}}}"));

logger.info("--> deleting alias1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.concurrent.CountDown;
import org.elasticsearch.index.Index;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
Expand Down Expand Up @@ -504,7 +505,7 @@ static void resolveIndices(String[] names, IndicesOptions indicesOptions, Metada
includeDataStreams);
SortedMap<String, IndexAbstraction> lookup = metadata.getIndicesLookup();
for (String s : resolvedIndexAbstractions) {
enrichIndexAbstraction(s, lookup, indices, aliases, dataStreams);
enrichIndexAbstraction(metadata, s, lookup, indices, aliases, dataStreams);
}
indices.sort(Comparator.comparing(ResolvedIndexAbstraction::getName));
aliases.sort(Comparator.comparing(ResolvedIndexAbstraction::getName));
Expand All @@ -529,42 +530,41 @@ private static void mergeResults(Map<String, Response> remoteResponses, List<Res
}
}

private static void enrichIndexAbstraction(String indexAbstraction, SortedMap<String, IndexAbstraction> lookup,
private static void enrichIndexAbstraction(Metadata metadata, String indexAbstraction, SortedMap<String, IndexAbstraction> lookup,
List<ResolvedIndex> indices, List<ResolvedAlias> aliases,
List<ResolvedDataStream> dataStreams) {
IndexAbstraction ia = lookup.get(indexAbstraction);
if (ia != null) {
switch (ia.getType()) {
case CONCRETE_INDEX:
IndexAbstraction.Index index = (IndexAbstraction.Index) ia;

String[] aliasNames = index.getWriteIndex().getAliases().keySet().stream().sorted().toArray(String[]::new);
IndexMetadata writeIndex = metadata.index(ia.getWriteIndex());
String[] aliasNames = writeIndex.getAliases().keySet().stream().sorted().toArray(String[]::new);

List<String> attributes = new ArrayList<>();
attributes.add(index.getWriteIndex().getState() == IndexMetadata.State.OPEN ? "open" : "closed");
attributes.add(writeIndex.getState() == IndexMetadata.State.OPEN ? "open" : "closed");
if (ia.isHidden()) {
attributes.add("hidden");
}
final boolean isFrozen = Boolean.parseBoolean(ia.getWriteIndex().getSettings().get("index.frozen"));
final boolean isFrozen = Boolean.parseBoolean(writeIndex.getSettings().get("index.frozen"));
if (isFrozen) {
attributes.add("frozen");
}
attributes.sort(String::compareTo);

indices.add(new ResolvedIndex(
index.getName(),
ia.getName(),
aliasNames,
attributes.toArray(Strings.EMPTY_ARRAY),
index.getParentDataStream() == null ? null : index.getParentDataStream().getName()));
ia.getParentDataStream() == null ? null : ia.getParentDataStream().getName()));
break;
case ALIAS:
String[] indexNames = ia.getIndices().stream().map(i -> i.getIndex().getName()).toArray(String[]::new);
String[] indexNames = ia.getIndices().stream().map(Index::getName).toArray(String[]::new);
Arrays.sort(indexNames);
aliases.add(new ResolvedAlias(ia.getName(), indexNames));
break;
case DATA_STREAM:
IndexAbstraction.DataStream dataStream = (IndexAbstraction.DataStream) ia;
String[] backingIndices = dataStream.getIndices().stream().map(i -> i.getIndex().getName()).toArray(String[]::new);
String[] backingIndices = dataStream.getIndices().stream().map(Index::getName).toArray(String[]::new);
dataStreams.add(new ResolvedDataStream(
dataStream.getName(),
backingIndices,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.indices.SystemDataStreamDescriptor;
import org.elasticsearch.indices.SystemIndices;
import org.elasticsearch.snapshots.SnapshotInProgressException;
Expand Down Expand Up @@ -126,7 +127,7 @@ public NameResolution resolveRolloverNames(ClusterState currentState, String rol
final IndexAbstraction indexAbstraction = currentState.metadata().getIndicesLookup().get(rolloverTarget);
switch (indexAbstraction.getType()) {
case ALIAS:
return resolveAliasRolloverNames((IndexAbstraction.Alias) indexAbstraction, newIndexName);
return resolveAliasRolloverNames(currentState.metadata(), indexAbstraction, newIndexName);
case DATA_STREAM:
return resolveDataStreamRolloverNames(currentState.getMetadata(), (IndexAbstraction.DataStream) indexAbstraction);
default:
Expand All @@ -148,8 +149,8 @@ public static class NameResolution {
}
}

private NameResolution resolveAliasRolloverNames(IndexAbstraction.Alias alias, String newIndexName) {
final IndexMetadata writeIndex = alias.getWriteIndex();
private NameResolution resolveAliasRolloverNames(Metadata metadata, IndexAbstraction alias, String newIndexName) {
final IndexMetadata writeIndex = metadata.index(alias.getWriteIndex());
final String sourceProvidedName = writeIndex.getSettings().get(IndexMetadata.SETTING_INDEX_PROVIDED_NAME,
writeIndex.getIndex().getName());
final String sourceIndexName = writeIndex.getIndex().getName();
Expand All @@ -162,20 +163,20 @@ private NameResolution resolveAliasRolloverNames(IndexAbstraction.Alias alias, S

private NameResolution resolveDataStreamRolloverNames(Metadata metadata, IndexAbstraction.DataStream dataStream) {
final DataStream ds = dataStream.getDataStream();
final IndexMetadata originalWriteIndex = dataStream.getWriteIndex();
final IndexMetadata originalWriteIndex = metadata.index(dataStream.getWriteIndex());
final DataStream rolledDataStream = ds.rollover(metadata, "uuid");
return new NameResolution(originalWriteIndex.getIndex().getName(), null, rolledDataStream.getWriteIndex().getName());
}

private RolloverResult rolloverAlias(ClusterState currentState, IndexAbstraction.Alias alias, String aliasName,
String newIndexName, CreateIndexRequest createIndexRequest, List<Condition<?>> metConditions,
boolean silent, boolean onlyValidate) throws Exception {
final NameResolution names = resolveAliasRolloverNames(alias, newIndexName);
final NameResolution names = resolveAliasRolloverNames(currentState.metadata(), alias, newIndexName);
final String sourceIndexName = names.sourceName;
final String rolloverIndexName = names.rolloverName;
final String unresolvedName = names.unresolvedName;
final Metadata metadata = currentState.metadata();
final IndexMetadata writeIndex = alias.getWriteIndex();
final IndexMetadata writeIndex = currentState.metadata().index(alias.getWriteIndex());
final AliasMetadata aliasMetadata = writeIndex.getAliases().get(alias.getName());
final boolean explicitWriteIndex = Boolean.TRUE.equals(aliasMetadata.writeIndex());
final Boolean isHidden = IndexMetadata.INDEX_HIDDEN_SETTING.exists(createIndexRequest.settings()) ?
Expand Down Expand Up @@ -228,11 +229,11 @@ private RolloverResult rolloverDataStream(ClusterState currentState, IndexAbstra
}

final DataStream ds = dataStream.getDataStream();
final IndexMetadata originalWriteIndex = dataStream.getWriteIndex();
final Index originalWriteIndex = dataStream.getWriteIndex();
DataStream rolledDataStream = ds.rollover(currentState.metadata(), "uuid");
createIndexService.validateIndexName(rolledDataStream.getWriteIndex().getName(), currentState); // fails if the index already exists
if (onlyValidate) {
return new RolloverResult(rolledDataStream.getWriteIndex().getName(), originalWriteIndex.getIndex().getName(), currentState);
return new RolloverResult(rolledDataStream.getWriteIndex().getName(), originalWriteIndex.getName(), currentState);
}

CreateIndexClusterStateUpdateRequest createIndexClusterStateRequest = prepareDataStreamCreateIndexRequest(
Expand All @@ -247,11 +248,11 @@ private RolloverResult rolloverDataStream(ClusterState currentState, IndexAbstra
RolloverInfo rolloverInfo = new RolloverInfo(dataStreamName, metConditions, threadPool.absoluteTimeInMillis());
newState = ClusterState.builder(newState)
.metadata(Metadata.builder(newState.metadata())
.put(IndexMetadata.builder(newState.metadata().index(originalWriteIndex.getIndex()))
.put(IndexMetadata.builder(newState.metadata().index(originalWriteIndex))
.putRolloverInfo(rolloverInfo)))
.build();

return new RolloverResult(rolledDataStream.getWriteIndex().getName(), originalWriteIndex.getIndex().getName(), newState);
return new RolloverResult(rolledDataStream.getWriteIndex().getName(), originalWriteIndex.getName(), newState);
}

static String generateRolloverIndexName(String sourceIndexName, IndexNameExpressionResolver indexNameExpressionResolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static AliasMetadata getFirstAliasMetadata(IndexAbstraction ia) {
public static AliasMetadata getFirstAliasMetadata(Metadata metadata, IndexAbstraction ia) {
if (ia.getType() != IndexAbstraction.Type.ALIAS) {
throw new IllegalArgumentException("unexpected type: [" + ia.getType() + "]");
}

return ia.getIndices().get(0).getAliases().get(ia.getName());
IndexMetadata firstIndex = metadata.index(ia.getIndices().get(0));
return firstIndex.getAliases().get(ia.getName());
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public DataStream addBackingIndex(Metadata clusterMetadata, Index index) {
}

// ensure that no aliases reference index
IndexMetadata im = clusterMetadata.getIndicesLookup().get(index.getName()).getWriteIndex();
IndexMetadata im = clusterMetadata.index(clusterMetadata.getIndicesLookup().get(index.getName()).getWriteIndex());
if (im.getAliases().size() > 0) {
throw new IllegalArgumentException(
String.format(Locale.ROOT,
Expand Down
Loading

0 comments on commit f5dc38c

Please sign in to comment.