Search in sources :

Example 1 with DataStreamOutput

use of org.apache.ratis.client.api.DataStreamOutput in project incubator-ratis by apache.

the class FileStoreWriter method streamWriteAndVerify.

public FileStoreWriter streamWriteAndVerify(RoutingTable routingTable) {
    final int size = fileSize.getSizeInt();
    final DataStreamOutput dataStreamOutput = client.getStreamOutput(fileName, size, routingTable);
    final List<CompletableFuture<DataStreamReply>> futures = new ArrayList<>();
    final List<Integer> sizes = new ArrayList<>();
    for (int offset = 0; offset < size; ) {
        final int remaining = size - offset;
        final int length = Math.min(remaining, bufferSize);
        final boolean close = length == remaining;
        LOG.trace("write {}, offset={}, length={}, close? {}", fileName, offset, length, close);
        final ByteBuffer bf = DataStreamTestUtils.initBuffer(0, length);
        futures.add(close ? dataStreamOutput.writeAsync(bf, StandardWriteOption.CLOSE) : dataStreamOutput.writeAsync(bf));
        sizes.add(length);
        offset += length;
    }
    DataStreamReply reply = dataStreamOutput.closeAsync().join();
    Assert.assertTrue(reply.isSuccess());
    // check writeAsync requests
    for (int i = 0; i < futures.size(); i++) {
        reply = futures.get(i).join();
        Assert.assertTrue(reply.isSuccess());
        Assert.assertEquals(sizes.get(i).longValue(), reply.getBytesWritten());
        Assert.assertEquals(reply.getType(), RaftProtos.DataStreamPacketHeaderProto.Type.STREAM_DATA);
    }
    return this;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) DataStreamOutput(org.apache.ratis.client.api.DataStreamOutput)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DataStreamOutput (org.apache.ratis.client.api.DataStreamOutput)1 DataStreamReply (org.apache.ratis.protocol.DataStreamReply)1