Repository: incubator-brooklyn Updated Branches: refs/heads/master 60af015b7 -> f517b66c2
Add basic sensors to RedisCluster Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/731a1113 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/731a1113 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/731a1113 Branch: refs/heads/master Commit: 731a1113e975782ca42d20da36359774d2946a02 Parents: d90a8bf Author: Mike Zaccardo <[email protected]> Authored: Fri Jul 31 17:33:16 2015 -0400 Committer: Mike Zaccardo <[email protected]> Committed: Fri Jul 31 17:33:16 2015 -0400 ---------------------------------------------------------------------- .../entity/group/DynamicClusterTest.java | 14 ++++++ .../entity/nosql/redis/RedisClusterImpl.java | 49 ++++++++++++++------ 2 files changed, 50 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/731a1113/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java b/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java index 947068d..1d144a0 100644 --- a/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java +++ b/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java @@ -122,6 +122,20 @@ public class DynamicClusterTest extends BrooklynAppUnitTestSupport { if (Exceptions.getFirstThrowableOfType(e, IllegalStateException.class) == null) throw e; } } + + @Test + public void startThenStopThenStartWithNewLocationFails() throws Exception { + DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) + .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class))); + try { + cluster.start(ImmutableList.of(loc)); + cluster.stop(); + cluster.start(ImmutableList.of(loc2)); + fail(); + } catch (Exception e) { + if (Exceptions.getFirstThrowableOfType(e, IllegalStateException.class) == null) throw e; + } + } @Test public void startMethodFailsIfLocationsParameterHasMoreThanOneElement() throws Exception { http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/731a1113/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 cfa186a..0da5155 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 @@ -20,49 +20,73 @@ package brooklyn.entity.nosql.redis; 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.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 com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + public class RedisClusterImpl extends AbstractEntity implements RedisCluster { - protected RedisStore master; - protected DynamicCluster slaves; + private AttributeSensor<RedisStore> MASTER = Sensors.newSensor(RedisStore.class, "redis.master"); + private 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 start(Collection<? extends Location> locations) { - master = addChild(EntitySpec.create(RedisStore.class)); + RedisStore master = getMaster(); + if (master == null) { + master = addChild(EntitySpec.create(RedisStore.class)); + setAttribute(MASTER, master); + } + Entities.manage(master); - master.start(locations); + master.invoke(RedisStore.START, ImmutableMap.<String, Object>of("locations", ImmutableList.copyOf(locations))).getUnchecked(); - slaves = addChild(EntitySpec.create(DynamicCluster.class) - .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(RedisSlave.class).configure(RedisSlave.MASTER, master))); - slaves.start(locations); + 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() { - if (slaves != null) slaves.stop(); - if (master != null) master.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(); setAttribute(Startable.SERVICE_UP, false); } @@ -74,10 +98,9 @@ public class RedisClusterImpl extends AbstractEntity implements RedisCluster { protected boolean calculateServiceUp() { boolean up = false; - for (Entity member : slaves.getMembers()) { + for (Entity member : getSlaves().getMembers()) { if (Boolean.TRUE.equals(member.getAttribute(SERVICE_UP))) up = true; } return up; } - }
