Search in sources :

Example 11 with RpcMessage

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

the class AbstractNettyRemoting method buildResponseMessage.

protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byte messageType) {
    RpcMessage rpcMsg = new RpcMessage();
    rpcMsg.setMessageType(messageType);
    // same with request
    rpcMsg.setCodec(rpcMessage.getCodec());
    rpcMsg.setCompressor(rpcMessage.getCompressor());
    rpcMsg.setBody(msg);
    rpcMsg.setId(rpcMessage.getId());
    return rpcMsg;
}
Also used : RpcMessage(io.seata.core.protocol.RpcMessage)

Example 12 with RpcMessage

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

the class AbstractNettyRemoting method sendSync.

/**
 * rpc sync request
 * Obtain the return result through MessageFuture blocking.
 *
 * @param channel       netty channel
 * @param rpcMessage    rpc message
 * @param timeoutMillis rpc communication timeout
 * @return response message
 * @throws TimeoutException
 */
protected Object sendSync(Channel channel, RpcMessage rpcMessage, long timeoutMillis) throws TimeoutException {
    if (timeoutMillis <= 0) {
        throw new FrameworkException("timeout should more than 0ms");
    }
    if (channel == null) {
        LOGGER.warn("sendSync nothing, caused by null channel.");
        return null;
    }
    MessageFuture messageFuture = new MessageFuture();
    messageFuture.setRequestMessage(rpcMessage);
    messageFuture.setTimeout(timeoutMillis);
    futures.put(rpcMessage.getId(), messageFuture);
    channelWritableCheck(channel, rpcMessage.getBody());
    String remoteAddr = ChannelUtil.getAddressFromChannel(channel);
    doBeforeRpcHooks(remoteAddr, rpcMessage);
    channel.writeAndFlush(rpcMessage).addListener((ChannelFutureListener) future -> {
        if (!future.isSuccess()) {
            MessageFuture messageFuture1 = futures.remove(rpcMessage.getId());
            if (messageFuture1 != null) {
                messageFuture1.setResultMessage(future.cause());
            }
            destroyChannel(future.channel());
        }
    });
    try {
        Object result = messageFuture.get(timeoutMillis, TimeUnit.MILLISECONDS);
        doAfterRpcHooks(remoteAddr, rpcMessage, result);
        return result;
    } catch (Exception exx) {
        LOGGER.error("wait response error:{},ip:{},request:{}", exx.getMessage(), channel.remoteAddress(), rpcMessage.getBody());
        if (exx instanceof TimeoutException) {
            throw (TimeoutException) exx;
        } else {
            throw new RuntimeException(exx);
        }
    }
}
Also used : SocketAddress(java.net.SocketAddress) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Random(java.util.Random) Pair(io.seata.core.rpc.processor.Pair) RpcMessage(io.seata.core.protocol.RpcMessage) PositiveAtomicCounter(io.seata.common.thread.PositiveAtomicCounter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ProtocolConstants(io.seata.core.protocol.ProtocolConstants) ManagementFactory(java.lang.management.ManagementFactory) ExecutorService(java.util.concurrent.ExecutorService) EnhancedServiceLoader(io.seata.common.loader.EnhancedServiceLoader) MessageTypeAware(io.seata.core.protocol.MessageTypeAware) Logger(org.slf4j.Logger) RpcHook(io.seata.core.rpc.hook.RpcHook) MessageFuture(io.seata.core.protocol.MessageFuture) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Disposable(io.seata.core.rpc.Disposable) FrameworkErrorCode(io.seata.common.exception.FrameworkErrorCode) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) NamedThreadFactory(io.seata.common.thread.NamedThreadFactory) List(java.util.List) MDC(org.slf4j.MDC) RemotingProcessor(io.seata.core.rpc.processor.RemotingProcessor) FrameworkException(io.seata.common.exception.FrameworkException) MessageType(io.seata.core.protocol.MessageType) FrameworkException(io.seata.common.exception.FrameworkException) MessageFuture(io.seata.core.protocol.MessageFuture) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) FrameworkException(io.seata.common.exception.FrameworkException) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with RpcMessage

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

the class AbstractNettyRemotingServer method sendAsyncRequest.

@Override
public void sendAsyncRequest(Channel channel, Object msg) {
    if (channel == null) {
        throw new RuntimeException("client is not connected");
    }
    RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY);
    super.sendAsync(channel, rpcMessage);
}
Also used : RpcMessage(io.seata.core.protocol.RpcMessage)

Example 14 with RpcMessage

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

the class AbstractNettyRemotingClient method sendSyncRequest.

@Override
public Object sendSyncRequest(Object msg) throws TimeoutException {
    String serverAddress = loadBalance(getTransactionServiceGroup(), msg);
    int timeoutMillis = NettyClientConfig.getRpcRequestTimeout();
    RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC);
    // put message into basketMap, @see MergedSendRunnable
    if (NettyClientConfig.isEnableClientBatchSendRequest()) {
        // send batch message is sync request, needs to create messageFuture and put it in futures.
        MessageFuture messageFuture = new MessageFuture();
        messageFuture.setRequestMessage(rpcMessage);
        messageFuture.setTimeout(timeoutMillis);
        futures.put(rpcMessage.getId(), messageFuture);
        // put message into basketMap
        BlockingQueue<RpcMessage> basket = CollectionUtils.computeIfAbsent(basketMap, serverAddress, key -> new LinkedBlockingQueue<>());
        if (!basket.offer(rpcMessage)) {
            LOGGER.error("put message into basketMap offer failed, serverAddress:{},rpcMessage:{}", serverAddress, rpcMessage);
            return null;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("offer message: {}", rpcMessage.getBody());
        }
        if (!isSending) {
            synchronized (mergeLock) {
                mergeLock.notifyAll();
            }
        }
        try {
            return messageFuture.get(timeoutMillis, TimeUnit.MILLISECONDS);
        } catch (Exception exx) {
            LOGGER.error("wait response error:{},ip:{},request:{}", exx.getMessage(), serverAddress, rpcMessage.getBody());
            if (exx instanceof TimeoutException) {
                throw (TimeoutException) exx;
            } else {
                throw new RuntimeException(exx);
            }
        }
    } else {
        Channel channel = clientChannelManager.acquireChannel(serverAddress);
        return super.sendSync(channel, rpcMessage, timeoutMillis);
    }
}
Also used : Channel(io.netty.channel.Channel) MessageFuture(io.seata.core.protocol.MessageFuture) RpcMessage(io.seata.core.protocol.RpcMessage) TimeoutException(java.util.concurrent.TimeoutException) FrameworkException(io.seata.common.exception.FrameworkException) TimeoutException(java.util.concurrent.TimeoutException)

Example 15 with RpcMessage

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

the class AbstractNettyRemotingClient method sendAsyncRequest.

@Override
public void sendAsyncRequest(Channel channel, Object msg) {
    if (channel == null) {
        LOGGER.warn("sendAsyncRequest nothing, caused by null channel.");
        return;
    }
    RpcMessage rpcMessage = buildRequestMessage(msg, msg instanceof HeartbeatMessage ? ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST : ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY);
    if (rpcMessage.getBody() instanceof MergeMessage) {
        mergeMsgMap.put(rpcMessage.getId(), (MergeMessage) rpcMessage.getBody());
    }
    super.sendAsync(channel, rpcMessage);
}
Also used : HeartbeatMessage(io.seata.core.protocol.HeartbeatMessage) RpcMessage(io.seata.core.protocol.RpcMessage) MergeMessage(io.seata.core.protocol.MergeMessage)

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