-
Notifications
You must be signed in to change notification settings - Fork 725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
client: use ServiceDiscovery to update member in HTTP client #7668
Conversation
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
[REVIEW NOTIFICATION] This pull request has been approved by:
To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #7668 +/- ##
==========================================
+ Coverage 73.80% 73.87% +0.06%
==========================================
Files 429 429
Lines 47439 47413 -26
==========================================
+ Hits 35014 35025 +11
+ Misses 9424 9409 -15
+ Partials 3001 2979 -22
Flags with carried forward coverage won't be shown. Click here to find out more. |
client/http/client.go
Outdated
@@ -58,8 +57,8 @@ type clientInner struct { | |||
cancel context.CancelFunc | |||
|
|||
sync.RWMutex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this lock still needed?
sd pd.ServiceDiscovery, | ||
opts ...ClientOption, | ||
) Client { | ||
ctx, cancel := context.WithCancel(context.Background()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use a context passed through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found out that when we created innerClient, it created a context itself and cancel it with the Close
function, so I kept it. Do we need to change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot about the logic. Then let's keep it the same.
client/http/client.go
Outdated
if leaderAddrIdx != -1 { | ||
addr = pdAddrs[leaderAddrIdx] | ||
clients := ci.sd.GetAllServiceClients() | ||
for _, cli := range clients { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we maintain the original logic of requesting the leader first and then the followers? This is to reduce the unnecessary redirect as much as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the slice returned by GetAllServiceClients
will hold the leader as the first place. Let me add some comments.
client/pd_service_discovery.go
Outdated
func newPDServiceClient(addr, leaderAddr string, conn *grpc.ClientConn, isLeader bool) ServiceClient { | ||
func newPDServiceClient(addr, leaderAddr string, tlsCfg *tls.Config, conn *grpc.ClientConn, isLeader bool) ServiceClient { | ||
var httpAddress string | ||
if strings.HasPrefix(addr, httpScheme) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the address starts with http
but still has a TLS config passed in? In this case, replacing the scheme is also needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. It seems that we didn't handle this case well before.
@@ -732,6 +780,21 @@ func (c *pdServiceDiscovery) GetServiceClient() ServiceClient { | |||
return leaderClient | |||
} | |||
|
|||
// GetAllServiceClients implments ServiceDiscovery | |||
func (c *pdServiceDiscovery) GetAllServiceClients() []ServiceClient { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe return leader and follower addresses respectively could help with the above comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's both OK to me. My idea is that we only need one for loop
, so I just return all clients together
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
// Apply the options first. | ||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
c.inner.setPDAddrs(pdAddrs, -1) | ||
c.inner.init() | ||
sd := pd.NewDefaultPDServiceDiscovery(ctx, cancel, pdAddrs, c.inner.tlsConf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just use newPDServiceDiscovery?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I didn't think it was a good idea to pass in a lot of nil params before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot that option
is unexported, so I think we can use NewDefaultPDServiceDiscovery
to avoid to input some unconcerned params.
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@@ -101,77 +65,6 @@ func TestCallerID(t *testing.T) { | |||
c.Close() | |||
} | |||
|
|||
func TestRedirectWithMetrics(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move it into integration test.
sd pd.ServiceDiscovery, | ||
opts ...ClientOption, | ||
) Client { | ||
ctx, cancel := context.WithCancel(context.Background()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot about the logic. Then let's keep it the same.
client/http/client.go
Outdated
type requestChecker struct { | ||
checker func(req *http.Request) error | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about just:
type requestChecker struct { | |
checker func(req *http.Request) error | |
} | |
type requestChecker func(req *http.Request) error |
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
func (c *client) UpdateMembersInfo() { | ||
c.inner.updateMembersInfo(c.inner.ctx) | ||
// requestChecker is used to check the HTTP request sent by the client. | ||
type requestChecker func(req *http.Request) error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @HuSharp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it just used in the test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe better to add some comments for NewHTTPClientWithRequestChecker
client/pd_service_discovery.go
Outdated
@@ -732,6 +792,21 @@ func (c *pdServiceDiscovery) GetServiceClient() ServiceClient { | |||
return leaderClient | |||
} | |||
|
|||
// GetAllServiceClients implments ServiceDiscovery | |||
func (c *pdServiceDiscovery) GetAllServiceClients() []ServiceClient { | |||
ret := make([]ServiceClient, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it better to specify make(client, 1)
or another larger value? perhaps the number of clients in the main scenario is greater than 0.
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
/merge |
@JmPotato: It seems you want to merge this PR, I will help you trigger all the tests: /run-all-tests You only need to trigger
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
This pull request has been accepted and is ready to merge. Commit hash: 9d21255
|
ref tikv#7576 Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> Signed-off-by: pingandb <songge102@pingan.com.cn>
What problem does this PR solve?
Issue Number: ref #7576
What is changed and how does it work?
use ServiceDiscovery to update member in HTTP client
Check List
Tests
Code changes
Side effects
Related changes
pingcap/docs
/pingcap/docs-cn
:pingcap/tiup
:Release note