001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.util.concurrent.Semaphore; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileSystem; 028import org.apache.hadoop.hbase.ChoreService; 029import org.apache.hadoop.hbase.CoordinatedStateManager; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.Server; 033import org.apache.hadoop.hbase.ServerName; 034import org.apache.hadoop.hbase.client.ClusterConnection; 035import org.apache.hadoop.hbase.client.Connection; 036import org.apache.hadoop.hbase.monitoring.MonitoredTask; 037import org.apache.hadoop.hbase.testclassification.MasterTests; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.apache.hadoop.hbase.zookeeper.ClusterStatusTracker; 040import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker; 041import org.apache.hadoop.hbase.zookeeper.MetaTableLocator; 042import org.apache.hadoop.hbase.zookeeper.ZKListener; 043import org.apache.hadoop.hbase.zookeeper.ZKUtil; 044import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 045import org.apache.zookeeper.KeeperException; 046import org.junit.AfterClass; 047import org.junit.BeforeClass; 048import org.junit.ClassRule; 049import org.junit.Test; 050import org.junit.experimental.categories.Category; 051import org.mockito.Mockito; 052import org.slf4j.Logger; 053import org.slf4j.LoggerFactory; 054 055/** 056 * Test the {@link ActiveMasterManager}. 057 */ 058@Category({MasterTests.class, MediumTests.class}) 059public class TestActiveMasterManager { 060 061 @ClassRule 062 public static final HBaseClassTestRule CLASS_RULE = 063 HBaseClassTestRule.forClass(TestActiveMasterManager.class); 064 065 private final static Logger LOG = LoggerFactory.getLogger(TestActiveMasterManager.class); 066 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 067 068 @BeforeClass 069 public static void setUpBeforeClass() throws Exception { 070 TEST_UTIL.startMiniZKCluster(); 071 } 072 073 @AfterClass 074 public static void tearDownAfterClass() throws Exception { 075 TEST_UTIL.shutdownMiniZKCluster(); 076 } 077 078 @Test public void testRestartMaster() throws IOException, KeeperException { 079 ZKWatcher zk = new ZKWatcher(TEST_UTIL.getConfiguration(), 080 "testActiveMasterManagerFromZK", null, true); 081 try { 082 ZKUtil.deleteNode(zk, zk.znodePaths.masterAddressZNode); 083 ZKUtil.deleteNode(zk, zk.znodePaths.clusterStateZNode); 084 } catch(KeeperException.NoNodeException nne) {} 085 086 // Create the master node with a dummy address 087 ServerName master = ServerName.valueOf("localhost", 1, System.currentTimeMillis()); 088 // Should not have a master yet 089 DummyMaster dummyMaster = new DummyMaster(zk,master); 090 ClusterStatusTracker clusterStatusTracker = 091 dummyMaster.getClusterStatusTracker(); 092 ActiveMasterManager activeMasterManager = 093 dummyMaster.getActiveMasterManager(); 094 assertFalse(activeMasterManager.clusterHasActiveMaster.get()); 095 096 // First test becoming the active master uninterrupted 097 MonitoredTask status = Mockito.mock(MonitoredTask.class); 098 clusterStatusTracker.setClusterUp(); 099 100 activeMasterManager.blockUntilBecomingActiveMaster(100, status); 101 assertTrue(activeMasterManager.clusterHasActiveMaster.get()); 102 assertMaster(zk, master); 103 104 // Now pretend master restart 105 DummyMaster secondDummyMaster = new DummyMaster(zk,master); 106 ActiveMasterManager secondActiveMasterManager = 107 secondDummyMaster.getActiveMasterManager(); 108 assertFalse(secondActiveMasterManager.clusterHasActiveMaster.get()); 109 activeMasterManager.blockUntilBecomingActiveMaster(100, status); 110 assertTrue(activeMasterManager.clusterHasActiveMaster.get()); 111 assertMaster(zk, master); 112 } 113 114 /** 115 * Unit tests that uses ZooKeeper but does not use the master-side methods 116 * but rather acts directly on ZK. 117 * @throws Exception 118 */ 119 @Test 120 public void testActiveMasterManagerFromZK() throws Exception { 121 ZKWatcher zk = new ZKWatcher(TEST_UTIL.getConfiguration(), 122 "testActiveMasterManagerFromZK", null, true); 123 try { 124 ZKUtil.deleteNode(zk, zk.znodePaths.masterAddressZNode); 125 ZKUtil.deleteNode(zk, zk.znodePaths.clusterStateZNode); 126 } catch(KeeperException.NoNodeException nne) {} 127 128 // Create the master node with a dummy address 129 ServerName firstMasterAddress = 130 ServerName.valueOf("localhost", 1, System.currentTimeMillis()); 131 ServerName secondMasterAddress = 132 ServerName.valueOf("localhost", 2, System.currentTimeMillis()); 133 134 // Should not have a master yet 135 DummyMaster ms1 = new DummyMaster(zk,firstMasterAddress); 136 ActiveMasterManager activeMasterManager = 137 ms1.getActiveMasterManager(); 138 assertFalse(activeMasterManager.clusterHasActiveMaster.get()); 139 140 // First test becoming the active master uninterrupted 141 ClusterStatusTracker clusterStatusTracker = 142 ms1.getClusterStatusTracker(); 143 clusterStatusTracker.setClusterUp(); 144 activeMasterManager.blockUntilBecomingActiveMaster(100, 145 Mockito.mock(MonitoredTask.class)); 146 assertTrue(activeMasterManager.clusterHasActiveMaster.get()); 147 assertMaster(zk, firstMasterAddress); 148 149 // New manager will now try to become the active master in another thread 150 WaitToBeMasterThread t = new WaitToBeMasterThread(zk, secondMasterAddress); 151 t.start(); 152 // Wait for this guy to figure out there is another active master 153 // Wait for 1 second at most 154 int sleeps = 0; 155 while(!t.manager.clusterHasActiveMaster.get() && sleeps < 100) { 156 Thread.sleep(10); 157 sleeps++; 158 } 159 160 // Both should see that there is an active master 161 assertTrue(activeMasterManager.clusterHasActiveMaster.get()); 162 assertTrue(t.manager.clusterHasActiveMaster.get()); 163 // But secondary one should not be the active master 164 assertFalse(t.isActiveMaster); 165 166 // Close the first server and delete it's master node 167 ms1.stop("stopping first server"); 168 169 // Use a listener to capture when the node is actually deleted 170 NodeDeletionListener listener = new NodeDeletionListener(zk, zk.znodePaths.masterAddressZNode); 171 zk.registerListener(listener); 172 173 LOG.info("Deleting master node"); 174 ZKUtil.deleteNode(zk, zk.znodePaths.masterAddressZNode); 175 176 // Wait for the node to be deleted 177 LOG.info("Waiting for active master manager to be notified"); 178 listener.waitForDeletion(); 179 LOG.info("Master node deleted"); 180 181 // Now we expect the secondary manager to have and be the active master 182 // Wait for 1 second at most 183 sleeps = 0; 184 while(!t.isActiveMaster && sleeps < 100) { 185 Thread.sleep(10); 186 sleeps++; 187 } 188 LOG.debug("Slept " + sleeps + " times"); 189 190 assertTrue(t.manager.clusterHasActiveMaster.get()); 191 assertTrue(t.isActiveMaster); 192 193 LOG.info("Deleting master node"); 194 ZKUtil.deleteNode(zk, zk.znodePaths.masterAddressZNode); 195 } 196 197 /** 198 * Assert there is an active master and that it has the specified address. 199 * @param zk 200 * @param thisMasterAddress 201 * @throws KeeperException 202 * @throws IOException 203 */ 204 private void assertMaster(ZKWatcher zk, 205 ServerName expectedAddress) 206 throws KeeperException, IOException { 207 ServerName readAddress = MasterAddressTracker.getMasterAddress(zk); 208 assertNotNull(readAddress); 209 assertTrue(expectedAddress.equals(readAddress)); 210 } 211 212 public static class WaitToBeMasterThread extends Thread { 213 214 ActiveMasterManager manager; 215 DummyMaster dummyMaster; 216 boolean isActiveMaster; 217 218 public WaitToBeMasterThread(ZKWatcher zk, ServerName address) { 219 this.dummyMaster = new DummyMaster(zk,address); 220 this.manager = this.dummyMaster.getActiveMasterManager(); 221 isActiveMaster = false; 222 } 223 224 @Override 225 public void run() { 226 manager.blockUntilBecomingActiveMaster(100, 227 Mockito.mock(MonitoredTask.class)); 228 LOG.info("Second master has become the active master!"); 229 isActiveMaster = true; 230 } 231 } 232 233 public static class NodeDeletionListener extends ZKListener { 234 private static final Logger LOG = LoggerFactory.getLogger(NodeDeletionListener.class); 235 236 private Semaphore lock; 237 private String node; 238 239 public NodeDeletionListener(ZKWatcher watcher, String node) { 240 super(watcher); 241 lock = new Semaphore(0); 242 this.node = node; 243 } 244 245 @Override 246 public void nodeDeleted(String path) { 247 if(path.equals(node)) { 248 LOG.debug("nodeDeleted(" + path + ")"); 249 lock.release(); 250 } 251 } 252 253 public void waitForDeletion() throws InterruptedException { 254 lock.acquire(); 255 } 256 } 257 258 /** 259 * Dummy Master Implementation. 260 */ 261 public static class DummyMaster implements Server { 262 private volatile boolean stopped; 263 private ClusterStatusTracker clusterStatusTracker; 264 private ActiveMasterManager activeMasterManager; 265 266 public DummyMaster(ZKWatcher zk, ServerName master) { 267 this.clusterStatusTracker = 268 new ClusterStatusTracker(zk, this); 269 clusterStatusTracker.start(); 270 271 this.activeMasterManager = 272 new ActiveMasterManager(zk, master, this); 273 zk.registerListener(activeMasterManager); 274 } 275 276 @Override 277 public void abort(final String msg, final Throwable t) {} 278 279 @Override 280 public boolean isAborted() { 281 return false; 282 } 283 284 @Override 285 public Configuration getConfiguration() { 286 return null; 287 } 288 289 @Override 290 public ZKWatcher getZooKeeper() { 291 return null; 292 } 293 294 @Override 295 public CoordinatedStateManager getCoordinatedStateManager() { 296 return null; 297 } 298 299 @Override 300 public ServerName getServerName() { 301 return null; 302 } 303 304 @Override 305 public boolean isStopped() { 306 return this.stopped; 307 } 308 309 @Override 310 public void stop(String why) { 311 this.stopped = true; 312 } 313 314 @Override 315 public ClusterConnection getConnection() { 316 return null; 317 } 318 319 @Override 320 public MetaTableLocator getMetaTableLocator() { 321 return null; 322 } 323 324 public ClusterStatusTracker getClusterStatusTracker() { 325 return clusterStatusTracker; 326 } 327 328 public ActiveMasterManager getActiveMasterManager() { 329 return activeMasterManager; 330 } 331 332 @Override 333 public ChoreService getChoreService() { 334 return null; 335 } 336 337 @Override 338 public ClusterConnection getClusterConnection() { 339 // TODO Auto-generated method stub 340 return null; 341 } 342 343 @Override 344 public FileSystem getFileSystem() { 345 return null; 346 } 347 348 @Override 349 public boolean isStopping() { 350 return false; 351 } 352 353 @Override 354 public Connection createConnection(Configuration conf) throws IOException { 355 return null; 356 } 357 } 358}