Search in sources :

Example 1 with EventMeshTcpSendResult

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult 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)

Aggregations

CloudEvent (io.cloudevents.CloudEvent)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)1 Command (org.apache.eventmesh.common.protocol.tcp.Command)1 Header (org.apache.eventmesh.common.protocol.tcp.Header)1 Package (org.apache.eventmesh.common.protocol.tcp.Package)1 EventMeshTcpSendResult (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult)1