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.client;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.StartTestingClusterOption;
026import org.apache.hadoop.hbase.TableDescriptors;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.TableNameTestRule;
029import org.apache.hadoop.hbase.master.HMaster;
030import org.apache.hadoop.hbase.regionserver.StorefileRefresherChore;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.testclassification.MiscTests;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Rule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040@Category({ MiscTests.class, MediumTests.class })
041public class TestMetaReplicaAssignment {
042  protected static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
043
044  protected static final int REGIONSERVERS_COUNT = 3;
045
046  @Rule
047  public TableNameTestRule name = new TableNameTestRule();
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestMetaReplicaAssignment.class);
052
053  @AfterClass
054  public static void tearDown() throws Exception {
055    TEST_UTIL.shutdownMiniCluster();
056  }
057
058  @BeforeClass
059  public static void setUp() throws Exception {
060    TEST_UTIL.getConfiguration().setInt("zookeeper.session.timeout", 30000);
061    TEST_UTIL.getConfiguration()
062      .setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000);
063    StartTestingClusterOption option = StartTestingClusterOption.builder()
064      .numAlwaysStandByMasters(1).numMasters(1).numRegionServers(REGIONSERVERS_COUNT).build();
065    TEST_UTIL.startMiniCluster(option);
066    Admin admin = TEST_UTIL.getAdmin();
067    TEST_UTIL.waitFor(30000, () -> TEST_UTIL.getMiniHBaseCluster().getMaster() != null);
068    HBaseTestingUtil.setReplicas(admin, TableName.META_TABLE_NAME, 1);
069  }
070
071  @Test
072  public void testUpgradeSameReplicaCount() throws Exception {
073    HMaster oldMaster = TEST_UTIL.getMiniHBaseCluster().getMaster();
074    TableDescriptors oldTds = oldMaster.getTableDescriptors();
075    TableDescriptor oldMetaTd = oldTds.get(TableName.META_TABLE_NAME);
076
077    assertEquals(1, oldMaster.getAssignmentManager().getRegionStates()
078      .getRegionsOfTable(TableName.META_TABLE_NAME).size());
079    assertEquals(1, oldMetaTd.getRegionReplication());
080    // force update the replica count to 3 and then kill the master, to simulate that
081    // HBase storage location is reused, resulting a sane-looking meta and only the
082    // RegionInfoBuilder.FIRST_META_REGIONINFO gets assigned in InitMetaProcedure.
083    // Meta region replicas are not assigned, but we have replication in meta table descriptor.
084
085    oldTds.update(TableDescriptorBuilder.newBuilder(oldMetaTd).setRegionReplication(3).build());
086    oldMaster.stop("Restarting");
087    TEST_UTIL.waitFor(30000, () -> oldMaster.isStopped());
088
089    // increase replica count to 3 through Configuration
090    TEST_UTIL.getMiniHBaseCluster().getConfiguration().setInt(HConstants.META_REPLICAS_NUM, 3);
091    TEST_UTIL.getMiniHBaseCluster().startMaster();
092    TEST_UTIL.waitFor(30000,
093      () -> TEST_UTIL.getZooKeeperWatcher().getMetaReplicaNodes().size() == 3);
094    HMaster newMaster = TEST_UTIL.getMiniHBaseCluster().getMaster();
095    assertEquals(3, newMaster.getAssignmentManager().getRegionStates()
096      .getRegionsOfTable(TableName.META_TABLE_NAME).size());
097    assertEquals(3,
098      newMaster.getTableDescriptors().get(TableName.META_TABLE_NAME).getRegionReplication());
099  }
100}