Apache Solr open-source search software
Break out SolrCoreState locks to avoid deadlock SOLR-3659: on replication, rollback old writer rather than closing it randomly turn on auto soft commit in monkey test
Mark Robert Miller5ab5df93ce9d
diff --git a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.javaindex 74dd04f5425..41e0317dbb8 100644--- a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java@@ -887,7 +887,7 @@ public class ReplicationHandler extends RequestHandlerBase implements SolrCoreAw}// reboot the writer on the new index- core.getUpdateHandler().newIndexWriter();+ core.getUpdateHandler().newIndexWriter(true);} catch (IOException e) {LOG.warn("Unable to get IndexCommit on startup", e);diff --git a/solr/core/src/java/org/apache/solr/handler/SnapPuller.java b/solr/core/src/java/org/apache/solr/handler/SnapPuller.javaindex 79638f4ae66..ce3c0f4daf1 100644--- a/solr/core/src/java/org/apache/solr/handler/SnapPuller.java+++ b/solr/core/src/java/org/apache/solr/handler/SnapPuller.java@@ -535,7 +535,7 @@ public class SnapPuller {SolrQueryRequest req = new LocalSolrQueryRequest(solrCore,new ModifiableSolrParams());// reboot the writer on the new index and get a new searcher- solrCore.getUpdateHandler().newIndexWriter();+ solrCore.getUpdateHandler().newIndexWriter(true);try {// first try to open an NRT searcher so that the newdiff --git a/solr/core/src/java/org/apache/solr/update/CommitTracker.java b/solr/core/src/java/org/apache/solr/update/CommitTracker.javaindex 8df3d1e01b4..375d2e5fb37 100644--- a/solr/core/src/java/org/apache/solr/update/CommitTracker.java+++ b/solr/core/src/java/org/apache/solr/update/CommitTracker.java@@ -39,8 +39,10 @@ import org.slf4j.LoggerFactory;* definitely change in the future, so the interface should not be relied-upon** Note: all access must be synchronized.+ *+ * Public for tests.*/-final class CommitTracker implements Runnable {+public final class CommitTracker implements Runnable {protected final static Logger log = LoggerFactory.getLogger(CommitTracker.class);// scheduler delay for maxDoc-triggered autocommits@@ -248,7 +250,8 @@ final class CommitTracker implements Runnable {this.docsUpperBound = docsUpperBound;}- void setTimeUpperBound(long timeUpperBound) {+ // only for testing - not thread safe+ public void setTimeUpperBound(long timeUpperBound) {this.timeUpperBound = timeUpperBound;}}diff --git a/solr/core/src/java/org/apache/solr/update/DefaultSolrCoreState.java b/solr/core/src/java/org/apache/solr/update/DefaultSolrCoreState.javaindex f0de4224588..dd5815dfcdb 100644--- a/solr/core/src/java/org/apache/solr/update/DefaultSolrCoreState.java+++ b/solr/core/src/java/org/apache/solr/update/DefaultSolrCoreState.java@@ -35,6 +35,10 @@ public final class DefaultSolrCoreState extends SolrCoreState {private final boolean SKIP_AUTO_RECOVERY = Boolean.getBoolean("solrcloud.skip.autorecovery");private final Object recoveryLock = new Object();++ // protects pauseWriter and writerFree+ private final Object writerPauseLock = new Object();+private int refCnt = 1;private SolrIndexWriter indexWriter = null;private DirectoryFactory directoryFactory;@@ -55,68 +59,74 @@ public final class DefaultSolrCoreState extends SolrCoreState {@Overridepublic synchronized RefCounted<IndexWriter> getIndexWriter(SolrCore core)throws IOException {-- if (core == null) {- // core == null is a signal to just return the current writer, or null if none.- if (refCntWriter != null) refCntWriter.incref();- return refCntWriter;- }-- while (pauseWriter) {- try {- wait();- } catch (InterruptedException e) {}- }-- if (indexWriter == null) {- indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2", false,- false);- }- if (refCntWriter == null) {- refCntWriter = new RefCounted<IndexWriter>(indexWriter) {- @Override- public void close() {- synchronized (DefaultSolrCoreState.this) {- writerFree = true;- DefaultSolrCoreState.this.notifyAll();+ synchronized (writerPauseLock) {+ if (core == null) {+ // core == null is a signal to just return the current writer, or null+ // if none.+ if (refCntWriter != null) refCntWriter.incref();+ return refCntWriter;+ }++ while (pauseWriter) {+ try {+ writerPauseLock.wait();+ } catch (InterruptedException e) {}+ }++ if (indexWriter == null) {+ indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2",+ false, false);+ }+ if (refCntWriter == null) {+ refCntWriter = new RefCounted<IndexWriter>(indexWriter) {+ @Override+ public void close() {+ synchronized (writerPauseLock) {+ writerFree = true;+ writerPauseLock.notifyAll();+ }}- }- };+ };+ }+ writerFree = false;+ writerPauseLock.notifyAll();+ refCntWriter.incref();+ return refCntWriter;}- writerFree = false;- notifyAll();- refCntWriter.incref();- return refCntWriter;}@Override- public synchronized void newIndexWriter(SolrCore core) throws IOException {- // we need to wait for the Writer to fall out of use- // first lets stop it from being lent out- pauseWriter = true;- // then lets wait until its out of use- while(!writerFree) {- try {- wait();- } catch (InterruptedException e) {}- }+ public synchronized void newIndexWriter(SolrCore core, boolean rollback) throws IOException {- try {- if (indexWriter != null) {+ synchronized (writerPauseLock) {+ // we need to wait for the Writer to fall out of use+ // first lets stop it from being lent out+ pauseWriter = true;+ // then lets wait until its out of use+ while (!writerFree) {try {- indexWriter.close();- } catch (Throwable t) {- SolrException.log(log, "Error closing old IndexWriter", t);- }+ writerPauseLock.wait();+ } catch (InterruptedException e) {}}- indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2", false,- true);- // we need to null this so it picks up the new writer next get call- refCntWriter = null;- } finally {- pauseWriter = false;- notifyAll();+ try {+ if (indexWriter != null) {+ try {+ indexWriter.close();+ } catch (Throwable t) {+ SolrException.log(log, "Error closing old IndexWriter", t);+ }+ }++ indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2",+ false, true);+ // we need to null this so it picks up the new writer next get call+ refCntWriter = null;+ } finally {++ pauseWriter = false;+ writerPauseLock.notifyAll();+ }}}@@ -161,7 +171,7 @@ public final class DefaultSolrCoreState extends SolrCoreState {@Overridepublic synchronized void rollbackIndexWriter(SolrCore core) throws IOException {indexWriter.rollback();- newIndexWriter(core);+ newIndexWriter(core, true);}protected SolrIndexWriter createMainIndexWriter(SolrCore core, String name,diff --git a/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java b/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.javaindex cee969c4031..4b1120b0ffc 100644--- a/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java+++ b/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java@@ -343,7 +343,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState// currently for testing purposes. Do a delete of complete index w/o worrying about versions, don't log, clean up most state in update log, etcif (delAll && cmd.getVersion() == -Long.MAX_VALUE) {- synchronized (solrCoreState) {+ synchronized (solrCoreState.getUpdateLock()) {deleteAll();ulog.deleteAll();return;@@ -356,7 +356,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState// a realtime view of the index. When a new searcher is opened after a DBQ, that// flag can be cleared. If those thing happen concurrently, it's not thread safe.//- synchronized (solrCoreState) {+ synchronized (solrCoreState.getUpdateLock()) {if (delAll) {deleteAll();} else {@@ -392,7 +392,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreStateTerm idTerm = new Term(idField.getName(), cmd.getIndexedId());// see comment in deleteByQuery- synchronized (solrCoreState) {+ synchronized (solrCoreState.getUpdateLock()) {RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);try {IndexWriter writer = iw.get();@@ -518,7 +518,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState}if (!cmd.softCommit) {- synchronized (solrCoreState) { // sync is currently needed to prevent preCommit+ synchronized (solrCoreState.getUpdateLock()) { // sync is currently needed to prevent preCommit// from being called between preSoft and// postSoft... see postSoft comments.if (ulog != null) ulog.preCommit(cmd);@@ -547,14 +547,14 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreStateif (cmd.softCommit) {// ulog.preSoftCommit();- synchronized (solrCoreState) {+ synchronized (solrCoreState.getUpdateLock()) {if (ulog != null) ulog.preSoftCommit(cmd);core.getSearcher(true, false, waitSearcher, true);if (ulog != null) ulog.postSoftCommit(cmd);}// ulog.postSoftCommit();} else {- synchronized (solrCoreState) {+ synchronized (solrCoreState.getUpdateLock()) {if (ulog != null) ulog.preSoftCommit(cmd);if (cmd.openSearcher) {core.getSearcher(true, false, waitSearcher);@@ -606,8 +606,8 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState}@Override- public void newIndexWriter() throws IOException {- solrCoreState.newIndexWriter(core);+ public void newIndexWriter(boolean rollback) throws IOException {+ solrCoreState.newIndexWriter(core, rollback);}/**@@ -705,7 +705,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState// TODO: keep other commit callbacks from being called?// this.commit(cmd); // too many test failures using this method... is it because of callbacks?- synchronized (solrCoreState) {+ synchronized (solrCoreState.getUpdateLock()) {ulog.preCommit(cmd);}@@ -714,7 +714,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreStatecommitData.put(SolrIndexWriter.COMMIT_TIME_MSEC_KEY, String.valueOf(System.currentTimeMillis()));writer.commit(commitData);- synchronized (solrCoreState) {+ synchronized (solrCoreState.getUpdateLock()) {ulog.postCommit(cmd);}}@@ -823,4 +823,14 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreStatepublic void incref() {solrCoreState.incref();}++ // allow access for tests+ public CommitTracker getCommitTracker() {+ return commitTracker;+ }++ // allow access for tests+ public CommitTracker getSoftCommitTracker() {+ return softCommitTracker;+ }}diff --git a/solr/core/src/java/org/apache/solr/update/SolrCoreState.java b/solr/core/src/java/org/apache/solr/update/SolrCoreState.javaindex 0c0559bca00..398a407bc72 100644--- a/solr/core/src/java/org/apache/solr/update/SolrCoreState.java+++ b/solr/core/src/java/org/apache/solr/update/SolrCoreState.java@@ -31,15 +31,21 @@ import org.apache.solr.util.RefCounted;**/public abstract class SolrCoreState {+ private final Object deleteLock = new Object();++ public Object getUpdateLock() {+ return deleteLock;+ }/*** Force the creation of a new IndexWriter using the settings from the given* SolrCore.** @param core+ * @param rollback close IndexWriter if false, else rollback* @throws IOException*/- public abstract void newIndexWriter(SolrCore core) throws IOException;+ public abstract void newIndexWriter(SolrCore core, boolean rollback) throws IOException;/*** Get the current IndexWriter. If a new IndexWriter must be created, use thediff --git a/solr/core/src/java/org/apache/solr/update/UpdateHandler.java b/solr/core/src/java/org/apache/solr/update/UpdateHandler.javaindex 99de26be831..abd21d0738c 100644--- a/solr/core/src/java/org/apache/solr/update/UpdateHandler.java+++ b/solr/core/src/java/org/apache/solr/update/UpdateHandler.java@@ -127,9 +127,11 @@ public abstract class UpdateHandler implements SolrInfoMBean {* Called when the Writer should be opened again - eg when replication replaces* all of the index files.*+ * @param rollback IndexWriter if true else close+ ** @throws IOException*/- public abstract void newIndexWriter() throws IOException;+ public abstract void newIndexWriter(boolean rollback) throws IOException;public abstract SolrCoreState getSolrCoreState();diff --git a/solr/core/src/test/org/apache/solr/cloud/ChaosMonkeySafeLeaderTest.java b/solr/core/src/test/org/apache/solr/cloud/ChaosMonkeySafeLeaderTest.javaindex e1671508e82..c38f63619e1 100644--- a/solr/core/src/test/org/apache/solr/cloud/ChaosMonkeySafeLeaderTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ChaosMonkeySafeLeaderTest.java@@ -22,6 +22,9 @@ import java.util.List;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.core.SolrCore;+import org.apache.solr.servlet.SolrDispatchFilter;+import org.apache.solr.update.DirectUpdateHandler2;import org.junit.After;import org.junit.AfterClass;import org.junit.Before;@@ -35,7 +38,7 @@ public class ChaosMonkeySafeLeaderTest extends FullSolrCloudTest {@BeforeClasspublic static void beforeSuperClass() {-+}@AfterClass@@ -77,9 +80,10 @@ public class ChaosMonkeySafeLeaderTest extends FullSolrCloudTest {handle.put("QTime", SKIPVAL);handle.put("timestamp", SKIPVAL);- // we cannot do delete by query- // as it's not supported for recovery- //del("*:*");+ // randomly turn on 5 seconds 'soft' commit+ randomlyEnableAutoSoftCommit();++ del("*:*");List<StopableIndexingThread> threads = new ArrayList<StopableIndexingThread>();int threadCount = 2;@@ -116,6 +120,24 @@ public class ChaosMonkeySafeLeaderTest extends FullSolrCloudTest {if (VERBOSE) System.out.println("control docs:" + controlClient.query(new SolrQuery("*:*")).getResults().getNumFound() + "\n\n");}++ private void randomlyEnableAutoSoftCommit() {+ if (r.nextBoolean()) {+ log.info("Turning on auto soft commit");+ for (CloudJettyRunner jetty : shardToJetty.get("shard1")) {+ SolrCore core = ((SolrDispatchFilter) jetty.jetty.getDispatchFilter()+ .getFilter()).getCores().getCore("collection1");+ try {+ ((DirectUpdateHandler2) core.getUpdateHandler()).getCommitTracker()+ .setTimeUpperBound(5000);+ } finally {+ core.close();+ }+ }+ } else {+ log.info("Not turning on auto soft commit");+ }+ }// skip the randoms - they can deadlock...protected void indexr(Object... fields) throws Exception {