Search in sources :

Example 6 with RequestOrResponse

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

the class AmbryServerRequestsTest method sendAndVerifyGetOriginalStoreKeys.

/**
 * Sends and verifies that GetRequest with a list of original blobIds works correctly.
 * @param blobIds List of blobIds for GetRequest.
 * @param expectedErrorCode the {@link ServerErrorCode} expected in the response.
 * @throws InterruptedException
 * @throws IOException
 */
private void sendAndVerifyGetOriginalStoreKeys(List<BlobId> blobIds, ServerErrorCode expectedErrorCode) throws InterruptedException, IOException {
    PartitionId partitionId = blobIds.get(0).getPartition();
    int correlationId = blobIds.get(0).getContainerId();
    String clientId = TestUtils.getRandomString(10);
    PartitionRequestInfo pRequestInfo = new PartitionRequestInfo(partitionId, blobIds);
    RequestOrResponse request = new GetRequest(correlationId, clientId, MessageFormatFlags.All, Collections.singletonList(pRequestInfo), GetOption.Include_All);
    storageManager.resetStore();
    if (!expectedErrorCode.equals(ServerErrorCode.Unknown_Error)) {
        // known error will be filled to each PartitionResponseInfo and set ServerErrorCode.No_Error in response.
        Response response = sendRequestGetResponse(request, ServerErrorCode.No_Error);
        assertEquals("Operation received at the store not as expected", RequestOrResponseType.GetRequest, MockStorageManager.operationReceived);
        for (PartitionResponseInfo info : ((GetResponse) response).getPartitionResponseInfoList()) {
            assertEquals("Error code does not match expected", expectedErrorCode, info.getErrorCode());
        }
        response.release();
    } else {
        sendRequestGetResponse(request, ServerErrorCode.Unknown_Error).release();
    }
}
Also used : CatchupStatusAdminResponse(com.github.ambry.protocol.CatchupStatusAdminResponse) Response(com.github.ambry.protocol.Response) GetResponse(com.github.ambry.protocol.GetResponse) ReplicaMetadataResponse(com.github.ambry.protocol.ReplicaMetadataResponse) AdminResponse(com.github.ambry.protocol.AdminResponse) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) GetRequest(com.github.ambry.protocol.GetRequest) PartitionResponseInfo(com.github.ambry.protocol.PartitionResponseInfo) MockPartitionId(com.github.ambry.clustermap.MockPartitionId) PartitionId(com.github.ambry.clustermap.PartitionId) PartitionRequestInfo(com.github.ambry.protocol.PartitionRequestInfo) GetResponse(com.github.ambry.protocol.GetResponse)

Example 7 with RequestOrResponse

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

the class AmbryRequestsTest method sendRequestGetResponse.

// helpers
// general
/**
 * Calls {@link AmbryRequests#handleRequests(Request)} with {@code request} and returns the {@link Response} received.
 * @param request the {@link Request} to process
 * @param expectedServerErrorCode the expected {@link ServerErrorCode} in the server response.
 * @return the {@link Response} received.
 * @throws InterruptedException
 * @throws IOException
 */
private Response sendRequestGetResponse(RequestOrResponse request, ServerErrorCode expectedServerErrorCode) throws InterruptedException, IOException {
    Request mockRequest = MockRequest.fromRequest(request);
    ambryRequests.handleRequests(mockRequest);
    assertEquals("Request accompanying response does not match original request", mockRequest, requestResponseChannel.lastOriginalRequest);
    assertNotNull("Response not sent", requestResponseChannel.lastResponse);
    Response response = (Response) requestResponseChannel.lastResponse;
    assertNotNull("Response is not of type Response", response);
    assertEquals("Correlation id in response does match the one in the request", request.getCorrelationId(), response.getCorrelationId());
    assertEquals("Client id in response does match the one in the request", request.getClientId(), response.getClientId());
    assertEquals("Error code does not match expected", expectedServerErrorCode, response.getError());
    return response;
}
Also used : CatchupStatusAdminResponse(com.github.ambry.protocol.CatchupStatusAdminResponse) GetResponse(com.github.ambry.protocol.GetResponse) ReplicaMetadataResponse(com.github.ambry.protocol.ReplicaMetadataResponse) AdminResponse(com.github.ambry.protocol.AdminResponse) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) Response(com.github.ambry.protocol.Response) ReplicaMetadataRequest(com.github.ambry.protocol.ReplicaMetadataRequest) GetRequest(com.github.ambry.protocol.GetRequest) Request(com.github.ambry.network.Request) CatchupStatusAdminRequest(com.github.ambry.protocol.CatchupStatusAdminRequest) AdminRequest(com.github.ambry.protocol.AdminRequest) ReplicationControlAdminRequest(com.github.ambry.protocol.ReplicationControlAdminRequest) PutRequest(com.github.ambry.protocol.PutRequest) RequestControlAdminRequest(com.github.ambry.protocol.RequestControlAdminRequest) DeleteRequest(com.github.ambry.protocol.DeleteRequest) BlobStoreControlAdminRequest(com.github.ambry.protocol.BlobStoreControlAdminRequest)

Example 8 with RequestOrResponse

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

the class BackgroundDeleter method onResponse.

/**
 * Handle the response from polling the {@link NetworkClient}.
 * @param responseInfoList the list of {@link ResponseInfo} containing the responses.
 */
protected void onResponse(List<ResponseInfo> responseInfoList) {
    for (ResponseInfo responseInfo : responseInfoList) {
        try {
            RequestInfo requestInfo = responseInfo.getRequestInfo();
            if (requestInfo == null) {
                // If requestInfo is null, it means request has been failed previously due to long wait in pending requests
                // queue. The failed request was already handled by one of the managers(PutManager, GetManager, etc). Current
                // response comes from timed-out connection associated with previous request. Router only needs to notify
                // responseHandler to mark the data node resource down.
                DataNodeId dataNodeId = responseInfo.getDataNode();
                responseHandler.onConnectionTimeout(dataNodeId);
            } else {
                long responseReceiveTime = requestInfo.getStreamHeaderFrameReceiveTime();
                if (responseReceiveTime != -1) {
                    routerMetrics.responseReceiveToHandleLatencyMs.update(System.currentTimeMillis() - responseReceiveTime);
                }
                RequestOrResponseType type = ((RequestOrResponse) requestInfo.getRequest()).getRequestType();
                logger.debug("Handling response of type {} for {}", type, requestInfo.getRequest().getCorrelationId());
                switch(type) {
                    case PutRequest:
                        putManager.handleResponse(responseInfo);
                        break;
                    case GetRequest:
                        getManager.handleResponse(responseInfo);
                        break;
                    case DeleteRequest:
                        deleteManager.handleResponse(responseInfo);
                        break;
                    case TtlUpdateRequest:
                        ttlUpdateManager.handleResponse(responseInfo);
                        break;
                    case UndeleteRequest:
                        undeleteManager.handleResponse(responseInfo);
                        break;
                    default:
                        logger.error("Unexpected response type: {} received, discarding", type);
                }
            }
        } catch (Exception e) {
            logger.error("Unexpected error received while handling a response: ", e);
            routerMetrics.operationManagerHandleResponseErrorCount.inc();
        }
    }
}
Also used : ResponseInfo(com.github.ambry.network.ResponseInfo) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) RequestInfo(com.github.ambry.network.RequestInfo) DataNodeId(com.github.ambry.clustermap.DataNodeId) IOException(java.io.IOException)

Example 9 with RequestOrResponse

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

the class UndeleteManagerTest method sendRequestsGetResponse.

private void sendRequestsGetResponse(FutureResult<Void> future, UndeleteManager undeleteManager, boolean advanceTime) {
    List<RequestInfo> requestInfoList = new ArrayList<>();
    Set<Integer> requestsToDrop = new HashSet<>();
    Set<RequestInfo> requestAcks = new HashSet<>();
    List<RequestInfo> referenceRequestInfos = new ArrayList<>();
    while (!future.isDone()) {
        undeleteManager.poll(requestInfoList, requestsToDrop);
        referenceRequestInfos.addAll(requestInfoList);
        List<ResponseInfo> responseInfoList = new ArrayList<>();
        try {
            responseInfoList = networkClient.sendAndPoll(requestInfoList, requestsToDrop, AWAIT_TIMEOUT_MS);
        } catch (RuntimeException | Error e) {
            if (!advanceTime) {
                throw e;
            }
        }
        for (ResponseInfo responseInfo : responseInfoList) {
            RequestInfo requestInfo = responseInfo.getRequestInfo();
            assertNotNull("Request is null", requestInfo);
            if (!referenceRequestInfos.contains(requestInfo)) {
                throw new IllegalStateException("Received Response for unrecognized request");
            } else if (requestAcks.contains(requestInfo)) {
                throw new IllegalStateException("Received response more than once for a request");
            }
            requestAcks.add(requestInfo);
            RequestInfo routerRequestInfo = responseInfo.getRequestInfo();
            RequestOrResponseType type = ((RequestOrResponse) routerRequestInfo.getRequest()).getRequestType();
            switch(type) {
                case UndeleteRequest:
                    undeleteManager.handleResponse(responseInfo);
                    break;
                default:
                    throw new IllegalStateException("Unrecognized request type: " + type);
            }
        }
        if (advanceTime) {
            time.sleep(ADVANCE_TIME_INCREMENT_MS);
        }
        requestInfoList.clear();
    }
}
Also used : ResponseInfo(com.github.ambry.network.ResponseInfo) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) ArrayList(java.util.ArrayList) RequestInfo(com.github.ambry.network.RequestInfo) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) HashSet(java.util.HashSet)

Example 10 with RequestOrResponse

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

the class TtlUpdateNotificationSystem method sendRequestsGetResponses.

// helpers
// general
/**
 * Sends all the requests that the {@code manager} may have ready
 * @param futureResult the {@link FutureResult} that tracks the operation
 * @param manager the {@link TtlUpdateManager} to poll for requests
 * @param advanceTime if {@code true}, advances time after each poll and handleResponse iteration
 * @param ignoreUnrecognizedRequests if {@code true}, doesn't throw an exception if a response is received for a
 *                                   request not sent in this execution of the function
 */
private void sendRequestsGetResponses(FutureResult<Void> futureResult, TtlUpdateManager manager, boolean advanceTime, boolean ignoreUnrecognizedRequests) {
    List<RequestInfo> requestInfoList = new ArrayList<>();
    Set<Integer> requestsToDrop = new HashSet<>();
    Set<RequestInfo> requestAcks = new HashSet<>();
    List<RequestInfo> referenceRequestInfos = new ArrayList<>();
    while (!futureResult.isDone()) {
        manager.poll(requestInfoList, requestsToDrop);
        referenceRequestInfos.addAll(requestInfoList);
        List<ResponseInfo> responseInfoList = new ArrayList<>();
        try {
            responseInfoList = networkClient.sendAndPoll(requestInfoList, requestsToDrop, AWAIT_TIMEOUT_MS);
        } catch (RuntimeException | Error e) {
            if (!advanceTime) {
                throw e;
            }
        }
        for (ResponseInfo responseInfo : responseInfoList) {
            RequestInfo requestInfo = responseInfo.getRequestInfo();
            assertNotNull("RequestInfo is null", requestInfo);
            if (!referenceRequestInfos.contains(requestInfo)) {
                if (ignoreUnrecognizedRequests) {
                    continue;
                }
                throw new IllegalStateException("Received response for unrecognized request");
            } else if (requestAcks.contains(requestInfo)) {
                // received a second response for the same request
                throw new IllegalStateException("Received response more than once for a request");
            }
            requestAcks.add(requestInfo);
            RequestInfo routerRequestInfo = responseInfo.getRequestInfo();
            RequestOrResponseType type = ((RequestOrResponse) routerRequestInfo.getRequest()).getRequestType();
            switch(type) {
                case TtlUpdateRequest:
                    manager.handleResponse(responseInfo);
                    break;
                default:
                    throw new IllegalStateException("Unrecognized request type: " + type);
            }
        }
        if (advanceTime) {
            time.sleep(ADVANCE_TIME_INCREMENT_MS);
        }
        responseInfoList.forEach(ResponseInfo::release);
        requestInfoList.clear();
    }
}
Also used : ResponseInfo(com.github.ambry.network.ResponseInfo) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) ArrayList(java.util.ArrayList) RequestInfo(com.github.ambry.network.RequestInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) HashSet(java.util.HashSet)

Aggregations

RequestOrResponse (com.github.ambry.protocol.RequestOrResponse)10 GetResponse (com.github.ambry.protocol.GetResponse)7 AdminResponse (com.github.ambry.protocol.AdminResponse)6 CatchupStatusAdminResponse (com.github.ambry.protocol.CatchupStatusAdminResponse)6 ReplicaMetadataResponse (com.github.ambry.protocol.ReplicaMetadataResponse)6 Response (com.github.ambry.protocol.Response)6 GetRequest (com.github.ambry.protocol.GetRequest)5 RequestOrResponseType (com.github.ambry.protocol.RequestOrResponseType)5 DeleteRequest (com.github.ambry.protocol.DeleteRequest)4 PartitionRequestInfo (com.github.ambry.protocol.PartitionRequestInfo)4 PartitionResponseInfo (com.github.ambry.protocol.PartitionResponseInfo)4 PutRequest (com.github.ambry.protocol.PutRequest)4 ArrayList (java.util.ArrayList)4 PartitionId (com.github.ambry.clustermap.PartitionId)3 BlobProperties (com.github.ambry.messageformat.BlobProperties)3 RequestInfo (com.github.ambry.network.RequestInfo)3 ResponseInfo (com.github.ambry.network.ResponseInfo)3 ReplicaMetadataRequest (com.github.ambry.protocol.ReplicaMetadataRequest)3 MockPartitionId (com.github.ambry.clustermap.MockPartitionId)2 BlobId (com.github.ambry.commons.BlobId)2