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