pandaapo commented on code in PR #4578:
URL: https://github.com/apache/eventmesh/pull/4578#discussion_r1404369019


##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/FilterEngine.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.runtime.boot;
+
+import org.apache.eventmesh.api.meta.MetaServiceListener;
+import org.apache.eventmesh.common.utils.JsonUtils;
+import org.apache.eventmesh.common.utils.LogUtils;
+import org.apache.eventmesh.filter.pattern.Pattern;
+import org.apache.eventmesh.filter.patternbuild.PatternBuilder;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerGroupManager;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerManager;
+import org.apache.eventmesh.runtime.core.protocol.producer.EventMeshProducer;
+import org.apache.eventmesh.runtime.core.protocol.producer.ProducerManager;
+import org.apache.eventmesh.runtime.meta.MetaStorage;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class FilterEngine {
+
+    /**
+     * key:group-topic
+     **/
+    private final Map<String, Pattern> filterPatternMap = new HashMap<>();
+
+    private final String filterPrefix = "filter-";
+
+    private final MetaStorage metaStorage;
+
+    private MetaServiceListener metaServiceListener;
+
+    private final ProducerManager producerManager;
+
+    private final ConsumerManager consumerManager;
+
+    private final ScheduledExecutorService scheduledExecutorService = 
Executors.newSingleThreadScheduledExecutor();
+
+    public FilterEngine(MetaStorage metaStorage, ProducerManager 
producerManager, ConsumerManager consumerManager) {
+        this.metaStorage = metaStorage;
+        this.producerManager = producerManager;
+        this.consumerManager = consumerManager;
+    }
+
+    public void start() {
+        Map<String, String> filterMetaData = 
metaStorage.getMetaData(filterPrefix, true);
+        for (Entry<String, String> filterDataEntry : 
filterMetaData.entrySet()) {
+            // filter-group
+            String key = filterDataEntry.getKey();
+            // topic-filterRule list
+            String value = filterDataEntry.getValue();
+            updateFilterPatternMap(key, value);
+        }
+        metaServiceListener = this::updateFilterPatternMap;
+
+        // addListeners for producerManager & consumerManager
+        scheduledExecutorService.scheduleAtFixedRate(() -> {

Review Comment:
   There is one more issue, and I haven't figured out how to handle it. Perhaps 
a note can be made and optimize it later. Can expired or obsolete rules be 
detected locally, and then the corresponding rules in the remote configuration 
can be deleted? This can save some manual management of remote configuration 
work and control the traversal here as effectively as possible.
   
   
还有一个问题,但我没想到怎么处理,也许可以备注一下后期再优化。能否从本地检测出已经失效或过时的规则,然后删除远程配置中对应的规则?这样可以省去一部分手动管理远程配置的工作,也可以控制这里的遍历尽量有效。



##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/FilterEngine.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.runtime.boot;
+
+import org.apache.eventmesh.api.meta.MetaServiceListener;
+import org.apache.eventmesh.common.utils.JsonUtils;
+import org.apache.eventmesh.common.utils.LogUtils;
+import org.apache.eventmesh.filter.pattern.Pattern;
+import org.apache.eventmesh.filter.patternbuild.PatternBuilder;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerGroupManager;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerManager;
+import org.apache.eventmesh.runtime.core.protocol.producer.EventMeshProducer;
+import org.apache.eventmesh.runtime.core.protocol.producer.ProducerManager;
+import org.apache.eventmesh.runtime.meta.MetaStorage;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class FilterEngine {
+
+    /**
+     * key:group-topic
+     **/
+    private final Map<String, Pattern> filterPatternMap = new HashMap<>();
+
+    private final String filterPrefix = "filter-";
+
+    private final MetaStorage metaStorage;
+
+    private MetaServiceListener metaServiceListener;
+
+    private final ProducerManager producerManager;
+
+    private final ConsumerManager consumerManager;
+
+    private final ScheduledExecutorService scheduledExecutorService = 
Executors.newSingleThreadScheduledExecutor();
+
+    public FilterEngine(MetaStorage metaStorage, ProducerManager 
producerManager, ConsumerManager consumerManager) {
+        this.metaStorage = metaStorage;
+        this.producerManager = producerManager;
+        this.consumerManager = consumerManager;
+    }
+
+    public void start() {
+        Map<String, String> filterMetaData = 
metaStorage.getMetaData(filterPrefix, true);
+        for (Entry<String, String> filterDataEntry : 
filterMetaData.entrySet()) {
+            // filter-group
+            String key = filterDataEntry.getKey();
+            // topic-filterRule list
+            String value = filterDataEntry.getValue();
+            updateFilterPatternMap(key, value);
+        }
+        metaServiceListener = this::updateFilterPatternMap;
+
+        // addListeners for producerManager & consumerManager
+        scheduledExecutorService.scheduleAtFixedRate(() -> {

Review Comment:
   There is one more issue, and I haven't figured out how to handle it. Perhaps 
a note can be made and optimize it later. Can expired or obsolete rules be 
detected locally, and then the corresponding rules in the remote configuration 
can be deleted? This can save some manual management of remote configuration 
work and control the traversal here as effectively as possible.
   
   
还有一个问题,但我没想到怎么处理,也许可以备注一下后期再优化。能否从本地检测出已经失效或过时的规则,然后删除远程配置中对应的规则?这样可以省去一部分手动管理远程配置的工作,也可以控制这里的遍历尽量有效。



##########
eventmesh-meta/eventmesh-meta-nacos/src/main/java/org/apache/eventmesh/meta/nacos/service/NacosMetaService.java:
##########
@@ -242,16 +277,67 @@ public void registerMetadata(Map<String, String> 
metadataMap) {
         }
     }
 
+    // implement with http
     @Override
-    public String getMetaData(String key) {
-        try {
-            return this.nacosConfigService.getConfig(key, group, 5000L);
-        } catch (NacosException e) {
+    public Map<String, String> getMetaData(String key, boolean fuzzyEnabled) {
+        if (fuzzyEnabled) {
+            key = key + "*";
+        }
+        int pageNo = 1;
+        int pageSize = 100;

Review Comment:
   Can `pageNo` and `pageSize` be parameterized to make it easier to use for 
eventmesh-admin calls in the future?
   
   `pageNo`和`pageSize`是不是可以参数化,方便后续eventmesh-admin调用时使用?



##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncEventProcessor.java:
##########
@@ -237,41 +241,53 @@ public void handler(final HandlerService.HandlerSpecific 
handlerSpecific, final
         eventMeshHTTPServer.getMetrics().getSummaryMetrics().recordSendMsg();
 
         final long startTime = System.currentTimeMillis();
-
+        boolean isFiltered = true;
         try {
             event = CloudEventBuilder.from(sendMessageContext.getEvent())
                 .withExtension(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, 
String.valueOf(System.currentTimeMillis()))
                 .build();
             
handlerSpecific.getTraceOperation().createClientTraceOperation(EventMeshUtil.getCloudEventExtensionMap(SpecVersion.V1.toString(),
 event),
                 EventMeshTraceConstants.TRACE_UPSTREAM_EVENTMESH_CLIENT_SPAN, 
false);
+            if (filterPattern != null) {
+                isFiltered = 
filterPattern.filter(JsonUtils.toJSONString(event));
+            }
 
-            eventMeshProducer.send(sendMessageContext, new SendCallback() {
-
-                @Override
-                public void onSuccess(final SendResult sendResult) {
-                    responseBodyMap.put(EventMeshConstants.RET_CODE, 
EventMeshRetCode.SUCCESS.getRetCode());
-                    responseBodyMap.put(EventMeshConstants.RET_MSG, 
EventMeshRetCode.SUCCESS.getErrMsg() + sendResult);
-
-                    LogUtils.info(log, 
"message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}",
-                        System.currentTimeMillis() - startTime, topic, bizNo, 
uniqueId);
-                    
handlerSpecific.getTraceOperation().endLatestTrace(sendMessageContext.getEvent());
-                    handlerSpecific.sendResponse(responseHeaderMap, 
responseBodyMap);
-                }
+            if (isFiltered) {
+                eventMeshProducer.send(sendMessageContext, new SendCallback() {

Review Comment:
   I don't quite understand this point. Is it necessary for producers to set 
filtering rules when sending messages?
   
   这一点没太明白,生产者发送消息有必要设置过滤规则?



##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/FilterEngine.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.runtime.boot;
+
+import org.apache.eventmesh.api.meta.MetaServiceListener;
+import org.apache.eventmesh.common.utils.JsonUtils;
+import org.apache.eventmesh.common.utils.LogUtils;
+import org.apache.eventmesh.filter.pattern.Pattern;
+import org.apache.eventmesh.filter.patternbuild.PatternBuilder;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerGroupManager;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerManager;
+import org.apache.eventmesh.runtime.core.protocol.producer.EventMeshProducer;
+import org.apache.eventmesh.runtime.core.protocol.producer.ProducerManager;
+import org.apache.eventmesh.runtime.meta.MetaStorage;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class FilterEngine {
+
+    /**
+     * key:group-topic
+     **/
+    private final Map<String, Pattern> filterPatternMap = new HashMap<>();
+
+    private final String filterPrefix = "filter-";
+
+    private final MetaStorage metaStorage;
+
+    private MetaServiceListener metaServiceListener;
+
+    private final ProducerManager producerManager;
+
+    private final ConsumerManager consumerManager;
+
+    private final ScheduledExecutorService scheduledExecutorService = 
Executors.newSingleThreadScheduledExecutor();
+
+    public FilterEngine(MetaStorage metaStorage, ProducerManager 
producerManager, ConsumerManager consumerManager) {
+        this.metaStorage = metaStorage;
+        this.producerManager = producerManager;
+        this.consumerManager = consumerManager;
+    }
+
+    public void start() {
+        Map<String, String> filterMetaData = 
metaStorage.getMetaData(filterPrefix, true);
+        for (Entry<String, String> filterDataEntry : 
filterMetaData.entrySet()) {
+            // filter-group
+            String key = filterDataEntry.getKey();
+            // topic-filterRule list
+            String value = filterDataEntry.getValue();
+            updateFilterPatternMap(key, value);
+        }
+        metaServiceListener = this::updateFilterPatternMap;
+
+        // addListeners for producerManager & consumerManager
+        scheduledExecutorService.scheduleAtFixedRate(() -> {

Review Comment:
   Could you add another task to pull the newly added rules from the remote 
configuration center?
   
   这里面是不是还可以再增加一个任务,去拉取远程配置中心新增的规则配置?



##########
eventmesh-meta/eventmesh-meta-nacos/src/main/java/org/apache/eventmesh/meta/nacos/service/NacosMetaService.java:
##########
@@ -242,16 +277,67 @@ public void registerMetadata(Map<String, String> 
metadataMap) {
         }
     }
 
+    // implement with http
     @Override
-    public String getMetaData(String key) {
-        try {
-            return this.nacosConfigService.getConfig(key, group, 5000L);
-        } catch (NacosException e) {
+    public Map<String, String> getMetaData(String key, boolean fuzzyEnabled) {
+        if (fuzzyEnabled) {
+            key = key + "*";
+        }
+        int pageNo = 1;
+        int pageSize = 100;

Review Comment:
   Can `pageNo` and `pageSize` be parameterized to make it easier to use for 
eventmesh-admin calls in the future?
   
   `pageNo`和`pageSize`是不是可以参数化,方便后续eventmesh-admin调用时使用?



##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/FilterEngine.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.runtime.boot;
+
+import org.apache.eventmesh.api.meta.MetaServiceListener;
+import org.apache.eventmesh.common.utils.JsonUtils;
+import org.apache.eventmesh.common.utils.LogUtils;
+import org.apache.eventmesh.filter.pattern.Pattern;
+import org.apache.eventmesh.filter.patternbuild.PatternBuilder;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerGroupManager;
+import 
org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerManager;
+import org.apache.eventmesh.runtime.core.protocol.producer.EventMeshProducer;
+import org.apache.eventmesh.runtime.core.protocol.producer.ProducerManager;
+import org.apache.eventmesh.runtime.meta.MetaStorage;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class FilterEngine {
+
+    /**
+     * key:group-topic
+     **/
+    private final Map<String, Pattern> filterPatternMap = new HashMap<>();
+
+    private final String filterPrefix = "filter-";
+
+    private final MetaStorage metaStorage;
+
+    private MetaServiceListener metaServiceListener;
+
+    private final ProducerManager producerManager;
+
+    private final ConsumerManager consumerManager;
+
+    private final ScheduledExecutorService scheduledExecutorService = 
Executors.newSingleThreadScheduledExecutor();
+
+    public FilterEngine(MetaStorage metaStorage, ProducerManager 
producerManager, ConsumerManager consumerManager) {
+        this.metaStorage = metaStorage;
+        this.producerManager = producerManager;
+        this.consumerManager = consumerManager;
+    }
+
+    public void start() {
+        Map<String, String> filterMetaData = 
metaStorage.getMetaData(filterPrefix, true);
+        for (Entry<String, String> filterDataEntry : 
filterMetaData.entrySet()) {
+            // filter-group
+            String key = filterDataEntry.getKey();
+            // topic-filterRule list
+            String value = filterDataEntry.getValue();
+            updateFilterPatternMap(key, value);
+        }
+        metaServiceListener = this::updateFilterPatternMap;
+
+        // addListeners for producerManager & consumerManager
+        scheduledExecutorService.scheduleAtFixedRate(() -> {

Review Comment:
   Could you add another task to pull the newly added rules from the remote 
configuration center?
   
   这里面是不是还可以再增加一个任务,去拉取远程配置中心新增的规则配置?



##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/http/processor/SendAsyncEventProcessor.java:
##########
@@ -237,41 +241,53 @@ public void handler(final HandlerService.HandlerSpecific 
handlerSpecific, final
         eventMeshHTTPServer.getMetrics().getSummaryMetrics().recordSendMsg();
 
         final long startTime = System.currentTimeMillis();
-
+        boolean isFiltered = true;
         try {
             event = CloudEventBuilder.from(sendMessageContext.getEvent())
                 .withExtension(EventMeshConstants.REQ_EVENTMESH2MQ_TIMESTAMP, 
String.valueOf(System.currentTimeMillis()))
                 .build();
             
handlerSpecific.getTraceOperation().createClientTraceOperation(EventMeshUtil.getCloudEventExtensionMap(SpecVersion.V1.toString(),
 event),
                 EventMeshTraceConstants.TRACE_UPSTREAM_EVENTMESH_CLIENT_SPAN, 
false);
+            if (filterPattern != null) {
+                isFiltered = 
filterPattern.filter(JsonUtils.toJSONString(event));
+            }
 
-            eventMeshProducer.send(sendMessageContext, new SendCallback() {
-
-                @Override
-                public void onSuccess(final SendResult sendResult) {
-                    responseBodyMap.put(EventMeshConstants.RET_CODE, 
EventMeshRetCode.SUCCESS.getRetCode());
-                    responseBodyMap.put(EventMeshConstants.RET_MSG, 
EventMeshRetCode.SUCCESS.getErrMsg() + sendResult);
-
-                    LogUtils.info(log, 
"message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}",
-                        System.currentTimeMillis() - startTime, topic, bizNo, 
uniqueId);
-                    
handlerSpecific.getTraceOperation().endLatestTrace(sendMessageContext.getEvent());
-                    handlerSpecific.sendResponse(responseHeaderMap, 
responseBodyMap);
-                }
+            if (isFiltered) {
+                eventMeshProducer.send(sendMessageContext, new SendCallback() {

Review Comment:
   I don't quite understand this point. Is it necessary for producers to set 
filtering rules when sending messages?
   
   这一点没太明白,生产者发送消息有必要设置过滤规则?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to