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.util.List;
23  import java.util.Map;
24  import java.util.concurrent.ExecutorService;
25  
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.Server;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.master.AssignmentManager;
31  import org.apache.hadoop.hbase.master.BulkAssigner;
32  import org.apache.hadoop.hbase.master.RegionPlan;
33  import org.apache.commons.logging.Log;
34  
35  /**
36   * Performs bulk reopen of the list of regions provided to it.
37   */
38  public class BulkReOpen extends BulkAssigner {
39    private final Map<ServerName, List<HRegionInfo>> rsToRegions;
40    private final AssignmentManager assignmentManager;
41    private static final Log LOG = LogFactory.getLog(BulkReOpen.class);
42  
43    public BulkReOpen(final Server server,
44        final Map<ServerName, List<HRegionInfo>> serverToRegions,
45      final AssignmentManager am) {
46      super(server);
47      this.assignmentManager = am;
48      this.rsToRegions = serverToRegions;
49    }
50  
51    /**
52     * Unassign all regions, so that they go through the regular region
53     * assignment flow (in assignment manager) and are re-opened.
54     */
55    @Override
56    protected void populatePool(ExecutorService pool) {
57      LOG.debug("Creating threads for each region server ");
58      for (Map.Entry<ServerName, List<HRegionInfo>> e : rsToRegions
59          .entrySet()) {
60        final List<HRegionInfo> hris = e.getValue();
61        // add a plan for each of the regions that needs to be reopened
62        for (HRegionInfo hri : hris) {
63          RegionPlan reOpenPlan = new RegionPlan(hri, null,
64              assignmentManager.getRegionServerOfRegion(hri));
65          assignmentManager.addPlan(hri.getEncodedName(), reOpenPlan);
66        }
67        pool.execute(new Runnable() {
68          public void run() {
69            assignmentManager.unassign(hris);
70          }
71        });
72      }
73    }
74  
75   /**
76    * Reopen the regions asynchronously, so always returns true immediately.
77    * @return true
78    */
79    @Override
80    protected boolean waitUntilDone(long timeout) {
81      return true;
82    }
83  
84    /**
85     * Configuration knobs "hbase.bulk.reopen.threadpool.size" number of regions
86     * that can be reopened concurrently. The maximum number of threads the master
87     * creates is never more than the number of region servers.
88     * If configuration is not defined it defaults to 20
89     */
90    protected int getThreadCount() {
91      int defaultThreadCount = super.getThreadCount();
92      return this.server.getConfiguration().getInt(
93          "hbase.bulk.reopen.threadpool.size", defaultThreadCount);
94    }
95  
96    public boolean bulkReOpen() throws InterruptedException {
97      return bulkAssign();
98    }
99  }