You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bc...@apache.org on 2018/07/31 21:15:45 UTC

[trafficserver] branch 8.0.x updated (f17e8c9 -> 85189b2)

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

bcall pushed a change to branch 8.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git.


    from f17e8c9  Removes duplicated validity check
     new 0047d30  Fix bug on loading of lua script
     new 3913cf5  Add support for reloading the lua script in global plugin mode
     new 85189b2  Add flag to enable the reload feature. Disable by default

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 plugins/lua/ts_lua.c        | 35 +++++++++++++++++++++---
 plugins/lua/ts_lua_common.h |  2 ++
 plugins/lua/ts_lua_util.c   | 66 +++++++++++++++++++++++++++++++++++++++++++++
 plugins/lua/ts_lua_util.h   |  2 +-
 4 files changed, 101 insertions(+), 4 deletions(-)


[trafficserver] 03/03: Add flag to enable the reload feature. Disable by default

Posted by bc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bcall pushed a commit to branch 8.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 85189b229ad04be27857f0105b2b346b3e40e080
Author: Kit Chan <ki...@apache.org>
AuthorDate: Wed Jul 11 00:07:20 2018 -0700

    Add flag to enable the reload feature. Disable by default
    
    (cherry picked from commit 3192bce394b95de37f231a8c472c78cf8313e57a)
---
 plugins/lua/ts_lua.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/plugins/lua/ts_lua.c b/plugins/lua/ts_lua.c
index ea73143..0d8af2c 100644
--- a/plugins/lua/ts_lua.c
+++ b/plugins/lua/ts_lua.c
@@ -457,8 +457,10 @@ TSPluginInit(int argc, const char *argv[])
   }
 
   int states                           = TS_LUA_MAX_STATE_COUNT;
+  int reload                           = 0;
   static const struct option longopt[] = {
     {"states", required_argument, 0, 's'},
+    {"enable-reload", no_argument, 0, 'r'},
     {0, 0, 0, 0},
   };
 
@@ -471,6 +473,10 @@ TSPluginInit(int argc, const char *argv[])
       states = atoi(optarg);
       // set state
       break;
+    case 'r':
+      reload = 1;
+      TSDebug(TS_LUA_DEBUG_TAG, "[%s] enable global plugin reload [%d]", __FUNCTION__, reload);
+      break;
     }
 
     if (opt == -1) {
@@ -607,12 +613,15 @@ TSPluginInit(int argc, const char *argv[])
 
   ts_lua_destroy_http_ctx(http_ctx);
 
-  TSCont config_contp = TSContCreate(configHandler, NULL);
-  if (!config_contp) {
-    TSError("[ts_lua][%s] could not create configuration continuation", __FUNCTION__);
-    return;
-  }
-  TSContDataSet(config_contp, conf);
+  // support for reload as global plugin
+  if (reload) {
+    TSCont config_contp = TSContCreate(configHandler, NULL);
+    if (!config_contp) {
+      TSError("[ts_lua][%s] could not create configuration continuation", __FUNCTION__);
+      return;
+    }
+    TSContDataSet(config_contp, conf);
 
-  TSMgmtUpdateRegister(config_contp, "ts_lua");
+    TSMgmtUpdateRegister(config_contp, "ts_lua");
+  }
 }


[trafficserver] 01/03: Fix bug on loading of lua script

Posted by bc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bcall pushed a commit to branch 8.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 0047d30644613416914d104b6a9f90543cd44799
Author: Kit Chan <ki...@apache.org>
AuthorDate: Tue Jul 10 00:13:46 2018 -0700

    Fix bug on loading of lua script
    
    (cherry picked from commit 3c0c413321d8fa59cbb4cc8f9e5ddbd9d5b04e98)
---
 plugins/lua/ts_lua.c        | 8 +++++---
 plugins/lua/ts_lua_common.h | 2 ++
 plugins/lua/ts_lua_util.c   | 3 +++
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/plugins/lua/ts_lua.c b/plugins/lua/ts_lua.c
index bd8cec2..cbfc782 100644
--- a/plugins/lua/ts_lua.c
+++ b/plugins/lua/ts_lua.c
@@ -133,8 +133,9 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int errbuf_s
     }
 
     memset(conf, 0, sizeof(ts_lua_instance_conf));
-    conf->states = states;
-    conf->remap  = 1;
+    conf->states    = states;
+    conf->remap     = 1;
+    conf->init_func = 0;
 
     if (fn) {
       snprintf(conf->script, TS_LUA_MAX_SCRIPT_FNAME_LENGTH, "%s", argv[optind]);
@@ -150,7 +151,8 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int errbuf_s
       return TS_ERROR;
     }
 
-    if (fn) {
+    // register the script only if it is from a file and has no __init__ function
+    if (fn && !conf->init_func) {
       // we only need to register the script for the first lua VM
       ts_lua_script_register(ts_lua_main_ctx_array[0].lua, conf->script, conf);
     }
diff --git a/plugins/lua/ts_lua_common.h b/plugins/lua/ts_lua_common.h
index e355e03..0115f0e 100644
--- a/plugins/lua/ts_lua_common.h
+++ b/plugins/lua/ts_lua_common.h
@@ -96,6 +96,8 @@ typedef struct {
 
   int remap;
   int states;
+
+  int init_func;
 } ts_lua_instance_conf;
 
 /* lua state for http request */
diff --git a/plugins/lua/ts_lua_util.c b/plugins/lua/ts_lua_util.c
index cb7d8c0..9455c1b 100644
--- a/plugins/lua/ts_lua_util.c
+++ b/plugins/lua/ts_lua_util.c
@@ -227,6 +227,9 @@ ts_lua_add_module(ts_lua_instance_conf *conf, ts_lua_main_ctx *arr, int n, int a
     lua_getglobal(L, "__init__");
 
     if (lua_type(L, -1) == LUA_TFUNCTION) {
+      // specifying that the file has an __init__ function
+      conf->init_func = 1;
+
       lua_newtable(L);
 
       for (t = 0; t < argc; t++) {


[trafficserver] 02/03: Add support for reloading the lua script in global plugin mode

Posted by bc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bcall pushed a commit to branch 8.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 3913cf54f459e2101ecc7644133058bc81317663
Author: Kit Chan <ki...@apache.org>
AuthorDate: Sun Jul 8 21:43:42 2018 -0700

    Add support for reloading the lua script in global plugin mode
    
    (cherry picked from commit e6147753cd65c3edd32b365e09b4d65edcffdd01)
---
 plugins/lua/ts_lua.c      | 18 ++++++++++++++
 plugins/lua/ts_lua_util.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 plugins/lua/ts_lua_util.h |  2 +-
 3 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/plugins/lua/ts_lua.c b/plugins/lua/ts_lua.c
index cbfc782..ea73143 100644
--- a/plugins/lua/ts_lua.c
+++ b/plugins/lua/ts_lua.c
@@ -265,6 +265,15 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
 }
 
 static int
+configHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata ATS_UNUSED)
+{
+  TSDebug(TS_LUA_DEBUG_TAG, "[%s] calling configuration handler", __FUNCTION__);
+  ts_lua_instance_conf *conf = (ts_lua_instance_conf *)TSContDataGet(contp);
+  ts_lua_reload_module(conf, ts_lua_g_main_ctx_array, conf->states);
+  return 0;
+}
+
+static int
 globalHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata)
 {
   TSHttpTxn txnp = (TSHttpTxn)edata;
@@ -597,4 +606,13 @@ TSPluginInit(int argc, const char *argv[])
   lua_pop(l, 1);
 
   ts_lua_destroy_http_ctx(http_ctx);
+
+  TSCont config_contp = TSContCreate(configHandler, NULL);
+  if (!config_contp) {
+    TSError("[ts_lua][%s] could not create configuration continuation", __FUNCTION__);
+    return;
+  }
+  TSContDataSet(config_contp, conf);
+
+  TSMgmtUpdateRegister(config_contp, "ts_lua");
 }
diff --git a/plugins/lua/ts_lua_util.c b/plugins/lua/ts_lua_util.c
index 9455c1b..b72b076 100644
--- a/plugins/lua/ts_lua_util.c
+++ b/plugins/lua/ts_lua_util.c
@@ -315,6 +315,69 @@ ts_lua_del_module(ts_lua_instance_conf *conf, ts_lua_main_ctx *arr, int n)
 }
 
 int
+ts_lua_reload_module(ts_lua_instance_conf *conf, ts_lua_main_ctx *arr, int n)
+{
+  int i;
+  lua_State *L;
+
+  for (i = 0; i < n; i++) {
+    TSMutexLock(arr[i].mutexp);
+
+    L = arr[i].lua;
+
+    /* call "__reload__", to clean resources if necessary */
+    lua_pushlightuserdata(L, conf);
+    lua_rawget(L, LUA_REGISTRYINDEX);
+    lua_replace(L, LUA_GLOBALSINDEX); /* L[GLOBAL] = L[REG][conf] */
+
+    lua_getglobal(L, "__reload__"); /* get __clean__ function */
+
+    if (lua_type(L, -1) == LUA_TFUNCTION) {
+      if (lua_pcall(L, 0, 0, 0)) {
+        TSError("[ts_lua][%s] lua_pcall %s failed: %s", __FUNCTION__, conf->script, lua_tostring(L, -1));
+      }
+    } else {
+      lua_pop(L, 1); /* pop nil */
+    }
+
+    // new global context
+    lua_newtable(L);                                /* new TB1 */
+    lua_pushvalue(L, -1);                           /* new TB2 */
+    lua_setfield(L, -2, "_G");                      /* TB1[_G] = TB2 empty table, we can change _G to xx */
+    lua_newtable(L);                                /* new TB3 */
+    lua_rawgeti(L, LUA_REGISTRYINDEX, arr[i].gref); /* push L[GLOBAL] */
+    lua_setfield(L, -2, "__index");                 /* TB3[__index] = L[GLOBAL] which has ts.xxx api */
+    lua_setmetatable(L, -2);                        /* TB1[META]  = TB3 */
+    lua_replace(L, LUA_GLOBALSINDEX);               /* L[GLOBAL] = TB1 */
+
+    ts_lua_set_instance_conf(L, conf);
+
+    if (strlen(conf->script)) {
+      if (luaL_loadfile(L, conf->script)) {
+        TSError("[ts_lua][%s] luaL_loadfile %s failed: %s", __FUNCTION__, conf->script, lua_tostring(L, -1));
+      } else {
+        if (lua_pcall(L, 0, 0, 0)) {
+          TSError("[ts_lua][%s] lua_pcall %s failed: %s", __FUNCTION__, conf->script, lua_tostring(L, -1));
+        }
+      }
+    }
+
+    lua_pushlightuserdata(L, conf);
+    lua_pushvalue(L, LUA_GLOBALSINDEX);
+    lua_rawset(L, LUA_REGISTRYINDEX); /* L[REG][conf] = L[GLOBAL] */
+
+    lua_newtable(L);
+    lua_replace(L, LUA_GLOBALSINDEX); /* L[GLOBAL] = EMPTY */
+
+    lua_gc(L, LUA_GCCOLLECT, 0);
+
+    TSMutexUnlock(arr[i].mutexp);
+  }
+
+  return 0;
+}
+
+int
 ts_lua_init_instance(ts_lua_instance_conf *conf ATS_UNUSED)
 {
   return 0;
diff --git a/plugins/lua/ts_lua_util.h b/plugins/lua/ts_lua_util.h
index e0025ef..d75db9f 100644
--- a/plugins/lua/ts_lua_util.h
+++ b/plugins/lua/ts_lua_util.h
@@ -28,8 +28,8 @@ void ts_lua_script_register(lua_State *L, char *script, ts_lua_instance_conf *co
 
 int ts_lua_add_module(ts_lua_instance_conf *conf, ts_lua_main_ctx *arr, int n, int argc, char *argv[], char *errbuf,
                       int errbuf_len);
-
 int ts_lua_del_module(ts_lua_instance_conf *conf, ts_lua_main_ctx *arr, int n);
+int ts_lua_reload_module(ts_lua_instance_conf *conf, ts_lua_main_ctx *arr, int n);
 
 int ts_lua_init_instance(ts_lua_instance_conf *conf);
 int ts_lua_del_instance(ts_lua_instance_conf *conf);