Search in sources :

Example 11 with LongRange

use of org.apache.commons.lang.math.LongRange in project openhab1-addons by openhab.

the class GCalEventDownloader method processEntries.

/**
     * <p>
     * Iterates through <code>entries</code>, extracts the event content and
     * creates quartz calendars, jobs and corresponding triggers for each event.
     * </p>
     * <p>
     * The following steps are done at event processing:
     * <ul>
     * <li>find events with empty content</li>
     * <li>create a {@link TimeRangeCalendar} for each event (unique by title) and add a TimeRange for each {@link When}
     * </li>
     * <li>add each {@link TimeRangeCalendar} to the {@link Scheduler}</li>
     * <li>find events with content</li>
     * <li>add a Job with the corresponding Triggers for each event</li>
     * </ul>
     *
     * @param entries the GCalendar events to create quart jobs for.
     * @throws SchedulerException if there is an internal Scheduler error.
     */
private void processEntries(List<Event> entries) throws SchedulerException {
    Map<String, TimeRangeCalendar> calendarCache = new HashMap<String, TimeRangeCalendar>();
    // the scheduler
    for (Event event : entries) {
        String eventContent = event.getDescription();
        String eventTitle = event.getSummary();
        if (StringUtils.isBlank(eventContent)) {
            logger.debug("found event '{}' with no content, add this event to the excluded TimeRangesCalendar - this event could be referenced by the modifiedBy clause", eventTitle);
            if (!calendarCache.containsKey(eventTitle)) {
                calendarCache.put(eventTitle, new TimeRangeCalendar());
            }
            TimeRangeCalendar timeRangeCalendar = calendarCache.get(eventTitle);
            timeRangeCalendar.addTimeRange(new LongRange(event.getStart().getDateTime().getValue(), event.getEnd().getDateTime().getValue()));
        }
    }
    // the calendars has to be added first, to schedule Triggers successfully
    for (Entry<String, TimeRangeCalendar> entry : calendarCache.entrySet()) {
        scheduler.addCalendar(entry.getKey(), entry.getValue(), true, true);
    }
    // now we process all events with content
    for (Event event : entries) {
        String eventContent = event.getDescription();
        String eventTitle = event.getSummary();
        if (StringUtils.isNotBlank(eventContent)) {
            CalendarEventContent cec = parseEventContent(eventContent, (eventTitle != null) && eventTitle.startsWith("[PresenceSimulation]"));
            String modifiedByEvent = null;
            if (calendarCache.containsKey(cec.modifiedByEvent)) {
                modifiedByEvent = cec.modifiedByEvent;
            }
            JobDetail startJob = createJob(cec.startCommands, event, true);
            boolean triggersCreated = createTriggerAndSchedule(startJob, event, modifiedByEvent, true);
            if (triggersCreated) {
                logger.debug("created new startJob '{}' with details '{}'", eventTitle, createJobInfo(event, startJob));
            }
            // do only create end-jobs if there are end-commands ...
            if (StringUtils.isNotBlank(cec.endCommands)) {
                JobDetail endJob = createJob(cec.endCommands, event, false);
                triggersCreated = createTriggerAndSchedule(endJob, event, modifiedByEvent, false);
                if (triggersCreated) {
                    logger.debug("created new endJob '{}' with details '{}'", eventTitle, createJobInfo(event, endJob));
                }
            }
        }
    }
}
Also used : TimeRangeCalendar(org.openhab.io.gcal.internal.util.TimeRangeCalendar) JobDetail(org.quartz.JobDetail) LongRange(org.apache.commons.lang.math.LongRange) HashMap(java.util.HashMap) Event(com.google.api.services.calendar.model.Event)

Example 12 with LongRange

use of org.apache.commons.lang.math.LongRange in project hadoop by apache.

the class WebServices method getApps.

public AppsInfo getApps(HttpServletRequest req, HttpServletResponse res, String stateQuery, Set<String> statesQuery, String finalStatusQuery, String userQuery, String queueQuery, String count, String startedBegin, String startedEnd, String finishBegin, String finishEnd, Set<String> applicationTypes) {
    UserGroupInformation callerUGI = getUser(req);
    boolean checkEnd = false;
    boolean checkAppTypes = false;
    boolean checkAppStates = false;
    long countNum = Long.MAX_VALUE;
    // set values suitable in case both of begin/end not specified
    long sBegin = 0;
    long sEnd = Long.MAX_VALUE;
    long fBegin = 0;
    long fEnd = Long.MAX_VALUE;
    if (count != null && !count.isEmpty()) {
        countNum = Long.parseLong(count);
        if (countNum <= 0) {
            throw new BadRequestException("limit value must be greater then 0");
        }
    }
    if (startedBegin != null && !startedBegin.isEmpty()) {
        sBegin = Long.parseLong(startedBegin);
        if (sBegin < 0) {
            throw new BadRequestException("startedTimeBegin must be greater than 0");
        }
    }
    if (startedEnd != null && !startedEnd.isEmpty()) {
        sEnd = Long.parseLong(startedEnd);
        if (sEnd < 0) {
            throw new BadRequestException("startedTimeEnd must be greater than 0");
        }
    }
    if (sBegin > sEnd) {
        throw new BadRequestException("startedTimeEnd must be greater than startTimeBegin");
    }
    if (finishBegin != null && !finishBegin.isEmpty()) {
        checkEnd = true;
        fBegin = Long.parseLong(finishBegin);
        if (fBegin < 0) {
            throw new BadRequestException("finishTimeBegin must be greater than 0");
        }
    }
    if (finishEnd != null && !finishEnd.isEmpty()) {
        checkEnd = true;
        fEnd = Long.parseLong(finishEnd);
        if (fEnd < 0) {
            throw new BadRequestException("finishTimeEnd must be greater than 0");
        }
    }
    if (fBegin > fEnd) {
        throw new BadRequestException("finishTimeEnd must be greater than finishTimeBegin");
    }
    Set<String> appTypes = parseQueries(applicationTypes, false);
    if (!appTypes.isEmpty()) {
        checkAppTypes = true;
    }
    // stateQuery is deprecated.
    if (stateQuery != null && !stateQuery.isEmpty()) {
        statesQuery.add(stateQuery);
    }
    Set<String> appStates = parseQueries(statesQuery, true);
    if (!appStates.isEmpty()) {
        checkAppStates = true;
    }
    AppsInfo allApps = new AppsInfo();
    Collection<ApplicationReport> appReports = null;
    final GetApplicationsRequest request = GetApplicationsRequest.newInstance();
    request.setLimit(countNum);
    request.setStartRange(new LongRange(sBegin, sEnd));
    try {
        if (callerUGI == null) {
            // TODO: the request should take the params like what RMWebServices does
            // in YARN-1819.
            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();
                }
            });
        }
    } catch (Exception e) {
        rewrapAndThrowException(e);
    }
    if (appReports == null) {
        return allApps;
    }
    for (ApplicationReport appReport : appReports) {
        if (checkAppStates && !appStates.contains(StringUtils.toLowerCase(appReport.getYarnApplicationState().toString()))) {
            continue;
        }
        if (finalStatusQuery != null && !finalStatusQuery.isEmpty()) {
            FinalApplicationStatus.valueOf(finalStatusQuery);
            if (!appReport.getFinalApplicationStatus().toString().equalsIgnoreCase(finalStatusQuery)) {
                continue;
            }
        }
        if (userQuery != null && !userQuery.isEmpty()) {
            if (!appReport.getUser().equals(userQuery)) {
                continue;
            }
        }
        if (queueQuery != null && !queueQuery.isEmpty()) {
            if (!appReport.getQueue().equals(queueQuery)) {
                continue;
            }
        }
        if (checkAppTypes && !appTypes.contains(StringUtils.toLowerCase(appReport.getApplicationType().trim()))) {
            continue;
        }
        if (checkEnd && (appReport.getFinishTime() < fBegin || appReport.getFinishTime() > fEnd)) {
            continue;
        }
        AppInfo app = new AppInfo(appReport);
        allApps.add(app);
    }
    return allApps;
}
Also used : PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) GetApplicationsRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest) 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) AppInfo(org.apache.hadoop.yarn.server.webapp.dao.AppInfo) ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) LongRange(org.apache.commons.lang.math.LongRange) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) AppsInfo(org.apache.hadoop.yarn.server.webapp.dao.AppsInfo) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 13 with LongRange

use of org.apache.commons.lang.math.LongRange in project hadoop by apache.

the class ClientRMService method getApplications.

/**
   * Get applications matching the {@link GetApplicationsRequest}. If
   * caseSensitive is set to false, applicationTypes in
   * GetApplicationRequest are expected to be in all-lowercase
   */
@Private
public GetApplicationsResponse getApplications(GetApplicationsRequest request, boolean caseSensitive) throws YarnException {
    UserGroupInformation callerUGI;
    try {
        callerUGI = UserGroupInformation.getCurrentUser();
    } catch (IOException ie) {
        LOG.info("Error getting UGI ", ie);
        throw RPCUtil.getRemoteException(ie);
    }
    Set<String> applicationTypes = request.getApplicationTypes();
    EnumSet<YarnApplicationState> applicationStates = request.getApplicationStates();
    Set<String> users = request.getUsers();
    Set<String> queues = request.getQueues();
    Set<String> tags = request.getApplicationTags();
    long limit = request.getLimit();
    LongRange start = request.getStartRange();
    LongRange finish = request.getFinishRange();
    ApplicationsRequestScope scope = request.getScope();
    final Map<ApplicationId, RMApp> apps = rmContext.getRMApps();
    Iterator<RMApp> appsIter;
    // of those queues by asking the scheduler for the apps in those queues.
    if (queues != null && !queues.isEmpty()) {
        // Construct an iterator over apps in given queues
        // Collect list of lists to avoid copying all apps
        final List<List<ApplicationAttemptId>> queueAppLists = new ArrayList<List<ApplicationAttemptId>>();
        for (String queue : queues) {
            List<ApplicationAttemptId> appsInQueue = scheduler.getAppsInQueue(queue);
            if (appsInQueue != null && !appsInQueue.isEmpty()) {
                queueAppLists.add(appsInQueue);
            }
        }
        appsIter = new Iterator<RMApp>() {

            Iterator<List<ApplicationAttemptId>> appListIter = queueAppLists.iterator();

            Iterator<ApplicationAttemptId> schedAppsIter;

            @Override
            public boolean hasNext() {
                // current list hasNext or whether there are any remaining lists
                return (schedAppsIter != null && schedAppsIter.hasNext()) || appListIter.hasNext();
            }

            @Override
            public RMApp next() {
                if (schedAppsIter == null || !schedAppsIter.hasNext()) {
                    schedAppsIter = appListIter.next().iterator();
                }
                return apps.get(schedAppsIter.next().getApplicationId());
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Remove not supported");
            }
        };
    } else {
        appsIter = apps.values().iterator();
    }
    List<ApplicationReport> reports = new ArrayList<ApplicationReport>();
    while (appsIter.hasNext() && reports.size() < limit) {
        RMApp application = appsIter.next();
        // Check if current application falls under the specified scope
        if (scope == ApplicationsRequestScope.OWN && !callerUGI.getUserName().equals(application.getUser())) {
            continue;
        }
        if (applicationTypes != null && !applicationTypes.isEmpty()) {
            String appTypeToMatch = caseSensitive ? application.getApplicationType() : StringUtils.toLowerCase(application.getApplicationType());
            if (!applicationTypes.contains(appTypeToMatch)) {
                continue;
            }
        }
        if (applicationStates != null && !applicationStates.isEmpty()) {
            if (!applicationStates.contains(application.createApplicationState())) {
                continue;
            }
        }
        if (users != null && !users.isEmpty() && !users.contains(application.getUser())) {
            continue;
        }
        if (start != null && !start.containsLong(application.getStartTime())) {
            continue;
        }
        if (finish != null && !finish.containsLong(application.getFinishTime())) {
            continue;
        }
        if (tags != null && !tags.isEmpty()) {
            Set<String> appTags = application.getApplicationTags();
            if (appTags == null || appTags.isEmpty()) {
                continue;
            }
            boolean match = false;
            for (String tag : tags) {
                if (appTags.contains(tag)) {
                    match = true;
                    break;
                }
            }
            if (!match) {
                continue;
            }
        }
        // checkAccess can grab the scheduler lock so call it last
        boolean allowAccess = checkAccess(callerUGI, application.getUser(), ApplicationAccessType.VIEW_APP, application);
        if (scope == ApplicationsRequestScope.VIEWABLE && !allowAccess) {
            continue;
        }
        reports.add(application.createAndGetApplicationReport(callerUGI.getUserName(), allowAccess));
    }
    GetApplicationsResponse response = recordFactory.newRecordInstance(GetApplicationsResponse.class);
    response.setApplicationList(reports);
    return response;
}
Also used : RMApp(org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp) YarnApplicationState(org.apache.hadoop.yarn.api.records.YarnApplicationState) ArrayList(java.util.ArrayList) LongRange(org.apache.commons.lang.math.LongRange) ArrayList(java.util.ArrayList) List(java.util.List) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) ApplicationsRequestScope(org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope) IOException(java.io.IOException) ApplicationAttemptId(org.apache.hadoop.yarn.api.records.ApplicationAttemptId) ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) GetApplicationsResponse(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Private(org.apache.hadoop.classification.InterfaceAudience.Private)

Example 14 with LongRange

use of org.apache.commons.lang.math.LongRange in project hadoop by apache.

the class TestPBImplRecords method setup.

@BeforeClass
public static void setup() throws Exception {
    typeValueCache.put(LongRange.class, new LongRange(1000, 2000));
    typeValueCache.put(URL.class, URL.newInstance("http", "localhost", 8080, "file0"));
    typeValueCache.put(SerializedException.class, SerializedException.newInstance(new IOException("exception for test")));
    generateByNewInstance(ExecutionTypeRequest.class);
    generateByNewInstance(LogAggregationContext.class);
    generateByNewInstance(ApplicationId.class);
    generateByNewInstance(ApplicationAttemptId.class);
    generateByNewInstance(ContainerId.class);
    generateByNewInstance(Resource.class);
    generateByNewInstance(ResourceBlacklistRequest.class);
    generateByNewInstance(ResourceOption.class);
    generateByNewInstance(LocalResource.class);
    generateByNewInstance(Priority.class);
    generateByNewInstance(NodeId.class);
    generateByNewInstance(NodeReport.class);
    generateByNewInstance(Token.class);
    generateByNewInstance(NMToken.class);
    generateByNewInstance(ResourceRequest.class);
    generateByNewInstance(ApplicationAttemptReport.class);
    generateByNewInstance(ApplicationResourceUsageReport.class);
    generateByNewInstance(ApplicationReport.class);
    generateByNewInstance(Container.class);
    generateByNewInstance(ContainerRetryContext.class);
    generateByNewInstance(ContainerLaunchContext.class);
    generateByNewInstance(ApplicationSubmissionContext.class);
    generateByNewInstance(ContainerReport.class);
    generateByNewInstance(UpdateContainerRequest.class);
    generateByNewInstance(UpdateContainerError.class);
    generateByNewInstance(IncreaseContainersResourceRequest.class);
    generateByNewInstance(IncreaseContainersResourceResponse.class);
    generateByNewInstance(ContainerStatus.class);
    generateByNewInstance(PreemptionContainer.class);
    generateByNewInstance(PreemptionResourceRequest.class);
    generateByNewInstance(PreemptionContainer.class);
    generateByNewInstance(PreemptionContract.class);
    generateByNewInstance(StrictPreemptionContract.class);
    generateByNewInstance(PreemptionMessage.class);
    generateByNewInstance(StartContainerRequest.class);
    generateByNewInstance(NodeLabel.class);
    generateByNewInstance(UpdatedContainer.class);
    // genByNewInstance does not apply to QueueInfo, cause
    // it is recursive(has sub queues)
    typeValueCache.put(QueueInfo.class, QueueInfo.newInstance("root", 1.0f, 1.0f, 0.1f, null, null, QueueState.RUNNING, ImmutableSet.of("x", "y"), "x && y", null, false));
    generateByNewInstance(QueueStatistics.class);
    generateByNewInstance(QueueUserACLInfo.class);
    generateByNewInstance(YarnClusterMetrics.class);
    // for reservation system
    generateByNewInstance(ReservationId.class);
    generateByNewInstance(ReservationRequest.class);
    generateByNewInstance(ReservationRequests.class);
    generateByNewInstance(ReservationDefinition.class);
    generateByNewInstance(ResourceAllocationRequest.class);
    generateByNewInstance(ReservationAllocationState.class);
    generateByNewInstance(ResourceUtilization.class);
    generateByNewInstance(ReInitializeContainerRequest.class);
    generateByNewInstance(ReInitializeContainerResponse.class);
    generateByNewInstance(RestartContainerResponse.class);
    generateByNewInstance(RollbackResponse.class);
    generateByNewInstance(CommitResponse.class);
    generateByNewInstance(ApplicationTimeout.class);
}
Also used : LongRange(org.apache.commons.lang.math.LongRange) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 15 with LongRange

use of org.apache.commons.lang.math.LongRange in project ovirt-engine by oVirt.

the class DecoratedMacPoolFactoryTest method testToString.

@Test
public void testToString() throws Exception {
    Guid underlyingPoolId = Guid.newGuid();
    MacPoolUsingRanges underlyingPool = new MacPoolUsingRanges(underlyingPoolId, Collections.singletonList(new LongRange(1, 2)), false, auditLogDirector);
    DelegatingMacPoolDecorator decoratorA = new DelegatingMacPoolDecorator();
    DelegatingMacPoolDecorator decoratorB = new DelegatingMacPoolDecorator();
    MacPool decoratedPool = decoratePoolWhileNotUsingLocking(underlyingPool, Arrays.asList(decoratorA, decoratorB));
    String expectedToStringResult = String.format("%1$s:{macPool='%2$s:{macPool='%3$s:{id='%4$s'}'}'}", decoratorA.getClass().getSimpleName(), decoratorB.getClass().getSimpleName(), underlyingPool.getClass().getSimpleName(), underlyingPoolId);
    assertThat(decoratedPool.toString(), is(expectedToStringResult));
}
Also used : LongRange(org.apache.commons.lang.math.LongRange) Guid(org.ovirt.engine.core.compat.Guid) Test(org.junit.Test)

Aggregations

LongRange (org.apache.commons.lang.math.LongRange)18 Test (org.junit.Test)6 Date (java.util.Date)3 GetApplicationsRequest (org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest)3 ApplicationReport (org.apache.hadoop.yarn.api.records.ApplicationReport)3 YarnApplicationState (org.apache.hadoop.yarn.api.records.YarnApplicationState)3 IOException (java.io.IOException)2 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)2 ApplicationsRequestScope (org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope)2 GetApplicationsRequestProtoOrBuilder (org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationsRequestProtoOrBuilder)2 BadRequestException (org.apache.hadoop.yarn.webapp.BadRequestException)2 Event (com.google.api.services.calendar.model.Event)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Private (org.apache.hadoop.classification.InterfaceAudience.Private)1