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.HBaseConfiguration;
34  import org.apache.hadoop.hbase.HBaseTestingUtility;
35  import org.apache.hadoop.hbase.HRegionInfo;
36  import org.apache.hadoop.hbase.HTableDescriptor;
37  import org.apache.hadoop.hbase.MiniHBaseCluster;
38  import org.apache.hadoop.hbase.Server;
39  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
40  import org.apache.hadoop.hbase.executor.EventHandler.EventType;
41  import org.apache.hadoop.hbase.master.AssignmentManager.RegionState;
42  import org.apache.hadoop.hbase.master.handler.OpenedRegionHandler;
43  import org.apache.hadoop.hbase.regionserver.HRegion;
44  import org.apache.hadoop.hbase.regionserver.HRegionServer;
45  import org.apache.hadoop.hbase.util.Bytes;
46  import org.apache.hadoop.hbase.util.MockServer;
47  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
48  import org.apache.hadoop.hbase.zookeeper.ZKTable;
49  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
50  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
51  import org.apache.zookeeper.KeeperException;
52  import org.apache.zookeeper.data.Stat;
53  import org.junit.After;
54  import org.junit.Before;
55  import org.junit.Test;
56  import org.mockito.Mockito;
57  
58  public class TestOpenedRegionHandler {
59  
60    private static final Log LOG = LogFactory
61        .getLog(TestOpenedRegionHandler.class);
62  
63    private HBaseTestingUtility TEST_UTIL;
64    private final int NUM_MASTERS = 1;
65    private final int NUM_RS = 1;
66    private Configuration conf;
67    private Configuration resetConf;
68    private ZooKeeperWatcher zkw;
69  
70    @Before
71    public void setUp() throws Exception {
72      conf = HBaseConfiguration.create();
73      TEST_UTIL = new HBaseTestingUtility(conf);
74    }
75    
76    @After
77    public void tearDown() throws Exception {
78      // Stop the cluster
79      TEST_UTIL.shutdownMiniCluster();
80      TEST_UTIL = new HBaseTestingUtility(resetConf);
81    }
82  
83    @Test
84    public void testOpenedRegionHandlerOnMasterRestart() throws Exception {
85      // Start the cluster
86      log("Starting cluster");
87      conf = HBaseConfiguration.create();
88      resetConf = conf;
89      conf.setInt("hbase.master.assignment.timeoutmonitor.period", 2000);
90      conf.setInt("hbase.master.assignment.timeoutmonitor.timeout", 5000);
91      TEST_UTIL = new HBaseTestingUtility(conf);
92      TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
93      String tableName = "testOpenedRegionHandlerOnMasterRestart";
94      MiniHBaseCluster cluster = createRegions(tableName);
95      abortMaster(cluster);
96  
97      HRegionServer regionServer = cluster.getRegionServer(0);
98      HRegion region = getRegionBeingServed(cluster, regionServer);
99  
100     // forcefully move a region to OPENED state in zk
101     // Create a ZKW to use in the test
102     zkw = HBaseTestingUtility.createAndForceNodeToOpenedState(TEST_UTIL,
103         region, regionServer.getServerName());
104 
105     // Start up a new master
106     log("Starting up a new master");
107     cluster.startMaster().getMaster();
108     log("Waiting for master to be ready");
109     cluster.waitForActiveAndReadyMaster();
110     log("Master is ready");
111 
112     // Failover should be completed, now wait for no RIT
113     log("Waiting for no more RIT");
114     ZKAssign.blockUntilNoRIT(zkw);
115   }
116   @Test
117   public void testShouldNotCompeleteOpenedRegionSuccessfullyIfVersionMismatches()
118       throws Exception {
119     try {
120       int testIndex = 0;
121       TEST_UTIL.startMiniZKCluster();
122       final Server server = new MockServer(TEST_UTIL);
123       HTableDescriptor htd = new HTableDescriptor(
124           "testShouldNotCompeleteOpenedRegionSuccessfullyIfVersionMismatches");
125       HRegionInfo hri = new HRegionInfo(htd.getName(),
126           Bytes.toBytes(testIndex), Bytes.toBytes(testIndex + 1));
127       HRegion region = HRegion.createHRegion(hri, TEST_UTIL
128           .getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
129       assertNotNull(region);
130       AssignmentManager am = Mockito.mock(AssignmentManager.class);
131       when(am.isRegionInTransition(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       TEST_UTIL.shutdownMiniZKCluster();
168     }
169   }
170   private MiniHBaseCluster createRegions(String tableName)
171       throws InterruptedException, ZooKeeperConnectionException, IOException,
172       KeeperException {
173     MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
174     log("Waiting for active/ready master");
175     cluster.waitForActiveAndReadyMaster();
176     zkw = new ZooKeeperWatcher(conf, "testOpenedRegionHandler", null);
177 
178     // Create a table with regions
179     byte[] table = Bytes.toBytes(tableName);
180     byte[] family = Bytes.toBytes("family");
181     TEST_UTIL.createTable(table, family);
182 
183     //wait till the regions are online
184     log("Waiting for no more RIT");
185     ZKAssign.blockUntilNoRIT(zkw);
186 
187     return cluster;
188   }
189   private void abortMaster(MiniHBaseCluster cluster) {
190     // Stop the master
191     log("Aborting master");
192     cluster.abortMaster(0);
193     cluster.waitOnMaster(0);
194     log("Master has aborted");
195   }
196   private HRegion getRegionBeingServed(MiniHBaseCluster cluster,
197       HRegionServer regionServer) {
198     Collection<HRegion> onlineRegionsLocalContext = regionServer
199         .getOnlineRegionsLocalContext();
200     Iterator<HRegion> iterator = onlineRegionsLocalContext.iterator();
201     HRegion region = null;
202     while (iterator.hasNext()) {
203       region = iterator.next();
204       if (!region.getRegionInfo().isMetaTable()) {
205         break;
206       }
207     }
208     return region;
209   }
210   private void log(String msg) {
211     LOG.debug("\n\nTRR: " + msg + "\n");
212   }
213 }