Skip to content

Commit af9c0da

Browse files
Use cheaper map for RoutingNode.shardsByIndex (#87599)
No need for preserving order on this map, it's only used for counting shards of a specific index per node.
1 parent 0e3a6dd commit af9c0da

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.ArrayList;
1818
import java.util.Collection;
1919
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.HashSet;
2022
import java.util.Iterator;
2123
import java.util.LinkedHashMap;
2224
import java.util.LinkedHashSet;
@@ -44,15 +46,15 @@ public class RoutingNode implements Iterable<ShardRouting> {
4446

4547
private final LinkedHashSet<ShardRouting> relocatingShards;
4648

47-
private final Map<Index, LinkedHashSet<ShardRouting>> shardsByIndex;
49+
private final Map<Index, Set<ShardRouting>> shardsByIndex;
4850

4951
RoutingNode(String nodeId, @Nullable DiscoveryNode node) {
5052
this.nodeId = nodeId;
5153
this.node = node;
5254
this.shards = new LinkedHashMap<>();
5355
this.relocatingShards = new LinkedHashSet<>();
5456
this.initializingShards = new LinkedHashSet<>();
55-
this.shardsByIndex = new LinkedHashMap<>();
57+
this.shardsByIndex = new HashMap<>();
5658
assert invariant();
5759
}
5860

@@ -62,9 +64,9 @@ private RoutingNode(RoutingNode original) {
6264
this.shards = new LinkedHashMap<>(original.shards);
6365
this.relocatingShards = new LinkedHashSet<>(original.relocatingShards);
6466
this.initializingShards = new LinkedHashSet<>(original.initializingShards);
65-
this.shardsByIndex = Maps.newLinkedHashMapWithExpectedSize(original.shardsByIndex.size());
66-
for (Map.Entry<Index, LinkedHashSet<ShardRouting>> entry : original.shardsByIndex.entrySet()) {
67-
shardsByIndex.put(entry.getKey(), new LinkedHashSet<>(entry.getValue()));
67+
this.shardsByIndex = Maps.newMapWithExpectedSize(original.shardsByIndex.size());
68+
for (Map.Entry<Index, Set<ShardRouting>> entry : original.shardsByIndex.entrySet()) {
69+
shardsByIndex.put(entry.getKey(), new HashSet<>(entry.getValue()));
6870
}
6971
assert invariant();
7072
}
@@ -133,7 +135,7 @@ void add(ShardRouting shard) {
133135
} else if (shard.relocating()) {
134136
relocatingShards.add(shard);
135137
}
136-
shardsByIndex.computeIfAbsent(shard.index(), k -> new LinkedHashSet<>()).add(shard);
138+
shardsByIndex.computeIfAbsent(shard.index(), k -> new HashSet<>()).add(shard);
137139
assert invariant();
138140
}
139141

@@ -154,16 +156,14 @@ void update(ShardRouting oldShard, ShardRouting newShard) {
154156
boolean exist = relocatingShards.remove(oldShard);
155157
assert exist : "expected shard " + oldShard + " to exist in relocatingShards";
156158
}
157-
shardsByIndex.get(oldShard.index()).remove(oldShard);
158-
if (shardsByIndex.get(oldShard.index()).isEmpty()) {
159-
shardsByIndex.remove(oldShard.index());
160-
}
159+
final Set<ShardRouting> byIndex = shardsByIndex.get(oldShard.index());
160+
byIndex.remove(oldShard);
161+
byIndex.add(newShard);
161162
if (newShard.initializing()) {
162163
initializingShards.add(newShard);
163164
} else if (newShard.relocating()) {
164165
relocatingShards.add(newShard);
165166
}
166-
shardsByIndex.computeIfAbsent(newShard.index(), k -> new LinkedHashSet<>()).add(newShard);
167167
assert invariant();
168168
}
169169

@@ -178,8 +178,9 @@ void remove(ShardRouting shard) {
178178
boolean exist = relocatingShards.remove(shard);
179179
assert exist : "expected shard " + shard + " to exist in relocatingShards";
180180
}
181-
shardsByIndex.get(shard.index()).remove(shard);
182-
if (shardsByIndex.get(shard.index()).isEmpty()) {
181+
final Set<ShardRouting> byIndex = shardsByIndex.get(shard.index());
182+
byIndex.remove(shard);
183+
if (byIndex.isEmpty()) {
183184
shardsByIndex.remove(shard.index());
184185
}
185186
assert invariant();
@@ -285,7 +286,7 @@ public int numberOfOwningShards() {
285286
}
286287

287288
public int numberOfOwningShardsForIndex(final Index index) {
288-
final LinkedHashSet<ShardRouting> shardRoutings = shardsByIndex.get(index);
289+
final Set<ShardRouting> shardRoutings = shardsByIndex.get(index);
289290
if (shardRoutings == null) {
290291
return 0;
291292
} else {

0 commit comments

Comments
 (0)