Skip to content
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

Merged
merged 16 commits into from
Jan 12, 2024

Conversation

CabinfeverB
Copy link
Member

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

  • Unit test
  • Integration test

Code changes

Side effects

  • Possible performance regression
  • Increased code complexity
  • Breaking backward compatibility

Related changes

Release note

None.

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>
Copy link
Contributor

ti-chi-bot bot commented Jan 4, 2024

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • HuSharp
  • JmPotato

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 4, 2024
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 5, 2024
Copy link
Contributor

ti-chi-bot bot commented Jan 5, 2024

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>
Copy link

codecov bot commented Jan 8, 2024

Codecov Report

Merging #7668 (9d21255) into master (2a2a615) will increase coverage by 0.06%.
The diff coverage is 94.59%.

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     
Flag Coverage Δ
unittests 73.87% <94.59%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@@ -58,8 +57,8 @@ type clientInner struct {
cancel context.CancelFunc

sync.RWMutex
Copy link
Member

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())
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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.

if leaderAddrIdx != -1 {
addr = pdAddrs[leaderAddrIdx]
clients := ci.sd.GetAllServiceClients()
for _, cli := range clients {
Copy link
Member

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.

Copy link
Member Author

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.

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) {
Copy link
Member

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.

Copy link
Member Author

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 {
Copy link
Member

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.

Copy link
Member Author

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>
@CabinfeverB CabinfeverB removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 9, 2024
// 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)
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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) {
Copy link
Member Author

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())
Copy link
Member

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.

Comment on lines 369 to 371
type requestChecker struct {
checker func(req *http.Request) error
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just:

Suggested change
type requestChecker struct {
checker func(req *http.Request) error
}
type requestChecker func(req *http.Request) error

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 10, 2024
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Member

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/retry/backoff.go Outdated Show resolved Hide resolved
client/http/client.go Outdated Show resolved Hide resolved
@@ -732,6 +792,21 @@ func (c *pdServiceDiscovery) GetServiceClient() ServiceClient {
return leaderClient
}

// GetAllServiceClients implments ServiceDiscovery
func (c *pdServiceDiscovery) GetAllServiceClients() []ServiceClient {
ret := make([]ServiceClient, 0)
Copy link
Member

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>
@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jan 10, 2024
client/http/client.go Outdated Show resolved Hide resolved
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 10, 2024
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@ti-chi-bot ti-chi-bot bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jan 12, 2024
@ti-chi-bot ti-chi-bot bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jan 12, 2024
@JmPotato
Copy link
Member

/merge

Copy link
Contributor

ti-chi-bot bot commented Jan 12, 2024

@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 /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

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.

Copy link
Contributor

ti-chi-bot bot commented Jan 12, 2024

This pull request has been accepted and is ready to merge.

Commit hash: 9d21255

@ti-chi-bot ti-chi-bot bot added the status/can-merge Indicates a PR has been approved by a committer. label Jan 12, 2024
@ti-chi-bot ti-chi-bot bot merged commit 9cdbeb6 into tikv:master Jan 12, 2024
25 of 26 checks passed
pingandb pushed a commit to pingandb/pd that referenced this pull request Jan 18, 2024
ref tikv#7576

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: pingandb <songge102@pingan.com.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants