public static final EnricherSpec<?> newEnricherForServiceUpFromChildrenWithQuorumCheck(QuorumCheck quorumCheck) { EnricherSpec<?> serviceUp = newEnricherForServiceUpFromChildren() .configure(ComputeServiceIndicatorsFromChildrenAndMembers.RUNNING_QUORUM_CHECK, quorumCheck); return serviceUp; } public static final EnricherSpec<?> newEnricherForServiceUp(Boolean fromChildren, Boolean fromMembers) {
@Override public <V> K configure(ConfigKey<V> key, V val) { super.configure(key, val); return self(); }
@Override public <V> K configure(ConfigKey<V> key, Task<? extends V> val) { super.configure(key, val); return self(); }
@Override public <V> K configure(HasConfigKey<V> key, V val) { super.configure(key, val); return self(); }
protected void activateAdditionalEnrichers(EntityLocal entity) { Duration rollupWindowSize = getConfig(ROLLUP_WINDOW_SIZE); if (rollupWindowSize!=null) { entity.enrichers().add(EnricherSpec.create(RollingTimeWindowMeanEnricher.class) .configure("producer", entity) .configure("source", REQUEST_LATENCY_IN_SECONDS_MOST_RECENT) .configure("target", REQUEST_LATENCY_IN_SECONDS_IN_WINDOW) .configure("timePeriod", rollupWindowSize)); } }
@Override public K configure(Map<?, ?> val) { super.configure(val); return self(); }
@Override protected void initEnrichers() { // Deliberately not calling `super.initEnrichers()`. For our state (i.e. "service.state" // and "service.isUp"), we rely on the `serviceStateComputer`. This keeps things a lot // simpler. However, it means that if someone manually sets a "service.notUp.indicators" // or "service.problems" then that won't cause the entity to transition to false or ON_FIRE. enrichers().add(EnricherSpec.create(ServiceStateComputer.class) .configure(ServiceStateComputer.FROM_CHILDREN, true) .configure(ServiceStateComputer.UP_QUORUM_CHECK, config().get(UP_QUORUM_CHECK)) .configure(ServiceStateComputer.RUNNING_QUORUM_CHECK, config().get(RUNNING_QUORUM_CHECK))); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testTotalCycleNoProducer() { app.enrichers().add(EnricherSpec.create(PercentageEnricher.class) .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor) .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor) .configure(PercentageEnricher.TARGET_SENSOR, totalSensor)); }
@Override public void onManagementStarted() { super.onManagementStarted(); LOG.info("mgmt started for "+this); enrichers().add(EnricherSpec.create(RollingTimeWindowMeanEnricher.class) .configure("producer", this) .configure("source", PROCESS_CPU_TIME) .configure("target", AVG1) .configure("timePeriod", Duration.TEN_SECONDS)); }
@Test public void testPropagatorAvoidsInfiniteLoopInSameSensorMapping() throws Exception { AttributeSensor<String> mySensor = Sensors.newSensor(String.class, "mySensor"); EnricherSpec<?> spec = EnricherSpec.create(Propagator.class) .configure(Propagator.PRODUCER, app) .configure(Propagator.SENSOR_MAPPING, ImmutableMap.of(mySensor, mySensor)); assertAddEnricherThrowsIllegalStateException(spec, "when publishing to own entity"); assertAttributeNotRepublished(app, mySensor); }
@Test public void testAddsEnricherSpec() throws Exception { entity = app.createAndManageChild(EntitySpec.create(TestEntity.class, TestEntityNoEnrichersImpl.class) .enricher(EnricherSpec.create(MyEnricher.class) .displayName("myenrichername") .configure(MyEnricher.CONF1, "myconf1val") .configure("myfield", "myfieldval"))); Enricher enricher = Iterables.getOnlyElement(entity.enrichers()); assertTrue(enricher instanceof MyEnricher, "enricher="+enricher); assertEquals(enricher.getDisplayName(), "myenrichername"); assertEquals(enricher.getConfig(MyEnricher.CONF1), "myconf1val"); }
@Test public void testRebindWithFailingEnricherContinuesWithoutEnricher() throws Exception { origApp.enrichers().add(EnricherSpec.create(MyEnricherFailingImpl.class) .configure(MyEnricherFailingImpl.FAIL_ON_REBIND, true)); newApp = rebind(); Optional<Enricher> newEnricher = Iterables.tryFind(newApp.enrichers(), Predicates.instanceOf(MyEnricherFailingImpl.class)); assertFalse(newEnricher.isPresent(), "enricher="+newEnricher); }
@Test public void testDeltaEnricher() throws Exception { origApp.enrichers().add(EnricherSpec.create(DeltaEnricher.class) .configure("producer", origApp) .configure("source", INT_METRIC) .configure("target", INT_METRIC2)); TestApplication newApp = rebind(); newApp.sensors().set(INT_METRIC, 1); newApp.sensors().set(INT_METRIC, 10); EntityAsserts.assertAttributeEqualsEventually(newApp, INT_METRIC2, 9); }
@Test public void testConfigCannotBeSetAfterApplicationIsStarted() throws Exception { MyEnricher enricher = app.enrichers().add(EnricherSpec.create(MyEnricher.class) .configure(MyEnricher.STR_KEY, "origval")); try { enricher.config().set(MyEnricher.STR_KEY,"newval"); fail(); } catch (UnsupportedOperationException e) { // success } assertEquals(enricher.getConfig(MyEnricher.STR_KEY), "origval"); }
@Test public void testIgnoresWhenNoSubnetAddress() throws Exception { entity.sensors().set(Attributes.HTTP_PORT, 1234); entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTP_PORT))); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT), entity, Sensors.newStringSensor(Attributes.HTTP_PORT.getName()+".mapped.subnet"), null); }
@Test public <T> void testCoercesSensorName() throws Exception { AttributeSensor<String> sensor = Sensors.newStringSensor("mysensor"); entity.sensors().set(sensor, "127.0.0.1:1234"); portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost", 5678), machine, 1234); entity.addLocations(ImmutableList.of(machine)); entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) .configure(OnPublicNetworkEnricher.SENSORS.getName(), ImmutableList.of("mysensor"))); assertAttributeEqualsEventually("mysensor.mapped.public", "mypublichost:5678"); }
@Test public void testReportsFailureWhenAlreadyDownOnRegisteringPolicy() throws Exception { ServiceStateLogic.setExpectedState(e1, Lifecycle.RUNNING); e1.sensors().set(TestEntity.SERVICE_UP, false); e1.enrichers().add(EnricherSpec.create(ServiceFailureDetector.class) .configure(ServiceFailureDetector.ENTITY_FAILED_ONLY_IF_PREVIOUSLY_UP, false)); assertHasEventEventually(HASensors.ENTITY_FAILED, Predicates.<Object>equalTo(e1), null); }
@Test(dataProvider = "invalidVariants") public <T> void testIgnoresWhenInvalidAttribute(AttributeSensor<T> sensor, T sensorVal) throws Exception { entity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1"); entity.sensors().set(sensor, sensorVal); entity.addLocations(ImmutableList.of(machine)); entity.enrichers().add(EnricherSpec.create(OnSubnetNetworkEnricher.class) .configure(OnSubnetNetworkEnricher.SENSORS, ImmutableList.of(sensor))); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT), entity, Sensors.newStringSensor(sensor.getName()+".mapped.subnet"), null); }
@Test public void testIgnoresWhenNoMapping() throws Exception { // Creates associate for a non-matching port entity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1"); entity.sensors().set(Attributes.HTTP_PORT, 1234); portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost", 5678), machine, 4321); entity.addLocations(ImmutableList.of(machine)); entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTP_PORT))); EntityAsserts.assertAttributeEquals(entity, Sensors.newStringSensor(Attributes.HTTP_PORT.getName()+".mapped.public"), null); }
@Test public void testKeepsLocationsReferencedByEnricherInFlag() throws Exception { Location loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); origApp.enrichers().add(EnricherSpec.create(MyEnricher.class) .configure("myobj", loc)); assertTransformIsNoop(); }