EventsCacheLoader extends CacheLoader<EventsCacheKey, EventsCacheValue> { @Override public EventsCacheValue load(final EventsCacheKey key) throws Exception { logger.fine(EventsUtils.getThreadId() + "Could not find events for key " + key + " in cache. Loading from container logs..."); DeploymentEvents events = new DeploymentEvents(); Set<GridServiceContainer> containersForDeployment = containerProvider .getContainersForDeployment(key.getDeploymentId()); Set<ProcessingUnit> processingUnitsForDeployment = new HashSet<ProcessingUnit>(); int index = 0; for (GridServiceContainer container : containersForDeployment) { ProcessingUnitInstance[] processingUnitInstances = container.getProcessingUnitInstances(); if (processingUnitInstances != null && processingUnitInstances.length > 0) { processingUnitsForDeployment.add(processingUnitInstances[0].getProcessingUnit()); } LogEntryMatcherProviderKey logEntryMatcherProviderKey = createKey(container, key); if (container.isDiscovered()) { LogEntries logEntries = container.logEntries(matcherProvider.get(logEntryMatcherProviderKey)); for (LogEntry logEntry : logEntries) { if (logEntry.isLog()) { DeploymentEvent event = EventsUtils.logToEvent(logEntry, logEntries.getHostName(), logEntries.getHostAddress()); event.setIndex(++index); events.getEvents().add(event); } } } } EventsCacheValue value = new EventsCacheValue(); value.setLastEventIndex(index); value.setEvents(events); value.getProcessingUnits().addAll(processingUnitsForDeployment); value.setContainers(containersForDeployment); value.setLastRefreshedTimestamp(System.currentTimeMillis()); return value; } EventsCacheLoader(final GridServiceContainerProvider containerProvider); @Override EventsCacheValue load(final EventsCacheKey key); @Override ListenableFuture<EventsCacheValue> reload(final EventsCacheKey key, final EventsCacheValue oldValue); LogEntryMatcherProvider getMatcherProvider();  }
@Test public void testLoad() throws Exception { EventsCacheLoader loader = new EventsCacheLoader(new MockOneGridServiceContainerProvider()); EventsCacheKey eventsCacheKey = new EventsCacheKey("deploymentId"); EventsCacheValue loadedValue = loader.load(eventsCacheKey); Assert.assertEquals(LOG_ENTRIES_BATCH_SIZE, loadedValue.getLastEventIndex()); List<DeploymentEvent> events = loadedValue.getEvents().getEvents(); for (int i = 1; i < LOG_ENTRIES_BATCH_SIZE; i++) { DeploymentEvent event = EventsUtils.retrieveEventWithIndex(i, events); Assert.assertNotNull(event); } }