use of org.apache.eventmesh.common.protocol.http.HttpCommand in project incubator-eventmesh by apache.
the class MeshMessageProtocolAdaptor method toCloudEvent.
@Override
public CloudEvent toCloudEvent(ProtocolTransportObject protocol) throws ProtocolHandleException {
if (protocol instanceof Package) {
Package tcpPackage = (Package) protocol;
Header header = tcpPackage.getHeader();
String bodyJson = (String) tcpPackage.getBody();
return deserializeTcpProtocol(header, bodyJson);
} else if (protocol instanceof HttpCommand) {
org.apache.eventmesh.common.protocol.http.header.Header header = ((HttpCommand) protocol).getHeader();
Body body = ((HttpCommand) protocol).getBody();
String requestCode = ((HttpCommand) protocol).getRequestCode();
return deserializeHttpProtocol(requestCode, header, body);
} else if (protocol instanceof SimpleMessageWrapper) {
SimpleMessage message = ((SimpleMessageWrapper) protocol).getMessage();
return deserializeGrpcProtocol(message);
} else {
throw new ProtocolHandleException(String.format("protocol class: %s", protocol.getClass()));
}
}
use of org.apache.eventmesh.common.protocol.http.HttpCommand 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));
}
}
use of org.apache.eventmesh.common.protocol.http.HttpCommand 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));
}
}
use of org.apache.eventmesh.common.protocol.http.HttpCommand in project incubator-eventmesh by apache.
the class CloudEventsProtocolAdaptor method toCloudEvent.
@Override
public CloudEvent toCloudEvent(ProtocolTransportObject cloudEvent) throws ProtocolHandleException {
if (cloudEvent instanceof Package) {
Package tcpPackage = (Package) cloudEvent;
Header header = tcpPackage.getHeader();
String cloudEventJson = tcpPackage.getBody().toString();
return deserializeTcpProtocol(header, cloudEventJson);
} else if (cloudEvent instanceof HttpCommand) {
org.apache.eventmesh.common.protocol.http.header.Header header = ((HttpCommand) cloudEvent).getHeader();
Body body = ((HttpCommand) cloudEvent).getBody();
String requestCode = ((HttpCommand) cloudEvent).getRequestCode();
return deserializeHttpProtocol(requestCode, header, body);
} else if (cloudEvent instanceof SimpleMessageWrapper) {
SimpleMessage simpleMessage = ((SimpleMessageWrapper) cloudEvent).getMessage();
return GrpcMessageProtocolResolver.buildEvent(simpleMessage);
} else {
throw new ProtocolHandleException(String.format("protocol class: %s", cloudEvent.getClass()));
}
}
use of org.apache.eventmesh.common.protocol.http.HttpCommand in project incubator-eventmesh by apache.
the class UnSubscribeProcessor method processRequest.
@Override
public void processRequest(ChannelHandlerContext ctx, AsyncContext<HttpCommand> asyncContext) throws Exception {
HttpCommand responseEventMeshCommand;
httpLogger.info("cmd={}|{}|client2eventMesh|from={}|to={}", RequestCode.get(Integer.valueOf(asyncContext.getRequest().getRequestCode())), EventMeshConstants.PROTOCOL_HTTP, RemotingHelper.parseChannelRemoteAddr(ctx.channel()), IPUtils.getLocalAddress());
UnSubscribeRequestHeader unSubscribeRequestHeader = (UnSubscribeRequestHeader) asyncContext.getRequest().getHeader();
UnSubscribeRequestBody unSubscribeRequestBody = (UnSubscribeRequestBody) asyncContext.getRequest().getBody();
UnSubscribeResponseHeader unSubscribeResponseHeader = UnSubscribeResponseHeader.buildHeader(Integer.valueOf(asyncContext.getRequest().getRequestCode()), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshCluster, IPUtils.getLocalAddress(), eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshEnv, eventMeshHTTPServer.getEventMeshHttpConfiguration().eventMeshIDC);
// validate header
if (StringUtils.isBlank(unSubscribeRequestHeader.getIdc()) || StringUtils.isBlank(unSubscribeRequestHeader.getPid()) || !StringUtils.isNumeric(unSubscribeRequestHeader.getPid()) || StringUtils.isBlank(unSubscribeRequestHeader.getSys())) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(unSubscribeResponseHeader, UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_HEADER_ERR.getErrMsg()));
asyncContext.onComplete(responseEventMeshCommand);
return;
}
// validate body
if (StringUtils.isBlank(unSubscribeRequestBody.getUrl()) || CollectionUtils.isEmpty(unSubscribeRequestBody.getTopics()) || StringUtils.isBlank(unSubscribeRequestBody.getConsumerGroup())) {
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(unSubscribeResponseHeader, UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_PROTOCOL_BODY_ERR.getErrMsg()));
asyncContext.onComplete(responseEventMeshCommand);
return;
}
String env = unSubscribeRequestHeader.getEnv();
String idc = unSubscribeRequestHeader.getIdc();
String sys = unSubscribeRequestHeader.getSys();
String ip = unSubscribeRequestHeader.getIp();
String pid = unSubscribeRequestHeader.getPid();
String consumerGroup = unSubscribeRequestBody.getConsumerGroup();
String unSubscribeUrl = unSubscribeRequestBody.getUrl();
List<String> unSubTopicList = unSubscribeRequestBody.getTopics();
final CompleteHandler<HttpCommand> handler = new CompleteHandler<HttpCommand>() {
@Override
public void onResponse(HttpCommand httpCommand) {
try {
if (httpLogger.isDebugEnabled()) {
httpLogger.debug("{}", httpCommand);
}
eventMeshHTTPServer.sendResponse(ctx, httpCommand.httpResponse());
eventMeshHTTPServer.metrics.getSummaryMetrics().recordHTTPReqResTimeCost(System.currentTimeMillis() - asyncContext.getRequest().getReqTime());
} catch (Exception ex) {
// ignore
}
}
};
synchronized (eventMeshHTTPServer.localClientInfoMapping) {
boolean isChange = true;
registerClient(unSubscribeRequestHeader, consumerGroup, unSubTopicList, unSubscribeUrl);
for (String unSubTopic : unSubTopicList) {
List<Client> groupTopicClients = eventMeshHTTPServer.localClientInfoMapping.get(consumerGroup + "@" + unSubTopic);
Iterator<Client> clientIterator = groupTopicClients.iterator();
while (clientIterator.hasNext()) {
Client client = clientIterator.next();
if (StringUtils.equals(client.pid, pid) && StringUtils.equals(client.url, unSubscribeUrl)) {
httpLogger.warn("client {} start unsubscribe", JsonUtils.serialize(client));
clientIterator.remove();
}
}
if (groupTopicClients.size() > 0) {
// change url
Map<String, List<String>> idcUrls = new HashMap<>();
Set<String> clientUrls = new HashSet<>();
for (Client client : groupTopicClients) {
// remove subscribed url
if (!StringUtils.equals(unSubscribeUrl, client.url)) {
clientUrls.add(client.url);
if (idcUrls.containsKey(client.idc)) {
idcUrls.get(client.idc).add(StringUtils.deleteWhitespace(client.url));
} else {
List<String> urls = new ArrayList<>();
urls.add(client.url);
idcUrls.put(client.idc, urls);
}
}
}
synchronized (eventMeshHTTPServer.localConsumerGroupMapping) {
ConsumerGroupConf consumerGroupConf = eventMeshHTTPServer.localConsumerGroupMapping.get(consumerGroup);
Map<String, ConsumerGroupTopicConf> map = consumerGroupConf.getConsumerGroupTopicConf();
for (String topicKey : map.keySet()) {
// only modify the topic to subscribe
if (StringUtils.equals(unSubTopic, topicKey)) {
ConsumerGroupTopicConf latestTopicConf = new ConsumerGroupTopicConf();
latestTopicConf.setConsumerGroup(consumerGroup);
latestTopicConf.setTopic(unSubTopic);
latestTopicConf.setSubscriptionItem(map.get(topicKey).getSubscriptionItem());
latestTopicConf.setUrls(clientUrls);
latestTopicConf.setIdcUrls(idcUrls);
map.put(unSubTopic, latestTopicConf);
}
}
eventMeshHTTPServer.localConsumerGroupMapping.put(consumerGroup, consumerGroupConf);
}
} else {
isChange = false;
break;
}
}
long startTime = System.currentTimeMillis();
if (isChange) {
try {
eventMeshHTTPServer.getConsumerManager().notifyConsumerManager(consumerGroup, eventMeshHTTPServer.localConsumerGroupMapping.get(consumerGroup));
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(EventMeshRetCode.SUCCESS);
asyncContext.onComplete(responseEventMeshCommand, handler);
} catch (Exception e) {
HttpCommand err = asyncContext.getRequest().createHttpCommandResponse(unSubscribeResponseHeader, UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2)));
asyncContext.onComplete(err);
long endTime = System.currentTimeMillis();
httpLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms" + "|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, JsonUtils.serialize(unSubscribeRequestBody.getTopics()), unSubscribeRequestBody.getUrl(), e);
eventMeshHTTPServer.metrics.getSummaryMetrics().recordSendMsgFailed();
eventMeshHTTPServer.metrics.getSummaryMetrics().recordSendMsgCost(endTime - startTime);
}
} else {
// remove
try {
eventMeshHTTPServer.getConsumerManager().notifyConsumerManager(consumerGroup, null);
responseEventMeshCommand = asyncContext.getRequest().createHttpCommandResponse(EventMeshRetCode.SUCCESS);
asyncContext.onComplete(responseEventMeshCommand, handler);
// clean ClientInfo
eventMeshHTTPServer.localClientInfoMapping.keySet().removeIf(s -> StringUtils.contains(s, consumerGroup));
// clean ConsumerGroupInfo
eventMeshHTTPServer.localConsumerGroupMapping.keySet().removeIf(s -> StringUtils.equals(consumerGroup, s));
} catch (Exception e) {
HttpCommand err = asyncContext.getRequest().createHttpCommandResponse(unSubscribeResponseHeader, UnSubscribeResponseBody.buildBody(EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getRetCode(), EventMeshRetCode.EVENTMESH_UNSUBSCRIBE_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2)));
asyncContext.onComplete(err);
long endTime = System.currentTimeMillis();
httpLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms" + "|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, JsonUtils.serialize(unSubscribeRequestBody.getTopics()), unSubscribeRequestBody.getUrl(), e);
eventMeshHTTPServer.metrics.getSummaryMetrics().recordSendMsgFailed();
eventMeshHTTPServer.metrics.getSummaryMetrics().recordSendMsgCost(endTime - startTime);
}
}
}
}
Aggregations