Search in sources :

Example 6 with RequestOrResponseType

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

the class AmbryRequests method handleRequests.

public void handleRequests(Request request) throws InterruptedException {
    try {
        DataInputStream stream = new DataInputStream(request.getInputStream());
        RequestOrResponseType type = RequestOrResponseType.values()[stream.readShort()];
        switch(type) {
            case PutRequest:
                handlePutRequest(request);
                break;
            case GetRequest:
                handleGetRequest(request);
                break;
            case DeleteRequest:
                handleDeleteRequest(request);
                break;
            case ReplicaMetadataRequest:
                handleReplicaMetadataRequest(request);
                break;
            case AdminRequest:
                handleAdminRequest(request);
                break;
            default:
                throw new UnsupportedOperationException("Request type not supported");
        }
    } catch (Exception e) {
        logger.error("Error while handling request " + request + " closing connection", e);
        requestResponseChannel.closeConnection(request);
    }
}
Also used : RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) DataInputStream(java.io.DataInputStream) StoreException(com.github.ambry.store.StoreException) IOException(java.io.IOException) MessageFormatException(com.github.ambry.messageformat.MessageFormatException)

Example 7 with RequestOrResponseType

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

the class AmbryRequestsTest method controlRequestSuccessTest.

/**
 * Tests that {@link AdminRequestOrResponseType#RequestControl} works correctly.
 * @throws InterruptedException
 * @throws IOException
 */
@Test
public void controlRequestSuccessTest() throws InterruptedException, IOException {
    RequestOrResponseType[] requestOrResponseTypes = { RequestOrResponseType.PutRequest, RequestOrResponseType.DeleteRequest, RequestOrResponseType.GetRequest, RequestOrResponseType.ReplicaMetadataRequest };
    for (RequestOrResponseType requestType : requestOrResponseTypes) {
        List<? extends PartitionId> partitionIds = clusterMap.getWritablePartitionIds();
        for (PartitionId id : partitionIds) {
            doRequestControlRequestTest(requestType, id);
        }
        doRequestControlRequestTest(requestType, null);
    }
}
Also used : AdminRequestOrResponseType(com.github.ambry.protocol.AdminRequestOrResponseType) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) PartitionId(com.github.ambry.clustermap.PartitionId) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 8 with RequestOrResponseType

use of com.github.ambry.protocol.RequestOrResponseType 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 RequestOrResponseType

use of com.github.ambry.protocol.RequestOrResponseType 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 RequestOrResponseType

use of com.github.ambry.protocol.RequestOrResponseType 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

RequestOrResponseType (com.github.ambry.protocol.RequestOrResponseType)11 RequestOrResponse (com.github.ambry.protocol.RequestOrResponse)5 PartitionId (com.github.ambry.clustermap.PartitionId)4 AdminRequestOrResponseType (com.github.ambry.protocol.AdminRequestOrResponseType)4 RequestInfo (com.github.ambry.network.RequestInfo)3 ResponseInfo (com.github.ambry.network.ResponseInfo)3 AdminResponse (com.github.ambry.protocol.AdminResponse)3 CatchupStatusAdminResponse (com.github.ambry.protocol.CatchupStatusAdminResponse)3 ClusterMap (com.github.ambry.clustermap.ClusterMap)2 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)2 MessageFormatInputStreamTest (com.github.ambry.messageformat.MessageFormatInputStreamTest)2 GetResponse (com.github.ambry.protocol.GetResponse)2 PartitionResponseInfo (com.github.ambry.protocol.PartitionResponseInfo)2 RequestControlAdminRequest (com.github.ambry.protocol.RequestControlAdminRequest)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Test (org.junit.Test)2