Search in sources :

Example 1 with MessageOutputStream

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

the class MessageStreamImpl method streamAsync.

@Override
public CompletableFuture<RaftClientReply> streamAsync(Message message, SizeInBytes subSize) {
    final int n = subSize.getSizeInt();
    final MessageOutputStream out = stream();
    final ByteString bytes = message.getContent();
    for (int i = 0; i < bytes.size(); ) {
        final int j = Math.min(i + n, bytes.size());
        final ByteString sub = bytes.substring(i, j);
        out.sendAsync(Message.valueOf(sub));
        i = j;
    }
    return out.closeAsync();
}
Also used : MessageOutputStream(org.apache.ratis.client.api.MessageOutputStream) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString)

Example 2 with MessageOutputStream

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

the class MessageStreamApiTests method runTestStream.

void runTestStream(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    // stream multiple parts
    final int numParts = 9;
    final int endOfRequest = 6;
    final StringBuilder key = new StringBuilder();
    try (RaftClient client = cluster.createClient();
        MessageOutputStream out = client.getMessageStreamApi().stream()) {
        for (int i = 1; i <= numParts; i++) {
            key.append(i);
            out.sendAsync(new SimpleMessage(i + ""), i == endOfRequest);
        }
    }
    // check if all the parts are streamed as a single message.
    final String k = key.toString();
    try (RaftClient client = cluster.createClient()) {
        final String k1 = k.substring(0, endOfRequest);
        final RaftClientReply r1 = client.io().sendReadOnly(new SimpleMessage(k1));
        Assert.assertTrue(r1.isSuccess());
        final String k2 = k.substring(endOfRequest);
        final RaftClientReply r2 = client.io().sendReadOnly(new SimpleMessage(k2));
        Assert.assertTrue(r2.isSuccess());
    }
}
Also used : MessageOutputStream(org.apache.ratis.client.api.MessageOutputStream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

MessageOutputStream (org.apache.ratis.client.api.MessageOutputStream)2 ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)2 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)1 RaftClient (org.apache.ratis.client.RaftClient)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1