SzyWilliam commented on code in PR #15014: URL: https://github.com/apache/iotdb/pull/15014#discussion_r2004666806
########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/service/TopologyService.java: ########## @@ -0,0 +1,275 @@ +/* + * 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.iotdb.confignode.manager.load.service; + +import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TNodeLocations; +import org.apache.iotdb.common.rpc.thrift.TTestConnectionResp; +import org.apache.iotdb.common.rpc.thrift.TTestConnectionResult; +import org.apache.iotdb.commons.cluster.NodeStatus; +import org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory; +import org.apache.iotdb.commons.concurrent.ThreadName; +import org.apache.iotdb.confignode.client.async.CnToDnAsyncRequestType; +import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager; +import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext; +import org.apache.iotdb.confignode.conf.ConfigNodeConfig; +import org.apache.iotdb.confignode.conf.ConfigNodeDescriptor; +import org.apache.iotdb.confignode.manager.IManager; +import org.apache.iotdb.confignode.manager.load.cache.AbstractHeartbeatSample; +import org.apache.iotdb.confignode.manager.load.cache.IFailureDetector; +import org.apache.iotdb.confignode.manager.load.cache.detector.FixedDetector; +import org.apache.iotdb.confignode.manager.load.cache.detector.PhiAccrualDetector; +import org.apache.iotdb.confignode.manager.load.cache.node.NodeHeartbeatSample; +import org.apache.iotdb.confignode.manager.load.cache.node.NodeStatistics; +import org.apache.iotdb.confignode.manager.load.subscriber.IClusterStatusSubscriber; +import org.apache.iotdb.confignode.manager.load.subscriber.NodeStatisticsChangeEvent; + +import org.apache.ratis.util.AwaitForSignal; +import org.apache.tsfile.utils.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +public class TopologyService implements Runnable, IClusterStatusSubscriber { + private static final Logger LOGGER = LoggerFactory.getLogger(TopologyService.class); + private static final long PROBING_INTERVAL_MS = 5_000L; + private static final long PROBING_TIMEOUT_MS = PROBING_INTERVAL_MS; + private static final int SAMPLING_WINDOW_SIZE = 100; + + private final ExecutorService topologyThread = + IoTDBThreadPoolFactory.newSingleThreadExecutor( + ThreadName.CONFIG_NODE_TOPOLOGY_SERVICE.getName()); + private final Consumer<Map<Integer, Set<Integer>>> topologyChangeListener; + + private final AwaitForSignal awaitForSignal; + private final IManager configManager; + + private final AtomicBoolean shouldRun; + + /* (fromDataNodeId, toDataNodeId) -> heartbeat history */ + private final Map<Pair<Integer, Integer>, List<AbstractHeartbeatSample>> heartbeats; + private final List<Integer> startingDataNodes = new CopyOnWriteArrayList<>(); + + private final IFailureDetector failureDetector; + private static final ConfigNodeConfig CONF = ConfigNodeDescriptor.getInstance().getConf(); + + public TopologyService( + IManager configManager, Consumer<Map<Integer, Set<Integer>>> topologyChangeListener) { + this.configManager = configManager; + this.topologyChangeListener = topologyChangeListener; + this.heartbeats = new HashMap<>(); + this.shouldRun = new AtomicBoolean(false); + this.awaitForSignal = new AwaitForSignal(this.getClass().getSimpleName()); + + // here we use the same failure + switch (CONF.getFailureDetector()) { + case IFailureDetector.PHI_ACCRUAL_DETECTOR: + this.failureDetector = + new PhiAccrualDetector( + CONF.getFailureDetectorPhiThreshold(), + CONF.getFailureDetectorPhiAcceptablePauseInMs() * 1000_000L, + CONF.getHeartbeatIntervalInMs() * 200_000L, + IFailureDetector.PHI_COLD_START_THRESHOLD, + new FixedDetector(CONF.getFailureDetectorFixedThresholdInMs() * 1000_000L)); + break; + case IFailureDetector.FIXED_DETECTOR: + default: + this.failureDetector = + new FixedDetector(CONF.getFailureDetectorFixedThresholdInMs() * 1000_000L); + } + } + + public synchronized void startTopologyService() { + shouldRun.set(true); + topologyThread.submit(this); Review Comment: Let's keep this thread running by the boolean flag. -- 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]
