Search in sources :

Example 1 with UnitEvent

use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.

the class EmitterTest method testSanityWithGeneralizedCreation.

@Test
public void testSanityWithGeneralizedCreation() throws Exception {
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1), new UnitEvent("test", 2));
    emitter = sizeBasedEmitterGeneralizedCreation(2);
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals(TARGET_URL, request.getUrl());
            Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
            Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(1));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    waitForEmission(emitter, 1);
    closeNoFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Example 2 with UnitEvent

use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.

the class EmitterTest method testFailedEmission.

@Test(timeout = 60_000L)
public void testFailedEmission() throws Exception {
    final UnitEvent event1 = new UnitEvent("test", 1);
    final UnitEvent event2 = new UnitEvent("test", 2);
    emitter = sizeBasedEmitter(1);
    Assert.assertEquals(0, emitter.getTotalEmittedEvents());
    Assert.assertEquals(0, emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount());
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) {
            Response response = responseBuilder(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST).build();
            return GoHandlers.immediateFuture(response);
        }
    });
    emitter.emit(event1);
    emitter.flush();
    waitForEmission(emitter, 1);
    Assert.assertTrue(httpClient.succeeded());
    // Failed to emit the first event.
    Assert.assertEquals(0, emitter.getTotalEmittedEvents());
    Assert.assertEquals(0, emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount());
    Assert.assertTrue(emitter.getFailedSendingTimeCounter().getTimeSumAndCount() > 0);
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) {
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(2));
    emitter.emit(event2);
    emitter.flush();
    waitForEmission(emitter, 2);
    closeNoFlush(emitter);
    // Failed event is emitted inside emitter thread, there is no other way to wait for it other than joining the
    // emitterThread
    emitter.joinEmitterThread();
    // Succeed to emit both events.
    Assert.assertEquals(2, emitter.getTotalEmittedEvents());
    Assert.assertTrue(emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertTrue(emitter.getFailedSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) ListenableFuture(org.asynchttpclient.ListenableFuture) Test(org.junit.Test)

Example 3 with UnitEvent

use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.

the class EmitterTest method testSizeBasedEmission.

@Test
public void testSizeBasedEmission() throws Exception {
    emitter = sizeBasedEmitter(3);
    httpClient.setGoHandler(GoHandlers.failingHandler());
    emitter.emit(new UnitEvent("test", 1));
    emitter.emit(new UnitEvent("test", 2));
    httpClient.setGoHandler(GoHandlers.passingHandler(okResponse()).times(1));
    emitter.emit(new UnitEvent("test", 3));
    waitForEmission(emitter, 1);
    httpClient.setGoHandler(GoHandlers.failingHandler());
    emitter.emit(new UnitEvent("test", 4));
    emitter.emit(new UnitEvent("test", 5));
    closeAndExpectFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Test(org.junit.Test)

Example 4 with UnitEvent

use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.

the class EmitterTest method testBatchSplitting.

@Test
public void testBatchSplitting() throws Exception {
    final byte[] big = new byte[500 * 1024];
    for (int i = 0; i < big.length; i++) {
        big[i] = 'x';
    }
    final String bigString = StringUtils.fromUtf8(big);
    final List<UnitEvent> events = Arrays.asList(new UnitEvent(bigString, 1), new UnitEvent(bigString, 2), new UnitEvent(bigString, 3), new UnitEvent(bigString, 4));
    final AtomicInteger counter = new AtomicInteger();
    emitter = manualFlushEmitterWithBatchSize(1024 * 1024);
    Assert.assertEquals(0, emitter.getTotalEmittedEvents());
    Assert.assertEquals(0, emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount());
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals(TARGET_URL, request.getUrl());
            Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
            Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(counter.getAndIncrement())), JSON_MAPPER.writeValueAsString(events.get(counter.getAndIncrement()))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(3));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    waitForEmission(emitter, 1);
    Assert.assertEquals(2, emitter.getTotalEmittedEvents());
    Assert.assertTrue(emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    emitter.flush();
    waitForEmission(emitter, 2);
    Assert.assertEquals(4, emitter.getTotalEmittedEvents());
    Assert.assertTrue(emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    closeNoFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Example 5 with UnitEvent

use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.

the class EmitterTest method testBasicAuthenticationAndNewlineSeparating.

@Test
public void testBasicAuthenticationAndNewlineSeparating() throws Exception {
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1), new UnitEvent("test", 2));
    emitter = manualFlushEmitterWithBasicAuthenticationAndNewlineSeparating(new DefaultPasswordProvider("foo:bar"));
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals(TARGET_URL, request.getUrl());
            Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
            Assert.assertEquals("Basic " + StringUtils.encodeBase64String(StringUtils.toUtf8("foo:bar")), request.getHeaders().get(HttpHeaders.Names.AUTHORIZATION));
            Assert.assertEquals(StringUtils.format("%s\n%s\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(1));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    emitter.flush();
    waitForEmission(emitter, 1);
    closeNoFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) DefaultPasswordProvider(org.apache.druid.metadata.DefaultPasswordProvider) Test(org.junit.Test)

Aggregations

UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)12 Test (org.junit.Test)12 Request (org.asynchttpclient.Request)10 Response (org.asynchttpclient.Response)10 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DefaultPasswordProvider (org.apache.druid.metadata.DefaultPasswordProvider)1 ListenableFuture (org.asynchttpclient.ListenableFuture)1