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.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.classification.InterfaceAudience;
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.fs.FileSystem;
34  import org.apache.hadoop.hbase.HBaseConfiguration;
35  import org.apache.hadoop.hbase.HConstants;
36  import org.apache.hadoop.hbase.HRegionInfo;
37  import org.apache.hadoop.hbase.HTableDescriptor;
38  import org.apache.hadoop.hbase.KeyValue;
39  import org.apache.hadoop.hbase.catalog.MetaEditor;
40  import org.apache.hadoop.hbase.client.Delete;
41  import org.apache.hadoop.hbase.client.Get;
42  import org.apache.hadoop.hbase.client.HTable;
43  import org.apache.hadoop.hbase.client.Put;
44  import org.apache.hadoop.hbase.client.Result;
45  import org.apache.hadoop.hbase.client.Scan;
46  import org.apache.hadoop.hbase.regionserver.HRegion;
47  import org.apache.hadoop.hbase.regionserver.InternalScanner;
48  import org.apache.hadoop.hbase.regionserver.wal.HLog;
49  import org.apache.hadoop.hbase.regionserver.wal.HLogFactory;
50  
51  /**
52   * Contains utility methods for manipulating HBase meta tables.
53   * Be sure to call {@link #shutdown()} when done with this class so it closes
54   * resources opened during meta processing (ROOT, META, etc.).  Be careful
55   * how you use this class.  If used during migrations, be careful when using
56   * this class to check whether migration is needed.
57   */
58  @InterfaceAudience.Private
59  public class MetaUtils {
60    private static final Log LOG = LogFactory.getLog(MetaUtils.class);
61    private final Configuration conf;
62    private FileSystem fs;
63    private HLog log;
64    private HRegion metaRegion;
65    private Map<byte [], HRegion> metaRegions = Collections.synchronizedSortedMap(
66      new TreeMap<byte [], HRegion>(Bytes.BYTES_COMPARATOR));
67  
68    /** Default constructor
69     * @throws IOException e
70     */
71    public MetaUtils() throws IOException {
72      this(HBaseConfiguration.create());
73    }
74  
75    /**
76     * @param conf Configuration
77     * @throws IOException e
78     */
79    public MetaUtils(Configuration conf) throws IOException {
80      this.conf = conf;
81      conf.setInt("hbase.client.retries.number", 1);
82      this.metaRegion = null;
83      initialize();
84    }
85  
86    /**
87     * Verifies that DFS is available and that HBase is off-line.
88     * @throws IOException e
89     */
90    private void initialize() throws IOException {
91      this.fs = FileSystem.get(this.conf);
92    }
93  
94    /**
95     * @return the HLog
96     * @throws IOException e
97     */
98    public synchronized HLog getLog() throws IOException {
99      if (this.log == null) {
100       String logName = 
101           HConstants.HREGION_LOGDIR_NAME + "_" + System.currentTimeMillis();
102       this.log = HLogFactory.createHLog(this.fs, this.fs.getHomeDirectory(),
103                                         logName, this.conf);
104     }
105     return this.log;
106   }
107 
108   /**
109    * @return HRegion for meta region
110    * @throws IOException e
111    */
112   public HRegion getMetaRegion() throws IOException {
113     if (this.metaRegion == null) {
114       openMetaRegion();
115     }
116     return this.metaRegion;
117   }
118 
119   /**
120    * Closes catalog regions if open. Also closes and deletes the HLog. You
121    * must call this method if you want to persist changes made during a
122    * MetaUtils edit session.
123    */
124   public void shutdown() {
125     if (this.metaRegion != null) {
126       try {
127         this.metaRegion.close();
128       } catch (IOException e) {
129         LOG.error("closing meta region", e);
130       } finally {
131         this.metaRegion = null;
132       }
133     }
134     try {
135       for (HRegion r: metaRegions.values()) {
136         LOG.info("CLOSING META " + r.toString());
137         r.close();
138       }
139     } catch (IOException e) {
140       LOG.error("closing meta region", e);
141     } finally {
142       metaRegions.clear();
143     }
144     try {
145       if (this.log != null) {
146         this.log.rollWriter();
147         this.log.closeAndDelete();
148       }
149     } catch (IOException e) {
150       LOG.error("closing HLog", e);
151     } finally {
152       this.log = null;
153     }
154   }
155 
156   private synchronized HRegion openMetaRegion() throws IOException {
157     if (this.metaRegion != null) {
158       return this.metaRegion;
159     }
160     this.metaRegion = HRegion.openHRegion(HRegionInfo.FIRST_META_REGIONINFO,
161       HTableDescriptor.META_TABLEDESC, getLog(),
162       this.conf);
163     this.metaRegion.compactStores();
164     return this.metaRegion;
165   }
166 }