Search in sources :

Example 16 with Wire

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

the class ThroughputPerfMain method main.

public static void main(String[] args) {
    String base = path + "/delete-" + Time.uniqueId() + ".me";
    long start = System.nanoTime();
    long count = 0;
    nbs = BytesStore.nativeStoreWithFixedCapacity(size);
    long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
    try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
        ExcerptAppender appender = q.acquireAppender();
        long lastIndex = -1;
        do {
            int defaultIndexSpacing = q.rollCycle().defaultIndexSpacing();
            Wire wire = appender.wire();
            int writeCount = (int) (defaultIndexSpacing - (lastIndex & (defaultIndexSpacing - 1)) - 1);
            if (wire != null && writeCount > 0) {
                MappedBytes bytes = (MappedBytes) wire.bytes();
                long address = bytes.addressForWrite(bytes.writePosition());
                long bstart = bytes.start();
                long bcap = bytes.realCapacity();
                long canWrite = bcap - (bytes.writePosition() - bstart);
                long lengthCount = writeMessages(address, canWrite, writeCount);
                bytes.writeSkip((int) lengthCount);
                lastIndex += lengthCount >> 32;
                count += lengthCount >> 32;
            } else {
                try (DocumentContext dc = appender.writingDocument()) {
                    dc.wire().bytes().write(nbs);
                }
                lastIndex = appender.lastIndexAppended();
                count++;
            }
        } while (start + time * 1e9 > System.nanoTime());
    }
    nbs.releaseLast();
    long mid = System.nanoTime();
    long time1 = mid - start;
    Bytes bytes = Bytes.allocateElasticDirect(64);
    try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
        ExcerptTailer tailer = q.createTailer();
        for (long i = 0; i < count; i++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                bytes.clear();
                bytes.write(dc.wire().bytes());
            }
        }
    }
    bytes.releaseLast();
    long end = System.nanoTime();
    long time2 = end - mid;
    System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time1 / 1e9, (long) (1e9 * count / time1));
    System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time2 / 1e9, (long) (1e9 * count / time2));
    // make sure its cleaned up for windows to delete.
    System.gc();
    IOTools.deleteDirWithFiles(base, 2);
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 17 with Wire

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

the class AppenderLockOnlyAppliesToFileTest method concurrentLockItUp.

/*
    Failed tests:
  AppenderLockOnlyAppliesToFileTest.concurrentLockItUp:59 Writer thread completed before timeout

     */
@Ignore("fails too often")
@Test
public void concurrentLockItUp() throws InterruptedException {
    final AtomicReference<String> writerQueueFile = new AtomicReference<>();
    final File path = DirectoryUtils.tempDir(this.getClass().getSimpleName());
    final SingleChronicleQueueBuilder builder = ChronicleQueueBuilder.single(path).sourceId(1).rollCycle(ROLL_CYCLE).timeoutMS(TIMEOUT_MS);
    final String initialFile;
    final DocumentContext initialContext = builder.build().acquireAppender().writingDocument();
    initialContext.wire().writeText("abcd");
    initialFile = getFilename(initialContext);
    // don't close context
    final long afterInitialWrite = System.currentTimeMillis();
    final CountDownLatch writerStarted = new CountDownLatch(1);
    final CountDownLatch writerFinished = new CountDownLatch(1);
    Thread writerThread = new Thread(() -> {
        ExcerptAppender appender = builder.build().acquireAppender();
        writerStarted.countDown();
        // wait for less than timeout and more than roll
        Jvm.pause(WAIT_FOR_ROLL_MS);
        try (@NotNull DocumentContext context = appender.writingDocument()) {
            // should not have been held up by locking
            writerQueueFile.set(getFilename(context));
            Wire wire = context.wire();
            wire.writeText("hello");
            writerFinished.countDown();
        }
    });
    writerThread.start();
    assertTrue("Writer thread not started", writerStarted.await(1, TimeUnit.SECONDS));
    assertTrue("Writer thread completed before timeout", writerFinished.await(WAIT_FOR_ROLL_MS + 50, TimeUnit.MILLISECONDS));
    assertFalse("Threads wrote to different queue cycles, so no locking occurred", initialFile.equals(writerQueueFile.get()));
    assertTrue("We are within timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
    ExcerptTailer tailer = builder.build().createTailer();
    // this call to readingDocument waits for timeout and logs out ".... resetting header after timeout ...."
    try (DocumentContext rd = tailer.readingDocument()) {
        assertFalse("We are outside timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
        assertTrue("Something was written", rd.isPresent());
        String value = rd.wire().readText();
        assertEquals("the first (locked) write is lost", "hello", value);
    }
    try (DocumentContext rd = tailer.readingDocument()) {
        assertFalse("Should be only one message in the queue", rd.isPresent());
    }
    try {
        initialContext.close();
        fail("close should have thrown");
    } catch (IllegalStateException e) {
    // expected for close to throw
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Wire(net.openhft.chronicle.wire.Wire) NotNull(org.jetbrains.annotations.NotNull) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with Wire

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

the class DetectNotReadyEntriesTest method testDeadEntries.

@Test
public void testDeadEntries() throws FileNotFoundException {
    // TODO FIX.
    if (OS.isWindows())
        return;
    File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
    dir.mkdir();
    MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101" + SingleChronicleQueue.SUFFIX), 64 << 10);
    Wire wire = new BinaryWire(bytes);
    try (DocumentContext dc = wire.writingDocument(true)) {
        dc.wire().writeEventName(() -> "header").typePrefix(SingleChronicleQueueStore.class).marshallable(w -> {
            w.write(() -> "wireType").object(WireType.BINARY);
            w.write(() -> "writePosition").int64forBinding(288 + 4 + 17);
            w.write(() -> "roll").typedMarshallable(new SCQRoll(RollCycles.DAILY, 0));
            w.write(() -> "indexing").typedMarshallable(new SCQIndexing(WireType.BINARY, 32 << 10, 32));
            w.write(() -> "lastAcknowledgedIndexReplicated").int64forBinding(0);
        });
    }
    long pos = wire.bytes().writePosition();
    try (DocumentContext dc = wire.writingDocument(false)) {
        dc.wire().write("test").text("Hello World");
    }
    assertEquals(17, wire.bytes().readInt(pos));
    // make it incomplete, note that the length is removed,
    // since writing a length into an incomplete excerpt is not allowed
    wire.bytes().writeInt(pos, Wires.NOT_COMPLETE);
    assertEquals("--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  wireType: !WireType BINARY,\n" + "  writePosition: 309,\n" + "  roll: !SCQSRoll {\n" + "    length: !int 86400000,\n" + "    format: yyyyMMdd,\n" + "    epoch: 0\n" + "  },\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: !int 32768,\n" + "    indexSpacing: 32,\n" + "    index2Index: 0,\n" + "    lastIndex: 0\n" + "  },\n" + "  lastAcknowledgedIndexReplicated: 0\n" + "}\n" + "# position: 288, header: -1 or 0\n" + "--- !!not-ready-data! #binary\n" + "...\n" + "# 17 bytes remaining\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
    bytes.release();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().build()) {
        queue.acquireAppender().writeText("Bye for now");
    }
    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BinaryWire(net.openhft.chronicle.wire.BinaryWire) Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) FileNotFoundException(java.io.FileNotFoundException) BinaryWire(net.openhft.chronicle.wire.BinaryWire) Test(org.junit.Test)

Example 19 with Wire

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

the class RollEOFTest method removeEOF.

private void removeEOF(Path path) throws IOException {
    long blockSize = 64 << 10;
    long chunkSize = OS.pageAlign(blockSize);
    long overlapSize = OS.pageAlign(blockSize / 4);
    final MappedBytes mappedBytes = MappedBytes.mappedBytes(path.toFile(), chunkSize, overlapSize, false);
    mappedBytes.reserve();
    try {
        final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes);
        final Bytes<?> bytes = wire.bytes();
        bytes.readLimit(bytes.capacity());
        bytes.readSkip(4);
        // move past header
        try (final SingleChronicleQueueStore qs = loadStore(wire)) {
            assertNotNull(qs);
            long l = qs.writePosition();
            long len = Wires.lengthOf(bytes.readVolatileInt(l));
            long eofOffset = l + len + 4L;
            bytes.writePosition(eofOffset);
            bytes.writeInt(0);
        }
    } finally {
        mappedBytes.release();
    }
}
Also used : SingleChronicleQueueStore(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueStore) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 20 with Wire

use of net.openhft.chronicle.wire.Wire 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)

Aggregations

Wire (net.openhft.chronicle.wire.Wire)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)22 Test (org.junit.Test)11 File (java.io.File)9 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)8 NotNull (org.jetbrains.annotations.NotNull)8 Bytes (net.openhft.chronicle.bytes.Bytes)7 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)7 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 IOException (java.io.IOException)5 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)5 BytesStore (net.openhft.chronicle.bytes.BytesStore)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 Assert (org.junit.Assert)3 Ignore (org.junit.Ignore)3 LocalDateTime (java.time.LocalDateTime)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Jvm (net.openhft.chronicle.core.Jvm)2 OS (net.openhft.chronicle.core.OS)2