Search in sources :

Example 6 with MutableObject

use of org.apache.commons.lang.mutable.MutableObject in project uPortal by Jasig.

the class JpaBaseAggregationDaoTest method testModifyingClosedAggregationRangeQuery.

@Test
public final void testModifyingClosedAggregationRangeQuery() throws Exception {
    final IEntityGroup entityGroupA = mock(IEntityGroup.class);
    when(entityGroupA.getServiceName()).thenReturn(new CompositeName("local"));
    when(entityGroupA.getName()).thenReturn("Group A");
    when(compositeGroupService.findGroup("local.0")).thenReturn(entityGroupA);
    final MutableInt aggrs = new MutableInt();
    //Create 10 minutes of aggregations
    final DateTime start = new DateTime(1326734644000l, DateTimeZone.UTC).minuteOfDay().roundFloorCopy();
    final DateTime end = start.plusMinutes(10);
    final AggregationInterval interval = AggregationInterval.FIVE_MINUTE;
    final MutableObject startObj = new MutableObject();
    final MutableObject endObj = new MutableObject();
    this.executeInTransaction(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Random r = new Random(0);
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            populateDateTimeDimensions(start, end, new FunctionWithoutResult<Tuple<DateDimension, TimeDimension>>() {

                @Override
                protected void applyWithoutResult(Tuple<DateDimension, TimeDimension> input) {
                    final TimeDimension td = input.second;
                    final DateDimension dd = input.first;
                    final DateTime instant = td.getTime().toDateTime(dd.getDate());
                    if (startObj.getValue() == null) {
                        startObj.setValue(instant);
                    }
                    endObj.setValue(instant);
                    if (instant.equals(interval.determineStart(instant))) {
                        final AggregationIntervalInfo intervalInfo = aggregationIntervalHelper.getIntervalInfo(interval, instant);
                        final T baseAggregationA = getAggregationDao().createAggregation(createAggregationKey(intervalInfo, groupA));
                        for (int u = 0; u < r.nextInt(50); u++) {
                            updateAggregation(intervalInfo, baseAggregationA, r);
                        }
                        baseAggregationA.intervalComplete(5);
                        getAggregationDao().updateAggregation(baseAggregationA);
                        aggrs.add(1);
                    }
                }
            });
        }
    });
    //Verify all aggrs created
    assertEquals(2, aggrs.intValue());
    //Find unclosed 1 aggr
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Random r = new Random(0);
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final K key = createAggregationKey(interval, groupA);
            final List<T> aggregations = getAggregationDao().getAggregations(start.minusDays(1), end.plusDays(1), key);
            assertEquals(2, aggregations.size());
            for (final T baseAggregationImpl : aggregations) {
                final DateTime instant = baseAggregationImpl.getDateTime();
                final AggregationIntervalInfo intervalInfo = aggregationIntervalHelper.getIntervalInfo(interval, instant);
                updateAggregation(intervalInfo, baseAggregationImpl, r);
            //TODO verify unchanged
            }
        }
    });
}
Also used : CompositeName(javax.naming.CompositeName) DateTime(org.joda.time.DateTime) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) IEntityGroup(org.apereo.portal.groups.IEntityGroup) FunctionWithoutResult(org.apereo.portal.concurrency.FunctionWithoutResult) AggregatedGroupMapping(org.apereo.portal.events.aggr.groups.AggregatedGroupMapping) Random(java.util.Random) MutableInt(org.apache.commons.lang.mutable.MutableInt) LinkedList(java.util.LinkedList) List(java.util.List) Tuple(org.apereo.portal.utils.Tuple) MutableObject(org.apache.commons.lang.mutable.MutableObject) Test(org.junit.Test) BaseAggrEventsJpaDaoTest(org.apereo.portal.test.BaseAggrEventsJpaDaoTest)

Example 7 with MutableObject

use of org.apache.commons.lang.mutable.MutableObject in project uPortal by Jasig.

the class PortalRawEventsAggregatorImpl method doAggregateRawEventsInternal.

private EventProcessingResult doAggregateRawEventsInternal() {
    if (!this.clusterLockService.isLockOwner(AGGREGATION_LOCK_NAME)) {
        throw new IllegalStateException("The cluster lock " + AGGREGATION_LOCK_NAME + " must be owned by the current thread and server");
    }
    if (!this.portalEventDimensionPopulator.isCheckedDimensions()) {
        //First time aggregation has happened, run populateDimensions to ensure enough dimension data exists
        final boolean populatedDimensions = this.portalEventAggregationManager.populateDimensions();
        if (!populatedDimensions) {
            this.logger.warn("Aborting raw event aggregation, populateDimensions returned false so the state of date/time dimensions is unknown");
            return null;
        }
    }
    //Flush any dimension creation before aggregation
    final EntityManager entityManager = this.getEntityManager();
    entityManager.flush();
    entityManager.setFlushMode(FlushModeType.COMMIT);
    final IEventAggregatorStatus eventAggregatorStatus = eventAggregationManagementDao.getEventAggregatorStatus(IEventAggregatorStatus.ProcessingType.AGGREGATION, true);
    //Update status with current server name
    final String serverName = this.portalInfoProvider.getUniqueServerName();
    final String previousServerName = eventAggregatorStatus.getServerName();
    if (previousServerName != null && !serverName.equals(previousServerName)) {
        this.logger.debug("Last aggregation run on {} clearing all aggregation caches", previousServerName);
        final Session session = getEntityManager().unwrap(Session.class);
        final Cache cache = session.getSessionFactory().getCache();
        cache.evictEntityRegions();
    }
    eventAggregatorStatus.setServerName(serverName);
    //Calculate date range for aggregation
    DateTime lastAggregated = eventAggregatorStatus.getLastEventDate();
    if (lastAggregated == null) {
        lastAggregated = portalEventDao.getOldestPortalEventTimestamp();
        //No portal events to aggregate, skip aggregation
        if (lastAggregated == null) {
            return new EventProcessingResult(0, null, null, true);
        }
        //First time aggregation has run, initialize the CLEAN_UNCLOSED status to save catch-up time
        final IEventAggregatorStatus cleanUnclosedStatus = eventAggregationManagementDao.getEventAggregatorStatus(IEventAggregatorStatus.ProcessingType.CLEAN_UNCLOSED, true);
        AggregationIntervalInfo oldestMinuteInterval = this.intervalHelper.getIntervalInfo(AggregationInterval.MINUTE, lastAggregated);
        cleanUnclosedStatus.setLastEventDate(oldestMinuteInterval.getStart().minusMinutes(1));
        eventAggregationManagementDao.updateEventAggregatorStatus(cleanUnclosedStatus);
    }
    final DateTime newestEventTime = DateTime.now().minus(this.aggregationDelay).secondOfMinute().roundFloorCopy();
    final Thread currentThread = Thread.currentThread();
    final String currentName = currentThread.getName();
    final MutableInt events = new MutableInt();
    final MutableObject lastEventDate = new MutableObject(newestEventTime);
    boolean complete;
    try {
        currentThread.setName(currentName + "-" + lastAggregated + "_" + newestEventTime);
        logger.debug("Starting aggregation of events between {} (inc) and {} (exc)", lastAggregated, newestEventTime);
        //Do aggregation, capturing the start and end dates
        eventAggregatorStatus.setLastStart(DateTime.now());
        complete = portalEventDao.aggregatePortalEvents(lastAggregated, newestEventTime, this.eventAggregationBatchSize, new AggregateEventsHandler(events, lastEventDate, eventAggregatorStatus));
        eventAggregatorStatus.setLastEventDate((DateTime) lastEventDate.getValue());
        eventAggregatorStatus.setLastEnd(DateTime.now());
    } finally {
        currentThread.setName(currentName);
    }
    //Store the results of the aggregation
    eventAggregationManagementDao.updateEventAggregatorStatus(eventAggregatorStatus);
    complete = complete && (this.eventAggregationBatchSize <= 0 || events.intValue() < this.eventAggregationBatchSize);
    return new EventProcessingResult(events.intValue(), lastAggregated, eventAggregatorStatus.getLastEventDate(), complete);
}
Also used : DateTime(org.joda.time.DateTime) EntityManager(javax.persistence.EntityManager) MutableInt(org.apache.commons.lang.mutable.MutableInt) EventSession(org.apereo.portal.events.aggr.session.EventSession) Session(org.hibernate.Session) Cache(org.hibernate.Cache) MutableObject(org.apache.commons.lang.mutable.MutableObject)

Aggregations

MutableObject (org.apache.commons.lang.mutable.MutableObject)7 MutableInt (org.apache.commons.lang.mutable.MutableInt)4 Random (java.util.Random)3 CompositeName (javax.naming.CompositeName)3 CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)3 FunctionWithoutResult (org.apereo.portal.concurrency.FunctionWithoutResult)3 DateTime (org.joda.time.DateTime)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 AggregatedGroupMapping (org.apereo.portal.events.aggr.groups.AggregatedGroupMapping)2 IEntityGroup (org.apereo.portal.groups.IEntityGroup)2 BaseAggrEventsJpaDaoTest (org.apereo.portal.test.BaseAggrEventsJpaDaoTest)2 Tuple (org.apereo.portal.utils.Tuple)2 Test (org.junit.Test)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 Type (java.lang.reflect.Type)1 BindException (java.net.BindException)1 UnknownHostException (java.net.UnknownHostException)1