Search in sources :

Example 11 with RouterException

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();
    }
}
Also used : RouterException(com.github.ambry.router.RouterException) GetBlobResult(com.github.ambry.router.GetBlobResult) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) FutureResult(com.github.ambry.router.FutureResult) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException)

Example 12 with RouterException

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());
    }
}
Also used : GetBlobOptionsBuilder(com.github.ambry.router.GetBlobOptionsBuilder) RouterException(com.github.ambry.router.RouterException) HashMap(java.util.HashMap) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with RouterException

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);
            }
        }
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) RouterException(com.github.ambry.router.RouterException) IOException(java.io.IOException) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException) RestServiceException(com.github.ambry.rest.RestServiceException)

Aggregations

RouterException (com.github.ambry.router.RouterException)13 IOException (java.io.IOException)8 Map (java.util.Map)4 ExecutionException (java.util.concurrent.ExecutionException)4 Test (org.junit.Test)4 ClusterMap (com.github.ambry.clustermap.ClusterMap)3 BlobId (com.github.ambry.commons.BlobId)3 RestRequest (com.github.ambry.rest.RestRequest)3 GetBlobOptionsBuilder (com.github.ambry.router.GetBlobOptionsBuilder)3 HashMap (java.util.HashMap)3 AccountService (com.github.ambry.account.AccountService)2 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)2 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)2 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)2 RestServiceException (com.github.ambry.rest.RestServiceException)2 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)2 FutureResult (com.github.ambry.router.FutureResult)2 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)2 Router (com.github.ambry.router.Router)2 RouterErrorCode (com.github.ambry.router.RouterErrorCode)2