1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.catalog;
20
21 import java.io.IOException;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.exceptions.DeserializationException;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.catalog.MetaReader.Visitor;
30 import org.apache.hadoop.hbase.client.Put;
31 import org.apache.hadoop.hbase.client.Result;
32 import org.apache.hadoop.hbase.master.MasterServices;
33 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34 import org.apache.hadoop.hbase.util.Bytes;
35
36
37
38
39
40
41 @Deprecated
42 public class MetaMigrationConvertingToPB {
43
44 private static final Log LOG = LogFactory.getLog(MetaMigrationConvertingToPB.class);
45
46 private static class ConvertToPBMetaVisitor implements Visitor {
47 private final MasterServices services;
48 private long numMigratedRows;
49
50 public ConvertToPBMetaVisitor(MasterServices services) {
51 this.services = services;
52 numMigratedRows = 0;
53 }
54
55 @Override
56 public boolean visit(Result r) throws IOException {
57 if (r == null || r.isEmpty()) return true;
58
59
60 byte [] hriBytes = getBytes(r, HConstants.REGIONINFO_QUALIFIER);
61
62
63
64 if (isMigrated(hriBytes)) return true;
65
66
67
68
69 HRegionInfo hri = parseFrom(hriBytes);
70
71
72 Put p = MetaEditor.makePutFromRegionInfo(hri);
73
74
75 migrateSplitIfNecessary(r, p, HConstants.SPLITA_QUALIFIER);
76 migrateSplitIfNecessary(r, p, HConstants.SPLITB_QUALIFIER);
77
78 MetaEditor.putToCatalogTable(this.services.getCatalogTracker(), p);
79 if (LOG.isDebugEnabled()) {
80 LOG.debug("Migrated " + Bytes.toString(p.getRow()));
81 }
82 numMigratedRows++;
83 return true;
84 }
85 }
86
87 static void migrateSplitIfNecessary(final Result r, final Put p, final byte [] which)
88 throws IOException {
89 byte [] hriSplitBytes = getBytes(r, which);
90 if (!isMigrated(hriSplitBytes)) {
91
92
93 HRegionInfo hri = parseFrom(hriSplitBytes);
94 p.addImmutable(HConstants.CATALOG_FAMILY, which, hri.toByteArray());
95 }
96 }
97
98 static HRegionInfo parseFrom(byte[] hriBytes) throws IOException {
99 try {
100 return HRegionInfo.parseFrom(hriBytes);
101 } catch (DeserializationException ex) {
102 throw new IOException(ex);
103 }
104 }
105
106
107
108
109
110
111 static byte [] getBytes(final Result r, final byte [] qualifier) {
112 byte [] hriBytes = r.getValue(HConstants.CATALOG_FAMILY, qualifier);
113 if (hriBytes == null || hriBytes.length <= 0) return null;
114 return hriBytes;
115 }
116
117 static boolean isMigrated(final byte [] hriBytes) {
118 if (hriBytes == null || hriBytes.length <= 0) return true;
119
120 return ProtobufUtil.isPBMagicPrefix(hriBytes);
121 }
122
123
124
125
126
127
128
129 public static long updateMetaIfNecessary(final MasterServices services)
130 throws IOException {
131 if (isMetaTableUpdated(services.getCatalogTracker())) {
132 LOG.info("META already up-to date with PB serialization");
133 return 0;
134 }
135 LOG.info("META has Writable serializations, migrating hbase:meta to PB serialization");
136 try {
137 long rows = updateMeta(services);
138 LOG.info("META updated with PB serialization. Total rows updated: " + rows);
139 return rows;
140 } catch (IOException e) {
141 LOG.warn("Update hbase:meta with PB serialization failed." + "Master startup aborted.");
142 throw e;
143 }
144 }
145
146
147
148
149
150 static long updateMeta(final MasterServices masterServices) throws IOException {
151 LOG.info("Starting update of META");
152 ConvertToPBMetaVisitor v = new ConvertToPBMetaVisitor(masterServices);
153 MetaReader.fullScan(masterServices.getCatalogTracker(), v);
154 LOG.info("Finished update of META. Total rows updated:" + v.numMigratedRows);
155 return v.numMigratedRows;
156 }
157
158
159
160
161
162
163 static boolean isMetaTableUpdated(final CatalogTracker catalogTracker) throws IOException {
164 List<Result> results = MetaReader.fullScanOfMeta(catalogTracker);
165 if (results == null || results.isEmpty()) {
166 LOG.info("hbase:meta doesn't have any entries to update.");
167 return true;
168 }
169 for (Result r : results) {
170 byte[] value = r.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
171 if (!isMigrated(value)) {
172 return false;
173 }
174 }
175 return true;
176 }
177 }