Search in sources :

Example 6 with PutResponse

use of com.github.ambry.protocol.PutResponse in project ambry by linkedin.

the class DirectSender method run.

@Override
public void run() {
    try {
        for (int i = 0; i < blobIds.size(); i++) {
            PutRequest putRequest = new PutRequest(1, "client1", blobIds.get(i), blobProperties, ByteBuffer.wrap(usermetadata), ByteBuffer.wrap(data), blobProperties.getBlobSize(), BlobType.DataBlob, encryptionKey != null ? ByteBuffer.wrap(encryptionKey) : null);
            channel.send(putRequest);
            InputStream putResponseStream = channel.receive().getInputStream();
            PutResponse response = PutResponse.readFrom(new DataInputStream(putResponseStream));
            Assert.assertEquals(response.getError(), ServerErrorCode.No_Error);
        }
    } catch (Exception e) {
        Assert.assertTrue(false);
    } finally {
        endLatch.countDown();
    }
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) PutRequest(com.github.ambry.protocol.PutRequest) PutResponse(com.github.ambry.protocol.PutResponse) DataInputStream(java.io.DataInputStream)

Example 7 with PutResponse

use of com.github.ambry.protocol.PutResponse in project ambry by linkedin.

the class ServerHardDeleteTest method putBlob.

/**
 * Uploads a single blob to ambry server node
 * @param blobId the {@link BlobId} that needs to be put
 * @param properties the {@link BlobProperties} of the blob being uploaded
 * @param usermetadata the user metadata of the blob being uploaded
 * @param data the blob content of the blob being uploaded
 * @param channel the {@link BlockingChannel} to use to send and receive data
 * @throws IOException
 */
void putBlob(BlobId blobId, BlobProperties properties, byte[] encryptionKey, byte[] usermetadata, byte[] data, BlockingChannel channel) throws IOException {
    PutRequest putRequest0 = new PutRequest(1, "client1", blobId, properties, ByteBuffer.wrap(usermetadata), ByteBuffer.wrap(data), properties.getBlobSize(), BlobType.DataBlob, encryptionKey == null ? null : ByteBuffer.wrap(encryptionKey));
    channel.send(putRequest0);
    InputStream putResponseStream = channel.receive().getInputStream();
    PutResponse response0 = PutResponse.readFrom(new DataInputStream(putResponseStream));
    Assert.assertEquals(response0.getError(), ServerErrorCode.No_Error);
}
Also used : DataInputStream(java.io.DataInputStream) CrcInputStream(com.github.ambry.utils.CrcInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PutRequest(com.github.ambry.protocol.PutRequest) PutResponse(com.github.ambry.protocol.PutResponse) DataInputStream(java.io.DataInputStream)

Example 8 with PutResponse

use of com.github.ambry.protocol.PutResponse in project ambry by linkedin.

the class ServerTestUtil method testLatePutRequest.

/**
 * Tests the case where a late PutRequest is sent to each server when it already has a record for the given BlobId.
 * The expected error from each server should be the given error.
 * @param blobId the {@link BlobId} of the blob to be put.
 * @param properties the {@link BlobProperties} of the blob to be put.
 * @param usermetadata the usermetadata of the blob to be put.
 * @param data the blob data of the blob to be put.
 * @param encryptionKey the encryption key of the blob. Could be null for non-encrypted blobs
 * @param channelToDatanode1 the {@link BlockingChannel} to the Datanode1.
 * @param channelToDatanode2 the {@link BlockingChannel} to the Datanode2.
 * @param channelToDatanode3 the {@link BlockingChannel} to the Datanode3.
 * @param expectedErrorCode the {@link ServerErrorCode} that is expected from every Datanode.
 * @throws IOException
 */
private static void testLatePutRequest(BlobId blobId, BlobProperties properties, byte[] usermetadata, byte[] data, byte[] encryptionKey, BlockingChannel channelToDatanode1, BlockingChannel channelToDatanode2, BlockingChannel channelToDatanode3, ServerErrorCode expectedErrorCode) throws IOException {
    // Send put requests for an existing blobId for the exact blob to simulate a request arriving late.
    PutRequest latePutRequest1 = new PutRequest(1, "client1", blobId, properties, ByteBuffer.wrap(usermetadata), ByteBuffer.wrap(data), properties.getBlobSize(), BlobType.DataBlob, encryptionKey != null ? ByteBuffer.wrap(encryptionKey) : null);
    PutRequest latePutRequest2 = new PutRequest(1, "client2", blobId, properties, ByteBuffer.wrap(usermetadata), ByteBuffer.wrap(data), properties.getBlobSize(), BlobType.DataBlob, encryptionKey != null ? ByteBuffer.wrap(encryptionKey) : null);
    PutRequest latePutRequest3 = new PutRequest(1, "client3", blobId, properties, ByteBuffer.wrap(usermetadata), ByteBuffer.wrap(data), properties.getBlobSize(), BlobType.DataBlob, encryptionKey != null ? ByteBuffer.wrap(encryptionKey) : null);
    channelToDatanode1.send(latePutRequest1);
    InputStream putResponseStream = channelToDatanode1.receive().getInputStream();
    PutResponse response = PutResponse.readFrom(new DataInputStream(putResponseStream));
    assertEquals(expectedErrorCode, response.getError());
    channelToDatanode2.send(latePutRequest2);
    putResponseStream = channelToDatanode2.receive().getInputStream();
    response = PutResponse.readFrom(new DataInputStream(putResponseStream));
    assertEquals(expectedErrorCode, response.getError());
    channelToDatanode3.send(latePutRequest3);
    putResponseStream = channelToDatanode3.receive().getInputStream();
    response = PutResponse.readFrom(new DataInputStream(putResponseStream));
    assertEquals(expectedErrorCode, response.getError());
}
Also used : DataInputStream(java.io.DataInputStream) CrcInputStream(com.github.ambry.utils.CrcInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PutRequest(com.github.ambry.protocol.PutRequest) PutResponse(com.github.ambry.protocol.PutResponse) DataInputStream(java.io.DataInputStream)

Example 9 with PutResponse

use of com.github.ambry.protocol.PutResponse in project ambry by linkedin.

the class AmbryRequests method handlePutRequest.

public void handlePutRequest(Request request) throws IOException, InterruptedException {
    PutRequest.ReceivedPutRequest receivedRequest = PutRequest.readFrom(new DataInputStream(request.getInputStream()), clusterMap);
    long requestQueueTime = SystemTime.getInstance().milliseconds() - request.getStartTimeInMs();
    long totalTimeSpent = requestQueueTime;
    metrics.putBlobRequestQueueTimeInMs.update(requestQueueTime);
    metrics.putBlobRequestRate.mark();
    long startTime = SystemTime.getInstance().milliseconds();
    PutResponse response = null;
    try {
        ServerErrorCode error = validateRequest(receivedRequest.getBlobId().getPartition(), RequestOrResponseType.PutRequest);
        if (error != ServerErrorCode.No_Error) {
            logger.error("Validating put request failed with error {} for request {}", error, receivedRequest);
            response = new PutResponse(receivedRequest.getCorrelationId(), receivedRequest.getClientId(), error);
        } else {
            MessageFormatInputStream stream = new PutMessageFormatInputStream(receivedRequest.getBlobId(), receivedRequest.getBlobEncryptionKey(), receivedRequest.getBlobProperties(), receivedRequest.getUsermetadata(), receivedRequest.getBlobStream(), receivedRequest.getBlobSize(), receivedRequest.getBlobType());
            MessageInfo info = new MessageInfo(receivedRequest.getBlobId(), stream.getSize(), false, Utils.addSecondsToEpochTime(receivedRequest.getBlobProperties().getCreationTimeInMs(), receivedRequest.getBlobProperties().getTimeToLiveInSeconds()), receivedRequest.getCrc(), receivedRequest.getBlobProperties().getAccountId(), receivedRequest.getBlobProperties().getContainerId(), receivedRequest.getBlobProperties().getCreationTimeInMs());
            ArrayList<MessageInfo> infoList = new ArrayList<MessageInfo>();
            infoList.add(info);
            MessageFormatWriteSet writeset = new MessageFormatWriteSet(stream, infoList, false);
            Store storeToPut = storageManager.getStore(receivedRequest.getBlobId().getPartition());
            storeToPut.put(writeset);
            response = new PutResponse(receivedRequest.getCorrelationId(), receivedRequest.getClientId(), ServerErrorCode.No_Error);
            metrics.blobSizeInBytes.update(receivedRequest.getBlobSize());
            metrics.blobUserMetadataSizeInBytes.update(receivedRequest.getUsermetadata().limit());
            if (notification != null) {
                notification.onBlobReplicaCreated(currentNode.getHostname(), currentNode.getPort(), receivedRequest.getBlobId().getID(), BlobReplicaSourceType.PRIMARY);
            }
        }
    } catch (StoreException e) {
        logger.error("Store exception on a put with error code " + e.getErrorCode() + " for request " + receivedRequest, e);
        if (e.getErrorCode() == StoreErrorCodes.Already_Exist) {
            metrics.idAlreadyExistError.inc();
        } else if (e.getErrorCode() == StoreErrorCodes.IOError) {
            metrics.storeIOError.inc();
        } else {
            metrics.unExpectedStorePutError.inc();
        }
        response = new PutResponse(receivedRequest.getCorrelationId(), receivedRequest.getClientId(), ErrorMapping.getStoreErrorMapping(e.getErrorCode()));
    } catch (Exception e) {
        logger.error("Unknown exception on a put for request " + receivedRequest, e);
        response = new PutResponse(receivedRequest.getCorrelationId(), receivedRequest.getClientId(), ServerErrorCode.Unknown_Error);
    } finally {
        long processingTime = SystemTime.getInstance().milliseconds() - startTime;
        totalTimeSpent += processingTime;
        publicAccessLogger.info("{} {} processingTime {}", receivedRequest, response, processingTime);
        metrics.putBlobProcessingTimeInMs.update(processingTime);
        metrics.updatePutBlobProcessingTimeBySize(receivedRequest.getBlobSize(), processingTime);
    }
    sendPutResponse(requestResponseChannel, response, request, metrics.putBlobResponseQueueTimeInMs, metrics.putBlobSendTimeInMs, metrics.putBlobTotalTimeInMs, totalTimeSpent, receivedRequest.getBlobSize(), metrics);
}
Also used : ArrayList(java.util.ArrayList) PutRequest(com.github.ambry.protocol.PutRequest) PutMessageFormatInputStream(com.github.ambry.messageformat.PutMessageFormatInputStream) Store(com.github.ambry.store.Store) DeleteMessageFormatInputStream(com.github.ambry.messageformat.DeleteMessageFormatInputStream) MessageFormatInputStream(com.github.ambry.messageformat.MessageFormatInputStream) PutMessageFormatInputStream(com.github.ambry.messageformat.PutMessageFormatInputStream) DataInputStream(java.io.DataInputStream) PutResponse(com.github.ambry.protocol.PutResponse) ServerErrorCode(com.github.ambry.commons.ServerErrorCode) StoreException(com.github.ambry.store.StoreException) IOException(java.io.IOException) MessageFormatException(com.github.ambry.messageformat.MessageFormatException) MessageInfo(com.github.ambry.store.MessageInfo) StoreException(com.github.ambry.store.StoreException) MessageFormatWriteSet(com.github.ambry.messageformat.MessageFormatWriteSet)

Example 10 with PutResponse

use of com.github.ambry.protocol.PutResponse in project ambry by linkedin.

the class DirectoryUploader method getPutResponseFromStream.

/**
 * Method to send PutRequest and receive PutResponse to and from a blocking channel. If it fails, blocking channel is destroyed
 *
 * @param blockingChannel BlockingChannel used to send and receive request and response
 * @param putRequest      PutRequest which has to be sent through the blockingchannel
 * @param connectionPool  ConnectionPool from which connections are checked out
 * @return PutResponse the response from the PutRequest made
 */
private static PutResponse getPutResponseFromStream(ConnectedChannel blockingChannel, PutRequest putRequest, ConnectionPool connectionPool) {
    PutResponse putResponse = null;
    try {
        blockingChannel.send(putRequest);
        InputStream stream = blockingChannel.receive().getInputStream();
        putResponse = PutResponse.readFrom(new DataInputStream(stream));
    } catch (Exception exception) {
        connectionPool.destroyConnection(blockingChannel);
        System.out.println("Unknown Exception Error" + exception);
        return null;
    }
    return putResponse;
}
Also used : DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) InputStream(java.io.InputStream) PutResponse(com.github.ambry.protocol.PutResponse) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

PutResponse (com.github.ambry.protocol.PutResponse)11 PutRequest (com.github.ambry.protocol.PutRequest)9 DataInputStream (java.io.DataInputStream)9 InputStream (java.io.InputStream)6 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)4 CrcInputStream (com.github.ambry.utils.CrcInputStream)4 IOException (java.io.IOException)4 BlobId (com.github.ambry.commons.BlobId)3 BlobProperties (com.github.ambry.messageformat.BlobProperties)3 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)3 ByteBuffer (java.nio.ByteBuffer)3 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)2 MockPartitionId (com.github.ambry.clustermap.MockPartitionId)2 PartitionId (com.github.ambry.clustermap.PartitionId)2 BlobIdFactory (com.github.ambry.commons.BlobIdFactory)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 BlobAll (com.github.ambry.messageformat.BlobAll)2 BlockingChannel (com.github.ambry.network.BlockingChannel)2