Skip to content

Commit

Permalink
query_executor: Record WaitingForConnection stat in all cases (#15073)
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Coleman <[email protected]>
  • Loading branch information
tycol7 authored Jan 31, 2024
1 parent 3147240 commit 416d7f4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
11 changes: 7 additions & 4 deletions go/vt/vttablet/tabletserver/query_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,12 +775,13 @@ func (qre *QueryExecutor) getConn() (*connpool.PooledConn, error) {
span, ctx := trace.NewSpan(qre.ctx, "QueryExecutor.getConn")
defer span.Finish()

start := time.Now()
defer func(start time.Time) {
qre.logStats.WaitingForConnection += time.Since(start)
}(time.Now())
conn, err := qre.tsv.qe.conns.Get(ctx, qre.setting)

switch err {
case nil:
qre.logStats.WaitingForConnection += time.Since(start)
return conn, nil
case connpool.ErrConnPoolClosed:
return nil, err
Expand All @@ -792,11 +793,13 @@ func (qre *QueryExecutor) getStreamConn() (*connpool.PooledConn, error) {
span, ctx := trace.NewSpan(qre.ctx, "QueryExecutor.getStreamConn")
defer span.Finish()

start := time.Now()
defer func(start time.Time) {
qre.logStats.WaitingForConnection += time.Since(start)
}(time.Now())
conn, err := qre.tsv.qe.streamConns.Get(ctx, qre.setting)

switch err {
case nil:
qre.logStats.WaitingForConnection += time.Since(start)
return conn, nil
case connpool.ErrConnPoolClosed:
return nil, err
Expand Down
38 changes: 38 additions & 0 deletions go/vt/vttablet/tabletserver/query_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,44 @@ func TestQueryExecutorShouldConsolidate(t *testing.T) {
}
}

func TestGetConnectionLogStats(t *testing.T) {
db := setUpQueryExecutorTest(t)
defer db.Close()

ctx := context.Background()
tsv := newTestTabletServer(ctx, noFlags, db)
input := "select * from test_table limit 1"

// getConn() happy path
qre := newTestQueryExecutor(ctx, tsv, input, 0)
conn, err := qre.getConn()
assert.NoError(t, err)
assert.NotNil(t, conn)
assert.True(t, qre.logStats.WaitingForConnection > 0)

// getStreamConn() happy path
qre = newTestQueryExecutor(ctx, tsv, input, 0)
conn, err = qre.getStreamConn()
assert.NoError(t, err)
assert.NotNil(t, conn)
assert.True(t, qre.logStats.WaitingForConnection > 0)

// Close the db connection to induce connection errors
db.Close()

// getConn() error path
qre = newTestQueryExecutor(ctx, tsv, input, 0)
_, err = qre.getConn()
assert.Error(t, err)
assert.True(t, qre.logStats.WaitingForConnection > 0)

// getStreamConn() error path
qre = newTestQueryExecutor(ctx, tsv, input, 0)
_, err = qre.getStreamConn()
assert.Error(t, err)
assert.True(t, qre.logStats.WaitingForConnection > 0)
}

type executorFlags int64

const (
Expand Down

0 comments on commit 416d7f4

Please sign in to comment.