-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
optimize: support fast fail when channel disconnect after message sent for RemotingClient #7095
Open
lyl2008dsg
wants to merge
7
commits into
apache:2.x
Choose a base branch
from
smartscity:feature_client_fast_fail
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cf841e
fast fail when channel is null
lyl2008dsg 01d776d
add optimize changes
lyl2008dsg 89a1ceb
add failFuturesForChannel
lyl2008dsg 386651a
sync 2.x
lyl2008dsg c3c81fa
add change logs
lyl2008dsg f738f3e
add comment
lyl2008dsg 4ba1621
add ChannelIsNotWritable
lyl2008dsg 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 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 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 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import java.net.InetSocketAddress; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ExecutorService; | ||
|
@@ -91,6 +92,9 @@ public abstract class AbstractNettyRemotingClient extends AbstractNettyRemoting | |
|
||
protected final Map<Integer, Integer> childToParentMap = new ConcurrentHashMap<>(); | ||
|
||
// Channel -> Set<msgId> | ||
protected final ConcurrentHashMap<Channel, Set<Integer>> channelToRequestIds = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* When batch sending is enabled, the message will be stored to basketMap | ||
* Send via asynchronous thread {@link AbstractNettyRemotingClient.MergedSendRunnable} | ||
|
@@ -181,6 +185,7 @@ public Object sendSyncRequest(Object msg) throws TimeoutException { | |
} | ||
} else { | ||
Channel channel = clientChannelManager.acquireChannel(serverAddress); | ||
channelToRequestIds.computeIfAbsent(channel, ch -> ConcurrentHashMap.newKeySet()).add(rpcMessage.getId()); | ||
return super.sendSync(channel, rpcMessage, timeoutMillis); | ||
} | ||
|
||
|
@@ -193,6 +198,7 @@ public Object sendSyncRequest(Channel channel, Object msg) throws TimeoutExcepti | |
return null; | ||
} | ||
RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); | ||
channelToRequestIds.computeIfAbsent(channel, ch -> ConcurrentHashMap.newKeySet()).add(rpcMessage.getId()); | ||
return super.sendSync(channel, rpcMessage, this.getRpcRequestTimeout()); | ||
} | ||
|
||
|
@@ -214,6 +220,7 @@ public void sendAsyncRequest(Channel channel, Object msg) { | |
childToParentMap.put(msgId, parentId); | ||
} | ||
} | ||
channelToRequestIds.computeIfAbsent(channel, ch -> ConcurrentHashMap.newKeySet()).add(rpcMessage.getId()); | ||
} | ||
super.sendAsync(channel, rpcMessage); | ||
} | ||
|
@@ -222,6 +229,7 @@ public void sendAsyncRequest(Channel channel, Object msg) { | |
public void sendAsyncResponse(String serverAddress, RpcMessage rpcMessage, Object msg) { | ||
RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, ProtocolConstants.MSGTYPE_RESPONSE); | ||
Channel channel = clientChannelManager.acquireChannel(serverAddress); | ||
channelToRequestIds.computeIfAbsent(channel, ch -> ConcurrentHashMap.newKeySet()).add(rpcMessage.getId()); | ||
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. Is fast-failure necessary for AsyncSend? Is there Future waiting in this case? |
||
super.sendAsync(channel, rpcMsg); | ||
} | ||
|
||
|
@@ -423,7 +431,9 @@ class ClientHandler extends ChannelDuplexHandler { | |
@Override | ||
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { | ||
if (msg instanceof RpcMessage) { | ||
processMessage(ctx, (RpcMessage)msg); | ||
RpcMessage rpcMessage = (RpcMessage)msg; | ||
processMessage(ctx, rpcMessage); | ||
removeRequestIdFromChannel(ctx.channel(), rpcMessage.getId()); | ||
} else { | ||
LOGGER.error("rpcMessage type error"); | ||
} | ||
|
@@ -448,6 +458,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { | |
LOGGER.info("channel inactive: {}", ctx.channel()); | ||
} | ||
clientChannelManager.releaseChannel(ctx.channel(), NetUtil.toStringAddress(ctx.channel().remoteAddress())); | ||
failFuturesForChannel(ctx.channel(), new FrameworkException(FrameworkErrorCode.ChannelIsNotWritable)); | ||
super.channelInactive(ctx); | ||
} | ||
|
||
|
@@ -489,6 +500,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E | |
if (LOGGER.isInfoEnabled()) { | ||
LOGGER.info("remove exception rm channel:{}", ctx.channel()); | ||
} | ||
failFuturesForChannel(ctx.channel(), cause); | ||
super.exceptionCaught(ctx, cause); | ||
} | ||
|
||
|
@@ -501,4 +513,59 @@ public void close(ChannelHandlerContext ctx, ChannelPromise future) throws Excep | |
} | ||
} | ||
|
||
/** | ||
* Iterates over futures and marks all MessageFutures associated with the specified Channel as failed. | ||
* | ||
* @param channel The Channel that has been disconnected or encountered an exception. | ||
* @param cause The reason for the failure. | ||
*/ | ||
private void failFuturesForChannel(Channel channel, Throwable cause) { | ||
Set<Integer> requestIds = channelToRequestIds.remove(channel); | ||
if (requestIds != null) { | ||
for (Integer requestId : requestIds) { | ||
MessageFuture future = futures.remove(requestId); | ||
if (future != null) { | ||
future.setResultMessage(cause); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Removes the association between the specified Channel and requestId from the channelToRequestIds mapping. | ||
* If the Channel no longer has any associated requestId, the Channel will be removed from the mapping. | ||
* | ||
* @param channel The Channel that has been disconnected or encountered an exception. | ||
* @param requestId The requestId to be removed. | ||
*/ | ||
private void removeRequestIdFromChannel(Channel channel, Integer requestId) { | ||
if (channel == null) { | ||
if (requestId != null) { | ||
LOGGER.warn("Attempted to remove requestId {} from a null channel.", requestId); | ||
} else { | ||
LOGGER.warn("Attempted to remove a null requestId from a null channel."); | ||
} | ||
return; | ||
} | ||
|
||
if (requestId == null) { | ||
LOGGER.warn("Attempted to remove a null requestId from channel {}.", channel); | ||
return; | ||
} | ||
|
||
channelToRequestIds.computeIfPresent(channel, (ch, requestIds) -> { | ||
boolean removed = requestIds.remove(requestId); | ||
if (removed) { | ||
LOGGER.debug("Removed requestId {} from channel {}.", requestId, ch); | ||
} else { | ||
LOGGER.warn("Attempted to remove non-existing requestId {} from channel {}.", requestId, ch); | ||
} | ||
|
||
if (requestIds.isEmpty()) { | ||
LOGGER.debug("No more requestIds associated with channel {}. Channel removed from mapping.", ch); | ||
return null; | ||
} | ||
return requestIds; | ||
}); | ||
} | ||
} |
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.
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.
Should the computeIfAbsent operation be extracted method? and integrated with super.sendSync? Avoid code duplication and missing calls