MCPcopy Create free account
hub / github.com/apache/brpc / SelectServer

Method SelectServer

src/brpc/policy/dynpart_load_balancer.cpp:101–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

99}
100
101int DynPartLoadBalancer::SelectServer(const SelectIn& in, SelectOut* out) {
102 butil::DoublyBufferedData<Servers>::ScopedPtr s;
103 if (_db_servers.Read(&s) != 0) {
104 return ENOMEM;
105 }
106 const size_t n = s->server_list.size();
107 if (n == 0) {
108 return ENODATA;
109 }
110 if (n == 1) {
111 if (Socket::Address(s->server_list[0].id, out->ptr) == 0) {
112 return 0;
113 }
114 return EHOSTDOWN;
115 }
116 int64_t total_weight = 0;
117 std::pair<SocketUniquePtr, int64_t> ptrs[8];
118 int nptr = 0;
119 bool exclusion = true;
120 do {
121 for (size_t i = 0; i < n; ++i) {
122 const SocketId id = s->server_list[i].id;
123 if ((!exclusion || !ExcludedServers::IsExcluded(in.excluded, id))
124 && Socket::Address(id, &ptrs[nptr].first) == 0) {
125 int w = schan::GetSubChannelWeight(ptrs[nptr].first->user());
126 total_weight += w;
127 RELEASE_ASSERT_VERBOSE(nptr < 8, "Not supported yet");
128 ptrs[nptr].second = total_weight;
129 ++nptr;
130 }
131 }
132 if (nptr != 0) {
133 break;
134 }
135 if (!exclusion) {
136 return EHOSTDOWN;
137 }
138 exclusion = false;
139 CHECK_EQ(0, total_weight);
140 total_weight = 0;
141 } while (1);
142
143 if (nptr == 1) {
144 out->ptr->reset(ptrs[0].first.release());
145 return 0;
146 }
147 uint32_t r = butil::fast_rand_less_than(total_weight);
148 for (int i = 0; i < nptr; ++i) {
149 if (ptrs[i].second > r) {
150 out->ptr->reset(ptrs[i].first.release());
151 return 0;
152 }
153 }
154 return EHOSTDOWN;
155}
156
157DynPartLoadBalancer* DynPartLoadBalancer::New(const butil::StringPiece&) const {
158 return new (std::nothrow) DynPartLoadBalancer;

Callers 5

select_serverFunction · 0.45
TEST_FFunction · 0.45
TEST_FFunction · 0.45
IssueRPCMethod · 0.45
CheckHealthMethod · 0.45

Calls 6

GetSubChannelWeightFunction · 0.85
fast_rand_less_thanFunction · 0.85
ReadMethod · 0.45
sizeMethod · 0.45
resetMethod · 0.45
releaseMethod · 0.45

Tested by 3

select_serverFunction · 0.36
TEST_FFunction · 0.36
TEST_FFunction · 0.36