Pengzna commented on code in PR #12355: URL: https://github.com/apache/iotdb/pull/12355#discussion_r1617101732
########## iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/pipe/PipeConsensus.java: ########## @@ -0,0 +1,485 @@ +/* + * 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.consensus.pipe; + +import org.apache.iotdb.common.rpc.thrift.TEndPoint; +import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.commons.client.IClientManager; +import org.apache.iotdb.commons.consensus.ConsensusGroupId; +import org.apache.iotdb.commons.exception.StartupException; +import org.apache.iotdb.commons.pipe.task.meta.PipeStatus; +import org.apache.iotdb.commons.service.RegisterManager; +import org.apache.iotdb.commons.utils.FileUtils; +import org.apache.iotdb.commons.utils.StatusUtils; +import org.apache.iotdb.commons.utils.TestOnly; +import org.apache.iotdb.consensus.IConsensus; +import org.apache.iotdb.consensus.IStateMachine; +import org.apache.iotdb.consensus.common.DataSet; +import org.apache.iotdb.consensus.common.Peer; +import org.apache.iotdb.consensus.common.request.IConsensusRequest; +import org.apache.iotdb.consensus.config.ConsensusConfig; +import org.apache.iotdb.consensus.config.PipeConsensusConfig; +import org.apache.iotdb.consensus.exception.ConsensusException; +import org.apache.iotdb.consensus.exception.ConsensusGroupAlreadyExistException; +import org.apache.iotdb.consensus.exception.ConsensusGroupModifyPeerException; +import org.apache.iotdb.consensus.exception.ConsensusGroupNotExistException; +import org.apache.iotdb.consensus.exception.IllegalPeerEndpointException; +import org.apache.iotdb.consensus.exception.IllegalPeerNumException; +import org.apache.iotdb.consensus.exception.PeerAlreadyInConsensusGroupException; +import org.apache.iotdb.consensus.exception.PeerNotInConsensusGroupException; +import org.apache.iotdb.consensus.pipe.client.AsyncPipeConsensusServiceClient; +import org.apache.iotdb.consensus.pipe.client.PipeConsensusClientPool; +import org.apache.iotdb.consensus.pipe.client.SyncPipeConsensusServiceClient; +import org.apache.iotdb.consensus.pipe.consensuspipe.ConsensusPipeGuardian; +import org.apache.iotdb.consensus.pipe.consensuspipe.ConsensusPipeManager; +import org.apache.iotdb.consensus.pipe.consensuspipe.ConsensusPipeName; +import org.apache.iotdb.consensus.pipe.service.PipeConsensusRPCService; +import org.apache.iotdb.consensus.pipe.service.PipeConsensusRPCServiceProcessor; +import org.apache.iotdb.rpc.RpcUtils; +import org.apache.iotdb.rpc.TSStatusCode; + +import com.google.common.collect.ImmutableMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Collectors; + +import static org.apache.iotdb.consensus.iot.IoTConsensus.getConsensusGroupIdsFromDir; + +// TODO: support syncLag +public class PipeConsensus implements IConsensus { + private static final String CONSENSUS_PIPE_GUARDIAN_TASK_ID = "consensus_pipe_guardian"; + private static final String CLASS_NAME = PipeConsensus.class.getSimpleName(); + private static final Logger LOGGER = LoggerFactory.getLogger(PipeConsensus.class); + + private final TEndPoint thisNode; + private final int thisNodeId; + private final File storageDir; + private final IStateMachine.Registry registry; + private final Map<ConsensusGroupId, PipeConsensusServerImpl> stateMachineMap = + new ConcurrentHashMap<>(); + private final PipeConsensusRPCService rpcService; + private final RegisterManager registerManager = new RegisterManager(); + private final ReentrantLock stateMachineMapLock = new ReentrantLock(); + private final PipeConsensusConfig config; + private final ConsensusPipeManager consensusPipeManager; + private final ConsensusPipeGuardian consensusPipeGuardian; + private final IClientManager<TEndPoint, AsyncPipeConsensusServiceClient> asyncClientManager; + private final IClientManager<TEndPoint, SyncPipeConsensusServiceClient> syncClientManager; + + public PipeConsensus(ConsensusConfig config, IStateMachine.Registry registry) { + this.thisNode = config.getThisNodeEndPoint(); + this.thisNodeId = config.getThisNodeId(); + this.storageDir = new File(config.getStorageDir()); + this.config = config.getPipeConsensusConfig(); + this.registry = registry; + this.rpcService = new PipeConsensusRPCService(thisNode, config.getPipeConsensusConfig()); + this.consensusPipeManager = + new ConsensusPipeManager( + config.getPipeConsensusConfig().getPipe(), + config.getPipeConsensusConfig().getReplicateMode()); + this.consensusPipeGuardian = + config.getPipeConsensusConfig().getPipe().getConsensusPipeGuardian(); + this.asyncClientManager = + new IClientManager.Factory<TEndPoint, AsyncPipeConsensusServiceClient>() + .createClientManager( + new PipeConsensusClientPool.AsyncPipeConsensusServiceClientPoolFactory( + config.getPipeConsensusConfig())); + this.syncClientManager = + new IClientManager.Factory<TEndPoint, SyncPipeConsensusServiceClient>() + .createClientManager( + new PipeConsensusClientPool.SyncPipeConsensusServiceClientPoolFactory( + config.getPipeConsensusConfig())); + } + + @Override + public synchronized void start() throws IOException { + initAndRecover(); + + rpcService.initAsyncedServiceImpl(new PipeConsensusRPCServiceProcessor(this, config.getPipe())); + try { + registerManager.register(rpcService); + } catch (StartupException e) { + throw new IOException(e); + } + + consensusPipeGuardian.start( + CONSENSUS_PIPE_GUARDIAN_TASK_ID, + this::checkAllConsensusPipe, + config.getPipe().getConsensusPipeGuardJobIntervalInSeconds()); + } + + private void initAndRecover() throws IOException { + if (!storageDir.exists()) { + if (!storageDir.mkdirs()) { + LOGGER.warn("Unable to create consensus dir at {}", storageDir); + throw new IOException(String.format("Unable to create consensus dir at %s", storageDir)); + } + } else { + try (DirectoryStream<Path> stream = Files.newDirectoryStream(storageDir.toPath())) { + for (Path path : stream) { + ConsensusGroupId consensusGroupId = parsePeerDir(path.getFileName().toString()); + PipeConsensusServerImpl consensus = + new PipeConsensusServerImpl( + new Peer(consensusGroupId, thisNodeId, thisNode), + registry.apply(consensusGroupId), + path.toString(), + new ArrayList<>(), + config, + consensusPipeManager, + syncClientManager); + stateMachineMap.put(consensusGroupId, consensus); + consensus.start(true); + } + } + } + } + + @Override + public synchronized void stop() { + asyncClientManager.close(); + syncClientManager.close(); + registerManager.deregisterAll(); + consensusPipeGuardian.stop(); + stateMachineMap.values().parallelStream().forEach(PipeConsensusServerImpl::stop); + } + + public void checkAllConsensusPipe() { Review Comment: makes sense. fixed ########## iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java: ########## @@ -108,7 +108,7 @@ import java.util.stream.Collectors; /** A multi-raft consensus implementation based on Apache Ratis. */ -class RatisConsensus implements IConsensus { +public class RatisConsensus implements IConsensus { Review Comment: fixed -- 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]
