use of io.seata.server.coordinator.DefaultCoordinator in project seata by seata.
the class AbstractServerTest method startSeataServer.
protected static void startSeataServer() throws InterruptedException {
(new Thread(new Runnable() {
@Override
public void run() {
File file = new File("sessionStore/root.data");
if (file.exists()) {
file.delete();
}
ParameterParser parameterParser = new ParameterParser(new String[] {});
// initialize the metrics
MetricsManager.get().init();
System.setProperty(ConfigurationKeys.STORE_MODE, parameterParser.getStoreMode());
nettyServer = new NettyRemotingServer(workingThreads);
// server port
nettyServer.setListenPort(parameterParser.getPort());
UUIDGenerator.init(parameterParser.getServerNode());
// log store mode : filećdb
SessionHolder.init(parameterParser.getStoreMode());
DefaultCoordinator coordinator = new DefaultCoordinator(nettyServer);
coordinator.init();
nettyServer.setHandler(coordinator);
// register ShutdownHook
ShutdownHook.getInstance().addDisposable(coordinator);
// 127.0.0.1 and 0.0.0.0 are not valid here.
if (NetUtil.isValidIp(parameterParser.getHost(), false)) {
XID.setIpAddress(parameterParser.getHost());
} else {
XID.setIpAddress(NetUtil.getLocalIp());
}
XID.setPort(nettyServer.getListenPort());
nettyServer.init();
}
})).start();
Thread.sleep(5000);
}
use of io.seata.server.coordinator.DefaultCoordinator in project seata by seata.
the class TmNettyClientTest method testDoConnect.
/**
* Client rely on server's starting first
*
* @throws Exception
*/
@Test
public void testDoConnect() throws Exception {
// start services server first
workingThreads.submit(new Runnable() {
@Override
public void run() {
NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(workingThreads);
nettyRemotingServer.setHandler(new DefaultCoordinator(nettyRemotingServer));
UUIDGenerator.init(1L);
nettyRemotingServer.init();
}
});
// then test client
Thread.sleep(3000);
String applicationId = "app 1";
String transactionServiceGroup = "group A";
TmNettyRemotingClient tmNettyRemotingClient = TmNettyRemotingClient.getInstance(applicationId, transactionServiceGroup);
tmNettyRemotingClient.init();
Method doConnectMethod = TmNettyRemotingClient.class.getDeclaredMethod("doConnect", String.class);
doConnectMethod.setAccessible(true);
String serverAddress = "0.0.0.0:8091";
Channel channel = (Channel) doConnectMethod.invoke(tmNettyRemotingClient, serverAddress);
Assertions.assertNotNull(channel);
}
use of io.seata.server.coordinator.DefaultCoordinator in project seata by seata.
the class TmNettyClientTest method testSendMsgWithResponse.
@Test
public void testSendMsgWithResponse() throws Exception {
workingThreads.submit(new Runnable() {
@Override
public void run() {
NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(workingThreads);
nettyRemotingServer.setHandler(new DefaultCoordinator(nettyRemotingServer));
UUIDGenerator.init(1L);
nettyRemotingServer.init();
}
});
Thread.sleep(3000);
String applicationId = "app 1";
String transactionServiceGroup = "my_test_tx_group";
TmNettyRemotingClient tmNettyRemotingClient = TmNettyRemotingClient.getInstance(applicationId, transactionServiceGroup);
tmNettyRemotingClient.init();
Method doConnectMethod = TmNettyRemotingClient.class.getDeclaredMethod("doConnect", String.class);
doConnectMethod.setAccessible(true);
String serverAddress = "0.0.0.0:8091";
Channel channel = (Channel) doConnectMethod.invoke(tmNettyRemotingClient, serverAddress);
Assertions.assertNotNull(channel);
BranchRegisterRequest request = new BranchRegisterRequest();
request.setXid("127.0.0.1:8091:1249853");
request.setLockKey("lock key testSendMsgWithResponse");
request.setResourceId("resoutceId1");
BranchRegisterResponse branchRegisterResponse = (BranchRegisterResponse) tmNettyRemotingClient.sendSyncRequest(request);
Assertions.assertNotNull(branchRegisterResponse);
Assertions.assertEquals(ResultCode.Failed, branchRegisterResponse.getResultCode());
Assertions.assertEquals("RuntimeException[SessionManager is NOT init!]", branchRegisterResponse.getMsg());
}
use of io.seata.server.coordinator.DefaultCoordinator in project seata by seata.
the class DefaultCoreForEventBusTest method test.
@Test
public void test() throws IOException, TransactionException, InterruptedException {
class GlobalTransactionEventSubscriber {
private final Map<GlobalStatus, AtomicInteger> eventCounters;
public Map<GlobalStatus, AtomicInteger> getEventCounters() {
return eventCounters;
}
public GlobalTransactionEventSubscriber() {
this.eventCounters = new ConcurrentHashMap<>();
}
@Subscribe
public void processGlobalTransactionEvent(GlobalTransactionEvent event) {
AtomicInteger counter = eventCounters.computeIfAbsent(event.getStatus(), status -> new AtomicInteger(0));
counter.addAndGet(1);
}
}
SessionHolder.init(null);
RemotingServer remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
DefaultCoordinator coordinator = new DefaultCoordinator(remotingServer);
coordinator.init();
try {
DefaultCore core = new DefaultCore(remotingServer);
GlobalTransactionEventSubscriber subscriber = new GlobalTransactionEventSubscriber();
// avoid transactional interference from other unit tests
Thread.sleep(1500);
EventBusManager.get().register(subscriber);
// start a transaction
String xid = core.begin("test_app_id", "default_group", "test_tran_name", 30000);
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
// commit this transaction
core.commit(xid);
// we need sleep for a short while because default canBeCommittedAsync() is true
Thread.sleep(1000);
// check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.AsyncCommitting).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Committed).get());
// start another new transaction
xid = core.begin("test_app_id", "default_group", "test_tran_name2", 30000);
Assertions.assertEquals(2, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
core.rollback(xid);
// check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacking).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacked).get());
// start more one new transaction for test timeout and let this transaction immediately timeout
xid = core.begin("test_app_id", "default_group", "test_tran_name3", 0);
// sleep for check -> DefaultCoordinator.timeoutCheck
Thread.sleep(1000);
// at lease retry once because DefaultCoordinator.timeoutCheck is 1 second
Assertions.assertTrue(subscriber.getEventCounters().get(GlobalStatus.TimeoutRollbacking).get() >= 1);
} finally {
coordinator.destroy();
SessionHolder.destroy();
}
}
use of io.seata.server.coordinator.DefaultCoordinator in project XHuiCloud by sindaZeng.
the class Server method main.
/**
* The entry point of application.
*
* @param args the input arguments
* @throws IOException the io exception
*/
public static void main(String[] args) throws IOException {
// get port first, use to logback.xml
int port = PortHelper.getPort(args);
System.setProperty(ConfigurationKeys.SERVER_PORT, Integer.toString(port));
// create logger
final Logger logger = LoggerFactory.getLogger(Server.class);
if (ContainerHelper.isRunningInContainer()) {
logger.info("The server is running in container.");
}
// initialize the parameter parser
// Note that the parameter parser should always be the first line to execute.
// Because, here we need to parse the parameters needed for startup.
ParameterParser parameterParser = new ParameterParser(args);
// initialize the metrics
MetricsManager.get().init();
System.setProperty(ConfigurationKeys.STORE_MODE, parameterParser.getStoreMode());
NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(WORKING_THREADS);
// server port
nettyRemotingServer.setListenPort(parameterParser.getPort());
UUIDGenerator.init(parameterParser.getServerNode());
// log store mode : file, db, redis
SessionHolder.init(parameterParser.getStoreMode());
DefaultCoordinator coordinator = new DefaultCoordinator(nettyRemotingServer);
coordinator.init();
nettyRemotingServer.setHandler(coordinator);
// register ShutdownHook
ShutdownHook.getInstance().addDisposable(coordinator);
ShutdownHook.getInstance().addDisposable(nettyRemotingServer);
// 127.0.0.1 and 0.0.0.0 are not valid here.
if (NetUtil.isValidIp(parameterParser.getHost(), false)) {
XID.setIpAddress(parameterParser.getHost());
} else {
XID.setIpAddress(NetUtil.getLocalIp());
}
XID.setPort(nettyRemotingServer.getListenPort());
try {
nettyRemotingServer.init();
} catch (Throwable e) {
logger.error("nettyServer init error:{}", e.getMessage(), e);
System.exit(-1);
}
System.exit(0);
}
Aggregations