-
Notifications
You must be signed in to change notification settings - Fork 37
Added support of direct write mode #641
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ae3ac3
Split TopicStream to interface and implementation
alex268 6060bb3
Moved StreamFactory to separate class
alex268 15d4d23
Updated TopicRpc interface and implemetation
alex268 8a0177b
Added support of direct write mode
alex268 5ac9424
Added tests for WriteStreamFactory
alex268 5b5ad03
Added logs to WriteStreamFactory
alex268 df2e727
Fixed by copylot
alex268 bdb9c8c
Revert to use raw GrpcRequestSettings in TopicRpc
alex268 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,15 @@ | ||
| package tech.ydb.topic.impl; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import com.google.protobuf.Message; | ||
| import org.slf4j.Logger; | ||
|
|
||
| import tech.ydb.core.Status; | ||
| import tech.ydb.core.StatusCode; | ||
| import tech.ydb.core.grpc.GrpcReadWriteStream; | ||
|
|
||
| public abstract class TopicStream<R extends Message, W extends Message> { | ||
| private final Logger logger; | ||
| private final String debugId; | ||
| private final GrpcReadWriteStream<R, W> stream; | ||
| private final CompletableFuture<Status> streamStatus = new CompletableFuture<>(); | ||
| private volatile String token; | ||
| public interface TopicStream<R extends Message, W extends Message> { | ||
| CompletableFuture<Status> start(W initReq, Consumer<R> messageHandler); | ||
| void send(W request); | ||
|
|
||
| public TopicStream(Logger logger, String debugId, GrpcReadWriteStream<R, W> stream) { | ||
| this.logger = logger; | ||
| this.debugId = debugId; | ||
| this.stream = stream; | ||
| this.token = stream.authToken(); | ||
| } | ||
|
|
||
| protected abstract W updateTokenMessage(String token); | ||
| protected abstract Status parseMessageStatus(R message); | ||
|
|
||
| public CompletableFuture<Status> start(W initReq, Consumer<R> messageHandler) { | ||
| this.logger.debug("[{}] is about to start", debugId); | ||
| this.stream.start((R msg) -> { | ||
| Status messageStatus = parseMessageStatus(msg); | ||
| if (messageStatus.isSuccess()) { | ||
| messageHandler.accept(msg); | ||
| } else { | ||
| logger.warn("[{}] stopped by getting status {}", debugId, messageStatus); | ||
| if (streamStatus.complete(messageStatus)) { | ||
| stream.close(); | ||
| } | ||
| } | ||
| }).whenComplete((st, th) -> { | ||
| Status status = st != null ? st : Status.of(StatusCode.CLIENT_INTERNAL_ERROR, th); | ||
| logger.debug("[{}] finished with status {}", debugId, status); | ||
| streamStatus.complete(status); | ||
| }); | ||
|
|
||
| if (!streamStatus.isDone()) { | ||
| stream.sendNext(initReq); | ||
| } | ||
|
|
||
| return streamStatus; | ||
| } | ||
|
|
||
| public void close() { | ||
| logger.debug("[{}] closed by app", debugId); | ||
| if (!streamStatus.isDone()) { | ||
| stream.close(); | ||
| } | ||
| } | ||
|
|
||
| public void send(W req) { | ||
| if (streamStatus.isDone()) { | ||
| logger.warn("[{}] is already closed. Next message with type {} was NOT sent", debugId, | ||
| req.getDescriptorForType().getName()); | ||
| return; | ||
| } | ||
|
|
||
| String currentToken = stream.authToken(); | ||
| if (!Objects.equals(token, currentToken)) { | ||
| token = currentToken; | ||
| logger.info("{} sends new token", this); | ||
| stream.sendNext(updateTokenMessage(token)); | ||
| } | ||
|
|
||
| stream.sendNext(req); | ||
| } | ||
| void close(); | ||
| } |
82 changes: 82 additions & 0 deletions
82
topic/src/main/java/tech/ydb/topic/impl/TopicStreamBase.java
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package tech.ydb.topic.impl; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import com.google.protobuf.Message; | ||
| import org.slf4j.Logger; | ||
|
|
||
| import tech.ydb.core.Status; | ||
| import tech.ydb.core.StatusCode; | ||
| import tech.ydb.core.grpc.GrpcReadWriteStream; | ||
|
|
||
| public abstract class TopicStreamBase<R extends Message, W extends Message> implements TopicStream<R, W> { | ||
| private final Logger logger; | ||
| private final String debugId; | ||
| private final GrpcReadWriteStream<R, W> stream; | ||
| private final CompletableFuture<Status> streamStatus = new CompletableFuture<>(); | ||
| private volatile String token; | ||
|
|
||
| public TopicStreamBase(Logger logger, String debugId, GrpcReadWriteStream<R, W> stream) { | ||
| this.logger = logger; | ||
| this.debugId = debugId; | ||
| this.stream = stream; | ||
| this.token = stream.authToken(); | ||
| } | ||
|
|
||
| protected abstract W updateTokenMessage(String token); | ||
| protected abstract Status parseMessageStatus(R message); | ||
|
|
||
| @Override | ||
| public CompletableFuture<Status> start(W initReq, Consumer<R> messageHandler) { | ||
| this.logger.debug("[{}] is about to start", debugId); | ||
| this.stream.start((R msg) -> { | ||
| Status messageStatus = parseMessageStatus(msg); | ||
| if (messageStatus.isSuccess()) { | ||
| messageHandler.accept(msg); | ||
| } else { | ||
| logger.warn("[{}] stopped by getting status {}", debugId, messageStatus); | ||
| if (streamStatus.complete(messageStatus)) { | ||
| stream.close(); | ||
| } | ||
| } | ||
| }).whenComplete((st, th) -> { | ||
| Status status = st != null ? st : Status.of(StatusCode.CLIENT_INTERNAL_ERROR, th); | ||
| logger.debug("[{}] finished with status {}", debugId, status); | ||
| streamStatus.complete(status); | ||
| }); | ||
|
|
||
| if (!streamStatus.isDone()) { | ||
| stream.sendNext(initReq); | ||
| } | ||
|
|
||
| return streamStatus; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| logger.debug("[{}] closed by app", debugId); | ||
| if (!streamStatus.isDone()) { | ||
| stream.close(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void send(W req) { | ||
| if (streamStatus.isDone()) { | ||
| logger.warn("[{}] is already closed. Next message with type {} was NOT sent", debugId, | ||
| req.getDescriptorForType().getName()); | ||
| return; | ||
| } | ||
|
|
||
| String currentToken = stream.authToken(); | ||
| if (!Objects.equals(token, currentToken)) { | ||
| token = currentToken; | ||
| logger.info("{} sends new token", this); | ||
| stream.sendNext(updateTokenMessage(token)); | ||
| } | ||
|
|
||
| stream.sendNext(req); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.