use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class ReplicationTest method getPutMessage.
/**
* Constructs an entire message with header, blob properties, user metadata and blob content.
* @param id id for which the message has to be constructed.
* @param accountId accountId of the blob
* @param containerId containerId of the blob
* @param enableEncryption {@code true} if encryption needs to be enabled. {@code false} otherwise
* @return a {@link Pair} of {@link ByteBuffer} and {@link MessageInfo} representing the entire message and the
* associated {@link MessageInfo}
* @throws MessageFormatException
* @throws IOException
*/
private Pair<ByteBuffer, MessageInfo> getPutMessage(StoreKey id, short accountId, short containerId, boolean enableEncryption) throws MessageFormatException, IOException {
int blobSize = TestUtils.RANDOM.nextInt(500) + 501;
int userMetadataSize = TestUtils.RANDOM.nextInt(blobSize / 2);
int encryptionKeySize = TestUtils.RANDOM.nextInt(blobSize / 4);
byte[] blob = new byte[blobSize];
byte[] usermetadata = new byte[userMetadataSize];
byte[] encryptionKey = enableEncryption ? new byte[encryptionKeySize] : null;
TestUtils.RANDOM.nextBytes(blob);
TestUtils.RANDOM.nextBytes(usermetadata);
BlobProperties blobProperties = new BlobProperties(blobSize, "test", accountId, containerId, encryptionKey != null);
MessageFormatInputStream stream = new PutMessageFormatInputStream(id, encryptionKey == null ? null : ByteBuffer.wrap(encryptionKey), blobProperties, ByteBuffer.wrap(usermetadata), new ByteBufferInputStream(ByteBuffer.wrap(blob)), blobSize);
byte[] message = Utils.readBytesFromStream(stream, (int) stream.getSize());
return new Pair<>(ByteBuffer.wrap(message), new MessageInfo(id, message.length, Utils.Infinite_Time, accountId, containerId, blobProperties.getCreationTimeInMs()));
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class DeleteManager method extractDeleteResponseAndNotifyResponseHandler.
/**
* Extract the {@link DeleteResponse} from the given {@link ResponseInfo}
* @param responseInfo the {@link ResponseInfo} from which the {@link DeleteResponse} is to be extracted.
* @return the extracted {@link DeleteResponse} if there is one; null otherwise.
*/
private DeleteResponse extractDeleteResponseAndNotifyResponseHandler(ResponseInfo responseInfo) {
DeleteResponse deleteResponse = null;
ReplicaId replicaId = ((RouterRequestInfo) responseInfo.getRequestInfo()).getReplicaId();
NetworkClientErrorCode networkClientErrorCode = responseInfo.getError();
if (networkClientErrorCode == null) {
try {
deleteResponse = DeleteResponse.readFrom(new DataInputStream(new ByteBufferInputStream(responseInfo.getResponse())));
responseHandler.onEvent(replicaId, deleteResponse.getError());
} catch (Exception e) {
// Ignore. There is no value in notifying the response handler.
logger.error("Response deserialization received unexpected error", e);
routerMetrics.responseDeserializationErrorCount.inc();
}
} else {
responseHandler.onEvent(replicaId, networkClientErrorCode);
}
return deleteResponse;
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class GetBlobResultInternal method extractGetResponseAndNotifyResponseHandler.
/**
* Extract the {@link GetResponse} from the given {@link ResponseInfo}
* @param responseInfo the {@link ResponseInfo} from which the {@link GetResponse} is to be extracted.
* @return the extracted {@link GetResponse} if there is one; null otherwise.
*/
private GetResponse extractGetResponseAndNotifyResponseHandler(ResponseInfo responseInfo) {
GetResponse getResponse = null;
ReplicaId replicaId = ((RouterRequestInfo) responseInfo.getRequestInfo()).getReplicaId();
NetworkClientErrorCode networkClientErrorCode = responseInfo.getError();
if (networkClientErrorCode == null) {
try {
getResponse = GetResponse.readFrom(new DataInputStream(new ByteBufferInputStream(responseInfo.getResponse())), clusterMap);
ServerErrorCode serverError = getResponse.getError();
if (serverError == ServerErrorCode.No_Error) {
serverError = getResponse.getPartitionResponseInfoList().get(0).getErrorCode();
}
responseHandler.onEvent(replicaId, serverError);
} catch (Exception e) {
// Ignore. There is no value in notifying the response handler.
logger.error("Response deserialization received unexpected error", e);
routerMetrics.responseDeserializationErrorCount.inc();
}
} else {
responseHandler.onEvent(replicaId, networkClientErrorCode);
}
return getResponse;
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class PutManager method extractPutResponseAndNotifyResponseHandler.
/**
* Extract the {@link PutResponse} from the given {@link ResponseInfo}
* @param responseInfo the {@link ResponseInfo} from which the {@link PutResponse} is to be extracted.
* @return the extracted {@link PutResponse} if there is one; null otherwise.
*/
private PutResponse extractPutResponseAndNotifyResponseHandler(ResponseInfo responseInfo) {
PutResponse putResponse = null;
ReplicaId replicaId = ((RouterRequestInfo) responseInfo.getRequestInfo()).getReplicaId();
NetworkClientErrorCode networkClientErrorCode = responseInfo.getError();
if (networkClientErrorCode == null) {
try {
putResponse = PutResponse.readFrom(new DataInputStream(new ByteBufferInputStream(responseInfo.getResponse())));
responseHandler.onEvent(replicaId, putResponse.getError());
} catch (Exception e) {
// Ignore. There is no value in notifying the response handler.
logger.error("Response deserialization received unexpected error", e);
routerMetrics.responseDeserializationErrorCount.inc();
}
} else {
responseHandler.onEvent(replicaId, networkClientErrorCode);
}
return putResponse;
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class MockRouterCallback method assertOperationFailure.
/**
* Assert that operation fails with the expected error code
* @param errorCode expected error code on failure
* @throws RouterException
*/
private void assertOperationFailure(RouterErrorCode errorCode) throws RouterException, IOException, InterruptedException {
NonBlockingRouter.currentOperationsCount.incrementAndGet();
GetBlobInfoOperation op = new GetBlobInfoOperation(routerConfig, routerMetrics, mockClusterMap, responseHandler, blobId, options, null, routerCallback, kms, cryptoService, cryptoJobHandler, time);
ArrayList<RequestInfo> requestListToFill = new ArrayList<>();
requestRegistrationCallback.requestListToFill = requestListToFill;
op.poll(requestRegistrationCallback);
Assert.assertEquals("There should only be as many requests at this point as requestParallelism", requestParallelism, correlationIdToGetOperation.size());
CountDownLatch onPollLatch = new CountDownLatch(1);
routerCallback.setOnPollLatch(onPollLatch);
List<ResponseInfo> responses = sendAndWaitForResponses(requestListToFill);
for (ResponseInfo responseInfo : responses) {
GetResponse getResponse = responseInfo.getError() == null ? GetResponse.readFrom(new DataInputStream(new ByteBufferInputStream(responseInfo.getResponse())), mockClusterMap) : null;
op.handleResponse(responseInfo, getResponse);
if (op.isOperationComplete()) {
break;
}
}
if (!op.isOperationComplete()) {
Assert.assertTrue("Latch should have been zeroed ", onPollLatch.await(500, TimeUnit.MILLISECONDS));
op.poll(requestRegistrationCallback);
}
Assert.assertTrue("Operation should be complete at this time", op.isOperationComplete());
RouterException routerException = (RouterException) op.getOperationException();
Assert.assertEquals(errorCode, routerException.getErrorCode());
}
Aggregations