Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 24842200B68 for ; Fri, 19 Aug 2016 10:10:40 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 231BA160AAC; Fri, 19 Aug 2016 08:10:40 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 42E74160A8E for ; Fri, 19 Aug 2016 10:10:39 +0200 (CEST) Received: (qmail 25584 invoked by uid 500); 19 Aug 2016 08:10:38 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 25575 invoked by uid 99); 19 Aug 2016 08:10:38 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 19 Aug 2016 08:10:38 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 0FBCBC0439 for ; Fri, 19 Aug 2016 08:10:38 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -5.446 X-Spam-Level: X-Spam-Status: No, score=-5.446 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id gVYxUU6cf3IW for ; Fri, 19 Aug 2016 08:10:35 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 8C4F05F245 for ; Fri, 19 Aug 2016 08:10:34 +0000 (UTC) Received: (qmail 25490 invoked by uid 99); 19 Aug 2016 08:10:33 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 19 Aug 2016 08:10:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 8121CE02D4; Fri, 19 Aug 2016 08:10:33 +0000 (UTC) From: mxm To: issues@flink.incubator.apache.org Reply-To: issues@flink.incubator.apache.org References: In-Reply-To: Subject: [GitHub] flink pull request #2313: [FLINK-4273] Modify JobClient to attach to running... Content-Type: text/plain Message-Id: <20160819081033.8121CE02D4@git1-us-west.apache.org> Date: Fri, 19 Aug 2016 08:10:33 +0000 (UTC) archived-at: Fri, 19 Aug 2016 08:10:40 -0000 Github user mxm commented on a diff in the pull request: https://github.com/apache/flink/pull/2313#discussion_r75442774 --- Diff: flink-runtime/src/main/java/org/apache/flink/runtime/client/JobClient.java --- @@ -118,27 +138,162 @@ public static JobExecutionResult submitJobAndWait( sysoutLogUpdates); ActorRef jobClientActor = actorSystem.actorOf(jobClientActorProps); - + + Future submissionFuture = Patterns.ask( + jobClientActor, + new JobClientMessages.SubmitJobAndWait(jobGraph), + new Timeout(AkkaUtils.INF_TIMEOUT())); + + return new JobListeningContext( + jobGraph.getJobID(), + submissionFuture, + jobClientActor, + classLoader); + } + + + /** + * Attaches to a running Job using the JobID. + * Reconstructs the user class loader by downloading the jars from the JobManager. + * @throws JobRetrievalException if anything goes wrong while retrieving the job + */ + public static JobListeningContext attachToRunningJob( + JobID jobID, + ActorGateway jobManagerGateWay, + Configuration configuration, + ActorSystem actorSystem, + LeaderRetrievalService leaderRetrievalService, + FiniteDuration timeout, + boolean sysoutLogUpdates) throws JobRetrievalException { + + checkNotNull(jobID, "The jobID must not be null."); + checkNotNull(jobManagerGateWay, "The jobManagerGateWay must not be null."); + checkNotNull(configuration, "The configuration must not be null."); + checkNotNull(actorSystem, "The actorSystem must not be null."); + checkNotNull(leaderRetrievalService, "The jobManagerGateway must not be null."); + checkNotNull(timeout, "The timeout must not be null."); + + // retrieve classloader first before doing anything + ClassLoader classloader; + try { + classloader = retrieveClassLoader(jobID, jobManagerGateWay, configuration, timeout); + LOG.info("Reconstructed class loader for Job {}" , jobID); + } catch (Exception e) { + LOG.warn("Couldn't retrieve classloader for {}. Using system class loader", jobID, e); + classloader = JobClient.class.getClassLoader(); + } + + // we create a proxy JobClientActor that deals with all communication with + // the JobManager. It forwards the job submission, checks the success/failure responses, logs + // update messages, watches for disconnect between client and JobManager, ... + Props jobClientActorProps = JobClientActor.createJobClientActorProps( + leaderRetrievalService, + timeout, + sysoutLogUpdates); + + ActorRef jobClientActor = actorSystem.actorOf(jobClientActorProps); + + Future attachmentFuture = Patterns.ask( + jobClientActor, + new JobClientMessages.AttachToJobAndWait(jobID), + new Timeout(AkkaUtils.INF_TIMEOUT())); + + return new JobListeningContext( + jobID, + attachmentFuture, + jobClientActor, + classloader); + } + + /** + * Reconstructs the class loader by first requesting information about it at the JobManager + * and then downloading missing jar files. + * @param jobID id of job + * @param jobManager gateway to the JobManager + * @param config the flink configuration + * @param timeout timeout for querying the jobmanager + * @return A classloader that should behave like the original classloader + * @throws JobRetrievalException if anything goes wrong + */ + public static ClassLoader retrieveClassLoader( + JobID jobID, + ActorGateway jobManager, + Configuration config, + FiniteDuration timeout) + throws JobRetrievalException { + + final Object jmAnswer; + try { + jmAnswer = Await.result( + jobManager.ask( + new JobManagerMessages.RequestClassloadingProps(jobID), timeout), timeout); + } catch (Exception e) { + throw new JobRetrievalException(jobID, "JobManager didn't respond", e); + } + + if (jmAnswer instanceof JobManagerMessages.ClassloadingProps) { + JobManagerMessages.ClassloadingProps props = ((JobManagerMessages.ClassloadingProps) jmAnswer); + + Option jmHost = jobManager.actor().path().address().host(); + String jmHostname = jmHost.isDefined() ? jmHost.get() : "localhost"; + InetSocketAddress serverAddress = new InetSocketAddress(jmHostname, props.blobManagerPort()); + final BlobCache blobClient = new BlobCache(serverAddress, config); + + final List requiredJarFiles = props.requiredJarFiles(); + final List requiredClasspaths = props.requiredClasspaths(); + + final URL[] allURLs = new URL[requiredJarFiles.size() + requiredClasspaths.size()]; + + int pos = 0; + for (BlobKey blobKey : props.requiredJarFiles()) { + try { + allURLs[pos++] = blobClient.getURL(blobKey); + } catch (Exception e) { + blobClient.shutdown(); + throw new JobRetrievalException(jobID, "Failed to download BlobKey " + blobKey); + } + } + + for (URL url : requiredClasspaths) { + allURLs[pos++] = url; + } + + return new URLClassLoader(allURLs, JobClient.class.getClassLoader()); + } else if (jmAnswer instanceof JobManagerMessages.JobNotFound) { + throw new JobRetrievalException(jobID, "Couldn't retrieve class loader. Job " + jobID + " not found"); + } else { + throw new JobRetrievalException(jobID, "Unknown response from JobManager: " + jmAnswer); + } + } + + /** + * Given a JobListeningContext, awaits the result of the job execution that this context is bound to + * @param listeningContext The listening context of the job execution + * @return The result of the execution + * @throws JobExecutionException if anything goes wrong while monitoring the job + */ + public static JobExecutionResult awaitJobResult(JobListeningContext listeningContext) throws JobExecutionException { + + final JobID jobID = listeningContext.jobID; + final Future jobSubmissionFuture = listeningContext.jobResultFuture; + final ClassLoader classLoader = listeningContext.classLoader; + // first block handles errors while waiting for the result - Object answer; + final Object answer; try { - Future future = Patterns.ask(jobClientActor, - new JobClientMessages.SubmitJobAndWait(jobGraph), - new Timeout(AkkaUtils.INF_TIMEOUT())); - - answer = Await.result(future, AkkaUtils.INF_TIMEOUT()); + answer = Await.result(jobSubmissionFuture, AkkaUtils.INF_TIMEOUT()); --- End diff -- That's an issue that was present before in `JobClientActor`. Couldn't we ensure with Akka that the actor sends a message before it dies? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---