Search in sources :

Example 41 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class OutputStreamBaseTest method runTestWriteWithOffset.

private void runTestWriteWithOffset(CLUSTER cluster) throws Exception {
    final int bufferSize = ByteValue.BUFFERSIZE;
    final RaftServer.Division leader = waitForLeader(cluster);
    final OutputStream out = newOutputStream(cluster, bufferSize);
    byte[] b1 = new byte[ByteValue.BUFFERSIZE / 2];
    Arrays.fill(b1, (byte) 1);
    byte[] b2 = new byte[ByteValue.BUFFERSIZE];
    Arrays.fill(b2, (byte) 2);
    byte[] b3 = new byte[ByteValue.BUFFERSIZE * 2 + ByteValue.BUFFERSIZE / 2];
    Arrays.fill(b3, (byte) 3);
    byte[] b4 = new byte[ByteValue.BUFFERSIZE * 4];
    Arrays.fill(b3, (byte) 4);
    byte[] expected = new byte[ByteValue.BUFFERSIZE * 8];
    byte[][] data = new byte[][] { b1, b2, b3, b4 };
    final Random random = new Random();
    int totalSize = 0;
    for (byte[] b : data) {
        System.arraycopy(b, 0, expected, totalSize, b.length);
        totalSize += b.length;
        int written = 0;
        while (written < b.length) {
            int toWrite = random.nextInt(b.length - written) + 1;
            LOG.info("write {} bytes", toWrite);
            out.write(b, written, toWrite);
            written += toWrite;
        }
    }
    out.close();
    // 0.5 + 1 + 2.5 + 4 = 8
    final int expectedEntries = 8;
    final RaftLog raftLog = assertRaftLog(expectedEntries, leader);
    final LogEntryHeader[] entries = raftLog.getEntries(1, Long.MAX_VALUE);
    final byte[] actual = new byte[ByteValue.BUFFERSIZE * expectedEntries];
    totalSize = 0;
    for (LogEntryHeader ti : entries) {
        final LogEntryProto e = raftLog.get(ti.getIndex());
        if (e.hasStateMachineLogEntry()) {
            final byte[] eValue = e.getStateMachineLogEntry().getLogData().toByteArray();
            Assert.assertEquals(ByteValue.BUFFERSIZE, eValue.length);
            System.arraycopy(eValue, 0, actual, totalSize, eValue.length);
            totalSize += eValue.length;
        }
    }
    Assert.assertArrayEquals(expected, actual);
}
Also used : LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Random(java.util.Random) RaftServer(org.apache.ratis.server.RaftServer) RaftOutputStream(org.apache.ratis.client.impl.RaftOutputStream) OutputStream(java.io.OutputStream) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 42 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class RaftTestUtil method getStateMachineLogEntries.

static List<LogEntryProto> getStateMachineLogEntries(RaftLog log) {
    final List<LogEntryProto> entries = new ArrayList<>();
    for (LogEntryProto e : getLogEntryProtos(log)) {
        final String s = LogProtoUtils.toLogEntryString(e);
        if (e.hasStateMachineLogEntry()) {
            LOG.info(s + ", " + e.getStateMachineLogEntry().toString().trim().replace("\n", ", "));
            entries.add(e);
        } else if (e.hasConfigurationEntry()) {
            LOG.info("Found {}, ignoring it.", s);
        } else if (e.hasMetadataEntry()) {
            LOG.info("Found {}, ignoring it.", s);
        } else {
            throw new AssertionError("Unexpected LogEntryBodyCase " + e.getLogEntryBodyCase() + " at " + s);
        }
    }
    return entries;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) ArrayList(java.util.ArrayList) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString)

Example 43 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class RaftTestUtil method countEntries.

static EnumMap<LogEntryBodyCase, AtomicLong> countEntries(RaftLog raftLog) throws Exception {
    final EnumMap<LogEntryBodyCase, AtomicLong> counts = new EnumMap<>(LogEntryBodyCase.class);
    for (long i = 0; i < raftLog.getNextIndex(); i++) {
        final LogEntryProto e = raftLog.get(i);
        counts.computeIfAbsent(e.getLogEntryBodyCase(), c -> new AtomicLong()).incrementAndGet();
    }
    return counts;
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) BooleanSupplier(java.util.function.BooleanSupplier) ProtoUtils(org.apache.ratis.util.ProtoUtils) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) JavaUtils(org.apache.ratis.util.JavaUtils) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) EnumMap(java.util.EnumMap) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) Objects(java.util.Objects) List(java.util.List) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase) ClientId(org.apache.ratis.protocol.ClientId) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Preconditions(org.apache.ratis.thirdparty.com.google.common.base.Preconditions) Optional(java.util.Optional) DelayLocalExecutionInjection(org.apache.ratis.server.impl.DelayLocalExecutionInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftLog(org.apache.ratis.server.raftlog.RaftLog) CollectionUtils(org.apache.ratis.util.CollectionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Message(org.apache.ratis.protocol.Message) StreamSupport(java.util.stream.StreamSupport) IntSupplier(java.util.function.IntSupplier) AssumptionViolatedException(org.junit.AssumptionViolatedException) Logger(org.slf4j.Logger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) IOException(java.io.IOException) Field(java.lang.reflect.Field) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) RaftServer(org.apache.ratis.server.RaftServer) RaftLogBase(org.apache.ratis.server.raftlog.RaftLogBase) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) EnumMap(java.util.EnumMap) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase)

Example 44 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class SimpleStateMachine4Testing method query.

/**
 * Query the n-th log entry.
 * @param request an index represented in a UTF-8 String, or an empty message.
 * @return a completed future of the n-th log entry,
 *         where n is the last applied index if the request is empty,
 *         otherwise, n is the index represented in the request.
 */
@Override
public CompletableFuture<Message> query(Message request) {
    final String string = request.getContent().toStringUtf8();
    Exception exception;
    try {
        LOG.info("query " + string);
        final LogEntryProto entry = dataMap.get(string);
        if (entry != null) {
            return CompletableFuture.completedFuture(Message.valueOf(entry.toByteString()));
        }
        exception = new IndexOutOfBoundsException(getId() + ": LogEntry not found for query " + string);
    } catch (Exception e) {
        LOG.warn("Failed request " + request, e);
        exception = e;
    }
    return JavaUtils.completeExceptionally(new StateMachineException("Failed request " + request, exception));
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 45 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class MemoryRaftLogTest method testEntryDoNotPerformTruncation.

@Test
public void testEntryDoNotPerformTruncation() throws Exception {
    final RaftProperties prop = new RaftProperties();
    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    final RaftPeerId peerId = RaftPeerId.valueOf("s0");
    final RaftGroupId groupId = RaftGroupId.randomId();
    final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
    MemoryRaftLog raftLog = new MemoryRaftLog(memberId, () -> -1, prop);
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    LogEntryProto[] entries1 = new LogEntryProto[2];
    entries1[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
    entries1[1] = LogEntryProto.newBuilder().setIndex(1).setTerm(0).build();
    raftLog.append(entries1).forEach(CompletableFuture::join);
    LogEntryProto[] entries2 = new LogEntryProto[1];
    entries2[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
    raftLog.append(entries2).forEach(CompletableFuture::join);
    final LogEntryHeader[] termIndices = raftLog.getEntries(0, 10);
    assertEquals(2, termIndices.length);
    for (int i = 0; i < 2; i++) {
        assertEquals(entries1[i].getIndex(), termIndices[i].getIndex());
        assertEquals(entries1[i].getTerm(), termIndices[i].getTerm());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)61 Test (org.junit.Test)22 BaseTest (org.apache.ratis.BaseTest)20 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)15 CompletableFuture (java.util.concurrent.CompletableFuture)14 File (java.io.File)13 IOException (java.io.IOException)13 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)12 RaftLog (org.apache.ratis.server.raftlog.RaftLog)12 RaftStorage (org.apache.ratis.server.storage.RaftStorage)11 ArrayList (java.util.ArrayList)10 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)10 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)7 RaftClient (org.apache.ratis.client.RaftClient)6 RaftProperties (org.apache.ratis.conf.RaftProperties)6 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)6 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)6 RaftServer (org.apache.ratis.server.RaftServer)6 RandomAccessFile (java.io.RandomAccessFile)5