-
Notifications
You must be signed in to change notification settings - Fork 106
feat: recycle channel on consecutive new stream failures #2903
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -65,6 +65,8 @@ public class ChannelPoolDpImpl implements ChannelPool { | |
| private static final String DEFAULT_LOG_NAME = "pool"; | ||
| private static final AtomicInteger INDEX = new AtomicInteger(); | ||
|
|
||
| private static final int CONSECUTIVE_OPEN_FAILURE_THRESHOLD = 5; | ||
|
|
||
| private final String poolLogId; | ||
|
|
||
| @VisibleForTesting volatile int minGroups; | ||
|
|
@@ -221,6 +223,7 @@ public void start(Listener responseListener, Metadata headers) { | |
| public void onBeforeSessionStart(PeerInfo peerInfo) { | ||
| afeId = AfeId.extract(peerInfo); | ||
| synchronized (ChannelPoolDpImpl.this) { | ||
| channelWrapper.consecutiveFailures = 0; | ||
| rehomeChannel(channelWrapper, afeId); | ||
| sessionsPerAfeId.add(afeId); | ||
| } | ||
|
|
@@ -232,6 +235,8 @@ public void onClose(Status status, Metadata trailers) { | |
| synchronized (ChannelPoolDpImpl.this) { | ||
| if (afeId != null) { | ||
| sessionsPerAfeId.remove(afeId); | ||
| } else if (!status.isOk() && status.getCode() != Code.CANCELLED) { | ||
| channelWrapper.consecutiveFailures++; | ||
| } | ||
| releaseChannel(channelWrapper, status); | ||
|
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. Not related to this PR, but I feel releaseChannel should be called releaseStream or something, with recycleChannel the naming gets a little confusing..
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 guess the meaning here was that a session is closing and releasing the channel it was using. |
||
| } | ||
|
|
@@ -306,12 +311,12 @@ private void releaseChannel(ChannelWrapper channelWrapper, Status status) { | |
| channelWrapper.group.numStreams--; | ||
| channelWrapper.numOutstanding--; | ||
|
|
||
| if (shouldRecycleChannel(status)) { | ||
| if (shouldRecycleChannel(channelWrapper, status)) { | ||
| recycleChannel(channelWrapper); | ||
| } | ||
| } | ||
|
|
||
| private static boolean shouldRecycleChannel(Status status) { | ||
| private static boolean shouldRecycleChannel(ChannelWrapper channelWrapper, Status status) { | ||
|
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. Also unrelated to this PR.. I'm wondering how this works.. if we get unimplemented, shouldn't the client fallback to unary? (iiuc which doesn't use this channel pool) recycling the channel and try to create a new one likely still won't work?
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. Yes, we will eventually fallback to unary if recycling doesn't help. But I've added a backoff not to spam new connections rapidly and indefinitely. |
||
| if (status.getCode() == Code.UNIMPLEMENTED) { | ||
| return true; | ||
| } | ||
|
|
@@ -322,6 +327,10 @@ private static boolean shouldRecycleChannel(Status status) { | |
| return true; | ||
| } | ||
|
|
||
| if (channelWrapper.consecutiveFailures >= CONSECUTIVE_OPEN_FAILURE_THRESHOLD) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -480,6 +489,7 @@ static class ChannelWrapper { | |
| private final ManagedChannel channel; | ||
| private final Instant createdAt; | ||
| private int numOutstanding = 0; | ||
| private int consecutiveFailures = 0; | ||
|
|
||
| public ChannelWrapper(AfeChannelGroup group, ManagedChannel channel, Clock clock) { | ||
| this.group = group; | ||
|
|
||
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.
wondering if this should be called CONSECUTIVE_OPEN_SESSION_FAILURE_THRESHOLD.
And I think this should come from client config?
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.
Renamed and added a todo.