/** * @param bytes bytes to check * @throws DeserializationException if we are missing the pb magic prefix */ public static void expectPBMagicPrefix(final byte[] bytes) throws DeserializationException { if (!isPBMagicPrefix(bytes)) { String bytesPrefix = bytes == null ? "null" : Bytes.toStringBinary(bytes, 0, PB_MAGIC.length); throw new DeserializationException( "Missing pb magic " + Bytes.toString(PB_MAGIC) + " prefix, bytes: " + bytesPrefix); } }
/** * Reads back User auth data written to zookeeper. * @param data * @return User auth details * @throws DeserializationException */ public static MultiUserAuthorizations readUserAuthsFromZKData(byte[] data) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(data)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try { MultiUserAuthorizations.Builder builder = MultiUserAuthorizations.newBuilder(); ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen); return builder.build(); } catch (IOException e) { throw new DeserializationException(e); } } return null; }
public static ListMultimap<String, Permission> readPermissions(byte[] data, Configuration conf) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(data)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try { AccessControlProtos.UsersAndPermissions.Builder builder = AccessControlProtos.UsersAndPermissions.newBuilder(); ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen); return AccessControlUtil.toPermission(builder.build()); } catch (IOException e) { throw new DeserializationException(e); } } else { // TODO: We have to re-write non-PB data as PB encoded. Otherwise we will carry old Writables // forever (here and a couple of other places). ListMultimap<String, Permission> perms = ArrayListMultimap.create(); try { DataInput in = new DataInputStream(new ByteArrayInputStream(data)); int length = in.readInt(); for (int i = 0; i < length; i++) { String user = Text.readString(in); perms.putAll(user, readWritableUserPermission(in, conf)); } } catch (IOException | ClassNotFoundException e) { throw new DeserializationException(e); } return perms; } }
/** * Reads back from the zookeeper. The data read here is of the form written by * writeToZooKeeper(Map<byte[], Integer> entries). * * @param data * @return Labels and their ordinal details * @throws DeserializationException */ public static List<VisibilityLabel> readLabelsFromZKData(byte[] data) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(data)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try { VisibilityLabelsRequest.Builder builder = VisibilityLabelsRequest.newBuilder(); ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen); return builder.getVisLabelList(); } catch (IOException e) { throw new DeserializationException(e); } } return null; }
@VisibleForTesting public static ListMultimap<String, UserPermission> readUserPermission(byte[] data, Configuration conf) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(data)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try {
/** * @param bytes bytes to check * @throws DeserializationException if we are missing the pb magic prefix */ public static void expectPBMagicPrefix(final byte[] bytes) throws DeserializationException { if (!isPBMagicPrefix(bytes)) { String bytesPrefix = bytes == null ? "null" : Bytes.toStringBinary(bytes, 0, PB_MAGIC.length); throw new DeserializationException( "Missing pb magic " + Bytes.toString(PB_MAGIC) + " prefix, bytes: " + bytesPrefix); } }
/** * @param bytes Bytes to check. * @return True if passed <code>bytes</code> has {@link #PB_MAGIC} for a prefix. */ public static boolean isPBMagicPrefix(final byte [] bytes) { if (bytes == null) return false; return isPBMagicPrefix(bytes, 0, bytes.length); }
static boolean isMigrated(final byte [] hriBytes) { if (hriBytes == null || hriBytes.length <= 0) return true; return ProtobufUtil.isPBMagicPrefix(hriBytes); }
/** * @param bytes bytes to check * @throws DeserializationException if we are missing the pb magic prefix */ public static void expectPBMagicPrefix(final byte [] bytes) throws DeserializationException { if (!isPBMagicPrefix(bytes)) { throw new DeserializationException("Missing pb magic " + Bytes.toString(ProtobufMagic.PB_MAGIC) + " prefix"); } }
/** * @param bytes bytes to check * @throws DeserializationException if we are missing the pb magic prefix */ public static void expectPBMagicPrefix(final byte [] bytes) throws DeserializationException { if (!isPBMagicPrefix(bytes)) { throw new DeserializationException("Missing pb magic " + PB_MAGIC_STR + " prefix"); } }
protected static Quotas quotasFromData(final byte[] data) throws IOException { int magicLen = ProtobufUtil.lengthOfPBMagic(); if (!ProtobufUtil.isPBMagicPrefix(data, 0, magicLen)) { throw new IOException("Missing pb magic prefix"); } return Quotas.parseFrom(new ByteArrayInputStream(data, magicLen, data.length - magicLen)); }
private void checkAndMigratePeerZnodesToPB(ZooKeeperWatcher zkw, String znode, List<String> peers) throws KeeperException, NoNodeException, InterruptedException { for (String peer : peers) { String peerZnode = ZKUtil.joinZNode(znode, peer); byte[] data = ZKUtil.getData(zkw, peerZnode); if (!ProtobufUtil.isPBMagicPrefix(data)) { migrateClusterKeyToPB(zkw, peerZnode, data); } String peerStatePath = ZKUtil.joinZNode(peerZnode, getConf().get("zookeeper.znode.replication.peers.state", "peer-state")); if (ZKUtil.checkExists(zkw, peerStatePath) != -1) { data = ZKUtil.getData(zkw, peerStatePath); if (ProtobufUtil.isPBMagicPrefix(data)) continue; migratePeerStateToPB(zkw, data, peerStatePath); } } }
private void checkAndMigrateQueuesToPB(ZooKeeperWatcher zkw, String znode, String rs) throws KeeperException, NoNodeException, InterruptedException { String rsPath = ZKUtil.joinZNode(znode, rs); List<String> peers = ZKUtil.listChildrenNoWatch(zkw, rsPath); if (peers == null || peers.isEmpty()) return; String peerPath = null; for (String peer : peers) { peerPath = ZKUtil.joinZNode(rsPath, peer); List<String> files = ZKUtil.listChildrenNoWatch(zkw, peerPath); if (files == null || files.isEmpty()) continue; String filePath = null; for (String file : files) { filePath = ZKUtil.joinZNode(peerPath, file); byte[] data = ZKUtil.getData(zkw, filePath); if (data == null || Bytes.equals(data, HConstants.EMPTY_BYTE_ARRAY)) continue; if (ProtobufUtil.isPBMagicPrefix(data)) continue; ZKUtil.setData(zkw, filePath, ZKUtil.positionToByteArray(Long.parseLong(Bytes.toString(data)))); } } }
/** * Reads back User auth data written to zookeeper. * @param data * @return User auth details * @throws DeserializationException */ public static MultiUserAuthorizations readUserAuthsFromZKData(byte[] data) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(data)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try { MultiUserAuthorizations.Builder builder = MultiUserAuthorizations.newBuilder(); ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen); return builder.build(); } catch (IOException e) { throw new DeserializationException(e); } } return null; }
/** * Reads back from the zookeeper. The data read here is of the form written by * writeToZooKeeper(Map<byte[], Integer> entries). * * @param data * @return Labels and their ordinal details * @throws DeserializationException */ public static List<VisibilityLabel> readLabelsFromZKData(byte[] data) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(data)) { int pblen = ProtobufUtil.lengthOfPBMagic(); try { VisibilityLabelsRequest.Builder builder = VisibilityLabelsRequest.newBuilder(); ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen); return builder.getVisLabelList(); } catch (IOException e) { throw new DeserializationException(e); } } return null; }
/** * @param bytes A pb serialized {@link HTableDescriptor} instance with pb magic prefix * @return An instance of {@link HTableDescriptor} made from <code>bytes</code> * @throws DeserializationException * @throws IOException * @see #toByteArray() */ public static HTableDescriptor parseFrom(final byte [] bytes) throws DeserializationException, IOException { if (!ProtobufUtil.isPBMagicPrefix(bytes)) { return (HTableDescriptor)Writables.getWritable(bytes, new HTableDescriptor()); } int pblen = ProtobufUtil.lengthOfPBMagic(); TableSchema.Builder builder = TableSchema.newBuilder(); TableSchema ts; try { ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen); ts = builder.build(); } catch (IOException e) { throw new DeserializationException(e); } return convert(ts); }
/** * @param bytes A pb serialized {@link ClusterId} instance with pb magic prefix * @return An instance of {@link ClusterId} made from <code>bytes</code> * @throws DeserializationException * @see #toByteArray() */ public static ClusterId parseFrom(final byte [] bytes) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(bytes)) { int pblen = ProtobufUtil.lengthOfPBMagic(); ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder(); ClusterIdProtos.ClusterId cid = null; try { ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen); cid = builder.build(); } catch (IOException e) { throw new DeserializationException(e); } return convert(cid); } else { // Presume it was written out this way, the old way. return new ClusterId(Bytes.toString(bytes)); } }
private void checkAndMigrateTableStatesToPB(ZooKeeperWatcher zkw) throws KeeperException, InterruptedException { List<String> tables = ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode); if (tables == null) { LOG.info("No table present to migrate table state to PB. returning.."); return; } for (String table : tables) { String znode = ZKUtil.joinZNode(zkw.tableZNode, table); // Delete -ROOT- table state znode since its no longer present in 0.95.0 // onwards. if (table.equals("-ROOT-") || table.equals(".META.")) { ZKUtil.deleteNode(zkw, znode); continue; } byte[] data = ZKUtil.getData(zkw, znode); if (ProtobufUtil.isPBMagicPrefix(data)) continue; ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder(); builder.setState(ZooKeeperProtos.Table.State.valueOf(Bytes.toString(data))); data = ProtobufUtil.prependPBMagic(builder.build().toByteArray()); ZKUtil.setData(zkw, znode, data); } }
/** * @param bytes Content of serialized data of RegionStoreSequenceIds * @return a RegionStoreSequenceIds object * @throws DeserializationException */ public static RegionStoreSequenceIds parseRegionStoreSequenceIds(final byte[] bytes) throws DeserializationException { if (bytes == null || !ProtobufUtil.isPBMagicPrefix(bytes)) { throw new DeserializationException("Unable to parse RegionStoreSequenceIds."); } RegionStoreSequenceIds.Builder regionSequenceIdsBuilder = ClusterStatusProtos.RegionStoreSequenceIds.newBuilder(); int pblen = ProtobufUtil.lengthOfPBMagic(); RegionStoreSequenceIds storeIds = null; try { ProtobufUtil.mergeFrom(regionSequenceIdsBuilder, bytes, pblen, bytes.length - pblen); storeIds = regionSequenceIdsBuilder.build(); } catch (IOException e) { throw new DeserializationException(e); } return storeIds; } }
/** * @param bytes A pb serialized {@link HColumnDescriptor} instance with pb magic prefix * @return An instance of {@link HColumnDescriptor} made from <code>bytes</code> * @throws DeserializationException * @see #toByteArray() */ public static HColumnDescriptor parseFrom(final byte [] bytes) throws DeserializationException { if (!ProtobufUtil.isPBMagicPrefix(bytes)) throw new DeserializationException("No magic"); int pblen = ProtobufUtil.lengthOfPBMagic(); ColumnFamilySchema.Builder builder = ColumnFamilySchema.newBuilder(); ColumnFamilySchema cfs = null; try { ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen); cfs = builder.build(); } catch (IOException e) { throw new DeserializationException(e); } return convert(cfs); }