Search in sources :

Example 1 with LifecycleAdapter

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

the class QueryLoggerKernelExtension method newInstance.

@Override
public Lifecycle newInstance(@SuppressWarnings("unused") KernelContext context, final Dependencies dependencies) throws Throwable {
    final Config config = dependencies.config();
    boolean queryLogEnabled = config.get(GraphDatabaseSettings.log_queries);
    final File queryLogFile = config.get(GraphDatabaseSettings.log_queries_filename);
    final FileSystemAbstraction fileSystem = dependencies.fileSystem();
    final JobScheduler jobScheduler = dependencies.jobScheduler();
    final Monitors monitoring = dependencies.monitoring();
    if (!queryLogEnabled) {
        return createEmptyAdapter();
    }
    return new LifecycleAdapter() {

        Closeable closable;

        @Override
        public void init() throws Throwable {
            Long thresholdMillis = config.get(GraphDatabaseSettings.log_queries_threshold);
            Long rotationThreshold = config.get(GraphDatabaseSettings.log_queries_rotation_threshold);
            int maxArchives = config.get(GraphDatabaseSettings.log_queries_max_archives);
            boolean logQueryParameters = config.get(GraphDatabaseSettings.log_queries_parameter_logging_enabled);
            FormattedLog.Builder logBuilder = FormattedLog.withUTCTimeZone();
            Log log;
            if (rotationThreshold == 0) {
                OutputStream logOutputStream = createOrOpenAsOuputStream(fileSystem, queryLogFile, true);
                log = logBuilder.toOutputStream(logOutputStream);
                closable = logOutputStream;
            } else {
                RotatingFileOutputStreamSupplier rotatingSupplier = new RotatingFileOutputStreamSupplier(fileSystem, queryLogFile, rotationThreshold, 0, maxArchives, jobScheduler.executor(JobScheduler.Groups.queryLogRotation));
                log = logBuilder.toOutputStream(rotatingSupplier);
                closable = rotatingSupplier;
            }
            QueryLogger logger = new QueryLogger(Clocks.systemClock(), log, thresholdMillis, logQueryParameters);
            monitoring.addMonitorListener(logger);
        }

        @Override
        public void shutdown() throws Throwable {
            closable.close();
        }
    };
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) FormattedLog(org.neo4j.logging.FormattedLog) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) Log(org.neo4j.logging.Log) FormattedLog(org.neo4j.logging.FormattedLog) Config(org.neo4j.kernel.configuration.Config) Closeable(java.io.Closeable) OutputStream(java.io.OutputStream) LifecycleAdapter(org.neo4j.kernel.lifecycle.LifecycleAdapter) RotatingFileOutputStreamSupplier(org.neo4j.logging.RotatingFileOutputStreamSupplier) Monitors(org.neo4j.kernel.monitoring.Monitors) File(java.io.File)

Example 2 with LifecycleAdapter

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

the class DatabaseRebuildTool method console.

private ConsoleInput console(final File fromPath, final GraphDatabaseBuilder dbBuilder, InputStream in, Listener<PrintStream> prompt, LifeSupport life) throws Exception {
    // We must have this indirection here since in order to perform CC (one of the commands) we must shut down
    // the database and let CC instantiate its own to run on. After that completes the db
    // should be restored. The commands has references to providers of things to accommodate for this.
    final AtomicReference<Store> store = new AtomicReference<>(new Store(dbBuilder));
    final Supplier<StoreAccess> storeAccess = () -> store.get().access;
    final Supplier<GraphDatabaseAPI> dbAccess = () -> store.get().db;
    ConsoleInput consoleInput = life.add(new ConsoleInput(in, out, prompt));
    consoleInput.add("apply", new ApplyTransactionsCommand(fromPath, dbAccess));
    consoleInput.add(DumpRecordsCommand.NAME, new DumpRecordsCommand(storeAccess));
    consoleInput.add("cc", new ArgsCommand() {

        @Override
        public void run(Args action, PrintStream out) throws Exception {
            File storeDir = store.get().storeDir;
            store.get().shutdown();
            try {
                Result result = new ConsistencyCheckService().runFullConsistencyCheck(storeDir, Config.defaults(), ProgressMonitorFactory.textual(out), FormattedLogProvider.toOutputStream(System.out), false);
                out.println(result.isSuccessful() ? "consistent" : "INCONSISTENT");
            } finally {
                store.set(new Store(dbBuilder));
            }
        }

        @Override
        public String toString() {
            return "Runs consistency check on the database for data that has been applied up to this point";
        }
    });
    life.add(new LifecycleAdapter() {

        @Override
        public void shutdown() {
            store.get().shutdown();
        }
    });
    return consoleInput;
}
Also used : PrintStream(java.io.PrintStream) Args(org.neo4j.helpers.Args) StoreAccess(org.neo4j.kernel.impl.store.StoreAccess) AtomicReference(java.util.concurrent.atomic.AtomicReference) LifecycleAdapter(org.neo4j.kernel.lifecycle.LifecycleAdapter) IOException(java.io.IOException) Result(org.neo4j.consistency.ConsistencyCheckService.Result) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) ConsoleInput(org.neo4j.tools.console.input.ConsoleInput) ArgsCommand(org.neo4j.tools.console.input.ArgsCommand) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) File(java.io.File)

Example 3 with LifecycleAdapter

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

the class ClusterManager method start.

@Override
public void start() throws Throwable {
    FreePorts.Session session = PORTS.newSession();
    Cluster cluster = clustersProvider.apply(session);
    life = new LifeSupport();
    life.add(new LifecycleAdapter() {

        @Override
        public void shutdown() throws Throwable {
            session.close();
        }
    });
    // Started so instances added here will be started immediately, and in case of exceptions they can be
    // shutdown() or stop()ped properly
    life.start();
    managedCluster = new ManagedCluster(cluster, session);
    life.add(managedCluster);
    availabilityChecks.forEach(managedCluster::await);
    if (initialDatasetCreator != null) {
        initialDatasetCreator.receive(managedCluster.getMaster());
        managedCluster.sync();
    }
}
Also used : FreePorts(org.neo4j.cluster.FreePorts) Session(org.neo4j.cluster.FreePorts.Session) Cluster(org.neo4j.cluster.client.Cluster) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) LifecycleAdapter(org.neo4j.kernel.lifecycle.LifecycleAdapter)

Aggregations

LifecycleAdapter (org.neo4j.kernel.lifecycle.LifecycleAdapter)3 File (java.io.File)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FreePorts (org.neo4j.cluster.FreePorts)1 Session (org.neo4j.cluster.FreePorts.Session)1 Cluster (org.neo4j.cluster.client.Cluster)1 ConsistencyCheckService (org.neo4j.consistency.ConsistencyCheckService)1 Result (org.neo4j.consistency.ConsistencyCheckService.Result)1 Args (org.neo4j.helpers.Args)1 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)1 Config (org.neo4j.kernel.configuration.Config)1 StoreAccess (org.neo4j.kernel.impl.store.StoreAccess)1 JobScheduler (org.neo4j.kernel.impl.util.JobScheduler)1 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)1 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)1 Monitors (org.neo4j.kernel.monitoring.Monitors)1