Skip to content

Commit

Permalink
Make ClusterInfo use immutable maps in all cases (#88447)
Browse files Browse the repository at this point in the history
This class's maps are used very hot in the disk threshold allocation
decider. Moving them from hppc maps to unmodifiable map wrapping
`HashMap` has led to a measurable slowdown in the many-shards benchmark
bootstrapping. Lets use immutable map copies here exclusively to make
performance outright better and more predictable via a single implementation.
  • Loading branch information
original-brownbear authored Jul 12, 2022
1 parent 47510ad commit 9ebbe1c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 26 deletions.
12 changes: 6 additions & 6 deletions server/src/main/java/org/elasticsearch/cluster/ClusterInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public ClusterInfo(
Map<ShardRouting, String> routingToDataPath,
Map<NodeAndPath, ReservedSpace> reservedSpace
) {
this.leastAvailableSpaceUsage = leastAvailableSpaceUsage;
this.shardSizes = shardSizes;
this.shardDataSetSizes = shardDataSetSizes;
this.mostAvailableSpaceUsage = mostAvailableSpaceUsage;
this.routingToDataPath = routingToDataPath;
this.reservedSpace = reservedSpace;
this.leastAvailableSpaceUsage = Map.copyOf(leastAvailableSpaceUsage);
this.shardSizes = Map.copyOf(shardSizes);
this.shardDataSetSizes = Map.copyOf(shardDataSetSizes);
this.mostAvailableSpaceUsage = Map.copyOf(mostAvailableSpaceUsage);
this.routingToDataPath = Map.copyOf(routingToDataPath);
this.reservedSpace = Map.copyOf(reservedSpace);
}

public ClusterInfo(StreamInput in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.elasticsearch.threadpool.ThreadPool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -188,8 +187,8 @@ public void onResponse(NodesStatsResponse nodesStatsResponse) {
leastAvailableUsagesBuilder,
mostAvailableUsagesBuilder
);
leastAvailableSpaceUsages = Collections.unmodifiableMap(leastAvailableUsagesBuilder);
mostAvailableSpaceUsages = Collections.unmodifiableMap(mostAvailableUsagesBuilder);
leastAvailableSpaceUsages = Map.copyOf(leastAvailableUsagesBuilder);
mostAvailableSpaceUsages = Map.copyOf(mostAvailableUsagesBuilder);
}

@Override
Expand Down Expand Up @@ -262,10 +261,10 @@ public void onResponse(IndicesStatsResponse indicesStatsResponse) {
reservedSpaceBuilders.forEach((nodeAndPath, builder) -> rsrvdSpace.put(nodeAndPath, builder.build()));

indicesStatsSummary = new IndicesStatsSummary(
Collections.unmodifiableMap(shardSizeByIdentifierBuilder),
Collections.unmodifiableMap(shardDataSetSizeBuilder),
Collections.unmodifiableMap(dataPathByShardRoutingBuilder),
Collections.unmodifiableMap(rsrvdSpace)
Map.copyOf(shardSizeByIdentifierBuilder),
Map.copyOf(shardDataSetSizeBuilder),
Map.copyOf(dataPathByShardRoutingBuilder),
Map.copyOf(rsrvdSpace)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ private static ClusterInfo clusterInfo(
Map<String, DiskUsage> diskUsages,
Map<ClusterInfo.NodeAndPath, ClusterInfo.ReservedSpace> reservedSpace
) {
return new ClusterInfo(diskUsages, null, null, null, null, reservedSpace);
return new ClusterInfo(diskUsages, Map.of(), Map.of(), Map.of(), Map.of(), reservedSpace);
}

private static DiscoveryNode newFrozenOnlyNode(String nodeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ static class DevNullClusterInfo extends ClusterInfo {
Map<String, Long> shardSizes,
Map<NodeAndPath, ReservedSpace> reservedSpace
) {
super(leastAvailableSpaceUsage, mostAvailableSpaceUsage, shardSizes, null, null, reservedSpace);
super(leastAvailableSpaceUsage, mostAvailableSpaceUsage, shardSizes, Map.of(), Map.of(), reservedSpace);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testCanAllocateUsesMaxAvailableSpace() {
leastAvailableUsages,
mostAvailableUsage,
Map.of("[test][0][p]", 10L), // 10 bytes,
null,
Map.of(),
Map.of(),
Map.of()
);
Expand Down Expand Up @@ -185,7 +185,7 @@ public void testCannotAllocateDueToLackOfDiskResources() {
leastAvailableUsages,
mostAvailableUsage,
Map.of("[test][0][p]", shardSize),
null,
Map.of(),
Map.of(),
Map.of()
);
Expand Down Expand Up @@ -307,7 +307,7 @@ public void testCanRemainUsesLeastAvailableSpace() {
leastAvailableUsages,
mostAvailableUsage,
shardSizes,
null,
Map.of(),
shardRoutingMap,
Map.of()
);
Expand Down Expand Up @@ -745,7 +745,7 @@ public void testDecidesYesIfWatermarksIgnored() {
allFullUsages,
allFullUsages,
Map.of("[test][0][p]", 10L),
null,
Map.of(),
Map.of(),
Map.of()
);
Expand Down Expand Up @@ -815,7 +815,7 @@ public void testCannotForceAllocateOver100PercentUsage() {
// bigger than available space
final long shardSize = randomIntBetween(1, 10);
shardSizes.put("[test][0][p]", shardSize);
ClusterInfo clusterInfo = new ClusterInfo(leastAvailableUsages, mostAvailableUsage, shardSizes, null, Map.of(), Map.of());
ClusterInfo clusterInfo = new ClusterInfo(leastAvailableUsages, mostAvailableUsage, shardSizes, Map.of(), Map.of(), Map.of());
RoutingAllocation allocation = new RoutingAllocation(
new AllocationDeciders(Collections.singleton(decider)),
clusterState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,14 @@ private static class ExtendedClusterInfo extends ClusterInfo {
private final ClusterInfo delegate;

private ExtendedClusterInfo(Map<String, Long> extraShardSizes, ClusterInfo info) {
super(info.getNodeLeastAvailableDiskUsages(), info.getNodeMostAvailableDiskUsages(), extraShardSizes, Map.of(), null, null);
super(
info.getNodeLeastAvailableDiskUsages(),
info.getNodeMostAvailableDiskUsages(),
extraShardSizes,
Map.of(),
Map.of(),
Map.of()
);
this.delegate = info;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void testContext() {
}
}
state = ClusterState.builder(ClusterName.DEFAULT).nodes(nodes).build();
info = new ClusterInfo(leastUsages, mostUsages, null, null, null, null);
info = new ClusterInfo(leastUsages, mostUsages, Map.of(), Map.of(), Map.of(), Map.of());
context = new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(
roleNames,
state,
Expand Down Expand Up @@ -306,7 +306,7 @@ public void testContext() {
)
);

info = new ClusterInfo(leastUsages, mostUsages, null, null, null, null);
info = new ClusterInfo(leastUsages, mostUsages, Map.of(), Map.of(), Map.of(), Map.of());
context = new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(
roleNames,
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Tuple<Long, ClusterInfo> sizeAndClusterInfo(IndexMetadata indexMetadata)
// add irrelevant shards noise for completeness (should not happen IRL).
sizes.put(new ShardId(index, i), randomLongBetween(0, Integer.MAX_VALUE));
}
ClusterInfo info = new ClusterInfo(null, null, null, sizes, null, null);
ClusterInfo info = new ClusterInfo(Map.of(), Map.of(), Map.of(), sizes, Map.of(), Map.of());
return Tuple.tuple(totalSize, info);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public void validateSizeOf(ClusterState clusterState, ShardRouting subjectShard,
}

private ReactiveStorageDeciderService.AllocationState createAllocationState(Map<String, Long> shardSize, ClusterState clusterState) {
ClusterInfo info = new ClusterInfo(null, null, shardSize, null, null, null);
ClusterInfo info = new ClusterInfo(Map.of(), Map.of(), shardSize, Map.of(), Map.of(), Map.of());
ReactiveStorageDeciderService.AllocationState allocationState = new ReactiveStorageDeciderService.AllocationState(
clusterState,
null,
Expand Down Expand Up @@ -567,7 +567,7 @@ public void testUnmovableSize() {
if (shardsWithSizes.isEmpty() == false) {
shardSize.put(shardIdentifier(randomFrom(shardsWithSizes)), ByteSizeUnit.KB.toBytes(minShardSize));
}
ClusterInfo info = new ClusterInfo(diskUsages, diskUsages, shardSize, null, null, null);
ClusterInfo info = new ClusterInfo(diskUsages, diskUsages, shardSize, Map.of(), Map.of(), Map.of());

ReactiveStorageDeciderService.AllocationState allocationState = new ReactiveStorageDeciderService.AllocationState(
clusterState,
Expand Down

0 comments on commit 9ebbe1c

Please sign in to comment.