Skip to content

bugfix: fix Alibaba Dubbo convert error #6628

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

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#4410](https://github.com/seata/seata/pull/4410)] support jdk9+ compile code
- [[#6104](https://github.com/seata/seata/pull/6104)] fix dubbo 3.x consumer can't generate TCC proxy in tcc mode.
- [[#6409](https://github.com/seata/seata/pull/6409)] fix the failure of RemotingParser to prevent AT mode from executing.
- [[#6628](https://github.com/seata/seata/pull/6628)] fix Alibaba Dubbo convert error.

### optimize:
- [[#6044](https://github.com/seata/seata/pull/6044)] optimize derivative product check base on mysql
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [[#4410](https://github.com/seata/seata/pull/4410)] 修复jdk9+版本编译后,引入后ByteBuffer#flip NoSuchMethodError的问题
- [[#6104](https://github.com/seata/seata/pull/6104)] 修复在TCC模式下, dubbo 3.x版本消费者端不能生成TCC代理的问题
- [[#6409](https://github.com/seata/seata/pull/6409)] 修复 RemotingParser 失败导致 AT 模式无法执行的问题
- [[#6628](https://github.com/seata/seata/pull/6628)] 修复 Alibaba Dubbo 转换错误

### optimize:
- [[#6044](https://github.com/seata/seata/pull/6044)] 优化MySQL衍生数据库判断逻辑
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.integration.dubbo.alibaba;

import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;

import io.seata.core.constants.DubboConstants;
import io.seata.core.context.RootContext;
import io.seata.core.model.BranchType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The type Alibaba dubbo transaction consumer filter.
*/
@Activate(group = {DubboConstants.CONSUMER}, order = 100)
public class AlibabaDubboTransactionConsumerFilter implements Filter {

private static final Logger LOGGER = LoggerFactory.getLogger(AlibabaDubboTransactionConsumerFilter.class);

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (!DubboConstants.ALIBABADUBBO) {
return invoker.invoke(invocation);
}
String xid = RootContext.getXID();
BranchType branchType = RootContext.getBranchType();

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("consumer xid in RootContext[{}], branchType in RootContext[{}]", xid, branchType);
}
if (xid != null) {
RpcContext.getContext().setAttachment(RootContext.KEY_XID, xid);
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name());
}
try {
return invoker.invoke(invocation);
} finally {
RpcContext.getContext().removeAttachment(RootContext.KEY_XID);
RpcContext.getContext().removeAttachment(RootContext.KEY_BRANCH_TYPE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,41 @@
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;

import io.seata.common.util.StringUtils;
import io.seata.core.context.RootContext;
import io.seata.core.constants.DubboConstants;
import io.seata.core.context.RootContext;
import io.seata.core.model.BranchType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The type Transaction propagation filter.
*
* @author sharajava
* The type Alibaba dubbo transaction provider filter.
*/
@Activate(group = {DubboConstants.PROVIDER, DubboConstants.CONSUMER}, order = 100)
public class AlibabaDubboTransactionPropagationFilter implements Filter {
@Activate(group = {DubboConstants.PROVIDER}, order = 100)
public class AlibabaDubboTransactionProviderFilter implements Filter {

private static final Logger LOGGER = LoggerFactory.getLogger(AlibabaDubboTransactionPropagationFilter.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AlibabaDubboTransactionProviderFilter.class);

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (!DubboConstants.ALIBABADUBBO) {
return invoker.invoke(invocation);
}
String xid = RootContext.getXID();
BranchType branchType = RootContext.getBranchType();

String rpcXid = getRpcXid();
String rpcBranchType = RpcContext.getContext().getAttachment(RootContext.KEY_BRANCH_TYPE);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("xid in RootContext[{}] xid in RpcContext[{}]", xid, rpcXid);
LOGGER.debug("xid in RpcContext[{}], branchType in RpcContext[{}]", rpcXid, rpcBranchType);
}
boolean bind = false;
if (xid != null) {
RpcContext.getContext().setAttachment(RootContext.KEY_XID, xid);
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name());
} else {
if (rpcXid != null) {
RootContext.bind(rpcXid);
if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType)) {
RootContext.bindBranchType(BranchType.TCC);
}
bind = true;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("bind xid [{}] branchType [{}] to RootContext", rpcXid, rpcBranchType);
}
if (rpcXid != null) {
RootContext.bind(rpcXid);
if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType)) {
RootContext.bindBranchType(BranchType.TCC);
}
bind = true;
}

try {
return invoker.invoke(invocation);
} finally {
Expand All @@ -82,7 +71,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
}
if (!rpcXid.equalsIgnoreCase(unbindXid)) {
LOGGER.warn("xid in change during RPC from {} to {},branchType from {} to {}", rpcXid, unbindXid,
rpcBranchType != null ? rpcBranchType : "AT", previousBranchType);
rpcBranchType != null ? rpcBranchType : BranchType.AT, previousBranchType);
if (unbindXid != null) {
RootContext.bind(unbindXid);
LOGGER.warn("bind xid [{}] back to RootContext", unbindXid);
Expand All @@ -93,15 +82,14 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
}
}
}
RpcContext.getContext().removeAttachment(RootContext.KEY_XID);
RpcContext.getContext().removeAttachment(RootContext.KEY_BRANCH_TYPE);
RpcContext.getServerContext().removeAttachment(RootContext.KEY_XID);
RpcContext.getServerContext().removeAttachment(RootContext.KEY_BRANCH_TYPE);
}
}

/**
* get rpc xid
*
* @return
*/
private String getRpcXid() {
Expand All @@ -111,5 +99,4 @@ private String getRpcXid() {
}
return rpcXid;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
"condition": {
"typeReachable": "com.alibaba.dubbo.rpc.Filter"
},
"name": "io.seata.integration.dubbo.alibaba.AlibabaDubboTransactionPropagationFilter",
"name": "io.seata.integration.dubbo.alibaba.AlibabaDubboTransactionConsumerFilter",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"condition": {
"typeReachable": "com.alibaba.dubbo.rpc.Filter"
},
"name": "io.seata.integration.dubbo.alibaba.AlibabaDubboTransactionProviderFilter",
"methods": [
{
"name": "<init>",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.seata.integration.dubbo.alibaba.AlibabaDubboTransactionPropagationFilter
io.seata.integration.dubbo.alibaba.AlibabaDubboTransactionConsumerFilter
io.seata.integration.dubbo.alibaba.AlibabaDubboTransactionProviderFilter
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,27 @@
*/
package io.seata.integration.dubbo.alibaba;

import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcContext;

import io.seata.core.context.RootContext;
import io.seata.core.model.BranchType;
import io.seata.integration.dubbo.alibaba.mock.MockInvoker;
import org.junit.jupiter.api.Test;

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

/**
* @author wang.liang
*/
public class AlibabaDubboTransactionPropagationFilterTest {

private static final String DEFAULT_XID = "1234567890";

@Test
public void testInvoke_And_RootContext() {
AlibabaDubboTransactionPropagationFilter filter = new AlibabaDubboTransactionPropagationFilter();
AlibabaDubboTransactionProviderFilter providerFilter = new AlibabaDubboTransactionProviderFilter();

// SAGA
RpcContext.getContext().setAttachment(RootContext.KEY_XID, DEFAULT_XID);
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, BranchType.SAGA.name());
filter.invoke(new MockInvoker(() -> {
providerFilter.invoke(new MockInvoker(() -> {
assertThat(RootContext.getXID()).isEqualTo(DEFAULT_XID);
assertThat(RootContext.getBranchType()).isEqualTo(BranchType.AT);
}), null);
Expand All @@ -49,19 +45,20 @@ public void testInvoke_And_RootContext() {
// TCC
RpcContext.getContext().setAttachment(RootContext.KEY_XID, DEFAULT_XID);
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, BranchType.TCC.name());
filter.invoke(new MockInvoker(() -> {
providerFilter.invoke(new MockInvoker(() -> {
assertThat(RootContext.getXID()).isEqualTo(DEFAULT_XID);
assertThat(RootContext.getBranchType()).isEqualTo(BranchType.TCC);
}), null);
assertThat(RootContext.unbind()).isNull();
assertThat(RootContext.unbindBranchType()).isNull();

// TCC
AlibabaDubboTransactionConsumerFilter consumerFilter = new AlibabaDubboTransactionConsumerFilter();
RootContext.bind(DEFAULT_XID);
RootContext.bindBranchType(BranchType.SAGA);
RpcContext.getContext().setAttachment(RootContext.KEY_XID, DEFAULT_XID);
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, BranchType.TCC.name());
filter.invoke(new MockInvoker(() -> {
consumerFilter.invoke(new MockInvoker(() -> {
assertThat(RootContext.getXID()).isEqualTo(DEFAULT_XID);
assertThat(RootContext.getBranchType()).isEqualTo(BranchType.SAGA);
}), null);
Expand Down
Loading