HxpSerein commented on code in PR #15022: URL: https://github.com/apache/iotdb/pull/15022#discussion_r1984971580
########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/balancer/region/PartiteGraphPlacementDestNodeSelector.java: ########## @@ -0,0 +1,166 @@ +/* + * 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.balancer.region; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet; +import org.apache.iotdb.confignode.manager.load.balancer.region.GreedyRegionGroupAllocator.DataNodeEntry; + +import org.apache.tsfile.utils.Pair; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; + +public class PartiteGraphPlacementDestNodeSelector implements IDestNodeSelector { + private int replicationFactor; + + private int dataNodeNum; + // The number of allocated Regions in each DataNode + private int[] regionCounter; + // The number of edges in current cluster + private int[][] combinationCounter; + private Map<Integer, Integer> fakeToRealIdMap; + private Map<Integer, Integer> realToFakeIdMap; + + private Set<Integer> remainDataNodesFakeId; + + // Pair<combinationSum, RegionSum> + Pair<Integer, Integer> bestValue; + + private Integer targetDataNode = -1; + + @Override + public TDataNodeConfiguration selectDestDataNode( + Map<Integer, TDataNodeConfiguration> availableDataNodeMap, + Map<Integer, Double> freeDiskSpaceMap, + List<TRegionReplicaSet> allocatedRegionGroups, + List<TRegionReplicaSet> databaseAllocatedRegionGroups, + int replicationFactor, + TConsensusGroupId consensusGroupId, + TRegionReplicaSet remainReplicaSet) { + prepare(replicationFactor, availableDataNodeMap, allocatedRegionGroups, remainReplicaSet); + + searchTargetDataNode(freeDiskSpaceMap); + + return availableDataNodeMap.get(fakeToRealIdMap.get(targetDataNode)); + } + + private void prepare( + int replicationFactor, + Map<Integer, TDataNodeConfiguration> availableDataNodeMap, + List<TRegionReplicaSet> allocatedRegionGroups, + TRegionReplicaSet remainReplicaSet) { + this.replicationFactor = replicationFactor; + this.fakeToRealIdMap = new TreeMap<>(); + this.realToFakeIdMap = new TreeMap<>(); + this.dataNodeNum = availableDataNodeMap.size(); + List<Integer> dataNodeIdList = + availableDataNodeMap.values().stream() + .map(c -> c.getLocation().getDataNodeId()) + .collect(Collectors.toList()); + for (int i = 0; i < dataNodeNum; i++) { + fakeToRealIdMap.put(i, dataNodeIdList.get(i)); + realToFakeIdMap.put(dataNodeIdList.get(i), i); + } + + remainDataNodesFakeId = + remainReplicaSet.dataNodeLocations.stream() + .map(TDataNodeLocation::getDataNodeId) + .map(realToFakeIdMap::get) + .collect(Collectors.toSet()); + + // Compute regionCounter and combinationCounter + this.regionCounter = new int[dataNodeNum]; + Arrays.fill(regionCounter, 0); + this.combinationCounter = new int[dataNodeNum][dataNodeNum]; + for (int i = 0; i < dataNodeNum; i++) { + Arrays.fill(combinationCounter[i], 0); + } + for (TRegionReplicaSet regionReplicaSet : allocatedRegionGroups) { + List<TDataNodeLocation> dataNodeLocations = regionReplicaSet.getDataNodeLocations(); + for (int i = 0; i < dataNodeLocations.size(); i++) { + int fakeIId = realToFakeIdMap.get(dataNodeLocations.get(i).getDataNodeId()); + regionCounter[fakeIId]++; + for (int j = i + 1; j < dataNodeLocations.size(); j++) { + int fakeJId = realToFakeIdMap.get(dataNodeLocations.get(j).getDataNodeId()); + combinationCounter[fakeIId][fakeJId] = 1; + combinationCounter[fakeJId][fakeIId] = 1; + } + } + } + + this.bestValue = new Pair<>(Integer.MAX_VALUE, Integer.MAX_VALUE); + } + + private void searchTargetDataNode(Map<Integer, Double> freeDiskSpaceMap) { + List<DataNodeEntry> entryList = new ArrayList<>(); + + for (int i = 0; i < dataNodeNum; i++) { + if (remainDataNodesFakeId.contains(i)) { + continue; + } + entryList.add( + new DataNodeEntry(i, regionCounter[i], freeDiskSpaceMap.get(fakeToRealIdMap.get(i)))); + } + Collections.sort(entryList); + + int[] alternativeReplica = new int[replicationFactor]; + + int pos = 0; + for (Integer integer : remainDataNodesFakeId) { + alternativeReplica[pos] = integer; + pos++; + } + + double minFreeDiskSpace = entryList.get(0).freeDiskSpace; + for (DataNodeEntry dataNodeEntry : entryList) { + if (dataNodeEntry.freeDiskSpace > minFreeDiskSpace) { + break; + } + alternativeReplica[replicationFactor - 1] = dataNodeEntry.dataNodeId; + Pair<Integer, Integer> currentValue = valuation(alternativeReplica); Review Comment: Such is the case. Only dataNodeEntries that meet the criteria of `dataNodeEntry.freeDiskSpace == minFreeDiskSpace` will be considered. ########## iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/manager/load/balancer/region/DestNodeSelectorTest.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.balancer.region; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType; +import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class DestNodeSelectorTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(DestNodeSelectorTest.class); + + private static final IDestNodeSelector SELECTOR = new PartiteGraphPlacementDestNodeSelector(); + private static final IRegionGroupAllocator ALLOCATOR = new GreedyCopySetRegionGroupAllocator(); + + private static final TDataNodeLocation REMOVE_DATANODE_LOCATION = + new TDataNodeLocation().setDataNodeId(5); + private static final int TEST_DATA_NODE_NUM = 5; + private static final int DATA_REGION_PER_DATA_NODE = 4; + private static final int DATA_REPLICATION_FACTOR = 2; + + private static final Map<Integer, TDataNodeConfiguration> AVAILABLE_DATA_NODE_MAP = + new HashMap<>(); + private static final Map<Integer, Double> FREE_SPACE_MAP = new HashMap<>(); + + @BeforeClass + public static void setUp() { + // Construct TEST_DATA_NODE_NUM DataNodes + for (int i = 1; i <= TEST_DATA_NODE_NUM; i++) { + AVAILABLE_DATA_NODE_MAP.put( + i, new TDataNodeConfiguration().setLocation(new TDataNodeLocation().setDataNodeId(i))); + FREE_SPACE_MAP.put(i, Math.random()); + } + } + + @Test + public void testSelectDestNode() { + final int dataRegionGroupNum = + DATA_REGION_PER_DATA_NODE * TEST_DATA_NODE_NUM / DATA_REPLICATION_FACTOR; + + List<TRegionReplicaSet> allocateResult = new ArrayList<>(); + for (int index = 0; index < dataRegionGroupNum; index++) { + allocateResult.add( + ALLOCATOR.generateOptimalRegionReplicasDistribution( + AVAILABLE_DATA_NODE_MAP, + FREE_SPACE_MAP, + allocateResult, + allocateResult, + DATA_REPLICATION_FACTOR, + new TConsensusGroupId(TConsensusGroupType.DataRegion, index))); + } + List<TRegionReplicaSet> migratedReplicas = + allocateResult.stream() + .filter( + replicaSet -> replicaSet.getDataNodeLocations().contains(REMOVE_DATANODE_LOCATION)) + .collect(Collectors.toList()); + + AVAILABLE_DATA_NODE_MAP.remove(REMOVE_DATANODE_LOCATION.getDataNodeId()); + FREE_SPACE_MAP.remove(REMOVE_DATANODE_LOCATION.getDataNodeId()); + + List<TRegionReplicaSet> remainReplicas = new ArrayList<>(); + + for (TRegionReplicaSet replicaSet : migratedReplicas) { + List<TDataNodeLocation> dataNodeLocations = replicaSet.getDataNodeLocations(); + allocateResult.remove(replicaSet); + dataNodeLocations.remove(REMOVE_DATANODE_LOCATION); + replicaSet.setDataNodeLocations(dataNodeLocations); + allocateResult.add(replicaSet); + remainReplicas.add(replicaSet); + } + + Set<Integer> selectedNodeIds = new HashSet<>(); + + for (TRegionReplicaSet remainReplicaSet : remainReplicas) { + TDataNodeConfiguration selectedNode = + SELECTOR.selectDestDataNode( + AVAILABLE_DATA_NODE_MAP, + FREE_SPACE_MAP, + allocateResult, + allocateResult, + DATA_REPLICATION_FACTOR, + remainReplicaSet.regionId, + remainReplicaSet); + LOGGER.info( + "Selected DataNode {} for Region {}", + selectedNode.getLocation().getDataNodeId(), + remainReplicaSet.regionId); + allocateResult.remove(remainReplicaSet); + List<TDataNodeLocation> dataNodeLocations = remainReplicaSet.getDataNodeLocations(); + dataNodeLocations.add(selectedNode.getLocation()); + remainReplicaSet.setDataNodeLocations(dataNodeLocations); Review Comment: removed -- 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]
