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