Search in sources :

Example 1 with ByteBufferInputStream

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()));
}
Also used : BlobProperties(com.github.ambry.messageformat.BlobProperties) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) PutMessageFormatInputStream(com.github.ambry.messageformat.PutMessageFormatInputStream) DeleteMessageFormatInputStream(com.github.ambry.messageformat.DeleteMessageFormatInputStream) MessageFormatInputStream(com.github.ambry.messageformat.MessageFormatInputStream) PutMessageFormatInputStream(com.github.ambry.messageformat.PutMessageFormatInputStream) Pair(com.github.ambry.utils.Pair) MessageInfo(com.github.ambry.store.MessageInfo)

Example 2 with ByteBufferInputStream

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;
}
Also used : DeleteResponse(com.github.ambry.protocol.DeleteResponse) NetworkClientErrorCode(com.github.ambry.network.NetworkClientErrorCode) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) DataInputStream(java.io.DataInputStream) ReplicaId(com.github.ambry.clustermap.ReplicaId)

Example 3 with ByteBufferInputStream

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;
}
Also used : NetworkClientErrorCode(com.github.ambry.network.NetworkClientErrorCode) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) DataInputStream(java.io.DataInputStream) GetResponse(com.github.ambry.protocol.GetResponse) ReplicaId(com.github.ambry.clustermap.ReplicaId) ServerErrorCode(com.github.ambry.commons.ServerErrorCode)

Example 4 with ByteBufferInputStream

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;
}
Also used : NetworkClientErrorCode(com.github.ambry.network.NetworkClientErrorCode) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) PutResponse(com.github.ambry.protocol.PutResponse) DataInputStream(java.io.DataInputStream) ReplicaId(com.github.ambry.clustermap.ReplicaId)

Example 5 with ByteBufferInputStream

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());
}
Also used : ResponseInfo(com.github.ambry.network.ResponseInfo) ArrayList(java.util.ArrayList) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) RequestInfo(com.github.ambry.network.RequestInfo) CountDownLatch(java.util.concurrent.CountDownLatch) DataInputStream(java.io.DataInputStream) GetResponse(com.github.ambry.protocol.GetResponse)

Aggregations

ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)79 ByteBuffer (java.nio.ByteBuffer)48 DataInputStream (java.io.DataInputStream)34 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)19 InputStream (java.io.InputStream)16 MessageInfo (com.github.ambry.store.MessageInfo)15 StoreKey (com.github.ambry.store.StoreKey)14 IOException (java.io.IOException)12 MetricRegistry (com.codahale.metrics.MetricRegistry)10 Random (java.util.Random)10 GetResponse (com.github.ambry.protocol.GetResponse)8 MockId (com.github.ambry.store.MockId)8 ByteBufferOutputStream (com.github.ambry.utils.ByteBufferOutputStream)8 Crc32 (com.github.ambry.utils.Crc32)6 BlobId (com.github.ambry.commons.BlobId)5 BlobProperties (com.github.ambry.messageformat.BlobProperties)5 CrcInputStream (com.github.ambry.utils.CrcInputStream)5 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)4 RequestInfo (com.github.ambry.network.RequestInfo)4