Skip to content

Use HashMap for IndexMetadata.inSyncAllocationIds #86403

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
merged 4 commits into from
May 4, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.cluster.routing.allocation.IndexMetadataUpdater;
import org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider;
import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider;
import org.elasticsearch.common.collect.ImmutableOpenIntMap;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.compress.CompressedXContent;
Expand Down Expand Up @@ -58,6 +57,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -516,7 +516,7 @@ public Iterator<Setting<?>> settings() {

private final ImmutableOpenMap<String, DiffableStringMap> customData;

private final ImmutableOpenIntMap<Set<String>> inSyncAllocationIds;
private final Map<Integer, Set<String>> inSyncAllocationIds;

private final transient int totalNumberOfShards;

Expand Down Expand Up @@ -571,7 +571,7 @@ private IndexMetadata(
final MappingMetadata mapping,
final ImmutableOpenMap<String, AliasMetadata> aliases,
final ImmutableOpenMap<String, DiffableStringMap> customData,
final ImmutableOpenIntMap<Set<String>> inSyncAllocationIds,
final Map<Integer, Set<String>> inSyncAllocationIds,
final DiscoveryNodeFilters requireFilters,
final DiscoveryNodeFilters initialRecoveryFilters,
final DiscoveryNodeFilters includeFilters,
Expand Down Expand Up @@ -889,7 +889,7 @@ public Map<String, String> getCustomData(final String key) {
return this.customData.get(key);
}

public ImmutableOpenIntMap<Set<String>> getInSyncAllocationIds() {
public Map<Integer, Set<String>> getInSyncAllocationIds() {
return inSyncAllocationIds;
}

Expand Down Expand Up @@ -1031,7 +1031,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
private final Diff<ImmutableOpenMap<String, MappingMetadata>> mappings;
private final Diff<ImmutableOpenMap<String, AliasMetadata>> aliases;
private final Diff<ImmutableOpenMap<String, DiffableStringMap>> customData;
private final Diff<ImmutableOpenIntMap<Set<String>>> inSyncAllocationIds;
private final Diff<Map<Integer, Set<String>>> inSyncAllocationIds;
private final Diff<ImmutableOpenMap<String, RolloverInfo>> rolloverInfos;
private final boolean isSystem;
private final IndexLongFieldRange timestampRange;
Expand Down Expand Up @@ -1095,7 +1095,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
mappings = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), MAPPING_DIFF_VALUE_READER);
aliases = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), ALIAS_METADATA_DIFF_VALUE_READER);
customData = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), CUSTOM_DIFF_VALUE_READER);
inSyncAllocationIds = DiffableUtils.readImmutableOpenIntMapDiff(
inSyncAllocationIds = DiffableUtils.readJdkMapDiff(
in,
DiffableUtils.getVIntKeySerializer(),
DiffableUtils.StringSetValueSerializer.getInstance()
Expand Down Expand Up @@ -1153,7 +1153,7 @@ public IndexMetadata apply(IndexMetadata part) {
).get(MapperService.SINGLE_MAPPING_NAME);
builder.aliases.putAllFromMap(aliases.apply(part.aliases));
builder.customMetadata.putAllFromMap(customData.apply(part.customData));
builder.inSyncAllocationIds.putAll((Map<Integer, Set<String>>) inSyncAllocationIds.apply(part.inSyncAllocationIds));
builder.inSyncAllocationIds.putAll(inSyncAllocationIds.apply(part.inSyncAllocationIds));
builder.rolloverInfos.putAllFromMap(rolloverInfos.apply(part.rolloverInfos));
builder.system(isSystem);
builder.timestampRange(timestampRange);
Expand Down Expand Up @@ -1309,7 +1309,7 @@ public static class Builder {
private MappingMetadata mapping;
private final ImmutableOpenMap.Builder<String, AliasMetadata> aliases;
private final ImmutableOpenMap.Builder<String, DiffableStringMap> customMetadata;
private final ImmutableOpenIntMap.Builder<Set<String>> inSyncAllocationIds;
private final Map<Integer, Set<String>> inSyncAllocationIds;
private final ImmutableOpenMap.Builder<String, RolloverInfo> rolloverInfos;
private Integer routingNumShards;
private boolean isSystem;
Expand All @@ -1320,7 +1320,7 @@ public Builder(String index) {
this.index = index;
this.aliases = ImmutableOpenMap.builder();
this.customMetadata = ImmutableOpenMap.builder();
this.inSyncAllocationIds = ImmutableOpenIntMap.builder();
this.inSyncAllocationIds = new HashMap<>();
this.rolloverInfos = ImmutableOpenMap.builder();
this.isSystem = false;
}
Expand All @@ -1338,7 +1338,7 @@ public Builder(IndexMetadata indexMetadata) {
this.aliases = ImmutableOpenMap.builder(indexMetadata.aliases);
this.customMetadata = ImmutableOpenMap.builder(indexMetadata.customData);
this.routingNumShards = indexMetadata.routingNumShards;
this.inSyncAllocationIds = ImmutableOpenIntMap.builder(indexMetadata.inSyncAllocationIds);
this.inSyncAllocationIds = new HashMap<>(indexMetadata.inSyncAllocationIds);
this.rolloverInfos = ImmutableOpenMap.builder(indexMetadata.rolloverInfos);
this.isSystem = indexMetadata.isSystem;
this.timestampRange = indexMetadata.timestampRange;
Expand Down Expand Up @@ -1466,7 +1466,7 @@ public Set<String> getInSyncAllocationIds(int shardId) {
}

public Builder putInSyncAllocationIds(int shardId, Set<String> allocationIds) {
inSyncAllocationIds.put(shardId, new HashSet<>(allocationIds));
inSyncAllocationIds.put(shardId, Set.copyOf(allocationIds));
return this;
}

Expand Down Expand Up @@ -1595,13 +1595,11 @@ public IndexMetadata build() {
}

// fill missing slots in inSyncAllocationIds with empty set if needed and make all entries immutable
ImmutableOpenIntMap.Builder<Set<String>> filledInSyncAllocationIds = ImmutableOpenIntMap.builder();
@SuppressWarnings({ "unchecked", "rawtypes" })
Map.Entry<Integer, Set<String>> denseInSyncAllocationIds[] = new Map.Entry[numberOfShards];
for (int i = 0; i < numberOfShards; i++) {
if (inSyncAllocationIds.containsKey(i)) {
filledInSyncAllocationIds.put(i, Set.copyOf(inSyncAllocationIds.get(i)));
} else {
filledInSyncAllocationIds.put(i, Collections.emptySet());
}
Set<String> allocIds = inSyncAllocationIds.getOrDefault(i, Set.of());
denseInSyncAllocationIds[i] = Map.entry(i, allocIds);
}
var requireMap = INDEX_ROUTING_REQUIRE_GROUP_SETTING.getAsMap(settings);
final DiscoveryNodeFilters requireFilters;
Expand Down Expand Up @@ -1697,7 +1695,7 @@ public IndexMetadata build() {
mapping,
aliases.build(),
newCustomMetadata,
filledInSyncAllocationIds.build(),
Map.ofEntries(denseInSyncAllocationIds),
requireFilters,
initialRecoveryFilters,
includeFilters,
Expand Down Expand Up @@ -2019,7 +2017,7 @@ public static IndexMetadata legacyFromXContent(XContentParser parser) throws IOE
allocationIds.add(parser.text());
}
}
builder.putInSyncAllocationIds(Integer.valueOf(shardId), allocationIds);
builder.putInSyncAllocationIds(Integer.parseInt(shardId), allocationIds);
} else {
throw new IllegalArgumentException("Unexpected token: " + token);
}
Expand Down