Search in sources :

Example 46 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class RollCycleTest method testWriteToCorruptedFile.

@Test
public void testWriteToCorruptedFile() {
    File dir = DirectoryUtils.tempDir("testWriteToCorruptedFile");
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world");
        }
        Bytes bytes;
        long pos;
        try (DocumentContext dc = appender.writingDocument()) {
            bytes = dc.wire().bytes();
            pos = bytes.writePosition() - 4;
        }
        // write as not complete.
        bytes.writeInt(pos, Wires.NOT_COMPLETE_UNKNOWN_LENGTH);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world 2");
        }
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world 3");
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Test(org.junit.Test)

Example 47 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext 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 48 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class CycleNotFoundTest method tailerCycleNotFoundTest.

// reduced so that it runs quicker for the continuous integration (CI)
@Test(timeout = 50_000L)
@Ignore("TODO FIX")
public void tailerCycleNotFoundTest() throws IOException, InterruptedException, ExecutionException {
    // added nano time just to make
    File path = DirectoryUtils.tempDir("tailerCycleNotFoundTest");
    ExecutorService executorService = Executors.newFixedThreadPool((int) NUMBER_OF_MSG);
    AtomicLong counter = new AtomicLong();
    Runnable reader = () -> {
        long count = 0;
        try (RollingChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).build()) {
            final ExcerptTailer tailer = rqueue.createTailer();
            long last = -1;
            while (count < NUMBER_OF_MSG) {
                try (DocumentContext dc = tailer.readingDocument()) {
                    if (!dc.isPresent())
                        continue;
                    Assert.assertTrue(dc.isData());
                    Assert.assertEquals(last + 1, last = dc.wire().read().int64());
                    count++;
                    counter.incrementAndGet();
                }
                if (executorService.isShutdown())
                    Assert.fail();
            }
            // check nothing after the NUMBER_OF_MSG
            try (DocumentContext dc = tailer.readingDocument()) {
                Assert.assertFalse(dc.isPresent());
            }
        } finally {
            System.out.printf("Read %,d messages, thread=" + Thread.currentThread().getName() + "\n", count);
        }
    };
    List<Future> tailers = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_TAILERS; i++) {
        tailers.add(executorService.submit(reader));
    }
    // Appender run in a different thread
    ExecutorService executorService1 = Executors.newSingleThreadExecutor();
    Future<?> submit = executorService1.submit(() -> {
        ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(TEST_SECONDLY).build();
        final ExcerptAppender appender = wqueue.acquireAppender();
        long next = System.nanoTime() + INTERVAL_US * 1000;
        for (int i = 0; i < NUMBER_OF_MSG; i++) {
            while (System.nanoTime() < next) /* busy wait*/
            ;
            try (DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().int64(i);
            }
            next += INTERVAL_US * 1000;
            if (executorService1.isShutdown())
                return;
        }
        wqueue.close();
    });
    submit.get();
    System.out.println("appender is done.");
    // wait for all the tailer to finish
    for (Future f : tailers) {
        f.get();
    }
    assertEquals(NUMBER_OF_MSG * NUMBER_OF_TAILERS, counter.get());
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) ArrayList(java.util.ArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 49 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class LastIndexAppendedTest method testLastIndexAppendedAcrossRestarts.

@Test
public void testLastIndexAppendedAcrossRestarts() throws Exception {
    String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    for (int i = 0; i < 5; i++) {
        try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            try (DocumentContext documentContext = appender.writingDocument()) {
                int index = (int) documentContext.index();
                assertEquals(i, index);
                documentContext.wire().write().text("hello world");
            }
            assertEquals(i, (int) appender.lastIndexAppended());
        }
    }
    try {
        IOTools.deleteDirWithFiles(path, 2);
    } catch (Exception index) {
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 50 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class TailerIndexingQueueTest method tailerShouldBeAbleToMoveBackwardFromEndOfCycle.

@Test
public void tailerShouldBeAbleToMoveBackwardFromEndOfCycle() throws Exception {
    assumeFalse(OS.isWindows());
    try (final SingleChronicleQueue queue = createQueue(path, clock::get)) {
        final ExcerptAppender appender = queue.acquireAppender();
        // generate some cycle files
        range(0, 5).forEach(i -> {
            try (final DocumentContext ctx = appender.writingDocument()) {
                ctx.wire().write().int32(i);
                clock.addAndGet(TimeUnit.SECONDS.toMillis(10L));
            }
        });
    }
    // remove all but the first file
    final Path firstFile = Files.list(this.path.toPath()).sorted(Comparator.comparing(Path::toString)).findFirst().orElseThrow(AssertionError::new);
    Files.list(this.path.toPath()).filter(p -> !p.equals(firstFile)).forEach(TailerIndexingQueueTest::deleteFile);
    try (final SingleChronicleQueue queue = createQueue(path, SystemTimeProvider.INSTANCE)) {
        final ExcerptTailer tailer = queue.createTailer().toEnd();
        // move to END_OF_CYCLE
        try (final DocumentContext readCtx = tailer.readingDocument()) {
            assertThat(readCtx.isPresent(), is(false));
        }
        assertThat(tailer.state(), is(TailerState.END_OF_CYCLE));
        tailer.direction(TailerDirection.BACKWARD);
        tailer.toEnd();
        assertThat(tailer.readingDocument().isPresent(), is(true));
    }
}
Also used : Path(java.nio.file.Path) CoreMatchers.is(org.hamcrest.CoreMatchers.is) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Files(java.nio.file.Files) IntStream.range(java.util.stream.IntStream.range) Assume.assumeFalse(org.junit.Assume.assumeFalse) Test(org.junit.Test) IOException(java.io.IOException) WireType(net.openhft.chronicle.wire.WireType) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) AtomicLong(java.util.concurrent.atomic.AtomicLong) net.openhft.chronicle.queue(net.openhft.chronicle.queue) After(org.junit.After) TimeProvider(net.openhft.chronicle.core.time.TimeProvider) OS(net.openhft.chronicle.core.OS) SystemTimeProvider(net.openhft.chronicle.core.time.SystemTimeProvider) Comparator(java.util.Comparator) Path(java.nio.file.Path) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)54 Test (org.junit.Test)41 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)28 File (java.io.File)22 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)22 MappedFile (net.openhft.chronicle.bytes.MappedFile)12 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)9 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)8 Wire (net.openhft.chronicle.wire.Wire)8 NotNull (org.jetbrains.annotations.NotNull)6 Ignore (org.junit.Ignore)6 Future (java.util.concurrent.Future)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Bytes (net.openhft.chronicle.bytes.Bytes)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)3