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.jupiter.api.Assertions.assertEquals;
021
022import java.util.List;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.master.HMaster;
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.util.JVMClusterUtil;
028import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
029import org.junit.jupiter.api.BeforeAll;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032
033/**
034 * Make sure we will honor the {@link HConstants#META_REPLICAS_NUM}.
035 */
036@Tag(MiscTests.TAG)
037@Tag(MediumTests.TAG)
038public class TestCleanupMetaReplicaThroughConfig extends MetaWithReplicasTestBase {
039
040  @BeforeAll
041  public static void setUp() throws Exception {
042    startCluster();
043  }
044
045  @Test
046  public void testReplicaCleanup() throws Exception {
047    ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
048    List<String> metaReplicaZnodes = zkw.getMetaReplicaNodes();
049    assertEquals(3, metaReplicaZnodes.size());
050
051    final HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
052    master.stop("Restarting");
053    TEST_UTIL.waitFor(30000, () -> master.isStopped());
054    TEST_UTIL.getMiniHBaseCluster().getConfiguration().setInt(HConstants.META_REPLICAS_NUM, 1);
055
056    JVMClusterUtil.MasterThread newMasterThread = TEST_UTIL.getMiniHBaseCluster().startMaster();
057    final HMaster newMaster = newMasterThread.getMaster();
058
059    // wait until new master finished meta replica assignment logic
060    TEST_UTIL.waitFor(30000, () -> newMaster.getMasterQuotaManager() != null);
061    TEST_UTIL.waitFor(30000,
062      () -> TEST_UTIL.getZooKeeperWatcher().getMetaReplicaNodes().size() == 1);
063  }
064}