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.jupiter.api.Assertions.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.HBaseTestingUtil;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hdfs.DistributedFileSystem;
031import org.apache.hadoop.hdfs.MiniDFSCluster;
032import org.junit.jupiter.api.AfterEach;
033import org.junit.jupiter.api.BeforeEach;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036
037@Tag(MasterTests.TAG)
038@Tag(MediumTests.TAG)
039public class TestStochasticLoadBalancerHeterogeneousCostRulesLoadFromHDFS
040  extends StochasticBalancerTestBase {
041
042  private HeterogeneousRegionCountCostFunction costFunction;
043  private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
044
045  @BeforeEach
046  public void setUp() throws Exception {
047    HTU.startMiniCluster(1);
048  }
049
050  @AfterEach
051  public void tearDown() throws IOException {
052    HTU.shutdownMiniCluster();
053  }
054
055  @Test
056  public void testLoadingFomHDFS() throws Exception {
057    MiniDFSCluster cluster = HTU.getDFSCluster();
058    DistributedFileSystem fs = cluster.getFileSystem();
059    // Writing file
060    Path path = new Path(fs.getHomeDirectory(), DEFAULT_RULES_FILE_NAME);
061    FSDataOutputStream stream = fs.create(path);
062    stream.write("server1 10".getBytes());
063    stream.flush();
064    stream.close();
065
066    Configuration configuration = HTU.getConfiguration();
067
068    // start costFunction
069    configuration.set(
070      HeterogeneousRegionCountCostFunction.HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE,
071      path.toString());
072    this.costFunction = new HeterogeneousRegionCountCostFunction(configuration);
073    this.costFunction.loadRules();
074    assertEquals(1, this.costFunction.getNumberOfRulesLoaded());
075  }
076}