/** * @param clusterKey * @return Serialized protobuf of <code>clusterKey</code> with pb magic prefix prepended suitable * for use as content of a this.peersZNode; i.e. the content of PEER_ID znode under * /hbase/replication/peers/PEER_ID */ private static byte[] toByteArray(final String clusterKey) { byte[] bytes = ZooKeeperProtos.ReplicationPeer.newBuilder() .setClusterkey(clusterKey) .build() .toByteArray(); return ProtobufUtil.prependPBMagic(bytes); }
/** * @param bytes Content of a peer znode. * @return ClusterKey parsed from the passed bytes. * @throws DeserializationException */ private static String parsePeerFrom(final byte[] bytes) throws DeserializationException { if (ProtobufUtil.isPBMagicPrefix(bytes)) { int pblen = ProtobufUtil.lengthOfPBMagic(); ZooKeeperProtos.ReplicationPeer.Builder builder = ZooKeeperProtos.ReplicationPeer.newBuilder(); ZooKeeperProtos.ReplicationPeer peer; try { peer = builder.mergeFrom(bytes, pblen, bytes.length - pblen).build(); } catch (InvalidProtocolBufferException e) { throw new DeserializationException(e); } return peer.getClusterkey(); } else { if (bytes.length > 0) { return Bytes.toString(bytes); } return ""; } }