001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.balancer;
019
020import static org.apache.hadoop.hbase.master.balancer.HeterogeneousCostRulesTestHelper.DEFAULT_RULES_FILE_NAME;
021import static org.junit.Assert.assertEquals;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FSDataOutputStream;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.testclassification.MasterTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hdfs.DistributedFileSystem;
032import org.apache.hadoop.hdfs.MiniDFSCluster;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category({ MasterTests.class, MediumTests.class })
040public class TestStochasticLoadBalancerHeterogeneousCostRulesLoadFromHDFS
041  extends StochasticBalancerTestBase {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestStochasticLoadBalancerHeterogeneousCostRulesLoadFromHDFS.class);
046
047  private HeterogeneousRegionCountCostFunction costFunction;
048  private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
049
050  @Before
051  public void setUp() throws Exception {
052    HTU.startMiniCluster(1);
053  }
054
055  @After
056  public void tearDown() throws IOException {
057    HTU.shutdownMiniCluster();
058  }
059
060  @Test
061  public void testLoadingFomHDFS() throws Exception {
062    MiniDFSCluster cluster = HTU.getDFSCluster();
063    DistributedFileSystem fs = cluster.getFileSystem();
064    // Writing file
065    Path path = new Path(fs.getHomeDirectory(), DEFAULT_RULES_FILE_NAME);
066    FSDataOutputStream stream = fs.create(path);
067    stream.write("server1 10".getBytes());
068    stream.flush();
069    stream.close();
070
071    Configuration configuration = HTU.getConfiguration();
072
073    // start costFunction
074    configuration.set(
075      HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE,
076      path.toString());
077    this.costFunction = new HeterogeneousRegionCountCostFunction(configuration);
078    this.costFunction.loadRules();
079    assertEquals(1, this.costFunction.getNumberOfRulesLoaded());
080  }
081}