Search in sources :

Example 1 with Lifecycle

use of org.neo4j.kernel.lifecycle.Lifecycle in project neo4j by neo4j.

the class HighAvailabilitySlavesTest method shouldSupportConcurrentConsumptionOfSlaves.

@Test
public void shouldSupportConcurrentConsumptionOfSlaves() throws Exception {
    // Given
    LogEntryReader<ReadableClosablePositionAwareChannel> logEntryReader = new VersionAwareLogEntryReader<>();
    HighAvailabilitySlaves haSlaves = new HighAvailabilitySlaves(clusterMembersOfSize(1000), mock(Cluster.class), new DefaultSlaveFactory(NullLogProvider.getInstance(), new Monitors(), 42, Suppliers.singleton(logEntryReader)), new HostnamePort(null, 0));
    // When
    ExecutorService executor = Executors.newFixedThreadPool(5);
    for (int i = 0; i < 5; i++) {
        executor.submit(slavesConsumingRunnable(haSlaves));
    }
    executor.shutdown();
    executor.awaitTermination(30, SECONDS);
    // Then
    int slavesCount = 0;
    LifeSupport life = ReflectionUtil.getPrivateField(haSlaves, "life", LifeSupport.class);
    for (Lifecycle lifecycle : life.getLifecycleInstances()) {
        if (lifecycle instanceof Slave) {
            slavesCount++;
        }
    }
    // One instance is master
    assertEquals("Unexpected number of slaves", 1000 - 1, slavesCount);
}
Also used : HostnamePort(org.neo4j.helpers.HostnamePort) Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) Slave(org.neo4j.kernel.ha.com.master.Slave) Monitors(org.neo4j.kernel.monitoring.Monitors) ExecutorService(java.util.concurrent.ExecutorService) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) DefaultSlaveFactory(org.neo4j.kernel.ha.com.master.DefaultSlaveFactory) Test(org.junit.Test)

Example 2 with Lifecycle

use of org.neo4j.kernel.lifecycle.Lifecycle 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;
}
Also used : Service(org.neo4j.helpers.Service) UsageData(org.neo4j.udc.UsageData) Log(org.neo4j.logging.Log) Authentication(org.neo4j.bolt.security.auth.Authentication) BiFunction(java.util.function.BiFunction) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KernelContext(org.neo4j.kernel.impl.spi.KernelContext) SocketTransport(org.neo4j.bolt.transport.SocketTransport) BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) KeyStoreFactory(org.neo4j.bolt.security.ssl.KeyStoreFactory) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) GeneralSecurityException(java.security.GeneralSecurityException) ProtocolInitializer(org.neo4j.bolt.transport.NettyServer.ProtocolInitializer) Map(java.util.Map) KeyStoreInformation(org.neo4j.bolt.security.ssl.KeyStoreInformation) Groups.boltNetworkIO(org.neo4j.kernel.impl.util.JobScheduler.Groups.boltNetworkIO) BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) BoltFactory(org.neo4j.bolt.v1.runtime.BoltFactory) BoltConnector(org.neo4j.kernel.configuration.BoltConnector) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) LogService(org.neo4j.kernel.impl.logging.LogService) String.format(java.lang.String.format) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Netty4LoggerFactory(org.neo4j.bolt.transport.Netty4LoggerFactory) Settings.derivedSetting(org.neo4j.kernel.configuration.Settings.derivedSetting) List(java.util.List) Description(org.neo4j.configuration.Description) BoltFactoryImpl(org.neo4j.bolt.v1.runtime.BoltFactoryImpl) KernelExtensionFactory(org.neo4j.kernel.extension.KernelExtensionFactory) WorkerFactory(org.neo4j.bolt.v1.runtime.WorkerFactory) BasicAuthentication(org.neo4j.bolt.security.auth.BasicAuthentication) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Settings.pathSetting(org.neo4j.kernel.configuration.Settings.pathSetting) Internal(org.neo4j.configuration.Internal) Monitors(org.neo4j.kernel.monitoring.Monitors) HashMap(java.util.HashMap) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Configuration(org.neo4j.graphdb.config.Configuration) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1) Certificates(org.neo4j.bolt.security.ssl.Certificates) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NettyServer(org.neo4j.bolt.transport.NettyServer) ThreadedWorkerFactory(org.neo4j.bolt.v1.runtime.concurrent.ThreadedWorkerFactory) Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle) Config(org.neo4j.kernel.configuration.Config) SslContext(io.netty.handler.ssl.SslContext) BoltConnectionTracker(org.neo4j.kernel.api.bolt.BoltConnectionTracker) Setting(org.neo4j.graphdb.config.Setting) IOException(java.io.IOException) PATH(org.neo4j.kernel.configuration.Settings.PATH) File(java.io.File) Channel(io.netty.channel.Channel) UserManagerSupplier(org.neo4j.kernel.api.security.UserManagerSupplier) BoltWorker(org.neo4j.bolt.v1.runtime.BoltWorker) Collectors.toList(java.util.stream.Collectors.toList) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) Clock(java.time.Clock) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) MonitoredWorkerFactory(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory) AuthManager(org.neo4j.kernel.api.security.AuthManager) Config(org.neo4j.kernel.configuration.Config) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) Clock(java.time.Clock) BoltFactory(org.neo4j.bolt.v1.runtime.BoltFactory) WorkerFactory(org.neo4j.bolt.v1.runtime.WorkerFactory) ThreadedWorkerFactory(org.neo4j.bolt.v1.runtime.concurrent.ThreadedWorkerFactory) MonitoredWorkerFactory(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory) NettyServer(org.neo4j.bolt.transport.NettyServer) BoltFactoryImpl(org.neo4j.bolt.v1.runtime.BoltFactoryImpl) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) SslContext(io.netty.handler.ssl.SslContext) ProtocolInitializer(org.neo4j.bolt.transport.NettyServer.ProtocolInitializer) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Log(org.neo4j.logging.Log) SocketTransport(org.neo4j.bolt.transport.SocketTransport) Channel(io.netty.channel.Channel) BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) Authentication(org.neo4j.bolt.security.auth.Authentication) BasicAuthentication(org.neo4j.bolt.security.auth.BasicAuthentication) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) Netty4LoggerFactory(org.neo4j.bolt.transport.Netty4LoggerFactory) Map(java.util.Map) HashMap(java.util.HashMap) LogService(org.neo4j.kernel.impl.logging.LogService)

Example 3 with Lifecycle

use of org.neo4j.kernel.lifecycle.Lifecycle in project neo4j by neo4j.

the class KernelExtensions method init.

@Override
public void init() throws Throwable {
    for (KernelExtensionFactory kernelExtensionFactory : kernelExtensionFactories) {
        Object kernelExtensionDependencies = getKernelExtensionDependencies(kernelExtensionFactory);
        try {
            Lifecycle dependency = kernelExtensionFactory.newInstance(kernelContext, kernelExtensionDependencies);
            Objects.requireNonNull(dependency, kernelExtensionFactory.toString() + " returned a null " + "KernelExtension");
            life.add(dependencies.satisfyDependency(dependency));
        } catch (UnsatisfiedDependencyException e) {
            unsatisfiedDepencyStrategy.handle(kernelExtensionFactory, e);
        }
    }
    life.init();
}
Also used : UnsatisfiedDependencyException(org.neo4j.kernel.impl.util.UnsatisfiedDependencyException) Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle)

Example 4 with Lifecycle

use of org.neo4j.kernel.lifecycle.Lifecycle in project neo4j by neo4j.

the class AbstractExtensions method init.

@Override
public void init() {
    for (ExtensionFactory<?> extensionFactory : extensionFactories) {
        try {
            Object extensionDependencies = getExtensionDependencies(extensionFactory);
            Lifecycle dependency = newInstance(extensionContext, extensionFactory, extensionDependencies);
            Objects.requireNonNull(dependency, extensionFactory + " returned a null extension.");
            life.add(dependencies.satisfyDependency(dependency));
        } catch (UnsatisfiedDependencyException exception) {
            extensionFailureStrategy.handle(extensionFactory, exception);
        } catch (Throwable throwable) {
            extensionFailureStrategy.handle(extensionFactory, throwable);
        }
    }
    life.init();
}
Also used : UnsatisfiedDependencyException(org.neo4j.exceptions.UnsatisfiedDependencyException) Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle)

Example 5 with Lifecycle

use of org.neo4j.kernel.lifecycle.Lifecycle in project neo4j by neo4j.

the class TransactionLogsRecoveryTest method shouldInitSchemaLifeWhenRecoveryNotRequired.

@Test
void shouldInitSchemaLifeWhenRecoveryNotRequired() throws Exception {
    Lifecycle schemaLife = mock(Lifecycle.class);
    RecoveryService recoveryService = mock(RecoveryService.class);
    when(recoveryService.getRecoveryStartInformation()).thenReturn(NO_RECOVERY_REQUIRED);
    CorruptedLogsTruncator logPruner = new CorruptedLogsTruncator(storeDir, logFiles, fileSystem, INSTANCE);
    RecoveryMonitor monitor = mock(RecoveryMonitor.class);
    TransactionLogsRecovery logsRecovery = new TransactionLogsRecovery(recoveryService, logPruner, schemaLife, monitor, ProgressReporter.SILENT, true, EMPTY_CHECKER, NULL);
    logsRecovery.init();
    verify(monitor, never()).recoveryRequired(any());
    verify(schemaLife).init();
}
Also used : Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Lifecycle (org.neo4j.kernel.lifecycle.Lifecycle)8 ExecutorService (java.util.concurrent.ExecutorService)2 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)2 Monitors (org.neo4j.kernel.monitoring.Monitors)2 Channel (io.netty.channel.Channel)1 SslContext (io.netty.handler.ssl.SslContext)1 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)1 InternalLoggerFactory (io.netty.util.internal.logging.InternalLoggerFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 URI (java.net.URI)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1