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.io.IOException;
25  import java.util.List;
26  import java.util.NavigableSet;
27  import java.util.TreeSet;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.*;
33  import org.apache.hadoop.hbase.client.HBaseAdmin;
34  import org.apache.hadoop.hbase.client.HTable;
35  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
36  import org.apache.hadoop.hbase.testclassification.LargeTests;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
39  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
40  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
41  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
42  import org.apache.zookeeper.KeeperException;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  @Category(LargeTests.class)
47  public class TestMasterRestartAfterDisablingTable {
48  
49    private static final Log LOG = LogFactory.getLog(TestMasterRestartAfterDisablingTable.class);
50  
51    @Test
52    public void testForCheckingIfEnableAndDisableWorksFineAfterSwitch()
53        throws Exception {
54      final int NUM_MASTERS = 2;
55      final int NUM_RS = 1;
56      final int NUM_REGIONS_TO_CREATE = 4;
57  
58      // Start the cluster
59      log("Starting cluster");
60      Configuration conf = HBaseConfiguration.create();
61      conf.setInt("hbase.master.assignment.timeoutmonitor.period", 2000);
62      conf.setInt("hbase.master.assignment.timeoutmonitor.timeout", 5000);
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      byte[] table = Bytes.toBytes("tableRestart");
73      byte[] family = Bytes.toBytes("family");
74      log("Creating table with " + NUM_REGIONS_TO_CREATE + " regions");
75      HTable ht = TEST_UTIL.createTable(table, family);
76      int numRegions = TEST_UTIL.createMultiRegions(conf, ht, family,
77          NUM_REGIONS_TO_CREATE);
78      numRegions += 1; // catalogs
79      log("Waiting for no more RIT\n");
80      blockUntilNoRIT(zkw, master);
81      log("Disabling table\n");
82      TEST_UTIL.getHBaseAdmin().disableTable(table);
83  
84      NavigableSet<String> regions = getAllOnlineRegions(cluster);
85      assertEquals(
86          "The number of regions for the table tableRestart should be 0 and only"
87              + "the catalog and namespace tables should be present.", 2, regions.size());
88  
89      List<MasterThread> masterThreads = cluster.getMasterThreads();
90      MasterThread activeMaster = null;
91      if (masterThreads.get(0).getMaster().isActiveMaster()) {
92        activeMaster = masterThreads.get(0);
93      } else {
94        activeMaster = masterThreads.get(1);
95      }
96      activeMaster.getMaster().stop(
97          "stopping the active master so that the backup can become active");
98      cluster.hbaseCluster.waitOnMaster(activeMaster);
99      cluster.waitForActiveAndReadyMaster();
100 
101     assertTrue("The table should not be in enabled state", cluster.getMaster()
102         .getAssignmentManager().getZKTable().isDisablingOrDisabledTable(
103             TableName.valueOf("tableRestart")));
104     log("Enabling table\n");
105     // Need a new Admin, the previous one is on the old master
106     HBaseAdmin 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 = getAllOnlineRegions(cluster);
113     assertEquals(
114         "The assigned regions were not onlined after master 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().getZKTable()
118         .isEnabledTable(TableName.valueOf("tableRestart")));
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   private NavigableSet<String> getAllOnlineRegions(MiniHBaseCluster cluster)
134       throws IOException {
135     NavigableSet<String> online = new TreeSet<String>();
136     for (RegionServerThread rst : cluster.getLiveRegionServerThreads()) {
137       for (HRegionInfo region : ProtobufUtil.getOnlineRegions(rst.getRegionServer())) {
138         online.add(region.getRegionNameAsString());
139       }
140     }
141     return online;
142   }
143 
144 }
145