Search in sources :

Example 6 with BigEndianHeapChannelBuffer

use of org.jboss.netty.buffer.BigEndianHeapChannelBuffer in project druid by druid-io.

the class SequenceInputStreamResponseHandlerTest method testExceptionalChunkedStream.

@Test(expected = TesterException.class)
public void testExceptionalChunkedStream() throws IOException {
    Iterator<byte[]> it = BYTE_LIST.iterator();
    SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler();
    final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.setChunked(true);
    ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null);
    final int failAt = RANDOM.nextInt(allBytes.length);
    long chunkNum = 0;
    while (it.hasNext()) {
        final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next()) {

            @Override
            public void getBytes(int index, byte[] dst, int dstIndex, int length) {
                if (dstIndex + length >= failAt) {
                    throw new TesterException();
                }
                super.getBytes(index, dst, dstIndex, length);
            }
        });
        clientResponse = responseHandler.handleChunk(clientResponse, chunk, ++chunkNum);
    }
    clientResponse = responseHandler.done(clientResponse);
    final InputStream stream = clientResponse.getObj();
    final byte[] buff = new byte[allBytes.length];
    fillBuff(stream, buff);
}
Also used : DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BigEndianHeapChannelBuffer(org.jboss.netty.buffer.BigEndianHeapChannelBuffer) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.Test)

Example 7 with BigEndianHeapChannelBuffer

use of org.jboss.netty.buffer.BigEndianHeapChannelBuffer in project druid by druid-io.

the class SequenceInputStreamResponseHandlerTest method simpleSingleStreamTest.

@Test
public void simpleSingleStreamTest() throws IOException {
    SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler();
    final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.setChunked(false);
    response.setContent(new BigEndianHeapChannelBuffer(allBytes));
    ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null);
    clientResponse = responseHandler.done(clientResponse);
    final InputStream stream = clientResponse.getObj();
    final InputStream expectedStream = new ByteArrayInputStream(allBytes);
    int read = 0;
    while (read < allBytes.length) {
        final byte[] expectedBytes = new byte[Math.min(RANDOM.nextInt(128), allBytes.length - read)];
        final byte[] actualBytes = new byte[expectedBytes.length];
        fillBuff(stream, actualBytes);
        fillBuff(expectedStream, expectedBytes);
        Assert.assertArrayEquals(expectedBytes, actualBytes);
        read += expectedBytes.length;
    }
    Assert.assertEquals(allBytes.length, responseHandler.getByteCount());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BigEndianHeapChannelBuffer(org.jboss.netty.buffer.BigEndianHeapChannelBuffer) Test(org.junit.Test)

Example 8 with BigEndianHeapChannelBuffer

use of org.jboss.netty.buffer.BigEndianHeapChannelBuffer in project druid by druid-io.

the class ObjectOrErrorResponseHandlerTest method testExceptionAfterOk.

@Test
public void testExceptionAfterOk() throws Exception {
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.setChunked(false);
    response.setContent(new BigEndianHeapChannelBuffer("abcd".getBytes(StringUtils.UTF8_STRING)));
    final ObjectOrErrorResponseHandler<InputStreamFullResponseHolder, InputStreamFullResponseHolder> responseHandler = new ObjectOrErrorResponseHandler<>(new InputStreamFullResponseHandler());
    ClientResponse<Either<StringFullResponseHolder, InputStreamFullResponseHolder>> clientResp = responseHandler.handleResponse(response, null);
    Exception ex = new RuntimeException("dummy!");
    responseHandler.exceptionCaught(clientResp, ex);
    // Exception after HTTP OK still is handled by the "OK handler"
    // (The handler that starts the request gets to finish it.)
    Assert.assertTrue(clientResp.isFinished());
    Assert.assertTrue(clientResp.getObj().isValue());
    final InputStream responseStream = clientResp.getObj().valueOrThrow().getContent();
    final IOException e = Assert.assertThrows(IOException.class, () -> IOUtils.toString(responseStream, StandardCharsets.UTF_8));
    Assert.assertEquals("java.lang.RuntimeException: dummy!", e.getMessage());
}
Also used : InputStream(java.io.InputStream) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BigEndianHeapChannelBuffer(org.jboss.netty.buffer.BigEndianHeapChannelBuffer) IOException(java.io.IOException) IOException(java.io.IOException) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Either(org.apache.druid.java.util.common.Either) Test(org.junit.Test)

Example 9 with BigEndianHeapChannelBuffer

use of org.jboss.netty.buffer.BigEndianHeapChannelBuffer in project druid by druid-io.

the class HttpIndexingServiceClientTest method testGetTotalWorkerCapacityWithAutoScale.

@Test
public void testGetTotalWorkerCapacityWithAutoScale() throws Exception {
    int currentClusterCapacity = 5;
    int maximumCapacityWithAutoScale = 10;
    // Mock response for /druid/indexer/v1/totalWorkerCapacity
    HttpResponse totalWorkerCapacityResponse = EasyMock.createMock(HttpResponse.class);
    EasyMock.expect(totalWorkerCapacityResponse.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
    EasyMock.expect(totalWorkerCapacityResponse.getContent()).andReturn(new BigEndianHeapChannelBuffer(0));
    EasyMock.replay(totalWorkerCapacityResponse);
    IndexingTotalWorkerCapacityInfo indexingTotalWorkerCapacityInfo = new IndexingTotalWorkerCapacityInfo(currentClusterCapacity, maximumCapacityWithAutoScale);
    StringFullResponseHolder autoScaleResponseHolder = new StringFullResponseHolder(totalWorkerCapacityResponse, StandardCharsets.UTF_8).addChunk(jsonMapper.writeValueAsString(indexingTotalWorkerCapacityInfo));
    EasyMock.expect(druidLeaderClient.go(EasyMock.anyObject(Request.class))).andReturn(autoScaleResponseHolder).once();
    EasyMock.expect(druidLeaderClient.makeRequest(HttpMethod.GET, "/druid/indexer/v1/totalWorkerCapacity")).andReturn(new Request(HttpMethod.GET, new URL("http://localhost:8090/druid/indexer/v1/totalWorkerCapacity"))).once();
    EasyMock.replay(druidLeaderClient);
    final int actualResponse = httpIndexingServiceClient.getTotalWorkerCapacityWithAutoScale();
    Assert.assertEquals(maximumCapacityWithAutoScale, actualResponse);
    EasyMock.verify(druidLeaderClient);
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) Request(org.apache.druid.java.util.http.client.Request) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BigEndianHeapChannelBuffer(org.jboss.netty.buffer.BigEndianHeapChannelBuffer) URL(java.net.URL) Test(org.junit.Test)

Example 10 with BigEndianHeapChannelBuffer

use of org.jboss.netty.buffer.BigEndianHeapChannelBuffer in project druid by druid-io.

the class HttpIndexingServiceClientTest method testCompact.

@Test
public void testCompact() throws Exception {
    DataSegment segment = new DataSegment("test", Intervals.of("2015-04-12/2015-04-13"), "1", ImmutableMap.of("bucket", "bucket", "path", "test/2015-04-12T00:00:00.000Z_2015-04-13T00:00:00.000Z/1/0/index.zip"), null, null, NoneShardSpec.instance(), 0, 1);
    Capture captureTask = EasyMock.newCapture();
    HttpResponse response = EasyMock.createMock(HttpResponse.class);
    EasyMock.expect(response.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
    EasyMock.expect(response.getContent()).andReturn(new BigEndianHeapChannelBuffer(0));
    EasyMock.replay(response);
    StringFullResponseHolder responseHolder = new StringFullResponseHolder(response, StandardCharsets.UTF_8).addChunk(jsonMapper.writeValueAsString(ImmutableMap.of("task", "aaa")));
    EasyMock.expect(druidLeaderClient.makeRequest(HttpMethod.POST, "/druid/indexer/v1/task")).andReturn(new Request(HttpMethod.POST, new URL("http://localhost:8090/druid/indexer/v1/task"))).anyTimes();
    EasyMock.expect(druidLeaderClient.go(EasyMock.anyObject(Request.class))).andReturn(responseHolder).anyTimes();
    EasyMock.expect(mockMapper.writeValueAsBytes(EasyMock.capture(captureTask))).andReturn(new byte[] { 1, 2, 3 }).anyTimes();
    EasyMock.expect(mockMapper.readValue(EasyMock.anyString(), EasyMock.eq(JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT))).andReturn(ImmutableMap.of()).anyTimes();
    EasyMock.replay(druidLeaderClient, mockMapper);
    HttpIndexingServiceClient httpIndexingServiceClient = new HttpIndexingServiceClient(mockMapper, druidLeaderClient);
    try {
        httpIndexingServiceClient.compactSegments("test-compact", ImmutableList.of(segment), 50, null, null, null, null, null, null, null);
    } catch (Exception e) {
        // Ignore IllegalStateException as taskId is internally generated and returned task id will failed check
        Assert.assertEquals(IllegalStateException.class.getName(), e.getCause().getClass().getName());
    }
    ClientCompactionTaskQuery taskQuery = (ClientCompactionTaskQuery) captureTask.getValue();
    Assert.assertNull(taskQuery.getIoConfig().getInputSpec().getSha256OfSortedSegmentIds());
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) Request(org.apache.druid.java.util.http.client.Request) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BigEndianHeapChannelBuffer(org.jboss.netty.buffer.BigEndianHeapChannelBuffer) DataSegment(org.apache.druid.timeline.DataSegment) Capture(org.easymock.Capture) URL(java.net.URL) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Aggregations

BigEndianHeapChannelBuffer (org.jboss.netty.buffer.BigEndianHeapChannelBuffer)19 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)19 Test (org.junit.Test)18 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)10 URL (java.net.URL)8 Request (org.apache.druid.java.util.http.client.Request)8 StringFullResponseHolder (org.apache.druid.java.util.http.client.response.StringFullResponseHolder)8 InputStream (java.io.InputStream)6 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Either (org.apache.druid.java.util.common.Either)3 RetryPolicyFactory (org.apache.druid.indexing.common.RetryPolicyFactory)2 NoopTask (org.apache.druid.indexing.common.task.NoopTask)2 Task (org.apache.druid.indexing.common.task.Task)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 RetryPolicyConfig (org.apache.druid.indexing.common.RetryPolicyConfig)1 TaskLock (org.apache.druid.indexing.common.TaskLock)1 TimeChunkLock (org.apache.druid.indexing.common.TimeChunkLock)1 DataSegment (org.apache.druid.timeline.DataSegment)1