Search in sources :

Example 6 with RpcContext

use of io.seata.core.rpc.RpcContext in project seata by seata.

the class ChannelManager method updateChannelsResource.

private static void updateChannelsResource(String resourceId, String clientIp, String applicationId) {
    ConcurrentMap<Integer, RpcContext> sourcePortMap = RM_CHANNELS.get(resourceId).get(applicationId).get(clientIp);
    for (ConcurrentMap.Entry<String, ConcurrentMap<String, ConcurrentMap<String, ConcurrentMap<Integer, RpcContext>>>> rmChannelEntry : RM_CHANNELS.entrySet()) {
        if (rmChannelEntry.getKey().equals(resourceId)) {
            continue;
        }
        ConcurrentMap<String, ConcurrentMap<String, ConcurrentMap<Integer, RpcContext>>> applicationIdMap = rmChannelEntry.getValue();
        if (!applicationIdMap.containsKey(applicationId)) {
            continue;
        }
        ConcurrentMap<String, ConcurrentMap<Integer, RpcContext>> clientIpMap = applicationIdMap.get(applicationId);
        if (!clientIpMap.containsKey(clientIp)) {
            continue;
        }
        ConcurrentMap<Integer, RpcContext> portMap = clientIpMap.get(clientIp);
        for (ConcurrentMap.Entry<Integer, RpcContext> portMapEntry : portMap.entrySet()) {
            Integer port = portMapEntry.getKey();
            if (!sourcePortMap.containsKey(port)) {
                RpcContext rpcContext = portMapEntry.getValue();
                sourcePortMap.put(port, rpcContext);
                rpcContext.holdInResourceManagerChannels(resourceId, port);
            }
        }
    }
}
Also used : RpcContext(io.seata.core.rpc.RpcContext) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 7 with RpcContext

use of io.seata.core.rpc.RpcContext in project seata by seata.

the class ChannelManager method buildChannelHolder.

private static RpcContext buildChannelHolder(NettyPoolKey.TransactionRole clientRole, String version, String applicationId, String txServiceGroup, String dbkeys, Channel channel) {
    RpcContext holder = new RpcContext();
    holder.setClientRole(clientRole);
    holder.setVersion(version);
    holder.setClientId(buildClientId(applicationId, channel));
    holder.setApplicationId(applicationId);
    holder.setTransactionServiceGroup(txServiceGroup);
    holder.addResources(dbKeytoSet(dbkeys));
    holder.setChannel(channel);
    return holder;
}
Also used : RpcContext(io.seata.core.rpc.RpcContext)

Example 8 with RpcContext

use of io.seata.core.rpc.RpcContext in project seata by seata.

the class ChannelManager method getSameClientChannel.

/**
 * Gets get same income client channel.
 *
 * @param channel the channel
 * @return the get same income client channel
 */
public static Channel getSameClientChannel(Channel channel) {
    if (channel.isActive()) {
        return channel;
    }
    RpcContext rpcContext = getContextFromIdentified(channel);
    if (rpcContext == null) {
        LOGGER.error("rpcContext is null,channel:{},active:{}", channel, channel.isActive());
        return null;
    }
    if (rpcContext.getChannel().isActive()) {
        // recheck
        return rpcContext.getChannel();
    }
    Integer clientPort = ChannelUtil.getClientPortFromChannel(channel);
    NettyPoolKey.TransactionRole clientRole = rpcContext.getClientRole();
    if (clientRole == NettyPoolKey.TransactionRole.TMROLE) {
        String clientIdentified = rpcContext.getApplicationId() + Constants.CLIENT_ID_SPLIT_CHAR + ChannelUtil.getClientIpFromChannel(channel);
        if (!TM_CHANNELS.containsKey(clientIdentified)) {
            return null;
        }
        ConcurrentMap<Integer, RpcContext> clientRpcMap = TM_CHANNELS.get(clientIdentified);
        return getChannelFromSameClientMap(clientRpcMap, clientPort);
    } else if (clientRole == NettyPoolKey.TransactionRole.RMROLE) {
        for (Map<Integer, RpcContext> clientRmMap : rpcContext.getClientRMHolderMap().values()) {
            Channel sameClientChannel = getChannelFromSameClientMap(clientRmMap, clientPort);
            if (sameClientChannel != null) {
                return sameClientChannel;
            }
        }
    }
    return null;
}
Also used : RpcContext(io.seata.core.rpc.RpcContext) Channel(io.netty.channel.Channel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 9 with RpcContext

use of io.seata.core.rpc.RpcContext in project seata by seata.

the class ServerOnResponseProcessor method onResponseMessage.

private void onResponseMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("server received:{},clientIp:{},vgroup:{}", rpcMessage.getBody(), NetUtil.toIpAddress(ctx.channel().remoteAddress()), ChannelManager.getContextFromIdentified(ctx.channel()).getTransactionServiceGroup());
    } else {
        try {
            BatchLogHandler.INSTANCE.getLogQueue().put(rpcMessage.getBody() + ",clientIp:" + NetUtil.toIpAddress(ctx.channel().remoteAddress()) + ",vgroup:" + ChannelManager.getContextFromIdentified(ctx.channel()).getTransactionServiceGroup());
        } catch (InterruptedException e) {
            LOGGER.error("put message to logQueue error: {}", e.getMessage(), e);
        }
    }
    if (rpcMessage.getBody() instanceof AbstractResultMessage) {
        RpcContext rpcContext = ChannelManager.getContextFromIdentified(ctx.channel());
        transactionMessageHandler.onResponse((AbstractResultMessage) rpcMessage.getBody(), rpcContext);
    }
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) RpcContext(io.seata.core.rpc.RpcContext)

Example 10 with RpcContext

use of io.seata.core.rpc.RpcContext in project seata by seata.

the class ServerOnRequestProcessor method onRequestMessage.

private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) {
    Object message = rpcMessage.getBody();
    RpcContext rpcContext = ChannelManager.getContextFromIdentified(ctx.channel());
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("server received:{},clientIp:{},vgroup:{}", message, NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup());
    } else {
        try {
            BatchLogHandler.INSTANCE.getLogQueue().put(message + ",clientIp:" + NetUtil.toIpAddress(ctx.channel().remoteAddress()) + ",vgroup:" + rpcContext.getTransactionServiceGroup());
        } catch (InterruptedException e) {
            LOGGER.error("put message to logQueue error: {}", e.getMessage(), e);
        }
    }
    if (!(message instanceof AbstractMessage)) {
        return;
    }
    if (message instanceof MergedWarpMessage) {
        AbstractResultMessage[] results = new AbstractResultMessage[((MergedWarpMessage) message).msgs.size()];
        for (int i = 0; i < results.length; i++) {
            final AbstractMessage subMessage = ((MergedWarpMessage) message).msgs.get(i);
            results[i] = transactionMessageHandler.onRequest(subMessage, rpcContext);
        }
        MergeResultMessage resultMessage = new MergeResultMessage();
        resultMessage.setMsgs(results);
        remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), resultMessage);
    } else {
        // the single send request message
        final AbstractMessage msg = (AbstractMessage) message;
        AbstractResultMessage result = transactionMessageHandler.onRequest(msg, rpcContext);
        remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), result);
    }
}
Also used : RpcContext(io.seata.core.rpc.RpcContext) AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) AbstractMessage(io.seata.core.protocol.AbstractMessage) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) MergeResultMessage(io.seata.core.protocol.MergeResultMessage)

Aggregations

RpcContext (io.seata.core.rpc.RpcContext)10 ConcurrentMap (java.util.concurrent.ConcurrentMap)5 Channel (io.netty.channel.Channel)4 HashMap (java.util.HashMap)3 FrameworkException (io.seata.common.exception.FrameworkException)2 AbstractResultMessage (io.seata.core.protocol.AbstractResultMessage)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Constants (io.seata.common.Constants)1 CollectionUtils (io.seata.common.util.CollectionUtils)1 StringUtils (io.seata.common.util.StringUtils)1 AbstractMessage (io.seata.core.protocol.AbstractMessage)1 IncompatibleVersionException (io.seata.core.protocol.IncompatibleVersionException)1 MergeResultMessage (io.seata.core.protocol.MergeResultMessage)1 MergedWarpMessage (io.seata.core.protocol.MergedWarpMessage)1 RegisterRMRequest (io.seata.core.protocol.RegisterRMRequest)1 RegisterTMRequest (io.seata.core.protocol.RegisterTMRequest)1 Version (io.seata.core.protocol.Version)1 GlobalBeginRequest (io.seata.core.protocol.transaction.GlobalBeginRequest)1 GlobalBeginResponse (io.seata.core.protocol.transaction.GlobalBeginResponse)1