Java Code Examples for org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil#lengthOfPBMagic()

The following examples show how to use org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil#lengthOfPBMagic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClusterId.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @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));
  }
}
 
Example 2
Source File: ReplicationPeerConfigUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param bytes Content of a peer znode.
 * @return ClusterKey parsed from the passed bytes.
 * @throws DeserializationException
 */
public static ReplicationPeerConfig parsePeerFrom(final byte[] bytes)
    throws DeserializationException {
  if (ProtobufUtil.isPBMagicPrefix(bytes)) {
    int pbLen = ProtobufUtil.lengthOfPBMagic();
    ReplicationProtos.ReplicationPeer.Builder builder =
        ReplicationProtos.ReplicationPeer.newBuilder();
    ReplicationProtos.ReplicationPeer peer;
    try {
      ProtobufUtil.mergeFrom(builder, bytes, pbLen, bytes.length - pbLen);
      peer = builder.build();
    } catch (IOException e) {
      throw new DeserializationException(e);
    }
    return convert(peer);
  } else {
    if (bytes == null || bytes.length <= 0) {
      throw new DeserializationException("Bytes to deserialize should not be empty.");
    }
    return ReplicationPeerConfig.newBuilder().setClusterKey(Bytes.toString(bytes)).build();
  }
}
 
Example 3
Source File: RegionInfo.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @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 RegionInfo}
 */
@InterfaceAudience.Private
static RegionInfo 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 ProtobufUtil.toRegionInfo(ri);
    } catch (IOException e) {
      throw new DeserializationException(e);
    }
  } else {
    throw new DeserializationException("PB encoded RegionInfo expected");
  }
}
 
Example 4
Source File: ZKUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param bytes - Content of a WAL position znode.
 * @return long - The current WAL position.
 * @throws DeserializationException if the WAL position cannot be parsed
 */
public static long parseWALPositionFrom(final byte[] bytes) throws DeserializationException {
  if (bytes == null) {
    throw new DeserializationException("Unable to parse null WAL position.");
  }
  if (ProtobufUtil.isPBMagicPrefix(bytes)) {
    int pblen = ProtobufUtil.lengthOfPBMagic();
    ReplicationProtos.ReplicationHLogPosition.Builder builder =
        ReplicationProtos.ReplicationHLogPosition.newBuilder();
    ReplicationProtos.ReplicationHLogPosition position;
    try {
      ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
      position = builder.build();
    } catch (IOException e) {
      throw new DeserializationException(e);
    }
    return position.getPosition();
  } else {
    if (bytes.length > 0) {
      return Bytes.toLong(bytes);
    }
    return 0;
  }
}
 
Example 5
Source File: ColumnFamilyDescriptorBuilder.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param bytes A pb serialized {@link ModifyableColumnFamilyDescriptor} instance with pb
 * magic prefix
 * @return An instance of {@link ModifyableColumnFamilyDescriptor} made from
 * <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray()
 */
private static ColumnFamilyDescriptor 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 ProtobufUtil.toColumnFamilyDescriptor(cfs);
}
 
Example 6
Source File: ZKUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
private static void appendPeerState(ZKWatcher zkw, String znodeToProcess, StringBuilder sb)
        throws KeeperException, InvalidProtocolBufferException {
  String peerState = zkw.getConfiguration().get("zookeeper.znode.replication.peers.state",
    "peer-state");
  int pblen = ProtobufUtil.lengthOfPBMagic();
  for (String child : ZKUtil.listChildrenNoWatch(zkw, znodeToProcess)) {
    if (!child.equals(peerState)) {
      continue;
    }

    String peerStateZnode = ZNodePaths.joinZNode(znodeToProcess, child);
    sb.append("\n").append(peerStateZnode).append(": ");
    byte[] peerStateData;
    try {
      peerStateData = ZKUtil.getData(zkw, peerStateZnode);
      ReplicationProtos.ReplicationState.Builder builder =
          ReplicationProtos.ReplicationState.newBuilder();
      ProtobufUtil.mergeFrom(builder, peerStateData, pblen, peerStateData.length - pblen);
      sb.append(builder.getState().name());
    } catch (IOException ipbe) {
      LOG.warn("Got Exception while parsing peer: " + znodeToProcess, ipbe);
    } catch (InterruptedException e) {
      zkw.interruptedException(e);
      return;
    }
  }
}
 
Example 7
Source File: VisibilityUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Reads back from the zookeeper. The data read here is of the form written by
 * writeToZooKeeper(Map&lt;byte[], Integer&gt; 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;
}
 
Example 8
Source File: ZKUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
private static void appendPeersZnodes(ZKWatcher zkw, String peersZnode,
                                      StringBuilder sb) throws KeeperException {
  int pblen = ProtobufUtil.lengthOfPBMagic();
  sb.append("\n").append(peersZnode).append(": ");
  for (String peerIdZnode : ZKUtil.listChildrenNoWatch(zkw, peersZnode)) {
    String znodeToProcess = ZNodePaths.joinZNode(peersZnode, peerIdZnode);
    byte[] data;
    try {
      data = ZKUtil.getData(zkw, znodeToProcess);
    } catch (InterruptedException e) {
      zkw.interruptedException(e);
      return;
    }
    // parse the data of the above peer znode.
    try {
      ReplicationProtos.ReplicationPeer.Builder builder =
        ReplicationProtos.ReplicationPeer.newBuilder();
      ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen);
      String clusterKey = builder.getClusterkey();
      sb.append("\n").append(znodeToProcess).append(": ").append(clusterKey);
      // add the peer-state.
      appendPeerState(zkw, znodeToProcess, sb);
    } catch (IOException ipbe) {
      LOG.warn("Got Exception while parsing peer: " + znodeToProcess, ipbe);
    }
  }
}
 
Example 9
Source File: RegionNormalizerTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private RegionNormalizerProtos.RegionNormalizerState parseFrom(byte [] pbBytes)
  throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(pbBytes);
  RegionNormalizerProtos.RegionNormalizerState.Builder builder =
    RegionNormalizerProtos.RegionNormalizerState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 10
Source File: MasterAddressTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param data zookeeper data. may be null
 * @return pb object of master, null if no active master
 * @throws DeserializationException if the parsing fails
 */
public static ZooKeeperProtos.Master parse(byte[] data) throws DeserializationException {
  if (data == null) {
    return null;
  }
  int prefixLen = ProtobufUtil.lengthOfPBMagic();
  try {
    return ZooKeeperProtos.Master.PARSER.parseFrom(data, prefixLen, data.length - prefixLen);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }
}
 
Example 11
Source File: LoadBalancerTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private LoadBalancerProtos.LoadBalancerState parseFrom(byte [] pbBytes)
  throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(pbBytes);
  LoadBalancerProtos.LoadBalancerState.Builder builder =
    LoadBalancerProtos.LoadBalancerState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 12
Source File: SnapshotCleanupTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private SnapshotCleanupProtos.SnapshotCleanupState parseFrom(final byte[] pbBytes)
    throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(pbBytes);
  SnapshotCleanupProtos.SnapshotCleanupState.Builder builder =
      SnapshotCleanupProtos.SnapshotCleanupState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 13
Source File: TableDescriptorBuilder.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param bytes A pb serialized {@link ModifyableTableDescriptor} instance
 * with pb magic prefix
 * @return An instance of {@link ModifyableTableDescriptor} made from
 * <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray()
 */
private static TableDescriptor parseFrom(final byte[] bytes)
        throws DeserializationException {
  if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
    throw new DeserializationException("Expected PB encoded ModifyableTableDescriptor");
  }
  int pblen = ProtobufUtil.lengthOfPBMagic();
  HBaseProtos.TableSchema.Builder builder = HBaseProtos.TableSchema.newBuilder();
  try {
    ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
    return ProtobufUtil.toTableDescriptor(builder.build());
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
}
 
Example 14
Source File: TimeRangeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static TimeRangeTracker parseFrom(final byte[] data, Type type) throws IOException {
  Preconditions.checkNotNull(data, "input data is null!");
  if (ProtobufUtil.isPBMagicPrefix(data)) {
    int pblen = ProtobufUtil.lengthOfPBMagic();
    HBaseProtos.TimeRangeTracker.Builder builder = HBaseProtos.TimeRangeTracker.newBuilder();
    ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen);
    return TimeRangeTracker.create(type, builder.getFrom(), builder.getTo());
  } else {
    try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(data))) {
      return TimeRangeTracker.create(type, in.readLong(), in.readLong());
    }
  }
}
 
Example 15
Source File: FSUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the content of the ${HBASE_ROOTDIR}/hbase.version file.
 * @param bytes The byte content of the hbase.version file
 * @return The version found in the file as a String
 * @throws DeserializationException if the version data cannot be translated into a version
 */
static String parseVersionFrom(final byte [] bytes)
throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(bytes);
  int pblen = ProtobufUtil.lengthOfPBMagic();
  FSProtos.HBaseVersionFileContent.Builder builder =
    FSProtos.HBaseVersionFileContent.newBuilder();
  try {
    ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
    return builder.getVersion();
  } catch (IOException e) {
    // Convert
    throw new DeserializationException(e);
  }
}
 
Example 16
Source File: RSGroupInfoManagerImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
private List<RSGroupInfo> retrieveGroupListFromZookeeper() throws IOException {
  String groupBasePath = ZNodePaths.joinZNode(watcher.getZNodePaths().baseZNode, RS_GROUP_ZNODE);
  List<RSGroupInfo> RSGroupInfoList = Lists.newArrayList();
  // Overwrite any info stored by table, this takes precedence
  try {
    if (ZKUtil.checkExists(watcher, groupBasePath) != -1) {
      List<String> children = ZKUtil.listChildrenAndWatchForNewChildren(watcher, groupBasePath);
      if (children == null) {
        return RSGroupInfoList;
      }
      for (String znode : children) {
        byte[] data = ZKUtil.getData(watcher, ZNodePaths.joinZNode(groupBasePath, znode));
        if (data.length > 0) {
          ProtobufUtil.expectPBMagicPrefix(data);
          ByteArrayInputStream bis =
            new ByteArrayInputStream(data, ProtobufUtil.lengthOfPBMagic(), data.length);
          RSGroupInfoList
            .add(ProtobufUtil.toGroupInfo(RSGroupProtos.RSGroupInfo.parseFrom(bis)));
        }
      }
      LOG.debug("Read ZK GroupInfo count:" + RSGroupInfoList.size());
    }
  } catch (KeeperException | DeserializationException | InterruptedException e) {
    throw new IOException("Failed to read rsGroupZNode", e);
  }
  return RSGroupInfoList;
}
 
Example 17
Source File: SplitOrMergeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private SwitchState parseFrom(byte [] bytes)
  throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(bytes);
  SwitchState.Builder builder = SwitchState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, bytes, magicLen, bytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 18
Source File: RegionServerTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private Pair<ServerName, RegionServerInfo> getServerInfo(String name)
    throws KeeperException, IOException {
  ServerName serverName = ServerName.parseServerName(name);
  String nodePath = ZNodePaths.joinZNode(watcher.getZNodePaths().rsZNode, name);
  byte[] data;
  try {
    data = ZKUtil.getData(watcher, nodePath);
  } catch (InterruptedException e) {
    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
  }
  if (data == null) {
    // we should receive a children changed event later and then we will expire it, so we still
    // need to add it to the region server set.
    LOG.warn("Server node {} does not exist, already dead?", name);
    return Pair.newPair(serverName, null);
  }
  if (data.length == 0 || !ProtobufUtil.isPBMagicPrefix(data)) {
    // this should not happen actually, unless we have bugs or someone has messed zk up.
    LOG.warn("Invalid data for region server node {} on zookeeper, data length = {}", name,
      data.length);
    return Pair.newPair(serverName, null);
  }
  RegionServerInfo.Builder builder = RegionServerInfo.newBuilder();
  int magicLen = ProtobufUtil.lengthOfPBMagic();
  ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen);
  return Pair.newPair(serverName, builder.build());
}
 
Example 19
Source File: SplitLogTask.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param data Serialized date to parse.
 * @return An SplitLogTaskState instance made of the passed <code>data</code>
 * @throws DeserializationException
 * @see #toByteArray()
 */
public static SplitLogTask parseFrom(final byte [] data) throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(data);
  try {
    int prefixLen = ProtobufUtil.lengthOfPBMagic();
    ZooKeeperProtos.SplitLogTask.Builder builder = ZooKeeperProtos.SplitLogTask.newBuilder();
    ProtobufUtil.mergeFrom(builder, data, prefixLen, data.length - prefixLen);
    return new SplitLogTask(builder.build());
  } catch (IOException e) {
    throw new DeserializationException(Bytes.toStringBinary(data, 0, 64), e);
  }
}
 
Example 20
Source File: VisibilityUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}