Search in sources :

Example 1 with MessageFuture

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

the class ClientOnResponseProcessor method process.

@Override
public void process(ChannelHandlerContext ctx, RpcMessage rpcMessage) throws Exception {
    if (rpcMessage.getBody() instanceof MergeResultMessage) {
        MergeResultMessage results = (MergeResultMessage) rpcMessage.getBody();
        MergedWarpMessage mergeMessage = (MergedWarpMessage) mergeMsgMap.remove(rpcMessage.getId());
        for (int i = 0; i < mergeMessage.msgs.size(); i++) {
            int msgId = mergeMessage.msgIds.get(i);
            MessageFuture future = futures.remove(msgId);
            if (future == null) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("msg: {} is not found in futures.", msgId);
                }
            } else {
                future.setResultMessage(results.getMsgs()[i]);
            }
        }
    } else {
        MessageFuture messageFuture = futures.remove(rpcMessage.getId());
        if (messageFuture != null) {
            messageFuture.setResultMessage(rpcMessage.getBody());
        } else {
            if (rpcMessage.getBody() instanceof AbstractResultMessage) {
                if (transactionMessageHandler != null) {
                    transactionMessageHandler.onResponse((AbstractResultMessage) rpcMessage.getBody(), null);
                }
            }
        }
    }
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) MessageFuture(io.seata.core.protocol.MessageFuture)

Example 2 with MessageFuture

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

the class ServerOnResponseProcessor method process.

@Override
public void process(ChannelHandlerContext ctx, RpcMessage rpcMessage) throws Exception {
    MessageFuture messageFuture = futures.remove(rpcMessage.getId());
    if (messageFuture != null) {
        messageFuture.setResultMessage(rpcMessage.getBody());
    } else {
        if (ChannelManager.isRegistered(ctx.channel())) {
            onResponseMessage(ctx, rpcMessage);
        } else {
            try {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("closeChannelHandlerContext channel:" + ctx.channel());
                }
                ctx.disconnect();
                ctx.close();
            } catch (Exception exx) {
                LOGGER.error(exx.getMessage());
            }
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info(String.format("close a unhandled connection! [%s]", ctx.channel().toString()));
            }
        }
    }
}
Also used : MessageFuture(io.seata.core.protocol.MessageFuture)

Example 3 with MessageFuture

use of io.seata.core.protocol.MessageFuture 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 4 with MessageFuture

use of io.seata.core.protocol.MessageFuture 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)

Aggregations

MessageFuture (io.seata.core.protocol.MessageFuture)4 Channel (io.netty.channel.Channel)2 FrameworkException (io.seata.common.exception.FrameworkException)2 RpcMessage (io.seata.core.protocol.RpcMessage)2 TimeoutException (java.util.concurrent.TimeoutException)2 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 NamedThreadFactory (io.seata.common.thread.NamedThreadFactory)1 PositiveAtomicCounter (io.seata.common.thread.PositiveAtomicCounter)1 AbstractResultMessage (io.seata.core.protocol.AbstractResultMessage)1 MergeResultMessage (io.seata.core.protocol.MergeResultMessage)1 MergedWarpMessage (io.seata.core.protocol.MergedWarpMessage)1 MessageType (io.seata.core.protocol.MessageType)1 MessageTypeAware (io.seata.core.protocol.MessageTypeAware)1 ProtocolConstants (io.seata.core.protocol.ProtocolConstants)1 Disposable (io.seata.core.rpc.Disposable)1 RpcHook (io.seata.core.rpc.hook.RpcHook)1 Pair (io.seata.core.rpc.processor.Pair)1