Search in sources :

Example 1 with InvalidProtocolBufferException

use of org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException in project incubator-ratis by apache.

the class RaftAsyncTests method testStaleReadAsync.

@Test
public void testStaleReadAsync() throws Exception {
    final int numMesssages = 10;
    final CLUSTER cluster = getFactory().newCluster(NUM_SERVERS, properties);
    try (RaftClient client = cluster.createClient()) {
        cluster.start();
        RaftTestUtil.waitForLeader(cluster);
        // submit some messages
        final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
        for (int i = 0; i < numMesssages; i++) {
            final String s = "m" + i;
            LOG.info("sendAsync " + s);
            futures.add(client.sendAsync(new RaftTestUtil.SimpleMessage(s)));
        }
        Assert.assertEquals(numMesssages, futures.size());
        RaftClientReply lastWriteReply = null;
        for (CompletableFuture<RaftClientReply> f : futures) {
            lastWriteReply = f.join();
            Assert.assertTrue(lastWriteReply.isSuccess());
        }
        futures.clear();
        // Use a follower with the max commit index
        final RaftPeerId leader = lastWriteReply.getServerId();
        LOG.info("leader = " + leader);
        final Collection<CommitInfoProto> commitInfos = lastWriteReply.getCommitInfos();
        LOG.info("commitInfos = " + commitInfos);
        final CommitInfoProto followerCommitInfo = commitInfos.stream().filter(info -> !RaftPeerId.valueOf(info.getServer().getId()).equals(leader)).max(Comparator.comparing(CommitInfoProto::getCommitIndex)).get();
        final RaftPeerId follower = RaftPeerId.valueOf(followerCommitInfo.getServer().getId());
        LOG.info("max follower = " + follower);
        // test a failure case
        testFailureCaseAsync("sendStaleReadAsync(..) with a larger commit index", () -> client.sendStaleReadAsync(new RaftTestUtil.SimpleMessage("" + (numMesssages + 1)), followerCommitInfo.getCommitIndex(), follower), StateMachineException.class, IndexOutOfBoundsException.class);
        // test sendStaleReadAsync
        for (int i = 1; i < followerCommitInfo.getCommitIndex(); i++) {
            final int query = i;
            LOG.info("sendStaleReadAsync, query=" + query);
            final Message message = new RaftTestUtil.SimpleMessage("" + query);
            final CompletableFuture<RaftClientReply> readFuture = client.sendReadOnlyAsync(message);
            final CompletableFuture<RaftClientReply> staleReadFuture = client.sendStaleReadAsync(message, followerCommitInfo.getCommitIndex(), follower);
            futures.add(readFuture.thenApply(r -> getMessageContent(r)).thenCombine(staleReadFuture.thenApply(r -> getMessageContent(r)), (expected, computed) -> {
                try {
                    LOG.info("query " + query + " returns " + LogEntryProto.parseFrom(expected).getSmLogEntry().getData().toStringUtf8());
                } catch (InvalidProtocolBufferException e) {
                    throw new CompletionException(e);
                }
                Assert.assertEquals("log entry mismatch for query=" + query, expected, computed);
                return null;
            }));
        }
        JavaUtils.allOf(futures).join();
    } finally {
        cluster.shutdown();
    }
}
Also used : ByteString(org.apache.ratis.shaded.com.google.protobuf.ByteString) InvalidProtocolBufferException(org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException) RaftServerProxy(org.apache.ratis.server.impl.RaftServerProxy) org.apache.ratis.protocol(org.apache.ratis.protocol) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(org.apache.log4j.Level) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) CommitInfoProto(org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto) JavaUtils(org.apache.ratis.util.JavaUtils) LogUtils(org.apache.ratis.util.LogUtils) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) java.util.concurrent(java.util.concurrent) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) IOException(java.io.IOException) List(java.util.List) RaftProperties(org.apache.ratis.conf.RaftProperties) org.junit(org.junit) RaftClient(org.apache.ratis.client.RaftClient) Comparator(java.util.Comparator) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) TimeDuration(org.apache.ratis.util.TimeDuration) CommitInfoProto(org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException) ByteString(org.apache.ratis.shaded.com.google.protobuf.ByteString) RaftClient(org.apache.ratis.client.RaftClient)

Example 2 with InvalidProtocolBufferException

use of org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException in project incubator-ratis by apache.

the class FileStoreStateMachine method writeStateMachineData.

@Override
public CompletableFuture<Integer> writeStateMachineData(LogEntryProto entry) {
    final SMLogEntryProto smLog = entry.getSmLogEntry();
    final ByteString data = smLog.getData();
    final FileStoreRequestProto proto;
    try {
        proto = FileStoreRequestProto.parseFrom(data);
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally(entry.getIndex(), "Failed to parse data, entry=" + entry, e);
    }
    if (proto.getRequestCase() != FileStoreRequestProto.RequestCase.WRITEHEADER) {
        return null;
    }
    final WriteRequestHeaderProto h = proto.getWriteHeader();
    final CompletableFuture<Integer> f = files.write(entry.getIndex(), h.getPath().toStringUtf8(), h.getClose(), h.getOffset(), smLog.getStateMachineData());
    // sync only if closing the file
    return h.getClose() ? f : null;
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) ByteString(org.apache.ratis.shaded.com.google.protobuf.ByteString) InvalidProtocolBufferException(org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException)

Example 3 with InvalidProtocolBufferException

use of org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException in project incubator-ratis by apache.

the class FileStoreStateMachine method query.

@Override
public CompletableFuture<Message> query(Message request) {
    final ReadRequestProto proto;
    try {
        proto = ReadRequestProto.parseFrom(request.getContent());
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally("Failed to parse " + request, e);
    }
    final String path = proto.getPath().toStringUtf8();
    return files.read(path, proto.getOffset(), proto.getLength()).thenApply(reply -> Message.valueOf(reply.toByteString()));
}
Also used : InvalidProtocolBufferException(org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException) ByteString(org.apache.ratis.shaded.com.google.protobuf.ByteString)

Example 4 with InvalidProtocolBufferException

use of org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException in project incubator-ratis by apache.

the class FileStoreStateMachine method applyTransaction.

@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
    final LogEntryProto entry = trx.getLogEntry();
    final long index = entry.getIndex();
    updateLastAppliedTermIndex(entry.getTerm(), index);
    final SMLogEntryProto smLog = entry.getSmLogEntry();
    final FileStoreRequestProto request;
    try {
        request = FileStoreRequestProto.parseFrom(smLog.getData());
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally(index, "Failed to parse SmLogEntry", e);
    }
    switch(request.getRequestCase()) {
        case DELETE:
            return delete(index, request.getDelete());
        case WRITEHEADER:
            return writeCommit(index, request.getWriteHeader(), smLog.getStateMachineData().size());
        case WRITE:
        // startTransaction converts WRITE requests to WRITEHEADER requests.
        default:
            LOG.error(getId() + ": Unexpected request case " + request.getRequestCase());
            return FileStoreCommon.completeExceptionally(index, "Unexpected request case " + request.getRequestCase());
    }
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) InvalidProtocolBufferException(org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException)

Aggregations

InvalidProtocolBufferException (org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException)4 ByteString (org.apache.ratis.shaded.com.google.protobuf.ByteString)3 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)2 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 java.util.concurrent (java.util.concurrent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Level (org.apache.log4j.Level)1 RaftTestUtil.waitForLeader (org.apache.ratis.RaftTestUtil.waitForLeader)1 RaftClient (org.apache.ratis.client.RaftClient)1 RaftClientConfigKeys (org.apache.ratis.client.RaftClientConfigKeys)1 RaftClientTestUtil (org.apache.ratis.client.impl.RaftClientTestUtil)1 RaftProperties (org.apache.ratis.conf.RaftProperties)1 org.apache.ratis.protocol (org.apache.ratis.protocol)1 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)1 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)1