Skip to content

Commit fe5a3dc

Browse files
bochencwxlioncfliu
authored andcommitted
Optimize: Modify the metadata_map acquisition mode schema
1 parent 83cca7b commit fe5a3dc

2 files changed

Lines changed: 58 additions & 62 deletions

File tree

trpc/naming/polarismesh/polarismesh_selector.cc

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,10 @@ namespace naming::polarismesh {
3939

4040
uint32_t g_polarismesh_selector_plugin_id;
4141

42-
uint32_t GetPolarisMeshSelectorPluginID() {
43-
return g_polarismesh_selector_plugin_id;
44-
}
42+
uint32_t GetPolarisMeshSelectorPluginID() { return g_polarismesh_selector_plugin_id; }
4543

4644
} // namespace naming::polarismesh
4745

48-
4946
// Set the transparent information of the Selector-META-prefix to the polaris Routing rule
5047
void SetTransSelectorMeta(const ClientContextPtr& context, std::map<std::string, std::string>* metadata) {
5148
static constexpr char meta_prefix[] = "selector-meta-";
@@ -83,9 +80,9 @@ int PolarisMeshSelector::Init() noexcept {
8380
}
8481
polarismesh_context_ = trpc::TrpcShareContext::GetInstance()->GetPolarisContext();
8582
consumer_api_ = std::unique_ptr<polaris::ConsumerApi>(polaris::ConsumerApi::Create(polarismesh_context_.get()));
86-
// polaris::GetLogger()->SetLogLevel(polaris::kDebugLogLevel);
87-
// auto polarismesh_context_ = polaris::ConsumerApi::CreateWithDefaultFile();
88-
//consumer_api_ = std::unique_ptr<polaris::ConsumerApi>(polarismesh_context_);
83+
// polaris::GetLogger()->SetLogLevel(polaris::kDebugLogLevel);
84+
// auto polarismesh_context_ = polaris::ConsumerApi::CreateWithDefaultFile();
85+
// consumer_api_ = std::unique_ptr<polaris::ConsumerApi>(polarismesh_context_);
8986
if (!consumer_api_) {
9087
TRPC_FMT_ERROR("Create ConsumerApi failed");
9188
return -1;
@@ -136,11 +133,10 @@ int PolarisMeshSelector::SelectImpl(const SelectorInfo* info, polaris::Instances
136133
request.SetHashString(hash_key);
137134
// Set up a copy indexy
138135
uint64_t replicate_index = trpc::util::Convert<uint64_t, std::string>(
139-
GetValueFromContextOrExtend(info->context, info->extend_select_info, "replicate_index"));
136+
GetValueFromContextOrExtend(info->context, info->extend_select_info, "replicate_index"));
140137
request.SetReplicateIndex(replicate_index);
141138
// polarismesh bug, use SethashKey in the early stages
142-
uint64_t u64_hash_key = trpc::util::Convert<uint64_t, std::string>(
143-
hash_key);
139+
uint64_t u64_hash_key = trpc::util::Convert<uint64_t, std::string>(hash_key);
144140
request.SetHashKey(u64_hash_key);
145141
}
146142

@@ -158,10 +154,10 @@ int PolarisMeshSelector::SelectImpl(const SelectorInfo* info, polaris::Instances
158154
request.SetTimeout(timeout_);
159155

160156
// Fill in metadata
161-
auto* meta = naming::polarismesh::GetFilterMetadataOfNaming(info->context,
162-
PolarisMetadataType::kPolarisDstMetaRouteLable);
163-
if (meta) {
164-
request.SetMetadata(*const_cast<std::map<std::string, std::string>*>(meta));
157+
auto meta =
158+
naming::polarismesh::GetFilterMetadataOfNaming(info->context, PolarisMetadataType::kPolarisDstMetaRouteLable);
159+
if (meta != nullptr) {
160+
request.SetMetadata(*(meta.get()));
165161
}
166162

167163
// If it is a backup strategy, you need to set the number of Backup nodes
@@ -204,7 +200,8 @@ int PolarisMeshSelector::Select(const SelectorInfo* info, TrpcEndpointInfo* endp
204200
ConvertPolarisInstance(instances[0], *endpoint, true);
205201
}
206202
TRPC_FMT_DEBUG("Select result {}:{}, id:{}, service_name:{}, service_namespace:{}", endpoint->host, endpoint->port,
207-
endpoint->id, info->name, GetValueFromContextOrExtend(info->context, info->extend_select_info, "namespace"));
203+
endpoint->id, info->name,
204+
GetValueFromContextOrExtend(info->context, info->extend_select_info, "namespace"));
208205

209206
return 0;
210207
}
@@ -288,10 +285,10 @@ int PolarisMeshSelector::SelectBatch(const SelectorInfo* info, std::vector<TrpcE
288285
discovery_req.SetSourceService(source_service_info);
289286

290287
// Fill in metadata
291-
auto* meta = naming::polarismesh::GetFilterMetadataOfNaming(info->context,
292-
PolarisMetadataType::kPolarisDstMetaRouteLable);
288+
auto meta =
289+
naming::polarismesh::GetFilterMetadataOfNaming(info->context, PolarisMetadataType::kPolarisDstMetaRouteLable);
293290
if (meta) {
294-
discovery_req.SetMetadata(*const_cast<std::map<std::string, std::string>*>(meta));
291+
discovery_req.SetMetadata(*(meta.get()));
295292
}
296293

297294
polaris::ReturnCode ret = consumer_api_->GetInstances(discovery_req, discovery_rsp);
@@ -354,15 +351,15 @@ int PolarisMeshSelector::ReportInvokeResult(const InvokeResult* result) {
354351
result_req.SetDelay(result->cost_time);
355352
// load balancing algorithm Info
356353
uint64_t locality_aware_info = trpc::util::Convert<uint64_t, std::string>(
357-
GetValueFromContextOrExtend(result->context, nullptr, "locality_aware_info"));
354+
GetValueFromContextOrExtend(result->context, nullptr, "locality_aware_info"));
358355
if (locality_aware_info != 0) {
359356
result_req.SetLocalityAwareInfo(locality_aware_info);
360357
}
361358

362-
auto* circuit_breaker_lables = naming::polarismesh::GetFilterMetadataOfNaming(
363-
result->context, PolarisMetadataType::kPolarisCircuitBreakLable);
359+
auto circuit_breaker_lables =
360+
naming::polarismesh::GetFilterMetadataOfNaming(result->context, PolarisMetadataType::kPolarisCircuitBreakLable);
364361
if (circuit_breaker_lables) {
365-
result_req.SetLabels(*circuit_breaker_lables);
362+
result_req.SetLabels(*(circuit_breaker_lables.get()));
366363
}
367364

368365
int ret = consumer_api_->UpdateServiceCallResult(result_req);
@@ -384,14 +381,14 @@ bool PolarisMeshSelector::SetCircuitBreakWhiteList(const std::vector<int>& frame
384381
}
385382

386383
void PolarisMeshSelector::FillMetadataOfSourceServiceInfo(const SelectorInfo* info,
387-
polaris::ServiceInfo& source_service_info) {
384+
polaris::ServiceInfo& source_service_info) {
388385
const auto& context = info->context;
389386
auto& metadata = source_service_info.metadata_;
390387

391-
auto* filter_meta = naming::polarismesh::GetFilterMetadataOfNaming(info->context,
392-
PolarisMetadataType::kPolarisRuleRouteLable);
388+
auto filter_meta =
389+
naming::polarismesh::GetFilterMetadataOfNaming(info->context, PolarisMetadataType::kPolarisRuleRouteLable);
393390
if (filter_meta) {
394-
metadata = *filter_meta;
391+
metadata = *(filter_meta.get());
395392
}
396393

397394
// Set the ENV of the main party as the ENV in the frame configuration
@@ -407,19 +404,19 @@ void PolarisMeshSelector::FillMetadataOfSourceServiceInfo(const SelectorInfo* in
407404

408405
// Set the main information of the main party
409406
metadata[polaris::SetDivisionServiceRouter::enable_set_force] =
410-
GetValueFromContextOrExtend(info->context, info->extend_select_info, "enable_set_force");
407+
GetValueFromContextOrExtend(info->context, info->extend_select_info, "enable_set_force");
411408
TRPC_FMT_DEBUG("Enable set force, service_name:{}", info->name);
412409

413410
if (!GetValueFromContextOrExtend(info->context, info->extend_select_info, "callee_set_name").empty()) {
414411
metadata[polaris::constants::kRouterRequestSetNameKey] =
415412
GetValueFromContextOrExtend(info->context, info->extend_select_info, "callee_set_name");
416413
TRPC_FMT_DEBUG("Add source setname to metadata, CalleeSetName:{} service_name:{}",
417-
GetValueFromContextOrExtend(info->context, info->extend_select_info, "callee_set_name"), info->name);
414+
GetValueFromContextOrExtend(info->context, info->extend_select_info, "callee_set_name"), info->name);
418415
}
419416
}
420417

421418
void PolarisMeshSelector::GetSourceServiceKey(const ClientContextPtr& client_context_ptr,
422-
const std::any* extend_select_info, polaris::ServiceKey& service_key) {
419+
const std::any* extend_select_info, polaris::ServiceKey& service_key) {
423420
service_key.name_ = client_context_ptr->GetCallerName();
424421
auto& global_namespace = TrpcConfig::GetInstance()->GetGlobalConfig().env_namespace;
425422
if (!global_namespace.empty()) {
@@ -428,10 +425,11 @@ void PolarisMeshSelector::GetSourceServiceKey(const ClientContextPtr& client_con
428425
service_key.namespace_ = GetNamespaceFromContextOrExtend(client_context_ptr, extend_select_info);
429426
}
430427

431-
return ;
428+
return;
432429
}
433430

434-
std::string PolarisMeshSelector::ParseExtendSelectInfo(const std::any* extend_select_info, const std::string& field_name) {
431+
std::string PolarisMeshSelector::ParseExtendSelectInfo(const std::any* extend_select_info,
432+
const std::string& field_name) {
435433
if (extend_select_info == nullptr) {
436434
return "";
437435
}
@@ -447,16 +445,16 @@ std::string PolarisMeshSelector::ParseExtendSelectInfo(const std::any* extend_se
447445
return it->second;
448446
}
449447
} catch (const std::bad_any_cast& e) {
450-
TRPC_FMT_ERROR("Failed to cast extend_select_info to std::unordered_map<std::string, std::string>: {}",
451-
e.what());
448+
TRPC_FMT_ERROR("Failed to cast extend_select_info to std::unordered_map<std::string, std::string>: {}", e.what());
452449
}
453450

454451
// If the field is not found or the conversion fails, return an empty string
455452
return "";
456453
}
457454

458455
std::string PolarisMeshSelector::GetValueFromContextOrExtend(const ClientContextPtr& context,
459-
const std::any* extend_select_info, const std::string& field_name) {
456+
const std::any* extend_select_info,
457+
const std::string& field_name) {
460458
std::string value = naming::polarismesh::GetSelectorExtendInfo(context, field_name);
461459
if (value.empty()) {
462460
value = ParseExtendSelectInfo(extend_select_info, field_name);

trpc/naming/polarismesh/polarismesh_selector.h

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
#include <string>
1919
#include <unordered_map>
2020
#include <vector>
21-
#include "rapidjson/document.h"
22-
#include "rapidjson/stringbuffer.h"
23-
#include "rapidjson/writer.h"
21+
2422
#include "polaris/api/consumer_api.h"
2523
#include "polaris/consumer.h"
2624
#include "polaris/context.h"
25+
#include "rapidjson/document.h"
26+
#include "rapidjson/stringbuffer.h"
27+
#include "rapidjson/writer.h"
2728

28-
#include "readers_writer_data.h"
2929
#include "trpc/naming/common/common_defs.h"
3030
#include "trpc/naming/polarismesh/common.h"
31+
#include "trpc/naming/polarismesh/readers_writer_data.h"
3132
#include "trpc/naming/selector.h"
3233

3334
namespace trpc {
@@ -54,8 +55,8 @@ uint32_t GetPolarisMeshSelectorPluginID();
5455
/// @param key_value_pairs A variadic list of key-value pairs to set in the context's filter data.
5556
template <typename T, typename... Args>
5657
void SetSelectorExtendInfo(T& context, Args&&... key_value_pairs) {
57-
auto* data_map = context->template GetFilterData<std::unordered_map<std::string,
58-
std::string>>(GetPolarisMeshSelectorPluginID());
58+
auto* data_map =
59+
context->template GetFilterData<std::unordered_map<std::string, std::string>>(GetPolarisMeshSelectorPluginID());
5960
if (!data_map) {
6061
std::unordered_map<std::string, std::string> new_data_map;
6162
(new_data_map.emplace(std::forward<Args>(key_value_pairs)), ...);
@@ -82,8 +83,8 @@ void SetSelectorExtendInfo(T& context, Args&&... key_value_pairs) {
8283
/// @return The value of the specified property if found, or an empty string if not found.
8384
template <typename T>
8485
std::string GetSelectorExtendInfo(T& context, const std::string& key) {
85-
auto* data_map = context->template GetFilterData<std::unordered_map<std::string,
86-
std::string>>(GetPolarisMeshSelectorPluginID());
86+
auto* data_map =
87+
context->template GetFilterData<std::unordered_map<std::string, std::string>>(GetPolarisMeshSelectorPluginID());
8788
if (data_map) {
8889
auto it = data_map->find(key);
8990
if (it != data_map->end()) {
@@ -99,12 +100,12 @@ std::string GetSelectorExtendInfo(T& context, const std::string& key) {
99100
/// @param type The PolarisMetadataType index to set the metadata for
100101
/// @param metadata_map The metadata map to set in the context's filter data
101102
template <typename T>
102-
void SetFilterMetadataOfNaming(T& context, const std::map<std::string,
103-
std::string>& metadata_map, PolarisMetadataType type) {
103+
void SetFilterMetadataOfNaming(T& context, const std::map<std::string, std::string>& metadata_map,
104+
PolarisMetadataType type) {
104105
const std::string key = "metadata_" + std::to_string(static_cast<int>(type));
105106

106-
auto* data_map = context->template GetFilterData<std::unordered_map<std::string,
107-
std::string>>(GetPolarisMeshSelectorPluginID());
107+
auto* data_map =
108+
context->template GetFilterData<std::unordered_map<std::string, std::string>>(GetPolarisMeshSelectorPluginID());
108109
if (!data_map) {
109110
std::unordered_map<std::string, std::string> new_data_map;
110111

@@ -138,21 +139,21 @@ void SetFilterMetadataOfNaming(T& context, const std::map<std::string,
138139
/// @param context The context from which to retrieve the metadata
139140
/// @return A map containing the metadata, or an empty map if the metadata is not found
140141
template <typename T>
141-
const std::map<std::string, std::string>* GetFilterMetadataOfNaming(T& context, PolarisMetadataType type) {
142+
std::unique_ptr<std::map<std::string, std::string>> GetFilterMetadataOfNaming(T& context, PolarisMetadataType type) {
142143
const std::string key = "metadata_" + std::to_string(static_cast<int>(type));
143144

144-
auto* data_map = context->template GetFilterData<std::unordered_map<std::string,
145-
std::string>>(GetPolarisMeshSelectorPluginID());
145+
auto* data_map =
146+
context->template GetFilterData<std::unordered_map<std::string, std::string>>(GetPolarisMeshSelectorPluginID());
146147
if (data_map) {
147148
auto it = data_map->find(key);
148149
if (it != data_map->end()) {
149150
rapidjson::Document json_metadata;
150151
json_metadata.Parse(it->second.c_str());
151-
std::map<std::string, std::string> metadata_map;
152+
auto metadata_map_ptr = std::make_unique<std::map<std::string, std::string>>(); // Create a new unique_ptr
152153
for (auto& m : json_metadata.GetObject()) {
153-
metadata_map.emplace(m.name.GetString(), m.value.GetString());
154+
metadata_map_ptr->emplace(m.name.GetString(), m.value.GetString());
154155
}
155-
return metadata_map.size() > 0 ? &metadata_map : nullptr;
156+
return metadata_map_ptr->size() > 0 ? std::move(metadata_map_ptr) : nullptr;
156157
}
157158
}
158159

@@ -204,17 +205,15 @@ class PolarisMeshSelector : public Selector {
204205

205206
/// @brief Set framework error codes melting white list
206207
bool SetCircuitBreakWhiteList(const std::vector<int>& framework_retcodes) override;
207-
208+
208209
/// @brief Setter function for plugin_config_
209-
void SetPluginConfig(const naming::PolarisMeshNamingConfig& config) {
210-
plugin_config_ = config;
211-
}
210+
void SetPluginConfig(const naming::PolarisMeshNamingConfig& config) { plugin_config_ = config; }
212211

213212
/// @brief ServiceKey, the main tone
214213
/// @param[in] client_context_ptr Client context
215214
/// @param[out] service_key SERVICEKEY of the main tone
216-
void GetSourceServiceKey(const ClientContextPtr& client_context_ptr,
217-
const std::any* extend_select_info, polaris::ServiceKey& service_key);
215+
void GetSourceServiceKey(const ClientContextPtr& client_context_ptr, const std::any* extend_select_info,
216+
polaris::ServiceKey& service_key);
218217

219218
private:
220219
// Get the specific implementation of the service node from the SDK GetoneInstance interface
@@ -229,12 +228,11 @@ class PolarisMeshSelector : public Selector {
229228

230229
// Tries to get the value for the given field_name from the context.
231230
// If the value is not found in the context, it tries to get it from the extend_select_info.
232-
std::string GetValueFromContextOrExtend(const ClientContextPtr& context,
233-
const std::any* extend_select_info, const std::string& field_name);
231+
std::string GetValueFromContextOrExtend(const ClientContextPtr& context, const std::any* extend_select_info,
232+
const std::string& field_name);
234233

235234
// Tries to get the value for the given "namespace" from the context.
236-
std::string GetNamespaceFromContextOrExtend(const ClientContextPtr& context,
237-
const std::any* extend_select_info);
235+
std::string GetNamespaceFromContextOrExtend(const ClientContextPtr& context, const std::any* extend_select_info);
238236

239237
private:
240238
bool init_{false};

0 commit comments

Comments
 (0)