Repository navigation
perf: build each write request once and replay it as is - #433
Merged
Merged
Conversation
ManagedWriteStream took a Supplier<WriteRequest> and called it on the first send and again for every in-flight write replayed after a reconnect, so WriteBatch rebuilt its whole request each time. The supplier came from oxia-db#310: LightProto 0.6.6 serialized bytes fields with writeBytes(), which consumed the value buffer, so a replayed put went out with an empty value. LightProto 0.7.1 serializes them with absolute getBytes() and the client is on 0.9.0, so a request can be serialized more than once. Pass the built WriteRequest to send(), keep it in the in-flight entry, and send that same object on replay. replaysInflightWritesAfterRetryableError now replays the same request objects and checks their values at the new leader; with LightProto pinned to 0.7.0 it fails. Signed-off-by: Matteo Merli <mmerli@apache.org>
…-once Signed-off-by: Matteo Merli <mmerli@apache.org>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
For #412 (the "Each send rebuilds the whole
WriteRequest" item).Problem
WriteBatchhandsManagedWriteStreamaSupplier<WriteRequest>instead of a built request. The stream calls it on the first send, and again for every in-flight write each time it reconnects. When a shard's leader moves, every reconnect attempt rebuilds every in-flight batch's full request (one message per operation, plus a buffer wrapper per value) while the client is recovering. Each send also allocates the bound method reference.Example
With 8 batches in flight and 3 reconnect attempts before the new leader answers,
toProto()runs 32 times for 8 requests.The supplier came from #310. LightProto 0.6.6 wrote bytes fields with
_b.writeBytes(value), which advanced the value buffer's reader index, so a replayed put sent an empty value. LightProto 0.7.1 (streamnative/lightproto#6) writes them withvalue.getBytes(value.readerIndex(), ...), which leaves the buffer as it was, and the client is on 0.9.0.Change
send()takes the built request, the in-flight entry keeps it, and replay sends that same object:WriteBatchcallswriteStream.send(toProto()). The generated gRPC marshaller serializes insidestream(), andsend()andinitWithRecovery()both run under the stream lock, so a request is never serialized twice at the same time.The request now stays reachable until its response instead of becoming garbage right after the send. It wraps the operations' key strings and value arrays without copying them, so the extra memory is only the message objects.
Testing
ManagedWriteStreamTestnow passes requests directly.replaysInflightWritesAfterRetryableErrorsends each request object to a stale leader and then replays the same object to the new leader, and checks the values that arrive. With LightProto pinned to 0.7.0 it fails (["???????", "???????"]instead of["value-1", "value-2"]); on 0.9.0 it passes../gradlew :client:test: all 402 tests pass locally, including the Docker integration tests (ClientReconnectIT,OxiaClientIT, ...).