Search in sources :

Example 1 with BoltConnectionDescriptor

use of org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor in project neo4j by neo4j.

the class BoltKernelExtension method newVersions.

private Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> newVersions(LogService logging, WorkerFactory workerFactory) {
    Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> availableVersions = new HashMap<>();
    availableVersions.put((long) BoltProtocolV1.VERSION, (channel, isEncrypted) -> {
        BoltConnectionDescriptor descriptor = new BoltConnectionDescriptor(channel.remoteAddress(), channel.localAddress());
        BoltWorker worker = workerFactory.newWorker(descriptor, channel::close);
        return new BoltProtocolV1(worker, channel, logging);
    });
    return availableVersions;
}
Also used : BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) HashMap(java.util.HashMap) BiFunction(java.util.function.BiFunction) BoltWorker(org.neo4j.bolt.v1.runtime.BoltWorker) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1)

Example 2 with BoltConnectionDescriptor

use of org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor in project neo4j by neo4j.

the class ThreadedWorkerFactory method newWorker.

@Override
public BoltWorker newWorker(BoltConnectionDescriptor connectionDescriptor, Runnable onClose) {
    BoltStateMachine machine = connector.newMachine(connectionDescriptor, onClose, clock);
    RunnableBoltWorker worker = new RunnableBoltWorker(machine, logging);
    scheduler.schedule(sessionWorker, worker, stringMap(THREAD_ID, machine.key()));
    return worker;
}
Also used : BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine)

Example 3 with BoltConnectionDescriptor

use of org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor 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 4 with BoltConnectionDescriptor

use of org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor in project neo4j by neo4j.

the class TransactionIT method shouldTerminateQueriesEvenIfUsingPeriodicCommit.

@Test
public void shouldTerminateQueriesEvenIfUsingPeriodicCommit() throws Exception {
    // Spawns a throttled HTTP server, runs a PERIODIC COMMIT that fetches data from this server,
    // and checks that the query able to be terminated
    // We start with 3, because that is how many actors we have -
    // 1. the http server
    // 2. the running query
    // 3. the one terminating 2
    final DoubleLatch latch = new DoubleLatch(3, true);
    // This is used to block the http server between the first and second batch
    final Barrier.Control barrier = new Barrier.Control();
    // Serve CSV via local web server, let Jetty find a random port for us
    Server server = createHttpServer(latch, barrier, 20, 30);
    server.start();
    int localPort = getLocalPort(server);
    final BoltStateMachine[] machine = { null };
    Thread thread = new Thread() {

        @Override
        public void run() {
            try (BoltStateMachine stateMachine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<writeServer>", 7468)))) {
                machine[0] = stateMachine;
                stateMachine.init(USER_AGENT, emptyMap(), null);
                String query = format("USING PERIODIC COMMIT 10 LOAD CSV FROM 'http://localhost:%d' AS line " + "CREATE (n:A {id: line[0], square: line[1]}) " + "WITH count(*) as number " + "CREATE (n:ShouldNotExist)", localPort);
                try {
                    latch.start();
                    stateMachine.run(query, emptyMap(), nullResponseHandler());
                    stateMachine.pullAll(nullResponseHandler());
                } finally {
                    latch.finish();
                }
            } catch (BoltConnectionFatality connectionFatality) {
                throw new RuntimeException(connectionFatality);
            }
        }
    };
    thread.setName("query runner");
    thread.start();
    // We block this thread here, waiting for the http server to spin up and the running query to get started
    latch.startAndWaitForAllToStart();
    Thread.sleep(1000);
    // This is the call that RESETs the Bolt connection and will terminate the running query
    machine[0].reset(nullResponseHandler());
    barrier.release();
    // We block again here, waiting for the running query to have been terminated, and for the server to have
    // wrapped up and finished streaming http results
    latch.finishAndWaitForAllToFinish();
    // And now we check that the last node did not get created
    try (Transaction ignored = env.graph().beginTx()) {
        assertFalse("Query was not terminated in time - nodes were created!", env.graph().findNodes(Label.label("ShouldNotExist")).hasNext());
    }
}
Also used : Server(org.eclipse.jetty.server.Server) InetSocketAddress(java.net.InetSocketAddress) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality) Barrier(org.neo4j.test.Barrier) BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) Transaction(org.neo4j.graphdb.Transaction) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 5 with BoltConnectionDescriptor

use of org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor in project neo4j by neo4j.

the class BoltConnectionIT method shouldAllowUserControlledRollbackOnExplicitTxFailure.

@Test
public void shouldAllowUserControlledRollbackOnExplicitTxFailure() throws Throwable {
    // Given whenever en explicit transaction has a failure,
    // it is more natural for drivers to see the failure, acknowledge it
    // and send a `ROLLBACK`, because that means that all failures in the
    // transaction, be they client-local or inside neo, can be handled the
    // same way by a driver.
    BoltStateMachine machine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("bolt-test", 56789), new InetSocketAddress("test-server", 7468)));
    machine.init(USER_AGENT, emptyMap(), null);
    machine.run("BEGIN", EMPTY_PARAMS, nullResponseHandler());
    machine.discardAll(nullResponseHandler());
    machine.run("CREATE (n:Victim)-[:REL]->()", EMPTY_PARAMS, nullResponseHandler());
    machine.discardAll(nullResponseHandler());
    // When I perform an action that will fail
    BoltResponseRecorder recorder = new BoltResponseRecorder();
    machine.run("this is not valid syntax", EMPTY_PARAMS, recorder);
    // Then I should see a failure
    assertThat(recorder.nextResponse(), failedWithStatus(Status.Statement.SyntaxError));
    // And when I acknowledge that failure, and roll back the transaction
    recorder.reset();
    machine.ackFailure(recorder);
    machine.run("ROLLBACK", EMPTY_PARAMS, recorder);
    // Then both operations should succeed
    assertThat(recorder.nextResponse(), succeeded());
    assertThat(recorder.nextResponse(), succeeded());
}
Also used : BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) InetSocketAddress(java.net.InetSocketAddress) BoltResponseRecorder(org.neo4j.bolt.testing.BoltResponseRecorder) Test(org.junit.Test)

Aggregations

BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)5 BoltConnectionDescriptor (org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor)4 InetSocketAddress (java.net.InetSocketAddress)3 Test (org.junit.Test)3 BoltResponseRecorder (org.neo4j.bolt.testing.BoltResponseRecorder)2 BoltConnectionFatality (org.neo4j.bolt.v1.runtime.BoltConnectionFatality)2 Transaction (org.neo4j.graphdb.Transaction)2 HashMap (java.util.HashMap)1 BiFunction (java.util.function.BiFunction)1 Server (org.eclipse.jetty.server.Server)1 BoltWorker (org.neo4j.bolt.v1.runtime.BoltWorker)1 BoltProtocolV1 (org.neo4j.bolt.v1.transport.BoltProtocolV1)1 BinaryLatch (org.neo4j.concurrent.BinaryLatch)1 Node (org.neo4j.graphdb.Node)1 Barrier (org.neo4j.test.Barrier)1 DoubleLatch (org.neo4j.test.DoubleLatch)1