/** * Convert a HRegionInfo to the protobuf RegionInfo * * @return the converted RegionInfo */ HBaseProtos.RegionInfo convert() { return convert(this); }
private void openWithoutRestoringSnapshot() throws IOException { Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir); SnapshotProtos.SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir); SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshotDesc); List<SnapshotRegionManifest> regionManifests = manifest.getRegionManifests(); if (regionManifests == null) { throw new IllegalArgumentException("Snapshot seems empty, snapshotName: " + snapshotName); } regions = new ArrayList<>(regionManifests.size()); regionManifests.stream().map(r -> HRegionInfo.convert(r.getRegionInfo())) .filter(this::isValidRegion).sorted().forEach(r -> regions.add(r)); htd = manifest.getTableDescriptor(); }
@Override public void readFields(DataInput in) throws IOException { int len = in.readInt(); byte[] buf = new byte[len]; in.readFully(buf); TableSnapshotRegionSplit split = TableSnapshotRegionSplit.PARSER.parseFrom(buf); this.htd = ProtobufUtil.toTableDescriptor(split.getTable()); this.regionInfo = HRegionInfo.convert(split.getRegion()); List<String> locationsList = split.getLocationsList(); this.locations = locationsList.toArray(new String[locationsList.size()]); this.scan = Bytes.toString(Bytes.readByteArray(in)); this.restoreDir = Bytes.toString(Bytes.readByteArray(in)); } }
public static List<HRegionInfo> getRegionInfosFromManifest(SnapshotManifest manifest) { List<SnapshotRegionManifest> regionManifests = manifest.getRegionManifests(); if (regionManifests == null) { throw new IllegalArgumentException("Snapshot seems empty"); } List<HRegionInfo> regionInfos = Lists.newArrayListWithCapacity(regionManifests.size()); for (SnapshotRegionManifest regionManifest : regionManifests) { HRegionInfo hri = HRegionInfo.convert(regionManifest.getRegionInfo()); if (hri.isOffline() && (hri.isSplit() || hri.isSplitParent())) { continue; } regionInfos.add(hri); } return regionInfos; }
@Override public void write(DataOutput out) throws IOException { TableSnapshotRegionSplit.Builder builder = TableSnapshotRegionSplit.newBuilder() .setTable(ProtobufUtil.toTableSchema(htd)) .setRegion(HRegionInfo.convert(regionInfo)); for (String location : locations) { builder.addLocations(location); } TableSnapshotRegionSplit split = builder.build(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); split.writeTo(baos); baos.close(); byte[] buf = baos.toByteArray(); out.writeInt(buf.length); out.write(buf); Bytes.writeByteArray(out, Bytes.toBytes(scan)); Bytes.writeByteArray(out, Bytes.toBytes(restoreDir)); }
/** * Parses an HRegionInfo instance from the passed in stream. Presumes the HRegionInfo was * serialized to the stream with {@link #toDelimitedByteArray()} * @param in * @return An instance of HRegionInfo. * @throws IOException * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 * Use {@link RegionInfo#parseFrom(DataInputStream)}. */ @Deprecated @InterfaceAudience.Private public static HRegionInfo parseFrom(final DataInputStream in) throws IOException { // I need to be able to move back in the stream if this is not a pb serialization so I can // do the Writable decoding instead. int pblen = ProtobufUtil.lengthOfPBMagic(); byte [] pbuf = new byte[pblen]; if (in.markSupported()) { //read it with mark() in.mark(pblen); } //assumption: if Writable serialization, it should be longer than pblen. int read = in.read(pbuf); if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen); if (ProtobufUtil.isPBMagicPrefix(pbuf)) { return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in)); } else { throw new IOException("PB encoded HRegionInfo expected"); } }
/** * @param bytes A pb RegionInfo serialized with a pb magic prefix. * @param offset starting point in the byte array * @param len length to read on the byte array * @return A deserialized {@link HRegionInfo} * @throws DeserializationException * @see #toByteArray() * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 * Use {@link org.apache.hadoop.hbase.client.RegionInfo#parseFrom(byte[], int, int)}. */ @Deprecated public static HRegionInfo parseFrom(final byte [] bytes, int offset, int len) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(bytes, offset, len)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try { HBaseProtos.RegionInfo.Builder builder = HBaseProtos.RegionInfo.newBuilder(); ProtobufUtil.mergeFrom(builder, bytes, pblen + offset, len - pblen); HBaseProtos.RegionInfo ri = builder.build(); return convert(ri); } catch (IOException e) { throw new DeserializationException(e); } } else { throw new DeserializationException("PB encoded HRegionInfo expected"); } }
/** * Convert a HRegionInfo to the protobuf RegionInfo * * @return the converted RegionInfo */ HBaseProtos.RegionInfo convert() { return convert(this); }
@Test public void testConvert() { final TableName tableName = TableName.valueOf("ns1:" + name.getMethodName()); byte[] startKey = Bytes.toBytes("startKey"); byte[] endKey = Bytes.toBytes("endKey"); boolean split = false; long regionId = System.currentTimeMillis(); int replicaId = 42; HRegionInfo hri = new HRegionInfo(tableName, startKey, endKey, split, regionId, replicaId); // convert two times, compare HRegionInfo convertedHri = HRegionInfo.convert(HRegionInfo.convert(hri)); assertEquals(hri, convertedHri); // test convert RegionInfo without replicaId RegionInfo info = RegionInfo.newBuilder() .setTableName(HBaseProtos.TableName.newBuilder() .setQualifier(UnsafeByteOperations.unsafeWrap(tableName.getQualifier())) .setNamespace(UnsafeByteOperations.unsafeWrap(tableName.getNamespace())) .build()) .setStartKey(UnsafeByteOperations.unsafeWrap(startKey)) .setEndKey(UnsafeByteOperations.unsafeWrap(endKey)) .setSplit(split) .setRegionId(regionId) .build(); convertedHri = HRegionInfo.convert(info); HRegionInfo expectedHri = new HRegionInfo(tableName, startKey, endKey, split, regionId, 0); // expecting default replicaId assertEquals(expectedHri, convertedHri); } @Test
private Path createRegionManifest() throws IOException { byte[] startKey = Bytes.toBytes("AAAAAA"); byte[] stopKey = Bytes.toBytes("BBBBBB"); HRegionInfo regionInfo = new HRegionInfo(TABLE_NAME, startKey, stopKey, false); SnapshotRegionManifest.Builder dataRegionManifestBuilder = SnapshotRegionManifest.newBuilder(); dataRegionManifestBuilder.setRegionInfo(HRegionInfo.convert(regionInfo)); for (ColumnFamilyDescriptor hcd: builder.getTableDescriptor().getColumnFamilies()) { SnapshotRegionManifest.FamilyFiles.Builder family = SnapshotRegionManifest.FamilyFiles.newBuilder(); family.setFamilyName(UnsafeByteOperations.unsafeWrap(hcd.getName())); for (int j = 0; j < TEST_NUM_REGIONFILES; ++j) { SnapshotRegionManifest.StoreFile.Builder sfManifest = SnapshotRegionManifest.StoreFile.newBuilder(); sfManifest.setName(String.format("%064d", j)); sfManifest.setFileSize(j * 1024); family.addStoreFiles(sfManifest.build()); } dataRegionManifestBuilder.addFamilyFiles(family.build()); } SnapshotRegionManifest manifest = dataRegionManifestBuilder.build(); Path regionPath = new Path(snapshotDir, SnapshotManifestV2.SNAPSHOT_MANIFEST_PREFIX + regionInfo.getEncodedName()); FSDataOutputStream stream = fs.create(regionPath); try { manifest.writeTo(stream); } finally { stream.close(); } return regionPath; }
@Test public void testRegionInTransition() throws IOException { HBaseProtos.RegionInfo meta_ri = HRegionInfo.convert(HRegionInfo.FIRST_META_REGIONINFO); HBaseProtos.RegionInfo normal_ri = HRegionInfo.convert( new HRegionInfo(TableName.valueOf("test:table"), Bytes.toBytes("a"), Bytes.toBytes("b"), false));
dataRegionManifestBuilder.setRegionInfo(HRegionInfo.convert(regionInfo)); dataManifestBuilder.addRegionManifests(dataRegionManifestBuilder.build());
/** * Parses an HRegionInfo instance from the passed in stream. Presumes the HRegionInfo was * serialized to the stream with {@link #toDelimitedByteArray()} * @param in * @return An instance of HRegionInfo. * @throws IOException * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 * Use {@link RegionInfo#parseFrom(DataInputStream)}. */ @Deprecated @InterfaceAudience.Private public static HRegionInfo parseFrom(final DataInputStream in) throws IOException { // I need to be able to move back in the stream if this is not a pb serialization so I can // do the Writable decoding instead. int pblen = ProtobufUtil.lengthOfPBMagic(); byte [] pbuf = new byte[pblen]; if (in.markSupported()) { //read it with mark() in.mark(pblen); } //assumption: if Writable serialization, it should be longer than pblen. int read = in.read(pbuf); if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen); if (ProtobufUtil.isPBMagicPrefix(pbuf)) { return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in)); } else { throw new IOException("PB encoded HRegionInfo expected"); } }
/** * @param bytes A pb RegionInfo serialized with a pb magic prefix. * @param offset starting point in the byte array * @param len length to read on the byte array * @return A deserialized {@link HRegionInfo} * @throws DeserializationException * @see #toByteArray() * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 * Use {@link org.apache.hadoop.hbase.client.RegionInfo#parseFrom(byte[], int, int)}. */ @Deprecated public static HRegionInfo parseFrom(final byte [] bytes, int offset, int len) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(bytes, offset, len)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try { HBaseProtos.RegionInfo.Builder builder = HBaseProtos.RegionInfo.newBuilder(); ProtobufUtil.mergeFrom(builder, bytes, pblen + offset, len - pblen); HBaseProtos.RegionInfo ri = builder.build(); return convert(ri); } catch (IOException e) { throw new DeserializationException(e); } } else { throw new DeserializationException("PB encoded HRegionInfo expected"); } }
/** * Convert a HRegionInfo to the protobuf RegionInfo * * @return the converted RegionInfo */ HBaseProtos.RegionInfo convert() { return convert(this); }
/** * Convert a HRegionInfo to the protobuf RegionInfo * * @return the converted RegionInfo */ RegionInfo convert() { return convert(this); }
public static List<HRegionInfo> getRegionInfosFromManifest(SnapshotManifest manifest) { List<SnapshotRegionManifest> regionManifests = manifest.getRegionManifests(); if (regionManifests == null) { throw new IllegalArgumentException("Snapshot seems empty"); } List<HRegionInfo> regionInfos = Lists.newArrayListWithCapacity(regionManifests.size()); for (SnapshotRegionManifest regionManifest : regionManifests) { regionInfos.add(HRegionInfo.convert(regionManifest.getRegionInfo())); } return regionInfos; }
public SnapshotRegionManifest.Builder regionOpen(final HRegionInfo regionInfo) { SnapshotRegionManifest.Builder manifest = SnapshotRegionManifest.newBuilder(); manifest.setRegionInfo(HRegionInfo.convert(regionInfo)); return manifest; }
/** * @return This instance serialized as protobuf w/ a magic pb prefix. * @see #parseFrom(byte[]) */ public byte [] toByteArray() { byte [] bytes = convert().toByteArray(); return ProtobufUtil.prependPBMagic(bytes); }
/** * Convert a protobuf HBaseProtos.RegionState to a RegionState * * @return the RegionState */ public static RegionState convert(ClusterStatusProtos.RegionState proto) { return new RegionState(HRegionInfo.convert(proto.getRegionInfo()), State.convert(proto.getState()), proto.getStamp(), null); }