-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathflush_test.go
892 lines (737 loc) · 29.2 KB
/
flush_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
// Copyright 2021 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 persistedsqlstats_test
import (
"context"
gosql "database/sql"
"fmt"
"math"
"net/url"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/persistedsqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
type testCase struct {
query string
fingerprint string
count int64
}
var testQueries = []testCase{
{
query: "SELECT 1",
fingerprint: "SELECT _",
count: 3,
},
{
query: "SELECT 1, 2, 3",
fingerprint: "SELECT _, _, _",
count: 10,
},
{
query: "SELECT 1, 1 WHERE 1 < 10",
fingerprint: "SELECT _, _ WHERE _ < _",
count: 7,
},
}
func TestSQLStatsFlush(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
fakeTime := stubTime{
aggInterval: time.Hour,
}
fakeTime.setTime(timeutil.Now())
testCluster := serverutils.StartNewTestCluster(t, 3 /* numNodes */, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
SQLStatsKnobs: &sqlstats.TestingKnobs{
StubTimeNow: fakeTime.Now,
},
},
},
})
ctx := context.Background()
defer testCluster.Stopper().Stop(ctx)
stopper := stop.NewStopper()
defer stopper.Stop(ctx)
firstServer := testCluster.Server(0 /* idx */)
secondServer := testCluster.Server(1 /* idx */)
firstPgURL, firstServerConnCleanup := sqlutils.PGUrl(
t, firstServer.ServingSQLAddr(), "CreateConnections" /* prefix */, url.User(username.RootUser))
defer firstServerConnCleanup()
secondPgURL, secondServerConnCleanup := sqlutils.PGUrl(
t, secondServer.ServingSQLAddr(), "CreateConnections" /* prefix */, url.User(username.RootUser))
defer secondServerConnCleanup()
pgFirstSQLConn, err := gosql.Open("postgres", firstPgURL.String())
require.NoError(t, err)
firstSQLConn := sqlutils.MakeSQLRunner(pgFirstSQLConn)
pgSecondSQLConn, err := gosql.Open("postgres", secondPgURL.String())
require.NoError(t, err)
secondSQLConn := sqlutils.MakeSQLRunner(pgSecondSQLConn)
firstServerSQLStats := firstServer.SQLServer().(*sql.Server).GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats)
secondServerSQLStats := secondServer.SQLServer().(*sql.Server).GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats)
defer func() {
err := pgFirstSQLConn.Close()
require.NoError(t, err)
err = pgSecondSQLConn.Close()
require.NoError(t, err)
}()
firstSQLConn.Exec(t, "SET application_name = 'flush_unit_test'")
secondSQLConn.Exec(t, "SET application_name = 'flush_unit_test'")
require.NoError(t, err)
// Regular inserts.
{
for _, tc := range testQueries {
for i := int64(0); i < tc.count; i++ {
firstSQLConn.Exec(t, tc.query)
}
}
verifyInMemoryStatsCorrectness(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsEmpty(t, testQueries, secondServerSQLStats)
firstServerSQLStats.Flush(ctx)
secondServerSQLStats.Flush(ctx)
verifyInMemoryStatsEmpty(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsEmpty(t, testQueries, secondServerSQLStats)
// For each test case, we verify that it's being properly inserted exactly
// once and it is exactly executed tc.count number of times.
for _, tc := range testQueries {
verifyNumOfInsertedEntries(t, secondSQLConn, tc.fingerprint, firstServer.NodeID(), 1 /* expectedStmtEntryCnt */, 1 /* expectedTxnEntryCtn */)
verifyInsertedFingerprintExecCount(t, secondSQLConn, tc.fingerprint, fakeTime.getAggTimeTs(), firstServer.NodeID(), tc.count)
}
}
// We insert the same data during the same aggregation window to ensure that
// no new entries will be created but the statistics is updated.
{
for i := range testQueries {
// Increment the execution count.
testQueries[i].count++
for execCnt := int64(0); execCnt < testQueries[i].count; execCnt++ {
firstSQLConn.Exec(t, testQueries[i].query)
}
}
verifyInMemoryStatsCorrectness(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsEmpty(t, testQueries, secondServerSQLStats)
firstServerSQLStats.Flush(ctx)
secondServerSQLStats.Flush(ctx)
verifyInMemoryStatsEmpty(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsEmpty(t, testQueries, secondServerSQLStats)
for _, tc := range testQueries {
verifyNumOfInsertedEntries(t, secondSQLConn, tc.fingerprint, firstServer.NodeID(), 1 /* expectedStmtEntryCnt */, 1 /* expectedTxnEntryCtn */)
// The execution count is doubled here because we execute all of the
// statements here in the same aggregation interval.
verifyInsertedFingerprintExecCount(t, secondSQLConn, tc.fingerprint, fakeTime.getAggTimeTs(), firstServer.NodeID(), tc.count+tc.count-1 /* expectedCount */)
}
}
// We change the time to be in a different aggregation window.
{
fakeTime.setTime(fakeTime.Now().Add(time.Hour * 3))
for _, tc := range testQueries {
for i := int64(0); i < tc.count; i++ {
firstSQLConn.Exec(t, tc.query)
}
}
verifyInMemoryStatsCorrectness(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsEmpty(t, testQueries, secondServerSQLStats)
firstServerSQLStats.Flush(ctx)
secondServerSQLStats.Flush(ctx)
verifyInMemoryStatsEmpty(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsEmpty(t, testQueries, secondServerSQLStats)
for _, tc := range testQueries {
// We expect exactly 2 entries since we are in a different aggregation window.
verifyNumOfInsertedEntries(t, secondSQLConn, tc.fingerprint, firstServer.NodeID(), 2 /* expectedStmtEntryCnt */, 2 /* expectedTxnEntryCtn */)
verifyInsertedFingerprintExecCount(t, secondSQLConn, tc.fingerprint, fakeTime.getAggTimeTs(), firstServer.NodeID(), tc.count)
}
}
// We run queries in a different server and trigger the flush.
{
for _, tc := range testQueries {
for i := int64(0); i < tc.count; i++ {
secondSQLConn.Exec(t, tc.query)
require.NoError(t, err)
}
}
verifyInMemoryStatsEmpty(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsCorrectness(t, testQueries, secondServerSQLStats)
firstServerSQLStats.Flush(ctx)
secondServerSQLStats.Flush(ctx)
verifyInMemoryStatsEmpty(t, testQueries, firstServerSQLStats)
verifyInMemoryStatsEmpty(t, testQueries, secondServerSQLStats)
// Ensure that we encode the correct node_id for the new entry and did not
// accidentally tamper the entries written by another server.
for _, tc := range testQueries {
verifyNumOfInsertedEntries(t, firstSQLConn, tc.fingerprint, secondServer.NodeID(), 1 /* expectedStmtEntryCnt */, 1 /* expectedTxnEntryCtn */)
verifyInsertedFingerprintExecCount(t, firstSQLConn, tc.fingerprint, fakeTime.getAggTimeTs(), secondServer.NodeID(), tc.count)
verifyNumOfInsertedEntries(t, secondSQLConn, tc.fingerprint, firstServer.NodeID(), 2 /* expectedStmtEntryCnt */, 2 /* expectedTxnEntryCtn */)
verifyInsertedFingerprintExecCount(t, secondSQLConn, tc.fingerprint, fakeTime.getAggTimeTs(), firstServer.NodeID(), tc.count)
}
}
}
func TestSQLStatsInitialDelay(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
params, _ := tests.CreateTestServerParams()
s, _, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.Background())
initialNextFlushAt := s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).GetNextFlushAt()
// Since we introduced jitter in our flush interval, the next flush time
// is not entirely deterministic. However, we can still have an upperbound
// on this value.
maxNextRunAt :=
timeutil.Now().Add(persistedsqlstats.SQLStatsFlushInterval.Default() * 2)
require.True(t, maxNextRunAt.After(initialNextFlushAt),
"expected latest nextFlushAt to be %s, but found %s", maxNextRunAt, initialNextFlushAt)
}
func TestSQLStatsMinimumFlushInterval(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
fakeTime := stubTime{
aggInterval: time.Hour,
}
fakeTime.setTime(timeutil.Now())
params, _ := tests.CreateTestServerParams()
params.Knobs.SQLStatsKnobs = &sqlstats.TestingKnobs{
StubTimeNow: fakeTime.Now,
}
s, conn, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.Background())
sqlConn := sqlutils.MakeSQLRunner(conn)
sqlConn.Exec(t, "SET CLUSTER SETTING sql.stats.flush.minimum_interval = '10m'")
sqlConn.Exec(t, "SET application_name = 'min_flush_test'")
sqlConn.Exec(t, "SELECT 1")
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
sqlConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.statement_statistics
WHERE app_name = 'min_flush_test'
`, [][]string{{"1"}})
sqlConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.transaction_statistics
WHERE app_name = 'min_flush_test'
`, [][]string{{"1"}})
// Since by default, the minimum flush interval is 10 minutes, a subsequent
// flush should be no-op.
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
sqlConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.statement_statistics
WHERE app_name = 'min_flush_test'
`, [][]string{{"1"}})
sqlConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.transaction_statistics
WHERE app_name = 'min_flush_test'
`, [][]string{{"1"}})
// We manually set the time to past the minimum flush interval, now the flush
// should succeed.
fakeTime.setTime(fakeTime.Now().Add(time.Hour))
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
sqlConn.CheckQueryResults(t, `
SELECT count(*) > 1
FROM system.statement_statistics
WHERE app_name = 'min_flush_test'
`, [][]string{{"true"}})
}
func TestInMemoryStatsDiscard(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
params, _ := tests.CreateTestServerParams()
s, conn, _ := serverutils.StartServer(t, params)
observer :=
serverutils.OpenDBConn(t, s.ServingSQLAddr(), "", false /* insecure */, s.Stopper())
defer s.Stopper().Stop(context.Background())
sqlConn := sqlutils.MakeSQLRunner(conn)
sqlConn.Exec(t,
"SET CLUSTER SETTING sql.stats.flush.minimum_interval = '10m'")
observerConn := sqlutils.MakeSQLRunner(observer)
t.Run("flush_disabled", func(t *testing.T) {
sqlConn.Exec(t,
"SET CLUSTER SETTING sql.stats.flush.force_cleanup.enabled = true")
sqlConn.Exec(t,
"SET CLUSTER SETTING sql.stats.flush.enabled = false")
sqlConn.Exec(t, "SET application_name = 'flush_disabled_test'")
sqlConn.Exec(t, "SELECT 1")
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM crdb_internal.statement_statistics
WHERE app_name = 'flush_disabled_test'
`, [][]string{{"1"}})
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM crdb_internal.statement_statistics
WHERE app_name = 'flush_disabled_test'
`, [][]string{{"0"}})
})
t.Run("flush_enabled", func(t *testing.T) {
// Now turn back SQL Stats flush. If the flush is aborted due to violating
// minimum flush interval constraint, we should not be clearing in-memory
// stats.
sqlConn.Exec(t,
"SET CLUSTER SETTING sql.stats.flush.enabled = true")
sqlConn.Exec(t, "SET application_name = 'flush_enabled_test'")
sqlConn.Exec(t, "SELECT 1")
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM crdb_internal.statement_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"1"}})
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM crdb_internal.transaction_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"1"}})
// First flush should flush everything into the system tables.
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.statement_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"1"}})
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.transaction_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"1"}})
sqlConn.Exec(t, "SELECT 1,1")
// Second flush should be aborted due to violating the minimum flush
// interval requirement. Though the data should still remain in-memory.
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.statement_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"1"}})
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM system.transaction_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"1"}})
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM crdb_internal.statement_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"2"}})
observerConn.CheckQueryResults(t, `
SELECT count(*)
FROM crdb_internal.transaction_statistics
WHERE app_name = 'flush_enabled_test'
`, [][]string{{"2"}})
})
}
func TestSQLStatsGatewayNodeSetting(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
params, _ := tests.CreateTestServerParams()
s, conn, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.Background())
sqlConn := sqlutils.MakeSQLRunner(conn)
// Gateway Node ID enabled, so should persist the value.
sqlConn.Exec(t, "SET CLUSTER SETTING sql.metrics.statement_details.gateway_node.enabled = true")
sqlConn.Exec(t, "SET application_name = 'gateway_enabled'")
sqlConn.Exec(t, "SELECT 1")
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
verifyNodeID(t, sqlConn, "SELECT _", true, "gateway_enabled")
// Gateway Node ID disabled, so shouldn't persist the value on the node_id column, but it should
// still store the value on the statistics column.
sqlConn.Exec(t, "SET CLUSTER SETTING sql.metrics.statement_details.gateway_node.enabled = false")
sqlConn.Exec(t, "SET application_name = 'gateway_disabled'")
sqlConn.Exec(t, "SELECT 1")
s.SQLServer().(*sql.Server).
GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats).Flush(ctx)
verifyNodeID(t, sqlConn, "SELECT _", false, "gateway_disabled")
}
func countStats(t *testing.T, sqlConn *sqlutils.SQLRunner) (numStmtStats int64, numTxnStats int64) {
sqlConn.QueryRow(t, `
SELECT count(*)
FROM system.statement_statistics`).Scan(&numStmtStats)
sqlConn.QueryRow(t, `
SELECT count(*)
FROM system.transaction_statistics`).Scan(&numTxnStats)
return numStmtStats, numTxnStats
}
func TestSQLStatsPersistedLimitReached(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
params, _ := tests.CreateTestServerParams()
s, conn, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.Background())
sqlConn := sqlutils.MakeSQLRunner(conn)
pss := s.SQLServer().(*sql.Server).GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats)
// 1. Flush then count to get the initial number of rows.
pss.Flush(ctx)
stmtStatsCount, txnStatsCount := countStats(t, sqlConn)
const additionalStatements = int64(3)
sqlConn.Exec(t, "SELECT 1")
sqlConn.Exec(t, "SELECT 1, 2")
sqlConn.Exec(t, "SELECT 1, 2, 3")
pss.Flush(ctx)
stmtStatsCountFlush2, txnStatsCountFlush2 := countStats(t, sqlConn)
// 2. After flushing and counting a second time, we should see at least 3 more rows.
require.GreaterOrEqual(t, stmtStatsCountFlush2-stmtStatsCount, additionalStatements)
require.GreaterOrEqual(t, txnStatsCountFlush2-txnStatsCount, additionalStatements)
// 3. Set sql.stats.persisted_rows.max according to the smallest table.
smallest := math.Min(float64(stmtStatsCountFlush2), float64(txnStatsCountFlush2))
maxRows := int(math.Floor(smallest / 1.5))
sqlConn.Exec(t, fmt.Sprintf("SET CLUSTER SETTING sql.stats.persisted_rows.max=%d", maxRows))
// 4. Wait for the cluster setting to be applied.
testutils.SucceedsSoon(t, func() error {
var appliedSetting int
row := sqlConn.QueryRow(t, "SHOW CLUSTER SETTING sql.stats.persisted_rows.max")
row.Scan(&appliedSetting)
if appliedSetting != maxRows {
return errors.Newf("waiting for sql.stats.persisted_rows.max to be applied")
}
return nil
})
sqlConn.Exec(t, "SELECT 1, 2, 3, 4")
sqlConn.Exec(t, "SELECT 1, 2, 3, 4, 5")
sqlConn.Exec(t, "SELECT 1, 2, 3, 4, 6, 7")
pss.Flush(ctx)
stmtStatsCountFlush3, txnStatsCountFlush3 := countStats(t, sqlConn)
// 5. Assert that neither table has grown in length.
require.Equal(t, stmtStatsCountFlush3, stmtStatsCountFlush2)
require.Equal(t, txnStatsCountFlush3, txnStatsCountFlush2)
}
func TestSQLStatsReadLimitSizeOnLockedTable(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, conn, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(context.Background())
sqlConn := sqlutils.MakeSQLRunner(conn)
sqlConn.Exec(t, `INSERT INTO system.users VALUES ('node', NULL, true, 3); GRANT node TO root`)
waitForFollowerReadTimestamp(t, sqlConn)
pss := s.SQLServer().(*sql.Server).GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats)
const minNumExpectedStmts = int64(3)
// Maximum number of persisted rows less than minNumExpectedStmts/1.5
const maxNumPersistedRows = 1
sqlConn.Exec(t, "SELECT 1")
sqlConn.Exec(t, "SELECT 1, 2")
sqlConn.Exec(t, "SELECT 1, 2, 3")
pss.Flush(ctx)
stmtStatsCountFlush, _ := countStats(t, sqlConn)
// Ensure we have some rows in system.statement_statistics
require.GreaterOrEqual(t, stmtStatsCountFlush, minNumExpectedStmts)
// Set sql.stats.persisted_rows.max
sqlConn.Exec(t, fmt.Sprintf("SET CLUSTER SETTING sql.stats.persisted_rows.max=%d", maxNumPersistedRows))
testutils.SucceedsSoon(t, func() error {
var appliedSetting int
row := sqlConn.QueryRow(t, "SHOW CLUSTER SETTING sql.stats.persisted_rows.max")
row.Scan(&appliedSetting)
if appliedSetting != maxNumPersistedRows {
return errors.Newf("waiting for sql.stats.persisted_rows.max to be applied")
}
return nil
})
// Begin a transaction.
sqlConn.Exec(t, "BEGIN")
// Lock the table. Create a state of contention.
sqlConn.Exec(t, "SELECT * FROM system.statement_statistics FOR UPDATE")
// Ensure that we can read from the table despite it being locked, due to the follower read (AOST).
// Expect that the number of statements in the table exceeds sql.stats.persisted_rows.max * 1.5
// (meaning that the limit will be reached) and no error. We need SucceedsSoon here for the follower
// read timestamp to catch up enough for this state to be reached.
testutils.SucceedsSoon(t, func() error {
limitReached, err := pss.StmtsLimitSizeReached(ctx)
if limitReached != true {
return errors.New("waiting for limit reached to be true")
}
return err
})
// Close the transaction.
sqlConn.Exec(t, "COMMIT")
}
func TestSQLStatsPlanSampling(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
stubTime := &stubTime{}
setTime := func(inputTime string) {
parsedTime, err := time.Parse(time.RFC3339, inputTime)
require.NoError(t, err)
stubTime.setTime(parsedTime)
}
params, _ := tests.CreateTestServerParams()
params.Knobs.SQLStatsKnobs.(*sqlstats.TestingKnobs).StubTimeNow = stubTime.Now
ctx := context.Background()
s, conn, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(ctx)
sqlRun := sqlutils.MakeSQLRunner(conn)
dbName := "defaultdb"
appName := fmt.Sprintf("TestSQLStatsPlanSampling_%s", uuid.FastMakeV4().String())
sqlRun.Exec(t, "SET application_name = $1", appName)
sqlStats := s.SQLServer().(*sql.Server).GetSQLStatsProvider().(*persistedsqlstats.PersistedSQLStats)
appStats := sqlStats.GetApplicationStats(appName, false)
sqlRun.Exec(t, `SET CLUSTER SETTING sql.metrics.statement_details.plan_collection.enabled = true;`)
sqlRun.Exec(t, `SET CLUSTER SETTING sql.txn_stats.sample_rate = 0;`)
validateSample := func(fingerprint string, implicitTxn bool, expectedPreviouslySampledState bool, expectedSavePlanForStatsState bool) {
previouslySampled, savePlanForStats := appStats.ShouldSample(
fingerprint,
implicitTxn,
dbName,
)
errMessage := fmt.Sprintf("validate: %s, implicit: %t expected sample before: %t, actual sample before: %t, exptected save plan: %t actual save plan: %t\n",
fingerprint, implicitTxn, expectedPreviouslySampledState, previouslySampled, expectedSavePlanForStatsState, savePlanForStats)
require.Equal(t, expectedSavePlanForStatsState, savePlanForStats, errMessage)
require.Equal(t, expectedPreviouslySampledState, previouslySampled, errMessage)
}
setTime("2021-09-20T15:00:00Z")
// Logical plan should be sampled here, since we have not collected logical plan
// at all.
validateSample("SELECT _", true, false, true)
// Execute the query to trigger a collection of logical plan.
// (db_name=defaultdb implicitTxn=true fingerprint=SELECT _)
_, err := conn.ExecContext(ctx, "SELECT 1")
require.NoError(t, err)
// Ensure that if a query is to be subsequently executed, it will not cause
// logical plan sampling.
validateSample("SELECT _", true, true, false)
// However, if we are to execute the same statement but under explicit
// transaction, the plan will still need to be sampled.
validateSample("SELECT _", false, false, true)
// Execute the statement under explicit transaction.
// (db_name=defaultdb implicitTxn=false fingerprint=SELECT _)
tx, err := conn.BeginTx(ctx, &gosql.TxOptions{})
require.NoError(t, err)
_, err = tx.ExecContext(ctx, "SELECT 1")
require.NoError(t, err)
require.NoError(t, tx.Commit())
// Ensure that the subsequent execution of the query will not cause logical plan
// collection.
validateSample("SELECT _", false, true, false)
// Set the time to the future and ensure we will resample the logical plan.
setTime("2021-09-20T15:05:01Z")
// If tracing is not enabled the statement will not be sampled. To update the
// save plan for stats last sampled time the statement must have tracing enabled.
_, err = conn.ExecContext(ctx, "SET CLUSTER SETTING sql.txn_stats.sample_rate = 1;")
require.NoError(t, err)
validateSample("SELECT _", true, true, true)
// implicit txn
_, err = conn.ExecContext(ctx, "SELECT 1")
require.NoError(t, err)
validateSample("SELECT _", true, true, false)
// explicit txn
validateSample("SELECT _", false, true, true)
tx, err = conn.BeginTx(ctx, &gosql.TxOptions{})
require.NoError(t, err)
_, err = tx.ExecContext(ctx, "SELECT 1")
require.NoError(t, err)
require.NoError(t, tx.Commit())
// Ensure that the subsequent execution of the query will not cause logical plan
// collection.
validateSample("SELECT _", false, true, false)
}
type stubTime struct {
syncutil.RWMutex
t time.Time
aggInterval time.Duration
timeStubbed bool
}
func (s *stubTime) setTime(t time.Time) {
s.RWMutex.Lock()
defer s.RWMutex.Unlock()
s.t = t
s.timeStubbed = true
}
func (s *stubTime) getAggTimeTs() time.Time {
s.RWMutex.Lock()
defer s.RWMutex.Unlock()
return s.t.Truncate(s.aggInterval)
}
// Now implements the testing knob interface for persistedsqlstats.Provider.
func (s *stubTime) Now() time.Time {
s.RWMutex.RLock()
defer s.RWMutex.RUnlock()
if s.timeStubbed {
return s.t
}
return timeutil.Now()
}
func verifyInsertedFingerprintExecCount(
t *testing.T,
sqlConn *sqlutils.SQLRunner,
fingerprint string,
ts time.Time,
nodeID roachpb.NodeID,
expectedCount int64,
) {
row := sqlConn.Query(t,
`
SELECT
(S.statistics -> 'statistics' ->> 'cnt')::INT AS stmtCount,
(T.statistics -> 'statistics' ->> 'cnt')::INT AS txnCount
FROM
system.transaction_statistics T,
system.statement_statistics S
WHERE S.metadata ->> 'query' = $1
AND T.aggregated_ts = $2
AND T.node_id = $3
AND T.app_name = 'flush_unit_test'
AND decode(T.metadata -> 'stmtFingerprintIDs' ->> 0, 'hex') = S.fingerprint_id
AND S.node_id = T.node_id
AND S.aggregated_ts = T.aggregated_ts
AND S.app_name = T.app_name
`, fingerprint, ts, nodeID)
require.True(t, row.Next(), "no stats found for fingerprint: %s", fingerprint)
var actualTxnExecCnt int64
var actualStmtExecCnt int64
err := row.Scan(&actualStmtExecCnt, &actualTxnExecCnt)
require.NoError(t, err)
require.Equal(t, expectedCount, actualStmtExecCnt, "fingerprint: %s", fingerprint)
require.Equal(t, expectedCount, actualTxnExecCnt, "fingerprint: %s", fingerprint)
require.False(t, row.Next(), "more than one rows found for fingerprint: %s", fingerprint)
require.NoError(t, row.Close())
}
func verifyNumOfInsertedEntries(
t *testing.T,
sqlConn *sqlutils.SQLRunner,
fingerprint string,
nodeID roachpb.NodeID,
expectedStmtEntryCnt, expectedTxnEntryCnt int64,
) {
row2 := sqlConn.DB.QueryRowContext(context.Background(),
`
SELECT
encode(fingerprint_id, 'hex'),
count(*)
FROM
system.statement_statistics
WHERE
metadata ->> 'query' = $1 AND
node_id = $2 AND
app_name = 'flush_unit_test'
GROUP BY
(fingerprint_id, node_id)
`, fingerprint, nodeID)
var stmtFingerprintID string
var numOfInsertedStmtEntry int64
e := row2.Scan(&stmtFingerprintID, &numOfInsertedStmtEntry)
require.NoError(t, e)
require.Equal(t, expectedStmtEntryCnt, numOfInsertedStmtEntry, "fingerprint: %s", fingerprint)
row1 := sqlConn.DB.QueryRowContext(context.Background(), fmt.Sprintf(
`
SELECT
count(*)
FROM
system.transaction_statistics
WHERE
(metadata -> 'stmtFingerprintIDs' ->> 0) = '%s' AND
node_id = $1 AND
app_name = 'flush_unit_test'
GROUP BY
(fingerprint_id, node_id)
`, stmtFingerprintID), nodeID)
var numOfInsertedTxnEntry int64
err := row1.Scan(&numOfInsertedTxnEntry)
require.NoError(t, err)
require.Equal(t, expectedTxnEntryCnt, numOfInsertedTxnEntry, "fingerprint: %s", fingerprint)
}
func verifyInMemoryStatsCorrectness(
t *testing.T, tcs []testCase, statsProvider *persistedsqlstats.PersistedSQLStats,
) {
for _, tc := range tcs {
err := statsProvider.SQLStats.IterateStatementStats(context.Background(), sqlstats.IteratorOptions{}, func(ctx context.Context, statistics *appstatspb.CollectedStatementStatistics) error {
if tc.fingerprint == statistics.Key.Query {
require.Equal(t, tc.count, statistics.Stats.Count, "fingerprint: %s", tc.fingerprint)
}
// All the queries should be under 1 minute
require.Less(t, statistics.Stats.ServiceLat.Mean, 60.0)
require.Less(t, statistics.Stats.RunLat.Mean, 60.0)
require.Less(t, statistics.Stats.PlanLat.Mean, 60.0)
require.Less(t, statistics.Stats.ParseLat.Mean, 60.0)
require.Less(t, statistics.Stats.IdleLat.Mean, 60.0)
require.GreaterOrEqual(t, statistics.Stats.ServiceLat.Mean, 0.0)
require.GreaterOrEqual(t, statistics.Stats.RunLat.Mean, 0.0)
require.GreaterOrEqual(t, statistics.Stats.PlanLat.Mean, 0.0)
require.GreaterOrEqual(t, statistics.Stats.ParseLat.Mean, 0.0)
require.GreaterOrEqual(t, statistics.Stats.IdleLat.Mean, 0.0)
return nil
})
require.NoError(t, err)
}
}
func verifyInMemoryStatsEmpty(
t *testing.T, tcs []testCase, statsProvider *persistedsqlstats.PersistedSQLStats,
) {
for _, tc := range tcs {
err := statsProvider.SQLStats.IterateStatementStats(context.Background(), sqlstats.IteratorOptions{}, func(ctx context.Context, statistics *appstatspb.CollectedStatementStatistics) error {
if tc.fingerprint == statistics.Key.Query {
require.Equal(t, 0 /* expected */, statistics.Stats.Count, "fingerprint: %s", tc.fingerprint)
}
return nil
})
require.NoError(t, err)
}
}
func verifyNodeID(
t *testing.T,
sqlConn *sqlutils.SQLRunner,
fingerprint string,
gatewayEnabled bool,
appName string,
) {
row := sqlConn.DB.QueryRowContext(context.Background(),
`
SELECT
node_id,
statistics -> 'statistics' ->> 'nodes' as nodes
FROM
system.statement_statistics
WHERE
metadata ->> 'query' = $1 AND
app_name = $2
`, fingerprint, appName)
var gatewayNodeID int64
var allNodesIds string
e := row.Scan(&gatewayNodeID, &allNodesIds)
require.NoError(t, e)
nodeID := int64(1)
if !gatewayEnabled {
nodeID = int64(0)
}
require.Equal(t, nodeID, gatewayNodeID, "Gateway NodeID")
require.Equal(t, "[1]", allNodesIds, "All NodeIDs from statistics")
}
func waitForFollowerReadTimestamp(t *testing.T, sqlConn *sqlutils.SQLRunner) {
stmt := "SELECT * from system.descriptor AS OF SYSTEM TIME follower_read_timestamp() LIMIT 1"
testutils.SucceedsSoon(t, func() error {
_, err := sqlConn.DB.ExecContext(context.Background(), stmt)
return err
})
}