Search in sources :

Example 1 with SingleFieldsVisitor

use of org.elasticsearch.index.fieldvisitor.SingleFieldsVisitor in project elasticsearch by elastic.

the class RefreshListenersTests method testLotsOfThreads.

/**
     * Uses a bunch of threads to index, wait for refresh, and non-realtime get documents to validate that they are visible after waiting
     * regardless of what crazy sequence of events causes the refresh listener to fire.
     */
public void testLotsOfThreads() throws Exception {
    int threadCount = between(3, 10);
    maxListeners = between(1, threadCount * 2);
    // This thread just refreshes every once in a while to cause trouble.
    Cancellable refresher = threadPool.scheduleWithFixedDelay(() -> engine.refresh("because test"), timeValueMillis(100), Names.SAME);
    // These threads add and block until the refresh makes the change visible and then do a non-realtime get.
    Thread[] indexers = new Thread[threadCount];
    for (int thread = 0; thread < threadCount; thread++) {
        final String threadId = String.format(Locale.ROOT, "%04d", thread);
        indexers[thread] = new Thread(() -> {
            for (int iteration = 1; iteration <= 50; iteration++) {
                try {
                    String testFieldValue = String.format(Locale.ROOT, "%s%04d", threadId, iteration);
                    Engine.IndexResult index = index(threadId, testFieldValue);
                    assertEquals(iteration, index.getVersion());
                    DummyRefreshListener listener = new DummyRefreshListener();
                    listeners.addOrNotify(index.getTranslogLocation(), listener);
                    assertBusy(() -> assertNotNull("listener never called", listener.forcedRefresh.get()));
                    if (threadCount < maxListeners) {
                        assertFalse(listener.forcedRefresh.get());
                    }
                    listener.assertNoError();
                    Engine.Get get = new Engine.Get(false, new Term("_uid", Uid.createUid("test", threadId)));
                    try (Engine.GetResult getResult = engine.get(get)) {
                        assertTrue("document not found", getResult.exists());
                        assertEquals(iteration, getResult.version());
                        SingleFieldsVisitor visitor = new SingleFieldsVisitor("test");
                        getResult.docIdAndVersion().context.reader().document(getResult.docIdAndVersion().docId, visitor);
                        assertEquals(Arrays.asList(testFieldValue), visitor.fields().get("test"));
                    }
                } catch (Exception t) {
                    throw new RuntimeException("failure on the [" + iteration + "] iteration of thread [" + threadId + "]", t);
                }
            }
        });
        indexers[thread].start();
    }
    for (Thread indexer : indexers) {
        indexer.join();
    }
    refresher.cancel();
}
Also used : Cancellable(org.elasticsearch.threadpool.ThreadPool.Cancellable) Term(org.apache.lucene.index.Term) SingleFieldsVisitor(org.elasticsearch.index.fieldvisitor.SingleFieldsVisitor) IOException(java.io.IOException) Engine(org.elasticsearch.index.engine.Engine) InternalEngine(org.elasticsearch.index.engine.InternalEngine)

Aggregations

IOException (java.io.IOException)1 Term (org.apache.lucene.index.Term)1 Engine (org.elasticsearch.index.engine.Engine)1 InternalEngine (org.elasticsearch.index.engine.InternalEngine)1 SingleFieldsVisitor (org.elasticsearch.index.fieldvisitor.SingleFieldsVisitor)1 Cancellable (org.elasticsearch.threadpool.ThreadPool.Cancellable)1