- Notifications
You must be signed in to change notification settings - Fork 24
fix: keep track of internal seek #87
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 all commits
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 |
|---|---|---|
| | @@ -34,6 +34,7 @@ | |
| import com.google.cloud.pubsublite.proto.SubscriberServiceGrpc.SubscriberServiceStub; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.util.concurrent.Monitor; | ||
| import io.grpc.Status; | ||
| import io.grpc.StatusException; | ||
| import java.util.Optional; | ||
| | @@ -56,11 +57,24 @@ public class SubscriberImpl extends ProxyService | |
| private final TokenCounter tokenCounter = new TokenCounter(); | ||
| | ||
| @GuardedBy("monitor.monitor") | ||
| private Optional<SettableApiFuture<Offset>> inFlightSeek = Optional.empty(); | ||
| private Optional<InFlightSeek> inFlightSeek = Optional.empty(); | ||
| | ||
| @GuardedBy("monitor.monitor") | ||
| private boolean internalSeekInFlight = false; | ||
| | ||
| @GuardedBy("monitor.monitor") | ||
| private boolean shutdown = false; | ||
| | ||
| private static class InFlightSeek { | ||
| final SeekRequest seekRequest; | ||
| final SettableApiFuture<Offset> seekFuture; | ||
| | ||
| InFlightSeek(SeekRequest request, SettableApiFuture<Offset> future) { | ||
| seekRequest = request; | ||
| seekFuture = future; | ||
| } | ||
| } | ||
| | ||
| @VisibleForTesting | ||
| SubscriberImpl( | ||
| SubscriberServiceStub stub, | ||
| | @@ -91,7 +105,7 @@ public SubscriberImpl( | |
| protected void handlePermanentError(StatusException error) { | ||
| try (CloseableMonitor.Hold h = monitor.enter()) { | ||
| shutdown = true; | ||
| inFlightSeek.ifPresent(inFlight -> inFlight.setException(error)); | ||
| inFlightSeek.ifPresent(inFlight -> inFlight.seekFuture.setException(error)); | ||
| inFlightSeek = Optional.empty(); | ||
| onPermanentError(error); | ||
| } | ||
| | @@ -106,7 +120,7 @@ protected void stop() { | |
| shutdown = true; | ||
| inFlightSeek.ifPresent( | ||
| inFlight -> | ||
| inFlight.setException( | ||
| inFlight.seekFuture.setException( | ||
| Status.ABORTED | ||
| .withDescription("Client stopped while seek in flight.") | ||
| .asException())); | ||
| | @@ -115,13 +129,21 @@ protected void stop() { | |
| | ||
| @Override | ||
| public ApiFuture<Offset> seek(SeekRequest request) { | ||
| try (CloseableMonitor.Hold h = monitor.enter()) { | ||
| try (CloseableMonitor.Hold h = | ||
| monitor.enterWhenUninterruptibly( | ||
| new Monitor.Guard(monitor.monitor) { | ||
| @Override | ||
| public boolean isSatisfied() { | ||
| return !internalSeekInFlight || shutdown; | ||
| 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. If shutdown == true, we'll checkState-fail right below this. I think we should return an immediately-failed Future. Contributor Author 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. I think this should already happen. The catch block will catch the checkState failure and will return an immediateFailedFuture. | ||
| } | ||
| })) { | ||
| checkArgument( | ||
| Predicates.isValidSeekRequest(request), "Sent SeekRequest with no location set."); | ||
| checkState(!shutdown, "Seeked after the stream shut down."); | ||
| checkState(!inFlightSeek.isPresent(), "Seeked while seek is already in flight."); | ||
| SettableApiFuture<Offset> future = SettableApiFuture.create(); | ||
| inFlightSeek = Optional.of(future); | ||
| inFlightSeek = Optional.of(new InFlightSeek(request, future)); | ||
| tokenCounter.onClientSeek(); | ||
| connection.modifyConnection( | ||
| connectedSubscriber -> | ||
| connectedSubscriber.ifPresent(subscriber -> subscriber.seek(request))); | ||
| | @@ -164,13 +186,17 @@ public void triggerReinitialize() { | |
| connectedSubscriber -> { | ||
| checkArgument(monitor.monitor.isOccupiedByCurrentThread()); | ||
| checkArgument(connectedSubscriber.isPresent()); | ||
| nextOffsetTracker | ||
| .requestForRestart() | ||
| .ifPresent( | ||
| request -> { | ||
| inFlightSeek = Optional.of(SettableApiFuture.create()); | ||
| connectedSubscriber.get().seek(request); | ||
| }); | ||
| if (inFlightSeek.isPresent()) { | ||
| connectedSubscriber.get().seek(inFlightSeek.get().seekRequest); | ||
| } else { | ||
| nextOffsetTracker | ||
| .requestForRestart() | ||
| .ifPresent( | ||
| request -> { | ||
| internalSeekInFlight = true; | ||
| connectedSubscriber.get().seek(request); | ||
| }); | ||
| } | ||
| tokenCounter | ||
| .requestForRestart() | ||
| .ifPresent(request -> connectedSubscriber.get().allowFlow(request)); | ||
| | @@ -212,9 +238,13 @@ private Status onSeekResponse(Offset seekOffset) { | |
| if (shutdown) { | ||
| return Status.OK; | ||
| } | ||
| if (internalSeekInFlight) { | ||
| internalSeekInFlight = false; | ||
| return Status.OK; | ||
| } | ||
| checkState(inFlightSeek.isPresent(), "No in flight seek, but received a seek response."); | ||
| nextOffsetTracker.onClientSeek(seekOffset); | ||
| inFlightSeek.get().set(seekOffset); | ||
| inFlightSeek.get().seekFuture.set(seekOffset); | ||
| inFlightSeek = Optional.empty(); | ||
| return Status.OK; | ||
| } catch (StatusException e) { | ||
| | ||
Uh oh!
There was an error while loading. Please reload this page.