ruanwenjun commented on code in PR #10406: URL: https://github.com/apache/dolphinscheduler/pull/10406#discussion_r894984811
########## dolphinscheduler-registry/dolphinscheduler-registry-plugins/dolphinscheduler-registry-mysql/src/main/java/org/apache/dolphinscheduler/plugin/registry/mysql/task/SubscribeDataManager.java: ########## @@ -0,0 +1,163 @@ +/* + * 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.dolphinscheduler.plugin.registry.mysql.task; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.apache.dolphinscheduler.plugin.registry.mysql.MysqlOperator; +import org.apache.dolphinscheduler.plugin.registry.mysql.MysqlRegistryConstant; +import org.apache.dolphinscheduler.plugin.registry.mysql.model.MysqlRegistryData; +import org.apache.dolphinscheduler.registry.api.Event; +import org.apache.dolphinscheduler.registry.api.SubscribeListener; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; + +/** + * Used to refresh if the subscribe path has been changed. + */ +public class SubscribeDataManager implements AutoCloseable { + + private static final Logger LOGGER = LoggerFactory.getLogger(SubscribeDataManager.class); + + private final MysqlOperator mysqlOperator; + private final Map<String, List<SubscribeListener>> dataSubScribeMap = new ConcurrentHashMap<>(); + private final ScheduledExecutorService dataSubscribeCheckThreadPool; + private final Map<String, MysqlRegistryData> mysqlRegistryDataMap = new ConcurrentHashMap<>(); + + public SubscribeDataManager(MysqlOperator mysqlOperator) { + this.mysqlOperator = mysqlOperator; + this.dataSubscribeCheckThreadPool = Executors.newScheduledThreadPool( + 1, + new ThreadFactoryBuilder().setNameFormat("MysqlRegistrySubscribeDataCheckThread").setDaemon(true).build()); + } + + public void start() { + dataSubscribeCheckThreadPool.scheduleWithFixedDelay( + new RegistrySubscribeDataCheckTask(mysqlRegistryDataMap, dataSubScribeMap, mysqlOperator), + MysqlRegistryConstant.TERM_REFRESH_INTERVAL, + MysqlRegistryConstant.TERM_REFRESH_INTERVAL, + TimeUnit.MILLISECONDS); + } + + public void addListener(String path, SubscribeListener subscribeListener) { + dataSubScribeMap.computeIfAbsent(path, k -> new ArrayList<>()).add(subscribeListener); + } + + public void removeListener(String path) { + dataSubScribeMap.remove(path); + } + + public String getData(String path) throws SQLException { + MysqlRegistryData mysqlRegistryData = mysqlRegistryDataMap.get(path); + if (mysqlRegistryData == null) { + return null; + } + return mysqlRegistryData.getData(); + } + + @Override + public void close() { + dataSubscribeCheckThreadPool.shutdownNow(); + dataSubScribeMap.clear(); + } + + static class RegistrySubscribeDataCheckTask implements Runnable { + + private final Map<String, List<SubscribeListener>> dataSubScribeMap; + private final MysqlOperator mysqlOperator; + private final Map<String, MysqlRegistryData> mysqlRegistryDataMap; + + public RegistrySubscribeDataCheckTask( + Map<String, MysqlRegistryData> mysqlRegistryDataMap, + Map<String, List<SubscribeListener>> dataSubScribeMap, + MysqlOperator mysqlOperator) { + this.mysqlRegistryDataMap = checkNotNull(mysqlRegistryDataMap); + this.dataSubScribeMap = checkNotNull(dataSubScribeMap); + this.mysqlOperator = checkNotNull(mysqlOperator); + } Review Comment: Done. -- 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]
