in software/nosql/src/main/java/org/apache/brooklyn/entity/nosql/mongodb/MongoDBReplicaSetImpl.java [344:413]
public void start(Collection<? extends Location> locations) {
// Promises that all the cluster's members have SERVICE_UP true on returning.
super.start(locations);
policy = policies().add(PolicySpec.create(MemberTrackingPolicy.class)
.displayName(getName() + " membership tracker")
.configure("group", this));
for (AttributeSensor<Long> sensor: SENSORS_TO_SUM)
enrichers().add(Enrichers.builder()
.aggregating(sensor)
.publishing(sensor)
.fromMembers()
.computingSum()
.valueToReportIfNoSensors(null)
.defaultValueForUnreportedSensors(null)
.build());
// FIXME would it be simpler to have a *subscription* on four or five sensors on allMembers, including SERVICE_UP
// (which we currently don't check), rather than an enricher, and call to an "update" method?
enrichers().add(Enrichers.builder()
.aggregating(MongoDBServer.REPLICA_SET_PRIMARY_ENDPOINT)
.publishing(MongoDBServer.REPLICA_SET_PRIMARY_ENDPOINT)
.fromMembers()
.valueToReportIfNoSensors(null)
.computing(new Function<Collection<String>, String>() {
@Override
public String apply(Collection<String> input) {
if (input==null || input.isEmpty()) return null;
Set<String> distinct = MutableSet.of();
for (String endpoint: input)
if (!Strings.isBlank(endpoint))
distinct.add(endpoint);
if (distinct.size()>1)
LOG.warn("Mongo replica set "+MongoDBReplicaSetImpl.this+" detetcted multiple masters (transitioning?): "+distinct);
return input.iterator().next();
}})
.build());
enrichers().add(Enrichers.builder()
.aggregating(MongoDBServer.MONGO_SERVER_ENDPOINT)
.publishing(REPLICA_SET_ENDPOINTS)
.fromMembers()
.valueToReportIfNoSensors(null)
.computing(new Function<Collection<String>, List<String>>() {
@Override
public List<String> apply(Collection<String> input) {
Set<String> endpoints = new TreeSet<String>();
for (String endpoint: input) {
if (!Strings.isBlank(endpoint)) {
endpoints.add(endpoint);
}
}
return MutableList.copyOf(endpoints);
}})
.build());
enrichers().add(Enrichers.builder()
.transforming(REPLICA_SET_ENDPOINTS)
.publishing(DATASTORE_URL)
.computing(new EndpointsToDatastoreUrlMapper(this))
.build());
subscriptions().subscribeToMembers(this, MongoDBServer.IS_PRIMARY_FOR_REPLICA_SET, new SensorEventListener<Boolean>() {
@Override public void onEvent(SensorEvent<Boolean> event) {
if (Boolean.TRUE == event.getValue())
sensors().set(PRIMARY_ENTITY, (MongoDBServer)event.getSource());
}
});
}