Search in sources :

Example 6 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class AbstractNettyRemotingServer method sendAsyncResponse.

@Override
public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg) {
    Channel clientChannel = channel;
    if (!(msg instanceof HeartbeatMessage)) {
        clientChannel = ChannelManager.getSameClientChannel(channel);
    }
    if (clientChannel != null) {
        RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, msg instanceof HeartbeatMessage ? ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE : ProtocolConstants.MSGTYPE_RESPONSE);
        super.sendAsync(clientChannel, rpcMsg);
    } else {
        throw new RuntimeException("channel is error.");
    }
}
Also used : HeartbeatMessage(io.seata.core.protocol.HeartbeatMessage) Channel(io.netty.channel.Channel) RpcMessage(io.seata.core.protocol.RpcMessage)

Example 7 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class ClientChannelHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof RpcMessage) {
        RpcMessage rpcMessage = (RpcMessage) msg;
        int msgId = rpcMessage.getId();
        DefaultPromise future = (DefaultPromise) client.futureMap.remove(msgId);
        if (future != null) {
            future.setSuccess(msg);
        } else {
            LOGGER.warn("miss msg id:{}", msgId);
        }
    }
}
Also used : DefaultPromise(io.netty.util.concurrent.DefaultPromise) RpcMessage(io.seata.core.protocol.RpcMessage)

Example 8 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class ProtocolV1Client method sendRpc.

public Future sendRpc(Map<String, String> head, Object body) {
    int msgId = idGenerator.incrementAndGet();
    RpcMessage rpcMessage = new RpcMessage();
    rpcMessage.setId(msgId);
    rpcMessage.setCodec(SerializerType.SEATA.getCode());
    rpcMessage.setCompressor(ProtocolConstants.CONFIGURED_COMPRESSOR);
    rpcMessage.setHeadMap(head);
    rpcMessage.setBody(body);
    rpcMessage.setMessageType(ProtocolConstants.MSGTYPE_RESQUEST_SYNC);
    if (channel != null) {
        DefaultPromise promise = new DefaultPromise(defaultEventExecutor);
        futureMap.put(msgId, promise);
        channel.writeAndFlush(rpcMessage);
        return promise;
    } else {
        LOGGER.warn("channel is null");
    }
    return null;
}
Also used : DefaultPromise(io.netty.util.concurrent.DefaultPromise) RpcMessage(io.seata.core.protocol.RpcMessage)

Example 9 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class ProtocolV1Client method main.

// can test tps
public static void main(String[] args) {
    ProtocolV1Client client = new ProtocolV1Client();
    client.connect("127.0.0.1", 8811, 500);
    Map<String, String> head = new HashMap<>();
    head.put("tracerId", "xxadadadada");
    head.put("token", "adadadad");
    BranchCommitRequest body = new BranchCommitRequest();
    body.setBranchId(12345L);
    body.setApplicationData("application");
    body.setBranchType(BranchType.AT);
    body.setResourceId("resource-1234");
    body.setXid("xid-1234");
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    // no queue
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), new NamedThreadFactory("client-", false));
    for (int i = 0; i < threads; i++) {
        service1.execute(() -> {
            while (true) {
                try {
                    Future future = client.sendRpc(head, body);
                    RpcMessage resp = (RpcMessage) future.get(200, TimeUnit.MILLISECONDS);
                    if (resp != null) {
                        cnt.incrementAndGet();
                    }
                } catch (Exception e) {
                // ignore
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NamedThreadFactory(io.seata.common.thread.NamedThreadFactory) BranchCommitRequest(io.seata.core.protocol.transaction.BranchCommitRequest) RpcMessage(io.seata.core.protocol.RpcMessage) AtomicLong(java.util.concurrent.atomic.AtomicLong) Future(java.util.concurrent.Future) ChannelFuture(io.netty.channel.ChannelFuture) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 10 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class ServerChannelHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    Channel channel = ctx.channel();
    if (msg instanceof RpcMessage) {
        ((RpcMessage) msg).setMessageType(ProtocolConstants.MSGTYPE_RESPONSE);
    }
    channel.writeAndFlush(msg);
}
Also used : Channel(io.netty.channel.Channel) RpcMessage(io.seata.core.protocol.RpcMessage)

Aggregations

RpcMessage (io.seata.core.protocol.RpcMessage)16 Channel (io.netty.channel.Channel)6 NamedThreadFactory (io.seata.common.thread.NamedThreadFactory)3 HashMap (java.util.HashMap)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 DefaultPromise (io.netty.util.concurrent.DefaultPromise)2 FrameworkException (io.seata.common.exception.FrameworkException)2 Compressor (io.seata.core.compressor.Compressor)2 HeartbeatMessage (io.seata.core.protocol.HeartbeatMessage)2 MessageFuture (io.seata.core.protocol.MessageFuture)2 BranchCommitRequest (io.seata.core.protocol.transaction.BranchCommitRequest)2 Serializer (io.seata.core.serializer.Serializer)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Future (java.util.concurrent.Future)2 TimeoutException (java.util.concurrent.TimeoutException)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 FrameworkErrorCode (io.seata.common.exception.FrameworkErrorCode)1 EnhancedServiceLoader (io.seata.common.loader.EnhancedServiceLoader)1