Search in sources :

Example 6 with SinkContext

use of com.twitter.heron.spi.metricsmgr.sink.SinkContext 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();
}
Also used : HashMap(java.util.HashMap) SinkContext(com.twitter.heron.spi.metricsmgr.sink.SinkContext) SinkContextImpl(com.twitter.heron.metricsmgr.sink.SinkContextImpl) MultiCountMetric(com.twitter.heron.api.metric.MultiCountMetric) TopologyMaster(com.twitter.heron.proto.tmaster.TopologyMaster) Test(org.junit.Test)

Aggregations

SinkContext (com.twitter.heron.spi.metricsmgr.sink.SinkContext)6 MultiCountMetric (com.twitter.heron.api.metric.MultiCountMetric)4 SinkContextImpl (com.twitter.heron.metricsmgr.sink.SinkContextImpl)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)3 SlaveLooper (com.twitter.heron.common.basics.SlaveLooper)2 TopologyMaster (com.twitter.heron.proto.tmaster.TopologyMaster)2 Before (org.junit.Before)2 Communicator (com.twitter.heron.common.basics.Communicator)1 MetricsCollector (com.twitter.heron.common.utils.metrics.MetricsCollector)1 SinkExecutor (com.twitter.heron.metricsmgr.executor.SinkExecutor)1 MetricsRecord (com.twitter.heron.spi.metricsmgr.metrics.MetricsRecord)1 IMetricsSink (com.twitter.heron.spi.metricsmgr.sink.IMetricsSink)1