Search in sources :

Example 1 with Command

use of org.apache.eventmesh.common.protocol.tcp.Command 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 2 with Command

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

the class SessionSender method send.

public EventMeshTcpSendResult send(Header header, CloudEvent event, SendCallback sendCallback, long startTime, long taskExecuteTime) {
    try {
        if (upstreamBuff.tryAcquire(TRY_PERMIT_TIME_OUT, TimeUnit.MILLISECONDS)) {
            upMsgs.incrementAndGet();
            UpStreamMsgContext upStreamMsgContext = null;
            Command cmd = header.getCmd();
            long ttl = EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS;
            if (Command.REQUEST_TO_SERVER == cmd) {
                if (event.getExtension(EventMeshConstants.PROPERTY_MESSAGE_TTL) != null) {
                    ttl = Long.parseLong((String) Objects.requireNonNull(event.getExtension(EventMeshConstants.PROPERTY_MESSAGE_TTL)));
                }
                // long ttl = msg.getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_TTL) != null ?
                // Long.parseLong(msg.getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_TTL))
                // : EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS;
                upStreamMsgContext = new UpStreamMsgContext(session, event, header, startTime, taskExecuteTime);
                session.getClientGroupWrapper().get().request(upStreamMsgContext, initSyncRRCallback(header, startTime, taskExecuteTime), ttl);
                upstreamBuff.release();
            } else if (Command.RESPONSE_TO_SERVER == cmd) {
                String cluster = (String) event.getExtension(EventMeshConstants.PROPERTY_MESSAGE_CLUSTER);
                // String cluster = msg.getUserProperties(EventMeshConstants.PROPERTY_MESSAGE_CLUSTER);
                if (!StringUtils.isEmpty(cluster)) {
                    String replyTopic = EventMeshConstants.RR_REPLY_TOPIC;
                    replyTopic = cluster + "-" + replyTopic;
                    event = CloudEventBuilder.from(event).withSubject(replyTopic).build();
                // msg.getSystemProperties().put(Constants.PROPERTY_MESSAGE_DESTINATION, replyTopic);
                // event(replyTopic);
                }
                // //for rocketmq support
                // MessageAccessor.putProperty(msg, MessageConst.PROPERTY_MESSAGE_TYPE, MixAll.REPLY_MESSAGE_FLAG);
                // MessageAccessor.putProperty(msg, MessageConst.PROPERTY_CORRELATION_ID,
                // msg.getProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID));
                // MessageAccessor.putProperty(msg, MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT,
                // msg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO));
                upStreamMsgContext = new UpStreamMsgContext(session, event, header, startTime, taskExecuteTime);
                session.getClientGroupWrapper().get().reply(upStreamMsgContext);
                upstreamBuff.release();
            } else {
                upStreamMsgContext = new UpStreamMsgContext(session, event, header, startTime, taskExecuteTime);
                session.getClientGroupWrapper().get().send(upStreamMsgContext, sendCallback);
            }
            session.getClientGroupWrapper().get().getEventMeshTcpMonitor().getTcpSummaryMetrics().getEventMesh2mqMsgNum().incrementAndGet();
        } else {
            logger.warn("send too fast,session flow control,session:{}", session.getClient());
            return new EventMeshTcpSendResult(header.getSeq(), EventMeshTcpSendStatus.SEND_TOO_FAST, EventMeshTcpSendStatus.SEND_TOO_FAST.name());
        }
    } catch (Exception e) {
        logger.warn("SessionSender send failed", e);
        if (!(e instanceof InterruptedException)) {
            upstreamBuff.release();
        }
        failMsgCount.incrementAndGet();
        return new EventMeshTcpSendResult(header.getSeq(), EventMeshTcpSendStatus.OTHER_EXCEPTION, e.getCause().toString());
    }
    return new EventMeshTcpSendResult(header.getSeq(), EventMeshTcpSendStatus.SUCCESS, EventMeshTcpSendStatus.SUCCESS.name());
}
Also used : Command(org.apache.eventmesh.common.protocol.tcp.Command)

Example 3 with Command

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

the class SessionPusher method push.

public void push(final DownStreamMsgContext downStreamMsgContext) {
    Command cmd;
    if (SubscriptionMode.BROADCASTING.equals(downStreamMsgContext.subscriptionItem.getMode())) {
        cmd = Command.BROADCAST_MESSAGE_TO_CLIENT;
    } else if (SubscriptionType.SYNC.equals(downStreamMsgContext.subscriptionItem.getType())) {
        cmd = Command.REQUEST_TO_CLIENT;
    } else {
        cmd = Command.ASYNC_MESSAGE_TO_CLIENT;
    }
    String protocolType = Objects.requireNonNull(downStreamMsgContext.event.getExtension(Constants.PROTOCOL_TYPE)).toString();
    ProtocolAdaptor protocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
    Package pkg = new Package();
    downStreamMsgContext.event = CloudEventBuilder.from(downStreamMsgContext.event).withExtension(EventMeshConstants.REQ_EVENTMESH2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())).build();
    EventMeshMessage body = null;
    int retCode = 0;
    String retMsg = null;
    try {
        pkg = (Package) protocolAdaptor.fromCloudEvent(downStreamMsgContext.event);
        pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, downStreamMsgContext.seq));
        pkg.getHeader().putProperty(Constants.PROTOCOL_TYPE, protocolType);
        messageLogger.info("pkg|mq2eventMesh|cmd={}|mqMsg={}|user={}", cmd, pkg, session.getClient());
    } catch (Exception e) {
        pkg.setHeader(new Header(cmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), downStreamMsgContext.seq));
        retCode = -1;
        retMsg = e.toString();
    } finally {
        session.getClientGroupWrapper().get().getEventMeshTcpMonitor().getTcpSummaryMetrics().getEventMesh2clientMsgNum().incrementAndGet();
        session.getContext().writeAndFlush(pkg).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    logger.error("downstreamMsg fail,seq:{}, retryTimes:{}, event:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, downStreamMsgContext.event);
                    deliverFailMsgsCount.incrementAndGet();
                    // how long to isolate client when push fail
                    long isolateTime = System.currentTimeMillis() + session.getEventMeshTCPConfiguration().eventMeshTcpPushFailIsolateTimeInMills;
                    session.setIsolateTime(isolateTime);
                    logger.warn("isolate client:{},isolateTime:{}", session.getClient(), isolateTime);
                    // retry
                    long delayTime = SubscriptionType.SYNC.equals(downStreamMsgContext.subscriptionItem.getType()) ? session.getEventMeshTCPConfiguration().eventMeshTcpMsgRetrySyncDelayInMills : session.getEventMeshTCPConfiguration().eventMeshTcpMsgRetryAsyncDelayInMills;
                    downStreamMsgContext.delay(delayTime);
                    session.getClientGroupWrapper().get().getEventMeshTcpRetryer().pushRetry(downStreamMsgContext);
                } else {
                    deliveredMsgsCount.incrementAndGet();
                    logger.info("downstreamMsg success,seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.event));
                    if (session.isIsolated()) {
                        logger.info("cancel isolated,client:{}", session.getClient());
                        session.setIsolateTime(System.currentTimeMillis());
                    }
                }
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Header(org.apache.eventmesh.common.protocol.tcp.Header) Command(org.apache.eventmesh.common.protocol.tcp.Command) ProtocolAdaptor(org.apache.eventmesh.protocol.api.ProtocolAdaptor) Package(org.apache.eventmesh.common.protocol.tcp.Package) ChannelFutureListener(io.netty.channel.ChannelFutureListener) EventMeshMessage(org.apache.eventmesh.common.protocol.tcp.EventMeshMessage)

Example 4 with Command

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

the class AbstractEventMeshTCPSubHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Package msg) throws Exception {
    Preconditions.checkNotNull(msg, "TCP package cannot be null");
    Preconditions.checkNotNull(msg.getHeader(), "TCP package header cannot be null");
    Command cmd = msg.getHeader().getCmd();
    log.info("|receive|type={}|msg={}", cmd, msg);
    switch(cmd) {
        case REQUEST_TO_CLIENT:
            callback(getProtocolMessage(msg), ctx);
            response(MessageUtils.requestToClientAck(msg));
            break;
        case ASYNC_MESSAGE_TO_CLIENT:
            callback(getProtocolMessage(msg), ctx);
            response(MessageUtils.asyncMessageAck(msg));
            break;
        case BROADCAST_MESSAGE_TO_CLIENT:
            callback(getProtocolMessage(msg), ctx);
            response(MessageUtils.broadcastMessageAck(msg));
            break;
        case SERVER_GOODBYE_REQUEST:
            // TODO
            break;
        default:
            log.error("msg ignored|{}|{}", cmd, msg);
    }
    RequestContext context = contexts.get(RequestContext.key(msg));
    if (context != null) {
        contexts.remove(context.getKey());
        context.finish(msg);
    } else {
        log.error("msg ignored,context not found.|{}|{}", cmd, msg);
    }
}
Also used : Command(org.apache.eventmesh.common.protocol.tcp.Command) RequestContext(org.apache.eventmesh.client.tcp.common.RequestContext)

Example 5 with Command

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

the class PubClientImpl method dispatcher.

/**
 * Add test case assertions on the basis of the original IO
 */
public Package dispatcher(Package request, long timeout) throws Exception {
    Assert.assertNotNull(request);
    Package response = super.io(request, timeout);
    Assert.assertNotNull(response);
    Command cmd = response.getHeader().getCommand();
    switch(request.getHeader().getCommand()) {
        case RECOMMEND_REQUEST:
            Assert.assertEquals(cmd, Command.RECOMMEND_RESPONSE);
            break;
        case HELLO_REQUEST:
            Assert.assertEquals(cmd, Command.HELLO_RESPONSE);
            break;
        case HEARTBEAT_REQUEST:
            Assert.assertEquals(cmd, Command.HEARTBEAT_RESPONSE);
            break;
        case CLIENT_GOODBYE_REQUEST:
            Assert.assertEquals(cmd, Command.CLIENT_GOODBYE_RESPONSE);
            break;
        case BROADCAST_MESSAGE_TO_SERVER:
            Assert.assertEquals(cmd, Command.BROADCAST_MESSAGE_TO_SERVER_ACK);
            break;
        case ASYNC_MESSAGE_TO_SERVER:
            Assert.assertEquals(cmd, Command.ASYNC_MESSAGE_TO_SERVER_ACK);
            break;
        case REQUEST_TO_SERVER:
            Assert.assertEquals(cmd, Command.RESPONSE_TO_CLIENT);
            break;
        default:
            break;
    }
    assert response.getHeader().getCode() == OPStatus.SUCCESS.getCode();
    return response;
}
Also used : Command(org.apache.eventmesh.common.protocol.tcp.Command) Package(org.apache.eventmesh.common.protocol.tcp.Package)

Aggregations

Command (org.apache.eventmesh.common.protocol.tcp.Command)10 Package (org.apache.eventmesh.common.protocol.tcp.Package)4 Header (org.apache.eventmesh.common.protocol.tcp.Header)3 CloudEvent (io.cloudevents.CloudEvent)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 RequestContext (org.apache.eventmesh.client.tcp.common.RequestContext)2 ProtocolAdaptor (org.apache.eventmesh.protocol.api.ProtocolAdaptor)2 RequestReplyCallback (org.apache.eventmesh.api.RequestReplyCallback)1 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)1 EventMeshMessage (org.apache.eventmesh.common.protocol.tcp.EventMeshMessage)1 DownStreamMsgContext (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext)1 EventMeshTcpSendResult (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult)1 HelloTask (org.apache.eventmesh.runtime.core.protocol.tcp.client.task.HelloTask)1 RecommendTask (org.apache.eventmesh.runtime.core.protocol.tcp.client.task.RecommendTask)1