Apache Solr open-source search software
SOLR-18155: Abort shard leader election if container shutdown has started (#4224)
Pierre Salagnac8873cf1242be
diff --git a/changelog/unreleased/SOLR-18155-election-leak.yml b/changelog/unreleased/SOLR-18155-election-leak.ymlnew file mode 100644index 00000000000..a43436d0980--- /dev/null+++ b/changelog/unreleased/SOLR-18155-election-leak.yml@@ -0,0 +1,7 @@+title: Abort shard leader election if container shutdown sequence has started, so we don't have leaders elected very late and not properly closed.+type: fixed+authors:+ - name: Pierre Salagnac+links:+ - name: SOLR-18155+ url: https://issues.apache.org/jira/browse/SOLR-18155diff --git a/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContext.java b/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContext.javaindex 96401345503..fc77cee2e20 100644--- a/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContext.java+++ b/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContext.java@@ -77,6 +77,14 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {syncStrategy.close();}+ /**+ * Internally check whether we should abort the election process. This returns true if either this+ * context was explicitly closed, or Solr server is being shut down.+ */+ private boolean shouldAbort() {+ return isClosed || cc.isShutDown();+ }+@Overridepublic void cancelElection() throws InterruptedException, KeeperException {String coreName = leaderProps.getStr(ZkStateReader.CORE_NAME_PROP);@@ -154,7 +162,7 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {waitForReplicasToComeUp(leaderVoteWait);}- if (isClosed) {+ if (shouldAbort()) {// Solr is shutting down or the ZooKeeper session expired while waiting for replicas. If the// later, we cannot be sure we are still the leader, so we should bail out. The OnReconnect// handler will re-register the cores and handle a new leadership election.@@ -185,7 +193,7 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {}}- if (isClosed) {+ if (shouldAbort()) {return;}@@ -267,7 +275,7 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {}}- if (!isClosed) {+ if (!shouldAbort()) {try {if (replicaType.replicateFromLeader) {// stop replicate from old leader@@ -361,7 +369,7 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {ZkShardTerms zkShardTerms, String coreNodeName, int timeout) throws InterruptedException {long timeoutAt =System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);- while (!isClosed && !cc.isShutDown()) {+ while (!shouldAbort()) {if (System.nanoTime() > timeoutAt) {log.warn("After waiting for {}ms, no other potential leader was found, {} try to become leader anyway (core_term:{}, highest_term:{})",@@ -446,7 +454,7 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {DocCollection docCollection = zkController.getClusterState().getCollectionOrNull(collection);Slice slices = (docCollection == null) ? null : docCollection.getSlice(shardId);int cnt = 0;- while (!isClosed && !cc.isShutDown()) {+ while (!shouldAbort()) {// wait for everyone to be upif (slices != null) {int found = 0;