Search in sources :

Example 1 with Package

use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.

the class EventMeshTcp2Client method serverGoodby2Client.

public static InetSocketAddress serverGoodby2Client(EventMeshTCPServer eventMeshTCPServer, Session session, ClientSessionGroupMapping mapping) {
    logger.info("serverGoodby2Client client[{}]", session.getClient());
    try {
        long startTime = System.currentTimeMillis();
        Package msg = new Package();
        msg.setHeader(new Header(SERVER_GOODBYE_REQUEST, OPStatus.SUCCESS.getCode(), "graceful normal quit from eventmesh", null));
        eventMeshTCPServer.getScheduler().submit(new Runnable() {

            @Override
            public void run() {
                long taskExecuteTime = System.currentTimeMillis();
                Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
            }
        });
        InetSocketAddress address = (InetSocketAddress) session.getContext().channel().remoteAddress();
        closeSessionIfTimeout(eventMeshTCPServer, session, mapping);
        return address;
    } catch (Exception e) {
        logger.error("exception occur while serverGoodby2Client", e);
        return null;
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) InetSocketAddress(java.net.InetSocketAddress) Package(org.apache.eventmesh.common.protocol.tcp.Package)

Example 2 with Package

use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.

the class EventMeshTcpMessageDispatcher method writeToClient.

private void writeToClient(Command cmd, Package pkg, ChannelHandlerContext ctx, Exception e) {
    try {
        Package res = new Package();
        res.setHeader(new Header(getReplyCommand(cmd), OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq()));
        ctx.writeAndFlush(res);
    } catch (Exception ex) {
        logger.warn("writeToClient failed", ex);
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) Package(org.apache.eventmesh.common.protocol.tcp.Package)

Example 3 with Package

use of org.apache.eventmesh.common.protocol.tcp.Package 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)

Example 4 with Package

use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.

the class MessageTransferTask method run.

@Override
public void run() {
    long taskExecuteTime = System.currentTimeMillis();
    Command cmd = pkg.getHeader().getCmd();
    Command replyCmd = getReplyCmd(cmd);
    String protocolType = "EventMeshMessage";
    if (pkg.getHeader().getProperties() != null && pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE) != null) {
        protocolType = (String) pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE);
    }
    ProtocolAdaptor<ProtocolTransportObject> protocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
    Package msg = new Package();
    // EventMeshMessage eventMeshMessage = (EventMeshMessage) pkg.getBody();
    int retCode = 0;
    EventMeshTcpSendResult sendStatus;
    CloudEvent event = null;
    try {
        event = protocolAdaptor.toCloudEvent(pkg);
        if (event == null) {
            throw new Exception("event is null");
        }
        String content = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
        if (content.length() > eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshEventSize) {
            throw new Exception("event size exceeds the limit: " + eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshEventSize);
        }
        // do acl check in sending msg
        if (eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerSecurityEnable) {
            String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            Acl.doAclCheckInTcpSend(remoteAddr, session.getClient(), event.getSubject(), cmd.value());
        }
        if (eventMeshTCPServer.getRateLimiter().tryAcquire(TRY_PERMIT_TIME_OUT, TimeUnit.MILLISECONDS)) {
            synchronized (session) {
                long sendTime = System.currentTimeMillis();
                event = addTimestamp(event, cmd, sendTime);
                sendStatus = session.upstreamMsg(pkg.getHeader(), event, createSendCallback(replyCmd, taskExecuteTime, event), startTime, taskExecuteTime);
                if (StringUtils.equals(EventMeshTcpSendStatus.SUCCESS.name(), sendStatus.getSendStatus().name())) {
                    messageLogger.info("pkg|eventMesh2mq|cmd={}|Msg={}|user={}|wait={}ms|cost={}ms", cmd, event, session.getClient(), taskExecuteTime - startTime, sendTime - startTime);
                } else {
                    throw new Exception(sendStatus.getDetail());
                }
            }
        } else {
            msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), "Tps overload, global flow control", pkg.getHeader().getSeq()));
            ctx.writeAndFlush(msg).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    Utils.logSucceedMessageFlow(msg, session.getClient(), startTime, taskExecuteTime);
                }
            });
            logger.warn("======Tps overload, global flow control, rate:{}! PLEASE CHECK!========", eventMeshTCPServer.getRateLimiter().getRate());
            return;
        }
    } catch (Exception e) {
        logger.error("MessageTransferTask failed|cmd={}|event={}|user={}|errMsg={}", cmd, event, session.getClient(), e);
        if (!cmd.equals(RESPONSE_TO_SERVER)) {
            msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), pkg.getHeader().getSeq()));
            Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) EventMeshTcpSendResult(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Header(org.apache.eventmesh.common.protocol.tcp.Header) Command(org.apache.eventmesh.common.protocol.tcp.Command) Package(org.apache.eventmesh.common.protocol.tcp.Package) CloudEvent(io.cloudevents.CloudEvent)

Example 5 with Package

use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.

the class RecommendTask method run.

@Override
public void run() {
    long taskExecuteTime = System.currentTimeMillis();
    Package res = new Package();
    try {
        if (!eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerRegistryEnable) {
            throw new Exception("registry enable config is false, not support");
        }
        UserAgent user = (UserAgent) pkg.getBody();
        validateUserAgent(user);
        String group = getGroupOfClient(user);
        EventMeshRecommendStrategy eventMeshRecommendStrategy = new EventMeshRecommendImpl(eventMeshTCPServer);
        String eventMeshRecommendResult = eventMeshRecommendStrategy.calculateRecommendEventMesh(group, user.getPurpose());
        res.setHeader(new Header(RECOMMEND_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq()));
        res.setBody(eventMeshRecommendResult);
    } catch (Exception e) {
        messageLogger.error("RecommendTask failed|address={}|errMsg={}", ctx.channel().remoteAddress(), e);
        res.setHeader(new Header(RECOMMEND_RESPONSE, OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq()));
    } finally {
        writeAndFlush(res, startTime, taskExecuteTime, session.getContext(), session);
    // session.write2Client(res);
    }
}
Also used : EventMeshRecommendStrategy(org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendStrategy) Header(org.apache.eventmesh.common.protocol.tcp.Header) UserAgent(org.apache.eventmesh.common.protocol.tcp.UserAgent) Package(org.apache.eventmesh.common.protocol.tcp.Package) EventMeshRecommendImpl(org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendImpl)

Aggregations

Package (org.apache.eventmesh.common.protocol.tcp.Package)97 Header (org.apache.eventmesh.common.protocol.tcp.Header)58 EventMeshException (org.apache.eventmesh.common.exception.EventMeshException)16 SubscriptionItem (org.apache.eventmesh.common.protocol.SubscriptionItem)8 EventMeshMessage (org.apache.eventmesh.common.protocol.tcp.EventMeshMessage)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 ReceiveMsgHook (org.apache.eventmesh.runtime.client.hook.ReceiveMsgHook)6 CloudEvent (io.cloudevents.CloudEvent)4 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)4 Command (org.apache.eventmesh.common.protocol.tcp.Command)4 UserAgent (org.apache.eventmesh.common.protocol.tcp.UserAgent)4 SubClientImpl (org.apache.eventmesh.runtime.client.impl.SubClientImpl)4 ArrayList (java.util.ArrayList)3 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)3 Body (org.apache.eventmesh.common.protocol.http.body.Body)3 ProtocolHandleException (org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2