Search in sources :

Example 11 with Flushable

use of java.io.Flushable in project neo4j by neo4j.

the class PageCacheTest method flushAndForceMustNotLockPageCacheForWholeDuration.

@Test
void flushAndForceMustNotLockPageCacheForWholeDuration() {
    assertTimeoutPreemptively(ofMillis(SHORT_TIMEOUT_MILLIS), () -> {
        maxPages = 5000;
        configureStandardPageCache();
        Path a = existingFile("a");
        Path b = existingFile("b");
        BinaryLatch limiterStartLatch = new BinaryLatch();
        BinaryLatch limiterBlockLatch = new BinaryLatch();
        var ioController = new EmptyIOController() {

            @Override
            public void maybeLimitIO(int recentlyCompletedIOs, Flushable flushable, MajorFlushEvent flushEvent) {
                limiterStartLatch.release();
                limiterBlockLatch.await();
                super.maybeLimitIO(recentlyCompletedIOs, flushable, flushEvent);
            }
        };
        try (PagedFile pfA = pageCache.map(a, filePageSize, DEFAULT_DATABASE_NAME, immutable.empty(), ioController)) {
            // Dirty a bunch of pages.
            try (PageCursor cursor = pfA.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
                for (int i = 0; i < maxPages; i++) {
                    assertTrue(cursor.next());
                }
            }
            Future<?> flusher = executor.submit(() -> {
                pfA.flushAndForce();
                return null;
            });
            // Flusher is now stuck inside flushAndForce.
            limiterStartLatch.await();
            // We should be able to map and close paged files.
            map(pageCache, b, filePageSize).close();
            // We should be able to get and list existing mappings.
            pageCache.listExistingMappings();
            pageCache.getExistingMapping(a).ifPresent(PagedFile::close);
            limiterBlockLatch.release();
            flusher.get();
        }
    });
}
Also used : Path(java.nio.file.Path) MajorFlushEvent(org.neo4j.io.pagecache.tracing.MajorFlushEvent) Flushable(java.io.Flushable) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 12 with Flushable

use of java.io.Flushable in project neo4j by neo4j.

the class TransactionLogFile method force.

private void force() throws IOException {
    // Empty buffer into writer. We want to synchronize with appenders somehow so that they
    // don't append while we're doing that. The way rotation is coordinated we can't synchronize
    // on logFile because it would cause deadlocks. Synchronizing on writer assumes that appenders
    // also synchronize on writer.
    Flushable flushable;
    synchronized (this) {
        databaseHealth.assertHealthy(IOException.class);
        flushable = writer.prepareForFlush();
    }
    // This allows other threads access to the buffer while the writer is being forced.
    try {
        flushable.flush();
    } catch (ClosedChannelException ignored) {
    // This is ok, we were already successful in emptying the buffer, so the channel being closed here means
    // that some other thread is rotating the log and has closed the underlying channel. But since we were
    // successful in emptying the buffer *UNDER THE LOCK* we know that the rotating thread included the changes
    // we emptied into the channel, and thus it is already flushed by that thread.
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Flushable(java.io.Flushable)

Aggregations

Flushable (java.io.Flushable)12 Test (org.junit.Test)4 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Path (java.nio.file.Path)2 RepeatedTest (org.junit.jupiter.api.RepeatedTest)2 MajorFlushEvent (org.neo4j.io.pagecache.tracing.MajorFlushEvent)2 BinaryLatch (org.neo4j.util.concurrent.BinaryLatch)2 SoyDict (com.google.template.soy.data.SoyDict)1 SoyRecord (com.google.template.soy.data.SoyRecord)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1 Closeable (java.io.Closeable)1 File (java.io.File)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 RemoteException (java.rmi.RemoteException)1 Formatter (java.util.Formatter)1 FormatterClosedException (java.util.FormatterClosedException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1