@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 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"))); }
@Test public void testAggregatingRespectsUnchanged() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .<Object>publishing(LONG1) .fromMembers() .computing(new Function<Iterable<Integer>, Object>() { @Override public Object apply(Iterable<Integer> input) { if (input != null && Iterables.contains(input, 123)) { return Enrichers.sum(input, 0, 0, new TypeToken<Integer>(){}); } else { return Entities.UNCHANGED; } }}) .build()); entity.sensors().set(NUM1, 123); EntityAsserts.assertAttributeEqualsEventually(group, LONG1, Long.valueOf(123)); entity.sensors().set(NUM1, 987654); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", "100ms"), group, LONG1, Long.valueOf(123)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void initEnrichers() { super.initEnrichers(); propagateMasterAttribute(MySqlNode.HOSTNAME); propagateMasterAttribute(MySqlNode.ADDRESS); propagateMasterAttribute(MySqlNode.SUBNET_HOSTNAME); propagateMasterAttribute(MySqlNode.SUBNET_ADDRESS); propagateMasterAttribute(MySqlNode.MYSQL_PORT); propagateMasterAttribute(MySqlNode.DATASTORE_URL); enrichers().add(Enrichers.builder() .aggregating(MySqlNode.DATASTORE_URL) .publishing(SLAVE_DATASTORE_URL_LIST) .computing((Function<Collection<String>, List<String>>)(Function)Functions.identity()) .entityFilter(Predicates.not(MySqlClusterUtils.IS_MASTER)) .fromMembers() .build()); enrichers().add(Enrichers.builder() .aggregating(MySqlNode.QUERIES_PER_SECOND_FROM_MYSQL) .publishing(QUERIES_PER_SECOND_FROM_MYSQL_PER_NODE) .fromMembers() .computingAverage() .defaultValueForUnreportedSensors(0d) .build()); }
@Test public void testAggregatingExcludingBlankString() { group.addMember(entity); group.addMember(entity2); group.enrichers().add(Enrichers.builder() .aggregating(STR1) .publishing(SET1) .fromMembers() .excludingBlank() .computing(new Function<Collection<?>, Set<Object>>() { @Override public Set<Object> apply(Collection<?> input) { // accept null values, so don't use ImmutableSet return (input == null) ? ImmutableSet.<Object>of() : MutableSet.<Object>copyOf(input); }}) .build()); entity.sensors().set(STR1, "1"); entity2.sensors().set(STR1, "2"); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("1", "2")); entity.sensors().set(STR1, "3"); entity2.sensors().set(STR1, null); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("3")); entity.sensors().set(STR1, ""); entity2.sensors().set(STR1, "4"); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("4")); }
@Test public void testCustomAggregatingFunction() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Function<Collection<Integer>,Integer> aggregator = new Function<Collection<Integer>, Integer>() { @Override public Integer apply(Collection<Integer> input) { int result = 1; for (Integer in : input) result += in*in; return result; } }; entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computing(aggregator) .fromHardcodedProducers(ImmutableList.of(producer1)) .defaultValueForUnreportedSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); // Event by producer producer1.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 5); } }
@Test public void testAggregatingExcludingNull() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .publishing(SET1) .fromMembers() .excludingBlank() .computing(new Function<Collection<?>, Set<Object>>() { @Override public Set<Object> apply(Collection<?> input) { // accept null values, so don't use ImmutableSet return (input == null) ? ImmutableSet.<Object>of() : MutableSet.<Object>copyOf(input); }}) .build()); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of()); entity.sensors().set(NUM1, 1); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of(1)); entity.sensors().set(NUM1, null); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of()); entity.sensors().set(NUM1, 2); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of(2)); }
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()); }