Apache Solr open-source search software
SOLR-4158: When a core is registering in ZooKeeper it may not wait long enough to find the leader due to how long the potential leader waits to see replicas.
Mark Robert Millerab00c42eff2d
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex b9afe30600d..7d4de18afa0 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -283,6 +283,9 @@ Bug Fixesnot consider it's last published state when deciding if it can be the newleader. (Mark Miller)+* SOLR-4158: When a core is registering in ZooKeeper it may not wait long+ enough to find the leader due to how long the potential leader waits to see+ replicas. (Mark Miller, Alain Rogister)Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.javaindex da436c79c74..caa858c2e57 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java@@ -140,14 +140,6 @@ public final class ZkController {private int clientTimeout;-- public ZkController(final CoreContainer cc, String zkServerAddress, int zkClientTimeout, int zkClientConnectTimeout, String localHost, String locaHostPort,- String localHostContext, final CurrentCoreDescriptorProvider registerOnReconnect) throws InterruptedException,- TimeoutException, IOException {- this(cc, zkServerAddress, zkClientTimeout, zkClientConnectTimeout, localHost, locaHostPort, localHostContext, null, registerOnReconnect);- }--public ZkController(final CoreContainer cc, String zkServerAddress, int zkClientTimeout, int zkClientConnectTimeout, String localHost, String locaHostPort,String localHostContext, String leaderVoteWait, final CurrentCoreDescriptorProvider registerOnReconnect) throws InterruptedException,TimeoutException, IOException {@@ -601,7 +593,10 @@ public final class ZkController {throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e);}- String leaderUrl = getLeader(cloudDesc);++ // in this case, we want to wait for the leader as long as the leader might+ // wait for a vote, at least+ String leaderUrl = getLeader(cloudDesc, Integer.parseInt(leaderVoteWait) + 1000);String ourUrl = ZkCoreNodeProps.getCoreUrl(baseUrl, coreName);log.info("We are " + ourUrl + " and leader is " + leaderUrl);@@ -653,7 +648,9 @@ public final class ZkController {return shardId;}- private String getLeader(final CloudDescriptor cloudDesc) {+ // timeoutms is the timeout for the first call to get the leader - there is then+ // a longer wait to make sure that leader matches our local state+ private String getLeader(final CloudDescriptor cloudDesc, int timeoutms) {String collection = cloudDesc.getCollectionName();String shardId = cloudDesc.getShardId();@@ -662,7 +659,7 @@ public final class ZkController {// cluster state node that won't be updated for a momentString leaderUrl;try {- leaderUrl = getLeaderProps(collection, cloudDesc.getShardId())+ leaderUrl = getLeaderProps(collection, cloudDesc.getShardId(), timeoutms).getCoreUrl();// now wait until our currently cloud state contains the latest leader@@ -680,7 +677,7 @@ public final class ZkController {tries++;clusterStateLeader = zkStateReader.getLeaderUrl(collection, shardId,30000);- leaderUrl = getLeaderProps(collection, cloudDesc.getShardId())+ leaderUrl = getLeaderProps(collection, cloudDesc.getShardId(), timeoutms).getCoreUrl();}@@ -696,8 +693,8 @@ public final class ZkController {* Get leader props directly from zk nodes.*/public ZkCoreNodeProps getLeaderProps(final String collection,- final String slice) throws InterruptedException {- return getLeaderProps(collection, slice, false);+ final String slice, int timeoutms) throws InterruptedException {+ return getLeaderProps(collection, slice, timeoutms, false);}/**@@ -706,8 +703,8 @@ public final class ZkController {* @return leader props*/public ZkCoreNodeProps getLeaderProps(final String collection,- final String slice, boolean failImmediatelyOnExpiration) throws InterruptedException {- int iterCount = 60;+ final String slice, int timeoutms, boolean failImmediatelyOnExpiration) throws InterruptedException {+ int iterCount = timeoutms / 1000;Exception exp = null;while (iterCount-- > 0) {try {@@ -724,10 +721,10 @@ public final class ZkController {throw new RuntimeException("Session has expired - could not get leader props", exp);}exp = e;- Thread.sleep(500);+ Thread.sleep(1000);} catch (Exception e) {exp = e;- Thread.sleep(500);+ Thread.sleep(1000);}if (cc.isShutDown()) {throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "CoreContainer is shutdown");@@ -805,6 +802,7 @@ public final class ZkController {* Publish core state to overseer.*/public void publish(final CoreDescriptor cd, final String state, boolean updateLastState) throws KeeperException, InterruptedException {+ log.info("publishing core={} state={}", cd.getName(), state);//System.out.println(Thread.currentThread().getStackTrace()[3]);Integer numShards = cd.getCloudDescriptor().getNumShards();if (numShards == null) { //XXX sys prop hack@@ -1100,7 +1098,7 @@ public final class ZkController {for (int i = 0; i < retries; i++) {try {// go straight to zk, not the cloud state - we must have current info- leaderProps = getLeaderProps(collection, shard);+ leaderProps = getLeaderProps(collection, shard, 30000);break;} catch (Exception e) {SolrException.log(log, "There was a problem finding the leader in zk", e);diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex 88bea2ac934..888f8299d50 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -158,7 +158,7 @@ public class CoreContainerprotected LogWatcher logging = null;private String zkHost;private Map<SolrCore,String> coreToOrigName = new ConcurrentHashMap<SolrCore,String>();- private String leaderVoteWait;+ private String leaderVoteWait = LEADER_VOTE_WAIT;protected int swappableCacheSize = Integer.MAX_VALUE; // Use as a flag too, if swappableCacheSize set in solr.xml this will be changedprivate int coreLoadThreads;diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java b/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.javaindex d6bb54b198e..34879322279 100644--- a/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java@@ -82,7 +82,7 @@ public class ZkControllerTest extends SolrTestCaseJ4 {cc = getCoreContainer();ZkController zkController = new ZkController(cc, server.getZkAddress(), TIMEOUT, 10000,- "127.0.0.1", "8983", "solr", new CurrentCoreDescriptorProvider() {+ "127.0.0.1", "8983", "solr", "0", new CurrentCoreDescriptorProvider() {@Overridepublic List<CoreDescriptor> getCurrentDescriptors() {@@ -122,7 +122,7 @@ public class ZkControllerTest extends SolrTestCaseJ4 {cc = getCoreContainer();zkController = new ZkController(cc, server.getZkAddress(),- TIMEOUT, 10000, "127.0.0.1", "8983", "solr", new CurrentCoreDescriptorProvider() {+ TIMEOUT, 10000, "127.0.0.1", "8983", "solr", "0", new CurrentCoreDescriptorProvider() {@Overridepublic List<CoreDescriptor> getCurrentDescriptors() {diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.java b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.javaindex 9a3f00a4e95..87ba269983a 100644--- a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.java+++ b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.java@@ -62,7 +62,6 @@ public abstract class AbstractZkTestCase extends SolrTestCaseJ4 {@BeforeClasspublic static void azt_beforeClass() throws Exception {- System.out.println("azt beforeclass");createTempDir();zkDir = dataDir.getAbsolutePath() + File.separator+ "zookeeper/server1/data";