1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.catalog;
19
20 import java.io.IOException;
21 import java.net.ConnectException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
30 import org.apache.hadoop.hbase.ServerName;
31 import org.apache.hadoop.hbase.client.Delete;
32 import org.apache.hadoop.hbase.client.HTable;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.client.Result;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.apache.hadoop.hbase.util.Writables;
37
38
39
40
41
42
43 public class MetaEditor {
44
45
46
47 private static final Log LOG = LogFactory.getLog(MetaEditor.class);
48
49 private static Put makePutFromRegionInfo(HRegionInfo regionInfo)
50 throws IOException {
51 Put put = new Put(regionInfo.getRegionName());
52 put.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
53 Writables.getBytes(regionInfo));
54 return put;
55 }
56
57
58
59
60
61
62
63 static void putToMetaTable(final CatalogTracker ct, final Put p)
64 throws IOException {
65 put(MetaReader.getMetaHTable(ct), p);
66 }
67
68
69
70
71
72
73
74 static void putToRootTable(final CatalogTracker ct, final Put p)
75 throws IOException {
76 put(MetaReader.getRootHTable(ct), p);
77 }
78
79
80
81
82
83
84
85 static void putToCatalogTable(final CatalogTracker ct, final Put p)
86 throws IOException {
87 HTable t = MetaReader.getCatalogHTable(ct, p.getRow());
88 put(t, p);
89 }
90
91
92
93
94
95
96 private static void put(final HTable t, final Put p) throws IOException {
97 try {
98 t.put(p);
99 } finally {
100 t.close();
101 }
102 }
103
104
105
106
107
108
109
110 static void putsToMetaTable(final CatalogTracker ct, final List<Put> ps)
111 throws IOException {
112 HTable t = MetaReader.getMetaHTable(ct);
113 try {
114 t.put(ps);
115 } finally {
116 t.close();
117 }
118 }
119
120
121
122
123
124
125
126 static void deleteMetaTable(final CatalogTracker ct, final Delete d)
127 throws IOException {
128 HTable t = MetaReader.getMetaHTable(ct);
129 try {
130 t.delete(d);
131 } finally {
132 t.close();
133 }
134 }
135
136
137
138
139
140
141 public static void addRegionToMeta(CatalogTracker catalogTracker,
142 HRegionInfo regionInfo)
143 throws IOException {
144 putToMetaTable(catalogTracker, makePutFromRegionInfo(regionInfo));
145 LOG.info("Added region " + regionInfo.getRegionNameAsString() + " to META");
146 }
147
148
149
150
151
152
153
154 public static void addRegionsToMeta(CatalogTracker catalogTracker,
155 List<HRegionInfo> regionInfos)
156 throws IOException {
157 List<Put> puts = new ArrayList<Put>();
158 for (HRegionInfo regionInfo : regionInfos) {
159 puts.add(makePutFromRegionInfo(regionInfo));
160 }
161 putsToMetaTable(catalogTracker, puts);
162 LOG.info("Added " + puts.size() + " regions in META");
163 }
164
165
166
167
168
169
170
171
172
173
174
175 public static void offlineParentInMeta(CatalogTracker catalogTracker,
176 HRegionInfo parent, final HRegionInfo a, final HRegionInfo b)
177 throws NotAllMetaRegionsOnlineException, IOException {
178 HRegionInfo copyOfParent = new HRegionInfo(parent);
179 copyOfParent.setOffline(true);
180 copyOfParent.setSplit(true);
181 Put put = new Put(copyOfParent.getRegionName());
182 addRegionInfo(put, copyOfParent);
183 put.add(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
184 Writables.getBytes(a));
185 put.add(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
186 Writables.getBytes(b));
187 putToMetaTable(catalogTracker, put);
188 LOG.info("Offlined parent region " + parent.getRegionNameAsString() +
189 " in META");
190 }
191
192 public static void addDaughter(final CatalogTracker catalogTracker,
193 final HRegionInfo regionInfo, final ServerName sn)
194 throws NotAllMetaRegionsOnlineException, IOException {
195 Put put = new Put(regionInfo.getRegionName());
196 addRegionInfo(put, regionInfo);
197 if (sn != null) addLocation(put, sn);
198 putToMetaTable(catalogTracker, put);
199 LOG.info("Added daughter " + regionInfo.getRegionNameAsString() +
200 (sn == null? ", serverName=null": ", serverName=" + sn.toString()));
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 public static void updateMetaLocation(CatalogTracker catalogTracker,
219 HRegionInfo regionInfo, ServerName sn)
220 throws IOException, ConnectException {
221 updateLocation(catalogTracker, regionInfo, sn);
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236 public static void updateRegionLocation(CatalogTracker catalogTracker,
237 HRegionInfo regionInfo, ServerName sn)
238 throws IOException {
239 updateLocation(catalogTracker, regionInfo, sn);
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254 private static void updateLocation(final CatalogTracker catalogTracker,
255 HRegionInfo regionInfo, ServerName sn)
256 throws IOException {
257 Put put = new Put(regionInfo.getRegionName());
258 addLocation(put, sn);
259 putToCatalogTable(catalogTracker, put);
260 LOG.info("Updated row " + regionInfo.getRegionNameAsString() +
261 " with server=" + sn);
262 }
263
264
265
266
267
268
269
270 public static void deleteRegion(CatalogTracker catalogTracker,
271 HRegionInfo regionInfo)
272 throws IOException {
273 Delete delete = new Delete(regionInfo.getRegionName());
274 deleteMetaTable(catalogTracker, delete);
275 LOG.info("Deleted region " + regionInfo.getRegionNameAsString() + " from META");
276 }
277
278
279
280
281
282
283
284
285 public static void deleteDaughtersReferencesInParent(CatalogTracker catalogTracker,
286 final HRegionInfo parent)
287 throws NotAllMetaRegionsOnlineException, IOException {
288 Delete delete = new Delete(parent.getRegionName());
289 delete.deleteColumns(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER);
290 delete.deleteColumns(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER);
291 deleteMetaTable(catalogTracker, delete);
292 LOG.info("Deleted daughters references, qualifier=" + Bytes.toStringBinary(HConstants.SPLITA_QUALIFIER) +
293 " and qualifier=" + Bytes.toStringBinary(HConstants.SPLITB_QUALIFIER) +
294 ", from parent " + parent.getRegionNameAsString());
295 }
296
297 public static HRegionInfo getHRegionInfo(
298 Result data) throws IOException {
299 byte [] bytes =
300 data.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
301 if (bytes == null) return null;
302 HRegionInfo info = Writables.getHRegionInfo(bytes);
303 LOG.info("Current INFO from scan results = " + info);
304 return info;
305 }
306
307 private static Put addRegionInfo(final Put p, final HRegionInfo hri)
308 throws IOException {
309 p.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
310 Writables.getBytes(hri));
311 return p;
312 }
313
314 private static Put addLocation(final Put p, final ServerName sn) {
315 p.add(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
316 Bytes.toBytes(sn.getHostAndPort()));
317 p.add(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
318 Bytes.toBytes(sn.getStartcode()));
319 return p;
320 }
321 }