yraivo commented on code in PR #11551:
URL: https://github.com/apache/rocketmq/pull/11551#discussion_r4666231741
##########
controller/src/main/java/org/apache/rocketmq/controller/impl/heartbeat/DefaultBrokerHeartbeatManager.java:
##########
@@ -74,27 +84,104 @@ public void initialize() {
public void scanNotActiveBroker() {
try {
log.info("start scanNotActiveBroker");
- final Iterator<Map.Entry<BrokerIdentityInfo, BrokerLiveInfo>>
iterator = this.brokerLiveTable.entrySet().iterator();
- while (iterator.hasNext()) {
- final Map.Entry<BrokerIdentityInfo, BrokerLiveInfo> next =
iterator.next();
- long last = next.getValue().getLastUpdateTimestamp();
- long timeoutMillis =
next.getValue().getHeartbeatTimeoutMillis();
- if (System.currentTimeMillis() - last > timeoutMillis) {
- final Channel channel = next.getValue().getChannel();
- iterator.remove();
- if (channel != null) {
- RemotingHelper.closeChannel(channel);
+ for (Map.Entry<BrokerIdentityInfo, BrokerLiveInfo> entry :
this.brokerLiveTable.entrySet()) {
+ final BrokerIdentityInfo identity = entry.getKey();
+ final BrokerLiveInfo info = entry.getValue();
+ long last = info.getLastUpdateTimestamp();
+ long timeoutMillis = info.getHeartbeatTimeoutMillis();
+ if (System.currentTimeMillis() - last <= timeoutMillis) {
+ continue;
+ }
+ final AtomicReference<BrokerLiveInfo> expiredRef = new
AtomicReference<>();
+ final AtomicReference<Channel> expiredChannelRef = new
AtomicReference<>();
+ final AtomicLong expiredTimeoutRef = new AtomicLong();
+ this.brokerLiveTable.computeIfPresent(identity, (key, current)
-> {
+ long currentLast = current.getLastUpdateTimestamp();
+ long currentTimeout = current.getHeartbeatTimeoutMillis();
+ if (System.currentTimeMillis() - currentLast >
currentTimeout) {
+ expiredRef.set(current);
+ expiredChannelRef.set(current.getChannel());
+ expiredTimeoutRef.set(currentTimeout);
+ return null;
}
+ return current;
+ });
+ final BrokerLiveInfo expired = expiredRef.get();
+ if (expired != null) {
+ final Channel channel = expiredChannelRef.get();
+ final String brokerName = expired.getBrokerName();
+ final long brokerId = expired.getBrokerId();
+ closeChannelIfUnused(channel);
this.executor.submit(() ->
- notifyBrokerInActive(next.getKey().getClusterName(),
next.getValue().getBrokerName(), next.getValue().getBrokerId()));
- log.warn("The broker channel {} expired, brokerInfo {},
expired {}ms", next.getValue().getChannel(), next.getKey(), timeoutMillis);
+ notifyBrokerInActive(identity.getClusterName(),
brokerName, brokerId));
+ log.warn("The broker channel {} expired, brokerInfo {},
expired {}ms",
+ channel, identity, expiredTimeoutRef.get());
}
}
} catch (Exception e) {
log.error("scanNotActiveBroker exception", e);
}
}
+ private void closeChannelIfUnused(Channel channel) {
+ if (channel == null || isChannelInUse(channel)) {
+ return;
+ }
+ this.channelLifecycleLock.writeLock().lock();
+ try {
+ if (!isChannelInUse(channel)) {
+ final Object closeToken = new Object();
+ this.closingChannels.put(channel, closeToken);
+ final ChannelFuture closeFuture = channel.closeFuture();
+ if (closeFuture != null) {
+ closeFuture.addListener(future ->
this.closingChannels.remove(channel, closeToken));
+ }
+ boolean closeListenerAttached = false;
+ try {
+ final ChannelFuture opFuture = closeChannel(channel);
+ if (opFuture != null) {
+ closeListenerAttached = true;
+ opFuture.addListener((ChannelFutureListener) future ->
{
+ if (!future.isSuccess() || closeFuture == null ||
!channel.isActive()) {
+ this.closingChannels.remove(channel,
closeToken);
+ }
+ });
+ }
+ } finally {
+ if (!closeListenerAttached || !channel.isActive()) {
+ this.closingChannels.remove(channel, closeToken);
+ }
+ }
+ }
+ } finally {
+ this.channelLifecycleLock.writeLock().unlock();
+ }
+ }
+
+ private ChannelFuture closeChannel(Channel channel) {
+ final String addrRemote =
RemotingHelper.parseChannelRemoteAddr(channel);
+ final ChannelFuture closeFuture = channel.close();
+ if (closeFuture != null && addrRemote != null &&
!addrRemote.isEmpty()) {
+ closeFuture.addListener((ChannelFutureListener) future ->
+ log.info("closeChannel: close the connection to remote
address[{}] result: {}", addrRemote,
+ future.isSuccess()));
+ }
+ return closeFuture;
+ }
+
+ private boolean isChannelInUse(Channel channel) {
+ for (BrokerLiveInfo liveInfo : this.brokerLiveTable.values()) {
Review Comment:
Updated in c58b61edf. Expiry now makes one pass over the live table. A
per-channel owner index lets us mark a channel for close in the same update
that removes its last owner, without a global write lock. The close callback
uses the same index. In the 32-channel regression test, reads of a healthy
broker's channel dropped from 64 to 0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]