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