Search in sources :

Example 61 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project druid by druid-io.

the class LookupCoordinatorManagerTest method testUpdateAllOnHostFailsWithFailedThings.

@Test
public void testUpdateAllOnHostFailsWithFailedThings() throws Exception {
    final HttpResponseHandler<InputStream, InputStream> responseHandler = EasyMock.createStrictMock(HttpResponseHandler.class);
    final String failedLookup = "failedLookup";
    final URL url = LookupCoordinatorManager.getLookupsURL(HostAndPort.fromString("localhost"));
    final SettableFuture<InputStream> future = SettableFuture.create();
    future.set(new ByteArrayInputStream(StringUtils.toUtf8(mapper.writeValueAsString(ImmutableMap.of("status", "accepted", LookupModule.FAILED_UPDATES_KEY, ImmutableMap.of(failedLookup, ImmutableMap.of()))))));
    EasyMock.expect(client.go(EasyMock.<Request>anyObject(), EasyMock.<SequenceInputStreamResponseHandler>anyObject(), EasyMock.<Duration>anyObject())).andReturn(future).once();
    EasyMock.replay(client, responseHandler);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig) {

        @Override
        HttpResponseHandler<InputStream, InputStream> makeResponseHandler(final AtomicInteger returnCode, final AtomicReference<String> reasonString) {
            returnCode.set(200);
            reasonString.set("");
            return responseHandler;
        }
    };
    expectedException.expectMessage("Lookups failed to update: [\"" + failedLookup + "\"]");
    try {
        manager.updateAllOnHost(url, SINGLE_LOOKUP_MAP);
    } finally {
        EasyMock.verify(client, responseHandler);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) Duration(org.joda.time.Duration) AtomicReference(java.util.concurrent.atomic.AtomicReference) SequenceInputStreamResponseHandler(com.metamx.http.client.response.SequenceInputStreamResponseHandler) URL(java.net.URL) Test(org.junit.Test)

Example 62 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project druid by druid-io.

the class LookupCoordinatorManagerTest method testDeleteAllTierMissing.

@Test
public void testDeleteAllTierMissing() throws Exception {
    final HttpResponseHandler<InputStream, InputStream> responseHandler = EasyMock.createStrictMock(HttpResponseHandler.class);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig) {

        @Override
        HttpResponseHandler<InputStream, InputStream> makeResponseHandler(final AtomicInteger returnCode, final AtomicReference<String> reasonString) {
            returnCode.set(404);
            reasonString.set("");
            return responseHandler;
        }
    };
    final HostAndPort hostAndPort = HostAndPort.fromParts("someHost", 8080);
    final Collection<String> drop = ImmutableList.of("lookup1");
    EasyMock.expect(discoverer.getNodes(LookupModule.getTierListenerPath(LOOKUP_TIER))).andReturn(ImmutableList.of(hostAndPort)).once();
    final SettableFuture<InputStream> future = SettableFuture.create();
    future.set(new ByteArrayInputStream(new byte[0]));
    EasyMock.expect(client.go(EasyMock.<Request>anyObject(), EasyMock.<SequenceInputStreamResponseHandler>anyObject(), EasyMock.<Duration>anyObject())).andReturn(future).once();
    EasyMock.replay(client, discoverer, responseHandler);
    manager.deleteAllOnTier(LOOKUP_TIER, drop);
    EasyMock.verify(client, discoverer, responseHandler);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(org.joda.time.Duration) SequenceInputStreamResponseHandler(com.metamx.http.client.response.SequenceInputStreamResponseHandler) HostAndPort(com.google.common.net.HostAndPort) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 63 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project druid by druid-io.

the class LookupCoordinatorManagerTest method testDeleteAllTierContinuesOnMissing.

@Test
public void testDeleteAllTierContinuesOnMissing() throws Exception {
    final HttpResponseHandler<InputStream, InputStream> responseHandler = EasyMock.createStrictMock(HttpResponseHandler.class);
    final AtomicInteger responseHandlerCalls = new AtomicInteger(0);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig) {

        @Override
        HttpResponseHandler<InputStream, InputStream> makeResponseHandler(final AtomicInteger returnCode, final AtomicReference<String> reasonString) {
            if (responseHandlerCalls.getAndIncrement() == 0) {
                returnCode.set(404);
                reasonString.set("Not Found");
            } else {
                returnCode.set(202);
                reasonString.set("");
            }
            return responseHandler;
        }
    };
    final HostAndPort hostAndPort = HostAndPort.fromParts("someHost", 8080);
    final Collection<String> drop = ImmutableList.of("lookup1");
    EasyMock.expect(discoverer.getNodes(LookupModule.getTierListenerPath(LOOKUP_TIER))).andReturn(ImmutableList.of(hostAndPort, hostAndPort)).once();
    final SettableFuture<InputStream> future = SettableFuture.create();
    future.set(new ByteArrayInputStream(new byte[0]));
    EasyMock.expect(client.go(EasyMock.<Request>anyObject(), EasyMock.<SequenceInputStreamResponseHandler>anyObject(), EasyMock.<Duration>anyObject())).andReturn(future).times(2);
    EasyMock.replay(client, discoverer, responseHandler);
    manager.deleteAllOnTier(LOOKUP_TIER, drop);
    EasyMock.verify(client, discoverer, responseHandler);
    Assert.assertEquals(2, responseHandlerCalls.get());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(org.joda.time.Duration) SequenceInputStreamResponseHandler(com.metamx.http.client.response.SequenceInputStreamResponseHandler) HostAndPort(com.google.common.net.HostAndPort) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 64 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project druid by druid-io.

the class RemoteTaskRunnerTestUtils method makeRemoteTaskRunner.

RemoteTaskRunner makeRemoteTaskRunner(RemoteTaskRunnerConfig config) throws Exception {
    RemoteTaskRunner remoteTaskRunner = new RemoteTaskRunner(jsonMapper, config, new IndexerZkConfig(new ZkPathsConfig() {

        @Override
        public String getBase() {
            return basePath;
        }
    }, null, null, null, null, null), cf, new PathChildrenCacheFactory.Builder(), null, DSuppliers.of(new AtomicReference<>(WorkerBehaviorConfig.defaultConfig())), ScheduledExecutors.fixed(1, "Remote-Task-Runner-Cleanup--%d"), new NoopResourceManagementStrategy<WorkerTaskRunner>());
    remoteTaskRunner.start();
    return remoteTaskRunner;
}
Also used : IndexerZkConfig(io.druid.server.initialization.IndexerZkConfig) ZkPathsConfig(io.druid.server.initialization.ZkPathsConfig) AtomicReference(java.util.concurrent.atomic.AtomicReference) PathChildrenCacheFactory(io.druid.curator.cache.PathChildrenCacheFactory)

Example 65 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project druid by druid-io.

the class CompressedLongsSerdeTest method testConcurrentThreadReads.

// This test attempts to cause a race condition with the DirectByteBuffers, it's non-deterministic in causing it,
// which sucks but I can't think of a way to deterministically cause it...
private void testConcurrentThreadReads(final Supplier<IndexedLongs> supplier, final IndexedLongs indexed, final long[] vals) throws Exception {
    final AtomicReference<String> reason = new AtomicReference<String>("none");
    final int numRuns = 1000;
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch stopLatch = new CountDownLatch(2);
    final AtomicBoolean failureHappened = new AtomicBoolean(false);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.await();
            } catch (InterruptedException e) {
                failureHappened.set(true);
                reason.set("interrupt.");
                stopLatch.countDown();
                return;
            }
            try {
                for (int i = 0; i < numRuns; ++i) {
                    for (int j = 0; j < indexed.size(); ++j) {
                        final long val = vals[j];
                        final long indexedVal = indexed.get(j);
                        if (Longs.compare(val, indexedVal) != 0) {
                            failureHappened.set(true);
                            reason.set(String.format("Thread1[%d]: %d != %d", j, val, indexedVal));
                            stopLatch.countDown();
                            return;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                failureHappened.set(true);
                reason.set(e.getMessage());
            }
            stopLatch.countDown();
        }
    }).start();
    final IndexedLongs indexed2 = supplier.get();
    try {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    startLatch.await();
                } catch (InterruptedException e) {
                    stopLatch.countDown();
                    return;
                }
                try {
                    for (int i = 0; i < numRuns; ++i) {
                        for (int j = indexed2.size() - 1; j >= 0; --j) {
                            final long val = vals[j];
                            final long indexedVal = indexed2.get(j);
                            if (Longs.compare(val, indexedVal) != 0) {
                                failureHappened.set(true);
                                reason.set(String.format("Thread2[%d]: %d != %d", j, val, indexedVal));
                                stopLatch.countDown();
                                return;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    reason.set(e.getMessage());
                    failureHappened.set(true);
                }
                stopLatch.countDown();
            }
        }).start();
        startLatch.countDown();
        stopLatch.await();
    } finally {
        CloseQuietly.close(indexed2);
    }
    if (failureHappened.get()) {
        Assert.fail("Failure happened.  Reason: " + reason.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1331 Test (org.junit.Test)668 CountDownLatch (java.util.concurrent.CountDownLatch)437 IOException (java.io.IOException)263 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)205 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)159 ArrayList (java.util.ArrayList)108 HashMap (java.util.HashMap)105 List (java.util.List)95 Map (java.util.Map)77 Test (org.testng.annotations.Test)76 File (java.io.File)64 ExecutionException (java.util.concurrent.ExecutionException)60 HashSet (java.util.HashSet)54 URI (java.net.URI)48 TimeoutException (java.util.concurrent.TimeoutException)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)48 HttpServletResponse (javax.servlet.http.HttpServletResponse)46 MockResponse (okhttp3.mockwebserver.MockResponse)46 ByteBuffer (java.nio.ByteBuffer)44