Unverified Commit 8fe8640e authored by Ge Jun's avatar Ge Jun Committed by GitHub

Merge pull request #776 from zyearn/dicovery_metadata

transmit discovery metadata into tag field of ServerNode
parents b627d16f b4c943f8
......@@ -16,6 +16,8 @@
#include <gflags/gflags.h>
#include "butil/third_party/rapidjson/document.h"
#include "butil/third_party/rapidjson/memorybuffer.h"
#include "butil/third_party/rapidjson/writer.h"
#include "butil/string_printf.h"
#include "butil/fast_rand.h"
#include "bthread/bthread.h"
......@@ -163,6 +165,16 @@ int ParseFetchsResult(const butil::IOBuf& buf,
}
for (BUTIL_RAPIDJSON_NAMESPACE::SizeType i = 0; i < instances.Size(); ++i) {
std::string metadata;
// convert metadata in object to string
auto itr_metadata = instances[i].FindMember("metadata");
if (itr_metadata != instances[i].MemberEnd()) {
BUTIL_RAPIDJSON_NAMESPACE::MemoryBuffer buffer;
BUTIL_RAPIDJSON_NAMESPACE::Writer<BUTIL_RAPIDJSON_NAMESPACE::MemoryBuffer> writer(buffer);
itr_metadata->value.Accept(writer);
metadata.assign(buffer.GetBuffer(), buffer.GetSize());
}
auto itr = instances[i].FindMember("addrs");
if (itr == instances[i].MemberEnd() || !itr->value.IsArray()) {
LOG(ERROR) << "Fail to find addrs or addrs is not an array";
......@@ -186,6 +198,7 @@ int ParseFetchsResult(const butil::IOBuf& buf,
addr.remove_prefix(pos + 3);
}
ServerNode node;
node.tag = metadata;
// Variable addr contains data from addrs[j].GetString(), it is a
// null-terminated string, so it is safe to pass addr.data() as the
// first parameter to str2endpoint.
......
......@@ -448,7 +448,8 @@ static const std::string s_fetchs_result = R"({
"rpc":"",
"version":"123",
"metadata":{
"weight": "10",
"cluster": ""
},
"addrs":[
"http://127.0.0.1:8999",
......@@ -475,7 +476,8 @@ static const std::string s_fetchs_result = R"({
"rpc":"",
"version":"123",
"metadata":{
"weight": "10",
"cluster": ""
},
"addrs":[
"http://127.0.0.1:8999",
......@@ -525,6 +527,7 @@ TEST(NamingServiceTest, discovery_parse_function) {
buf.append(s_fetchs_result);
ASSERT_EQ(0, brpc::policy::ParseFetchsResult(buf, "admin.test", &servers));
ASSERT_EQ((size_t)1, servers.size());
ASSERT_EQ(servers[0].tag, "{\"weight\":\"10\",\"cluster\":\"\"}");
buf.clear();
buf.append(s_nodes_result);
std::string server;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment