Search in sources :

Example 1 with ContainerInfo

use of org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo in project hadoop by apache.

the class AHSWebServices method getLogs.

//TODO: YARN-4993: Refactory ContainersLogsBlock, AggregatedLogsBlock and
//      container log webservice introduced in AHS to minimize
//      the duplication.
@GET
@Path("/containerlogs/{containerid}/{filename}")
@Produces({ MediaType.TEXT_PLAIN + "; " + JettyUtils.UTF_8 })
@Public
@Unstable
public Response getLogs(@Context HttpServletRequest req, @Context HttpServletResponse res, @PathParam(YarnWebServiceParams.CONTAINER_ID) String containerIdStr, @PathParam(YarnWebServiceParams.CONTAINER_LOG_FILE_NAME) String filename, @QueryParam(YarnWebServiceParams.RESPONSE_CONTENT_FORMAT) String format, @QueryParam(YarnWebServiceParams.RESPONSE_CONTENT_SIZE) String size, @QueryParam(YarnWebServiceParams.NM_ID) String nmId, @QueryParam(YarnWebServiceParams.REDIRECTED_FROM_NODE) @DefaultValue("false") boolean redirected_from_node) {
    init(res);
    ContainerId containerId;
    try {
        containerId = ContainerId.fromString(containerIdStr);
    } catch (IllegalArgumentException ex) {
        return createBadResponse(Status.NOT_FOUND, "Invalid ContainerId: " + containerIdStr);
    }
    final long length = parseLongParam(size);
    ApplicationId appId = containerId.getApplicationAttemptId().getApplicationId();
    AppInfo appInfo;
    try {
        appInfo = super.getApp(req, res, appId.toString());
    } catch (Exception ex) {
        // directly find logs from HDFS.
        return sendStreamOutputResponse(appId, null, null, containerIdStr, filename, format, length, false);
    }
    String appOwner = appInfo.getUser();
    if (isFinishedState(appInfo.getAppState())) {
        // directly find logs from HDFS.
        return sendStreamOutputResponse(appId, appOwner, null, containerIdStr, filename, format, length, false);
    }
    if (isRunningState(appInfo.getAppState())) {
        String nodeHttpAddress = null;
        if (nmId != null && !nmId.isEmpty()) {
            try {
                nodeHttpAddress = getNMWebAddressFromRM(conf, nmId);
            } catch (Exception ex) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(ex.getMessage());
                }
            }
        }
        if (nodeHttpAddress == null || nodeHttpAddress.isEmpty()) {
            ContainerInfo containerInfo;
            try {
                containerInfo = super.getContainer(req, res, appId.toString(), containerId.getApplicationAttemptId().toString(), containerId.toString());
            } catch (Exception ex) {
                // output the aggregated logs
                return sendStreamOutputResponse(appId, appOwner, null, containerIdStr, filename, format, length, true);
            }
            nodeHttpAddress = containerInfo.getNodeHttpAddress();
            // request back. Simply output the aggregated logs.
            if (nodeHttpAddress == null || nodeHttpAddress.isEmpty() || redirected_from_node) {
                // output the aggregated logs
                return sendStreamOutputResponse(appId, appOwner, null, containerIdStr, filename, format, length, true);
            }
        }
        String uri = "/" + containerId.toString() + "/logs/" + filename;
        String resURI = JOINER.join(getAbsoluteNMWebAddress(nodeHttpAddress), NM_DOWNLOAD_URI_STR, uri);
        String query = req.getQueryString();
        if (query != null && !query.isEmpty()) {
            resURI += "?" + query;
        }
        ResponseBuilder response = Response.status(HttpServletResponse.SC_TEMPORARY_REDIRECT);
        response.header("Location", resURI);
        return response.build();
    } else {
        return createBadResponse(Status.NOT_FOUND, "The application is not at Running or Finished State.");
    }
}
Also used : ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) ContainerInfo(org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) WebApplicationException(javax.ws.rs.WebApplicationException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) AppInfo(org.apache.hadoop.yarn.server.webapp.dao.AppInfo) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Unstable(org.apache.hadoop.classification.InterfaceStability.Unstable) Public(org.apache.hadoop.classification.InterfaceAudience.Public)

Example 2 with ContainerInfo

use of org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo in project hadoop by apache.

the class AppBlock method generateApplicationTable.

protected void generateApplicationTable(Block html, UserGroupInformation callerUGI, Collection<ApplicationAttemptReport> attempts) {
    // Application Attempt Table
    TBODY<TABLE<Hamlet>> tbody = html.table("#attempts").thead().tr().th(".id", "Attempt ID").th(".started", "Started").th(".node", "Node").th(".logs", "Logs")._()._().tbody();
    StringBuilder attemptsTableData = new StringBuilder("[\n");
    for (final ApplicationAttemptReport appAttemptReport : attempts) {
        AppAttemptInfo appAttempt = new AppAttemptInfo(appAttemptReport);
        ContainerReport containerReport;
        try {
            final GetContainerReportRequest request = GetContainerReportRequest.newInstance(appAttemptReport.getAMContainerId());
            if (callerUGI == null) {
                containerReport = appBaseProt.getContainerReport(request).getContainerReport();
            } else {
                containerReport = callerUGI.doAs(new PrivilegedExceptionAction<ContainerReport>() {

                    @Override
                    public ContainerReport run() throws Exception {
                        ContainerReport report = null;
                        if (request.getContainerId() != null) {
                            try {
                                report = appBaseProt.getContainerReport(request).getContainerReport();
                            } catch (ContainerNotFoundException ex) {
                                LOG.warn(ex.getMessage());
                            }
                        }
                        return report;
                    }
                });
            }
        } catch (Exception e) {
            String message = "Failed to read the AM container of the application attempt " + appAttemptReport.getApplicationAttemptId() + ".";
            LOG.error(message, e);
            html.p()._(message)._();
            return;
        }
        long startTime = 0L;
        String logsLink = null;
        String nodeLink = null;
        if (containerReport != null) {
            ContainerInfo container = new ContainerInfo(containerReport);
            startTime = container.getStartedTime();
            logsLink = containerReport.getLogUrl();
            nodeLink = containerReport.getNodeHttpAddress();
        }
        attemptsTableData.append("[\"<a href='").append(url("appattempt", appAttempt.getAppAttemptId())).append("'>").append(appAttempt.getAppAttemptId()).append("</a>\",\"").append(startTime).append("\",\"<a ").append(nodeLink == null ? "#" : "href='" + nodeLink).append("'>").append(nodeLink == null ? "N/A" : StringEscapeUtils.escapeJavaScript(StringEscapeUtils.escapeHtml(nodeLink))).append("</a>\",\"<a ").append(logsLink == null ? "#" : "href='" + logsLink).append("'>").append(logsLink == null ? "N/A" : "Logs").append("</a>\"],\n");
    }
    if (attemptsTableData.charAt(attemptsTableData.length() - 2) == ',') {
        attemptsTableData.delete(attemptsTableData.length() - 2, attemptsTableData.length() - 1);
    }
    attemptsTableData.append("]");
    html.script().$type("text/javascript")._("var attemptsTableData=" + attemptsTableData)._();
    tbody._()._();
}
Also used : AppAttemptInfo(org.apache.hadoop.yarn.server.webapp.dao.AppAttemptInfo) ApplicationAttemptReport(org.apache.hadoop.yarn.api.records.ApplicationAttemptReport) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) ContainerNotFoundException(org.apache.hadoop.yarn.exceptions.ContainerNotFoundException) GetContainerReportRequest(org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportRequest) TABLE(org.apache.hadoop.yarn.webapp.hamlet.Hamlet.TABLE) ContainerReport(org.apache.hadoop.yarn.api.records.ContainerReport) ContainerInfo(org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo) ContainerNotFoundException(org.apache.hadoop.yarn.exceptions.ContainerNotFoundException)

Example 3 with ContainerInfo

use of org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo in project hadoop by apache.

the class WebServices method getContainer.

public ContainerInfo getContainer(HttpServletRequest req, HttpServletResponse res, String appId, String appAttemptId, String containerId) {
    UserGroupInformation callerUGI = getUser(req);
    ApplicationId aid = parseApplicationId(appId);
    ApplicationAttemptId aaid = parseApplicationAttemptId(appAttemptId);
    final ContainerId cid = parseContainerId(containerId);
    validateIds(aid, aaid, cid);
    ContainerReport container = null;
    try {
        if (callerUGI == null) {
            GetContainerReportRequest request = GetContainerReportRequest.newInstance(cid);
            container = appBaseProt.getContainerReport(request).getContainerReport();
        } else {
            container = callerUGI.doAs(new PrivilegedExceptionAction<ContainerReport>() {

                @Override
                public ContainerReport run() throws Exception {
                    GetContainerReportRequest request = GetContainerReportRequest.newInstance(cid);
                    return appBaseProt.getContainerReport(request).getContainerReport();
                }
            });
        }
    } catch (Exception e) {
        rewrapAndThrowException(e);
    }
    if (container == null) {
        throw new NotFoundException("container with id: " + containerId + " not found");
    }
    return new ContainerInfo(container);
}
Also used : ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) ContainerReport(org.apache.hadoop.yarn.api.records.ContainerReport) ContainerInfo(org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo) ContainerNotFoundException(org.apache.hadoop.yarn.exceptions.ContainerNotFoundException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) ApplicationAttemptNotFoundException(org.apache.hadoop.yarn.exceptions.ApplicationAttemptNotFoundException) ApplicationNotFoundException(org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException) ApplicationAttemptId(org.apache.hadoop.yarn.api.records.ApplicationAttemptId) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) ForbiddenException(org.apache.hadoop.yarn.webapp.ForbiddenException) AuthorizationException(org.apache.hadoop.security.authorize.AuthorizationException) ContainerNotFoundException(org.apache.hadoop.yarn.exceptions.ContainerNotFoundException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) ApplicationAttemptNotFoundException(org.apache.hadoop.yarn.exceptions.ApplicationAttemptNotFoundException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationNotFoundException(org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) GetContainerReportRequest(org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportRequest)

Example 4 with ContainerInfo

use of org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo in project hadoop by apache.

the class AHSWebServices method getContainerLogsInfo.

// TODO: YARN-6080: Create WebServiceUtils to have common functions used in
//       RMWebService, NMWebService and AHSWebService.
/**
   * Returns log file's name as well as current file size for a container.
   *
   * @param req
   *    HttpServletRequest
   * @param res
   *    HttpServletResponse
   * @param containerIdStr
   *    The container ID
   * @param nmId
   *    The Node Manager NodeId
   * @param redirected_from_node
   *    Whether this is a redirected request from NM
   * @return
   *    The log file's name and current file size
   */
@GET
@Path("/containers/{containerid}/logs")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getContainerLogsInfo(@Context HttpServletRequest req, @Context HttpServletResponse res, @PathParam(YarnWebServiceParams.CONTAINER_ID) String containerIdStr, @QueryParam(YarnWebServiceParams.NM_ID) String nmId, @QueryParam(YarnWebServiceParams.REDIRECTED_FROM_NODE) @DefaultValue("false") boolean redirected_from_node) {
    ContainerId containerId = null;
    init(res);
    try {
        containerId = ContainerId.fromString(containerIdStr);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("invalid container id, " + containerIdStr);
    }
    ApplicationId appId = containerId.getApplicationAttemptId().getApplicationId();
    AppInfo appInfo;
    try {
        appInfo = super.getApp(req, res, appId.toString());
    } catch (Exception ex) {
        // directly find logs from HDFS.
        return getContainerLogMeta(appId, null, null, containerIdStr, false);
    }
    // from HDFS.
    if (isFinishedState(appInfo.getAppState())) {
        return getContainerLogMeta(appId, null, null, containerIdStr, false);
    }
    if (isRunningState(appInfo.getAppState())) {
        String appOwner = appInfo.getUser();
        String nodeHttpAddress = null;
        if (nmId != null && !nmId.isEmpty()) {
            try {
                nodeHttpAddress = getNMWebAddressFromRM(conf, nmId);
            } catch (Exception ex) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(ex.getMessage());
                }
            }
        }
        if (nodeHttpAddress == null || nodeHttpAddress.isEmpty()) {
            ContainerInfo containerInfo;
            try {
                containerInfo = super.getContainer(req, res, appId.toString(), containerId.getApplicationAttemptId().toString(), containerId.toString());
            } catch (Exception ex) {
                // It will also return empty log meta for the local logs.
                return getContainerLogMeta(appId, appOwner, null, containerIdStr, true);
            }
            nodeHttpAddress = containerInfo.getNodeHttpAddress();
            // re-directing the request
            if (nodeHttpAddress == null || nodeHttpAddress.isEmpty() || redirected_from_node) {
                // re-direct the request back. Simply output the aggregated log meta.
                return getContainerLogMeta(appId, appOwner, null, containerIdStr, true);
            }
        }
        String uri = "/" + containerId.toString() + "/logs";
        String resURI = JOINER.join(getAbsoluteNMWebAddress(nodeHttpAddress), NM_DOWNLOAD_URI_STR, uri);
        String query = req.getQueryString();
        if (query != null && !query.isEmpty()) {
            resURI += "?" + query;
        }
        ResponseBuilder response = Response.status(HttpServletResponse.SC_TEMPORARY_REDIRECT);
        response.header("Location", resURI);
        return response.build();
    } else {
        throw new NotFoundException("The application is not at Running or Finished State.");
    }
}
Also used : ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) ContainerInfo(org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) WebApplicationException(javax.ws.rs.WebApplicationException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) AppInfo(org.apache.hadoop.yarn.server.webapp.dao.AppInfo) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with ContainerInfo

use of org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo in project hadoop by apache.

the class AppAttemptBlock method render.

@Override
protected void render(Block html) {
    String attemptid = $(APPLICATION_ATTEMPT_ID);
    if (attemptid.isEmpty()) {
        puts("Bad request: requires application attempt ID");
        return;
    }
    try {
        appAttemptId = ApplicationAttemptId.fromString(attemptid);
    } catch (IllegalArgumentException e) {
        puts("Invalid application attempt ID: " + attemptid);
        return;
    }
    UserGroupInformation callerUGI = getCallerUGI();
    ApplicationAttemptReport appAttemptReport;
    try {
        final GetApplicationAttemptReportRequest request = GetApplicationAttemptReportRequest.newInstance(appAttemptId);
        if (callerUGI == null) {
            appAttemptReport = appBaseProt.getApplicationAttemptReport(request).getApplicationAttemptReport();
        } else {
            appAttemptReport = callerUGI.doAs(new PrivilegedExceptionAction<ApplicationAttemptReport>() {

                @Override
                public ApplicationAttemptReport run() throws Exception {
                    return appBaseProt.getApplicationAttemptReport(request).getApplicationAttemptReport();
                }
            });
        }
    } catch (Exception e) {
        String message = "Failed to read the application attempt " + appAttemptId + ".";
        LOG.error(message, e);
        html.p()._(message)._();
        return;
    }
    if (appAttemptReport == null) {
        puts("Application Attempt not found: " + attemptid);
        return;
    }
    boolean exceptionWhenGetContainerReports = false;
    Collection<ContainerReport> containers = null;
    try {
        final GetContainersRequest request = GetContainersRequest.newInstance(appAttemptId);
        if (callerUGI == null) {
            containers = appBaseProt.getContainers(request).getContainerList();
        } else {
            containers = callerUGI.doAs(new PrivilegedExceptionAction<Collection<ContainerReport>>() {

                @Override
                public Collection<ContainerReport> run() throws Exception {
                    return appBaseProt.getContainers(request).getContainerList();
                }
            });
        }
    } catch (RuntimeException e) {
        // have this block to suppress the findbugs warning
        exceptionWhenGetContainerReports = true;
    } catch (Exception e) {
        exceptionWhenGetContainerReports = true;
    }
    AppAttemptInfo appAttempt = new AppAttemptInfo(appAttemptReport);
    setTitle(join("Application Attempt ", attemptid));
    String node = "N/A";
    if (appAttempt.getHost() != null && appAttempt.getRpcPort() >= 0 && appAttempt.getRpcPort() < 65536) {
        node = appAttempt.getHost() + ":" + appAttempt.getRpcPort();
    }
    generateOverview(appAttemptReport, containers, appAttempt, node);
    if (exceptionWhenGetContainerReports) {
        html.p()._("Sorry, Failed to get containers for application attempt" + attemptid + ".")._();
        return;
    }
    createAttemptHeadRoomTable(html);
    html._(InfoBlock.class);
    createTablesForAttemptMetrics(html);
    // Container Table
    TBODY<TABLE<Hamlet>> tbody = html.table("#containers").thead().tr().th(".id", "Container ID").th(".node", "Node").th(".exitstatus", "Container Exit Status").th(".logs", "Logs")._()._().tbody();
    StringBuilder containersTableData = new StringBuilder("[\n");
    for (ContainerReport containerReport : containers) {
        ContainerInfo container = new ContainerInfo(containerReport);
        containersTableData.append("[\"<a href='").append(url("container", container.getContainerId())).append("'>").append(container.getContainerId()).append("</a>\",\"<a ").append(container.getNodeHttpAddress() == null ? "#" : "href='" + container.getNodeHttpAddress()).append("'>").append(container.getNodeHttpAddress() == null ? "N/A" : StringEscapeUtils.escapeJavaScript(StringEscapeUtils.escapeHtml(container.getNodeHttpAddress()))).append("</a>\",\"").append(container.getContainerExitStatus()).append("\",\"<a href='").append(container.getLogUrl() == null ? "#" : container.getLogUrl()).append("'>").append(container.getLogUrl() == null ? "N/A" : "Logs").append("</a>\"],\n");
    }
    if (containersTableData.charAt(containersTableData.length() - 2) == ',') {
        containersTableData.delete(containersTableData.length() - 2, containersTableData.length() - 1);
    }
    containersTableData.append("]");
    html.script().$type("text/javascript")._("var containersTableData=" + containersTableData)._();
    tbody._()._();
}
Also used : AppAttemptInfo(org.apache.hadoop.yarn.server.webapp.dao.AppAttemptInfo) ApplicationAttemptReport(org.apache.hadoop.yarn.api.records.ApplicationAttemptReport) GetApplicationAttemptReportRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportRequest) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) TABLE(org.apache.hadoop.yarn.webapp.hamlet.Hamlet.TABLE) ContainerReport(org.apache.hadoop.yarn.api.records.ContainerReport) ContainerInfo(org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo) GetContainersRequest(org.apache.hadoop.yarn.api.protocolrecords.GetContainersRequest) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Aggregations

ContainerInfo (org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo)7 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)5 ContainerReport (org.apache.hadoop.yarn.api.records.ContainerReport)5 WebApplicationException (javax.ws.rs.WebApplicationException)4 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)4 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)4 ContainerId (org.apache.hadoop.yarn.api.records.ContainerId)4 BadRequestException (org.apache.hadoop.yarn.webapp.BadRequestException)4 NotFoundException (org.apache.hadoop.yarn.webapp.NotFoundException)4 GetContainerReportRequest (org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportRequest)3 ContainerNotFoundException (org.apache.hadoop.yarn.exceptions.ContainerNotFoundException)3 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)2 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)2 IOException (java.io.IOException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 AuthorizationException (org.apache.hadoop.security.authorize.AuthorizationException)2