use of org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader in project incubator-eventmesh by apache.
the class SendMessageBatchV2ProtocolResolver method buildEvent.
public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHandleException {
try {
SendMessageBatchV2RequestHeader sendMessageBatchV2RequestHeader = (SendMessageBatchV2RequestHeader) header;
SendMessageBatchV2RequestBody sendMessageBatchV2RequestBody = (SendMessageBatchV2RequestBody) body;
String protocolType = sendMessageBatchV2RequestHeader.getProtocolType();
String protocolDesc = sendMessageBatchV2RequestHeader.getProtocolDesc();
String protocolVersion = sendMessageBatchV2RequestHeader.getProtocolVersion();
String code = sendMessageBatchV2RequestHeader.getCode();
String env = sendMessageBatchV2RequestHeader.getEnv();
String idc = sendMessageBatchV2RequestHeader.getIdc();
String ip = sendMessageBatchV2RequestHeader.getIp();
String pid = sendMessageBatchV2RequestHeader.getPid();
String sys = sendMessageBatchV2RequestHeader.getSys();
String username = sendMessageBatchV2RequestHeader.getUsername();
String passwd = sendMessageBatchV2RequestHeader.getPasswd();
ProtocolVersion version = sendMessageBatchV2RequestHeader.getVersion();
String language = sendMessageBatchV2RequestHeader.getLanguage();
String producerGroup = sendMessageBatchV2RequestBody.getProducerGroup();
String content = sendMessageBatchV2RequestBody.getMsg();
CloudEvent event = null;
if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(content.getBytes(StandardCharsets.UTF_8));
event = CloudEventBuilder.from(event).withExtension(ProtocolKey.REQUEST_CODE, code).withExtension(ProtocolKey.ClientInstanceKey.ENV, env).withExtension(ProtocolKey.ClientInstanceKey.IDC, idc).withExtension(ProtocolKey.ClientInstanceKey.IP, ip).withExtension(ProtocolKey.ClientInstanceKey.PID, pid).withExtension(ProtocolKey.ClientInstanceKey.SYS, sys).withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username).withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd).withExtension(ProtocolKey.VERSION, version.getVersion()).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, producerGroup).build();
} else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(content.getBytes(StandardCharsets.UTF_8));
event = CloudEventBuilder.from(event).withExtension(ProtocolKey.REQUEST_CODE, code).withExtension(ProtocolKey.ClientInstanceKey.ENV, env).withExtension(ProtocolKey.ClientInstanceKey.IDC, idc).withExtension(ProtocolKey.ClientInstanceKey.IP, ip).withExtension(ProtocolKey.ClientInstanceKey.PID, pid).withExtension(ProtocolKey.ClientInstanceKey.SYS, sys).withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username).withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd).withExtension(ProtocolKey.VERSION, version.getVersion()).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, producerGroup).build();
}
return event;
} catch (Exception e) {
throw new ProtocolHandleException(e.getMessage(), e.getCause());
}
}
use of org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader in project incubator-eventmesh by apache.
the class SendMessageBatchV2ProtocolResolver method buildEvent.
public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHandleException {
try {
SendMessageBatchV2RequestHeader sendMessageBatchV2RequestHeader = (SendMessageBatchV2RequestHeader) header;
SendMessageBatchV2RequestBody sendMessageBatchV2RequestBody = (SendMessageBatchV2RequestBody) body;
String protocolType = sendMessageBatchV2RequestHeader.getProtocolType();
String protocolDesc = sendMessageBatchV2RequestHeader.getProtocolDesc();
String protocolVersion = sendMessageBatchV2RequestHeader.getProtocolVersion();
String code = sendMessageBatchV2RequestHeader.getCode();
String env = sendMessageBatchV2RequestHeader.getEnv();
String idc = sendMessageBatchV2RequestHeader.getIdc();
String ip = sendMessageBatchV2RequestHeader.getIp();
String pid = sendMessageBatchV2RequestHeader.getPid();
String sys = sendMessageBatchV2RequestHeader.getSys();
String username = sendMessageBatchV2RequestHeader.getUsername();
String passwd = sendMessageBatchV2RequestHeader.getPasswd();
ProtocolVersion version = sendMessageBatchV2RequestHeader.getVersion();
String language = sendMessageBatchV2RequestHeader.getLanguage();
String content = sendMessageBatchV2RequestBody.getMsg();
CloudEvent event = null;
CloudEventBuilder cloudEventBuilder;
if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v1();
cloudEventBuilder = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()).withSubject(sendMessageBatchV2RequestBody.getTopic()).withType("eventmeshmessage").withSource(URI.create("/")).withData(content.getBytes(StandardCharsets.UTF_8)).withExtension(ProtocolKey.REQUEST_CODE, code).withExtension(ProtocolKey.ClientInstanceKey.ENV, env).withExtension(ProtocolKey.ClientInstanceKey.IDC, idc).withExtension(ProtocolKey.ClientInstanceKey.IP, ip).withExtension(ProtocolKey.ClientInstanceKey.PID, pid).withExtension(ProtocolKey.ClientInstanceKey.SYS, sys).withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username).withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd).withExtension(ProtocolKey.VERSION, version.getVersion()).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageBatchV2RequestBody.getBizSeqNo()).withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, sendMessageBatchV2RequestBody.getProducerGroup()).withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl());
if (StringUtils.isNotEmpty(sendMessageBatchV2RequestBody.getTag())) {
cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageBatchV2RequestBody.getTag());
}
event = cloudEventBuilder.build();
} else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v03();
cloudEventBuilder = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()).withSubject(sendMessageBatchV2RequestBody.getTopic()).withType("eventmeshmessage").withSource(URI.create("/")).withData(content.getBytes(StandardCharsets.UTF_8)).withExtension(ProtocolKey.REQUEST_CODE, code).withExtension(ProtocolKey.ClientInstanceKey.ENV, env).withExtension(ProtocolKey.ClientInstanceKey.IDC, idc).withExtension(ProtocolKey.ClientInstanceKey.IP, ip).withExtension(ProtocolKey.ClientInstanceKey.PID, pid).withExtension(ProtocolKey.ClientInstanceKey.SYS, sys).withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username).withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd).withExtension(ProtocolKey.VERSION, version.getVersion()).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageBatchV2RequestBody.getBizSeqNo()).withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, sendMessageBatchV2RequestBody.getProducerGroup()).withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl());
if (StringUtils.isNotEmpty(sendMessageBatchV2RequestBody.getTag())) {
cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageBatchV2RequestBody.getTag());
}
event = cloudEventBuilder.build();
}
return event;
} catch (Exception e) {
throw new ProtocolHandleException(e.getMessage(), e.getCause());
}
}
use of org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader 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);
}
Aggregations