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.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.classification.InterfaceAudience;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.fs.FileSystem;
34 import org.apache.hadoop.hbase.HBaseConfiguration;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.HRegionInfo;
37 import org.apache.hadoop.hbase.HTableDescriptor;
38 import org.apache.hadoop.hbase.KeyValue;
39 import org.apache.hadoop.hbase.catalog.MetaEditor;
40 import org.apache.hadoop.hbase.client.Delete;
41 import org.apache.hadoop.hbase.client.Get;
42 import org.apache.hadoop.hbase.client.HTable;
43 import org.apache.hadoop.hbase.client.Put;
44 import org.apache.hadoop.hbase.client.Result;
45 import org.apache.hadoop.hbase.client.Scan;
46 import org.apache.hadoop.hbase.regionserver.HRegion;
47 import org.apache.hadoop.hbase.regionserver.InternalScanner;
48 import org.apache.hadoop.hbase.regionserver.wal.HLog;
49 import org.apache.hadoop.hbase.regionserver.wal.HLogFactory;
50
51
52
53
54
55
56
57
58 @InterfaceAudience.Private
59 public class MetaUtils {
60 private static final Log LOG = LogFactory.getLog(MetaUtils.class);
61 private final Configuration conf;
62 private FileSystem fs;
63 private HLog log;
64 private HRegion metaRegion;
65 private Map<byte [], HRegion> metaRegions = Collections.synchronizedSortedMap(
66 new TreeMap<byte [], HRegion>(Bytes.BYTES_COMPARATOR));
67
68
69
70
71 public MetaUtils() throws IOException {
72 this(HBaseConfiguration.create());
73 }
74
75
76
77
78
79 public MetaUtils(Configuration conf) throws IOException {
80 this.conf = conf;
81 conf.setInt("hbase.client.retries.number", 1);
82 this.metaRegion = null;
83 initialize();
84 }
85
86
87
88
89
90 private void initialize() throws IOException {
91 this.fs = FileSystem.get(this.conf);
92 }
93
94
95
96
97
98 public synchronized HLog getLog() throws IOException {
99 if (this.log == null) {
100 String logName =
101 HConstants.HREGION_LOGDIR_NAME + "_" + System.currentTimeMillis();
102 this.log = HLogFactory.createHLog(this.fs, this.fs.getHomeDirectory(),
103 logName, this.conf);
104 }
105 return this.log;
106 }
107
108
109
110
111
112 public HRegion getMetaRegion() throws IOException {
113 if (this.metaRegion == null) {
114 openMetaRegion();
115 }
116 return this.metaRegion;
117 }
118
119
120
121
122
123
124 public void shutdown() {
125 if (this.metaRegion != null) {
126 try {
127 this.metaRegion.close();
128 } catch (IOException e) {
129 LOG.error("closing meta region", e);
130 } finally {
131 this.metaRegion = null;
132 }
133 }
134 try {
135 for (HRegion r: metaRegions.values()) {
136 LOG.info("CLOSING META " + r.toString());
137 r.close();
138 }
139 } catch (IOException e) {
140 LOG.error("closing meta region", e);
141 } finally {
142 metaRegions.clear();
143 }
144 try {
145 if (this.log != null) {
146 this.log.rollWriter();
147 this.log.closeAndDelete();
148 }
149 } catch (IOException e) {
150 LOG.error("closing HLog", e);
151 } finally {
152 this.log = null;
153 }
154 }
155
156 private synchronized HRegion openMetaRegion() throws IOException {
157 if (this.metaRegion != null) {
158 return this.metaRegion;
159 }
160 this.metaRegion = HRegion.openHRegion(HRegionInfo.FIRST_META_REGIONINFO,
161 HTableDescriptor.META_TABLEDESC, getLog(),
162 this.conf);
163 this.metaRegion.compactStores();
164 return this.metaRegion;
165 }
166 }