You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by aledsage <gi...@git.apache.org> on 2016/06/23 14:52:59 UTC
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
GitHub user aledsage opened a pull request:
https://github.com/apache/brooklyn-server/pull/218
Yaml entity refs: support $brooklyn:self()
- Adds proper test coverage
- Fixes sibling/descendant/ancestor so will never return self
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/aledsage/brooklyn-server yaml-dsl-entity-refs
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/brooklyn-server/pull/218.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #218
----
commit b21c5fcfc3ebc000b321419f4bb2934a0f85d5d6
Author: Aled Sage <al...@gmail.com>
Date: 2016-06-23T14:49:12Z
Yaml entity refs: support $brooklyn:self()
- Adds proper test coverage
- Fixes sibling/descendant/ancestor so will never return self
----
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/218#discussion_r69441669
--- Diff: launcher/src/test/java/org/apache/brooklyn/launcher/blueprints/Windows7zipBlueprintLiveTest.java ---
@@ -81,7 +81,7 @@ public void test7zip() throws Exception {
Predicate<Application> asserter = new Predicate<Application>() {
@Override public boolean apply(Application app) {
- VanillaWindowsProcess entity = Iterables.getOnlyElement(Entities.descendants(app, VanillaWindowsProcess.class));
+ VanillaWindowsProcess entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, VanillaWindowsProcess.class));
--- End diff --
The author of this test clearly only wanted self here - is it worth adding a self method to `Entities`? (or if this comes up a lot a `rootElement()` method on Application? - not necessary for this PR)
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
Thanks @robertgmoss - incorporated those comments; merging now.
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/218#discussion_r69440113
--- Diff: camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java ---
@@ -100,30 +103,36 @@ protected EntityInternal getEntity() {
@Override
public Entity call() throws Exception {
Iterable<Entity> entitiesToSearch = null;
+ EntityInternal entity = getEntity();
+ Predicate<Entity> notSelfPredicate = Predicates.not(Predicates.<Entity>equalTo(entity));
+
switch (scope) {
case THIS:
- return getEntity();
+ return entity;
case PARENT:
- return getEntity().getParent();
+ return entity.getParent();
case GLOBAL:
- entitiesToSearch = ((EntityManagerInternal)getEntity().getManagementContext().getEntityManager())
+ entitiesToSearch = ((EntityManagerInternal)entity.getManagementContext().getEntityManager())
.getAllEntitiesInApplication( entity().getApplication() );
break;
case ROOT:
- return getEntity().getApplication();
+ return entity.getApplication();
case SCOPE_ROOT:
- return Entities.catalogItemScopeRoot(getEntity());
+ return Entities.catalogItemScopeRoot(entity);
case DESCENDANT:
- entitiesToSearch = Entities.descendants(getEntity());
+ entitiesToSearch = Entities.descendantsWithoutSelf(entity);
+ entitiesToSearch = Iterables.filter(entitiesToSearch, notSelfPredicate);
--- End diff --
is this required? previous line suggests self will not be included
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
@grkvlt we briefly changed the behaviour of `Propagator` to require that the producer be passed in (because previously the defaults were crazy - if no configuration, then propagate all your own sensors to yourself, thus causing an infinite loop!). We reverted that back to have the default producer being oneself, with the proper checks that there are no infinite loops.
But the point of this PR still stands for me: that we had now way to refer to self when supplying a config key value (for whatever use-cases of other enrichers, entities, etc). Therefore I'd argue this is still useful to include.
---
I'll also do as @robertgmoss suggests and rename `Entities.descendants()` to `Entities.descendantsAndSelf()`, and same for `Entities.ancestors()` (deprecating the old version).
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/218#discussion_r69441816
--- Diff: core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindEntityTest.java ---
@@ -310,7 +310,7 @@ public void testEntityTags() throws Exception {
origE.tags().addTag(origApp);
newApp = rebind();
- MyEntity newE = Iterables.getOnlyElement( Entities.descendants(newApp, MyEntity.class) );
+ MyEntity newE = Iterables.getOnlyElement( Entities.descendantsAndSelf(newApp, MyEntity.class) );
--- End diff --
same
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
@grkvlt you're right that `$brooklyn:self().attributeWhenReady("foo")` would be a pointless thing to write. The main use-case is for when you want to pass the entity itself as a config value (e.g. when configuring some kinds of enricher). For example, see the yaml edit in https://github.com/apache/brooklyn-library/pull/41/files.
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/brooklyn-server/pull/218
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
@aledsage assuming you're talking about `producer: $brooklyn:component("this", "ignored")` being replaced with `producer: $brooklyn:self()` isn't the default for an enricher to have the producer be the current entity context, anyway?
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
Makes sense not to have self included in `$brooklyn:descendants`, but I find it contrary to expectations that `Entities.descendants(entity)` does not mirror this. I presume this is to preserve backwards compatibility? To me it would make sense to call it `descendantsOrSelf()`.
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
Note the change to `test-referencing-entities.yaml` is to fix `EntitiesYamlTest.testMultipleReferences()`. That test relied on `$brooklyn:descendants("app1")` including itself in the search. I changed it so that it did not include itself (given one can easily do `$brooklyn:self()` instead). Please shout out if you think that's a bad idea @robertgmoss @neykov (but note that it does differ now compared to the behaviour of `Entities.descendants(entity)`).
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/218#discussion_r69440473
--- Diff: core/src/main/java/org/apache/brooklyn/core/entity/Entities.java ---
@@ -655,34 +677,112 @@ public static boolean isDescendant(Entity ancestor, Entity potentialDescendant)
* @see #descendants(Entity)
* @see Iterables#filter(Iterable, Class)
*/
- public static <T extends Entity> Iterable<T> descendants(Entity root, Class<T> ofType) {
- return Iterables.filter(descendants(root), ofType);
+ public static <T extends Entity> Iterable<T> descendantsAndSelf(Entity root, Class<T> ofType) {
+ return Iterables.filter(descendantsAndSelf(root), ofType);
}
/** Returns the entity, its parent, its parent, and so on. */
+ @SuppressWarnings("unused")
+ public static Iterable<Entity> ancestorsAndSelf(final Entity root) {
+ if (false) {
+ // Keeping this unused code, so that anonymous inner class numbers don't change!
+ return new Iterable<Entity>() {
+ @Override
+ public Iterator<Entity> iterator() {
+ return new Iterator<Entity>() {
+ Entity next = root;
+ @Override
+ public boolean hasNext() {
+ return next!=null;
+ }
+ @Override
+ public Entity next() {
+ Entity result = next;
+ next = next.getParent();
+ return result;
+ }
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ };
+ }
+ Set<Entity> result = Sets.newLinkedHashSet();
+ Entity current = root;
+ while (current != null) {
+ result.add(current);
+ current = current.getParent();
+ }
+ return result;
+ }
+
+ /** Returns the entity's parent, its parent's parent, and so on. */
+ public static Iterable<Entity> ancestorsWithoutSelf(Entity root) {
+ Set<Entity> result = Sets.newLinkedHashSet();
+ Entity parent = (root != null) ? root.getParent() : null;
+ while (parent != null) {
+ result.add(parent);
+ parent = parent.getParent();
+ }
+ return result;
+ }
+
+ /**
+ * Side-effects {@code result} to return descendants (not including {@code root}).
+ */
+ private static void descendantsWithoutSelf(Entity root, Collection<Entity> result) {
+ Stack<Entity> tovisit = new Stack<Entity>();
+ tovisit.add(root);
+
+ while (!tovisit.isEmpty()) {
+ Entity e = tovisit.pop();
+ result.addAll(e.getChildren());
+ tovisit.addAll(e.getChildren());
+ }
+ }
+
+
+
+
+
+
+
+
--- End diff --
excessive whitespace
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
@aledsage but isn't `$brooklyn:self().attributeWhenReady("foo")` exactly the same as `$brooklyn:attributeWhenReady("foo")` or am I missing something clever?
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #218: Yaml entity refs: support $brooklyn:self()
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on the issue:
https://github.com/apache/brooklyn-server/pull/218
@aledsage test failure in `EntitiesYamlTest.testMultipleReferences()`
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/218#discussion_r69442254
--- Diff: launcher/src/test/java/org/apache/brooklyn/launcher/blueprints/Windows7zipBlueprintLiveTest.java ---
@@ -81,7 +81,7 @@ public void test7zip() throws Exception {
Predicate<Application> asserter = new Predicate<Application>() {
@Override public boolean apply(Application app) {
- VanillaWindowsProcess entity = Iterables.getOnlyElement(Entities.descendants(app, VanillaWindowsProcess.class));
+ VanillaWindowsProcess entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, VanillaWindowsProcess.class));
--- End diff --
Ignore me, it might not be the root being searched for.
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #218: Yaml entity refs: support $brooklyn:self(...
Posted by robertgmoss <gi...@git.apache.org>.
Github user robertgmoss commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/218#discussion_r69440147
--- Diff: camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java ---
@@ -100,30 +103,36 @@ protected EntityInternal getEntity() {
@Override
public Entity call() throws Exception {
Iterable<Entity> entitiesToSearch = null;
+ EntityInternal entity = getEntity();
+ Predicate<Entity> notSelfPredicate = Predicates.not(Predicates.<Entity>equalTo(entity));
+
switch (scope) {
case THIS:
- return getEntity();
+ return entity;
case PARENT:
- return getEntity().getParent();
+ return entity.getParent();
case GLOBAL:
- entitiesToSearch = ((EntityManagerInternal)getEntity().getManagementContext().getEntityManager())
+ entitiesToSearch = ((EntityManagerInternal)entity.getManagementContext().getEntityManager())
.getAllEntitiesInApplication( entity().getApplication() );
break;
case ROOT:
- return getEntity().getApplication();
+ return entity.getApplication();
case SCOPE_ROOT:
- return Entities.catalogItemScopeRoot(getEntity());
+ return Entities.catalogItemScopeRoot(entity);
case DESCENDANT:
- entitiesToSearch = Entities.descendants(getEntity());
+ entitiesToSearch = Entities.descendantsWithoutSelf(entity);
+ entitiesToSearch = Iterables.filter(entitiesToSearch, notSelfPredicate);
break;
case ANCESTOR:
- entitiesToSearch = Entities.ancestors(getEntity());
+ entitiesToSearch = Entities.ancestorsWithoutSelf(entity);
+ entitiesToSearch = Iterables.filter(entitiesToSearch, notSelfPredicate);
--- End diff --
same
---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---