Example usage for org.apache.solr.common.util ObjectReleaseTracker release

List of usage examples for org.apache.solr.common.util ObjectReleaseTracker release

Introduction

In this page you can find the example usage for org.apache.solr.common.util ObjectReleaseTracker release.

Prototype

public static boolean release(Object object) 

Source Link

Usage

From source file:org.apache.beam.sdk.io.solr.SolrIOTest.java

License:Apache License

/**
 * Test that retries are invoked when Solr returns error. We invoke this by calling a non existing
 * collection, and use a strategy that will retry on any SolrException. The logger is used to
 * verify expected behavior.//  w  w w. j a va2s. c  o  m
 */
@Test
public void testWriteRetry() throws Throwable {
    thrown.expect(IOException.class);
    thrown.expectMessage("Error writing to Solr");

    // entry state of the release tracker to ensure we only unregister newly created objects
    @SuppressWarnings("unchecked")
    Set<Object> entryState = ImmutableSet.copyOf(ObjectReleaseTracker.OBJECTS.keySet());

    SolrIO.Write write = SolrIO.write().withConnectionConfiguration(connectionConfiguration)
            .withRetryConfiguration(SolrIO.RetryConfiguration.create(3, Duration.standardMinutes(3))
                    .withRetryPredicate(new LenientRetryPredicate()))
            .to("wrong-collection");

    List<SolrInputDocument> data = SolrIOTestUtils.createDocuments(NUM_DOCS);
    pipeline.apply(Create.of(data)).apply(write);

    try {
        pipeline.run();
    } catch (final Pipeline.PipelineExecutionException e) {
        // Hack: await all worker threads completing (BEAM-4040)
        int waitAttempts = 30; // defensive coding
        while (namedThreadIsAlive("direct-runner-worker") && waitAttempts-- >= 0) {
            LOG.info("Pausing to allow direct-runner-worker threads to finish");
            Thread.sleep(1000);
        }

        // remove solrClients created by us as there are no guarantees on Teardown here
        for (Object o : ObjectReleaseTracker.OBJECTS.keySet()) {
            if (o instanceof SolrZkClient && !entryState.contains(o)) {
                LOG.info("Removing unreleased SolrZkClient");
                ObjectReleaseTracker.release(o);
            }
        }

        // check 2 retries were initiated by inspecting the log before passing on the exception
        expectedLogs.verifyWarn(String.format(SolrIO.Write.WriteFn.RETRY_ATTEMPT_LOG, 1));
        expectedLogs.verifyWarn(String.format(SolrIO.Write.WriteFn.RETRY_ATTEMPT_LOG, 2));

        throw e.getCause();
    }

    fail("Pipeline should not have run to completion");
}