You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by tw...@apache.org on 2023/06/06 10:47:29 UTC

[incubator-kvrocks] branch unstable updated: Remove global server ptr getter used in scripting (#1486)

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

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new c471fa53 Remove global server ptr getter used in scripting (#1486)
c471fa53 is described below

commit c471fa537d2fa67d6da9338dc93452900937d52d
Author: Twice <tw...@gmail.com>
AuthorDate: Tue Jun 6 18:47:22 2023 +0800

    Remove global server ptr getter used in scripting (#1486)
---
 src/main.cc              |  2 --
 src/server/server.cc     |  4 ++--
 src/server/server.h      |  2 --
 src/server/worker.cc     |  2 +-
 src/storage/scripting.cc | 14 +++++++++++---
 src/storage/scripting.h  |  2 +-
 6 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/main.cc b/src/main.cc
index 2ad8b040..e8689e4b 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -55,8 +55,6 @@ bool Symbolize(void *pc, char *out, size_t out_size);
 
 Server *srv = nullptr;
 
-Server *GetServer() { return srv; }
-
 extern "C" void SignalHandler(int sig) {
   if (srv && !srv->IsStopped()) {
     LOG(INFO) << "Bye Bye";
diff --git a/src/server/server.cc b/src/server/server.cc
index 0d3dcd60..21d299a0 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -91,7 +91,7 @@ Server::Server(engine::Storage *storage, Config *config)
   AdjustOpenFilesLimit();
   slow_log_.SetMaxEntries(config->slowlog_max_len);
   perf_log_.SetMaxEntries(config->profiling_sample_record_max_len);
-  lua_ = lua::CreateState();
+  lua_ = lua::CreateState(this);
 }
 
 Server::~Server() {
@@ -1539,7 +1539,7 @@ Status Server::ScriptSet(const std::string &sha, const std::string &body) const
 }
 
 void Server::ScriptReset() {
-  auto lua = lua_.exchange(lua::CreateState());
+  auto lua = lua_.exchange(lua::CreateState(this));
   lua::DestroyState(lua);
 }
 
diff --git a/src/server/server.h b/src/server/server.h
index 1f56d644..79f7d228 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -309,5 +309,3 @@ class Server {
   std::map<std::string, std::set<redis::Connection *>> watched_key_map_;
   std::shared_mutex watched_key_mutex_;
 };
-
-Server *GetServer();
diff --git a/src/server/worker.cc b/src/server/worker.cc
index 377f3abe..befaf97a 100644
--- a/src/server/worker.cc
+++ b/src/server/worker.cc
@@ -72,7 +72,7 @@ Worker::Worker(Server *svr, Config *config) : svr(svr), base_(event_base_new())
       LOG(INFO) << "[worker] Listening on: " << bind << ":" << *port;
     }
   }
-  lua_ = lua::CreateState(true);
+  lua_ = lua::CreateState(svr, true);
 }
 
 Worker::~Worker() {
diff --git a/src/storage/scripting.cc b/src/storage/scripting.cc
index 83a53264..a5850e68 100644
--- a/src/storage/scripting.cc
+++ b/src/storage/scripting.cc
@@ -51,11 +51,15 @@ enum {
 
 namespace lua {
 
-lua_State *CreateState(bool read_only) {
+lua_State *CreateState(Server *svr, bool read_only) {
   lua_State *lua = lua_open();
   LoadLibraries(lua);
   RemoveUnsupportedFunctions(lua);
   LoadFuncs(lua, read_only);
+
+  lua_pushlightuserdata(lua, svr);
+  lua_setglobal(lua, "__server_ptr");
+
   EnableGlobalsProtection(lua);
   return lua;
 }
@@ -351,7 +355,11 @@ int RedisGenericCommand(lua_State *lua, int raise_error) {
   }
 
   std::string cmd_name = util::ToLower(args[0]);
-  Server *srv = GetServer();
+
+  lua_getglobal(lua, "__server_ptr");
+  auto srv = reinterpret_cast<Server *>(lua_touserdata(lua, -1));
+  lua_pop(lua, 1);
+
   Config *config = srv->GetConfig();
 
   redis::Connection *conn = srv->GetCurrentConnection();
@@ -386,7 +394,7 @@ int RedisGenericCommand(lua_State *lua, int raise_error) {
   auto start = std::chrono::high_resolution_clock::now();
   bool is_profiling = conn->IsProfilingEnabled(cmd_name);
   std::string output;
-  s = cmd->Execute(GetServer(), srv->GetCurrentConnection(), &output);
+  s = cmd->Execute(srv, srv->GetCurrentConnection(), &output);
   auto end = std::chrono::high_resolution_clock::now();
   uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
   if (is_profiling) conn->RecordProfilingSampleIfNeed(cmd_name, duration);
diff --git a/src/storage/scripting.h b/src/storage/scripting.h
index 41b75e54..512fb668 100644
--- a/src/storage/scripting.h
+++ b/src/storage/scripting.h
@@ -31,7 +31,7 @@
 
 namespace lua {
 
-lua_State *CreateState(bool read_only = false);
+lua_State *CreateState(Server *svr, bool read_only = false);
 void DestroyState(lua_State *lua);
 
 void LoadFuncs(lua_State *lua, bool read_only = false);