#ignite-964: add affinityRun
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/06d3a296 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/06d3a296 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/06d3a296 Branch: refs/heads/ignite-964 Commit: 06d3a2969c09fd614fe4ccd5aad0081970c4dc3a Parents: d4c223c Author: ivasilinets <ivasilin...@gridgain.com> Authored: Fri Jul 10 11:52:20 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Fri Jul 10 11:52:20 2015 +0300 ---------------------------------------------------------------------- .../processors/rest/GridRestCommand.java | 3 ++ .../IgniteScriptingCommandHandler.java | 53 +++++++++++++++++++- .../rest/request/RestRunScriptRequest.java | 34 +++++++++++++ modules/nodejs/src/main/js/compute.js | 17 ++++++- .../ignite/internal/NodeJsComputeSelfTest.java | 7 +++ modules/nodejs/src/test/js/test-compute.js | 35 +++++++++++++ .../http/jetty/GridJettyRestHandler.java | 17 +++++++ 7 files changed, 163 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/06d3a296/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java index 00eb746..45e86e0 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestCommand.java @@ -138,6 +138,9 @@ public enum GridRestCommand { /** Run script. */ RUN_SCRIPT("runscript"), + /** Affinity run script. */ + AFFINITY_RUN_SCRIPT("affrun"), + /** Execute map reduce script. */ EXECUTE_MAP_REDUCE_SCRIPT("excmapreduce"), http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/06d3a296/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 f2ddd59..d3f26da 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 @@ -44,7 +44,8 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter /** Supported commands. */ private static final Collection<GridRestCommand> SUPPORTED_COMMANDS = U.sealList( EXECUTE_MAP_REDUCE_SCRIPT, - RUN_SCRIPT); + RUN_SCRIPT, + AFFINITY_RUN_SCRIPT); /** Emit result. */ private IgniteJsEmitResult emitRes; @@ -95,7 +96,13 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter assert req instanceof RestRunScriptRequest : "Invalid type of run script request."; return ctx.closure().callLocalSafe( - new RunScriptCallable(ctx, (RestRunScriptRequest) req), false); + new RunScriptCallable(ctx, (RestRunScriptRequest)req), false); + } + + case AFFINITY_RUN_SCRIPT: { + assert req instanceof RestRunScriptRequest : "Invalid type of run script request."; + + return ctx.closure().callLocalSafe(new AffinityRunScriptCallable(ctx, (RestRunScriptRequest)req)); } case EXECUTE_MAP_REDUCE_SCRIPT: { @@ -300,6 +307,48 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter } /** + * Affinity run script callable. + */ + private static class AffinityRunScriptCallable implements IgniteCallable<GridRestResponse> { + /** */ + private static final long serialVersionUID = 0L; + + /** Kernal context. */ + private GridKernalContext ctx; + + /** Run script request. */ + private RestRunScriptRequest req; + + /** Cache name. */ + private String cacheName; + + /** Key. */ + private Object key; + + /** + * @param ctx Kernal context. + * @param req Run script request. + */ + public AffinityRunScriptCallable(GridKernalContext ctx, RestRunScriptRequest req) { + this.cacheName = req.cacheName(); + this.key = req.affinityKey(); + this.ctx = ctx; + this.req = req; + } + + /** {@inheritDoc} */ + @Override public GridRestResponse call() throws Exception { + try { + return new GridRestResponse(ctx.grid().compute().affinityCall(cacheName, key, + new JsFunctionCallable(req.script(), req.argument()))); + } + catch (Exception e) { + return new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage()); + } + } + } + + /** * Map reduce callable. */ private static class MapReduceCallable implements Callable<GridRestResponse> { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/06d3a296/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/request/RestRunScriptRequest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/request/RestRunScriptRequest.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/request/RestRunScriptRequest.java index cf74802..416fbf9 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/request/RestRunScriptRequest.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/request/RestRunScriptRequest.java @@ -27,6 +27,12 @@ public class RestRunScriptRequest extends GridRestRequest { /** Function arguments. */ private Object arg; + /** Key for affinity run. */ + private Object key; + + /** Cache name for affinity run. */ + private String cacheName; + /** * @return Java script function. */ @@ -54,4 +60,32 @@ public class RestRunScriptRequest extends GridRestRequest { public void argument(Object arg) { this.arg = arg; } + + /** + * @return Key for affinity run. + */ + public Object affinityKey() { + return key; + } + + /** + * @param key Key for affinity run. + */ + public void affinityKey(Object key) { + this.key = key; + } + + /** + * @return Cache name for affinity run. + */ + public String cacheName() { + return cacheName; + } + + /** + * @param cacheName Cache name for affinity run. + */ + public void cacheName(String cacheName) { + this.cacheName = cacheName; + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/06d3a296/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 16de9e4..5c28418 100644 --- a/modules/nodejs/src/main/js/compute.js +++ b/modules/nodejs/src/main/js/compute.js @@ -35,7 +35,22 @@ function Compute(server) { */ Compute.prototype.run = function(job, args, callback) { this._server.runCommand(new Command("runscript").addParam("func", job). - setPostData(JSON.stringify({"arg" : args})), callback); + setPostData(JSON.stringify({"arg" : args})), callback); +} + +/** + * Executes given job on the node where data for provided affinity key is located. + * + * @this {Compute} + * @param {string} cacheName Cache name + * @param {string|number|JSONObject} key Key. + * @param job Function + * @param args Function arguments + * @param {onGet} callback Callback + */ +Compute.prototype.affinityRun = function(cacheName, key, job, args, callback) { + this._server.runCommand(new Command("affrun").addParam("func", job).addParam("cacheName", cacheName). + setPostData(JSON.stringify({"arg" : args, "key" : key})), callback); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/06d3a296/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java index 48b2855..1f8c3de 100644 --- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java +++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsComputeSelfTest.java @@ -156,6 +156,13 @@ public class NodeJsComputeSelfTest extends NodeJsAbstractTest { /** * @throws Exception If failed. */ + public void testComputeAffinityRunScriptContainsKey() throws Exception { + runJsScript("testComputeAffinityRunScriptContainsKey"); + } + + /** + * @throws Exception If failed. + */ public void _testRestartGrid() throws Exception { final AtomicInteger id = new AtomicInteger(2); IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/06d3a296/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 5d865ce..5cdc3e0 100644 --- a/modules/nodejs/src/test/js/test-compute.js +++ b/modules/nodejs/src/test/js/test-compute.js @@ -77,6 +77,41 @@ testComputeRunScriptContainsKey = function() { TestUtils.startIgniteNode(computeRunScriptContainsKey); } +testComputeAffinityRunScriptContainsKey = function() { + function computeRunScriptContainsKey(error, ignite) { + assert(error == null, "Error on start:" + error); + + var comp = ignite.compute(); + + var f = function(key) { + var cache = ignite.cache("mycache"); + cache.put(key, "[AAAAAAA]"); + + if (!cache.containsKey(key)) + throw "Contains key does not work." + + return key; + } + + function onEnd(err, res) { + assert(err == null, err); + assert(TestUtils.compareObject(initKey, res), "Incorrect result after script.") + + ignite.cache("mycache").containsKey(initKey, function(err0, res0) { + assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); + assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); + TestUtils.testDone(); + }); + } + + var initKey = {"1" : ["1", "2"]}; + + comp.affinityRun("mycache", initKey, f, initKey, onEnd.bind(null)); + } + + TestUtils.startIgniteNode(computeRunScriptContainsKey); +} + testComputeRunScriptContainsKeys = function() { function computeRunScriptContainsKey(error, ignite) { assert(error == null, "Error on start:" + error); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/06d3a296/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java ---------------------------------------------------------------------- diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java index d601c17..cad7cc2 100644 --- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java +++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java @@ -584,6 +584,23 @@ public class GridJettyRestHandler extends AbstractHandler { break; } + case AFFINITY_RUN_SCRIPT: { + RestRunScriptRequest restReq0 = new RestRunScriptRequest(); + + restReq0.script((String)params.get("func")); + restReq0.cacheName((String)params.get("cacheName")); + + JSONObject o = parseRequest(req); + restReq0.argument(o.get("arg")); + + Object cacheObj = JSONCacheObject.toSimpleObject(o.get("key")); + restReq0.affinityKey(cacheObj); + + restReq = restReq0; + + break; + } + case EXECUTE_MAP_REDUCE_SCRIPT: { RestMapReduceScriptRequest restReq0 = new RestMapReduceScriptRequest();