Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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,83 @@
/*
* 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.hadoop.hbase.master.balancer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.yetus.audience.InterfaceAudience;

import java.util.*;

/**
* Ensure Tables are mutually exclusive on an RS
* for example: META and SYSTEM.CATALOG can be mutually exclusive Tables
* on same RS
*/
@InterfaceAudience.Private public class MutuallyExclusiveTablesCostFunction extends CostFunction {
public static final String MUTUALLY_EXCLUSIVE_TABLES_KEY =
Copy link
Contributor

@Umeshkumar9414 Umeshkumar9414 Aug 27, 2024

Choose a reason for hiding this comment

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

NIT: good to add comments for config.

"hbase.master.balancer.stochastic.mutuallyExclusiveTables";
public static final String MUTUALLY_EXCLUSIVE_TABLES_COST_KEY =
"hbase.master.balancer.stochastic.mutuallyExclusiveTablesCost";
public static final float DEFAULT_MUTUALLY_EXCLUSIVE_TABLES_COST = 10000;
private static final int VIOLATION_COST = 1000;
private Set<String> mutuallyExclusiveTables = Collections.emptySet();

public MutuallyExclusiveTablesCostFunction(Configuration conf) {
this.setMultiplier(
conf.getFloat(MUTUALLY_EXCLUSIVE_TABLES_COST_KEY, DEFAULT_MUTUALLY_EXCLUSIVE_TABLES_COST));
initializeMutuallyExclusiveTables(conf);
}

private void initializeMutuallyExclusiveTables(Configuration conf) {
String[] tables = conf.getStrings(MUTUALLY_EXCLUSIVE_TABLES_KEY);
if (tables != null && tables.length > 0) {
this.mutuallyExclusiveTables = new HashSet<>(Arrays.asList(tables));
}
}

@Override void prepare(BalancerClusterState cluster) {
super.prepare(cluster);
}

@Override protected double cost() {
double totalCost = 0;

Map<ServerName, List<RegionInfo>> clusterState = this.cluster.clusterState;

for (Map.Entry<ServerName, List<RegionInfo>> entry : clusterState.entrySet()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let us add comment about what we are doing and how it would impact the balancer decision with an example.

List<RegionInfo> regions = entry.getValue();
Set<String> exclusiveTablesOnServer = new HashSet<>();
for (RegionInfo regionInfo : regions) {
String tableName = regionInfo.getTable().getNameAsString();
if (mutuallyExclusiveTables.contains(tableName)) {
// If its already seen table, continue
if (exclusiveTablesOnServer.contains(tableName)) {
continue;
}
exclusiveTablesOnServer.add(tableName);
if (exclusiveTablesOnServer.size() > 1) {
totalCost += VIOLATION_COST;
}
}
}
}

return totalCost;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.hadoop.hbase.master.balancer;

import static org.apache.hadoop.hbase.master.balancer.MutuallyExclusiveTablesCostFunction.DEFAULT_MUTUALLY_EXCLUSIVE_TABLES_COST;
import static org.apache.hadoop.hbase.master.balancer.MutuallyExclusiveTablesCostFunction.MUTUALLY_EXCLUSIVE_TABLES_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -45,6 +47,7 @@
import org.apache.hadoop.hbase.Size;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
import org.apache.hadoop.hbase.master.RegionPlan;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
Expand Down Expand Up @@ -401,6 +404,43 @@ public void testLocalityCost() throws Exception {
}
}

@Test
public void testMutuallyExclusiveTablesOnDifferentServers() throws Exception
{
conf.setStrings(MUTUALLY_EXCLUSIVE_TABLES_KEY, "table1", "table2");
loadBalancer.onConfigurationChange(conf);
Map<ServerName, List<RegionInfo>> clusterState = new HashMap<>();
ServerName host1 = ServerName.valueOf("host1", 1000, 1000);
ServerName host2 = ServerName.valueOf("host2", 1000, 1000);
RegionInfo region1 = RegionInfoBuilder.newBuilder(TableName.valueOf("table1")).build();
RegionInfo region2 = RegionInfoBuilder.newBuilder(TableName.valueOf("table2")).build();
clusterState.put(host1 , Arrays.asList(region1));
clusterState.put(host2 , Arrays.asList(region2));
CostFunction costFunction = new MutuallyExclusiveTablesCostFunction(conf);
costFunction.prepare(new BalancerClusterState(clusterState, null, null, null));
double cost = costFunction.cost();
assertEquals(0.0, cost, 0.001);
}

@Test
public void testMutuallyExclusiveTablesOnSameServer() throws Exception
{
conf.setStrings(MUTUALLY_EXCLUSIVE_TABLES_KEY, "table1", "table2");
loadBalancer.onConfigurationChange(conf);
Map<ServerName, List<RegionInfo>> clusterState = new HashMap<>();
ServerName host1 = ServerName.valueOf("host1", 1000, 1000);
RegionInfo region1 = RegionInfoBuilder.newBuilder(TableName.valueOf("table1")).build();
RegionInfo region2 = RegionInfoBuilder.newBuilder(TableName.valueOf("table2")).build();
List<RegionInfo> regions = new ArrayList<>();
regions.add(region1);
regions.add(region2);
clusterState.put(host1 , regions);
CostFunction costFunction = new MutuallyExclusiveTablesCostFunction(conf);
costFunction.prepare(new BalancerClusterState(clusterState, null, null, null));
double cost = costFunction.cost();
assertEquals(DEFAULT_MUTUALLY_EXCLUSIVE_TABLES_COST, cost, 0.001);
}

@Test
public void testMoveCostMultiplier() throws Exception {
Configuration conf = HBaseConfiguration.create();
Expand Down