ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [4/4] incubator-ignite git commit: #ignite-965: processor throws ignite-checked exception.
Date Wed, 24 Jun 2015 10:06:50 GMT
#ignite-965: processor throws ignite-checked exception.


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

Branch: refs/heads/ignite-965
Commit: b7f2393e0d4bb4700b9d563295c85f2b122d4460
Parents: 64d1108
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Wed Jun 24 13:06:32 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Wed Jun 24 13:06:32 2015 +0300

----------------------------------------------------------------------
 .../IgniteScriptingCommandHandler.java          | 119 ++++++++++++-------
 .../scripting/IgniteScriptProcessor.java        |  15 ++-
 2 files changed, 82 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b7f2393e/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
index 687f72a..839f412 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/IgniteScriptingCommandHandler.java
@@ -50,19 +50,24 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
     public IgniteScriptingCommandHandler(GridKernalContext ctx) {
         super(ctx);
 
-        IgniteScriptProcessor script = ctx.scripting();
+        try {
+            IgniteScriptProcessor script = ctx.scripting();
 
-        String emitFunction = "function emit(result, f, args, nodeId) {result.push([f.toString(),
args, nodeId])}";
+            String emitFunction = "function emit(result, f, args, nodeId) {result.push([f.toString(),
args, nodeId])}";
 
-        String computeFunction = "function __compute(mapFuncSource, ids, args) {"  +
-            "       var res = [];" +
-            "       var f = __createJSFunction(mapFuncSource);" +
-            "       f(ids, args, emit.bind(null, res)); "  +
-            "       return res;" +
-            "   }";
+            String computeFunction = "function __compute(mapFuncSource, ids, args) {"  +
+                "       var res = [];" +
+                "       var f = __createJSFunction(mapFuncSource);" +
+                "       f(ids, args, emit.bind(null, res)); "  +
+                "       return res;" +
+                "   }";
 
-        script.addEngineFunction(emitFunction);
-        script.addEngineFunction(computeFunction);
+            script.addEngineFunction(emitFunction);
+            script.addEngineFunction(computeFunction);
+        }
+        catch (IgniteCheckedException e) {
+            ctx.log().error(e.getMessage());
+        }
     }
 
     /** {@inheritDoc} */
@@ -82,16 +87,22 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
 
                 final RestRunScriptRequest req0 = (RestRunScriptRequest) req;
 
-                Object callRes = ctx.grid().compute().call(new IgniteCallable<Object>()
{
+                GridRestResponse res = ctx.grid().compute().call(new IgniteCallable<GridRestResponse>()
{
                     @IgniteInstanceResource
                     private Ignite ignite;
 
-                    @Override public Object call() {
-                        return ((IgniteKernal) ignite).context().scripting().invokeFunction(req0.script());
+                    @Override public GridRestResponse call() {
+                        try {
+                            return new GridRestResponse(((IgniteKernal)ignite).
+                                context().scripting().invokeFunction(req0.script()));
+                        }
+                        catch (IgniteCheckedException e) {
+                            return new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage());
+                        }
                     }
                 });
 
-                return new GridFinishedFuture<>(new GridRestResponse(callRes));
+                return new GridFinishedFuture<>(res);
             }
 
             case EXECUTE_MAP_REDUCE_SCRIPT: {
@@ -102,10 +113,10 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
 
                 final RestMapReduceScriptRequest req0 = (RestMapReduceScriptRequest) req;
 
-                Object execRes = ctx.grid().compute().execute(
+                GridRestResponse execRes = ctx.grid().compute().execute(
                     new JsTask(req0.mapFunction(), req0.argument(), req0.reduceFunction(),
ctx), null);
 
-                return new GridFinishedFuture<>(new GridRestResponse(execRes));
+                return new GridFinishedFuture<>(execRes);
             }
         }
 
@@ -115,7 +126,7 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
     /**
      * JS Compute Task.
      */
-    private static class JsTask extends ComputeTaskAdapter<String, Object> {
+    private static class JsTask extends ComputeTaskAdapter<String, GridRestResponse>
{
         /** */
         private static final long serialVersionUID = 0L;
 
@@ -146,51 +157,67 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
 
         /** {@inheritDoc} */
         @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode>
nodes, String arg) {
-            Map<ComputeJob, ClusterNode> map = new HashMap<>();
+            try {
+                Map<ComputeJob, ClusterNode> map = new HashMap<>();
 
-            String[] ids = new String[nodes.size()];
+                String[] ids = new String[nodes.size()];
 
-            for (int i = 0; i < ids.length; ++i)
-                ids[i] = nodes.get(i).id().toString();
+                for (int i = 0; i < ids.length; ++i)
+                    ids[i] = nodes.get(i).id().toString();
 
-            List jsMapRes = (List)ctx.scripting().invokeFunctionByName("__compute",
-                mapFunc, ids, this.arg);
+                List jsMapRes = (List)ctx.scripting().invokeFunctionByName("__compute",
+                    mapFunc, ids, this.arg);
 
-            for (Object jobMapping : jsMapRes) {
-                List task = (List)jobMapping;
+                for (Object jobMapping : jsMapRes) {
+                    List task = (List)jobMapping;
 
-                final String func = (String)task.get(0);
-                final List argv = (List)task.get(1);
-                String nodeId = (String)task.get(2);
+                    final String func = (String)task.get(0);
+                    final List argv = (List)task.get(1);
+                    String nodeId = (String)task.get(2);
 
-                ClusterNode node = ctx.grid().cluster().node(UUID.fromString(nodeId));
+                    ClusterNode node = ctx.grid().cluster().node(UUID.fromString(nodeId));
 
-                map.put(new ComputeJobAdapter() {
-                    @IgniteInstanceResource
-                    private Ignite ignite;
+                    map.put(new ComputeJobAdapter() {
+                        @IgniteInstanceResource
+                        private Ignite ignite;
 
-                    @Override public Object execute() throws IgniteException {
-                        String[] argv1 = new String[argv.size()];
+                        @Override public Object execute() throws IgniteException {
+                            try {
+                                String[] argv1 = new String[argv.size()];
 
-                        for (int i = 0; i < argv1.length; ++i)
-                            argv1[i] = argv.get(i).toString();
+                                for (int i = 0; i < argv1.length; ++i)
+                                    argv1[i] = argv.get(i).toString();
 
-                        return ((IgniteKernal)ignite).context().scripting().invokeFunction(func,
argv1);
-                    }
-                }, node);
-            }
 
-            return map;
+                                return ((IgniteKernal)ignite).context().scripting().invokeFunction(func,
argv1);
+                            }
+                            catch (IgniteCheckedException e) {
+                               throw U.convertException(e);
+                            }
+                        }
+                    }, node);
+                }
+
+                return map;
+            }
+            catch (IgniteCheckedException e) {
+                throw U.convertException(e);
+            }
         }
 
         /** {@inheritDoc} */
-        @Nullable @Override public Object reduce(List<ComputeJobResult> results) {
-            String[] data = new String[results.size()];
+        @Nullable @Override public GridRestResponse reduce(List<ComputeJobResult> results)
{
+            try {
+                String[] data = new String[results.size()];
 
-            for (int i = 0; i < results.size(); ++i)
-                data[i] = results.get(i).getData().toString();
+                for (int i = 0; i < results.size(); ++i)
+                    data[i] = results.get(i).getData().toString();
 
-            return ctx.scripting().invokeFunction(reduceFunc, (Object)data);
+                return new GridRestResponse(ctx.scripting().invokeFunction(reduceFunc, (Object)data));
+            }
+            catch (IgniteCheckedException e) {
+                return new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage());
+            }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b7f2393e/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
index 9c8a94e..b6f29ed 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java
@@ -72,13 +72,14 @@ public class IgniteScriptProcessor extends GridProcessorAdapter {
      * Add function to scope.
      *
      * @param script Function script.
+     * @throws IgniteCheckedException If script failed.
      */
-    public void addEngineFunction(String script) {
+    public void addEngineFunction(String script)  throws IgniteCheckedException {
         try {
             jsEngine.eval(script);
         }
         catch (ScriptException e) {
-            throw new IgniteException("Script Engine does not work.", e);
+            throw new IgniteCheckedException("Script evaluation failed [script=" + script
+ "].", e);
         }
     }
 
@@ -86,8 +87,9 @@ public class IgniteScriptProcessor extends GridProcessorAdapter {
      * @param source Script source.
      * @param args Arguments.
      * @return Result of the function.
+     * @throws IgniteCheckedException If script failed.
      */
-    public Object invokeFunction(String source, Object... args) {
+    public Object invokeFunction(String source, Object... args) throws IgniteCheckedException
{
         Object[] newArgs = new Object[args.length + 1];
 
         newArgs[0] = source;
@@ -103,18 +105,19 @@ public class IgniteScriptProcessor extends GridProcessorAdapter {
      * @param nameFunc Function name.
      * @param args Function arguments.
      * @return Result of the function.
+     * @throws IgniteCheckedException If script failed.
      */
-    public Object invokeFunctionByName(String nameFunc, Object... args) {
+    public Object invokeFunctionByName(String nameFunc, Object... args) throws IgniteCheckedException
{
         Invocable invocable = (Invocable) jsEngine;
 
         try {
             return invocable.invokeFunction(nameFunc, args);
         }
         catch (ScriptException e) {
-            throw new IgniteException("Script Engine does not work.", e);
+            throw new IgniteCheckedException("Function evaluation failed [funcName=" + nameFunc
+ "].", e);
         }
         catch (NoSuchMethodException e) {
-            throw new IgniteException("Script Engine does not work.", e);
+            throw new IgniteCheckedException("Cannot find function [funcName=" + nameFunc
+ "].", e);
         }
     }
 }


Mime
View raw message