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.testclassification.MediumTests;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.apache.hadoop.hbase.util.MockServer;
41  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
42  import org.apache.hadoop.hbase.zookeeper.ZKTable;
43  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
44  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
45  import org.apache.zookeeper.KeeperException;
46  import org.apache.zookeeper.data.Stat;
47  import org.junit.After;
48  import org.junit.Before;
49  import org.junit.Test;
50  import org.junit.experimental.categories.Category;
51  import org.mockito.Mockito;
52  
53  @Category(MediumTests.class)
54  public class TestOpenedRegionHandler {
55  
56    private static final Log LOG = LogFactory
57        .getLog(TestOpenedRegionHandler.class);
58  
59    private HBaseTestingUtility TEST_UTIL;
60    private final int NUM_MASTERS = 1;
61    private final int NUM_RS = 1;
62    private Configuration conf;
63    private Configuration resetConf;
64    private ZooKeeperWatcher zkw;
65  
66    @Before
67    public void setUp() throws Exception {
68      conf = HBaseConfiguration.create();
69      conf.setBoolean("hbase.assignment.usezk", true);
70      TEST_UTIL = HBaseTestingUtility.createLocalHTU(conf);
71    }
72  
73    @After
74    public void tearDown() throws Exception {
75      // Stop the cluster
76      TEST_UTIL.shutdownMiniCluster();
77      TEST_UTIL = new HBaseTestingUtility(resetConf);
78    }
79  
80    @Test
81    public void testOpenedRegionHandlerOnMasterRestart() throws Exception {
82      // Start the cluster
83      log("Starting cluster");
84      conf = HBaseConfiguration.create();
85      conf.setBoolean("hbase.assignment.usezk", true);
86      resetConf = conf;
87      conf.setInt("hbase.master.assignment.timeoutmonitor.period", 2000);
88      conf.setInt("hbase.master.assignment.timeoutmonitor.timeout", 5000);
89      TEST_UTIL = new HBaseTestingUtility(conf);
90      TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
91      String tableName = "testOpenedRegionHandlerOnMasterRestart";
92      MiniHBaseCluster cluster = createRegions(tableName);
93      abortMaster(cluster);
94  
95      HRegionServer regionServer = cluster.getRegionServer(0);
96      HRegion region = getRegionBeingServed(cluster, regionServer);
97  
98      // forcefully move a region to OPENED state in zk
99      // Create a ZKW to use in the test
100     zkw = HBaseTestingUtility.createAndForceNodeToOpenedState(TEST_UTIL,
101         region, regionServer.getServerName());
102 
103     // Start up a new master
104     log("Starting up a new master");
105     cluster.startMaster().getMaster();
106     log("Waiting for master to be ready");
107     cluster.waitForActiveAndReadyMaster();
108     log("Master is ready");
109 
110     // Failover should be completed, now wait for no RIT
111     log("Waiting for no more RIT");
112     ZKAssign.blockUntilNoRIT(zkw);
113   }
114   @Test
115   public void testShouldNotCompeleteOpenedRegionSuccessfullyIfVersionMismatches()
116       throws Exception {
117     HRegion region = null;
118     try {
119       int testIndex = 0;
120       TEST_UTIL.startMiniZKCluster();
121       final Server server = new MockServer(TEST_UTIL);
122       HTableDescriptor htd = new HTableDescriptor(
123           TableName.valueOf("testShouldNotCompeleteOpenedRegionSuccessfullyIfVersionMismatches"));
124       HRegionInfo hri = new HRegionInfo(htd.getTableName(),
125           Bytes.toBytes(testIndex), Bytes.toBytes(testIndex + 1));
126       region = HRegion.createHRegion(hri, TEST_UTIL.getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
127       assertNotNull(region);
128       AssignmentManager am = Mockito.mock(AssignmentManager.class);
129       RegionStates rsm = Mockito.mock(RegionStates.class);
130       Mockito.doReturn(rsm).when(am).getRegionStates();
131       when(rsm.isRegionInTransition(hri)).thenReturn(false);
132       when(rsm.getRegionState(hri)).thenReturn(
133         new RegionState(region.getRegionInfo(), RegionState.State.OPEN,
134           System.currentTimeMillis(), server.getServerName()));
135       // create a node with OPENED state
136       zkw = HBaseTestingUtility.createAndForceNodeToOpenedState(TEST_UTIL,
137           region, server.getServerName());
138       when(am.getZKTable()).thenReturn(new ZKTable(zkw));
139       Stat stat = new Stat();
140       String nodeName = ZKAssign.getNodeName(zkw, region.getRegionInfo()
141           .getEncodedName());
142       ZKUtil.getDataAndWatch(zkw, nodeName, stat);
143 
144       // use the version for the OpenedRegionHandler
145       OpenedRegionHandler handler = new OpenedRegionHandler(server, am, region
146           .getRegionInfo(), server.getServerName(), stat.getVersion());
147       // Once again overwrite the same znode so that the version changes.
148       ZKAssign.transitionNode(zkw, region.getRegionInfo(), server
149           .getServerName(), EventType.RS_ZK_REGION_OPENED,
150           EventType.RS_ZK_REGION_OPENED, stat.getVersion());
151 
152       // Should not invoke assignmentmanager.regionOnline. If it is 
153       // invoked as per current mocking it will throw null pointer exception.
154       boolean expectedException = false;
155       try {
156         handler.process();
157       } catch (Exception e) {
158         expectedException = true;
159       }
160       assertFalse("The process method should not throw any exception.",
161           expectedException);
162       List<String> znodes = ZKUtil.listChildrenAndWatchForNewChildren(zkw,
163           zkw.assignmentZNode);
164       String regionName = znodes.get(0);
165       assertEquals("The region should not be opened successfully.", regionName,
166           region.getRegionInfo().getEncodedName());
167     } finally {
168       HRegion.closeHRegion(region);
169       TEST_UTIL.shutdownMiniZKCluster();
170     }
171   }
172   private MiniHBaseCluster createRegions(String tableName)
173       throws InterruptedException, ZooKeeperConnectionException, IOException,
174       KeeperException {
175     MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
176     log("Waiting for active/ready master");
177     cluster.waitForActiveAndReadyMaster();
178     zkw = new ZooKeeperWatcher(conf, "testOpenedRegionHandler", null);
179 
180     // Create a table with regions
181     byte[] table = Bytes.toBytes(tableName);
182     byte[] family = Bytes.toBytes("family");
183     TEST_UTIL.createTable(table, family);
184 
185     //wait till the regions are online
186     log("Waiting for no more RIT");
187     ZKAssign.blockUntilNoRIT(zkw);
188 
189     return cluster;
190   }
191   private void abortMaster(MiniHBaseCluster cluster) {
192     // Stop the master
193     log("Aborting master");
194     cluster.abortMaster(0);
195     cluster.waitOnMaster(0);
196     log("Master has aborted");
197   }
198   private HRegion getRegionBeingServed(MiniHBaseCluster cluster,
199       HRegionServer regionServer) {
200     Collection<HRegion> onlineRegionsLocalContext = regionServer
201         .getOnlineRegionsLocalContext();
202     Iterator<HRegion> iterator = onlineRegionsLocalContext.iterator();
203     HRegion region = null;
204     while (iterator.hasNext()) {
205       region = iterator.next();
206       if (!region.getRegionInfo().isMetaTable()) {
207         break;
208       }
209     }
210     return region;
211   }
212   private void log(String msg) {
213     LOG.debug("\n\nTRR: " + msg + "\n");
214   }
215 
216 }
217