Skip to content

Commit c99be63

Browse files
authored
Avoid serialising state if it was already serialised (elastic#39179)
When preparing the state to send to other nodes, we're serializing it for each node, despite using putIfAbsent. This commit checks if the state was already serialized for this node version before performing the potentially expensive computation. The map is not used by multiple threads, so computeIfAbsent is not needed (and could not be used here easily, because IOException could be thrown).
1 parent 98bbb41 commit c99be63

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,17 @@ private static void buildDiffAndSerializeStates(ClusterState clusterState, Clust
273273
}
274274
try {
275275
if (sendFullVersion || !previousState.nodes().nodeExists(node)) {
276-
serializedStates.putIfAbsent(node.getVersion(), serializeFullClusterState(clusterState, node.getVersion()));
276+
if (serializedStates.containsKey(node.getVersion()) == false) {
277+
serializedStates.put(node.getVersion(), serializeFullClusterState(clusterState, node.getVersion()));
278+
}
277279
} else {
278280
// will send a diff
279281
if (diff == null) {
280282
diff = clusterState.diff(previousState);
281283
}
282-
serializedDiffs.putIfAbsent(node.getVersion(), serializeDiffClusterState(diff, node.getVersion()));
284+
if (serializedDiffs.containsKey(node.getVersion()) == false) {
285+
serializedDiffs.put(node.getVersion(), serializeDiffClusterState(diff, node.getVersion()));
286+
}
283287
}
284288
} catch (IOException e) {
285289
throw new ElasticsearchException("failed to serialize cluster state for publishing to node {}", e, node);

0 commit comments

Comments
 (0)