use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollCycleMultiThreadTest method testRead2.
@Test
public void testRead2() throws Exception {
File path = DirectoryUtils.tempDir("testRead2");
TestTimeProvider timeProvider = new TestTimeProvider();
try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
final ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("Day 1 data");
}
Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer).get());
// two days pass
timeProvider.add(TimeUnit.DAYS.toMillis(2));
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("Day 3 data");
}
Assert.assertEquals(2, (int) scheduledExecutorService.submit(observer).get());
System.out.println(queue.dump());
assertEquals(2, observer.documentsRead);
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollCycleMultiThreadTest method testRead1.
@Test
public void testRead1() throws Exception {
File path = DirectoryUtils.tempDir(getClass().getSimpleName());
TestTimeProvider timeProvider = new TestTimeProvider();
try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
ExcerptAppender appender = queue.acquireAppender();
Assert.assertEquals(0, (int) scheduledExecutorService.submit(observer::call).get());
// two days pass
timeProvider.add(TimeUnit.DAYS.toMillis(2));
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("Day 3 data");
}
Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer::call).get());
assertEquals(1, observer.documentsRead);
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueStoreTest method writeMessagesStoreIndices.
private static long[] writeMessagesStoreIndices(final ExcerptAppender appender, final ExcerptTailer tailer) {
final long[] indices = new long[RECORD_COUNT];
for (int i = 0; i < RECORD_COUNT; i++) {
try (final DocumentContext ctx = appender.writingDocument()) {
ctx.wire().getValueOut().int32(i);
}
}
for (int i = 0; i < RECORD_COUNT; i++) {
try (final DocumentContext ctx = tailer.readingDocument()) {
assertThat("Expected record at index " + i, ctx.isPresent(), is(true));
indices[i] = tailer.index();
}
}
return indices;
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class GarbageFreeMethodPublisher method onEightyByteMessage.
@Override
public void onEightyByteMessage(final EightyByteMessage message) {
final ExcerptAppender appender = outputSupplier.get();
// DebugTimestamps.operationStart(DebugTimestamps.Operation.GET_WRITING_DOCUMENT);
@NotNull DocumentContext context = appender.writingDocument();
// DebugTimestamps.operationEnd(DebugTimestamps.Operation.GET_WRITING_DOCUMENT);
try {
Wire wire = context.wire();
// DebugTimestamps.operationStart(DebugTimestamps.Operation.WRITE_EVENT);
try {
wire.write(MethodReader.HISTORY).marshallable(MessageHistory.get());
final ValueOut valueOut = wire.writeEventName("onEightyByteMessage");
valueOut.object(EightyByteMessage.class, message);
wire.padToCacheAlign();
} finally {
// DebugTimestamps.operationEnd(DebugTimestamps.Operation.WRITE_EVENT);
}
} finally {
// DebugTimestamps.operationStart(DebugTimestamps.Operation.CLOSE_CONTEXT);
try {
context.close();
} finally {
// DebugTimestamps.operationEnd(DebugTimestamps.Operation.CLOSE_CONTEXT);
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class VisibilityOfMessagesBetweenTailorsAndAppenderTest method test.
/**
* check if a message is written with an appender its visible to the tailor, without locks etc.
*
* @throws InterruptedException
* @throws ExecutionException
*/
@Test
public void test() throws InterruptedException, ExecutionException {
SingleChronicleQueue x = SingleChronicleQueueBuilder.binary(getTmpDir()).build();
ExecutorService e1 = Executors.newSingleThreadExecutor();
e1.submit(() -> {
ExcerptAppender excerptAppender = x.acquireAppender();
for (long i = 0; i < 1_000_000; i++) {
try (DocumentContext dc = excerptAppender.writingDocument()) {
dc.wire().getValueOut().int64(i);
}
lastWrittenIndex = excerptAppender.lastIndexAppended();
}
});
ExecutorService e2 = Executors.newSingleThreadExecutor();
Future f2 = e2.submit(() -> {
ExcerptTailer tailer = x.createTailer();
for (; ; ) {
long i = lastWrittenIndex;
if (i != Long.MIN_VALUE)
if (!tailer.moveToIndex(i))
throw new ExecutionException("non atomic, index=" + Long.toHexString(i), null);
}
});
try {
f2.get(5, TimeUnit.SECONDS);
} catch (TimeoutException ignore) {
}
e2.shutdownNow();
e1.shutdownNow();
}
Aggregations