Apache Solr open-source search software
LUCENE-4876: Deeply clone IndexWriterConfigs.
Adrien Grand1f964c57642e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 7f1df1e50cc..e2619bdceb7 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -50,12 +50,16 @@ Changes in backwards compatibility policy* LUCENE-4833: IndexWriterConfig used to use LogByteSizeMergePolicy whencalling setMergePolicy(null) although the default merge policy isTieredMergePolicy. IndexWriterConfig setters now throw an exception when- passed null if null is not a valid value.+ passed null if null is not a valid value. (Adrien Grand)* LUCENE-4849: Made ParallelTaxonomyArrays abstract with a concreteimplementation for DirectoryTaxonomyWriter/Reader. Also moved it undero.a.l.facet.taxonomy. (Shai Erera)+* LUCENE-4876: IndexDeletionPolicy is now an abstract class instead of an+ interface. IndexDeletionPolicy, MergeScheduler and InfoStream now implement+ Cloneable. (Adrien Grand)+New Features* LUCENE-4815: DrillSideways now allows more than one FacetRequest per@@ -175,7 +179,11 @@ Bug Fixesthe scores array. (Shai Erera)* LUCENE-4882: FacetsAccumulator did not allow to count ROOT category (i.e.- count dimensions). (Shai Erera)+ count dimensions). (Shai Erera)++* LUCENE-4876: IndexWriterConfig.clone() now clones its MergeScheduler,+ IndexDeletionPolicy and InfoStream in order to make an IndexWriterConfig and+ its clone fully independent. (Adrien Grand)Documentationdiff --git a/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java b/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.javaindex 60b655c5daa..ddda077e432 100644--- a/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java+++ b/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java@@ -561,4 +561,13 @@ public class ConcurrentMergeScheduler extends MergeScheduler {sb.append("mergeThreadPriority=").append(mergeThreadPriority);return sb.toString();}++ @Override+ public MergeScheduler clone() {+ ConcurrentMergeScheduler clone = (ConcurrentMergeScheduler) super.clone();+ clone.writer = null;+ clone.dir = null;+ clone.mergeThreads = new ArrayList<MergeThread>();+ return clone;+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexDeletionPolicy.java b/lucene/core/src/java/org/apache/lucene/index/IndexDeletionPolicy.javaindex 9b4241c42a0..1e8ca352a8d 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexDeletionPolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexDeletionPolicy.java@@ -48,7 +48,10 @@ import java.io.IOException;* for details.</p>*/-public interface IndexDeletionPolicy {+public abstract class IndexDeletionPolicy implements Cloneable {++ /** Sole constructor, typically called by sub-classes constructors. */+ protected IndexDeletionPolicy() {}/*** <p>This is called once when a writer is first@@ -70,7 +73,7 @@ public interface IndexDeletionPolicy {* {@link IndexCommit point-in-time commits},* sorted by age (the 0th one is the oldest commit).*/- public void onInit(List<? extends IndexCommit> commits) throws IOException;+ public abstract void onInit(List<? extends IndexCommit> commits) throws IOException;/*** <p>This is called each time the writer completed a commit.@@ -94,5 +97,15 @@ public interface IndexDeletionPolicy {* @param commits List of {@link IndexCommit},* sorted by age (the 0th one is the oldest commit).*/- public void onCommit(List<? extends IndexCommit> commits) throws IOException;+ public abstract void onCommit(List<? extends IndexCommit> commits) throws IOException;++ @Override+ public IndexDeletionPolicy clone() {+ try {+ return (IndexDeletionPolicy) super.clone();+ } catch (CloneNotSupportedException e) {+ throw new Error(e);+ }+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriterConfig.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriterConfig.javaindex 8ccb314cf92..de8d9586ebb 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriterConfig.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriterConfig.java@@ -151,9 +151,14 @@ public final class IndexWriterConfig extends LiveIndexWriterConfig implements Cl// Mostly shallow clone, but do a deepish clone of// certain objects that have state that cannot be shared// across IW instances:+ clone.delPolicy = delPolicy.clone();clone.flushPolicy = flushPolicy.clone();clone.indexerThreadPool = indexerThreadPool.clone();+ // we clone the infoStream because some impls might have state variables+ // such as line numbers, message throughput, ...+ clone.infoStream = infoStream.clone();clone.mergePolicy = mergePolicy.clone();+ clone.mergeScheduler = mergeScheduler.clone();return clone;} catch (CloneNotSupportedException e) {diff --git a/lucene/core/src/java/org/apache/lucene/index/KeepOnlyLastCommitDeletionPolicy.java b/lucene/core/src/java/org/apache/lucene/index/KeepOnlyLastCommitDeletionPolicy.javaindex 49c44ea0936..d0615ecd496 100644--- a/lucene/core/src/java/org/apache/lucene/index/KeepOnlyLastCommitDeletionPolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/KeepOnlyLastCommitDeletionPolicy.java@@ -26,7 +26,7 @@ import java.util.List;* the default deletion policy.*/-public final class KeepOnlyLastCommitDeletionPolicy implements IndexDeletionPolicy {+public final class KeepOnlyLastCommitDeletionPolicy extends IndexDeletionPolicy {/** Sole constructor. */public KeepOnlyLastCommitDeletionPolicy() {diff --git a/lucene/core/src/java/org/apache/lucene/index/MergeScheduler.java b/lucene/core/src/java/org/apache/lucene/index/MergeScheduler.javaindex a1c8867dce0..8cd0b8d96b1 100644--- a/lucene/core/src/java/org/apache/lucene/index/MergeScheduler.java+++ b/lucene/core/src/java/org/apache/lucene/index/MergeScheduler.java@@ -27,7 +27,7 @@ import java.io.IOException;** @lucene.experimental*/-public abstract class MergeScheduler implements Closeable {+public abstract class MergeScheduler implements Closeable, Cloneable {/** Sole constructor. (For invocation by subclass* constructors, typically implicit.) */@@ -40,4 +40,13 @@ public abstract class MergeScheduler implements Closeable {/** Close this MergeScheduler. */@Overridepublic abstract void close() throws IOException;++ @Override+ public MergeScheduler clone() {+ try {+ return (MergeScheduler) super.clone();+ } catch (CloneNotSupportedException e) {+ throw new Error(e);+ }+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/NoDeletionPolicy.java b/lucene/core/src/java/org/apache/lucene/index/NoDeletionPolicy.javaindex 3b445ab1531..2791cdef4c8 100644--- a/lucene/core/src/java/org/apache/lucene/index/NoDeletionPolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/NoDeletionPolicy.java@@ -24,7 +24,7 @@ import java.util.List;* deleting them. This class is a singleton and can be accessed by referencing* {@link #INSTANCE}.*/-public final class NoDeletionPolicy implements IndexDeletionPolicy {+public final class NoDeletionPolicy extends IndexDeletionPolicy {/** The single instance of this class. */public static final IndexDeletionPolicy INSTANCE = new NoDeletionPolicy();@@ -38,5 +38,10 @@ public final class NoDeletionPolicy implements IndexDeletionPolicy {@Overridepublic void onInit(List<? extends IndexCommit> commits) {}-++ @Override+ public IndexDeletionPolicy clone() {+ return this;+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/index/NoMergeScheduler.java b/lucene/core/src/java/org/apache/lucene/index/NoMergeScheduler.javaindex e3aba03c20b..b71ed36f124 100644--- a/lucene/core/src/java/org/apache/lucene/index/NoMergeScheduler.java+++ b/lucene/core/src/java/org/apache/lucene/index/NoMergeScheduler.java@@ -41,4 +41,10 @@ public final class NoMergeScheduler extends MergeScheduler {@Overridepublic void merge(IndexWriter writer) {}++ @Override+ public MergeScheduler clone() {+ return this;+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java b/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.javaindex b5393e5affb..eb8bf0f0724 100644--- a/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java@@ -25,8 +25,6 @@ import java.util.Map.Entry;import java.util.Map;import org.apache.lucene.document.Document;-import org.apache.lucene.document.Field;-import org.apache.lucene.document.FieldType;import org.apache.lucene.document.StoredField;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.Directory;@@ -46,6 +44,12 @@ import org.apache.lucene.util.Version;* <p>* <b>NOTE:</b> you should call {@link #close()} when you're done using this* class for safety (it will close the {@link IndexWriter} instance used).+ * <p>+ * <b>NOTE:</b> Sharing {@link PersistentSnapshotDeletionPolicy}s that write to+ * the same directory across {@link IndexWriter}s will corrupt snapshots. You+ * should make sure every {@link IndexWriter} has its own+ * {@link PersistentSnapshotDeletionPolicy} and that they all write to a+ * different {@link Directory}.*/public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy implements Closeable {diff --git a/lucene/core/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java b/lucene/core/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.javaindex fa977165e90..85b40dcf697 100644--- a/lucene/core/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java@@ -49,7 +49,7 @@ import org.apache.lucene.store.Directory;** @lucene.experimental*/-public class SnapshotDeletionPolicy implements IndexDeletionPolicy {+public class SnapshotDeletionPolicy extends IndexDeletionPolicy {/** Holds a Snapshot's information. */private static class SnapshotInfo {@@ -373,4 +373,14 @@ public class SnapshotDeletionPolicy implements IndexDeletionPolicy {return lastCommit;}+ @Override+ public IndexDeletionPolicy clone() {+ SnapshotDeletionPolicy other = (SnapshotDeletionPolicy) super.clone();+ other.primary = this.primary.clone();+ other.lastCommit = null;+ other.segmentsFileToIDs = new HashMap<String, Set<String>>(segmentsFileToIDs);+ other.idToSnapshot = new HashMap<String, SnapshotInfo>(idToSnapshot);+ return other;+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/util/InfoStream.java b/lucene/core/src/java/org/apache/lucene/util/InfoStream.javaindex 7e8a5d36f07..deef9fa33c3 100644--- a/lucene/core/src/java/org/apache/lucene/util/InfoStream.java+++ b/lucene/core/src/java/org/apache/lucene/util/InfoStream.java@@ -30,7 +30,7 @@ import java.io.Closeable;** @lucene.internal*/-public abstract class InfoStream implements Closeable {+public abstract class InfoStream implements Closeable, Cloneable {/** Instance of InfoStream that does no logging at all. */public static final InfoStream NO_OUTPUT = new NoOutput();@@ -74,5 +74,14 @@ public abstract class InfoStream implements Closeable {}defaultInfoStream = infoStream;}-++ @Override+ public InfoStream clone() {+ try {+ return (InfoStream) super.clone();+ } catch (CloneNotSupportedException e) {+ throw new Error(e);+ }+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestConcurrentMergeScheduler.java b/lucene/core/src/test/org/apache/lucene/index/TestConcurrentMergeScheduler.javaindex a90fe6f7f35..0c76427366e 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestConcurrentMergeScheduler.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestConcurrentMergeScheduler.java@@ -365,7 +365,7 @@ public class TestConcurrentMergeScheduler extends LuceneTestCase {w.deleteDocuments(new Term("id", ""+random().nextInt(i+1)));}}- assertTrue(((TrackingCMS) iwc.getMergeScheduler()).totMergedBytes != 0);+ assertTrue(((TrackingCMS) w.w.getConfig().getMergeScheduler()).totMergedBytes != 0);w.close();d.close();}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.javaindex 50dafb41de3..65b503d2430 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.java@@ -57,10 +57,15 @@ public class TestDeletionPolicy extends LuceneTestCase {}}- class KeepAllDeletionPolicy implements IndexDeletionPolicy {+ class KeepAllDeletionPolicy extends IndexDeletionPolicy {int numOnInit;int numOnCommit;Directory dir;++ KeepAllDeletionPolicy(Directory dir) {+ this.dir = dir;+ }+@Overridepublic void onInit(List<? extends IndexCommit> commits) throws IOException {verifyCommitOrder(commits);@@ -75,13 +80,14 @@ public class TestDeletionPolicy extends LuceneTestCase {verifyCommitOrder(commits);numOnCommit++;}+}/*** This is useful for adding to a big index when you know* readers are not using it.*/- class KeepNoneOnInitDeletionPolicy implements IndexDeletionPolicy {+ class KeepNoneOnInitDeletionPolicy extends IndexDeletionPolicy {int numOnInit;int numOnCommit;@Override@@ -106,7 +112,7 @@ public class TestDeletionPolicy extends LuceneTestCase {}}- class KeepLastNDeletionPolicy implements IndexDeletionPolicy {+ class KeepLastNDeletionPolicy extends IndexDeletionPolicy {int numOnInit;int numOnCommit;int numToKeep;@@ -165,7 +171,7 @@ public class TestDeletionPolicy extends LuceneTestCase {* Delete a commit only when it has been obsoleted by N* seconds.*/- class ExpirationTimeDeletionPolicy implements IndexDeletionPolicy {+ class ExpirationTimeDeletionPolicy extends IndexDeletionPolicy {Directory dir;double expirationTimeSeconds;@@ -209,15 +215,15 @@ public class TestDeletionPolicy extends LuceneTestCase {final double SECONDS = 2.0;Directory dir = newDirectory();- ExpirationTimeDeletionPolicy policy = new ExpirationTimeDeletionPolicy(dir, SECONDS);IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT,new MockAnalyzer(random()))- .setIndexDeletionPolicy(policy);+ .setIndexDeletionPolicy(new ExpirationTimeDeletionPolicy(dir, SECONDS));MergePolicy mp = conf.getMergePolicy();if (mp instanceof LogMergePolicy) {((LogMergePolicy) mp).setUseCompoundFile(true);}IndexWriter writer = new IndexWriter(dir, conf);+ ExpirationTimeDeletionPolicy policy = (ExpirationTimeDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();Map<String,String> commitData = new HashMap<String,String>();commitData.put("commitTime", String.valueOf(System.currentTimeMillis()));writer.setCommitData(commitData);@@ -238,6 +244,7 @@ public class TestDeletionPolicy extends LuceneTestCase {((LogMergePolicy) mp).setUseCompoundFile(true);}writer = new IndexWriter(dir, conf);+ policy = (ExpirationTimeDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();for(int j=0;j<17;j++) {addDoc(writer);}@@ -305,21 +312,19 @@ public class TestDeletionPolicy extends LuceneTestCase {boolean useCompoundFile = (pass % 2) != 0;- // Never deletes a commit- KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy();-Directory dir = newDirectory();- policy.dir = dir;IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))- .setIndexDeletionPolicy(policy).setMaxBufferedDocs(10)+ .setIndexDeletionPolicy(new KeepAllDeletionPolicy(dir))+ .setMaxBufferedDocs(10).setMergeScheduler(new SerialMergeScheduler());MergePolicy mp = conf.getMergePolicy();if (mp instanceof LogMergePolicy) {((LogMergePolicy) mp).setUseCompoundFile(useCompoundFile);}IndexWriter writer = new IndexWriter(dir, conf);+ KeepAllDeletionPolicy policy = (KeepAllDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();for(int i=0;i<107;i++) {addDoc(writer);}@@ -343,9 +348,11 @@ public class TestDeletionPolicy extends LuceneTestCase {System.out.println("TEST: open writer for forceMerge");}writer = new IndexWriter(dir, conf);+ policy = (KeepAllDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.forceMerge(1);writer.close();}+assertEquals(needsMerging ? 1:0, policy.numOnInit);// If we are not auto committing then there should@@ -397,19 +404,16 @@ public class TestDeletionPolicy extends LuceneTestCase {* then, opens a new IndexWriter on a previous commit* point. */public void testOpenPriorSnapshot() throws IOException {- // Never deletes a commit- KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy();-Directory dir = newDirectory();- policy.dir = dir;IndexWriter writer = new IndexWriter(dir,newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).- setIndexDeletionPolicy(policy).+ setIndexDeletionPolicy(new KeepAllDeletionPolicy(dir)).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy(10)));+ KeepAllDeletionPolicy policy = (KeepAllDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();for(int i=0;i<10;i++) {addDoc(writer);if ((1+i)%2 == 0)@@ -508,19 +512,19 @@ public class TestDeletionPolicy extends LuceneTestCase {boolean useCompoundFile = (pass % 2) != 0;- KeepNoneOnInitDeletionPolicy policy = new KeepNoneOnInitDeletionPolicy();-Directory dir = newDirectory();IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))- .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)+ .setOpenMode(OpenMode.CREATE)+ .setIndexDeletionPolicy(new KeepNoneOnInitDeletionPolicy()).setMaxBufferedDocs(10);MergePolicy mp = conf.getMergePolicy();if (mp instanceof LogMergePolicy) {((LogMergePolicy) mp).setUseCompoundFile(useCompoundFile);}IndexWriter writer = new IndexWriter(dir, conf);+ KeepNoneOnInitDeletionPolicy policy = (KeepNoneOnInitDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();for(int i=0;i<107;i++) {addDoc(writer);}@@ -533,6 +537,7 @@ public class TestDeletionPolicy extends LuceneTestCase {((LogMergePolicy) mp).setUseCompoundFile(true);}writer = new IndexWriter(dir, conf);+ policy = (KeepNoneOnInitDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.forceMerge(1);writer.close();@@ -563,17 +568,18 @@ public class TestDeletionPolicy extends LuceneTestCase {Directory dir = newDirectory();KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);-for(int j=0;j<N+1;j++) {IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))- .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)+ .setOpenMode(OpenMode.CREATE)+ .setIndexDeletionPolicy(policy).setMaxBufferedDocs(10);MergePolicy mp = conf.getMergePolicy();if (mp instanceof LogMergePolicy) {((LogMergePolicy) mp).setUseCompoundFile(useCompoundFile);}IndexWriter writer = new IndexWriter(dir, conf);+ policy = (KeepLastNDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();for(int i=0;i<17;i++) {addDoc(writer);}@@ -623,18 +629,18 @@ public class TestDeletionPolicy extends LuceneTestCase {boolean useCompoundFile = (pass % 2) != 0;- KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);-Directory dir = newDirectory();IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))- .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)+ .setOpenMode(OpenMode.CREATE)+ .setIndexDeletionPolicy(new KeepLastNDeletionPolicy(N)).setMaxBufferedDocs(10);MergePolicy mp = conf.getMergePolicy();if (mp instanceof LogMergePolicy) {((LogMergePolicy) mp).setUseCompoundFile(useCompoundFile);}IndexWriter writer = new IndexWriter(dir, conf);+ KeepLastNDeletionPolicy policy = (KeepLastNDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.close();Term searchTerm = new Term("content", "aaa");Query query = new TermQuery(searchTerm);@@ -650,6 +656,7 @@ public class TestDeletionPolicy extends LuceneTestCase {((LogMergePolicy) mp).setUseCompoundFile(useCompoundFile);}writer = new IndexWriter(dir, conf);+ policy = (KeepLastNDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();for(int j=0;j<17;j++) {addDocWithID(writer, i*(N+1)+j);}@@ -659,6 +666,7 @@ public class TestDeletionPolicy extends LuceneTestCase {.setIndexDeletionPolicy(policy).setMergePolicy(NoMergePolicy.COMPOUND_FILES);writer = new IndexWriter(dir, conf);+ policy = (KeepLastNDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.deleteDocuments(new Term("id", "" + (i*(N+1)+3)));// this is a commitwriter.close();@@ -671,6 +679,7 @@ public class TestDeletionPolicy extends LuceneTestCase {writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy));+ policy = (KeepLastNDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();// This will not commit: there are no changes// pending because we opened for "create":writer.close();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.javaindex 6fa2ad7f2a3..32123c5bd2e 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java@@ -893,9 +893,9 @@ public void testFilesOpenClose() throws IOException {// LUCENE-2753public void testListCommits() throws Exception {Directory dir = newDirectory();- SnapshotDeletionPolicy sdp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(- TEST_VERSION_CURRENT, null).setIndexDeletionPolicy(sdp));+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, null)+ .setIndexDeletionPolicy(new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy())));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.addDocument(new Document());writer.commit();sdp.snapshot("c1");diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.javaindex 1c8cbbe5d3e..e80d1bb19a3 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReaderReopen.java@@ -530,7 +530,7 @@ public class TestDirectoryReaderReopen extends LuceneTestCase {protected abstract void modifyIndex(int i) throws IOException;}- static class KeepAllCommits implements IndexDeletionPolicy {+ static class KeepAllCommits extends IndexDeletionPolicy {@Overridepublic void onInit(List<? extends IndexCommit> commits) {}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 3f86f9a1598..9d7028ee7b9 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -1371,10 +1371,10 @@ public class TestIndexWriter extends LuceneTestCase {// Validates that iw.deleteUnusedFiles() also deletes unused index commits// in case a deletion policy which holds onto commits is used.Directory dir = newDirectory();- SnapshotDeletionPolicy sdp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))- .setIndexDeletionPolicy(sdp));+ .setIndexDeletionPolicy(new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy())));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();// First commitDocument doc = new Document();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.javaindex 444ca87ac38..2f65d10d54a 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java@@ -216,8 +216,26 @@ public class TestIndexWriterConfig extends LuceneTestCase {IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()));IndexWriterConfig clone = conf.clone();- // Clone is shallow since not all parameters are cloneable.- assertTrue(conf.getIndexDeletionPolicy() == clone.getIndexDeletionPolicy());+ // Make sure parameters that can't be reused are cloned+ IndexDeletionPolicy delPolicy = conf.delPolicy;+ IndexDeletionPolicy delPolicyClone = clone.delPolicy;+ assertTrue(delPolicy.getClass() == delPolicyClone.getClass() && (delPolicy != delPolicyClone || delPolicy.clone() == delPolicyClone.clone()));++ FlushPolicy flushPolicy = conf.flushPolicy;+ FlushPolicy flushPolicyClone = clone.flushPolicy;+ assertTrue(flushPolicy.getClass() == flushPolicyClone.getClass() && (flushPolicy != flushPolicyClone || flushPolicy.clone() == flushPolicyClone.clone()));++ DocumentsWriterPerThreadPool pool = conf.indexerThreadPool;+ DocumentsWriterPerThreadPool poolClone = clone.indexerThreadPool;+ assertTrue(pool.getClass() == poolClone.getClass() && (pool != poolClone || pool.clone() == poolClone.clone()));++ MergePolicy mergePolicy = conf.mergePolicy;+ MergePolicy mergePolicyClone = clone.mergePolicy;+ assertTrue(mergePolicy.getClass() == mergePolicyClone.getClass() && (mergePolicy != mergePolicyClone || mergePolicy.clone() == mergePolicyClone.clone()));++ MergeScheduler mergeSched = conf.mergeScheduler;+ MergeScheduler mergeSchedClone = clone.mergeScheduler;+ assertTrue(mergeSched.getClass() == mergeSchedClone.getClass() && (mergeSched != mergeSchedClone || mergeSched.clone() == mergeSchedClone.clone()));conf.setMergeScheduler(new SerialMergeScheduler());assertEquals(ConcurrentMergeScheduler.class, clone.getMergeScheduler().getClass());diff --git a/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.javaindex 5c10657dea2..0ccc94a5ee5 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java@@ -25,7 +25,6 @@ import org.apache.lucene.document.Document;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.Directory;import org.apache.lucene.store.LockObtainFailedException;-import org.apache.lucene.store.MockDirectoryWrapper;import org.junit.After;import org.junit.Before;import org.junit.Test;@@ -79,8 +78,8 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPopublic void testExistingSnapshots() throws Exception {int numSnapshots = 3;Directory dir = newDirectory();- PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), psdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();prepareIndexAndSnapshots(psdp, writer, numSnapshots, "snapshot");writer.close();psdp.close();@@ -143,8 +142,8 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPo@Testpublic void testSnapshotRelease() throws Exception {Directory dir = newDirectory();- PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), psdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();prepareIndexAndSnapshots(psdp, writer, 1, "snapshot");writer.close();@@ -166,8 +165,8 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPo// static read method works.int numSnapshots = 1;Directory dir = newDirectory();- PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), psdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();prepareIndexAndSnapshots(psdp, writer, numSnapshots, "snapshot");writer.close();dir.close();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.javaindex e31b71c21d9..2f7e3c08830 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java@@ -101,10 +101,10 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {// Run for ~1 secondsfinal long stopTime = System.currentTimeMillis() + 1000;- SnapshotDeletionPolicy dp = getDeletionPolicy();final IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(- TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexDeletionPolicy(dp)+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexDeletionPolicy(getDeletionPolicy()).setMaxBufferedDocs(2));+ SnapshotDeletionPolicy dp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.commit();final Thread t = new Thread() {@@ -235,11 +235,11 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {@Testpublic void testBasicSnapshots() throws Exception {int numSnapshots = 3;- SnapshotDeletionPolicy sdp = getDeletionPolicy();// Create 3 snapshots: snapshot0, snapshot1, snapshot2Directory dir = newDirectory();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), sdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();prepareIndexAndSnapshots(sdp, writer, numSnapshots, "snapshot");writer.close();@@ -269,8 +269,8 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {@Testpublic void testMultiThreadedSnapshotting() throws Exception {Directory dir = newDirectory();- final SnapshotDeletionPolicy sdp = getDeletionPolicy();- final IndexWriter writer = new IndexWriter(dir, getConfig(random(), sdp));+ final IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ final SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();Thread[] threads = new Thread[10];for (int i = 0; i < threads.length; i++) {@@ -315,8 +315,8 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {int numSnapshots = 2;Directory dir = newDirectory();- SnapshotDeletionPolicy sdp = getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), sdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();prepareIndexAndSnapshots(sdp, writer, numSnapshots, "snapshot");writer.close();@@ -338,8 +338,8 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {@Testpublic void testReleaseSnapshot() throws Exception {Directory dir = newDirectory();- SnapshotDeletionPolicy sdp = getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), sdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();prepareIndexAndSnapshots(sdp, writer, 1, "snapshot");// Create another commit - we must do that, because otherwise the "snapshot"@@ -370,8 +370,8 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {// asserts that those snapshots/commit points are protected.int numSnapshots = 3;Directory dir = newDirectory();- SnapshotDeletionPolicy sdp = getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), sdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();prepareIndexAndSnapshots(sdp, writer, numSnapshots, "snapshot");writer.close();@@ -389,8 +389,8 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {public void testSnapshotLastCommitTwice() throws Exception {Directory dir = newDirectory();- SnapshotDeletionPolicy sdp = getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), sdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.addDocument(new Document());writer.commit();@@ -418,8 +418,8 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {// Tests the behavior of SDP when commits that are given at ctor are missing// on onInit().Directory dir = newDirectory();- SnapshotDeletionPolicy sdp = getDeletionPolicy();- IndexWriter writer = new IndexWriter(dir, getConfig(random(), sdp));+ IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy()));+ SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();writer.addDocument(new Document());writer.commit();IndexCommit ic = sdp.snapshot("s1");@@ -436,8 +436,9 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {// Now reinit SDP from the commits in the index - the snapshot id should not// exist anymore.- sdp = getDeletionPolicy(sdp.getSnapshots());- new IndexWriter(dir, getConfig(random(), sdp)).close();+ writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy(sdp.getSnapshots())));+ sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();+ writer.close();try {sdp.getSnapshot("s1");diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTransactionRollback.java b/lucene/core/src/test/org/apache/lucene/index/TestTransactionRollback.javaindex 77b0e403fcc..ef926dbf577 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestTransactionRollback.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestTransactionRollback.java@@ -156,7 +156,7 @@ public class TestTransactionRollback extends LuceneTestCase {}// Rolls back to previous commit point- class RollbackDeletionPolicy implements IndexDeletionPolicy {+ class RollbackDeletionPolicy extends IndexDeletionPolicy {private int rollbackPoint;public RollbackDeletionPolicy(int rollbackPoint) {@@ -196,7 +196,7 @@ public class TestTransactionRollback extends LuceneTestCase {}}- class DeleteLastCommitPolicy implements IndexDeletionPolicy {+ class DeleteLastCommitPolicy extends IndexDeletionPolicy {@Overridepublic void onCommit(List<? extends IndexCommit> commits) throws IOException {}@@ -221,7 +221,7 @@ public class TestTransactionRollback extends LuceneTestCase {}// Keeps all commit points (used to build index)- class KeepAllDeletionPolicy implements IndexDeletionPolicy {+ class KeepAllDeletionPolicy extends IndexDeletionPolicy {@Overridepublic void onCommit(List<? extends IndexCommit> commits) throws IOException {}@Overridediff --git a/solr/core/src/java/org/apache/solr/core/IndexDeletionPolicyWrapper.java b/solr/core/src/java/org/apache/solr/core/IndexDeletionPolicyWrapper.javaindex ac0c9853fab..57692e845c0 100644--- a/solr/core/src/java/org/apache/solr/core/IndexDeletionPolicyWrapper.java+++ b/solr/core/src/java/org/apache/solr/core/IndexDeletionPolicyWrapper.java@@ -18,6 +18,7 @@ package org.apache.solr.core;import org.apache.lucene.index.IndexCommit;import org.apache.lucene.index.IndexDeletionPolicy;+import org.apache.lucene.index.IndexWriter;import org.apache.lucene.store.Directory;import org.apache.solr.update.SolrIndexWriter;@@ -32,11 +33,15 @@ import java.util.concurrent.atomic.AtomicInteger;* Provides features for looking up IndexCommit given a version. Allows reserving index* commit points for certain amounts of time to support features such as index replication* or snapshooting directly out of a live index directory.- *+ * <p/>+ * <b>NOTE</b>: The {@link #clone()} method returns <tt>this</tt> in order to make+ * this {@link IndexDeletionPolicy} instance trackable across {@link IndexWriter}+ * instantiations. This is correct because each core has its own+ * {@link IndexDeletionPolicy} and never has more than one open {@link IndexWriter}.** @see org.apache.lucene.index.IndexDeletionPolicy*/-public class IndexDeletionPolicyWrapper implements IndexDeletionPolicy {+public final class IndexDeletionPolicyWrapper extends IndexDeletionPolicy {private final IndexDeletionPolicy deletionPolicy;private volatile Map<Long, IndexCommit> solrVersionVsCommits = new ConcurrentHashMap<Long, IndexCommit>();private final Map<Long, Long> reserves = new ConcurrentHashMap<Long,Long>();@@ -244,5 +249,11 @@ public class IndexDeletionPolicyWrapper implements IndexDeletionPolicy {return 0;}}++ @Override+ public IndexDeletionPolicy clone() {+ // see class-level javadocs+ return this;+ }}diff --git a/solr/core/src/java/org/apache/solr/core/SolrDeletionPolicy.java b/solr/core/src/java/org/apache/solr/core/SolrDeletionPolicy.javaindex 2cd9141249e..9c5593cae15 100644--- a/solr/core/src/java/org/apache/solr/core/SolrDeletionPolicy.java+++ b/solr/core/src/java/org/apache/solr/core/SolrDeletionPolicy.java@@ -41,7 +41,7 @@ import java.util.Locale;** @see org.apache.lucene.index.IndexDeletionPolicy*/-public class SolrDeletionPolicy implements IndexDeletionPolicy, NamedListInitializedPlugin {+public class SolrDeletionPolicy extends IndexDeletionPolicy implements NamedListInitializedPlugin {public static Logger log = LoggerFactory.getLogger(SolrCore.class);private String maxCommitAge = null;diff --git a/solr/core/src/test/org/apache/solr/core/FakeDeletionPolicy.java b/solr/core/src/test/org/apache/solr/core/FakeDeletionPolicy.javaindex b452f1a43c7..3f463a11c69 100644--- a/solr/core/src/test/org/apache/solr/core/FakeDeletionPolicy.java+++ b/solr/core/src/test/org/apache/solr/core/FakeDeletionPolicy.java@@ -26,7 +26,7 @@ import java.util.List;/****/-public class FakeDeletionPolicy implements IndexDeletionPolicy, NamedListInitializedPlugin {+public class FakeDeletionPolicy extends IndexDeletionPolicy implements NamedListInitializedPlugin {private String var1;private String var2;