SOLR-2705: Improve fix - refactor SolrCore out of DefaultIndexProvider and into it's methods

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1156333 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2011-08-10 19:37:13 +00:00
parent 1a70ccb893
commit adab6ebc59
9 changed files with 43 additions and 54 deletions

View File

@ -25,26 +25,25 @@ import org.apache.solr.core.SolrCore;
public final class DefaultIndexWriterProvider implements IndexWriterProvider { public final class DefaultIndexWriterProvider implements IndexWriterProvider {
private int refCnt = 1; private int refCnt = 1;
private IndexWriter indexWriter = null; private IndexWriter indexWriter = null;
private SolrCore core;
public DefaultIndexWriterProvider(SolrCore core) { public DefaultIndexWriterProvider() {
this.core = core;
} }
@Override @Override
public synchronized IndexWriter getIndexWriter() throws IOException { public synchronized IndexWriter getIndexWriter(SolrCore core) throws IOException {
if (indexWriter == null) { if (indexWriter == null) {
indexWriter = createMainIndexWriter("DirectUpdateHandler2", false); indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2", false);
} }
return indexWriter; return indexWriter;
} }
@Override @Override
public synchronized void newIndexWriter() throws IOException { public synchronized void newIndexWriter(SolrCore core) throws IOException {
if (indexWriter != null) { if (indexWriter != null) {
indexWriter.close(); indexWriter.close();
} }
indexWriter = createMainIndexWriter("DirectUpdateHandler2", indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2",
false); false);
} }
@ -65,21 +64,16 @@ public final class DefaultIndexWriterProvider implements IndexWriterProvider {
} }
@Override @Override
public synchronized void rollbackIndexWriter() throws IOException { public synchronized void rollbackIndexWriter(SolrCore core) throws IOException {
indexWriter.rollback(); indexWriter.rollback();
newIndexWriter(); newIndexWriter(core);
} }
protected SolrIndexWriter createMainIndexWriter(String name, protected SolrIndexWriter createMainIndexWriter(SolrCore core, String name,
boolean removeAllExisting) throws IOException { boolean removeAllExisting) throws IOException {
return new SolrIndexWriter(name, core.getNewIndexDir(), return new SolrIndexWriter(name, core.getNewIndexDir(),
core.getDirectoryFactory(), removeAllExisting, core.getSchema(), core.getDirectoryFactory(), removeAllExisting, core.getSchema(),
core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy(), core.getCodecProvider()); core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy(), core.getCodecProvider());
} }
@Override
public synchronized void updateCore(SolrCore core) {
this.core = core;
}
} }

View File

@ -20,31 +20,29 @@
package org.apache.solr.update; package org.apache.solr.update;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import java.util.concurrent.Future; import org.apache.lucene.search.Query;
import java.util.concurrent.ExecutionException; import org.apache.lucene.search.TermQuery;
import java.util.concurrent.atomic.AtomicLong;
import java.io.IOException;
import java.net.URL;
import org.apache.solr.search.QParser;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrConfig.UpdateHandlerInfo; import org.apache.solr.core.SolrConfig.UpdateHandlerInfo;
import org.apache.solr.core.SolrCore;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SolrIndexSearcher; import org.apache.solr.search.SolrIndexSearcher;
/** /**
@ -79,7 +77,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
public DirectUpdateHandler2(SolrCore core) throws IOException { public DirectUpdateHandler2(SolrCore core) throws IOException {
super(core); super(core);
indexWriterProvider = new DefaultIndexWriterProvider(core); indexWriterProvider = new DefaultIndexWriterProvider();
UpdateHandlerInfo updateHandlerInfo = core.getSolrConfig() UpdateHandlerInfo updateHandlerInfo = core.getSolrConfig()
.getUpdateHandlerInfo(); .getUpdateHandlerInfo();
@ -96,11 +94,10 @@ public class DirectUpdateHandler2 extends UpdateHandler {
super(core); super(core);
if (updateHandler instanceof DirectUpdateHandler2) { if (updateHandler instanceof DirectUpdateHandler2) {
this.indexWriterProvider = ((DirectUpdateHandler2)updateHandler).indexWriterProvider; this.indexWriterProvider = ((DirectUpdateHandler2)updateHandler).indexWriterProvider;
this.indexWriterProvider.updateCore(core);
} else { } else {
// the impl has changed, so we cannot use the old state - decref it // the impl has changed, so we cannot use the old state - decref it
updateHandler.decref(); updateHandler.decref();
indexWriterProvider = new DefaultIndexWriterProvider(core); indexWriterProvider = new DefaultIndexWriterProvider();
} }
UpdateHandlerInfo updateHandlerInfo = core.getSolrConfig() UpdateHandlerInfo updateHandlerInfo = core.getSolrConfig()
@ -117,18 +114,18 @@ public class DirectUpdateHandler2 extends UpdateHandler {
private void deleteAll() throws IOException { private void deleteAll() throws IOException {
SolrCore.log.info(core.getLogId()+"REMOVING ALL DOCUMENTS FROM INDEX"); SolrCore.log.info(core.getLogId()+"REMOVING ALL DOCUMENTS FROM INDEX");
indexWriterProvider.getIndexWriter().deleteAll(); indexWriterProvider.getIndexWriter(core).deleteAll();
} }
protected void rollbackWriter() throws IOException { protected void rollbackWriter() throws IOException {
numDocsPending.set(0); numDocsPending.set(0);
indexWriterProvider.rollbackIndexWriter(); indexWriterProvider.rollbackIndexWriter(core);
} }
@Override @Override
public int addDoc(AddUpdateCommand cmd) throws IOException { public int addDoc(AddUpdateCommand cmd) throws IOException {
IndexWriter writer = indexWriterProvider.getIndexWriter(); IndexWriter writer = indexWriterProvider.getIndexWriter(core);
addCommands.incrementAndGet(); addCommands.incrementAndGet();
addCommandsCumulative.incrementAndGet(); addCommandsCumulative.incrementAndGet();
int rc=-1; int rc=-1;
@ -193,7 +190,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
deleteByIdCommands.incrementAndGet(); deleteByIdCommands.incrementAndGet();
deleteByIdCommandsCumulative.incrementAndGet(); deleteByIdCommandsCumulative.incrementAndGet();
indexWriterProvider.getIndexWriter().deleteDocuments(new Term(idField.getName(), cmd.getIndexedId())); indexWriterProvider.getIndexWriter(core).deleteDocuments(new Term(idField.getName(), cmd.getIndexedId()));
if (commitTracker.timeUpperBound > 0) { if (commitTracker.timeUpperBound > 0) {
commitTracker.scheduleCommitWithin(commitTracker.timeUpperBound); commitTracker.scheduleCommitWithin(commitTracker.timeUpperBound);
@ -222,7 +219,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
if (delAll) { if (delAll) {
deleteAll(); deleteAll();
} else { } else {
indexWriterProvider.getIndexWriter().deleteDocuments(q); indexWriterProvider.getIndexWriter(core).deleteDocuments(q);
} }
madeIt = true; madeIt = true;
@ -250,7 +247,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
IndexReader[] readers = cmd.readers; IndexReader[] readers = cmd.readers;
if (readers != null && readers.length > 0) { if (readers != null && readers.length > 0) {
indexWriterProvider.getIndexWriter().addIndexes(readers); indexWriterProvider.getIndexWriter(core).addIndexes(readers);
rc = 1; rc = 1;
} else { } else {
rc = 0; rc = 0;
@ -269,7 +266,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
@Override @Override
public void commit(CommitUpdateCommand cmd) throws IOException { public void commit(CommitUpdateCommand cmd) throws IOException {
IndexWriter writer = indexWriterProvider.getIndexWriter(); IndexWriter writer = indexWriterProvider.getIndexWriter(core);
if (cmd.optimize) { if (cmd.optimize) {
optimizeCommands.incrementAndGet(); optimizeCommands.incrementAndGet();
} else { } else {
@ -352,7 +349,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
IndexReader currentReader = previousSearcher.getIndexReader(); IndexReader currentReader = previousSearcher.getIndexReader();
IndexReader newReader; IndexReader newReader;
newReader = currentReader.reopen(indexWriterProvider.getIndexWriter(), true); newReader = currentReader.reopen(indexWriterProvider.getIndexWriter(core), true);
if (newReader == currentReader) { if (newReader == currentReader) {
@ -364,7 +361,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
@Override @Override
public void newIndexWriter() throws IOException { public void newIndexWriter() throws IOException {
indexWriterProvider.newIndexWriter(); indexWriterProvider.newIndexWriter(core);
} }
/** /**

View File

@ -24,16 +24,14 @@ import org.apache.solr.core.SolrCore;
public interface IndexWriterProvider { public interface IndexWriterProvider {
public void newIndexWriter() throws IOException; public void newIndexWriter(SolrCore core) throws IOException;
public IndexWriter getIndexWriter() throws IOException; public IndexWriter getIndexWriter(SolrCore core) throws IOException;
public void decref() throws IOException; public void decref() throws IOException;
public void incref(); public void incref();
public void rollbackIndexWriter() throws IOException; public void rollbackIndexWriter(SolrCore core) throws IOException;
public void updateCore(SolrCore core);
} }

View File

@ -120,7 +120,7 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 {
// test merge factor picked up // test merge factor picked up
SolrCore core = h.getCore(); SolrCore core = h.getCore();
IndexWriter writer = ((DirectUpdateHandler2)core.getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)core.getUpdateHandler()).getIndexWriterProvider().getIndexWriter(core);
assertEquals("Mergefactor was not picked up", ((LogMergePolicy)writer.getConfig().getMergePolicy()).getMergeFactor(), 8); assertEquals("Mergefactor was not picked up", ((LogMergePolicy)writer.getConfig().getMergePolicy()).getMergeFactor(), 8);
lrf.args.put(CommonParams.VERSION,"2.2"); lrf.args.put(CommonParams.VERSION,"2.2");

View File

@ -47,7 +47,7 @@ public class BasicZkTest extends AbstractZkTestCase {
// test merge factor picked up // test merge factor picked up
SolrCore core = h.getCore(); SolrCore core = h.getCore();
IndexWriter writer = ((DirectUpdateHandler2)core.getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)core.getUpdateHandler()).getIndexWriterProvider().getIndexWriter(core);
assertEquals("Mergefactor was not picked up", ((LogMergePolicy)writer.getConfig().getMergePolicy()).getMergeFactor(), 8); assertEquals("Mergefactor was not picked up", ((LogMergePolicy)writer.getConfig().getMergePolicy()).getMergeFactor(), 8);

View File

@ -116,7 +116,7 @@ public class TestConfig extends SolrTestCaseJ4 {
@Test @Test
public void testTermIndexInterval() throws Exception { public void testTermIndexInterval() throws Exception {
IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(h.getCore());
int interval = writer.getConfig().getTermIndexInterval(); int interval = writer.getConfig().getTermIndexInterval();
assertEquals(256, interval); assertEquals(256, interval);
} }

View File

@ -33,7 +33,7 @@ public class TestLegacyMergeSchedulerPolicyConfig extends SolrTestCaseJ4 {
@Test @Test
public void testLegacy() throws Exception { public void testLegacy() throws Exception {
IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(h.getCore());
assertTrue(writer.getConfig().getMergePolicy().getClass().getName().equals(LogDocMergePolicy.class.getName())); assertTrue(writer.getConfig().getMergePolicy().getClass().getName().equals(LogDocMergePolicy.class.getName()));
assertTrue(writer.getConfig().getMergeScheduler().getClass().getName().equals(SerialMergeScheduler.class.getName())); assertTrue(writer.getConfig().getMergeScheduler().getClass().getName().equals(SerialMergeScheduler.class.getName()));
} }

View File

@ -37,13 +37,13 @@ public class TestPropInject extends AbstractSolrTestCase {
} }
public void testMergePolicy() throws Exception { public void testMergePolicy() throws Exception {
IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(h.getCore());
LogByteSizeMergePolicy mp = (LogByteSizeMergePolicy)writer.getConfig().getMergePolicy(); LogByteSizeMergePolicy mp = (LogByteSizeMergePolicy)writer.getConfig().getMergePolicy();
assertEquals(64.0, mp.getMaxMergeMB(), 0); assertEquals(64.0, mp.getMaxMergeMB(), 0);
} }
public void testProps() throws Exception { public void testProps() throws Exception {
IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(h.getCore());
ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler)writer.getConfig().getMergeScheduler(); ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler)writer.getConfig().getMergeScheduler();
assertEquals(2, cms.getMaxThreadCount()); assertEquals(2, cms.getMaxThreadCount());
} }

View File

@ -33,14 +33,14 @@ public class TestPropInjectDefaults extends SolrTestCaseJ4 {
@Test @Test
public void testMergePolicyDefaults() throws Exception { public void testMergePolicyDefaults() throws Exception {
IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(h.getCore());
LogByteSizeMergePolicy mp = (LogByteSizeMergePolicy)writer.getConfig().getMergePolicy(); LogByteSizeMergePolicy mp = (LogByteSizeMergePolicy)writer.getConfig().getMergePolicy();
assertEquals(32.0, mp.getMaxMergeMB(), 0); assertEquals(32.0, mp.getMaxMergeMB(), 0);
} }
@Test @Test
public void testPropsDefaults() throws Exception { public void testPropsDefaults() throws Exception {
IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(); IndexWriter writer = ((DirectUpdateHandler2)h.getCore().getUpdateHandler()).getIndexWriterProvider().getIndexWriter(h.getCore());
ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler)writer.getConfig().getMergeScheduler(); ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler)writer.getConfig().getMergeScheduler();
assertEquals(4, cms.getMaxThreadCount()); assertEquals(4, cms.getMaxThreadCount());
} }