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

Fix for FullStatus gRPC connection pooling #15520

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions go/test/endtoend/vtorc/general/vtorc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,76 @@ func TestDurabilityPolicySetLater(t *testing.T) {
assert.NotNil(t, primary, "should have elected a primary")
utils.CheckReplication(t, newCluster, primary, shard0.Vttablets, 10*time.Second)
}

func TestFullStatusConnectionPooling(t *testing.T) {
defer utils.PrintVTOrcLogsOnFailure(t, clusterInfo.ClusterInstance)
defer cluster.PanicHandler(t)
utils.SetupVttabletsAndVTOrcs(t, clusterInfo, 4, 0, []string{
"--tablet_manager_grpc_concurrency=1",
}, cluster.VTOrcConfiguration{
PreventCrossDataCenterPrimaryFailover: true,
}, 1, "")
keyspace := &clusterInfo.ClusterInstance.Keyspaces[0]
shard0 := &keyspace.Shards[0]
vtorc := clusterInfo.ClusterInstance.VTOrcProcesses[0]

// find primary from topo
curPrimary := utils.ShardPrimaryTablet(t, clusterInfo, keyspace, shard0)
assert.NotNil(t, curPrimary, "should have elected a primary")
vtOrcProcess := clusterInfo.ClusterInstance.VTOrcProcesses[0]
utils.WaitForSuccessfulRecoveryCount(t, vtOrcProcess, logic.ElectNewPrimaryRecoveryName, 1)
utils.WaitForSuccessfulPRSCount(t, vtOrcProcess, keyspace.Name, shard0.Name, 1)

// Kill the current primary.
_ = curPrimary.VttabletProcess.Kill()

// Wait until VTOrc notices some problems
status, resp := utils.MakeAPICallRetry(t, vtorc, "/api/replication-analysis", func(_ int, response string) bool {
return response == "null"
})
assert.Equal(t, 200, status)
assert.Contains(t, resp, "UnreachablePrimary")

time.Sleep(1 * time.Minute)

// Change the primaries ports and restart it.
curPrimary.VttabletProcess.Port = clusterInfo.ClusterInstance.GetAndReservePort()
curPrimary.VttabletProcess.GrpcPort = clusterInfo.ClusterInstance.GetAndReservePort()
err := curPrimary.VttabletProcess.Setup()
require.NoError(t, err)

// See that VTOrc eventually reports no errors.
// Wait until there are no problems and the api endpoint returns null
status, resp = utils.MakeAPICallRetry(t, vtorc, "/api/replication-analysis", func(_ int, response string) bool {
return response != "null"
})
assert.Equal(t, 200, status)
assert.Equal(t, "null", resp)

// REPEATED
// Kill the current primary.
_ = curPrimary.VttabletProcess.Kill()

// Wait until VTOrc notices some problems
status, resp = utils.MakeAPICallRetry(t, vtorc, "/api/replication-analysis", func(_ int, response string) bool {
return response == "null"
})
assert.Equal(t, 200, status)
assert.Contains(t, resp, "UnreachablePrimary")

time.Sleep(1 * time.Minute)

// Change the primaries ports back to original and restart it.
curPrimary.VttabletProcess.Port = curPrimary.HTTPPort
curPrimary.VttabletProcess.GrpcPort = curPrimary.GrpcPort
err = curPrimary.VttabletProcess.Setup()
require.NoError(t, err)

// See that VTOrc eventually reports no errors.
// Wait until there are no problems and the api endpoint returns null
status, resp = utils.MakeAPICallRetry(t, vtorc, "/api/replication-analysis", func(_ int, response string) bool {
return response != "null"
})
assert.Equal(t, 200, status)
assert.Equal(t, "null", resp)
}
2 changes: 1 addition & 1 deletion go/test/endtoend/vtorc/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func MakeAPICall(t *testing.T, vtorc *cluster.VTOrcProcess, url string) (status
// The function provided takes in the status and response and returns if we should continue to retry or not
func MakeAPICallRetry(t *testing.T, vtorc *cluster.VTOrcProcess, url string, retry func(int, string) bool) (status int, response string) {
t.Helper()
timeout := time.After(10 * time.Second)
timeout := time.After(30 * time.Second)
for {
select {
case <-timeout:
Expand Down
53 changes: 43 additions & 10 deletions go/vt/vttablet/grpctmclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/status"

"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/vt/callerid"
Expand Down Expand Up @@ -108,7 +109,7 @@ type dialer interface {
}

type poolDialer interface {
dialPool(ctx context.Context, tablet *topodatapb.Tablet) (tabletmanagerservicepb.TabletManagerClient, error)
dialPool(ctx context.Context, tablet *topodatapb.Tablet) (c tabletmanagerservicepb.TabletManagerClient, invalidator func(error), err error)
}

// Client implements tmclient.TabletManagerClient.
Expand Down Expand Up @@ -152,11 +153,11 @@ func (client *grpcClient) dial(ctx context.Context, tablet *topodatapb.Tablet) (
return tabletmanagerservicepb.NewTabletManagerClient(cc), cc, nil
}

func (client *grpcClient) dialPool(ctx context.Context, tablet *topodatapb.Tablet) (tabletmanagerservicepb.TabletManagerClient, error) {
func (client *grpcClient) dialPool(ctx context.Context, tablet *topodatapb.Tablet) (tabletmanagerservicepb.TabletManagerClient, func(error) /* map invalidator */, error) {
addr := netutil.JoinHostPort(tablet.Hostname, int32(tablet.PortMap["grpc"]))
opt, err := grpcclient.SecureDialOption(cert, key, ca, crl, name)
if err != nil {
return nil, err
return nil, nil, err
}

client.mu.Lock()
Expand All @@ -172,7 +173,7 @@ func (client *grpcClient) dialPool(ctx context.Context, tablet *topodatapb.Table
for i := 0; i < cap(c); i++ {
cc, err := grpcclient.Dial(addr, grpcclient.FailFast(false), opt)
if err != nil {
return nil, err
return nil, nil, err
}
c <- &tmc{
cc: cc,
Expand All @@ -185,7 +186,19 @@ func (client *grpcClient) dialPool(ctx context.Context, tablet *topodatapb.Table

result := <-c
c <- result
return result.client, nil
invalidator := func(err error) {
if err == nil {
return
}
if _, ok := status.FromError(err); !ok {
// Not a gRPC error
return
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@harshit-gangal Would some error codes from MySQL also pass this check and end up causing us to invalidate the grpc connection?

client.mu.Lock()
defer client.mu.Unlock()
delete(client.rpcClientMap, addr)
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
}
return result.client, invalidator, nil
}

// Close is part of the tmclient.TabletManagerClient interface.
Expand Down Expand Up @@ -470,9 +483,10 @@ func (client *Client) ExecuteQuery(ctx context.Context, tablet *topodatapb.Table
func (client *Client) ExecuteFetchAsDba(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, req *tabletmanagerdatapb.ExecuteFetchAsDbaRequest) (*querypb.QueryResult, error) {
var c tabletmanagerservicepb.TabletManagerClient
var err error
var invalidator func(error)
if usePool {
if poolDialer, ok := client.dialer.(poolDialer); ok {
c, err = poolDialer.dialPool(ctx, tablet)
c, invalidator, err = poolDialer.dialPool(ctx, tablet)
if err != nil {
return nil, err
}
Expand All @@ -497,6 +511,9 @@ func (client *Client) ExecuteFetchAsDba(ctx context.Context, tablet *topodatapb.
DisableForeignKeyChecks: req.DisableForeignKeyChecks,
})
if err != nil {
if invalidator != nil {
invalidator(err)
}
return nil, err
}
return response.Result, nil
Expand All @@ -506,9 +523,10 @@ func (client *Client) ExecuteFetchAsDba(ctx context.Context, tablet *topodatapb.
func (client *Client) ExecuteMultiFetchAsDba(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, req *tabletmanagerdatapb.ExecuteMultiFetchAsDbaRequest) ([]*querypb.QueryResult, error) {
var c tabletmanagerservicepb.TabletManagerClient
var err error
var invalidator func(error)
if usePool {
if poolDialer, ok := client.dialer.(poolDialer); ok {
c, err = poolDialer.dialPool(ctx, tablet)
c, invalidator, err = poolDialer.dialPool(ctx, tablet)
if err != nil {
return nil, err
}
Expand All @@ -533,6 +551,9 @@ func (client *Client) ExecuteMultiFetchAsDba(ctx context.Context, tablet *topoda
DisableForeignKeyChecks: req.DisableForeignKeyChecks,
})
if err != nil {
if invalidator != nil {
invalidator(err)
}
return nil, err
}
return response.Results, err
Expand Down Expand Up @@ -562,9 +583,10 @@ func (client *Client) ExecuteFetchAsAllPrivs(ctx context.Context, tablet *topoda
func (client *Client) ExecuteFetchAsApp(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, req *tabletmanagerdatapb.ExecuteFetchAsAppRequest) (*querypb.QueryResult, error) {
var c tabletmanagerservicepb.TabletManagerClient
var err error
var invalidator func(error)
if usePool {
if poolDialer, ok := client.dialer.(poolDialer); ok {
c, err = poolDialer.dialPool(ctx, tablet)
c, invalidator, err = poolDialer.dialPool(ctx, tablet)
if err != nil {
return nil, err
}
Expand All @@ -582,6 +604,9 @@ func (client *Client) ExecuteFetchAsApp(ctx context.Context, tablet *topodatapb.

response, err := c.ExecuteFetchAsApp(ctx, req)
if err != nil {
if invalidator != nil {
invalidator(err)
}
return nil, err
}
return response.Result, nil
Expand Down Expand Up @@ -612,8 +637,9 @@ func (client *Client) ReplicationStatus(ctx context.Context, tablet *topodatapb.
func (client *Client) FullStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.FullStatus, error) {
var c tabletmanagerservicepb.TabletManagerClient
var err error
var invalidator func(error)
if poolDialer, ok := client.dialer.(poolDialer); ok {
c, err = poolDialer.dialPool(ctx, tablet)
c, invalidator, err = poolDialer.dialPool(ctx, tablet)
if err != nil {
return nil, err
}
Expand All @@ -630,6 +656,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(err)
}
return nil, err
}
return response.Status, nil
Expand Down Expand Up @@ -1102,8 +1131,9 @@ func (client *Client) Backup(ctx context.Context, tablet *topodatapb.Tablet, req
func (client *Client) CheckThrottler(ctx context.Context, tablet *topodatapb.Tablet, req *tabletmanagerdatapb.CheckThrottlerRequest) (*tabletmanagerdatapb.CheckThrottlerResponse, error) {
var c tabletmanagerservicepb.TabletManagerClient
var err error
var invalidator func(error)
if poolDialer, ok := client.dialer.(poolDialer); ok {
c, err = poolDialer.dialPool(ctx, tablet)
c, invalidator, err = poolDialer.dialPool(ctx, tablet)
if err != nil {
return nil, err
}
Expand All @@ -1120,6 +1150,9 @@ func (client *Client) CheckThrottler(ctx context.Context, tablet *topodatapb.Tab

response, err := c.CheckThrottler(ctx, req)
if err != nil {
if invalidator != nil {
invalidator(err)
}
return nil, err
}
Comment on lines 1140 to 1145
Copy link
Member

Choose a reason for hiding this comment

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

nit: can remove passing error to invalidator

return response, nil
Expand Down
Loading