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.util;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.NoSuchElementException;
25  import java.util.Random;
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.conf.Configuration;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.HRegionInfo;
35  import org.apache.hadoop.hbase.HTableDescriptor;
36  import org.apache.hadoop.hbase.MetaTableAccessor;
37  import org.apache.hadoop.hbase.TableName;
38  import org.apache.hadoop.hbase.TableNotDisabledException;
39  import org.apache.hadoop.hbase.client.Admin;
40  import org.apache.hadoop.hbase.client.Delete;
41  import org.apache.hadoop.hbase.client.HBaseAdmin;
42  import org.apache.hadoop.hbase.client.HConnectable;
43  import org.apache.hadoop.hbase.client.HConnection;
44  import org.apache.hadoop.hbase.client.HConnectionManager;
45  import org.apache.hadoop.hbase.client.HTable;
46  import org.apache.hadoop.hbase.client.Result;
47  import org.apache.hadoop.hbase.client.ResultScanner;
48  import org.apache.hadoop.hbase.client.Table;
49  import org.apache.hadoop.hbase.regionserver.HRegion;
50  import org.apache.hadoop.hbase.wal.WALFactory;
51  import org.apache.hadoop.ipc.RemoteException;
52  
53  /**
54   * A non-instantiable class that has a static method capable of compacting
55   * a table by merging adjacent regions.
56   */
57  @InterfaceAudience.Private
58  class HMerge {
59    // TODO: Where is this class used?  How does it relate to Merge in same package?
60    static final Log LOG = LogFactory.getLog(HMerge.class);
61    static final Random rand = new Random();
62  
63    /*
64     * Not instantiable
65     */
66    private HMerge() {
67      super();
68    }
69  
70    /**
71     * Scans the table and merges two adjacent regions if they are small. This
72     * only happens when a lot of rows are deleted.
73     *
74     * When merging the hbase:meta region, the HBase instance must be offline.
75     * When merging a normal table, the HBase instance must be online, but the
76     * table must be disabled.
77     *
78     * @param conf        - configuration object for HBase
79     * @param fs          - FileSystem where regions reside
80     * @param tableName   - Table to be compacted
81     * @throws IOException
82     */
83    public static void merge(Configuration conf, FileSystem fs,
84      final TableName tableName)
85    throws IOException {
86      merge(conf, fs, tableName, true);
87    }
88  
89    /**
90     * Scans the table and merges two adjacent regions if they are small. This
91     * only happens when a lot of rows are deleted.
92     *
93     * When merging the hbase:meta region, the HBase instance must be offline.
94     * When merging a normal table, the HBase instance must be online, but the
95     * table must be disabled.
96     *
97     * @param conf        - configuration object for HBase
98     * @param fs          - FileSystem where regions reside
99     * @param tableName   - Table to be compacted
100    * @param testMasterRunning True if we are to verify master is down before
101    * running merge
102    * @throws IOException
103    */
104   public static void merge(Configuration conf, FileSystem fs,
105     final TableName tableName, final boolean testMasterRunning)
106   throws IOException {
107     boolean masterIsRunning = false;
108     if (testMasterRunning) {
109       masterIsRunning = HConnectionManager
110           .execute(new HConnectable<Boolean>(conf) {
111             @Override
112             public Boolean connect(HConnection connection) throws IOException {
113               return connection.isMasterRunning();
114             }
115           });
116     }
117     if (tableName.equals(TableName.META_TABLE_NAME)) {
118       if (masterIsRunning) {
119         throw new IllegalStateException(
120             "Can not compact hbase:meta table if instance is on-line");
121       }
122       // TODO reenable new OfflineMerger(conf, fs).process();
123     } else {
124       if(!masterIsRunning) {
125         throw new IllegalStateException(
126             "HBase instance must be running to merge a normal table");
127       }
128       Admin admin = new HBaseAdmin(conf);
129       try {
130         if (!admin.isTableDisabled(tableName)) {
131           throw new TableNotDisabledException(tableName);
132         }
133       } finally {
134         admin.close();
135       }
136       new OnlineMerger(conf, fs, tableName).process();
137     }
138   }
139 
140   private static abstract class Merger {
141     protected final Configuration conf;
142     protected final FileSystem fs;
143     protected final Path rootDir;
144     protected final HTableDescriptor htd;
145     protected final WALFactory walFactory;
146     private final long maxFilesize;
147 
148 
149     protected Merger(Configuration conf, FileSystem fs, final TableName tableName)
150     throws IOException {
151       this.conf = conf;
152       this.fs = fs;
153       this.maxFilesize = conf.getLong(HConstants.HREGION_MAX_FILESIZE,
154           HConstants.DEFAULT_MAX_FILE_SIZE);
155 
156       this.rootDir = FSUtils.getRootDir(conf);
157       Path tabledir = FSUtils.getTableDir(this.rootDir, tableName);
158       this.htd = FSTableDescriptors.getTableDescriptorFromFs(this.fs, tabledir)
159           .getHTableDescriptor();
160       String logname = "merge_" + System.currentTimeMillis() + HConstants.HREGION_LOGDIR_NAME;
161 
162       final Configuration walConf = new Configuration(conf);
163       FSUtils.setRootDir(walConf, tabledir);
164       this.walFactory = new WALFactory(walConf, null, logname);
165     }
166 
167     void process() throws IOException {
168       try {
169         for (HRegionInfo[] regionsToMerge = next();
170             regionsToMerge != null;
171             regionsToMerge = next()) {
172           if (!merge(regionsToMerge)) {
173             return;
174           }
175         }
176       } finally {
177         try {
178           walFactory.close();
179         } catch(IOException e) {
180           LOG.error(e);
181         }
182       }
183     }
184 
185     protected boolean merge(final HRegionInfo[] info) throws IOException {
186       if (info.length < 2) {
187         LOG.info("only one region - nothing to merge");
188         return false;
189       }
190 
191       HRegion currentRegion = null;
192       long currentSize = 0;
193       HRegion nextRegion = null;
194       long nextSize = 0;
195       for (int i = 0; i < info.length - 1; i++) {
196         if (currentRegion == null) {
197           currentRegion = HRegion.openHRegion(conf, fs, this.rootDir, info[i], this.htd,
198               walFactory.getWAL(info[i].getEncodedNameAsBytes()));
199           currentSize = currentRegion.getLargestHStoreSize();
200         }
201         nextRegion = HRegion.openHRegion(conf, fs, this.rootDir, info[i + 1], this.htd,
202             walFactory.getWAL(info[i+1].getEncodedNameAsBytes()));
203         nextSize = nextRegion.getLargestHStoreSize();
204 
205         if ((currentSize + nextSize) <= (maxFilesize / 2)) {
206           // We merge two adjacent regions if their total size is less than
207           // one half of the desired maximum size
208           LOG.info("Merging regions " + currentRegion.getRegionNameAsString() +
209             " and " + nextRegion.getRegionNameAsString());
210           HRegion mergedRegion =
211             HRegion.mergeAdjacent(currentRegion, nextRegion);
212           updateMeta(currentRegion.getRegionName(), nextRegion.getRegionName(),
213               mergedRegion);
214           break;
215         }
216         LOG.info("not merging regions " + Bytes.toStringBinary(currentRegion.getRegionName())
217             + " and " + Bytes.toStringBinary(nextRegion.getRegionName()));
218         currentRegion.close();
219         currentRegion = nextRegion;
220         currentSize = nextSize;
221       }
222       if(currentRegion != null) {
223         currentRegion.close();
224       }
225       return true;
226     }
227 
228     protected abstract HRegionInfo[] next() throws IOException;
229 
230     protected abstract void updateMeta(final byte [] oldRegion1,
231       final byte [] oldRegion2, HRegion newRegion)
232     throws IOException;
233 
234   }
235 
236   /** Instantiated to compact a normal user table */
237   private static class OnlineMerger extends Merger {
238     private final TableName tableName;
239     private final Table table;
240     private final ResultScanner metaScanner;
241     private HRegionInfo latestRegion;
242 
243     OnlineMerger(Configuration conf, FileSystem fs,
244       final TableName tableName)
245     throws IOException {
246       super(conf, fs, tableName);
247       this.tableName = tableName;
248       this.table = new HTable(conf, TableName.META_TABLE_NAME);
249       this.metaScanner = table.getScanner(HConstants.CATALOG_FAMILY,
250           HConstants.REGIONINFO_QUALIFIER);
251       this.latestRegion = null;
252     }
253 
254     private HRegionInfo nextRegion() throws IOException {
255       try {
256         Result results = getMetaRow();
257         if (results == null) {
258           return null;
259         }
260         HRegionInfo region = HRegionInfo.getHRegionInfo(results);
261         if (region == null) {
262           throw new NoSuchElementException("meta region entry missing " +
263               Bytes.toString(HConstants.CATALOG_FAMILY) + ":" +
264               Bytes.toString(HConstants.REGIONINFO_QUALIFIER));
265         }
266         if (!region.getTable().equals(this.tableName)) {
267           return null;
268         }
269         return region;
270       } catch (IOException e) {
271         e = e instanceof RemoteException ? ((RemoteException) e).unwrapRemoteException() : e;
272         LOG.error("meta scanner error", e);
273         metaScanner.close();
274         throw e;
275       }
276     }
277 
278     /*
279      * Check current row has a HRegionInfo.  Skip to next row if HRI is empty.
280      * @return A Map of the row content else null if we are off the end.
281      * @throws IOException
282      */
283     private Result getMetaRow() throws IOException {
284       Result currentRow = metaScanner.next();
285       boolean foundResult = false;
286       while (currentRow != null) {
287         LOG.info("Row: <" + Bytes.toStringBinary(currentRow.getRow()) + ">");
288         byte[] regionInfoValue = currentRow.getValue(HConstants.CATALOG_FAMILY,
289             HConstants.REGIONINFO_QUALIFIER);
290         if (regionInfoValue == null || regionInfoValue.length == 0) {
291           currentRow = metaScanner.next();
292           continue;
293         }
294         HRegionInfo region = HRegionInfo.getHRegionInfo(currentRow);
295         if (!region.getTable().equals(this.tableName)) {
296           currentRow = metaScanner.next();
297           continue;
298         }
299         foundResult = true;
300         break;
301       }
302       return foundResult ? currentRow : null;
303     }
304 
305     @Override
306     protected HRegionInfo[] next() throws IOException {
307       List<HRegionInfo> regions = new ArrayList<HRegionInfo>();
308       if(latestRegion == null) {
309         latestRegion = nextRegion();
310       }
311       if(latestRegion != null) {
312         regions.add(latestRegion);
313       }
314       latestRegion = nextRegion();
315       if(latestRegion != null) {
316         regions.add(latestRegion);
317       }
318       return regions.toArray(new HRegionInfo[regions.size()]);
319     }
320 
321     @Override
322     protected void updateMeta(final byte [] oldRegion1,
323         final byte [] oldRegion2,
324       HRegion newRegion)
325     throws IOException {
326       byte[][] regionsToDelete = {oldRegion1, oldRegion2};
327       for (int r = 0; r < regionsToDelete.length; r++) {
328         if(Bytes.equals(regionsToDelete[r], latestRegion.getRegionName())) {
329           latestRegion = null;
330         }
331         Delete delete = new Delete(regionsToDelete[r]);
332         table.delete(delete);
333         if(LOG.isDebugEnabled()) {
334           LOG.debug("updated columns in row: " + Bytes.toStringBinary(regionsToDelete[r]));
335         }
336       }
337       newRegion.getRegionInfo().setOffline(true);
338 
339       MetaTableAccessor.addRegionToMeta(table, newRegion.getRegionInfo());
340 
341       if(LOG.isDebugEnabled()) {
342         LOG.debug("updated columns in row: "
343             + Bytes.toStringBinary(newRegion.getRegionName()));
344       }
345     }
346   }
347 }