From 52fd102e23929e3a006c822470e256126fb66f19 Mon Sep 17 00:00:00 2001 From: Andrey Ershov Date: Wed, 6 Mar 2019 13:49:24 +0100 Subject: [PATCH] Avoid serialising state if it was already serialised (#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 c99be63b43f5250f3cd220130df73c5e9e097459) --- .../cluster/coordination/PublicationTransportHandler.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java b/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java index b0b91cd0980..deaca572e97 100644 --- a/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java +++ b/server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java @@ -320,13 +320,17 @@ public class PublicationTransportHandler { } try { if (sendFullVersion || !previousState.nodes().nodeExists(node)) { - serializedStates.putIfAbsent(node.getVersion(), serializeFullClusterState(clusterState, node.getVersion())); + if (serializedStates.containsKey(node.getVersion()) == false) { + serializedStates.put(node.getVersion(), serializeFullClusterState(clusterState, node.getVersion())); + } } else { // will send a diff if (diff == null) { diff = clusterState.diff(previousState); } - serializedDiffs.putIfAbsent(node.getVersion(), serializeDiffClusterState(diff, node.getVersion())); + if (serializedDiffs.containsKey(node.getVersion()) == false) { + serializedDiffs.put(node.getVersion(), serializeDiffClusterState(diff, node.getVersion())); + } } } catch (IOException e) { throw new ElasticsearchException("failed to serialize cluster state for publishing to node {}", e, node);