-
Notifications
You must be signed in to change notification settings - Fork 8.9k
feature : mock server #6205
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
feature : mock server #6205
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ec6c1ed
mock server
Bughue 34ae9f7
license
Bughue 45e85f5
style
Bughue ad4064d
style
Bughue 777bdb7
license
Bughue 8b92b8d
license
Bughue cf0bfbb
fix
Bughue c8455da
expect
Bughue a27ba75
expect
Bughue 48f047a
style
Bughue 63c7371
changes
Bughue a05f7a6
test
Bughue 8fc0867
test
Bughue 818c1db
test
Bughue ea1d9c4
Update pom.xml
xingfudeshi 3859655
Merge branch '2.x' into dev-mlv-mockserver
xingfudeshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
|
|
||
| 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. | ||
|
|
||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <groupId>io.seata</groupId> | ||
| <artifactId>seata-parent</artifactId> | ||
| <version>${revision}</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>seata-mock-server</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>seata-mock-server</name> | ||
| <description>Seata mock server</description> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-deploy-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.seata</groupId> | ||
| <artifactId>seata-server</artifactId> | ||
| <version>${project.version}</version> | ||
| <exclusions> | ||
| <exclusion> | ||
| <artifactId>slf4j-log4j12</artifactId> | ||
| <groupId>org.slf4j</groupId> | ||
| </exclusion> | ||
| <exclusion> | ||
| <artifactId>log4j</artifactId> | ||
| <groupId>log4j</groupId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
|
||
| </project> | ||
54 changes: 54 additions & 0 deletions
54
test-mock-server/src/main/java/io/seata/mockserver/ExpectTransactionResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * 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 io.seata.mockserver; | ||
|
|
||
| /** | ||
| * The enum Expect transaction result. | ||
| * | ||
| */ | ||
| public enum ExpectTransactionResult { | ||
|
|
||
| AllCommitted(0, "all success"), | ||
| AllRollbacked(1, "all rollback"), | ||
| PhaseOneTimeoutRollbacked(2, "phase one failed"); | ||
|
|
||
| private final int code; | ||
| private final String desc; | ||
|
|
||
| ExpectTransactionResult(int code, String desc) { | ||
| this.code = code; | ||
| this.desc = desc; | ||
| } | ||
|
|
||
| /** | ||
| * Gets code. | ||
| * | ||
| * @return the code | ||
| */ | ||
| public int getCode() { | ||
| return code; | ||
| } | ||
|
|
||
| public static ExpectTransactionResult covert(int code) { | ||
| for (ExpectTransactionResult result : ExpectTransactionResult.values()) { | ||
| if (result.getCode() == code) { | ||
| return result; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
232 changes: 232 additions & 0 deletions
232
test-mock-server/src/main/java/io/seata/mockserver/MockCoordinator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| /* | ||
| * 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 io.seata.mockserver; | ||
|
|
||
| import io.seata.common.util.CollectionUtils; | ||
| import io.seata.core.exception.TransactionException; | ||
| import io.seata.core.model.BranchStatus; | ||
| import io.seata.core.model.GlobalStatus; | ||
| import io.seata.core.protocol.AbstractMessage; | ||
| import io.seata.core.protocol.AbstractResultMessage; | ||
| import io.seata.core.protocol.ResultCode; | ||
| import io.seata.core.protocol.transaction.AbstractTransactionRequestToTC; | ||
| import io.seata.core.protocol.transaction.BranchRegisterRequest; | ||
| import io.seata.core.protocol.transaction.BranchRegisterResponse; | ||
| import io.seata.core.protocol.transaction.BranchReportRequest; | ||
| import io.seata.core.protocol.transaction.BranchReportResponse; | ||
| import io.seata.core.protocol.transaction.GlobalBeginRequest; | ||
| import io.seata.core.protocol.transaction.GlobalBeginResponse; | ||
| import io.seata.core.protocol.transaction.GlobalCommitRequest; | ||
| import io.seata.core.protocol.transaction.GlobalCommitResponse; | ||
| import io.seata.core.protocol.transaction.GlobalLockQueryRequest; | ||
| import io.seata.core.protocol.transaction.GlobalLockQueryResponse; | ||
| import io.seata.core.protocol.transaction.GlobalReportRequest; | ||
| import io.seata.core.protocol.transaction.GlobalReportResponse; | ||
| import io.seata.core.protocol.transaction.GlobalRollbackRequest; | ||
| import io.seata.core.protocol.transaction.GlobalRollbackResponse; | ||
| import io.seata.core.protocol.transaction.GlobalStatusRequest; | ||
| import io.seata.core.protocol.transaction.GlobalStatusResponse; | ||
| import io.seata.core.rpc.Disposable; | ||
| import io.seata.core.rpc.RemotingServer; | ||
| import io.seata.core.rpc.RpcContext; | ||
| import io.seata.core.rpc.TransactionMessageHandler; | ||
| import io.seata.mockserver.call.CallRm; | ||
| import io.seata.server.AbstractTCInboundHandler; | ||
| import io.seata.server.UUIDGenerator; | ||
| import io.seata.server.session.BranchSession; | ||
| import io.seata.server.session.GlobalSession; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| /** | ||
| * Mock Coordinator | ||
| **/ | ||
| public class MockCoordinator extends AbstractTCInboundHandler implements TransactionMessageHandler, Disposable { | ||
|
|
||
| protected static final Logger LOGGER = LoggerFactory.getLogger(MockCoordinator.class); | ||
|
|
||
| RemotingServer remotingServer; | ||
|
|
||
| private static MockCoordinator coordinator; | ||
|
|
||
| private Map<String, ExpectTransactionResult> expectTransactionResultMap; | ||
| private Map<String, Integer> expectRetryTimesMap; | ||
| private Map<String, List<BranchSession>> branchMap; | ||
|
|
||
| private MockCoordinator() { | ||
| } | ||
|
|
||
|
|
||
| public static MockCoordinator getInstance() { | ||
| if (coordinator == null) { | ||
| synchronized (MockCoordinator.class) { | ||
| if (coordinator == null) { | ||
| coordinator = new MockCoordinator(); | ||
| coordinator.expectTransactionResultMap = new ConcurrentHashMap<>(); | ||
| coordinator.expectRetryTimesMap = new ConcurrentHashMap<>(); | ||
| coordinator.branchMap = new ConcurrentHashMap<>(); | ||
| } | ||
| } | ||
| } | ||
| return coordinator; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void destroy() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public AbstractResultMessage onRequest(AbstractMessage request, RpcContext context) { | ||
| if (!(request instanceof AbstractTransactionRequestToTC)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| AbstractTransactionRequestToTC transactionRequest = (AbstractTransactionRequestToTC) request; | ||
| transactionRequest.setTCInboundHandler(this); | ||
|
|
||
| return transactionRequest.handle(context); | ||
| } | ||
|
|
||
| @Override | ||
| public void onResponse(AbstractResultMessage response, RpcContext context) { | ||
| response.setResultCode(ResultCode.Success); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doGlobalBegin(GlobalBeginRequest request, GlobalBeginResponse response, RpcContext rpcContext) throws TransactionException { | ||
| GlobalSession session = GlobalSession.createGlobalSession(rpcContext.getApplicationId(), | ||
| rpcContext.getTransactionServiceGroup(), request.getTransactionName(), request.getTimeout()); | ||
| expectTransactionResultMap.putIfAbsent(session.getXid(), ExpectTransactionResult.AllCommitted); | ||
| response.setXid(session.getXid()); | ||
| response.setResultCode(ResultCode.Success); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doGlobalCommit(GlobalCommitRequest request, GlobalCommitResponse response, RpcContext rpcContext) throws TransactionException { | ||
| response.setGlobalStatus(GlobalStatus.Committed); | ||
| response.setResultCode(ResultCode.Success); | ||
|
|
||
| int retry = expectRetryTimesMap.getOrDefault(request.getXid(), 0); | ||
| List<BranchSession> branchSessions = branchMap.get(request.getXid()); | ||
| if (CollectionUtils.isEmpty(branchSessions)) { | ||
| LOGGER.info("branchSessions is empty,XID=" + request.getXid()); | ||
| } | ||
| branchSessions.forEach(branch -> { | ||
| CallRm.branchCommit(remotingServer, branch.getResourceId(), branch.getClientId()); | ||
| IntStream.range(0, retry).forEach(i -> | ||
| CallRm.branchCommit(remotingServer, branch.getResourceId(), branch.getClientId())); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doGlobalRollback(GlobalRollbackRequest request, GlobalRollbackResponse response, RpcContext rpcContext) throws TransactionException { | ||
| response.setGlobalStatus(GlobalStatus.Rollbacked); | ||
| response.setResultCode(ResultCode.Success); | ||
|
|
||
|
|
||
| int retry = expectRetryTimesMap.getOrDefault(request.getXid(), 0); | ||
| List<BranchSession> branchSessions = branchMap.get(request.getXid()); | ||
| if (CollectionUtils.isEmpty(branchSessions)) { | ||
| LOGGER.info("branchSessions is empty,XID=" + request.getXid()); | ||
| } | ||
| branchSessions.forEach(branch -> { | ||
| CallRm.branchRollback(remotingServer, branch.getResourceId(), branch.getClientId()); | ||
| IntStream.range(0, retry).forEach(i -> | ||
| CallRm.branchRollback(remotingServer, branch.getResourceId(), branch.getClientId())); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doBranchRegister(BranchRegisterRequest request, BranchRegisterResponse response, RpcContext rpcContext) throws TransactionException { | ||
|
|
||
| BranchSession branchSession = new BranchSession(request.getBranchType()); | ||
|
|
||
| String xid = request.getXid(); | ||
| branchSession.setXid(xid); | ||
| // branchSession.setTransactionId(request.getTransactionId()); | ||
| branchSession.setBranchId(UUIDGenerator.generateUUID()); | ||
| branchSession.setResourceId(request.getResourceId()); | ||
| branchSession.setLockKey(request.getLockKey()); | ||
| branchSession.setClientId(rpcContext.getClientId()); | ||
| branchSession.setApplicationData(request.getApplicationData()); | ||
| branchSession.setStatus(BranchStatus.Registered); | ||
| branchMap.compute(xid, (key, val) -> { | ||
| if (val == null) { | ||
| val = new ArrayList<>(); | ||
| } | ||
| val.add(branchSession); | ||
| return val; | ||
| }); | ||
|
|
||
| response.setBranchId(branchSession.getBranchId()); | ||
| response.setResultCode(ResultCode.Success); | ||
|
|
||
| // Thread thread = new Thread(() -> { | ||
| // try { | ||
| // Thread.sleep(1000); | ||
| // if (ProtocolConstants.VERSION_0 != Version.calcProtocolVersion(rpcContext.getVersion())) { | ||
| // CallRm.deleteUndoLog(remotingServer, resourceId, clientId); | ||
| // } | ||
| // } catch (Exception e) { | ||
| // e.printStackTrace(); | ||
| // } | ||
| // }); | ||
| // thread.start(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doBranchReport(BranchReportRequest request, BranchReportResponse response, RpcContext rpcContext) throws TransactionException { | ||
| response.setResultCode(ResultCode.Success); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doLockCheck(GlobalLockQueryRequest request, GlobalLockQueryResponse response, RpcContext rpcContext) throws TransactionException { | ||
| response.setResultCode(ResultCode.Success); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doGlobalStatus(GlobalStatusRequest request, GlobalStatusResponse response, RpcContext rpcContext) throws TransactionException { | ||
| response.setGlobalStatus(GlobalStatus.Committed); | ||
| response.setResultCode(ResultCode.Success); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doGlobalReport(GlobalReportRequest request, GlobalReportResponse response, RpcContext rpcContext) throws TransactionException { | ||
| response.setGlobalStatus(GlobalStatus.Committed); | ||
| response.setResultCode(ResultCode.Success); | ||
| } | ||
|
|
||
| public void setRemotingServer(RemotingServer remotingServer) { | ||
| this.remotingServer = remotingServer; | ||
| } | ||
|
|
||
|
|
||
| public void setExpectedResult(String xid, ExpectTransactionResult expected) { | ||
| expectTransactionResultMap.put(xid, expected); | ||
| } | ||
|
|
||
| public void setExpectedRetry(String xid, int times) { | ||
| expectRetryTimesMap.put(xid, times); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.