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
2232e8af
Commit
2232e8af
authored
Dec 12, 2018
by
cdjgit
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bugs fix
parent
cc795ae5
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
35 additions
and
23 deletions
+35
-23
global.cpp
src/brpc/global.cpp
+1
-1
load_balancer.cpp
src/brpc/load_balancer.cpp
+18
-7
load_balancer.h
src/brpc/load_balancer.h
+3
-2
consistent_hashing_load_balancer.cpp
src/brpc/policy/consistent_hashing_load_balancer.cpp
+9
-9
consistent_hashing_load_balancer.h
src/brpc/policy/consistent_hashing_load_balancer.h
+1
-1
hasher.cpp
src/brpc/policy/hasher.cpp
+1
-1
brpc_load_balancer_unittest.cpp
test/brpc_load_balancer_unittest.cpp
+2
-2
No files found.
src/brpc/global.cpp
View file @
2232e8af
...
@@ -108,7 +108,7 @@ const char* const DUMMY_SERVER_PORT_FILE = "dummy_server.port";
...
@@ -108,7 +108,7 @@ const char* const DUMMY_SERVER_PORT_FILE = "dummy_server.port";
struct
GlobalExtensions
{
struct
GlobalExtensions
{
GlobalExtensions
()
GlobalExtensions
()
:
ch_mh_lb
(
"murmurhash3
2
"
)
:
ch_mh_lb
(
"murmurhash3"
)
,
ch_md5_lb
(
"md5"
)
,
ch_md5_lb
(
"md5"
)
,
ch_ketama_lb
(
"ketama"
)
,
ch_ketama_lb
(
"ketama"
)
,
constant_cl
(
0
)
{
,
constant_cl
(
0
)
{
...
...
src/brpc/load_balancer.cpp
View file @
2232e8af
...
@@ -65,7 +65,10 @@ SharedLoadBalancer::~SharedLoadBalancer() {
...
@@ -65,7 +65,10 @@ SharedLoadBalancer::~SharedLoadBalancer() {
int
SharedLoadBalancer
::
Init
(
const
char
*
lb_protocol
)
{
int
SharedLoadBalancer
::
Init
(
const
char
*
lb_protocol
)
{
std
::
string
lb_name
;
std
::
string
lb_name
;
butil
::
StringPairs
lb_parms
;
butil
::
StringPairs
lb_parms
;
ParseParameters
(
lb_protocol
,
&
lb_name
,
&
lb_parms
);
if
(
!
ParseParameters
(
lb_protocol
,
&
lb_name
,
&
lb_parms
))
{
LOG
(
FATAL
)
<<
"Fail to parse this load balancer protocol '"
<<
lb_protocol
<<
'\''
;
return
-
1
;
}
const
LoadBalancer
*
lb
=
LoadBalancerExtension
()
->
Find
(
lb_name
.
c_str
());
const
LoadBalancer
*
lb
=
LoadBalancerExtension
()
->
Find
(
lb_name
.
c_str
());
if
(
lb
==
NULL
)
{
if
(
lb
==
NULL
)
{
LOG
(
FATAL
)
<<
"Fail to find LoadBalancer by `"
<<
lb_name
<<
"'"
;
LOG
(
FATAL
)
<<
"Fail to find LoadBalancer by `"
<<
lb_name
<<
"'"
;
...
@@ -96,20 +99,28 @@ void SharedLoadBalancer::Describe(std::ostream& os,
...
@@ -96,20 +99,28 @@ void SharedLoadBalancer::Describe(std::ostream& os,
}
}
}
}
void
SharedLoadBalancer
::
ParseParameters
(
const
butil
::
StringPiece
lb_protocol
,
bool
SharedLoadBalancer
::
ParseParameters
(
const
butil
::
StringPiece
&
lb_protocol
,
std
::
string
*
lb_name
,
std
::
string
*
lb_name
,
butil
::
StringPairs
*
par
ms
)
{
butil
::
StringPairs
*
lb_para
ms
)
{
lb_name
->
clear
();
lb_name
->
clear
();
parms
->
clear
();
lb_params
->
clear
();
if
(
lb_protocol
.
empty
())
{
return
false
;
}
size_t
pos
=
lb_protocol
.
find
(
':'
);
size_t
pos
=
lb_protocol
.
find
(
':'
);
if
(
pos
==
std
::
string
::
npos
)
{
if
(
pos
==
std
::
string
::
npos
)
{
lb_name
->
append
(
lb_protocol
.
data
(),
lb_protocol
.
size
());
lb_name
->
append
(
lb_protocol
.
data
(),
lb_protocol
.
size
());
}
else
{
}
else
{
lb_name
->
append
(
lb_protocol
.
data
(),
pos
);
lb_name
->
append
(
lb_protocol
.
data
(),
pos
);
butil
::
StringPiece
parms_piece
=
lb_protocol
.
substr
(
pos
+
sizeof
(
':'
));
butil
::
StringPiece
params_piece
=
lb_protocol
.
substr
(
pos
+
sizeof
(
':'
));
std
::
string
parms_str
(
parms_piece
.
data
(),
parms_piece
.
size
());
std
::
string
params_str
(
params_piece
.
data
(),
params_piece
.
size
());
butil
::
SplitStringIntoKeyValuePairs
(
parms_str
,
'='
,
' '
,
parms
);
if
(
!
butil
::
SplitStringIntoKeyValuePairs
(
params_str
,
'='
,
' '
,
lb_params
))
{
lb_params
->
clear
();
return
false
;
}
}
}
return
true
;
}
}
}
// namespace brpc
}
// namespace brpc
src/brpc/load_balancer.h
View file @
2232e8af
...
@@ -104,6 +104,7 @@ public:
...
@@ -104,6 +104,7 @@ public:
// Caller is responsible for Destroy() the instance after usage.
// Caller is responsible for Destroy() the instance after usage.
virtual
LoadBalancer
*
New
()
const
=
0
;
virtual
LoadBalancer
*
New
()
const
=
0
;
// Set other
virtual
bool
SetParameters
(
const
butil
::
StringPairs
&
parms
)
{
return
true
;
}
virtual
bool
SetParameters
(
const
butil
::
StringPairs
&
parms
)
{
return
true
;
}
protected
:
protected
:
...
@@ -168,9 +169,9 @@ public:
...
@@ -168,9 +169,9 @@ public:
}
}
private
:
private
:
static
void
ParseParameters
(
const
butil
::
StringPiece
lb_protocl
,
static
bool
ParseParameters
(
const
butil
::
StringPiece
&
lb_protocol
,
std
::
string
*
lb_name
,
std
::
string
*
lb_name
,
butil
::
StringPairs
*
par
ms
);
butil
::
StringPairs
*
lb_para
ms
);
static
void
DescribeLB
(
std
::
ostream
&
os
,
void
*
arg
);
static
void
DescribeLB
(
std
::
ostream
&
os
,
void
*
arg
);
void
ExposeLB
();
void
ExposeLB
();
...
...
src/brpc/policy/consistent_hashing_load_balancer.cpp
View file @
2232e8af
...
@@ -94,7 +94,7 @@ ConsistentHashingLoadBalancer::ConsistentHashingLoadBalancer(const char* name)
...
@@ -94,7 +94,7 @@ ConsistentHashingLoadBalancer::ConsistentHashingLoadBalancer(const char* name)
}
}
void
ConsistentHashingLoadBalancer
::
Init
(
const
std
::
string
&
name
)
{
void
ConsistentHashingLoadBalancer
::
Init
(
const
std
::
string
&
name
)
{
if
(
name
.
compare
(
"murmurhash3"
)
==
0
)
{
if
(
name
==
"murmurhash3"
)
{
_build_replicas
=
std
::
bind
(
BuildReplicasDefault
,
_build_replicas
=
std
::
bind
(
BuildReplicasDefault
,
std
::
placeholders
::
_1
,
std
::
placeholders
::
_1
,
std
::
placeholders
::
_2
,
std
::
placeholders
::
_2
,
...
@@ -102,7 +102,7 @@ void ConsistentHashingLoadBalancer::Init(const std::string& name) {
...
@@ -102,7 +102,7 @@ void ConsistentHashingLoadBalancer::Init(const std::string& name) {
std
::
placeholders
::
_3
);
std
::
placeholders
::
_3
);
return
;
return
;
}
}
if
(
name
.
compare
(
"md5"
)
==
0
)
{
if
(
name
==
"md5"
)
{
_build_replicas
=
std
::
bind
(
BuildReplicasDefault
,
_build_replicas
=
std
::
bind
(
BuildReplicasDefault
,
std
::
placeholders
::
_1
,
std
::
placeholders
::
_1
,
std
::
placeholders
::
_2
,
std
::
placeholders
::
_2
,
...
@@ -110,7 +110,7 @@ void ConsistentHashingLoadBalancer::Init(const std::string& name) {
...
@@ -110,7 +110,7 @@ void ConsistentHashingLoadBalancer::Init(const std::string& name) {
std
::
placeholders
::
_3
);
std
::
placeholders
::
_3
);
return
;
return
;
}
}
if
(
name
.
compare
(
"ketama"
)
==
0
)
{
if
(
name
==
"ketama"
)
{
_build_replicas
=
BuildReplicasKetam
;
_build_replicas
=
BuildReplicasKetam
;
return
;
return
;
}
}
...
@@ -341,18 +341,18 @@ void ConsistentHashingLoadBalancer::GetLoads(
...
@@ -341,18 +341,18 @@ void ConsistentHashingLoadBalancer::GetLoads(
}
}
}
}
bool
ConsistentHashingLoadBalancer
::
SetParameters
(
const
butil
::
StringPairs
&
parms
)
{
bool
ConsistentHashingLoadBalancer
::
SetParameters
(
const
butil
::
StringPairs
&
par
a
ms
)
{
for
(
const
std
::
pair
<
std
::
string
,
std
::
string
>&
par
m
:
par
ms
)
{
for
(
const
std
::
pair
<
std
::
string
,
std
::
string
>&
par
am
:
para
ms
)
{
if
(
par
m
.
first
.
compare
(
"replicas"
)
==
0
)
{
if
(
par
am
.
first
==
"replicas"
)
{
size_t
replicas
=
0
;
size_t
replicas
=
0
;
if
(
butil
::
StringToSizeT
(
parm
.
second
,
&
replicas
))
{
if
(
butil
::
StringToSizeT
(
par
a
m
.
second
,
&
replicas
))
{
_num_replicas
=
replicas
;
_num_replicas
=
replicas
;
}
else
{
}
else
{
return
false
;
return
false
;
}
}
}
else
{
continue
;
return
false
;
}
}
LOG
(
ERROR
)
<<
"Failed to set this unknown parameters "
<<
param
.
first
<<
'='
<<
param
.
second
;
}
}
return
true
;
return
true
;
...
...
src/brpc/policy/consistent_hashing_load_balancer.h
View file @
2232e8af
...
@@ -43,7 +43,7 @@ public:
...
@@ -43,7 +43,7 @@ public:
return
hash
<
code
;
return
hash
<
code
;
}
}
};
};
using
HashFun
=
uint32_t
(
*
)(
const
void
*
,
size_t
);
using
HashFun
c
=
uint32_t
(
*
)(
const
void
*
,
size_t
);
using
BuildReplicasFunc
=
using
BuildReplicasFunc
=
std
::
function
<
bool
(
const
ServerId
server
,
const
size_t
num_replicas
,
std
::
vector
<
Node
>*
replicas
)
>
;
std
::
function
<
bool
(
const
ServerId
server
,
const
size_t
num_replicas
,
std
::
vector
<
Node
>*
replicas
)
>
;
explicit
ConsistentHashingLoadBalancer
(
const
char
*
name
);
explicit
ConsistentHashingLoadBalancer
(
const
char
*
name
);
...
...
src/brpc/policy/hasher.cpp
View file @
2232e8af
...
@@ -157,7 +157,7 @@ uint32_t CRCHash32(const void* key, size_t len) {
...
@@ -157,7 +157,7 @@ uint32_t CRCHash32(const void* key, size_t len) {
const
char
*
GetHashName
(
uint32_t
(
*
hasher
)(
const
void
*
key
,
size_t
len
))
{
const
char
*
GetHashName
(
uint32_t
(
*
hasher
)(
const
void
*
key
,
size_t
len
))
{
if
(
hasher
==
MurmurHash32
)
{
if
(
hasher
==
MurmurHash32
)
{
return
"murmurhash3
2
"
;
return
"murmurhash3"
;
}
}
if
(
hasher
==
MD5Hash32
)
{
if
(
hasher
==
MD5Hash32
)
{
return
"md5"
;
return
"md5"
;
...
...
test/brpc_load_balancer_unittest.cpp
View file @
2232e8af
...
@@ -251,7 +251,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
...
@@ -251,7 +251,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
}
else
if
(
round
==
3
)
{
}
else
if
(
round
==
3
)
{
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
}
else
{
}
else
{
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash3
2
"
);
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash3"
);
sa
.
hash
=
::
brpc
::
policy
::
MurmurHash32
;
sa
.
hash
=
::
brpc
::
policy
::
MurmurHash32
;
}
}
sa
.
lb
=
lb
;
sa
.
lb
=
lb
;
...
@@ -364,7 +364,7 @@ TEST_F(LoadBalancerTest, fairness) {
...
@@ -364,7 +364,7 @@ TEST_F(LoadBalancerTest, fairness) {
}
else
if
(
3
==
round
||
4
==
round
)
{
}
else
if
(
3
==
round
||
4
==
round
)
{
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
}
else
{
}
else
{
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash3
2
"
);
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash3"
);
sa
.
hash
=
brpc
::
policy
::
MurmurHash32
;
sa
.
hash
=
brpc
::
policy
::
MurmurHash32
;
}
}
sa
.
lb
=
lb
;
sa
.
lb
=
lb
;
...
...
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