Skip to content

Commit

Permalink
Prevent multiple sets copies while adding index aliases (elastic#115934)
Browse files Browse the repository at this point in the history
  • Loading branch information
idegtiarenko authored and jfreden committed Nov 4, 2024
1 parent a2d9924 commit 52811ac
Showing 1 changed file with 14 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,6 @@ public static class Builder {
private DiffableStringMap hashesOfConsistentSettings = DiffableStringMap.EMPTY;

private final ImmutableOpenMap.Builder<String, IndexMetadata> indices;
private final ImmutableOpenMap.Builder<String, Set<Index>> aliasedIndices;
private final ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templates;
private final ImmutableOpenMap.Builder<String, Custom> customs;

Expand Down Expand Up @@ -1826,7 +1825,6 @@ public Builder() {
this.hashesOfConsistentSettings = metadata.hashesOfConsistentSettings;
this.version = metadata.version;
this.indices = ImmutableOpenMap.builder(metadata.indices);
this.aliasedIndices = ImmutableOpenMap.builder(metadata.aliasedIndices);
this.templates = ImmutableOpenMap.builder(metadata.templates);
this.customs = ImmutableOpenMap.builder(metadata.customs);
this.previousIndicesLookup = metadata.indicesLookup;
Expand All @@ -1839,7 +1837,6 @@ public Builder() {
private Builder(Map<String, MappingMetadata> mappingsByHash, int indexCountHint) {
clusterUUID = UNKNOWN_CLUSTER_UUID;
indices = ImmutableOpenMap.builder(indexCountHint);
aliasedIndices = ImmutableOpenMap.builder();
templates = ImmutableOpenMap.builder();
customs = ImmutableOpenMap.builder();
reservedStateMetadata = new HashMap<>();
Expand All @@ -1854,7 +1851,6 @@ public Builder put(IndexMetadata.Builder indexMetadataBuilder) {
dedupeMapping(indexMetadataBuilder);
IndexMetadata indexMetadata = indexMetadataBuilder.build();
IndexMetadata previous = indices.put(indexMetadata.getIndex().getName(), indexMetadata);
updateAliases(previous, indexMetadata);
if (unsetPreviousIndicesLookup(previous, indexMetadata)) {
previousIndicesLookup = null;
}
Expand All @@ -1879,7 +1875,6 @@ public Builder put(IndexMetadata indexMetadata, boolean incrementVersion) {
return this;
}
}
updateAliases(previous, indexMetadata);
if (unsetPreviousIndicesLookup(previous, indexMetadata)) {
previousIndicesLookup = null;
}
Expand Down Expand Up @@ -1954,8 +1949,7 @@ public IndexMetadata getSafe(Index index) {
public Builder remove(String index) {
previousIndicesLookup = null;
checkForUnusedMappings = true;
IndexMetadata previous = indices.remove(index);
updateAliases(previous, null);
indices.remove(index);
return this;
}

Expand All @@ -1965,7 +1959,6 @@ public Builder removeAllIndices() {

indices.clear();
mappingsByHash.clear();
aliasedIndices.clear();
return this;
}

Expand All @@ -1976,67 +1969,6 @@ public Builder indices(Map<String, IndexMetadata> indices) {
return this;
}

void updateAliases(IndexMetadata previous, IndexMetadata current) {
if (previous == null && current != null) {
for (var key : current.getAliases().keySet()) {
putAlias(key, current.getIndex());
}
} else if (previous != null && current == null) {
for (var key : previous.getAliases().keySet()) {
removeAlias(key, previous.getIndex());
}
} else if (previous != null && current != null) {
if (Objects.equals(previous.getAliases(), current.getAliases())) {
return;
}

for (var key : current.getAliases().keySet()) {
if (previous.getAliases().containsKey(key) == false) {
putAlias(key, current.getIndex());
}
}
for (var key : previous.getAliases().keySet()) {
if (current.getAliases().containsKey(key) == false) {
removeAlias(key, current.getIndex());
}
}
}
}

private Builder putAlias(String alias, Index index) {
Objects.requireNonNull(alias);
Objects.requireNonNull(index);

Set<Index> indices = new HashSet<>(aliasedIndices.getOrDefault(alias, Set.of()));
if (indices.add(index) == false) {
return this; // indices already contained this index
}
aliasedIndices.put(alias, Collections.unmodifiableSet(indices));
return this;
}

private Builder removeAlias(String alias, Index index) {
Objects.requireNonNull(alias);
Objects.requireNonNull(index);

Set<Index> indices = aliasedIndices.get(alias);
if (indices == null || indices.isEmpty()) {
throw new IllegalStateException("Cannot remove non-existent alias [" + alias + "] for index [" + index.getName() + "]");
}

indices = new HashSet<>(indices);
if (indices.remove(index) == false) {
throw new IllegalStateException("Cannot remove non-existent alias [" + alias + "] for index [" + index.getName() + "]");
}

if (indices.isEmpty()) {
aliasedIndices.remove(alias); // for consistency, we don't store empty sets, so null it out
} else {
aliasedIndices.put(alias, Collections.unmodifiableSet(indices));
}
return this;
}

public Builder put(IndexTemplateMetadata.Builder template) {
return put(template.build());
}
Expand Down Expand Up @@ -2358,6 +2290,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
int totalNumberOfShards = 0;
int totalOpenIndexShards = 0;

ImmutableOpenMap.Builder<String, Set<Index>> aliasedIndicesBuilder = ImmutableOpenMap.builder();
final String[] allIndicesArray = new String[indicesMap.size()];
int i = 0;
final Set<String> sha256HashesInUse = checkForUnusedMappings ? Sets.newHashSetWithExpectedSize(mappingsByHash.size()) : null;
Expand Down Expand Up @@ -2389,9 +2322,19 @@ public Metadata build(boolean skipNameCollisionChecks) {
sha256HashesInUse.add(mapping.getSha256());
}
}
for (var alias : indexMetadata.getAliases().keySet()) {
var indices = aliasedIndicesBuilder.get(alias);
if (indices == null) {
indices = new HashSet<>();
aliasedIndicesBuilder.put(alias, indices);
}
indices.add(indexMetadata.getIndex());
}
}

var aliasedIndices = this.aliasedIndices.build();
for (String alias : aliasedIndicesBuilder.keys()) {
aliasedIndicesBuilder.put(alias, Collections.unmodifiableSet(aliasedIndicesBuilder.get(alias)));
}
var aliasedIndices = aliasedIndicesBuilder.build();
for (var entry : aliasedIndices.entrySet()) {
List<IndexMetadata> aliasIndices = entry.getValue().stream().map(idx -> indicesMap.get(idx.getName())).toList();
validateAlias(entry.getKey(), aliasIndices);
Expand Down

0 comments on commit 52811ac

Please sign in to comment.