-
Notifications
You must be signed in to change notification settings - Fork 197
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
fix #387: fix tls block #379
Changes from 12 commits
bb586bb
aad4c0c
effcc86
8844b79
68a41a3
646d2d7
f69ee0d
fc90258
0d06eea
f528215
a969315
b352c5d
9021f53
6eb8493
1b77810
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,41 @@ CppPluginManagerImpl::CppPluginManagerImpl(LightningGraph* db, const std::string | |
|
||
CppPluginManagerImpl::~CppPluginManagerImpl() {} | ||
|
||
void CppPluginManagerImpl::OpenDynamicLib(const PluginInfoBase* pinfo, DynamicLibinfo &dinfo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文件头加点儿注释说明一下tls block的问题吧 |
||
using namespace lgraph::dll; | ||
const auto* info = dynamic_cast<const PluginInfo*>(pinfo); | ||
dinfo.lib_handle = LoadDynamicLibrary(info->path); | ||
if (!dinfo.lib_handle) { | ||
auto errMsg = GetLastErrorMsg(); | ||
if (errMsg.find("cannot allocate memory in static TLS block") != std::string::npos) { | ||
errMsg += ". Wait for other tasks to finish and try again"; | ||
} | ||
throw InputError("Failed to load the DLL: " + errMsg); | ||
} | ||
if (info->has_func) { | ||
dinfo.func = GetDllFunction<PluginFunc*>(dinfo.lib_handle, "Process"); | ||
if (!dinfo.func) { | ||
UnloadDynamicLibrary(dinfo.lib_handle); | ||
throw InputError("Failed to get Process() function in the DLL: " + GetLastErrorMsg()); | ||
} | ||
} else { | ||
dinfo.get_sig_spec = GetDllFunction<SignatureGetter*>(dinfo.lib_handle, "GetSignature"); | ||
dinfo.func_txn = GetDllFunction<PluginFuncInTxn*>(dinfo.lib_handle, "ProcessInTxn"); | ||
if (!dinfo.func_txn) { | ||
UnloadDynamicLibrary(dinfo.lib_handle); | ||
throw InputError( | ||
"Failed to get ProcessInTxn() function in the DLL: " | ||
+ GetLastErrorMsg()); | ||
} | ||
} | ||
} | ||
|
||
void CppPluginManagerImpl::CloseDynamicLib(DynamicLibinfo &dinfo) { | ||
using namespace lgraph::dll; | ||
if (!UnloadDynamicLibrary(dinfo.lib_handle)) | ||
throw InternalError("Failed to unload library."); | ||
} | ||
|
||
void CppPluginManagerImpl::DoCall(lgraph_api::Transaction* txn, | ||
const std::string& user, | ||
AccessControlledDB* db_with_access_control, | ||
|
@@ -45,54 +80,71 @@ void CppPluginManagerImpl::DoCall(lgraph_api::Transaction* txn, | |
|
||
// TODO: support in_process // NOLINT | ||
bool r = false; | ||
const PluginInfo* info = dynamic_cast<const PluginInfo*>(pinfo); | ||
if (info->func) { | ||
PluginFunc* procedure = info->func; | ||
lgraph_api::GraphDB db(db_with_access_control, info->read_only); | ||
DynamicLibinfo info; | ||
OpenDynamicLib(pinfo, info); | ||
if (info.func) { | ||
PluginFunc* procedure = info.func; | ||
lgraph_api::GraphDB db(db_with_access_control, pinfo->read_only); | ||
r = procedure(db, request, output); | ||
} else if (info->func_txn && txn != nullptr) { | ||
PluginFuncInTxn * procedure = info->func_txn; | ||
} else if (info.func_txn && txn != nullptr) { | ||
PluginFuncInTxn * procedure = info.func_txn; | ||
r = procedure(*txn, request, output); | ||
} | ||
CloseDynamicLib(info); | ||
|
||
if (!r) throw InputError(FMA_FMT("Plugin returned false. Output: {}.", output)); | ||
} | ||
|
||
void CppPluginManagerImpl::LoadPlugin(const std::string& user, const std::string& name, | ||
PluginInfoBase* pinfo) { | ||
using namespace lgraph::dll; | ||
PluginInfo* info = dynamic_cast<PluginInfo*>(pinfo); | ||
std::string path = GetPluginPath(name); | ||
info->lib_handle = LoadDynamicLibrary(path); | ||
if (!info->lib_handle) throw InputError("Failed to load the DLL: " + GetLastErrorMsg()); | ||
info->get_sig_spec = GetDllFunction<SignatureGetter*>(info->lib_handle, "GetSignature"); | ||
// it's ok for plugin which DOES NOT have `GetSignature` function. | ||
// Plugins without `GetSignature` are not guaranteed to call safely in InQueryCall context. | ||
if (!info->get_sig_spec) { | ||
info->func = GetDllFunction<PluginFunc*>(info->lib_handle, "Process"); | ||
if (!info->func) { | ||
UnloadDynamicLibrary(info->lib_handle); | ||
throw InputError("Failed to get Process() function in the DLL: " + GetLastErrorMsg()); | ||
info->path = GetPluginPath(name); | ||
auto lib_handle = LoadDynamicLibrary(info->path); | ||
if (!lib_handle) { | ||
auto errMsg = GetLastErrorMsg(); | ||
if (errMsg.find("cannot allocate memory in static TLS block") != std::string::npos) { | ||
errMsg += ". Wait for other tasks to finish and try again"; | ||
} | ||
info->sig_spec = nullptr; | ||
return; | ||
} else { | ||
info->func_txn = GetDllFunction<PluginFuncInTxn*>(info->lib_handle, "ProcessInTxn"); | ||
if (!info->func_txn) { | ||
UnloadDynamicLibrary(info->lib_handle); | ||
throw InputError("Failed to get Process() function in the DLL: " + GetLastErrorMsg()); | ||
throw InputError("Failed to load the DLL: " + errMsg); | ||
} | ||
{ | ||
auto get_sig_spec = GetDllFunction<SignatureGetter*>(lib_handle, "GetSignature"); | ||
// it's ok for plugin which DOES NOT have `GetSignature` function. | ||
// Plugins without `GetSignature` are not guaranteed to call safely in InQueryCall context. | ||
if (!get_sig_spec) { | ||
auto func = GetDllFunction<PluginFunc*>(lib_handle, "Process"); | ||
if (!func) { | ||
UnloadDynamicLibrary(lib_handle); | ||
throw InputError("Failed to get Process() function in the DLL: " + | ||
GetLastErrorMsg()); | ||
} | ||
info->sig_spec = nullptr; | ||
info->has_func = true; | ||
} else { | ||
auto func_txn = GetDllFunction<PluginFuncInTxn*>(lib_handle, "ProcessInTxn"); | ||
if (!func_txn) { | ||
UnloadDynamicLibrary(lib_handle); | ||
throw InputError("Failed to get Process() function in the DLL: " + | ||
GetLastErrorMsg()); | ||
} | ||
auto sig_spec = std::make_unique<lgraph_api::SigSpec>(); | ||
bool r = get_sig_spec(*sig_spec); | ||
if (!r) throw InputError(FMA_FMT("Failed to get Signature")); | ||
info->sig_spec = std::move(sig_spec); | ||
info->has_func = false; | ||
} | ||
auto sig_spec = std::make_unique<lgraph_api::SigSpec>(); | ||
bool r = info->get_sig_spec(*sig_spec); | ||
if (!r) throw InputError(FMA_FMT("Failed to get Signature")); | ||
info->sig_spec = std::move(sig_spec); | ||
} | ||
if (!UnloadDynamicLibrary(lib_handle)) { | ||
throw InputError("Failed to unload DLL: " + GetLastErrorMsg()); | ||
} | ||
} | ||
|
||
void CppPluginManagerImpl::UnloadPlugin(const std::string& user, const std::string& name, | ||
PluginInfoBase* pinfo) { | ||
using namespace lgraph::dll; | ||
PluginInfo* info = dynamic_cast<PluginInfo*>(pinfo); | ||
if (!UnloadDynamicLibrary(info->lib_handle)) | ||
throw InternalError("Failed to unload library [{}].", name); | ||
// using namespace lgraph::dll; | ||
// PluginInfo* info = dynamic_cast<PluginInfo*>(pinfo); | ||
// if (!UnloadDynamicLibrary(info->lib_handle)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 注释清掉,然后留一个函数头说明一下 |
||
// throw InternalError("Failed to unload library [{}].", name); | ||
} | ||
} // namespace lgraph |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,10 @@ void build_so() { | |
"../../test/test_procedures/add_label_v.cpp", LIBLGRAPH); | ||
rt = system(cmd.c_str()); | ||
UT_EXPECT_EQ(rt, 0); | ||
cmd = UT_FMT(cmd_f.c_str(), INCLUDE_DIR, DEPS_INCLUDE_DIR, "./bfs.so", | ||
"../../test/test_procedures/bfs.cpp", LIBLGRAPH); | ||
rt = system(cmd.c_str()); | ||
UT_EXPECT_EQ(rt, 0); | ||
} | ||
|
||
void read_code(const std::string& code_path, std::string& code) { | ||
|
@@ -211,6 +215,7 @@ TEST_F(TestCppPlugin, CppPlugin) { | |
std::string code_zip = ""; | ||
std::string code_scan_graph = ""; | ||
std::string code_add_label = ""; | ||
std::string code_bfs = ""; | ||
{ | ||
// read file to string | ||
std::string code_so_path = "./sortstr.so"; | ||
|
@@ -224,12 +229,14 @@ TEST_F(TestCppPlugin, CppPlugin) { | |
#endif | ||
std::string code_scan_graph_path = "./scan_graph.so"; | ||
std::string code_add_label_path = "./add_label.so"; | ||
std::string code_bfs_path = "./bfs.so"; | ||
build_so(); | ||
read_code(code_so_path, code_so); | ||
read_code(code_cpp_path, code_cpp); | ||
read_code(code_zip_path, code_zip); | ||
read_code(code_scan_graph_path, code_scan_graph); | ||
read_code(code_add_label_path, code_add_label); | ||
read_code(code_bfs_path, code_bfs); | ||
UT_EXPECT_NE(code_so, ""); | ||
UT_EXPECT_NE(code_cpp, ""); | ||
UT_EXPECT_NE(code_zip, ""); | ||
|
@@ -445,6 +452,23 @@ TEST_F(TestCppPlugin, CppPlugin) { | |
"#include <stdlib.h>", (plugin::CodeType)6, | ||
"test", true, "v1")); | ||
} | ||
#ifndef ENABLE_ASAN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一般是#ifndef __SANITIZE_ADDRESS__ 下面这里有内存问题? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. asan下dlclose并不会真正卸载动态库,这是asan一直就有的问题了:google/sanitizers#89 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我改成__SANITIZE_ADDRESS__吧 |
||
{ | ||
UT_LOG() << "Testing load many plugins"; | ||
for (int i = 0; i < 32; i++) { | ||
UT_LOG() << "try load bfs_" << i; | ||
bool r = false; | ||
UT_EXPECT_NO_THROW( | ||
r = pm.LoadPluginFromCode(lgraph::_detail::DEFAULT_ADMIN_NAME, | ||
"bfs_" + std::to_string(i), | ||
code_bfs, plugin::CodeType::SO, | ||
"bfs v1", true, "v1")); | ||
UT_EXPECT_TRUE(r); | ||
fma_common::SleepS(5); | ||
} | ||
pm.DeleteAllPlugins(lgraph::_detail::DEFAULT_ADMIN_NAME); | ||
} | ||
#endif // ENABLE_ASAN | ||
} | ||
#endif | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个做什么用?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看到下面了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DENABLE_ASAN 改完了删掉吧