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;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertTrue;
023import static org.junit.Assert.fail;
024
025import java.io.IOException;
026import java.util.List;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HColumnDescriptor;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.HTableDescriptor;
032import org.apache.hadoop.hbase.MetaTableAccessor;
033import org.apache.hadoop.hbase.MiniHBaseCluster;
034import org.apache.hadoop.hbase.PleaseHoldException;
035import org.apache.hadoop.hbase.ServerName;
036import org.apache.hadoop.hbase.TableName;
037import org.apache.hadoop.hbase.UnknownRegionException;
038import org.apache.hadoop.hbase.client.Admin;
039import org.apache.hadoop.hbase.client.RegionInfo;
040import org.apache.hadoop.hbase.client.RegionInfoBuilder;
041import org.apache.hadoop.hbase.client.Table;
042import org.apache.hadoop.hbase.client.TableState;
043import org.apache.hadoop.hbase.master.assignment.RegionStates;
044import org.apache.hadoop.hbase.testclassification.MasterTests;
045import org.apache.hadoop.hbase.testclassification.MediumTests;
046import org.apache.hadoop.hbase.util.Bytes;
047import org.apache.hadoop.hbase.util.Pair;
048import org.apache.hadoop.util.StringUtils;
049import org.junit.AfterClass;
050import org.junit.BeforeClass;
051import org.junit.ClassRule;
052import org.junit.Rule;
053import org.junit.Test;
054import org.junit.experimental.categories.Category;
055import org.junit.rules.TestName;
056import org.slf4j.Logger;
057import org.slf4j.LoggerFactory;
058
059import org.apache.hbase.thirdparty.com.google.common.base.Joiner;
060
061@Category({MasterTests.class, MediumTests.class})
062public class TestMaster {
063
064  @ClassRule
065  public static final HBaseClassTestRule CLASS_RULE =
066      HBaseClassTestRule.forClass(TestMaster.class);
067
068  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
069  private static final Logger LOG = LoggerFactory.getLogger(TestMaster.class);
070  private static final TableName TABLENAME =
071      TableName.valueOf("TestMaster");
072  private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
073  private static Admin admin;
074
075  @Rule
076  public TestName name = new TestName();
077
078  @BeforeClass
079  public static void beforeAllTests() throws Exception {
080    // we will retry operations when PleaseHoldException is thrown
081    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 3);
082    // Start a cluster of two regionservers.
083    TEST_UTIL.startMiniCluster(2);
084    admin = TEST_UTIL.getAdmin();
085  }
086
087  @AfterClass
088  public static void afterAllTests() throws Exception {
089    TEST_UTIL.shutdownMiniCluster();
090  }
091
092  @Test
093  @SuppressWarnings("deprecation")
094  public void testMasterOpsWhileSplitting() throws Exception {
095    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
096    HMaster m = cluster.getMaster();
097
098    try (Table ht = TEST_UTIL.createTable(TABLENAME, FAMILYNAME)) {
099      assertTrue(m.getTableStateManager().isTableState(TABLENAME, TableState.State.ENABLED));
100      TEST_UTIL.loadTable(ht, FAMILYNAME, false);
101    }
102
103    List<Pair<RegionInfo, ServerName>> tableRegions = MetaTableAccessor.getTableRegionsAndLocations(
104        m.getConnection(), TABLENAME);
105    LOG.info("Regions after load: " + Joiner.on(',').join(tableRegions));
106    assertEquals(1, tableRegions.size());
107    assertArrayEquals(HConstants.EMPTY_START_ROW,
108        tableRegions.get(0).getFirst().getStartKey());
109    assertArrayEquals(HConstants.EMPTY_END_ROW,
110        tableRegions.get(0).getFirst().getEndKey());
111
112    // Now trigger a split and stop when the split is in progress
113    LOG.info("Splitting table");
114    TEST_UTIL.getAdmin().split(TABLENAME);
115
116    LOG.info("Making sure we can call getTableRegions while opening");
117    while (tableRegions.size() < 3) {
118      tableRegions = MetaTableAccessor.getTableRegionsAndLocations(m.getConnection(),
119          TABLENAME, false);
120      Thread.sleep(100);
121    }
122    LOG.info("Regions: " + Joiner.on(',').join(tableRegions));
123    // We have three regions because one is split-in-progress
124    assertEquals(3, tableRegions.size());
125    LOG.info("Making sure we can call getTableRegionClosest while opening");
126    Pair<RegionInfo, ServerName> pair =
127        m.getTableRegionForRow(TABLENAME, Bytes.toBytes("cde"));
128    LOG.info("Result is: " + pair);
129    Pair<RegionInfo, ServerName> tableRegionFromName =
130        MetaTableAccessor.getRegion(m.getConnection(),
131          pair.getFirst().getRegionName());
132    assertTrue(RegionInfo.COMPARATOR.compare(tableRegionFromName.getFirst(), pair.getFirst()) == 0);
133  }
134
135  @Test
136  public void testMoveRegionWhenNotInitialized() {
137    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
138    HMaster m = cluster.getMaster();
139    try {
140      m.setInitialized(false); // fake it, set back later
141      RegionInfo meta = RegionInfoBuilder.FIRST_META_REGIONINFO;
142      m.move(meta.getEncodedNameAsBytes(), null);
143      fail("Region should not be moved since master is not initialized");
144    } catch (IOException ioe) {
145      assertTrue(ioe instanceof PleaseHoldException);
146    } finally {
147      m.setInitialized(true);
148    }
149  }
150
151  @Test
152  public void testMoveThrowsUnknownRegionException() throws IOException {
153    final TableName tableName = TableName.valueOf(name.getMethodName());
154    HTableDescriptor htd = new HTableDescriptor(tableName);
155    HColumnDescriptor hcd = new HColumnDescriptor("value");
156    htd.addFamily(hcd);
157
158    admin.createTable(htd, null);
159    try {
160      RegionInfo hri = RegionInfoBuilder.newBuilder(tableName)
161          .setStartKey(Bytes.toBytes("A"))
162          .setEndKey(Bytes.toBytes("Z"))
163          .build();
164      admin.move(hri.getEncodedNameAsBytes(), null);
165      fail("Region should not be moved since it is fake");
166    } catch (IOException ioe) {
167      assertTrue(ioe instanceof UnknownRegionException);
168    } finally {
169      TEST_UTIL.deleteTable(tableName);
170    }
171  }
172
173  @Test
174  public void testMoveThrowsPleaseHoldException() throws IOException {
175    final TableName tableName = TableName.valueOf(name.getMethodName());
176    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
177    HTableDescriptor htd = new HTableDescriptor(tableName);
178    HColumnDescriptor hcd = new HColumnDescriptor("value");
179    htd.addFamily(hcd);
180
181    admin.createTable(htd, null);
182    try {
183      List<RegionInfo> tableRegions = admin.getRegions(tableName);
184
185      master.setInitialized(false); // fake it, set back later
186      admin.move(tableRegions.get(0).getEncodedNameAsBytes(), null);
187      fail("Region should not be moved since master is not initialized");
188    } catch (IOException ioe) {
189      assertTrue(StringUtils.stringifyException(ioe).contains("PleaseHoldException"));
190    } finally {
191      master.setInitialized(true);
192      TEST_UTIL.deleteTable(tableName);
193    }
194  }
195}
196