use of org.neo4j.bolt.v1.runtime.WorkerFactory in project neo4j by neo4j.
the class MonitoredBoltWorkerFactoryTest method shouldReportStartedSessions.
@Test
public void shouldReportStartedSessions() {
int workersCount = 42;
Monitors monitors = new Monitors();
CountingSessionMonitor monitor = new CountingSessionMonitor();
monitors.addMonitorListener(monitor);
WorkerFactory mockWorkers = mock(WorkerFactory.class);
when(mockWorkers.newWorker(anyObject(), any())).thenReturn(mock(BoltWorker.class));
MonitoredWorkerFactory workerFactory = new MonitoredWorkerFactory(monitors, mockWorkers, systemClock());
for (int i = 0; i < workersCount; i++) {
workerFactory.newWorker(CONNECTION_DESCRIPTOR);
}
assertEquals(workersCount, monitor.sessionsStarted);
}
use of org.neo4j.bolt.v1.runtime.WorkerFactory in project neo4j by neo4j.
the class MonitoredBoltWorkerFactoryTest method shouldNotWrapWithMonitoredSessionIfNobodyIsListening.
@Test
public void shouldNotWrapWithMonitoredSessionIfNobodyIsListening() throws Throwable {
// Given
// Monitoring adds GC overhead, so we only want to do the work involved
// if someone has actually registered a listener. We still allow plugging
// monitoring in at runtime, but it will only apply to sessions started
// after monitor listeners are added
WorkerFactory workerFactory = mock(WorkerFactory.class);
BoltWorker innerSession = mock(BoltWorker.class);
when(workerFactory.newWorker(anyObject(), anyObject())).thenReturn(innerSession);
Monitors monitors = new Monitors();
MonitoredWorkerFactory monitoredWorkerFactory = new MonitoredWorkerFactory(monitors, workerFactory, Clocks.fakeClock());
// When
BoltWorker worker = monitoredWorkerFactory.newWorker(CONNECTION_DESCRIPTOR);
// Then
assertEquals(innerSession, worker);
// But when I register a listener
monitors.addMonitorListener(new CountingSessionMonitor());
// Then new sessions should be monitored
assertThat(monitoredWorkerFactory.newWorker(CONNECTION_DESCRIPTOR), instanceOf(MonitoredBoltWorker.class));
}
use of org.neo4j.bolt.v1.runtime.WorkerFactory in project neo4j by neo4j.
the class BoltKernelExtension method newInstance.
@Override
public Lifecycle newInstance(KernelContext context, Dependencies dependencies) throws Throwable {
Config config = dependencies.config();
GraphDatabaseService gdb = dependencies.db();
GraphDatabaseAPI api = (GraphDatabaseAPI) gdb;
LogService logService = dependencies.logService();
Clock clock = dependencies.clock();
Log log = logService.getInternalLog(WorkerFactory.class);
LifeSupport life = new LifeSupport();
JobScheduler scheduler = dependencies.scheduler();
InternalLoggerFactory.setDefaultFactory(new Netty4LoggerFactory(logService.getInternalLogProvider()));
Authentication authentication = authentication(dependencies.authManager(), dependencies.userManagerSupplier());
BoltFactory boltFactory = life.add(new BoltFactoryImpl(api, dependencies.usageData(), logService, dependencies.txBridge(), authentication, dependencies.sessionTracker(), config));
WorkerFactory workerFactory = createWorkerFactory(boltFactory, scheduler, dependencies, logService, clock);
List<ProtocolInitializer> connectors = config.enabledBoltConnectors().stream().map((connConfig) -> {
ListenSocketAddress listenAddress = config.get(connConfig.listen_address);
AdvertisedSocketAddress advertisedAddress = config.get(connConfig.advertised_address);
SslContext sslCtx;
boolean requireEncryption;
final BoltConnector.EncryptionLevel encryptionLevel = config.get(connConfig.encryption_level);
switch(encryptionLevel) {
case REQUIRED:
// Encrypted connections are mandatory, a self-signed certificate may be generated.
requireEncryption = true;
sslCtx = createSslContext(config, log, advertisedAddress);
break;
case OPTIONAL:
// Encrypted connections are optional, a self-signed certificate may be generated.
requireEncryption = false;
sslCtx = createSslContext(config, log, advertisedAddress);
break;
case DISABLED:
// Encryption is turned off, no self-signed certificate will be generated.
requireEncryption = false;
sslCtx = null;
break;
default:
// In the unlikely event that we happen to fall through to the default option here,
// there is a mismatch between the BoltConnector.EncryptionLevel enum and the options
// handled in this switch statement. In this case, we'll log a warning and default to
// disabling encryption, since this mirrors the functionality introduced in 3.0.
log.warn(format("Unhandled encryption level %s - assuming DISABLED.", encryptionLevel.name()));
requireEncryption = false;
sslCtx = null;
break;
}
final Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> versions = newVersions(logService, workerFactory);
return new SocketTransport(listenAddress, sslCtx, requireEncryption, logService.getInternalLogProvider(), versions);
}).collect(toList());
if (connectors.size() > 0 && !config.get(GraphDatabaseSettings.disconnected)) {
life.add(new NettyServer(scheduler.threadFactory(boltNetworkIO), connectors));
log.info("Bolt Server extension loaded.");
for (ProtocolInitializer connector : connectors) {
logService.getUserLog(WorkerFactory.class).info("Bolt enabled on %s.", connector.address());
}
}
return life;
}
use of org.neo4j.bolt.v1.runtime.WorkerFactory 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;
}
use of org.neo4j.bolt.v1.runtime.WorkerFactory in project neo4j by neo4j.
the class BoltFailuresIT method throwsWhenWorkerCreationFails.
@Test(timeout = TEST_TIMEOUT)
public void throwsWhenWorkerCreationFails() {
WorkerFactory workerFactory = mock(WorkerFactory.class);
when(workerFactory.newWorker(anyObject(), any())).thenThrow(new IllegalStateException("Oh!"));
BoltKernelExtension extension = new BoltKernelExtensionWithWorkerFactory(workerFactory);
db = startDbWithBolt(new GraphDatabaseFactoryWithCustomBoltKernelExtension(extension));
driver = createDriver();
try {
driver.session();
fail("Exception expected");
} catch (Exception e) {
assertThat(e, instanceOf(ConnectionFailureException.class));
}
}
Aggregations