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