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