Java Code Examples for org.apache.ratis.thirdparty.com.google.protobuf.ByteString#copyFrom()

The following examples show how to use org.apache.ratis.thirdparty.com.google.protobuf.ByteString#copyFrom() . 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: FileInfo.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
ByteString read(CheckedFunction<Path, Path, IOException> resolver, long offset, long length)
    throws IOException {
  flush();
  if (offset + length > getSize()) {
    throw new IOException("Failed to read: offset (=" + offset
        + " + length (=" + length + ") > size = " + getSize()
        + ", path=" + getRelativePath());
  }

  try(SeekableByteChannel in = Files.newByteChannel(
      resolver.apply(getRelativePath()), StandardOpenOption.READ)) {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(FileStoreCommon.getChunkSize(length));
    in.position(offset).read(buffer);
    buffer.flip();
    return ByteString.copyFrom(buffer);
  }
}
 
Example 2
Source File: FileInfo.java    From ratis with Apache License 2.0 6 votes vote down vote up
ByteString read(CheckedFunction<Path, Path, IOException> resolver, long offset, long length)
    throws IOException {
  flush();
  if (offset + length > getSize()) {
    throw new IOException("Failed to read: offset (=" + offset
        + " + length (=" + length + ") > size = " + getSize()
        + ", path=" + getRelativePath());
  }

  try(final SeekableByteChannel in = Files.newByteChannel(
      resolver.apply(getRelativePath()), StandardOpenOption.READ)) {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(FileStoreCommon.getChunkSize(length));
    in.position(offset).read(buffer);
    buffer.flip();
    return ByteString.copyFrom(buffer);
  }
}
 
Example 3
Source File: TestHddsDispatcher.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private ContainerCommandRequestProto getWriteChunkRequest(
    String datanodeId, Long containerId, Long localId) {

  ByteString data = ByteString.copyFrom(
      UUID.randomUUID().toString().getBytes(UTF_8));
  ContainerProtos.ChunkInfo chunk = ContainerProtos.ChunkInfo
      .newBuilder()
      .setChunkName(
          DigestUtils.md5Hex("dummy-key") + "_stream_"
              + containerId + "_chunk_" + localId)
      .setOffset(0)
      .setLen(data.size())
      .setChecksumData(Checksum.getNoChecksumDataProto())
      .build();

  WriteChunkRequestProto.Builder writeChunkRequest = WriteChunkRequestProto
      .newBuilder()
      .setBlockID(new BlockID(containerId, localId)
          .getDatanodeBlockIDProtobuf())
      .setChunkData(chunk)
      .setData(data);

  return ContainerCommandRequestProto
      .newBuilder()
      .setContainerID(containerId)
      .setCmdType(ContainerProtos.Type.WriteChunk)
      .setDatanodeUuid(datanodeId)
      .setWriteChunk(writeChunkRequest)
      .build();
}
 
Example 4
Source File: DummyChunkInputStream.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
protected ByteString readChunk(ChunkInfo readChunkInfo) {
  ByteString byteString = ByteString.copyFrom(chunkData,
      (int) readChunkInfo.getOffset(),
      (int) readChunkInfo.getLen());
  getReadByteBuffers().add(byteString);
  return byteString;
}
 
Example 5
Source File: Checksum.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private static Function<ByteBuffer, ByteString> newMessageDigestFunction(
    String algorithm) {
  final MessageDigest md;
  try {
    md = MessageDigest.getInstance(algorithm);
  } catch (NoSuchAlgorithmException e) {
    throw new IllegalStateException(
        "Failed to get MessageDigest for " + algorithm,  e);
  }
  return data -> {
    md.reset();
    md.update(data);
    return ByteString.copyFrom(md.digest());
  };
}
 
Example 6
Source File: RaftId.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static ByteString toByteString(UUID uuid) {
  Objects.requireNonNull(uuid, "uuid == null");
  final ByteBuffer buf = ByteBuffer.wrap(new byte[BYTE_LENGTH]);
  buf.putLong(uuid.getMostSignificantBits());
  buf.putLong(uuid.getLeastSignificantBits());
  return ByteString.copyFrom(buf.array());
}
 
Example 7
Source File: RaftId.java    From ratis with Apache License 2.0 5 votes vote down vote up
private static ByteString toByteString(UUID uuid) {
  Objects.requireNonNull(uuid, "uuid == null");
  final ByteBuffer buf = ByteBuffer.wrap(new byte[BYTE_LENGTH]);
  buf.putLong(uuid.getMostSignificantBits());
  buf.putLong(uuid.getLeastSignificantBits());
  return ByteString.copyFrom(buf.array());
}
 
Example 8
Source File: ByteStringConversion.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
public static ByteString safeWrap(ByteBuffer buffer) {
  ByteString retval = ByteString.copyFrom(buffer);
  buffer.flip();
  return retval;
}
 
Example 9
Source File: Checksum.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
public static ByteString int2ByteString(int n) {
  return ByteString.copyFrom(Ints.toByteArray(n));
}
 
Example 10
Source File: DatanodeChunkGenerator.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Override
public Void call() throws Exception {

  init();

  OzoneConfiguration ozoneConf = createOzoneConfiguration();
  if (OzoneSecurityUtil.isSecurityEnabled(ozoneConf)) {
    throw new IllegalArgumentException(
        "Datanode chunk generator is not supported in secure environment");
  }

  try (StorageContainerLocationProtocol scmLocationClient =
      createStorageContainerLocationClient(ozoneConf)) {
    List<Pipeline> pipelines = scmLocationClient.listPipelines();
    Pipeline pipeline;
    if (pipelineId != null && pipelineId.length() > 0) {
      pipeline = pipelines.stream()
          .filter(p -> p.getId().toString().equals(pipelineId))
          .findFirst()
          .orElseThrow(() -> new IllegalArgumentException(
              "Pipeline ID is defined, but there is no such pipeline: "
                  + pipelineId));

    } else {
      pipeline = pipelines.stream()
          .filter(p -> p.getFactor() == ReplicationFactor.THREE)
          .findFirst()
          .orElseThrow(() -> new IllegalArgumentException(
              "Pipeline ID is NOT defined, and no pipeline " +
                  "has been found with factor=THREE"));
      LOG.info("Using pipeline {}", pipeline.getId());
    }

    try (XceiverClientManager xceiverClientManager =
             new XceiverClientManager(ozoneConf)) {
      xceiverClientSpi = xceiverClientManager.acquireClient(pipeline);

      timer = getMetrics().timer("chunk-write");

      byte[] data = RandomStringUtils.randomAscii(chunkSize)
          .getBytes(StandardCharsets.UTF_8);

      dataToWrite = ByteString.copyFrom(data);

      Checksum checksum = new Checksum(ChecksumType.CRC32, chunkSize);
      checksumProtobuf = checksum.computeChecksum(data).getProtoBufMessage();

      runTests(this::writeChunk);
    }
  } finally {
    if (xceiverClientSpi != null) {
      xceiverClientSpi.close();
    }
  }
  return null;
}
 
Example 11
Source File: OMRatisHelper.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
public static ByteString convertRequestToByteString(OMRequest request) {
  byte[] requestBytes = request.toByteArray();
  return ByteString.copyFrom(requestBytes);
}
 
Example 12
Source File: RaftPeerId.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private RaftPeerId(String id) {
  this.idString = Objects.requireNonNull(id, "id == null");
  Preconditions.assertTrue(!id.isEmpty(), "id is an empty string.");
  this.id = ByteString.copyFrom(idString, StandardCharsets.UTF_8);
}
 
Example 13
Source File: ProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static ByteString toByteString(byte[] bytes, int offset, int size) {
  // return singleton to reduce object allocation
  return bytes.length == 0 ?
      ByteString.EMPTY : ByteString.copyFrom(bytes, offset, size);
}
 
Example 14
Source File: RaftPeerId.java    From ratis with Apache License 2.0 4 votes vote down vote up
private RaftPeerId(String id) {
  this.idString = Objects.requireNonNull(id, "id == null");
  Preconditions.assertTrue(!id.isEmpty(), "id is an empty string.");
  this.id = ByteString.copyFrom(idString, StandardCharsets.UTF_8);
}
 
Example 15
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static ByteString toByteString(byte[] bytes, int offset, int size) {
  // return singleton to reduce object allocation
  return bytes.length == 0 ?
      ByteString.EMPTY : ByteString.copyFrom(bytes, offset, size);
}