From c724737593281935890ccd66fa4248f8e565eea2 Mon Sep 17 00:00:00 2001 From: Xin Hao Zhang Date: Mon, 30 Aug 2021 11:19:37 -0400 Subject: [PATCH] sql: deflake TestStatusAPIStatements and TestStatusAPICombinedStatements Fixes: #69557 As mentioned in #69533, we have a race condition in tests where we request statements stats with start=now. In these tests we expect to see no results, but because in-memory stats have the aggregated_ts field set on iterator return, depending on the time the test is run we might see results returned. For example, suppose we insert stats at 15:45. If we then request stats at 16:00, requesting only stats aggregated after or at the current time, i.e. start=16:00, the aggregated_ts for in-memory stats will be set to 16:00 and thus we will see results returned. To deflake these tests, we stub the aggregated_ts field to a predetermined value. Release justification: non-production code changes Release note: None --- pkg/server/status_test.go | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/server/status_test.go b/pkg/server/status_test.go index 3472012e7df9..aa09eee7ade0 100644 --- a/pkg/server/status_test.go +++ b/pkg/server/status_test.go @@ -1813,9 +1813,17 @@ func TestStatusAPIStatements(t *testing.T) { defer leaktest.AfterTest(t)() defer log.Scope(t).Close(t) - params, _ := tests.CreateTestServerParams() + // Aug 30 2021 19:50:00 GMT+0000 + aggregatedTs := int64(1630353000) testCluster := serverutils.StartNewTestCluster(t, 3, base.TestClusterArgs{ - ServerArgs: params, + ServerArgs: base.TestServerArgs{ + Knobs: base.TestingKnobs{ + SQLStatsKnobs: &sqlstats.TestingKnobs{ + AOSTClause: "AS OF SYSTEM TIME '-1us'", + StubTimeNow: func() time.Time { return timeutil.Unix(aggregatedTs, 0) }, + }, + }, + }, }) defer testCluster.Stopper().Stop(context.Background()) @@ -1897,17 +1905,24 @@ func TestStatusAPIStatements(t *testing.T) { // Test no params testPath("statements", expectedStatements) // Test combined=true forwards to CombinedStatements - nowInSecs := timeutil.Now().Unix() - testPath(fmt.Sprintf("statements?combined=true&start=%d", nowInSecs), nil) + testPath(fmt.Sprintf("statements?combined=true&start=%d", aggregatedTs+60), nil) } func TestStatusAPICombinedStatements(t *testing.T) { defer leaktest.AfterTest(t)() defer log.Scope(t).Close(t) - params, _ := tests.CreateTestServerParams() + // Aug 30 2021 19:50:00 GMT+0000 + aggregatedTs := int64(1630353000) testCluster := serverutils.StartNewTestCluster(t, 3, base.TestClusterArgs{ - ServerArgs: params, + ServerArgs: base.TestServerArgs{ + Knobs: base.TestingKnobs{ + SQLStatsKnobs: &sqlstats.TestingKnobs{ + AOSTClause: "AS OF SYSTEM TIME '-1us'", + StubTimeNow: func() time.Time { return timeutil.Unix(aggregatedTs, 0) }, + }, + }, + }, }) defer testCluster.Stopper().Stop(context.Background()) @@ -1989,11 +2004,13 @@ func TestStatusAPICombinedStatements(t *testing.T) { // Test with no query params testPath("combinedstmts", expectedStatements) - nowInSecs := timeutil.Now().Unix() - // Test with end = now; should give the same results as get all. - testPath(fmt.Sprintf("combinedstmts?end=%d", nowInSecs), expectedStatements) - // Test with start = 1 hour ago end = now; should give the same results as get all. - testPath(fmt.Sprintf("combinedstmts?start=%d&end=%d", nowInSecs-3600, nowInSecs), expectedStatements) + oneMinAfterAggregatedTs := aggregatedTs + 60 + // Test with end = 1 min after aggregatedTs; should give the same results as get all. + testPath(fmt.Sprintf("combinedstmts?end=%d", oneMinAfterAggregatedTs), expectedStatements) + // Test with start = 1 hour before aggregatedTs end = 1 min after aggregatedTs; should give same results as get all. + testPath(fmt.Sprintf("combinedstmts?start=%d&end=%d", aggregatedTs-3600, oneMinAfterAggregatedTs), expectedStatements) + // Test with start = 1 min after aggregatedTs; should give no results + testPath(fmt.Sprintf("combinedstmts?start=%d", oneMinAfterAggregatedTs), nil) } func TestListSessionsSecurity(t *testing.T) {