Search in sources :

Example 11 with ByteBufferReadableStreamChannel

use of com.github.ambry.commons.ByteBufferReadableStreamChannel in project ambry by linkedin.

the class RouterStore method writeAccountMapToRouter.

/**
 * Save the given {@link Account} metadata, as json object, in a blob in {@code AmbryServer}.
 * @param accountMap The {@link Account} metadata to save.
 * @param router The {@link Router} instance.
 * @return A blob id if the operation is finished successfully.
 * @throws Exception If there is any exceptions while saving the bytes to {@code AmbryServer}.
 */
static String writeAccountMapToRouter(Map<String, String> accountMap, Router router) throws Exception {
    Objects.requireNonNull(router, "Router is null");
    // Construct the json object and save it to ambry server.
    JSONObject object = new JSONObject();
    for (Map.Entry<String, String> entry : accountMap.entrySet()) {
        object.put(entry.getKey(), entry.getValue());
    }
    ByteBufferReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(object.toString().getBytes(Charsets.UTF_8)));
    BlobProperties properties = new BlobProperties(channel.getSize(), SERVICE_ID, ACCOUNT_ID, CONTAINER_ID, false);
    return router.putBlob(properties, null, channel, PutBlobOptions.DEFAULT).get();
}
Also used : ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) JSONObject(org.json.JSONObject) BlobProperties(com.github.ambry.messageformat.BlobProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with ByteBufferReadableStreamChannel

use of com.github.ambry.commons.ByteBufferReadableStreamChannel 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 13 with ByteBufferReadableStreamChannel

use of com.github.ambry.commons.ByteBufferReadableStreamChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method oldStyleUserMetadataTest.

/**
 * Tests how metadata that has not been POSTed in the form of headers is returned.
 * @throws Exception
 */
@Test
public void oldStyleUserMetadataTest() throws Exception {
    ByteBuffer content = ByteBuffer.allocate(0);
    BlobProperties blobProperties = new BlobProperties(0, "userMetadataTestOldStyleServiceID", Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false);
    byte[] usermetadata = TestUtils.getRandomBytes(25);
    String blobId = router.putBlob(blobProperties, usermetadata, new ByteBufferReadableStreamChannel(content), new PutBlobOptionsBuilder().build()).get();
    RestUtils.SubResource[] subResources = { RestUtils.SubResource.UserMetadata, RestUtils.SubResource.BlobInfo };
    for (RestUtils.SubResource subResource : subResources) {
        RestRequest restRequest = createRestRequest(RestMethod.GET, blobId + "/" + subResource, null, null);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        doOperation(restRequest, restResponseChannel);
        assertEquals("Unexpected response status for " + subResource, ResponseStatus.Ok, restResponseChannel.getStatus());
        assertEquals("Unexpected Content-Type for " + subResource, "application/octet-stream", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
        assertEquals("Unexpected Content-Length for " + subResource, usermetadata.length, Integer.parseInt(restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        assertArrayEquals("Unexpected user metadata for " + subResource, usermetadata, restResponseChannel.getResponseBody());
    }
}
Also used : ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) PutBlobOptionsBuilder(com.github.ambry.router.PutBlobOptionsBuilder) BlobProperties(com.github.ambry.messageformat.BlobProperties) RestUtils(com.github.ambry.rest.RestUtils) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 14 with ByteBufferReadableStreamChannel

use of com.github.ambry.commons.ByteBufferReadableStreamChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method submitResponseTest.

/**
 * Tests
 * {@link FrontendRestRequestService#submitResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)}.
 * @throws JSONException
 * @throws UnsupportedEncodingException
 * @throws URISyntaxException
 */
@Test
public void submitResponseTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
    String exceptionMsg = TestUtils.getRandomString(10);
    responseHandler.shutdown();
    // handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
    try {
        // there is an exception already.
        RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        assertTrue("RestRequest channel is not open", restRequest.isOpen());
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, new RuntimeException(exceptionMsg));
        assertEquals("Unexpected exception message", exceptionMsg, restResponseChannel.getException().getMessage());
        // there is no exception and exception thrown when the response is submitted.
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        assertTrue("RestRequest channel is not open", restRequest.isOpen());
        restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel response = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
        assertTrue("Response channel is not open", response.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, response, null);
        assertNotNull("There is no cause of failure", restResponseChannel.getException());
        // resources should have been cleaned up.
        assertFalse("Response channel is not cleaned up", response.isOpen());
    } finally {
        frontendRestRequestService.setupResponseHandler(responseHandler);
        responseHandler.start();
    }
    // verify tracking infos are attached accordingly.
    RestRequest restRequest;
    MockRestResponseChannel restResponseChannel;
    for (String header : RestUtils.TrackingHeaders.TRACKING_HEADERS) {
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        restResponseChannel = new MockRestResponseChannel();
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
        assertTrue("Response header should not contain tracking info", restResponseChannel.getHeader(header) == null);
    }
    restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    restRequest.setArg(RestUtils.InternalKeys.SEND_TRACKING_INFO, new Boolean(true));
    restResponseChannel = new MockRestResponseChannel();
    frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
    assertEquals("Unexpected or missing tracking info", datacenterName, restResponseChannel.getHeader(RestUtils.TrackingHeaders.DATACENTER_NAME));
    assertEquals("Unexpected or missing tracking info", hostname, restResponseChannel.getHeader(RestUtils.TrackingHeaders.FRONTEND_NAME));
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 15 with ByteBufferReadableStreamChannel

use of com.github.ambry.commons.ByteBufferReadableStreamChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method verifyResponsePathAccountAndContainerInjection.

/**
 * Test response path account and container injection for V1 blob IDs.
 * @param serviceId the service ID for the blob.
 * @param isPrivate {@code true} if the blob is private.
 * @param expectedAccount the expected {@link Account} to verify its presence in {@link RestRequest}.
 * @param expectedContainer the expected {@link Container} to verify its presence in {@link RestRequest}.
 * @throws Exception
 */
private void verifyResponsePathAccountAndContainerInjection(String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer) throws Exception {
    BlobProperties blobProperties = new BlobProperties(0, serviceId, "owner", "image/gif", isPrivate, Utils.Infinite_Time, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false, null, null, null);
    ReadableStreamChannel content = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
    String blobId = router.putBlobWithIdVersion(blobProperties, new byte[0], content, BlobId.BLOB_ID_V1).get();
    verifyAccountAndContainerFromBlobId(blobId, expectedAccount, expectedContainer, null);
}
Also used : ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) BlobProperties(com.github.ambry.messageformat.BlobProperties)

Aggregations

ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)26 BlobProperties (com.github.ambry.messageformat.BlobProperties)18 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 LoggingNotificationSystem (com.github.ambry.commons.LoggingNotificationSystem)8 InMemAccountService (com.github.ambry.account.InMemAccountService)7 PutBlobOptionsBuilder (com.github.ambry.router.PutBlobOptionsBuilder)6 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)6 IOException (java.io.IOException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 VerifiableProperties (com.github.ambry.config.VerifiableProperties)4 ResponseInfo (com.github.ambry.network.ResponseInfo)4 PutResponse (com.github.ambry.protocol.PutResponse)4 MockRestRequest (com.github.ambry.rest.MockRestRequest)4 RestRequest (com.github.ambry.rest.RestRequest)4 NettyByteBufDataInputStream (com.github.ambry.utils.NettyByteBufDataInputStream)4 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)3 BlobId (com.github.ambry.commons.BlobId)3 ResponseHandler (com.github.ambry.commons.ResponseHandler)3 RouterConfig (com.github.ambry.config.RouterConfig)3