View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.mockito.Mockito.when;
24  
25  import java.io.IOException;
26  import java.util.Collection;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.hbase.*;
34  import org.apache.hadoop.hbase.executor.EventType;
35  import org.apache.hadoop.hbase.master.handler.OpenedRegionHandler;
36  import org.apache.hadoop.hbase.regionserver.HRegion;
37  import org.apache.hadoop.hbase.regionserver.HRegionServer;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.util.MockServer;
40  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
41  import org.apache.hadoop.hbase.zookeeper.ZKTable;
42  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
43  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
44  import org.apache.zookeeper.KeeperException;
45  import org.apache.zookeeper.data.Stat;
46  import org.junit.After;
47  import org.junit.Before;
48  import org.junit.Test;
49  import org.junit.experimental.categories.Category;
50  import org.mockito.Mockito;
51  
52  @Category(MediumTests.class)
53  public class TestOpenedRegionHandler {
54  
55    private static final Log LOG = LogFactory
56        .getLog(TestOpenedRegionHandler.class);
57  
58    private HBaseTestingUtility TEST_UTIL;
59    private final int NUM_MASTERS = 1;
60    private final int NUM_RS = 1;
61    private Configuration conf;
62    private Configuration resetConf;
63    private ZooKeeperWatcher zkw;
64  
65    @Before
66    public void setUp() throws Exception {
67      conf = HBaseConfiguration.create();
68      TEST_UTIL = HBaseTestingUtility.createLocalHTU(conf);
69    }
70  
71    @After
72    public void tearDown() throws Exception {
73      // Stop the cluster
74      TEST_UTIL.shutdownMiniCluster();
75      TEST_UTIL = new HBaseTestingUtility(resetConf);
76    }
77  
78    @Test
79    public void testOpenedRegionHandlerOnMasterRestart() throws Exception {
80      // Start the cluster
81      log("Starting cluster");
82      conf = HBaseConfiguration.create();
83      resetConf = conf;
84      conf.setInt("hbase.master.assignment.timeoutmonitor.period", 2000);
85      conf.setInt("hbase.master.assignment.timeoutmonitor.timeout", 5000);
86      TEST_UTIL = new HBaseTestingUtility(conf);
87      TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
88      String tableName = "testOpenedRegionHandlerOnMasterRestart";
89      MiniHBaseCluster cluster = createRegions(tableName);
90      abortMaster(cluster);
91  
92      HRegionServer regionServer = cluster.getRegionServer(0);
93      HRegion region = getRegionBeingServed(cluster, regionServer);
94  
95      // forcefully move a region to OPENED state in zk
96      // Create a ZKW to use in the test
97      zkw = HBaseTestingUtility.createAndForceNodeToOpenedState(TEST_UTIL,
98          region, regionServer.getServerName());
99  
100     // Start up a new master
101     log("Starting up a new master");
102     cluster.startMaster().getMaster();
103     log("Waiting for master to be ready");
104     cluster.waitForActiveAndReadyMaster();
105     log("Master is ready");
106 
107     // Failover should be completed, now wait for no RIT
108     log("Waiting for no more RIT");
109     ZKAssign.blockUntilNoRIT(zkw);
110   }
111   @Test
112   public void testShouldNotCompeleteOpenedRegionSuccessfullyIfVersionMismatches()
113       throws Exception {
114     HRegion region = null;
115     try {
116       int testIndex = 0;
117       TEST_UTIL.startMiniZKCluster();
118       final Server server = new MockServer(TEST_UTIL);
119       HTableDescriptor htd = new HTableDescriptor(
120           TableName.valueOf("testShouldNotCompeleteOpenedRegionSuccessfullyIfVersionMismatches"));
121       HRegionInfo hri = new HRegionInfo(htd.getTableName(),
122           Bytes.toBytes(testIndex), Bytes.toBytes(testIndex + 1));
123       region = HRegion.createHRegion(hri, TEST_UTIL.getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
124       assertNotNull(region);
125       AssignmentManager am = Mockito.mock(AssignmentManager.class);
126       RegionStates rsm = Mockito.mock(RegionStates.class);
127       Mockito.doReturn(rsm).when(am).getRegionStates();
128       when(rsm.isRegionInTransition(hri)).thenReturn(false);
129       when(rsm.getRegionState(hri)).thenReturn(
130         new RegionState(region.getRegionInfo(), RegionState.State.OPEN,
131           System.currentTimeMillis(), server.getServerName()));
132       // create a node with OPENED state
133       zkw = HBaseTestingUtility.createAndForceNodeToOpenedState(TEST_UTIL,
134           region, server.getServerName());
135       when(am.getZKTable()).thenReturn(new ZKTable(zkw));
136       Stat stat = new Stat();
137       String nodeName = ZKAssign.getNodeName(zkw, region.getRegionInfo()
138           .getEncodedName());
139       ZKUtil.getDataAndWatch(zkw, nodeName, stat);
140 
141       // use the version for the OpenedRegionHandler
142       OpenedRegionHandler handler = new OpenedRegionHandler(server, am, region
143           .getRegionInfo(), server.getServerName(), stat.getVersion());
144       // Once again overwrite the same znode so that the version changes.
145       ZKAssign.transitionNode(zkw, region.getRegionInfo(), server
146           .getServerName(), EventType.RS_ZK_REGION_OPENED,
147           EventType.RS_ZK_REGION_OPENED, stat.getVersion());
148 
149       // Should not invoke assignmentmanager.regionOnline. If it is 
150       // invoked as per current mocking it will throw null pointer exception.
151       boolean expectedException = false;
152       try {
153         handler.process();
154       } catch (Exception e) {
155         expectedException = true;
156       }
157       assertFalse("The process method should not throw any exception.",
158           expectedException);
159       List<String> znodes = ZKUtil.listChildrenAndWatchForNewChildren(zkw,
160           zkw.assignmentZNode);
161       String regionName = znodes.get(0);
162       assertEquals("The region should not be opened successfully.", regionName,
163           region.getRegionInfo().getEncodedName());
164     } finally {
165       HRegion.closeHRegion(region);
166       TEST_UTIL.shutdownMiniZKCluster();
167     }
168   }
169   private MiniHBaseCluster createRegions(String tableName)
170       throws InterruptedException, ZooKeeperConnectionException, IOException,
171       KeeperException {
172     MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
173     log("Waiting for active/ready master");
174     cluster.waitForActiveAndReadyMaster();
175     zkw = new ZooKeeperWatcher(conf, "testOpenedRegionHandler", null);
176 
177     // Create a table with regions
178     byte[] table = Bytes.toBytes(tableName);
179     byte[] family = Bytes.toBytes("family");
180     TEST_UTIL.createTable(table, family);
181 
182     //wait till the regions are online
183     log("Waiting for no more RIT");
184     ZKAssign.blockUntilNoRIT(zkw);
185 
186     return cluster;
187   }
188   private void abortMaster(MiniHBaseCluster cluster) {
189     // Stop the master
190     log("Aborting master");
191     cluster.abortMaster(0);
192     cluster.waitOnMaster(0);
193     log("Master has aborted");
194   }
195   private HRegion getRegionBeingServed(MiniHBaseCluster cluster,
196       HRegionServer regionServer) {
197     Collection<HRegion> onlineRegionsLocalContext = regionServer
198         .getOnlineRegionsLocalContext();
199     Iterator<HRegion> iterator = onlineRegionsLocalContext.iterator();
200     HRegion region = null;
201     while (iterator.hasNext()) {
202       region = iterator.next();
203       if (!region.getRegionInfo().isMetaTable()) {
204         break;
205       }
206     }
207     return region;
208   }
209   private void log(String msg) {
210     LOG.debug("\n\nTRR: " + msg + "\n");
211   }
212 
213 }
214