-
Notifications
You must be signed in to change notification settings - Fork 450
RATIS-2625. Streaming: support sending commands in Data Stream #1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
ab19857
2cac57a
14fc6c3
eea3b1b
00fd13b
c2a7221
ffad563
29d0df7
51d0d14
3fcff8d
2b80b08
1b69a7a
d417115
02ee091
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,18 @@ private CompletableFuture<DataStreamReply> writeAsyncImpl(Object data, long leng | |
| return f; | ||
| } | ||
|
|
||
| private CompletableFuture<DataStreamReply> commandAsyncImpl(Object data, long length) { | ||
| if (isClosed()) { | ||
| if (data instanceof ByteBuf) { | ||
| ((ByteBuf) data).release(); | ||
| } | ||
| return JavaUtils.completeExceptionally(new AlreadyClosedException( | ||
| clientId + ": stream already closed, request=" + header)); | ||
| } | ||
| return combineHeader(send(Type.STREAM_COMMAND, data, length, | ||
| Collections.singleton(StandardWriteOption.FLUSH))); | ||
| } | ||
|
|
||
| public CompletableFuture<DataStreamReply> writeAsync(ByteBuf src, Iterable<WriteOption> options) { | ||
| return writeAsyncImpl(src, src.readableBytes(), options); | ||
| } | ||
|
|
@@ -187,6 +199,15 @@ public CompletableFuture<DataStreamReply> writeAsync(FilePositionCount src, Writ | |
| return writeAsyncImpl(src, src.getCount(), Arrays.asList(options)); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<DataStreamReply> commandAsync(ByteBuffer src) { | ||
| return commandAsyncImpl(src, src.remaining()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. commandAsyncImpl is used just once. Let's inline the code. public CompletableFuture<DataStreamReply> commandAsync(ByteBuffer src) {
if (isClosed()) {
return JavaUtils.completeExceptionally(new AlreadyClosedException(
clientId + ": stream already closed, request=" + header));
}
return combineHeader(send(Type.STREAM_COMMAND, src, src.remaining(),
Collections.singleton(StandardWriteOption.FLUSH)));
} |
||
| } | ||
|
|
||
| public CompletableFuture<DataStreamReply> commandAsync(ByteBuf src) { | ||
| return commandAsyncImpl(src, src.readableBytes()); | ||
| } | ||
|
|
||
| boolean isClosed() { | ||
| return closeFuture != null; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,20 +89,30 @@ public class DataStreamManagement { | |
| static class LocalStream { | ||
| private final CompletableFuture<DataStream> streamFuture; | ||
| private final AtomicReference<CompletableFuture<Long>> writeFuture; | ||
| private final RequestMetrics metrics; | ||
| private final RequestMetrics writeMetrics; | ||
| private final RequestMetrics commandMetrics; | ||
|
|
||
| LocalStream(CompletableFuture<DataStream> streamFuture, RequestMetrics metrics) { | ||
| LocalStream(CompletableFuture<DataStream> streamFuture, RequestMetrics writeMetrics, | ||
| RequestMetrics commandMetrics) { | ||
| this.streamFuture = streamFuture; | ||
| this.writeFuture = new AtomicReference<>(streamFuture.thenApply(s -> 0L)); | ||
| this.metrics = metrics; | ||
| this.writeMetrics = writeMetrics; | ||
| this.commandMetrics = commandMetrics; | ||
| } | ||
|
|
||
| CompletableFuture<Long> write(ByteBuf buf, Iterable<WriteOption> options, | ||
| Executor executor) { | ||
| final Timekeeper.Context context = metrics.start(); | ||
| final Timekeeper.Context context = writeMetrics.start(); | ||
| return composeAsync(writeFuture, executor, | ||
| n -> streamFuture.thenCompose(stream -> writeToAsync(buf, options, stream, executor) | ||
| .whenComplete((l, e) -> metrics.stop(context, e == null)))); | ||
| .whenComplete((l, e) -> writeMetrics.stop(context, e == null)))); | ||
| } | ||
|
|
||
| CompletableFuture<Long> command(ByteBuf buf, long streamOffset, Executor executor) { | ||
| final Timekeeper.Context context = commandMetrics.start(); | ||
| return composeAsync(writeFuture, executor, | ||
| n -> streamFuture.thenCompose(stream -> commandToAsync(buf, streamOffset, stream, executor) | ||
| .whenComplete((l, e) -> commandMetrics.stop(context, e == null)))); | ||
| } | ||
|
|
||
| void cleanUp() { | ||
|
|
@@ -114,10 +124,12 @@ static class RemoteStream { | |
| private final DataStreamOutputImpl out; | ||
| private final AtomicReference<CompletableFuture<DataStreamReply>> sendFuture | ||
| = new AtomicReference<>(CompletableFuture.completedFuture(null)); | ||
| private final RequestMetrics metrics; | ||
| private final RequestMetrics writeMetrics; | ||
| private final RequestMetrics commandMetrics; | ||
|
|
||
| RemoteStream(DataStreamOutputImpl out, RequestMetrics metrics) { | ||
| this.metrics = metrics; | ||
| RemoteStream(DataStreamOutputImpl out, RequestMetrics writeMetrics, RequestMetrics commandMetrics) { | ||
| this.writeMetrics = writeMetrics; | ||
| this.commandMetrics = commandMetrics; | ||
| this.out = out; | ||
| } | ||
|
|
||
|
|
@@ -130,10 +142,17 @@ static Iterable<WriteOption> addFlush(List<WriteOption> original) { | |
| } | ||
|
|
||
| CompletableFuture<DataStreamReply> write(DataStreamRequestByteBuf request, Executor executor) { | ||
| final Timekeeper.Context context = metrics.start(); | ||
| final Timekeeper.Context context = writeMetrics.start(); | ||
| return composeAsync(sendFuture, executor, | ||
| n -> out.writeAsync(request.slice().retain(), addFlush(request.getWriteOptionList())) | ||
| .whenComplete((l, e) -> metrics.stop(context, e == null))); | ||
| .whenComplete((l, e) -> writeMetrics.stop(context, e == null))); | ||
| } | ||
|
|
||
| CompletableFuture<DataStreamReply> command(DataStreamRequestByteBuf request, Executor executor) { | ||
| final Timekeeper.Context context = commandMetrics.start(); | ||
| return composeAsync(sendFuture, executor, | ||
| n -> out.commandAsync(request.slice().retain()) | ||
| .whenComplete((l, e) -> commandMetrics.stop(context, e == null))); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -152,12 +171,16 @@ static class StreamInfo { | |
| throws IOException { | ||
| this.request = request; | ||
| this.primary = primary; | ||
| this.local = new LocalStream(stream, metricsConstructor.apply(RequestType.LOCAL_WRITE)); | ||
| this.local = new LocalStream(stream, | ||
| metricsConstructor.apply(RequestType.LOCAL_WRITE), | ||
| metricsConstructor.apply(RequestType.LOCAL_COMMAND)); | ||
| this.division = division; | ||
| final Set<RaftPeer> successors = getSuccessors(division.getId()); | ||
| final Set<DataStreamOutputImpl> outs = getStreams.apply(request, successors); | ||
| this.remotes = outs.stream() | ||
| .map(o -> new RemoteStream(o, metricsConstructor.apply(RequestType.REMOTE_WRITE))) | ||
| .map(o -> new RemoteStream(o, | ||
| metricsConstructor.apply(RequestType.REMOTE_WRITE), | ||
| metricsConstructor.apply(RequestType.REMOTE_COMMAND))) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
|
|
@@ -303,6 +326,24 @@ static CompletableFuture<Long> writeToAsync(ByteBuf buf, | |
| return CompletableFuture.supplyAsync(() -> writeTo(buf, options, stream), e); | ||
| } | ||
|
|
||
| static CompletableFuture<Long> commandToAsync(ByteBuf buf, long streamOffset, DataStream stream, | ||
| Executor defaultExecutor) { | ||
| final Executor e = Optional.ofNullable(stream.getExecutor()).orElse(defaultExecutor); | ||
| final ByteBuffer command = copyBuffer(buf); | ||
| return CompletableFuture.runAsync(() -> {}, e) | ||
| .thenCompose(v -> stream.onCommand(command, streamOffset)) | ||
| .thenApply(v -> 0L); | ||
| } | ||
|
|
||
| static ByteBuffer copyBuffer(ByteBuf buf) { | ||
| final ByteBuffer copy = ByteBuffer.allocate(buf.readableBytes()); | ||
| for (ByteBuffer buffer : buf.nioBuffers()) { | ||
| copy.put(buffer); | ||
| } | ||
| copy.flip(); | ||
| return copy; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make it readonly return copy.asReadOnlyBuffer(); |
||
| } | ||
|
|
||
| static long writeTo(ByteBuf buf, Iterable<WriteOption> options, | ||
| DataStream stream) { | ||
| final DataChannel channel = stream.getDataChannel(); | ||
|
|
@@ -477,6 +518,9 @@ private void readImpl(DataStreamRequestByteBuf request, ChannelHandlerContext ct | |
| } else if (request.getType() == Type.STREAM_DATA) { | ||
| localWrite = info.getLocal().write(request.slice(), request.getWriteOptionList(), writeExecutor); | ||
| remoteWrites = info.applyToRemotes(out -> out.write(request, requestExecutor)); | ||
| } else if (request.getType() == Type.STREAM_COMMAND) { | ||
| localWrite = info.getLocal().command(request.slice(), request.getStreamOffset(), writeExecutor); | ||
| remoteWrites = info.applyToRemotes(out -> out.command(request, requestExecutor)); | ||
| } else { | ||
| throw new IllegalStateException(this + ": Unexpected type " + request.getType() + ", request=" + request); | ||
| } | ||
|
|
@@ -485,6 +529,7 @@ private void readImpl(DataStreamRequestByteBuf request, ChannelHandlerContext ct | |
| .thenCombineAsync(localWrite, (v, bytesWritten) -> { | ||
| if (request.getType() == Type.STREAM_HEADER | ||
| || request.getType() == Type.STREAM_DATA | ||
| || request.getType() == Type.STREAM_COMMAND | ||
| || close) { | ||
| sendReply(remoteWrites, request, bytesWritten, info.getCommitInfos(), ctx); | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -385,6 +385,19 @@ interface DataStream { | |
| default Executor getExecutor() { | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Handle a command received in the middle of a data stream. | ||
| * The {@code streamOffset} indicates the current byte offset in the stream | ||
| * (i.e. the total number of data bytes received so far). | ||
| * | ||
| * @param command the command payload | ||
| * @param streamOffset the current stream byte offset | ||
| * @return a future for the command task | ||
| */ | ||
| default CompletableFuture<?> onCommand(ByteBuffer command, long streamOffset) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a commnad, it should return CompletableFuture<ByteBuffer>. |
||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen on the client side if it issues two commands without data in between? As far as I can see, NettyClientReply, when it maps requests, considers only the stream offset and type, so the second command with the same offset wouldn't have a ReplyEntry.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good call. It seems to me that the first command's
ReplyEntrywill be used for the second command and the second command'sRequestEntrywon't be in the map. Anyway it looks like a mess without a handling.So I instead fail the commands if there is already one at the same stream offset. After all, we do not expect the caller issue multiple commands at the same stream offset for now.