Search in sources :

Example 11 with DeleteRequest

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

the class StoredBlob method send.

/**
 * Take in a request in the form of {@link Send} and return a response in the form of a
 * {@link BoundedNettyByteBufReceive}.
 * @param send the request.
 * @return the response.
 * @throws IOException if there was an error in interpreting the request.
 */
public BoundedNettyByteBufReceive send(Send send) throws IOException {
    if (!shouldRespond) {
        return null;
    }
    ServerErrorCode serverError = hardError != null ? hardError : serverErrors.size() > 0 ? serverErrors.poll() : ServerErrorCode.No_Error;
    RequestOrResponseType type = ((RequestOrResponse) send).getRequestType();
    RequestOrResponse response;
    requestCounts.computeIfAbsent(type, k -> new LongAdder()).increment();
    switch(type) {
        case PutRequest:
            response = makePutResponse((PutRequest) send, serverError);
            break;
        case GetRequest:
            response = makeGetResponse((GetRequest) send, serverError);
            break;
        case DeleteRequest:
            response = makeDeleteResponse((DeleteRequest) send, serverError);
            break;
        case TtlUpdateRequest:
            response = makeTtlUpdateResponse((TtlUpdateRequest) send, serverError);
            break;
        case UndeleteRequest:
            response = makeUndeleteResponse((UndeleteRequest) send, serverError);
            break;
        default:
            throw new IOException("Unknown request type received");
    }
    ByteBufferChannel channel = new ByteBufferChannel(ByteBuffer.allocate((int) response.sizeInBytes()));
    response.writeTo(channel);
    response.release();
    ByteBuffer payload = channel.getBuffer();
    payload.flip();
    BoundedNettyByteBufReceive receive = new BoundedNettyByteBufReceive(100 * 1024 * 1024);
    receive.readFrom(Channels.newChannel(new ByteBufferInputStream(payload)));
    return receive;
}
Also used : PartitionRequestInfo(com.github.ambry.protocol.PartitionRequestInfo) GetOption(com.github.ambry.protocol.GetOption) DataInputStream(java.io.DataInputStream) LongAdder(java.util.concurrent.atomic.LongAdder) ByteBufferChannel(com.github.ambry.utils.ByteBufferChannel) BlobProperties(com.github.ambry.messageformat.BlobProperties) ServerErrorCode(com.github.ambry.server.ServerErrorCode) UndeleteResponse(com.github.ambry.protocol.UndeleteResponse) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) GetResponse(com.github.ambry.protocol.GetResponse) ByteBufferSend(com.github.ambry.network.ByteBufferSend) DeleteResponse(com.github.ambry.protocol.DeleteResponse) Map(java.util.Map) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) UndeleteRequest(com.github.ambry.protocol.UndeleteRequest) SystemTime(com.github.ambry.utils.SystemTime) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) TtlUpdateResponse(com.github.ambry.protocol.TtlUpdateResponse) LinkedList(java.util.LinkedList) PutRequest(com.github.ambry.protocol.PutRequest) GetRequest(com.github.ambry.protocol.GetRequest) Container(com.github.ambry.account.Container) MessageMetadata(com.github.ambry.messageformat.MessageMetadata) PartitionResponseInfo(com.github.ambry.protocol.PartitionResponseInfo) DeleteRequest(com.github.ambry.protocol.DeleteRequest) BoundedNettyByteBufReceive(com.github.ambry.network.BoundedNettyByteBufReceive) Channels(java.nio.channels.Channels) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterMap(com.github.ambry.clustermap.ClusterMap) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) MessageFormatException(com.github.ambry.messageformat.MessageFormatException) PutResponse(com.github.ambry.protocol.PutResponse) StoreKey(com.github.ambry.store.StoreKey) List(java.util.List) TtlUpdateRequest(com.github.ambry.protocol.TtlUpdateRequest) MessageInfo(com.github.ambry.store.MessageInfo) Crc32(com.github.ambry.utils.Crc32) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) Account(com.github.ambry.account.Account) Send(com.github.ambry.network.Send) MessageFormatRecord(com.github.ambry.messageformat.MessageFormatRecord) BlobType(com.github.ambry.messageformat.BlobType) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) TtlUpdateRequest(com.github.ambry.protocol.TtlUpdateRequest) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) PutRequest(com.github.ambry.protocol.PutRequest) UndeleteRequest(com.github.ambry.protocol.UndeleteRequest) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ServerErrorCode(com.github.ambry.server.ServerErrorCode) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) LongAdder(java.util.concurrent.atomic.LongAdder) BoundedNettyByteBufReceive(com.github.ambry.network.BoundedNettyByteBufReceive) ByteBufferChannel(com.github.ambry.utils.ByteBufferChannel) GetRequest(com.github.ambry.protocol.GetRequest) DeleteRequest(com.github.ambry.protocol.DeleteRequest)

Example 12 with DeleteRequest

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

the class ServerHardDeleteTest method deleteBlob.

/**
 * Deletes a single blob from ambry server node
 * @param blobId the {@link BlobId} that needs to be deleted
 * @param channel the {@link BlockingChannel} to use to send and receive data
 * @throws IOException
 */
void deleteBlob(BlobId blobId, BlockingChannel channel) throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest(1, "client1", blobId, time.milliseconds());
    channel.send(deleteRequest);
    InputStream deleteResponseStream = channel.receive().getInputStream();
    DeleteResponse deleteResponse = DeleteResponse.readFrom(new DataInputStream(deleteResponseStream));
    Assert.assertEquals(deleteResponse.getError(), ServerErrorCode.No_Error);
}
Also used : DeleteResponse(com.github.ambry.protocol.DeleteResponse) DataInputStream(java.io.DataInputStream) CrcInputStream(com.github.ambry.utils.CrcInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataInputStream(java.io.DataInputStream) DeleteRequest(com.github.ambry.protocol.DeleteRequest)

Example 13 with DeleteRequest

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

the class AmbryRequests method handleDeleteRequest.

public void handleDeleteRequest(Request request) throws IOException, InterruptedException {
    DeleteRequest deleteRequest = DeleteRequest.readFrom(new DataInputStream(request.getInputStream()), clusterMap);
    long requestQueueTime = SystemTime.getInstance().milliseconds() - request.getStartTimeInMs();
    long totalTimeSpent = requestQueueTime;
    metrics.deleteBlobRequestQueueTimeInMs.update(requestQueueTime);
    metrics.deleteBlobRequestRate.mark();
    long startTime = SystemTime.getInstance().milliseconds();
    DeleteResponse response = null;
    try {
        ServerErrorCode error = validateRequest(deleteRequest.getBlobId().getPartition(), RequestOrResponseType.DeleteRequest);
        if (error != ServerErrorCode.No_Error) {
            logger.error("Validating delete request failed with error {} for request {}", error, deleteRequest);
            response = new DeleteResponse(deleteRequest.getCorrelationId(), deleteRequest.getClientId(), error);
        } else {
            MessageFormatInputStream stream = new DeleteMessageFormatInputStream(deleteRequest.getBlobId(), deleteRequest.getAccountId(), deleteRequest.getContainerId(), deleteRequest.getDeletionTimeInMs());
            MessageInfo info = new MessageInfo(deleteRequest.getBlobId(), stream.getSize(), deleteRequest.getAccountId(), deleteRequest.getContainerId(), deleteRequest.getDeletionTimeInMs());
            ArrayList<MessageInfo> infoList = new ArrayList<MessageInfo>();
            infoList.add(info);
            MessageFormatWriteSet writeset = new MessageFormatWriteSet(stream, infoList, false);
            Store storeToDelete = storageManager.getStore(deleteRequest.getBlobId().getPartition());
            storeToDelete.delete(writeset);
            response = new DeleteResponse(deleteRequest.getCorrelationId(), deleteRequest.getClientId(), ServerErrorCode.No_Error);
            if (notification != null) {
                notification.onBlobReplicaDeleted(currentNode.getHostname(), currentNode.getPort(), deleteRequest.getBlobId().getID(), BlobReplicaSourceType.PRIMARY);
            }
        }
    } catch (StoreException e) {
        boolean logInErrorLevel = false;
        if (e.getErrorCode() == StoreErrorCodes.ID_Not_Found) {
            metrics.idNotFoundError.inc();
        } else if (e.getErrorCode() == StoreErrorCodes.TTL_Expired) {
            metrics.ttlExpiredError.inc();
        } else if (e.getErrorCode() == StoreErrorCodes.ID_Deleted) {
            metrics.idDeletedError.inc();
        } else if (e.getErrorCode() == StoreErrorCodes.Authorization_Failure) {
            metrics.deleteAuthorizationFailure.inc();
        } else {
            logInErrorLevel = true;
            metrics.unExpectedStoreDeleteError.inc();
        }
        if (logInErrorLevel) {
            logger.error("Store exception on a delete with error code {} for request {}", e.getErrorCode(), deleteRequest, e);
        } else {
            logger.trace("Store exception on a delete with error code {} for request {}", e.getErrorCode(), deleteRequest, e);
        }
        response = new DeleteResponse(deleteRequest.getCorrelationId(), deleteRequest.getClientId(), ErrorMapping.getStoreErrorMapping(e.getErrorCode()));
    } catch (Exception e) {
        logger.error("Unknown exception for delete request " + deleteRequest, e);
        response = new DeleteResponse(deleteRequest.getCorrelationId(), deleteRequest.getClientId(), ServerErrorCode.Unknown_Error);
        metrics.unExpectedStoreDeleteError.inc();
    } finally {
        long processingTime = SystemTime.getInstance().milliseconds() - startTime;
        totalTimeSpent += processingTime;
        publicAccessLogger.info("{} {} processingTime {}", deleteRequest, response, processingTime);
        metrics.deleteBlobProcessingTimeInMs.update(processingTime);
    }
    requestResponseChannel.sendResponse(response, request, new ServerNetworkResponseMetrics(metrics.deleteBlobResponseQueueTimeInMs, metrics.deleteBlobSendTimeInMs, metrics.deleteBlobTotalTimeInMs, null, null, totalTimeSpent));
}
Also used : ServerNetworkResponseMetrics(com.github.ambry.network.ServerNetworkResponseMetrics) ArrayList(java.util.ArrayList) Store(com.github.ambry.store.Store) DeleteMessageFormatInputStream(com.github.ambry.messageformat.DeleteMessageFormatInputStream) DeleteMessageFormatInputStream(com.github.ambry.messageformat.DeleteMessageFormatInputStream) MessageFormatInputStream(com.github.ambry.messageformat.MessageFormatInputStream) PutMessageFormatInputStream(com.github.ambry.messageformat.PutMessageFormatInputStream) DataInputStream(java.io.DataInputStream) 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) DeleteResponse(com.github.ambry.protocol.DeleteResponse) DeleteRequest(com.github.ambry.protocol.DeleteRequest) MessageFormatWriteSet(com.github.ambry.messageformat.MessageFormatWriteSet)

Example 14 with DeleteRequest

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

the class InMemoryCloudDestinationErrorSimulationTest method testUndeleteBlobErrorSimulation.

/**
 * test error simulation for UndeleteRequest
 * @throws Exception
 */
@Test
public void testUndeleteBlobErrorSimulation() throws Exception {
    BlobId blobId = doPut(partitionId);
    // Delete the blob first
    {
        DeleteRequest request = new DeleteRequest(1234, "clientId", blobId, SystemTime.getInstance().milliseconds());
        RequestInfo requestInfo = new RequestInfo(hostname, port, request, replica, null);
        ResponseInfo responseInfo = sendAndWaitForResponses(requestInfo);
        DeleteResponse response = responseInfo.getError() == null ? (DeleteResponse) RouterUtils.mapToReceivedResponse((Response) responseInfo.getResponse()) : null;
        Assert.assertEquals("DeleteRequest should succeed.", response.getError(), ServerErrorCode.No_Error);
    }
    UndeleteRequest request = new UndeleteRequest(1234, "clientId", blobId, SystemTime.getInstance().milliseconds());
    RequestInfo requestInfo = new RequestInfo(hostname, port, request, replica, null);
    ResponseInfo responseInfo = sendAndWaitForResponses(requestInfo);
    UndeleteResponse response = responseInfo.getError() == null ? (UndeleteResponse) RouterUtils.mapToReceivedResponse((UndeleteResponse) responseInfo.getResponse()) : null;
    Assert.assertEquals("UndeleteRequest should succeed.", response.getError(), ServerErrorCode.No_Error);
    response.release();
    // inject error for cloud colo.
    cloudDestination.setServerErrorForAllRequests(StoreErrorCodes.Unknown_Error);
    request = new UndeleteRequest(1234, "clientId", blobId, SystemTime.getInstance().milliseconds());
    requestInfo = new RequestInfo(hostname, port, request, replica, null);
    responseInfo = sendAndWaitForResponses(requestInfo);
    response = responseInfo.getError() == null ? (UndeleteResponse) RouterUtils.mapToReceivedResponse((UndeleteResponse) responseInfo.getResponse()) : null;
    Assert.assertEquals("UndeleteRequest should return Unknown_Error.", response.getError(), ServerErrorCode.Unknown_Error);
    response.release();
}
Also used : ResponseInfo(com.github.ambry.network.ResponseInfo) PartitionResponseInfo(com.github.ambry.protocol.PartitionResponseInfo) GetResponse(com.github.ambry.protocol.GetResponse) TtlUpdateResponse(com.github.ambry.protocol.TtlUpdateResponse) UndeleteResponse(com.github.ambry.protocol.UndeleteResponse) DeleteResponse(com.github.ambry.protocol.DeleteResponse) PutResponse(com.github.ambry.protocol.PutResponse) Response(com.github.ambry.protocol.Response) DeleteResponse(com.github.ambry.protocol.DeleteResponse) UndeleteRequest(com.github.ambry.protocol.UndeleteRequest) UndeleteResponse(com.github.ambry.protocol.UndeleteResponse) PartitionRequestInfo(com.github.ambry.protocol.PartitionRequestInfo) RequestInfo(com.github.ambry.network.RequestInfo) BlobId(com.github.ambry.commons.BlobId) DeleteRequest(com.github.ambry.protocol.DeleteRequest) Test(org.junit.Test)

Example 15 with DeleteRequest

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

the class InMemoryCloudDestinationErrorSimulationTest method testDeleteBlobErrorSimulation.

/**
 * test error simulation for DeleteRequest
 * @throws Exception
 */
@Test
public void testDeleteBlobErrorSimulation() throws Exception {
    BlobId blobId = doPut(partitionId);
    DeleteRequest request = new DeleteRequest(1234, "clientId", blobId, SystemTime.getInstance().milliseconds());
    RequestInfo requestInfo = new RequestInfo(hostname, port, request, replica, null);
    ResponseInfo responseInfo = sendAndWaitForResponses(requestInfo);
    DeleteResponse response = responseInfo.getError() == null ? ((DeleteResponse) RouterUtils.mapToReceivedResponse((Response) responseInfo.getResponse())) : null;
    Assert.assertEquals("DeleteBlob should succeed.", response.getError(), ServerErrorCode.No_Error);
    // inject error for cloud colo.
    cloudDestination.setServerErrorForAllRequests(StoreErrorCodes.Unknown_Error);
    request = new DeleteRequest(1234, "clientId", blobId, SystemTime.getInstance().milliseconds());
    requestInfo = new RequestInfo(hostname, port, request, replica, null);
    responseInfo = sendAndWaitForResponses(requestInfo);
    response = responseInfo.getError() == null ? ((DeleteResponse) RouterUtils.mapToReceivedResponse((Response) responseInfo.getResponse())) : null;
    Assert.assertEquals("DeleteBlob should return Unknown_Error.", response.getError(), ServerErrorCode.Unknown_Error);
    response.release();
}
Also used : ResponseInfo(com.github.ambry.network.ResponseInfo) PartitionResponseInfo(com.github.ambry.protocol.PartitionResponseInfo) GetResponse(com.github.ambry.protocol.GetResponse) TtlUpdateResponse(com.github.ambry.protocol.TtlUpdateResponse) UndeleteResponse(com.github.ambry.protocol.UndeleteResponse) DeleteResponse(com.github.ambry.protocol.DeleteResponse) PutResponse(com.github.ambry.protocol.PutResponse) Response(com.github.ambry.protocol.Response) DeleteResponse(com.github.ambry.protocol.DeleteResponse) PartitionRequestInfo(com.github.ambry.protocol.PartitionRequestInfo) RequestInfo(com.github.ambry.network.RequestInfo) BlobId(com.github.ambry.commons.BlobId) DeleteRequest(com.github.ambry.protocol.DeleteRequest) Test(org.junit.Test)

Aggregations

DeleteRequest (com.github.ambry.protocol.DeleteRequest)20 DeleteResponse (com.github.ambry.protocol.DeleteResponse)14 BlobId (com.github.ambry.commons.BlobId)12 GetResponse (com.github.ambry.protocol.GetResponse)11 PartitionRequestInfo (com.github.ambry.protocol.PartitionRequestInfo)11 DataInputStream (java.io.DataInputStream)10 ArrayList (java.util.ArrayList)9 BlobProperties (com.github.ambry.messageformat.BlobProperties)8 GetRequest (com.github.ambry.protocol.GetRequest)8 PutResponse (com.github.ambry.protocol.PutResponse)8 PartitionId (com.github.ambry.clustermap.PartitionId)7 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)7 PutRequest (com.github.ambry.protocol.PutRequest)7 IOException (java.io.IOException)7 MockPartitionId (com.github.ambry.clustermap.MockPartitionId)6 ReplicaId (com.github.ambry.clustermap.ReplicaId)6 VerifiableProperties (com.github.ambry.config.VerifiableProperties)6 UndeleteResponse (com.github.ambry.protocol.UndeleteResponse)6 NettyByteBufDataInputStream (com.github.ambry.utils.NettyByteBufDataInputStream)6 AccountService (com.github.ambry.account.AccountService)5