From cb1fef7f6e1fcf37b88d4c6a4e9b9a665388d780 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Wed, 31 Jan 2018 09:24:15 +0100 Subject: [PATCH] Fix intermittent failure in InternalEngineTest#testRefreshScopedSearcher (#28417) This change switches the merge policy to none (for this specific test) in order to make sure that refreshes are always triggered by a change in the writer. Closes #27514 --- .../index/engine/InternalEngineTests.java | 104 +++++++++--------- 1 file changed, 55 insertions(+), 49 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index fde7d0459fe..74e94406142 100644 --- a/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -4094,61 +4094,67 @@ public class InternalEngineTests extends EngineTestCase { } public void testRefreshScopedSearcher() throws IOException { - try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); - Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)){ - assertSameReader(getSearcher, searchSearcher); - } - for (int i = 0; i < 10; i++) { - final String docId = Integer.toString(i); + try (Store store = createStore(); + InternalEngine engine = + // disable merges to make sure that the reader doesn't change unexpectedly during the test + createEngine(defaultSettings, store, createTempDir(), NoMergePolicy.INSTANCE)) { + + try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); + Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)) { + assertSameReader(getSearcher, searchSearcher); + } + for (int i = 0; i < 10; i++) { + final String docId = Integer.toString(i); + final ParsedDocument doc = + testParsedDocument(docId, null, testDocumentWithTextField(), SOURCE, null); + Engine.Index primaryResponse = indexForDoc(doc); + engine.index(primaryResponse); + } + assertTrue(engine.refreshNeeded()); + engine.refresh("test", Engine.SearcherScope.INTERNAL); + try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); + Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)) { + assertEquals(10, getSearcher.reader().numDocs()); + assertEquals(0, searchSearcher.reader().numDocs()); + assertNotSameReader(getSearcher, searchSearcher); + } + engine.refresh("test", Engine.SearcherScope.EXTERNAL); + + try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); + Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)) { + assertEquals(10, getSearcher.reader().numDocs()); + assertEquals(10, searchSearcher.reader().numDocs()); + assertSameReader(getSearcher, searchSearcher); + } + + // now ensure external refreshes are reflected on the internal reader + final String docId = Integer.toString(10); final ParsedDocument doc = testParsedDocument(docId, null, testDocumentWithTextField(), SOURCE, null); Engine.Index primaryResponse = indexForDoc(doc); engine.index(primaryResponse); - } - assertTrue(engine.refreshNeeded()); - engine.refresh("test", Engine.SearcherScope.INTERNAL); - try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); - Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)){ - assertEquals(10, getSearcher.reader().numDocs()); - assertEquals(0, searchSearcher.reader().numDocs()); - assertNotSameReader(getSearcher, searchSearcher); - } - engine.refresh("test", Engine.SearcherScope.EXTERNAL); - try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); - Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)){ - assertEquals(10, getSearcher.reader().numDocs()); - assertEquals(10, searchSearcher.reader().numDocs()); - assertSameReader(getSearcher, searchSearcher); - } - - // now ensure external refreshes are reflected on the internal reader - final String docId = Integer.toString(10); - final ParsedDocument doc = - testParsedDocument(docId, null, testDocumentWithTextField(), SOURCE, null); - Engine.Index primaryResponse = indexForDoc(doc); - engine.index(primaryResponse); - - engine.refresh("test", Engine.SearcherScope.EXTERNAL); - - try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); - Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)){ - assertEquals(11, getSearcher.reader().numDocs()); - assertEquals(11, searchSearcher.reader().numDocs()); - assertSameReader(getSearcher, searchSearcher); - } - - try (Searcher searcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL)){ - engine.refresh("test", Engine.SearcherScope.INTERNAL); - try (Searcher nextSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL)){ - assertSame(searcher.searcher(), nextSearcher.searcher()); - } - } - - try (Searcher searcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)){ engine.refresh("test", Engine.SearcherScope.EXTERNAL); - try (Searcher nextSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)){ - assertSame(searcher.searcher(), nextSearcher.searcher()); + + try (Searcher getSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL); + Searcher searchSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)) { + assertEquals(11, getSearcher.reader().numDocs()); + assertEquals(11, searchSearcher.reader().numDocs()); + assertSameReader(getSearcher, searchSearcher); + } + + try (Searcher searcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL)) { + engine.refresh("test", Engine.SearcherScope.INTERNAL); + try (Searcher nextSearcher = engine.acquireSearcher("test", Engine.SearcherScope.INTERNAL)) { + assertSame(searcher.searcher(), nextSearcher.searcher()); + } + } + + try (Searcher searcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)) { + engine.refresh("test", Engine.SearcherScope.EXTERNAL); + try (Searcher nextSearcher = engine.acquireSearcher("test", Engine.SearcherScope.EXTERNAL)) { + assertSame(searcher.searcher(), nextSearcher.searcher()); + } } } }