Search in sources :

Example 1 with ReadableStreamChannelInputStream

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

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

the class MockRouter method putBlob.

@Override
public Future<String> putBlob(BlobProperties blobProperties, byte[] userMetadata, ReadableStreamChannel channel, PutBlobOptions options, Callback<String> callback, QuotaChargeCallback quotaChargeCallback) {
    lock.lock();
    try {
        FutureResult<String> future = new FutureResult<>();
        long size = channel.getSize();
        try {
            InputStream input = new ReadableStreamChannelInputStream(channel);
            byte[] bytes = Utils.readBytesFromStream(input, (int) size);
            BlobInfoAndData blob = new BlobInfoAndData(new BlobInfo(blobProperties, userMetadata), bytes);
            String id;
            do {
                id = TestUtils.getRandomString(10);
            } while (allBlobs.putIfAbsent(id, blob) != null);
            future.done(id, null);
            if (callback != null) {
                callback.onCompletion(id, null);
            }
            return future;
        } catch (Exception e) {
            logger.error("Failed to put blob", e);
            future.done(null, e);
            if (callback != null) {
                callback.onCompletion(null, e);
            }
            return future;
        }
    } finally {
        lock.unlock();
    }
}
Also used : FutureResult(com.github.ambry.router.FutureResult) ReadableStreamChannelInputStream(com.github.ambry.commons.ReadableStreamChannelInputStream) InputStream(java.io.InputStream) ReadableStreamChannelInputStream(com.github.ambry.commons.ReadableStreamChannelInputStream) BlobInfo(com.github.ambry.messageformat.BlobInfo) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException)

Aggregations

ReadableStreamChannelInputStream (com.github.ambry.commons.ReadableStreamChannelInputStream)2 InputStream (java.io.InputStream)2 BlobInfo (com.github.ambry.messageformat.BlobInfo)1 FutureResult (com.github.ambry.router.FutureResult)1 GetBlobOptionsBuilder (com.github.ambry.router.GetBlobOptionsBuilder)1 GetBlobResult (com.github.ambry.router.GetBlobResult)1 RouterException (com.github.ambry.router.RouterException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1