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;
}
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);
}
Aggregations