use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class ChronicleHistoryReaderTest method checkWithQueueHistoryRecordHistoryInitial.
private void checkWithQueueHistoryRecordHistoryInitial(Class<? extends DummyListener> dummyClass) {
expectException("Overriding sourceId from existing metadata, was 0, overriding to");
MessageHistory.set(null);
int extraTiming = 1;
File queuePath1 = IOTools.createTempFile("testWithQueueHistory1-");
File queuePath2 = IOTools.createTempFile("testWithQueueHistory2-");
File queuePath3 = IOTools.createTempFile("testWithQueueHistory3-");
try {
try (ChronicleQueue out = queue(queuePath1, 1)) {
DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).useMethodIds(true).get();
// this will write the 1st timestamps
writer.say("hello");
}
try (ChronicleQueue in = queue(queuePath1, 1);
ChronicleQueue out = queue(queuePath2, 2)) {
DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).get();
final AtomicInteger numberRead = new AtomicInteger();
// if this listener is a DummyListener then messages with methodId won't be routed to it
DummyListenerId dummy = msg -> {
numberRead.incrementAndGet();
MessageHistory history = MessageHistory.get();
Assert.assertEquals(1, history.sources());
// written 1st then received by me
Assert.assertEquals(1 + extraTiming, history.timings());
// this writes 2 more timestamps
writer.say(msg);
};
MethodReader reader = in.createTailer().methodReader(dummy);
assertTrue(reader.readOne());
assertEquals("check routed to correct dest", 1, numberRead.get());
assertFalse(reader.readOne());
}
try (ChronicleQueue in = queue(queuePath2, 2);
ChronicleQueue out = queue(queuePath3, 3)) {
DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).get();
final AtomicInteger numberRead = new AtomicInteger();
DummyListenerId dummy = msg -> {
numberRead.incrementAndGet();
MessageHistory history = MessageHistory.get();
Assert.assertEquals(2, history.sources());
Assert.assertEquals(3 + extraTiming, history.timings());
// this writes 2 more timestamps
writer.say(msg);
};
MethodReader reader = in.createTailer().methodReader(dummy);
assertTrue(reader.readOne());
assertEquals("check routed to correct dest", 1, numberRead.get());
assertFalse(reader.readOne());
}
ChronicleHistoryReader chronicleHistoryReader = new ChronicleHistoryReader().withBasePath(queuePath3.toPath()).withTimeUnit(TimeUnit.MICROSECONDS).withMessageSink(System.out::println);
Map<String, Histogram> histos = chronicleHistoryReader.readChronicle();
chronicleHistoryReader.outputData();
Assert.assertEquals(5, histos.size());
Assert.assertEquals("[1, startTo1, 2, 1to2, endToEnd]", histos.keySet().toString());
} finally {
IOTools.deleteDirWithFiles(queuePath1.toString(), queuePath2.toString(), queuePath3.toString());
}
}
use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class Queue36Test method testTail.
@Test
public void testTail() {
File basePath = getTmpDir();
try (ChronicleQueue queue = ChronicleQueue.singleBuilder(basePath).testBlockSize().build()) {
checkNoFiles(basePath);
ExcerptTailer tailer = queue.createTailer();
checkNoFiles(basePath);
tailer.toStart();
checkNoFiles(basePath);
assertFalse(tailer.readDocument(d -> {
}));
checkNoFiles(basePath);
}
}
use of net.openhft.chronicle.queue.ChronicleQueue 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.queue.ChronicleQueue 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.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.
the class QueueReadJitterMain method run.
protected void run() {
MappedFile.warmup();
String path = "test-q-" + Time.uniqueId();
// System.out.println("Writing to " + path);
AtomicLong lastRead = new AtomicLong();
Thread reader = new Thread(() -> {
try (ChronicleQueue q = createQueue(path)) {
ExcerptTailer tailer = q.createTailer().toEnd();
long time = System.currentTimeMillis();
while (running) {
Jvm.safepoint();
if (!tailer.peekDocument()) {
long now = System.currentTimeMillis();
if (now != time)
time = now;
else
continue;
}
try (DocumentContext dc = tailer.readingDocument(false)) {
if (!dc.isPresent()) {
Jvm.safepoint();
continue;
}
Jvm.safepoint();
Bytes<?> bytes = dc.wire().bytes();
long count = bytes.readLong();
while (bytes.readRemaining() > 7) bytes.readLong();
lastRead.set(count);
Jvm.safepoint();
}
Jvm.safepoint();
}
}
});
reader.setDaemon(true);
reader.start();
// give it time to start
Jvm.pause(100);
long count = 0;
try (ChronicleQueue q = createQueue(path)) {
ExcerptAppender appender = q.acquireAppender();
long start0 = System.currentTimeMillis();
do {
try (DocumentContext dc = appender.writingDocument()) {
Bytes<?> bytes = dc.wire().bytes();
bytes.writeLong(++count);
for (int i = 8; i < size; i += 8) bytes.writeLong(0);
}
long start1 = System.nanoTime();
do {
Thread.yield();
} while (System.nanoTime() < start1 + sampleTime * 1000);
long time1 = System.nanoTime() - start1;
if (time1 > sampleTime * 1000 * 10) {
System.out.println("Time paused = " + time1 / 1000 + " us");
}
if (lastRead.get() != count) {
StackTraceElement[] stes = reader.getStackTrace();
StringBuilder sb = new StringBuilder();
sb.append(PROFILE_OF_THE_THREAD);
Jvm.trimStackTrace(sb, stes);
System.out.println(sb);
}
} while (System.currentTimeMillis() < start0 + runTime * 1_000);
}
running = false;
IOTools.deleteDirWithFiles(path, 2);
}
Aggregations