Package org.rhq.core.domain.measurement

Examples of org.rhq.core.domain.measurement.Availability


     * @return the new availability interval for a given resource, or null if there is already an existing availability
     */
    private Availability getNewInterval(ResourceIdWithAvailabilityComposite record, Date startDate,
        AvailabilityType aType) {
        // if there is already an existing availability, update it
        Availability old = record.getAvailability();

        if (old != null) {
            if (old.getAvailabilityType() == aType) {
                // existing availability is the same type, just extend it without creating a new entity
                old.setEndTime(null); // don't really need to do this; just enforces the fact that we extend the last interval
                return null;
            }

            old.setEndTime(startDate.getTime());
        }

        Resource resource = new Resource();
        resource.setId(record.getResourceId());

        Availability newAvail = new Availability(resource, startDate.getTime(), aType);
        entityManager.persist(newAvail);

        return newAvail;
    }
View Full Code Here


        // get the existing availability interval where the new availability will be shoe-horned in
        Query query = entityManager.createNamedQuery(Availability.FIND_BY_RESOURCE_AND_DATE);
        query.setParameter("resourceId", toInsert.getResource().getId());
        query.setParameter("aTime", toInsert.getStartTime());

        Availability existing;

        try {
            existing = (Availability) query.getSingleResult();

        } catch (NoResultException nre) {
            // this should never happen since we create an initial Availability when the resource is persisted.
            log.warn("Resource [" + toInsert.getResource()
                + "] has no Availabilities, this should not happen.  Correcting situation by adding an Availability.");

            // we are inserting this as the very first interval
            query = entityManager.createNamedQuery(Availability.FIND_BY_RESOURCE);
            query.setParameter("resourceId", toInsert.getResource().getId());
            query.setMaxResults(1); // we only need the very first one
            Availability firstAvail = ((List<Availability>) query.getResultList()).get(0);

            // only add a new row if its a different status; otherwise, just move the first interval back further
            if (firstAvail.getAvailabilityType() != toInsert.getAvailabilityType()) {
                toInsert.setEndTime(firstAvail.getStartTime());
                entityManager.persist(toInsert);
            } else {
                firstAvail.setStartTime(toInsert.getStartTime());
            }

            return;

        } catch (NonUniqueResultException nure) {
            // This should not happen but can happen if the startTime exactly matches an existing start time. In
            // this case assume we have somehow been passed a duplicate report, and ignore the entry.
            log.warn("Resource [" + toInsert.getResource()
                + "] received a duplicate Availability. It is being ignored: " + toInsert);

            return;
        }

        // If we are inserting the same availability type, the first one can just continue
        // and there is nothing to do!
        if (existing.getAvailabilityType() != toInsert.getAvailabilityType()) {

            // get the afterExisting availability. note: we are assured this query will return something;
            // semantics of this method is that it is never called if we are inserting in the last interval
            query = entityManager.createNamedQuery(Availability.FIND_BY_RESOURCE_AND_DATE);
            query.setParameter("resourceId", toInsert.getResource().getId());
            query.setParameter("aTime", existing.getEndTime() + 1);
            Availability afterExisting = (Availability) query.getSingleResult();

            if (toInsert.getAvailabilityType() == afterExisting.getAvailabilityType()) {
                // the inserted avail type is the same as the following avail type, we don't need to
                // insert a new avail record, just adjust the start/end times of the existing records.

                if (existing.getStartTime() == toInsert.getStartTime()) {
                    // Edge Case: If the insertTo start time equals the existing start time
                    // just remove the existing record and let afterExisting cover the interval.
                    entityManager.remove(existing);
                } else {
                    existing.setEndTime(toInsert.getStartTime());
                }

                // stretch next interval to cover the inserted interval
                afterExisting.setStartTime(toInsert.getStartTime());

            } else {
                // the inserted avail type is NOT the same as the following avail type, we likely need to
                // insert a new avail record.

                if (existing.getStartTime() == toInsert.getStartTime()) {
                    // Edge Case: If the insertTo start time equals the existing end time
                    // just update the existing avail type to be the new avail type and keep the same boundary.
                    existing.setAvailabilityType(toInsert.getAvailabilityType());

                } else {
                    // insert the new avail type interval, witch is different than existing and afterExisting.
                    existing.setEndTime(toInsert.getStartTime());
                    toInsert.setEndTime(afterExisting.getStartTime());
                    entityManager.persist(toInsert);
                }
            }
        }
View Full Code Here

        // At this point we should be able to just checkConditions because if there is only one avail record for the
        // duration period it means nothing has changed.  But, we'll perform a sanity check just to ensure the avail
        // type is what we think it should be...

        Availability avail = avails.get(0);
        AvailabilityType availType = avail.getAvailabilityType();

        boolean checkConditions = false;
        switch (operator) {
        case AVAIL_DURATION_DOWN:
            checkConditions = (AvailabilityType.DOWN == availType);
View Full Code Here

    public Availability getAvailabilityIfKnown(Resource resource) {
        ResourceContainer resourceContainer = getResourceContainer(resource);

        if (resourceContainer != null) {
            if (ResourceComponentState.STARTED == resourceContainer.getResourceComponentState()) {
                Availability availability = resourceContainer.getAvailability();
                return availability;
            }
        }

        return null;
View Full Code Here

    public Availability updateAvailability(AvailabilityType availabilityType) {
        synchronized (this) {
            this.currentAvailType = availabilityType;
            this.currentAvailStart = System.currentTimeMillis();

            Availability tmp = new Availability(this.resource, availabilityType);
            tmp.setStartTime(this.currentAvailStart);
            return tmp;
        }
    }
View Full Code Here

     * @return resource's availability or <code>null</code> if it is not known
     */
    @Nullable
    public Availability getAvailability() {
        synchronized (this) {
            Availability tmp = new Availability(this.resource, this.currentAvailType);
            tmp.setStartTime(this.currentAvailStart);
            return tmp;
        }
    }
View Full Code Here

        return result;
    }

    private AvailabilityType getLastAvailabilityType() {
        Availability av = this.resourceContainer.getAvailability();
        if (av != null) {
            AvailabilityType avt = av.getAvailabilityType();
            return (avt != null) ? avt : AvailabilityType.UNKNOWN;
        } else {
            return AvailabilityType.UNKNOWN;
        }
    }
View Full Code Here

        // SERVER.  This means we won't miss an opportunity to do discovery for stale DOWN resource, and we won't
        // waste time doing discovery on a stale UP SERVER, which can be time consuming.  Since most resources are
        // SERVICEs, and also are typically UP and stay UP, performing checks in these two situations should
        // not add much overhead. Finally, make sure to use facet proxy to do the avail check, this allows us to use
        // a timeout, and therefore not hang discovery if the avail check is slow.
        Availability currentAvailability = parentContainer.getAvailability();
        AvailabilityType currentAvailabilityType = (null == currentAvailability) ? AvailabilityType.DOWN
            : currentAvailability.getAvailabilityType();

        // If there is no current avail, or this is a SERVER, we must perform the live check.
        if (AvailabilityType.UP != currentAvailabilityType
            || ResourceCategory.SERVER == parentContainer.getResource().getResourceType().getCategory()) {
View Full Code Here

        AvailabilityType parentAvailabilityType = null;

        //determine the parent availability
        Resource parent = scanRoot.getParentResource();
        while (parent != null) {
            Availability parentAvail = inventoryManager.getAvailabilityIfKnown(parent);
            if (parentAvail != null && parentAvail.getAvailabilityType() == DOWN) {
                parentAvailabilityType = DOWN;
                break;
            }

            parent = parent.getParentResource();
View Full Code Here

                }
            }
        }

        // find out what the avail was the last time we checked it. this may be null
        Availability previous = this.inventoryManager.getAvailabilityIfKnown(resource);
        AvailabilityType previousType = (null == previous) ? UNKNOWN : previous.getAvailabilityType();
        AvailabilityType current = null;

        // If the resource's parent is DOWN, the rules are that the resource and all of the parent's other
        // descendants, must also be DOWN. So, there's no need to even ask the resource component
        // for its current availability - its current avail is set to the parent avail type and that's that.
        // Otherwise, checkAvail as needed.
        if (deferToParent || (DOWN == parentAvailType)) {
            current = parentAvailType;
            ++scan.numDeferToParent;

            // For the DOWN parent case it's unclear to me whether we should push out the avail check time of
            // the child.  For now, we'll leave it alone and let the next check happen according to the
            // schedule already established.

            if (traceEnabled) {
                LOG.trace("Gave parent availability " + parentAvailType + " to " + resource);
            }
        } else {
            // regardless of whether the avail schedule is met, we still must check avail if isForce is true or if
            // it's a full report and we don't yet have an avail for the resource.
            if (!checkAvail && (isForced || (scan.isFull && null == previous))) {
                checkAvail = true;
            }

            if (checkAvail) {
                if (traceEnabled) {
                    LOG.trace("Now checking availability for " + resource);
                }

                try {
                    ++scan.numGetAvailabilityCalls;

                    // if the component is started, ask what its current availability is as of right now;
                    // if it's not started, then assume it's down, and the next time we check,
                    // we'll see if it's started and check for real then - otherwise, keep assuming it's
                    // down (this is for the case when a plugin component can't start for whatever reason
                    // or is just slow to start)
                    if (resourceContainer.getResourceComponentState() == ResourceComponentState.STARTED) {
                        current = translate(resourceAvailabilityProxy.getAvailability(), previousType);

                    } else {
                        // try to start the component and then perform the avail check
                        this.inventoryManager.activateResource(resource, resourceContainer, false);
                        if (resourceContainer.getResourceComponentState() == ResourceComponentState.STARTED) {
                            current = translate(resourceAvailabilityProxy.getAvailability(), previousType);
                        } else {
                            current = DOWN;
                        }
                    }
                    if (traceEnabled) {
                        LOG.trace("Current availability is " + current + " for " + resource);
                    }
                } catch (Throwable t) {
                    ResourceError resourceError = new ResourceError(resource, ResourceErrorType.AVAILABILITY_CHECK,
                        t.getLocalizedMessage(), ThrowableUtil.getStackAsString(t), System.currentTimeMillis());
                    this.inventoryManager.sendResourceErrorToServer(resourceError);
                    LOG.warn("Availability collection failed with exception on " + resource
                        + ", availability will be reported as " + DOWN.name() + ", reason=" + t.getMessage());
                    current = DOWN;
                }
            } else {
                current = previousType;
            }
        }

        // Add the availability to the report if it changed from its previous state or if this is a full report.
        // Update the resource container only if the avail has changed.
        boolean availChanged = (UNKNOWN != current && current != previousType);

        if (availChanged || scan.isFull) {
            Availability availability;

            if (availChanged) {
                if (traceEnabled) {
                    LOG.trace("Availability changed for " + resource);
                }
                ++scan.numAvailabilityChanges;

                availability = this.inventoryManager.updateAvailability(resource, current);

                // if the resource avail changed to UP then we must perform avail checks for all
                // children, to ensure their avails are up to date. Note that if it changed to NOT UP
                // then the children will just get the parent avail type and there is no avail check anyway.
                if (!isForced && (UP == current)) {
                    if (traceEnabled) {
                        LOG.trace("Forcing availability check for children of " + resource);
                    }
                    isForced = true;
                }
            } else {
                // avoid the overhead of updating the resource container, the avail type did not change
                availability = new Availability(resource, current);
            }

            // update the report
            availabilityReport.addAvailability(availability);
        }
View Full Code Here

TOP

Related Classes of org.rhq.core.domain.measurement.Availability

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.