Search in sources :

Example 11 with MappedBytes

use of net.openhft.chronicle.bytes.MappedBytes in project Chronicle-Queue by OpenHFT.

the class SingleCQFormatTest method testCompleteHeader2.

@Test
public void testCompleteHeader2() throws FileNotFoundException {
    @NotNull File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
    dir.mkdir();
    @NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101-02" + SingleChronicleQueue.SUFFIX), ChronicleQueue.TEST_BLOCK_SIZE * 2);
    @NotNull Wire wire = new BinaryWire(bytes);
    try (DocumentContext dc = wire.writingDocument(true)) {
        dc.wire().writeEventName(() -> "header").typedMarshallable(new SingleChronicleQueueStore(RollCycles.HOURLY, WireType.BINARY, bytes, 60 * 60 * 1000, 4 << 10, 4, new TimedStoreRecovery(WireType.BINARY), -1, 0));
    }
    assertEquals("--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  wireType: !WireType BINARY,\n" + "  writePosition: [\n" + "    0,\n" + "    0\n" + "  ],\n" + "  roll: !SCQSRoll {\n" + "    length: !int 3600000,\n" + "    format: yyyyMMdd-HH,\n" + "    epoch: !int 3600000\n" + "  },\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: !short 4096,\n" + "    indexSpacing: 4,\n" + "    index2Index: 0,\n" + "    lastIndex: 0\n" + "  },\n" + "  lastAcknowledgedIndexReplicated: -1,\n" + "  recovery: !TimedStoreRecovery {\n" + "    timeStamp: 0\n" + "  },\n" + "  deltaCheckpointInterval: !byte -1,\n" + "  lastIndexReplicated: -1,\n" + "  sourceId: 0\n" + "}\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
    bytes.release();
    @NotNull SingleChronicleQueue queue = binary(dir).testBlockSize().rollCycle(RollCycles.HOURLY).build();
    testQueue(queue);
    assertEquals(2, queue.firstCycle());
    queue.close();
    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 12 with MappedBytes

use of net.openhft.chronicle.bytes.MappedBytes in project Chronicle-Queue by OpenHFT.

the class SingleTableBuilder method build.

// *************************************************************************
// 
// *************************************************************************
@NotNull
public TableStore build() {
    if (readOnly && !file.exists()) {
        throw new IORuntimeException("File not found in readOnly mode");
    }
    try {
        MappedBytes bytes = MappedBytes.mappedBytes(file, 64 << 10, 0, readOnly);
        Wire wire = wireType.apply(bytes);
        StoreRecovery recovery = recoverySupplier.apply(wireType);
        try {
            TableStore tableStore;
            if ((!readOnly) && wire.writeFirstHeader()) {
                tableStore = writeTableStore(bytes, wire, recovery);
            } else {
                wire.readFirstHeader(timeoutMS, TimeUnit.MILLISECONDS);
                StringBuilder name = Wires.acquireStringBuilder();
                ValueIn valueIn = wire.readEventName(name);
                if (StringUtils.isEqual(name, MetaDataKeys.header.name())) {
                    tableStore = valueIn.typedMarshallable();
                } else {
                    // noinspection unchecked
                    throw new StreamCorruptedException("The first message should be the header, was " + name);
                }
            }
            return tableStore;
        } catch (TimeoutException e) {
            recovery.recoverAndWriteHeader(wire, 10_000, null, null);
            return writeTableStore(bytes, wire, recovery);
        }
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) TimedStoreRecovery(net.openhft.chronicle.queue.impl.single.TimedStoreRecovery) StoreRecovery(net.openhft.chronicle.queue.impl.single.StoreRecovery) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TableStore(net.openhft.chronicle.queue.impl.TableStore) TimeoutException(java.util.concurrent.TimeoutException) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with MappedBytes

use of net.openhft.chronicle.bytes.MappedBytes in project Chronicle-Queue by OpenHFT.

the class MultiQueueStressMain method main.

public static void main(String[] args) throws FileNotFoundException {
    for (int t = 0; t < runs; t++) {
        long start0 = System.currentTimeMillis();
        int count = 0;
        String baseDir = target + "/deleteme/" + System.nanoTime();
        new File(baseDir).mkdirs();
        MappedBytes[] queues = new MappedBytes[queueCount];
        int pagesPer10Second = (int) (10L * (throughput << 20) / queueCount / (4 << 10));
        for (int i = 0; i < queueCount; i++) {
            long start1 = System.currentTimeMillis();
            String filename = baseDir + "/" + count++;
            queues[i] = MappedBytes.mappedBytes(filename, pagesPer10Second * (4 << 10));
            long time1 = System.currentTimeMillis() - start1;
            if (time1 > 20)
                System.out.printf("Creating %s took %.3f seconds%n", filename, time1 / 1e3);
        }
        long mid1 = System.currentTimeMillis();
        for (int i = 0; i < pagesPer10Second; i++) {
            for (MappedBytes bytes : queues) {
                bytes.writeLong(i * (4 << 10), i);
            }
        }
        long mid2 = System.currentTimeMillis();
        for (MappedBytes bytes : queues) {
            bytes.release();
        }
        long end0 = System.currentTimeMillis();
        long time0 = end0 - start0;
        System.out.printf("Took %.3f seconds to write %,d MB, create: %.3f, write: %.3f, close: %.3f%n", time0 / 1e3, throughput * 10, (mid1 - start0) / 1e3, (mid2 - mid1) / 1e3, (end0 - mid2) / 1e3);
        long delay = 10000 - time0;
        if (delay > 0)
            Jvm.pause(delay);
    }
}
Also used : File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 14 with MappedBytes

use of net.openhft.chronicle.bytes.MappedBytes 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)

Aggregations

MappedBytes (net.openhft.chronicle.bytes.MappedBytes)14 File (java.io.File)9 IOException (java.io.IOException)8 NotNull (org.jetbrains.annotations.NotNull)8 Test (org.junit.Test)8 TimeoutException (java.util.concurrent.TimeoutException)7 FileNotFoundException (java.io.FileNotFoundException)6 MappedFile (net.openhft.chronicle.bytes.MappedFile)6 Wire (net.openhft.chronicle.wire.Wire)4 ByteBuffer (java.nio.ByteBuffer)2 DocumentContext (net.openhft.chronicle.wire.DocumentContext)2 WireDumper (net.openhft.chronicle.wire.WireDumper)2 Ignore (org.junit.Ignore)2 EOFException (java.io.EOFException)1 PrintWriter (java.io.PrintWriter)1 StreamCorruptedException (java.io.StreamCorruptedException)1 StringWriter (java.io.StringWriter)1 LocalDateTime (java.time.LocalDateTime)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 CommonStore (net.openhft.chronicle.queue.impl.CommonStore)1