From 9571f891bdc40c73917cc1aac14892e3ff565fac Mon Sep 17 00:00:00 2001 From: slievrly Date: Sat, 22 Jun 2024 18:11:49 +0800 Subject: [PATCH 1/2] bugfix: fix hsf ConsumerModel convert error --- .../hsf/HsfTransactionConsumerFilter.java | 61 +++++++++++++++++++ ...java => HsfTransactionProviderFilter.java} | 40 +++++------- ...com.taobao.hsf.invocation.filter.RPCFilter | 3 +- 3 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionConsumerFilter.java rename integration/hsf/src/main/java/org/apache/seata/integration/hsf/{HsfTransactionFilter.java => HsfTransactionProviderFilter.java} (70%) diff --git a/integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionConsumerFilter.java b/integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionConsumerFilter.java new file mode 100644 index 00000000000..c291f30df89 --- /dev/null +++ b/integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionConsumerFilter.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.seata.integration.hsf; + +import com.taobao.hsf.context.RPCContext; +import com.taobao.hsf.invocation.Invocation; +import com.taobao.hsf.invocation.InvocationHandler; +import com.taobao.hsf.invocation.RPCResult; +import com.taobao.hsf.invocation.filter.ClientFilter; +import com.taobao.hsf.util.concurrent.ListenableFuture; +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.model.BranchType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The type Hsf transaction consumer filter. + */ +public class HsfTransactionConsumerFilter implements ClientFilter { + + private static final Logger LOGGER = LoggerFactory.getLogger(HsfTransactionConsumerFilter.class); + + @Override + public ListenableFuture invoke(InvocationHandler nextHandler, Invocation invocation) throws Throwable { + String xid = RootContext.getXID(); + BranchType branchType = RootContext.getBranchType(); + + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("xid in RootContext[{}], branchType in RootContext[{}]", xid, branchType); + } + if (xid != null) { + RPCContext.getClientContext().putAttachment(RootContext.KEY_XID, xid); + RPCContext.getClientContext().putAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name()); + } + try { + return nextHandler.invoke(invocation); + } finally { + RPCContext.getClientContext().removeAttachment(RootContext.KEY_XID); + RPCContext.getClientContext().removeAttachment(RootContext.KEY_BRANCH_TYPE); + } + } + + @Override + public void onResponse(Invocation invocation, RPCResult rpcResult) { + + } +} diff --git a/integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionFilter.java b/integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionProviderFilter.java similarity index 70% rename from integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionFilter.java rename to integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionProviderFilter.java index c2d59193079..3c85858afb4 100644 --- a/integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionFilter.java +++ b/integration/hsf/src/main/java/org/apache/seata/integration/hsf/HsfTransactionProviderFilter.java @@ -20,7 +20,6 @@ import com.taobao.hsf.invocation.Invocation; import com.taobao.hsf.invocation.InvocationHandler; import com.taobao.hsf.invocation.RPCResult; -import com.taobao.hsf.invocation.filter.ClientFilter; import com.taobao.hsf.invocation.filter.ServerFilter; import com.taobao.hsf.util.concurrent.ListenableFuture; import org.apache.seata.common.util.StringUtils; @@ -30,42 +29,33 @@ import org.slf4j.LoggerFactory; /** - * The type Transaction propagation filter. - * + * The type Hsf transaction provider filter. */ -public class HsfTransactionFilter implements ClientFilter, ServerFilter { +public class HsfTransactionProviderFilter implements ServerFilter { - private static final Logger LOGGER = LoggerFactory.getLogger(HsfTransactionFilter.class); + private static final Logger LOGGER = LoggerFactory.getLogger(HsfTransactionProviderFilter.class); @Override - public ListenableFuture invoke(InvocationHandler invocationHandler, Invocation invocation) - throws Throwable { - String xid = RootContext.getXID(); - BranchType branchType = RootContext.getBranchType(); + public ListenableFuture invoke(InvocationHandler nextHandler, Invocation invocation) throws Throwable { Object rpcXid = RPCContext.getServerContext().getAttachment(RootContext.KEY_XID); Object rpcBranchType = RPCContext.getServerContext().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.getClientContext().putAttachment(RootContext.KEY_XID, xid); - RPCContext.getClientContext().putAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name()); - } else { - if (rpcXid != null) { - RootContext.bind(rpcXid.toString()); - if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType.toString())) { - RootContext.bindBranchType(BranchType.TCC); - } - bind = true; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("bind xid [{}] branchType [{}] to RootContext", rpcXid, rpcBranchType); - } + if (rpcXid != null) { + RootContext.bind(rpcXid.toString()); + if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType.toString())) { + RootContext.bindBranchType(BranchType.TCC); + } + bind = true; + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("bind xid [{}] branchType [{}] to RootContext", rpcXid, rpcBranchType); } } try { - return invocationHandler.invoke(invocation); + return nextHandler.invoke(invocation); } finally { if (bind) { BranchType previousBranchType = RootContext.getBranchType(); @@ -89,8 +79,6 @@ public ListenableFuture invoke(InvocationHandler invocationHandler, I } } } - RPCContext.getClientContext().removeAttachment(RootContext.KEY_XID); - RPCContext.getClientContext().removeAttachment(RootContext.KEY_BRANCH_TYPE); RPCContext.getServerContext().removeAttachment(RootContext.KEY_XID); RPCContext.getServerContext().removeAttachment(RootContext.KEY_BRANCH_TYPE); } diff --git a/integration/hsf/src/main/resources/META-INF/services/com.taobao.hsf.invocation.filter.RPCFilter b/integration/hsf/src/main/resources/META-INF/services/com.taobao.hsf.invocation.filter.RPCFilter index bd53bc9e9e3..371dd4f3952 100644 --- a/integration/hsf/src/main/resources/META-INF/services/com.taobao.hsf.invocation.filter.RPCFilter +++ b/integration/hsf/src/main/resources/META-INF/services/com.taobao.hsf.invocation.filter.RPCFilter @@ -1 +1,2 @@ -org.apache.seata.integration.hsf.HsfTransactionFilter \ No newline at end of file +org.apache.seata.integration.hsf.HsfTransactionConsumerFilter +org.apache.seata.integration.hsf.HsfTransactionProviderFilter \ No newline at end of file From 93430ef1bc8a9a7e5bfdf5a6777ce6e4038f7fa7 Mon Sep 17 00:00:00 2001 From: slievrly Date: Sat, 22 Jun 2024 18:16:15 +0800 Subject: [PATCH 2/2] add change --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + 2 files changed, 2 insertions(+) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index f4de08bc261..4fb6f6fad76 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -8,6 +8,7 @@ Add changes here for all PR submitted to the 2.x branch. ### bugfix: - [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async annotation not working in ClusterWatcherManager - [[#6624](https://github.com/apache/incubator-seata/pull/6624)] fix Alibaba Dubbo convert error +- [[#6626](https://github.com/apache/incubator-seata/pull/6626)] fix hsf ConsumerModel convert error ### optimize: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index f9a0eab0c7c..e7099f400f2 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -7,6 +7,7 @@ ### bugfix: - [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async注解ClusterWatcherManager中不生效的问题 - [[#6624](https://github.com/apache/incubator-seata/pull/6624)] 修复 Alibaba Dubbo 转换错误 +- [[#6626](https://github.com/apache/incubator-seata/pull/6626)] 修复 hsf ConsumerModel 转换错误 ### optimize: