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.testclassification.LargeTests;
33  import org.apache.hadoop.hbase.MiniHBaseCluster;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.client.Admin;
36  import org.apache.hadoop.hbase.client.HBaseAdmin;
37  import org.apache.hadoop.hbase.client.HTable;
38  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
41  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
42  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
43  import org.apache.zookeeper.KeeperException;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  
47  @Category(LargeTests.class)
48  public class TestMasterRestartAfterDisablingTable {
49  
50    private static final Log LOG = LogFactory.getLog(TestMasterRestartAfterDisablingTable.class);
51  
52    @Test
53    public void testForCheckingIfEnableAndDisableWorksFineAfterSwitch()
54        throws Exception {
55      final int NUM_MASTERS = 2;
56      final int NUM_RS = 1;
57      final int NUM_REGIONS_TO_CREATE = 4;
58  
59      // Start the cluster
60      log("Starting cluster");
61      Configuration conf = HBaseConfiguration.create();
62      HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
63      TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
64      MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
65      log("Waiting for active/ready master");
66      cluster.waitForActiveAndReadyMaster();
67      ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf, "testmasterRestart", null);
68      HMaster master = cluster.getMaster();
69  
70      // Create a table with regions
71      TableName table = TableName.valueOf("tableRestart");
72      byte[] family = Bytes.toBytes("family");
73      log("Creating table with " + NUM_REGIONS_TO_CREATE + " regions");
74      HTable ht = TEST_UTIL.createTable(table, family);
75      int numRegions = TEST_UTIL.createMultiRegions(conf, ht, family,
76          NUM_REGIONS_TO_CREATE);
77      numRegions += 1; // catalogs
78      log("Waiting for no more RIT\n");
79      blockUntilNoRIT(zkw, master);
80      log("Disabling table\n");
81      TEST_UTIL.getHBaseAdmin().disableTable(table);
82  
83      NavigableSet<String> regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
84      assertEquals(
85          "The number of regions for the table tableRestart should be 0 and only"
86              + "the catalog and namespace tables should be present.", 2, regions.size());
87  
88      List<MasterThread> masterThreads = cluster.getMasterThreads();
89      MasterThread activeMaster = null;
90      if (masterThreads.get(0).getMaster().isActiveMaster()) {
91        activeMaster = masterThreads.get(0);
92      } else {
93        activeMaster = masterThreads.get(1);
94      }
95      activeMaster.getMaster().stop(
96          "stopping the active master so that the backup can become active");
97      cluster.hbaseCluster.waitOnMaster(activeMaster);
98      cluster.waitForActiveAndReadyMaster();
99  
100     assertTrue("The table should not be in enabled state", cluster.getMaster()
101         .getAssignmentManager().getTableStateManager().isTableState(
102         TableName.valueOf("tableRestart"), ZooKeeperProtos.Table.State.DISABLED,
103         ZooKeeperProtos.Table.State.DISABLING));
104     log("Enabling table\n");
105     // Need a new Admin, the previous one is on the old master
106     Admin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
107     admin.enableTable(table);
108     admin.close();
109     log("Waiting for no more RIT\n");
110     blockUntilNoRIT(zkw, master);
111     log("Verifying there are " + numRegions + " assigned on cluster\n");
112     regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
113     assertEquals("The assigned regions were not onlined after master"
114         + " switch except for the catalog and namespace tables.",
115           6, regions.size());
116     assertTrue("The table should be in enabled state", cluster.getMaster()
117         .getAssignmentManager().getTableStateManager()
118         .isTableState(TableName.valueOf("tableRestart"), ZooKeeperProtos.Table.State.ENABLED));
119     ht.close();
120     TEST_UTIL.shutdownMiniCluster();
121   }
122 
123   private void log(String msg) {
124     LOG.debug("\n\nTRR: " + msg + "\n");
125   }
126 
127   private void blockUntilNoRIT(ZooKeeperWatcher zkw, HMaster master)
128       throws KeeperException, InterruptedException {
129     ZKAssign.blockUntilNoRIT(zkw);
130     master.assignmentManager.waitUntilNoRegionsInTransition(60000);
131   }
132 }
133