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