Search in sources :

Example 1 with BinaryLatch

use of org.neo4j.concurrent.BinaryLatch in project neo4j by neo4j.

the class TransactionIT method shouldReadYourOwnWrites.

@Test
public void shouldReadYourOwnWrites() throws Exception {
    try (Transaction tx = env.graph().beginTx()) {
        Node node = env.graph().createNode(Label.label("A"));
        node.setProperty("prop", "one");
        tx.success();
    }
    BinaryLatch latch = new BinaryLatch();
    long dbVersion = env.lastClosedTxId();
    Thread thread = new Thread() {

        @Override
        public void run() {
            try (BoltStateMachine machine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<writeServer>", 7468)))) {
                machine.init(USER_AGENT, emptyMap(), null);
                latch.await();
                machine.run("MATCH (n:A) SET n.prop = 'two'", emptyMap(), nullResponseHandler());
                machine.pullAll(nullResponseHandler());
            } catch (BoltConnectionFatality connectionFatality) {
                throw new RuntimeException(connectionFatality);
            }
        }
    };
    thread.start();
    long dbVersionAfterWrite = dbVersion + 1;
    try (BoltStateMachine machine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<readServer>", 7468)))) {
        BoltResponseRecorder recorder = new BoltResponseRecorder();
        machine.init(USER_AGENT, emptyMap(), null);
        latch.release();
        final String bookmark = "neo4j:bookmark:v1:tx" + Long.toString(dbVersionAfterWrite);
        machine.run("BEGIN", singletonMap("bookmark", bookmark), nullResponseHandler());
        machine.pullAll(recorder);
        machine.run("MATCH (n:A) RETURN n.prop", emptyMap(), nullResponseHandler());
        machine.pullAll(recorder);
        machine.run("COMMIT", emptyMap(), nullResponseHandler());
        machine.pullAll(recorder);
        assertThat(recorder.nextResponse(), succeededWithMetadata("bookmark", BOOKMARK_PATTERN));
        assertThat(recorder.nextResponse(), succeededWithRecord("two"));
        assertThat(recorder.nextResponse(), succeededWithMetadata("bookmark", BOOKMARK_PATTERN));
    }
    thread.join();
}
Also used : BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) Transaction(org.neo4j.graphdb.Transaction) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) InetSocketAddress(java.net.InetSocketAddress) Node(org.neo4j.graphdb.Node) BoltResponseRecorder(org.neo4j.bolt.testing.BoltResponseRecorder) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality) BinaryLatch(org.neo4j.concurrent.BinaryLatch) Test(org.junit.Test)

Example 2 with BinaryLatch

use of org.neo4j.concurrent.BinaryLatch in project neo4j by neo4j.

the class MuninnPageCursor method initiatePageFault.

private Object initiatePageFault(long filePageId, long chunkOffset, Object[] chunk) throws IOException {
    BinaryLatch latch = new BinaryLatch();
    Object item = null;
    if (UnsafeUtil.compareAndSwapObject(chunk, chunkOffset, null, latch)) {
        // We managed to inject our latch, so we now own the right to perform the page fault. We also
        // have a duty to eventually release and remove the latch, no matter what happens now.
        item = pageFault(filePageId, swapper, chunkOffset, chunk, latch);
    }
    return item;
}
Also used : BinaryLatch(org.neo4j.concurrent.BinaryLatch)

Example 3 with BinaryLatch

use of org.neo4j.concurrent.BinaryLatch in project neo4j by neo4j.

the class SlaveUpdatePuller method start.

@Override
public void start() {
    if (shutdownLatch != null) {
        // This SlaveUpdatePuller has already been initialised
        return;
    }
    shutdownLatch = new BinaryLatch();
    JobHandle handle = jobScheduler.schedule(JobScheduler.Groups.pullUpdates, this);
    handle.registerCancelListener(this);
}
Also used : JobHandle(org.neo4j.kernel.impl.util.JobScheduler.JobHandle) BinaryLatch(org.neo4j.concurrent.BinaryLatch)

Example 4 with BinaryLatch

use of org.neo4j.concurrent.BinaryLatch in project neo4j by neo4j.

the class MuninnPageCursor method awaitPageFault.

private Object awaitPageFault(Object item) {
    BinaryLatch latch = (BinaryLatch) item;
    latch.await();
    return null;
}
Also used : BinaryLatch(org.neo4j.concurrent.BinaryLatch)

Example 5 with BinaryLatch

use of org.neo4j.concurrent.BinaryLatch in project neo4j by neo4j.

the class PageCacheTest method writeUnlockMustInvalidateReadLocks.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void writeUnlockMustInvalidateReadLocks() throws Exception {
    configureStandardPageCache();
    BinaryLatch startLatch = new BinaryLatch();
    BinaryLatch continueLatch = new BinaryLatch();
    try (PagedFile pf = pageCache.map(existingFile("a"), filePageSize);
        PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK)) {
        // Lock page 0
        assertTrue(cursor.next());
        Future<Object> read = executor.submit(() -> {
            try (PageCursor innerCursor = pf.io(0, PF_SHARED_READ_LOCK)) {
                assertTrue(innerCursor.next());
                assertTrue(innerCursor.shouldRetry());
                startLatch.release();
                continueLatch.await();
                assertTrue(innerCursor.shouldRetry());
            }
            return null;
        });
        startLatch.await();
        // Unlock page 0
        assertTrue(cursor.next());
        continueLatch.release();
        read.get();
    }
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) BinaryLatch(org.neo4j.concurrent.BinaryLatch) Test(org.junit.Test)

Aggregations

BinaryLatch (org.neo4j.concurrent.BinaryLatch)9 Test (org.junit.Test)3 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)2 Flushable (java.io.Flushable)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 BoltResponseRecorder (org.neo4j.bolt.testing.BoltResponseRecorder)1 BoltConnectionDescriptor (org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor)1 BoltConnectionFatality (org.neo4j.bolt.v1.runtime.BoltConnectionFatality)1 BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)1 Node (org.neo4j.graphdb.Node)1 Transaction (org.neo4j.graphdb.Transaction)1 IOLimiter (org.neo4j.io.pagecache.IOLimiter)1 JobHandle (org.neo4j.kernel.impl.util.JobScheduler.JobHandle)1