Search in sources :

Example 1 with JobStatusMessage

use of org.apache.flink.runtime.client.JobStatusMessage in project flink by apache.

the class CliFrontend method list.

/**
	 * Executes the list action.
	 * 
	 * @param args Command line arguments for the list action.
	 */
protected int list(String[] args) {
    LOG.info("Running 'list' command.");
    ListOptions options;
    try {
        options = CliFrontendParser.parseListCommand(args);
    } catch (CliArgsException e) {
        return handleArgException(e);
    } catch (Throwable t) {
        return handleError(t);
    }
    // evaluate help flag
    if (options.isPrintHelp()) {
        CliFrontendParser.printHelpForList();
        return 0;
    }
    boolean running = options.getRunning();
    boolean scheduled = options.getScheduled();
    // print running and scheduled jobs if not option supplied
    if (!running && !scheduled) {
        running = true;
        scheduled = true;
    }
    try {
        ActorGateway jobManagerGateway = getJobManagerGateway(options);
        LOG.info("Connecting to JobManager to retrieve list of jobs");
        Future<Object> response = jobManagerGateway.ask(JobManagerMessages.getRequestRunningJobsStatus(), clientTimeout);
        Object result;
        try {
            result = Await.result(response, clientTimeout);
        } catch (Exception e) {
            throw new Exception("Could not retrieve running jobs from the JobManager.", e);
        }
        if (result instanceof RunningJobsStatus) {
            LOG.info("Successfully retrieved list of jobs");
            List<JobStatusMessage> jobs = ((RunningJobsStatus) result).getStatusMessages();
            ArrayList<JobStatusMessage> runningJobs = null;
            ArrayList<JobStatusMessage> scheduledJobs = null;
            if (running) {
                runningJobs = new ArrayList<JobStatusMessage>();
            }
            if (scheduled) {
                scheduledJobs = new ArrayList<JobStatusMessage>();
            }
            for (JobStatusMessage rj : jobs) {
                if (running && (rj.getJobState().equals(JobStatus.RUNNING) || rj.getJobState().equals(JobStatus.RESTARTING))) {
                    runningJobs.add(rj);
                }
                if (scheduled && rj.getJobState().equals(JobStatus.CREATED)) {
                    scheduledJobs.add(rj);
                }
            }
            SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            Comparator<JobStatusMessage> njec = new Comparator<JobStatusMessage>() {

                @Override
                public int compare(JobStatusMessage o1, JobStatusMessage o2) {
                    return (int) (o1.getStartTime() - o2.getStartTime());
                }
            };
            if (running) {
                if (runningJobs.size() == 0) {
                    System.out.println("No running jobs.");
                } else {
                    Collections.sort(runningJobs, njec);
                    System.out.println("------------------ Running/Restarting Jobs -------------------");
                    for (JobStatusMessage rj : runningJobs) {
                        System.out.println(df.format(new Date(rj.getStartTime())) + " : " + rj.getJobId() + " : " + rj.getJobName() + " (" + rj.getJobState() + ")");
                    }
                    System.out.println("--------------------------------------------------------------");
                }
            }
            if (scheduled) {
                if (scheduledJobs.size() == 0) {
                    System.out.println("No scheduled jobs.");
                } else {
                    Collections.sort(scheduledJobs, njec);
                    System.out.println("----------------------- Scheduled Jobs -----------------------");
                    for (JobStatusMessage rj : scheduledJobs) {
                        System.out.println(df.format(new Date(rj.getStartTime())) + " : " + rj.getJobId() + " : " + rj.getJobName());
                    }
                    System.out.println("--------------------------------------------------------------");
                }
            }
            return 0;
        } else {
            throw new Exception("ReqeustRunningJobs requires a response of type " + "RunningJobs. Instead the response is of type " + result.getClass() + ".");
        }
    } catch (Throwable t) {
        return handleError(t);
    }
}
Also used : ListOptions(org.apache.flink.client.cli.ListOptions) CliArgsException(org.apache.flink.client.cli.CliArgsException) ProgramInvocationException(org.apache.flink.client.program.ProgramInvocationException) ProgramMissingJobException(org.apache.flink.client.program.ProgramMissingJobException) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) ProgramParametrizationException(org.apache.flink.client.program.ProgramParametrizationException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) CliArgsException(org.apache.flink.client.cli.CliArgsException) IOException(java.io.IOException) Date(java.util.Date) Comparator(java.util.Comparator) RunningJobsStatus(org.apache.flink.runtime.messages.JobManagerMessages.RunningJobsStatus) JobStatusMessage(org.apache.flink.runtime.client.JobStatusMessage) ActorGateway(org.apache.flink.runtime.instance.ActorGateway) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with JobStatusMessage

use of org.apache.flink.runtime.client.JobStatusMessage in project flink by apache.

the class JobManagerCommunicationUtils method waitUntilJobIsRunning.

public static void waitUntilJobIsRunning(ActorGateway jobManager, String name) throws Exception {
    while (true) {
        Future<Object> listResponse = jobManager.ask(JobManagerMessages.getRequestRunningJobsStatus(), askTimeout);
        List<JobStatusMessage> jobs;
        try {
            Object result = Await.result(listResponse, askTimeout);
            jobs = ((JobManagerMessages.RunningJobsStatus) result).getStatusMessages();
        } catch (Exception e) {
            throw new Exception("Could not wait for job to start - failed to retrieve running jobs from the JobManager.", e);
        }
        // see if the running jobs contain the requested job
        for (JobStatusMessage job : jobs) {
            if (job.getJobName().equals(name)) {
                return;
            }
        }
        Thread.sleep(50);
    }
}
Also used : JobStatusMessage(org.apache.flink.runtime.client.JobStatusMessage) JobManagerMessages(org.apache.flink.runtime.messages.JobManagerMessages)

Example 3 with JobStatusMessage

use of org.apache.flink.runtime.client.JobStatusMessage in project flink by apache.

the class JobManagerCommunicationUtils method cancelCurrentJob.

public static void cancelCurrentJob(ActorGateway jobManager, String name) throws Exception {
    JobStatusMessage status = null;
    for (int i = 0; i < 200; i++) {
        // find the jobID
        Future<Object> listResponse = jobManager.ask(JobManagerMessages.getRequestRunningJobsStatus(), askTimeout);
        List<JobStatusMessage> jobs;
        try {
            Object result = Await.result(listResponse, askTimeout);
            jobs = ((JobManagerMessages.RunningJobsStatus) result).getStatusMessages();
        } catch (Exception e) {
            throw new Exception("Could not cancel job - failed to retrieve running jobs from the JobManager.", e);
        }
        if (jobs.isEmpty()) {
            // try again, fall through the loop
            Thread.sleep(50);
        } else if (jobs.size() == 1) {
            status = jobs.get(0);
        } else if (name != null) {
            for (JobStatusMessage msg : jobs) {
                if (msg.getJobName().equals(name)) {
                    status = msg;
                }
            }
            if (status == null) {
                throw new Exception("Could not cancel job - no job matched expected name = '" + name + "' in " + jobs);
            }
        } else {
            String jobNames = "";
            for (JobStatusMessage jsm : jobs) {
                jobNames += jsm.getJobName() + ", ";
            }
            throw new Exception("Could not cancel job - more than one running job: " + jobNames);
        }
    }
    if (status == null) {
        throw new Exception("Could not cancel job - no running jobs");
    } else if (status.getJobState().isGloballyTerminalState()) {
        throw new Exception("Could not cancel job - job is not running any more");
    }
    JobID jobId = status.getJobId();
    Future<Object> response = jobManager.ask(new JobManagerMessages.CancelJob(jobId), askTimeout);
    try {
        Await.result(response, askTimeout);
    } catch (Exception e) {
        throw new Exception("Sending the 'cancel' message failed.", e);
    }
}
Also used : JobStatusMessage(org.apache.flink.runtime.client.JobStatusMessage) JobManagerMessages(org.apache.flink.runtime.messages.JobManagerMessages) JobID(org.apache.flink.api.common.JobID)

Example 4 with JobStatusMessage

use of org.apache.flink.runtime.client.JobStatusMessage in project flink by apache.

the class CliFrontendListTest method createClusterClient.

private static ClusterClient<String> createClusterClient() throws Exception {
    final ClusterClient<String> clusterClient = mock(ClusterClient.class);
    when(clusterClient.listJobs()).thenReturn(CompletableFuture.completedFuture(Arrays.asList(new JobStatusMessage(new JobID(), "job1", JobStatus.RUNNING, 1L), new JobStatusMessage(new JobID(), "job2", JobStatus.CREATED, 1L), new JobStatusMessage(new JobID(), "job3", JobStatus.FINISHED, 3L))));
    return clusterClient;
}
Also used : JobStatusMessage(org.apache.flink.runtime.client.JobStatusMessage) JobID(org.apache.flink.api.common.JobID)

Example 5 with JobStatusMessage

use of org.apache.flink.runtime.client.JobStatusMessage in project flink by apache.

the class RestClusterClientTest method testListJobs.

@Test
public void testListJobs() throws Exception {
    try (TestRestServerEndpoint restServerEndpoint = createRestServerEndpoint(new TestListJobsHandler())) {
        RestClusterClient<?> restClusterClient = createRestClusterClient(restServerEndpoint.getServerAddress().getPort());
        try {
            CompletableFuture<Collection<JobStatusMessage>> jobDetailsFuture = restClusterClient.listJobs();
            Collection<JobStatusMessage> jobDetails = jobDetailsFuture.get();
            Iterator<JobStatusMessage> jobDetailsIterator = jobDetails.iterator();
            JobStatusMessage job1 = jobDetailsIterator.next();
            JobStatusMessage job2 = jobDetailsIterator.next();
            Assert.assertNotEquals("The job status should not be equal.", job1.getJobState(), job2.getJobState());
        } finally {
            restClusterClient.close();
        }
    }
}
Also used : TestRestServerEndpoint(org.apache.flink.runtime.rest.util.TestRestServerEndpoint) JobStatusMessage(org.apache.flink.runtime.client.JobStatusMessage) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

JobStatusMessage (org.apache.flink.runtime.client.JobStatusMessage)11 JobID (org.apache.flink.api.common.JobID)6 ProgramInvocationException (org.apache.flink.client.program.ProgramInvocationException)6 Configuration (org.apache.flink.configuration.Configuration)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 Collection (java.util.Collection)3 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)3 ProgramMissingJobException (org.apache.flink.client.program.ProgramMissingJobException)3 ProgramParametrizationException (org.apache.flink.client.program.ProgramParametrizationException)3 Test (org.junit.Test)3 FiniteDuration (scala.concurrent.duration.FiniteDuration)3 Timeout (akka.util.Timeout)2 File (java.io.File)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 URI (java.net.URI)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2