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.hbase.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            try {
71              unassign(hris);
72            } catch (Throwable t) {
73              LOG.warn("Failed bulking re-open " + hris.size()
74                + " region(s)", t);
75            }
76          }
77        });
78      }
79    }
80  
81   /**
82    * Reopen the regions asynchronously, so always returns true immediately.
83    * @return true
84    */
85    @Override
86    protected boolean waitUntilDone(long timeout) {
87      return true;
88    }
89  
90    /**
91     * Configuration knobs "hbase.bulk.reopen.threadpool.size" number of regions
92     * that can be reopened concurrently. The maximum number of threads the master
93     * creates is never more than the number of region servers.
94     * If configuration is not defined it defaults to 20
95     */
96    protected int getThreadCount() {
97      int defaultThreadCount = super.getThreadCount();
98      return this.server.getConfiguration().getInt(
99          "hbase.bulk.reopen.threadpool.size", defaultThreadCount);
100   }
101 
102   public boolean bulkReOpen() throws InterruptedException, IOException {
103     return bulkAssign();
104   }
105 
106   /**
107    * Unassign the list of regions. Configuration knobs:
108    * hbase.bulk.waitbetween.reopen indicates the number of milliseconds to
109    * wait before unassigning another region from this region server
110    *
111    * @param regions
112    * @throws InterruptedException
113    */
114   private void unassign(
115       List<HRegionInfo> regions) throws InterruptedException {
116     int waitTime = this.server.getConfiguration().getInt(
117         "hbase.bulk.waitbetween.reopen", 0);
118     RegionStates regionStates = assignmentManager.getRegionStates();
119     for (HRegionInfo region : regions) {
120       if (server.isStopped()) {
121         return;
122       }
123       if (regionStates.isRegionInTransition(region)) {
124         continue;
125       }
126       assignmentManager.unassign(region, false);
127       while (regionStates.isRegionInTransition(region)
128           && !server.isStopped()) {
129         regionStates.waitForUpdate(100);
130       }
131       if (waitTime > 0 && !server.isStopped()) {
132         Thread.sleep(waitTime);
133       }
134     }
135   }
136 }