-
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ab19857
update
amaliujia 2cac57a
fix
amaliujia 14fc6c3
update
amaliujia eea3b1b
update
amaliujia 00fd13b
update
amaliujia c2a7221
update
amaliujia ffad563
remove filestore change
amaliujia 29d0df7
update
amaliujia 51d0d14
address review comments
amaliujia 3fcff8d
update
amaliujia 2b80b08
address comments
1b69a7a
fix findsBug
d417115
address comments
amaliujia 02ee091
Make one call to the map
amaliujia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ | |
| import org.apache.ratis.util.MemoizedSupplier; | ||
| import org.apache.ratis.util.Preconditions; | ||
| import org.apache.ratis.util.ReferenceCountedObject; | ||
| import org.apache.ratis.util.StringUtils; | ||
| import org.apache.ratis.util.TimeDuration; | ||
| import org.apache.ratis.util.TimeoutExecutor; | ||
| import org.apache.ratis.util.UncheckedAutoCloseable; | ||
|
|
@@ -86,23 +87,70 @@ | |
| public class DataStreamManagement { | ||
| public static final Logger LOG = LoggerFactory.getLogger(DataStreamManagement.class); | ||
|
|
||
| static final class LocalResult { | ||
| static final LocalResult ZERO = of(0); | ||
|
|
||
| static LocalResult of(long byteWritten) { | ||
| return new LocalResult(byteWritten, null); | ||
| } | ||
|
|
||
| static LocalResult of(ByteBuffer reply) { | ||
| return new LocalResult(0, reply); | ||
| } | ||
|
|
||
| /** For {@link Type#STREAM_DATA}. */ | ||
| private final long byteWritten; | ||
| /** For {@link Type#STREAM_COMMAND}. */ | ||
| private final ByteBuffer commandReply; | ||
|
|
||
| private LocalResult(long byteWritten, ByteBuffer commandReply) { | ||
| this.byteWritten = byteWritten; | ||
| this.commandReply = commandReply; | ||
| } | ||
|
|
||
| long getByteWritten() { | ||
| return byteWritten; | ||
| } | ||
|
|
||
| ByteBuffer getCommandReply() { | ||
| return commandReply; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return commandReply != null ? "commandReply:" + StringUtils.bytes2HexString(commandReply) | ||
| : "byteWritten:" + byteWritten; | ||
| } | ||
| } | ||
|
|
||
| static class LocalStream { | ||
| private final CompletableFuture<DataStream> streamFuture; | ||
| private final AtomicReference<CompletableFuture<Long>> writeFuture; | ||
| private final RequestMetrics metrics; | ||
| private final AtomicReference<CompletableFuture<LocalResult>> resultFuture; | ||
| 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.resultFuture = new AtomicReference<>(streamFuture.thenApply(s -> LocalResult.ZERO)); | ||
| this.writeMetrics = writeMetrics; | ||
| this.commandMetrics = commandMetrics; | ||
| } | ||
|
|
||
| CompletableFuture<Long> write(ByteBuf buf, Iterable<WriteOption> options, | ||
| Executor executor) { | ||
| final Timekeeper.Context context = metrics.start(); | ||
| return composeAsync(writeFuture, executor, | ||
| CompletableFuture<LocalResult> write(ByteBuf buf, Iterable<WriteOption> options, Executor executor) { | ||
| final Timekeeper.Context context = writeMetrics.start(); | ||
| return composeAsync(resultFuture, 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))) | ||
| .thenApply(LocalResult::of)); | ||
| } | ||
|
|
||
| CompletableFuture<LocalResult> command(ByteBuffer command, long streamOffset, Executor executor) { | ||
| final Timekeeper.Context context = commandMetrics.start(); | ||
| return composeAsync(resultFuture, executor, | ||
| n -> streamFuture.thenCompose(stream -> commandToAsync(command, streamOffset, stream, executor) | ||
| .whenComplete((l, e) -> commandMetrics.stop(context, e == null))) | ||
| .thenApply(LocalResult::of)); | ||
| } | ||
|
|
||
| void cleanUp() { | ||
|
|
@@ -114,10 +162,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 +180,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(ByteBuffer command, Executor executor) { | ||
| final Timekeeper.Context context = commandMetrics.start(); | ||
| return composeAsync(sendFuture, executor, | ||
| n -> out.commandAsync(command) | ||
| .whenComplete((l, e) -> commandMetrics.stop(context, e == null))); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -152,12 +209,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 +364,21 @@ static CompletableFuture<Long> writeToAsync(ByteBuf buf, | |
| return CompletableFuture.supplyAsync(() -> writeTo(buf, options, stream), e); | ||
| } | ||
|
|
||
| static ByteBuffer copyBuffer(ByteBuf buf) { | ||
| final ByteBuffer copy = ByteBuffer.allocate(buf.readableBytes()); | ||
| for (ByteBuffer buffer : buf.nioBuffers()) { | ||
| copy.put(buffer); | ||
| } | ||
| copy.flip(); | ||
| return copy.asReadOnlyBuffer(); | ||
| } | ||
|
|
||
| static CompletableFuture<ByteBuffer> commandToAsync(ByteBuffer command, long streamOffset, | ||
| DataStream stream, Executor defaultExecutor) { | ||
| return CompletableFuture.completedFuture(null) | ||
| .thenCompose(ignored -> stream.onCommand(command, streamOffset)); | ||
| } | ||
|
|
||
| static long writeTo(ByteBuf buf, Iterable<WriteOption> options, | ||
| DataStream stream) { | ||
| final DataChannel channel = stream.getDataChannel(); | ||
|
|
@@ -357,15 +433,18 @@ static DataStreamReplyByteBuffer newDataStreamReplyByteBuffer(DataStreamRequestB | |
| } | ||
|
|
||
| private void sendReply(List<CompletableFuture<DataStreamReply>> remoteWrites, | ||
| DataStreamRequestByteBuf request, long bytesWritten, Collection<CommitInfoProto> commitInfos, | ||
| DataStreamRequestByteBuf request, LocalResult localResult, Collection<CommitInfoProto> commitInfos, | ||
| ChannelHandlerContext ctx) { | ||
| final boolean success = checkSuccessRemoteWrite(remoteWrites, bytesWritten, request); | ||
| final boolean success = checkSuccessRemoteWrite(remoteWrites, localResult, request); | ||
| final DataStreamReplyByteBuffer.Builder builder = DataStreamReplyByteBuffer.newBuilder() | ||
| .setDataStreamPacket(request) | ||
| .setSuccess(success) | ||
| .setCommitInfos(commitInfos); | ||
| if (success) { | ||
| builder.setBytesWritten(bytesWritten); | ||
| builder.setBytesWritten(localResult.getByteWritten()); | ||
| if (localResult.getCommandReply() != null) { | ||
| builder.setBuffer(localResult.getCommandReply()); | ||
| } | ||
| } | ||
| ctx.writeAndFlush(builder.build()); | ||
| } | ||
|
|
@@ -469,24 +548,30 @@ private void readImpl(DataStreamRequestByteBuf request, ChannelHandlerContext ct | |
| () -> new IllegalStateException("Failed to get StreamInfo for " + request)); | ||
| } | ||
|
|
||
| final CompletableFuture<Long> localWrite; | ||
| final CompletableFuture<LocalResult> localResult; | ||
| final List<CompletableFuture<DataStreamReply>> remoteWrites; | ||
| if (request.getType() == Type.STREAM_HEADER) { | ||
| localWrite = CompletableFuture.completedFuture(0L); | ||
| localResult = CompletableFuture.completedFuture(LocalResult.ZERO); | ||
| remoteWrites = Collections.emptyList(); | ||
| } else if (request.getType() == Type.STREAM_DATA) { | ||
| localWrite = info.getLocal().write(request.slice(), request.getWriteOptionList(), writeExecutor); | ||
| localResult = info.getLocal().write(request.slice(), request.getWriteOptionList(), writeExecutor); | ||
| remoteWrites = info.applyToRemotes(out -> out.write(request, requestExecutor)); | ||
| } else if (request.getType() == Type.STREAM_COMMAND) { | ||
| // command is supposed to have small data size, just copy it | ||
| final ByteBuffer command = copyBuffer(request.slice()); | ||
| localResult = info.getLocal().command(command.duplicate(), request.getStreamOffset(), writeExecutor); | ||
| remoteWrites = info.applyToRemotes(out -> out.command(command, requestExecutor)); | ||
| } else { | ||
| throw new IllegalStateException(this + ": Unexpected type " + request.getType() + ", request=" + request); | ||
| } | ||
|
|
||
| composeAsync(info.getPrevious(), requestExecutor, n -> JavaUtils.allOf(remoteWrites) | ||
| .thenCombineAsync(localWrite, (v, bytesWritten) -> { | ||
| .thenCombineAsync(localResult, (v, local) -> { | ||
| if (request.getType() == Type.STREAM_HEADER | ||
| || request.getType() == Type.STREAM_DATA | ||
| || request.getType() == Type.STREAM_COMMAND | ||
| || close) { | ||
| sendReply(remoteWrites, request, bytesWritten, info.getCommitInfos(), ctx); | ||
| sendReply(remoteWrites, request, local, info.getCommitInfos(), ctx); | ||
| } else { | ||
| throw new IllegalStateException(this + ": Unexpected type " + request.getType() + ", request=" + request); | ||
| } | ||
|
|
@@ -519,30 +604,48 @@ static void assertReplyCorrespondingToRequest( | |
| Preconditions.assertTrue(request.getStreamOffset() == reply.getStreamOffset()); | ||
| } | ||
|
|
||
| private boolean checkSuccessRemoteWrite(List<CompletableFuture<DataStreamReply>> replyFutures, long bytesWritten, | ||
| final DataStreamRequestByteBuf request) { | ||
| private boolean checkSuccessRemoteWrite(List<CompletableFuture<DataStreamReply>> replyFutures, | ||
| LocalResult localResult, final DataStreamRequestByteBuf request) { | ||
| for (CompletableFuture<DataStreamReply> replyFuture : replyFutures) { | ||
| final DataStreamReply reply; | ||
| try { | ||
| reply = replyFuture.get(requestTimeout.getDuration(), requestTimeout.getUnit()); | ||
| } catch (Exception e) { | ||
| throw new CompletionException("Failed to get reply for bytesWritten=" + bytesWritten + ", " + request, e); | ||
| throw new CompletionException("Failed to get reply for " + localResult + ", " + request, e); | ||
|
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. Add toString() @Override
public String toString() {
return commandReply != null ? "commandReply:" + StringUtils.bytes2HexString(commandReply)
: "byteWritten:" + byteWritten;
} |
||
| } | ||
| assertReplyCorrespondingToRequest(request, reply); | ||
| if (!reply.isSuccess()) { | ||
| LOG.warn("reply is not success, request: {}", request); | ||
| return false; | ||
| } | ||
| if (reply.getBytesWritten() != bytesWritten) { | ||
| LOG.warn( | ||
| "reply written bytes not match, local size: {} remote size: {} request: {}", | ||
| bytesWritten, reply.getBytesWritten(), request); | ||
| if (reply.getBytesWritten() != localResult.getByteWritten()) { | ||
| LOG.warn("reply written bytes not match, local size: {} remote size: {} request: {}", | ||
| localResult.getByteWritten(), reply.getBytesWritten(), request); | ||
| return false; | ||
| } | ||
| if (!commandReplyEquals(reply.nioBuffer(), localResult.getCommandReply())) { | ||
| LOG.warn("reply buffer not match, local: {} remote: {} request: {}", | ||
| commandReplyString(localResult.getCommandReply()), | ||
| commandReplyString(reply.nioBuffer()), | ||
| request); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static boolean commandReplyEquals(ByteBuffer a, ByteBuffer b) { | ||
| if (a == null || a.remaining() == 0) { | ||
| return b == null || b.remaining() == 0; | ||
| } | ||
| return a.equals(b); | ||
| } | ||
|
|
||
| private static String commandReplyString(ByteBuffer buffer) { | ||
| return buffer == null || buffer.remaining() == 0 | ||
| ? "null" : StringUtils.bytes2HexString(buffer); | ||
| } | ||
|
|
||
| NettyServerStreamRpcMetrics getMetrics() { | ||
| return nettyServerStreamRpcMetrics; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.