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.HConstants;
024import org.apache.hadoop.hbase.TableDescriptors;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.master.HMaster;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034/**
035 * Make sure we will honor the {@link HConstants#META_REPLICAS_NUM}.And also test upgrading.
036 */
037@Category({ MiscTests.class, MediumTests.class })
038public class TestIncreaseMetaReplicaThroughConfig extends MetaWithReplicasTestBase {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestIncreaseMetaReplicaThroughConfig.class);
043
044  @BeforeClass
045  public static void setUp() throws Exception {
046    startCluster();
047  }
048
049  @Test
050  public void testUpgradeAndIncreaseReplicaCount() throws Exception {
051    HMaster oldMaster = TEST_UTIL.getMiniHBaseCluster().getMaster();
052    TableDescriptors oldTds = oldMaster.getTableDescriptors();
053    TableDescriptor oldMetaTd = oldTds.get(TableName.META_TABLE_NAME);
054    assertEquals(3, oldMetaTd.getRegionReplication());
055    // force update the replica count to 1 and then kill the master, to simulate that hen upgrading,
056    // we have no region replication in meta table descriptor but we actually have meta region
057    // replicas
058    oldTds.update(TableDescriptorBuilder.newBuilder(oldMetaTd).setRegionReplication(1).build());
059    oldMaster.stop("Restarting");
060    TEST_UTIL.waitFor(30000, () -> oldMaster.isStopped());
061
062    // increase replica count to 5 through Configuration
063    TEST_UTIL.getMiniHBaseCluster().getConfiguration().setInt(HConstants.META_REPLICAS_NUM, 5);
064    TEST_UTIL.getMiniHBaseCluster().startMaster();
065    TEST_UTIL.waitFor(30000,
066      () -> TEST_UTIL.getZooKeeperWatcher().getMetaReplicaNodes().size() == 5);
067  }
068}