Skip to content

Commit f627bdd

Browse files
authored
Use HashMap inside cluster info service (#87899)
The InternalClusterInfoService internally uses ImmutableOpenMap for keeping track of available space on each node. This commit converts those usages to use HashMap. Note that an unmodifiableMap wrapper is used because updates to this (from each node) are likely to happen often as disk is used. relates #86239
1 parent 312ddd6 commit f627bdd

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

server/src/main/java/org/elasticsearch/cluster/InternalClusterInfoService.java

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.cluster.routing.ShardRouting;
2828
import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings;
2929
import org.elasticsearch.cluster.service.ClusterService;
30-
import org.elasticsearch.common.collect.ImmutableOpenMap;
3130
import org.elasticsearch.common.settings.ClusterSettings;
3231
import org.elasticsearch.common.settings.Setting;
3332
import org.elasticsearch.common.settings.Setting.Property;
@@ -40,6 +39,7 @@
4039
import org.elasticsearch.threadpool.ThreadPool;
4140

4241
import java.util.ArrayList;
42+
import java.util.Collections;
4343
import java.util.HashMap;
4444
import java.util.HashSet;
4545
import java.util.List;
@@ -83,8 +83,8 @@ public class InternalClusterInfoService implements ClusterInfoService, ClusterSt
8383
private volatile TimeValue updateFrequency;
8484
private volatile TimeValue fetchTimeout;
8585

86-
private volatile ImmutableOpenMap<String, DiskUsage> leastAvailableSpaceUsages;
87-
private volatile ImmutableOpenMap<String, DiskUsage> mostAvailableSpaceUsages;
86+
private volatile Map<String, DiskUsage> leastAvailableSpaceUsages;
87+
private volatile Map<String, DiskUsage> mostAvailableSpaceUsages;
8888
private volatile IndicesStatsSummary indicesStatsSummary;
8989

9090
private final ThreadPool threadPool;
@@ -97,8 +97,8 @@ public class InternalClusterInfoService implements ClusterInfoService, ClusterSt
9797
private RefreshScheduler refreshScheduler;
9898

9999
public InternalClusterInfoService(Settings settings, ClusterService clusterService, ThreadPool threadPool, Client client) {
100-
this.leastAvailableSpaceUsages = ImmutableOpenMap.of();
101-
this.mostAvailableSpaceUsages = ImmutableOpenMap.of();
100+
this.leastAvailableSpaceUsages = Map.of();
101+
this.mostAvailableSpaceUsages = Map.of();
102102
this.indicesStatsSummary = IndicesStatsSummary.EMPTY;
103103
this.threadPool = threadPool;
104104
this.client = client;
@@ -181,15 +181,15 @@ public void onResponse(NodesStatsResponse nodesStatsResponse) {
181181
logger.warn(() -> "failed to retrieve stats for node [" + failure.nodeId() + "]", failure.getCause());
182182
}
183183

184-
ImmutableOpenMap.Builder<String, DiskUsage> leastAvailableUsagesBuilder = ImmutableOpenMap.builder();
185-
ImmutableOpenMap.Builder<String, DiskUsage> mostAvailableUsagesBuilder = ImmutableOpenMap.builder();
184+
Map<String, DiskUsage> leastAvailableUsagesBuilder = new HashMap<>();
185+
Map<String, DiskUsage> mostAvailableUsagesBuilder = new HashMap<>();
186186
fillDiskUsagePerNode(
187187
adjustNodesStats(nodesStatsResponse.getNodes()),
188188
leastAvailableUsagesBuilder,
189189
mostAvailableUsagesBuilder
190190
);
191-
leastAvailableSpaceUsages = leastAvailableUsagesBuilder.build();
192-
mostAvailableSpaceUsages = mostAvailableUsagesBuilder.build();
191+
leastAvailableSpaceUsages = Collections.unmodifiableMap(leastAvailableUsagesBuilder);
192+
mostAvailableSpaceUsages = Collections.unmodifiableMap(mostAvailableUsagesBuilder);
193193
}
194194

195195
@Override
@@ -199,8 +199,8 @@ public void onFailure(Exception e) {
199199
} else {
200200
logger.warn("failed to retrieve node stats", e);
201201
}
202-
leastAvailableSpaceUsages = ImmutableOpenMap.of();
203-
mostAvailableSpaceUsages = ImmutableOpenMap.of();
202+
leastAvailableSpaceUsages = Map.of();
203+
mostAvailableSpaceUsages = Map.of();
204204
}
205205
}, this::onStatsProcessed));
206206

@@ -246,9 +246,9 @@ public void onResponse(IndicesStatsResponse indicesStatsResponse) {
246246
}
247247

248248
final ShardStats[] stats = indicesStatsResponse.getShards();
249-
final ImmutableOpenMap.Builder<String, Long> shardSizeByIdentifierBuilder = ImmutableOpenMap.builder();
250-
final ImmutableOpenMap.Builder<ShardId, Long> shardDataSetSizeBuilder = ImmutableOpenMap.builder();
251-
final ImmutableOpenMap.Builder<ShardRouting, String> dataPathByShardRoutingBuilder = ImmutableOpenMap.builder();
249+
final Map<String, Long> shardSizeByIdentifierBuilder = new HashMap<>();
250+
final Map<ShardId, Long> shardDataSetSizeBuilder = new HashMap<>();
251+
final Map<ShardRouting, String> dataPathByShardRoutingBuilder = new HashMap<>();
252252
final Map<ClusterInfo.NodeAndPath, ClusterInfo.ReservedSpace.Builder> reservedSpaceBuilders = new HashMap<>();
253253
buildShardLevelInfo(
254254
stats,
@@ -258,15 +258,14 @@ public void onResponse(IndicesStatsResponse indicesStatsResponse) {
258258
reservedSpaceBuilders
259259
);
260260

261-
final ImmutableOpenMap.Builder<ClusterInfo.NodeAndPath, ClusterInfo.ReservedSpace> rsrvdSpace = ImmutableOpenMap
262-
.builder();
261+
final Map<ClusterInfo.NodeAndPath, ClusterInfo.ReservedSpace> rsrvdSpace = new HashMap<>();
263262
reservedSpaceBuilders.forEach((nodeAndPath, builder) -> rsrvdSpace.put(nodeAndPath, builder.build()));
264263

265264
indicesStatsSummary = new IndicesStatsSummary(
266-
shardSizeByIdentifierBuilder.build(),
267-
shardDataSetSizeBuilder.build(),
268-
dataPathByShardRoutingBuilder.build(),
269-
rsrvdSpace.build()
265+
Collections.unmodifiableMap(shardSizeByIdentifierBuilder),
266+
Collections.unmodifiableMap(shardDataSetSizeBuilder),
267+
Collections.unmodifiableMap(dataPathByShardRoutingBuilder),
268+
Collections.unmodifiableMap(rsrvdSpace)
270269
);
271270
}
272271

@@ -342,8 +341,8 @@ private Runnable getNewRefresh() {
342341
return currentRefresh::execute;
343342
} else {
344343
return () -> {
345-
leastAvailableSpaceUsages = ImmutableOpenMap.of();
346-
mostAvailableSpaceUsages = ImmutableOpenMap.of();
344+
leastAvailableSpaceUsages = Map.of();
345+
mostAvailableSpaceUsages = Map.of();
347346
indicesStatsSummary = IndicesStatsSummary.EMPTY;
348347
thisRefreshListeners.forEach(l -> l.onResponse(ClusterInfo.EMPTY));
349348
};
@@ -413,9 +412,9 @@ public void addListener(Consumer<ClusterInfo> clusterInfoConsumer) {
413412

414413
static void buildShardLevelInfo(
415414
ShardStats[] stats,
416-
ImmutableOpenMap.Builder<String, Long> shardSizes,
417-
ImmutableOpenMap.Builder<ShardId, Long> shardDataSetSizeBuilder,
418-
ImmutableOpenMap.Builder<ShardRouting, String> newShardRoutingToDataPath,
415+
Map<String, Long> shardSizes,
416+
Map<ShardId, Long> shardDataSetSizeBuilder,
417+
Map<ShardRouting, String> newShardRoutingToDataPath,
419418
Map<ClusterInfo.NodeAndPath, ClusterInfo.ReservedSpace.Builder> reservedSpaceByShard
420419
) {
421420
for (ShardStats s : stats) {
@@ -448,8 +447,8 @@ static void buildShardLevelInfo(
448447

449448
static void fillDiskUsagePerNode(
450449
List<NodeStats> nodeStatsArray,
451-
ImmutableOpenMap.Builder<String, DiskUsage> newLeastAvailableUsages,
452-
ImmutableOpenMap.Builder<String, DiskUsage> newMostAvailableUsages
450+
Map<String, DiskUsage> newLeastAvailableUsages,
451+
Map<String, DiskUsage> newMostAvailableUsages
453452
) {
454453
for (NodeStats nodeStats : nodeStatsArray) {
455454
if (nodeStats.getFs() == null) {
@@ -534,18 +533,12 @@ static void fillDiskUsagePerNode(
534533
}
535534

536535
private record IndicesStatsSummary(
537-
ImmutableOpenMap<String, Long> shardSizes,
538-
ImmutableOpenMap<ShardId, Long> shardDataSetSizes,
539-
ImmutableOpenMap<ShardRouting, String> shardRoutingToDataPath,
540-
ImmutableOpenMap<ClusterInfo.NodeAndPath, ClusterInfo.ReservedSpace> reservedSpace
536+
Map<String, Long> shardSizes,
537+
Map<ShardId, Long> shardDataSetSizes,
538+
Map<ShardRouting, String> shardRoutingToDataPath,
539+
Map<ClusterInfo.NodeAndPath, ClusterInfo.ReservedSpace> reservedSpace
541540
) {
542-
static final IndicesStatsSummary EMPTY = new IndicesStatsSummary(
543-
ImmutableOpenMap.of(),
544-
ImmutableOpenMap.of(),
545-
ImmutableOpenMap.of(),
546-
ImmutableOpenMap.of()
547-
);
548-
541+
static final IndicesStatsSummary EMPTY = new IndicesStatsSummary(Map.of(), Map.of(), Map.of(), Map.of());
549542
}
550543

551544
}

server/src/test/java/org/elasticsearch/cluster/DiskUsageTests.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.elasticsearch.cluster.routing.ShardRouting;
1818
import org.elasticsearch.cluster.routing.ShardRoutingHelper;
1919
import org.elasticsearch.cluster.routing.UnassignedInfo;
20-
import org.elasticsearch.common.collect.ImmutableOpenMap;
2120
import org.elasticsearch.index.Index;
2221
import org.elasticsearch.index.shard.ShardId;
2322
import org.elasticsearch.index.shard.ShardPath;
@@ -29,6 +28,7 @@
2928
import java.util.Arrays;
3029
import java.util.HashMap;
3130
import java.util.List;
31+
import java.util.Map;
3232

3333
import static java.util.Collections.emptyMap;
3434
import static java.util.Collections.emptySet;
@@ -116,10 +116,9 @@ public void testFillShardLevelInfo() {
116116
new ShardStats(test_0, new ShardPath(false, test0Path, test0Path, test_0.shardId()), commonStats0, null, null, null),
117117
new ShardStats(test_1, new ShardPath(false, test1Path, test1Path, test_1.shardId()), commonStats1, null, null, null),
118118
new ShardStats(test_1, new ShardPath(false, test1Path, test1Path, test_1.shardId()), commonStats2, null, null, null) };
119-
ImmutableOpenMap.Builder<String, Long> shardSizes = ImmutableOpenMap.builder();
120-
ImmutableOpenMap.Builder<ShardId, Long> shardDataSetSizes = ImmutableOpenMap.builder();
121-
ImmutableOpenMap.Builder<ShardRouting, String> routingToPath = ImmutableOpenMap.builder();
122-
ClusterState state = ClusterState.builder(new ClusterName("blarg")).version(0).build();
119+
Map<String, Long> shardSizes = new HashMap<>();
120+
Map<ShardId, Long> shardDataSetSizes = new HashMap<>();
121+
Map<ShardRouting, String> routingToPath = new HashMap<>();
123122
InternalClusterInfoService.buildShardLevelInfo(stats, shardSizes, shardDataSetSizes, routingToPath, new HashMap<>());
124123
assertEquals(2, shardSizes.size());
125124
assertTrue(shardSizes.containsKey(ClusterInfo.shardIdentifierFromRouting(test_0)));
@@ -141,8 +140,8 @@ public void testFillShardLevelInfo() {
141140
}
142141

143142
public void testFillDiskUsage() {
144-
ImmutableOpenMap.Builder<String, DiskUsage> newLeastAvaiableUsages = ImmutableOpenMap.builder();
145-
ImmutableOpenMap.Builder<String, DiskUsage> newMostAvaiableUsages = ImmutableOpenMap.builder();
143+
Map<String, DiskUsage> newLeastAvaiableUsages = new HashMap<>();
144+
Map<String, DiskUsage> newMostAvaiableUsages = new HashMap<>();
146145
FsInfo.Path[] node1FSInfo = new FsInfo.Path[] {
147146
new FsInfo.Path("/middle", "/dev/sda", 100, 90, 80),
148147
new FsInfo.Path("/least", "/dev/sdb", 200, 190, 70),
@@ -229,8 +228,8 @@ public void testFillDiskUsage() {
229228
}
230229

231230
public void testFillDiskUsageSomeInvalidValues() {
232-
ImmutableOpenMap.Builder<String, DiskUsage> newLeastAvailableUsages = ImmutableOpenMap.builder();
233-
ImmutableOpenMap.Builder<String, DiskUsage> newMostAvailableUsages = ImmutableOpenMap.builder();
231+
Map<String, DiskUsage> newLeastAvailableUsages = new HashMap<>();
232+
Map<String, DiskUsage> newMostAvailableUsages = new HashMap<>();
234233
FsInfo.Path[] node1FSInfo = new FsInfo.Path[] {
235234
new FsInfo.Path("/middle", "/dev/sda", 100, 90, 80),
236235
new FsInfo.Path("/least", "/dev/sdb", -1, -1, -1),

0 commit comments

Comments
 (0)