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