Search in sources :

Example 6 with WireOut

use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.

the class BinLogTest method testOffer.

@Test
public void testOffer() throws Exception {
    assertTrue(binLog.offer(record(testString)));
    assertTrue(binLog.offer(record(testString2)));
    Util.spinAssertEquals(2, () -> readBinLogRecords(path).size(), 60);
    List<String> records = readBinLogRecords(path);
    assertEquals(testString, records.get(0));
    assertEquals(testString2, records.get(1));
    // Prevent the bin log thread from making progress
    Semaphore blockBinLog = new Semaphore(0);
    // Get notified when the bin log thread has blocked and definitely won't batch drain tasks
    Semaphore binLogBlocked = new Semaphore(0);
    try {
        assertTrue(binLog.offer(new BinLog.ReleaseableWriteMarshallable() {

            public void release() {
            }

            protected long version() {
                return 0;
            }

            protected String type() {
                return "test";
            }

            public void writeMarshallablePayload(WireOut wire) {
                // Notify the bing log thread is about to block
                binLogBlocked.release();
                try {
                    // Block the bin log thread so it doesn't process more tasks
                    blockBinLog.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }));
        // Wait for the bin log thread to block so it doesn't batch drain
        Util.spinAssertEquals(true, binLogBlocked::tryAcquire, 60);
        // Now fill the queue up to capacity and it should always accept
        for (int ii = 0; ii < 10; ii++) {
            assertTrue(binLog.offer(record(testString)));
        }
        // it shoudl reject this record since it is full
        assertFalse(binLog.offer(record(testString)));
    } finally {
        blockBinLog.release();
    }
    Util.spinAssertEquals(13, () -> readBinLogRecords(path).size(), 60);
    assertTrue(binLog.offer(record(testString)));
    Util.spinAssertEquals(14, () -> readBinLogRecords(path).size(), 60);
}
Also used : WireOut(net.openhft.chronicle.wire.WireOut) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 7 with WireOut

use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.

the class BinLogTest method testPut.

/**
 * Test that put blocks and unblocks and creates records
 */
@Test
public void testPut() throws Exception {
    binLog.put(record(testString));
    binLog.put(record(testString2));
    Util.spinAssertEquals(2, () -> readBinLogRecords(path).size(), 60);
    List<String> records = readBinLogRecords(path);
    assertEquals(testString, records.get(0));
    assertEquals(testString2, records.get(1));
    // Prevent the bin log thread from making progress
    Semaphore blockBinLog = new Semaphore(0);
    // Get notified when the bin log thread has blocked and definitely won't batch drain tasks
    Semaphore binLogBlocked = new Semaphore(0);
    try {
        binLog.put(new BinLog.ReleaseableWriteMarshallable() {

            public void release() {
            }

            protected long version() {
                return 0;
            }

            protected String type() {
                return "test";
            }

            public void writeMarshallablePayload(WireOut wire) {
                // Notify the bing log thread is about to block
                binLogBlocked.release();
                try {
                    // Block the bin log thread so it doesn't process more tasks
                    blockBinLog.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        // Wait for the bin log thread to block so it doesn't batch drain
        Util.spinAssertEquals(true, binLogBlocked::tryAcquire, 60);
        // Now fill the queue up to capacity and it shouldn't block
        for (int ii = 0; ii < 10; ii++) {
            binLog.put(record(testString));
        }
        // Thread to block on the full queue
        Thread t = new Thread(() -> {
            try {
                binLog.put(record(testString));
                // Should be able to do it again after unblocking
                binLog.put(record(testString));
            } catch (InterruptedException e) {
                throw new AssertionError(e);
            }
        });
        t.start();
        Thread.sleep(500);
        // If the thread is not terminated then it is probably blocked on the queue
        assertTrue(t.getState() != Thread.State.TERMINATED);
    } finally {
        blockBinLog.release();
    }
    // Expect all the records to eventually be there including one from the blocked thread
    Util.spinAssertEquals(15, () -> readBinLogRecords(path).size(), 60);
}
Also used : WireOut(net.openhft.chronicle.wire.WireOut) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 8 with WireOut

use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.

the class BinLogTest method testBinLogFinalizer.

/**
 * Check that the finalizer releases any stragglers in the queue
 */
@Test
public void testBinLogFinalizer() throws Exception {
    binLog.stop();
    Semaphore released = new Semaphore(0);
    binLog.sampleQueue.put(new BinLog.ReleaseableWriteMarshallable() {

        public void release() {
            released.release();
        }

        protected long version() {
            return 0;
        }

        protected String type() {
            return "test";
        }

        public void writeMarshallablePayload(WireOut wire) {
        }
    });
    binLog = null;
    for (int ii = 0; ii < 30; ii++) {
        System.gc();
        System.runFinalization();
        Thread.sleep(100);
        if (released.tryAcquire())
            return;
    }
    fail("Finalizer never released resources");
}
Also used : WireOut(net.openhft.chronicle.wire.WireOut) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 9 with WireOut

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

the class StatelessAppender method writeDocument.

@Override
public void writeDocument(@NotNull WriteMarshallable writer) {
    WireOut wire = wire();
    writer.writeMarshallable(wire);
    lastWrittenIndex = statelessRawBytesAppender.appendExcept(wire.bytes());
}
Also used : WireOut(net.openhft.chronicle.wire.WireOut)

Aggregations

WireOut (net.openhft.chronicle.wire.WireOut)9 Test (org.junit.Test)8 Semaphore (java.util.concurrent.Semaphore)5 File (java.io.File)2 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)2 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)2 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)2 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)2 Query (org.apache.cassandra.fql.FullQueryLogger.Query)2 ParsedTargetHost.fromString (org.apache.cassandra.fqltool.QueryReplayer.ParsedTargetHost.fromString)2 BinLog (org.apache.cassandra.utils.binlog.BinLog)2 BinLogTest (org.apache.cassandra.utils.binlog.BinLogTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)1