Search in sources :

Example 1 with TimestampedURIQueryResult

use of com.emc.storageos.db.client.TimestampedURIQueryResult in project coprhd-controller by CoprHD.

the class TaskService method getLatestTasks.

// This method uses heap sort to return latest n tasks where n <= 10K.
private TasksList getLatestTasks(Set<URI> tenantIds, String startTime, String endTime, Integer maxCount) {
    PriorityQueue<TimestampedURIQueryResult.TimestampedURI> taskHeap = new PriorityQueue<>(maxCount, new MinTaskComparator());
    Date startWindowDate = TimeUtils.getDateTimestamp(startTime);
    Date endWindowDate = TimeUtils.getDateTimestamp(endTime);
    // Fetch index entries and load into sorted set
    int taskCount = 0;
    for (URI normalizedTenantId : tenantIds) {
        log.debug("Retriving tasks from tenant {}", normalizedTenantId);
        TimestampedURIQueryResult taskIds = new TimestampedURIQueryResult();
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getTimedTenantOrgTaskConstraint(normalizedTenantId, startWindowDate, endWindowDate), taskIds);
        Iterator<TimestampedURIQueryResult.TimestampedURI> it = taskIds.iterator();
        while (it.hasNext()) {
            TimestampedURIQueryResult.TimestampedURI timestampedURI = it.next();
            // Add first maxCount tasks to PQ
            if (taskHeap.size() < maxCount) {
                taskHeap.add(timestampedURI);
                taskCount++;
            } else {
                // Add the rest tasks into PQ if task timestamp is >= than the lowest timestamp task in PQ
                if (timestampedURI.getTimestamp() >= taskHeap.peek().getTimestamp()) {
                    taskHeap.poll();
                    taskHeap.add(timestampedURI);
                }
                taskCount++;
            }
        }
    }
    log.debug("The number of tasks of all tenants is {}, heap size is {}", taskCount, taskHeap.size());
    List<NamedRelatedResourceRep> resourceReps = Lists.newArrayList();
    while (!taskHeap.isEmpty()) {
        TimestampedURIQueryResult.TimestampedURI uri = taskHeap.poll();
        RestLinkRep link = new RestLinkRep("self", RestLinkFactory.newLink(ResourceTypeEnum.TASK, uri.getUri()));
        resourceReps.add(new NamedRelatedResourceRep(uri.getUri(), link, uri.getName()));
    }
    return new TasksList(resourceReps);
}
Also used : RestLinkRep(com.emc.storageos.model.RestLinkRep) TimestampedURIQueryResult(com.emc.storageos.db.client.TimestampedURIQueryResult) PriorityQueue(java.util.PriorityQueue) NamedRelatedResourceRep(com.emc.storageos.model.NamedRelatedResourceRep) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) Date(java.util.Date) AggregatedConstraint(com.emc.storageos.db.client.constraint.AggregatedConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) TasksList(com.emc.storageos.model.tasks.TasksList)

Example 2 with TimestampedURIQueryResult

use of com.emc.storageos.db.client.TimestampedURIQueryResult in project coprhd-controller by CoprHD.

the class TaskService method getAllTasks.

// Original method to return task list. Will be used when max_count is either NOT specified or set but > max limit like 10K.
// This could cause out of memory issue
private TasksList getAllTasks(Set<URI> tenantIds, String startTime, String endTime, Integer maxCount) {
    // Entries from the index, sorted with most recent first
    Set<TimestampedURIQueryResult.TimestampedURI> sortedIndexEntries = Sets.newTreeSet(new TaskComparator());
    Date startWindowDate = TimeUtils.getDateTimestamp(startTime);
    Date endWindowDate = TimeUtils.getDateTimestamp(endTime);
    // Fetch index entries and load into sorted set
    List<NamedRelatedResourceRep> resourceReps = Lists.newArrayList();
    for (URI normalizedTenantId : tenantIds) {
        TimestampedURIQueryResult taskIds = new TimestampedURIQueryResult();
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getTimedTenantOrgTaskConstraint(normalizedTenantId, startWindowDate, endWindowDate), taskIds);
        Iterator<TimestampedURIQueryResult.TimestampedURI> it = taskIds.iterator();
        while (it.hasNext()) {
            TimestampedURIQueryResult.TimestampedURI timestampedURI = it.next();
            sortedIndexEntries.add(timestampedURI);
        }
    }
    if (maxCount == null || maxCount < 0) {
        maxCount = FETCH_ALL;
    } else {
        maxCount = Math.min(maxCount, sortedIndexEntries.size());
    }
    // Produce the requested number of results
    Iterator<TimestampedURIQueryResult.TimestampedURI> it = sortedIndexEntries.iterator();
    int pos = 0;
    while (it.hasNext() && (maxCount == FETCH_ALL || pos < maxCount)) {
        TimestampedURIQueryResult.TimestampedURI uri = it.next();
        RestLinkRep link = new RestLinkRep("self", RestLinkFactory.newLink(ResourceTypeEnum.TASK, uri.getUri()));
        resourceReps.add(new NamedRelatedResourceRep(uri.getUri(), link, uri.getName()));
        pos++;
    }
    return new TasksList(resourceReps);
}
Also used : RestLinkRep(com.emc.storageos.model.RestLinkRep) TimestampedURIQueryResult(com.emc.storageos.db.client.TimestampedURIQueryResult) NamedRelatedResourceRep(com.emc.storageos.model.NamedRelatedResourceRep) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) Date(java.util.Date) AggregatedConstraint(com.emc.storageos.db.client.constraint.AggregatedConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) TasksList(com.emc.storageos.model.tasks.TasksList)

Aggregations

TimestampedURIQueryResult (com.emc.storageos.db.client.TimestampedURIQueryResult)2 AggregatedConstraint (com.emc.storageos.db.client.constraint.AggregatedConstraint)2 Constraint (com.emc.storageos.db.client.constraint.Constraint)2 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)2 NamedURI (com.emc.storageos.db.client.model.NamedURI)2 NamedRelatedResourceRep (com.emc.storageos.model.NamedRelatedResourceRep)2 RestLinkRep (com.emc.storageos.model.RestLinkRep)2 TasksList (com.emc.storageos.model.tasks.TasksList)2 URI (java.net.URI)2 Date (java.util.Date)2 PriorityQueue (java.util.PriorityQueue)1