Skip to content

Apache Lucene open-source search software

SOLR-8372: continue buffering if recovery is canceled/failed

Yonik Seeleya3b62326a3eb
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index f8c35a44ab1..6ad6059d288 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -247,6 +247,14 @@ Bug Fixes
* SOLR-2469: MM ignored in edismax queries with operators.
(Greg Pendlebury, Jan Høydahl et. al. via Erick Erickson)
+* SOLR-8372: Canceled recovery can rarely lead to inconsistent shards:
+ If a replica is recovering via index replication, and that recovery fails
+ (for example if the leader goes down), and then some more updates are received
+ (there could be a few left to be processed from the leader that just went down),
+ and then that replica is brought down, it will think it is up-to-date when
+ restarted. (shalin, Mark Miller, yonik)
+
+
Other Changes
----------------------
diff --git a/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java b/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java
index b57038df27f..f0d52ae0399 100644
--- a/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java
+++ b/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java
@@ -457,11 +457,19 @@ public class RecoveryStrategy extends Thread implements ClosableThread {
SolrException.log(log, "Error while trying to recover. core=" + coreName, e);
} finally {
if (!replayed) {
+ // dropBufferedUpdate()s currently only supports returning to ACTIVE state, which risks additional updates
+ // being added w/o UpdateLog.FLAG_GAP, hence losing the info on restart that we are not up-to-date.
+ // For now, ulog will simply remain in BUFFERING state, and an additional call to bufferUpdates() will
+ // reset our starting point for playback.
+ log.info("Replay not started, or was not successful... still buffering updates.");
+
+ /** this prev code is retained in case we want to switch strategies.
try {
ulog.dropBufferedUpdates();
} catch (Exception e) {
SolrException.log(log, "", e);
}
+ **/
}
if (successfulRecovery) {
log.info("Registering as Active after recovery.");
diff --git a/solr/core/src/java/org/apache/solr/update/UpdateLog.java b/solr/core/src/java/org/apache/solr/update/UpdateLog.java
index 43136d345af..7c4ace21773 100644
--- a/solr/core/src/java/org/apache/solr/update/UpdateLog.java
+++ b/solr/core/src/java/org/apache/solr/update/UpdateLog.java
@@ -1115,13 +1115,19 @@ public class UpdateLog implements PluginInfoInitialized {
// it checks the state first
// assert state == State.ACTIVE;
- recoveryInfo = new RecoveryInfo();
-
// block all updates to eliminate race conditions
- // reading state and acting on it in the update processor
+ // reading state and acting on it in the distributed update processor
versionInfo.blockUpdates();
try {
- if (state != State.ACTIVE) return;
+ if (state == State.BUFFERING) {
+ log.info("Restarting buffering. previous=" + recoveryInfo);
+ } else if (state != State.ACTIVE) {
+ // we don't currently have support for handling other states
+ log.warn("Unexpected state for bufferUpdates: " + state + ", Ignoring request.");
+ return;
+ }
+
+ recoveryInfo = new RecoveryInfo();
if (log.isInfoEnabled()) {
log.info("Starting to buffer updates. " + this);
diff --git a/solr/core/src/test/org/apache/solr/search/TestRecovery.java b/solr/core/src/test/org/apache/solr/search/TestRecovery.java
index 5d6ced464c1..38feeef2726 100644
--- a/solr/core/src/test/org/apache/solr/search/TestRecovery.java
+++ b/solr/core/src/test/org/apache/solr/search/TestRecovery.java
@@ -488,6 +488,110 @@ public class TestRecovery extends SolrTestCaseJ4 {
}
+ @Test
+ public void testBufferedMultipleCalls() throws Exception {
+
+ DirectUpdateHandler2.commitOnClose = false;
+ final Semaphore logReplay = new Semaphore(0);
+ final Semaphore logReplayFinish = new Semaphore(0);
+
+ UpdateLog.testing_logReplayHook = new Runnable() {
+ @Override
+ public void run() {
+ try {
+ assertTrue(logReplay.tryAcquire(timeout, TimeUnit.SECONDS));
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ UpdateLog.testing_logReplayFinishHook = new Runnable() {
+ @Override
+ public void run() {
+ logReplayFinish.release();
+ }
+ };
+
+
+ SolrQueryRequest req = req();
+ UpdateHandler uhandler = req.getCore().getUpdateHandler();
+ UpdateLog ulog = uhandler.getUpdateLog();
+ Future<UpdateLog.RecoveryInfo> rinfoFuture;
+
+ try {
+ clearIndex();
+ assertU(commit());
+ assertEquals(UpdateLog.State.ACTIVE, ulog.getState());
+
+ ulog.bufferUpdates();
+ assertEquals(UpdateLog.State.BUFFERING, ulog.getState());
+
+ // simulate updates from a leader
+ updateJ(jsonAdd(sdoc("id","c1", "_version_","101")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+ updateJ(jsonAdd(sdoc("id","c2", "_version_","102")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+ updateJ(jsonAdd(sdoc("id","c3", "_version_","103")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+
+ // call bufferUpdates again (this currently happens when recovery fails)... we should get a new starting point
+ ulog.bufferUpdates();
+ assertEquals(UpdateLog.State.BUFFERING, ulog.getState());
+
+ updateJ(jsonAdd(sdoc("id", "c4", "_version_","104")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+ updateJ(jsonAdd(sdoc("id", "c5", "_version_","105")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+
+ logReplay.release(1000);
+ rinfoFuture = ulog.applyBufferedUpdates();
+ UpdateLog.RecoveryInfo rinfo = rinfoFuture.get();
+ assertEquals(2, rinfo.adds);
+
+ assertJQ(req("qt","/get", "getVersions","2")
+ ,"=={'versions':[105,104]}"
+ );
+
+ // this time add some docs first before buffering starts (so tlog won't be at pos 0)
+ updateJ(jsonAdd(sdoc("id","c100", "_version_","200")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+ updateJ(jsonAdd(sdoc("id","c101", "_version_","201")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+
+ ulog.bufferUpdates();
+ updateJ(jsonAdd(sdoc("id","c103", "_version_","203")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+ updateJ(jsonAdd(sdoc("id","c104", "_version_","204")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+
+ // call bufferUpdates again (this currently happens when recovery fails)... we should get a new starting point
+ ulog.bufferUpdates();
+ updateJ(jsonAdd(sdoc("id","c105", "_version_","205")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+ updateJ(jsonAdd(sdoc("id","c106", "_version_","206")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
+
+ rinfoFuture = ulog.applyBufferedUpdates();
+ rinfo = rinfoFuture.get();
+ assertEquals(2, rinfo.adds);
+
+ assertJQ(req("q", "*:*", "sort","_version_ asc", "fl","id,_version_")
+ , "/response/docs==["
+ + "{'id':'c4','_version_':104}"
+ + ",{'id':'c5','_version_':105}"
+ + ",{'id':'c100','_version_':200}"
+ + ",{'id':'c101','_version_':201}"
+ + ",{'id':'c105','_version_':205}"
+ + ",{'id':'c106','_version_':206}"
+ +"]"
+ );
+
+ // The updates that were buffered (but never applied) still appear in recent versions!
+ // This is good for some uses, but may not be good for others.
+ assertJQ(req("qt","/get", "getVersions","11")
+ ,"=={'versions':[206,205,204,203,201,200,105,104,103,102,101]}"
+ );
+
+ assertEquals(UpdateLog.State.ACTIVE, ulog.getState()); // leave each test method in a good state
+ } finally {
+ DirectUpdateHandler2.commitOnClose = true;
+ UpdateLog.testing_logReplayHook = null;
+ UpdateLog.testing_logReplayFinishHook = null;
+
+ req().close();
+ }
+
+ }
// we need to make sure that the log is informed of a core reload