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.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.regionserver.HRegion;
37 import org.apache.hadoop.hbase.regionserver.wal.HLog;
38 import org.apache.hadoop.hbase.regionserver.wal.HLogFactory;
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 FileSystem fs;
52 private HLog log;
53 private HRegion metaRegion;
54 private Map<byte [], HRegion> metaRegions = Collections.synchronizedSortedMap(
55 new TreeMap<byte [], HRegion>(Bytes.BYTES_COMPARATOR));
56
57
58
59
60 public MetaUtils() throws IOException {
61 this(HBaseConfiguration.create());
62 }
63
64
65
66
67
68 public MetaUtils(Configuration conf) throws IOException {
69 this.conf = conf;
70 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
71 this.metaRegion = null;
72 initialize();
73 }
74
75
76
77
78
79 private void initialize() throws IOException {
80 this.fs = FileSystem.get(this.conf);
81 }
82
83
84
85
86
87 public synchronized HLog getLog() throws IOException {
88 if (this.log == null) {
89 String logName =
90 HConstants.HREGION_LOGDIR_NAME + "_" + System.currentTimeMillis();
91 this.log = HLogFactory.createHLog(this.fs, this.fs.getHomeDirectory(),
92 logName, this.conf);
93 }
94 return this.log;
95 }
96
97
98
99
100
101 public HRegion getMetaRegion() throws IOException {
102 if (this.metaRegion == null) {
103 openMetaRegion();
104 }
105 return this.metaRegion;
106 }
107
108
109
110
111
112
113 public void shutdown() {
114 if (this.metaRegion != null) {
115 try {
116 this.metaRegion.close();
117 } catch (IOException e) {
118 LOG.error("closing meta region", e);
119 } finally {
120 this.metaRegion = null;
121 }
122 }
123 try {
124 for (HRegion r: metaRegions.values()) {
125 LOG.info("CLOSING hbase:meta " + r.toString());
126 r.close();
127 }
128 } catch (IOException e) {
129 LOG.error("closing meta region", e);
130 } finally {
131 metaRegions.clear();
132 }
133 try {
134 if (this.log != null) {
135 this.log.rollWriter();
136 this.log.closeAndDelete();
137 }
138 } catch (IOException e) {
139 LOG.error("closing HLog", e);
140 } finally {
141 this.log = null;
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 HTableDescriptor.META_TABLEDESC, getLog(),
151 this.conf);
152 this.metaRegion.compactStores();
153 return this.metaRegion;
154 }
155 }