brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [2/4] git commit: Start mirroring remote entities asynchronously.
Date Tue, 09 Sep 2014 10:21:36 GMT
Start mirroring remote entities asynchronously.

The MIRRORED_ENTITY_URL could be a task instead of a plain property which could block on an
attempt to access it. Instead of potentially blocking .init() create a task to start the mirroring
as soon as the property is accessible.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/de6b1109
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/de6b1109
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/de6b1109

Branch: refs/heads/master
Commit: de6b11090794a7f2963d9637d1dac2b7cb9f1601
Parents: 3213671
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Fri Sep 5 22:12:42 2014 +0300
Committer: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Committed: Mon Sep 8 14:50:18 2014 +0300

----------------------------------------------------------------------
 .../brooklynnode/BrooklynEntityMirrorImpl.java  | 40 ++++++++++++++++----
 1 file changed, 33 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/de6b1109/software/base/src/main/java/brooklyn/entity/brooklynnode/BrooklynEntityMirrorImpl.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/brooklynnode/BrooklynEntityMirrorImpl.java
b/software/base/src/main/java/brooklyn/entity/brooklynnode/BrooklynEntityMirrorImpl.java
index 504a408..fb87f39 100644
--- a/software/base/src/main/java/brooklyn/entity/brooklynnode/BrooklynEntityMirrorImpl.java
+++ b/software/base/src/main/java/brooklyn/entity/brooklynnode/BrooklynEntityMirrorImpl.java
@@ -20,6 +20,7 @@ package brooklyn.entity.brooklynnode;
 
 import java.net.URI;
 import java.util.Map;
+import java.util.concurrent.Callable;
 
 import javax.annotation.Nullable;
 
@@ -33,6 +34,8 @@ import brooklyn.entity.basic.Attributes;
 import brooklyn.entity.basic.BrooklynTaskTags;
 import brooklyn.entity.basic.Entities;
 import brooklyn.entity.basic.EntityFunctions;
+import brooklyn.entity.basic.Lifecycle;
+import brooklyn.entity.basic.ServiceStateLogic;
 import brooklyn.entity.effector.EffectorBody;
 import brooklyn.event.basic.Sensors;
 import brooklyn.event.feed.http.HttpFeed;
@@ -47,6 +50,7 @@ import brooklyn.util.http.HttpTool.HttpClientBuilder;
 import brooklyn.util.http.HttpToolResponse;
 import brooklyn.util.net.Urls;
 import brooklyn.util.stream.Streams;
+import brooklyn.util.task.DynamicTasks;
 import brooklyn.util.task.Tasks;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -70,10 +74,34 @@ public class BrooklynEntityMirrorImpl extends AbstractEntity implements
Brooklyn
     @Override
     public void init() {
         super.init();
-        connectSensors();
+        connectSensorsAsync();
+
+        //start spinning, could take some time before MIRRORED_ENTITY_URL is available for
first time mirroring
+        setAttribute(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.STARTING);
     }
 
-    protected void connectSensors() {
+    protected void connectSensorsAsync() {
+        Callable<Void> asyncTask = new Callable<Void>() {
+            @Override
+            public Void call() throws Exception {
+                //blocks until available (could be a task)
+                String mirroredEntityUrl = getConfig(MIRRORED_ENTITY_URL);
+                Preconditions.checkNotNull(mirroredEntityUrl, "Required config: "+MIRRORED_ENTITY_URL);
+
+                connectSensors(mirroredEntityUrl);
+                return null;
+            }
+        };
+
+        DynamicTasks.queueIfPossible(
+                Tasks.<Void>builder()
+                    .description("Start entity mirror feed")
+                    .body(asyncTask)
+                    .build())
+            .orSubmitAsync(this);
+    }
+
+    protected void connectSensors(String mirroredEntityUrl) {
         Function<HttpToolResponse, Void> mirrorSensors = new Function<HttpToolResponse,Void>()
{
             @SuppressWarnings("rawtypes")
             @Override
@@ -85,11 +113,9 @@ public class BrooklynEntityMirrorImpl extends AbstractEntity implements
Brooklyn
                 return null;
             }
         };
-        
-        String sensorsUri = Urls.mergePaths(
-            Preconditions.checkNotNull(getConfig(MIRRORED_ENTITY_URL), "Required config:
"+MIRRORED_ENTITY_URL),
-            "sensors/current-state");
-        
+
+        String sensorsUri = Urls.mergePaths(mirroredEntityUrl, "sensors/current-state");
+
         mirror = HttpFeed.builder().entity(this)
             .baseUri(sensorsUri)
             .credentialsIfNotNull(getConfig(BrooklynNode.MANAGEMENT_USER), getConfig(BrooklynNode.MANAGEMENT_PASSWORD))


Mime
View raw message