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;
20  
21  import java.io.IOException;
22  import java.security.PrivilegedExceptionAction;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.client.Admin;
33  import org.apache.hadoop.hbase.client.HBaseAdmin;
34  import org.apache.hadoop.hbase.regionserver.HRegionServer;
35  import org.apache.hadoop.hbase.security.User;
36  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
37  import org.apache.hadoop.hbase.util.Threads;
38  
39  import java.util.concurrent.CopyOnWriteArrayList;
40  import org.apache.hadoop.hbase.master.HMaster;
41  import org.apache.hadoop.hbase.util.JVMClusterUtil;
42  
43  /**
44   * This class creates a single process HBase cluster. One thread is created for
45   * a master and one per region server.
46   *
47   * Call {@link #startup()} to start the cluster running and {@link #shutdown()}
48   * to close it all down. {@link #join} the cluster is you want to wait on
49   * shutdown completion.
50   *
51   * <p>Runs master on port 16000 by default.  Because we can't just kill the
52   * process -- not till HADOOP-1700 gets fixed and even then.... -- we need to
53   * be able to find the master with a remote client to run shutdown.  To use a
54   * port other than 16000, set the hbase.master to a value of 'local:PORT':
55   * that is 'local', not 'localhost', and the port number the master should use
56   * instead of 16000.
57   *
58   */
59  @InterfaceAudience.Public
60  @InterfaceStability.Evolving
61  public class LocalHBaseCluster {
62    static final Log LOG = LogFactory.getLog(LocalHBaseCluster.class);
63    private final List<JVMClusterUtil.MasterThread> masterThreads =
64      new CopyOnWriteArrayList<JVMClusterUtil.MasterThread>();
65    private final List<JVMClusterUtil.RegionServerThread> regionThreads =
66      new CopyOnWriteArrayList<JVMClusterUtil.RegionServerThread>();
67    private final static int DEFAULT_NO = 1;
68    /** local mode */
69    public static final String LOCAL = "local";
70    /** 'local:' */
71    public static final String LOCAL_COLON = LOCAL + ":";
72    private final Configuration conf;
73    private final Class<? extends HMaster> masterClass;
74    private final Class<? extends HRegionServer> regionServerClass;
75  
76    /**
77     * Constructor.
78     * @param conf
79     * @throws IOException
80     */
81    public LocalHBaseCluster(final Configuration conf)
82    throws IOException {
83      this(conf, DEFAULT_NO);
84    }
85  
86    /**
87     * Constructor.
88     * @param conf Configuration to use.  Post construction has the master's
89     * address.
90     * @param noRegionServers Count of regionservers to start.
91     * @throws IOException
92     */
93    public LocalHBaseCluster(final Configuration conf, final int noRegionServers)
94    throws IOException {
95      this(conf, 1, noRegionServers, getMasterImplementation(conf),
96          getRegionServerImplementation(conf));
97    }
98  
99    /**
100    * Constructor.
101    * @param conf Configuration to use.  Post construction has the active master
102    * address.
103    * @param noMasters Count of masters to start.
104    * @param noRegionServers Count of regionservers to start.
105    * @throws IOException
106    */
107   public LocalHBaseCluster(final Configuration conf, final int noMasters,
108       final int noRegionServers)
109   throws IOException {
110     this(conf, noMasters, noRegionServers, getMasterImplementation(conf),
111         getRegionServerImplementation(conf));
112   }
113 
114   @SuppressWarnings("unchecked")
115   private static Class<? extends HRegionServer> getRegionServerImplementation(final Configuration conf) {
116     return (Class<? extends HRegionServer>)conf.getClass(HConstants.REGION_SERVER_IMPL,
117        HRegionServer.class);
118   }
119 
120   @SuppressWarnings("unchecked")
121   private static Class<? extends HMaster> getMasterImplementation(final Configuration conf) {
122     return (Class<? extends HMaster>)conf.getClass(HConstants.MASTER_IMPL,
123        HMaster.class);
124   }
125 
126   /**
127    * Constructor.
128    * @param conf Configuration to use.  Post construction has the master's
129    * address.
130    * @param noMasters Count of masters to start.
131    * @param noRegionServers Count of regionservers to start.
132    * @param masterClass
133    * @param regionServerClass
134    * @throws IOException
135    */
136   @SuppressWarnings("unchecked")
137   public LocalHBaseCluster(final Configuration conf, final int noMasters,
138     final int noRegionServers, final Class<? extends HMaster> masterClass,
139     final Class<? extends HRegionServer> regionServerClass)
140   throws IOException {
141     this.conf = conf;
142 
143     // Always have masters and regionservers come up on port '0' so we don't
144     // clash over default ports.
145     conf.set(HConstants.MASTER_PORT, "0");
146     conf.set(HConstants.REGIONSERVER_PORT, "0");
147     conf.set(HConstants.REGIONSERVER_INFO_PORT, "0");
148 
149     this.masterClass = (Class<? extends HMaster>)
150       conf.getClass(HConstants.MASTER_IMPL, masterClass);
151     // Start the HMasters.
152     for (int i = 0; i < noMasters; i++) {
153       addMaster(new Configuration(conf), i);
154     }
155     // Start the HRegionServers.
156     this.regionServerClass =
157       (Class<? extends HRegionServer>)conf.getClass(HConstants.REGION_SERVER_IMPL,
158        regionServerClass);
159 
160     for (int i = 0; i < noRegionServers; i++) {
161       addRegionServer(new Configuration(conf), i);
162     }
163   }
164 
165   public JVMClusterUtil.RegionServerThread addRegionServer()
166       throws IOException {
167     return addRegionServer(new Configuration(conf), this.regionThreads.size());
168   }
169 
170   @SuppressWarnings("unchecked")
171   public JVMClusterUtil.RegionServerThread addRegionServer(
172       Configuration config, final int index)
173   throws IOException {
174     // Create each regionserver with its own Configuration instance so each has
175     // its HConnection instance rather than share (see HBASE_INSTANCES down in
176     // the guts of HConnectionManager.
177 
178     // Also, create separate CoordinatedStateManager instance per Server.
179     // This is special case when we have to have more than 1 CoordinatedStateManager
180     // within 1 process.
181     CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
182 
183     JVMClusterUtil.RegionServerThread rst =
184         JVMClusterUtil.createRegionServerThread(config, cp, (Class<? extends HRegionServer>) conf
185             .getClass(HConstants.REGION_SERVER_IMPL, this.regionServerClass), index);
186 
187     this.regionThreads.add(rst);
188     return rst;
189   }
190 
191   public JVMClusterUtil.RegionServerThread addRegionServer(
192       final Configuration config, final int index, User user)
193   throws IOException, InterruptedException {
194     return user.runAs(
195         new PrivilegedExceptionAction<JVMClusterUtil.RegionServerThread>() {
196           public JVMClusterUtil.RegionServerThread run() throws Exception {
197             return addRegionServer(config, index);
198           }
199         });
200   }
201 
202   public JVMClusterUtil.MasterThread addMaster() throws IOException {
203     return addMaster(new Configuration(conf), this.masterThreads.size());
204   }
205 
206   public JVMClusterUtil.MasterThread addMaster(Configuration c, final int index)
207   throws IOException {
208     // Create each master with its own Configuration instance so each has
209     // its HConnection instance rather than share (see HBASE_INSTANCES down in
210     // the guts of HConnectionManager.
211 
212     // Also, create separate CoordinatedStateManager instance per Server.
213     // This is special case when we have to have more than 1 CoordinatedStateManager
214     // within 1 process.
215     CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
216 
217     JVMClusterUtil.MasterThread mt = JVMClusterUtil.createMasterThread(c, cp,
218         (Class<? extends HMaster>) conf.getClass(HConstants.MASTER_IMPL, this.masterClass), index);
219     this.masterThreads.add(mt);
220     return mt;
221   }
222 
223   public JVMClusterUtil.MasterThread addMaster(
224       final Configuration c, final int index, User user)
225   throws IOException, InterruptedException {
226     return user.runAs(
227         new PrivilegedExceptionAction<JVMClusterUtil.MasterThread>() {
228           public JVMClusterUtil.MasterThread run() throws Exception {
229             return addMaster(c, index);
230           }
231         });
232   }
233 
234   /**
235    * @param serverNumber
236    * @return region server
237    */
238   public HRegionServer getRegionServer(int serverNumber) {
239     return regionThreads.get(serverNumber).getRegionServer();
240   }
241 
242   /**
243    * @return Read-only list of region server threads.
244    */
245   public List<JVMClusterUtil.RegionServerThread> getRegionServers() {
246     return Collections.unmodifiableList(this.regionThreads);
247   }
248 
249   /**
250    * @return List of running servers (Some servers may have been killed or
251    * aborted during lifetime of cluster; these servers are not included in this
252    * list).
253    */
254   public List<JVMClusterUtil.RegionServerThread> getLiveRegionServers() {
255     List<JVMClusterUtil.RegionServerThread> liveServers =
256       new ArrayList<JVMClusterUtil.RegionServerThread>();
257     List<RegionServerThread> list = getRegionServers();
258     for (JVMClusterUtil.RegionServerThread rst: list) {
259       if (rst.isAlive()) liveServers.add(rst);
260       else LOG.info("Not alive " + rst.getName());
261     }
262     return liveServers;
263   }
264 
265   /**
266    * @return the Configuration used by this LocalHBaseCluster
267    */
268   public Configuration getConfiguration() {
269     return this.conf;
270   }
271 
272   /**
273    * Wait for the specified region server to stop
274    * Removes this thread from list of running threads.
275    * @param serverNumber
276    * @return Name of region server that just went down.
277    */
278   public String waitOnRegionServer(int serverNumber) {
279     JVMClusterUtil.RegionServerThread regionServerThread =
280       this.regionThreads.remove(serverNumber);
281     while (regionServerThread.isAlive()) {
282       try {
283         LOG.info("Waiting on " +
284           regionServerThread.getRegionServer().toString());
285         regionServerThread.join();
286       } catch (InterruptedException e) {
287         e.printStackTrace();
288       }
289     }
290     return regionServerThread.getName();
291   }
292 
293   /**
294    * Wait for the specified region server to stop
295    * Removes this thread from list of running threads.
296    * @param rst
297    * @return Name of region server that just went down.
298    */
299   public String waitOnRegionServer(JVMClusterUtil.RegionServerThread rst) {
300     while (rst.isAlive()) {
301       try {
302         LOG.info("Waiting on " +
303           rst.getRegionServer().toString());
304         rst.join();
305       } catch (InterruptedException e) {
306         e.printStackTrace();
307       }
308     }
309     for (int i=0;i<regionThreads.size();i++) {
310       if (regionThreads.get(i) == rst) {
311         regionThreads.remove(i);
312         break;
313       }
314     }
315     return rst.getName();
316   }
317 
318   /**
319    * @param serverNumber
320    * @return the HMaster thread
321    */
322   public HMaster getMaster(int serverNumber) {
323     return masterThreads.get(serverNumber).getMaster();
324   }
325 
326   /**
327    * Gets the current active master, if available.  If no active master, returns
328    * null.
329    * @return the HMaster for the active master
330    */
331   public HMaster getActiveMaster() {
332     for (JVMClusterUtil.MasterThread mt : masterThreads) {
333       if (mt.getMaster().isActiveMaster()) {
334         // Ensure that the current active master is not stopped.
335         // We don't want to return a stopping master as an active master.
336         if (mt.getMaster().isActiveMaster()  && !mt.getMaster().isStopped()) {
337           return mt.getMaster();
338         }
339       }
340     }
341     return null;
342   }
343 
344   /**
345    * @return Read-only list of master threads.
346    */
347   public List<JVMClusterUtil.MasterThread> getMasters() {
348     return Collections.unmodifiableList(this.masterThreads);
349   }
350 
351   /**
352    * @return List of running master servers (Some servers may have been killed
353    * or aborted during lifetime of cluster; these servers are not included in
354    * this list).
355    */
356   public List<JVMClusterUtil.MasterThread> getLiveMasters() {
357     List<JVMClusterUtil.MasterThread> liveServers =
358       new ArrayList<JVMClusterUtil.MasterThread>();
359     List<JVMClusterUtil.MasterThread> list = getMasters();
360     for (JVMClusterUtil.MasterThread mt: list) {
361       if (mt.isAlive()) {
362         liveServers.add(mt);
363       }
364     }
365     return liveServers;
366   }
367 
368   /**
369    * Wait for the specified master to stop
370    * Removes this thread from list of running threads.
371    * @param serverNumber
372    * @return Name of master that just went down.
373    */
374   public String waitOnMaster(int serverNumber) {
375     JVMClusterUtil.MasterThread masterThread = this.masterThreads.remove(serverNumber);
376     while (masterThread.isAlive()) {
377       try {
378         LOG.info("Waiting on " + masterThread.getMaster().getServerName().toString());
379         masterThread.join();
380       } catch (InterruptedException e) {
381         e.printStackTrace();
382       }
383     }
384     return masterThread.getName();
385   }
386 
387   /**
388    * Wait for the specified master to stop
389    * Removes this thread from list of running threads.
390    * @param masterThread
391    * @return Name of master that just went down.
392    */
393   public String waitOnMaster(JVMClusterUtil.MasterThread masterThread) {
394     while (masterThread.isAlive()) {
395       try {
396         LOG.info("Waiting on " +
397           masterThread.getMaster().getServerName().toString());
398         masterThread.join();
399       } catch (InterruptedException e) {
400         e.printStackTrace();
401       }
402     }
403     for (int i=0;i<masterThreads.size();i++) {
404       if (masterThreads.get(i) == masterThread) {
405         masterThreads.remove(i);
406         break;
407       }
408     }
409     return masterThread.getName();
410   }
411 
412   /**
413    * Wait for Mini HBase Cluster to shut down.
414    * Presumes you've already called {@link #shutdown()}.
415    */
416   public void join() {
417     if (this.regionThreads != null) {
418       for(Thread t: this.regionThreads) {
419         if (t.isAlive()) {
420           try {
421             Threads.threadDumpingIsAlive(t);
422           } catch (InterruptedException e) {
423             LOG.debug("Interrupted", e);
424           }
425         }
426       }
427     }
428     if (this.masterThreads != null) {
429       for (Thread t : this.masterThreads) {
430         if (t.isAlive()) {
431           try {
432             Threads.threadDumpingIsAlive(t);
433           } catch (InterruptedException e) {
434             LOG.debug("Interrupted", e);
435           }
436         }
437       }
438     }
439   }
440 
441   /**
442    * Start the cluster.
443    */
444   public void startup() throws IOException {
445     JVMClusterUtil.startup(this.masterThreads, this.regionThreads);
446   }
447 
448   /**
449    * Shut down the mini HBase cluster
450    */
451   public void shutdown() {
452     JVMClusterUtil.shutdown(this.masterThreads, this.regionThreads);
453   }
454 
455   /**
456    * @param c Configuration to check.
457    * @return True if a 'local' address in hbase.master value.
458    */
459   public static boolean isLocal(final Configuration c) {
460     boolean mode = c.getBoolean(HConstants.CLUSTER_DISTRIBUTED, HConstants.DEFAULT_CLUSTER_DISTRIBUTED);
461     return(mode == HConstants.CLUSTER_IS_LOCAL);
462   }
463 
464   /**
465    * Test things basically work.
466    * @param args
467    * @throws IOException
468    */
469   public static void main(String[] args) throws IOException {
470     Configuration conf = HBaseConfiguration.create();
471     LocalHBaseCluster cluster = new LocalHBaseCluster(conf);
472     cluster.startup();
473     Admin admin = new HBaseAdmin(conf);
474     try {
475       HTableDescriptor htd =
476         new HTableDescriptor(TableName.valueOf(cluster.getClass().getName()));
477       admin.createTable(htd);
478     } finally {
479       admin.close();
480     }
481     cluster.shutdown();
482   }
483 }