Search in sources :

Example 1 with WallClockTimestamp

use of io.atomix.utils.time.WallClockTimestamp in project atomix by atomix.

the class FileSnapshotStoreTest method testStreamSnapshot.

/**
 * Tests writing multiple times to a snapshot designed to mimic chunked snapshots from leaders.
 */
@Test
public void testStreamSnapshot() throws Exception {
    SnapshotStore store = createSnapshotStore();
    Snapshot snapshot = store.newSnapshot(1, new WallClockTimestamp());
    for (long i = 1; i <= 10; i++) {
        try (SnapshotWriter writer = snapshot.openWriter()) {
            writer.writeLong(i);
        }
    }
    snapshot.complete();
    snapshot = store.getSnapshot(1);
    try (SnapshotReader reader = snapshot.openReader()) {
        for (long i = 1; i <= 10; i++) {
            assertEquals(i, reader.readLong());
        }
    }
}
Also used : WallClockTimestamp(io.atomix.utils.time.WallClockTimestamp) Test(org.junit.Test)

Example 2 with WallClockTimestamp

use of io.atomix.utils.time.WallClockTimestamp in project atomix by atomix.

the class RaftServiceManager method snapshot.

/**
 * Takes snapshots for the given index.
 *
 * @param index the index for which to take snapshots
 */
private Snapshot snapshot(long index) {
    Snapshot snapshot = raft.getSnapshotStore().newTemporarySnapshot(index, new WallClockTimestamp());
    try (SnapshotWriter writer = snapshot.openWriter()) {
        for (RaftServiceContext service : raft.getServices()) {
            writer.buffer().mark();
            SnapshotWriter serviceWriter = new SnapshotWriter(writer.buffer().writeInt(0).slice(), writer.snapshot());
            snapshotService(serviceWriter, service);
            int length = serviceWriter.buffer().position();
            writer.buffer().reset().writeInt(length).skip(length);
        }
    } catch (Exception e) {
        snapshot.close();
        throw e;
    }
    return snapshot;
}
Also used : Snapshot(io.atomix.protocols.raft.storage.snapshot.Snapshot) WallClockTimestamp(io.atomix.utils.time.WallClockTimestamp) SnapshotWriter(io.atomix.protocols.raft.storage.snapshot.SnapshotWriter) RaftServiceContext(io.atomix.protocols.raft.service.RaftServiceContext) RaftException(io.atomix.protocols.raft.RaftException)

Example 3 with WallClockTimestamp

use of io.atomix.utils.time.WallClockTimestamp in project atomix by atomix.

the class DefaultServiceExecutorTest method testScheduling.

@Test
public void testScheduling() throws Exception {
    ServiceExecutor executor = executor();
    executor.register(OperationId.command("a"), () -> {
    });
    executor.apply(commit(OperationId.command("a"), 1, null, 0));
    Set<String> calls = new HashSet<>();
    executor.tick(new WallClockTimestamp(1));
    executor.schedule(Duration.ofMillis(100), () -> calls.add("a"));
    executor.tick(new WallClockTimestamp(100));
    assertFalse(calls.contains("a"));
    executor.tick(new WallClockTimestamp(101));
    assertTrue(calls.contains("a"));
}
Also used : WallClockTimestamp(io.atomix.utils.time.WallClockTimestamp) DefaultServiceExecutor(io.atomix.primitive.service.impl.DefaultServiceExecutor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with WallClockTimestamp

use of io.atomix.utils.time.WallClockTimestamp in project atomix by atomix.

the class AbstractSnapshotStoreTest method testWriteSnapshotChunks.

/**
 * Tests writing a snapshot.
 */
@Test
public void testWriteSnapshotChunks() {
    SnapshotStore store = createSnapshotStore();
    WallClockTimestamp timestamp = new WallClockTimestamp();
    Snapshot snapshot = store.newSnapshot(2, timestamp);
    assertEquals(snapshot.index(), 2);
    assertEquals(snapshot.timestamp(), timestamp);
    assertNull(store.getSnapshot(2));
    try (SnapshotWriter writer = snapshot.openWriter()) {
        writer.writeLong(10);
    }
    assertNull(store.getSnapshot(2));
    try (SnapshotWriter writer = snapshot.openWriter()) {
        writer.writeLong(11);
    }
    assertNull(store.getSnapshot(2));
    try (SnapshotWriter writer = snapshot.openWriter()) {
        writer.writeLong(12);
    }
    assertNull(store.getSnapshot(2));
    snapshot.complete();
    assertEquals(store.getSnapshot(2).index(), 2);
    try (SnapshotReader reader = store.getSnapshot(2).openReader()) {
        assertEquals(reader.readLong(), 10);
        assertEquals(reader.readLong(), 11);
        assertEquals(reader.readLong(), 12);
    }
}
Also used : WallClockTimestamp(io.atomix.utils.time.WallClockTimestamp) Test(org.junit.Test)

Example 5 with WallClockTimestamp

use of io.atomix.utils.time.WallClockTimestamp in project atomix by atomix.

the class FileSnapshotStoreTest method testStoreLoadSnapshot.

/**
 * Tests storing and loading snapshots.
 */
@Test
public void testStoreLoadSnapshot() {
    SnapshotStore store = createSnapshotStore();
    Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
    try (SnapshotWriter writer = snapshot.openWriter()) {
        writer.writeLong(10);
    }
    snapshot.complete();
    assertNotNull(store.getSnapshot(2));
    store.close();
    store = createSnapshotStore();
    assertNotNull(store.getSnapshot(2));
    assertEquals(store.getSnapshot(2).index(), 2);
    try (SnapshotReader reader = snapshot.openReader()) {
        assertEquals(reader.readLong(), 10);
    }
}
Also used : WallClockTimestamp(io.atomix.utils.time.WallClockTimestamp) Test(org.junit.Test)

Aggregations

WallClockTimestamp (io.atomix.utils.time.WallClockTimestamp)6 Test (org.junit.Test)5 DefaultServiceExecutor (io.atomix.primitive.service.impl.DefaultServiceExecutor)1 RaftException (io.atomix.protocols.raft.RaftException)1 RaftServiceContext (io.atomix.protocols.raft.service.RaftServiceContext)1 Snapshot (io.atomix.protocols.raft.storage.snapshot.Snapshot)1 SnapshotWriter (io.atomix.protocols.raft.storage.snapshot.SnapshotWriter)1 HashSet (java.util.HashSet)1