use of com.twitter.heron.api.metric.MultiCountMetric in project heron by twitter.
the class MetricsCacheSinkTest method testHandleMetricsCacheLocation.
/**
* Test whether MetricsCacheSink would handle MetricsCacheLocation in SingletonRegistry automatically
*/
@Test
public void testHandleMetricsCacheLocation() throws Exception {
// create a new MetricsCacheClientService
MetricsCacheSink metricsCacheSink = new MetricsCacheSink();
Map<String, Object> sinkConfig = new HashMap<String, Object>();
// Fill with necessary config
sinkConfig.put("metricscache-location-check-interval-sec", METRICSCACHE_LOCATION_CHECK_INTERVAL_SECONDS);
// These are config for MetricsCacheClient
Map<String, Object> serviceConfig = buildServiceConfig();
sinkConfig.put("metricscache-client", serviceConfig);
// It is null since we have not set it
Assert.assertNull(metricsCacheSink.getCurrentMetricsCacheLocation());
SinkContext sinkContext = new SinkContextImpl("topology-name", "metricsmgr-id", "sink-id", new MultiCountMetric());
// Start the MetricsCacheSink
metricsCacheSink.init(sinkConfig, sinkContext);
// Put the MetricsCacheLocation into SingletonRegistry
TopologyMaster.MetricsCacheLocation oldLoc = TopologyMaster.MetricsCacheLocation.newBuilder().setTopologyName("topology-name").setTopologyId("topology-id").setHost("host").setControllerPort(0).setMasterPort(0).build();
SingletonRegistry.INSTANCE.registerSingleton(METRICSCACHE_LOCATION_BEAN_NAME, oldLoc);
Thread.sleep(WAIT_SECONDS * 1000);
// The MetricsCacheService should start
Assert.assertTrue(metricsCacheSink.getMetricsCacheStartedAttempts() > 0);
Assert.assertEquals(oldLoc, metricsCacheSink.getCurrentMetricsCacheLocation());
Assert.assertEquals(oldLoc, metricsCacheSink.getCurrentMetricsCacheLocationInService());
// Update it, the MetricsCacheSink should pick up the new one.
TopologyMaster.MetricsCacheLocation newLoc = TopologyMaster.MetricsCacheLocation.newBuilder().setTopologyName("topology-name").setTopologyId("topology-id").setHost("host").setControllerPort(0).setMasterPort(1).build();
SingletonRegistry.INSTANCE.updateSingleton(METRICSCACHE_LOCATION_BEAN_NAME, newLoc);
int lastMetricsCacheStartedAttempts = metricsCacheSink.getMetricsCacheStartedAttempts();
Thread.sleep(WAIT_SECONDS * 1000);
// The MetricsCacheService should use the new MetricsCacheLocation
Assert.assertTrue(metricsCacheSink.getMetricsCacheStartedAttempts() > lastMetricsCacheStartedAttempts);
Assert.assertNotSame(oldLoc, metricsCacheSink.getCurrentMetricsCacheLocation());
Assert.assertNotSame(oldLoc, metricsCacheSink.getCurrentMetricsCacheLocationInService());
Assert.assertEquals(newLoc, metricsCacheSink.getCurrentMetricsCacheLocation());
Assert.assertEquals(newLoc, metricsCacheSink.getCurrentMetricsCacheLocationInService());
metricsCacheSink.close();
}
Aggregations