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
c9a9ffa3
Commit
c9a9ffa3
authored
Apr 08, 2019
by
caidaojin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move SetParameters function to New()
parent
f97f4b89
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
42 additions
and
37 deletions
+42
-37
load_balancer.cpp
src/brpc/load_balancer.cpp
+2
-7
load_balancer.h
src/brpc/load_balancer.h
+1
-5
consistent_hashing_load_balancer.cpp
src/brpc/policy/consistent_hashing_load_balancer.cpp
+23
-13
consistent_hashing_load_balancer.h
src/brpc/policy/consistent_hashing_load_balancer.h
+2
-2
dynpart_load_balancer.cpp
src/brpc/policy/dynpart_load_balancer.cpp
+1
-1
dynpart_load_balancer.h
src/brpc/policy/dynpart_load_balancer.h
+1
-1
locality_aware_load_balancer.cpp
src/brpc/policy/locality_aware_load_balancer.cpp
+2
-1
locality_aware_load_balancer.h
src/brpc/policy/locality_aware_load_balancer.h
+1
-1
randomized_load_balancer.cpp
src/brpc/policy/randomized_load_balancer.cpp
+2
-1
randomized_load_balancer.h
src/brpc/policy/randomized_load_balancer.h
+1
-1
round_robin_load_balancer.cpp
src/brpc/policy/round_robin_load_balancer.cpp
+2
-1
round_robin_load_balancer.h
src/brpc/policy/round_robin_load_balancer.h
+1
-1
weighted_round_robin_load_balancer.cpp
src/brpc/policy/weighted_round_robin_load_balancer.cpp
+2
-1
weighted_round_robin_load_balancer.h
src/brpc/policy/weighted_round_robin_load_balancer.h
+1
-1
No files found.
src/brpc/load_balancer.cpp
View file @
c9a9ffa3
...
...
@@ -74,16 +74,11 @@ int SharedLoadBalancer::Init(const char* lb_protocol) {
LOG
(
FATAL
)
<<
"Fail to find LoadBalancer by `"
<<
lb_name
<<
"'"
;
return
-
1
;
}
LoadBalancer
*
lb_copy
=
lb
->
New
(
);
if
(
lb_copy
==
NULL
)
{
LoadBalancer
*
_lb
=
lb
->
New
(
lb_params
);
if
(
_lb
==
NULL
)
{
LOG
(
FATAL
)
<<
"Fail to new LoadBalancer"
;
return
-
1
;
}
_lb
=
lb_copy
;
if
(
!
_lb
->
SetParameters
(
lb_params
))
{
LOG
(
FATAL
)
<<
"Fail to set parameters of lb `"
<<
lb_protocol
<<
"'"
;
return
-
1
;
}
if
(
FLAGS_show_lb_in_vars
&&
!
_exposed
)
{
ExposeLB
();
}
...
...
src/brpc/load_balancer.h
View file @
c9a9ffa3
...
...
@@ -102,11 +102,7 @@ public:
// Create/destroy an instance.
// Caller is responsible for Destroy() the instance after usage.
virtual
LoadBalancer
*
New
()
const
=
0
;
// Config user passed parameters to lb after construction which
// make lb function more flexible.
virtual
bool
SetParameters
(
const
butil
::
StringPiece
&
params
)
{
return
true
;
}
virtual
LoadBalancer
*
New
(
const
butil
::
StringPiece
&
params
)
const
=
0
;
protected
:
virtual
~
LoadBalancer
()
{
}
...
...
src/brpc/policy/consistent_hashing_load_balancer.cpp
View file @
c9a9ffa3
...
...
@@ -31,6 +31,9 @@ namespace policy {
DEFINE_int32
(
chash_num_replicas
,
100
,
"default number of replicas per server in chash"
);
// Defined in hasher.cpp.
const
char
*
GetHashName
(
HashFunc
hasher
);
class
ReplicaPolicy
{
public
:
virtual
~
ReplicaPolicy
()
=
default
;
...
...
@@ -38,6 +41,7 @@ public:
virtual
bool
Build
(
ServerId
server
,
size_t
num_replicas
,
std
::
vector
<
ConsistentHashingLoadBalancer
::
Node
>*
replicas
)
const
=
0
;
virtual
const
char
*
name
()
const
=
0
;
};
class
DefaultReplicaPolicy
:
public
ReplicaPolicy
{
...
...
@@ -47,6 +51,9 @@ public:
virtual
bool
Build
(
ServerId
server
,
size_t
num_replicas
,
std
::
vector
<
ConsistentHashingLoadBalancer
::
Node
>*
replicas
)
const
;
virtual
const
char
*
name
()
const
{
return
GetHashName
(
_hash_func
);
}
private
:
HashFunc
_hash_func
;
};
...
...
@@ -77,6 +84,8 @@ public:
virtual
bool
Build
(
ServerId
server
,
size_t
num_replicas
,
std
::
vector
<
ConsistentHashingLoadBalancer
::
Node
>*
replicas
)
const
;
virtual
const
char
*
name
()
const
{
return
"ketama"
;
}
};
bool
KetamaReplicaPolicy
::
Build
(
ServerId
server
,
...
...
@@ -112,19 +121,14 @@ bool KetamaReplicaPolicy::Build(ServerId server,
namespace
{
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
std
::
array
<
const
ReplicaPolicy
*
,
CONS_HASH_LB_LAST
>
g_replica_policy
=
{
new
DefaultReplicaPolicy
(
MurmurHash32
),
new
DefaultReplicaPolicy
(
MD5Hash32
),
new
KetamaReplicaPolicy
};
inline
const
ReplicaPolicy
*
GetReplicaPolicy
(
ConsistentHashingLoadBalancerType
type
)
{
return
g_replica_policy
.
at
(
type
).
first
;
}
inline
const
std
::
string
&
GetLbName
(
ConsistentHashingLoadBalancerType
type
)
{
return
g_replica_policy
.
at
(
type
).
second
;
return
g_replica_policy
.
at
(
type
);
}
}
// namespace
...
...
@@ -261,8 +265,14 @@ size_t ConsistentHashingLoadBalancer::RemoveServersInBatch(
return
n
;
}
LoadBalancer
*
ConsistentHashingLoadBalancer
::
New
()
const
{
return
new
(
std
::
nothrow
)
ConsistentHashingLoadBalancer
(
_type
);
LoadBalancer
*
ConsistentHashingLoadBalancer
::
New
(
const
butil
::
StringPiece
&
params
)
const
{
ConsistentHashingLoadBalancer
*
lb
=
new
(
std
::
nothrow
)
ConsistentHashingLoadBalancer
(
_type
);
if
(
lb
!=
nullptr
&&
!
lb
->
SetParameters
(
params
))
{
delete
lb
;
lb
=
nullptr
;
}
return
lb
;
}
void
ConsistentHashingLoadBalancer
::
Destroy
()
{
...
...
@@ -313,7 +323,7 @@ void ConsistentHashingLoadBalancer::Describe(
return
;
}
os
<<
"ConsistentHashingLoadBalancer {
\n
"
<<
" hash function: "
<<
GetLbName
(
_type
)
<<
'\n'
<<
" hash function: "
<<
_replicas_policy
->
name
(
)
<<
'\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 @
c9a9ffa3
...
...
@@ -59,11 +59,11 @@ public:
bool
RemoveServer
(
const
ServerId
&
server
);
size_t
AddServersInBatch
(
const
std
::
vector
<
ServerId
>
&
servers
);
size_t
RemoveServersInBatch
(
const
std
::
vector
<
ServerId
>
&
servers
);
LoadBalancer
*
New
()
const
;
LoadBalancer
*
New
(
const
butil
::
StringPiece
&
params
)
const
;
void
Destroy
();
int
SelectServer
(
const
SelectIn
&
in
,
SelectOut
*
out
);
void
Describe
(
std
::
ostream
&
os
,
const
DescribeOptions
&
options
);
virtual
bool
SetParameters
(
const
butil
::
StringPiece
&
params
);
bool
SetParameters
(
const
butil
::
StringPiece
&
params
);
private
:
void
GetLoads
(
std
::
map
<
butil
::
EndPoint
,
double
>
*
load_map
);
...
...
src/brpc/policy/dynpart_load_balancer.cpp
View file @
c9a9ffa3
...
...
@@ -159,7 +159,7 @@ int DynPartLoadBalancer::SelectServer(const SelectIn& in, SelectOut* out) {
return
EHOSTDOWN
;
}
DynPartLoadBalancer
*
DynPartLoadBalancer
::
New
()
const
{
DynPartLoadBalancer
*
DynPartLoadBalancer
::
New
(
const
butil
::
StringPiece
&
)
const
{
return
new
(
std
::
nothrow
)
DynPartLoadBalancer
;
}
...
...
src/brpc/policy/dynpart_load_balancer.h
View file @
c9a9ffa3
...
...
@@ -36,7 +36,7 @@ public:
size_t
AddServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
size_t
RemoveServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
int
SelectServer
(
const
SelectIn
&
in
,
SelectOut
*
out
);
DynPartLoadBalancer
*
New
()
const
;
DynPartLoadBalancer
*
New
(
const
butil
::
StringPiece
&
)
const
;
void
Destroy
();
void
Describe
(
std
::
ostream
&
,
const
DescribeOptions
&
options
);
...
...
src/brpc/policy/locality_aware_load_balancer.cpp
View file @
c9a9ffa3
...
...
@@ -460,7 +460,8 @@ int64_t LocalityAwareLoadBalancer::Weight::Update(
return
ResetWeight
(
index
,
end_time_us
);
}
LocalityAwareLoadBalancer
*
LocalityAwareLoadBalancer
::
New
()
const
{
LocalityAwareLoadBalancer
*
LocalityAwareLoadBalancer
::
New
(
const
butil
::
StringPiece
&
)
const
{
return
new
(
std
::
nothrow
)
LocalityAwareLoadBalancer
;
}
...
...
src/brpc/policy/locality_aware_load_balancer.h
View file @
c9a9ffa3
...
...
@@ -44,7 +44,7 @@ public:
bool
RemoveServer
(
const
ServerId
&
id
);
size_t
AddServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
size_t
RemoveServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
LocalityAwareLoadBalancer
*
New
()
const
;
LocalityAwareLoadBalancer
*
New
(
const
butil
::
StringPiece
&
)
const
;
void
Destroy
();
int
SelectServer
(
const
SelectIn
&
in
,
SelectOut
*
out
);
void
Feedback
(
const
CallInfo
&
info
);
...
...
src/brpc/policy/randomized_load_balancer.cpp
View file @
c9a9ffa3
...
...
@@ -134,7 +134,8 @@ int RandomizedLoadBalancer::SelectServer(const SelectIn& in, SelectOut* out) {
return
EHOSTDOWN
;
}
RandomizedLoadBalancer
*
RandomizedLoadBalancer
::
New
()
const
{
RandomizedLoadBalancer
*
RandomizedLoadBalancer
::
New
(
const
butil
::
StringPiece
&
)
const
{
return
new
(
std
::
nothrow
)
RandomizedLoadBalancer
;
}
...
...
src/brpc/policy/randomized_load_balancer.h
View file @
c9a9ffa3
...
...
@@ -36,7 +36,7 @@ public:
size_t
AddServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
size_t
RemoveServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
int
SelectServer
(
const
SelectIn
&
in
,
SelectOut
*
out
);
RandomizedLoadBalancer
*
New
()
const
;
RandomizedLoadBalancer
*
New
(
const
butil
::
StringPiece
&
)
const
;
void
Destroy
();
void
Describe
(
std
::
ostream
&
os
,
const
DescribeOptions
&
);
...
...
src/brpc/policy/round_robin_load_balancer.cpp
View file @
c9a9ffa3
...
...
@@ -131,7 +131,8 @@ int RoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut* out) {
return
EHOSTDOWN
;
}
RoundRobinLoadBalancer
*
RoundRobinLoadBalancer
::
New
()
const
{
RoundRobinLoadBalancer
*
RoundRobinLoadBalancer
::
New
(
const
butil
::
StringPiece
&
)
const
{
return
new
(
std
::
nothrow
)
RoundRobinLoadBalancer
;
}
...
...
src/brpc/policy/round_robin_load_balancer.h
View file @
c9a9ffa3
...
...
@@ -35,7 +35,7 @@ public:
size_t
AddServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
size_t
RemoveServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
int
SelectServer
(
const
SelectIn
&
in
,
SelectOut
*
out
);
RoundRobinLoadBalancer
*
New
()
const
;
RoundRobinLoadBalancer
*
New
(
const
butil
::
StringPiece
&
)
const
;
void
Destroy
();
void
Describe
(
std
::
ostream
&
,
const
DescribeOptions
&
options
);
...
...
src/brpc/policy/weighted_round_robin_load_balancer.cpp
View file @
c9a9ffa3
...
...
@@ -213,7 +213,8 @@ SocketId WeightedRoundRobinLoadBalancer::GetServerInNextStride(
return
final_server
;
}
LoadBalancer
*
WeightedRoundRobinLoadBalancer
::
New
()
const
{
LoadBalancer
*
WeightedRoundRobinLoadBalancer
::
New
(
const
butil
::
StringPiece
&
)
const
{
return
new
(
std
::
nothrow
)
WeightedRoundRobinLoadBalancer
;
}
...
...
src/brpc/policy/weighted_round_robin_load_balancer.h
View file @
c9a9ffa3
...
...
@@ -34,7 +34,7 @@ public:
size_t
AddServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
size_t
RemoveServersInBatch
(
const
std
::
vector
<
ServerId
>&
servers
);
int
SelectServer
(
const
SelectIn
&
in
,
SelectOut
*
out
);
LoadBalancer
*
New
()
const
;
LoadBalancer
*
New
(
const
butil
::
StringPiece
&
)
const
;
void
Destroy
();
void
Describe
(
std
::
ostream
&
,
const
DescribeOptions
&
options
);
...
...
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