Skip to content

Commit 52fd102

Browse files
author
Andrey Ershov
committed
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). (cherry picked from commit c99be63)
1 parent 95409d3 commit 52fd102

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
@@ -320,13 +320,17 @@ private static void buildDiffAndSerializeStates(ClusterState clusterState, Clust
320320
}
321321
try {
322322
if (sendFullVersion || !previousState.nodes().nodeExists(node)) {
323-
serializedStates.putIfAbsent(node.getVersion(), serializeFullClusterState(clusterState, node.getVersion()));
323+
if (serializedStates.containsKey(node.getVersion()) == false) {
324+
serializedStates.put(node.getVersion(), serializeFullClusterState(clusterState, node.getVersion()));
325+
}
324326
} else {
325327
// will send a diff
326328
if (diff == null) {
327329
diff = clusterState.diff(previousState);
328330
}
329-
serializedDiffs.putIfAbsent(node.getVersion(), serializeDiffClusterState(diff, node.getVersion()));
331+
if (serializedDiffs.containsKey(node.getVersion()) == false) {
332+
serializedDiffs.put(node.getVersion(), serializeDiffClusterState(diff, node.getVersion()));
333+
}
330334
}
331335
} catch (IOException e) {
332336
throw new ElasticsearchException("failed to serialize cluster state for publishing to node {}", e, node);

0 commit comments

Comments
 (0)