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  
20  package org.apache.hadoop.hbase.util;
21  
22  import java.io.IOException;
23  import java.io.InterruptedIOException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  import java.util.concurrent.Callable;
28  import java.util.concurrent.CompletionService;
29  import java.util.concurrent.ExecutionException;
30  import java.util.concurrent.ExecutorCompletionService;
31  import java.util.concurrent.ThreadFactory;
32  import java.util.concurrent.ThreadPoolExecutor;
33  import java.util.concurrent.TimeUnit;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.hadoop.hbase.classification.InterfaceAudience;
38  import org.apache.hadoop.conf.Configuration;
39  import org.apache.hadoop.fs.Path;
40  import org.apache.hadoop.hbase.HRegionInfo;
41  import org.apache.hadoop.hbase.HTableDescriptor;
42  import org.apache.hadoop.hbase.regionserver.HRegion;
43  import org.apache.hadoop.hbase.master.AssignmentManager;
44  
45  /**
46   * Utility methods for interacting with the regions.
47   */
48  @InterfaceAudience.Private
49  public abstract class ModifyRegionUtils {
50    private static final Log LOG = LogFactory.getLog(ModifyRegionUtils.class);
51  
52    private ModifyRegionUtils() {
53    }
54  
55    public interface RegionFillTask {
56      void fillRegion(final HRegion region) throws IOException;
57    }
58  
59    public interface RegionEditTask {
60      void editRegion(final HRegionInfo region) throws IOException;
61    }
62  
63    /**
64     * Create new set of regions on the specified file-system.
65     * NOTE: that you should add the regions to hbase:meta after this operation.
66     *
67     * @param conf {@link Configuration}
68     * @param rootDir Root directory for HBase instance
69     * @param hTableDescriptor description of the table
70     * @param newRegions {@link HRegionInfo} that describes the regions to create
71     * @throws IOException
72     */
73    public static List<HRegionInfo> createRegions(final Configuration conf, final Path rootDir,
74        final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions) throws IOException {
75      return createRegions(conf, rootDir, hTableDescriptor, newRegions, null);
76    }
77  
78    /**
79     * Create new set of regions on the specified file-system.
80     * NOTE: that you should add the regions to hbase:meta after this operation.
81     *
82     * @param conf {@link Configuration}
83     * @param rootDir Root directory for HBase instance
84     * @param hTableDescriptor description of the table
85     * @param newRegions {@link HRegionInfo} that describes the regions to create
86     * @param task {@link RegionFillTask} custom code to populate region after creation
87     * @throws IOException
88     */
89    public static List<HRegionInfo> createRegions(final Configuration conf, final Path rootDir,
90        final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
91        final RegionFillTask task) throws IOException {
92  
93        Path tableDir = FSUtils.getTableDir(rootDir, hTableDescriptor.getTableName());
94        return createRegions(conf, rootDir, tableDir, hTableDescriptor, newRegions, task);
95    }
96  
97    /**
98     * Create new set of regions on the specified file-system.
99     * NOTE: that you should add the regions to hbase:meta after this operation.
100    *
101    * @param conf {@link Configuration}
102    * @param rootDir Root directory for HBase instance
103    * @param tableDir table directory
104    * @param hTableDescriptor description of the table
105    * @param newRegions {@link HRegionInfo} that describes the regions to create
106    * @param task {@link RegionFillTask} custom code to populate region after creation
107    * @throws IOException
108    */
109   public static List<HRegionInfo> createRegions(final Configuration conf, final Path rootDir,
110       final Path tableDir, final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
111       final RegionFillTask task) throws IOException {
112     if (newRegions == null) return null;
113     int regionNumber = newRegions.length;
114     ThreadPoolExecutor exec = getRegionOpenAndInitThreadPool(conf,
115         "RegionOpenAndInitThread-" + hTableDescriptor.getTableName(), regionNumber);
116     try {
117       return createRegions(exec, conf, rootDir, tableDir, hTableDescriptor, newRegions, task);
118     } finally {
119       exec.shutdownNow();
120     }
121   }
122 
123   /**
124    * Create new set of regions on the specified file-system.
125    * NOTE: that you should add the regions to hbase:meta after this operation.
126    *
127    * @param exec Thread Pool Executor
128    * @param conf {@link Configuration}
129    * @param rootDir Root directory for HBase instance
130    * @param tableDir table directory
131    * @param hTableDescriptor description of the table
132    * @param newRegions {@link HRegionInfo} that describes the regions to create
133    * @param task {@link RegionFillTask} custom code to populate region after creation
134    * @throws IOException
135    */
136   public static List<HRegionInfo> createRegions(final ThreadPoolExecutor exec,
137       final Configuration conf, final Path rootDir, final Path tableDir,
138       final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
139       final RegionFillTask task) throws IOException {
140     if (newRegions == null) return null;
141     int regionNumber = newRegions.length;
142     CompletionService<HRegionInfo> completionService =
143       new ExecutorCompletionService<HRegionInfo>(exec);
144     List<HRegionInfo> regionInfos = new ArrayList<HRegionInfo>();
145     for (final HRegionInfo newRegion : newRegions) {
146       completionService.submit(new Callable<HRegionInfo>() {
147         @Override
148         public HRegionInfo call() throws IOException {
149           return createRegion(conf, rootDir, tableDir, hTableDescriptor, newRegion, task);
150         }
151       });
152     }
153     try {
154       // wait for all regions to finish creation
155       for (int i = 0; i < regionNumber; i++) {
156         regionInfos.add(completionService.take().get());
157       }
158     } catch (InterruptedException e) {
159       LOG.error("Caught " + e + " during region creation");
160       throw new InterruptedIOException(e.getMessage());
161     } catch (ExecutionException e) {
162       throw new IOException(e);
163     }
164     return regionInfos;
165   }
166 
167   /**
168    * Create new set of regions on the specified file-system.
169    * @param conf {@link Configuration}
170    * @param rootDir Root directory for HBase instance
171    * @param tableDir table directory
172    * @param hTableDescriptor description of the table
173    * @param newRegion {@link HRegionInfo} that describes the region to create
174    * @param task {@link RegionFillTask} custom code to populate region after creation
175    * @throws IOException
176    */
177   public static HRegionInfo createRegion(final Configuration conf, final Path rootDir,
178       final Path tableDir, final HTableDescriptor hTableDescriptor, final HRegionInfo newRegion,
179       final RegionFillTask task) throws IOException {
180     // 1. Create HRegion
181     HRegion region = HRegion.createHRegion(newRegion,
182       rootDir, tableDir, conf, hTableDescriptor, null,
183       false, true);
184     try {
185       // 2. Custom user code to interact with the created region
186       if (task != null) {
187         task.fillRegion(region);
188       }
189     } finally {
190       // 3. Close the new region to flush to disk. Close log file too.
191       region.close();
192     }
193     return region.getRegionInfo();
194   }
195 
196   /**
197    * Execute the task on the specified set of regions.
198    *
199    * @param exec Thread Pool Executor
200    * @param regions {@link HRegionInfo} that describes the regions to edit
201    * @param task {@link RegionFillTask} custom code to edit the region
202    * @throws IOException
203    */
204   public static void editRegions(final ThreadPoolExecutor exec,
205       final Collection<HRegionInfo> regions, final RegionEditTask task) throws IOException {
206     final ExecutorCompletionService<Void> completionService =
207       new ExecutorCompletionService<Void>(exec);
208     for (final HRegionInfo hri: regions) {
209       completionService.submit(new Callable<Void>() {
210         @Override
211         public Void call() throws IOException {
212           task.editRegion(hri);
213           return null;
214         }
215       });
216     }
217 
218     try {
219       for (HRegionInfo hri: regions) {
220         completionService.take().get();
221       }
222     } catch (InterruptedException e) {
223       throw new InterruptedIOException(e.getMessage());
224     } catch (ExecutionException e) {
225       IOException ex = new IOException();
226       ex.initCause(e.getCause());
227       throw ex;
228     }
229   }
230 
231   /*
232    * used by createRegions() to get the thread pool executor based on the
233    * "hbase.hregion.open.and.init.threads.max" property.
234    */
235   static ThreadPoolExecutor getRegionOpenAndInitThreadPool(final Configuration conf,
236       final String threadNamePrefix, int regionNumber) {
237     int maxThreads = Math.min(regionNumber, conf.getInt(
238         "hbase.hregion.open.and.init.threads.max", 10));
239     ThreadPoolExecutor regionOpenAndInitThreadPool = Threads
240     .getBoundedCachedThreadPool(maxThreads, 30L, TimeUnit.SECONDS,
241         new ThreadFactory() {
242           private int count = 1;
243 
244           @Override
245           public Thread newThread(Runnable r) {
246             Thread t = new Thread(r, threadNamePrefix + "-" + count++);
247             return t;
248           }
249         });
250     return regionOpenAndInitThreadPool;
251   }
252 
253   /**
254    * Triggers a bulk assignment of the specified regions
255    *
256    * @param assignmentManager the Assignment Manger
257    * @param regionInfos the list of regions to assign
258    * @throws IOException if an error occurred during the assignment
259    */
260   public static void assignRegions(final AssignmentManager assignmentManager,
261       final List<HRegionInfo> regionInfos) throws IOException {
262     try {
263       assignmentManager.getRegionStates().createRegionStates(regionInfos);
264       assignmentManager.assign(regionInfos);
265     } catch (InterruptedException e) {
266       LOG.error("Caught " + e + " during round-robin assignment");
267       InterruptedIOException ie = new InterruptedIOException(e.getMessage());
268       ie.initCause(e);
269       throw ie;
270     }
271   }
272 }