-
Notifications
You must be signed in to change notification settings - Fork 720
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: fix scheme when tls config not match #7901
Changes from 5 commits
6438a65
55b6c9a
8115700
0d69a07
52ad50d
0c5fec7
24ce773
a6b7ca9
727d5d2
588bc61
74349ef
af92378
3fde8de
6cf0b07
7429fbd
f3888ca
34eedb0
5f21733
26de3db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,8 +46,8 @@ const ( | |||||
updateMemberTimeout = time.Second // Use a shorter timeout to recover faster from network isolation. | ||||||
updateMemberBackOffBaseTime = 100 * time.Millisecond | ||||||
|
||||||
httpScheme = "http" | ||||||
httpsScheme = "https" | ||||||
httpScheme = "http://" | ||||||
httpsScheme = "https://" | ||||||
) | ||||||
|
||||||
// MemberHealthCheckInterval might be changed in the unit to shorten the testing time. | ||||||
|
@@ -124,10 +124,8 @@ type ServiceDiscovery interface { | |||||
|
||||||
// ServiceClient is an interface that defines a set of operations for a raw PD gRPC client to specific PD server. | ||||||
type ServiceClient interface { | ||||||
// GetAddress returns the address information of the PD server. | ||||||
// GetAddress returns the address with HTTP scheme of the PD server. | ||||||
GetAddress() string | ||||||
// GetHTTPAddress returns the address with HTTP scheme of the PD server. | ||||||
GetHTTPAddress() string | ||||||
// GetClientConn returns the gRPC connection of the service client | ||||||
GetClientConn() *grpc.ClientConn | ||||||
// BuildGRPCTargetContext builds a context object with a gRPC context. | ||||||
|
@@ -158,34 +156,15 @@ type pdServiceClient struct { | |||||
networkFailure atomic.Bool | ||||||
} | ||||||
|
||||||
func newPDServiceClient(addr, leaderAddr string, tlsCfg *tls.Config, conn *grpc.ClientConn, isLeader bool) ServiceClient { | ||||||
var httpAddress string | ||||||
if tlsCfg == nil { | ||||||
if strings.HasPrefix(addr, httpsScheme) { | ||||||
addr = strings.TrimPrefix(addr, httpsScheme) | ||||||
httpAddress = fmt.Sprintf("%s%s", httpScheme, addr) | ||||||
} else if strings.HasPrefix(addr, httpScheme) { | ||||||
httpAddress = addr | ||||||
} else { | ||||||
httpAddress = fmt.Sprintf("%s://%s", httpScheme, addr) | ||||||
} | ||||||
} else { | ||||||
if strings.HasPrefix(addr, httpsScheme) { | ||||||
httpAddress = addr | ||||||
} else if strings.HasPrefix(addr, httpScheme) { | ||||||
addr = strings.TrimPrefix(addr, httpScheme) | ||||||
httpAddress = fmt.Sprintf("%s%s", httpsScheme, addr) | ||||||
} else { | ||||||
httpAddress = fmt.Sprintf("%s://%s", httpsScheme, addr) | ||||||
} | ||||||
} | ||||||
|
||||||
// NOTE: In the current implementation, the address passed in is bound to have an http scheme, | ||||||
// because it is processed in `newPDServiceDiscovery`, and the url returned by etcd member is its own. | ||||||
// When testing, the address is also bound to have an http scheme. | ||||||
HuSharp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
func newPDServiceClient(addr, leaderAddr string, conn *grpc.ClientConn, isLeader bool) ServiceClient { | ||||||
cli := &pdServiceClient{ | ||||||
HuSharp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
addr: addr, | ||||||
httpAddress: httpAddress, | ||||||
conn: conn, | ||||||
isLeader: isLeader, | ||||||
leaderAddr: leaderAddr, | ||||||
addr: addr, | ||||||
conn: conn, | ||||||
isLeader: isLeader, | ||||||
leaderAddr: leaderAddr, | ||||||
} | ||||||
if conn == nil { | ||||||
cli.networkFailure.Store(true) | ||||||
|
@@ -201,14 +180,6 @@ func (c *pdServiceClient) GetAddress() string { | |||||
return c.addr | ||||||
} | ||||||
|
||||||
// GetHTTPAddress implements ServiceClient. | ||||||
func (c *pdServiceClient) GetHTTPAddress() string { | ||||||
if c == nil { | ||||||
return "" | ||||||
} | ||||||
return c.httpAddress | ||||||
} | ||||||
|
||||||
// BuildGRPCTargetContext implements ServiceClient. | ||||||
func (c *pdServiceClient) BuildGRPCTargetContext(ctx context.Context, toLeader bool) context.Context { | ||||||
if c == nil || c.isLeader { | ||||||
|
@@ -506,7 +477,7 @@ func newPDServiceDiscovery( | |||||
tlsCfg: tlsCfg, | ||||||
option: option, | ||||||
} | ||||||
urls = addrsToUrls(urls) | ||||||
urls = addrsToUrls(urls, tlsCfg) | ||||||
pdsd.urls.Store(urls) | ||||||
return pdsd | ||||||
} | ||||||
|
@@ -1032,7 +1003,7 @@ func (c *pdServiceDiscovery) switchLeader(addrs []string) (bool, error) { | |||||
// If gRPC connect is created successfully or leader is new, still saves. | ||||||
if addr != oldLeader.GetAddress() || newConn != nil { | ||||||
// Set PD leader and Global TSO Allocator (which is also the PD leader) | ||||||
leaderClient := newPDServiceClient(addr, addr, c.tlsCfg, newConn, true) | ||||||
leaderClient := newPDServiceClient(addr, addr, newConn, true) | ||||||
c.leader.Store(leaderClient) | ||||||
} | ||||||
// Run callbacks | ||||||
|
@@ -1069,15 +1040,15 @@ func (c *pdServiceDiscovery) updateFollowers(members []*pdpb.Member, leader *pdp | |||||
log.Warn("[pd] failed to connect follower", zap.String("follower", addr), errs.ZapError(err)) | ||||||
continue | ||||||
} | ||||||
follower := newPDServiceClient(addr, leader.GetClientUrls()[0], c.tlsCfg, conn, false) | ||||||
follower := newPDServiceClient(addr, leader.GetClientUrls()[0], conn, false) | ||||||
c.followers.Store(addr, follower) | ||||||
changed = true | ||||||
} | ||||||
delete(followers, addr) | ||||||
} else { | ||||||
changed = true | ||||||
conn, err := c.GetOrCreateGRPCConn(addr) | ||||||
follower := newPDServiceClient(addr, leader.GetClientUrls()[0], c.tlsCfg, conn, false) | ||||||
follower := newPDServiceClient(addr, leader.GetClientUrls()[0], conn, false) | ||||||
if err != nil || conn == nil { | ||||||
log.Warn("[pd] failed to connect follower", zap.String("follower", addr), errs.ZapError(err)) | ||||||
} | ||||||
|
@@ -1150,15 +1121,28 @@ func (c *pdServiceDiscovery) GetOrCreateGRPCConn(addr string) (*grpc.ClientConn, | |||||
return grpcutil.GetOrCreateGRPCConn(c.ctx, &c.clientConns, addr, c.tlsCfg, c.option.gRPCDialOptions...) | ||||||
} | ||||||
|
||||||
func addrsToUrls(addrs []string) []string { | ||||||
func addrsToUrls(addrs []string, tlsCfg *tls.Config) []string { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Add default schema "http://" to addrs. | ||||||
urls := make([]string, 0, len(addrs)) | ||||||
for _, addr := range addrs { | ||||||
if strings.Contains(addr, "://") { | ||||||
urls = append(urls, addr) | ||||||
} else { | ||||||
urls = append(urls, "http://"+addr) | ||||||
} | ||||||
urls = append(urls, addrToUrl(addr, tlsCfg)) | ||||||
} | ||||||
return urls | ||||||
} | ||||||
|
||||||
func addrToUrl(addr string, tlsCfg *tls.Config) string { | ||||||
if tlsCfg == nil { | ||||||
if strings.HasPrefix(addr, httpsScheme) { | ||||||
addr = fmt.Sprintf("%s%s", httpScheme, strings.TrimPrefix(addr, httpsScheme)) | ||||||
} else if !strings.HasPrefix(addr, httpScheme) { | ||||||
addr = fmt.Sprintf("%s%s", httpScheme, addr) | ||||||
} | ||||||
} else { | ||||||
if strings.HasPrefix(addr, httpScheme) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only used for |
||||||
addr = fmt.Sprintf("%s%s", httpsScheme, strings.TrimPrefix(addr, httpScheme)) | ||||||
} else if !strings.HasPrefix(addr, httpsScheme) { | ||||||
addr = fmt.Sprintf("%s%s", httpsScheme, addr) | ||||||
} | ||||||
} | ||||||
return addr | ||||||
} |
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.
tests seems like will return https scheme?