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 java.io.IOException;
22  import java.lang.Thread.UncaughtExceptionHandler;
23  import java.util.concurrent.Executors;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.Server;
27  
28  import com.google.common.util.concurrent.ThreadFactoryBuilder;
29  
30  /**
31   * Base class used bulk assigning and unassigning regions.
32   * Encapsulates a fixed size thread pool of executors to run assignment/unassignment.
33   * Implement {@link #populatePool(java.util.concurrent.ExecutorService)} and
34   * {@link #waitUntilDone(long)}.  The default implementation of
35   * the {@link #getUncaughtExceptionHandler()} is to abort the hosting
36   * Server.
37   */
38  @InterfaceAudience.Private
39  public abstract class BulkAssigner {
40    protected final Server server;
41  
42    /**
43     * @param server An instance of Server
44     */
45    public BulkAssigner(final Server server) {
46      this.server = server;
47    }
48  
49    /**
50     * @return What to use for a thread prefix when executor runs.
51     */
52    protected String getThreadNamePrefix() {
53      return this.server.getServerName() + "-" + this.getClass().getName(); 
54    }
55  
56    protected UncaughtExceptionHandler getUncaughtExceptionHandler() {
57      return new UncaughtExceptionHandler() {
58        @Override
59        public void uncaughtException(Thread t, Throwable e) {
60          // Abort if exception of any kind.
61          server.abort("Uncaught exception in " + t.getName(), e);
62        }
63      };
64    }
65  
66    protected int getThreadCount() {
67      return this.server.getConfiguration().
68        getInt("hbase.bulk.assignment.threadpool.size", 20);
69    }
70  
71    protected long getTimeoutOnRIT() {
72      return this.server.getConfiguration().
73        getLong("hbase.bulk.assignment.waiton.empty.rit", 5 * 60 * 1000);
74    }
75  
76    protected abstract void populatePool(
77        final java.util.concurrent.ExecutorService pool) throws IOException;
78  
79    public boolean bulkAssign() throws InterruptedException, IOException {
80      return bulkAssign(true);
81    }
82  
83    /**
84     * Run the bulk assign.
85     * 
86     * @param sync
87     *          Whether to assign synchronously.
88     * @throws InterruptedException
89     * @return True if done.
90     * @throws IOException
91     */
92    public boolean bulkAssign(boolean sync) throws InterruptedException,
93        IOException {
94      boolean result = false;
95      ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
96      builder.setDaemon(true);
97      builder.setNameFormat(getThreadNamePrefix() + "-%1$d");
98      builder.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
99      int threadCount = getThreadCount();
100     java.util.concurrent.ExecutorService pool =
101       Executors.newFixedThreadPool(threadCount, builder.build());
102     try {
103       populatePool(pool);
104       // How long to wait on empty regions-in-transition.  If we timeout, the
105       // RIT monitor should do fixup.
106       if (sync) result = waitUntilDone(getTimeoutOnRIT());
107     } finally {
108       // We're done with the pool.  It'll exit when its done all in queue.
109       pool.shutdown();
110     }
111     return result;
112   }
113 
114   /**
115    * Wait until bulk assign is done.
116    * @param timeout How long to wait.
117    * @throws InterruptedException
118    * @return True if the condition we were waiting on happened.
119    */
120   protected abstract boolean waitUntilDone(final long timeout)
121   throws InterruptedException;
122 }