use of com.github.ambry.router.RouterException in project ambry by linkedin.
the class MockRouter method getBlob.
@Override
public Future<GetBlobResult> getBlob(String blobId, GetBlobOptions options, Callback<GetBlobResult> callback, QuotaChargeCallback quotaChargeCallback) {
lock.lock();
try {
BlobInfoAndData blob = allBlobs.get(blobId);
FutureResult<GetBlobResult> future = new FutureResult<>();
if (blob == null) {
Exception e = new RouterException("NotFound", RouterErrorCode.BlobDoesNotExist);
future.done(null, e);
if (callback != null) {
callback.onCompletion(null, e);
}
return future;
}
// Discard the options and only return the BlobAll.
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(blob.getData()));
GetBlobResult result = new GetBlobResult(blob.getBlobInfo(), channel);
future.done(result, null);
if (callback != null) {
callback.onCompletion(result, null);
}
return future;
} finally {
lock.unlock();
}
}
use of com.github.ambry.router.RouterException in project ambry by linkedin.
the class RouterStoreTest method testSizeLimitInList.
/**
* Test when the number of updates exceeds the maximum number of versions to save. RouterStore should start removing
* the old blobs.
*/
@Test
public void testSizeLimitInList() throws Exception {
RouterStore store = new RouterStore(accountServiceMetrics, backup, helixStore, new AtomicReference<>(router), forBackfill, TOTAL_NUMBER_OF_VERSION_TO_KEEP, config);
Map<Short, Account> idToRefAccountMap = new HashMap<>();
Map<Short, Map<Short, Container>> idtoRefContainerMap = new HashMap<>();
Set<Short> accountIDSet = new HashSet<>();
for (int i = 0; i < TOTAL_NUMBER_OF_VERSION_TO_KEEP; i++) {
// generate an new account and test update and fetch on this account
Map<Short, Account> newIdToRefAccountMap = new HashMap<>();
AccountTestUtils.generateRefAccounts(newIdToRefAccountMap, idtoRefContainerMap, accountIDSet, 1, 1);
if (!forBackfill) {
for (Map.Entry<Short, Account> entry : newIdToRefAccountMap.entrySet()) {
idToRefAccountMap.put(entry.getKey(), entry.getValue());
}
} else {
idToRefAccountMap = newIdToRefAccountMap;
}
assertUpdateAndFetch(store, idToRefAccountMap, newIdToRefAccountMap, i + 1, i + 1);
}
// Now we already have maximum number of versions in the list, adding a new version should remove the oldest one.
List<RouterStore.BlobIDAndVersion> blobIDAndVersions = getBlobIDAndVersionInHelix(TOTAL_NUMBER_OF_VERSION_TO_KEEP);
Collections.sort(blobIDAndVersions, Comparator.comparing(RouterStore.BlobIDAndVersion::getVersion));
assertEquals(1, blobIDAndVersions.get(0).getVersion());
Map<Short, Account> newIdToRefAccountMap = new HashMap<>();
AccountTestUtils.generateRefAccounts(newIdToRefAccountMap, idtoRefContainerMap, accountIDSet, 1, 1);
if (!forBackfill) {
for (Map.Entry<Short, Account> entry : newIdToRefAccountMap.entrySet()) {
idToRefAccountMap.put(entry.getKey(), entry.getValue());
}
} else {
idToRefAccountMap = newIdToRefAccountMap;
}
assertUpdateAndFetch(store, idToRefAccountMap, newIdToRefAccountMap, TOTAL_NUMBER_OF_VERSION_TO_KEEP + 1, TOTAL_NUMBER_OF_VERSION_TO_KEEP);
// Get the list again and compare the blob ids
List<RouterStore.BlobIDAndVersion> blobIDAndVersionsAfterUpdate = getBlobIDAndVersionInHelix(TOTAL_NUMBER_OF_VERSION_TO_KEEP);
Collections.sort(blobIDAndVersionsAfterUpdate, Comparator.comparing(RouterStore.BlobIDAndVersion::getVersion));
assertEquals("First version should be removed", 2, blobIDAndVersionsAfterUpdate.get(0).getVersion());
assertEquals("Version mismatch", TOTAL_NUMBER_OF_VERSION_TO_KEEP + 1, blobIDAndVersionsAfterUpdate.get(TOTAL_NUMBER_OF_VERSION_TO_KEEP - 1).getVersion());
for (int i = 1; i < TOTAL_NUMBER_OF_VERSION_TO_KEEP; i++) {
assertEquals("BlobIDAndVersion mismatch at index " + i, blobIDAndVersions.get(i), blobIDAndVersionsAfterUpdate.get(i - 1));
}
try {
router.getBlob(blobIDAndVersions.get(0).getBlobID(), new GetBlobOptionsBuilder().build()).get();
fail("Expecting not found exception");
} catch (ExecutionException e) {
Throwable t = e.getCause();
assertTrue("Cause should be RouterException", t instanceof RouterException);
assertEquals("ErrorCode mismatch", RouterErrorCode.BlobDoesNotExist, ((RouterException) t).getErrorCode());
}
}
use of com.github.ambry.router.RouterException in project ambry by linkedin.
the class FrontendRestRequestService method submitResponse.
/**
* Submits the response and {@code responseBody} (and any {@code exception})for the {@code restRequest} to the
* {@code responseHandler}.
* @param restRequest the {@link RestRequest} for which a response is ready.
* @param restResponseChannel the {@link RestResponseChannel} over which the response can be sent.
* @param responseBody the body of the response in the form of a {@link ReadableStreamChannel}.
* @param exception any {@link Exception} that occurred during the handling of {@code restRequest}.
*/
void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) {
try {
if (restRequest.getArgs().containsKey(InternalKeys.SEND_TRACKING_INFO) && (Boolean) restRequest.getArgs().get(InternalKeys.SEND_TRACKING_INFO)) {
restResponseChannel.setHeader(TrackingHeaders.DATACENTER_NAME, datacenterName);
restResponseChannel.setHeader(TrackingHeaders.FRONTEND_NAME, hostname);
}
if (exception instanceof RouterException) {
exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode()));
}
responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception);
} catch (Exception e) {
frontendMetrics.responseSubmissionError.inc();
if (exception != null) {
logger.error("Error submitting response to response handler", e);
} else {
exception = e;
}
logger.error("Handling of request {} failed", restRequest.getUri(), exception);
restResponseChannel.onResponseComplete(exception);
if (responseBody != null) {
try {
responseBody.close();
} catch (IOException ioe) {
frontendMetrics.resourceReleaseError.inc();
logger.error("Error closing ReadableStreamChannel", e);
}
}
}
}
Aggregations