Search in sources :

Example 91 with DocumentContext

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

the class LastIndexAppendedTest method testLastIndexAppendedAcrossRestarts.

@Test
public void testLastIndexAppendedAcrossRestarts() {
    String path = OS.getTarget() + "/" + getClass().getSimpleName() + "-" + Time.uniqueId();
    for (int i = 0; i < 5; i++) {
        try (ChronicleQueue queue = 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 : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 92 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)
public void tailerCycleNotFoundTest() throws InterruptedException, ExecutionException {
    // added nano time just to make
    File path = getTmpDir();
    ExecutorService executorService = Executors.newFixedThreadPool((int) NUMBER_OF_MSG, new NamedThreadFactory("tailerCycleNotFoundTest"));
    AtomicLong counter = new AtomicLong();
    Runnable reader = () -> {
        long count = 0;
        try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.binary(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(new NamedThreadFactory("appender"));
    Future<?> submit = executorService1.submit(() -> {
        ChronicleQueue wqueue = SingleChronicleQueueBuilder.binary(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();
    // wait for all the tailer to finish
    for (Future f : tailers) {
        f.get();
    }
    executorService.shutdownNow();
    executorService1.shutdownNow();
    executorService.awaitTermination(5, TimeUnit.SECONDS);
    executorService1.awaitTermination(5, TimeUnit.SECONDS);
    assertEquals(NUMBER_OF_MSG * NUMBER_OF_TAILERS, counter.get());
}
Also used : NamedThreadFactory(net.openhft.chronicle.threads.NamedThreadFactory) ArrayList(java.util.ArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 93 with DocumentContext

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

the class ChronicleQueueIndexTest method readKeyed.

@NotNull
private List<String> readKeyed(ChronicleQueue queue, boolean includeMetaData) {
    try (ExcerptTailer tailer = queue.createTailer()) {
        List<String> allReads = new ArrayList<>();
        for (; ; ) {
            try (DocumentContext dc = tailer.readingDocument(includeMetaData)) {
                if (!dc.isPresent())
                    return allReads;
                final Wire wire = dc.wire();
                final String key = wire.readEvent(String.class);
                if (!key.equals("key"))
                    continue;
                String str = wire.getValueIn().text();
                allReads.add(str);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) NotNull(org.jetbrains.annotations.NotNull)

Example 94 with DocumentContext

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

the class ChronicleQueueIndexTest method writeReadMetadata.

// https://github.com/OpenHFT/Chronicle-Queue/issues/822
@Test
public void writeReadMetadata() {
    try (final ChronicleQueue queue = ChronicleQueue.singleBuilder(getTmpDir()).rollCycle(RollCycles.TEST4_SECONDLY).testBlockSize().build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        final ExcerptTailer tailer = queue.createTailer();
        boolean metadata = true;
        try (DocumentContext dc = appender.writingDocument(metadata)) {
            dc.wire().write("a").text("hello");
        }
        try (DocumentContext dc = tailer.readingDocument(metadata)) {
            Assert.assertTrue(dc.isPresent());
        }
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 95 with DocumentContext

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

the class Stackoveflow52274284Test method fails.

@Test
public void fails() throws IOException {
    String basePath = OS.getTarget();
    String path = Files.createTempDirectory(Paths.get(basePath), "chronicle-").toAbsolutePath().toString();
    try (ChronicleQueue chronicleQueue = ChronicleQueue.singleBuilder(path).testBlockSize().build()) {
        // Create Appender
        ExcerptAppender appender = chronicleQueue.acquireAppender();
        // Create Tailer
        ExcerptTailer tailer = chronicleQueue.createTailer();
        tailer.toStart();
        int numberOfRecords = 10;
        // Write
        for (int i = 0; i <= numberOfRecords; i++) {
            // System.out.println("Writing " + i);
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write("msg").text("Hello World!");
            // System.out.println("your data was store to index=" + dc.index());
            } catch (Exception e) {
                System.err.println("Unable to store value to chronicle");
                e.printStackTrace();
            }
        }
        // Read
        for (int i = 0; i <= numberOfRecords; i++) {
            // System.out.println("Reading " + i);
            try (DocumentContext documentContext = tailer.readingDocument()) {
                long currentOffset = documentContext.index();
                // System.out.println("Current offset: " + currentOffset);
                Wire wire = documentContext.wire();
                if (wire != null) {
                    String msg = wire.read("msg").text();
                // System.out.println(msg);
                }
            }
        }
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)127 Test (org.junit.Test)76 File (java.io.File)46 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)32 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)27 Wire (net.openhft.chronicle.wire.Wire)23 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)22 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)13 NotNull (org.jetbrains.annotations.NotNull)12 Bytes (net.openhft.chronicle.bytes.Bytes)11 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)11 MappedFile (net.openhft.chronicle.bytes.MappedFile)10 Ignore (org.junit.Ignore)7 ArrayList (java.util.ArrayList)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ValueOut (net.openhft.chronicle.wire.ValueOut)6 Histogram (net.openhft.chronicle.core.util.Histogram)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 NamedThreadFactory (net.openhft.chronicle.threads.NamedThreadFactory)5 IOException (java.io.IOException)4