Apache Lucene open-source search software
SOLR-11278: Fix a race condition in the CDCR bootstrap process which could lead to bootstraps cancelling itself
Varun Thacker26677ab2b041
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 2b8a9c591c8..13cee847862 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -153,6 +153,9 @@ Bug Fixes* SOLR-11224: SolrStream.close can hit an NPE (Erick Erickson)+* SOLR-11278: Fix a race condition in the CDCR bootstrap process which could lead to bootstraps cancelling itself+ (Amrit Sarkar, shalin, Varun Thacker)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.java b/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.javaindex caf88f40852..430237e5c8d 100644--- a/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.java@@ -26,10 +26,12 @@ import java.util.List;import java.util.Map;import java.util.concurrent.Callable;import java.util.concurrent.CancellationException;+import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Future;import java.util.concurrent.RejectedExecutionException;+import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Lock;import org.apache.solr.client.solrj.SolrRequest;@@ -617,7 +619,7 @@ public class CdcrRequestHandler extends RequestHandlerBase implements SolrCoreAwrsp.add(CdcrParams.ERRORS, hosts);}- private void handleBootstrapAction(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException, SolrServerException {+ private void handleBootstrapAction(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException, InterruptedException, SolrServerException {String collectionName = core.getCoreDescriptor().getCloudDescriptor().getCollectionName();String shard = core.getCoreDescriptor().getCloudDescriptor().getShardId();if (!leaderStateManager.amILeader()) {@@ -625,6 +627,7 @@ public class CdcrRequestHandler extends RequestHandlerBase implements SolrCoreAwthrow new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Action " + CdcrParams.CdcrAction.BOOTSTRAP +" sent to non-leader replica");}+ CountDownLatch latch = new CountDownLatch(1); // latch to make sure BOOTSTRAP_STATUS gives correct responseRunnable runnable = () -> {Lock recoveryLock = req.getCore().getSolrCoreState().getRecoveryLock();@@ -635,6 +638,7 @@ public class CdcrRequestHandler extends RequestHandlerBase implements SolrCoreAwhandleCancelBootstrap(req, rsp);} else if (leaderStateManager.amILeader()) {coreState.setCdcrBootstrapRunning(true);+ latch.countDown(); // free the latch as current bootstrap is executing//running.set(true);String masterUrl = req.getParams().get(ReplicationHandler.MASTER_URL);BootstrapCallable bootstrapCallable = new BootstrapCallable(masterUrl, core);@@ -657,6 +661,8 @@ public class CdcrRequestHandler extends RequestHandlerBase implements SolrCoreAwif (locked) {coreState.setCdcrBootstrapRunning(false);recoveryLock.unlock();+ } else {+ latch.countDown(); // free the latch as current bootstrap is executing}}};@@ -664,6 +670,7 @@ public class CdcrRequestHandler extends RequestHandlerBase implements SolrCoreAwtry {core.getCoreContainer().getUpdateShardHandler().getUpdateExecutor().submit(runnable);rsp.add(RESPONSE_STATUS, "submitted");+ latch.await(10000, TimeUnit.MILLISECONDS); // put the latch for current bootstrap command} catch (RejectedExecutionException ree) {// no problem, we're probably shutting downrsp.add(RESPONSE_STATUS, "failed");