apache/openwhisk
在 GitHub 查看Add test to BasicActionRunnerTests to confirm runtime supports global state
Open
#4,215 创建于 2019年1月14日
good first issuehelp wantedneeds tests
描述
Environment details:
- native ubuntu
Steps to reproduce the issue:
I've tested whether global variables could be reused when container was reused. The test results show that only nodejs runtime can reuse global scope variable.
The AWS Lambda service is not stateless for all runtimes. (it means an action can reuse global variable if container is reused) Is there any OW spec for a global variable?
1. Javascript
let globalCache = {};
function main(params) {
globalCache[getRandom()] = getRandom();
return globalCache;
}
function getRandom() {
return Math.random();
}
If the container is reused, the global variable is reused.
{
"0.25746017280185063": 0.7853652208888477
}
{
"0.25746017280185063": 0.7853652208888477,
"0.4376083800717476": 0.6122601078429637
}
2. PHP
<?php
print_r($GLOBALS);
if (!isset($GLOBALS['count'])) $GLOBALS['count'] = 0;
function main(array $args) : array
{
$GLOBALS['count'] = $GLOBALS['count'] + 1;
return ["count" => $GLOBALS['count']];
}
The global variable is not retained even if the container is reused. (Only returns 1)
{
"count": 1
}
3. Python
g = 0
def main(args):
try:
global g
g = g + 1
return {"count": g}
except Exception as e:
# please handle exception here
# you can return or hide an error message
return {"error": str(e)}
The global variable is not retained even if the container is reused. (Only returns 1)
If you run the same code in AWS Lambda, the count will increase.
{
"count": 1
}
4. Java
The static variable is not retained even if the container is reused. (counter variable is always 1)
class Global {
public static int counter = 0;
}
public class Application {
public static JsonObject main(JsonObject args) {
Global.counter++;
System.out.println(String.valueOf(Global.counter));
return null;
}
}
log
1