View Javadoc

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