CRZbulabula commented on code in PR #12165: URL: https://github.com/apache/iotdb/pull/12165#discussion_r1527743866
########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/region/AddRegionPeerProcedure.java: ########## @@ -0,0 +1,203 @@ +/* + * 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.procedure.impl.region; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TRegionMigrateResult; +import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.commons.exception.runtime.ThriftSerDeException; +import org.apache.iotdb.commons.utils.ThriftCommonsSerDeUtils; +import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv; +import org.apache.iotdb.confignode.procedure.env.RegionMaintainHandler; +import org.apache.iotdb.confignode.procedure.exception.ProcedureException; +import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedException; +import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException; +import org.apache.iotdb.confignode.procedure.impl.StateMachineProcedure; +import org.apache.iotdb.confignode.procedure.state.AddRegionPeerState; +import org.apache.iotdb.confignode.procedure.store.ProcedureType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +import static org.apache.iotdb.confignode.procedure.state.AddRegionPeerState.UPDATE_REGION_LOCATION_CACHE; +import static org.apache.iotdb.rpc.TSStatusCode.SUCCESS_STATUS; + +public class AddRegionPeerProcedure + extends StateMachineProcedure<ConfigNodeProcedureEnv, AddRegionPeerState> { + private static final Logger LOGGER = LoggerFactory.getLogger(AddRegionPeerProcedure.class); + private TConsensusGroupId consensusGroupId; + + private TDataNodeLocation coordinator; + + private TDataNodeLocation destDataNode; + + public AddRegionPeerProcedure() { + super(); + } + + public AddRegionPeerProcedure( + TConsensusGroupId consensusGroupId, + TDataNodeLocation coordinator, + TDataNodeLocation destDataNode) { + super(); + this.consensusGroupId = consensusGroupId; + this.coordinator = coordinator; + this.destDataNode = destDataNode; + } + + @Override + protected Flow executeFromState(ConfigNodeProcedureEnv env, AddRegionPeerState state) + throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException { + if (consensusGroupId == null) { + return Flow.NO_MORE_STATE; + } + RegionMaintainHandler handler = env.getRegionMaintainHandler(); + try { + outerSwitch: + switch (state) { + case CREATE_NEW_REGION_PEER: + handler.createNewRegionPeer(consensusGroupId, destDataNode); + setNextState(AddRegionPeerState.DO_ADD_REGION_PEER); + break; + case DO_ADD_REGION_PEER: + TSStatus tsStatus = + handler.addRegionPeer(this.getProcId(), destDataNode, consensusGroupId, coordinator); + TRegionMigrateResult result; + if (tsStatus.getCode() == SUCCESS_STATUS.getStatusCode()) { + result = handler.waitTaskFinish(this.getProcId(), coordinator); + } else { + throw new ProcedureException("ADD_REGION_PEER executed failed in DataNode"); + } + switch (result.getTaskStatus()) { + case TASK_NOT_EXIST: + // coordinator crashed and lost its task table + case FAIL: + // maybe some DataNode crash + LOGGER.warn( + "result is {}, will use resetPeerList to clean in the future", + result.getTaskStatus()); + // List<TDataNodeLocation> correctDataNodeLocations = + // + // env.getConfigManager().getPartitionManager().getAllReplicaSets().stream() + // .filter( + // tRegionReplicaSet -> + // + // tRegionReplicaSet.getRegionId().equals(consensusGroupId)) + // .findAny() + // .get() + // .getDataNodeLocations(); + // handler.resetPeerList(consensusGroupId, correctDataNodeLocations); Review Comment: Why left those comment lines? ########## integration-test/src/main/java/org/apache/iotdb/it/env/cluster/env/AbstractEnv.java: ########## @@ -1007,4 +1009,22 @@ public String getToolsPath() { public String getLibPath() { return TEMPLATE_NODE_LIB_PATH; } + + @Override + public Optional<DataNodeWrapper> dataNodeIdToWrapper(int nodeId) { + try (SyncConfigNodeIServiceClient leaderClient = + (SyncConfigNodeIServiceClient) getLeaderConfigNodeConnection()) { + TShowDataNodesResp resp = leaderClient.showDataNodes(); Review Comment: Why get DataNode through cluster rpc? The DataNodeWrapper should exist in AbstractEnv already ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java: ########## @@ -535,125 +537,136 @@ public boolean removeDataNode(RemoveDataNodePlan removeDataNodePlan) { return true; } - public TSStatus migrateRegion(TMigrateRegionReq migrateRegionReq) { - TConsensusGroupId regionGroupId; + // region region migration + + private TConsensusGroupId idToTConsensusGroupId(final int regionId) throws ProcedureException { Review Comment: Packaging a `TConsensusGroupId generateConsensusGroupIdIfExist(int regionId)` interface in PartitionManager would be more convenient for others to use. ########## iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/DataNodeKillPoints.java: ########## @@ -0,0 +1,27 @@ +/* + * 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.commons.utils; + +public enum DataNodeKillPoints { Review Comment: Why not move this class to integration-test if it will only be used in that package? ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/service/StatisticsService.java: ########## @@ -279,17 +279,21 @@ private void recordRegionPriorityMap( regionPriorityEntry : priorityMap.entrySet()) { if (!Objects.equals( regionPriorityEntry.getValue().getRight(), regionPriorityEntry.getValue().getLeft())) { - LOGGER.info( - "[RegionPriority]\t {}: {}->{}", - regionPriorityEntry.getKey(), - regionPriorityEntry.getValue().getLeft() == null - ? "null" - : regionPriorityEntry.getValue().getLeft().getDataNodeLocations().stream() - .map(TDataNodeLocation::getDataNodeId) - .collect(Collectors.toList()), - regionPriorityEntry.getValue().getRight().getDataNodeLocations().stream() - .map(TDataNodeLocation::getDataNodeId) - .collect(Collectors.toList())); + try { + LOGGER.info( + "[RegionPriority]\t {}: {}->{}", + regionPriorityEntry.getKey(), + regionPriorityEntry.getValue().getLeft() == null + ? "null" + : regionPriorityEntry.getValue().getLeft().getDataNodeLocations().stream() + .map(TDataNodeLocation::getDataNodeId) + .collect(Collectors.toList()), + regionPriorityEntry.getValue().getRight().getDataNodeLocations().stream() + .map(TDataNodeLocation::getDataNodeId) + .collect(Collectors.toList())); + } catch (Exception e) { Review Comment: Why surrounding with try-catch? -- 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