Search in sources :

Example 1 with DefaultCoordinator

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);
}
Also used : NettyRemotingServer(io.seata.core.rpc.netty.NettyRemotingServer) ParameterParser(io.seata.server.ParameterParser) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) File(java.io.File)

Example 2 with DefaultCoordinator

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);
}
Also used : Channel(io.netty.channel.Channel) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultCoordinator

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());
}
Also used : BranchRegisterResponse(io.seata.core.protocol.transaction.BranchRegisterResponse) BranchRegisterRequest(io.seata.core.protocol.transaction.BranchRegisterRequest) Channel(io.netty.channel.Channel) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultCoordinator

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();
    }
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultCore(io.seata.server.coordinator.DefaultCore) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) RemotingServer(io.seata.core.rpc.RemotingServer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) GlobalTransactionEvent(io.seata.core.event.GlobalTransactionEvent) Test(org.junit.jupiter.api.Test) DefaultCoordinatorTest(io.seata.server.coordinator.DefaultCoordinatorTest)

Example 5 with DefaultCoordinator

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);
}
Also used : NettyRemotingServer(io.seata.core.rpc.netty.NettyRemotingServer) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) Logger(org.slf4j.Logger)

Aggregations

DefaultCoordinator (io.seata.server.coordinator.DefaultCoordinator)8 NettyRemotingServer (io.seata.core.rpc.netty.NettyRemotingServer)4 Test (org.junit.jupiter.api.Test)4 Method (java.lang.reflect.Method)3 Channel (io.netty.channel.Channel)2 Logger (org.slf4j.Logger)2 NamedThreadFactory (io.seata.common.thread.NamedThreadFactory)1 GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)1 GlobalStatus (io.seata.core.model.GlobalStatus)1 BranchRegisterRequest (io.seata.core.protocol.transaction.BranchRegisterRequest)1 BranchRegisterResponse (io.seata.core.protocol.transaction.BranchRegisterResponse)1 RemotingServer (io.seata.core.rpc.RemotingServer)1 ParameterParser (io.seata.server.ParameterParser)1 DefaultCoordinatorTest (io.seata.server.coordinator.DefaultCoordinatorTest)1 DefaultCore (io.seata.server.coordinator.DefaultCore)1 File (java.io.File)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1