Search in sources :

Example 11 with FutureResult

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

the class ByteBufferReadableStreamChannel method readInto.

@Override
public Future<Long> readInto(AsyncWritableChannel asyncWritableChannel, Callback<Long> callback) {
    Future<Long> future;
    if (!channelOpen.get()) {
        ClosedChannelException closedChannelException = new ClosedChannelException();
        FutureResult<Long> futureResult = new FutureResult<Long>();
        futureResult.done(0L, closedChannelException);
        future = futureResult;
        if (callback != null) {
            callback.onCompletion(0L, closedChannelException);
        }
    } else if (!channelEmptied.compareAndSet(false, true)) {
        throw new IllegalStateException("ReadableStreamChannel cannot be read more than once");
    } else {
        Callback<Long> bufferCallback = callback;
        if (addEmptyChunk) {
            bufferCallback = null;
        }
        future = asyncWritableChannel.write(buffer, bufferCallback);
        if (addEmptyChunk) {
            future = asyncWritableChannel.write(Unpooled.EMPTY_BUFFER, callback);
        }
    }
    return future;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FutureResult(com.github.ambry.router.FutureResult)

Example 12 with FutureResult

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

the class NoSizeRSC method readInto.

/**
 * Either throws the exception provided or returns immediately saying no bytes were read.
 * @param asyncWritableChannel the {@link AsyncWritableChannel} to read the data into.
 * @param callback the {@link Callback} that will be invoked either when all the data in the channel has been emptied
 *                 into the {@code asyncWritableChannel} or if there is an exception in doing so. This can be null.
 * @return a {@link Future} that will eventually contain the result of the operation.
 */
@Override
public Future<Long> readInto(AsyncWritableChannel asyncWritableChannel, Callback<Long> callback) {
    Exception exception;
    if (!channelOpen.get()) {
        exception = new ClosedChannelException();
    } else {
        exception = exceptionToThrow;
    }
    FutureResult<Long> futureResult = new FutureResult<Long>();
    futureResult.done(0L, exception);
    if (callback != null) {
        callback.onCompletion(0L, exception);
    }
    return futureResult;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FutureResult(com.github.ambry.router.FutureResult) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException)

Example 13 with FutureResult

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

the class InputStreamReadableStreamChannel method readInto.

@Override
public Future<Long> readInto(AsyncWritableChannel asyncWritableChannel, Callback<Long> callback) {
    Future<Long> future;
    if (!channelOpen.get()) {
        ClosedChannelException closedChannelException = new ClosedChannelException();
        FutureResult<Long> futureResult = new FutureResult<Long>();
        futureResult.done(0L, closedChannelException);
        future = futureResult;
        if (callback != null) {
            callback.onCompletion(0L, closedChannelException);
        }
    } else if (!readIntoCalled.compareAndSet(false, true)) {
        throw new IllegalStateException("ReadableStreamChannel cannot be read more than once");
    } else {
        callbackWrapper = new ReadIntoCallbackWrapper(callback);
        future = callbackWrapper.futureResult;
        if (size == 0) {
            callbackWrapper.invokeCallback(null);
        } else {
            writeToChannel(asyncWritableChannel);
        }
    }
    return future;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FutureResult(com.github.ambry.router.FutureResult) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 14 with FutureResult

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

the class RetainingAsyncWritableChannel method writeInternal.

/**
 * Internal method for writing to the channel that allows for a pluggable action to be taken to produce a
 * retained {@link ByteBuf} that can be added to the composite buffer.
 * @param retainedBufSupplier creates a retained {@link ByteBuf} that can be freely added to a
 *                            {@link CompositeByteBuf}.
 * @param callback called once the buffer has been added.
 * @return a future that is completed once the buffer has been added.
 */
private Future<Long> writeInternal(Supplier<ByteBuf> retainedBufSupplier, Callback<Long> callback) {
    FutureResult<Long> future = new FutureResult<>();
    ByteBuf buf = null;
    long bytesWritten = 0;
    Exception exception = null;
    try {
        if (!isOpen()) {
            throw new ClosedChannelException();
        } else if (totalBytesWritten.get() > sizeLimitInBytes) {
            throw new RestServiceException("Request is larger than allowed size: " + sizeLimitInBytes, RestServiceErrorCode.RequestTooLarge);
        } else {
            synchronized (bufferLock) {
                if (compositeBuffer == null) {
                    throw new IllegalStateException("Cannot write more data; content already consumed or channel was closed");
                }
                buf = retainedBufSupplier.get();
                bytesWritten = buf.readableBytes();
                compositeBuffer.addComponent(true, buf);
            }
            if (totalBytesWritten.addAndGet(bytesWritten) > sizeLimitInBytes) {
                exception = new RestServiceException("Request is larger than allowed size: " + sizeLimitInBytes, RestServiceErrorCode.RequestTooLarge);
            }
        }
    } catch (Exception e) {
        exception = e;
        if (buf != null) {
            buf.release();
        }
    } finally {
        future.done(bytesWritten, exception);
        if (callback != null) {
            callback.onCompletion(bytesWritten, exception);
        }
    }
    return future;
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) ClosedChannelException(java.nio.channels.ClosedChannelException) FutureResult(com.github.ambry.router.FutureResult) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ClosedChannelException(java.nio.channels.ClosedChannelException) RestServiceException(com.github.ambry.rest.RestServiceException)

Example 15 with FutureResult

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

the class FrontendTestUrlSigningServiceFactory method completeOperation.

/**
 * Completes the operation by creating and invoking a {@link Future} and invoking the {@code callback} if non-null.
 * @param result the result to return.
 * @param callback the {@link Callback} to invoke. Can be null.
 * @param opType the type of operation calling this function.
 * @param <T> the type of future/callback.
 * @return the created {@link Future}.
 */
private <T> Future<T> completeOperation(T result, Callback<T> callback, OpType opType) {
    if (!isOpen) {
        throw new IllegalStateException("Router not open");
    }
    Exception exception = null;
    if (opType == exceptionOpType) {
        if (exceptionToThrow != null) {
            throw new RuntimeException(exceptionToThrow);
        } else if (exceptionToReturn != null) {
            exception = exceptionToReturn;
            result = null;
        }
    }
    FutureResult<T> futureResult = new FutureResult<T>();
    futureResult.done(result, exception);
    if (callback != null) {
        callback.onCompletion(result, exception);
    }
    return futureResult;
}
Also used : FutureResult(com.github.ambry.router.FutureResult) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RestServiceException(com.github.ambry.rest.RestServiceException) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AccountServiceException(com.github.ambry.account.AccountServiceException)

Aggregations

FutureResult (com.github.ambry.router.FutureResult)22 IOException (java.io.IOException)8 RestRequest (com.github.ambry.rest.RestRequest)7 RestServiceException (com.github.ambry.rest.RestServiceException)7 JSONObject (org.json.JSONObject)7 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)6 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)6 RouterException (com.github.ambry.router.RouterException)6 ClosedChannelException (java.nio.channels.ClosedChannelException)6 ExecutionException (java.util.concurrent.ExecutionException)5 GetBlobResult (com.github.ambry.router.GetBlobResult)4 InMemoryRouter (com.github.ambry.router.InMemoryRouter)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URISyntaxException (java.net.URISyntaxException)4 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)3 BlobInfo (com.github.ambry.messageformat.BlobInfo)3 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2