Search in sources :

Example 31 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class ChronicleReaderTest method readOnlyQueueTailerInFollowModeShouldObserveChangesAfterInitiallyObservedReadLimit.

@Test
@Ignore("TODO FIX")
public void readOnlyQueueTailerInFollowModeShouldObserveChangesAfterInitiallyObservedReadLimit() throws Exception {
    DirectoryUtils.deleteDir(dataDir.toFile());
    dataDir.toFile().mkdirs();
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dataDir).testBlockSize().build()) {
        final StringEvents events = queue.acquireAppender().methodWriterBuilder(StringEvents.class).build();
        events.say("hello");
        final long readerCapacity = getCurrentQueueFileLength(dataDir);
        final AtomicReference<String> messageReceiver = new AtomicReference<>();
        final ChronicleReader chronicleReader = basicReader().tail().withMessageSink(messageReceiver::set);
        final ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<?> submit = executorService.submit(chronicleReader::execute);
        final long expectedReadingDocumentCount = (readerCapacity / ONE_KILOBYTE.length) + 1;
        int i;
        for (i = 0; i < expectedReadingDocumentCount; i++) {
            events.say(new String(ONE_KILOBYTE));
        }
        events.say(LAST_MESSAGE);
        while (!(messageReceiver.get() != null && messageReceiver.get().contains(LAST_MESSAGE))) {
            LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1000L));
        }
        executorService.shutdownNow();
        executorService.awaitTermination(5L, TimeUnit.SECONDS);
        submit.get();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 32 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class ChronicleReaderTest method shouldReadQueueWithDifferentRollCycleWhenCreatedAfterReader.

@Test(timeout = 10_000L)
public void shouldReadQueueWithDifferentRollCycleWhenCreatedAfterReader() throws IOException, InterruptedException {
    Path path = DirectoryUtils.tempDir("shouldReadQueueWithDifferentRollCycleWhenCreatedAfterReader").toPath();
    path.toFile().mkdirs();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicLong recordsProcessed = new AtomicLong(0);
    final ChronicleReader reader = new ChronicleReader().withBasePath(path).withMessageSink(m -> {
        latch.countDown();
        recordsProcessed.incrementAndGet();
    });
    final AtomicReference<Throwable> readerException = new AtomicReference<>();
    final CountDownLatch executeLatch = new CountDownLatch(1);
    final Thread readerThread = new Thread(() -> {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                reader.execute();
                executeLatch.countDown();
            } catch (Throwable t) {
                readerException.set(t);
                throw t;
            }
        }
    });
    readerThread.start();
    assertTrue(executeLatch.await(5, TimeUnit.SECONDS));
    assertTrue(capturedOutput.isEmpty());
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.MINUTELY).build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final MethodWriterBuilder<StringEvents> methodWriterBuilder = excerptAppender.methodWriterBuilder(StringEvents.class);
        methodWriterBuilder.recordHistory(true);
        final StringEvents events = methodWriterBuilder.build();
        for (int i = 0; i < 24; i++) {
            events.say(i % 2 == 0 ? "hello" : "goodbye");
        }
    }
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    while (recordsProcessed.get() < 10) {
        LockSupport.parkNanos(1L);
    }
    readerThread.interrupt();
    assertThat(readerException.get(), is(nullValue()));
}
Also used : Path(java.nio.file.Path) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) Test(org.junit.Test)

Example 33 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class RollEOFTest method createQueueAndWriteData.

private void createQueueAndWriteData(MutableTimeProvider timeProvider) {
    final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_DAILY).timeProvider(timeProvider).build();
    ExcerptAppender excerptAppender = queue.acquireAppender();
    try (DocumentContext dc = excerptAppender.writingDocument(false)) {
        dc.wire().write(() -> "test").int64(0);
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 34 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class PublishDeltaGenerator method main.

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        throw new IllegalArgumentException("Usage: <program> [resource-name]");
    }
    Jvm.setExceptionHandlers((c, m, t) -> {
        System.out.println(m);
    }, (c, m, t) -> {
        System.out.println(m);
        t.printStackTrace();
    }, (c, m, t) -> System.out.println(m));
    final ConfigParser configParser = new ConfigParser(args[0]);
    final List<StageConfig> allStageConfigs = configParser.getAllStageConfigs();
    final StageConfig lastStageConfig = allStageConfigs.get(allStageConfigs.size() - 1);
    try (final SingleChronicleQueue pubQueue = SingleChronicleQueueBuilder.binary(configParser.getPublisherConfig().outputDir()).build();
        final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(lastStageConfig.getOutputPath()).build();
        final Writer resultsWriter = new FileWriter("publish-deltas.txt", false);
        final Writer s0Pub = new FileWriter("s0-deltas.txt", false);
        final Writer s1Pub = new FileWriter("s1-deltas.txt", false);
        final Writer s0s2Pub = new FileWriter("s0-s2-deltas.txt", false);
        final Writer s1s2Pub = new FileWriter("s1-s2-deltas.txt", false)) {
        final MethodReader reader = pubQueue.createTailer().methodReader(new CapturingReceiver(resultsWriter, m -> m.publishNanos));
        while (reader.readOne()) {
        // report
        }
        final MethodReader methodReader = queue.createTailer().methodReader(new DelegatingReceiver(new CapturingReceiver(s0Pub, m -> m.t0), new CapturingReceiver(s1Pub, m -> m.t1), new CapturingReceiver(s0s2Pub, m -> m.t2, 5), new CapturingReceiver(s1s2Pub, m -> m.t2, 6)));
        while (methodReader.readOne()) {
        // report
        }
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) FileWriter(java.io.FileWriter) LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) EightyByteMessage(net.openhft.load.messages.EightyByteMessage) Jvm(net.openhft.chronicle.core.Jvm) TimeUnit(java.util.concurrent.TimeUnit) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) StageConfig(net.openhft.load.config.StageConfig) DateTimeFormatter(java.time.format.DateTimeFormatter) Writer(java.io.Writer) ZoneOffset(java.time.ZoneOffset) ToLongFunction(java.util.function.ToLongFunction) ConfigParser(net.openhft.load.config.ConfigParser) FileWriter(java.io.FileWriter) ConfigParser(net.openhft.load.config.ConfigParser) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) MethodReader(net.openhft.chronicle.bytes.MethodReader) StageConfig(net.openhft.load.config.StageConfig) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 35 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class PublisherMain method createOutput.

private static MethodDefinition createOutput(final Path path) {
    final SingleChronicleQueue queue = outputQueue(path);
    final ExcerptAppender appender = queue.acquireAppender();
    return new GarbageFreeMethodPublisher(() -> appender);
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)

Aggregations

SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)36 Test (org.junit.Test)24 File (java.io.File)13 MethodReader (net.openhft.chronicle.bytes.MethodReader)10 DocumentContext (net.openhft.chronicle.wire.DocumentContext)10 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)7 Ignore (org.junit.Ignore)6 OS (net.openhft.chronicle.core.OS)4 IOTools (net.openhft.chronicle.core.io.IOTools)4 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 Path (java.nio.file.Path)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 NotNull (org.jetbrains.annotations.NotNull)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2