From 2a13b648fa7b2d763024577a6508222895ffa751 Mon Sep 17 00:00:00 2001 From: Mark Robert Miller Date: Tue, 21 Feb 2012 22:30:33 +0000 Subject: [PATCH] fix a concurrency issue around writing hashmap to json git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1292064 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/solr/cloud/ZkController.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.java index 8d3027f6b08..b5f4225f8bc 100644 --- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java +++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java @@ -85,7 +85,7 @@ public final class ZkController { public final static String COLLECTION_PARAM_PREFIX="collection."; public final static String CONFIGNAME_PROP="configName"; - private final Map coreStates = Collections.synchronizedMap(new HashMap()); + private final Map coreStates = new HashMap(); private SolrZkClient zkClient; private ZkCmdExecutor cmdExecutor; @@ -900,22 +900,25 @@ public final class ZkController { } CoreState coreState = new CoreState(coreName, cloudDesc.getCollectionName(), props, numShards); - coreStates.put(shardZkNodeName, coreState); final String nodePath = "/node_states/" + getNodeName(); - - try { - zkClient.setData(nodePath, ZkStateReader.toJSON(coreStates.values()), - true); - - } catch (KeeperException e) { - throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, - "could not publish node state", e); - } catch (InterruptedException e) { - // Restore the interrupted status - Thread.currentThread().interrupt(); - throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, - "could not publish node state", e); + + synchronized (coreStates) { + coreStates.put(shardZkNodeName, coreState); + try { + zkClient.setData(nodePath, ZkStateReader.toJSON(coreStates.values()), + true); + + } catch (KeeperException e) { + throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, + "could not publish node state", e); + } catch (InterruptedException e) { + // Restore the interrupted status + Thread.currentThread().interrupt(); + throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, + "could not publish node state", e); + } } + } private String doGetShardIdProcess(String coreName, CloudDescriptor descriptor)