@Test public void testAggregatingCastsResult() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .publishing(LONG1) .fromMembers() .computing(Functions.constant(Long.valueOf(1))) .build()); entity.sensors().set(NUM1, 123); EntityAsserts.assertAttributeEqualsEventually(group, LONG1, Long.valueOf(1)); }
@Test public void testMonospaceTimeWeightedDeltaEnricher() { AttributeSensor<Double> deltaSensor = new BasicAttributeSensor<Double>(Double.class, "per second delta delta sensor"); @SuppressWarnings("unchecked") TimeWeightedDeltaEnricher<Integer> delta = producer.enrichers().add(EnricherSpec.create(TimeWeightedDeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor) .configure("unitMillis", 1000)); // Don't start with timestamp=0: that may be treated special delta.onEvent(intSensor.newEvent(producer, 0), 1000); assertEquals(producer.getAttribute(deltaSensor), null); delta.onEvent(intSensor.newEvent(producer, 0), 2000); assertEquals(producer.getAttribute(deltaSensor), 0d); delta.onEvent(intSensor.newEvent(producer, 1), 3000); assertEquals(producer.getAttribute(deltaSensor), 1d); delta.onEvent(intSensor.newEvent(producer, 3), 4000); assertEquals(producer.getAttribute(deltaSensor), 2d); delta.onEvent(intSensor.newEvent(producer, 8), 5000); assertEquals(producer.getAttribute(deltaSensor), 5d); }
@Test public void testDoesNotCreateDriverAfterRebind() throws Exception { MyService origE = origApp.createAndManageChild(EntitySpec.create(MyService.class) .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)); //the entity skips enricher initialization, do it explicitly origE.enrichers().add(ServiceStateLogic.newEnricherForServiceStateFromProblemsAndUp()); MyProvisioningLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class) .displayName("mylocname")); origApp.start(ImmutableList.of(origLoc)); assertEquals(origE.getAttribute(Attributes.SERVICE_STATE_EXPECTED).getState(), Lifecycle.RUNNING); EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); ServiceStateLogic.setExpectedState(origE, Lifecycle.ON_FIRE); EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); newApp = rebind(); MyService newE = (MyService) Iterables.getOnlyElement(newApp.getChildren()); assertNull(newE.getDriver(), "driver should not be initialized because entity is in a permanent failure"); }
@Test public void testAggregatesExistingMembersOfGroup() { BasicGroup group = app.addChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = group.addChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = group.addChild(EntitySpec.create(TestEntity.class)); group.addMember(p1); group.addMember(p2); p1.sensors().set(intSensor, 1); group.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromMembers() .build()); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(group, target, 3); group.removeMember(p2); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); }
@BeforeMethod(alwaysRun=true) @SuppressWarnings("unchecked") @Override public void setUp() throws Exception { super.setUp(); producer = app.addChild(EntitySpec.create(TestEntity.class)); intSensor = new BasicAttributeSensor<Integer>(Integer.class, "int sensor"); deltaSensor = new BasicAttributeSensor<Integer>(Integer.class, "delta sensor"); avgSensor = new BasicAttributeSensor<Double>(Double.class, "avg sensor"); producer.enrichers().add(EnricherSpec.create(DeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor)); averager = producer.enrichers().add(EnricherSpec.create(RollingTimeWindowMeanEnricher.class) .configure("producer", producer) .configure("source", deltaSensor) .configure("target", avgSensor) .configure("timePeriod", timePeriod)); }
@Test public void testAggregatingMap() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(STR1, STR2) .publishing(MAP1) .fromMembers() .excludingBlank() .build()); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of()); entity.sensors().set(STR1, "a"); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of()); entity.sensors().set(STR2, "b"); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of("a", "b")); entity.sensors().set(STR2, "c"); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of("a", "c")); }
@Test public void testDeltaEnricher() { AttributeSensor<Integer> deltaSensor = new BasicAttributeSensor<Integer>(Integer.class, "delta sensor"); @SuppressWarnings("unchecked") DeltaEnricher<Integer> delta = producer.enrichers().add(EnricherSpec.create(DeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor)); delta.onEvent(intSensor.newEvent(producer, 0)); delta.onEvent(intSensor.newEvent(producer, 0)); assertEquals(producer.getAttribute(deltaSensor), (Integer)0); delta.onEvent(intSensor.newEvent(producer, 1)); assertEquals(producer.getAttribute(deltaSensor), (Integer)1); delta.onEvent(intSensor.newEvent(producer, 3)); assertEquals(producer.getAttribute(deltaSensor), (Integer)2); delta.onEvent(intSensor.newEvent(producer, 8)); assertEquals(producer.getAttribute(deltaSensor), (Integer)5); }
@SuppressWarnings("unchecked") @BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); producer = app.addChild(EntitySpec.create(BasicEntity.class)); intSensor = new BasicAttributeSensor<Integer>(Integer.class, "int sensor"); deltaSensor = new BasicAttributeSensor<Double>(Double.class, "delta sensor"); avgSensor = new BasicAttributeSensor<Double>(Double.class, "avg sensor"); delta = producer.enrichers().add(EnricherSpec.create(YamlTimeWeightedDeltaEnricher.class) .configure(YamlTimeWeightedDeltaEnricher.PRODUCER, producer) .configure(YamlTimeWeightedDeltaEnricher.SOURCE_SENSOR, intSensor) .configure(YamlTimeWeightedDeltaEnricher.TARGET_SENSOR, deltaSensor)); averager = producer.enrichers().add(EnricherSpec.create(YamlRollingTimeWindowMeanEnricher.class) .configure(YamlRollingTimeWindowMeanEnricher.PRODUCER, producer) .configure(YamlRollingTimeWindowMeanEnricher.SOURCE_SENSOR, deltaSensor) .configure(YamlRollingTimeWindowMeanEnricher.TARGET_SENSOR, avgSensor) .configure(YamlRollingTimeWindowMeanEnricher.WINDOW_DURATION, timePeriod)); }
@Test public void testCreatesDriverAfterRebind() throws Exception { MyService origE = origApp.createAndManageChild(EntitySpec.create(MyService.class) .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)); //the entity skips enricher initialization, do it explicitly origE.enrichers().add(ServiceStateLogic.newEnricherForServiceStateFromProblemsAndUp()); MyProvisioningLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class) .displayName("mylocname")); origApp.start(ImmutableList.of(origLoc)); assertEquals(origE.getAttribute(Attributes.SERVICE_STATE_EXPECTED).getState(), Lifecycle.RUNNING); EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); ServiceProblemsLogic.updateProblemsIndicator(origE, "test", "fire"); EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); newApp = rebind(); MyService newE = (MyService) Iterables.getOnlyElement(newApp.getChildren()); assertTrue(newE.getDriver() != null, "driver should be initialized"); }
@Test public void testAggregatingGroupSum() { TestEntity child1 = group.addChild(EntitySpec.create(TestEntity.class)); group.addMember(entity); group.addMember(entity2); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .publishing(NUM2) .fromMembers() .computingSum() .build()); child1.sensors().set(NUM1, 1); entity.sensors().set(NUM1, 2); entity2.sensors().set(NUM1, 3); EntityAsserts.assertAttributeEqualsEventually(group, NUM2, 5); }
@Test public void testPostProcessorCalledForDeltaEnricher() { AttributeSensor<Double> deltaSensor = new BasicAttributeSensor<Double>(Double.class, "per second delta delta sensor"); @SuppressWarnings("unchecked") TimeWeightedDeltaEnricher<Integer> delta = producer.enrichers().add(EnricherSpec.create(TimeWeightedDeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", deltaSensor) .configure("unitMillis", 1000) .configure("postProcessor", new AddConstant(123d))); delta.onEvent(intSensor.newEvent(producer, 0), 1000); delta.onEvent(intSensor.newEvent(producer, 0), 2000); assertEquals(producer.getAttribute(deltaSensor), 123+0d); delta.onEvent(intSensor.newEvent(producer, 1), 3000); assertEquals(producer.getAttribute(deltaSensor), 123+1d); }
public void addTo(Entity entity) { entity.enrichers().add(this); }
public static void addMachineMetricsEnrichers(Entity entity) { entity.enrichers().add(EnricherSpec.create(YamlTimeWeightedDeltaEnricher.class) .configure(YamlTimeWeightedDeltaEnricher.SOURCE_SENSOR, MachineAttributes.USED_MEMORY) .configure(YamlTimeWeightedDeltaEnricher.TARGET_SENSOR, MachineAttributes.USED_MEMORY_DELTA_PER_SECOND_LAST)); entity.enrichers().add(EnricherSpec.create(YamlRollingTimeWindowMeanEnricher.class) .configure(YamlRollingTimeWindowMeanEnricher.SOURCE_SENSOR, MachineAttributes.USED_MEMORY_DELTA_PER_SECOND_LAST) .configure(YamlRollingTimeWindowMeanEnricher.TARGET_SENSOR, MachineAttributes.USED_MEMORY_DELTA_PER_SECOND_IN_WINDOW)); entity.enrichers().add(EnricherSpec.create(PercentageEnricher.class) .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, MachineAttributes.USED_MEMORY) .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, MachineAttributes.TOTAL_MEMORY) .configure(PercentageEnricher.TARGET_SENSOR, MachineAttributes.USED_MEMORY_PERCENT) .configure(PercentageEnricher.SUPPRESS_DUPLICATES, true)); }
@Test public void testConverts100NanosTimeBlocks() { @SuppressWarnings("unchecked") TimeFractionDeltaEnricher<Integer> enricher = producer.enrichers().add(EnricherSpec.create(TimeFractionDeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", fractionSensor) .configure("durationPerOrigUnit", Duration.nanos(100))); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 0, 1000000L)); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 10000000, 1001000L)); assertEquals(producer.getAttribute(fractionSensor), 1d); } }
@Test public void testAggregatingChildrenSum() { group.addMember(entity); TestEntity child1 = group.addChild(EntitySpec.create(TestEntity.class)); TestEntity child2 = group.addChild(EntitySpec.create(TestEntity.class)); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .publishing(NUM2) .fromChildren() .computingSum() .build()); entity.sensors().set(NUM1, 1); child1.sensors().set(NUM1, 2); child2.sensors().set(NUM1, 3); EntityAsserts.assertAttributeEqualsEventually(group, NUM2, 5); }
@Test public void testConvertsTimeUnits() { @SuppressWarnings("unchecked") TimeFractionDeltaEnricher<Integer> enricher = producer.enrichers().add(EnricherSpec.create(TimeFractionDeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", fractionSensor) .configure("durationPerOrigUnit", Duration.micros(1))); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 0, 1000000L)); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 1000000, 1001000L)); assertEquals(producer.getAttribute(fractionSensor), 1d); }
public static void connectJavaAppServerPolicies(Entity entity, Duration windowPeriod) { entity.enrichers().add(EnricherSpec.create(TimeFractionDeltaEnricher.class) .configure("producer", entity) .configure("source", UsesJavaMXBeans.PROCESS_CPU_TIME) .configure("target", UsesJavaMXBeans.PROCESS_CPU_TIME_FRACTION_LAST) .configure("durationPerOrigUnit", Duration.millis(1))); entity.enrichers().add(EnricherSpec.create(RollingTimeWindowMeanEnricher.class) .configure("producer", entity) .configure("source", UsesJavaMXBeans.PROCESS_CPU_TIME_FRACTION_LAST) .configure("target", UsesJavaMXBeans.PROCESS_CPU_TIME_FRACTION_IN_WINDOW) .configure("timePeriod", windowPeriod)); }
@Test public void testDeletesOrphanedEnricher() throws Exception { Entity entity = origApp.addChild(EntitySpec.create(TestEntity.class).impl(MyEntity.class)); MyEnricher enricher = entity.enrichers().add(EnricherSpec.create(MyEnricher.class)); MementoTweaker tweaker = new MementoTweaker(new Deletions().entities(entity.getId())); assertTransformDeletes(new Deletions().enrichers(enricher.getId()), tweaker); }
@Test @SuppressWarnings("unchecked") public void testZeroWindowSize() { averager = producer.enrichers().add(EnricherSpec.create(RollingMeanEnricher.class) .configure("producer", producer) .configure("source", deltaSensor) .configure("target", avgSensor) .configure("windowSize", 0)); averager.onEvent(intSensor.newEvent(producer, 10)); assertEquals(averager.getAverage(), null); }
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)); } }