Skip to content
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

fix: avoid blocking alarm pool during stream reconnects #1400

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,4 @@ static AlarmFactory create(Duration duration) {
duration.toNanos(),
NANOSECONDS);
}

/** Runnable is executed by an unbounded pool, although the alarm pool is bounded. */
static AlarmFactory createUnbounded(Duration duration) {
AlarmFactory underlying = create(duration);
return runnable ->
underlying.newAlarm(() -> SystemExecutors.getFuturesExecutor().execute(runnable));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ void failBatch(int startIdx, CheckedApiException e) {
@GuardedBy("monitor.monitor")
private final Queue<InFlightBatch> batchesInFlight = new ArrayDeque<>();

// reconnectingMonitor is always acquired after monitor.monitor when both are held.
private final CloseableMonitor reconnectingMonitor = new CloseableMonitor();

@GuardedBy("reconnectingMonitor.monitor")
private boolean reconnecting = false;

@VisibleForTesting
PublisherImpl(
PublishStreamFactory streamFactory,
Expand Down Expand Up @@ -146,7 +152,7 @@ public PublisherImpl(
this(
streamFactory,
new BatchPublisherImpl.Factory(),
AlarmFactory.createUnbounded(
AlarmFactory.create(
Duration.ofNanos(
Objects.requireNonNull(batchingSettings.getDelayThreshold()).toNanos())),
initialRequest,
Expand Down Expand Up @@ -189,6 +195,9 @@ private void rebatchForRestart() {
@Override
public void triggerReinitialize(CheckedApiException streamError) {
try (CloseableMonitor.Hold h = monitor.enter()) {
try (CloseableMonitor.Hold rh = reconnectingMonitor.enter()) {
reconnecting = true;
}
connection.reinitialize(initialRequest);
rebatchForRestart();
Collection<InFlightBatch> batches = batchesInFlight;
Expand All @@ -201,6 +210,9 @@ public void triggerReinitialize(CheckedApiException streamError) {
.get()
.publish(batch.messagesToSend(), batch.firstSequenceNumber()));
});
try (CloseableMonitor.Hold rh = reconnectingMonitor.enter()) {
reconnecting = false;
}
} catch (CheckedApiException e) {
onPermanentError(e);
}
Expand All @@ -219,7 +231,7 @@ protected void handlePermanentError(CheckedApiException error) {
@Override
protected void start() {
try (CloseableMonitor.Hold h = monitor.enter()) {
this.alarmFuture = Optional.of(alarmFactory.newAlarm(this::flushToStream));
this.alarmFuture = Optional.of(alarmFactory.newAlarm(this::backgroundFlushToStream));
}
}

Expand Down Expand Up @@ -276,6 +288,15 @@ public void cancelOutstandingPublishes() {
}
}

private void backgroundFlushToStream() {
try (CloseableMonitor.Hold h = reconnectingMonitor.enter()) {
if (reconnecting) {
return;
}
}
flushToStream();
}

private void flushToStream() {
try (CloseableMonitor.Hold h = monitor.enter()) {
if (shutdown) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public class SubscriberImpl extends ProxyService
@GuardedBy("monitor.monitor")
private boolean shutdown = false;

// reconnectingMonitor is always acquired after monitor.monitor when both are held.
private final CloseableMonitor reconnectingMonitor = new CloseableMonitor();

@GuardedBy("reconnectingMonitor.monitor")
private boolean reconnecting = false;

@VisibleForTesting
SubscriberImpl(
SubscribeStreamFactory streamFactory,
Expand Down Expand Up @@ -205,6 +211,9 @@ public void triggerReinitialize(CheckedApiException streamError) {

try (CloseableMonitor.Hold h = monitor.enter()) {
if (shutdown) return;
try (CloseableMonitor.Hold rh = reconnectingMonitor.enter()) {
reconnecting = true;
}
connection.reinitialize(getInitialRequest());
connection.modifyConnection(
connectedSubscriber -> {
Expand All @@ -214,6 +223,9 @@ public void triggerReinitialize(CheckedApiException streamError) {
.requestForRestart()
.ifPresent(request -> connectedSubscriber.get().allowFlow(request));
});
try (CloseableMonitor.Hold rh = reconnectingMonitor.enter()) {
reconnecting = false;
}
} catch (CheckedApiException e) {
onPermanentError(e);
}
Expand All @@ -239,6 +251,11 @@ public void onClientResponse(List<SequencedMessage> messages) throws CheckedApiE
}

private void processBatchFlowRequest() {
try (CloseableMonitor.Hold h = reconnectingMonitor.enter()) {
if (reconnecting) {
return;
}
}
try (CloseableMonitor.Hold h = monitor.enter()) {
if (shutdown) return;
connection.modifyConnection(
Expand Down
Loading