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.io.IOException;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ExecutorService;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
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  public class BulkReOpen extends BulkAssigner {
38    private final Map<ServerName, List<HRegionInfo>> rsToRegions;
39    private final AssignmentManager assignmentManager;
40    private static final Log LOG = LogFactory.getLog(BulkReOpen.class);
41  
42    public BulkReOpen(final Server server,
43        final Map<ServerName, List<HRegionInfo>> serverToRegions,
44      final AssignmentManager am) {
45      super(server);
46      this.assignmentManager = am;
47      this.rsToRegions = serverToRegions;
48    }
49  
50    /**
51     * Unassign all regions, so that they go through the regular region
52     * assignment flow (in assignment manager) and are re-opened.
53     */
54    @Override
55    protected void populatePool(ExecutorService pool) {
56      LOG.debug("Creating threads for each region server ");
57      for (Map.Entry<ServerName, List<HRegionInfo>> e : rsToRegions
58          .entrySet()) {
59        final List<HRegionInfo> hris = e.getValue();
60        // add plans for the regions that need to be reopened
61        Map<String, RegionPlan> plans = new HashMap<String, RegionPlan>();
62        for (HRegionInfo hri : hris) {
63          RegionPlan reOpenPlan = new RegionPlan(hri, null,
64              assignmentManager.getRegionServerOfRegion(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 }