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
3b174fe1
Commit
3b174fe1
authored
Dec 03, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move discovery getserver logic int client
parent
378d5ad2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
53 deletions
+83
-53
discovery_naming_service.cpp
src/brpc/policy/discovery_naming_service.cpp
+55
-38
discovery_naming_service.h
src/brpc/policy/discovery_naming_service.h
+28
-15
No files found.
src/brpc/policy/discovery_naming_service.cpp
View file @
3b174fe1
...
...
@@ -41,6 +41,28 @@ DEFINE_int32(discovery_reregister_threshold, 3, "The renew error threshold beyon
static
Channel
s_discovery_channel
;
static
pthread_once_t
s_init_channel_once
=
PTHREAD_ONCE_INIT
;
int
DiscoveryNamingService
::
GetServers
(
const
char
*
service_name
,
std
::
vector
<
ServerNode
>*
servers
)
{
DiscoveryFetchsParam
params
{
service_name
,
FLAGS_discovery_env
,
FLAGS_discovery_status
};
return
_client
.
Fetchs
(
params
,
servers
);
}
void
DiscoveryNamingService
::
Describe
(
std
::
ostream
&
os
,
const
DescribeOptions
&
)
const
{
os
<<
"discovery"
;
return
;
}
NamingService
*
DiscoveryNamingService
::
New
()
const
{
return
new
DiscoveryNamingService
;
}
void
DiscoveryNamingService
::
Destroy
()
{
delete
this
;
}
int
ParseNodesResult
(
const
butil
::
IOBuf
&
buf
,
std
::
string
*
server_addr
)
{
BUTIL_RAPIDJSON_NAMESPACE
::
Document
d
;
const
std
::
string
response
=
buf
.
to_string
();
...
...
@@ -178,36 +200,6 @@ int ParseFetchsResult(const butil::IOBuf& buf,
return
0
;
}
int
DiscoveryNamingService
::
GetServers
(
const
char
*
service_name
,
std
::
vector
<
ServerNode
>*
servers
)
{
pthread_once
(
&
s_init_channel_once
,
InitChannel
);
servers
->
clear
();
Controller
cntl
;
cntl
.
http_request
().
uri
()
=
butil
::
string_printf
(
"/discovery/fetchs?appid=%s&env=%s&status=%s"
,
service_name
,
FLAGS_discovery_env
.
c_str
(),
FLAGS_discovery_status
.
c_str
());
s_discovery_channel
.
CallMethod
(
NULL
,
&
cntl
,
NULL
,
NULL
,
NULL
);
if
(
cntl
.
Failed
())
{
LOG
(
ERROR
)
<<
"Fail to make /discovery/fetchs request: "
<<
cntl
.
ErrorText
();
return
-
1
;
}
return
ParseFetchsResult
(
cntl
.
response_attachment
(),
service_name
,
servers
);
}
void
DiscoveryNamingService
::
Describe
(
std
::
ostream
&
os
,
const
DescribeOptions
&
)
const
{
os
<<
"discovery"
;
return
;
}
NamingService
*
DiscoveryNamingService
::
New
()
const
{
return
new
DiscoveryNamingService
;
}
void
DiscoveryNamingService
::
Destroy
()
{
delete
this
;
}
bool
DiscoveryRegisterParam
::
IsValid
()
const
{
if
(
appid
.
empty
()
||
hostname
.
empty
()
||
addrs
.
empty
()
||
env
.
empty
()
||
zone
.
empty
()
||
version
.
empty
())
{
...
...
@@ -216,6 +208,13 @@ bool DiscoveryRegisterParam::IsValid() const {
return
true
;
}
bool
DiscoveryFetchsParam
::
IsValid
()
const
{
if
(
appid
.
empty
()
||
env
.
empty
()
||
status
.
empty
())
{
return
false
;
}
return
true
;
}
DiscoveryClient
::
DiscoveryClient
()
:
_th
(
INVALID_BTHREAD
)
,
_state
(
INIT
)
{}
...
...
@@ -246,7 +245,7 @@ int ParseCommonResult(const butil::IOBuf& buf, std::string* error_text) {
return
code
;
}
int
DiscoveryClient
::
do_r
enew
()
const
{
int
DiscoveryClient
::
DoR
enew
()
const
{
Controller
cntl
;
cntl
.
http_request
().
set_method
(
HTTP_METHOD_POST
);
cntl
.
http_request
().
uri
()
=
"/discovery/renew"
;
...
...
@@ -304,7 +303,7 @@ void* DiscoveryClient::PeriodicRenew(void* arg) {
}
// Do register until succeed or Cancel is called
while
(
!
bthread_stopped
(
bthread_self
()))
{
if
(
d
->
do_r
egister
()
==
0
)
{
if
(
d
->
DoR
egister
()
==
0
)
{
break
;
}
bthread_usleep
(
FLAGS_discovery_renew_interval_s
*
1000000
);
...
...
@@ -312,7 +311,7 @@ void* DiscoveryClient::PeriodicRenew(void* arg) {
consecutive_renew_error
=
0
;
}
if
(
d
->
do_r
enew
()
!=
0
)
{
if
(
d
->
DoR
enew
()
!=
0
)
{
consecutive_renew_error
++
;
continue
;
}
...
...
@@ -360,7 +359,7 @@ int DiscoveryClient::Register(const DiscoveryRegisterParam& req) {
_version
=
req
.
version
;
_metadata
=
req
.
metadata
;
if
(
do_r
egister
()
!=
0
)
{
if
(
DoR
egister
()
!=
0
)
{
return
-
1
;
}
if
(
bthread_start_background
(
&
_th
,
NULL
,
PeriodicRenew
,
this
)
!=
0
)
{
...
...
@@ -391,12 +390,12 @@ int DiscoveryClient::Register(const DiscoveryRegisterParam& req) {
if
(
is_canceled
)
{
bthread_stop
(
_th
);
bthread_join
(
_th
,
NULL
);
return
do_c
ancel
();
return
DoC
ancel
();
}
return
0
;
}
int
DiscoveryClient
::
do_r
egister
()
const
{
int
DiscoveryClient
::
DoR
egister
()
const
{
Controller
cntl
;
cntl
.
http_request
().
set_method
(
HTTP_METHOD_POST
);
cntl
.
http_request
().
uri
()
=
"/discovery/register"
;
...
...
@@ -447,10 +446,10 @@ int DiscoveryClient::Cancel() {
CHECK_NE
(
_th
,
INVALID_BTHREAD
);
bthread_stop
(
_th
);
bthread_join
(
_th
,
NULL
);
return
do_c
ancel
();
return
DoC
ancel
();
}
int
DiscoveryClient
::
do_c
ancel
()
const
{
int
DiscoveryClient
::
DoC
ancel
()
const
{
pthread_once
(
&
s_init_channel_once
,
InitChannel
);
Controller
cntl
;
cntl
.
http_request
().
set_method
(
HTTP_METHOD_POST
);
...
...
@@ -476,6 +475,24 @@ int DiscoveryClient::do_cancel() const {
return
0
;
}
int
DiscoveryClient
::
Fetchs
(
const
DiscoveryFetchsParam
&
req
,
std
::
vector
<
ServerNode
>*
servers
)
{
if
(
!
req
.
IsValid
())
{
return
false
;
}
pthread_once
(
&
s_init_channel_once
,
InitChannel
);
servers
->
clear
();
Controller
cntl
;
cntl
.
http_request
().
uri
()
=
butil
::
string_printf
(
"/discovery/fetchs?appid=%s&env=%s&status=%s"
,
req
.
appid
.
c_str
(),
req
.
env
.
c_str
(),
req
.
status
.
c_str
());
s_discovery_channel
.
CallMethod
(
NULL
,
&
cntl
,
NULL
,
NULL
,
NULL
);
if
(
cntl
.
Failed
())
{
LOG
(
ERROR
)
<<
"Fail to get /discovery/fetchs: "
<<
cntl
.
ErrorText
();
return
-
1
;
}
return
ParseFetchsResult
(
cntl
.
response_attachment
(),
req
.
appid
.
c_str
(),
servers
);
}
}
// namespace policy
}
// namespace brpc
src/brpc/policy/discovery_naming_service.h
View file @
3b174fe1
...
...
@@ -24,18 +24,6 @@
namespace
brpc
{
namespace
policy
{
class
DiscoveryNamingService
:
public
PeriodicNamingService
{
private
:
int
GetServers
(
const
char
*
service_name
,
std
::
vector
<
ServerNode
>*
servers
)
override
;
void
Describe
(
std
::
ostream
&
os
,
const
DescribeOptions
&
)
const
override
;
NamingService
*
New
()
const
override
;
void
Destroy
()
override
;
};
struct
DiscoveryRegisterParam
{
std
::
string
appid
;
std
::
string
hostname
;
...
...
@@ -50,6 +38,14 @@ struct DiscoveryRegisterParam {
bool
IsValid
()
const
;
};
struct
DiscoveryFetchsParam
{
std
::
string
appid
;
std
::
string
env
;
std
::
string
status
;
bool
IsValid
()
const
;
};
// ONE DiscoveryClient corresponds to ONE service instance.
// If your program has multiple service instances to register,
// you need multiple DiscoveryClient.
...
...
@@ -60,12 +56,13 @@ public:
int
Register
(
const
DiscoveryRegisterParam
&
req
);
int
Cancel
();
int
Fetchs
(
const
DiscoveryFetchsParam
&
req
,
std
::
vector
<
ServerNode
>*
servers
);
private
:
static
void
*
PeriodicRenew
(
void
*
arg
);
int
do_c
ancel
()
const
;
int
do_r
egister
()
const
;
int
do_r
enew
()
const
;
int
DoC
ancel
()
const
;
int
DoR
egister
()
const
;
int
DoR
enew
()
const
;
private
:
enum
State
{
...
...
@@ -88,6 +85,22 @@ private:
std
::
string
_metadata
;
};
class
DiscoveryNamingService
:
public
PeriodicNamingService
{
private
:
int
GetServers
(
const
char
*
service_name
,
std
::
vector
<
ServerNode
>*
servers
)
override
;
void
Describe
(
std
::
ostream
&
os
,
const
DescribeOptions
&
)
const
override
;
NamingService
*
New
()
const
override
;
void
Destroy
()
override
;
private
:
DiscoveryClient
_client
;
};
}
// namespace policy
}
// namespace brpc
...
...
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