Search in sources :

Example 16 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class FileStoreStateMachine method startTransaction.

@Override
public TransactionContext startTransaction(RaftClientRequest request) throws IOException {
    final ByteString content = request.getMessage().getContent();
    final FileStoreRequestProto proto = FileStoreRequestProto.parseFrom(content);
    final TransactionContext.Builder b = TransactionContext.newBuilder().setStateMachine(this).setClientRequest(request);
    if (proto.getRequestCase() == FileStoreRequestProto.RequestCase.WRITE) {
        final WriteRequestProto write = proto.getWrite();
        final FileStoreRequestProto newProto = FileStoreRequestProto.newBuilder().setWriteHeader(write.getHeader()).build();
        b.setLogData(newProto.toByteString()).setStateMachineData(write.getData());
    } else {
        b.setLogData(content);
    }
    return b.build();
}
Also used : FileStoreRequestProto(org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) TransactionContext(org.apache.ratis.statemachine.TransactionContext) WriteRequestProto(org.apache.ratis.proto.ExamplesProtos.WriteRequestProto) StreamWriteRequestProto(org.apache.ratis.proto.ExamplesProtos.StreamWriteRequestProto)

Example 17 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class TestRaftServerWithGrpc method randomByteString.

static ByteString randomByteString(int size) {
    final ByteString.Output out = ByteString.newOutput(size);
    final ThreadLocalRandom random = ThreadLocalRandom.current();
    final byte[] buffer = new byte[4096];
    for (; size > 0; ) {
        random.nextBytes(buffer);
        final int n = Math.min(size, buffer.length);
        out.write(buffer, 0, n);
        size -= n;
    }
    return out.toByteString();
}
Also used : ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 18 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class TestRaftServerWithGrpc method testRequestMetrics.

void testRequestMetrics(MiniRaftClusterWithGrpc cluster) throws Exception {
    try (RaftClient client = cluster.createClient()) {
        // send a request to make sure leader is ready
        final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("testing"));
        Assert.assertTrue(f.get().isSuccess());
    }
    SimpleStateMachine4Testing stateMachine = SimpleStateMachine4Testing.get(cluster.getLeader());
    stateMachine.blockFlushStateMachineData();
    // Block stateMachine flush data, so that 2nd request will not be
    // completed, and so it will not be removed from pending request map.
    List<RaftClient> clients = new ArrayList<>();
    try {
        RaftClient client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
        clients.add(client);
        client.async().send(new SimpleMessage("2nd Message"));
        final SortedMap<String, Gauge> gaugeMap = getRaftServerMetrics(cluster.getLeader()).getRegistry().getGauges((s, metric) -> s.contains(REQUEST_MEGA_BYTE_SIZE));
        for (int i = 0; i < 10; i++) {
            client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
            clients.add(client);
            client.async().send(new SimpleMessage("message " + i));
        }
        // Because we have passed 11 requests, and the element queue size is 10.
        RaftTestUtil.waitFor(() -> getRaftServerMetrics(cluster.getLeader()).getCounter(REQUEST_QUEUE_LIMIT_HIT_COUNTER).getCount() == 1, 300, 5000);
        stateMachine.unblockFlushStateMachineData();
        // Send a message with 1025kb , our byte size limit is 1024kb (1mb) , so it should fail
        // and byte size counter limit will be hit.
        client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
        final SizeInBytes size = SizeInBytes.valueOf("1025kb");
        final ByteString bytes = randomByteString(size.getSizeInt());
        Assert.assertEquals(size.getSizeInt(), bytes.size());
        client.async().send(new SimpleMessage(size + "-message", bytes));
        clients.add(client);
        RaftTestUtil.waitFor(() -> getRaftServerMetrics(cluster.getLeader()).getCounter(REQUEST_BYTE_SIZE_LIMIT_HIT_COUNTER).getCount() == 1, 300, 5000);
        Assert.assertEquals(2, getRaftServerMetrics(cluster.getLeader()).getCounter(RESOURCE_LIMIT_HIT_COUNTER).getCount());
    } finally {
        for (RaftClient client : clients) {
            client.close();
        }
    }
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) SizeInBytes(org.apache.ratis.util.SizeInBytes) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) Gauge(com.codahale.metrics.Gauge) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 19 with ByteString

use of org.apache.ratis.thirdparty.com.google.protobuf.ByteString in project incubator-ratis by apache.

the class MessageStreamApiTests method runTestStreamAsync.

void runTestStreamAsync(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    ByteString bytes = ByteString.EMPTY;
    for (int i = 0; i < 10; ) {
        final String s = (char) ('A' + i) + "1234567";
        LOG.info("s=" + s);
        final ByteString b = ByteString.copyFrom(s, StandardCharsets.UTF_8);
        Assert.assertEquals(8, b.size());
        for (int j = 0; j < 128; j++) {
            bytes = bytes.concat(b);
        }
        i++;
        Assert.assertEquals(i * SUBMESSAGE_SIZE.getSizeInt(), bytes.size());
    }
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.getMessageStreamApi().streamAsync(Message.valueOf(bytes)).get();
        Assert.assertTrue(reply.isSuccess());
    }
    // check if all the parts are streamed as a single message.
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.io().sendReadOnly(new SimpleMessage(bytes.toString(StandardCharsets.UTF_8)));
        Assert.assertTrue(reply.isSuccess());
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)19 FileStoreRequestProto (org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto)4 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)4 InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)4 Test (org.junit.Test)4 BaseTest (org.apache.ratis.BaseTest)3 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)3 RaftClient (org.apache.ratis.client.RaftClient)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 WriteRequestHeaderProto (org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto)2 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)2 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)2 TimeDuration (org.apache.ratis.util.TimeDuration)2 Gauge (com.codahale.metrics.Gauge)1 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Random (java.util.Random)1