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
cc795ae5
Commit
cc795ae5
authored
Dec 11, 2018
by
caidaojin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fix && unitest fix
parent
50eed9b0
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
22 additions
and
13 deletions
+22
-13
global.cpp
src/brpc/global.cpp
+1
-1
consistent_hashing_load_balancer.cpp
src/brpc/policy/consistent_hashing_load_balancer.cpp
+1
-3
consistent_hashing_load_balancer.h
src/brpc/policy/consistent_hashing_load_balancer.h
+1
-0
hasher.cpp
src/brpc/policy/hasher.cpp
+10
-2
hasher.h
src/brpc/policy/hasher.h
+3
-1
brpc_load_balancer_unittest.cpp
test/brpc_load_balancer_unittest.cpp
+6
-6
No files found.
src/brpc/global.cpp
View file @
cc795ae5
...
...
@@ -108,7 +108,7 @@ const char* const DUMMY_SERVER_PORT_FILE = "dummy_server.port";
struct
GlobalExtensions
{
GlobalExtensions
()
:
ch_mh_lb
(
"murmurhash3"
)
:
ch_mh_lb
(
"murmurhash3
2
"
)
,
ch_md5_lb
(
"md5"
)
,
ch_ketama_lb
(
"ketama"
)
,
constant_cl
(
0
)
{
...
...
src/brpc/policy/consistent_hashing_load_balancer.cpp
View file @
cc795ae5
...
...
@@ -33,11 +33,9 @@ DEFINE_int32(chash_num_replicas, 100,
namespace
{
using
HashFun
=
uint32_t
(
*
)(
const
void
*
,
size_t
);
bool
BuildReplicasDefault
(
const
ServerId
server
,
const
size_t
num_replicas
,
HashFun
hash
,
ConsistentHashingLoadBalancer
::
HashFunc
hash
,
std
::
vector
<
ConsistentHashingLoadBalancer
::
Node
>*
replicas
)
{
SocketUniquePtr
ptr
;
if
(
Socket
::
AddressFailedAsWell
(
server
.
id
,
&
ptr
)
==
-
1
)
{
...
...
src/brpc/policy/consistent_hashing_load_balancer.h
View file @
cc795ae5
...
...
@@ -43,6 +43,7 @@ public:
return
hash
<
code
;
}
};
using
HashFun
=
uint32_t
(
*
)(
const
void
*
,
size_t
);
using
BuildReplicasFunc
=
std
::
function
<
bool
(
const
ServerId
server
,
const
size_t
num_replicas
,
std
::
vector
<
Node
>*
replicas
)
>
;
explicit
ConsistentHashingLoadBalancer
(
const
char
*
name
);
...
...
src/brpc/policy/hasher.cpp
View file @
cc795ae5
...
...
@@ -23,7 +23,7 @@
namespace
brpc
{
namespace
policy
{
uint32_t
MD5HashSignature
(
const
void
*
key
,
size_t
len
,
unsigned
char
*
results
)
{
void
MD5HashSignature
(
const
void
*
key
,
size_t
len
,
unsigned
char
*
results
)
{
MD5_CTX
my_md5
;
MD5_Init
(
&
my_md5
);
MD5_Update
(
&
my_md5
,
(
const
unsigned
char
*
)
key
,
len
);
...
...
@@ -39,6 +39,10 @@ uint32_t MD5Hash32(const void* key, size_t len) {
|
(
results
[
0
]
&
0xFF
);
}
uint32_t
KetamaHash
(
const
void
*
key
,
size_t
len
)
{
return
MD5Hash32
(
key
,
len
);
}
uint32_t
MD5Hash32V
(
const
butil
::
StringPiece
*
keys
,
size_t
num_keys
)
{
MD5_CTX
ctx
;
MD5_Init
(
&
ctx
);
...
...
@@ -153,7 +157,7 @@ uint32_t CRCHash32(const void* key, size_t len) {
const
char
*
GetHashName
(
uint32_t
(
*
hasher
)(
const
void
*
key
,
size_t
len
))
{
if
(
hasher
==
MurmurHash32
)
{
return
"murmurhash3"
;
return
"murmurhash3
2
"
;
}
if
(
hasher
==
MD5Hash32
)
{
return
"md5"
;
...
...
@@ -161,6 +165,10 @@ const char *GetHashName(uint32_t (*hasher)(const void* key, size_t len)) {
if
(
hasher
==
CRCHash32
)
{
return
"crc32"
;
}
if
(
hasher
==
KetamaHash
)
{
return
"ketama"
;
}
return
"user_defined"
;
}
...
...
src/brpc/policy/hasher.h
View file @
cc795ae5
...
...
@@ -25,13 +25,15 @@
namespace
brpc
{
namespace
policy
{
uint32_t
MD5HashSignature
(
const
void
*
key
,
size_t
len
,
unsigned
char
*
results
);
void
MD5HashSignature
(
const
void
*
key
,
size_t
len
,
unsigned
char
*
results
);
uint32_t
MD5Hash32
(
const
void
*
key
,
size_t
len
);
uint32_t
MD5Hash32V
(
const
butil
::
StringPiece
*
keys
,
size_t
num_keys
);
uint32_t
MurmurHash32
(
const
void
*
key
,
size_t
len
);
uint32_t
MurmurHash32V
(
const
butil
::
StringPiece
*
keys
,
size_t
num_keys
);
uint32_t
KetamaHash
(
const
void
*
key
,
size_t
len
);
}
// namespace policy
}
// namespace brpc
...
...
test/brpc_load_balancer_unittest.cpp
View file @
cc795ae5
...
...
@@ -25,6 +25,7 @@
namespace
brpc
{
namespace
policy
{
extern
uint32_t
CRCHash32
(
const
char
*
key
,
size_t
len
);
extern
const
char
*
GetHashName
(
uint32_t
(
*
hasher
)(
const
void
*
key
,
size_t
len
));
}}
namespace
{
...
...
@@ -250,8 +251,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
}
else
if
(
round
==
3
)
{
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
}
else
{
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
::
brpc
::
policy
::
MurmurHash32
);
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash32"
);
sa
.
hash
=
::
brpc
::
policy
::
MurmurHash32
;
}
sa
.
lb
=
lb
;
...
...
@@ -364,8 +364,7 @@ TEST_F(LoadBalancerTest, fairness) {
}
else
if
(
3
==
round
||
4
==
round
)
{
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
}
else
{
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
brpc
::
policy
::
MurmurHash32
);
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash32"
);
sa
.
hash
=
brpc
::
policy
::
MurmurHash32
;
}
sa
.
lb
=
lb
;
...
...
@@ -488,7 +487,8 @@ TEST_F(LoadBalancerTest, fairness) {
TEST_F
(
LoadBalancerTest
,
consistent_hashing
)
{
::
brpc
::
policy
::
ConsistentHashingLoadBalancer
::
HashFunc
hashs
[]
=
{
::
brpc
::
policy
::
MurmurHash32
,
::
brpc
::
policy
::
MD5Hash32
::
brpc
::
policy
::
MD5Hash32
,
::
brpc
::
policy
::
KetamaHash
// ::brpc::policy::CRCHash32 crc is a bad hash function in test
};
const
char
*
servers
[]
=
{
...
...
@@ -499,7 +499,7 @@ TEST_F(LoadBalancerTest, consistent_hashing) {
"10.42.122.201:8833"
,
};
for
(
size_t
round
=
0
;
round
<
ARRAY_SIZE
(
hashs
);
++
round
)
{
brpc
::
policy
::
ConsistentHashingLoadBalancer
chlb
(
hashs
[
round
]
);
brpc
::
policy
::
ConsistentHashingLoadBalancer
chlb
(
brpc
::
policy
::
GetHashName
(
hashs
[
round
])
);
std
::
vector
<
brpc
::
ServerId
>
ids
;
std
::
vector
<
butil
::
EndPoint
>
addrs
;
for
(
int
j
=
0
;
j
<
5
;
++
j
)
...
...
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