Skip to content

Commit c8477a8

Browse files
committed
Log warning message at method exceptionCaught of Netty handlers
1 parent e2c0411 commit c8477a8

3 files changed

Lines changed: 77 additions & 26 deletions

File tree

dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyHandler.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.dubbo.remoting.transport.netty;
1818

1919
import org.apache.dubbo.common.URL;
20-
import org.apache.dubbo.common.logger.Logger;
20+
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
2121
import org.apache.dubbo.common.logger.LoggerFactory;
2222
import org.apache.dubbo.common.utils.NetUtils;
2323
import org.apache.dubbo.remoting.Channel;
@@ -34,13 +34,15 @@
3434
import org.jboss.netty.channel.MessageEvent;
3535
import org.jboss.netty.channel.SimpleChannelHandler;
3636

37+
import static org.apache.dubbo.common.constants.LoggerCodeConstants.TRANSPORT_UNEXPECTED_EXCEPTION;
38+
3739
/**
3840
* NettyHandler
3941
*/
4042
@Sharable
4143
public class NettyHandler extends SimpleChannelHandler {
4244

43-
private static final Logger logger = LoggerFactory.getLogger(NettyHandler.class);
45+
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NettyHandler.class);
4446

4547
private final Map<String, Channel> channels = new ConcurrentHashMap<>(); // <ip:port, channel>
4648

@@ -65,39 +67,43 @@ public Map<String, Channel> getChannels() {
6567

6668
@Override
6769
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
68-
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler);
70+
org.jboss.netty.channel.Channel ch = ctx.getChannel();
71+
NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler);
6972
try {
7073
if (channel != null) {
71-
channels.put(
72-
NetUtils.toAddressString(
73-
(InetSocketAddress) ctx.getChannel().getRemoteAddress()),
74-
channel);
74+
channels.put(NetUtils.toAddressString((InetSocketAddress) ch.getRemoteAddress()), channel);
7575
}
7676
handler.connected(channel);
7777
} finally {
78-
NettyChannel.removeChannelIfDisconnected(ctx.getChannel());
78+
NettyChannel.removeChannelIfDisconnected(ch);
7979
}
8080

8181
if (logger.isInfoEnabled() && channel != null) {
82-
logger.info("The connection between " + channel.getRemoteAddress() + " and " + channel.getLocalAddress()
83-
+ " is established");
82+
logger.info(
83+
"The connection {} between {} and {} is established.",
84+
ch,
85+
channel.getRemoteAddress(),
86+
channel.getLocalAddress());
8487
}
8588
}
8689

8790
@Override
8891
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
89-
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler);
92+
org.jboss.netty.channel.Channel ch = ctx.getChannel();
93+
NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler);
9094
try {
91-
channels.remove(NetUtils.toAddressString(
92-
(InetSocketAddress) ctx.getChannel().getRemoteAddress()));
95+
channels.remove(NetUtils.toAddressString((InetSocketAddress) ch.getRemoteAddress()));
9396
handler.disconnected(channel);
9497
} finally {
95-
NettyChannel.removeChannelIfDisconnected(ctx.getChannel());
98+
NettyChannel.removeChannelIfDisconnected(ch);
9699
}
97100

98101
if (logger.isInfoEnabled()) {
99-
logger.info("The connection between " + channel.getRemoteAddress() + " and " + channel.getLocalAddress()
100-
+ " is disconnected");
102+
logger.info(
103+
"The connection {} between {} and {} is disconnected.",
104+
ch,
105+
channel.getRemoteAddress(),
106+
channel.getLocalAddress());
101107
}
102108
}
103109

@@ -124,11 +130,25 @@ public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exc
124130

125131
@Override
126132
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
127-
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler);
133+
org.jboss.netty.channel.Channel ch = ctx.getChannel();
134+
NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler);
128135
try {
129136
handler.caught(channel, e.getCause());
130137
} finally {
131-
NettyChannel.removeChannelIfDisconnected(ctx.getChannel());
138+
NettyChannel.removeChannelIfDisconnected(ch);
139+
}
140+
141+
if (logger.isWarnEnabled()) {
142+
logger.warn(
143+
TRANSPORT_UNEXPECTED_EXCEPTION,
144+
"",
145+
"",
146+
channel == null
147+
? String.format("The connection %s has exception.", ch)
148+
: String.format(
149+
"The connection %s between %s and %s has exception.",
150+
ch, channel.getRemoteAddress(), channel.getLocalAddress()),
151+
e.getCause());
132152
}
133153
}
134154
}

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import org.apache.dubbo.common.URL;
2020
import org.apache.dubbo.common.Version;
21-
import org.apache.dubbo.common.logger.Logger;
21+
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
2222
import org.apache.dubbo.common.logger.LoggerFactory;
2323
import org.apache.dubbo.remoting.ChannelHandler;
2424
import org.apache.dubbo.remoting.exchange.Request;
@@ -30,13 +30,14 @@
3030
import io.netty.handler.timeout.IdleStateEvent;
3131

3232
import static org.apache.dubbo.common.constants.CommonConstants.HEARTBEAT_EVENT;
33+
import static org.apache.dubbo.common.constants.LoggerCodeConstants.TRANSPORT_UNEXPECTED_EXCEPTION;
3334

3435
/**
3536
* NettyClientHandler
3637
*/
3738
@io.netty.channel.ChannelHandler.Sharable
3839
public class NettyClientHandler extends ChannelDuplexHandler {
39-
private static final Logger logger = LoggerFactory.getLogger(NettyClientHandler.class);
40+
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NettyClientHandler.class);
4041

4142
private final URL url;
4243

@@ -122,11 +123,25 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
122123

123124
@Override
124125
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
125-
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
126+
Channel ch = ctx.channel();
127+
NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler);
126128
try {
127129
handler.caught(channel, cause);
128130
} finally {
129-
NettyChannel.removeChannelIfDisconnected(ctx.channel());
131+
NettyChannel.removeChannelIfDisconnected(ch);
132+
}
133+
134+
if (logger.isWarnEnabled()) {
135+
logger.warn(
136+
TRANSPORT_UNEXPECTED_EXCEPTION,
137+
"",
138+
"",
139+
channel == null
140+
? String.format("The connection %s has exception.", ch)
141+
: String.format(
142+
"The connection %s of %s -> %s has exception.",
143+
ch, channel.getLocalAddressKey(), channel.getRemoteAddressKey()),
144+
cause);
130145
}
131146
}
132147
}

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.dubbo.remoting.transport.netty4;
1818

1919
import org.apache.dubbo.common.URL;
20-
import org.apache.dubbo.common.logger.Logger;
20+
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
2121
import org.apache.dubbo.common.logger.LoggerFactory;
2222
import org.apache.dubbo.common.utils.NetUtils;
2323
import org.apache.dubbo.remoting.Channel;
@@ -36,12 +36,14 @@
3636
import io.netty.handler.timeout.IdleStateEvent;
3737
import io.netty.util.AttributeKey;
3838

39+
import static org.apache.dubbo.common.constants.LoggerCodeConstants.TRANSPORT_UNEXPECTED_EXCEPTION;
40+
3941
/**
4042
* NettyServerHandler.
4143
*/
4244
@io.netty.channel.ChannelHandler.Sharable
4345
public class NettyServerHandler extends ChannelDuplexHandler {
44-
private static final Logger logger = LoggerFactory.getLogger(NettyServerHandler.class);
46+
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NettyServerHandler.class);
4547
/**
4648
* the cache for alive worker channel.
4749
* <ip:port, dubbo channel>
@@ -148,11 +150,25 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
148150

149151
@Override
150152
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
151-
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
153+
io.netty.channel.Channel ch = ctx.channel();
154+
NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler);
152155
try {
153156
handler.caught(channel, cause);
154157
} finally {
155-
NettyChannel.removeChannelIfDisconnected(ctx.channel());
158+
NettyChannel.removeChannelIfDisconnected(ch);
159+
}
160+
161+
if (logger.isWarnEnabled()) {
162+
logger.warn(
163+
TRANSPORT_UNEXPECTED_EXCEPTION,
164+
"",
165+
"",
166+
channel == null
167+
? String.format("The connection %s has exception.", ch)
168+
: String.format(
169+
"The connection %s of %s -> %s has exception.",
170+
ch, channel.getRemoteAddressKey(), channel.getLocalAddressKey()),
171+
cause);
156172
}
157173
}
158174
}

0 commit comments

Comments
 (0)