Address PR comments
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/8cfe58e5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/8cfe58e5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/8cfe58e5 Branch: refs/heads/master Commit: 8cfe58e52c031126489a46a89872cd2c9c696552 Parents: 731a111 Author: Mike Zaccardo <[email protected]> Authored: Mon Aug 3 13:55:11 2015 -0400 Committer: Mike Zaccardo <[email protected]> Committed: Mon Aug 3 13:55:11 2015 -0400 ---------------------------------------------------------------------- .../entity/nosql/redis/RedisClusterImpl.java | 91 +++++++++++++------- 1 file changed, 58 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8cfe58e5/software/nosql/src/main/java/brooklyn/entity/nosql/redis/RedisClusterImpl.java ---------------------------------------------------------------------- diff --git a/software/nosql/src/main/java/brooklyn/entity/nosql/redis/RedisClusterImpl.java b/software/nosql/src/main/java/brooklyn/entity/nosql/redis/RedisClusterImpl.java index 0da5155..e46ce65 100644 --- a/software/nosql/src/main/java/brooklyn/entity/nosql/redis/RedisClusterImpl.java +++ b/software/nosql/src/main/java/brooklyn/entity/nosql/redis/RedisClusterImpl.java @@ -23,21 +23,24 @@ import java.util.Collection; import brooklyn.enricher.Enrichers; import brooklyn.entity.Entity; 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.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.exceptions.Exceptions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; public class RedisClusterImpl extends AbstractEntity implements RedisCluster { - private AttributeSensor<RedisStore> MASTER = Sensors.newSensor(RedisStore.class, "redis.master"); - private AttributeSensor<DynamicCluster> SLAVES = Sensors.newSensor(DynamicCluster.class, "redis.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() { } @@ -51,44 +54,65 @@ public class RedisClusterImpl extends AbstractEntity implements RedisCluster { public DynamicCluster getSlaves() { return getAttribute(SLAVES); } + + 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 public void start(Collection<? extends Location> locations) { - RedisStore master = getMaster(); - if (master == null) { - master = addChild(EntitySpec.create(RedisStore.class)); - setAttribute(MASTER, master); + 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); + } finally { + setAttribute(Startable.SERVICE_UP, calculateServiceUp()); } + } - Entities.manage(master); + private void doStart(Collection<? extends Location> locations) { + RedisStore master = getMaster(); master.invoke(RedisStore.START, ImmutableMap.<String, Object>of("locations", ImmutableList.copyOf(locations))).getUnchecked(); DynamicCluster slaves = getSlaves(); - if (slaves == null) { - slaves = addChild(EntitySpec.create(DynamicCluster.class) - .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(RedisSlave.class).configure(RedisSlave.MASTER, master))); - setAttribute(SLAVES, slaves); - } - slaves.invoke(DynamicCluster.START, ImmutableMap.<String, Object>of("locations", ImmutableList.copyOf(locations))).getUnchecked(); - - setAttribute(Startable.SERVICE_UP, calculateServiceUp()); - - addEnricher(Enrichers.builder() - .propagating(RedisStore.HOSTNAME, RedisStore.ADDRESS, RedisStore.SUBNET_HOSTNAME, RedisStore.SUBNET_ADDRESS, RedisStore.REDIS_PORT) - .from(master) - .build()); } @Override public void stop() { - DynamicCluster slaves = getSlaves(); - RedisStore master = getMaster(); - - if (slaves != null) slaves.invoke(DynamicCluster.STOP, ImmutableMap.<String, Object>of()).getUnchecked(); - if (master != null) master.invoke(RedisStore.STOP, ImmutableMap.<String, Object>of()).getUnchecked(); + 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); + } finally { + setAttribute(Startable.SERVICE_UP, calculateServiceUp()); + } + } - setAttribute(Startable.SERVICE_UP, false); + private void doStop() { + getSlaves().invoke(DynamicCluster.STOP, ImmutableMap.<String, Object>of()).getUnchecked(); + getMaster().invoke(RedisStore.STOP, ImmutableMap.<String, Object>of()).getUnchecked(); } @Override @@ -96,11 +120,12 @@ public class RedisClusterImpl extends AbstractEntity implements RedisCluster { throw new UnsupportedOperationException(); } - protected boolean calculateServiceUp() { - boolean up = false; - for (Entity member : getSlaves().getMembers()) { - if (Boolean.TRUE.equals(member.getAttribute(SERVICE_UP))) up = true; - } - return up; + private boolean calculateServiceUp() { + if (!Boolean.TRUE.equals(getMaster().getAttribute(SERVICE_UP))) return false; + + for (Entity member : getSlaves().getMembers()) + if (!Boolean.TRUE.equals(member.getAttribute(SERVICE_UP))) return false; + + return true; } }
