Search in sources :

Example 1 with TimeProvider

use of net.openhft.chronicle.core.time.TimeProvider in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method testOverreadForwardFromFutureCycleThenReadBackwardTailer.

@Test
public void testOverreadForwardFromFutureCycleThenReadBackwardTailer() {
    RollCycle cycle = TEST2_DAILY;
    // when "forwardToFuture" flag is set, go one cycle to the future
    AtomicBoolean forwardToFuture = new AtomicBoolean(false);
    TimeProvider timeProvider = () -> forwardToFuture.get() ? System.currentTimeMillis() + TimeUnit.MILLISECONDS.toDays(1) : System.currentTimeMillis();
    try (RollingChronicleQueue chronicle = builder(getTmpDir(), this.wireType).rollCycle(cycle).timeProvider(timeProvider).build()) {
        ExcerptAppender appender = chronicle.acquireAppender();
        appender.writeDocument(w -> w.writeEventName("hello").text("world"));
        // go to the cycle next to the one the write was made on
        forwardToFuture.set(true);
        ExcerptTailer forwardTailer = chronicle.createTailer().direction(TailerDirection.FORWARD).toStart();
        try (DocumentContext context = forwardTailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        try (DocumentContext context = forwardTailer.readingDocument()) {
            assertFalse(context.isPresent());
        }
        ExcerptTailer backwardTailer = chronicle.createTailer().direction(TailerDirection.BACKWARD).toEnd();
        try (DocumentContext context = backwardTailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) TimeProvider(net.openhft.chronicle.core.time.TimeProvider) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue)

Example 2 with TimeProvider

use of net.openhft.chronicle.core.time.TimeProvider 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 3 with TimeProvider

use of net.openhft.chronicle.core.time.TimeProvider in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method mappedSegmentsShouldBeUnmappedAsCycleRolls.

@Test
public void mappedSegmentsShouldBeUnmappedAsCycleRolls() throws Exception {
    final Random random = new Random(0xDEADBEEF);
    final File queueFolder = DirectoryUtils.tempDir("mappedSegmentsShouldBeUnmappedAsCycleRolls");
    final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(queueFolder).timeProvider(clock::get).testBlockSize().rollCycle(RollCycles.HOURLY).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        for (int i = 0; i < 20_000; i++) {
            final int batchSize = random.nextInt(10);
            appender.writeDocument(batchSize, ValueOut::int64);
            final byte payload = (byte) random.nextInt();
            for (int j = 0; j < batchSize; j++) {
                appender.writeDocument(payload, ValueOut::int8);
            }
            if (random.nextDouble() > 0.995) {
                clock.addAndGet(TimeUnit.MINUTES.toMillis(37L));
                // this give the reference processor a chance to run
                LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(15L));
            }
        }
        if (OS.isLinux())
            assertTrue("Too many mapped files: " + getMappedQueueFileCount(), getMappedQueueFileCount() < 40);
        assertTrue(Files.list(queueFolder.toPath()).filter(p -> p.toString().endsWith(SUFFIX)).count() > 10L);
    }
}
Also used : net.openhft.chronicle.bytes(net.openhft.chronicle.bytes) IntStream(java.util.stream.IntStream) SUFFIX(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue.SUFFIX) CoreMatchers.is(org.hamcrest.CoreMatchers.is) java.util(java.util) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Jvm(net.openhft.chronicle.core.Jvm) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) ISO_8859_1(java.nio.charset.StandardCharsets.ISO_8859_1) net.openhft.chronicle.wire(net.openhft.chronicle.wire) LogLevel(net.openhft.chronicle.core.onoes.LogLevel) StringUtils(net.openhft.chronicle.core.util.StringUtils) BiConsumer(java.util.function.BiConsumer) InternalAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender) TimeProvider(net.openhft.chronicle.core.time.TimeProvider) Parameterized(org.junit.runners.Parameterized) ExceptionKey(net.openhft.chronicle.core.onoes.ExceptionKey) ALWAYS(net.openhft.chronicle.wire.MarshallableOut.Padding.ALWAYS) Files(java.nio.file.Files) java.util.concurrent(java.util.concurrent) StoreAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.StoreAppender) LockSupport(java.util.concurrent.locks.LockSupport) AtomicLong(java.util.concurrent.atomic.AtomicLong) UsedViaReflection(net.openhft.chronicle.core.annotation.UsedViaReflection) java.io(java.io) net.openhft.chronicle.queue(net.openhft.chronicle.queue) FileVisitOption(java.nio.file.FileVisitOption) Closeable.closeQuietly(net.openhft.chronicle.core.io.Closeable.closeQuietly) RollCycles(net.openhft.chronicle.queue.RollCycles) Assume.assumeTrue(org.junit.Assume.assumeTrue) org.junit(org.junit) OS(net.openhft.chronicle.core.OS) ThreadDump(net.openhft.chronicle.core.threads.ThreadDump) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

TimeProvider (net.openhft.chronicle.core.time.TimeProvider)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)2 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)2 java.io (java.io)1 File (java.io.File)1 ISO_8859_1 (java.nio.charset.StandardCharsets.ISO_8859_1)1 FileVisitOption (java.nio.file.FileVisitOption)1 Files (java.nio.file.Files)1 java.util (java.util)1 java.util.concurrent (java.util.concurrent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LockSupport (java.util.concurrent.locks.LockSupport)1 BiConsumer (java.util.function.BiConsumer)1 Supplier (java.util.function.Supplier)1 IntStream (java.util.stream.IntStream)1 net.openhft.chronicle.bytes (net.openhft.chronicle.bytes)1 Jvm (net.openhft.chronicle.core.Jvm)1