Search in sources :

Example 36 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class RollAtEndOfCycleTest method shouldAppendToExistingQueueFile.

@Test
public void shouldAppendToExistingQueueFile() throws Exception {
    try (final SingleChronicleQueue queue = createQueue()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(1, (w, i) -> {
            w.int32(i);
        });
        final ExcerptTailer tailer = queue.createTailer();
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        assertQueueFileCount(queue.path.toPath(), 1);
        assertFalse(tailer.readingDocument().isPresent());
        appender.writeDocument(2, (w, i) -> {
            w.int32(i);
        });
        assertQueueFileCount(queue.path.toPath(), 1);
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 37 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class RollAtEndOfCycleTest method shouldRollAndAppendToNewFile.

@Test
public void shouldRollAndAppendToNewFile() throws Exception {
    try (final SingleChronicleQueue queue = createQueue()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(1, (w, i) -> {
            w.int32(i);
        });
        final ExcerptTailer tailer = queue.createTailer();
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        assertQueueFileCount(queue.path.toPath(), 1);
        clock.addAndGet(TimeUnit.SECONDS.toMillis(2));
        assertFalse(tailer.readingDocument().isPresent());
        appender.writeDocument(2, (w, i) -> {
            w.int32(i);
        });
        assertQueueFileCount(queue.path.toPath(), 2);
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        final ExcerptTailer newTailer = queue.createTailer();
        int totalCount = 0;
        while (true) {
            final DocumentContext context = newTailer.readingDocument();
            if (context.isPresent() && context.isData()) {
                assertTrue(context.wire().read().int32() != 0);
                totalCount++;
            } else if (!context.isPresent()) {
                break;
            }
        }
        assertThat(totalCount, is(2));
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 38 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer 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;
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) WireParselet(net.openhft.chronicle.wire.WireParselet) VanillaMethodReader(net.openhft.chronicle.wire.VanillaMethodReader) VanillaMessageHistory(net.openhft.chronicle.wire.VanillaMessageHistory) MethodReader(net.openhft.chronicle.bytes.MethodReader) VanillaMethodReader(net.openhft.chronicle.wire.VanillaMethodReader) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 39 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class ChronicleReader method execute.

public void execute() {
    try {
        long lastObservedTailIndex = Long.MAX_VALUE;
        long highestReachedIndex = 0L;
        boolean isFirstIteration = true;
        boolean retryLastOperation = false;
        boolean queueHasBeenModified = false;
        do {
            try (final SingleChronicleQueue queue = createQueue();
                final QueueEntryHandler messageConverter = entryHandlerFactory.get()) {
                final ExcerptTailer tailer = queue.createTailer();
                queueHasBeenModified = false;
                if (highestReachedIndex != 0L) {
                    tailer.moveToIndex(highestReachedIndex);
                }
                final Bytes textConversionTarget = Bytes.elasticByteBuffer();
                try {
                    moveToSpecifiedPosition(queue, tailer, isFirstIteration);
                    lastObservedTailIndex = tailer.index();
                    while (!Thread.currentThread().isInterrupted()) {
                        try (DocumentContext dc = pollMethod.apply(tailer)) {
                            if (!dc.isPresent()) {
                                if (tailInputSource) {
                                    pauser.pause();
                                }
                                break;
                            }
                            pauser.reset();
                            if (customPlugin == null) {
                                messageConverter.accept(dc.wire(), text -> {
                                    applyFiltersAndLog(text, tailer.index());
                                });
                            } else {
                                customPlugin.onReadDocument(dc);
                            }
                        }
                    }
                } finally {
                    textConversionTarget.release();
                    highestReachedIndex = tailer.index();
                    isFirstIteration = false;
                }
                queueHasBeenModified = queueHasBeenModifiedSinceLastCheck(lastObservedTailIndex);
            } catch (final RuntimeException e) {
                if (e.getCause() != null && e.getCause() instanceof DateTimeParseException) {
                    // ignore this error - due to a race condition between
                    // the reader creating a Queue (with default roll-cycle due to no files on disk)
                    // and the writer appending to the Queue with a non-default roll-cycle
                    retryLastOperation = true;
                } else {
                    throw e;
                }
            }
        } while (tailInputSource || retryLastOperation || queueHasBeenModified);
    } catch (Throwable t) {
        t.printStackTrace();
        throw t;
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) DateTimeParseException(java.time.format.DateTimeParseException) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 40 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class TailerPollingEmptyQueueTest method shouldNotGenerateExcessGarbage.

@Test
public void shouldNotGenerateExcessGarbage() throws Exception {
    try (final SingleChronicleQueue queue = createQueue()) {
        queue.path.mkdirs();
        assertThat(queue.path.list((d, n) -> n.endsWith(SingleChronicleQueue.SUFFIX)).length, is(0));
        final ExcerptTailer tailer = queue.createTailer();
        for (int i = 0; i < 50; i++) {
            assertFalse(tailer.readingDocument().isPresent());
        }
        final long startCollectionCount = GcControls.getGcCount();
        for (int i = 0; i < 1_000_000; i++) {
            assertFalse(tailer.readingDocument().isPresent());
        }
        assertThat(GcControls.getGcCount() - startCollectionCount, is(0L));
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Aggregations

ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)45 Test (org.junit.Test)39 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)34 File (java.io.File)24 DocumentContext (net.openhft.chronicle.wire.DocumentContext)22 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)15 MappedFile (net.openhft.chronicle.bytes.MappedFile)11 ArrayList (java.util.ArrayList)5 MethodReader (net.openhft.chronicle.bytes.MethodReader)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 IOException (java.io.IOException)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 Bytes (net.openhft.chronicle.bytes.Bytes)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 Wire (net.openhft.chronicle.wire.Wire)3 ByteBuffer (java.nio.ByteBuffer)2 Future (java.util.concurrent.Future)2