Search in sources :

Example 1 with ContextStoreReporter

use of org.apache.gobblin.metrics.test.ContextStoreReporter 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);
}
Also used : ContextStoreReporter(org.apache.gobblin.metrics.test.ContextStoreReporter) WeakReference(java.lang.ref.WeakReference) NotificationStore(org.apache.gobblin.metrics.callback.NotificationStore) Test(org.testng.annotations.Test)

Example 2 with ContextStoreReporter

use of org.apache.gobblin.metrics.test.ContextStoreReporter in project incubator-gobblin by apache.

the class RootMetricContextTest method testReporterCanBeAddedToStartedContext.

@Test
public void testReporterCanBeAddedToStartedContext() throws Exception {
    RootMetricContext.get().startReporting();
    ContextStoreReporter reporter = new ContextStoreReporter("testReporter", ConfigFactory.empty());
    Assert.assertTrue(reporter.isStarted());
    RootMetricContext.get().stopReporting();
}
Also used : ContextStoreReporter(org.apache.gobblin.metrics.test.ContextStoreReporter) Test(org.testng.annotations.Test)

Example 3 with ContextStoreReporter

use of org.apache.gobblin.metrics.test.ContextStoreReporter in project incubator-gobblin by apache.

the class ScheduledReporterTest method testScheduledReporter.

@Test
public void testScheduledReporter() throws Exception {
    long reportingIntervalMillis = 1000;
    String context1Name = ScheduledReporterTest.class.getSimpleName() + "_1";
    String context2Name = ScheduledReporterTest.class.getSimpleName() + "_2";
    String context3Name = "SomeOtherName";
    // Create a context name (to check that initialized reporter gets existing contexts correctly)
    MetricContext context1 = MetricContext.builder(context1Name).build();
    // Set up config for reporter
    Properties props = new Properties();
    ScheduledReporter.setReportingInterval(props, reportingIntervalMillis, TimeUnit.MILLISECONDS);
    Config config = ConfigUtils.propertiesToConfig(props);
    config = PrefixContextFilter.setPrefixString(config, ScheduledReporterTest.class.getSimpleName());
    config = ContextFilterFactory.setContextFilterClass(config, PrefixContextFilter.class);
    // Create reporter
    ContextStoreReporter reporter = new ContextStoreReporter("testContext", config);
    // Check that reporter correctly found created context
    Set<String> contextNames = getContextNames(reporter);
    Assert.assertEquals(contextNames.size(), 1);
    Assert.assertTrue(contextNames.contains(context1Name));
    // Create two more contexts
    MetricContext context2 = context1.childBuilder(context2Name).build();
    context1.childBuilder(context3Name).build();
    // Check that reporter correctly found new reporter, but skipped the one that does not satisfy filter
    contextNames = getContextNames(reporter);
    Assert.assertEquals(contextNames.size(), 2);
    Assert.assertTrue(contextNames.contains(context1Name));
    Assert.assertTrue(contextNames.contains(context2Name));
    // Check that nothing has been reported
    Assert.assertEquals(reporter.getReportedContexts().size(), 0);
    // Start reporter
    reporter.start();
    // Wait for up to 10 reporting intervals for 3 reports to run
    long maxWaitMillis = 10 * reportingIntervalMillis;
    long totalWait = 0;
    while (reporter.getReportedContexts().size() < 6 && maxWaitMillis > 0) {
        long wait = 100;
        Thread.sleep(wait);
        maxWaitMillis -= wait;
        totalWait += wait;
    }
    // stop reporter
    reporter.stop();
    // Check wait makes sense given reporting interval (e.g. if wait = 100 millis, then 2 reports in 100 millis,
    // something is wrong with schedule).
    Assert.assertTrue(totalWait > reportingIntervalMillis);
    Assert.assertTrue(reporter.getReportedContexts().size() >= 6);
    // Check that it didn't report excessively
    Assert.assertTrue(reporter.getReportedContexts().size() <= 10);
    // Check that first report indeed reported the correct contexts
    Set<String> firstReport = Sets.newHashSet(reporter.getReportedContexts().get(0).getName(), reporter.getReportedContexts().get(1).getName());
    Assert.assertEquals(firstReport, Sets.newHashSet(context1Name, context2Name));
    // Check that second report indeed reported the correct contexts
    Set<String> secondReport = Sets.newHashSet(reporter.getReportedContexts().get(2).getName(), reporter.getReportedContexts().get(3).getName());
    Assert.assertEquals(secondReport, Sets.newHashSet(context1Name, context2Name));
    int totalReports = reporter.getReportedContexts().size();
    // Wait for reporting interval to make sure reporting has actually stopped
    Thread.sleep(2 * reportingIntervalMillis);
    Assert.assertEquals(reporter.getReportedContexts().size(), totalReports);
    reporter.getReportedContexts().clear();
    // Dereference context 2 to ensure that it gets reported
    context2 = null;
    // Wait for context to be GCed
    maxWaitMillis = 2000;
    System.gc();
    while (reporter.getReportedContexts().size() < 1 && maxWaitMillis > 0) {
        System.gc();
        long wait = 100;
        Thread.sleep(wait);
        maxWaitMillis -= wait;
    }
    // Check that GCed context was reported
    Assert.assertEquals(reporter.getReportedContexts().size(), 1);
    Assert.assertEquals(reporter.getReportedContexts().get(0).getName(), context2Name);
    // Test close method
    reporter.close();
}
Also used : MetricContext(org.apache.gobblin.metrics.MetricContext) ContextStoreReporter(org.apache.gobblin.metrics.test.ContextStoreReporter) Config(com.typesafe.config.Config) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Aggregations

ContextStoreReporter (org.apache.gobblin.metrics.test.ContextStoreReporter)3 Test (org.testng.annotations.Test)3 Config (com.typesafe.config.Config)1 WeakReference (java.lang.ref.WeakReference)1 Properties (java.util.Properties)1 MetricContext (org.apache.gobblin.metrics.MetricContext)1 NotificationStore (org.apache.gobblin.metrics.callback.NotificationStore)1