Search in sources :

Example 1 with ProcessStreamHandler

use of org.neo4j.test.ProcessStreamHandler in project neo4j by neo4j.

the class LegacyDatabaseImpl method start.

public static Future<LegacyDatabase> start(String classpath, File storeDir, Map<String, String> config) throws Exception {
    List<String> args = new ArrayList<String>();
    args.add(storeDir.getAbsolutePath());
    int rmiPort = 7000 + parseInt(config.get("ha.server_id"));
    args.add("" + rmiPort);
    args.addAll(asList(new Args(config).asArgs()));
    final Process process = execJava(appendNecessaryTestClasses(classpath), LegacyDatabaseImpl.class.getName(), args.toArray(new String[0]));
    new ProcessStreamHandler(process, false).launch();
    final RmiLocation rmiLocation = rmiLocation(rmiPort);
    ExecutorService executor = newSingleThreadExecutor();
    Future<LegacyDatabase> future = executor.submit(new Callable<LegacyDatabase>() {

        @Override
        public LegacyDatabase call() throws Exception {
            long endTime = currentTimeMillis() + SECONDS.toMillis(10000);
            while (currentTimeMillis() < endTime) {
                try {
                    return (LegacyDatabase) rmiLocation.getBoundObject();
                } catch (RemoteException e) {
                    // OK
                    sleep(100);
                }
            }
            process.destroy();
            throw new IllegalStateException("Couldn't get remote to legacy database");
        }
    });
    executor.shutdown();
    return future;
}
Also used : Args(org.neo4j.helpers.Args) RmiLocation(org.neo4j.shell.impl.RmiLocation) ArrayList(java.util.ArrayList) Exceptions.launderedException(org.neo4j.helpers.Exceptions.launderedException) RemoteException(java.rmi.RemoteException) ExecutorService(java.util.concurrent.ExecutorService) ProcessStreamHandler(org.neo4j.test.ProcessStreamHandler) RemoteException(java.rmi.RemoteException)

Example 2 with ProcessStreamHandler

use of org.neo4j.test.ProcessStreamHandler in project neo4j by neo4j.

the class RmiPublicationIT method waitForExit.

private int waitForExit(Process process, int maxSeconds) throws InterruptedException {
    try {
        long endTime = System.currentTimeMillis() + maxSeconds * 1000;
        ProcessStreamHandler streamHandler = new ProcessStreamHandler(process, false);
        streamHandler.launch();
        try {
            while (System.currentTimeMillis() < endTime) {
                try {
                    return process.exitValue();
                } catch (IllegalThreadStateException e) {
                    // OK, not exited yet
                    Thread.sleep(100);
                }
            }
            tempHackToGetThreadDump(process);
            throw new RuntimeException("Process didn't exit on its own.");
        } finally {
            streamHandler.cancel();
        }
    } finally {
        process.destroy();
    }
}
Also used : ProcessStreamHandler(org.neo4j.test.ProcessStreamHandler)

Example 3 with ProcessStreamHandler

use of org.neo4j.test.ProcessStreamHandler in project neo4j by neo4j.

the class RmiPublicationIT method tempHackToGetThreadDump.

private void tempHackToGetThreadDump(Process process) {
    try {
        Field pidField = process.getClass().getDeclaredField("pid");
        pidField.setAccessible(true);
        int pid = (int) pidField.get(process);
        ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", "-c", "kill -3 " + pid);
        processBuilder.redirectErrorStream(true);
        Process dumpProc = processBuilder.start();
        ProcessStreamHandler streamHandler = new ProcessStreamHandler(dumpProc, false);
        streamHandler.launch();
        try {
            process.waitFor();
        } finally {
            streamHandler.cancel();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : Field(java.lang.reflect.Field) ProcessStreamHandler(org.neo4j.test.ProcessStreamHandler)

Example 4 with ProcessStreamHandler

use of org.neo4j.test.ProcessStreamHandler in project neo4j by neo4j.

the class HardKillIT method run.

private Process run(int machineId) throws IOException {
    List<String> allArgs = new ArrayList<>(Arrays.asList("java", "-cp", System.getProperty("java.class.path"), "-Djava.awt.headless=true", HardKillIT.class.getName()));
    allArgs.add("" + machineId);
    allArgs.add(path(machineId).getAbsolutePath());
    Process process = Runtime.getRuntime().exec(allArgs.toArray(new String[allArgs.size()]));
    processHandler = new ProcessStreamHandler(process, false);
    processHandler.launch();
    return process;
}
Also used : ArrayList(java.util.ArrayList) ProcessStreamHandler(org.neo4j.test.ProcessStreamHandler)

Example 5 with ProcessStreamHandler

use of org.neo4j.test.ProcessStreamHandler in project neo4j by neo4j.

the class ArbiterBootstrapperIT method startArbiter.

private boolean startArbiter(File configDir, CountDownLatch latch) throws Exception {
    Process process = null;
    ProcessStreamHandler handler = null;
    try {
        process = startArbiterProcess(configDir);
        new InputStreamAwaiter(process.getInputStream()).awaitLine(START_SIGNAL, 20, SECONDS);
        handler = new ProcessStreamHandler(process, false, "", IGNORE_FAILURES);
        handler.launch();
        // being able to start is assumed in this test to be that the specified port already is in use.
        return latch.await(10, SECONDS);
    } finally {
        if (process != null) {
            // Tell it to leave the cluster and shut down now
            try (OutputStream inputToOtherProcess = process.getOutputStream()) {
                inputToOtherProcess.write(0);
                inputToOtherProcess.flush();
            }
            if (!process.waitFor(10, SECONDS)) {
                kill(process);
            }
        }
        if (handler != null) {
            handler.done();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) ProcessStreamHandler(org.neo4j.test.ProcessStreamHandler) InputStreamAwaiter(org.neo4j.test.InputStreamAwaiter)

Aggregations

ProcessStreamHandler (org.neo4j.test.ProcessStreamHandler)6 ArrayList (java.util.ArrayList)3 OutputStream (java.io.OutputStream)1 Field (java.lang.reflect.Field)1 RemoteException (java.rmi.RemoteException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Args (org.neo4j.helpers.Args)1 Exceptions.launderedException (org.neo4j.helpers.Exceptions.launderedException)1 RmiLocation (org.neo4j.shell.impl.RmiLocation)1 InputStreamAwaiter (org.neo4j.test.InputStreamAwaiter)1