void unref() { synchronized(LauncherServer.class) { if (refCount.decrementAndGet() == 0) { try { close(); } catch (IOException ioe) { // no-op. } finally { serverInstance = null; } } } }
/** * Removes the client handle from the pending list (in case it's still there), and unrefs * the server. */ void unregister(AbstractAppHandle handle) { for (Map.Entry<String, AbstractAppHandle> e : secretToPendingApps.entrySet()) { if (e.getValue().equals(handle)) { String secret = e.getKey(); secretToPendingApps.remove(secret); break; } } unref(); }
/** * Creates a handle for an app to be launched. This method will start a server if one hasn't been * started yet. The server is shared for multiple handles, and once all handles are disposed of, * the server is shut down. */ static synchronized ChildProcAppHandle newAppHandle() throws IOException { LauncherServer server = serverInstance != null ? serverInstance : new LauncherServer(); server.ref(); serverInstance = server; String secret = server.createSecret(); while (server.pending.containsKey(secret)) { secret = server.createSecret(); } return server.newAppHandle(secret); }
static synchronized LauncherServer getOrCreateServer() throws IOException { LauncherServer server; do { server = serverInstance != null ? serverInstance : new LauncherServer(); } while (!server.running); server.ref(); serverInstance = server; return server; }
LauncherServer server = LauncherServer.getOrCreateServer(); ChildProcAppHandle handle = new ChildProcAppHandle(server); for (SparkAppHandle.Listener l : listeners) { String secret = server.registerHandle(handle); pb.environment().put(LauncherProtocol.ENV_LAUNCHER_PORT, String.valueOf(server.getPort())); pb.environment().put(LauncherProtocol.ENV_LAUNCHER_SECRET, secret); try {
ChildProcAppHandle handle = LauncherServer.newAppHandle(); for (SparkAppHandle.Listener l : listeners) { handle.addListener(l); String.valueOf(LauncherServer.getServerInstance().getPort())); pb.environment().put(LauncherProtocol.ENV_LAUNCHER_SECRET, handle.getSecret()); try {
@Test public void testLauncherServerReuse() throws Exception { LauncherServer server1 = LauncherServer.getOrCreateServer(); ChildProcAppHandle handle = new ChildProcAppHandle(server1); handle.kill(); LauncherServer server2 = LauncherServer.getOrCreateServer(); try { assertNotSame(server1, server2); } finally { server2.unref(); } }
@Override public synchronized void disconnect() { if (!disposed) { disposed = true; if (connection != null) { try { connection.close(); } catch (IOException ioe) { // no-op. } } server.unregister(this); if (redirector != null) { redirector.stop(); } } }
clients.add(clientConnection); long timeoutMs = getConnectionTimeout(); timeoutTimer.schedule(timeout, getConnectionTimeout()); } else { timeout.run();
@Test public void testLauncherServerReuse() throws Exception { ChildProcAppHandle handle1 = null; ChildProcAppHandle handle2 = null; ChildProcAppHandle handle3 = null; try { handle1 = LauncherServer.newAppHandle(); handle2 = LauncherServer.newAppHandle(); LauncherServer server1 = handle1.getServer(); assertSame(server1, handle2.getServer()); handle1.kill(); handle2.kill(); handle3 = LauncherServer.newAppHandle(); assertNotSame(server1, handle3.getServer()); handle3.kill(); assertNull(LauncherServer.getServerInstance()); } finally { kill(handle1); kill(handle2); kill(handle3); } }
/** * Registers a handle with the server, and returns the secret the child app needs to connect * back. */ synchronized String registerHandle(AbstractAppHandle handle) { String secret = createSecret(); secretToPendingApps.put(secret, handle); return secret; }
LauncherServer server = LauncherServer.getOrCreateServer(); ChildProcAppHandle handle = new ChildProcAppHandle(server); for (SparkAppHandle.Listener l : listeners) { String secret = server.registerHandle(handle); pb.environment().put(LauncherProtocol.ENV_LAUNCHER_PORT, String.valueOf(server.getPort())); pb.environment().put(LauncherProtocol.ENV_LAUNCHER_SECRET, secret); try {
ChildProcAppHandle handle = LauncherServer.newAppHandle(); for (SparkAppHandle.Listener l : listeners) { handle.addListener(l); String.valueOf(LauncherServer.getServerInstance().getPort())); pb.environment().put(LauncherProtocol.ENV_LAUNCHER_SECRET, handle.getSecret()); try {
static synchronized LauncherServer getOrCreateServer() throws IOException { LauncherServer server; do { server = serverInstance != null ? serverInstance : new LauncherServer(); } while (!server.running); server.ref(); serverInstance = server; return server; }
@Override public synchronized void disconnect() { if (!disposed) { disposed = true; if (connection != null) { try { connection.close(); } catch (IOException ioe) { // no-op. } } server.unregister(this); if (redirector != null) { redirector.stop(); } } }
long timeoutMs = getConnectionTimeout();
/** * Registers a handle with the server, and returns the secret the child app needs to connect * back. */ synchronized String registerHandle(AbstractAppHandle handle) { String secret = createSecret(); secretToPendingApps.put(secret, handle); return secret; }
/** * Starts a Spark application. * * @see AbstractLauncher#startApplication(SparkAppHandle.Listener...) * @param listeners Listeners to add to the handle before the app is launched. * @return A handle for the launched application. */ @Override public SparkAppHandle startApplication(SparkAppHandle.Listener... listeners) throws IOException { if (builder.isClientMode(builder.getEffectiveConfig())) { LOG.warning("It's not recommended to run client-mode applications using InProcessLauncher."); } Method main = findSparkSubmit(); LauncherServer server = LauncherServer.getOrCreateServer(); InProcessAppHandle handle = new InProcessAppHandle(server); for (SparkAppHandle.Listener l : listeners) { handle.addListener(l); } String secret = server.registerHandle(handle); setConf(LauncherProtocol.CONF_LAUNCHER_PORT, String.valueOf(server.getPort())); setConf(LauncherProtocol.CONF_LAUNCHER_SECRET, secret); List<String> sparkArgs = builder.buildSparkSubmitArgs(); String[] argv = sparkArgs.toArray(new String[sparkArgs.size()]); String appName = CommandBuilderUtils.firstNonEmpty(builder.appName, builder.mainClass, "<unknown>"); handle.start(appName, main, argv); return handle; }
@Test public void testTimeout() throws Exception { ChildProcAppHandle handle = null; TestClient client = null; try { // LauncherServer will immediately close the server-side socket when the timeout is set // to 0. SparkLauncher.setConfig(SparkLauncher.CHILD_CONNECTION_TIMEOUT, "0"); handle = LauncherServer.newAppHandle(); Socket s = new Socket(InetAddress.getLoopbackAddress(), LauncherServer.getServerInstance().getPort()); client = new TestClient(s); waitForError(client, handle.getSecret()); } finally { SparkLauncher.launcherConfig.remove(SparkLauncher.CHILD_CONNECTION_TIMEOUT); kill(handle); close(client); } }