HBASE-25939 Move more tests code for StochasticLoadBalancer to hbase-balancer module (#3331)

Signed-off-by: Yulin Niu <niuyulin@apache.org>
This commit is contained in:
Duo Zhang 2021-05-30 22:00:18 +08:00 committed by GitHub
parent f119a865cf
commit f2ff816532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 241 additions and 141 deletions

View File

@ -27,7 +27,10 @@ import org.apache.yetus.audience.InterfaceAudience;
* The class that creates a load balancer from a conf. * The class that creates a load balancer from a conf.
*/ */
@InterfaceAudience.Private @InterfaceAudience.Private
public class LoadBalancerFactory { public final class LoadBalancerFactory {
private LoadBalancerFactory() {
}
/** /**
* The default {@link LoadBalancer} class. * The default {@link LoadBalancer} class.

View File

@ -0,0 +1,67 @@
/*
* 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 java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class HeterogeneousCostRulesTestHelper {
private static final Logger LOG = LoggerFactory.getLogger(HeterogeneousCostRulesTestHelper.class);
static final String DEFAULT_RULES_FILE_NAME = "hbase-balancer.rules";
private HeterogeneousCostRulesTestHelper() {
}
/**
* Create rule file with the given rules.
* @param file Name of file to write rules into.
* @return Full file name of the rules file which is <code>dir</code> + DEFAULT_RULES_FILE_NAME.
*/
static String createRulesFile(String file, final List<String> rules) throws IOException {
cleanup(file);
Path path = Files.createFile(FileSystems.getDefault().getPath(file));
return Files.write(path, rules, StandardCharsets.UTF_8).toString();
}
/**
* Create rule file with empty rules.
* @param file Name of file to write rules into.
* @return Full file name of the rules file which is <code>dir</code> + DEFAULT_RULES_FILE_NAME.
*/
static String createRulesFile(String file) throws IOException {
return createRulesFile(file, Collections.emptyList());
}
static void cleanup(String file) throws IOException {
try {
Files.delete(FileSystems.getDefault().getPath(file));
} catch (NoSuchFileException nsfe) {
LOG.warn("FileNotFoundException for {}", file, nsfe);
}
}
}

View File

@ -87,7 +87,8 @@ public class LoadBalancerPerformanceEvaluation extends AbstractHBaseTool {
// Non-default configurations. // Non-default configurations.
private void setupConf() { private void setupConf() {
conf.setClass(HConstants.HBASE_MASTER_LOADBALANCER_CLASS, loadBalancerClazz, LoadBalancer.class); conf.setClass(HConstants.HBASE_MASTER_LOADBALANCER_CLASS, loadBalancerClazz,
LoadBalancer.class);
loadBalancer = LoadBalancerFactory.getLoadBalancer(conf); loadBalancer = LoadBalancerFactory.getLoadBalancer(conf);
} }

View File

@ -1,27 +1,32 @@
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license * Licensed to the Apache Software Foundation (ASF) under one
* agreements. See the NOTICE file distributed with this work for additional information regarding * or more contributor license agreements. See the NOTICE file
* copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the * distributed with this work for additional information
* "License"); you may not use this file except in compliance with the License. You may obtain a * regarding copyright ownership. The ASF licenses this file
* copy of the License at * to you under the Apache License, Version 2.0 (the
* <p> * "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 * http://www.apache.org/licenses/LICENSE-2.0
* <p> *
* Unless required by applicable law or agreed to in writing, software distributed under the License * Unless required by applicable law or agreed to in writing, software
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * distributed under the License is distributed on an "AS IS" BASIS,
* or implied. See the License for the specific language governing permissions and limitations under * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* the License. * See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.apache.hadoop.hbase.master.balancer; package org.apache.hadoop.hbase.master.balancer;
import static junit.framework.TestCase.assertNotNull; import static org.apache.hadoop.hbase.master.balancer.HeterogeneousCostRulesTestHelper.DEFAULT_RULES_FILE_NAME;
import static junit.framework.TestCase.assertTrue; import static org.apache.hadoop.hbase.master.balancer.HeterogeneousCostRulesTestHelper.createRulesFile;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
@ -30,7 +35,7 @@ import java.util.TreeMap;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtility; import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.client.RegionInfo; import org.apache.hadoop.hbase.client.RegionInfo;
@ -48,6 +53,7 @@ import org.slf4j.LoggerFactory;
@Category({ MasterTests.class, MediumTests.class }) @Category({ MasterTests.class, MediumTests.class })
public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalancerTestBase { public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalancerTestBase {
@ClassRule @ClassRule
public static final HBaseClassTestRule CLASS_RULE = public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestStochasticLoadBalancerHeterogeneousCost.class); HBaseClassTestRule.forClass(TestStochasticLoadBalancerHeterogeneousCost.class);
@ -55,7 +61,7 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
private static final Logger LOG = private static final Logger LOG =
LoggerFactory.getLogger(TestStochasticLoadBalancerHeterogeneousCost.class); LoggerFactory.getLogger(TestStochasticLoadBalancerHeterogeneousCost.class);
private static final double ALLOWED_WINDOW = 1.20; private static final double ALLOWED_WINDOW = 1.20;
private static final HBaseTestingUtility HTU = new HBaseTestingUtility(); private static final HBaseCommonTestingUtility HTU = new HBaseCommonTestingUtility();
private static String RULES_FILE; private static String RULES_FILE;
@BeforeClass @BeforeClass
@ -69,10 +75,8 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
HeterogeneousRegionCountCostFunction.class.getName()); HeterogeneousRegionCountCostFunction.class.getName());
// Need to ensure test dir has been created. // Need to ensure test dir has been created.
assertTrue(FileSystem.get(HTU.getConfiguration()).mkdirs(HTU.getDataTestDir())); assertTrue(FileSystem.get(HTU.getConfiguration()).mkdirs(HTU.getDataTestDir()));
RULES_FILE = HTU.getDataTestDir( RULES_FILE = HTU.getDataTestDir(DEFAULT_RULES_FILE_NAME).toString();
TestStochasticLoadBalancerHeterogeneousCostRules.DEFAULT_RULES_FILE_NAME).toString(); conf.set(HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE,
conf.set(
HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE,
RULES_FILE); RULES_FILE);
loadBalancer = new StochasticLoadBalancer(); loadBalancer = new StochasticLoadBalancer();
loadBalancer.setClusterInfoProvider(new DummyClusterInfoProvider(conf)); loadBalancer.setClusterInfoProvider(new DummyClusterInfoProvider(conf));
@ -146,7 +150,7 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
final int numRegions = 120; final int numRegions = 120;
final int numRegionsPerServer = 60; final int numRegionsPerServer = 60;
TestStochasticLoadBalancerHeterogeneousCostRules.createRulesFile(RULES_FILE); createRulesFile(RULES_FILE);
final Map<ServerName, List<RegionInfo>> serverMap = final Map<ServerName, List<RegionInfo>> serverMap =
this.createServerMap(numNodes, numRegions, numRegionsPerServer, 1, 1); this.createServerMap(numNodes, numRegions, numRegionsPerServer, 1, 1);
final List<RegionPlan> plans = final List<RegionPlan> plans =
@ -159,12 +163,13 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
private void testHeterogeneousWithCluster(final int numNodes, final int numRegions, private void testHeterogeneousWithCluster(final int numNodes, final int numRegions,
final int numRegionsPerServer, final List<String> rules) throws IOException { final int numRegionsPerServer, final List<String> rules) throws IOException {
TestStochasticLoadBalancerHeterogeneousCostRules.createRulesFile(RULES_FILE, rules); createRulesFile(RULES_FILE, rules);
final Map<ServerName, List<RegionInfo>> serverMap = final Map<ServerName, List<RegionInfo>> serverMap =
this.createServerMap(numNodes, numRegions, numRegionsPerServer, 1, 1); this.createServerMap(numNodes, numRegions, numRegionsPerServer, 1, 1);
this.testWithCluster(serverMap, null, true, false); this.testWithCluster(serverMap, null, true, false);
} }
@Override
protected void testWithCluster(final Map<ServerName, List<RegionInfo>> serverMap, protected void testWithCluster(final Map<ServerName, List<RegionInfo>> serverMap,
final RackManager rackManager, final boolean assertFullyBalanced, final RackManager rackManager, final boolean assertFullyBalanced,
final boolean assertFullyBalancedForReplicas) { final boolean assertFullyBalancedForReplicas) {
@ -196,8 +201,7 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
final HeterogeneousRegionCountCostFunction cf = final HeterogeneousRegionCountCostFunction cf =
new HeterogeneousRegionCountCostFunction(conf); new HeterogeneousRegionCountCostFunction(conf);
assertNotNull(cf); assertNotNull(cf);
BalancerClusterState cluster = BalancerClusterState cluster = new BalancerClusterState(serverMap, null, null, null);
new BalancerClusterState(serverMap, null, null, null);
cf.prepare(cluster); cf.prepare(cluster);
// checking that we all hosts have a number of regions below their limit // checking that we all hosts have a number of regions below their limit
@ -212,10 +216,9 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
// as the balancer is stochastic, we cannot check exactly the result of the balancing, // as the balancer is stochastic, we cannot check exactly the result of the balancing,
// hence the allowedWindow parameter // hence the allowedWindow parameter
assertTrue("Host " + sn.getHostname() + " should be below " assertTrue("Host " + sn.getHostname() + " should be below " +
+ cf.overallUsage * ALLOWED_WINDOW * 100 + "%; " + cf.overallUsage + cf.overallUsage * ALLOWED_WINDOW * 100 + "%; " + cf.overallUsage + ", " + usage + ", " +
", " + usage + ", " + numberRegions + ", " + limit, numberRegions + ", " + limit, usage <= cf.overallUsage * ALLOWED_WINDOW);
usage <= cf.overallUsage * ALLOWED_WINDOW);
} }
} }
@ -266,7 +269,7 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
return servers; return servers;
} }
private Queue<ServerName> serverQueue = new LinkedList<>(); private Queue<ServerName> serverQueue = new ArrayDeque<>();
private ServerAndLoad createServer(final String host) { private ServerAndLoad createServer(final String host) {
if (!this.serverQueue.isEmpty()) { if (!this.serverQueue.isEmpty()) {
@ -280,12 +283,11 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends StochasticBalan
return new ServerAndLoad(sn, 0); return new ServerAndLoad(sn, 0);
} }
static class FairRandomCandidateGenerator extends static class FairRandomCandidateGenerator extends RandomCandidateGenerator {
RandomCandidateGenerator {
@Override @Override
public BalanceAction pickRandomRegions(BalancerClusterState cluster, public BalanceAction pickRandomRegions(BalancerClusterState cluster, int thisServer,
int thisServer, int otherServer) { int otherServer) {
if (thisServer < 0 || otherServer < 0) { if (thisServer < 0 || otherServer < 0) {
return BalanceAction.NULL_ACTION; return BalanceAction.NULL_ACTION;
} }

View File

@ -1,36 +1,34 @@
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license * Licensed to the Apache Software Foundation (ASF) under one
* agreements. See the NOTICE file distributed with this work for additional information regarding * or more contributor license agreements. See the NOTICE file
* copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the * distributed with this work for additional information
* "License"); you may not use this file except in compliance with the License. You may obtain a * regarding copyright ownership. The ASF licenses this file
* copy of the License at * to you under the Apache License, Version 2.0 (the
* <p> * "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 * http://www.apache.org/licenses/LICENSE-2.0
* <p> *
* Unless required by applicable law or agreed to in writing, software distributed under the License * Unless required by applicable law or agreed to in writing, software
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * distributed under the License is distributed on an "AS IS" BASIS,
* or implied. See the License for the specific language governing permissions and limitations under * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* the License. * See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.apache.hadoop.hbase.master.balancer; package org.apache.hadoop.hbase.master.balancer;
import static org.apache.hadoop.hbase.master.balancer.HeterogeneousCostRulesTestHelper.DEFAULT_RULES_FILE_NAME;
import static org.apache.hadoop.hbase.master.balancer.HeterogeneousCostRulesTestHelper.cleanup;
import static org.apache.hadoop.hbase.master.balancer.HeterogeneousCostRulesTestHelper.createRulesFile;
import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.NoSuchFileException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtility; import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
import org.apache.hadoop.hbase.testclassification.MasterTests; import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests; import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.ClassRule; import org.junit.ClassRule;
@ -41,15 +39,15 @@ import org.junit.rules.TestName;
@Category({ MasterTests.class, MediumTests.class }) @Category({ MasterTests.class, MediumTests.class })
public class TestStochasticLoadBalancerHeterogeneousCostRules extends StochasticBalancerTestBase { public class TestStochasticLoadBalancerHeterogeneousCostRules extends StochasticBalancerTestBase {
@ClassRule @ClassRule
public static final HBaseClassTestRule CLASS_RULE = public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestStochasticLoadBalancerHeterogeneousCostRules.class); HBaseClassTestRule.forClass(TestStochasticLoadBalancerHeterogeneousCostRules.class);
@Rule @Rule
public TestName name = new TestName(); public TestName name = new TestName();
static final String DEFAULT_RULES_FILE_NAME = "hbase-balancer.rules";
private HeterogeneousRegionCountCostFunction costFunction; private HeterogeneousRegionCountCostFunction costFunction;
private static final HBaseTestingUtility HTU = new HBaseTestingUtility(); private static final HBaseCommonTestingUtility HTU = new HBaseCommonTestingUtility();
/** /**
* Make a file for rules that is inside a temporary test dir named for the method so it doesn't * Make a file for rules that is inside a temporary test dir named for the method so it doesn't
@ -60,47 +58,22 @@ public class TestStochasticLoadBalancerHeterogeneousCostRules extends Stochastic
@BeforeClass @BeforeClass
public static void beforeClass() throws IOException { public static void beforeClass() throws IOException {
// Ensure test dir is created // Ensure test dir is created
HTU.getTestFileSystem().mkdirs(HTU.getDataTestDir()); HTU.getDataTestDir().getFileSystem(HTU.getConfiguration()).mkdirs(HTU.getDataTestDir());
} }
@Before @Before
public void before() throws IOException { public void before() throws IOException {
// New rules file name per test. // New rules file name per test.
this.rulesFilename = HTU.getDataTestDir( this.rulesFilename = HTU
this.name.getMethodName() + "." + DEFAULT_RULES_FILE_NAME).toString(); .getDataTestDir(
this.name.getMethodName() + "." + DEFAULT_RULES_FILE_NAME)
.toString();
// Set the created rules filename into the configuration. // Set the created rules filename into the configuration.
HTU.getConfiguration().set( HTU.getConfiguration().set(
HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE, HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE,
this.rulesFilename); this.rulesFilename);
} }
/**
* @param file Name of file to write rules into.
* @return Full file name of the rules file which is <code>dir</code> + DEFAULT_RULES_FILE_NAME.
*/
static String createRulesFile(String file, final List<String> lines) throws IOException {
cleanup(file);
java.nio.file.Path path =
java.nio.file.Files.createFile(FileSystems.getDefault().getPath(file));
return java.nio.file.Files.write(path, lines, Charset.forName("UTF-8")).toString();
}
/**
* @param file Name of file to write rules into.
* @return Full file name of the rules file which is <code>dir</code> + DEFAULT_RULES_FILE_NAME.
*/
static String createRulesFile(String file) throws IOException {
return createRulesFile(file, Collections.emptyList());
}
private static void cleanup(String file) throws IOException {
try {
java.nio.file.Files.delete(FileSystems.getDefault().getPath(file));
} catch (NoSuchFileException nsfe) {
System.out.println("FileNotFoundException for " + file);
}
}
@Test @Test
public void testNoRules() throws IOException { public void testNoRules() throws IOException {
// Override what is in the configuration with the name of a non-existent file! // Override what is in the configuration with the name of a non-existent file!
@ -109,7 +82,7 @@ public class TestStochasticLoadBalancerHeterogeneousCostRules extends Stochastic
"non-existent-file!"); "non-existent-file!");
this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration()); this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration());
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(0, this.costFunction.getNumberOfRulesLoaded()); assertEquals(0, this.costFunction.getNumberOfRulesLoaded());
} }
@Test @Test
@ -119,18 +92,18 @@ public class TestStochasticLoadBalancerHeterogeneousCostRules extends Stochastic
// in the configuration. // in the configuration.
this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration()); this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration());
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(0, this.costFunction.getNumberOfRulesLoaded()); assertEquals(0, this.costFunction.getNumberOfRulesLoaded());
createRulesFile(this.rulesFilename, Collections.singletonList("bad rules format")); createRulesFile(this.rulesFilename, Collections.singletonList("bad rules format"));
this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration()); this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration());
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(0, this.costFunction.getNumberOfRulesLoaded()); assertEquals(0, this.costFunction.getNumberOfRulesLoaded());
createRulesFile(this.rulesFilename, Arrays.asList("srv[1-2] 10", createRulesFile(this.rulesFilename, Arrays.asList("srv[1-2] 10",
"bad_rules format", "a")); "bad_rules format", "a"));
this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration()); this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration());
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(1, this.costFunction.getNumberOfRulesLoaded()); assertEquals(1, this.costFunction.getNumberOfRulesLoaded());
} }
@Test @Test
@ -144,7 +117,7 @@ public class TestStochasticLoadBalancerHeterogeneousCostRules extends Stochastic
createRulesFile(this.rulesFilename, Arrays.asList("^server1$ 10", "^server2 21")); createRulesFile(this.rulesFilename, Arrays.asList("^server1$ 10", "^server2 21"));
this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration()); this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration());
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(2, this.costFunction.getNumberOfRulesLoaded()); assertEquals(2, this.costFunction.getNumberOfRulesLoaded());
} }
@Test @Test
@ -158,7 +131,7 @@ public class TestStochasticLoadBalancerHeterogeneousCostRules extends Stochastic
createRulesFile(this.rulesFilename, Collections.singletonList("server[ 1")); createRulesFile(this.rulesFilename, Collections.singletonList("server[ 1"));
this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration()); this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration());
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(0, this.costFunction.getNumberOfRulesLoaded()); assertEquals(0, this.costFunction.getNumberOfRulesLoaded());
} }
@Test @Test
@ -169,38 +142,11 @@ public class TestStochasticLoadBalancerHeterogeneousCostRules extends Stochastic
createRulesFile(this.rulesFilename, Arrays.asList("^server1$ 10", "^server2 21")); createRulesFile(this.rulesFilename, Arrays.asList("^server1$ 10", "^server2 21"));
this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration()); this.costFunction = new HeterogeneousRegionCountCostFunction(HTU.getConfiguration());
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(2, this.costFunction.getNumberOfRulesLoaded()); assertEquals(2, this.costFunction.getNumberOfRulesLoaded());
// loading malformed configuration does not overload current // loading malformed configuration does not overload current
cleanup(this.rulesFilename); cleanup(this.rulesFilename);
this.costFunction.loadRules(); this.costFunction.loadRules();
Assert.assertEquals(2, this.costFunction.getNumberOfRulesLoaded()); assertEquals(2, this.costFunction.getNumberOfRulesLoaded());
}
@Test
public void testLoadingFomHDFS() throws Exception {
HTU.startMiniDFSCluster(3);
try {
MiniDFSCluster cluster = HTU.getDFSCluster();
DistributedFileSystem fs = cluster.getFileSystem();
// Writing file
Path path = new Path(fs.getHomeDirectory(), DEFAULT_RULES_FILE_NAME);
FSDataOutputStream stream = fs.create(path);
stream.write("server1 10".getBytes());
stream.flush();
stream.close();
Configuration configuration = HTU.getConfiguration();
// start costFunction
configuration.set(
HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE,
path.toString());
this.costFunction = new HeterogeneousRegionCountCostFunction(configuration);
this.costFunction.loadRules();
Assert.assertEquals(1, this.costFunction.getNumberOfRulesLoaded());
} finally {
HTU.shutdownMiniCluster();
}
} }
} }

View File

@ -0,0 +1,81 @@
/*
* 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 static org.apache.hadoop.hbase.master.balancer.HeterogeneousCostRulesTestHelper.DEFAULT_RULES_FILE_NAME;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@Category({ MasterTests.class, MediumTests.class })
public class TestStochasticLoadBalancerHeterogeneousCostRulesLoadFromHDFS
extends StochasticBalancerTestBase {
@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestStochasticLoadBalancerHeterogeneousCostRulesLoadFromHDFS.class);
private HeterogeneousRegionCountCostFunction costFunction;
private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
@Before
public void setUp() throws Exception {
HTU.startMiniCluster(1);
}
@After
public void tearDown() throws IOException {
HTU.shutdownMiniCluster();
}
@Test
public void testLoadingFomHDFS() throws Exception {
MiniDFSCluster cluster = HTU.getDFSCluster();
DistributedFileSystem fs = cluster.getFileSystem();
// Writing file
Path path = new Path(fs.getHomeDirectory(), DEFAULT_RULES_FILE_NAME);
FSDataOutputStream stream = fs.create(path);
stream.write("server1 10".getBytes());
stream.flush();
stream.close();
Configuration configuration = HTU.getConfiguration();
// start costFunction
configuration.set(
HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE,
path.toString());
this.costFunction = new HeterogeneousRegionCountCostFunction(configuration);
this.costFunction.loadRules();
assertEquals(1, this.costFunction.getNumberOfRulesLoaded());
}
}