@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 testSummingEnricherWithNoValues() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testSummingEnricherWhenNoSensorValuesYetDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testAggregatingMembersEnricher() throws Exception { origApp.start(ImmutableList.of(origLoc)); origCluster.resize(2); origApp.enrichers().add(Enrichers.builder() .aggregating(METRIC1) .from(origCluster) .fromMembers() .computing(StringFunctions.joiner(",")) .publishing(METRIC2) .build()); TestApplication newApp = rebind(); DynamicCluster newCluster = (DynamicCluster) Iterables.find(newApp.getChildren(), Predicates.instanceOf(DynamicCluster.class)); int i = 1; for (Entity member : newCluster.getMembers()) { ((EntityInternal)member).sensors().set(METRIC1, "myval"+(i++)); } EntityAsserts.assertAttributeEventually(newApp, METRIC2, Predicates.or(Predicates.equalTo("myval1,myval2"), Predicates.equalTo("myval2,myval1"))); }
@Override protected void initEnrichers() { super.initEnrichers(); EnricherSpec<?> zks = Enrichers.builder() .aggregating(ZooKeeperNode.ZOOKEEPER_ENDPOINT) .publishing(ZOOKEEPER_SERVERS) .fromMembers() .build(); EnricherSpec<?> zke = Enrichers.builder() .joining(ZOOKEEPER_SERVERS) .publishing(ZOOKEEPER_ENDPOINTS) .quote(false) .separator(",") .build(); enrichers().add(zks); enrichers().add(zke); }
@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 testAppliesFilterWhenAggregatingChildrenOfGroup() { TestEntity p1 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); p1.sensors().set(intSensor, 1); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromChildren() .entityFilter(Predicates.equalTo((Entity)p1)) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); TestEntity p2 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", SHORT_WAIT_MS), entity, target, 1); }
@Test public void testAveragingEnricherWhenDefaultValueForUnreportedSensors() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1)) .defaultValueForUnreportedSensors(3) .valueToReportIfNoSensors(5) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); producer1.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, 3d); producer1.sensors().set(intSensor, 4); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 4d); }
@Test public void testAggregatesChildrenOfProducer() { TestEntity p1 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); p1.sensors().set(intSensor, 1); app.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .from(entity) .fromChildren() .build()); EntityAsserts.assertAttributeEqualsEventually(app, target, 1); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(app, target, 3); Entities.unmanage(p2); EntityAsserts.assertAttributeEqualsEventually(app, target, 1); }
@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 testAveragingEnricherWhenNoAndNullSensorValues() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); producer1.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); }
@Test public void testAveragingEnricherWhenNoSensors() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromChildren() .defaultValueForUnreportedSensors(3) .valueToReportIfNoSensors(5) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 5d); }
@Test public void testSummingEnricherWhenNoSensorValuesYet() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .defaultValueForUnreportedSensors(11) .valueToReportIfNoSensors(40) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 11); }
@Test public void testSummingEnricherWithNoProducersDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromChildren() .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Override public void init() { super.init(); enrichers().add(Enrichers.builder() .aggregating(Changeable.GROUP_SIZE) .publishing(FABRIC_SIZE) .fromMembers() .computingSum() .valueToReportIfNoSensors(0) .build()); sensors().set(SERVICE_UP, false); }
private <T> void propagateMasterAttribute(AttributeSensor<T> att) { enrichers().add(Enrichers.builder() .aggregating(att) .publishing(att) .computing(IfFunctions.<Collection<T>>ifPredicate(CollectionFunctionals.notEmpty()) .apply(CollectionFunctionals.firstElement()) .defaultValue(null)) .entityFilter(MySqlClusterUtils.IS_MASTER) .build()); }
@Test public void testAveragingEnricherWhenNoProducersDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromChildren() .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); }
@Test public void testSummingEnricherWhenNullSensorValue() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); entity.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testSummingEnricherWithOneValue() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); entity.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); }
@Test public void testSummingEnricherWithNoProducers() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromChildren() .defaultValueForUnreportedSensors(11) .valueToReportIfNoSensors(40) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 40); }