Search in sources :

Example 1 with ProtocolTransportObject

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

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

the class MeshMessageProtocolAdaptor method fromCloudEvent.

@Override
public ProtocolTransportObject fromCloudEvent(CloudEvent cloudEvent) throws ProtocolHandleException {
    String protocolDesc = cloudEvent.getExtension(Constants.PROTOCOL_DESC).toString();
    if (StringUtils.equals("http", protocolDesc)) {
        HttpCommand httpCommand = new HttpCommand();
        Body body = new Body() {

            final Map<String, Object> map = new HashMap<>();

            @Override
            public Map<String, Object> toMap() {
                map.put("content", new String(cloudEvent.getData().toBytes(), StandardCharsets.UTF_8));
                return map;
            }
        };
        body.toMap();
        httpCommand.setBody(body);
        return httpCommand;
    } else if (StringUtils.equals("grpc", protocolDesc)) {
        return GrpcMessageProtocolResolver.buildSimpleMessage(cloudEvent);
    } else if (StringUtils.equals("tcp", protocolDesc)) {
        return TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent);
    } else {
        throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc));
    }
}
Also used : ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) Body(org.apache.eventmesh.common.protocol.http.body.Body) HashMap(java.util.HashMap) Map(java.util.Map) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 3 with ProtocolTransportObject

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

the class CloudEventsProtocolAdaptor method fromCloudEvent.

@Override
public ProtocolTransportObject fromCloudEvent(CloudEvent cloudEvent) throws ProtocolHandleException {
    String protocolDesc = cloudEvent.getExtension(Constants.PROTOCOL_DESC).toString();
    if (StringUtils.equals("http", protocolDesc)) {
        HttpCommand httpCommand = new HttpCommand();
        Body body = new Body() {

            final Map<String, Object> map = new HashMap<>();

            @Override
            public Map<String, Object> toMap() {
                byte[] eventByte = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).serialize(cloudEvent);
                map.put("content", new String(eventByte, StandardCharsets.UTF_8));
                return map;
            }
        };
        body.toMap();
        httpCommand.setBody(body);
        return httpCommand;
    } else if (StringUtils.equals("tcp", protocolDesc)) {
        Package pkg = new Package();
        String dataContentType = cloudEvent.getDataContentType();
        Preconditions.checkNotNull(dataContentType, "DateContentType cannot be null");
        EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(dataContentType);
        Preconditions.checkNotNull(eventFormat, String.format("DateContentType:%s is not supported", dataContentType));
        pkg.setBody(eventFormat.serialize(cloudEvent));
        return pkg;
    } else if (StringUtils.equals("grpc", protocolDesc)) {
        return GrpcMessageProtocolResolver.buildSimpleMessage(cloudEvent);
    } else {
        throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc));
    }
}
Also used : EventFormat(io.cloudevents.core.format.EventFormat) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HashMap(java.util.HashMap) Map(java.util.Map) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 4 with ProtocolTransportObject

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

the class AsyncHTTPPushRequest method tryHTTPRequest.

@Override
public void tryHTTPRequest() {
    currPushUrl = getUrl();
    if (StringUtils.isBlank(currPushUrl)) {
        return;
    }
    HttpPost builder = new HttpPost(currPushUrl);
    String requestCode = "";
    if (SubscriptionType.SYNC.equals(handleMsgContext.getSubscriptionItem().getType())) {
        requestCode = String.valueOf(RequestCode.HTTP_PUSH_CLIENT_SYNC.getRequestCode());
    } else {
        requestCode = String.valueOf(RequestCode.HTTP_PUSH_CLIENT_ASYNC.getRequestCode());
    }
    builder.addHeader(ProtocolKey.REQUEST_CODE, requestCode);
    builder.addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA);
    builder.addHeader(ProtocolKey.VERSION, ProtocolVersion.V1.getVersion());
    builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, handleMsgContext.getEventMeshHTTPServer().getEventMeshHttpConfiguration().eventMeshCluster);
    builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, IPUtils.getLocalAddress());
    builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, handleMsgContext.getEventMeshHTTPServer().getEventMeshHttpConfiguration().eventMeshEnv);
    builder.addHeader(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, handleMsgContext.getEventMeshHTTPServer().getEventMeshHttpConfiguration().eventMeshIDC);
    CloudEvent event = CloudEventBuilder.from(handleMsgContext.getEvent()).withExtension(EventMeshConstants.REQ_EVENTMESH2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())).build();
    handleMsgContext.setEvent(event);
    String content = "";
    try {
        String protocolType = Objects.requireNonNull(event.getExtension(Constants.PROTOCOL_TYPE)).toString();
        ProtocolAdaptor<ProtocolTransportObject> protocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
        ProtocolTransportObject protocolTransportObject = protocolAdaptor.fromCloudEvent(handleMsgContext.getEvent());
        content = ((HttpCommand) protocolTransportObject).getBody().toMap().get("content").toString();
    } catch (Exception ex) {
        return;
    }
    List<NameValuePair> body = new ArrayList<>();
    body.add(new BasicNameValuePair(PushMessageRequestBody.CONTENT, content));
    if (StringUtils.isBlank(handleMsgContext.getBizSeqNo())) {
        body.add(new BasicNameValuePair(PushMessageRequestBody.BIZSEQNO, RandomStringUtils.generateNum(20)));
    } else {
        body.add(new BasicNameValuePair(PushMessageRequestBody.BIZSEQNO, handleMsgContext.getBizSeqNo()));
    }
    if (StringUtils.isBlank(handleMsgContext.getUniqueId())) {
        body.add(new BasicNameValuePair(PushMessageRequestBody.UNIQUEID, RandomStringUtils.generateNum(20)));
    } else {
        body.add(new BasicNameValuePair(PushMessageRequestBody.UNIQUEID, handleMsgContext.getUniqueId()));
    }
    body.add(new BasicNameValuePair(PushMessageRequestBody.RANDOMNO, handleMsgContext.getMsgRandomNo()));
    body.add(new BasicNameValuePair(PushMessageRequestBody.TOPIC, handleMsgContext.getTopic()));
    body.add(new BasicNameValuePair(PushMessageRequestBody.EXTFIELDS, JsonUtils.serialize(EventMeshUtil.getEventProp(handleMsgContext.getEvent()))));
    HttpEntity httpEntity = new UrlEncodedFormEntity(body, StandardCharsets.UTF_8);
    builder.setEntity(httpEntity);
    // for CloudEvents Webhook spec
    String urlAuthType = handleMsgContext.getConsumerGroupConfig().getConsumerGroupTopicConf().get(handleMsgContext.getTopic()).getHttpAuthTypeMap().get(currPushUrl);
    WebhookUtil.setWebhookHeaders(builder, httpEntity.getContentType().getValue(), eventMeshHttpConfiguration.eventMeshWebhookOrigin, urlAuthType);
    eventMeshHTTPServer.metrics.getSummaryMetrics().recordPushMsg();
    this.lastPushTime = System.currentTimeMillis();
    addToWaitingMap(this);
    cmdLogger.info("cmd={}|eventMesh2client|from={}|to={}", requestCode, IPUtils.getLocalAddress(), currPushUrl);
    try {
        eventMeshHTTPServer.httpClientPool.getClient().execute(builder, new ResponseHandler<Object>() {

            @Override
            public Object handleResponse(HttpResponse response) {
                removeWaitingMap(AsyncHTTPPushRequest.this);
                long cost = System.currentTimeMillis() - lastPushTime;
                eventMeshHTTPServer.metrics.getSummaryMetrics().recordHTTPPushTimeCost(cost);
                if (processResponseStatus(response.getStatusLine().getStatusCode(), response)) {
                    // this is successful response, process response payload
                    String res = "";
                    try {
                        res = EntityUtils.toString(response.getEntity(), Charset.forName(EventMeshConstants.DEFAULT_CHARSET));
                    } catch (IOException e) {
                        handleMsgContext.finish();
                        return new Object();
                    }
                    ClientRetCode result = processResponseContent(res);
                    messageLogger.info("message|eventMesh2client|{}|url={}|topic={}|bizSeqNo={}" + "|uniqueId={}|cost={}", result, currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getBizSeqNo(), handleMsgContext.getUniqueId(), cost);
                    if (result == ClientRetCode.OK) {
                        complete();
                        if (isComplete()) {
                            handleMsgContext.finish();
                        }
                    } else if (result == ClientRetCode.RETRY) {
                        delayRetry();
                        if (isComplete()) {
                            handleMsgContext.finish();
                        }
                    } else if (result == ClientRetCode.NOLISTEN) {
                        delayRetry();
                        if (isComplete()) {
                            handleMsgContext.finish();
                        }
                    } else if (result == ClientRetCode.FAIL) {
                        complete();
                        if (isComplete()) {
                            handleMsgContext.finish();
                        }
                    }
                } else {
                    eventMeshHTTPServer.metrics.getSummaryMetrics().recordHttpPushMsgFailed();
                    messageLogger.info("message|eventMesh2client|exception|url={}|topic={}|bizSeqNo={}" + "|uniqueId={}|cost={}", currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getBizSeqNo(), handleMsgContext.getUniqueId(), cost);
                    if (isComplete()) {
                        handleMsgContext.finish();
                    }
                }
                return new Object();
            }
        });
        if (messageLogger.isDebugEnabled()) {
            messageLogger.debug("message|eventMesh2client|url={}|topic={}|event={}", currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getEvent());
        } else {
            messageLogger.info("message|eventMesh2client|url={}|topic={}|bizSeqNo={}|uniqueId={}", currPushUrl, handleMsgContext.getTopic(), handleMsgContext.getBizSeqNo(), handleMsgContext.getUniqueId());
        }
    } catch (IOException e) {
        messageLogger.error("push2client err", e);
        removeWaitingMap(this);
        delayRetry();
        if (isComplete()) {
            handleMsgContext.finish();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientRetCode(org.apache.eventmesh.common.protocol.http.common.ClientRetCode) JsonException(org.apache.eventmesh.common.exception.JsonException) IOException(java.io.IOException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) CloudEvent(io.cloudevents.CloudEvent) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 5 with ProtocolTransportObject

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

the class ReplyMessageProcessor method process.

public void process(SimpleMessage message, EventEmitter<SimpleMessage> emitter) throws Exception {
    RequestHeader requestHeader = message.getHeader();
    if (!ServiceUtils.validateHeader(requestHeader)) {
        ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
        return;
    }
    if (!ServiceUtils.validateMessage(message)) {
        ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    try {
        doAclCheck(message);
    } catch (Exception e) {
        aclLogger.warn("CLIENT HAS NO PERMISSION,RequestReplyMessageProcessor reply failed", e);
        ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_ACL_ERR, e.getMessage(), emitter);
        return;
    }
    // control flow rate limit
    if (!eventMeshGrpcServer.getMsgRateLimiter().tryAcquire(EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) {
        logger.error("Send message speed over limit.");
        ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_SEND_MESSAGE_SPEED_OVER_LIMIT_ERR, emitter);
        return;
    }
    String seqNum = message.getSeqNum();
    String uniqueId = message.getUniqueId();
    String producerGroup = message.getProducerGroup();
    // set reply topic for ths message
    String mqCluster = message.getPropertiesOrDefault(EventMeshConstants.PROPERTY_MESSAGE_CLUSTER, "defaultCluster");
    String replyTopic = mqCluster + "-" + EventMeshConstants.RR_REPLY_TOPIC;
    message = SimpleMessage.newBuilder(message).setTopic(replyTopic).build();
    String protocolType = requestHeader.getProtocolType();
    ProtocolAdaptor<ProtocolTransportObject> grpcCommandProtocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
    CloudEvent cloudEvent = grpcCommandProtocolAdaptor.toCloudEvent(new SimpleMessageWrapper(message));
    ProducerManager producerManager = eventMeshGrpcServer.getProducerManager();
    EventMeshProducer eventMeshProducer = producerManager.getEventMeshProducer(producerGroup);
    SendMessageContext sendMessageContext = new SendMessageContext(message.getSeqNum(), cloudEvent, eventMeshProducer, eventMeshGrpcServer);
    long startTime = System.currentTimeMillis();
    eventMeshProducer.reply(sendMessageContext, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            long endTime = System.currentTimeMillis();
            logger.info("message|mq2eventmesh|REPLY|ReplyToServer|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, replyTopic, seqNum, uniqueId);
        }

        @Override
        public void onException(OnExceptionContext onExceptionContext) {
            ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_REPLY_MSG_ERR, EventMeshUtil.stackTrace(onExceptionContext.getException(), 2), emitter);
            long endTime = System.currentTimeMillis();
            logger.error("message|mq2eventmesh|REPLY|ReplyToServer|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, replyTopic, seqNum, uniqueId, onExceptionContext.getException());
        }
    });
}
Also used : EventMeshProducer(org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) ProducerManager(org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) AclException(org.apache.eventmesh.api.exception.AclException) SendMessageContext(org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext) SendResult(org.apache.eventmesh.api.SendResult) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) CloudEvent(io.cloudevents.CloudEvent) SendCallback(org.apache.eventmesh.api.SendCallback)

Aggregations

ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)15 CloudEvent (io.cloudevents.CloudEvent)11 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)8 SendCallback (org.apache.eventmesh.api.SendCallback)7 SendResult (org.apache.eventmesh.api.SendResult)7 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)7 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)5 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer)5 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext)5 AclException (org.apache.eventmesh.api.exception.AclException)4 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)4 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer)3 ProducerManager (org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager)3 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext)3 CompleteHandler (org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RequestReplyCallback (org.apache.eventmesh.api.RequestReplyCallback)2 Body (org.apache.eventmesh.common.protocol.http.body.Body)2