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

Protect the .Conn property of tabletHealthCheck from being read/modified concurrently. #16362

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions go/test/endtoend/tabletgateway/vtgate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ func TestReplicaTransactions(t *testing.T) {
_ = replicaTablet.VttabletProcess.TearDown()
// Healthcheck interval on tablet is set to 1s, so sleep for 2s
time.Sleep(2 * time.Second)
utils.AssertContainsError(t, readConn, fetchAllCustomers, "is either down or nonexistent")
utils.AssertContainsError(t, readConn, fetchAllCustomers, "connect: connection refused")
Copy link
Member

@deepthi deepthi Jul 10, 2024

Choose a reason for hiding this comment

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

We'll need a replacement test that produces the original error.


// bring up the tablet again
// trying to use the same session/transaction should fail as the vtgate has
// trying to use the same session/transaction should fail as the vttablet has
// been restarted and the session lost
replicaTablet.VttabletProcess.ServingStatus = "SERVING"
err = replicaTablet.VttabletProcess.Setup()
Expand Down
2 changes: 1 addition & 1 deletion go/vt/discovery/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func (hc *HealthCheckImpl) TabletConnection(ctx context.Context, alias *topodata
hc.mu.Lock()
thc := hc.healthByAlias[tabletAliasString(topoproto.TabletAliasString(alias))]
hc.mu.Unlock()
if thc == nil || thc.Conn == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this because I don't think this particular check makes sense (and it causes the race detector to be unhappy).

thc.Conn can become nil at any time, including right after this check.

Calling thc.Connection a few lines below will return a new connection object that will either be usable (if the vttablet on the other side of the connection can be accessed), or it will return an error when the connection is attempted to be used.

Removing this line seems to have impact on some integration tests and I'll try if I can figure out how to update them accordingly, but as-is this check simply leads to flaky behaviour and can't be trusted.

Copy link
Member

Choose a reason for hiding this comment

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

It seems correct to remove this check for the reasons you stated. We should wait and see what your test failures tell you.
One thing to note is that there seem to be exactly two paths to thc.Connection which, as you noted, is responsible for initializing the connection object. One is from here, and the other is from stream.

Copy link
Member

Choose a reason for hiding this comment

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

I went back and looked at the blame for this. Perhaps it is actually protecting us from some other race condition.

Copy link
Member

Choose a reason for hiding this comment

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

We may end up recreating a connection to a tablet that we are trying to drop.

if thc == nil {
// TODO: test that throws this error
return nil, vterrors.Errorf(vtrpc.Code_NOT_FOUND, "tablet: %v is either down or nonexistent", alias)
}
Expand Down
72 changes: 72 additions & 0 deletions go/vt/discovery/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,78 @@ func init() {
refreshInterval = time.Minute
}

func TestHealthCheckRace(t *testing.T) {
ctx := utils.LeakCheckContext(t)

// reset error counters
hcErrorCounters.ResetAll()
ts := memorytopo.NewServer(ctx, "cell")
defer ts.Close()
hc := createTestHc(ctx, ts)
// close healthcheck
defer hc.Close()
tablet := createTestTablet(0, "cell", "a")
tablet.Type = topodatapb.TabletType_REPLICA

target := &querypb.Target{Keyspace: "k", Shard: "s", TabletType: topodatapb.TabletType_REPLICA}
shr := &querypb.StreamHealthResponse{
TabletAlias: tablet.Alias,
Target: target,
Serving: true,

PrimaryTermStartTimestamp: 0,
RealtimeStats: &querypb.RealtimeStats{ReplicationLagSeconds: 1, CpuUsage: 0.5},
}

input := make(chan *querypb.StreamHealthResponse)
fc := createFakeConn(tablet, input)
fc.errCh = make(chan error)

go func() {
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
input <- shr

}
}
}()

hc.AddTablet(tablet)

// Wait for the tablet to become healthy
time.Sleep(30 * time.Millisecond)
Comment on lines +110 to +111
Copy link
Member

Choose a reason for hiding this comment

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

To avoid flakiness, we should probably check in hc for the healthy tablet instead of sleeping for such a short amount of time.


// This goroutine simulates accessing the tablet connection
go func() {
ticker := time.NewTicker(5 * time.Millisecond)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
conn, err := hc.TabletConnection(context.Background(), tablet.Alias, target)

assert.NoError(t, err)
assert.NotNil(t, conn)
}
}
}()

// Inject connection failures to simulate `closeConnection` being called
for i := 0; i < 10; i++ {
fc.errCh <- fmt.Errorf("some stream error")
time.Sleep(30 * time.Millisecond)
}
}

func TestHealthCheck(t *testing.T) {
ctx := utils.LeakCheckContext(t)
// reset error counters
Expand Down
6 changes: 6 additions & 0 deletions go/vt/discovery/tablet_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ func (thc *tabletHealthCheck) checkConn(hc *HealthCheckImpl) {
}

func (thc *tabletHealthCheck) closeConnection(ctx context.Context, err error) {
thc.connMu.Lock()
defer thc.connMu.Unlock()

log.Warningf("tablet %v healthcheck stream error: %v", thc.Tablet, err)
thc.setServingState(false, err.Error())
thc.LastError = err
Expand All @@ -366,6 +369,9 @@ func (thc *tabletHealthCheck) closeConnection(ctx context.Context, err error) {
// finalizeConn closes the health checking connection.
// To be called only on exit from checkConn().
func (thc *tabletHealthCheck) finalizeConn() {
thc.connMu.Lock()
defer thc.connMu.Unlock()

thc.setServingState(false, "finalizeConn closing connection")
// Note: checkConn() exits only when thc.ctx.Done() is closed. Thus it's
// safe to simply get Err() value here and assign to LastError.
Expand Down
Loading