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 {
private int refCnt = 1;
private IndexWriter indexWriter = null;
private SolrCore core;
public DefaultIndexWriterProvider(SolrCore core) {
this.core = core;
public DefaultIndexWriterProvider() {
}
@Override
public synchronized IndexWriter getIndexWriter() throws IOException {
public synchronized IndexWriter getIndexWriter(SolrCore core) throws IOException {
if (indexWriter == null) {
indexWriter = createMainIndexWriter("DirectUpdateHandler2", false);
indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2", false);
}
return indexWriter;
}
@Override
public synchronized void newIndexWriter() throws IOException {
public synchronized void newIndexWriter(SolrCore core) throws IOException {
if (indexWriter != null) {
indexWriter.close();
}
indexWriter = createMainIndexWriter("DirectUpdateHandler2",
indexWriter = createMainIndexWriter(core, "DirectUpdateHandler2",
false);
}
@ -65,21 +64,16 @@ public final class DefaultIndexWriterProvider implements IndexWriterProvider {
}
@Override
public synchronized void rollbackIndexWriter() throws IOException {
public synchronized void rollbackIndexWriter(SolrCore core) throws IOException {
indexWriter.rollback();
newIndexWriter();
newIndexWriter(core);
}
protected SolrIndexWriter createMainIndexWriter(String name,
protected SolrIndexWriter createMainIndexWriter(SolrCore core, String name,
boolean removeAllExisting) throws IOException {
return new SolrIndexWriter(name, core.getNewIndexDir(),
core.getDirectoryFactory(), removeAllExisting, core.getSchema(),
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;
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.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
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.store.Directory;
import java.util.concurrent.Future;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;
import java.io.IOException;
import java.net.URL;
import org.apache.solr.search.QParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.util.NamedList;
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.SolrCore;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SolrIndexSearcher;
/**
@ -79,7 +77,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
public DirectUpdateHandler2(SolrCore core) throws IOException {
super(core);
indexWriterProvider = new DefaultIndexWriterProvider(core);
indexWriterProvider = new DefaultIndexWriterProvider();
UpdateHandlerInfo updateHandlerInfo = core.getSolrConfig()
.getUpdateHandlerInfo();
@ -96,11 +94,10 @@ public class DirectUpdateHandler2 extends UpdateHandler {
super(core);
if (updateHandler instanceof DirectUpdateHandler2) {
this.indexWriterProvider = ((DirectUpdateHandler2)updateHandler).indexWriterProvider;
this.indexWriterProvider.updateCore(core);
} else {
// the impl has changed, so we cannot use the old state - decref it
updateHandler.decref();
indexWriterProvider = new DefaultIndexWriterProvider(core);
indexWriterProvider = new DefaultIndexWriterProvider();
}
UpdateHandlerInfo updateHandlerInfo = core.getSolrConfig()
@ -117,18 +114,18 @@ public class DirectUpdateHandler2 extends UpdateHandler {
private void deleteAll() throws IOException {
SolrCore.log.info(core.getLogId()+"REMOVING ALL DOCUMENTS FROM INDEX");
indexWriterProvider.getIndexWriter().deleteAll();
indexWriterProvider.getIndexWriter(core).deleteAll();
}
protected void rollbackWriter() throws IOException {
numDocsPending.set(0);
indexWriterProvider.rollbackIndexWriter();
indexWriterProvider.rollbackIndexWriter(core);
}
@Override
public int addDoc(AddUpdateCommand cmd) throws IOException {
IndexWriter writer = indexWriterProvider.getIndexWriter();
IndexWriter writer = indexWriterProvider.getIndexWriter(core);
addCommands.incrementAndGet();
addCommandsCumulative.incrementAndGet();
int rc=-1;
@ -193,7 +190,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
deleteByIdCommands.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) {
commitTracker.scheduleCommitWithin(commitTracker.timeUpperBound);
@ -222,7 +219,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
if (delAll) {
deleteAll();
} else {
indexWriterProvider.getIndexWriter().deleteDocuments(q);
indexWriterProvider.getIndexWriter(core).deleteDocuments(q);
}
madeIt = true;
@ -250,7 +247,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
IndexReader[] readers = cmd.readers;
if (readers != null && readers.length > 0) {
indexWriterProvider.getIndexWriter().addIndexes(readers);
indexWriterProvider.getIndexWriter(core).addIndexes(readers);
rc = 1;
} else {
rc = 0;
@ -269,7 +266,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
@Override
public void commit(CommitUpdateCommand cmd) throws IOException {
IndexWriter writer = indexWriterProvider.getIndexWriter();
IndexWriter writer = indexWriterProvider.getIndexWriter(core);
if (cmd.optimize) {
optimizeCommands.incrementAndGet();
} else {
@ -352,7 +349,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
IndexReader currentReader = previousSearcher.getIndexReader();
IndexReader newReader;
newReader = currentReader.reopen(indexWriterProvider.getIndexWriter(), true);
newReader = currentReader.reopen(indexWriterProvider.getIndexWriter(core), true);
if (newReader == currentReader) {
@ -364,7 +361,7 @@ public class DirectUpdateHandler2 extends UpdateHandler {
@Override
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 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 incref();
public void rollbackIndexWriter() throws IOException;
public void updateCore(SolrCore core);
public void rollbackIndexWriter(SolrCore core) throws IOException;
}

View File

@ -120,7 +120,7 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 {
// test merge factor picked up
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);
lrf.args.put(CommonParams.VERSION,"2.2");

View File

@ -47,7 +47,7 @@ public class BasicZkTest extends AbstractZkTestCase {
// test merge factor picked up
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);

View File

@ -116,7 +116,7 @@ public class TestConfig extends SolrTestCaseJ4 {
@Test
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();
assertEquals(256, interval);
}

View File

@ -33,7 +33,7 @@ public class TestLegacyMergeSchedulerPolicyConfig extends SolrTestCaseJ4 {
@Test
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().getMergeScheduler().getClass().getName().equals(SerialMergeScheduler.class.getName()));
}

View File

@ -37,13 +37,13 @@ public class TestPropInject extends AbstractSolrTestCase {
}
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();
assertEquals(64.0, mp.getMaxMergeMB(), 0);
}
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();
assertEquals(2, cms.getMaxThreadCount());
}

View File

@ -33,14 +33,14 @@ public class TestPropInjectDefaults extends SolrTestCaseJ4 {
@Test
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();
assertEquals(32.0, mp.getMaxMergeMB(), 0);
}
@Test
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();
assertEquals(4, cms.getMaxThreadCount());
}