ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject incubator-ignite git commit: #ignite-965: compute nodejs task runs on all nodes.
Date Tue, 23 Jun 2015 08:30:11 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-965 5ddc04b29 -> 01c9c3acb


#ignite-965: compute nodejs task runs on all nodes.


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

Branch: refs/heads/ignite-965
Commit: 01c9c3acbfb7a937a774183a613afed4e7d04b8c
Parents: 5ddc04b
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Tue Jun 23 11:30:01 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Tue Jun 23 11:30:01 2015 +0300

----------------------------------------------------------------------
 .../IgniteComputeTaskCommandHandler.java        | 71 +++++++++++++++++---
 modules/nodejs/src/main/js/compute.js           |  2 +-
 modules/nodejs/src/test/js/test-compute.js      |  4 +-
 3 files changed, 64 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/01c9c3ac/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
index b20f9c2..a3708ab 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java
@@ -17,6 +17,9 @@
 
 package org.apache.ignite.internal.processors.rest.handlers.compute;
 
+import org.apache.ignite.*;
+import org.apache.ignite.cluster.*;
+import org.apache.ignite.compute.*;
 import org.apache.ignite.internal.*;
 import org.apache.ignite.internal.processors.rest.*;
 import org.apache.ignite.internal.processors.rest.handlers.*;
@@ -24,6 +27,8 @@ import org.apache.ignite.internal.processors.rest.request.*;
 import org.apache.ignite.internal.util.future.*;
 import org.apache.ignite.internal.util.typedef.T3;
 import org.apache.ignite.internal.util.typedef.internal.*;
+import org.apache.ignite.resources.*;
+import org.jetbrains.annotations.*;
 
 import javax.script.ScriptException;
 import java.util.*;
@@ -61,18 +66,64 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt
 
         List<T3<String, String, String>> mapping =  req0.mapping();
 
-        Set<Object> res = new HashSet<>();
-        System.out.println("MAPPING RESULTS " + mapping.size());
-        for (T3<String, String, String> f : mapping) {
-            try {
-                Object locRes = ctx.scripting().runJS(f.get1(), f.get3());
-                System.out.println("LOCAL RESULT=" +locRes);
-                res.add(locRes);
-            } catch (ScriptException e) {
-                e.printStackTrace();
+        Object res = ctx.grid().compute().execute(new JsTask(mapping, ctx), null);
+
+        return new GridFinishedFuture<>(new GridRestResponse(res));
+    }
+
+    private static class JsTask extends ComputeTaskAdapter<String, Object> {
+        /** Mapping. */
+        private List<T3<String, String, String>> mapping;
+
+        /** Grid kernal context. */
+        private GridKernalContext ctx;
+
+        /**
+         * @param mapping Task mapping.
+         */
+        public JsTask(List<T3<String, String, String>> mapping, GridKernalContext
ctx) {
+            this.mapping = mapping;
+            this.ctx = ctx;
+        }
+
+        /** {@inheritDoc} */
+        @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode>
nodes, String arg) {
+            Map<ComputeJob, ClusterNode> map = new HashMap<>();
+
+            for (final T3<String, String, String> job : mapping) {
+                UUID nodeId = UUID.fromString(job.get2());
+
+                ClusterNode node = ctx.grid().cluster().node(nodeId);
+
+                map.put(new ComputeJobAdapter() {
+                    /** Ignite. */
+                    @IgniteInstanceResource
+                    private Ignite ignite;
+
+                    @Override public Object execute() throws IgniteException {
+                        System.out.println("Compute job on node " + ignite.cluster().localNode().id());
+
+                        try {
+                            return ((IgniteKernal)ignite).context().scripting().runJS(job.get1(),
job.get3());
+                        }
+                        catch (ScriptException e) {
+                            throw new IgniteException(e);
+                        }
+                    }
+                }, node);
             }
+
+            return map;
         }
 
-        return new GridFinishedFuture<>(new GridRestResponse(res));
+        /** {@inheritDoc} */
+        @Nullable @Override public Object reduce(List<ComputeJobResult> results) {
+            List<Object> data = new ArrayList<>();
+
+            for (ComputeJobResult res : results)
+                data.add(res.getData());
+
+            return data;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/01c9c3ac/modules/nodejs/src/main/js/compute.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/main/js/compute.js b/modules/nodejs/src/main/js/compute.js
index cb83e5d..fb38e7a 100644
--- a/modules/nodejs/src/main/js/compute.js
+++ b/modules/nodejs/src/main/js/compute.js
@@ -113,7 +113,7 @@ Compute.prototype._onNodesExecute = function(task, arg, callback, err,
nodes) {
   for (var job of computeJobList) {
     params.push(Server.pair("f" + i, this._escape(job.func)));
     params.push(Server.pair("args" + i,  JSON.stringify(job.args)));
-    params.push(Server.pair("n" + i, job.node.nodeId));
+    params.push(Server.pair("n" + i, job.node));
     i++;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/01c9c3ac/modules/nodejs/src/test/js/test-compute.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/js/test-compute.js b/modules/nodejs/src/test/js/test-compute.js
index b8606ac..87a284c 100644
--- a/modules/nodejs/src/test/js/test-compute.js
+++ b/modules/nodejs/src/test/js/test-compute.js
@@ -87,8 +87,6 @@ function onError1(error, res) {
 
   assert(error == null);
 
-  assert(res.indexOf("AFFINITY CALL") !== -1);
-
   console.log("!!!!!!!!RES = " + res);
 
   TestUtils.testDone();
@@ -107,5 +105,7 @@ function onComputeResult(error, res) {
 
   console.log("!!!!!!!!EXECUTE TASK RESULT = " + res);
 
+  assert(res === 7, "Result is not correct. [expected=7, value=" + res + "].")
+
   TestUtils.testDone();
 }


Mime
View raw message