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

    https://github.com/apache/incubator-brooklyn/pull/782#discussion_r36251169
  
    --- Diff: 
software/nosql/src/main/java/brooklyn/entity/nosql/redis/RedisClusterImpl.java 
---
    @@ -20,64 +20,111 @@
     
     import java.util.Collection;
     
    -import brooklyn.entity.Entity;
    +import brooklyn.enricher.Enrichers;
     import brooklyn.entity.basic.AbstractEntity;
    -import brooklyn.entity.basic.Entities;
    +import brooklyn.entity.basic.Lifecycle;
    +import brooklyn.entity.basic.ServiceStateLogic;
    +import 
brooklyn.entity.basic.ServiceStateLogic.ComputeServiceIndicatorsFromChildrenAndMembers;
    +import brooklyn.entity.basic.ServiceStateLogic.ServiceProblemsLogic;
     import brooklyn.entity.group.DynamicCluster;
     import brooklyn.entity.proxying.EntitySpec;
    -import brooklyn.entity.trait.Startable;
    +import brooklyn.event.AttributeSensor;
    +import brooklyn.event.basic.Sensors;
     import brooklyn.location.Location;
    +import brooklyn.util.collections.QuorumCheck.QuorumChecks;
    +import brooklyn.util.exceptions.Exceptions;
    +
    +import com.google.common.collect.ImmutableList;
    +import com.google.common.collect.ImmutableMap;
    +import com.google.common.collect.ImmutableSet;
     
     public class RedisClusterImpl extends AbstractEntity implements 
RedisCluster {
     
    -    protected RedisStore master;
    -    protected DynamicCluster slaves;
    +    private static AttributeSensor<RedisStore> MASTER = 
Sensors.newSensor(RedisStore.class, "redis.master");
    +    private static AttributeSensor<DynamicCluster> SLAVES = 
Sensors.newSensor(DynamicCluster.class, "redis.slaves");
     
         public RedisClusterImpl() {
         }
     
         @Override
         public RedisStore getMaster() {
    -        return master;
    +        return getAttribute(MASTER);
         }
         
         @Override
         public DynamicCluster getSlaves() {
    -        return slaves;
    +        return getAttribute(SLAVES);
    +    }
    +
    +    @Override
    +    public void init() {
    +        super.init();
    +
    +        RedisStore master = addChild(EntitySpec.create(RedisStore.class));
    +        setAttribute(MASTER, master);
    +
    +        DynamicCluster slaves = 
addChild(EntitySpec.create(DynamicCluster.class)
    +                .configure(DynamicCluster.MEMBER_SPEC, 
EntitySpec.create(RedisSlave.class).configure(RedisSlave.MASTER, master)));
    +        setAttribute(SLAVES, slaves);
    +
    +        addEnricher(Enrichers.builder()
    +                .propagating(RedisStore.HOSTNAME, RedisStore.ADDRESS, 
RedisStore.SUBNET_HOSTNAME, RedisStore.SUBNET_ADDRESS, RedisStore.REDIS_PORT)
    +                .from(master)
    +                .build());
    +    }
    +
    +    @Override
    +    protected void initEnrichers() {
    +        super.initEnrichers();
    +        ServiceStateLogic.newEnricherFromChildrenUp().
    +            checkChildrenOnly().
    +            requireUpChildren(QuorumChecks.all()).
    +            
configure(ComputeServiceIndicatorsFromChildrenAndMembers.IGNORE_ENTITIES_WITH_THESE_SERVICE_STATES,
 ImmutableSet.<Lifecycle>of()).
    +            addTo(this);
         }
         
         @Override
         public void start(Collection<? extends Location> locations) {
    -        master = addChild(EntitySpec.create(RedisStore.class));
    -        Entities.manage(master);
    -        master.start(locations);
    +        ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);
    +        ServiceProblemsLogic.clearProblemsIndicator(this, START);
    +        try {
    +            doStart(locations);
    +            ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
    +        } catch (Exception e) {
    +            ServiceProblemsLogic.updateProblemsIndicator(this, START, 
"Start failed with error: "+e);
    +            ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
    +            throw Exceptions.propagate(e);
    +        }
    +    }
     
    -        slaves = addChild(EntitySpec.create(DynamicCluster.class)
    -                .configure(DynamicCluster.MEMBER_SPEC, 
EntitySpec.create(RedisSlave.class).configure(RedisSlave.MASTER, master)));
    -        slaves.start(locations);
    +    private void doStart(Collection<? extends Location> locations) {
    +        RedisStore master = getMaster();
    +        master.invoke(RedisStore.START, ImmutableMap.<String, 
Object>of("locations", ImmutableList.copyOf(locations))).getUnchecked();
     
    -        setAttribute(Startable.SERVICE_UP, calculateServiceUp());
    +        DynamicCluster slaves = getSlaves();
    +        slaves.invoke(DynamicCluster.START, ImmutableMap.<String, 
Object>of("locations", ImmutableList.copyOf(locations))).getUnchecked();
         }
     
         @Override
         public void stop() {
    -        if (slaves != null) slaves.stop();
    -        if (master != null) master.stop();
    +        ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING);
    +        try {
    +            doStop();
    +            ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED);
    +        } catch (Exception e) {
    +            ServiceProblemsLogic.updateProblemsIndicator(this, STOP, "Stop 
failed with error: "+e);
    +            ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
    +            throw Exceptions.propagate(e);
    +        }
    +    }
     
    -        setAttribute(Startable.SERVICE_UP, false);
    +    private void doStop() {
    +        getSlaves().invoke(DynamicCluster.STOP, ImmutableMap.<String, 
Object>of()).getUnchecked();
    --- End diff --
    
    This is fine for the PR, but I suspect if there is an error stopping the 
slaves then the master won't be stopped either. Not sure off-hand what best 
approach is for that.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to