You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by dg...@apache.org on 2018/08/22 17:39:32 UTC

[incubator-openwhisk-runtime-ballerina] branch master updated: Move initilization of global memory to the init handler. (#8)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 40305e9  Move initilization of global memory to the init handler. (#8)
40305e9 is described below

commit 40305e9e385d72f85fc0d40f429cd7a1f8e44f3c
Author: rodric rabbah <ro...@gmail.com>
AuthorDate: Wed Aug 22 13:39:29 2018 -0400

    Move initilization of global memory to the init handler. (#8)
    
    * Move init program to the init handler. This preserves the global memory area across invocations for hot invokes.
    * Allow ballerina.home to be set externally.
---
 .gitignore                                           |  6 +++---
 ballerina/.dockerignore                              |  3 +++
 .../openwhisk/runtime/BallerinaProxy.java            | 20 +++++++++++++++-----
 .../ballerinalang/openwhisk/runtime/Constants.java   |  8 ++++----
 tests/src/test/resources/hello/hello.bal             |  3 +--
 .../BallerinaActionContainerTests.scala              | 10 +++++-----
 6 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/.gitignore b/.gitignore
index cc37378..fe312df 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,10 +1,10 @@
 .vscode
 .idea
-.iml
-.balx
-.ballerina
+*.iml
+*.balx
 *~
 
+.ballerina
 ballerina-internal.log
 
 .gradle/
diff --git a/ballerina/.dockerignore b/ballerina/.dockerignore
new file mode 100644
index 0000000..ce47343
--- /dev/null
+++ b/ballerina/.dockerignore
@@ -0,0 +1,3 @@
+*~
+proxy/build
+proxy/out
diff --git a/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/BallerinaProxy.java b/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/BallerinaProxy.java
index 1734ad0..7579b60 100644
--- a/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/BallerinaProxy.java
+++ b/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/BallerinaProxy.java
@@ -92,11 +92,21 @@ import javax.ws.rs.core.Response;
 
                 java.nio.file.Path destinationPath = BalxLoader.saveBase64EncodedFile(balxIs);
 
-                programFile = BLangProgramLoader.read(destinationPath);
-
-                return buildResponse(Response.Status.OK, Constants.RESPONSE_SUCCESS, Constants.INIT_SUCCESS);
+                try {
+                    programFile = BLangProgramLoader.read(destinationPath);
+                    // initProgramFile initialized the Global Memory area for the function
+                    // and doing this once in the init handler permits reuse of global variables
+                    // across invocations; to disable this, the initProgramFile call should be moved
+                    // to the run handler instead.
+                    programFile = BalxLoader.initProgramFile(programFile);
+                    return buildResponse(Response.Status.OK, Constants.RESPONSE_SUCCESS, Constants.INIT_SUCCESS);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    return buildRunResponse(Response.Status.BAD_REQUEST, Constants.RESPONSE_ERROR,
+                            Constants.FUNCTION_NOT_INITIALIZED);
+                }
             } else {
-                return buildResponse(Response.Status.INTERNAL_SERVER_ERROR, Constants.RESPONSE_ERROR,
+                return buildResponse(Response.Status.BAD_REQUEST, Constants.RESPONSE_ERROR,
                                      Constants.FAILED_TO_LOCATE_BINARY);
             }
         } catch (ProgramFileFormatException | BLangRuntimeException e) {
@@ -142,10 +152,10 @@ import javax.ws.rs.core.Response;
 
         //Invoking the program file
         try {
-            programFile = BalxLoader.initProgramFile(programFile);
             result = BLangFunctions
                     .invokeEntrypointCallable(programFile, programFile.getEntryPkgName(), mainFunction, parameters);
         } catch (Exception e) {
+            e.printStackTrace();
             return buildRunResponse(Response.Status.BAD_REQUEST, Constants.RESPONSE_ERROR,
                                     Constants.FUNCTION_RUN_FAILURE);
         }
diff --git a/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/Constants.java b/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/Constants.java
index aa929db..ba6e099 100644
--- a/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/Constants.java
+++ b/ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/Constants.java
@@ -34,14 +34,14 @@ class Constants {
     static final String RESPONSE_ERROR = "\"error\"";
     static final String RESPONSE_SUCCESS = "\"success\"";
 
-    static final String INIT_SUCCESS = "\"Function init success\"";
+    static final String INIT_SUCCESS = "\"Function initialized successfully.\"";
 
     static final String INIT_ONCE_ERROR = "\"Cannot initialize the action more than once.\"";
     static final String FAILED_TO_LOCATE_BINARY =
             "\"The action failed to generate or locate a binary. See logs for details.\"";
     static final String MISSING_MAIN_ERROR = "\"Missing main/no code to " + "execute.\"";
-    static final String FUNCTION_NOT_INITIALIZED = "\"Function not initialized\"";
-    static final String INVALID_INPUT_PARAMS = "\"Invalid input parameters for action run\"";
-    static final String FUNCTION_RUN_FAILURE = "\"Running Function failed\"";
+    static final String FUNCTION_NOT_INITIALIZED = "\"Function not initialized.\"";
+    static final String INVALID_INPUT_PARAMS = "\"Invalid input parameters for action run.\"";
+    static final String FUNCTION_RUN_FAILURE = "\"Function failed while running.\"";
     static final String DICTIONARY_RETURN_FAILURE = "\"The action did not return a dictionary.\"";
 }
diff --git a/tests/src/test/resources/hello/hello.bal b/tests/src/test/resources/hello/hello.bal
index 6328206..68b3790 100644
--- a/tests/src/test/resources/hello/hello.bal
+++ b/tests/src/test/resources/hello/hello.bal
@@ -5,7 +5,6 @@ function main(string... args) {
 }
 
 function run(json jsonInput) returns json {
-   io:println(jsonInput);
-   json output = { "response": "hello-world"};
+   json output = { "response": "hello-world" };
    return output;
 }
diff --git a/tests/src/test/scala/actionContainers/BallerinaActionContainerTests.scala b/tests/src/test/scala/actionContainers/BallerinaActionContainerTests.scala
index 9a3e391..61d2aec 100644
--- a/tests/src/test/scala/actionContainers/BallerinaActionContainerTests.scala
+++ b/tests/src/test/scala/actionContainers/BallerinaActionContainerTests.scala
@@ -73,8 +73,6 @@ class BallerinaActionContainerTests extends BasicActionRunnerTests with WskActor
     TestConfig(buildBal("return-response"))
   }
 
-  behavior of ballerinaContainerImageName
-
   it should "Initialize with the hello code and invoke" in {
     val (out, err) = withActionContainer() { c =>
       val sourceFile = buildBal("hello")
@@ -137,9 +135,11 @@ class BallerinaActionContainerTests extends BasicActionRunnerTests with WskActor
   }
 
   def buildBal(functionName: String): String = {
-    // Set Ballerina home path to resolve dependency libs
-    val ballerinaHome = Paths.get(System.getProperty("user.dir"), "..", "ballerina", "proxy", "build")
-    System.setProperty("ballerina.home", ballerinaHome.toString)
+    // Set Ballerina home path to resolve dependency libs if necessary
+    if (System.getProperty("ballerina.home") == null) {
+      val ballerinaHome = Paths.get(System.getProperty("user.dir"), "..", "ballerina", "proxy", "build")
+      System.setProperty("ballerina.home", ballerinaHome.toString)
+    }
 
     val path = getClass.getResource("/".concat(functionName)).getPath
     val context = new CompilerContext