Search in sources :

Example 1 with RequestJobDetails

use of org.apache.flink.runtime.messages.webmonitor.RequestJobDetails in project flink by apache.

the class CurrentJobsOverviewHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    try {
        if (jobManager != null) {
            Future<Object> future = jobManager.ask(new RequestJobDetails(includeRunningJobs, includeFinishedJobs), timeout);
            MultipleJobsDetails result = (MultipleJobsDetails) Await.result(future, timeout);
            final long now = System.currentTimeMillis();
            StringWriter writer = new StringWriter();
            JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
            gen.writeStartObject();
            if (includeRunningJobs && includeFinishedJobs) {
                gen.writeArrayFieldStart("running");
                for (JobDetails detail : result.getRunningJobs()) {
                    writeJobDetailOverviewAsJson(detail, gen, now);
                }
                gen.writeEndArray();
                gen.writeArrayFieldStart("finished");
                for (JobDetails detail : result.getFinishedJobs()) {
                    writeJobDetailOverviewAsJson(detail, gen, now);
                }
                gen.writeEndArray();
            } else {
                gen.writeArrayFieldStart("jobs");
                for (JobDetails detail : includeRunningJobs ? result.getRunningJobs() : result.getFinishedJobs()) {
                    writeJobDetailOverviewAsJson(detail, gen, now);
                }
                gen.writeEndArray();
            }
            gen.writeEndObject();
            gen.close();
            return writer.toString();
        } else {
            throw new Exception("No connection to the leading JobManager.");
        }
    } catch (Exception e) {
        throw new Exception("Failed to fetch the status overview: " + e.getMessage(), e);
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RequestJobDetails(org.apache.flink.runtime.messages.webmonitor.RequestJobDetails) MultipleJobsDetails(org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails) JobDetails(org.apache.flink.runtime.messages.webmonitor.JobDetails) RequestJobDetails(org.apache.flink.runtime.messages.webmonitor.RequestJobDetails) IOException(java.io.IOException)

Example 2 with RequestJobDetails

use of org.apache.flink.runtime.messages.webmonitor.RequestJobDetails in project flink by apache.

the class MetricFetcher method fetchMetrics.

private void fetchMetrics() {
    try {
        Option<scala.Tuple2<ActorGateway, Integer>> jobManagerGatewayAndWebPort = retriever.getJobManagerGatewayAndWebPort();
        if (jobManagerGatewayAndWebPort.isDefined()) {
            ActorGateway jobManager = jobManagerGatewayAndWebPort.get()._1();
            /**
				 * Remove all metrics that belong to a job that is not running and no longer archived.
				 */
            Future<Object> jobDetailsFuture = jobManager.ask(new RequestJobDetails(true, true), timeout);
            jobDetailsFuture.onSuccess(new OnSuccess<Object>() {

                @Override
                public void onSuccess(Object result) throws Throwable {
                    MultipleJobsDetails details = (MultipleJobsDetails) result;
                    ArrayList<String> toRetain = new ArrayList<>();
                    for (JobDetails job : details.getRunningJobs()) {
                        toRetain.add(job.getJobId().toString());
                    }
                    for (JobDetails job : details.getFinishedJobs()) {
                        toRetain.add(job.getJobId().toString());
                    }
                    synchronized (metrics) {
                        metrics.jobs.keySet().retainAll(toRetain);
                    }
                }
            }, ctx);
            logErrorOnFailure(jobDetailsFuture, "Fetching of JobDetails failed.");
            String jobManagerPath = jobManager.path();
            String queryServicePath = jobManagerPath.substring(0, jobManagerPath.lastIndexOf('/') + 1) + MetricQueryService.METRIC_QUERY_SERVICE_NAME;
            ActorRef jobManagerQueryService = actorSystem.actorFor(queryServicePath);
            queryMetrics(jobManagerQueryService);
            /**
				 * We first request the list of all registered task managers from the job manager, and then
				 * request the respective metric dump from each task manager.
				 *
				 * All stored metrics that do not belong to a registered task manager will be removed.
				 */
            Future<Object> registeredTaskManagersFuture = jobManager.ask(JobManagerMessages.getRequestRegisteredTaskManagers(), timeout);
            registeredTaskManagersFuture.onSuccess(new OnSuccess<Object>() {

                @Override
                public void onSuccess(Object result) throws Throwable {
                    Iterable<Instance> taskManagers = ((JobManagerMessages.RegisteredTaskManagers) result).asJavaIterable();
                    List<String> activeTaskManagers = new ArrayList<>();
                    for (Instance taskManager : taskManagers) {
                        activeTaskManagers.add(taskManager.getId().toString());
                        String taskManagerPath = taskManager.getTaskManagerGateway().getAddress();
                        String queryServicePath = taskManagerPath.substring(0, taskManagerPath.lastIndexOf('/') + 1) + MetricQueryService.METRIC_QUERY_SERVICE_NAME + "_" + taskManager.getTaskManagerID().getResourceIdString();
                        ActorRef taskManagerQueryService = actorSystem.actorFor(queryServicePath);
                        queryMetrics(taskManagerQueryService);
                    }
                    synchronized (metrics) {
                        // remove all metrics belonging to unregistered task managers
                        metrics.taskManagers.keySet().retainAll(activeTaskManagers);
                    }
                }
            }, ctx);
            logErrorOnFailure(registeredTaskManagersFuture, "Fetchin list of registered TaskManagers failed.");
        }
    } catch (Exception e) {
        LOG.warn("Exception while fetching metrics.", e);
    }
}
Also used : Instance(org.apache.flink.runtime.instance.Instance) ActorRef(akka.actor.ActorRef) ArrayList(java.util.ArrayList) JobManagerMessages(org.apache.flink.runtime.messages.JobManagerMessages) RequestJobDetails(org.apache.flink.runtime.messages.webmonitor.RequestJobDetails) MultipleJobsDetails(org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails) RequestJobDetails(org.apache.flink.runtime.messages.webmonitor.RequestJobDetails) JobDetails(org.apache.flink.runtime.messages.webmonitor.JobDetails) ActorGateway(org.apache.flink.runtime.instance.ActorGateway) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

JobDetails (org.apache.flink.runtime.messages.webmonitor.JobDetails)2 MultipleJobsDetails (org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails)2 RequestJobDetails (org.apache.flink.runtime.messages.webmonitor.RequestJobDetails)2 ActorRef (akka.actor.ActorRef)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ActorGateway (org.apache.flink.runtime.instance.ActorGateway)1 Instance (org.apache.flink.runtime.instance.Instance)1 JobManagerMessages (org.apache.flink.runtime.messages.JobManagerMessages)1