pandaapo commented on code in PR #4578: URL: https://github.com/apache/eventmesh/pull/4578#discussion_r1405365200
########## 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: This way, dynamic changes with existing rules can be perceived during runtime. If a new group is added along with its corresponding rules, it requires a restart by executing FilterEngine#start(). Similarly, if an existing group initially lacks configured rules, and rules for that group are added during runtime, a restart is also necessary. 这样运行中能感知既有规则的动态变更。如果新增了group并相应地新增其规则就需要重启执行FilterEngine#start()了。或者某既有group先没有配置规则,然后运行中增加该group的规则,也需要重启才行。 ########## 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: This way, dynamic changes with existing rules can be perceived during runtime. If a new group is added along with its corresponding rules, it requires a restart by executing FilterEngine#start(). Similarly, if an existing group initially lacks configured rules, and rules for that group are added during runtime, a restart is also necessary. 这样运行中能感知既有规则的动态变更。如果新增了group并相应地新增其规则就需要重启执行FilterEngine#start()了。或者某既有group先没有配置规则,然后运行中增加该group的规则,也需要重启才行。 -- 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]
