You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by pd...@apache.org on 2019/04/16 20:52:58 UTC

[incubator-openwhisk-devtools] branch master updated: Make knative init/run logic more efficient based upon type of request (body) data (#248)

This is an automated email from the ASF dual-hosted git repository.

pdesai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-devtools.git


The following commit(s) were added to refs/heads/master by this push:
     new f9f66e2  Make knative init/run logic more efficient based upon type of request (body) data (#248)
f9f66e2 is described below

commit f9f66e251d4e80e6629bb3463f43126b6d32c999
Author: Matt Rutkowski <mr...@us.ibm.com>
AuthorDate: Tue Apr 16 15:52:53 2019 -0500

    Make knative init/run logic more efficient based upon type of request (body) data (#248)
    
    * Make knative init/run logic more efficient based upon type of request data
    
    * Make knative init/run logic more efficient based upon type of request data
---
 knative-build/runtimes/javascript/app.js           |  6 +-
 .../runtimes/javascript/platform/knative.js        | 92 ++++++++++++----------
 ...ta-init-run.json => knative-data-init-run.json} |  0
 .../{data-init.json => knative-data-init.json}     |  5 +-
 .../{data-run.json => knative-data-run.json}       | 10 ++-
 .../{data-init.json => openwhisk-data-init.json}   |  0
 .../{data-run.json => openwhisk-data-run.json}     |  0
 .../tests/webactionraw/payload-knative-init.http   | 14 ++++
 .../{data-run.json => payload-knative-run.http}    | 15 +++-
 9 files changed, 86 insertions(+), 56 deletions(-)

diff --git a/knative-build/runtimes/javascript/app.js b/knative-build/runtimes/javascript/app.js
index b4714d3..7658fe5 100644
--- a/knative-build/runtimes/javascript/app.js
+++ b/knative-build/runtimes/javascript/app.js
@@ -50,13 +50,13 @@ app.use(bodyParser.json({ limit: config.requestBodyLimit }));
 // identify the target Serverless platform
 const platformFactory = require('./platform/platform.js');
 const factory = new platformFactory(app, config, service);
-var targetPlatform = process.env.__OW_RUNTIME_PLATFORM;
+const targetPlatform = process.env.__OW_RUNTIME_PLATFORM;
 
 // default to "openwhisk" platform initialization if not defined
 // TODO export isvalid() from platform, if undefined this is OK to default, but if not valid value then error out
 if(typeof targetPlatform === "undefined") {
     targetPlatform = platformFactory.PLATFORM_OPENWHISK;
-    console.log("__OW_RUNTIME_PLATFORM is undefined; defaulting to 'openwhisk' ...");
+    // console.log("__OW_RUNTIME_PLATFORM is undefined; defaulting to 'openwhisk' ...");
 }
 
 if(!platformFactory.isSupportedPlatform(targetPlatform)){
@@ -70,7 +70,7 @@ if(!platformFactory.isSupportedPlatform(targetPlatform)){
  * to move data where the platform and function author expects it to be.
  */
 
-var platformImpl = factory.createPlatformImpl(targetPlatform);
+const platformImpl = factory.createPlatformImpl(targetPlatform);
 
 if(typeof platformImpl !== "undefined"){
 
diff --git a/knative-build/runtimes/javascript/platform/knative.js b/knative-build/runtimes/javascript/platform/knative.js
index 6cf8620..1fe3d2c 100644
--- a/knative-build/runtimes/javascript/platform/knative.js
+++ b/knative-build/runtimes/javascript/platform/knative.js
@@ -152,33 +152,7 @@ function preProcessInitData(env, initdata, valuedata, activationdata) {
     } catch(e){
         console.error(e);
         DEBUG.functionEndError(e.message);
-        throw("Unable to initialize the runtime: " + e.message);
-    }
-    DEBUG.functionEnd();
-}
-
-/**
- * Pre-process the incoming http request data, moving it to where the
- * route handlers expect it to be for an openwhisk runtime.
- */
-function preProcessActivationData(env, activationdata) {
-    DEBUG.functionStart();
-    try {
-        // Note: we move the values here so that the "run()" handler does not have
-        // to move them again.
-        Object.keys(activationdata).forEach(
-            function (k) {
-                if (typeof activationdata[k] === 'string') {
-                    var envVariable = OW_ENV_PREFIX + k.toUpperCase();
-                    process.env[envVariable] = activationdata[k];
-                    DEBUG.dumpObject(process.env[envVariable], envVariable, "preProcessActivationData");
-                }
-            }
-        );
-    } catch(e){
-        console.error(e);
-        DEBUG.functionEndError(e.message);
-        throw("Unable to initialize the runtime: " + e.message);
+        throw("Unable to process Initialization data: " + e.message);
     }
     DEBUG.functionEnd();
 }
@@ -218,6 +192,7 @@ function preProcessHTTPContext(req, valueData) {
                 // make value data available as __ow_body
                 const tmpBody = Object.assign({}, req.body.value);
                 // delete main, binary, raw, and code from the body before sending it as an action argument
+                removeInitData(tmpBody);
                 delete tmpBody.main;
                 delete tmpBody.code;
                 delete tmpBody.binary;
@@ -240,11 +215,36 @@ function preProcessHTTPContext(req, valueData) {
     } catch (e) {
         console.error(e);
         DEBUG.functionEndError(e.message);
-        throw ("Unable to initialize the runtime: " + e.message)
+        throw ("Unable to process HTTP Context: " + e.message)
     }
     DEBUG.functionEnd()
 }
 
+/**
+ * Pre-process the incoming http request data, moving it to where the
+ * route handlers expect it to be for an openwhisk runtime.
+ */
+function preProcessActivationData(env, activationdata) {
+    DEBUG.functionStart();
+    try {
+        // Note: we move the values here so that the "run()" handler does not have
+        // to move them again.
+        Object.keys(activationdata).forEach(
+            function (k) {
+                if (typeof activationdata[k] === 'string') {
+                    var envVariable = OW_ENV_PREFIX + k.toUpperCase();
+                    process.env[envVariable] = activationdata[k];
+                    DEBUG.dumpObject(process.env[envVariable], envVariable, "preProcessActivationData");
+                }
+            }
+        );
+    } catch(e){
+        console.error(e);
+        DEBUG.functionEndError(e.message);
+        throw("Unable to process Activation data: " + e.message);
+    }
+    DEBUG.functionEnd();
+}
 
 /**
  * Pre-process the incoming http request data, moving it to where the
@@ -253,13 +253,27 @@ function preProcessHTTPContext(req, valueData) {
 function preProcessRequest(req){
     DEBUG.functionStart();
     try {
+        let env = process.env || {};
+
         // Get or create valid references to the various data we might encounter
         // in a request such as Init., Activation and function parameter data.
         let body = req.body || {};
         let valueData = body.value || {};
         let initData = body.init || {};
         let activationData = body.activation || {};
-        let env = process.env || {};
+
+        // process initialization (i.e., "init") data
+        if (hasInitData(req)) {
+            preProcessInitData(env, initData, valueData, activationData);
+        }
+
+        if( hasActivationData(req)) {
+            // process HTTP request header and body to make it available to function as parameter data
+            preProcessHTTPContext(req, valueData);
+
+            // process per-activation (i.e, "run") data
+            preProcessActivationData(env, activationData);
+        }
 
         // Fix up pointers in case we had to allocate new maps
         req.body = body;
@@ -267,20 +281,11 @@ function preProcessRequest(req){
         req.body.init = initData;
         req.body.activation = activationData;
 
-        // process initialization (i.e., "init") data
-        preProcessInitData(env, initData, valueData, activationData);
-
-        // process HTTP request header and body to make it available to function as parameter data
-        preProcessHTTPContext(req, valueData);
-
-        // process per-activation (i.e, "run") data
-        preProcessActivationData(env, activationData);
-
     } catch(e){
         console.error(e);
         DEBUG.functionEndError(e.message);
         // TODO: test this error is handled properly and results in an HTTP error response
-        throw("Unable to initialize the runtime: " + e.message);
+        throw("Unable to process request data: " + e.message);
     }
     DEBUG.functionEnd();
 }
@@ -414,10 +419,11 @@ function PlatformKnativeImpl(platformFactory) {
                 // Process request and process env. variables to provide them in the manner
                 // an OpenWhisk Action expects them, as well as enable additional Http features.
                 preProcessRequest(req);
-
+                // Invoke the OW "init" entrypoint
                 service.initCode(req).then(function () {
                     // delete any INIT data (e.g., code, raw, etc.) from the 'value' data before calling run().
                     removeInitData(req.body);
+                    // Invoke the OW "run" entrypoint
                     service.runCode(req).then(function (result) {
                         postProcessResponse(req, result, res)
                     });
@@ -435,7 +441,7 @@ function PlatformKnativeImpl(platformFactory) {
                 // Process request and process env. variables to provide them in the manner
                 // an OpenWhisk Action expects them, as well as enable additional Http features.
                 preProcessRequest(req);
-
+                // Invoke the OW "init" entrypoint
                 service.initCode(req).then(function (result) {
                     res.status(result.code).send(result.response);
                 }).catch(function (error) {
@@ -451,7 +457,7 @@ function PlatformKnativeImpl(platformFactory) {
                 // Process request and process env. variables to provide them in the manner
                 // an OpenWhisk Action expects them, as well as enable additional Http features.
                 preProcessRequest(req);
-
+                // Invoke the OW "run" entrypoint
                 service.runCode(req).then(function (result) {
                     postProcessResponse(req, result, res)
                 }).catch(function (error) {
@@ -466,7 +472,7 @@ function PlatformKnativeImpl(platformFactory) {
             }
 
         } catch (e) {
-            res.status(500).json({error: "internal error during function initialization."})
+            res.status(500).json({error: "internal error during request processing."})
         }
     };
 
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-init-run.json b/knative-build/runtimes/javascript/tests/webactionraw/knative-data-init-run.json
similarity index 100%
rename from knative-build/runtimes/javascript/tests/webactionraw/data-init-run.json
rename to knative-build/runtimes/javascript/tests/webactionraw/knative-data-init-run.json
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-init.json b/knative-build/runtimes/javascript/tests/webactionraw/knative-data-init.json
similarity index 78%
copy from knative-build/runtimes/javascript/tests/webactionraw/data-init.json
copy to knative-build/runtimes/javascript/tests/webactionraw/knative-data-init.json
index 09fcbd6..0c8686b 100644
--- a/knative-build/runtimes/javascript/tests/webactionraw/data-init.json
+++ b/knative-build/runtimes/javascript/tests/webactionraw/knative-data-init.json
@@ -1,8 +1,9 @@
 {
-    "value": {
+    "init": {
         "name" : "nodejs-web-action-raw",
         "main" : "main",
         "binary": false,
-        "code" : "function main(params) { return { response: params }; }"
+        "code" : "function main(params) { return { response: params }; }",
+        "raw": true
     }
 }
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-run.json b/knative-build/runtimes/javascript/tests/webactionraw/knative-data-run.json
similarity index 73%
copy from knative-build/runtimes/javascript/tests/webactionraw/data-run.json
copy to knative-build/runtimes/javascript/tests/webactionraw/knative-data-run.json
index f020547..64d28fb 100644
--- a/knative-build/runtimes/javascript/tests/webactionraw/data-run.json
+++ b/knative-build/runtimes/javascript/tests/webactionraw/knative-data-run.json
@@ -1,11 +1,13 @@
-{ 
-    "value": {
-        "name": "Joe"
-    },
+{
+  "activation": {
     "namespace": "default",
     "action_name": "nodejs-web-action-raw",
     "api_host": "",
     "api_key": "",
     "activation_id": "",
     "deadline": "4102498800000"
+  },
+  "value": {
+    "name": "Joe"
+  }
 }
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-init.json b/knative-build/runtimes/javascript/tests/webactionraw/openwhisk-data-init.json
similarity index 100%
rename from knative-build/runtimes/javascript/tests/webactionraw/data-init.json
rename to knative-build/runtimes/javascript/tests/webactionraw/openwhisk-data-init.json
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-run.json b/knative-build/runtimes/javascript/tests/webactionraw/openwhisk-data-run.json
similarity index 100%
copy from knative-build/runtimes/javascript/tests/webactionraw/data-run.json
copy to knative-build/runtimes/javascript/tests/webactionraw/openwhisk-data-run.json
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-init.http b/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-init.http
new file mode 100644
index 0000000..bc9c6a0
--- /dev/null
+++ b/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-init.http
@@ -0,0 +1,14 @@
+POST http://localhost:8080/ HTTP/1.1
+content-type: application/json
+
+{
+  "init": {
+    "name" : "nodejs-web-action-raw",
+    "main" : "main",
+    "binary": false,
+    "code" : "function main(params) { return { response: params }; }",
+    "raw": true
+  }
+}
+
+###
diff --git a/knative-build/runtimes/javascript/tests/webactionraw/data-run.json b/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-run.http
similarity index 55%
rename from knative-build/runtimes/javascript/tests/webactionraw/data-run.json
rename to knative-build/runtimes/javascript/tests/webactionraw/payload-knative-run.http
index f020547..5aa06f8 100644
--- a/knative-build/runtimes/javascript/tests/webactionraw/data-run.json
+++ b/knative-build/runtimes/javascript/tests/webactionraw/payload-knative-run.http
@@ -1,11 +1,18 @@
-{ 
-    "value": {
-        "name": "Joe"
-    },
+POST http://localhost:8080/ HTTP/1.1
+content-type: application/json
+
+{
+  "activation": {
     "namespace": "default",
     "action_name": "nodejs-web-action-raw",
     "api_host": "",
     "api_key": "",
     "activation_id": "",
     "deadline": "4102498800000"
+  },
+  "value": {
+    "name": "Joe"
+  }
 }
+
+###