use of org.apache.gobblin.metrics.callback.NotificationStore in project incubator-gobblin by apache.
the class RootMetricContextTest method testMetricContextLifecycle.
@Test
public void testMetricContextLifecycle() throws Exception {
String name = UUID.randomUUID().toString();
NotificationStore store = new NotificationStore(new ContextNamePredicate(name));
RootMetricContext.get().addNotificationTarget(store);
// Create a new metric context
MetricContext metricContext = MetricContext.builder(name).build();
WeakReference<MetricContext> contextWeakReference = new WeakReference<MetricContext>(metricContext);
InnerMetricContext innerMetricContext = metricContext.getInnerMetricContext();
WeakReference<InnerMetricContext> innerMetricContextWeakReference = new WeakReference<InnerMetricContext>(innerMetricContext);
innerMetricContext = null;
// Check that existence of a reporter does not prevent GC
ContextStoreReporter reporter = new ContextStoreReporter("testReporter", ConfigFactory.empty());
// Check that metric context is a child of root metric context
Assert.assertTrue(RootMetricContext.get().getChildContextsAsMap().containsKey(name));
Assert.assertEquals(RootMetricContext.get().getChildContextsAsMap().get(name), metricContext);
// Check that notification on new metric context was generated
Assert.assertEquals(store.getNotificationList().size(), 1);
Assert.assertEquals(store.getNotificationList().get(0).getClass(), NewMetricContextNotification.class);
Assert.assertEquals(((NewMetricContextNotification) store.getNotificationList().get(0)).getMetricContext(), metricContext);
store.getNotificationList().clear();
// Create a counter
ContextAwareCounter counter1 = metricContext.contextAwareCounter("textCounter1");
// If losing reference of counter, should not be GCed while context is present
WeakReference<ContextAwareCounter> counterWeakReference1 = new WeakReference<ContextAwareCounter>(counter1);
counter1 = null;
ensureNotGarbageCollected(counterWeakReference1);
// Create some more metrics
ContextAwareCounter counter2 = metricContext.contextAwareCounter("testCounter");
WeakReference<ContextAwareCounter> counterWeakReference2 = new WeakReference<ContextAwareCounter>(counter2);
ContextAwareMeter meter = metricContext.contextAwareMeter("testMeter");
WeakReference<ContextAwareMeter> meterWeakReference = new WeakReference<ContextAwareMeter>(meter);
meter.mark();
ContextAwareHistogram histogram = metricContext.contextAwareHistogram("testHistogram");
WeakReference<ContextAwareHistogram> histogramWeakReference = new WeakReference<ContextAwareHistogram>(histogram);
ContextAwareTimer timer = metricContext.contextAwareTimer("testTimer");
WeakReference<ContextAwareTimer> timerWeakReference = new WeakReference<ContextAwareTimer>(timer);
// If losing reference to context, should not be GCed while reference to metric is present
metricContext = null;
ensureNotGarbageCollected(contextWeakReference);
ensureNotGarbageCollected(counterWeakReference2);
ensureNotGarbageCollected(meterWeakReference);
ensureNotGarbageCollected(timerWeakReference);
ensureNotGarbageCollected(histogramWeakReference);
// After losing reference to context and all metrics, context and all metrics should be GCed
store.getNotificationList().clear();
reporter.getReportedContexts().clear();
counter2 = null;
meter = null;
histogram = null;
timer = null;
ensureGarbageCollected(contextWeakReference);
ensureGarbageCollected(counterWeakReference1);
ensureGarbageCollected(counterWeakReference2);
ensureGarbageCollected(meterWeakReference);
ensureGarbageCollected(timerWeakReference);
ensureGarbageCollected(histogramWeakReference);
// Inner metric context should not be GCed
ensureNotGarbageCollected(innerMetricContextWeakReference);
// Notification on removal of metric context should be available
int maxWait = 10;
while (store.getNotificationList().isEmpty() && maxWait > 0) {
Thread.sleep(1000);
maxWait--;
}
Assert.assertEquals(store.getNotificationList().size(), 1);
Assert.assertEquals(store.getNotificationList().get(0).getClass(), MetricContextCleanupNotification.class);
Assert.assertEquals(((MetricContextCleanupNotification) store.getNotificationList().get(0)).getMetricContext(), innerMetricContextWeakReference.get());
// Reporter should have attempted to report metric context
Assert.assertEquals(reporter.getReportedContexts().size(), 1);
Assert.assertEquals(reporter.getReportedContexts().get(0), innerMetricContextWeakReference.get());
// Metrics in deleted metric context should still be readable
Assert.assertEquals(innerMetricContextWeakReference.get().getCounters().size(), 2);
Assert.assertEquals(innerMetricContextWeakReference.get().getMeters().size(), 1);
Assert.assertEquals(innerMetricContextWeakReference.get().getTimers().size(), 2);
Assert.assertEquals(innerMetricContextWeakReference.get().getHistograms().size(), 1);
Assert.assertEquals(innerMetricContextWeakReference.get().getMeters().get("testMeter").getCount(), 1);
// After clearing notification, inner metric context should be GCed
store.getNotificationList().clear();
reporter.getReportedContexts().clear();
ensureGarbageCollected(innerMetricContextWeakReference);
RootMetricContext.get().removeReporter(reporter);
}
Aggregations