This is an automated email from the ASF dual-hosted git repository.
kichan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 74ee4eb speedup the script load process when multiple remap rules
load the same lua script file
74ee4eb is described below
commit 74ee4ebe8ef5080a275e6d63f0ad60972611dae1
Author: Kit Chan <[email protected]>
AuthorDate: Sat Oct 21 04:14:12 2017 -0700
speedup the script load process when multiple remap rules load the same lua
script file
---
plugins/experimental/ts_lua/ts_lua.c | 55 ++++++++++++++++-------
plugins/experimental/ts_lua/ts_lua_util.c | 75 +++++++++++++++++++++++++++++++
plugins/experimental/ts_lua/ts_lua_util.h | 3 ++
3 files changed, 116 insertions(+), 17 deletions(-)
diff --git a/plugins/experimental/ts_lua/ts_lua.c
b/plugins/experimental/ts_lua/ts_lua.c
index 554a327..b63ad3f 100644
--- a/plugins/experimental/ts_lua/ts_lua.c
+++ b/plugins/experimental/ts_lua/ts_lua.c
@@ -80,6 +80,7 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char
*errbuf, int errbuf_s
switch (opt) {
case 's':
states = atoi(optarg);
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] setting number of lua VM [%d]",
__FUNCTION__, states);
// set state
break;
}
@@ -109,29 +110,49 @@ TSRemapNewInstance(int argc, char *argv[], void **ih,
char *errbuf, int errbuf_s
return TS_ERROR;
}
- ts_lua_instance_conf *conf = TSmalloc(sizeof(ts_lua_instance_conf));
- if (!conf) {
- strncpy(errbuf, "[TSRemapNewInstance] TSmalloc failed!!", errbuf_size - 1);
- errbuf[errbuf_size - 1] = '\0';
- return TS_ERROR;
+ ts_lua_instance_conf *conf = NULL;
+
+ // check to make sure it is a lua file and there is no parameter for the lua
file
+ if (fn && (argc - optind < 2)) {
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] checking if script has been registered",
__FUNCTION__);
+ char script[TS_LUA_MAX_SCRIPT_FNAME_LENGTH];
+ snprintf(script, TS_LUA_MAX_SCRIPT_FNAME_LENGTH, "%s", argv[optind]);
+ // we only need to check the first lua VM for script registration
+ conf = ts_lua_script_registered(ts_lua_main_ctx_array[0].lua, script);
}
- memset(conf, 0, sizeof(ts_lua_instance_conf));
- conf->states = states;
- conf->remap = 1;
+ if (!conf) {
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] creating new conf instance", __FUNCTION__);
- if (fn) {
- snprintf(conf->script, TS_LUA_MAX_SCRIPT_FNAME_LENGTH, "%s", argv[optind]);
- } else {
- conf->content = argv[optind];
- }
+ conf = TSmalloc(sizeof(ts_lua_instance_conf));
+ if (!conf) {
+ strncpy(errbuf, "[TSRemapNewInstance] TSmalloc failed!!", errbuf_size -
1);
+ errbuf[errbuf_size - 1] = '\0';
+ return TS_ERROR;
+ }
- ts_lua_init_instance(conf);
+ memset(conf, 0, sizeof(ts_lua_instance_conf));
+ conf->states = states;
+ conf->remap = 1;
- ret = ts_lua_add_module(conf, ts_lua_main_ctx_array, conf->states, argc -
optind, &argv[optind], errbuf, errbuf_size);
+ if (fn) {
+ snprintf(conf->script, TS_LUA_MAX_SCRIPT_FNAME_LENGTH, "%s",
argv[optind]);
+ } else {
+ conf->content = argv[optind];
+ }
- if (ret != 0) {
- return TS_ERROR;
+ ts_lua_init_instance(conf);
+
+ ret = ts_lua_add_module(conf, ts_lua_main_ctx_array, conf->states, argc -
optind, &argv[optind], errbuf, errbuf_size);
+
+ if (ret != 0) {
+ return TS_ERROR;
+ }
+
+ if (fn) {
+ // 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);
+ }
}
*ih = conf;
diff --git a/plugins/experimental/ts_lua/ts_lua_util.c
b/plugins/experimental/ts_lua/ts_lua_util.c
index 159c86c..818d070 100644
--- a/plugins/experimental/ts_lua/ts_lua_util.c
+++ b/plugins/experimental/ts_lua/ts_lua_util.c
@@ -98,6 +98,81 @@ ts_lua_new_state()
return L;
}
+ts_lua_instance_conf *
+ts_lua_script_registered(lua_State *L, char *script)
+{
+ TSMgmtInt curr_time;
+ ts_lua_instance_conf *conf = NULL;
+
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] checking if script [%s] is registered",
__FUNCTION__, script);
+
+ // first check the reconfigure_time for the script. if it is not found, then
it is new
+ // if it matches the current reconfigure_time, then it is loaded already
+ // And we return the conf pointer of it. Otherwise it can be loaded again.
+ if (TS_SUCCESS == TSMgmtIntGet("proxy.node.config.reconfigure_time",
&curr_time)) {
+ lua_pushliteral(L, "__scriptTime");
+ lua_pushstring(L, script);
+ lua_concat(L, 2);
+ lua_rawget(L, LUA_REGISTRYINDEX);
+ if (lua_isnil(L, -1)) {
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] failed to get script time for [%s]",
__FUNCTION__, script);
+ lua_pop(L, -1);
+ return NULL;
+ } else {
+ int time = lua_tonumber(L, -1);
+ lua_pop(L, -1);
+
+ if (time == curr_time) {
+ lua_pushliteral(L, "__scriptPtr");
+ lua_pushstring(L, script);
+ lua_concat(L, 2);
+ lua_rawget(L, LUA_REGISTRYINDEX);
+ if (lua_isnil(L, -1)) {
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] failed to get script ptr for [%s]",
__FUNCTION__, script);
+ lua_pop(L, -1);
+ return NULL;
+ } else {
+ conf = lua_touserdata(L, -1);
+ lua_pop(L, -1);
+ return conf;
+ }
+ } else {
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] script time not matching for [%s]",
__FUNCTION__, script);
+ return NULL;
+ }
+ }
+
+ } else {
+ TSError("[ts_lua][%s] failed to get node's reconfigure time while checking
script registration", __FUNCTION__);
+ return NULL;
+ }
+}
+
+void
+ts_lua_script_register(lua_State *L, char *script, ts_lua_instance_conf *conf)
+{
+ TSMgmtInt time;
+
+ TSDebug(TS_LUA_DEBUG_TAG, "[%s] registering script [%s]", __FUNCTION__,
script);
+
+ // we recorded the script reconfigure_time and its conf pointer in registry
+ if (TS_SUCCESS == TSMgmtIntGet("proxy.node.config.reconfigure_time", &time))
{
+ lua_pushliteral(L, "__scriptTime");
+ lua_pushstring(L, script);
+ lua_concat(L, 2);
+ lua_pushnumber(L, time);
+ lua_rawset(L, LUA_REGISTRYINDEX);
+
+ lua_pushliteral(L, "__scriptPtr");
+ lua_pushstring(L, script);
+ lua_concat(L, 2);
+ lua_pushlightuserdata(L, conf);
+ lua_rawset(L, LUA_REGISTRYINDEX);
+ } else {
+ TSError("[ts_lua][%s] failed to get node's reconfigure time while
registering script", __FUNCTION__);
+ }
+}
+
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_size)
{
diff --git a/plugins/experimental/ts_lua/ts_lua_util.h
b/plugins/experimental/ts_lua/ts_lua_util.h
index f0f3347..3a5a7b3 100644
--- a/plugins/experimental/ts_lua/ts_lua_util.h
+++ b/plugins/experimental/ts_lua/ts_lua_util.h
@@ -24,6 +24,9 @@
int ts_lua_create_vm(ts_lua_main_ctx *arr, int n);
void ts_lua_destroy_vm(ts_lua_main_ctx *arr, int n);
+ts_lua_instance_conf *ts_lua_script_registered(lua_State *L, char *script);
+void ts_lua_script_register(lua_State *L, char *script, ts_lua_instance_conf
*conf);
+
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);
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].