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);
}
}
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);
}
}
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);
}
}
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;
}
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();
}
}
}
Aggregations