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