Search in sources :

Example 1 with SendMessageBatchV2ResponseHeader

use of org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2ResponseHeader in project incubator-eventmesh by apache.

the class BatchSendMessageV2Processor method processRequest.

@Override
public void processRequest(ChannelHandlerContext ctx, AsyncContext<HttpCommand> asyncContext) throws Exception {
    HttpCommand responseEventMeshCommand;
    final HttpCommand request = asyncContext.getRequest();
    final Integer requestCode = Integer.valueOf(request.getRequestCode());
    cmdLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(requestCode), EventMeshConstants.PROTOCOL_HTTP, RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtils.getLocalAddress());
    SendMessageBatchV2RequestHeader sendMessageBatchV2RequestHeader = (SendMessageBatchV2RequestHeader) asyncContext.getRequest().getHeader();
    String protocolType = sendMessageBatchV2RequestHeader.getProtocolType();
    ProtocolAdaptor<ProtocolTransportObject> httpCommandProtocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
    CloudEvent event = httpCommandProtocolAdaptor.toCloudEvent(asyncContext.getRequest());
    SendMessageBatchV2ResponseHeader sendMessageBatchV2ResponseHeader = SendMessageBatchV2ResponseHeader.buildHeader(requestCode, eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, IPUtils.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC);
    // validate event
    if (StringUtils.isBlank(event.getId()) || event.getSource() == null || event.getSpecVersion() == null || StringUtils.isBlank(event.getType()) || StringUtils.isBlank(event.getSubject())) {
        responseEventMeshCommand = request.createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg()));
        asyncContext.onComplete(responseEventMeshCommand);
        return;
    }
    String idc = Objects.requireNonNull(event.getExtension(ProtocolKey.ClientInstanceKey.IDC)).toString();
    String pid = Objects.requireNonNull(event.getExtension(ProtocolKey.ClientInstanceKey.PID)).toString();
    String sys = Objects.requireNonNull(event.getExtension(ProtocolKey.ClientInstanceKey.SYS)).toString();
    // validate event-extension
    if (StringUtils.isBlank(idc) || StringUtils.isBlank(pid) || !StringUtils.isNumeric(pid) || StringUtils.isBlank(sys)) {
        responseEventMeshCommand = request.createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg()));
        asyncContext.onComplete(responseEventMeshCommand);
        return;
    }
    String bizNo = Objects.requireNonNull(event.getExtension(SendMessageBatchV2RequestBody.BIZSEQNO)).toString();
    String producerGroup = Objects.requireNonNull(event.getExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP)).toString();
    String topic = event.getSubject();
    if (StringUtils.isBlank(bizNo) || StringUtils.isBlank(topic) || StringUtils.isBlank(producerGroup) || event.getData() == null) {
        responseEventMeshCommand = request.createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg()));
        asyncContext.onComplete(responseEventMeshCommand);
        return;
    }
    String content = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
    if (content.length() > eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEventSize) {
        batchMessageLogger.error("Event size exceeds the limit: {}", eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEventSize);
        responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), "Event size exceeds the limit: " + eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEventSize));
        asyncContext.onComplete(responseEventMeshCommand);
        return;
    }
    // do acl check
    if (eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerSecurityEnable) {
        String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
        String user = event.getExtension(ProtocolKey.ClientInstanceKey.USERNAME).toString();
        String pass = event.getExtension(ProtocolKey.ClientInstanceKey.PASSWD).toString();
        String subsystem = event.getExtension(ProtocolKey.ClientInstanceKey.SYS).toString();
        try {
            Acl.doAclCheckInHttpSend(remoteAddr, user, pass, subsystem, topic, requestCode);
        } catch (Exception e) {
            // String errorMsg = String.format("CLIENT HAS NO PERMISSION,send failed, topic:%s, subsys:%s, realIp:%s", topic, subsys, realIp);
            responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_ACL_ERR.getRetCode(), e.getMessage()));
            asyncContext.onComplete(responseEventMeshCommand);
            aclLogger.warn("CLIENT HAS NO PERMISSION,BatchSendMessageV2Processor send failed", e);
            return;
        }
    }
    if (!eventMeshHTTPServer.getBatchRateLimiter().tryAcquire(EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) {
        responseEventMeshCommand = request.createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getErrMsg()));
        eventMeshHTTPServer.metrics.getSummaryMetrics().recordSendBatchMsgDiscard(1);
        asyncContext.onComplete(responseEventMeshCommand);
        return;
    }
    EventMeshProducer batchEventMeshProducer = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(producerGroup);
    batchEventMeshProducer.getMqProducerWrapper().getMeshMQProducer().setExtFields();
    if (!batchEventMeshProducer.getStarted().get()) {
        responseEventMeshCommand = request.createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_BATCH_PRODUCER_STOPED_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_BATCH_PRODUCER_STOPED_ERR.getErrMsg()));
        asyncContext.onComplete(responseEventMeshCommand);
        return;
    }
    long batchStartTime = System.currentTimeMillis();
    String ttl = String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS);
    // todo: use hashmap to avoid copy
    if (StringUtils.isBlank(event.getExtension(SendMessageRequestBody.TTL).toString()) && !StringUtils.isNumeric(event.getExtension(SendMessageRequestBody.TTL).toString())) {
        event = CloudEventBuilder.from(event).withExtension(SendMessageRequestBody.TTL, ttl).build();
    }
    try {
        event = CloudEventBuilder.from(event).withExtension("msgtype", "persistent").withExtension(EventMeshConstants.REQ_C2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())).withExtension(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())).build();
        if (batchMessageLogger.isDebugEnabled()) {
            batchMessageLogger.debug("msg2MQMsg suc, topic:{}, msg:{}", topic, event.getData());
        }
    } catch (Exception e) {
        batchMessageLogger.error("msg2MQMsg err, topic:{}, msg:{}", topic, event.getData(), e);
        responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PACKAGE_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2)));
        asyncContext.onComplete(responseEventMeshCommand);
        return;
    }
    eventMeshHTTPServer.metrics.getSummaryMetrics().recordSendBatchMsg(1);
    final SendMessageContext sendMessageContext = new SendMessageContext(bizNo, event, batchEventMeshProducer, eventMeshHTTPServer);
    try {
        batchEventMeshProducer.send(sendMessageContext, new SendCallback() {

            @Override
            public void onSuccess(SendResult sendResult) {
                long batchEndTime = System.currentTimeMillis();
                eventMeshHTTPServer.metrics.getSummaryMetrics().recordBatchSendMsgCost(batchEndTime - batchStartTime);
                batchMessageLogger.debug("batchMessageV2|eventMesh2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", bizNo, batchEndTime - batchStartTime, topic);
            }

            @Override
            public void onException(OnExceptionContext context) {
                long batchEndTime = System.currentTimeMillis();
                eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
                eventMeshHTTPServer.metrics.getSummaryMetrics().recordBatchSendMsgCost(batchEndTime - batchStartTime);
                batchMessageLogger.error("batchMessageV2|eventMesh2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", bizNo, batchEndTime - batchStartTime, topic, context.getException());
            }
        });
    } catch (Exception e) {
        responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_BATCHLOG_MSG_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_SEND_BATCHLOG_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2)));
        asyncContext.onComplete(responseEventMeshCommand);
        long batchEndTime = System.currentTimeMillis();
        eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
        eventMeshHTTPServer.metrics.getSummaryMetrics().recordBatchSendMsgCost(batchEndTime - batchStartTime);
        batchMessageLogger.error("batchMessageV2|eventMesh2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", bizNo, batchEndTime - batchStartTime, topic, e);
    }
    responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(sendMessageBatchV2ResponseHeader, SendMessageBatchV2ResponseBody.buildBody(EventMeshRetCode.SUCCESS.getRetCode(), EventMeshRetCode.SUCCESS.getErrMsg()));
    asyncContext.onComplete(responseEventMeshCommand);
}
Also used : EventMeshProducer(org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SendMessageBatchV2RequestHeader(org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader) SendMessageBatchV2ResponseHeader(org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2ResponseHeader) SendMessageContext(org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext) SendResult(org.apache.eventmesh.api.SendResult) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand) CloudEvent(io.cloudevents.CloudEvent) SendCallback(org.apache.eventmesh.api.SendCallback)

Aggregations

CloudEvent (io.cloudevents.CloudEvent)1 SendCallback (org.apache.eventmesh.api.SendCallback)1 SendResult (org.apache.eventmesh.api.SendResult)1 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)1 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)1 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)1 SendMessageBatchV2RequestHeader (org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader)1 SendMessageBatchV2ResponseHeader (org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2ResponseHeader)1 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer)1 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext)1