You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/09/27 15:38:26 UTC
[GitHub] [dubbo] stoneapple opened a new issue, #10688: Dubbo升级Netty4后,异步调用场景存在参数窜包的问题
stoneapple opened a new issue, #10688:
URL: https://github.com/apache/dubbo/issues/10688
问题发现在dubbo2.5.x,初步分析dubbo3应该同样存在。
问题场景:
消费方和提供方默认的通信SPI均切换到netty4,消费方侧reference配置async=true。在如下的代码场景:
Pojo a = new Pojo();
a.setName("zhangsan");
aysncService1.serve(a);
Future f1 = RpcContext.getContext().getFuture();
a.setName("lisi");
aysncService2.serve(a);
Future f2 = RpcContext.getContext().getFuture();
f1.get()+"--"+f2.get();
其中aysncService1和aysncService2两个服务引用均采用异步的配置,在netty4场景下,aysncService1.serve(a),传递给提供方的a对象,属性也编程lisi了,在netty3作为通信组件时,不存在该问题。
初步分析:由于netty4和netty3在客户端的线程模型上有较大的改动。netty3会先将数据序列化后交给nioworker,netty4中直接将对象打包到给nioworker的任务队列,在数据发送时才触发序列化。
解决思路:异步场景发送前对报文深拷贝后丢给任务队列,或者 将序列化前移到业务线程。
--
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: notifications-unsubscribe@dubbo.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] Dubbo升级Netty4后,异步调用场景存在参数窜包的问题 [dubbo]
Posted by "ByrsH (via GitHub)" <gi...@apache.org>.
ByrsH commented on issue #10688:
URL: https://github.com/apache/dubbo/issues/10688#issuecomment-2152201464
2.7.x 也有这样的现象,问题的关键是前后两次请求共用变量 a (注意:假如前后两次请求是两个不同的对象,但是对象的成员变量公用了,也会有同样的问题)。
该问题在 3.2.0 版本得到了解决(虽然此次调整是为了提升效率,并没有说明为了解决这个问题)。commit 记录:[https://github.com/apache/dubbo/commit/5016f550be52f14a232399409a3c97fa6d6db321](https://github.com/apache/dubbo/commit/5016f550be52f14a232399409a3c97fa6d6db321)
关键代码在 dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java 中:
```
try {
Object outputMessage = message;
if (!encodeInIOThread) {
ByteBuf buf = channel.alloc().buffer();
ChannelBuffer buffer = new NettyBackedChannelBuffer(buf);
codec.encode(this, buffer, message);
outputMessage = buf;
}
ChannelFuture future = writeQueue.enqueue(outputMessage).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!(message instanceof Request)) {
return;
}
ChannelHandler handler = getChannelHandler();
if (future.isSuccess()) {
handler.sent(NettyChannel.this, message);
} else {
Throwable t = future.cause();
if (t == null) {
return;
}
Response response = buildErrorResponse((Request) message, t);
handler.received(NettyChannel.this, response);
}
}
});
```
Dubbo 在切换到 Netty channel 前对 message 做了 encode,然后放到队列里就返回了。当第二个请求 a.setName("lisi"); 时,就不会影响已经 encode 的 message了。
之前的版本是直接通过 netty channel writeAndFlush message,在第二个请求 a.setName("lisi"); 时,第一个请求还未完成 encode ,就会造成参数覆盖的情况出现。
2.7.22 版本 NettyChannel:
```
try {
ChannelFuture future = channel.writeAndFlush(message);
if (sent) {
// wait timeout ms
timeout = getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
success = future.await(timeout);
}
Throwable cause = future.cause();
if (cause != null) {
throw cause;
}
} catch (Throwable e) {
removeChannelIfDisconnected(channel);
throw new RemotingException(this, "Failed to send message " + PayloadDropper.getRequestWithoutData(message) + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
}
```
对于使用者来说,应该尽量避免多次异步请求参数共享的情形出现,第二次调用时,可以对参数做一次深拷贝得到新的对象,然后再进行异步请求。
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] AlbumenJ commented on issue #10688: Dubbo升级Netty4后,异步调用场景存在参数窜包的问题
Posted by GitBox <gi...@apache.org>.
AlbumenJ commented on issue #10688:
URL: https://github.com/apache/dubbo/issues/10688#issuecomment-1263154710
这一块看起来在 Dubbo 3 是没问题的,你可以试下跑 3.1.1 版本看看,有问题的话再提交个 issue 吧
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] Dubbo升级Netty4后,异步调用场景存在参数窜包的问题 [dubbo]
Posted by "wcy666103 (via GitHub)" <gi...@apache.org>.
wcy666103 commented on issue #10688:
URL: https://github.com/apache/dubbo/issues/10688#issuecomment-2152233937
> 2.7.x 也有这样的现象,问题的关键是前后两次请求共用变量 a (注意:假如前后两次请求是两个不同的对象,但是对象的成员变量公用了,也会有同样的问题)。
>
> 该问题在 3.2.0 版本得到了解决(虽然此次调整是为了提升效率,并没有说明为了解决这个问题)。commit 记录:[5016f55](https://github.com/apache/dubbo/commit/5016f550be52f14a232399409a3c97fa6d6db321)
>
> 关键代码在 dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java 中:
>
> ```
> try {
> Object outputMessage = message;
> if (!encodeInIOThread) {
> ByteBuf buf = channel.alloc().buffer();
> ChannelBuffer buffer = new NettyBackedChannelBuffer(buf);
> codec.encode(this, buffer, message);
> outputMessage = buf;
> }
> ChannelFuture future = writeQueue.enqueue(outputMessage).addListener(new ChannelFutureListener() {
> @Override
> public void operationComplete(ChannelFuture future) throws Exception {
> if (!(message instanceof Request)) {
> return;
> }
> ChannelHandler handler = getChannelHandler();
> if (future.isSuccess()) {
> handler.sent(NettyChannel.this, message);
> } else {
> Throwable t = future.cause();
> if (t == null) {
> return;
> }
> Response response = buildErrorResponse((Request) message, t);
> handler.received(NettyChannel.this, response);
> }
> }
> });
> ```
>
> Dubbo 在切换到 Netty channel 前对 message 做了 encode,然后放到队列里就返回了。当第二个请求 a.setName("lisi"); 时,就不会影响已经 encode 的 message了。
>
> 之前的版本是直接通过 netty channel writeAndFlush message,在第二个请求 a.setName("lisi"); 时,第一个请求还未完成 encode ,就会造成参数覆盖的情况出现。 2.7.22 版本 NettyChannel:
>
> ```
> try {
> ChannelFuture future = channel.writeAndFlush(message);
> if (sent) {
> // wait timeout ms
> timeout = getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
> success = future.await(timeout);
> }
> Throwable cause = future.cause();
> if (cause != null) {
> throw cause;
> }
> } catch (Throwable e) {
> removeChannelIfDisconnected(channel);
> throw new RemotingException(this, "Failed to send message " + PayloadDropper.getRequestWithoutData(message) + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
> }
> ```
>
> 对于使用者来说,应该尽量避免多次异步请求参数共享的情形出现,第二次调用时,可以对参数做一次深拷贝得到新的对象,然后再进行异步请求。
你可以帮忙提交个pr解决这个问题吗
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org