View Javadoc

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