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.Future;
32  import java.util.concurrent.ThreadFactory;
33  import java.util.concurrent.ThreadPoolExecutor;
34  import java.util.concurrent.TimeUnit;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.hadoop.classification.InterfaceAudience;
39  import org.apache.hadoop.conf.Configuration;
40  import org.apache.hadoop.fs.Path;
41  import org.apache.hadoop.hbase.HRegionInfo;
42  import org.apache.hadoop.hbase.HTableDescriptor;
43  import org.apache.hadoop.hbase.regionserver.HRegion;
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         Future<HRegionInfo> future = completionService.take();
157         HRegionInfo regionInfo = future.get();
158         regionInfos.add(regionInfo);
159       }
160     } catch (InterruptedException e) {
161       LOG.error("Caught " + e + " during region creation");
162       throw new InterruptedIOException(e.getMessage());
163     } catch (ExecutionException e) {
164       throw new IOException(e);
165     }
166     return regionInfos;
167   }
168 
169   /**
170    * Create new set of regions on the specified file-system.
171    * @param conf {@link Configuration}
172    * @param rootDir Root directory for HBase instance
173    * @param tableDir table directory
174    * @param hTableDescriptor description of the table
175    * @param newRegion {@link HRegionInfo} that describes the region to create
176    * @param task {@link RegionFillTask} custom code to populate region after creation
177    * @throws IOException
178    */
179   public static HRegionInfo createRegion(final Configuration conf, final Path rootDir,
180       final Path tableDir, final HTableDescriptor hTableDescriptor, final HRegionInfo newRegion,
181       final RegionFillTask task) throws IOException {
182     // 1. Create HRegion
183     HRegion region = HRegion.createHRegion(newRegion,
184       rootDir, tableDir, conf, hTableDescriptor, null,
185       false, true);
186     try {
187       // 2. Custom user code to interact with the created region
188       if (task != null) {
189         task.fillRegion(region);
190       }
191     } finally {
192       // 3. Close the new region to flush to disk. Close log file too.
193       region.close();
194     }
195     return region.getRegionInfo();
196   }
197 
198   /**
199    * Execute the task on the specified set of regions.
200    *
201    * @param exec Thread Pool Executor
202    * @param regions {@link HRegionInfo} that describes the regions to edit
203    * @param task {@link RegionFillTask} custom code to edit the region
204    * @throws IOException
205    */
206   public static void editRegions(final ThreadPoolExecutor exec,
207       final Collection<HRegionInfo> regions, final RegionEditTask task) throws IOException {
208     final ExecutorCompletionService<Void> completionService =
209       new ExecutorCompletionService<Void>(exec);
210     for (final HRegionInfo hri: regions) {
211       completionService.submit(new Callable<Void>() {
212         @Override
213         public Void call() throws IOException {
214           task.editRegion(hri);
215           return null;
216         }
217       });
218     }
219 
220     try {
221       for (HRegionInfo hri: regions) {
222         completionService.take().get();
223       }
224     } catch (InterruptedException e) {
225       throw new InterruptedIOException(e.getMessage());
226     } catch (ExecutionException e) {
227       IOException ex = new IOException();
228       ex.initCause(e.getCause());
229       throw ex;
230     }
231   }
232 
233   /*
234    * used by createRegions() to get the thread pool executor based on the
235    * "hbase.hregion.open.and.init.threads.max" property.
236    */
237   static ThreadPoolExecutor getRegionOpenAndInitThreadPool(final Configuration conf,
238       final String threadNamePrefix, int regionNumber) {
239     int maxThreads = Math.min(regionNumber, conf.getInt(
240         "hbase.hregion.open.and.init.threads.max", 10));
241     ThreadPoolExecutor regionOpenAndInitThreadPool = Threads
242     .getBoundedCachedThreadPool(maxThreads, 30L, TimeUnit.SECONDS,
243         new ThreadFactory() {
244           private int count = 1;
245 
246           @Override
247           public Thread newThread(Runnable r) {
248             Thread t = new Thread(r, threadNamePrefix + "-" + count++);
249             return t;
250           }
251         });
252     return regionOpenAndInitThreadPool;
253   }
254 }