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();
}
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();
}
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();
}
}
}
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());
}
}
Aggregations