Skip to content

Commit 00351fe

Browse files
committed
BugFix: Support different plugins to have the same name
1 parent a2acc4f commit 00351fe

File tree

8 files changed

+73
-8
lines changed

8 files changed

+73
-8
lines changed

docs/en/plugin_management.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ tPPC-Cpp framework provides a set of solutions for plugin dependency management.
9292
public:
9393
/// @brief get the collection of plugins that this plugin depends on
9494
/// dependent plugins need to be initialized first
95+
/// @note If there are cases of dependencies with the same name (possible in cases where the types are
96+
/// different), the plugin_name needs to follow the following format: 'plugin_name#plugin_type', where
97+
/// plugin_name is the name of the plugin and plugin_type is the corresponding index of the plugin type.
9598
virtual void GetDependencies(std::vector<std::string>& plugin_names) const {}
9699
};
97100
```

docs/zh/plugin_management.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ tPPC-Cpp 框架提供了一套插件依赖管理的方案:
9292
public:
9393
/// @brief get the collection of plugins that this plugin depends on
9494
/// dependent plugins need to be initialized first
95+
/// @note If there are cases of dependencies with the same name (possible in cases where the types are
96+
/// different), the plugin_name needs to follow the following format: 'plugin_name#plugin_type', where
97+
/// plugin_name is the name of the plugin and plugin_type is the corresponding index of the plugin type.
9598
virtual void GetDependencies(std::vector<std::string>& plugin_names) const {}
9699
};
97100
```

trpc/client/client_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class ClientContext : public RefCounted<ClientContext> {
390390
/// The request will be send directly to the address of remote service instance set by user after this function
391391
/// is called by user. The naming selector will be ignored.
392392
void SetAddr(const std::string& ip, uint16_t port) {
393-
endpoint_info_.addr.ip = std::move(ip);
393+
endpoint_info_.addr.ip = ip;
394394
endpoint_info_.addr.port = port;
395395
endpoint_info_.addr.addr_type =
396396
ip.find(':') != std::string::npos ? NodeAddr::AddrType::kIpV6 : NodeAddr::AddrType::kIpV4;

trpc/common/plugin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Plugin : public RefCounted<Plugin> {
5858

5959
/// @brief get the collection of plugins that this plugin depends on
6060
/// dependent plugins need to be initialized first
61+
/// @note If there are cases of dependencies with the same name (possible in cases where the types are
62+
/// different), the plugin_name needs to follow the following format: 'plugin_name#plugin_type', where
63+
/// plugin_name is the name of the plugin and plugin_type is the corresponding index of the plugin type.
6164
virtual void GetDependencies(std::vector<std::string>& plugin_names) const {}
6265

6366
/// @brief init plugin

trpc/common/trpc_plugin.cc

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void TrpcPlugin::InitPlugin(PluginInfo& plugin_info) {
161161
plugin_info.plugin->GetDependencies(dep_plugin_names);
162162
if (!dep_plugin_names.empty()) {
163163
for (const auto& name : dep_plugin_names) {
164-
auto it = plugins_.find(name);
164+
auto it = FindPlugin(name);
165165
if (it == plugins_.end()) {
166166
TRPC_FMT_ERROR("plugin `{}` dependence plugin `{}` not found.", plugin_info.plugin->Name(), name);
167167
TRPC_ASSERT(false);
@@ -200,7 +200,7 @@ void TrpcPlugin::StartPlugin(PluginInfo& plugin_info) {
200200
plugin_info.plugin->GetDependencies(dep_plugin_names);
201201
if (!dep_plugin_names.empty()) {
202202
for (const auto& name : dep_plugin_names) {
203-
auto it = plugins_.find(name);
203+
auto it = FindPlugin(name);
204204
TRPC_ASSERT(it != plugins_.end());
205205

206206
StartPlugin(it->second);
@@ -261,7 +261,7 @@ void TrpcPlugin::StopPlugin(PluginInfo& plugin_info) {
261261
std::vector<std::string>& reverse_dep_plugin_names = plugins_reverse_deps_[plugin_info.plugin->Name()];
262262
if (!reverse_dep_plugin_names.empty()) {
263263
for (const auto& name : reverse_dep_plugin_names) {
264-
auto it = plugins_.find(name);
264+
auto it = FindPlugin(name);
265265
TRPC_ASSERT(it != plugins_.end());
266266

267267
StopPlugin(it->second);
@@ -293,7 +293,7 @@ void TrpcPlugin::DestroyPlugin(PluginInfo& plugin_info) {
293293
std::vector<std::string>& reverse_dep_plugin_names = plugins_reverse_deps_[plugin_info.plugin->Name()];
294294
if (!reverse_dep_plugin_names.empty()) {
295295
for (const auto& name : reverse_dep_plugin_names) {
296-
auto it = plugins_.find(name);
296+
auto it = FindPlugin(name);
297297
TRPC_ASSERT(it != plugins_.end());
298298

299299
DestroyPlugin(it->second);
@@ -305,6 +305,56 @@ void TrpcPlugin::DestroyPlugin(PluginInfo& plugin_info) {
305305
plugin_info.is_destroyed = true;
306306
}
307307

308+
std::unordered_map<std::string, TrpcPlugin::PluginInfo>::iterator TrpcPlugin::FindPlugin(const std::string& name) {
309+
auto it = plugins_.find(name);
310+
if (it != plugins_.end()) {
311+
return it;
312+
}
313+
314+
std::unordered_map<std::string, TrpcPlugin::PluginInfo>::iterator match_it = plugins_.end();
315+
bool already_match = false;
316+
auto new_name = name + "#";
317+
for (it = plugins_.begin(); it != plugins_.end(); ++it) {
318+
auto& plugin_name = it->first;
319+
if (!plugin_name.compare(0, new_name.size(), new_name)) {
320+
if (already_match) {
321+
TRPC_FMT_ERROR("Find multiple match {} plugins, you need specify the dependencies with the plugin_type", name);
322+
TRPC_ASSERT(false && "Find multiple match plugins");
323+
} else {
324+
already_match = true;
325+
match_it = it;
326+
}
327+
}
328+
}
329+
330+
return match_it;
331+
}
332+
333+
bool TrpcPlugin::IsDepPluginNameValid(const std::vector<std::string>& dep_plugin_names) {
334+
bool ret = true;
335+
for (auto& plugin_name : dep_plugin_names) {
336+
auto pos = plugin_name.find_first_of("#");
337+
if (pos == std::string::npos) {
338+
continue;
339+
}
340+
341+
// If it contains '#', then the string at the end should be PluginType.
342+
auto str = plugin_name.substr(pos + 1);
343+
try {
344+
auto plugin_type = std::stoi(str);
345+
if (plugin_type > static_cast<int>(PluginType::kUnspecified)) {
346+
ret = false;
347+
TRPC_FMT_ERROR("dependent plugin name {} invalid", plugin_name);
348+
}
349+
} catch (const std::exception&) {
350+
ret = false;
351+
TRPC_FMT_ERROR("dependent plugin name {} invalid", plugin_name);
352+
}
353+
}
354+
355+
return ret;
356+
}
357+
308358
bool TrpcPlugin::RegisterServerCodec(const ServerCodecPtr& codec) {
309359
std::scoped_lock<std::mutex> lock(mutex_);
310360

trpc/common/trpc_plugin.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class TrpcPlugin {
165165
void StopPlugin(PluginInfo& plugin_info);
166166
void DestroyPlugins();
167167
void DestroyPlugin(PluginInfo& plugin_info);
168+
std::unordered_map<std::string, PluginInfo>::iterator FindPlugin(const std::string& name);
169+
bool IsDepPluginNameValid(const std::vector<std::string>& dep_plugin_names);
168170

169171
template <typename T>
170172
bool AddPlugins(const std::unordered_map<std::string, T>& plugins) {
@@ -174,12 +176,15 @@ class TrpcPlugin {
174176
plugin_info.is_inited = false;
175177
plugin_info.is_destroyed = false;
176178

177-
plugins_.emplace(it.second->Name(), std::move(plugin_info));
179+
std::string plugin_name = it.second->Name() + "#" + std::to_string(static_cast<int>(it.second->Type()));
180+
plugins_.emplace(plugin_name, std::move(plugin_info));
178181

179182
std::vector<std::string> dep_plugin_names;
180183
it.second->GetDependencies(dep_plugin_names);
184+
// Check if the dependent plugin name is valid
185+
TRPC_ASSERT(IsDepPluginNameValid(dep_plugin_names) && "Dependent plugin name invalid");
181186
for (auto& dep_plugin_name : dep_plugin_names) {
182-
plugins_reverse_deps_[dep_plugin_name].push_back(it.second->Name());
187+
plugins_reverse_deps_[dep_plugin_name].push_back(plugin_name);
183188
}
184189
}
185190

trpc/naming/common/constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ namespace trpc::naming {
1818
/// Key of instance's metadata
1919
constexpr char kNodeSetName[] = "internal-set-name";
2020
constexpr char kNodeContainerName[] = "container_name";
21+
constexpr char kNodeInstanceId[] = "instance_id";
2122

2223
} // namespace trpc::naming

trpc/naming/selector_workflow.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void SelectorWorkFlow::SetBackupRequestAddrs(const ClientContextPtr& context,
159159
extend_addr.addr.port = ins.port;
160160
extend_addr.addr.addr_type = ins.is_ipv6 ? NodeAddr::AddrType::kIpV6 : NodeAddr::AddrType::kIpV4;
161161
if (has_metadata_) {
162-
// extend_addr.metadata["instance_id"] = std::move(ins.meta["instance_id"]);
162+
extend_addr.metadata[naming::kNodeInstanceId] = std::move(ins.meta[naming::kNodeInstanceId]);
163163
extend_addr.metadata[naming::kNodeSetName] = std::move(ins.meta[naming::kNodeSetName]);
164164
extend_addr.metadata[naming::kNodeContainerName] = std::move(ins.meta[naming::kNodeContainerName]);
165165
}

0 commit comments

Comments
 (0)