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.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.ExecutorService;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.Server;
32  import org.apache.hadoop.hbase.ServerName;
33  
34  /**
35   * Performs bulk reopen of the list of regions provided to it.
36   */
37  @InterfaceAudience.Private
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 plans for the regions that need to be reopened
62        Map<String, RegionPlan> plans = new HashMap<String, RegionPlan>();
63        for (HRegionInfo hri : hris) {
64          RegionPlan reOpenPlan = assignmentManager.getRegionReopenPlan(hri);
65          plans.put(hri.getEncodedName(), reOpenPlan);
66        }
67        assignmentManager.addPlans(plans);
68        pool.execute(new Runnable() {
69          public void run() {
70            assignmentManager.unassign(hris);
71          }
72        });
73      }
74    }
75  
76   /**
77    * Reopen the regions asynchronously, so always returns true immediately.
78    * @return true
79    */
80    @Override
81    protected boolean waitUntilDone(long timeout) {
82      return true;
83    }
84  
85    /**
86     * Configuration knobs "hbase.bulk.reopen.threadpool.size" number of regions
87     * that can be reopened concurrently. The maximum number of threads the master
88     * creates is never more than the number of region servers.
89     * If configuration is not defined it defaults to 20
90     */
91    protected int getThreadCount() {
92      int defaultThreadCount = super.getThreadCount();
93      return this.server.getConfiguration().getInt(
94          "hbase.bulk.reopen.threadpool.size", defaultThreadCount);
95    }
96  
97    public boolean bulkReOpen() throws InterruptedException, IOException {
98      return bulkAssign();
99    }
100 }