Search in sources :

Example 21 with TermIndex

use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.

the class TestRaftStream method testWriteWithOffset.

@Test
public void testWriteWithOffset() throws Exception {
    LOG.info("Running testWriteWithOffset");
    GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(ByteValue.BUFFERSIZE));
    cluster = MiniRaftClusterWithGRpc.FACTORY.newCluster(NUM_SERVERS, prop);
    cluster.start();
    RaftServerImpl leader = waitForLeader(cluster);
    RaftOutputStream out = new RaftOutputStream(prop, ClientId.randomId(), cluster.getGroup(), leader.getId());
    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();
    final RaftLog log = leader.getState().getLog();
    // 0.5 + 1 + 2.5 + 4 = 8
    Assert.assertEquals(8, leader.getState().getLastAppliedIndex());
    Assert.assertEquals(8, log.getLastCommittedIndex());
    TermIndex[] entries = log.getEntries(1, 9);
    byte[] actual = new byte[ByteValue.BUFFERSIZE * 8];
    totalSize = 0;
    for (TermIndex e : entries) {
        byte[] eValue = log.get(e.getIndex()).getSmLogEntry().getData().toByteArray();
        Assert.assertEquals(ByteValue.BUFFERSIZE, eValue.length);
        System.arraycopy(eValue, 0, actual, totalSize, eValue.length);
        totalSize += eValue.length;
    }
    Assert.assertArrayEquals(expected, actual);
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftOutputStream(org.apache.ratis.grpc.client.RaftOutputStream) RaftLog(org.apache.ratis.server.storage.RaftLog) TermIndex(org.apache.ratis.server.protocol.TermIndex) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 22 with TermIndex

use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.

the class RaftStorageTestUtils method printLog.

static void printLog(RaftLog log, Consumer<String> println) {
    if (log == null) {
        println.accept("log == null");
        return;
    }
    final TermIndex last;
    final long flushed, committed;
    try (AutoCloseableLock readlock = log.readLock()) {
        last = log.getLastEntryTermIndex();
        flushed = log.getLatestFlushedIndex();
        committed = log.getLastCommittedIndex();
    }
    final StringBuilder b = new StringBuilder();
    for (long i = 0; i <= last.getIndex(); i++) {
        b.setLength(0);
        b.append(i == flushed ? 'f' : ' ');
        b.append(i == committed ? 'c' : ' ');
        b.append(String.format("%3d: ", i));
        try {
            final RaftProtos.LogEntryProto entry = log.get(i);
            b.append(entry != null ? entry.getLogEntryBodyCase() : null);
        } catch (RaftLogIOException e) {
            b.append(e);
        }
        println.accept(b.toString());
    }
}
Also used : RaftProtos(org.apache.ratis.shaded.proto.RaftProtos) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 23 with TermIndex

use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.

the class TestRaftStorage method testSnapshotFileName.

@Test
public void testSnapshotFileName() throws Exception {
    final long term = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
    final long index = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
    final String name = SimpleStateMachineStorage.getSnapshotFileName(term, index);
    System.out.println("name = " + name);
    final File file = new File(storageDir, name);
    final TermIndex ti = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(file);
    System.out.println("file = " + file);
    Assert.assertEquals(term, ti.getTerm());
    Assert.assertEquals(index, ti.getIndex());
    System.out.println("ti = " + ti);
    final File foo = new File(storageDir, "foo");
    try {
        SimpleStateMachineStorage.getTermIndexFromSnapshotFile(foo);
        Assert.fail();
    } catch (IllegalArgumentException iae) {
        System.out.println("Good " + iae);
    }
}
Also used : File(java.io.File) TermIndex(org.apache.ratis.server.protocol.TermIndex) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 24 with TermIndex

use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.

the class TestSegmentedRaftLog method checkEntries.

private void checkEntries(RaftLog raftLog, List<LogEntryProto> expected, int offset, int size) throws IOException {
    if (size > 0) {
        for (int i = offset; i < size + offset; i++) {
            LogEntryProto entry = raftLog.get(expected.get(i).getIndex());
            Assert.assertEquals(expected.get(i), entry);
        }
        TermIndex[] termIndices = raftLog.getEntries(expected.get(offset).getIndex(), expected.get(offset + size - 1).getIndex() + 1);
        LogEntryProto[] entriesFromLog = Arrays.stream(termIndices).map(ti -> {
            try {
                return raftLog.get(ti.getIndex());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).toArray(LogEntryProto[]::new);
        LogEntryProto[] expectedArray = expected.subList(offset, offset + size).stream().toArray(LogEntryProto[]::new);
        Assert.assertArrayEquals(expectedArray, entriesFromLog);
    }
}
Also used : Mockito.doCallRealMethod(org.mockito.Mockito.doCallRealMethod) Arrays(java.util.Arrays) TermIndex(org.apache.ratis.server.protocol.TermIndex) RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ProtoUtils(org.apache.ratis.util.ProtoUtils) Level(org.apache.log4j.Level) After(org.junit.After) SizeInBytes(org.apache.ratis.util.SizeInBytes) Before(org.junit.Before) LogUtils(org.apache.ratis.util.LogUtils) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) BaseTest(org.apache.ratis.BaseTest) File(java.io.File) FileUtils(org.apache.ratis.util.FileUtils) Matchers.any(org.mockito.Matchers.any) List(java.util.List) ClientId(org.apache.ratis.protocol.ClientId) RaftProperties(org.apache.ratis.conf.RaftProperties) RetryCache(org.apache.ratis.server.impl.RetryCache) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Assert(org.junit.Assert) Collections(java.util.Collections) RaftServerConstants(org.apache.ratis.server.impl.RaftServerConstants) Mockito.mock(org.mockito.Mockito.mock) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) IOException(java.io.IOException) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 25 with TermIndex

use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.

the class TestSegmentedRaftLog method testLoadLogSegments.

@Test
public void testLoadLogSegments() throws Exception {
    // first generate log files
    List<SegmentRange> ranges = prepareRanges(5, 100, 0);
    LogEntryProto[] entries = prepareLog(ranges);
    // create RaftLog object and load log file
    try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, null, storage, -1, properties)) {
        raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
        // check if log entries are loaded correctly
        for (LogEntryProto e : entries) {
            LogEntryProto entry = raftLog.get(e.getIndex());
            Assert.assertEquals(e, entry);
        }
        TermIndex[] termIndices = raftLog.getEntries(0, 500);
        LogEntryProto[] entriesFromLog = Arrays.stream(termIndices).map(ti -> {
            try {
                return raftLog.get(ti.getIndex());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).toArray(LogEntryProto[]::new);
        Assert.assertArrayEquals(entries, entriesFromLog);
        Assert.assertEquals(entries[entries.length - 1], getLastEntry(raftLog));
    }
}
Also used : Mockito.doCallRealMethod(org.mockito.Mockito.doCallRealMethod) Arrays(java.util.Arrays) TermIndex(org.apache.ratis.server.protocol.TermIndex) RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ProtoUtils(org.apache.ratis.util.ProtoUtils) Level(org.apache.log4j.Level) After(org.junit.After) SizeInBytes(org.apache.ratis.util.SizeInBytes) Before(org.junit.Before) LogUtils(org.apache.ratis.util.LogUtils) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) BaseTest(org.apache.ratis.BaseTest) File(java.io.File) FileUtils(org.apache.ratis.util.FileUtils) Matchers.any(org.mockito.Matchers.any) List(java.util.List) ClientId(org.apache.ratis.protocol.ClientId) RaftProperties(org.apache.ratis.conf.RaftProperties) RetryCache(org.apache.ratis.server.impl.RetryCache) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Assert(org.junit.Assert) Collections(java.util.Collections) RaftServerConstants(org.apache.ratis.server.impl.RaftServerConstants) Mockito.mock(org.mockito.Mockito.mock) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) IOException(java.io.IOException) TermIndex(org.apache.ratis.server.protocol.TermIndex) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

TermIndex (org.apache.ratis.server.protocol.TermIndex)25 IOException (java.io.IOException)8 File (java.io.File)7 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)6 SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)5 BaseTest (org.apache.ratis.BaseTest)4 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)4 Test (org.junit.Test)4 Timer (com.codahale.metrics.Timer)3 FileNotFoundException (java.io.FileNotFoundException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CompletionException (java.util.concurrent.CompletionException)3 MD5Hash (org.apache.ratis.io.MD5Hash)3 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)3 FileInfo (org.apache.ratis.server.storage.FileInfo)3 SingleFileSnapshotInfo (org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo)3 AbortedException (alluxio.exception.status.AbortedException)2 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)2 NotFoundException (alluxio.exception.status.NotFoundException)2 FileOutputStream (java.io.FileOutputStream)2