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

The following examples show how to use org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil#prependPBMagic() . 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: ZKReplicationStorageBase.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Serialized protobuf of <code>state</code> with pb magic prefix prepended suitable for use as
 * content of a peer-state znode under a peer cluster id as in
 * /hbase/replication/peers/PEER_ID/peer-state.
 */
protected static byte[] toByteArray(final ReplicationProtos.ReplicationState.State state) {
  ReplicationProtos.ReplicationState msg =
      ReplicationProtos.ReplicationState.newBuilder().setState(state).build();
  // There is no toByteArray on this pb Message?
  // 32 bytes is default which seems fair enough here.
  try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    CodedOutputStream cos = CodedOutputStream.newInstance(baos, 16);
    msg.writeTo(cos);
    cos.flush();
    baos.flush();
    return ProtobufUtil.prependPBMagic(baos.toByteArray());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 2
Source File: MirroringTableStateManager.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void updateZooKeeper(TableState tableState) throws IOException {
  if (tableState == null) {
    return;
  }
  String znode = ZNodePaths.joinZNode(this.master.getZooKeeper().getZNodePaths().tableZNode,
    tableState.getTableName().getNameAsString());
  try {
    // Make sure znode exists.
    if (ZKUtil.checkExists(this.master.getZooKeeper(), znode) == -1) {
      ZKUtil.createAndFailSilent(this.master.getZooKeeper(), znode);
    }
    // Now set newState
    ZooKeeperProtos.DeprecatedTableState.Builder builder =
      ZooKeeperProtos.DeprecatedTableState.newBuilder();
    builder.setState(
      ZooKeeperProtos.DeprecatedTableState.State.valueOf(tableState.getState().toString()));
    byte[] data = ProtobufUtil.prependPBMagic(builder.build().toByteArray());
    ZKUtil.setData(this.master.getZooKeeper(), znode, data);
  } catch (KeeperException e) {
    // Only hbase1 clients suffer if this fails.
    LOG.warn("Failed setting table state to zookeeper mirrored for hbase-1.x clients", e);
  }
}
 
Example 3
Source File: ZKProcedureMemberRpcs.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * This should be called by the member and should write a serialized root cause exception as
 * to the abort znode.
 */
@Override
public void sendMemberAborted(Subprocedure sub, ForeignException ee) {
  if (sub == null) {
    LOG.error("Failed due to null subprocedure", ee);
    return;
  }
  String procName = sub.getName();
  LOG.debug("Aborting procedure (" + procName + ") in zk");
  String procAbortZNode = zkController.getAbortZNode(procName);
  try {
    String source = (ee.getSource() == null) ? memberName: ee.getSource();
    byte[] errorInfo = ProtobufUtil.prependPBMagic(ForeignException.serialize(source, ee));
    ZKUtil.createAndFailSilent(zkController.getWatcher(), procAbortZNode, errorInfo);
    LOG.debug("Finished creating abort znode:" + procAbortZNode);
  } catch (KeeperException e) {
    // possible that we get this error for the procedure if we already reset the zk state, but in
    // that case we should still get an error for that procedure anyways
    zkController.logZKTree(zkController.getBaseZnode());
    member.controllerConnectionFailure("Failed to post zk node:" + procAbortZNode
        + " to abort procedure", e, procName);
  }
}
 
Example 4
Source File: ZKProcedureCoordinator.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * This is the abort message being sent by the coordinator to member
 *
 * TODO this code isn't actually used but can be used to issue a cancellation from the
 * coordinator.
 */
@Override
final public void sendAbortToMembers(Procedure proc, ForeignException ee) {
  String procName = proc.getName();
  LOG.debug("Aborting procedure '" + procName + "' in zk");
  String procAbortNode = zkProc.getAbortZNode(procName);
  try {
    LOG.debug("Creating abort znode:" + procAbortNode);
    String source = (ee.getSource() == null) ? coordName : ee.getSource();
    byte[] errorInfo = ProtobufUtil.prependPBMagic(ForeignException.serialize(source, ee));
    // first create the znode for the procedure
    ZKUtil.createAndFailSilent(zkProc.getWatcher(), procAbortNode, errorInfo);
    LOG.debug("Finished creating abort node:" + procAbortNode);
  } catch (KeeperException e) {
    // possible that we get this error for the procedure if we already reset the zk state, but in
    // that case we should still get an error for that procedure anyways
    zkProc.logZKTree(zkProc.baseZNode);
    coordinator.rpcConnectionFailure("Failed to post zk node:" + procAbortNode
        + " to abort procedure '" + procName + "'", new IOException(e));
  }
}
 
Example 5
Source File: VisibilityUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the labels data to be written to zookeeper.
 * @param existingLabels
 * @return Bytes form of labels and their ordinal details to be written to zookeeper.
 */
public static byte[] getDataToWriteToZooKeeper(Map<String, Integer> existingLabels) {
  VisibilityLabelsRequest.Builder visReqBuilder = VisibilityLabelsRequest.newBuilder();
  for (Entry<String, Integer> entry : existingLabels.entrySet()) {
    VisibilityLabel.Builder visLabBuilder = VisibilityLabel.newBuilder();
    visLabBuilder.setLabel(ByteString.copyFrom(Bytes.toBytes(entry.getKey())));
    visLabBuilder.setOrdinal(entry.getValue());
    visReqBuilder.addVisLabel(visLabBuilder.build());
  }
  return ProtobufUtil.prependPBMagic(visReqBuilder.build().toByteArray());
}
 
Example 6
Source File: VisibilityUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the user auth data to be written to zookeeper.
 * @param userAuths
 * @return Bytes form of user auths details to be written to zookeeper.
 */
public static byte[] getUserAuthsDataToWriteToZooKeeper(Map<String, List<Integer>> userAuths) {
  MultiUserAuthorizations.Builder builder = MultiUserAuthorizations.newBuilder();
  for (Entry<String, List<Integer>> entry : userAuths.entrySet()) {
    UserAuthorizations.Builder userAuthsBuilder = UserAuthorizations.newBuilder();
    userAuthsBuilder.setUser(ByteString.copyFrom(Bytes.toBytes(entry.getKey())));
    for (Integer label : entry.getValue()) {
      userAuthsBuilder.addAuth(label);
    }
    builder.addUserAuths(userAuthsBuilder.build());
  }
  return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
}
 
Example 7
Source File: TestFsRegionsMetaRecoverer.java    From hbase-operator-tools with Apache License 2.0 5 votes vote down vote up
private Cell createCellForRegionInfo(RegionInfo info){
  byte[] regionInfoValue = ProtobufUtil.prependPBMagic(ProtobufUtil.toRegionInfo(info)
    .toByteArray());
  Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY)
    .setRow(info.getRegionName())
    .setFamily(Bytes.toBytes("info"))
    .setQualifier(Bytes.toBytes("regioninfo"))
    .setType(Cell.Type.Put)
    .setValue(regionInfoValue)
    .build();
  return cell;
}
 
Example 8
Source File: SplitLogTask.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @return This instance serialized into a byte array
 * @see #parseFrom(byte[])
 */
public byte [] toByteArray() {
  // First create a pb ServerName.  Then create a ByteString w/ the TaskState
  // bytes in it.  Finally create a SplitLogTaskState passing in the two
  // pbs just created.
  HBaseProtos.ServerName snpb = ProtobufUtil.toServerName(this.originServer);
  ZooKeeperProtos.SplitLogTask slts =
    ZooKeeperProtos.SplitLogTask.newBuilder().setServerName(snpb).setState(this.state).build();
  return ProtobufUtil.prependPBMagic(slts.toByteArray());
}
 
Example 9
Source File: HRegionServer.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void createMyEphemeralNode() throws KeeperException {
  RegionServerInfo.Builder rsInfo = RegionServerInfo.newBuilder();
  rsInfo.setInfoPort(infoServer != null ? infoServer.getPort() : -1);
  rsInfo.setVersionInfo(ProtobufUtil.getVersionInfo());
  byte[] data = ProtobufUtil.prependPBMagic(rsInfo.build().toByteArray());
  ZKUtil.createEphemeralNodeAndWatch(this.zooKeeper, getMyEphemeralNodePath(), data);
}
 
Example 10
Source File: SnapshotCleanupTracker.java    From hbase with Apache License 2.0 4 votes vote down vote up
private byte[] toByteArray(final boolean isSnapshotCleanupEnabled) {
  SnapshotCleanupProtos.SnapshotCleanupState.Builder builder =
      SnapshotCleanupProtos.SnapshotCleanupState.newBuilder();
  builder.setSnapshotCleanupEnabled(isSnapshotCleanupEnabled);
  return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
}
 
Example 11
Source File: ZKUtil.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * @param position the position to serialize
 * @return Serialized protobuf of <code>position</code> with pb magic prefix prepended suitable
 *         for use as content of an wal position in a replication queue.
 */
public static byte[] positionToByteArray(final long position) {
  byte[] bytes = ReplicationProtos.ReplicationHLogPosition.newBuilder().setPosition(position)
      .build().toByteArray();
  return ProtobufUtil.prependPBMagic(bytes);
}
 
Example 12
Source File: ClusterId.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * @return The clusterid serialized using pb w/ pb magic prefix
 */
public byte [] toByteArray() {
  return ProtobufUtil.prependPBMagic(convert().toByteArray());
}
 
Example 13
Source File: ColumnFamilyDescriptorBuilder.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * @return This instance serialized with pb with pb magic prefix
 * @see #parseFrom(byte[])
 */
private byte[] toByteArray() {
  return ProtobufUtil.prependPBMagic(ProtobufUtil.toColumnFamilySchema(this)
                  .toByteArray());
}
 
Example 14
Source File: RegionNormalizerTracker.java    From hbase with Apache License 2.0 4 votes vote down vote up
private byte [] toByteArray(boolean isNormalizerOn) {
  RegionNormalizerProtos.RegionNormalizerState.Builder builder =
    RegionNormalizerProtos.RegionNormalizerState.newBuilder();
  builder.setNormalizerOn(isNormalizerOn);
  return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
}
 
Example 15
Source File: RegionInfo.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * @return This instance serialized as protobuf w/ a magic pb prefix.
 * @see #parseFrom(byte[])
 */
static byte [] toByteArray(RegionInfo ri) {
  byte [] bytes = ProtobufUtil.toRegionInfo(ri).toByteArray();
  return ProtobufUtil.prependPBMagic(bytes);
}
 
Example 16
Source File: LoadBalancerTracker.java    From hbase with Apache License 2.0 4 votes vote down vote up
private byte [] toByteArray(boolean isBalancerOn) {
  LoadBalancerProtos.LoadBalancerState.Builder builder =
    LoadBalancerProtos.LoadBalancerState.newBuilder();
  builder.setBalancerOn(isBalancerOn);
  return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
}
 
Example 17
Source File: SplitOrMergeTracker.java    From hbase with Apache License 2.0 4 votes vote down vote up
private byte [] toByteArray(boolean enabled) {
  SwitchState.Builder builder = SwitchState.newBuilder();
  builder.setEnabled(enabled);
  return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
}
 
Example 18
Source File: PermissionStorage.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Writes a set of permissions as {@link org.apache.hadoop.io.Writable} instances and returns the
 * resulting byte array. Writes a set of permission [user: table permission]
 */
public static byte[] writePermissionsAsBytes(ListMultimap<String, UserPermission> perms,
    Configuration conf) {
  return ProtobufUtil
      .prependPBMagic(AccessControlUtil.toUserTablePermissions(perms).toByteArray());
}
 
Example 19
Source File: ReplicationPeerConfigUtil.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * @param peerConfig
 * @return Serialized protobuf of <code>peerConfig</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
 */
public static byte[] toByteArray(final ReplicationPeerConfig peerConfig) {
  byte[] bytes = convert(peerConfig).toByteArray();
  return ProtobufUtil.prependPBMagic(bytes);
}
 
Example 20
Source File: Reference.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * Use this when writing to a stream and you want to use the pb mergeDelimitedFrom
 * (w/o the delimiter, pb reads to EOF which may not be what you want).
 * @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
 * @throws IOException
 */
byte [] toByteArray() throws IOException {
  return ProtobufUtil.prependPBMagic(convert().toByteArray());
}