From 8361de87becd64c8b217313877b996ac20167856 Mon Sep 17 00:00:00 2001 From: Mike McCandless Date: Tue, 10 May 2016 05:31:02 -0400 Subject: [PATCH] LUCENE-6766: fix parallel reader's detection of conflicting index sort --- .../org/apache/lucene/index/DocValues.java | 2 +- .../lucene/index/ParallelLeafReader.java | 7 ++- .../lucene/index/TestParallelLeafReader.java | 59 ++++++++++++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/index/DocValues.java b/lucene/core/src/java/org/apache/lucene/index/DocValues.java index 15b15c6c72b..4de42387042 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DocValues.java +++ b/lucene/core/src/java/org/apache/lucene/index/DocValues.java @@ -210,7 +210,7 @@ public final class DocValues { (expected.length == 1 ? "(expected=" + expected[0] : "(expected one of " + Arrays.toString(expected)) + "). " + - " Re-index with correct docvalues type."); + "Re-index with correct docvalues type."); } } diff --git a/lucene/core/src/java/org/apache/lucene/index/ParallelLeafReader.java b/lucene/core/src/java/org/apache/lucene/index/ParallelLeafReader.java index ef9f28cf666..d85ff2d0fa2 100644 --- a/lucene/core/src/java/org/apache/lucene/index/ParallelLeafReader.java +++ b/lucene/core/src/java/org/apache/lucene/index/ParallelLeafReader.java @@ -107,10 +107,11 @@ public class ParallelLeafReader extends LeafReader { // build FieldInfos and fieldToReader map: for (final LeafReader reader : this.parallelReaders) { + Sort leafIndexSort = reader.getIndexSort(); if (indexSort == null) { - indexSort = reader.getIndexSort(); - } else if (indexSort.equals(reader.getIndexSort()) == false) { - throw new IllegalArgumentException("cannot combine LeafReaders that have different index sorts: saw both sort=" + indexSort + " and " + reader.getIndexSort()); + indexSort = leafIndexSort; + } else if (leafIndexSort != null && indexSort.equals(leafIndexSort) == false) { + throw new IllegalArgumentException("cannot combine LeafReaders that have different index sorts: saw both sort=" + indexSort + " and " + leafIndexSort); } final FieldInfos readerFieldInfos = reader.getFieldInfos(); diff --git a/lucene/core/src/test/org/apache/lucene/index/TestParallelLeafReader.java b/lucene/core/src/test/org/apache/lucene/index/TestParallelLeafReader.java index f7f401fca9a..35523f352a7 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestParallelLeafReader.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestParallelLeafReader.java @@ -23,10 +23,11 @@ import java.util.Random; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; -import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.*; +import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.Directory; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; @@ -314,4 +315,60 @@ public class TestParallelLeafReader extends LuceneTestCase { return dir2; } + // not ok to have one leaf w/ index sort and another with a different index sort + public void testWithIndexSort1() throws Exception { + Directory dir1 = newDirectory(); + IndexWriterConfig iwc1 = newIndexWriterConfig(new MockAnalyzer(random())); + iwc1.setIndexSort(new Sort(new SortField("foo", SortField.Type.INT))); + IndexWriter w1 = new IndexWriter(dir1, iwc1); + w1.addDocument(new Document()); + w1.commit(); + w1.addDocument(new Document()); + w1.forceMerge(1); + w1.close(); + IndexReader r1 = DirectoryReader.open(dir1); + + Directory dir2 = newDirectory(); + IndexWriterConfig iwc2 = newIndexWriterConfig(new MockAnalyzer(random())); + iwc2.setIndexSort(new Sort(new SortField("bar", SortField.Type.INT))); + IndexWriter w2 = new IndexWriter(dir2, iwc2); + w2.addDocument(new Document()); + w2.commit(); + w2.addDocument(new Document()); + w2.forceMerge(1); + w2.close(); + IndexReader r2 = DirectoryReader.open(dir2); + + String message = expectThrows(IllegalArgumentException.class, () -> { + new ParallelLeafReader(getOnlyLeafReader(r1), getOnlyLeafReader(r2)); + }).getMessage(); + assertEquals("cannot combine LeafReaders that have different index sorts: saw both sort= and ", message); + IOUtils.close(r1, dir1, r2, dir2); + } + + // ok to have one leaf w/ index sort and the other with no sort + public void testWithIndexSort2() throws Exception { + Directory dir1 = newDirectory(); + IndexWriterConfig iwc1 = newIndexWriterConfig(new MockAnalyzer(random())); + iwc1.setIndexSort(new Sort(new SortField("foo", SortField.Type.INT))); + IndexWriter w1 = new IndexWriter(dir1, iwc1); + w1.addDocument(new Document()); + w1.commit(); + w1.addDocument(new Document()); + w1.forceMerge(1); + w1.close(); + IndexReader r1 = DirectoryReader.open(dir1); + + Directory dir2 = newDirectory(); + IndexWriterConfig iwc2 = newIndexWriterConfig(new MockAnalyzer(random())); + IndexWriter w2 = new IndexWriter(dir2, iwc2); + w2.addDocument(new Document()); + w2.addDocument(new Document()); + w2.close(); + + IndexReader r2 = DirectoryReader.open(dir2); + new ParallelLeafReader(false, getOnlyLeafReader(r1), getOnlyLeafReader(r2)).close(); + new ParallelLeafReader(false, getOnlyLeafReader(r2), getOnlyLeafReader(r1)).close(); + IOUtils.close(r1, dir1, r2, dir2); + } }