View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.List;
25  import java.util.NavigableSet;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.HBaseConfiguration;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.MiniHBaseCluster;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.client.Admin;
35  import org.apache.hadoop.hbase.client.HBaseAdmin;
36  import org.apache.hadoop.hbase.client.HTable;
37  import org.apache.hadoop.hbase.client.RegionLocator;
38  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
39  import org.apache.hadoop.hbase.testclassification.LargeTests;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
42  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
43  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
44  import org.apache.zookeeper.KeeperException;
45  import org.junit.Test;
46  import org.junit.experimental.categories.Category;
47  
48  @Category(LargeTests.class)
49  public class TestMasterRestartAfterDisablingTable {
50  
51    private static final Log LOG = LogFactory.getLog(TestMasterRestartAfterDisablingTable.class);
52  
53    @Test
54    public void testForCheckingIfEnableAndDisableWorksFineAfterSwitch()
55        throws Exception {
56      final int NUM_MASTERS = 2;
57      final int NUM_RS = 1;
58      final int NUM_REGIONS_TO_CREATE = 4;
59  
60      // Start the cluster
61      log("Starting cluster");
62      Configuration conf = HBaseConfiguration.create();
63      HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
64      TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
65      MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
66      log("Waiting for active/ready master");
67      cluster.waitForActiveAndReadyMaster();
68      ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf, "testmasterRestart", null);
69      HMaster master = cluster.getMaster();
70  
71      // Create a table with regions
72      TableName table = TableName.valueOf("tableRestart");
73      byte[] family = Bytes.toBytes("family");
74      log("Creating table with " + NUM_REGIONS_TO_CREATE + " regions");
75      HTable ht = TEST_UTIL.createMultiRegionTable(table, family, NUM_REGIONS_TO_CREATE);
76      int numRegions = -1;
77      try (RegionLocator r = ht.getRegionLocator()) {
78        numRegions = r.getStartKeys().length;
79      }
80      numRegions += 1; // catalogs
81      log("Waiting for no more RIT\n");
82      blockUntilNoRIT(zkw, master);
83      log("Disabling table\n");
84      TEST_UTIL.getHBaseAdmin().disableTable(table);
85  
86      NavigableSet<String> regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
87      assertEquals(
88          "The number of regions for the table tableRestart should be 0 and only"
89              + "the catalog and namespace tables should be present.", 2, regions.size());
90  
91      List<MasterThread> masterThreads = cluster.getMasterThreads();
92      MasterThread activeMaster = null;
93      if (masterThreads.get(0).getMaster().isActiveMaster()) {
94        activeMaster = masterThreads.get(0);
95      } else {
96        activeMaster = masterThreads.get(1);
97      }
98      activeMaster.getMaster().stop(
99          "stopping the active master so that the backup can become active");
100     cluster.hbaseCluster.waitOnMaster(activeMaster);
101     cluster.waitForActiveAndReadyMaster();
102 
103     assertTrue("The table should not be in enabled state", cluster.getMaster()
104         .getAssignmentManager().getTableStateManager().isTableState(
105         TableName.valueOf("tableRestart"), ZooKeeperProtos.Table.State.DISABLED,
106         ZooKeeperProtos.Table.State.DISABLING));
107     log("Enabling table\n");
108     // Need a new Admin, the previous one is on the old master
109     Admin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
110     admin.enableTable(table);
111     admin.close();
112     log("Waiting for no more RIT\n");
113     blockUntilNoRIT(zkw, master);
114     log("Verifying there are " + numRegions + " assigned on cluster\n");
115     regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
116     assertEquals("The assigned regions were not onlined after master"
117         + " switch except for the catalog and namespace tables.",
118           6, regions.size());
119     assertTrue("The table should be in enabled state", cluster.getMaster()
120         .getAssignmentManager().getTableStateManager()
121         .isTableState(TableName.valueOf("tableRestart"), ZooKeeperProtos.Table.State.ENABLED));
122     ht.close();
123     TEST_UTIL.shutdownMiniCluster();
124   }
125 
126   private void log(String msg) {
127     LOG.debug("\n\nTRR: " + msg + "\n");
128   }
129 
130   private void blockUntilNoRIT(ZooKeeperWatcher zkw, HMaster master)
131       throws KeeperException, InterruptedException {
132     ZKAssign.blockUntilNoRIT(zkw);
133     master.assignmentManager.waitUntilNoRegionsInTransition(60000);
134   }
135 }
136