Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove global server ptr getter used in scripting #1486

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 0 additions & 2 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion src/server/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 11 additions & 3 deletions src/storage/scripting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/storage/scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down