use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollCycleMultiThreadTest method testRead1.
@Test
public void testRead1() throws ExecutionException, InterruptedException {
File path = getTmpDir();
TestTimeProvider timeProvider = new TestTimeProvider();
try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("test"));
try (ChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).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);
}
scheduledExecutorService.shutdown();
scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollCycleMultiThreadTest method testRead2.
@SuppressWarnings("unchecked")
@Test
public void testRead2() throws ExecutionException, InterruptedException {
File path = getTmpDir();
TestTimeProvider timeProvider = new TestTimeProvider();
try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
final ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("test"));
try (ChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).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);
}
scheduledExecutorService.shutdown();
scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class QueueLargeMessageJLBHBenchmark method run.
@Override
public void run(long startTimeNS) {
if (MSG_DIRECT)
bytesArr2.readLimit(MSG_LENGTH);
try (DocumentContext dc = appender.writingDocument()) {
Bytes<?> bytes = dc.wire().bytes();
bytes.writeLong(startTimeNS);
bytes.writeInt(bytes.length());
if (MSG_DIRECT)
bytes.write(bytesArr2);
else
bytes.write(bytesArr);
}
try (DocumentContext dc = tailer.readingDocument()) {
if (dc.wire() != null) {
Bytes<?> bytes = dc.wire().bytes();
long start = bytes.readLong();
int length = bytes.readInt();
assert length == MSG_LENGTH;
if (MSG_DIRECT)
bytes.read(bytesArr2.clear(), length);
else
bytes.read(bytesArr);
jlbh.sample(System.nanoTime() - start);
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class InternalAppenderWriteBytesTest method testJumpingAMessageThrowsAIllegalStateException.
@Test(expected = IllegalStateException.class)
public void testJumpingAMessageThrowsAIllegalStateException() {
try (SingleChronicleQueue q = binary(tempDir("q")).rollCycle(MINUTELY).timeProvider(() -> 0).build();
ExcerptAppender appender = q.acquireAppender()) {
appender.writeText("hello");
appender.writeText("hello2");
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().writeLong(1);
}
final long l = appender.lastIndexAppended();
final RollCycle rollCycle = q.rollCycle();
final int currentCycle = rollCycle.toCycle(l);
// try to write to next roll cycle and write at seqnum 1 (but miss the 0th seqnum of that roll cycle)
final long index = rollCycle.toIndex(currentCycle + 1, 1);
((InternalAppender) appender).writeBytes(index, Bytes.from("text"));
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class LastAcknowledgedTest method testLastAcknowledge.
@Test
public void testLastAcknowledge() {
String name = OS.getTarget() + "/testLastAcknowledge-" + Time.uniqueId();
long lastIndexAppended;
try (ChronicleQueue q = SingleChronicleQueueBuilder.single(name).testBlockSize().build()) {
ExcerptAppender excerptAppender = q.acquireAppender();
excerptAppender.writeText("Hello World");
lastIndexAppended = excerptAppender.lastIndexAppended();
ExcerptTailer tailer = q.createTailer();
try (DocumentContext dc = tailer.readingDocument()) {
assertTrue(dc.isData());
assertEquals(lastIndexAppended, tailer.index());
}
ExcerptTailer tailer2 = q.createTailer();
tailer2.readAfterReplicaAcknowledged(true);
try (DocumentContext dc = tailer2.readingDocument()) {
assertFalse(dc.isPresent());
}
}
try (ChronicleQueue q = SingleChronicleQueueBuilder.single(name).testBlockSize().build()) {
assertEquals(-1, q.lastAcknowledgedIndexReplicated());
q.lastAcknowledgedIndexReplicated(lastIndexAppended - 1);
ExcerptTailer tailer2 = q.createTailer();
tailer2.readAfterReplicaAcknowledged(true);
try (DocumentContext dc = tailer2.readingDocument()) {
assertFalse(dc.isPresent());
}
q.lastAcknowledgedIndexReplicated(lastIndexAppended);
try (DocumentContext dc = tailer2.readingDocument()) {
assertTrue(dc.isData());
assertEquals(lastIndexAppended, tailer2.index());
}
}
}
Aggregations