Search in sources :

Example 1 with IORuntimeException

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

the class AcquireReleaseTest method testAccquireAndRelease.

@Test
public void testAccquireAndRelease() throws Exception {
    File dir = DirectoryUtils.tempDir("AcquireReleaseTest");
    try {
        AtomicInteger acount = new AtomicInteger();
        AtomicInteger qcount = new AtomicInteger();
        StoreFileListener sfl = new StoreFileListener() {

            @Override
            public void onAcquired(int cycle, File file) {
                System.out.println("onAcquired(): " + file);
                acount.incrementAndGet();
            }

            @Override
            public void onReleased(int cycle, File file) {
                System.out.println("onReleased(): " + file);
                // TODO Auto-generated method stub
                qcount.incrementAndGet();
            }
        };
        AtomicLong time = new AtomicLong(1000l);
        TimeProvider tp = () -> time.getAndAccumulate(1000, (x, y) -> x + y);
        ChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).storeFileListener(sfl).timeProvider(tp).build();
        for (int i = 0; i < 10; i++) {
            queue.acquireAppender().writeDocument(w -> {
                w.write("a").marshallable(m -> {
                    m.write("b").text("c");
                });
            });
        }
        Assert.assertEquals(10, acount.get());
        Assert.assertEquals(9, qcount.get());
        queue.close();
    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
        // ignored
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) TimeProvider(net.openhft.chronicle.core.time.TimeProvider) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreFileListener(net.openhft.chronicle.queue.impl.StoreFileListener) File(java.io.File) Test(org.junit.Test)

Example 2 with IORuntimeException

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

the class ChronicleRollingIssueTest method test.

@Ignore
@Test
public void test() throws Exception {
    int threads = Runtime.getRuntime().availableProcessors() - 1;
    int messages = 50;
    String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    AtomicInteger count = new AtomicInteger();
    Runnable appendRunnable = () -> {
        try (final ChronicleQueue writeQueue = ChronicleQueueBuilder.single(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).build()) {
            for (int i = 0; i < messages; i++) {
                long millis = System.currentTimeMillis() % 1000;
                if (millis > 1 && millis < 999) {
                    Jvm.pause(999 - millis);
                }
                ExcerptAppender appender = writeQueue.acquireAppender();
                Map<String, Object> map = new HashMap<>();
                map.put("key", Thread.currentThread().getName() + " - " + i);
                appender.writeMap(map);
                count.incrementAndGet();
            }
        }
    };
    for (int i = 0; i < threads; i++) {
        new Thread(appendRunnable, "appender-" + i).start();
    }
    long start = System.currentTimeMillis();
    long lastIndex = 0;
    try (final ChronicleQueue queue = ChronicleQueueBuilder.single(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).build()) {
        ExcerptTailer tailer = queue.createTailer();
        int count2 = 0;
        while (count2 < threads * messages) {
            Map<String, Object> map = tailer.readMap();
            long index = tailer.index();
            if (map != null) {
                count2++;
            } else if (index >= 0) {
                if (RollCycles.TEST_SECONDLY.toCycle(lastIndex) != RollCycles.TEST_SECONDLY.toCycle(index)) {
                    System.out.println("Wrote: " + count + " read: " + count2 + " index: " + Long.toHexString(index));
                    lastIndex = index;
                }
            }
            final int i = count.get();
            if (System.currentTimeMillis() > start + 60000) {
                // System.out.println(queue.dump());
                throw new AssertionError("Wrote: " + count + " read: " + count2 + " index: " + Long.toHexString(index));
            }
        }
    } finally {
        try {
            IOTools.deleteDirWithFiles(path, 2);
        } catch (IORuntimeException todoFixOnWindows) {
        }
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HashMap(java.util.HashMap) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with IORuntimeException

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

the class CreateAtIndexTest method testWriteBytesWithIndex.

@Test
public void testWriteBytesWithIndex() throws Exception {
    String tmp = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().rollCycle(TEST_DAILY).build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        appender.writeBytes(0x421d00000001L, HELLO_WORLD);
    }
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        String before = queue.dump();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        String after = queue.dump();
        assertEquals(before, after);
    }
    boolean runIfAssertsOn = false;
    // assert runIfAssertsOn = true;
    if (runIfAssertsOn) {
        try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
            InternalAppender appender = (InternalAppender) queue.acquireAppender();
            String before = queue.dump();
            try {
                appender.writeBytes(0x421d00000000L, Bytes.from("hellooooo world"));
                fail();
            } catch (IllegalStateException e) {
            // expected
            }
            String after = queue.dump();
            assertEquals(before, after);
        }
    }
    // try too far
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        try {
            appender.writeBytes(0x421d00000003L, HELLO_WORLD);
            fail();
        } catch (IllegalStateException e) {
            assertEquals("Unable to move to index 421d00000003 beyond the end of the queue", e.getMessage());
        }
    }
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000002L, HELLO_WORLD);
        appender.writeBytes(0x421d00000003L, HELLO_WORLD);
    }
    try {
        IOTools.deleteDirWithFiles(tmp, 2);
    } catch (IORuntimeException ignored) {
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) InternalAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender) Test(org.junit.Test)

Example 4 with IORuntimeException

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

the class RandomDataInput method readUtf8Limited.

/**
 * Truncates {@code sb} (it must be a {@link StringBuilder} or {@link Bytes}) and reads a char
 * sequence from the given {@code offset}, encoded as Utf8, into it. Returns offset <i>after</i>
 * the read Utf8, if a normal char sequence was read, or {@code -1 - offset}, if {@code null}
 * was observed (in this case, {@code sb} is truncated too, but not updated then, by querying
 * {@code sb} only this case is indistinguishable from reading an empty char sequence). If
 * length of Utf8 encoding of the char sequence exceeds {@code maxUtf8Len},
 * {@code IllegalStateException} is thrown.
 *
 * @param offset     the offset in this {@code RandomDataInput} to read char sequence from
 * @param sb         the buffer to read char sequence into (truncated first)
 * @param maxUtf8Len the maximum allowed length of the char sequence in Utf8 encoding
 * @param <ACS>      buffer type, must be {@code StringBuilder} or {@code Bytes}
 * @return offset after the normal read char sequence, or -1 - offset, if char sequence is
 * {@code null}
 * @see RandomDataOutput#writeUtf8Limited(long, CharSequence, int)
 */
default <ACS extends Appendable & CharSequence> long readUtf8Limited(long offset, @NotNull ACS sb, int maxUtf8Len) throws IORuntimeException, IllegalArgumentException, BufferUnderflowException, IllegalStateException {
    AppendableUtil.setLength(sb, 0);
    // TODO insert some bounds check here
    long utfLen;
    if ((utfLen = readByte(offset++)) < 0) {
        utfLen &= 0x7FL;
        long b;
        int count = 7;
        while ((b = readByte(offset++)) < 0) {
            utfLen |= (b & 0x7FL) << count;
            count += 7;
        }
        if (b != 0) {
            if (count > 56)
                throw new IORuntimeException("Cannot read more than 9 stop bits of positive value");
            utfLen |= (b << count);
        } else {
            if (count > 63)
                throw new IORuntimeException("Cannot read more than 10 stop bits of negative value");
            utfLen = ~utfLen;
        }
    }
    if (utfLen == -1)
        return ~offset;
    if (utfLen > maxUtf8Len)
        throw new IllegalStateException("Attempted to read a char sequence of " + "utf8 size " + utfLen + ", when only " + maxUtf8Len + " allowed");
    BytesInternal.parseUtf8(this, offset, sb, (int) utfLen);
    return offset + utfLen;
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException)

Example 5 with IORuntimeException

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

the class StreamingDataInput method read8bit.

default boolean read8bit(@NotNull StringBuilder sb) throws IORuntimeException, BufferUnderflowException {
    sb.setLength(0);
    long len0 = BytesInternal.readStopBit(this);
    if (len0 == -1)
        return false;
    int len = Maths.toUInt31(len0);
    try {
        AppendableUtil.parse8bit(this, sb, len);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    return true;
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) IOException(java.io.IOException)

Aggregations

IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)15 Test (org.junit.Test)7 File (java.io.File)5 IOException (java.io.IOException)3 ValueIn (net.openhft.chronicle.wire.ValueIn)3 StreamCorruptedException (java.io.StreamCorruptedException)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 QueryOptions (org.apache.cassandra.cql3.QueryOptions)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 Arguments (io.airlift.airline.Arguments)1 Command (io.airlift.airline.Command)1 Option (io.airlift.airline.Option)1 Unpooled (io.netty.buffer.Unpooled)1 EOFException (java.io.EOFException)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1