Search in sources :

Example 16 with BadRequestException

use of org.apache.hadoop.yarn.webapp.BadRequestException in project hadoop by apache.

the class NMWebServices method getContainerLogsInfo.

/**
   * Returns log file's name as well as current file size for a container.
   *
   * @param hsr
   *    HttpServletRequest
   * @param res
   *    HttpServletResponse
   * @param containerIdStr
   *    The container ID
   * @return
   *    The log file's name and current file size
   */
@GET
@Path("/containers/{containerid}/logs")
@Produces({ MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8 })
public Response getContainerLogsInfo(@javax.ws.rs.core.Context HttpServletRequest hsr, @javax.ws.rs.core.Context HttpServletResponse res, @PathParam(YarnWebServiceParams.CONTAINER_ID) String containerIdStr) {
    ContainerId containerId = null;
    init();
    try {
        containerId = ContainerId.fromString(containerIdStr);
    } catch (IllegalArgumentException ex) {
        throw new BadRequestException("invalid container id, " + containerIdStr);
    }
    try {
        List<ContainerLogsInfo> containersLogsInfo = new ArrayList<>();
        containersLogsInfo.add(new NMContainerLogsInfo(this.nmContext, containerId, hsr.getRemoteUser(), ContainerLogAggregationType.LOCAL));
        // check whether we have aggregated logs in RemoteFS. If exists, show the
        // the log meta for the aggregated logs as well.
        ApplicationId appId = containerId.getApplicationAttemptId().getApplicationId();
        Application app = this.nmContext.getApplications().get(appId);
        String appOwner = app == null ? null : app.getUser();
        try {
            List<ContainerLogMeta> containerLogMeta = LogToolUtils.getContainerLogMetaFromRemoteFS(this.nmContext.getConf(), appId, containerIdStr, this.nmContext.getNodeId().toString(), appOwner);
            if (!containerLogMeta.isEmpty()) {
                for (ContainerLogMeta logMeta : containerLogMeta) {
                    containersLogsInfo.add(new ContainerLogsInfo(logMeta, ContainerLogAggregationType.AGGREGATED));
                }
            }
        } catch (IOException ex) {
            // Skip it and do nothing
            if (LOG.isDebugEnabled()) {
                LOG.debug(ex.getMessage());
            }
        }
        GenericEntity<List<ContainerLogsInfo>> meta = new GenericEntity<List<ContainerLogsInfo>>(containersLogsInfo) {
        };
        ResponseBuilder resp = Response.ok(meta);
        // Sending the X-Content-Type-Options response header with the value
        // nosniff will prevent Internet Explorer from MIME-sniffing a response
        // away from the declared content-type.
        resp.header("X-Content-Type-Options", "nosniff");
        return resp.build();
    } catch (Exception ex) {
        if (redirectWSUrl == null || redirectWSUrl.isEmpty()) {
            throw new WebApplicationException(ex);
        }
        // redirect the request to the configured log server
        String redirectURI = "/containers/" + containerIdStr + "/logs";
        return createRedirectResponse(hsr, redirectWSUrl, redirectURI);
    }
}
Also used : NMContainerLogsInfo(org.apache.hadoop.yarn.server.nodemanager.webapp.dao.NMContainerLogsInfo) ContainerLogsInfo(org.apache.hadoop.yarn.server.webapp.dao.ContainerLogsInfo) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) ContainerLogMeta(org.apache.hadoop.yarn.logaggregation.ContainerLogMeta) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) IOException(java.io.IOException) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) GenericEntity(javax.ws.rs.core.GenericEntity) NMContainerLogsInfo(org.apache.hadoop.yarn.server.nodemanager.webapp.dao.NMContainerLogsInfo) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) List(java.util.List) ArrayList(java.util.ArrayList) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Application(org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with BadRequestException

use of org.apache.hadoop.yarn.webapp.BadRequestException in project hadoop by apache.

the class HsWebServices method getJobs.

@GET
@Path("/mapreduce/jobs")
@Produces({ MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8 })
public JobsInfo getJobs(@QueryParam("user") String userQuery, @QueryParam("limit") String count, @QueryParam("state") String stateQuery, @QueryParam("queue") String queueQuery, @QueryParam("startedTimeBegin") String startedBegin, @QueryParam("startedTimeEnd") String startedEnd, @QueryParam("finishedTimeBegin") String finishBegin, @QueryParam("finishedTimeEnd") String finishEnd) {
    Long countParam = null;
    init();
    if (count != null && !count.isEmpty()) {
        try {
            countParam = Long.parseLong(count);
        } catch (NumberFormatException e) {
            throw new BadRequestException(e.getMessage());
        }
        if (countParam <= 0) {
            throw new BadRequestException("limit value must be greater then 0");
        }
    }
    Long sBegin = null;
    if (startedBegin != null && !startedBegin.isEmpty()) {
        try {
            sBegin = Long.parseLong(startedBegin);
        } catch (NumberFormatException e) {
            throw new BadRequestException("Invalid number format: " + e.getMessage());
        }
        if (sBegin < 0) {
            throw new BadRequestException("startedTimeBegin must be greater than 0");
        }
    }
    Long sEnd = null;
    if (startedEnd != null && !startedEnd.isEmpty()) {
        try {
            sEnd = Long.parseLong(startedEnd);
        } catch (NumberFormatException e) {
            throw new BadRequestException("Invalid number format: " + e.getMessage());
        }
        if (sEnd < 0) {
            throw new BadRequestException("startedTimeEnd must be greater than 0");
        }
    }
    if (sBegin != null && sEnd != null && sBegin > sEnd) {
        throw new BadRequestException("startedTimeEnd must be greater than startTimeBegin");
    }
    Long fBegin = null;
    if (finishBegin != null && !finishBegin.isEmpty()) {
        try {
            fBegin = Long.parseLong(finishBegin);
        } catch (NumberFormatException e) {
            throw new BadRequestException("Invalid number format: " + e.getMessage());
        }
        if (fBegin < 0) {
            throw new BadRequestException("finishedTimeBegin must be greater than 0");
        }
    }
    Long fEnd = null;
    if (finishEnd != null && !finishEnd.isEmpty()) {
        try {
            fEnd = Long.parseLong(finishEnd);
        } catch (NumberFormatException e) {
            throw new BadRequestException("Invalid number format: " + e.getMessage());
        }
        if (fEnd < 0) {
            throw new BadRequestException("finishedTimeEnd must be greater than 0");
        }
    }
    if (fBegin != null && fEnd != null && fBegin > fEnd) {
        throw new BadRequestException("finishedTimeEnd must be greater than finishedTimeBegin");
    }
    JobState jobState = null;
    if (stateQuery != null) {
        jobState = JobState.valueOf(stateQuery);
    }
    return ctx.getPartialJobs(0l, countParam, userQuery, queueQuery, sBegin, sEnd, fBegin, fEnd, jobState);
}
Also used : BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) JobState(org.apache.hadoop.mapreduce.v2.api.records.JobState) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 18 with BadRequestException

use of org.apache.hadoop.yarn.webapp.BadRequestException in project hadoop by apache.

the class AppsBlock method fetchData.

protected void fetchData() throws YarnException, IOException, InterruptedException {
    reqAppStates = EnumSet.noneOf(YarnApplicationState.class);
    String reqStateString = $(APP_STATE);
    if (reqStateString != null && !reqStateString.isEmpty()) {
        String[] appStateStrings = reqStateString.split(",");
        for (String stateString : appStateStrings) {
            reqAppStates.add(YarnApplicationState.valueOf(stateString.trim()));
        }
    }
    callerUGI = getCallerUGI();
    final GetApplicationsRequest request = GetApplicationsRequest.newInstance(reqAppStates);
    String appsNumStr = $(APPS_NUM);
    if (appsNumStr != null && !appsNumStr.isEmpty()) {
        long appsNum = Long.parseLong(appsNumStr);
        request.setLimit(appsNum);
    }
    String appStartedTimeBegainStr = $(APP_START_TIME_BEGIN);
    long appStartedTimeBegain = 0;
    if (appStartedTimeBegainStr != null && !appStartedTimeBegainStr.isEmpty()) {
        appStartedTimeBegain = Long.parseLong(appStartedTimeBegainStr);
        if (appStartedTimeBegain < 0) {
            throw new BadRequestException("app.started-time.begin must be greater than 0");
        }
    }
    String appStartedTimeEndStr = $(APP_START_TIME_END);
    long appStartedTimeEnd = Long.MAX_VALUE;
    if (appStartedTimeEndStr != null && !appStartedTimeEndStr.isEmpty()) {
        appStartedTimeEnd = Long.parseLong(appStartedTimeEndStr);
        if (appStartedTimeEnd < 0) {
            throw new BadRequestException("app.started-time.end must be greater than 0");
        }
    }
    if (appStartedTimeBegain > appStartedTimeEnd) {
        throw new BadRequestException("app.started-time.end must be greater than app.started-time.begin");
    }
    request.setStartRange(new LongRange(appStartedTimeBegain, appStartedTimeEnd));
    if (callerUGI == null) {
        appReports = appBaseProt.getApplications(request).getApplicationList();
    } else {
        appReports = callerUGI.doAs(new PrivilegedExceptionAction<Collection<ApplicationReport>>() {

            @Override
            public Collection<ApplicationReport> run() throws Exception {
                return appBaseProt.getApplications(request).getApplicationList();
            }
        });
    }
}
Also used : ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) LongRange(org.apache.commons.lang.math.LongRange) YarnApplicationState(org.apache.hadoop.yarn.api.records.YarnApplicationState) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) GetApplicationsRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest)

Example 19 with BadRequestException

use of org.apache.hadoop.yarn.webapp.BadRequestException in project hadoop by apache.

the class TimelineReaderWebServices method handleException.

private static void handleException(Exception e, String url, long startTime, String invalidNumMsg) throws BadRequestException, WebApplicationException {
    long endTime = Time.monotonicNow();
    LOG.info("Processed URL " + url + " but encountered exception (Took " + (endTime - startTime) + " ms.)");
    if (e instanceof NumberFormatException) {
        throw new BadRequestException(invalidNumMsg + " is not a numeric value.");
    } else if (e instanceof IllegalArgumentException) {
        throw new BadRequestException(e.getMessage() == null ? "Requested Invalid Field." : e.getMessage());
    } else if (e instanceof NotFoundException) {
        throw (NotFoundException) e;
    } else if (e instanceof TimelineParseException) {
        throw new BadRequestException(e.getMessage() == null ? "Filter Parsing failed." : e.getMessage());
    } else if (e instanceof BadRequestException) {
        throw (BadRequestException) e;
    } else {
        LOG.error("Error while processing REST request", e);
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException)

Example 20 with BadRequestException

use of org.apache.hadoop.yarn.webapp.BadRequestException in project hadoop by apache.

the class TimelineReaderWebServices method getFlowRunApps.

/**
   * Return a list of apps for given UID which is a delimited string containing
   * clusterid, userid, flow name and flowrun id. If number of matching apps are
   * more than the limit, most recent apps till the limit is reached, will be
   * returned.
   *
   * @param req Servlet request.
   * @param res Servlet response.
   * @param uId a delimited string containing clusterid, userid, flow name and
   *     flowrun id which are extracted from UID and then used to query backend.
   *     (Mandatory path param).
   * @param limit If specified, defines the number of apps to return. The
   *     maximum possible value for limit can be {@link Long#MAX_VALUE}. If it
   *     is not specified or has a value less than 0, then limit will be
   *     considered as 100. (Optional query param).
   * @param createdTimeStart If specified, matched apps should not be created
   *     before this timestamp(Optional query param).
   * @param createdTimeEnd If specified, matched apps should not be created
   *     after this timestamp(Optional query param).
   * @param relatesTo If specified, matched apps should relate to given
   *     entities associated with a entity type. relatesto is a comma separated
   *     list in the format [entitytype]:[entityid1]:[entityid2]... (Optional
   *     query param).
   * @param isRelatedTo If specified, matched apps should be related to given
   *     entities associated with a entity type. relatesto is a comma separated
   *     list in the format [entitytype]:[entityid1]:[entityid2]... (Optional
   *     query param).
   * @param infofilters If specified, matched apps should have exact matches
   *     to the given info represented as key-value pairs. This is represented
   *     as infofilters=info1:value1,info2:value2... (Optional query param).
   * @param conffilters If specified, matched apps should have exact matches
   *     to the given configs represented as key-value pairs. This is
   *     represented as conffilters=conf1:value1,conf2:value2... (Optional query
   *     param).
   * @param metricfilters If specified, matched apps should contain the given
   *     metrics. This is represented as metricfilters=metricid1, metricid2...
   *     (Optional query param).
   * @param eventfilters If specified, matched apps should contain the given
   *     events. This is represented as eventfilters=eventid1, eventid2...
   * @param confsToRetrieve If specified, defines which configurations to
   *     retrieve and send back in response. These configs will be retrieved
   *     irrespective of whether configs are specified in fields to retrieve or
   *     not.
   * @param metricsToRetrieve If specified, defines which metrics to retrieve
   *     and send back in response. These metrics will be retrieved
   *     irrespective of whether metrics are specified in fields to retrieve or
   *     not.
   * @param fields Specifies which fields of the app entity object to retrieve,
   *     see {@link Field}. All fields will be retrieved if fields=ALL. If not
   *     specified, 3 fields i.e. entity type(equivalent to YARN_APPLICATION),
   *     app id and app created time is returned(Optional query param).
   * @param metricsLimit If specified, defines the number of metrics to return.
   *     Considered only if fields contains METRICS/ALL or metricsToRetrieve is
   *     specified. Ignored otherwise. The maximum possible value for
   *     metricsLimit can be {@link Integer#MAX_VALUE}. If it is not specified
   *     or has a value less than 1, and metrics have to be retrieved, then
   *     metricsLimit will be considered as 1 i.e. latest single value of
   *     metric(s) will be returned. (Optional query param).
   *
   * @return If successful, a HTTP 200(OK) response having a JSON representing
   *     a set of <cite>TimelineEntity</cite> instances representing apps is
   *     returned.<br>
   *     On failures,<br>
   *     If any problem occurs in parsing request or UID is incorrect,
   *     HTTP 400(Bad Request) is returned.<br>
   *     For all other errors while retrieving data, HTTP 500(Internal Server
   *     Error) is returned.
   */
@GET
@Path("/run-uid/{uid}/apps")
@Produces(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8)
public Set<TimelineEntity> getFlowRunApps(@Context HttpServletRequest req, @Context HttpServletResponse res, @PathParam("uid") String uId, @QueryParam("limit") String limit, @QueryParam("createdtimestart") String createdTimeStart, @QueryParam("createdtimeend") String createdTimeEnd, @QueryParam("relatesto") String relatesTo, @QueryParam("isrelatedto") String isRelatedTo, @QueryParam("infofilters") String infofilters, @QueryParam("conffilters") String conffilters, @QueryParam("metricfilters") String metricfilters, @QueryParam("eventfilters") String eventfilters, @QueryParam("confstoretrieve") String confsToRetrieve, @QueryParam("metricstoretrieve") String metricsToRetrieve, @QueryParam("fields") String fields, @QueryParam("metricslimit") String metricsLimit) {
    String url = req.getRequestURI() + (req.getQueryString() == null ? "" : QUERY_STRING_SEP + req.getQueryString());
    UserGroupInformation callerUGI = TimelineReaderWebServicesUtils.getUser(req);
    LOG.info("Received URL " + url + " from user " + TimelineReaderWebServicesUtils.getUserName(callerUGI));
    long startTime = Time.monotonicNow();
    init(res);
    TimelineReaderManager timelineReaderManager = getTimelineReaderManager();
    Set<TimelineEntity> entities = null;
    try {
        TimelineReaderContext context = TimelineUIDConverter.FLOWRUN_UID.decodeUID(uId);
        if (context == null) {
            throw new BadRequestException("Incorrect UID " + uId);
        }
        context.setEntityType(TimelineEntityType.YARN_APPLICATION.toString());
        entities = timelineReaderManager.getEntities(context, TimelineReaderWebServicesUtils.createTimelineEntityFilters(limit, createdTimeStart, createdTimeEnd, relatesTo, isRelatedTo, infofilters, conffilters, metricfilters, eventfilters), TimelineReaderWebServicesUtils.createTimelineDataToRetrieve(confsToRetrieve, metricsToRetrieve, fields, metricsLimit));
    } catch (Exception e) {
        handleException(e, url, startTime, "createdTime start/end or limit or flowrunid");
    }
    long endTime = Time.monotonicNow();
    if (entities == null) {
        entities = Collections.emptySet();
    }
    LOG.info("Processed URL " + url + " (Took " + (endTime - startTime) + " ms.)");
    return entities;
}
Also used : BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) TimelineEntity(org.apache.hadoop.yarn.api.records.timelineservice.TimelineEntity) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) ParseException(java.text.ParseException) WebApplicationException(javax.ws.rs.WebApplicationException) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

BadRequestException (org.apache.hadoop.yarn.webapp.BadRequestException)38 Produces (javax.ws.rs.Produces)27 Path (javax.ws.rs.Path)26 IOException (java.io.IOException)18 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)18 GET (javax.ws.rs.GET)16 YarnException (org.apache.hadoop.yarn.exceptions.YarnException)16 NotFoundException (org.apache.hadoop.yarn.webapp.NotFoundException)16 AuthorizationException (org.apache.hadoop.security.authorize.AuthorizationException)13 WebApplicationException (javax.ws.rs.WebApplicationException)12 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)9 ForbiddenException (org.apache.hadoop.yarn.webapp.ForbiddenException)9 POST (javax.ws.rs.POST)8 Consumes (javax.ws.rs.Consumes)7 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)6 ParseException (java.text.ParseException)6 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)6 YarnRuntimeException (org.apache.hadoop.yarn.exceptions.YarnRuntimeException)6 ArrayList (java.util.ArrayList)4 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)3