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
6cb4475a
Commit
6cb4475a
authored
6 years ago
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
revived_from_all_failed: optimize get availble server process
parent
890679ec
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
14 deletions
+33
-14
revive_policy.cpp
src/brpc/revive_policy.cpp
+26
-14
revive_policy.h
src/brpc/revive_policy.h
+5
-0
brpc_load_balancer_unittest.cpp
test/brpc_load_balancer_unittest.cpp
+2
-0
No files found.
src/brpc/revive_policy.cpp
View file @
6cb4475a
...
...
@@ -15,6 +15,7 @@
// Authors: Jiashun Zhu(zhujiashun@bilibili.com)
#include <vector>
#include <gflags/gflags.h>
#include "brpc/revive_policy.h"
#include "butil/scoped_lock.h"
#include "butil/synchronization/lock.h"
...
...
@@ -25,14 +26,18 @@
namespace
brpc
{
DEFINE_int64
(
detect_available_server_interval_ms
,
10
,
"The interval "
"to detect available server count in DefaultRevivePolicy"
);
DefaultRevivePolicy
::
DefaultRevivePolicy
(
int64_t
minimum_working_instances
,
int64_t
hold_time_ms
)
:
_reviving
(
false
)
,
_minimum_working_instances
(
minimum_working_instances
)
,
_last_usable
(
0
)
,
_last_usable_change_time_ms
(
0
)
,
_hold_time_ms
(
hold_time_ms
)
{
}
,
_hold_time_ms
(
hold_time_ms
)
,
_usable_cache
(
0
)
,
_usable_cache_time_ms
(
0
)
{
}
void
DefaultRevivePolicy
::
StartReviving
()
{
std
::
unique_lock
<
butil
::
Mutex
>
mu
(
_mutex
);
...
...
@@ -41,12 +46,11 @@ void DefaultRevivePolicy::StartReviving() {
bool
DefaultRevivePolicy
::
StopRevivingIfNecessary
()
{
int64_t
now_ms
=
butil
::
gettimeofday_ms
();
{
std
::
unique_lock
<
butil
::
Mutex
>
mu
(
_mutex
);
if
(
!
_reviving
)
{
mu
.
unlock
();
return
false
;
}
{
std
::
unique_lock
<
butil
::
Mutex
>
mu
(
_mutex
);
if
(
_last_usable_change_time_ms
!=
0
&&
_last_usable
!=
0
&&
(
now_ms
-
_last_usable_change_time_ms
>
_hold_time_ms
))
{
_reviving
=
false
;
...
...
@@ -58,25 +62,33 @@ bool DefaultRevivePolicy::StopRevivingIfNecessary() {
return
true
;
}
bool
DefaultRevivePolicy
::
DoReject
(
const
std
::
vector
<
ServerId
>&
server_list
)
{
{
std
::
unique_lock
<
butil
::
Mutex
>
mu
(
_mutex
);
if
(
!
_reviving
)
{
mu
.
unlock
();
return
false
;
}
int
DefaultRevivePolicy
::
GetUsableServerCount
(
int64_t
now_ms
,
const
std
::
vector
<
ServerId
>&
server_list
)
{
if
(
now_ms
-
_usable_cache_time_ms
<
FLAGS_detect_available_server_interval_ms
)
{
return
_usable_cache
;
}
size_t
n
=
server_list
.
size
();
int
usable
=
0
;
size_t
n
=
server_list
.
size
();
SocketUniquePtr
ptr
;
// TODO(zhujiashun): optimize O(N)
for
(
size_t
i
=
0
;
i
<
n
;
++
i
)
{
if
(
Socket
::
Address
(
server_list
[
i
].
id
,
&
ptr
)
==
0
&&
!
ptr
->
IsLogOff
())
{
usable
++
;
}
}
std
::
unique_lock
<
butil
::
Mutex
>
mu
(
_mutex
);
_usable_cache
=
usable
;
_usable_cache_time_ms
=
now_ms
;
return
_usable_cache
;
}
bool
DefaultRevivePolicy
::
DoReject
(
const
std
::
vector
<
ServerId
>&
server_list
)
{
if
(
!
_reviving
)
{
return
false
;
}
int64_t
now_ms
=
butil
::
gettimeofday_ms
();
int
usable
=
GetUsableServerCount
(
now_ms
,
server_list
);
{
std
::
unique_lock
<
butil
::
Mutex
>
mu
(
_mutex
);
if
(
_last_usable
!=
usable
)
{
...
...
This diff is collapsed.
Click to expand it.
src/brpc/revive_policy.h
View file @
6cb4475a
...
...
@@ -59,6 +59,9 @@ public:
bool
DoReject
(
const
std
::
vector
<
ServerId
>&
server_list
);
bool
StopRevivingIfNecessary
();
private
:
int
GetUsableServerCount
(
int64_t
now_ms
,
const
std
::
vector
<
ServerId
>&
server_list
);
private
:
bool
_reviving
;
int64_t
_minimum_working_instances
;
...
...
@@ -66,6 +69,8 @@ private:
int64_t
_last_usable
;
int64_t
_last_usable_change_time_ms
;
int64_t
_hold_time_ms
;
int64_t
_usable_cache
;
int64_t
_usable_cache_time_ms
;
};
}
// namespace brpc
...
...
This diff is collapsed.
Click to expand it.
test/brpc_load_balancer_unittest.cpp
View file @
6cb4475a
...
...
@@ -31,6 +31,7 @@
namespace
brpc
{
DECLARE_int32
(
health_check_interval
);
DECLARE_int64
(
detect_available_server_interval_ms
);
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
));
...
...
@@ -831,6 +832,7 @@ TEST_F(LoadBalancerTest, revived_from_all_failed_sanity) {
ASSERT_EQ
(
1
,
brpc
::
Socket
::
AddressFailedAsWell
(
ptr
[
0
]
->
id
(),
&
dummy_ptr
));
dummy_ptr
->
Revive
();
}
bthread_usleep
(
brpc
::
FLAGS_detect_available_server_interval_ms
*
1000
);
// After one server is revived, the reject rate should be 50%
int
num_ereject
=
0
;
int
num_ok
=
0
;
...
...
This diff is collapsed.
Click to expand it.
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