Search in sources :

Example 6 with BoltChannel

use of org.neo4j.bolt.BoltChannel in project neo4j by neo4j.

the class DefaultBoltProtocolFactoryTest method shouldCreateBoltProtocol.

@ParameterizedTest(name = "V{0}.{1}")
@CsvSource({ "3, 0", "4, 0", "4, 1", "4, 2", "4, 3" })
void shouldCreateBoltProtocol(int majorVersion, int minorVersion) throws Throwable {
    EmbeddedChannel channel = new EmbeddedChannel();
    BoltChannel boltChannel = newTestBoltChannel(channel);
    BoltProtocolVersion boltProtocolVersion = new BoltProtocolVersion(majorVersion, minorVersion);
    BoltStateMachineFactory stateMachineFactory = mock(BoltStateMachineFactory.class);
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    var channelProtector = mock(ChannelProtector.class);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    when(stateMachineFactory.newStateMachine(boltProtocolVersion, boltChannel, MapValue.EMPTY, memoryTracker)).thenReturn(stateMachine);
    BoltConnectionFactory connectionFactory = mock(BoltConnectionFactory.class);
    BoltConnection connection = mock(BoltConnection.class);
    when(connectionFactory.newConnection(eq(boltChannel), eq(stateMachine), any())).thenReturn(connection);
    BoltProtocolFactory factory = new DefaultBoltProtocolFactory(connectionFactory, stateMachineFactory, Config.defaults(), NullLogService.getInstance(), new TestDatabaseIdRepository(), CustomBookmarkFormatParser.DEFAULT, mock(TransportThrottleGroup.class), Clocks.fakeClock(), Duration.ZERO);
    BoltProtocol protocol = factory.create(boltProtocolVersion, boltChannel, channelProtector, memoryTracker);
    protocol.install();
    // handler with correct version is created
    assertEquals(boltProtocolVersion, protocol.version());
    // it uses the expected worker
    verify(connectionFactory).newConnection(eq(boltChannel), any(BoltStateMachine.class), any(BoltResponseMessageWriter.class));
    verify(memoryTracker, times(5)).allocateHeap(anyLong());
    // and halts this same worker when closed
    verify(connection, never()).stop();
    channel.close();
    verify(connection).stop();
    channel.finishAndReleaseAll();
}
Also used : BoltResponseMessageWriter(org.neo4j.bolt.messaging.BoltResponseMessageWriter) BoltConnectionFactory(org.neo4j.bolt.runtime.BoltConnectionFactory) BoltStateMachineFactory(org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TestDatabaseIdRepository(org.neo4j.kernel.database.TestDatabaseIdRepository) BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) BoltChannel(org.neo4j.bolt.BoltChannel) BoltTestUtil.newTestBoltChannel(org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with BoltChannel

use of org.neo4j.bolt.BoltChannel in project neo4j by neo4j.

the class SocketTransport method channelInitializer.

@Override
public ChannelInitializer<Channel> channelInitializer() {
    return new ChannelInitializer<>() {

        @Override
        public void initChannel(Channel ch) {
            ch.config().setAllocator(allocator);
            var memoryTracker = new LocalMemoryTracker(memoryPool, 0, 64, null);
            ch.closeFuture().addListener(future -> memoryTracker.close());
            memoryTracker.allocateHeap(HeapEstimator.sizeOf(ch));
            memoryTracker.allocateHeap(UnauthenticatedChannelProtector.SHALLOW_SIZE + BoltChannel.SHALLOW_SIZE);
            var channelProtector = new UnauthenticatedChannelProtector(ch, channelTimeout, maxMessageSize, memoryTracker);
            BoltChannel boltChannel = newBoltChannel(ch, channelProtector, memoryTracker);
            connectionTracker.add(boltChannel);
            ch.closeFuture().addListener(future -> connectionTracker.remove(boltChannel));
            // install throttles
            throttleGroup.install(ch, memoryTracker);
            // add a close listener that will uninstall throttles
            ch.closeFuture().addListener(future -> throttleGroup.uninstall(ch));
            memoryTracker.allocateHeap(TransportSelectionHandler.SHALLOW_SIZE);
            TransportSelectionHandler transportSelectionHandler = new TransportSelectionHandler(boltChannel, sslCtx, encryptionRequired, false, logging, boltProtocolFactory, channelProtector, memoryTracker);
            ch.pipeline().addLast(transportSelectionHandler);
        }
    };
}
Also used : UnauthenticatedChannelProtector(org.neo4j.bolt.transport.pipeline.UnauthenticatedChannelProtector) BoltChannel(org.neo4j.bolt.BoltChannel) Channel(io.netty.channel.Channel) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) BoltChannel(org.neo4j.bolt.BoltChannel) ChannelInitializer(io.netty.channel.ChannelInitializer)

Example 8 with BoltChannel

use of org.neo4j.bolt.BoltChannel in project neo4j by neo4j.

the class DefaultBoltProtocolFactoryTest method shouldCreateNothingForUnknownProtocolVersion.

@Test
void shouldCreateNothingForUnknownProtocolVersion() {
    int protocolVersion = 42;
    BoltChannel channel = newTestBoltChannel();
    BoltProtocolFactory factory = new DefaultBoltProtocolFactory(mock(BoltConnectionFactory.class), mock(BoltStateMachineFactory.class), Config.defaults(), NullLogService.getInstance(), new TestDatabaseIdRepository(), CustomBookmarkFormatParser.DEFAULT, mock(TransportThrottleGroup.class), Clocks.fakeClock(), Duration.ZERO);
    BoltProtocol protocol = factory.create(new BoltProtocolVersion(protocolVersion, 0), channel, mock(ChannelProtector.class), mock(MemoryTracker.class));
    // handler is not created
    assertNull(protocol);
}
Also used : ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) BoltConnectionFactory(org.neo4j.bolt.runtime.BoltConnectionFactory) BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltStateMachineFactory(org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) BoltChannel(org.neo4j.bolt.BoltChannel) BoltTestUtil.newTestBoltChannel(org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel) TestDatabaseIdRepository(org.neo4j.kernel.database.TestDatabaseIdRepository) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with BoltChannel

use of org.neo4j.bolt.BoltChannel in project neo4j by neo4j.

the class DefaultBoltConnectionFactoryTest method shouldInstallKeepAliveHandler.

@Test
void shouldInstallKeepAliveHandler() {
    var schedulerProvider = mock(BoltSchedulerProvider.class);
    var config = Config.newBuilder().set(BoltConnectorInternalSettings.connection_keep_alive_type, BoltConnectorInternalSettings.KeepAliveRequestType.ALL).build();
    var logService = mock(LogService.class);
    var pipeline = mock(ChannelPipeline.class);
    var channel = mock(Channel.class, RETURNS_MOCKS);
    var protector = mock(ChannelProtector.class);
    var boltChannel = new BoltChannel("bolt123", "joffrey", channel, protector);
    var stateMachine = mock(BoltStateMachine.class);
    var messageWriter = mock(BoltResponseMessageWriter.class);
    when(channel.pipeline()).thenReturn(pipeline);
    var connectionFactory = new DefaultBoltConnectionFactory(schedulerProvider, config, logService, Clock.systemUTC(), new Monitors());
    connectionFactory.newConnection(boltChannel, stateMachine, messageWriter);
    verify(channel).pipeline();
    verify(pipeline).addLast(Mockito.any(KeepAliveHandler.class));
    verifyNoMoreInteractions(pipeline);
}
Also used : BoltChannel(org.neo4j.bolt.BoltChannel) Monitors(org.neo4j.monitoring.Monitors) KeepAliveHandler(org.neo4j.bolt.transport.pipeline.KeepAliveHandler) Test(org.junit.jupiter.api.Test)

Example 10 with BoltChannel

use of org.neo4j.bolt.BoltChannel in project neo4j by neo4j.

the class BoltStateMachineV4Test method shouldSetPendingErrorOnMarkFailedIfNoHandler.

@Test
void shouldSetPendingErrorOnMarkFailedIfNoHandler() {
    BoltStateMachineSPIImpl spi = mock(BoltStateMachineSPIImpl.class);
    BoltChannel boltChannel = mock(BoltChannel.class);
    var memoryTracker = mock(MemoryTracker.class);
    BoltStateMachine machine = new BoltStateMachineV4(spi, boltChannel, Clock.systemUTC(), mock(DefaultDatabaseResolver.class), MapValue.EMPTY, memoryTracker);
    Neo4jError error = Neo4jError.from(Status.Request.NoThreadsAvailable, "no threads");
    machine.markFailed(error);
    assertEquals(error, pendingError(machine));
    assertThat(machine).satisfies(inState(FailedState.class));
}
Also used : Neo4jError(org.neo4j.bolt.runtime.Neo4jError) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltChannel(org.neo4j.bolt.BoltChannel) BoltStateMachineV4(org.neo4j.bolt.v4.BoltStateMachineV4) DefaultDatabaseResolver(org.neo4j.kernel.database.DefaultDatabaseResolver) FailedState(org.neo4j.bolt.v4.runtime.FailedState) Test(org.junit.jupiter.api.Test)

Aggregations

BoltChannel (org.neo4j.bolt.BoltChannel)10 Test (org.junit.jupiter.api.Test)6 BoltStateMachine (org.neo4j.bolt.runtime.statemachine.BoltStateMachine)5 BoltProtocol (org.neo4j.bolt.BoltProtocol)4 BoltTestUtil.newTestBoltChannel (org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel)4 BoltConnectionFactory (org.neo4j.bolt.runtime.BoltConnectionFactory)3 BoltStateMachineFactory (org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory)3 ChannelProtector (org.neo4j.bolt.transport.pipeline.ChannelProtector)3 BoltStateMachineV4 (org.neo4j.bolt.v4.BoltStateMachineV4)3 DefaultDatabaseResolver (org.neo4j.kernel.database.DefaultDatabaseResolver)3 Channel (io.netty.channel.Channel)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 BoltProtocolVersion (org.neo4j.bolt.BoltProtocolVersion)2 BoltConnection (org.neo4j.bolt.runtime.BoltConnection)2 TestDatabaseIdRepository (org.neo4j.kernel.database.TestDatabaseIdRepository)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1 BoltResponseMessageWriter (org.neo4j.bolt.messaging.BoltResponseMessageWriter)1