Search in sources :

Example 1 with GetBlobResult

use of com.github.ambry.router.GetBlobResult in project ambry by linkedin.

the class PerfRouter method getBlob.

@Override
public Future<GetBlobResult> getBlob(String blobId, GetBlobOptions options, Callback<GetBlobResult> callback) {
    logger.trace("Received getBlob call");
    FutureResult<GetBlobResult> futureResult = new FutureResult<>();
    if (!routerOpen) {
        completeOperation(futureResult, callback, null, ROUTER_CLOSED_EXCEPTION);
    } else {
        GetBlobResult result = null;
        switch(options.getOperationType()) {
            case All:
                result = new GetBlobResult(new BlobInfo(blobProperties, usermetadata), new PerfRSC(chunk, blobProperties.getBlobSize()));
                break;
            case Data:
                result = new GetBlobResult(null, new PerfRSC(chunk, blobProperties.getBlobSize()));
                break;
            case BlobInfo:
                result = new GetBlobResult(new BlobInfo(blobProperties, usermetadata), null);
                break;
        }
        completeOperation(futureResult, callback, result, null);
    }
    return futureResult;
}
Also used : GetBlobResult(com.github.ambry.router.GetBlobResult) FutureResult(com.github.ambry.router.FutureResult) BlobInfo(com.github.ambry.messageformat.BlobInfo)

Example 2 with GetBlobResult

use of com.github.ambry.router.GetBlobResult in project ambry by linkedin.

the class TtlUpdateHandlerTest method assertTtl.

/**
 * Verifies that the TTL is {@code expectedTtlSecs}
 * @param expectedTtlSecs the expected TTL (in secs) of the blob
 * @throws Exception
 */
private void assertTtl(long expectedTtlSecs) throws Exception {
    GetBlobResult result = router.getBlob(blobId, new GetBlobOptionsBuilder().build()).get(1, TimeUnit.SECONDS);
    assertEquals("TTL not as expected", expectedTtlSecs, result.getBlobInfo().getBlobProperties().getTimeToLiveInSeconds());
}
Also used : GetBlobOptionsBuilder(com.github.ambry.router.GetBlobOptionsBuilder) GetBlobResult(com.github.ambry.router.GetBlobResult)

Example 3 with GetBlobResult

use of com.github.ambry.router.GetBlobResult in project ambry by linkedin.

the class RouterStore method readAccountMetadataFromBlobID.

/**
 * Fetch the {@link Account} metadata from the given blob id.
 * @param blobID The blobID to fetch {@link Account} metadata from.
 * @return {@link Account} metadata in a map, and null when there is any error.
 */
Map<String, String> readAccountMetadataFromBlobID(String blobID) {
    long startTimeMs = System.currentTimeMillis();
    Future<GetBlobResult> resultF = router.get().getBlob(blobID, new GetBlobOptionsBuilder().build());
    try {
        GetBlobResult result = resultF.get();
        accountServiceMetrics.accountFetchFromAmbryTimeInMs.update(System.currentTimeMillis() - startTimeMs);
        int blobSize = (int) result.getBlobInfo().getBlobProperties().getBlobSize();
        InputStream input = new ReadableStreamChannelInputStream(result.getBlobDataChannel());
        byte[] bytes = Utils.readBytesFromStream(input, blobSize);
        JSONObject object = new JSONObject(new String(bytes, Charsets.UTF_8));
        Map<String, String> map = new HashMap<>();
        object.keySet().forEach(key -> map.put(key, object.getString(key)));
        return map;
    } catch (Exception e) {
        logger.error("Failed to read account metadata from blob id={}", blobID, e);
        accountServiceMetrics.accountFetchFromAmbryServerErrorCount.inc();
    }
    return null;
}
Also used : GetBlobOptionsBuilder(com.github.ambry.router.GetBlobOptionsBuilder) GetBlobResult(com.github.ambry.router.GetBlobResult) HashMap(java.util.HashMap) ReadableStreamChannelInputStream(com.github.ambry.commons.ReadableStreamChannelInputStream) InputStream(java.io.InputStream) ReadableStreamChannelInputStream(com.github.ambry.commons.ReadableStreamChannelInputStream) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 4 with GetBlobResult

use of com.github.ambry.router.GetBlobResult in project ambry by linkedin.

the class PerfRouter method getBlob.

@Override
public Future<GetBlobResult> getBlob(String blobId, GetBlobOptions options, Callback<GetBlobResult> callback, QuotaChargeCallback quotaChargeCallback) {
    logger.trace("Received getBlob call");
    FutureResult<GetBlobResult> futureResult = new FutureResult<>();
    if (!routerOpen) {
        completeOperation(futureResult, callback, null, ROUTER_CLOSED_EXCEPTION);
    } else {
        GetBlobResult result = null;
        switch(options.getOperationType()) {
            case All:
                result = new GetBlobResult(new BlobInfo(blobProperties, usermetadata), new PerfRSC(chunk, blobProperties.getBlobSize()));
                break;
            case Data:
                result = new GetBlobResult(null, new PerfRSC(chunk, blobProperties.getBlobSize()));
                break;
            case BlobInfo:
                result = new GetBlobResult(new BlobInfo(blobProperties, usermetadata), null);
                break;
        }
        completeOperation(futureResult, callback, result, null);
    }
    return futureResult;
}
Also used : GetBlobResult(com.github.ambry.router.GetBlobResult) FutureResult(com.github.ambry.router.FutureResult) BlobInfo(com.github.ambry.messageformat.BlobInfo)

Example 5 with GetBlobResult

use of com.github.ambry.router.GetBlobResult 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)

Aggregations

GetBlobResult (com.github.ambry.router.GetBlobResult)8 GetBlobOptionsBuilder (com.github.ambry.router.GetBlobOptionsBuilder)5 FutureResult (com.github.ambry.router.FutureResult)4 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)2 BlobInfo (com.github.ambry.messageformat.BlobInfo)2 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)2 RouterException (com.github.ambry.router.RouterException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 BlobId (com.github.ambry.commons.BlobId)1 ReadableStreamChannelInputStream (com.github.ambry.commons.ReadableStreamChannelInputStream)1 RetainingAsyncWritableChannel (com.github.ambry.commons.RetainingAsyncWritableChannel)1 PutMessageFormatInputStream (com.github.ambry.messageformat.PutMessageFormatInputStream)1 ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)1 CrcInputStream (com.github.ambry.utils.CrcInputStream)1 NettyByteBufDataInputStream (com.github.ambry.utils.NettyByteBufDataInputStream)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1