Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[remove datanode] Load balancing for removing datanode #15022

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.TRegionReplicaSet;

import java.util.List;
import java.util.Map;

public interface IDestNodeSelector {

/**
* Select the optimal DataNode to place the new replica on along with the remaining replica set.
*
* @param availableDataNodeMap DataNodes that can be used for allocation
* @param freeDiskSpaceMap The free disk space of the DataNodes
* @param allocatedRegionGroups Allocated RegionGroups
* @param databaseAllocatedRegionGroups Allocated RegionGroups within the same Database with the
* replica set
* @param replicationFactor Replication factor of the replica set
* @param consensusGroupId TConsensusGroupId of the replica set
* @param remainReplicaSet the remaining replica set excluding the removed DataNodes
* @return The optimal DataNode to place the new replica on along with the remaining replica set
*/
TDataNodeConfiguration selectDestDataNode(
Map<Integer, TDataNodeConfiguration> availableDataNodeMap,
Map<Integer, Double> freeDiskSpaceMap,
List<TRegionReplicaSet> allocatedRegionGroups,
List<TRegionReplicaSet> databaseAllocatedRegionGroups,
int replicationFactor,
TConsensusGroupId consensusGroupId,
TRegionReplicaSet remainReplicaSet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* 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) {
if (pos >= replicationFactor) {
break;
}
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we consider the number of regions first instead of divergence?

Copy link
Collaborator Author

@HxpSerein HxpSerein Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such is the case. Only dataNodeEntries that meet the criteria of dataNodeEntry.freeDiskSpace == minFreeDiskSpace will be considered.

if (currentValue.left < bestValue.left
|| (currentValue.left.equals(bestValue.left) && currentValue.right < bestValue.right)) {
bestValue = currentValue;
targetDataNode = dataNodeEntry.dataNodeId;
}
}
}

private Pair<Integer, Integer> valuation(int[] nodes) {
int edgeSum = 0;
int regionSum = 0;
for (int iota : nodes) {
for (int kappa : nodes) {
edgeSum += combinationCounter[iota][kappa];
}
regionSum += regionCounter[iota];
}
return new Pair<>(edgeSum, regionSum);
}
}
Loading
Loading