Search in sources :

Example 26 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class GetClusterMapSnapshotHandlerTest method sendRequestGetResponse.

/**
 * Sends the given {@link RestRequest} to the {@link GetClusterMapSnapshotHandler} and waits for the response and
 * returns it.
 * @param restRequest the {@link RestRequest} to send.
 * @param restResponseChannel the {@link RestResponseChannel} where headers will be set.
 * @return the response body as a {@link ReadableStreamChannel}.
 * @throws Exception
 */
private ReadableStreamChannel sendRequestGetResponse(RestRequest restRequest, RestResponseChannel restResponseChannel) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
    final AtomicReference<ReadableStreamChannel> channelRef = new AtomicReference<>();
    handler.handle(restRequest, restResponseChannel, (result, exception) -> {
        channelRef.set(result);
        exceptionRef.set(exception);
        latch.countDown();
    });
    assertTrue("Latch did not count down in time", latch.await(1, TimeUnit.SECONDS));
    if (exceptionRef.get() != null) {
        throw exceptionRef.get();
    }
    return channelRef.get();
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Example 27 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class BadRestRequest method handleResponseExceptionTest.

/**
 * Tests the reaction of {@link AsyncRequestResponseHandler#handleResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)} to some misbehaving components.
 * @throws Exception
 */
@Test
public void handleResponseExceptionTest() throws Exception {
    ByteBufferRSC response = new ByteBufferRSC(ByteBuffer.allocate(0));
    // RestRequest null.
    try {
        asyncRequestResponseHandler.handleResponse(null, new MockRestResponseChannel(), response, null);
        fail("Test should have thrown exception, but did not");
    } catch (IllegalArgumentException e) {
    // expected. Nothing to do.
    }
    // RestResponseChannel null.
    MockRestRequest restRequest = createRestRequest(RestMethod.GET, "/", new JSONObject(), null);
    restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    try {
        asyncRequestResponseHandler.handleResponse(restRequest, null, response, null);
        fail("Test should have thrown exception, but did not");
    } catch (IllegalArgumentException e) {
    // expected. Nothing to do.
    }
    // Writing response throws exception.
    MockRestRequest goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    String exceptionMsg = "@@randomMsg@@";
    ReadableStreamChannel badResponse = new IncompleteReadReadableStreamChannel(null, new Exception(exceptionMsg));
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Exception message does not match", exceptionMsg, restResponseChannel.getException().getMessage());
    // Writing response is incomplete
    goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    badResponse = new IncompleteReadReadableStreamChannel(0L, null);
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Unexpected exception", IllegalStateException.class, restResponseChannel.getException().getClass());
    // No bytes read and no exception
    goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    badResponse = new IncompleteReadReadableStreamChannel(null, null);
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Unexpected exception", IllegalStateException.class, restResponseChannel.getException().getClass());
    // RestRequest is bad.
    BadRestRequest badRestRequest = new BadRestRequest();
    badRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    ByteBufferRSC goodResponse = new ByteBufferRSC(ByteBuffer.allocate(0));
    EventMonitor<ByteBufferRSC.Event> responseCloseMonitor = new EventMonitor<ByteBufferRSC.Event>(ByteBufferRSC.Event.Close);
    goodResponse.addListener(responseCloseMonitor);
    awaitResponse(asyncRequestResponseHandler, badRestRequest, restResponseChannel, goodResponse, null);
    assertTrue("Response is not closed", responseCloseMonitor.awaitEvent(1, TimeUnit.SECONDS));
    // AsyncRequestResponseHandler should still be alive and serving requests
    assertTrue("AsyncRequestResponseHandler is dead", asyncRequestResponseHandler.isRunning());
}
Also used : URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBufferRSC(com.github.ambry.router.ByteBufferRSC) JSONObject(org.json.JSONObject) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) Test(org.junit.Test)

Example 28 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class ServerTestUtil method checkBlobId.

private static void checkBlobId(Router router, BlobId blobId, byte[] data) throws Exception {
    GetBlobResult result = router.getBlob(blobId.getID(), new GetBlobOptionsBuilder().build()).get(20, TimeUnit.SECONDS);
    ReadableStreamChannel blob = result.getBlobDataChannel();
    assertEquals("Size does not match that of data", data.length, result.getBlobInfo().getBlobProperties().getBlobSize());
    RetainingAsyncWritableChannel channel = new RetainingAsyncWritableChannel();
    blob.readInto(channel, null).get(1, TimeUnit.SECONDS);
    try (InputStream is = channel.consumeContentAsInputStream()) {
        assertArrayEquals(data, Utils.readBytesFromStream(is, (int) channel.getBytesWritten()));
    }
}
Also used : GetBlobOptionsBuilder(com.github.ambry.router.GetBlobOptionsBuilder) GetBlobResult(com.github.ambry.router.GetBlobResult) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) NettyByteBufDataInputStream(com.github.ambry.utils.NettyByteBufDataInputStream) PutMessageFormatInputStream(com.github.ambry.messageformat.PutMessageFormatInputStream) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) DataInputStream(java.io.DataInputStream) CrcInputStream(com.github.ambry.utils.CrcInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Example 29 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class RouterServerTestFramework method startPutBlob.

/**
 * Submit a putBlob operation.
 * @param opChain the {@link OperationChain} object that this operation is a part of.
 */
private void startPutBlob(OperationChain opChain) {
    ReadableStreamChannel putChannel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(opChain.data));
    Callback<String> callback = new TestCallback<String>(opChain, false) {

        @Override
        void action(String result) {
            opChain.blobId = result;
        }
    };
    Future<String> future = router.putBlob(opChain.properties, opChain.userMetadata, putChannel, new PutBlobOptionsBuilder().build(), callback, quotaChargeCallback);
    TestFuture<String> testFuture = new TestFuture<String>(future, genLabel("putBlob", false), opChain) {

        @Override
        void check() throws Exception {
            checkBlobId(get(), opChain.properties, getOperationName());
        }
    };
    opChain.testFutures.add(testFuture);
}
Also used : ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) PutBlobOptionsBuilder(com.github.ambry.router.PutBlobOptionsBuilder)

Aggregations

ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)29 Test (org.junit.Test)14 MockRestRequest (com.github.ambry.rest.MockRestRequest)13 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)11 RestRequest (com.github.ambry.rest.RestRequest)10 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)9 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)8 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)7 InputStream (java.io.InputStream)7 RestServiceException (com.github.ambry.rest.RestServiceException)6 JSONObject (org.json.JSONObject)6 FutureResult (com.github.ambry.router.FutureResult)5 RetainingAsyncWritableChannel (com.github.ambry.commons.RetainingAsyncWritableChannel)4 RequestPath (com.github.ambry.rest.RequestPath)4 RestMethod (com.github.ambry.rest.RestMethod)4 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)4 RestUtils (com.github.ambry.rest.RestUtils)4 RouterException (com.github.ambry.router.RouterException)4 ThrowingConsumer (com.github.ambry.utils.ThrowingConsumer)4