use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.
the class MessageHistoryTest method shouldAccessMessageHistoryWhenTailerIsMovedToEnd.
@Test
public void shouldAccessMessageHistoryWhenTailerIsMovedToEnd() throws Exception {
try (final SingleChronicleQueue inputQueue = createQueue(inputQueueDir, 1);
final SingleChronicleQueue outputQueue = createQueue(outputQueueDir, 2)) {
generateTestData(inputQueue, outputQueue);
final ExcerptTailer tailer = outputQueue.createTailer();
tailer.direction(TailerDirection.BACKWARD).toEnd();
final ValidatingSecond validatingSecond = new ValidatingSecond();
final MethodReader validator = tailer.methodReader(validatingSecond);
assertThat(validator.readOne(), is(true));
assertThat(validatingSecond.messageHistoryPresent(), is(true));
}
}
use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.
the class NotCompleteTest method testInterruptedDuringSerialisation.
@Test
public void testInterruptedDuringSerialisation() throws InterruptedException {
final File tmpDir = DirectoryUtils.tempDir("testInterruptedDuringSerialisation_" + (lazyIndexing ? "lazy" : "eager"));
DirectoryUtils.deleteDir(tmpDir);
tmpDir.mkdirs();
final List<String> names = Collections.synchronizedList(new ArrayList<>());
final Person person1 = new Person(40, "Terry");
final Person interrupter = new Person(50, Person.INTERRUPT);
final Person thrower = new Person(80, Person.THROW);
final Person person2 = new Person(90, "Bert");
try (final ChronicleQueue queueReader = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).timeoutMS(500).build();
final ChronicleQueue queueWriter = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
ExcerptTailer tailer = queueReader.createTailer();
MethodReader reader = tailer.methodReader((PersonListener) person -> names.add(person.name));
final StringBuilder queueDumpBeforeInterruptedWrite = new StringBuilder();
// set up
doWrite(queueWriter, (proxy, queue) -> {
proxy.accept(person1);
queueDumpBeforeInterruptedWrite.append(queue.dump());
});
String cleanedQueueDump = cleanQueueDump(queueDumpBeforeInterruptedWrite.toString());
// start up writer thread
Thread writerThread = new Thread(() -> doWrite(queueWriter, (proxy, queue) -> {
// thread is interrupted during this
proxy.accept(interrupter);
}));
writerThread.start();
writerThread.join();
try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
String dump = cleanQueueDump(queue.dump());
assertEquals("queue should be unchanged by the interrupted write", cleanedQueueDump, dump);
}
// check only 1 written
assertTrue(reader.readOne());
assertEquals(1, names.size());
assertEquals(person1.name, names.get(0));
assertFalse(reader.readOne());
// do a write that throws an exception
doWrite(queueWriter, (proxy, queue) -> {
try {
proxy.accept(thrower);
} catch (NullPointerException npe) {
// ignore
}
});
try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
String dump = cleanQueueDump(queue.dump());
if (lazyIndexing) {
// reading the queue creates the index, thus changing it, so do a text comparison here
cleanedQueueDump = "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY_LIGHT,\n" + " writePosition: [\n" + " 442,\n" + " 0\n" + " ],\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: 8,\n" + " indexSpacing: 1,\n" + " index2Index: 475,\n" + " lastIndex: 0\n" + " },\n" + " lastAcknowledgedIndexReplicated: -1,\n" + " recovery: !TimedStoreRecovery {\n" + " timeStamp: 0\n" + " },\n" + " deltaCheckpointInterval: 0,\n" + " lastIndexReplicated: -1,\n" + " sourceId: 0\n" + "}\n" + "# position: 442, header: 0\n" + "--- !!data #binary\n" + "accept: {\n" + " age: 40,\n" + " name: Terry\n" + "}\n" + "# position: 475, header: 0\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + " # length: 8, used: 0\n" + " 0, 0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "...\n" + "\n";
}
assertEquals("queue should be unchanged by the failed write", cleanedQueueDump, dump);
}
// check nothing else written
assertFalse(reader.readOne());
// do an empty write
ExcerptAppender appender = queueWriter.acquireAppender().lazyIndexing(lazyIndexing);
DocumentContext wd = appender.writingDocument();
wd.rollbackOnClose();
wd.close();
// check queue unchanged
String dump = cleanQueueDump(queueWriter.dump());
assertEquals("queue should be unchanged by the failed write", cleanedQueueDump, dump);
// check nothing else written
assertFalse(reader.readOne());
// write another person to same queue in this thread
doWrite(queueWriter, (proxy, queue) -> proxy.accept(person2));
assertTrue(reader.readOne());
assertEquals(2, names.size());
assertEquals(person2.name, names.get(1));
assertFalse(reader.readOne());
}
}
use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.
the class ChronicleHistoryReader method readChronicle.
public Map<String, Histogram> readChronicle() {
final SingleChronicleQueue q = createQueue();
final ExcerptTailer tailer = q.createTailer();
final WireParselet parselet = parselet();
final MethodReader mr = new VanillaMethodReader(tailer, true, parselet, null, parselet);
MessageHistory.set(new VanillaMessageHistory());
while (!Thread.currentThread().isInterrupted() && mr.readOne()) {
++counter;
if (this.progress && counter % 1_000_000L == 0) {
System.out.println("Progress: " + counter);
}
}
return histos;
}
use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.
the class LastAppendedTest method testLastWritten.
@Test
public void testLastWritten() {
SetTimeProvider timeProvider = new SetTimeProvider();
try (SingleChronicleQueue outQueue = single(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).sourceId(1).timeProvider(timeProvider).build()) {
try (SingleChronicleQueue inQueue = single(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).sourceId(2).timeProvider(timeProvider).build()) {
// write some initial data to the inqueue
final Msg msg = inQueue.acquireAppender().methodWriterBuilder(Msg.class).recordHistory(true).build();
msg.msg("somedata-0");
timeProvider.advanceMillis(1000);
// write data into the inQueue
msg.msg("somedata-1");
// read a message on the in queue and write it to the out queue
{
Msg out = outQueue.acquireAppender().methodWriterBuilder(Msg.class).recordHistory(true).build();
MethodReader methodReader = inQueue.createTailer().methodReader((Msg) out::msg);
// reads the somedata-0
methodReader.readOne();
// reads the somedata-1
methodReader.readOne();
}
// write data into the inQueue
msg.msg("somedata-2");
timeProvider.advanceMillis(2000);
msg.msg("somedata-3");
msg.msg("somedata-4");
System.out.println(inQueue.dump());
AtomicReference<String> actualValue = new AtomicReference<>();
// check that we are able to pick up from where we left off, in other words the next read should be somedata-2
{
ExcerptTailer excerptTailer = inQueue.createTailer().afterLastWritten(outQueue);
MethodReader methodReader = excerptTailer.methodReader((Msg) actualValue::set);
methodReader.readOne();
Assert.assertEquals("somedata-2", actualValue.get());
methodReader.readOne();
Assert.assertEquals("somedata-3", actualValue.get());
methodReader.readOne();
Assert.assertEquals("somedata-4", actualValue.get());
}
}
}
}
use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.
the class MessageReaderWriterTest method testWriteWhileReading.
@Test
public void testWriteWhileReading() {
ClassAliasPool.CLASS_ALIASES.addAlias(Message1.class);
ClassAliasPool.CLASS_ALIASES.addAlias(Message2.class);
File path1 = DirectoryUtils.tempDir("testWriteWhileReading1");
File path2 = DirectoryUtils.tempDir("testWriteWhileReading2");
try (SingleChronicleQueue queue1 = SingleChronicleQueueBuilder.binary(path1).testBlockSize().build();
SingleChronicleQueue queue2 = SingleChronicleQueueBuilder.binary(path2).testBlockSize().build()) {
MethodReader reader2 = queue1.createTailer().methodReader(ObjectUtils.printAll(MessageListener.class));
MessageListener writer2 = queue2.acquireAppender().methodWriter(MessageListener.class);
MessageListener processor = new MessageProcessor(writer2);
MethodReader reader1 = queue1.createTailer().methodReader(processor);
MessageListener writer1 = queue1.acquireAppender().methodWriter(MessageListener.class);
for (int i = 0; i < 3; i++) {
// write a message
writer1.method1(new Message1("hello"));
writer1.method2(new Message2(234));
// read those messages
assertTrue(reader1.readOne());
assertTrue(reader1.readOne());
assertFalse(reader1.readOne());
// read the produced messages
assertTrue(reader2.readOne());
assertTrue(reader2.readOne());
assertFalse(reader2.readOne());
}
// System.out.println(queue1.dump());
}
}
Aggregations