Search in sources :

Example 76 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project kie-wb-common by kiegroup.

the class ClientIPCImpl method readThisDocument.

private DefaultKieCompilationResponseOffProcess readThisDocument(ExcerptTailer tailer) {
    if (logger.isDebugEnabled()) {
        logger.debug("current index on readThisDocument:{}", tailer.index());
    }
    DefaultKieCompilationResponseOffProcess res = null;
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Document Context index:{}", dc.index());
            }
            Wire wire = dc.wire();
            Bytes bytes = wire.bytes();
            if (!bytes.isEmpty()) {
                try {
                    Object obj = deserialize(bytes.toByteArray());
                    res = (DefaultKieCompilationResponseOffProcess) obj;
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }
    if (res == null) {
        res = new DefaultKieCompilationResponseOffProcess(false, "");
    }
    return res;
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) DefaultKieCompilationResponseOffProcess(org.kie.workbench.common.services.backend.compiler.impl.DefaultKieCompilationResponseOffProcess) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) IOException(java.io.IOException)

Example 77 with DocumentContext

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

the class StuckQueueTest method test.

@Test
public void test() throws IOException {
    // todo remove see https://github.com/OpenHFT/Chronicle-Queue/issues/837
    assumeFalse(Jvm.isMacArm());
    // java.nio.file.InvalidPathException: Illegal char <:> at index 2: /D:/BuildAgent/work/1e5875c1db7235db/target/test-classes/stuck.queue.test/20180508-1249.cq4
    assumeFalse(OS.isWindows());
    Path tmpDir = getTmpDir().toPath();
    expectException("Failback to readonly tablestore");
    expectException("reading control code as text");
    // expectException("Unable to copy TimedStoreRecovery safely will try anyway");
    // expectException("Unable to copy SCQStore safely will try anyway");
    // expectException("Unable to copy SCQSRoll safely");
    // expectException("Unable to copy SCQSIndexing safely");
    tmpDir.toFile().mkdirs();
    Path templatePath = Paths.get(StuckQueueTest.class.getResource("/stuck.queue.test/20180508-1249.cq4").getFile());
    Path to = tmpDir.resolve(templatePath.getFileName());
    Files.copy(templatePath, to, StandardCopyOption.REPLACE_EXISTING);
    try (RollingChronicleQueue q = ChronicleQueue.singleBuilder(tmpDir).rollCycle(RollCycles.MINUTELY).readOnly(true).build();
        ExcerptTailer tailer = q.createTailer()) {
        // System.out.println(q.dump());
        int cycle = q.rollCycle().toCycle(0x18406e100000000L);
        try (SingleChronicleQueueStore wireStore = q.storeForCycle(cycle, q.epoch(), false, null)) {
            String absolutePath = wireStore.file().getAbsolutePath();
            // System.out.println(absolutePath);
            Assert.assertTrue(absolutePath.endsWith("20180508-1249.cq4"));
        }
        try (DocumentContext dc = tailer.readingDocument()) {
        // Assert.assertTrue(!dc.isPresent());
        // System.out.println(Long.toHexString(dc.index()));
        }
        // Assert.assertTrue(tailer.moveToIndex(0x183efe300000000L));
        try (final SingleChronicleQueue q2 = ChronicleQueue.singleBuilder(tmpDir).rollCycle(RollCycles.MINUTELY).build()) {
            try (DocumentContext dc = q2.acquireAppender().writingDocument()) {
                dc.wire().write("hello").text("world");
            }
        }
        ExcerptTailer tailer2 = q.createTailer();
        try (DocumentContext dc = tailer2.readingDocument()) {
            Assert.assertTrue(dc.isPresent());
            String actual = dc.wire().read("hello").text();
            Assert.assertEquals("world", actual);
        // System.out.println(Long.toHexString(dc.index()));
        }
    }
}
Also used : Path(java.nio.file.Path) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 78 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 IOException {
    assumeFalse(OS.isWindows());
    try (final ChronicleQueue 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
    try (Stream<Path> list = Files.list(this.path.toPath());
        Stream<Path> list2 = Files.list(this.path.toPath())) {
        final Path firstFile = list.sorted(Comparator.comparing(Path::toString)).findFirst().orElseThrow(AssertionError::new);
        list2.filter(p -> !p.equals(firstFile)).forEach(TailerIndexingQueueTest::deleteFile);
        try (final ChronicleQueue queue = createQueue(path, SystemTimeProvider.INSTANCE)) {
            final ExcerptTailer tailer = queue.createTailer().toEnd();
            // move to END_OF_CYCLE
            try (final DocumentContext readCtx = tailer.readingDocument()) {
                assertFalse(readCtx.isPresent());
            }
            assertEquals(TailerState.END_OF_CYCLE, tailer.state());
            tailer.direction(TailerDirection.BACKWARD);
            tailer.toEnd();
            assertTrue(tailer.readingDocument().isPresent());
        }
    }
}
Also used : Path(java.nio.file.Path) 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) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) net.openhft.chronicle.queue(net.openhft.chronicle.queue) TimeProvider(net.openhft.chronicle.core.time.TimeProvider) OS(net.openhft.chronicle.core.OS) SystemTimeProvider(net.openhft.chronicle.core.time.SystemTimeProvider) Comparator(java.util.Comparator) Assert(org.junit.Assert) Path(java.nio.file.Path) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 79 with DocumentContext

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

the class ToEndTest method missingCyclesToEndTest.

@Test
public void missingCyclesToEndTest() {
    String path = OS.getTarget() + "/missingCyclesToEndTest-" + Time.uniqueId();
    IOTools.shallowDeleteDirWithFiles(path);
    final SetTimeProvider timeProvider = new SetTimeProvider();
    long now = 1470757797000L;
    long timeIncMs = 1001;
    timeProvider.currentTimeMillis(now);
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST4_SECONDLY).timeProvider(timeProvider).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(wire -> wire.write("msg").int32(1));
        // roll
        timeProvider.currentTimeMillis(now += timeIncMs);
        appender.writeDocument(wire -> wire.write("msg").int32(2));
        appender.writeDocument(wire -> wire.write("msg").int32(3));
        final ExcerptTailer tailer = queue.createTailer().toEnd();
        try (DocumentContext dc = tailer.readingDocument()) {
            if (dc.isPresent()) {
                fail("Should be at the end of the queue but dc.isPresent and we read: " + dc.wire().read("msg").int32());
            }
        }
        // append same cycle.
        appender.writeDocument(wire -> wire.write("msg").int32(4));
        try (DocumentContext dc = tailer.readingDocument()) {
            assertTrue("Should be able to read entry in this cycle. Got NoDocumentContext.", dc.isPresent());
            int i = dc.wire().read("msg").int32();
            assertEquals("Should've read 4, instead we read: " + i, 4, i);
        }
        // read from the beginning
        tailer.toStart();
        for (int j = 1; j <= 4; j++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                assertTrue(dc.isPresent());
                int i = dc.wire().read("msg").int32();
                assertEquals(j, i);
            }
        }
        try (DocumentContext dc = tailer.readingDocument()) {
            if (dc.isPresent()) {
                fail("Should be at the end of the queue but dc.isPresent and we read: " + String.valueOf(dc.wire().read("msg").int32()));
            }
        }
        // write another
        appender.writeDocument(wire -> wire.write("msg").int32(5));
        // roll 5 cycles
        timeProvider.currentTimeMillis(now += timeIncMs * 5);
        try (DocumentContext dc = tailer.readingDocument()) {
            assertTrue(dc.isPresent());
            assertEquals(5, dc.wire().read("msg").int32());
        }
        try (DocumentContext dc = tailer.readingDocument()) {
            assertFalse(dc.isPresent());
        }
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Test(org.junit.Test)

Example 80 with DocumentContext

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

the class StoreAppenderDoubleBufferTest method testDoubleBuffering.

// @Test(timeout = 10000L)
public void testDoubleBuffering() throws InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    try (SingleChronicleQueue q = binary(tempDir("q")).doubleBuffer(true).rollCycle(MINUTELY).timeProvider(() -> 0).build()) {
        BlockedWriterScenario blockedWriterScenario = scenarioClass.getConstructor(ChronicleQueue.class, Integer.class).newInstance(q, iterations);
        blockedWriterScenario.run();
        ExcerptTailer tailer = q.createTailer();
        for (int i = 0; i < iterations; i++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                assertEquals("blocker-before", dc.wire().read().text());
                assertEquals("blocker-after", dc.wire().read().text());
            }
            blockedWriterScenario.readBlockeeRecordForIteration(i, tailer);
        }
        // Test we're at the end
        try (DocumentContext dc = tailer.readingDocument()) {
            assertFalse(dc.isPresent());
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

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