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.wal.WAL;
39  import org.apache.hadoop.hbase.wal.WALFactory;
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 WALFactory walFactory;
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 WAL associated with the given region
88     * @throws IOException e
89     */
90    public synchronized WAL getLog(HRegionInfo info) throws IOException {
91      if (this.walFactory == null) {
92        String logName = 
93            HConstants.HREGION_LOGDIR_NAME + "_" + System.currentTimeMillis();
94        final Configuration walConf = new Configuration(this.conf);
95        FSUtils.setRootDir(walConf, fs.getHomeDirectory());
96        this.walFactory = new WALFactory(walConf, null, logName);
97      }
98      final byte[] region = info.getEncodedNameAsBytes();
99      return info.isMetaRegion() ? walFactory.getMetaWAL(region) : walFactory.getWAL(region);
100   }
101 
102   /**
103    * @return HRegion for meta region
104    * @throws IOException e
105    */
106   public HRegion getMetaRegion() throws IOException {
107     if (this.metaRegion == null) {
108       openMetaRegion();
109     }
110     return this.metaRegion;
111   }
112 
113   /**
114    * Closes catalog regions if open. Also closes and deletes the WAL. You
115    * must call this method if you want to persist changes made during a
116    * MetaUtils edit session.
117    */
118   public synchronized void shutdown() {
119     if (this.metaRegion != null) {
120       try {
121         this.metaRegion.close();
122       } catch (IOException e) {
123         LOG.error("closing meta region", e);
124       } finally {
125         this.metaRegion = null;
126       }
127     }
128     try {
129       for (HRegion r: metaRegions.values()) {
130         LOG.info("CLOSING hbase:meta " + r.toString());
131         r.close();
132       }
133     } catch (IOException e) {
134       LOG.error("closing meta region", e);
135     } finally {
136       metaRegions.clear();
137     }
138     try {
139       if (this.walFactory != null) {
140         this.walFactory.close();
141       }
142     } catch (IOException e) {
143       LOG.error("closing WAL", e);
144     }
145   }
146 
147   private synchronized HRegion openMetaRegion() throws IOException {
148     if (this.metaRegion != null) {
149       return this.metaRegion;
150     }
151     this.metaRegion = HRegion.openHRegion(HRegionInfo.FIRST_META_REGIONINFO,
152       descriptors.get(TableName.META_TABLE_NAME), getLog(HRegionInfo.FIRST_META_REGIONINFO),
153       this.conf);
154     this.metaRegion.compactStores();
155     return this.metaRegion;
156   }
157 }