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

The following examples show how to use org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil#isPBMagicPrefix() . 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: 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 2
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 3
Source File: RegionInfo.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an RegionInfo instance from the passed in stream.
 * Presumes the RegionInfo was serialized to the stream with
 * {@link #toDelimitedByteArray(RegionInfo)}.
 * @return An instance of RegionInfo.
 */
static RegionInfo 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 ProtobufUtil.toRegionInfo(HBaseProtos.RegionInfo.parseDelimitedFrom(in));
  } else {
    throw new IOException("PB encoded RegionInfo expected");
  }
}
 
Example 4
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 5
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 6
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 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: 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;
}
 
Example 9
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 10
Source File: FSUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies current version of file system
 *
 * @param fs filesystem object
 * @param rootdir root hbase directory
 * @return null if no version file exists, version string otherwise
 * @throws IOException if the version file fails to open
 * @throws DeserializationException if the version data cannot be translated into a version
 */
public static String getVersion(FileSystem fs, Path rootdir)
throws IOException, DeserializationException {
  final Path versionFile = new Path(rootdir, HConstants.VERSION_FILE_NAME);
  FileStatus[] status = null;
  try {
    // hadoop 2.0 throws FNFE if directory does not exist.
    // hadoop 1.0 returns null if directory does not exist.
    status = fs.listStatus(versionFile);
  } catch (FileNotFoundException fnfe) {
    return null;
  }
  if (ArrayUtils.getLength(status) == 0) {
    return null;
  }
  String version = null;
  byte [] content = new byte [(int)status[0].getLen()];
  FSDataInputStream s = fs.open(versionFile);
  try {
    IOUtils.readFully(s, content, 0, content.length);
    if (ProtobufUtil.isPBMagicPrefix(content)) {
      version = parseVersionFrom(content);
    } else {
      // Presume it pre-pb format.
      try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(content))) {
        version = dis.readUTF();
      }
    }
  } catch (EOFException eof) {
    LOG.warn("Version file was empty, odd, will try to set it.");
  } finally {
    s.close();
  }
  return version;
}
 
Example 11
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 12
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 13
Source File: ZKProcedureCoordinator.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Start monitoring znodes in ZK - subclass hook to start monitoring znodes they are about.
 * @return true if succeed, false if encountered initialization errors.
 */
@Override
final public boolean start(final ProcedureCoordinator coordinator) {
  if (this.coordinator != null) {
    throw new IllegalStateException(
      "ZKProcedureCoordinator already started and already has listener installed");
  }
  this.coordinator = coordinator;

  try {
    this.zkProc = new ZKProcedureUtil(watcher, procedureType) {
      @Override
      public void nodeCreated(String path) {
        if (!isInProcedurePath(path)) return;
        LOG.debug("Node created: " + path);
        logZKTree(this.baseZNode);
        if (isAcquiredPathNode(path)) {
          // node wasn't present when we created the watch so zk event triggers acquire
          coordinator.memberAcquiredBarrier(ZKUtil.getNodeName(ZKUtil.getParent(path)),
            ZKUtil.getNodeName(path));
        } else if (isReachedPathNode(path)) {
          // node was absent when we created the watch so zk event triggers the finished barrier.

          // TODO Nothing enforces that acquire and reached znodes from showing up in wrong order.
          String procName = ZKUtil.getNodeName(ZKUtil.getParent(path));
          String member = ZKUtil.getNodeName(path);
          // get the data from the procedure member
          try {
            byte[] dataFromMember = ZKUtil.getData(watcher, path);
            // ProtobufUtil.isPBMagicPrefix will check null
            if (dataFromMember != null && dataFromMember.length > 0) {
              if (!ProtobufUtil.isPBMagicPrefix(dataFromMember)) {
                ForeignException ee = new ForeignException(coordName,
                  "Failed to get data from finished node or data is illegally formatted:"
                      + path);
                coordinator.abortProcedure(procName, ee);
              } else {
                dataFromMember = Arrays.copyOfRange(dataFromMember, ProtobufUtil.lengthOfPBMagic(),
                  dataFromMember.length);
                LOG.debug("Finished data from procedure '{}' member '{}': {}", procName, member,
                    new String(dataFromMember, StandardCharsets.UTF_8));
                coordinator.memberFinishedBarrier(procName, member, dataFromMember);
              }
            } else {
              coordinator.memberFinishedBarrier(procName, member, dataFromMember);
            }
          } catch (KeeperException e) {
            ForeignException ee = new ForeignException(coordName, e);
            coordinator.abortProcedure(procName, ee);
          } catch (InterruptedException e) {
            ForeignException ee = new ForeignException(coordName, e);
            coordinator.abortProcedure(procName, ee);
          }
        } else if (isAbortPathNode(path)) {
          abort(path);
        } else {
          LOG.debug("Ignoring created notification for node:" + path);
        }
      }
    };
    zkProc.clearChildZNodes();
  } catch (KeeperException e) {
    LOG.error("Unable to start the ZK-based Procedure Coordinator rpcs.", e);
    return false;
  }

  LOG.debug("Starting controller for procedure member=" + coordName);
  return true;
}