-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
colfetcher: fix the bytes read statistic collection
During 21.2 release we adjusted the `cFetcher` to be `Close`d eagerly when it is returning the zero-length batch. This was done in order to release some references in order for the memory to be GCed sooner; additionally, the `cFetcher` started being used for the index join where the fetcher is restarted from scratch for every batch of spans, so it seemed reasonable to close it automatically. However, that eager closure broke "bytes read" statistic collection since the `row.KVFetcher` was responsible for providing it, and we were zeroing it out. This commit fixes this problem by the `cFetcher` memorizing the number of bytes it has read in `Close`. Some care needs to be taken to not double-count the bytes read in the index join, so a couple of helper methods have been introduced. Additionally this commit applies the same eager-close optimization to the `cFetcher` when the last batch is returned (which makes it so that if we've just exhausted all KVs, we close the fetcher - previously, we would set the zero length on the batch and might never get into `stateFinished`). Release note (bug fix): Previously, CockroachDB could incorrectly report `KV bytes read` statistic in `EXPLAIN ANALYZE` output. The bug is present only in 21.2.x versions.
- Loading branch information
1 parent
677ea44
commit 174a0d6
Showing
6 changed files
with
104 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package colfetcher_test | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestBytesRead verifies that the ColBatchScan and the ColIndexJoin correctly | ||
// report the number of bytes read. | ||
func TestBytesRead(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
testClusterArgs := base.TestClusterArgs{ReplicationMode: base.ReplicationAuto} | ||
tc := testcluster.StartTestCluster(t, 1, testClusterArgs) | ||
ctx := context.Background() | ||
defer tc.Stopper().Stop(ctx) | ||
|
||
conn := tc.Conns[0] | ||
|
||
// Create the table with disabled automatic table stats collection. The | ||
// stats collection is disabled so that the ColBatchScan would read the | ||
// first row in one batch and then the second row in another batch. | ||
_, err := conn.ExecContext(ctx, ` | ||
SET CLUSTER SETTING sql.stats.automatic_collection.enabled=false; | ||
CREATE TABLE t (a INT PRIMARY KEY, b INT, c INT, INDEX(b)); | ||
INSERT INTO t VALUES (1, 1, 1), (2, 2, 2); | ||
`) | ||
require.NoError(t, err) | ||
|
||
// Run the query that reads from the secondary index and then performs an | ||
// index join against the primary index. | ||
query := "EXPLAIN ANALYZE SELECT * FROM t@t_b_idx" | ||
kvBytesReadRegex := regexp.MustCompile(`KV bytes read: (\d+) B`) | ||
matchIdx := 0 | ||
rows, err := conn.QueryContext(ctx, query) | ||
require.NoError(t, err) | ||
for rows.Next() { | ||
var res string | ||
require.NoError(t, rows.Scan(&res)) | ||
if matches := kvBytesReadRegex.FindStringSubmatch(res); len(matches) > 0 { | ||
bytesRead, err := strconv.Atoi(matches[1]) | ||
require.NoError(t, err) | ||
// We're only interested in 'bytes read' statistic being non-zero. | ||
require.Greater( | ||
t, bytesRead, 0, "expected bytes read to be greater than zero", | ||
) | ||
matchIdx++ | ||
} | ||
} | ||
} |
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
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