apache/openwhisk

Add test to BasicActionRunnerTests to confirm runtime supports global state

开放

#4,215 创建于 2019年1月14日

 (9 条评论) (0 个反应) (0 位负责人)Scala (1,177 个派生)batch import
good first issuehelp wantedneeds tests

仓库指标

星标
 (6,777 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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

贡献者指南