Skip to content

Commit b51187e

Browse files
authored
bugfix: fix Alibaba Dubbo convert error (#6624)
1 parent 2a7dc88 commit b51187e

File tree

6 files changed

+88
-33
lines changed

6 files changed

+88
-33
lines changed

changes/en-us/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Add changes here for all PR submitted to the 2.x branch.
77

88
### bugfix:
99
- [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async annotation not working in ClusterWatcherManager
10+
- [[#6624](https://github.com/apache/incubator-seata/pull/6624)] fix Alibaba Dubbo convert error
1011

1112

1213
### optimize:

changes/zh-cn/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### bugfix:
88
- [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async注解ClusterWatcherManager中不生效的问题
9+
- [[#6624](https://github.com/apache/incubator-seata/pull/6624)] 修复 Alibaba Dubbo 转换错误
910

1011

1112
### optimize:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.integration.dubbo.alibaba;
18+
19+
import com.alibaba.dubbo.common.extension.Activate;
20+
import com.alibaba.dubbo.rpc.Filter;
21+
import com.alibaba.dubbo.rpc.Invocation;
22+
import com.alibaba.dubbo.rpc.Invoker;
23+
import com.alibaba.dubbo.rpc.Result;
24+
import com.alibaba.dubbo.rpc.RpcContext;
25+
import com.alibaba.dubbo.rpc.RpcException;
26+
27+
import org.apache.seata.core.constants.DubboConstants;
28+
import org.apache.seata.core.context.RootContext;
29+
import org.apache.seata.core.model.BranchType;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
/**
34+
* The type Alibaba dubbo transaction consumer filter.
35+
*/
36+
@Activate(group = {DubboConstants.CONSUMER}, order = 100)
37+
public class AlibabaDubboTransactionConsumerFilter implements Filter {
38+
39+
private static final Logger LOGGER = LoggerFactory.getLogger(AlibabaDubboTransactionConsumerFilter.class);
40+
41+
@Override
42+
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
43+
if (!DubboConstants.ALIBABADUBBO) {
44+
return invoker.invoke(invocation);
45+
}
46+
String xid = RootContext.getXID();
47+
BranchType branchType = RootContext.getBranchType();
48+
49+
if (LOGGER.isDebugEnabled()) {
50+
LOGGER.debug("consumer xid in RootContext[{}], branchType in RootContext[{}]", xid, branchType);
51+
}
52+
if (xid != null) {
53+
RpcContext.getContext().setAttachment(RootContext.KEY_XID, xid);
54+
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name());
55+
}
56+
try {
57+
return invoker.invoke(invocation);
58+
} finally {
59+
RpcContext.getContext().removeAttachment(RootContext.KEY_XID);
60+
RpcContext.getContext().removeAttachment(RootContext.KEY_BRANCH_TYPE);
61+
}
62+
}
63+
}
Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,41 @@
2323
import com.alibaba.dubbo.rpc.Result;
2424
import com.alibaba.dubbo.rpc.RpcContext;
2525
import com.alibaba.dubbo.rpc.RpcException;
26+
2627
import org.apache.seata.common.util.StringUtils;
27-
import org.apache.seata.core.context.RootContext;
2828
import org.apache.seata.core.constants.DubboConstants;
29+
import org.apache.seata.core.context.RootContext;
2930
import org.apache.seata.core.model.BranchType;
3031
import org.slf4j.Logger;
3132
import org.slf4j.LoggerFactory;
3233

3334
/**
34-
* The type Transaction propagation filter.
35-
*
35+
* The type Alibaba dubbo transaction provider filter.
3636
*/
37-
@Activate(group = {DubboConstants.PROVIDER, DubboConstants.CONSUMER}, order = 100)
38-
public class AlibabaDubboTransactionPropagationFilter implements Filter {
37+
@Activate(group = {DubboConstants.PROVIDER}, order = 100)
38+
public class AlibabaDubboTransactionProviderFilter implements Filter {
3939

40-
private static final Logger LOGGER = LoggerFactory.getLogger(AlibabaDubboTransactionPropagationFilter.class);
40+
private static final Logger LOGGER = LoggerFactory.getLogger(AlibabaDubboTransactionProviderFilter.class);
4141

4242
@Override
4343
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
4444
if (!DubboConstants.ALIBABADUBBO) {
4545
return invoker.invoke(invocation);
4646
}
47-
String xid = RootContext.getXID();
48-
BranchType branchType = RootContext.getBranchType();
49-
5047
String rpcXid = getRpcXid();
5148
String rpcBranchType = RpcContext.getContext().getAttachment(RootContext.KEY_BRANCH_TYPE);
5249
if (LOGGER.isDebugEnabled()) {
53-
LOGGER.debug("xid in RootContext[{}] xid in RpcContext[{}]", xid, rpcXid);
50+
LOGGER.debug("xid in RpcContext[{}], branchType in RpcContext[{}]", rpcXid, rpcBranchType);
5451
}
5552
boolean bind = false;
56-
if (xid != null) {
57-
RpcContext.getContext().setAttachment(RootContext.KEY_XID, xid);
58-
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name());
59-
} else {
60-
if (rpcXid != null) {
61-
RootContext.bind(rpcXid);
62-
if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType)) {
63-
RootContext.bindBranchType(BranchType.TCC);
64-
}
65-
bind = true;
66-
if (LOGGER.isDebugEnabled()) {
67-
LOGGER.debug("bind xid [{}] branchType [{}] to RootContext", rpcXid, rpcBranchType);
68-
}
53+
if (rpcXid != null) {
54+
RootContext.bind(rpcXid);
55+
if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType)) {
56+
RootContext.bindBranchType(BranchType.TCC);
6957
}
58+
bind = true;
7059
}
60+
7161
try {
7262
return invoker.invoke(invocation);
7363
} finally {
@@ -82,7 +72,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
8272
}
8373
if (!rpcXid.equalsIgnoreCase(unbindXid)) {
8474
LOGGER.warn("xid in change during RPC from {} to {},branchType from {} to {}", rpcXid, unbindXid,
85-
rpcBranchType != null ? rpcBranchType : BranchType.AT,previousBranchType);
75+
rpcBranchType != null ? rpcBranchType : BranchType.AT, previousBranchType);
8676
if (unbindXid != null) {
8777
RootContext.bind(unbindXid);
8878
LOGGER.warn("bind xid [{}] back to RootContext", unbindXid);
@@ -93,15 +83,14 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
9383
}
9484
}
9585
}
96-
RpcContext.getContext().removeAttachment(RootContext.KEY_XID);
97-
RpcContext.getContext().removeAttachment(RootContext.KEY_BRANCH_TYPE);
9886
RpcContext.getServerContext().removeAttachment(RootContext.KEY_XID);
9987
RpcContext.getServerContext().removeAttachment(RootContext.KEY_BRANCH_TYPE);
10088
}
10189
}
10290

10391
/**
10492
* get rpc xid
93+
*
10594
* @return
10695
*/
10796
private String getRpcXid() {
@@ -111,5 +100,4 @@ private String getRpcXid() {
111100
}
112101
return rpcXid;
113102
}
114-
115103
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
org.apache.seata.integration.dubbo.alibaba.AlibabaDubboTransactionPropagationFilter
1+
org.apache.seata.integration.dubbo.alibaba.AlibabaDubboTransactionConsumerFilter
2+
org.apache.seata.integration.dubbo.alibaba.AlibabaDubboTransactionProviderFilter

integration/dubbo-alibaba/src/test/java/org/apache/seata/integration/dubbo/alibaba/AlibabaDubboTransactionPropagationFilterTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@
1717
package org.apache.seata.integration.dubbo.alibaba;
1818

1919
import com.alibaba.dubbo.rpc.RpcContext;
20+
2021
import org.apache.seata.core.context.RootContext;
2122
import org.apache.seata.core.model.BranchType;
2223
import org.apache.seata.integration.dubbo.alibaba.mock.MockInvoker;
2324
import org.junit.jupiter.api.Test;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
2627

27-
2828
public class AlibabaDubboTransactionPropagationFilterTest {
2929

3030
private static final String DEFAULT_XID = "1234567890";
3131

3232
@Test
3333
public void testInvoke_And_RootContext() {
34-
AlibabaDubboTransactionPropagationFilter filter = new AlibabaDubboTransactionPropagationFilter();
34+
AlibabaDubboTransactionProviderFilter providerFilter = new AlibabaDubboTransactionProviderFilter();
3535

3636
// SAGA
3737
RpcContext.getContext().setAttachment(RootContext.KEY_XID, DEFAULT_XID);
3838
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, BranchType.SAGA.name());
39-
filter.invoke(new MockInvoker(() -> {
39+
providerFilter.invoke(new MockInvoker(() -> {
4040
assertThat(RootContext.getXID()).isEqualTo(DEFAULT_XID);
4141
assertThat(RootContext.getBranchType()).isEqualTo(BranchType.AT);
4242
}), null);
@@ -46,19 +46,20 @@ public void testInvoke_And_RootContext() {
4646
// TCC
4747
RpcContext.getContext().setAttachment(RootContext.KEY_XID, DEFAULT_XID);
4848
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, BranchType.TCC.name());
49-
filter.invoke(new MockInvoker(() -> {
49+
providerFilter.invoke(new MockInvoker(() -> {
5050
assertThat(RootContext.getXID()).isEqualTo(DEFAULT_XID);
5151
assertThat(RootContext.getBranchType()).isEqualTo(BranchType.TCC);
5252
}), null);
5353
assertThat(RootContext.unbind()).isNull();
5454
assertThat(RootContext.unbindBranchType()).isNull();
5555

5656
// TCC
57+
AlibabaDubboTransactionConsumerFilter consumerFilter = new AlibabaDubboTransactionConsumerFilter();
5758
RootContext.bind(DEFAULT_XID);
5859
RootContext.bindBranchType(BranchType.SAGA);
5960
RpcContext.getContext().setAttachment(RootContext.KEY_XID, DEFAULT_XID);
6061
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, BranchType.TCC.name());
61-
filter.invoke(new MockInvoker(() -> {
62+
consumerFilter.invoke(new MockInvoker(() -> {
6263
assertThat(RootContext.getXID()).isEqualTo(DEFAULT_XID);
6364
assertThat(RootContext.getBranchType()).isEqualTo(BranchType.SAGA);
6465
}), null);

0 commit comments

Comments
 (0)