Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update Eventmeshmessage plugin #599

Merged
merged 40 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fb84d90
[Feature #564] Support CloudEvents protocols for pub/sub in EventMesh…
xwm1992 Oct 28, 2021
4e55912
support cloudevents api in eventmesh-connector-api module
xwm1992 Nov 1, 2021
e602004
Merge branch 'develop' of https://github.com/apache/incubator-eventme…
xwm1992 Nov 1, 2021
9adb5e9
Merge branch 'develop' of https://github.com/apache/incubator-eventme…
xwm1992 Nov 1, 2021
670c2f2
fix checkStyle
xwm1992 Nov 1, 2021
691bcb3
fix checkStyle
xwm1992 Nov 1, 2021
b6a4613
fix checkStyle
xwm1992 Nov 1, 2021
0ac6d4f
1.support LifeCycle.java
xwm1992 Nov 1, 2021
2239a65
fix remove the extra blank line
xwm1992 Nov 1, 2021
086f4e6
Merge branch 'cloudevents' of https://github.com/apache/incubator-eve…
xwm1992 Nov 2, 2021
308dfd7
support cloudEvents
xwm1992 Nov 2, 2021
f288ae2
Add files via upload
qqeasonchen Nov 3, 2021
09b9a22
Update README.md
qqeasonchen Nov 3, 2021
f0f3108
support cloudEvents
xwm1992 Nov 8, 2021
0290837
support cloudEvents
xwm1992 Nov 9, 2021
90a28cc
[ISSUE #580] Add checkstyle gradle plugin (#581)
ruanwenjun Nov 10, 2021
4425aaf
support cloudEvents
xwm1992 Nov 10, 2021
0dce0ca
support cloudevents
xwm1992 Nov 10, 2021
9251e07
update wechat-official qr code
xwm1992 Nov 11, 2021
ff1c247
update mesh-helper qr code
xwm1992 Nov 11, 2021
00994c3
Add files via upload
qqeasonchen Nov 11, 2021
bcf0337
update README.md
xwm1992 Nov 11, 2021
4938cd7
update README.md
xwm1992 Nov 11, 2021
61d0410
Update .asf.yaml
qqeasonchen Nov 11, 2021
3166325
support cloudEvents
xwm1992 Nov 11, 2021
bbd3535
support cloudEvents
xwm1992 Nov 12, 2021
de99c54
Merge branch 'cloudevents' of https://github.com/apache/incubator-eve…
xwm1992 Nov 12, 2021
ceaf33b
[ISSUE #588] Fix typo in README.md (#589)
nichozhan Nov 15, 2021
468386c
Merge branch 'cloudevents' of https://github.com/apache/incubator-eve…
xwm1992 Nov 16, 2021
d2d2b7d
support cloudEvents
xwm1992 Nov 16, 2021
46f973d
[Bug #590] Consumer subscription topic is invalid (#590) (#592)
hagsyn Nov 16, 2021
cf8433a
support cloudEvents adaptor
xwm1992 Nov 16, 2021
18310a1
[Feature #562] Implement CloudEvents adaptor
xwm1992 Nov 17, 2021
3149552
Merge branch 'develop' of https://github.com/apache/incubator-eventme…
xwm1992 Nov 17, 2021
d7ddac8
Merge branch 'cloudevents' of https://github.com/apache/incubator-eve…
xwm1992 Nov 17, 2021
c534985
[Feature #562] Implement EventMeshMessage protocol adaptor
xwm1992 Nov 18, 2021
024e462
Merge branch 'cloudevents' of https://github.com/apache/incubator-eve…
xwm1992 Nov 18, 2021
f096294
supplement apache header
xwm1992 Nov 18, 2021
46d8a51
Merge branch 'cloudevents' of https://github.com/apache/incubator-eve…
xwm1992 Nov 19, 2021
0f34e98
1.update package name
xwm1992 Nov 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
support cloudEvents
  • Loading branch information
xwm1992 committed Nov 11, 2021
commit 316632577db5e48c3d0b7f13148ffe89617c891e
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.cloudevents.CloudEvent;
import io.cloudevents.core.v1.CloudEventV1;

import java.util.List;

/**
* Protocol transformer SPI interface, all protocol plugin should implementation.
*
Expand All @@ -43,6 +45,13 @@ public interface ProtocolAdaptor<T> {
*/
CloudEventV1 toCloudEventV1(T protocol) throws ProtocolHandleException;

/**
* transform protocol to {@link CloudEvent} list.
*
* @param protocol input protocol
* @return list cloud event
*/
List<CloudEventV1> toBatchCloudEventV1(T protocol) throws ProtocolHandleException;

/**
* Transform {@link CloudEvent} to target protocol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@

package org.apache.eventmesh.runtime.core.protocol.http.processor;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.v1.CloudEventBuilder;
import io.cloudevents.core.v1.CloudEventV1;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.eventmesh.api.SendCallback;
import org.apache.eventmesh.api.SendResult;
import org.apache.eventmesh.api.exception.OnExceptionContext;
import org.apache.eventmesh.common.Constants;
import org.apache.eventmesh.common.IPUtil;
import org.apache.eventmesh.common.command.HttpCommand;
import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchRequestBody;
import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchResponseBody;
import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody;
import org.apache.eventmesh.common.protocol.http.body.message.SendMessageResponseBody;
import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode;
import org.apache.eventmesh.common.protocol.http.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.http.common.RequestCode;
import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchRequestHeader;
import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchResponseHeader;
import org.apache.eventmesh.protocol.api.ProtocolAdaptor;
import org.apache.eventmesh.protocol.api.ProtocolPluginFactory;
import org.apache.eventmesh.runtime.acl.Acl;
import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer;
import org.apache.eventmesh.runtime.constants.EventMeshConstants;
Expand All @@ -44,6 +55,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

Expand All @@ -70,49 +82,87 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext<HttpCommand>
EventMeshConstants.PROTOCOL_HTTP,
RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtil.getLocalAddress());

SendMessageBatchRequestHeader sendMessageBatchRequestHeader = (SendMessageBatchRequestHeader) asyncContext.getRequest().getHeader();
SendMessageBatchRequestBody sendMessageBatchRequestBody = (SendMessageBatchRequestBody) asyncContext.getRequest().getBody();

SendMessageBatchResponseHeader sendMessageBatchResponseHeader =
SendMessageBatchResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster,
IPUtil.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv,
eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC);


if (StringUtils.isBlank(sendMessageBatchRequestHeader.getPid())
|| !StringUtils.isNumeric(sendMessageBatchRequestHeader.getPid())
|| StringUtils.isBlank(sendMessageBatchRequestHeader.getSys())) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg()));
asyncContext.onComplete(responseEventMeshCommand);
return;
}
ProtocolAdaptor httpCommandProtocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor("cloudevents");
List<CloudEvent> eventList = httpCommandProtocolAdaptor.toBatchCloudEventV1(asyncContext.getRequest());

if (CollectionUtils.isEmpty(sendMessageBatchRequestBody.getContents())
|| StringUtils.isBlank(sendMessageBatchRequestBody.getBatchId())
|| StringUtils.isBlank(sendMessageBatchRequestBody.getProducerGroup())
|| (Integer.valueOf(sendMessageBatchRequestBody.getSize()) != CollectionUtils.size(sendMessageBatchRequestBody.getContents()))) {
if (CollectionUtils.isEmpty(eventList)) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg()));
asyncContext.onComplete(responseEventMeshCommand);
return;
}

String batchId = "";
String producerGroup = "";
int eventSize = eventList.size();

for (CloudEvent event : eventList) {
//validate event
if (StringUtils.isBlank(event.getId())
|| event.getSource() != null
|| event.getSpecVersion() != null
|| StringUtils.isBlank(event.getType())
|| StringUtils.isBlank(event.getSubject())) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
SendMessageBatchResponseBody.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 = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg()));
asyncContext.onComplete(responseEventMeshCommand);
return;
}


batchId = Objects.requireNonNull(event.getExtension(SendMessageBatchRequestBody.BATCHID)).toString();
producerGroup = Objects.requireNonNull(event.getExtension(SendMessageBatchRequestBody.PRODUCERGROUP)).toString();
String topic = event.getSubject();
eventSize = Integer.parseInt(Objects.requireNonNull(event.getExtension(SendMessageBatchRequestBody.SIZE)).toString());
CloudEventData eventData = event.getData();

if (eventData != null || StringUtils.isBlank(batchId)
|| StringUtils.isBlank(producerGroup)
|| eventSize != eventList.size()) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg()));
asyncContext.onComplete(responseEventMeshCommand);
return;
}

}

if (!eventMeshHTTPServer.getBatchRateLimiter()
.tryAcquire(Integer.valueOf(sendMessageBatchRequestBody.getSize()), EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) {
.tryAcquire(eventSize, EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR.getErrMsg()));
eventMeshHTTPServer.metrics.summaryMetrics
.recordSendBatchMsgDiscard(Integer.valueOf(sendMessageBatchRequestBody.getSize()));
.recordSendBatchMsgDiscard(eventSize);
asyncContext.onComplete(responseEventMeshCommand);
return;
}


String producerGroup = sendMessageBatchRequestBody.getProducerGroup();
EventMeshProducer batchEventMeshProducer = eventMeshHTTPServer.getProducerManager().getEventMeshProducer(producerGroup);

batchEventMeshProducer.getMqProducerWrapper().getMeshMQProducer().setExtFields();
Expand All @@ -128,24 +178,25 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext<HttpCommand>
long batchStartTime = System.currentTimeMillis();

String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
String user = sendMessageBatchRequestHeader.getUsername();
String pass = sendMessageBatchRequestHeader.getPasswd();
String subsystem = sendMessageBatchRequestHeader.getSys();
int requestCode = Integer.valueOf(sendMessageBatchRequestHeader.getCode());

List<Message> msgList = new ArrayList<>();
Map<String, List<Message>> topicBatchMessageMappings = new ConcurrentHashMap<String, List<Message>>();
for (SendMessageBatchRequestBody.BatchMessageEntity msg : sendMessageBatchRequestBody.getContents()) {
if (StringUtils.isBlank(msg.topic)
|| StringUtils.isBlank(msg.msg)) {
int requestCode = Integer.parseInt(asyncContext.getRequest().getRequestCode());

Map<String, List<CloudEvent>> topicBatchMessageMappings = new ConcurrentHashMap<String, List<CloudEvent>>();

for (CloudEvent cloudEvent : eventList) {
if (StringUtils.isBlank(cloudEvent.getSubject())
|| cloudEvent.getData() != null) {
continue;
}

String user = Objects.requireNonNull(cloudEvent.getExtension(ProtocolKey.ClientInstanceKey.USERNAME)).toString();
String pass = Objects.requireNonNull(cloudEvent.getExtension(ProtocolKey.ClientInstanceKey.PASSWD)).toString();
String subsystem = Objects.requireNonNull(cloudEvent.getExtension(ProtocolKey.ClientInstanceKey.SYS)).toString();

//do acl check
if(eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerSecurityEnable) {
try {
Acl.doAclCheckInHttpSend(remoteAddr, user, pass, subsystem, msg.topic, requestCode);
}catch (Exception e){
Acl.doAclCheckInHttpSend(remoteAddr, user, pass, subsystem, cloudEvent.getSubject(), 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(
Expand All @@ -157,63 +208,49 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext<HttpCommand>
}
}

if (StringUtils.isBlank(msg.ttl) || !StringUtils.isNumeric(msg.ttl)) {
msg.ttl = String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS);
}

try {
// Message rocketMQMsg;
Message omsMsg = new Message();
// topic
omsMsg.setTopic(msg.topic);
// body
omsMsg.setBody(msg.msg.getBytes(EventMeshConstants.DEFAULT_CHARSET));
if (!StringUtils.isBlank(msg.tag)) {
omsMsg.putUserProperties(EventMeshConstants.TAG, msg.tag);
String ttl = Objects.requireNonNull(cloudEvent.getExtension(SendMessageRequestBody.TTL)).toString();

if (StringUtils.isBlank(ttl) || !StringUtils.isNumeric(ttl)) {
cloudEvent = new CloudEventBuilder(cloudEvent)
.withExtension(SendMessageRequestBody.TTL, String.valueOf(EventMeshConstants.DEFAULT_MSG_TTL_MILLS))
.withExtension("msgType", "persistent")
.build();
}
// if (StringUtils.isBlank(msg.tag)) {
// rocketMQMsg = new Message(msg.topic, msg.msg.getBytes(EventMeshConstants.DEFAULT_CHARSET));
// } else {
// rocketMQMsg = new Message(msg.topic, msg.tag, msg.msg.getBytes(EventMeshConstants.DEFAULT_CHARSET));
// }
omsMsg.putUserProperties("msgType", "persistent");
// ttl
omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, msg.ttl);
//MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, msg.ttl);
msgList.add(omsMsg);
if (topicBatchMessageMappings.containsKey(msg.topic)) {
topicBatchMessageMappings.get(msg.topic).add(omsMsg);

if (topicBatchMessageMappings.containsKey(cloudEvent.getSubject())) {
topicBatchMessageMappings.get(cloudEvent.getSubject()).add(cloudEvent);
} else {
List<Message> tmp = new ArrayList<>();
tmp.add(omsMsg);
topicBatchMessageMappings.put(msg.topic, tmp);
List<CloudEvent> tmp = new ArrayList<>();
tmp.add(cloudEvent);
topicBatchMessageMappings.put(cloudEvent.getSubject(), tmp);
}

if (batchMessageLogger.isDebugEnabled()) {
batchMessageLogger.debug("msg2MQMsg suc, msg:{}", msg.msg);
batchMessageLogger.debug("msg2MQMsg suc, event:{}", cloudEvent.getData());
}

} catch (Exception e) {
batchMessageLogger.error("msg2MQMsg err, msg:{}", msg, e);
batchMessageLogger.error("msg2MQMsg err, event:{}", cloudEvent.getData(), e);
}

}

if (CollectionUtils.isEmpty(msgList)) {
if (CollectionUtils.isEmpty(eventList)) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
SendMessageBatchResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg()));
asyncContext.onComplete(responseEventMeshCommand);
return;
}

String sizeStr = sendMessageBatchRequestBody.getSize();
long delta = StringUtils.isNumeric(sizeStr)? Integer.parseInt(sizeStr) : 0;
long delta = eventSize;
eventMeshHTTPServer.metrics.summaryMetrics.recordSendBatchMsg(delta);

if (eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshServerBatchMsgBatchEnabled) {
for (List<Message> batchMsgs : topicBatchMessageMappings.values()) {
for (List<CloudEvent> eventlist : topicBatchMessageMappings.values()) {
// TODO: Implementation in API. Consider whether to put it in the plug-in.
Message omsMsg = new Message();
CloudEvent event = null;
// Message omsMsg = new Message();
// try {
// msgBatch = msgBatch.generateFromList(batchMsgs);
// for (Message message : msgBatch.getMessages()) {
Expand All @@ -226,8 +263,8 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext<HttpCommand>
// continue;
// }

final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchRequestBody.getBatchId(), omsMsg, batchEventMeshProducer, eventMeshHTTPServer);
sendMessageContext.setMessageList(batchMsgs);
final SendMessageContext sendMessageContext = new SendMessageContext(batchId, event, batchEventMeshProducer, eventMeshHTTPServer);
sendMessageContext.setEventList(eventlist);
batchEventMeshProducer.send(sendMessageContext, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
Expand All @@ -239,16 +276,11 @@ public void onException(OnExceptionContext context) {
eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
}

// @Override
// public void onException(Throwable e) {
// batchMessageLogger.warn("", e);
// eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
// }
});
}
} else {
for (Message msg : msgList) {
final SendMessageContext sendMessageContext = new SendMessageContext(sendMessageBatchRequestBody.getBatchId(), msg, batchEventMeshProducer, eventMeshHTTPServer);
for (CloudEvent event : eventList) {
final SendMessageContext sendMessageContext = new SendMessageContext(batchId, event, batchEventMeshProducer, eventMeshHTTPServer);
batchEventMeshProducer.send(sendMessageContext, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
Expand All @@ -261,22 +293,14 @@ public void onException(OnExceptionContext context) {
eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
}

// @Override
// public void onException(Throwable e) {
// batchMessageLogger.warn("", e);
// eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
// }
});
}
}

long batchEndTime = System.currentTimeMillis();
eventMeshHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime);
batchMessageLogger.debug("batchMessage|eventMesh2mq|REQ|ASYNC|batchId={}|send2MQCost={}ms|msgNum={}|topics={}",
sendMessageBatchRequestBody.getBatchId(),
batchEndTime - batchStartTime,
sendMessageBatchRequestBody.getSize(),
topicBatchMessageMappings.keySet());
batchId, batchEndTime - batchStartTime, eventSize, topicBatchMessageMappings.keySet());

responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(
sendMessageBatchResponseHeader,
Expand Down
Loading