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.catalog;
21
22 import java.io.IOException;
23 import java.net.ConnectException;
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.HServerInfo;
30 import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
31 import org.apache.hadoop.hbase.client.Delete;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.ipc.HRegionInterface;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.apache.hadoop.hbase.util.Writables;
36
37
38
39
40
41
42
43 public class MetaEditor {
44 private static final Log LOG = LogFactory.getLog(MetaEditor.class);
45
46
47
48
49
50
51 public static void addRegionToMeta(CatalogTracker catalogTracker,
52 HRegionInfo regionInfo)
53 throws IOException {
54 Put put = new Put(regionInfo.getRegionName());
55 put.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
56 Writables.getBytes(regionInfo));
57 catalogTracker.waitForMetaServerConnectionDefault().put(
58 CatalogTracker.META_REGION, put);
59 LOG.info("Added region " + regionInfo.getRegionNameAsString() + " to META");
60 }
61
62
63
64
65
66
67
68
69
70
71
72 public static void offlineParentInMeta(CatalogTracker catalogTracker,
73 HRegionInfo parent, final HRegionInfo a, final HRegionInfo b)
74 throws NotAllMetaRegionsOnlineException, IOException {
75 HRegionInfo copyOfParent = new HRegionInfo(parent);
76 copyOfParent.setOffline(true);
77 copyOfParent.setSplit(true);
78 Put put = new Put(copyOfParent.getRegionName());
79 addRegionInfo(put, copyOfParent);
80 put.add(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
81 Writables.getBytes(a));
82 put.add(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
83 Writables.getBytes(b));
84 catalogTracker.waitForMetaServerConnectionDefault().put(CatalogTracker.META_REGION, put);
85 LOG.info("Offlined parent region " + parent.getRegionNameAsString() +
86 " in META");
87 }
88
89 public static void addDaughter(final CatalogTracker catalogTracker,
90 final HRegionInfo regionInfo, final HServerInfo serverInfo)
91 throws NotAllMetaRegionsOnlineException, IOException {
92 HRegionInterface server = catalogTracker.waitForMetaServerConnectionDefault();
93 byte [] catalogRegionName = CatalogTracker.META_REGION;
94 Put put = new Put(regionInfo.getRegionName());
95 addRegionInfo(put, regionInfo);
96 if (serverInfo != null) addLocation(put, serverInfo);
97 server.put(catalogRegionName, put);
98 LOG.info("Added daughter " + regionInfo.getRegionNameAsString() +
99 " in region " + Bytes.toString(catalogRegionName) +
100 (serverInfo == null?
101 ", serverInfo=null": ", serverInfo=" + serverInfo.getServerName()));
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public static void updateMetaLocation(CatalogTracker catalogTracker,
120 HRegionInfo regionInfo, HServerInfo serverInfo)
121 throws IOException, ConnectException {
122 HRegionInterface server = catalogTracker.waitForRootServerConnectionDefault();
123 if (server == null) throw new IOException("No server for -ROOT-");
124 updateLocation(server, CatalogTracker.ROOT_REGION, regionInfo, serverInfo);
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public static void updateRegionLocation(CatalogTracker catalogTracker,
140 HRegionInfo regionInfo, HServerInfo serverInfo)
141 throws IOException {
142 updateLocation(catalogTracker.waitForMetaServerConnectionDefault(),
143 CatalogTracker.META_REGION, regionInfo, serverInfo);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 private static void updateLocation(HRegionInterface server,
160 byte [] catalogRegionName, HRegionInfo regionInfo, HServerInfo serverInfo)
161 throws IOException {
162 Put put = new Put(regionInfo.getRegionName());
163 addLocation(put, serverInfo);
164 server.put(catalogRegionName, put);
165 LOG.info("Updated row " + regionInfo.getRegionNameAsString() +
166 " in region " + Bytes.toStringBinary(catalogRegionName) + " with " +
167 "server=" + serverInfo.getHostnamePort() + ", " +
168 "startcode=" + serverInfo.getStartCode());
169 }
170
171
172
173
174
175
176
177 public static void deleteRegion(CatalogTracker catalogTracker,
178 HRegionInfo regionInfo)
179 throws IOException {
180 Delete delete = new Delete(regionInfo.getRegionName());
181 catalogTracker.waitForMetaServerConnectionDefault().
182 delete(CatalogTracker.META_REGION, delete);
183 LOG.info("Deleted region " + regionInfo.getRegionNameAsString() + " from META");
184 }
185
186
187
188
189
190
191
192
193
194
195 public static void deleteDaughterReferenceInParent(CatalogTracker catalogTracker,
196 final HRegionInfo parent, final byte [] qualifier,
197 final HRegionInfo daughter)
198 throws NotAllMetaRegionsOnlineException, IOException {
199 Delete delete = new Delete(parent.getRegionName());
200 delete.deleteColumns(HConstants.CATALOG_FAMILY, qualifier);
201 catalogTracker.waitForMetaServerConnectionDefault().
202 delete(CatalogTracker.META_REGION, delete);
203 LOG.info("Deleted daughter reference " + daughter.getRegionNameAsString() +
204 ", qualifier=" + Bytes.toStringBinary(qualifier) + ", from parent " +
205 parent.getRegionNameAsString());
206 }
207
208
209
210
211
212
213
214 public static void updateRegionInfo(CatalogTracker catalogTracker,
215 HRegionInfo regionInfo)
216 throws IOException {
217 Put put = new Put(regionInfo.getRegionName());
218 addRegionInfo(put, regionInfo);
219 catalogTracker.waitForMetaServerConnectionDefault().put(
220 CatalogTracker.META_REGION, put);
221 LOG.info("Updated region " + regionInfo.getRegionNameAsString() + " in META");
222 }
223
224 private static Put addRegionInfo(final Put p, final HRegionInfo hri)
225 throws IOException {
226 p.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
227 Writables.getBytes(hri));
228 return p;
229 }
230
231 private static Put addLocation(final Put p, final HServerInfo hsi) {
232 p.add(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
233 Bytes.toBytes(hsi.getHostnamePort()));
234 p.add(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
235 Bytes.toBytes(hsi.getStartCode()));
236 return p;
237 }
238 }