View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.concurrent.Semaphore;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.ChoreService;
32  import org.apache.hadoop.hbase.CoordinatedStateManager;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.apache.hadoop.hbase.Server;
36  import org.apache.hadoop.hbase.ServerName;
37  import org.apache.hadoop.hbase.client.ClusterConnection;
38  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
39  import org.apache.hadoop.hbase.zookeeper.ClusterStatusTracker;
40  import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
41  import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
42  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
43  import org.apache.hadoop.hbase.zookeeper.ZooKeeperListener;
44  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
45  import org.apache.zookeeper.KeeperException;
46  import org.junit.AfterClass;
47  import org.junit.BeforeClass;
48  import org.junit.Test;
49  import org.junit.experimental.categories.Category;
50  import org.mockito.Mockito;
51  
52  /**
53   * Test the {@link ActiveMasterManager}.
54   */
55  @Category(MediumTests.class)
56  public class TestActiveMasterManager {
57    private final static Log LOG = LogFactory.getLog(TestActiveMasterManager.class);
58    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
59  
60    @BeforeClass
61    public static void setUpBeforeClass() throws Exception {
62      TEST_UTIL.startMiniZKCluster();
63    }
64  
65    @AfterClass
66    public static void tearDownAfterClass() throws Exception {
67      TEST_UTIL.shutdownMiniZKCluster();
68    }
69  
70    @Test public void testRestartMaster() throws IOException, KeeperException {
71      ZooKeeperWatcher zk = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
72        "testActiveMasterManagerFromZK", null, true);
73      try {
74        ZKUtil.deleteNode(zk, zk.getMasterAddressZNode());
75        ZKUtil.deleteNode(zk, zk.clusterStateZNode);
76      } catch(KeeperException.NoNodeException nne) {}
77  
78      // Create the master node with a dummy address
79      ServerName master = ServerName.valueOf("localhost", 1, System.currentTimeMillis());
80      // Should not have a master yet
81      DummyMaster dummyMaster = new DummyMaster(zk,master);
82      ClusterStatusTracker clusterStatusTracker =
83        dummyMaster.getClusterStatusTracker();
84      ActiveMasterManager activeMasterManager =
85        dummyMaster.getActiveMasterManager();
86      assertFalse(activeMasterManager.clusterHasActiveMaster.get());
87  
88      // First test becoming the active master uninterrupted
89      MonitoredTask status = Mockito.mock(MonitoredTask.class);
90      clusterStatusTracker.setClusterUp();
91  
92      activeMasterManager.blockUntilBecomingActiveMaster(100, status);
93      assertTrue(activeMasterManager.clusterHasActiveMaster.get());
94      assertMaster(zk, master);
95  
96      // Now pretend master restart
97      DummyMaster secondDummyMaster = new DummyMaster(zk,master);
98      ActiveMasterManager secondActiveMasterManager =
99        secondDummyMaster.getActiveMasterManager();
100     assertFalse(secondActiveMasterManager.clusterHasActiveMaster.get());
101     activeMasterManager.blockUntilBecomingActiveMaster(100, status);
102     assertTrue(activeMasterManager.clusterHasActiveMaster.get());
103     assertMaster(zk, master);
104   }
105 
106   /**
107    * Unit tests that uses ZooKeeper but does not use the master-side methods
108    * but rather acts directly on ZK.
109    * @throws Exception
110    */
111   @Test
112   public void testActiveMasterManagerFromZK() throws Exception {
113     ZooKeeperWatcher zk = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
114       "testActiveMasterManagerFromZK", null, true);
115     try {
116       ZKUtil.deleteNode(zk, zk.getMasterAddressZNode());
117       ZKUtil.deleteNode(zk, zk.clusterStateZNode);
118     } catch(KeeperException.NoNodeException nne) {}
119 
120     // Create the master node with a dummy address
121     ServerName firstMasterAddress =
122         ServerName.valueOf("localhost", 1, System.currentTimeMillis());
123     ServerName secondMasterAddress =
124         ServerName.valueOf("localhost", 2, System.currentTimeMillis());
125 
126     // Should not have a master yet
127     DummyMaster ms1 = new DummyMaster(zk,firstMasterAddress);
128     ActiveMasterManager activeMasterManager =
129       ms1.getActiveMasterManager();
130     assertFalse(activeMasterManager.clusterHasActiveMaster.get());
131 
132     // First test becoming the active master uninterrupted
133     ClusterStatusTracker clusterStatusTracker =
134       ms1.getClusterStatusTracker();
135     clusterStatusTracker.setClusterUp();
136     activeMasterManager.blockUntilBecomingActiveMaster(100,
137         Mockito.mock(MonitoredTask.class));
138     assertTrue(activeMasterManager.clusterHasActiveMaster.get());
139     assertMaster(zk, firstMasterAddress);
140 
141     // New manager will now try to become the active master in another thread
142     WaitToBeMasterThread t = new WaitToBeMasterThread(zk, secondMasterAddress);
143     t.start();
144     // Wait for this guy to figure out there is another active master
145     // Wait for 1 second at most
146     int sleeps = 0;
147     while(!t.manager.clusterHasActiveMaster.get() && sleeps < 100) {
148       Thread.sleep(10);
149       sleeps++;
150     }
151 
152     // Both should see that there is an active master
153     assertTrue(activeMasterManager.clusterHasActiveMaster.get());
154     assertTrue(t.manager.clusterHasActiveMaster.get());
155     // But secondary one should not be the active master
156     assertFalse(t.isActiveMaster);
157 
158     // Close the first server and delete it's master node
159     ms1.stop("stopping first server");
160 
161     // Use a listener to capture when the node is actually deleted
162     NodeDeletionListener listener = new NodeDeletionListener(zk, zk.getMasterAddressZNode());
163     zk.registerListener(listener);
164 
165     LOG.info("Deleting master node");
166     ZKUtil.deleteNode(zk, zk.getMasterAddressZNode());
167 
168     // Wait for the node to be deleted
169     LOG.info("Waiting for active master manager to be notified");
170     listener.waitForDeletion();
171     LOG.info("Master node deleted");
172 
173     // Now we expect the secondary manager to have and be the active master
174     // Wait for 1 second at most
175     sleeps = 0;
176     while(!t.isActiveMaster && sleeps < 100) {
177       Thread.sleep(10);
178       sleeps++;
179     }
180     LOG.debug("Slept " + sleeps + " times");
181 
182     assertTrue(t.manager.clusterHasActiveMaster.get());
183     assertTrue(t.isActiveMaster);
184 
185     LOG.info("Deleting master node");
186 
187     ZKUtil.deleteNode(zk, zk.getMasterAddressZNode());
188   }
189 
190   /**
191    * Assert there is an active master and that it has the specified address.
192    * @param zk single Zookeeper watcher
193    * @param expectedAddress the expected address of the master
194    * @throws KeeperException unexpected Zookeeper exception
195    * @throws IOException if an IO problem is encountered
196    */
197   private void assertMaster(ZooKeeperWatcher zk,
198       ServerName expectedAddress)
199   throws KeeperException, IOException {
200     ServerName readAddress = MasterAddressTracker.getMasterAddress(zk);
201     assertNotNull(readAddress);
202     assertTrue(expectedAddress.equals(readAddress));
203   }
204 
205   public static class WaitToBeMasterThread extends Thread {
206 
207     ActiveMasterManager manager;
208     DummyMaster dummyMaster;
209     boolean isActiveMaster;
210 
211     public WaitToBeMasterThread(ZooKeeperWatcher zk, ServerName address) {
212       this.dummyMaster = new DummyMaster(zk,address);
213       this.manager = this.dummyMaster.getActiveMasterManager();
214       isActiveMaster = false;
215     }
216 
217     @Override
218     public void run() {
219       manager.blockUntilBecomingActiveMaster(100,
220           Mockito.mock(MonitoredTask.class));
221       LOG.info("Second master has become the active master!");
222       isActiveMaster = true;
223     }
224   }
225 
226   public static class NodeDeletionListener extends ZooKeeperListener {
227     private static final Log LOG = LogFactory.getLog(NodeDeletionListener.class);
228 
229     private Semaphore lock;
230     private String node;
231 
232     public NodeDeletionListener(ZooKeeperWatcher watcher, String node) {
233       super(watcher);
234       lock = new Semaphore(0);
235       this.node = node;
236     }
237 
238     @Override
239     public void nodeDeleted(String path) {
240       if(path.equals(node)) {
241         LOG.debug("nodeDeleted(" + path + ")");
242         lock.release();
243       }
244     }
245 
246     public void waitForDeletion() throws InterruptedException {
247       lock.acquire();
248     }
249   }
250 
251   /**
252    * Dummy Master Implementation.
253    */
254   public static class DummyMaster implements Server {
255     private volatile boolean stopped;
256     private ClusterStatusTracker clusterStatusTracker;
257     private ActiveMasterManager activeMasterManager;
258 
259     public DummyMaster(ZooKeeperWatcher zk, ServerName master) {
260       this.clusterStatusTracker =
261         new ClusterStatusTracker(zk, this);
262       clusterStatusTracker.start();
263 
264       this.activeMasterManager =
265         new ActiveMasterManager(zk, master, this);
266       zk.registerListener(activeMasterManager);
267     }
268 
269     @Override
270     public void abort(final String msg, final Throwable t) {}
271 
272     @Override
273     public boolean isAborted() {
274       return false;
275     }
276 
277     @Override
278     public Configuration getConfiguration() {
279       return null;
280     }
281 
282     @Override
283     public ZooKeeperWatcher getZooKeeper() {
284       return null;
285     }
286 
287     @Override
288     public CoordinatedStateManager getCoordinatedStateManager() {
289       return null;
290     }
291 
292     @Override
293     public ServerName getServerName() {
294       return null;
295     }
296 
297     @Override
298     public boolean isStopped() {
299       return this.stopped;
300     }
301 
302     @Override
303     public void stop(String why) {
304       this.stopped = true;
305     }
306 
307     @Override
308     public ClusterConnection getConnection() {
309       return null;
310     }
311 
312     @Override
313     public MetaTableLocator getMetaTableLocator() {
314       return null;
315     }
316 
317     public ClusterStatusTracker getClusterStatusTracker() {
318       return clusterStatusTracker;
319     }
320 
321     public ActiveMasterManager getActiveMasterManager() {
322       return activeMasterManager;
323     }
324 
325     @Override
326     public ChoreService getChoreService() {
327       return null;
328     }
329   }
330 }