Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
f3e8790b
Commit
f3e8790b
authored
Feb 09, 2018
by
root
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change servers struct as comments
parent
42c1c32f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
14 deletions
+29
-14
weighted_round_robin_load_balancer.cpp
src/brpc/policy/weighted_round_robin_load_balancer.cpp
+23
-12
weighted_round_robin_load_balancer.h
src/brpc/policy/weighted_round_robin_load_balancer.h
+6
-2
No files found.
src/brpc/policy/weighted_round_robin_load_balancer.cpp
View file @
f3e8790b
...
...
@@ -21,14 +21,20 @@
namespace
brpc
{
namespace
policy
{
static
const
int
EraseBatchSize
=
100
;
bool
WeightedRoundRobinLoadBalancer
::
Add
(
Servers
&
bg
,
const
ServerId
&
id
)
{
if
(
bg
.
find
(
id
.
id
)
!=
bg
.
end
()
)
{
return
false
;
if
(
bg
.
server_list
.
capacity
()
<
128
)
{
bg
.
server_list
.
reserve
(
128
)
;
}
int
weight
=
0
;
if
(
butil
::
StringToInt
(
id
.
tag
,
&
weight
)
&&
weight
>
0
)
{
bg
.
emplace
(
id
.
id
,
weight
);
return
true
;
bool
insert_server
=
bg
.
server_map
.
emplace
(
id
.
id
,
bg
.
server_list
.
size
()).
second
;
if
(
insert_server
)
{
bg
.
server_list
.
emplace_back
(
id
.
id
,
weight
);
return
true
;
}
}
else
{
LOG
(
ERROR
)
<<
"Invalid weight is set: "
<<
id
.
tag
;
}
...
...
@@ -36,8 +42,13 @@ bool WeightedRoundRobinLoadBalancer::Add(Servers& bg, const ServerId& id) {
}
bool
WeightedRoundRobinLoadBalancer
::
Remove
(
Servers
&
bg
,
const
ServerId
&
id
)
{
if
(
bg
.
find
(
id
.
id
)
!=
bg
.
end
())
{
bg
.
erase
(
id
.
id
);
auto
iter
=
bg
.
server_map
.
find
(
id
.
id
);
if
(
iter
!=
bg
.
server_map
.
end
())
{
const
size_t
index
=
iter
->
second
;
bg
.
server_list
[
index
]
=
bg
.
server_list
.
back
();
bg
.
server_map
[
bg
.
server_list
[
index
].
first
]
=
index
;
bg
.
server_list
.
pop_back
();
bg
.
server_map
.
erase
(
iter
);
return
true
;
}
return
false
;
...
...
@@ -92,7 +103,7 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut*
if
(
_db_servers
.
Read
(
&
s
)
!=
0
)
{
return
ENOMEM
;
}
if
(
s
->
empty
())
{
if
(
s
->
server_list
.
empty
())
{
return
ENODATA
;
}
TLS
&
tls
=
s
.
tls
();
...
...
@@ -100,7 +111,7 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut*
int
total_weight
=
0
;
// TODO: each thread requsts service as the same sequence.
// We can set a random beginning position for each thread.
for
(
const
auto
&
server
:
*
s
)
{
for
(
const
auto
&
server
:
s
->
server_list
)
{
// A new server is added or the wrr fisrt run.
// Add the servers into TLS.
const
SocketId
server_id
=
server
.
first
;
...
...
@@ -118,10 +129,10 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut*
}
// If too many servers were removed from _db_servers(name service),
// remove these servers from TLS.
if
(
s
->
s
ize
()
+
100
<
tls
.
size
())
{
if
(
s
->
s
erver_list
.
size
()
+
EraseBatchSize
<
tls
.
size
())
{
auto
iter
=
tls
.
begin
();
while
(
iter
!=
tls
.
end
())
{
if
(
s
->
find
(
iter
->
first
)
==
s
->
end
())
{
if
(
s
->
server_map
.
find
(
iter
->
first
)
==
s
->
server_map
.
end
())
{
iter
=
tls
.
erase
(
iter
);
}
else
{
++
iter
;
...
...
@@ -158,8 +169,8 @@ void WeightedRoundRobinLoadBalancer::Describe(
if
(
_db_servers
.
Read
(
&
s
)
!=
0
)
{
os
<<
"fail to read _db_servers"
;
}
else
{
os
<<
"n="
<<
s
->
size
()
<<
':'
;
for
(
const
auto
&
server
:
*
s
)
{
os
<<
"n="
<<
s
->
s
erver_list
.
s
ize
()
<<
':'
;
for
(
const
auto
&
server
:
s
->
server_list
)
{
os
<<
' '
<<
server
.
first
<<
'('
<<
server
.
second
<<
')'
;
}
}
...
...
src/brpc/policy/weighted_round_robin_load_balancer.h
View file @
f3e8790b
...
...
@@ -39,8 +39,12 @@ public:
void
Describe
(
std
::
ostream
&
,
const
DescribeOptions
&
options
);
private
:
// The value is configured weight for each server.
using
Servers
=
std
::
map
<
SocketId
,
int
>
;
struct
Servers
{
// The value is configured weight for each server.
std
::
vector
<
std
::
pair
<
SocketId
,
int
>>
server_list
;
// The value is the index of the server in "server_list".
std
::
map
<
SocketId
,
size_t
>
server_map
;
};
// The value is current weight for a server.
// It will be changed in the selection of servers.
using
TLS
=
std
::
map
<
SocketId
,
int
>
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment