Skip to content

Commit 5e1b6de

Browse files
committed
Use HashMap for IndexMetadata.inSyncAllocationIds
The in sync allocation ids is a mapping from shard to the set of ids currently being processed. When being built, the metadata uses a sparse map, only filling a value for a shard as they are put into the builder. When the final metadata is built, the map is made dense. This commit converts to using a HashMap instead of ImmutableOpenIntMap. The boxed keys should not cause allocations, as long as number of shards is lower than 128. Long term the map itself should become an array, as we know the number of shards (much like the dense primaryTerms array here). However, that change will be a little trickier to make, since we will need to be backward compatible with how diffs are built, currently using Map differences. relates elastic#86239
1 parent 3e02748 commit 5e1b6de

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadata.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.Arrays;
5959
import java.util.Collections;
6060
import java.util.EnumSet;
61+
import java.util.HashMap;
6162
import java.util.HashSet;
6263
import java.util.Iterator;
6364
import java.util.List;
@@ -516,7 +517,7 @@ public Iterator<Setting<?>> settings() {
516517

517518
private final ImmutableOpenMap<String, DiffableStringMap> customData;
518519

519-
private final ImmutableOpenIntMap<Set<String>> inSyncAllocationIds;
520+
private final Map<Integer, Set<String>> inSyncAllocationIds;
520521

521522
private final transient int totalNumberOfShards;
522523

@@ -571,7 +572,7 @@ private IndexMetadata(
571572
final MappingMetadata mapping,
572573
final ImmutableOpenMap<String, AliasMetadata> aliases,
573574
final ImmutableOpenMap<String, DiffableStringMap> customData,
574-
final ImmutableOpenIntMap<Set<String>> inSyncAllocationIds,
575+
final Map<Integer, Set<String>> inSyncAllocationIds,
575576
final DiscoveryNodeFilters requireFilters,
576577
final DiscoveryNodeFilters initialRecoveryFilters,
577578
final DiscoveryNodeFilters includeFilters,
@@ -889,7 +890,7 @@ public Map<String, String> getCustomData(final String key) {
889890
return this.customData.get(key);
890891
}
891892

892-
public ImmutableOpenIntMap<Set<String>> getInSyncAllocationIds() {
893+
public Map<Integer, Set<String>> getInSyncAllocationIds() {
893894
return inSyncAllocationIds;
894895
}
895896

@@ -1031,7 +1032,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
10311032
private final Diff<ImmutableOpenMap<String, MappingMetadata>> mappings;
10321033
private final Diff<ImmutableOpenMap<String, AliasMetadata>> aliases;
10331034
private final Diff<ImmutableOpenMap<String, DiffableStringMap>> customData;
1034-
private final Diff<ImmutableOpenIntMap<Set<String>>> inSyncAllocationIds;
1035+
private final Diff<Map<Integer, Set<String>>> inSyncAllocationIds;
10351036
private final Diff<ImmutableOpenMap<String, RolloverInfo>> rolloverInfos;
10361037
private final boolean isSystem;
10371038
private final IndexLongFieldRange timestampRange;
@@ -1095,7 +1096,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
10951096
mappings = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), MAPPING_DIFF_VALUE_READER);
10961097
aliases = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), ALIAS_METADATA_DIFF_VALUE_READER);
10971098
customData = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), CUSTOM_DIFF_VALUE_READER);
1098-
inSyncAllocationIds = DiffableUtils.readImmutableOpenIntMapDiff(
1099+
inSyncAllocationIds = DiffableUtils.readJdkMapDiff(
10991100
in,
11001101
DiffableUtils.getVIntKeySerializer(),
11011102
DiffableUtils.StringSetValueSerializer.getInstance()
@@ -1153,7 +1154,7 @@ public IndexMetadata apply(IndexMetadata part) {
11531154
).get(MapperService.SINGLE_MAPPING_NAME);
11541155
builder.aliases.putAllFromMap(aliases.apply(part.aliases));
11551156
builder.customMetadata.putAllFromMap(customData.apply(part.customData));
1156-
builder.inSyncAllocationIds.putAll((Map<Integer, Set<String>>) inSyncAllocationIds.apply(part.inSyncAllocationIds));
1157+
builder.inSyncAllocationIds.putAll(inSyncAllocationIds.apply(part.inSyncAllocationIds));
11571158
builder.rolloverInfos.putAllFromMap(rolloverInfos.apply(part.rolloverInfos));
11581159
builder.system(isSystem);
11591160
builder.timestampRange(timestampRange);
@@ -1309,7 +1310,7 @@ public static class Builder {
13091310
private MappingMetadata mapping;
13101311
private final ImmutableOpenMap.Builder<String, AliasMetadata> aliases;
13111312
private final ImmutableOpenMap.Builder<String, DiffableStringMap> customMetadata;
1312-
private final ImmutableOpenIntMap.Builder<Set<String>> inSyncAllocationIds;
1313+
private final Map<Integer, Set<String>> inSyncAllocationIds;
13131314
private final ImmutableOpenMap.Builder<String, RolloverInfo> rolloverInfos;
13141315
private Integer routingNumShards;
13151316
private boolean isSystem;
@@ -1320,7 +1321,7 @@ public Builder(String index) {
13201321
this.index = index;
13211322
this.aliases = ImmutableOpenMap.builder();
13221323
this.customMetadata = ImmutableOpenMap.builder();
1323-
this.inSyncAllocationIds = ImmutableOpenIntMap.builder();
1324+
this.inSyncAllocationIds = new HashMap<>();
13241325
this.rolloverInfos = ImmutableOpenMap.builder();
13251326
this.isSystem = false;
13261327
}
@@ -1338,7 +1339,7 @@ public Builder(IndexMetadata indexMetadata) {
13381339
this.aliases = ImmutableOpenMap.builder(indexMetadata.aliases);
13391340
this.customMetadata = ImmutableOpenMap.builder(indexMetadata.customData);
13401341
this.routingNumShards = indexMetadata.routingNumShards;
1341-
this.inSyncAllocationIds = ImmutableOpenIntMap.builder(indexMetadata.inSyncAllocationIds);
1342+
this.inSyncAllocationIds = new HashMap<>(indexMetadata.inSyncAllocationIds);
13421343
this.rolloverInfos = ImmutableOpenMap.builder(indexMetadata.rolloverInfos);
13431344
this.isSystem = indexMetadata.isSystem;
13441345
this.timestampRange = indexMetadata.timestampRange;
@@ -2019,7 +2020,7 @@ public static IndexMetadata legacyFromXContent(XContentParser parser) throws IOE
20192020
allocationIds.add(parser.text());
20202021
}
20212022
}
2022-
builder.putInSyncAllocationIds(Integer.valueOf(shardId), allocationIds);
2023+
builder.putInSyncAllocationIds(Integer.parseInt(shardId), allocationIds);
20232024
} else {
20242025
throw new IllegalArgumentException("Unexpected token: " + token);
20252026
}

0 commit comments

Comments
 (0)