Search in sources :

Example 1 with HttpCommand

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()));
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 2 with HttpCommand

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));
    }
}
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 HttpCommand

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));
    }
}
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 HttpCommand

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()));
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 5 with HttpCommand

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);
            }
        }
    }
}
Also used : UnSubscribeRequestBody(org.apache.eventmesh.common.protocol.http.body.client.UnSubscribeRequestBody) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConsumerGroupTopicConf(org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf) UnSubscribeRequestHeader(org.apache.eventmesh.common.protocol.http.header.client.UnSubscribeRequestHeader) UnSubscribeResponseHeader(org.apache.eventmesh.common.protocol.http.header.client.UnSubscribeResponseHeader) CompleteHandler(org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerGroupConf(org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf) Client(org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand) HashSet(java.util.HashSet)

Aggregations

HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)14 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)8 CloudEvent (io.cloudevents.CloudEvent)6 CompleteHandler (org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler)6 ArrayList (java.util.ArrayList)5 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer)5 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext)5 HashMap (java.util.HashMap)4 List (java.util.List)4 SendCallback (org.apache.eventmesh.api.SendCallback)4 SendResult (org.apache.eventmesh.api.SendResult)4 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)4 Body (org.apache.eventmesh.common.protocol.http.body.Body)4 ProtocolHandleException (org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)4 Map (java.util.Map)3 Package (org.apache.eventmesh.common.protocol.tcp.Package)3 Client (org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)2 SimpleMessage (org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)2