Search in sources :

Example 1 with BoltConnectionFatality

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

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

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

the class BoltMatchers method verifyOneResponse.

public static void verifyOneResponse(BoltStateMachine.State initialState, ThrowingBiConsumer<BoltStateMachine, BoltResponseRecorder, BoltConnectionFatality> transition) throws AuthenticationException, BoltConnectionFatality {
    BoltStateMachine machine = newMachine(initialState);
    BoltResponseRecorder recorder = new BoltResponseRecorder();
    try {
        transition.accept(machine, recorder);
    } catch (BoltConnectionFatality connectionFatality) {
    // acceptable for invalid transitions
    }
    assertEquals(1, recorder.responseCount());
}
Also used : BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality)

Example 4 with BoltConnectionFatality

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

the class BoltMatchers method canReset.

public static Matcher<BoltStateMachine> canReset() {
    return new BaseMatcher<BoltStateMachine>() {

        @Override
        public boolean matches(final Object item) {
            final BoltStateMachine machine = (BoltStateMachine) item;
            final BoltResponseRecorder recorder = new BoltResponseRecorder();
            try {
                machine.reset(recorder);
                return recorder.responseCount() == 1 && machine.state() == READY;
            } catch (BoltConnectionFatality boltConnectionFatality) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("can reset");
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality)

Example 5 with BoltConnectionFatality

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

the class MonitoredBoltWorkerFactoryTest method shouldSignalReceivedStartAndComplete.

@Test
public void shouldSignalReceivedStartAndComplete() throws Throwable {
    // given
    FakeClock clock = Clocks.fakeClock();
    WorkerFactory delegate = mock(WorkerFactory.class);
    BoltStateMachine machine = mock(BoltStateMachine.class);
    when(delegate.newWorker(anyObject(), anyObject())).thenReturn(new BoltWorker() {

        @Override
        public void enqueue(Job job) {
            clock.forward(1337, TimeUnit.MILLISECONDS);
            try {
                job.perform(machine);
            } catch (BoltConnectionFatality connectionFatality) {
                throw new RuntimeException(connectionFatality);
            }
        }

        @Override
        public void interrupt() {
            throw new RuntimeException();
        }

        @Override
        public void halt() {
            throw new RuntimeException();
        }
    });
    Monitors monitors = new Monitors();
    CountingSessionMonitor monitor = new CountingSessionMonitor();
    monitors.addMonitorListener(monitor);
    MonitoredWorkerFactory workerFactory = new MonitoredWorkerFactory(monitors, delegate, clock);
    BoltWorker worker = workerFactory.newWorker(CONNECTION_DESCRIPTOR);
    // when
    worker.enqueue((stateMachine) -> {
        stateMachine.run("hello", null, nullResponseHandler());
        clock.forward(1338, TimeUnit.MILLISECONDS);
    });
    // then
    assertEquals(1, monitor.messagesReceived);
    assertEquals(1337, monitor.queueTime);
    assertEquals(1338, monitor.processingTime);
}
Also used : FakeClock(org.neo4j.time.FakeClock) MonitoredBoltWorker(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory.MonitoredBoltWorker) Monitors(org.neo4j.kernel.monitoring.Monitors) Test(org.junit.Test)

Aggregations

BoltConnectionFatality (org.neo4j.bolt.v1.runtime.BoltConnectionFatality)4 BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)4 Test (org.junit.Test)3 InetSocketAddress (java.net.InetSocketAddress)2 BoltConnectionDescriptor (org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor)2 Transaction (org.neo4j.graphdb.Transaction)2 Server (org.eclipse.jetty.server.Server)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1 BoltResponseRecorder (org.neo4j.bolt.testing.BoltResponseRecorder)1 MonitoredBoltWorker (org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory.MonitoredBoltWorker)1 BinaryLatch (org.neo4j.concurrent.BinaryLatch)1 Node (org.neo4j.graphdb.Node)1 Monitors (org.neo4j.kernel.monitoring.Monitors)1 Barrier (org.neo4j.test.Barrier)1 DoubleLatch (org.neo4j.test.DoubleLatch)1 FakeClock (org.neo4j.time.FakeClock)1