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