Apache Solr open-source search software
SOLR-6159: A ZooKeeper session expiry during setup can keep LeaderElector from joining elections
Shalin Shekhar Mangar7c103dbf623f
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 22409319ae7..e0dca882c37 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -145,6 +145,9 @@ Bug Fixes* SOLR-6211: TrieDateField doesn't default to omitNorms=true. (Michael Ryan, Steve Rowe)+* SOLR-6159: A ZooKeeper session expiry during setup can keep LeaderElector from joining elections.+ (Steven Bower, shalin)+Other Changes---------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.javaindex 6cbc2b0f3d4..88e19a2108e 100644--- a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java+++ b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java@@ -51,7 +51,7 @@ public abstract class ElectionContext {final ZkNodeProps leaderProps;final String id;final String leaderPath;- String leaderSeqPath;+ volatile String leaderSeqPath;private SolrZkClient zkClient;public ElectionContext(final String coreNodeName,@@ -66,12 +66,16 @@ public abstract class ElectionContext {public void close() {}public void cancelElection() throws InterruptedException, KeeperException {- try {- log.info("canceling election {}",leaderSeqPath );- zkClient.delete(leaderSeqPath, -1, true);- } catch (NoNodeException e) {- // fine- log.warn("cancelElection did not find election node to remove {}" ,leaderSeqPath);+ if( leaderSeqPath != null ){+ try {+ log.info("canceling election {}",leaderSeqPath );+ zkClient.delete(leaderSeqPath, -1, true);+ } catch (NoNodeException e) {+ // fine+ log.warn("cancelElection did not find election node to remove {}" ,leaderSeqPath);+ }+ } else {+ log.warn("cancelElection skipped as this context has not been initialized");}}diff --git a/solr/core/src/java/org/apache/solr/cloud/LeaderElector.java b/solr/core/src/java/org/apache/solr/cloud/LeaderElector.javaindex 524680349da..2481e69e32a 100644--- a/solr/core/src/java/org/apache/solr/cloud/LeaderElector.java+++ b/solr/core/src/java/org/apache/solr/cloud/LeaderElector.java@@ -298,6 +298,7 @@ public class LeaderElector {// we must have failed in creating the election node - someone else must// be working on it, lets try againif (tries++ > 20) {+ context = null;throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,"", e);}@@ -372,10 +373,10 @@ public class LeaderElector {*/public void setup(final ElectionContext context) throws InterruptedException,KeeperException {- this.context = context;String electZKPath = context.electionPath + LeaderElector.ELECTION_NODE;zkCmdExecutor.ensureExists(electZKPath, zkClient);+ this.context = context;}/**diff --git a/solr/core/src/test/org/apache/solr/cloud/TestLeaderElectionZkExpiry.java b/solr/core/src/test/org/apache/solr/cloud/TestLeaderElectionZkExpiry.javanew file mode 100644index 00000000000..3740c97742e--- /dev/null+++ b/solr/core/src/test/org/apache/solr/cloud/TestLeaderElectionZkExpiry.java@@ -0,0 +1,97 @@+package org.apache.solr.cloud;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.common.cloud.SolrZkClient;+import org.apache.solr.core.CoreContainer;+import org.apache.solr.core.CoreDescriptor;+import org.apache.zookeeper.KeeperException;+import org.junit.Test;++import java.util.Collections;+import java.util.List;+import java.util.concurrent.TimeUnit;++public class TestLeaderElectionZkExpiry extends SolrTestCaseJ4 {+ public static final String SOLRXML = "<solr></solr>";+ private static final int MAX_NODES = 16;+ private static final int MIN_NODES = 4;++ @Test+ public void testLeaderElectionWithZkExpiry() throws Exception {+ String zkDir = createTempDir("zkData").getAbsolutePath();+ String ccDir = createTempDir("testLeaderElectionWithZkExpiry-solr").getAbsolutePath();+ CoreContainer cc = createCoreContainer(ccDir, SOLRXML);+ final ZkTestServer server = new ZkTestServer(zkDir);+ server.setTheTickTime(1000);+ SolrZkClient zc = null;+ try {+ server.run();+ AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());+ cc.load();++ final ZkController zkController = new ZkController(cc, server.getZkAddress(), 15000, 30000, "dummy.host.com", "8984", "/solr", 180000, 180000, true, new CurrentCoreDescriptorProvider() {+ @Override+ public List<CoreDescriptor> getCurrentDescriptors() {+ return Collections.EMPTY_LIST;+ }+ });+ try {+ Thread killer = new Thread() {+ @Override+ public void run() {+ long timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);+ while (System.nanoTime() < timeout) {+ long sessionId = zkController.getZkClient().getSolrZooKeeper().getSessionId();+ server.expire(sessionId);+ try {+ Thread.sleep(10);+ } catch (InterruptedException e) {}+ }+ }+ };+ killer.start();+ killer.join();+ long timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(60, TimeUnit.SECONDS);+ zc = new SolrZkClient(server.getZkAddress(), LeaderElectionTest.TIMEOUT);+ boolean found = false;+ while (System.nanoTime() < timeout) {+ try {+ String leaderNode = OverseerCollectionProcessor.getLeaderNode(zc);+ if (leaderNode != null && !leaderNode.trim().isEmpty()) {+ log.info("Time={} Overseer leader is = {}", System.nanoTime(), leaderNode);+ found = true;+ break;+ }+ } catch (KeeperException.NoNodeException nne) {+ // ignore+ }+ }+ assertTrue(found);+ } finally {+ zkController.close();+ }+ } finally {+ if (zc != null) zc.close();+ cc.shutdown();+ server.shutdown();+ }+ }+}