Search in sources :

Example 16 with IORuntimeException

use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.

the class WriteBytesTest method testWriteBytes.

@Test
public void testWriteBytes() {
    File dir = getTmpDir();
    try (ChronicleQueue queue = binary(dir).testBlockSize().build()) {
        ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();
        outgoingMsgBytes[0] = 'A';
        outgoingBytes.write(outgoingMsgBytes);
        postOneMessage(appender);
        fetchOneMessage(tailer, incomingMsgBytes);
        // System.out.println(new String(incomingMsgBytes));
        outgoingBytes.clear();
        outgoingMsgBytes[0] = 'A';
        outgoingMsgBytes[1] = 'B';
        outgoingBytes.write(outgoingMsgBytes);
        postOneMessage(appender);
        fetchOneMessage(tailer, incomingMsgBytes);
    // System.out.println(new String(incomingMsgBytes));
    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
        // ignored
        }
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) File(java.io.File) Test(org.junit.Test)

Example 17 with IORuntimeException

use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.

the class MethodReaderBenchmark method init.

@Override
public void init(JLBH jlbh) {
    this.jlbh = jlbh;
    String benchmarkQueuePath = System.getProperty("benchmarkQueuePath");
    if (benchmarkQueuePath != null) {
        System.out.println("Creating queue in dir: " + benchmarkQueuePath);
        IOTools.deleteDirWithFiles(benchmarkQueuePath, 10);
        queue = ChronicleQueue.single(benchmarkQueuePath);
    } else {
        System.out.println("Creating queue in temp dir");
        try {
            queue = ChronicleQueue.single(Files.createTempDirectory("temp").toString());
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }
    appender = queue.acquireAppender();
    writer = appender.methodWriter(AnInterface.class);
    nextExecutionReport = new ExecutionReportDTO(ThreadLocalRandom.current());
    nextOrder = new OrderDTO(ThreadLocalRandom.current());
    consumerThread = new Thread(() -> {
        try (final AffinityLock affinityLock = AffinityLock.acquireCore()) {
            tailer = queue.createTailer().disableThreadSafetyCheck(true);
            noArgsCallSampler = jlbh.addProbe("No args call");
            oneIntCallSampler = jlbh.addProbe("One int call");
            oneLongCallSampler = jlbh.addProbe("One long call");
            smallDtoCallSampler = jlbh.addProbe("Small DTO call");
            stringAndSmallDtoCallSampler = jlbh.addProbe("String and small DTO call");
            bigDtoCallSampler = jlbh.addProbe("Big DTO call");
            stringAndBigDtoCallSampler = jlbh.addProbe("String and big DTO call");
            final AnInterfaceSamplingImpl samplingImpl = new AnInterfaceSamplingImpl();
            reader = tailer.methodReader(samplingImpl);
            while (!stopped) {
                if (reader.readOne()) {
                    String startNsString;
                    do {
                        startNsString = tailer.readText();
                    } while (startNsString == null);
                    long startNs = Long.parseLong(startNsString);
                    samplingImpl.doSample(startNs);
                    jlbh.sample(System.nanoTime() - startNs);
                }
            }
        }
    });
    consumerThread.start();
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) IOException(java.io.IOException) AffinityLock(net.openhft.affinity.AffinityLock)

Example 18 with IORuntimeException

use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.

the class MarshallableTest method testWriteText.

@Test
public void testWriteText() {
    File dir = getTmpDir();
    try (ChronicleQueue queue = binary(dir).testBlockSize().build()) {
        ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();
        ExcerptTailer tailer2 = queue.createTailer();
        int runs = 1000;
        for (int i = 0; i < runs; i++) appender.writeText("" + i);
        for (int i = 0; i < runs; i++) assertEquals("" + i, tailer.readText());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < runs; i++) {
            assertTrue(tailer2.readText(sb));
            assertEquals("" + i, sb.toString());
        }
    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
        // ignored
        }
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) File(java.io.File) Test(org.junit.Test)

Example 19 with IORuntimeException

use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.

the class SingleTableBuilder method build.

// *************************************************************************
// 
// *************************************************************************
@NotNull
public TableStore<T> build() {
    if (readOnly) {
        if (!file.exists())
            throw new IORuntimeException("Metadata file not found in readOnly mode");
        // Wait a short time for the file to be initialized
        TimingPauser pauser = Pauser.balanced();
        try {
            while (file.length() < OS.mapAlignment()) {
                pauser.pause(1, TimeUnit.SECONDS);
            }
        } catch (TimeoutException e) {
            throw new IORuntimeException("Metadata file found in readOnly mode, but not initialized yet");
        }
    }
    MappedBytes bytes = null;
    try {
        if (!readOnly && file.createNewFile() && !file.canWrite()) {
            throw new IllegalStateException("Cannot write to tablestore file " + file);
        }
        bytes = MappedBytes.mappedBytes(file, OS.SAFE_PAGE_SIZE, OS.SAFE_PAGE_SIZE, readOnly);
        // these MappedBytes are shared, but the assumption is they shouldn't grow. Supports 2K entries.
        bytes.disableThreadSafetyCheck(true);
        // eagerly initialize backing MappedFile page - otherwise wire.writeFirstHeader() will try to lock the file
        // to allocate the first byte store and that will cause lock overlap
        bytes.readVolatileInt(0);
        Wire wire = wireType.apply(bytes);
        if (readOnly)
            return SingleTableStore.doWithSharedLock(file, v -> {
                try {
                    return readTableStore(wire);
                } catch (IOException ex) {
                    throw Jvm.rethrow(ex);
                }
            }, () -> null);
        else {
            MappedBytes finalBytes = bytes;
            return SingleTableStore.doWithExclusiveLock(file, v -> {
                try {
                    if (wire.writeFirstHeader()) {
                        return writeTableStore(finalBytes, wire);
                    } else {
                        return readTableStore(wire);
                    }
                } catch (IOException ex) {
                    throw Jvm.rethrow(ex);
                }
            }, () -> null);
        }
    } catch (IOException e) {
        throw new IORuntimeException("file=" + file.getAbsolutePath(), e);
    } finally {
        if (bytes != null)
            bytes.clearUsedByThread();
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) StreamCorruptedException(java.io.StreamCorruptedException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Builder(net.openhft.chronicle.core.util.Builder) Wire(net.openhft.chronicle.wire.Wire) WireType(net.openhft.chronicle.wire.WireType) Jvm(net.openhft.chronicle.core.Jvm) File(java.io.File) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ValueIn(net.openhft.chronicle.wire.ValueIn) StringUtils(net.openhft.chronicle.core.util.StringUtils) CLASS_ALIASES(net.openhft.chronicle.core.pool.ClassAliasPool.CLASS_ALIASES) OS(net.openhft.chronicle.core.OS) NotNull(org.jetbrains.annotations.NotNull) MetaDataKeys(net.openhft.chronicle.queue.impl.single.MetaDataKeys) Path(java.nio.file.Path) Pauser(net.openhft.chronicle.threads.Pauser) Wires(net.openhft.chronicle.wire.Wires) TableStore(net.openhft.chronicle.queue.impl.TableStore) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TimeoutException(java.util.concurrent.TimeoutException) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with IORuntimeException

use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueBuilder method initializeMetadata.

protected void initializeMetadata() {
    File metapath = metapath();
    validateRollCycle(metapath);
    SCQMeta metadata = new SCQMeta(new SCQRoll(rollCycle(), epoch(), rollTime, rollTimeZone), deltaCheckpointInterval(), sourceId());
    try {
        boolean readOnly = readOnly();
        metaStore = SingleTableBuilder.binary(metapath, metadata).readOnly(readOnly).build();
        // check if metadata was overridden
        SCQMeta newMeta = metaStore.metadata();
        sourceId(newMeta.sourceId());
        String format = newMeta.roll().format();
        if (!format.equals(rollCycle().format())) {
            // roll cycle changed
            overrideRollCycleForFileName(format);
        }
        // if it was overridden - reset
        rollTime = newMeta.roll().rollTime();
        rollTimeZone = newMeta.roll().rollTimeZone();
        epoch = newMeta.roll().epoch();
    } catch (IORuntimeException ex) {
        // readonly=true and file doesn't exist
        if (OS.isWindows())
            // we cant have a read-only table store on windows so we have no option but to throw the ex.
            throw ex;
        if (ex.getMessage().equals("Metadata file not found in readOnly mode"))
            Jvm.warn().on(getClass(), "Failback to readonly tablestore " + ex);
        else
            Jvm.warn().on(getClass(), "Failback to readonly tablestore", ex);
        metaStore = new ReadonlyTableStore<>(metadata);
    }
}
Also used : ReadonlyTableStore(net.openhft.chronicle.queue.impl.table.ReadonlyTableStore) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) File(java.io.File)

Aggregations

IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)23 Test (org.junit.Test)10 File (java.io.File)9 IOException (java.io.IOException)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 EOFException (java.io.EOFException)3 StreamCorruptedException (java.io.StreamCorruptedException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ValueIn (net.openhft.chronicle.wire.ValueIn)3 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Bytes (net.openhft.chronicle.bytes.Bytes)2 Jvm (net.openhft.chronicle.core.Jvm)2 OS (net.openhft.chronicle.core.OS)2 LongValue (net.openhft.chronicle.core.values.LongValue)2 StoreFileListener (net.openhft.chronicle.queue.impl.StoreFileListener)2 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)2 Pauser (net.openhft.chronicle.threads.Pauser)2