LUCENE-10134: clean up the test from leaking threads and resources if an error occurs somewhere - this obscures the original cause of the problem.

This commit is contained in:
Dawid Weiss 2021-09-30 09:11:45 +02:00
parent 88b264a368
commit d2b88b7a0b
1 changed files with 438 additions and 446 deletions

View File

@ -45,7 +45,6 @@ import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.NamedThreadFactory;
import org.apache.lucene.util.TestUtil;
@ -56,12 +55,10 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
// randomly uses SortedSetDV
public void testBasic() throws Exception {
Directory dir = newDirectory();
FacetsConfig config = new FacetsConfig();
config.setMultiValued("a", true);
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField("a", "foo"));
doc.add(new SortedSetDocValuesFacetField("a", "bar"));
@ -77,13 +74,15 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
writer.addDocument(config.build(doc));
// NRT open
IndexSearcher searcher = newSearcher(writer.getReader());
try (IndexReader r = writer.getReader()) {
IndexSearcher searcher = newSearcher(r);
// Per-top-reader state:
SortedSetDocValuesReaderState state =
new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());
ExecutorService exec = randomExecutorServiceOrNull();
try {
Facets facets = getAllFacets(searcher, state, exec);
assertEquals(
@ -99,20 +98,18 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
q.add("b", "baz");
TopDocs hits = searcher.search(q, 1);
assertEquals(1, hits.totalHits.value);
if (exec != null) {
exec.shutdownNow();
} finally {
if (exec != null) exec.shutdownNow();
}
}
}
writer.close();
IOUtils.close(searcher.getIndexReader(), dir);
}
// See: LUCENE-10070
public void testCountAll() throws Exception {
Directory dir = newDirectory();
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
FacetsConfig config = new FacetsConfig();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new StringField("id", "0", Field.Store.NO));
@ -127,7 +124,8 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
writer.deleteDocuments(new Term("id", "0"));
// NRT open
IndexSearcher searcher = newSearcher(writer.getReader());
try (IndexReader r = writer.getReader()) {
IndexSearcher searcher = newSearcher(r);
// Per-top-reader state:
SortedSetDocValuesReaderState state =
@ -147,25 +145,24 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
new NamedThreadFactory("TestIndexSearcher"));
try {
facets = new ConcurrentSortedSetDocValuesFacetCounts(state, exec);
assertEquals(
"dim=a path=[] value=1 childCount=1\n bar (1)\n",
facets.getTopChildren(10, "a").toString());
writer.close();
IOUtils.close(searcher.getIndexReader(), dir);
} finally {
exec.shutdownNow();
}
}
}
}
public void testBasicSingleValued() throws Exception {
Directory dir = newDirectory();
FacetsConfig config = new FacetsConfig();
config.setMultiValued("a", false);
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField("a", "foo"));
doc.add(new SortedSetDocValuesFacetField("b", "bar"));
@ -182,13 +179,15 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
writer.addDocument(config.build(doc));
// NRT open
IndexSearcher searcher = newSearcher(writer.getReader());
try (IndexReader r = writer.getReader()) {
IndexSearcher searcher = newSearcher(r);
// Per-top-reader state:
SortedSetDocValuesReaderState state =
new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());
ExecutorService exec = randomExecutorServiceOrNull();
try {
Facets facets = getAllFacets(searcher, state, exec);
assertEquals(
@ -204,17 +203,14 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
q.add("b", "bar");
TopDocs hits = searcher.search(q, 1);
assertEquals(1, hits.totalHits.value);
if (exec != null) {
exec.shutdownNow();
} finally {
if (exec != null) exec.shutdownNow();
}
}
}
writer.close();
IOUtils.close(searcher.getIndexReader(), dir);
}
public void testDrillDownOptions() throws Exception {
Directory dir = newDirectory();
FacetsConfig config = new FacetsConfig();
config.setDrillDownTermsIndexing("c", FacetsConfig.DrillDownTermsIndexing.NONE);
config.setDrillDownTermsIndexing(
@ -222,7 +218,8 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
config.setDrillDownTermsIndexing("e", FacetsConfig.DrillDownTermsIndexing.ALL_PATHS_NO_DIM);
config.setDrillDownTermsIndexing("f", FacetsConfig.DrillDownTermsIndexing.FULL_PATH_ONLY);
config.setDrillDownTermsIndexing("g", FacetsConfig.DrillDownTermsIndexing.ALL);
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField("c", "foo"));
@ -240,9 +237,8 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
writer.addDocument(config.build(doc));
// NRT open
IndexSearcher searcher = newSearcher(writer.getReader());
ExecutorService exec = randomExecutorServiceOrNull();
try (IndexReader r = writer.getReader()) {
IndexSearcher searcher = newSearcher(r);
// Drill down with different indexing configuration options
DrillDownQuery q = new DrillDownQuery(config);
q.add("c");
@ -293,20 +289,15 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
q.add("g", "foo");
hits = searcher.search(q, 1);
assertEquals(1, hits.totalHits.value);
if (exec != null) {
exec.shutdownNow();
}
writer.close();
IOUtils.close(searcher.getIndexReader(), dir);
}
}
// LUCENE-5090
@SuppressWarnings("unused")
public void testStaleState() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
FacetsConfig config = new FacetsConfig();
@ -314,7 +305,7 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
doc.add(new SortedSetDocValuesFacetField("a", "foo"));
writer.addDocument(config.build(doc));
IndexReader r = writer.getReader();
try (IndexReader r = writer.getReader()) {
SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(r);
doc = new Document();
@ -325,7 +316,8 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
doc.add(new SortedSetDocValuesFacetField("a", "baz"));
writer.addDocument(config.build(doc));
IndexSearcher searcher = newSearcher(writer.getReader());
try (IndexReader r2 = writer.getReader()) {
IndexSearcher searcher = newSearcher(r2);
FacetsCollector c = new FacetsCollector();
@ -336,18 +328,15 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
() -> {
new SortedSetDocValuesFacetCounts(state, c);
});
r.close();
writer.close();
searcher.getIndexReader().close();
dir.close();
}
}
}
}
// LUCENE-5333
public void testSparseFacets() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
FacetsConfig config = new FacetsConfig();
@ -375,14 +364,15 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
writer.addDocument(config.build(doc));
// NRT open
IndexSearcher searcher = newSearcher(writer.getReader());
writer.close();
try (IndexReader r = writer.getReader()) {
IndexSearcher searcher = newSearcher(r);
// Per-top-reader state:
SortedSetDocValuesReaderState state =
new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());
ExecutorService exec = randomExecutorServiceOrNull();
try {
Facets facets = getAllFacets(searcher, state, exec);
// Ask for top 10 labels for any dims that have counts:
@ -393,8 +383,10 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
"dim=a path=[] value=3 childCount=3\n foo1 (1)\n foo2 (1)\n foo3 (1)\n",
results.get(0).toString());
assertEquals(
"dim=b path=[] value=2 childCount=2\n bar1 (1)\n bar2 (1)\n", results.get(1).toString());
assertEquals("dim=c path=[] value=1 childCount=1\n baz1 (1)\n", results.get(2).toString());
"dim=b path=[] value=2 childCount=2\n bar1 (1)\n bar2 (1)\n",
results.get(1).toString());
assertEquals(
"dim=c path=[] value=1 childCount=1\n baz1 (1)\n", results.get(2).toString());
Collection<Accountable> resources = state.getChildResources();
assertTrue(state.toString().contains(FacetsConfig.DEFAULT_INDEX_FIELD_NAME));
@ -406,18 +398,16 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
assertEquals(0, state.ramBytesUsed());
assertTrue(resources.isEmpty());
}
if (exec != null) {
exec.shutdownNow();
} finally {
if (exec != null) exec.shutdownNow();
}
}
}
searcher.getIndexReader().close();
dir.close();
}
public void testSomeSegmentsMissing() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
FacetsConfig config = new FacetsConfig();
@ -436,36 +426,35 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
writer.commit();
// NRT open
IndexSearcher searcher = newSearcher(writer.getReader());
writer.close();
try (IndexReader r = writer.getReader()) {
IndexSearcher searcher = newSearcher(r);
// Per-top-reader state:
SortedSetDocValuesReaderState state =
new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());
ExecutorService exec = randomExecutorServiceOrNull();
try {
Facets facets = getAllFacets(searcher, state, exec);
// Ask for top 10 labels for any dims that have counts:
assertEquals(
"dim=a path=[] value=2 childCount=2\n foo1 (1)\n foo2 (1)\n",
facets.getTopChildren(10, "a").toString());
if (exec != null) {
exec.shutdownNow();
} finally {
if (exec != null) exec.shutdownNow();
}
}
}
searcher.getIndexReader().close();
dir.close();
}
public void testRandom() throws Exception {
int fullIterations = LuceneTestCase.TEST_NIGHTLY ? 20 : 3;
for (int fullIter = 0; fullIter < fullIterations; fullIter++) {
String[] tokens = getRandomTokens(10);
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), indexDir);
try (Directory indexDir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), indexDir)) {
FacetsConfig config = new FacetsConfig();
int numDocs = atLeast(1000);
// Most of the time allow up to 7 dims per doc, but occasionally limit all docs to a single
@ -489,13 +478,14 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
}
// NRT open
IndexSearcher searcher = newSearcher(w.getReader());
try (IndexReader r = w.getReader()) {
IndexSearcher searcher = newSearcher(r);
// Per-top-reader state:
SortedSetDocValuesReaderState state =
new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());
ExecutorService exec = randomExecutorServiceOrNull();
try {
int iters = atLeast(100);
for (int iter = 0; iter < iters; iter++) {
String searchToken = tokens[random().nextInt(tokens.length)];
@ -503,7 +493,8 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
System.out.println("\nTEST: iter content=" + searchToken);
}
FacetsCollector fc = new FacetsCollector();
FacetsCollector.search(searcher, new TermQuery(new Term("content", searchToken)), 10, fc);
FacetsCollector.search(
searcher, new TermQuery(new Term("content", searchToken)), 10, fc);
Facets facets;
if (exec != null) {
facets = new ConcurrentSortedSetDocValuesFacetCounts(state, fc, exec);
@ -563,40 +554,41 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
assertEquals(expected, actual);
}
if (exec != null) {
exec.shutdownNow();
} finally {
if (exec != null) exec.shutdownNow();
}
}
}
w.close();
IOUtils.close(searcher.getIndexReader(), indexDir, taxoDir);
}
}
public void testNonExistentDimension() throws Exception {
Directory dir = newDirectory();
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
FacetsConfig config = new FacetsConfig();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField("foo", "bar"));
writer.addDocument(config.build(doc));
writer.commit();
IndexSearcher searcher = newSearcher(writer.getReader());
try (IndexReader r = writer.getReader()) {
IndexSearcher searcher = newSearcher(r);
SortedSetDocValuesReaderState state =
new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());
ExecutorService exec = randomExecutorServiceOrNull();
try {
Facets facets = getAllFacets(searcher, state, exec);
FacetResult result = facets.getTopChildren(5, "non-existent dimension");
// make sure the result is null (and no exception was thrown)
assertNull(result);
writer.close();
IOUtils.close(searcher.getIndexReader(), dir);
if (exec != null) {
exec.shutdownNow();
} finally {
if (exec != null) exec.shutdownNow();
}
}
}
}