Search in sources :

Example 1 with DataStreamOutputImpl

use of org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl in project incubator-ratis by apache.

the class DataStreamAsyncClusterTests method runTestDataStream.

long runTestDataStream(CLUSTER cluster, int numStreams, int bufferSize, int bufferNum, boolean stepDownLeader) {
    final Iterable<RaftServer> servers = CollectionUtils.as(cluster.getServers(), s -> s);
    final RaftPeerId leader = cluster.getLeader().getId();
    final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
    final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
    try (RaftClient client = cluster.createClient(primaryServer)) {
        ClientId primaryClientId = getPrimaryClientId(cluster, primaryServer);
        for (int i = 0; i < numStreams; i++) {
            final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer));
            futures.add(CompletableFuture.supplyAsync(() -> DataStreamTestUtils.writeAndCloseAndAssertReplies(servers, leader, out, bufferSize, bufferNum, primaryClientId, client.getId(), stepDownLeader).join(), executor));
        }
        Assert.assertEquals(numStreams, futures.size());
        return futures.stream().map(CompletableFuture::join).map(RaftClientReply::getLogIndex).max(Long::compareTo).orElseThrow(IllegalStateException::new);
    } catch (IOException e) {
        throw new CompletionException(e);
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) CompletableFuture(java.util.concurrent.CompletableFuture) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) CompletionException(java.util.concurrent.CompletionException) ClientId(org.apache.ratis.protocol.ClientId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 2 with DataStreamOutputImpl

use of org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl in project incubator-ratis by apache.

the class DataStreamClusterTests method transferToWritableByteChannel.

static CheckedConsumer<DataStreamOutputImpl, Exception> transferToWritableByteChannel(File f, int size) {
    return new CheckedConsumer<DataStreamOutputImpl, Exception>() {

        @Override
        public void accept(DataStreamOutputImpl out) throws Exception {
            try (FileInputStream in = new FileInputStream(f)) {
                final long transferred = in.getChannel().transferTo(0, size, out.getWritableByteChannel());
                Assert.assertEquals(size, transferred);
            }
        }

        @Override
        public String toString() {
            return "transferToWritableByteChannel";
        }
    };
}
Also used : CheckedConsumer(org.apache.ratis.util.function.CheckedConsumer) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) FileInputStream(java.io.FileInputStream)

Example 3 with DataStreamOutputImpl

use of org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl in project incubator-ratis by apache.

the class DataStreamClusterTests method runTestDataStreamOutput.

void runTestDataStreamOutput(CLUSTER cluster) throws Exception {
    final RaftClientRequest request;
    final CompletableFuture<RaftClientReply> reply;
    final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
    try (RaftClient client = cluster.createClient(primaryServer)) {
        try (final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer))) {
            request = out.getHeader();
            reply = out.getRaftClientReplyFuture();
            // write using DataStreamOutput
            DataStreamTestUtils.writeAndAssertReplies(out, 1000, 10);
        }
    }
    watchOrSleep(cluster, reply.join().getLogIndex());
    assertLogEntry(cluster, request);
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 4 with DataStreamOutputImpl

use of org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl in project incubator-ratis by apache.

the class DataStreamClusterTests method writeAsyncDefaultFileRegion.

static CheckedConsumer<DataStreamOutputImpl, Exception> writeAsyncDefaultFileRegion(File f, int size) {
    return new CheckedConsumer<DataStreamOutputImpl, Exception>() {

        @Override
        public void accept(DataStreamOutputImpl out) {
            final DataStreamReply dataStreamReply = out.writeAsync(f).join();
            DataStreamTestUtils.assertSuccessReply(Type.STREAM_DATA, size, dataStreamReply);
        }

        @Override
        public String toString() {
            return "writeAsyncDefaultFileRegion";
        }
    };
}
Also used : CheckedConsumer(org.apache.ratis.util.function.CheckedConsumer) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) DataStreamReply(org.apache.ratis.protocol.DataStreamReply)

Example 5 with DataStreamOutputImpl

use of org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl in project incubator-ratis by apache.

the class DataStreamClusterTests method runTestWriteFile.

void runTestWriteFile(CLUSTER cluster, int i, CheckedConsumer<DataStreamOutputImpl, Exception> testCase) throws Exception {
    final RaftClientRequest request;
    final CompletableFuture<RaftClientReply> reply;
    final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
    try (RaftClient client = cluster.createClient(primaryServer)) {
        try (final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer))) {
            request = out.getHeader();
            reply = out.getRaftClientReplyFuture();
            final Timestamp start = Timestamp.currentTime();
            testCase.accept(out);
            LOG.info("{}: {} elapsed {}ms", i, testCase, start.elapsedTimeMs());
        }
    }
    watchOrSleep(cluster, reply.join().getLogIndex());
    assertLogEntry(cluster, request);
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftPeer(org.apache.ratis.protocol.RaftPeer) Timestamp(org.apache.ratis.util.Timestamp) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

DataStreamOutputImpl (org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl)6 RaftClient (org.apache.ratis.client.RaftClient)4 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)4 RaftPeer (org.apache.ratis.protocol.RaftPeer)3 DataStreamReply (org.apache.ratis.protocol.DataStreamReply)2 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)2 RaftServer (org.apache.ratis.server.RaftServer)2 CheckedConsumer (org.apache.ratis.util.function.CheckedConsumer)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ClientId (org.apache.ratis.protocol.ClientId)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 DataStreamServer (org.apache.ratis.server.DataStreamServer)1 Timestamp (org.apache.ratis.util.Timestamp)1