-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Dedicated poolDialer logic for VTOrc, throttler #15562
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92c3641
test: add failing test for full status connection pooling
GuptaManan100 54276e4
Merge remote-tracking branch 'origin/main' into tmc-pool-dialer-5
shlomi-noach db6e3c3
separate logic for non default dial groups
shlomi-noach 033a2da
declare itnermediate variable
shlomi-noach f01fcc4
add unit tests
shlomi-noach 813456a
more unit testing
shlomi-noach 5d4303b
only invalidate on error
shlomi-noach e3f8c74
only invalidate on error, remove err argument
shlomi-noach 5528591
split logic into dialPool and dialDedicatedPool
shlomi-noach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,15 @@ import ( | |
topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
) | ||
|
||
type DialPoolGroup int | ||
|
||
const ( | ||
dialPoolGroupThrottler DialPoolGroup = iota | ||
dialPoolGroupVTOrc | ||
) | ||
|
||
type invalidatorFunc func() | ||
|
||
var ( | ||
concurrency = 8 | ||
cert string | ||
|
@@ -92,14 +101,17 @@ type tmc struct { | |
client tabletmanagerservicepb.TabletManagerClient | ||
} | ||
|
||
type addrTmcMap map[string]*tmc | ||
|
||
// grpcClient implements both dialer and poolDialer. | ||
type grpcClient struct { | ||
// This cache of connections is to maximize QPS for ExecuteFetchAs{Dba,App}, | ||
// CheckThrottler and FullStatus. Note we'll keep the clients open and close them upon Close() only. | ||
// But that's OK because usually the tasks that use them are one-purpose only. | ||
// The map is protected by the mutex. | ||
mu sync.Mutex | ||
rpcClientMap map[string]chan *tmc | ||
mu sync.Mutex | ||
rpcClientMap map[string]chan *tmc | ||
rpcDialPoolMap map[DialPoolGroup]addrTmcMap | ||
} | ||
|
||
type dialer interface { | ||
|
@@ -109,6 +121,7 @@ type dialer interface { | |
|
||
type poolDialer interface { | ||
dialPool(ctx context.Context, tablet *topodatapb.Tablet) (tabletmanagerservicepb.TabletManagerClient, error) | ||
dialDedicatedPool(ctx context.Context, dialPoolGroup DialPoolGroup, tablet *topodatapb.Tablet) (tabletmanagerservicepb.TabletManagerClient, invalidatorFunc, error) | ||
} | ||
|
||
// Client implements tmclient.TabletManagerClient. | ||
|
@@ -152,6 +165,17 @@ func (client *grpcClient) dial(ctx context.Context, tablet *topodatapb.Tablet) ( | |
return tabletmanagerservicepb.NewTabletManagerClient(cc), cc, nil | ||
} | ||
|
||
func (client *grpcClient) createTmc(addr string, opt grpc.DialOption) (*tmc, error) { | ||
cc, err := grpcclient.Dial(addr, grpcclient.FailFast(false), opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tmc{ | ||
cc: cc, | ||
client: tabletmanagerservicepb.NewTabletManagerClient(cc), | ||
}, nil | ||
} | ||
|
||
func (client *grpcClient) dialPool(ctx context.Context, tablet *topodatapb.Tablet) (tabletmanagerservicepb.TabletManagerClient, error) { | ||
addr := netutil.JoinHostPort(tablet.Hostname, int32(tablet.PortMap["grpc"])) | ||
opt, err := grpcclient.SecureDialOption(cert, key, ca, crl, name) | ||
|
@@ -170,14 +194,11 @@ func (client *grpcClient) dialPool(ctx context.Context, tablet *topodatapb.Table | |
client.mu.Unlock() | ||
|
||
for i := 0; i < cap(c); i++ { | ||
cc, err := grpcclient.Dial(addr, grpcclient.FailFast(false), opt) | ||
tm, err := client.createTmc(addr, opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c <- &tmc{ | ||
cc: cc, | ||
client: tabletmanagerservicepb.NewTabletManagerClient(cc), | ||
} | ||
c <- tm | ||
} | ||
} else { | ||
client.mu.Unlock() | ||
|
@@ -188,6 +209,38 @@ func (client *grpcClient) dialPool(ctx context.Context, tablet *topodatapb.Table | |
return result.client, nil | ||
} | ||
|
||
func (client *grpcClient) dialDedicatedPool(ctx context.Context, dialPoolGroup DialPoolGroup, tablet *topodatapb.Tablet) (tabletmanagerservicepb.TabletManagerClient, invalidatorFunc, error) { | ||
addr := netutil.JoinHostPort(tablet.Hostname, int32(tablet.PortMap["grpc"])) | ||
opt, err := grpcclient.SecureDialOption(cert, key, ca, crl, name) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
client.mu.Lock() | ||
defer client.mu.Unlock() | ||
if client.rpcDialPoolMap == nil { | ||
client.rpcDialPoolMap = make(map[DialPoolGroup]addrTmcMap) | ||
} | ||
if _, ok := client.rpcDialPoolMap[dialPoolGroup]; !ok { | ||
client.rpcDialPoolMap[dialPoolGroup] = make(addrTmcMap) | ||
} | ||
m := client.rpcDialPoolMap[dialPoolGroup] | ||
if _, ok := m[addr]; !ok { | ||
tm, err := client.createTmc(addr, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
m[addr] = tm | ||
} | ||
invalidator := func() { | ||
client.mu.Lock() | ||
defer client.mu.Unlock() | ||
m[addr].cc.Close() | ||
delete(m, addr) | ||
} | ||
return m[addr].client, invalidator, nil | ||
} | ||
|
||
// Close is part of the tmclient.TabletManagerClient interface. | ||
func (client *grpcClient) Close() { | ||
client.mu.Lock() | ||
|
@@ -611,9 +664,10 @@ func (client *Client) ReplicationStatus(ctx context.Context, tablet *topodatapb. | |
// and dialing the other tablet every time is not practical. | ||
func (client *Client) FullStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.FullStatus, error) { | ||
var c tabletmanagerservicepb.TabletManagerClient | ||
var invalidator invalidatorFunc | ||
var err error | ||
if poolDialer, ok := client.dialer.(poolDialer); ok { | ||
c, err = poolDialer.dialPool(ctx, tablet) | ||
c, invalidator, err = poolDialer.dialDedicatedPool(ctx, dialPoolGroupVTOrc, tablet) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -630,6 +684,9 @@ func (client *Client) FullStatus(ctx context.Context, tablet *topodatapb.Tablet) | |
|
||
response, err := c.FullStatus(ctx, &tabletmanagerdatapb.FullStatusRequest{}) | ||
if err != nil { | ||
if invalidator != nil { | ||
invalidator() | ||
} | ||
return nil, err | ||
} | ||
return response.Status, nil | ||
|
@@ -1101,9 +1158,10 @@ func (client *Client) Backup(ctx context.Context, tablet *topodatapb.Tablet, req | |
// and dialing the other tablet every time is not practical. | ||
func (client *Client) CheckThrottler(ctx context.Context, tablet *topodatapb.Tablet, req *tabletmanagerdatapb.CheckThrottlerRequest) (*tabletmanagerdatapb.CheckThrottlerResponse, error) { | ||
var c tabletmanagerservicepb.TabletManagerClient | ||
var invalidator invalidatorFunc | ||
var err error | ||
if poolDialer, ok := client.dialer.(poolDialer); ok { | ||
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. I wonder in what scenario is the |
||
c, err = poolDialer.dialPool(ctx, tablet) | ||
c, invalidator, err = poolDialer.dialDedicatedPool(ctx, dialPoolGroupThrottler, tablet) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -1120,6 +1178,9 @@ func (client *Client) CheckThrottler(ctx context.Context, tablet *topodatapb.Tab | |
|
||
response, err := c.CheckThrottler(ctx, req) | ||
if err != nil { | ||
if invalidator != nil { | ||
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. same. |
||
invalidator() | ||
} | ||
return nil, err | ||
} | ||
return response, nil | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: this will not be nil
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 could be
nil
, if this condition fails:Now, I'm not sure how it could possibly fail, but as golang goes, I created
var invalidator invalidatorFunc
and it could benil
.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.
If you like, I can pre-assign
var invalidator
to an empty function? That way it will never benil
.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.
This kinda returns to this comment: #14979 (comment)