Search in sources :

Example 1 with Session

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session in project incubator-eventmesh by apache.

the class ClientGroupWrapper method initClientGroupPersistentConsumer.

public synchronized void initClientGroupPersistentConsumer() throws Exception {
    if (inited4Persistent.get()) {
        return;
    }
    Properties keyValue = new Properties();
    keyValue.put("isBroadcast", "false");
    keyValue.put("consumerGroup", group);
    keyValue.put("eventMeshIDC", eventMeshTCPConfiguration.eventMeshIDC);
    keyValue.put("instanceName", EventMeshUtil.buildMeshTcpClientID(sysId, "SUB", eventMeshTCPConfiguration.eventMeshCluster));
    persistentMsgConsumer.init(keyValue);
    EventListener listener = (event, context) -> {
        eventMeshTcpMonitor.getTcpSummaryMetrics().getMq2eventMeshMsgNum().incrementAndGet();
        event = CloudEventBuilder.from(event).withExtension(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())).withExtension(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP, eventMeshTCPConfiguration.eventMeshServerIp).build();
        String topic = event.getSubject();
        EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = (EventMeshAsyncConsumeContext) context;
        Session session = downstreamDispatchStrategy.select(group, topic, groupConsumerSessions);
        String bizSeqNo = EventMeshUtil.getMessageBizSeq(event);
        if (session == null) {
            try {
                Integer sendBackTimes = 0;
                String sendBackFromEventMeshIp = "";
                if (StringUtils.isNotBlank(Objects.requireNonNull(event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES)).toString())) {
                    sendBackTimes = (Integer) event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES);
                }
                if (StringUtils.isNotBlank(Objects.requireNonNull(event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_IP)).toString())) {
                    sendBackFromEventMeshIp = (String) event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_IP);
                }
                logger.error("found no session to downstream msg,groupName:{}, topic:{}, " + "bizSeqNo:{}, sendBackTimes:{}, sendBackFromEventMeshIp:{}", group, topic, bizSeqNo, sendBackTimes, sendBackFromEventMeshIp);
                if (sendBackTimes >= eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSendBackMaxTimes) {
                    logger.error("sendBack to broker over max times:{}, groupName:{}, topic:{}, " + "bizSeqNo:{}", eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSendBackMaxTimes, group, topic, bizSeqNo);
                } else {
                    sendBackTimes++;
                    event = CloudEventBuilder.from(event).withExtension(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES, sendBackTimes.toString()).withExtension(EventMeshConstants.EVENTMESH_SEND_BACK_IP, eventMeshTCPConfiguration.eventMeshServerIp).build();
                    sendMsgBackToBroker(event, bizSeqNo);
                }
            } catch (Exception e) {
                logger.warn("handle msg exception when no session found", e);
            }
            eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage);
            return;
        }
        SubscriptionItem subscriptionItem = subscriptions.get(topic);
        DownStreamMsgContext downStreamMsgContext = new DownStreamMsgContext(event, session, persistentMsgConsumer, eventMeshAsyncConsumeContext.getAbstractContext(), false, subscriptionItem);
        // msg put in eventmesh,waiting client ack
        session.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext);
        session.downstreamMsg(downStreamMsgContext);
        eventMeshAsyncConsumeContext.commit(EventMeshAction.ManualAck);
    };
    persistentMsgConsumer.registerEventListener(listener);
    inited4Persistent.compareAndSet(false, true);
    logger.info("init persistentMsgConsumer success, group:{}", group);
}
Also used : OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) EventMeshConstants(org.apache.eventmesh.runtime.constants.EventMeshConstants) DownstreamDispatchStrategy(org.apache.eventmesh.runtime.core.protocol.tcp.client.group.dispatch.DownstreamDispatchStrategy) EventMeshAction(org.apache.eventmesh.api.EventMeshAction) CloudEvent(io.cloudevents.CloudEvent) DownStreamMsgContext(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext) LoggerFactory(org.slf4j.LoggerFactory) SendCallback(org.apache.eventmesh.api.SendCallback) MQProducerWrapper(org.apache.eventmesh.runtime.core.plugin.MQProducerWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EventMeshTcpRetryer(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.retry.EventMeshTcpRetryer) EventMeshUtil(org.apache.eventmesh.runtime.util.EventMeshUtil) EventMeshTCPServer(org.apache.eventmesh.runtime.boot.EventMeshTCPServer) EventMeshTcpMonitor(org.apache.eventmesh.runtime.metrics.tcp.EventMeshTcpMonitor) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) HttpTinyClient(org.apache.eventmesh.runtime.util.HttpTinyClient) EventMeshAsyncConsumeContext(org.apache.eventmesh.api.EventMeshAsyncConsumeContext) SendResult(org.apache.eventmesh.api.SendResult) Properties(java.util.Properties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) MQConsumerWrapper(org.apache.eventmesh.runtime.core.plugin.MQConsumerWrapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SubscriptionItem(org.apache.eventmesh.common.protocol.SubscriptionItem) EventMeshTCPConfiguration(org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration) UpStreamMsgContext(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext) Objects(java.util.Objects) Session(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session) List(java.util.List) CloudEventBuilder(io.cloudevents.core.builder.CloudEventBuilder) RequestReplyCallback(org.apache.eventmesh.api.RequestReplyCallback) Preconditions(com.google.common.base.Preconditions) EventListener(org.apache.eventmesh.api.EventListener) JsonUtils(org.apache.eventmesh.common.utils.JsonUtils) SubscriptionMode(org.apache.eventmesh.common.protocol.SubscriptionMode) SubscriptionItem(org.apache.eventmesh.common.protocol.SubscriptionItem) DownStreamMsgContext(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext) EventMeshAsyncConsumeContext(org.apache.eventmesh.api.EventMeshAsyncConsumeContext) EventListener(org.apache.eventmesh.api.EventListener) Properties(java.util.Properties) Session(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session)

Example 2 with Session

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session in project incubator-eventmesh by apache.

the class ClientSessionGroupMapping method createSession.

public Session createSession(UserAgent user, ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress addr = (InetSocketAddress) ctx.channel().remoteAddress();
    user.setHost(addr.getHostString());
    user.setPort(addr.getPort());
    Session session = null;
    if (!sessionTable.containsKey(addr)) {
        logger.info("createSession client[{}]", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
        session = new Session(user, ctx, eventMeshTCPServer.getEventMeshTCPConfiguration());
        initClientGroupWrapper(user, session);
        sessionTable.put(addr, session);
        sessionLogger.info("session|open|succeed|user={}", user);
    } else {
        session = sessionTable.get(addr);
        sessionLogger.error("session|open|failed|user={}|msg={}", user, "session has been created!");
    }
    return session;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Session(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session)

Example 3 with Session

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session in project incubator-eventmesh by apache.

the class ClientSessionGroupMapping method handleUnackMsgsInSession.

/**
 * handle unAck msgs in this session
 *
 * @param session
 */
private void handleUnackMsgsInSession(Session session) {
    ConcurrentHashMap<String, DownStreamMsgContext> /**
     * seq
     */
    unAckMsg = session.getPusher().getUnAckMsg();
    if (unAckMsg.size() > 0 && session.getClientGroupWrapper().get().getGroupConsumerSessions().size() > 0) {
        for (Map.Entry<String, DownStreamMsgContext> entry : unAckMsg.entrySet()) {
            DownStreamMsgContext downStreamMsgContext = entry.getValue();
            if (SubscriptionMode.BROADCASTING.equals(downStreamMsgContext.subscriptionItem.getMode())) {
                logger.warn("exist broadcast msg unack when closeSession,seq:{},bizSeq:{},client:{}", downStreamMsgContext.seq, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.event), session.getClient());
                continue;
            }
            Session reChooseSession = session.getClientGroupWrapper().get().getDownstreamDispatchStrategy().select(session.getClientGroupWrapper().get().getGroup(), downStreamMsgContext.event.getSubject(), Objects.requireNonNull(session.getClientGroupWrapper().get()).groupConsumerSessions);
            if (reChooseSession != null) {
                downStreamMsgContext.session = reChooseSession;
                reChooseSession.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext);
                reChooseSession.downstreamMsg(downStreamMsgContext);
                logger.info("rePush msg form unAckMsgs,seq:{},rePushClient:{}", entry.getKey(), downStreamMsgContext.session.getClient());
            } else {
                logger.warn("select session fail in handleUnackMsgsInSession,seq:{},topic:{}", entry.getKey(), downStreamMsgContext.event.getSubject());
            }
        }
    }
}
Also used : DownStreamMsgContext(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Session(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session)

Example 4 with Session

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session in project incubator-eventmesh by apache.

the class ClientSessionGroupMapping method closeSession.

public synchronized void closeSession(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress addr = (InetSocketAddress) ctx.channel().remoteAddress();
    Session session = MapUtils.getObject(sessionTable, addr, null);
    if (session == null) {
        final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
        logger.info("begin to close channel to remote address[{}]", remoteAddress);
        ctx.channel().close().addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                logger.info("close the connection to remote address[{}] result: {}", remoteAddress, future.isSuccess());
            }
        });
        sessionLogger.info("session|close|succeed|address={}|msg={}", addr, "no session was found");
        return;
    }
    closeSession(session);
    // remove session from sessionTable
    sessionTable.remove(addr);
    sessionLogger.info("session|close|succeed|user={}", session.getClient());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Session(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session)

Example 5 with Session

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session in project incubator-eventmesh by apache.

the class HelloTask method run.

@Override
public void run() {
    long taskExecuteTime = System.currentTimeMillis();
    Package res = new Package();
    Session session = null;
    UserAgent user = (UserAgent) pkg.getBody();
    try {
        // do acl check in connect
        if (eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerSecurityEnable) {
            String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            Acl.doAclCheckInTcpConnect(remoteAddr, user, HELLO_REQUEST.value());
        }
        if (eventMeshTCPServer.getEventMeshServer().getServiceState() != ServiceState.RUNNING) {
            logger.error("server state is not running:{}", eventMeshTCPServer.getEventMeshServer().getServiceState());
            throw new Exception("server state is not running, maybe deploying...");
        }
        validateUserAgent(user);
        session = eventMeshTCPServer.getClientSessionGroupMapping().createSession(user, ctx);
        res.setHeader(new Header(HELLO_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq()));
        Utils.writeAndFlush(res, startTime, taskExecuteTime, session.getContext(), session);
    } catch (Throwable e) {
        messageLogger.error("HelloTask failed|address={},errMsg={}", ctx.channel().remoteAddress(), e);
        res.setHeader(new Header(HELLO_RESPONSE, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), pkg.getHeader().getSeq()));
        ctx.writeAndFlush(res).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    Utils.logFailedMessageFlow(future, res, user, startTime, taskExecuteTime);
                } else {
                    Utils.logSucceedMessageFlow(res, user, startTime, taskExecuteTime);
                }
                logger.warn("HelloTask failed,close session,addr:{}", ctx.channel().remoteAddress());
                eventMeshTCPServer.getClientSessionGroupMapping().closeSession(ctx);
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Header(org.apache.eventmesh.common.protocol.tcp.Header) UserAgent(org.apache.eventmesh.common.protocol.tcp.UserAgent) Package(org.apache.eventmesh.common.protocol.tcp.Package) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Session(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session)

Aggregations

Session (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session)20 InetSocketAddress (java.net.InetSocketAddress)11 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)9 ClientSessionGroupMapping (org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping)9 ArrayList (java.util.ArrayList)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Map (java.util.Map)4 UserAgent (org.apache.eventmesh.common.protocol.tcp.UserAgent)4 HashSet (java.util.HashSet)3 Preconditions (com.google.common.base.Preconditions)2 CloudEvent (io.cloudevents.CloudEvent)2 CloudEventBuilder (io.cloudevents.core.builder.CloudEventBuilder)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Objects (java.util.Objects)2 Properties (java.util.Properties)2