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

test : fix mockserverTest fail cause using same port with seata-server #6325

Merged
merged 33 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 30 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/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] unbind xid in TransactionTemplateTest
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] increase common module unit test coverage
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] increase seata-core module unit test coverage
- [[#6325](https://github.com/apache/incubator-seata/pull/6325)] fix mockServerTest fail cause using same port with seata-server

### refactor:
- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] refactor Saga designer using diagram-js
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] TransactionTemplateTest单测unbind xid
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] 增加common模块单测覆盖率
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] 增加seata-core模块单测覆盖率
- [[#6325](https://github.com/apache/incubator-seata/pull/6325)] 修复mock-server相关测试用例

### refactor:
- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] 使用diagram-js重构Saga设计器
Expand Down
69 changes: 67 additions & 2 deletions test-mock-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,79 @@
<description>Seata mock server</description>

<build>
<finalName>seata-mock-server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>org.apache.seata.mockserver.MockServer</mainClass>
<!-- <layout>ZIP</layout>-->
<attach>false</attach>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<spring-boot-for-server.version>2.7.17</spring-boot-for-server.version>
<spring-framework-for-server.version>5.3.30</spring-framework-for-server.version>
<snakeyaml-for-server.version>2.0</snakeyaml-for-server.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- junit5 -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit-jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- spring-framework-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring-framework-for-server.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-for-server.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
</exclusion>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml-for-server.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.seata</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ protected void doGlobalCommit(GlobalCommitRequest request, GlobalCommitResponse
IntStream.range(0, retry).forEach(i ->
CallRm.branchCommit(remotingServer, branch));
});
branchMap.remove(request.getXid());
globalStatusMap.remove(request.getXid());
}

@Override
Expand All @@ -167,6 +169,8 @@ protected void doGlobalRollback(GlobalRollbackRequest request, GlobalRollbackRes
IntStream.range(0, retry).forEach(i ->
CallRm.branchRollback(remotingServer, branch));
});
branchMap.remove(request.getXid());
globalStatusMap.remove(request.getXid());
}

@Override
Expand All @@ -192,23 +196,20 @@ protected void doBranchRegister(BranchRegisterRequest request, BranchRegisterRes

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 {
checkMockActionFail(request.getXid());
String xid = request.getXid();
branchMap.compute(xid, (key, val) -> {
if (val != null) {
val.stream().filter(branch -> branch.getBranchId() == request.getBranchId()).forEach(branch -> {
branch.setApplicationData(request.getApplicationData());
});
}
return val;
});
response.setResultCode(ResultCode.Success);
}

Expand All @@ -224,7 +225,7 @@ protected void doGlobalStatus(GlobalStatusRequest request, GlobalStatusResponse
checkMockActionFail(request.getXid());
GlobalStatus globalStatus = globalStatusMap.get(request.getXid());
if (globalStatus == null) {
globalStatus = GlobalStatus.UnKnown;
globalStatus = GlobalStatus.Finished;
}
response.setGlobalStatus(globalStatus);
response.setResultCode(ResultCode.Success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.seata.common.XID;
import org.apache.seata.common.thread.NamedThreadFactory;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.server.ParameterParser;
import org.apache.seata.server.UUIDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,39 +42,62 @@ public class MockServer {
private static ThreadPoolExecutor workingThreads;
private static MockNettyRemotingServer nettyRemotingServer;

private static volatile boolean inited = false;

public static final int DEFAULT_PORT = 8091;

/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
SpringApplication.run(MockServer.class, args);
start();

ParameterParser parameterParser = new ParameterParser(args);
int port = parameterParser.getPort() > 0 ? parameterParser.getPort() : DEFAULT_PORT;
start(port);
}

public static void start() {
workingThreads = new ThreadPoolExecutor(50,
50, 500, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(20000),
new NamedThreadFactory("ServerHandlerThread", 500), new ThreadPoolExecutor.CallerRunsPolicy());
nettyRemotingServer = new MockNettyRemotingServer(workingThreads);

// set registry
XID.setIpAddress(NetUtil.getLocalIp());
XID.setPort(8092);
// init snowflake for transactionId, branchId
UUIDGenerator.init(1L);

MockCoordinator coordinator = MockCoordinator.getInstance();
coordinator.setRemotingServer(nettyRemotingServer);
nettyRemotingServer.setHandler(coordinator);
nettyRemotingServer.init();

LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName());
public static void start(int port) {
if (!inited) {
synchronized (MockServer.class) {
if (!inited) {
inited = true;
workingThreads = new ThreadPoolExecutor(50,
50, 500, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(20000),
new NamedThreadFactory("ServerHandlerThread", 500), new ThreadPoolExecutor.CallerRunsPolicy());
nettyRemotingServer = new MockNettyRemotingServer(workingThreads);

// set registry
XID.setIpAddress(NetUtil.getLocalIp());
XID.setPort(port);
// init snowflake for transactionId, branchId
UUIDGenerator.init(1L);

MockCoordinator coordinator = MockCoordinator.getInstance();
coordinator.setRemotingServer(nettyRemotingServer);
nettyRemotingServer.setHandler(coordinator);
nettyRemotingServer.init();

LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName());
}
}
}


}

public static void close() {
workingThreads.shutdown();
nettyRemotingServer.destroy();
if (inited) {
synchronized (MockServer.class) {
if (inited) {
inited = false;
workingThreads.shutdown();
nettyRemotingServer.destroy();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class CallRm {
public static BranchStatus branchCommit(RemotingServer remotingServer, BranchSession branchSession) {
BranchCommitRequest request = new BranchCommitRequest();
setReq(request, branchSession);

try {
BranchCommitResponse response = (BranchCommitResponse) remotingServer.sendSyncRequest(
branchSession.getResourceId(), branchSession.getClientId(), request, false);
Expand Down Expand Up @@ -98,8 +97,7 @@ private static void setReq(AbstractBranchEndRequest request, BranchSession branc
request.setXid(branchSession.getXid());
request.setBranchId(branchSession.getBranchId());
request.setResourceId(branchSession.getResourceId());
request.setApplicationData("{\"k\":\"v\"}");
request.setBranchType(BranchType.TCC);
// todo AT SAGA
request.setApplicationData(branchSession.getApplicationData());
request.setBranchType(branchSession.getBranchType());
}
}
1 change: 1 addition & 0 deletions test-mock-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#
server:
port: 7091
servicePort: 8091

spring:
application:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.common;

import org.apache.commons.lang.ObjectUtils;
import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.config.ConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* the type ConfigurationTestHelper
**/
public class ConfigurationTestHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationTestHelper.class);
private static final long PUT_CONFIG_TIMEOUT = 30000L;
private static final long PUT_CONFIG_CHECK_GAP = 500L;

public static void removeConfig(String dataId) {
putConfig(dataId, null);
}

public static void putConfig(String dataId, String content) {
Copy link
Member

Choose a reason for hiding this comment

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

You want to understand what the purpose of mock-server is? Connecting to the Seata-Server as if using a normal SDK eliminates the cumbersome Seata-Server setup, which is useful for testing client integration. If the user gets this mock-server, need to write such complex logic to test it, what is the point of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand the point of mock-server, this class is only used for tests where you need to dynamically change the configuration in the unit test. It is only involved in the mock-server unit tests because it shares a key with TC-server.
Normal users don't need to use this test class to use mock-server, if they want to change the port they just need to use mock-server/resource/application.yml
This complexity arises because the unit tests for both mock-server and seata-server are in one package, or maybe we can create another test subproject for mock-server?

我理解mock-server的意义,这个类仅用于测试时需要动态改变单元测试里的配置。在mock-server的单元测试里因为和TC-server共用了一个key才会涉及它。
正常的用户在使用mock-server里是不需要用到这个test类的,他们如果想改变端口只需要使用mock-server/resource/application.yml
这个复杂性的出现是因为mock-server和seata-server的单元测试都在一个包里,又或者我们可以为mock-server再建一个test子项目?

ConfigurationCache.addConfigListener(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
if (content == null) {
System.clearProperty(dataId);
ConfigurationFactory.getInstance().removeConfig(dataId);
return;
}

System.setProperty(dataId, content);
ConfigurationFactory.getInstance().putConfig(dataId, content);

long start = System.currentTimeMillis();
while (!ObjectUtils.equals(content, ConfigurationFactory.getInstance().getConfig(dataId))) {
Bughue marked this conversation as resolved.
Show resolved Hide resolved
if (PUT_CONFIG_TIMEOUT < System.currentTimeMillis() - start) {
LOGGER.error("putConfig timeout, dataId={}, timeout={}ms", dataId, PUT_CONFIG_TIMEOUT);
return;
}
try {
Thread.sleep(PUT_CONFIG_CHECK_GAP);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
LOGGER.info("putConfig ok, dataId={}, cost {}ms", dataId, System.currentTimeMillis() - start);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static ConcurrentMap<String, Channel> getChannelConcurrentMap(AbstractNet

public static Channel getChannel(TmNettyRemotingClient client) {
return getChannelManager(client)
.acquireChannel(ProtocolTestConstants.SERVER_ADDRESS);
.acquireChannel(ProtocolTestConstants.MOCK_SERVER_ADDRESS);
}
private static NettyClientChannelManager getChannelManager(AbstractNettyRemotingClient remotingClient) {
return remotingClient.getClientChannelManager();
Expand Down
Loading
Loading