liyuheng55555 commented on code in PR #12206: URL: https://github.com/apache/iotdb/pull/12206#discussion_r1535174838
########## iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerImpl.java: ########## @@ -635,22 +626,56 @@ public void recoverConfiguration() { // interrupted // unexpectedly, we need substitute configuration with tmpConfiguration file if (Files.exists(tmpConfigurationPath)) { - if (Files.exists(configurationPath)) { - Files.delete(configurationPath); - } + Files.deleteIfExists(configurationPath); Files.move(tmpConfigurationPath, configurationPath); } - buffer = ByteBuffer.wrap(Files.readAllBytes(configurationPath)); - int size = buffer.getInt(); - for (int i = 0; i < size; i++) { - configuration.add(Peer.deserialize(buffer)); + if (Files.exists(configurationPath)) { + // recover from old configuration file + buffer = ByteBuffer.wrap(Files.readAllBytes(configurationPath)); + int size = buffer.getInt(); + for (int i = 0; i < size; i++) { + configuration.add(Peer.deserialize(buffer)); + } + Files.delete(configurationPath); + } else { + // recover from split configuration file + Path dirPath = Paths.get(storageDir); + List<Peer> tmpPeerList = getConfiguration(dirPath, CONFIGURATION_TMP_FILE_NAME); + configuration.addAll(tmpPeerList); + List<Peer> peerList = getConfiguration(dirPath, CONFIGURATION_FILE_NAME); + for (Peer peer : peerList) { + if (!configuration.contains(peer)) { + configuration.add(peer); + } + } } logger.info("Recover IoTConsensus server Impl, configuration: {}", configuration); } catch (IOException e) { logger.error("Unexpected error occurs when recovering configuration", e); } } + private List<Peer> getConfiguration(Path dirPath, String configurationFileName) + throws IOException { + ByteBuffer buffer; + List<Peer> tmpConfiguration = new ArrayList<>(); + Path[] files = + Files.walk(dirPath) + .filter(Files::isRegularFile) + .filter(filePath -> filePath.getFileName().toString().contains(configurationFileName)) + .toArray(Path[]::new); + for (Path file : files) { + buffer = ByteBuffer.wrap(Files.readAllBytes(file)); + tmpConfiguration.add(Peer.deserialize(buffer)); + } + return tmpConfiguration; + } + + public void resetConfiguration(List<Peer> newConfiguration) { Review Comment: private ? or inline this function if it's only called once ? ########## iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java: ########## @@ -392,6 +402,30 @@ public List<ConsensusGroupId> getAllConsensusGroupIds() { return new ArrayList<>(stateMachineMap.keySet()); } + public void resetPeerList(ConsensusGroupId groupId, List<Peer> peers) throws ConsensusException { + IoTConsensusServerImpl impl = + Optional.ofNullable(stateMachineMap.get(groupId)) + .orElseThrow(() -> new ConsensusGroupNotExistException(groupId)); + if (impl.isReadOnly()) { + throw new ConsensusException("system is in read-only status now"); + } else if (!impl.isActive()) { + throw new ConsensusException( + "peer is inactive and not ready to receive reset configuration request."); + } else { Review Comment: maybe not necessary to use `else` and `else if` -- 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: reviews-unsubscr...@iotdb.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org