Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/879#discussion_r149962511
  
    --- Diff: 
policy/src/main/java/org/apache/brooklyn/policy/failover/PropagatePrimaryEnricher.java
 ---
    @@ -0,0 +1,208 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.policy.failover;
    +
    +import java.util.Collection;
    +import java.util.Map;
    +import java.util.Set;
    +
    +import org.apache.brooklyn.api.effector.Effector;
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.mgmt.TaskAdaptable;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.api.sensor.Sensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.effector.EffectorTasks.EffectorTaskFactory;
    +import org.apache.brooklyn.core.effector.Effectors;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.core.entity.EntityInternal;
    +import org.apache.brooklyn.core.sensor.Sensors;
    +import org.apache.brooklyn.enricher.stock.Propagator;
    +import org.apache.brooklyn.util.collections.MutableSet;
    +import org.apache.brooklyn.util.core.config.ConfigBag;
    +import org.apache.brooklyn.util.core.task.DynamicTasks;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.annotations.Beta;
    +import com.google.common.base.Objects;
    +
    +/** Makes all sensors/effectors available on primary mirrored at this node,
    + * apart from those already present here. */ 
    +@Beta
    +public class PropagatePrimaryEnricher extends AbstractEnricher implements 
SensorEventListener<Object> {
    +
    +    private static final Logger log = 
LoggerFactory.getLogger(PropagatePrimaryEnricher.class);
    +    
    +    public static final ConfigKey<String> PRIMARY_SENSOR_NAME = 
ElectPrimaryConfig.PRIMARY_SENSOR_NAME;
    +    
    +    public static final ConfigKey<Boolean> PROPAGATE_EFFECTORS = 
ConfigKeys.newBooleanConfigKey("propagate.effectors",
    +        "Whether to propagate effectors, default true (effectors already 
defined here will not be propagated)",
    +        true);
    +
    +    public static final ConfigKey<Boolean> PROPAGATING_ALL = 
Propagator.PROPAGATING_ALL;
    +    public static final ConfigKey<Collection<? extends Sensor<?>>> 
PROPAGATING_ALL_BUT = Propagator.PROPAGATING_ALL_BUT;
    +    public static final ConfigKey<Collection<? extends Sensor<?>>> 
PROPAGATING = Propagator.PROPAGATING;
    +    public static final ConfigKey<Map<? extends Sensor<?>, ? extends 
Sensor<?>>> SENSOR_MAPPING = Propagator.SENSOR_MAPPING;
    +    
    +    //TODO rebind
    +    
    +    Entity lastPrimary;
    +    Propagator propagator;
    +    
    +    Set<String> effectorsAddedForPrimary;
    +    Set<String> blacklistedEffectors = MutableSet.of("start", "stop", 
"restart", "promote", "demote",
    +        getConfig(ElectPrimaryConfig.PROMOTE_EFFECTOR_NAME), 
getConfig(ElectPrimaryConfig.DEMOTE_EFFECTOR_NAME));
    +    Set<String> blacklistedSensors = MutableSet.of();
    +    
    +    public void setEntity(@SuppressWarnings("deprecation") 
org.apache.brooklyn.api.entity.EntityLocal entity) {
    +        super.setEntity(entity);
    +        
    +        // TODO if not coming from rebind
    +        
blacklistedEffectors.addAll(((EntityInternal)entity).getMutableEntityType().getEffectors().keySet());
    +        
blacklistedSensors.addAll(((EntityInternal)entity).getMutableEntityType().getSensors().keySet());
    +        for (Sensor<?> s: Propagator.SENSORS_NOT_USUALLY_PROPAGATED) {
    +            blacklistedSensors.add(s.getName());
    +        }
    +        
blacklistedSensors.addAll(MutableSet.of(getConfig(PRIMARY_SENSOR_NAME), 
getConfig(ElectPrimaryConfig.PRIMARY_WEIGHT_NAME)));
    +        
    +        subscriptions().subscribe(entity, Sensors.newSensor(Entity.class, 
config().get(PRIMARY_SENSOR_NAME)), this);
    +        onEvent(null);
    +    }
    +
    +    @Override
    +    public synchronized void onEvent(SensorEvent<Object> event) {
    +        Entity primary = entity.getAttribute( 
Sensors.newSensor(Entity.class, config().get(PRIMARY_SENSOR_NAME)) );
    +        if (!Objects.equal(primary, lastPrimary)) {
    +            log.debug("Removing propagated items from "+lastPrimary+" at 
"+entity);
    +            
    +            // remove propagator
    +            if (propagator!=null) {
    +                entity.enrichers().remove(propagator);
    +                propagator = null;
    +            }
    +
    +            // remove propagated effectors
    +            if (effectorsAddedForPrimary!=null) {
    +                log.debug("Removing propagated effectors from 
"+lastPrimary+" at "+entity+": "+effectorsAddedForPrimary);
    +                for (String effN: effectorsAddedForPrimary) {
    +                    Effector<?> effE = 
((EntityInternal)entity).getMutableEntityType().getEffector(effN);
    +                    if (effE!=null) {
    +                        
((EntityInternal)entity).getMutableEntityType().removeEffector(effE);
    +                    }
    +                }
    +                effectorsAddedForPrimary = null;
    +            }
    +            
    +            // remove all but blacklisted sensors
    +            Set<AttributeSensor<?>> sensorsToRemove = MutableSet.of();
    +            for (AttributeSensor<?> s: 
((EntityInternal)entity).sensors().getAll().keySet()) {
    +                if (!blacklistedSensors.contains(s.getName())) {
    --- End diff --
    
    This feels dangerous. When we populate the `blacklistedSensors` in 
`setEntity` via `entityType.getSensors()`, it will do this very early in the 
entity's lifecycle. In `setEntity`, the entity may not yet have all the sensors 
that it will get at runtime.
    
    For example, consider this code:
    ```
        @Test
        public void testFoo() {
            
mgmt.getEntityManager().createEntity(EntitySpec.create(BasicApplication.class)
                    .enricher(EnricherSpec.create(MyEnricher.class)));
        }
        public static class MyEnricher extends AbstractEnricher {
            @Override
            public void setEntity(EntityLocal entity) {
                super.setEntity(entity);
                System.out.println(com.google.common.base.Joiner.on(", 
").join(((EntityInternal)entity).getMutableEntityType().getSensors().keySet()));
            }
        }
    ```
    The sensors that a `BasicApplication` has when the entity is being 
instantiated is just:
    ```
    service.isUp, entity.location.removed, entity.config_key.added, 
service.notUp.indicators, 
    entity.children.removed, entity.config_key.removed, entity.group.added, 
entity.effector.removed, 
    entity.sensor.removed, entity.id, entity.location.added, 
entity.group.removed, catalog.id, 
    entity.effector.added, entity.policy.removed, entity.effector.changed, 
entity.children.added, 
    entity.policy.added, entity.sensor.added, application.id
    ```
    
    It does not yet have sensors like `service.state`, `service.problems`, etc, 
which are added dynamically.
    
    If I read your code correctly, then these would be automatically removed 
when the primary changed!
    
    ---
    I really think we're trying to support too much for an initial version of 
this policy/enricher. We could simplify things a lot if it was only 
configurable with `PROPAGATING` and perhaps `SENSOR_MAPPING`.


---

Reply via email to