1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
45
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
59
60
61 public MetaUtils() throws IOException {
62 this(HBaseConfiguration.create());
63 }
64
65
66
67
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
79
80
81 private void initialize() throws IOException {
82 this.fs = FileSystem.get(this.conf);
83 }
84
85
86
87
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 return info.isMetaRegion() ? walFactory.getMetaWAL(region) : walFactory.getWAL(region);
99 }
100
101
102
103
104
105 public HRegion getMetaRegion() throws IOException {
106 if (this.metaRegion == null) {
107 openMetaRegion();
108 }
109 return this.metaRegion;
110 }
111
112
113
114
115
116
117 public synchronized void shutdown() {
118 if (this.metaRegion != null) {
119 try {
120 this.metaRegion.close();
121 } catch (IOException e) {
122 LOG.error("closing meta region", e);
123 } finally {
124 this.metaRegion = null;
125 }
126 }
127 try {
128 for (HRegion r: metaRegions.values()) {
129 LOG.info("CLOSING hbase:meta " + r.toString());
130 r.close();
131 }
132 } catch (IOException e) {
133 LOG.error("closing meta region", e);
134 } finally {
135 metaRegions.clear();
136 }
137 try {
138 if (this.walFactory != null) {
139 this.walFactory.close();
140 }
141 } catch (IOException e) {
142 LOG.error("closing WAL", e);
143 }
144 }
145
146 private synchronized HRegion openMetaRegion() throws IOException {
147 if (this.metaRegion != null) {
148 return this.metaRegion;
149 }
150 this.metaRegion = HRegion.openHRegion(HRegionInfo.FIRST_META_REGIONINFO,
151 descriptors.get(TableName.META_TABLE_NAME), getLog(HRegionInfo.FIRST_META_REGIONINFO),
152 this.conf);
153 this.metaRegion.compactStores();
154 return this.metaRegion;
155 }
156 }