Skip to content
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

bugfix: fix the key parameter problem of sofa-rpc setAttachment() method #5745

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
bugfix: fix the key parameter problem of sofa-rpc setAttachment() method
  • Loading branch information
dmego committed Jul 24, 2023
commit 2beb8d01a873b74541d7503ea5b0224f020ceddc
5 changes: 5 additions & 0 deletions common/src/main/java/io/seata/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public interface Constants {
*/
String ROW_LOCK_KEY_SPLIT_CHAR = ";";

/**
* The constant HIDE_KEY_PREFIX_CHAR.
*/
String HIDE_KEY_PREFIX_CHAR = ".";

/**
* the start time of transaction
*/
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/io/seata/core/context/RootContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import io.seata.common.Constants;
import io.seata.common.exception.ShouldNeverHappenException;
import io.seata.common.util.StringUtils;
import io.seata.core.model.BranchType;
Expand Down Expand Up @@ -47,6 +48,11 @@ private RootContext() {
*/
public static final String KEY_XID = "TX_XID";

/**
* The constant HIDDEN_KEY_XID.
*/
public static final String HIDDEN_KEY_XID = Constants.HIDE_KEY_PREFIX_CHAR + KEY_XID;

/**
* The constant KEY_TIMEOUT.
*/
Expand All @@ -69,6 +75,11 @@ private RootContext() {
*/
public static final String KEY_BRANCH_TYPE = "TX_BRANCH_TYPE";

/**
* The constant HIDDEN_KEY_BRANCH_TYPE
*/
public static final String HIDDEN_KEY_BRANCH_TYPE = Constants.HIDE_KEY_PREFIX_CHAR + KEY_BRANCH_TYPE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment.


/**
* The constant KEY_GLOBAL_LOCK_FLAG, VALUE_GLOBAL_LOCK_FLAG
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ public SofaResponse invoke(FilterInvoker filterInvoker, SofaRequest sofaRequest)
* @return
*/
private String getRpcXid() {
String rpcXid = (String) RpcInternalContext.getContext().getAttachment(RootContext.KEY_XID);
String rpcXid = (String) RpcInternalContext.getContext().getAttachment(RootContext.HIDDEN_KEY_XID);
if (rpcXid == null) {
rpcXid = (String) RpcInternalContext.getContext().getAttachment(RootContext.KEY_XID.toLowerCase());
rpcXid = (String) RpcInternalContext.getContext().getAttachment(RootContext.HIDDEN_KEY_XID.toLowerCase());
}
return rpcXid;
}
private String getBranchType() {
return (String) RpcInternalContext.getContext().getAttachment(RootContext.KEY_BRANCH_TYPE);
return (String) RpcInternalContext.getContext().getAttachment(RootContext.HIDDEN_KEY_BRANCH_TYPE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public SofaResponse invoke(FilterInvoker filterInvoker, SofaRequest sofaRequest)
}
boolean bind = false;
if (xid != null) {
RpcInternalContext.getContext().setAttachment(RootContext.KEY_XID, xid);
RpcInternalContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name());
RpcInternalContext.getContext().setAttachment(RootContext.HIDDEN_KEY_XID, xid);
RpcInternalContext.getContext().setAttachment(RootContext.HIDDEN_KEY_BRANCH_TYPE, branchType.name());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some case to prove RpcInternalContext.getContext().setAttachment(RootContext.KEY_XID, xid); not as expected ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test case added

} else {
if (null != rpcXid) {
RootContext.bind(rpcXid);
Expand All @@ -72,6 +72,10 @@ public SofaResponse invoke(FilterInvoker filterInvoker, SofaRequest sofaRequest)
try {
return filterInvoker.invoke(sofaRequest);
} finally {
if (xid != null) {
RpcInternalContext.getContext().removeAttachment(RootContext.HIDDEN_KEY_XID);
RpcInternalContext.getContext().removeAttachment(RootContext.HIDDEN_KEY_BRANCH_TYPE);
}
if (bind) {
String unbindXid = RootContext.unbind();
if (LOGGER.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.seata.integration.sofa.rpc;

import io.seata.core.context.RootContext;
import io.seata.core.model.BranchType;

/**
* @author Geng Zhang
Expand All @@ -26,6 +27,8 @@ public class HelloServiceImpl implements HelloService {

private String xid;

private BranchType branchType;

public HelloServiceImpl() {

}
Expand All @@ -37,10 +40,15 @@ public HelloServiceImpl(String result) {
@Override
public String sayHello(String name, int age) {
xid = RootContext.getXID();
branchType = RootContext.getBranchType();
return result != null ? result : "hello " + name + " from server! age: " + age;
}

public String getXid() {
return xid;
}

public BranchType getBranchType() {
return branchType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.seata.integration.sofa.rpc;

import io.seata.core.context.RootContext;
import io.seata.core.model.BranchType;

/**
* @author Geng Zhang
Expand All @@ -24,6 +25,8 @@ public class HelloServiceProxy implements HelloService {

private String xid;

private BranchType branchType;

private HelloService proxy;

public HelloServiceProxy(HelloService proxy) {
Expand All @@ -33,10 +36,15 @@ public HelloServiceProxy(HelloService proxy) {
@Override
public String sayHello(String name, int age) {
xid = RootContext.getXID();
branchType = RootContext.getBranchType();
return proxy.sayHello(name, age);
}

public String getXid() {
return xid;
}

public BranchType getBranchType() {
return branchType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import io.seata.core.context.RootContext;
import io.seata.core.model.BranchType;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -91,25 +92,32 @@ public void testAll() {
helloService.sayHello("xxx", 22);
// check C
Assertions.assertNull(helloServiceImpl.getXid());
Assertions.assertNull(helloServiceImpl.getBranchType());
// check B
Assertions.assertNull(helloServiceProxy.getXid());
Assertions.assertNull(helloServiceProxy.getBranchType());
} catch (Exception e) {
Assertions.assertTrue(e instanceof SofaRpcException);
} finally {
Assertions.assertNull(RootContext.unbind());
Assertions.assertNull(RootContext.unbindBranchType());
}

RootContext.bind("xidddd");
RootContext.bindBranchType(BranchType.AT);
try {
helloService.sayHello("xxx", 22);
// check C
Assertions.assertEquals(helloServiceImpl.getXid(), "xidddd");
Assertions.assertEquals(helloServiceImpl.getBranchType(), BranchType.AT);
// check B
Assertions.assertEquals(helloServiceProxy.getXid(), "xidddd");
Assertions.assertEquals(helloServiceProxy.getBranchType(), BranchType.AT);
} catch (Exception e) {
Assertions.assertTrue(e instanceof SofaRpcException);
} finally {
Assertions.assertEquals("xidddd", RootContext.unbind());
Assertions.assertEquals(BranchType.AT, RootContext.unbindBranchType());
}
}

Expand Down
Loading