Search in sources :

Example 1 with Callback

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

the class NettyRequest method writeContent.

/**
 * Writes the data in the provided {@code httpContent} to the given {@code writeChannel}.
 * @param writeChannel the {@link AsyncWritableChannel} to write the data of {@code httpContent} to.
 * @param callbackWrapper the {@link ReadIntoCallbackWrapper} for the read operation.
 * @param httpContent the piece of {@link HttpContent} that needs to be written to the {@code writeChannel}.
 */
protected void writeContent(AsyncWritableChannel writeChannel, ReadIntoCallbackWrapper callbackWrapper, HttpContent httpContent) {
    boolean retained = false;
    ByteBuffer[] contentBuffers;
    Callback<Long>[] writeCallbacks;
    // LastHttpContent in the end marker in netty http world.
    boolean isLast = httpContent instanceof LastHttpContent;
    if (isLast) {
        setAutoRead(true);
    }
    if (httpContent.content().nioBufferCount() > 0) {
        // not a copy.
        httpContent = ReferenceCountUtil.retain(httpContent);
        retained = true;
        contentBuffers = httpContent.content().nioBuffers();
        writeCallbacks = new ContentWriteCallback[contentBuffers.length];
        int i = 0;
        for (; i < contentBuffers.length - 1; i++) {
            writeCallbacks[i] = new ContentWriteCallback(null, false, callbackWrapper);
        }
        writeCallbacks[i] = new ContentWriteCallback(httpContent, isLast, callbackWrapper);
    } else {
        // this will not happen (looking at current implementations of ByteBuf in Netty), but if it does, we cannot avoid
        // a copy (or we can introduce a read(GatheringByteChannel) method in ReadableStreamChannel if required).
        nettyMetrics.contentCopyCount.inc();
        logger.warn("HttpContent had to be copied because ByteBuf did not have a backing ByteBuffer");
        ByteBuffer contentBuffer = ByteBuffer.allocate(httpContent.content().readableBytes());
        httpContent.content().readBytes(contentBuffer);
        contentBuffer.rewind();
        // no need to retain httpContent since we have a copy.
        ContentWriteCallback writeCallback = new ContentWriteCallback(null, isLast, callbackWrapper);
        contentBuffers = new ByteBuffer[] { contentBuffer };
        writeCallbacks = new ContentWriteCallback[] { writeCallback };
    }
    boolean asyncWritesCalled = false;
    try {
        for (int i = 0; i < contentBuffers.length; i++) {
            if (digest != null) {
                long startTime = System.currentTimeMillis();
                int savedPosition = contentBuffers[i].position();
                digest.update(contentBuffers[i]);
                contentBuffers[i].position(savedPosition);
                digestCalculationTimeInMs += (System.currentTimeMillis() - startTime);
            }
            writeChannel.write(contentBuffers[i], writeCallbacks[i]);
        }
        asyncWritesCalled = true;
    } finally {
        if (retained && !asyncWritesCalled) {
            ReferenceCountUtil.release(httpContent);
        }
    }
    allContentReceived = isLast;
}
Also used : Callback(com.github.ambry.router.Callback) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Callback

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

the class AmbrySecurityServiceTest method testExceptionCasesProcessResponse.

/**
 * Tests exception cases for
 * {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)}
 * @param restMethod the {@link RestMethod} of the request to be made
 * @param restResponseChannel the {@link RestResponseChannel} to write responses over.
 * @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
 * @param expectedErrorCode the {@link RestServiceErrorCode} expected in the exception returned.
 * @throws Exception
 */
private void testExceptionCasesProcessResponse(RestMethod restMethod, RestResponseChannel restResponseChannel, BlobInfo blobInfo, RestServiceErrorCode expectedErrorCode) throws Exception {
    TestUtils.ThrowingConsumer<ExecutionException> errorAction = e -> {
        Assert.assertTrue("Exception should have been an instance of RestServiceException", e.getCause() instanceof RestServiceException);
        RestServiceException re = (RestServiceException) e.getCause();
        Assert.assertEquals("Unexpected RestServerErrorCode (Future)", expectedErrorCode, re.getErrorCode());
    };
    RestRequest restRequest = createRestRequest(restMethod, "/", null);
    TestUtils.assertException(ExecutionException.class, () -> securityService.processResponse(restRequest, restResponseChannel, blobInfo).get(), errorAction);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) BlobProperties(com.github.ambry.messageformat.BlobProperties) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ResponseStatus(com.github.ambry.rest.ResponseStatus) SimpleDateFormat(java.text.SimpleDateFormat) RestTestUtils(com.github.ambry.rest.RestTestUtils) ByteBuffer(java.nio.ByteBuffer) Future(java.util.concurrent.Future) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) Locale(java.util.Locale) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Container(com.github.ambry.account.Container) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) Pair(com.github.ambry.utils.Pair) RestMethod(com.github.ambry.rest.RestMethod) TimeZone(java.util.TimeZone) VerifiableProperties(com.github.ambry.config.VerifiableProperties) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) ByteRange(com.github.ambry.router.ByteRange) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) Test(org.junit.Test) BlobInfo(com.github.ambry.messageformat.BlobInfo) ExecutionException(java.util.concurrent.ExecutionException) InMemAccountServiceFactory(com.github.ambry.account.InMemAccountServiceFactory) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) Callback(com.github.ambry.router.Callback) RestServiceException(com.github.ambry.rest.RestServiceException) RestTestUtils(com.github.ambry.rest.RestTestUtils) TestUtils(com.github.ambry.utils.TestUtils) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Callback (com.github.ambry.router.Callback)2 ByteBuffer (java.nio.ByteBuffer)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Account (com.github.ambry.account.Account)1 Container (com.github.ambry.account.Container)1 InMemAccountService (com.github.ambry.account.InMemAccountService)1 InMemAccountServiceFactory (com.github.ambry.account.InMemAccountServiceFactory)1 FrontendConfig (com.github.ambry.config.FrontendConfig)1 VerifiableProperties (com.github.ambry.config.VerifiableProperties)1 BlobInfo (com.github.ambry.messageformat.BlobInfo)1 BlobProperties (com.github.ambry.messageformat.BlobProperties)1 MockRestRequest (com.github.ambry.rest.MockRestRequest)1 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)1 ResponseStatus (com.github.ambry.rest.ResponseStatus)1 RestMethod (com.github.ambry.rest.RestMethod)1 RestRequest (com.github.ambry.rest.RestRequest)1 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)1 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)1 RestServiceException (com.github.ambry.rest.RestServiceException)1 RestTestUtils (com.github.ambry.rest.RestTestUtils)1