use of java.util.concurrent.atomic.AtomicReference in project druid by druid-io.
the class CompressedVSizeIntsIndexedSupplierTest 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...
@Test
public void testConcurrentThreadReads() throws Exception {
setupSimple(4);
final AtomicReference<String> reason = new AtomicReference<>("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 IndexedInts 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());
}
}
use of java.util.concurrent.atomic.AtomicReference in project elasticsearch by elastic.
the class Netty4HttpServerTransportTests method testBadRequest.
public void testBadRequest() throws InterruptedException {
final AtomicReference<Throwable> causeReference = new AtomicReference<>();
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
@Override
public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
throw new AssertionError();
}
@Override
public void dispatchBadRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
causeReference.set(cause);
try {
final ElasticsearchException e = new ElasticsearchException("you sent a bad request and you should feel bad");
channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
} catch (final IOException e) {
throw new AssertionError(e);
}
}
};
final Settings settings;
final int maxInitialLineLength;
final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
if (randomBoolean()) {
maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
settings = Settings.EMPTY;
} else {
maxInitialLineLength = randomIntBetween(1, 8192);
settings = Settings.builder().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
}
try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher)) {
transport.start();
final TransportAddress remoteAddress = randomFrom(transport.boundAddress.boundAddresses());
try (Netty4HttpClient client = new Netty4HttpClient()) {
final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
final FullHttpResponse response = client.post(remoteAddress.address(), request);
assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
}
}
assertNotNull(causeReference.get());
assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
use of java.util.concurrent.atomic.AtomicReference in project jetty.project by eclipse.
the class SettingsBodyParser method parseBody.
public static SettingsFrame parseBody(final ByteBuffer buffer) {
final int bodyLength = buffer.remaining();
final AtomicReference<SettingsFrame> frameRef = new AtomicReference<>();
SettingsBodyParser parser = new SettingsBodyParser(null, null) {
@Override
protected int getStreamId() {
return 0;
}
@Override
protected int getBodyLength() {
return bodyLength;
}
@Override
protected boolean onSettings(Map<Integer, Integer> settings) {
frameRef.set(new SettingsFrame(settings, false));
return true;
}
@Override
protected boolean connectionFailure(ByteBuffer buffer, int error, String reason) {
frameRef.set(null);
return false;
}
};
if (bodyLength == 0)
parser.emptyBody(buffer);
else
parser.parse(buffer);
return frameRef.get();
}
use of java.util.concurrent.atomic.AtomicReference in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithDeferredContentProviderRacingWithIterator.
@Test
public void testUploadWithDeferredContentProviderRacingWithIterator() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
final CountDownLatch latch = new CountDownLatch(1);
final byte[] data = new byte[512];
final AtomicReference<DeferredContentProvider> contentRef = new AtomicReference<>();
final DeferredContentProvider content = new DeferredContentProvider() {
@Override
public Iterator<ByteBuffer> iterator() {
return new Iterator<ByteBuffer>() {
// Data for the deferred content iterator:
// [0] => deferred
// [1] => deferred
// [2] => data
private final byte[][] iteratorData = new byte[3][];
private final AtomicInteger index = new AtomicInteger();
{
iteratorData[0] = null;
iteratorData[1] = null;
iteratorData[2] = data;
}
@Override
public boolean hasNext() {
return index.get() < iteratorData.length;
}
@Override
public ByteBuffer next() {
byte[] chunk = iteratorData[index.getAndIncrement()];
ByteBuffer result = chunk == null ? null : ByteBuffer.wrap(chunk);
if (index.get() < iteratorData.length) {
contentRef.get().offer(result == null ? BufferUtil.EMPTY_BUFFER : result);
contentRef.get().close();
}
return result;
}
@Override
public void remove() {
}
};
}
};
contentRef.set(content);
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded() && result.getResponse().getStatus() == 200 && Arrays.equals(data, getContent()))
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of java.util.concurrent.atomic.AtomicReference in project vert.x by eclipse.
the class HAManager method processFailover.
// Process the failover of a deployment
private void processFailover(JsonObject failedVerticle) {
if (failDuringFailover) {
throw new VertxException("Oops!");
}
// This method must block until the failover is complete - i.e. the verticle is successfully redeployed
final String verticleName = failedVerticle.getString("verticle_name");
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> err = new AtomicReference<>();
// Now deploy this verticle on this node
ContextImpl ctx = vertx.getContext();
if (ctx != null) {
// We could be on main thread in which case we don't want to overwrite tccl
ContextImpl.setContext(null);
}
JsonObject options = failedVerticle.getJsonObject("options");
try {
doDeployVerticle(verticleName, new DeploymentOptions(options), result -> {
if (result.succeeded()) {
log.info("Successfully redeployed verticle " + verticleName + " after failover");
} else {
log.error("Failed to redeploy verticle after failover", result.cause());
err.set(result.cause());
}
latch.countDown();
Throwable t = err.get();
if (t != null) {
throw new VertxException(t);
}
});
} finally {
if (ctx != null) {
ContextImpl.setContext(ctx);
}
}
try {
if (!latch.await(120, TimeUnit.SECONDS)) {
throw new VertxException("Timed out waiting for redeploy on failover");
}
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
Aggregations