Search in sources :

Example 6 with RetainingAsyncWritableChannel

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

the class GetAccountsHandlerTest method validRequestsTest.

/**
 * Test valid request cases.
 * @throws Exception
 */
@Test
public void validRequestsTest() throws Exception {
    Account account = accountService.createAndAddRandomAccount();
    ThrowingBiConsumer<RestRequest, Collection<Account>> testAction = (request, expectedAccounts) -> {
        RestResponseChannel restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel channel = sendRequestGetResponse(request, restResponseChannel);
        assertNotNull("There should be a response", channel);
        Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
        assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
        assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) channel.getSize());
        channel.readInto(asyncWritableChannel, null).get();
        assertEquals("Accounts do not match", new HashSet<>(expectedAccounts), new HashSet<>(AccountCollectionSerde.accountsFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream())));
    };
    testAction.accept(createRestRequest(null, null, null, Operations.ACCOUNTS), accountService.getAllAccounts());
    testAction.accept(createRestRequest(account.getName(), null, null, Operations.ACCOUNTS), Collections.singleton(account));
    testAction.accept(createRestRequest(null, Short.toString(account.getId()), null, Operations.ACCOUNTS), Collections.singleton(account));
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FutureResult(com.github.ambry.router.FutureResult) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) HashSet(java.util.HashSet) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) RestMethod(com.github.ambry.rest.RestMethod) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) Account(com.github.ambry.account.Account) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Collection(java.util.Collection) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with RetainingAsyncWritableChannel

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

the class NettyMultipartRequestTest method doMultipartDecodeTest.

// multipartRequestDecodeTest() helpers.
/**
 * Does a multipart decode test.
 * 1. Creates a {@link NettyMultipartRequest}.
 * 2. Adds the {@link HttpRequest} and {@link HttpContent} generated by encoding the {@code files} as a multipart
 * request to the {@link NettyMultipartRequest}.
 * 3. Reads data from the {@link NettyMultipartRequest} via read operations and
 * {@link NettyMultipartRequest#getArgs()} and verifies them against the source data ({@code files}).
 * @param expectedRequestSize the value expected on a call to {@link NettyMultipartRequest#getSize()}.
 * @param files the {@link InMemoryFile}s that form the parts of the multipart request.
 * @param digestAlgorithm the digest algorithm to use. Can be empty or {@code null} if digest checking is not
 *                        required.
 * @throws Exception
 */
private void doMultipartDecodeTest(int expectedRequestSize, InMemoryFile[] files, String digestAlgorithm) throws Exception {
    HttpHeaders httpHeaders = new DefaultHttpHeaders();
    httpHeaders.set(RestUtils.Headers.BLOB_SIZE, expectedRequestSize);
    NettyMultipartRequest request = createRequest(httpHeaders, files);
    assertEquals("Request size does not match", expectedRequestSize, request.getSize());
    request.prepare();
    RetainingAsyncWritableChannel asyncWritableChannel;
    byte[] readOutput;
    Map<String, Object> args = request.getArgs();
    ByteBuffer blobData = ByteBuffer.allocate(0);
    byte[] wholeDigest = null;
    if (files != null) {
        for (InMemoryFile file : files) {
            if (file.name.equals(RestUtils.MultipartPost.BLOB_PART)) {
                blobData = file.content;
                if (digestAlgorithm != null && !digestAlgorithm.isEmpty()) {
                    MessageDigest digest = MessageDigest.getInstance(digestAlgorithm);
                    digest.update(blobData);
                    wholeDigest = digest.digest();
                    blobData.rewind();
                    request.setDigestAlgorithm(digestAlgorithm);
                }
            } else {
                Object value = args.get(file.name);
                assertNotNull("Request does not contain " + file, value);
                assertTrue("Argument value is not ByteBuffer", value instanceof ByteBuffer);
                readOutput = new byte[((ByteBuffer) value).remaining()];
                ((ByteBuffer) value).get(readOutput);
                assertArrayEquals(file.name + " content does not match", file.content.array(), readOutput);
            }
        }
    }
    asyncWritableChannel = new RetainingAsyncWritableChannel(expectedRequestSize);
    request.readInto(asyncWritableChannel, null).get();
    try (InputStream is = asyncWritableChannel.consumeContentAsInputStream()) {
        readOutput = Utils.readBytesFromStream(is, (int) asyncWritableChannel.getBytesWritten());
    }
    assertArrayEquals(RestUtils.MultipartPost.BLOB_PART + " content does not match", blobData.array(), readOutput);
    assertArrayEquals("Part by part digest should match digest of whole", wholeDigest, request.getDigest());
    closeRequestAndValidate(request);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) InputStream(java.io.InputStream) MessageDigest(java.security.MessageDigest) ByteBuffer(java.nio.ByteBuffer)

Example 8 with RetainingAsyncWritableChannel

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

the class NonBlockingRouterQuotaCallbackTest method testRouterWithDefaultQuotaCallback.

/**
 * Test default {@link QuotaChargeCallback} doesn't charge anything and doesn't error out when throttling is disabled.
 */
@Test
public void testRouterWithDefaultQuotaCallback() throws Exception {
    try {
        setRouter();
        assertExpectedThreadCounts(2, 1);
        AtomicInteger listenerCalledCount = new AtomicInteger(0);
        QuotaConfig quotaConfig = new QuotaConfig(new VerifiableProperties(new Properties()));
        QuotaManager quotaManager = new ChargeTesterQuotaManager(quotaConfig, new SimpleQuotaRecommendationMergePolicy(quotaConfig), accountService, null, new QuotaMetrics(new MetricRegistry()), listenerCalledCount);
        QuotaChargeCallback quotaChargeCallback = QuotaUtils.buildQuotaChargeCallback(null, quotaManager, true);
        int blobSize = 3000;
        setOperationParams(blobSize, TTL_SECS);
        String compositeBlobId = router.putBlob(putBlobProperties, putUserMetadata, putChannel, PutBlobOptions.DEFAULT, null, quotaChargeCallback).get();
        assertEquals(0, listenerCalledCount.get());
        RetainingAsyncWritableChannel retainingAsyncWritableChannel = new RetainingAsyncWritableChannel();
        router.getBlob(compositeBlobId, new GetBlobOptionsBuilder().build(), null, quotaChargeCallback).get().getBlobDataChannel().readInto(retainingAsyncWritableChannel, null).get();
        // read out all the chunks.
        retainingAsyncWritableChannel.consumeContentAsInputStream().close();
        assertEquals(0, listenerCalledCount.get());
    } finally {
        router.close();
        assertExpectedThreadCounts(0, 0);
        // submission after closing should return a future that is already done.
        assertClosed();
    }
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) QuotaMetrics(com.github.ambry.quota.QuotaMetrics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QuotaConfig(com.github.ambry.config.QuotaConfig) SimpleQuotaRecommendationMergePolicy(com.github.ambry.quota.SimpleQuotaRecommendationMergePolicy) QuotaChargeCallback(com.github.ambry.quota.QuotaChargeCallback) QuotaManager(com.github.ambry.quota.QuotaManager) AmbryQuotaManager(com.github.ambry.quota.AmbryQuotaManager) Test(org.junit.Test)

Example 9 with RetainingAsyncWritableChannel

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

the class ServerTestUtil method checkBlobId.

private static void checkBlobId(Router router, BlobId blobId, byte[] data) throws Exception {
    GetBlobResult result = router.getBlob(blobId.getID(), new GetBlobOptionsBuilder().build()).get(20, TimeUnit.SECONDS);
    ReadableStreamChannel blob = result.getBlobDataChannel();
    assertEquals("Size does not match that of data", data.length, result.getBlobInfo().getBlobProperties().getBlobSize());
    RetainingAsyncWritableChannel channel = new RetainingAsyncWritableChannel();
    blob.readInto(channel, null).get(1, TimeUnit.SECONDS);
    try (InputStream is = channel.consumeContentAsInputStream()) {
        assertArrayEquals(data, Utils.readBytesFromStream(is, (int) channel.getBytesWritten()));
    }
}
Also used : GetBlobOptionsBuilder(com.github.ambry.router.GetBlobOptionsBuilder) GetBlobResult(com.github.ambry.router.GetBlobResult) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) NettyByteBufDataInputStream(com.github.ambry.utils.NettyByteBufDataInputStream) PutMessageFormatInputStream(com.github.ambry.messageformat.PutMessageFormatInputStream) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) DataInputStream(java.io.DataInputStream) CrcInputStream(com.github.ambry.utils.CrcInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Aggregations

RetainingAsyncWritableChannel (com.github.ambry.commons.RetainingAsyncWritableChannel)9 MetricRegistry (com.codahale.metrics.MetricRegistry)5 Test (org.junit.Test)5 RestRequest (com.github.ambry.rest.RestRequest)4 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)4 TestUtils (com.github.ambry.utils.TestUtils)4 Account (com.github.ambry.account.Account)3 AccountCollectionSerde (com.github.ambry.account.AccountCollectionSerde)3 Container (com.github.ambry.account.Container)3 InMemAccountService (com.github.ambry.account.InMemAccountService)3 VerifiableProperties (com.github.ambry.config.VerifiableProperties)3 MockRestRequest (com.github.ambry.rest.MockRestRequest)3 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)3 RequestPath (com.github.ambry.rest.RequestPath)3 RestMethod (com.github.ambry.rest.RestMethod)3 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)3 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)3 RestServiceException (com.github.ambry.rest.RestServiceException)3 RestUtils (com.github.ambry.rest.RestUtils)3 FutureResult (com.github.ambry.router.FutureResult)3