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
3d7b5476
Commit
3d7b5476
authored
Apr 08, 2019
by
cdjgit
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix for ci comments
parent
dcc7e003
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
25 deletions
+43
-25
global.cpp
src/brpc/global.cpp
+3
-3
consistent_hashing_load_balancer.cpp
src/brpc/policy/consistent_hashing_load_balancer.cpp
+18
-16
consistent_hashing_load_balancer.h
src/brpc/policy/consistent_hashing_load_balancer.h
+11
-2
brpc_load_balancer_unittest.cpp
test/brpc_load_balancer_unittest.cpp
+11
-4
No files found.
src/brpc/global.cpp
View file @
3d7b5476
...
...
@@ -108,9 +108,9 @@ const char* const DUMMY_SERVER_PORT_FILE = "dummy_server.port";
struct
GlobalExtensions
{
GlobalExtensions
()
:
ch_mh_lb
(
"murmurhash3"
)
,
ch_md5_lb
(
"md5"
)
,
ch_ketama_lb
(
"ketama"
)
:
ch_mh_lb
(
CONS_HASH_LB_MURMUR3
)
,
ch_md5_lb
(
CONS_HASH_LB_MD5
)
,
ch_ketama_lb
(
CONS_HASH_LB_KETAMA
)
,
constant_cl
(
0
)
{
}
...
...
src/brpc/policy/consistent_hashing_load_balancer.cpp
View file @
3d7b5476
...
...
@@ -112,27 +112,29 @@ bool KetamaReplicaPolicy::Build(ServerId server,
namespace
{
const
std
::
map
<
std
::
string
,
const
ReplicaPolicy
*>
g_replica_policy_map
=
{
{
"murmurhash3"
,
new
DefaultReplicaPolicy
(
MurmurHash32
)},
{
"md5"
,
new
DefaultReplicaPolicy
(
MD5Hash32
)},
{
"ketama"
,
new
KetamaReplicaPolicy
}
const
std
::
array
<
std
::
pair
<
const
ReplicaPolicy
*
,
std
::
string
>
,
CONS_HASH_LB_LAST
>
g_replica_policy
=
{
std
::
make_pair
(
new
DefaultReplicaPolicy
(
MurmurHash32
),
"murmurhash3"
),
std
::
make_pair
(
new
DefaultReplicaPolicy
(
MD5Hash32
),
"md5"
),
std
::
make_pair
(
new
KetamaReplicaPolicy
,
"ketama"
)
};
const
ReplicaPolicy
*
GetReplicaPolicy
(
const
std
::
string
&
nam
e
)
{
auto
iter
=
g_replica_policy_map
.
find
(
name
)
;
if
(
iter
!=
g_replica_policy_map
.
end
())
{
return
iter
->
second
;
}
return
nullptr
;
inline
const
ReplicaPolicy
*
GetReplicaPolicy
(
ConsistentHashingLoadBalancerType
typ
e
)
{
return
g_replica_policy
.
at
(
type
).
first
;
}
inline
const
std
::
string
&
GetLbName
(
ConsistentHashingLoadBalancerType
type
)
{
return
g_replica_policy
.
at
(
type
).
second
;
}
}
// namespace
ConsistentHashingLoadBalancer
::
ConsistentHashingLoadBalancer
(
const
char
*
name
)
:
_num_replicas
(
FLAGS_chash_num_replicas
),
_name
(
name
)
{
_replicas_policy
=
GetReplicaPolicy
(
name
);
ConsistentHashingLoadBalancer
::
ConsistentHashingLoadBalancer
(
ConsistentHashingLoadBalancerType
type
)
:
_num_replicas
(
FLAGS_chash_num_replicas
),
_type
(
type
)
{
_replicas_policy
=
GetReplicaPolicy
(
_type
);
CHECK
(
_replicas_policy
)
<<
"Fail to find replica policy for consistency lb
: '"
<<
nam
e
<<
'\''
;
<<
"Fail to find replica policy for consistency lb
type: '"
<<
typ
e
<<
'\''
;
}
size_t
ConsistentHashingLoadBalancer
::
AddBatch
(
...
...
@@ -260,7 +262,7 @@ size_t ConsistentHashingLoadBalancer::RemoveServersInBatch(
}
LoadBalancer
*
ConsistentHashingLoadBalancer
::
New
()
const
{
return
new
(
std
::
nothrow
)
ConsistentHashingLoadBalancer
(
_
name
.
c_str
()
);
return
new
(
std
::
nothrow
)
ConsistentHashingLoadBalancer
(
_
type
);
}
void
ConsistentHashingLoadBalancer
::
Destroy
()
{
...
...
@@ -311,7 +313,7 @@ void ConsistentHashingLoadBalancer::Describe(
return
;
}
os
<<
"ConsistentHashingLoadBalancer {
\n
"
<<
" hash function: "
<<
_name
<<
'\n'
<<
" hash function: "
<<
GetLbName
(
_type
)
<<
'\n'
<<
" replica per host: "
<<
_num_replicas
<<
'\n'
;
std
::
map
<
butil
::
EndPoint
,
double
>
load_map
;
GetLoads
(
&
load_map
);
...
...
src/brpc/policy/consistent_hashing_load_balancer.h
View file @
3d7b5476
...
...
@@ -30,6 +30,15 @@ namespace policy {
class
ReplicaPolicy
;
enum
ConsistentHashingLoadBalancerType
{
CONS_HASH_LB_MURMUR3
=
0
,
CONS_HASH_LB_MD5
=
1
,
CONS_HASH_LB_KETAMA
=
2
,
// Identify the last one.
CONS_HASH_LB_LAST
=
3
};
class
ConsistentHashingLoadBalancer
:
public
LoadBalancer
{
public
:
struct
Node
{
...
...
@@ -45,7 +54,7 @@ public:
return
hash
<
code
;
}
};
explicit
ConsistentHashingLoadBalancer
(
const
char
*
nam
e
);
explicit
ConsistentHashingLoadBalancer
(
ConsistentHashingLoadBalancerType
typ
e
);
bool
AddServer
(
const
ServerId
&
server
);
bool
RemoveServer
(
const
ServerId
&
server
);
size_t
AddServersInBatch
(
const
std
::
vector
<
ServerId
>
&
servers
);
...
...
@@ -66,7 +75,7 @@ private:
const
ServerId
&
server
,
bool
*
executed
);
const
ReplicaPolicy
*
_replicas_policy
;
size_t
_num_replicas
;
std
::
string
_nam
e
;
ConsistentHashingLoadBalancerType
_typ
e
;
butil
::
DoublyBufferedData
<
std
::
vector
<
Node
>
>
_db_hash_ring
;
};
...
...
test/brpc_load_balancer_unittest.cpp
View file @
3d7b5476
...
...
@@ -251,7 +251,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
}
else
if
(
round
==
3
)
{
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
}
else
{
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash3"
);
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
brpc
::
policy
::
CONS_HASH_LB_MURMUR3
);
sa
.
hash
=
::
brpc
::
policy
::
MurmurHash32
;
}
sa
.
lb
=
lb
;
...
...
@@ -364,7 +364,7 @@ TEST_F(LoadBalancerTest, fairness) {
}
else
if
(
3
==
round
||
4
==
round
)
{
lb
=
new
brpc
::
policy
::
WeightedRoundRobinLoadBalancer
;
}
else
{
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
"murmurhash3"
);
lb
=
new
brpc
::
policy
::
ConsistentHashingLoadBalancer
(
brpc
::
policy
::
CONS_HASH_LB_MURMUR3
);
sa
.
hash
=
brpc
::
policy
::
MurmurHash32
;
}
sa
.
lb
=
lb
;
...
...
@@ -485,12 +485,19 @@ TEST_F(LoadBalancerTest, fairness) {
}
TEST_F
(
LoadBalancerTest
,
consistent_hashing
)
{
::
brpc
::
policy
::
HashFunc
hashs
[]
=
{
::
brpc
::
policy
::
HashFunc
hashs
[
CONS_HASH_LB_LAST
]
=
{
::
brpc
::
policy
::
MurmurHash32
,
::
brpc
::
policy
::
MD5Hash32
,
::
brpc
::
policy
::
KetamaHash
// ::brpc::policy::CRCHash32 crc is a bad hash function in test
};
::
brpc
::
policy
::
ConsistentHashingLoadBalancerType
hash_type
[
CONS_HASH_LB_LAST
]
=
{
::
brpc
::
policy
::
CONS_HASH_LB_MURMUR3
,
::
brpc
::
policy
::
CONS_HASH_LB_MD5
,
::
brpc
::
policy
::
CONS_HASH_LB_KETAMA
};
const
char
*
servers
[]
=
{
"10.92.115.19:8833"
,
"10.42.108.25:8833"
,
...
...
@@ -499,7 +506,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
(
brpc
::
policy
::
GetHashName
(
hashs
[
round
])
);
brpc
::
policy
::
ConsistentHashingLoadBalancer
chlb
(
hash_type
[
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