EventsCacheLoader extends CacheLoader<EventsCacheKey, EventsCacheValue> { @Override public ListenableFuture<EventsCacheValue> reload(final EventsCacheKey key, final EventsCacheValue oldValue) throws Exception { logger.fine(EventsUtils.getThreadId() + "Reloading events cache entry for key " + key); oldValue.getContainers().addAll(containerProvider.getContainersForDeployment(key.getDeploymentId())); if (!oldValue.getContainers().isEmpty()) { int index = oldValue.getLastEventIndex(); for (GridServiceContainer container : oldValue.getContainers()) { LogEntryMatcherProviderKey logEntryMatcherProviderKey = createKey(container, key); LogEntryMatcher matcher = matcherProvider.get(logEntryMatcherProviderKey); if (container.isDiscovered()) { logger.fine(EventsUtils.getThreadId() + "Retrieving logs from container " + container.getUid() + container.getExactZones().getZones()); LogEntries logEntries = container.logEntries(matcher); for (LogEntry logEntry : logEntries) { if (logEntry.isLog()) { logger.finest(EventsUtils.getThreadId() + "Found log " + logEntry.getText() + " for " + "deployment id " + key.getDeploymentId() + " from container " + container.getUid() + container.getExactZones().getZones()); DeploymentEvent event = EventsUtils.logToEvent( logEntry, logEntries.getHostName(), logEntries.getHostAddress()); event.setIndex(++index); oldValue.getEvents().getEvents().add(event); } } } else { logger.fine(EventsUtils.getThreadId() + "Not retrieving logs from container " + container.getUid() + container.getExactZones().getZones() + " since it is not discovered by the admin"); } } oldValue.setLastRefreshedTimestamp(System.currentTimeMillis()); oldValue.setLastEventIndex(index); } return Futures.immediateFuture(oldValue); } 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 testReload() throws Exception { EventsCacheLoader loader = new EventsCacheLoader(new MockOneGridServiceContainerProvider()); EventsCacheKey eventsCacheKey = new EventsCacheKey("deploymentId"); EventsCacheValue oldValue = loader.load(eventsCacheKey); ListenableFuture<EventsCacheValue> reloadedValue = loader.reload(eventsCacheKey, oldValue); EventsCacheValue events = reloadedValue.get(); Assert.assertSame(events, oldValue); Assert.assertEquals(2 * LOG_ENTRIES_BATCH_SIZE, oldValue.getLastEventIndex()); for (int i = 1; i < LOG_ENTRIES_BATCH_SIZE * 2; i++) { DeploymentEvent event = EventsUtils.retrieveEventWithIndex(i, events.getEvents().getEvents()); Assert.assertNotNull(event); } }