-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
ss_mem_storage.go
1000 lines (865 loc) · 32.1 KB
/
ss_mem_storage.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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 sqlstats is a subsystem that is responsible for tracking the
// statistics of statements and transactions.
package ssmemstorage
import (
"bytes"
"context"
"encoding/json"
"fmt"
"time"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
// TODO(arul): The fields on stmtKey should really be immutable fields on
// stmtStats which are set once (on first addition to the map). Instead, we
// should use stmtFingerprintID (which is a hashed string of the fields below) as the
// stmtKey.
type stmtKey struct {
sampledPlanKey
planHash uint64
transactionFingerprintID appstatspb.TransactionFingerprintID
}
// sampledPlanKey is used by the Optimizer to determine if we should build a full EXPLAIN plan.
type sampledPlanKey struct {
stmtNoConstants string
implicitTxn bool
database string
}
func (p sampledPlanKey) size() int64 {
return int64(unsafe.Sizeof(p)) + int64(len(p.stmtNoConstants)) + int64(len(p.database))
}
func (s stmtKey) String() string {
return s.stmtNoConstants
}
func (s stmtKey) size() int64 {
return s.sampledPlanKey.size() + int64(unsafe.Sizeof(invalidStmtFingerprintID))
}
const invalidStmtFingerprintID = 0
// Container holds per-application statement and transaction statistics.
type Container struct {
st *cluster.Settings
appName string
// uniqueServerCount is a server level counter of all the unique fingerprints
uniqueServerCount *SQLStatsAtomicCounters
mu struct {
syncutil.RWMutex
// acc is the memory account that tracks memory allocations related to stmts
// and txns within this Container struct.
// Since currently we do not destroy the Container struct when we perform
// reset, we never close this account.
acc mon.BoundAccount
stmts map[stmtKey]*stmtStats
txns map[appstatspb.TransactionFingerprintID]*txnStats
// sampledPlanMetadataCache records when was the last time the plan was
// sampled. This data structure uses a subset of stmtKey as the key into
// in-memory dictionary in order to allow lookup for whether a plan has been
// sampled for a statement without needing to know the statement's
// transaction fingerprintID.
sampledPlanMetadataCache map[sampledPlanKey]time.Time
}
txnCounts transactionCounts
mon *mon.BytesMonitor
knobs *sqlstats.TestingKnobs
insights insights.Writer
latencyInformation insights.LatencyInformation
}
var _ sqlstats.ApplicationStats = &Container{}
// New returns a new instance of Container.
func New(
st *cluster.Settings,
uniqueServerCount *SQLStatsAtomicCounters,
mon *mon.BytesMonitor,
appName string,
knobs *sqlstats.TestingKnobs,
insightsWriter insights.Writer,
latencyInformation insights.LatencyInformation,
) *Container {
s := &Container{
st: st,
appName: appName,
mon: mon,
knobs: knobs,
insights: insightsWriter,
latencyInformation: latencyInformation,
uniqueServerCount: uniqueServerCount,
}
if mon != nil {
s.mu.acc = mon.MakeBoundAccount()
}
s.mu.stmts = make(map[stmtKey]*stmtStats)
s.mu.txns = make(map[appstatspb.TransactionFingerprintID]*txnStats)
s.mu.sampledPlanMetadataCache = make(map[sampledPlanKey]time.Time)
return s
}
// IterateAggregatedTransactionStats implements sqlstats.ApplicationStats
// interface.
func (s *Container) IterateAggregatedTransactionStats(
_ context.Context, _ sqlstats.IteratorOptions, visitor sqlstats.AggregatedTransactionVisitor,
) error {
txnStat := func() appstatspb.TxnStats {
s.txnCounts.mu.Lock()
defer s.txnCounts.mu.Unlock()
return s.txnCounts.mu.TxnStats
}()
err := visitor(s.appName, &txnStat)
if err != nil {
return errors.Wrap(err, "sql stats iteration abort")
}
return nil
}
// StmtStatsIterator returns an instance of StmtStatsIterator.
func (s *Container) StmtStatsIterator(options sqlstats.IteratorOptions) StmtStatsIterator {
return NewStmtStatsIterator(s, options)
}
// TxnStatsIterator returns an instance of TxnStatsIterator.
func (s *Container) TxnStatsIterator(options sqlstats.IteratorOptions) TxnStatsIterator {
return NewTxnStatsIterator(s, options)
}
// IterateStatementStats implements sqlstats.Provider interface.
func (s *Container) IterateStatementStats(
ctx context.Context, options sqlstats.IteratorOptions, visitor sqlstats.StatementVisitor,
) error {
iter := s.StmtStatsIterator(options)
for iter.Next() {
if err := visitor(ctx, iter.Cur()); err != nil {
return err
}
}
return nil
}
// IterateTransactionStats implements sqlstats.Provider interface.
func (s *Container) IterateTransactionStats(
ctx context.Context, options sqlstats.IteratorOptions, visitor sqlstats.TransactionVisitor,
) error {
iter := s.TxnStatsIterator(options)
for iter.Next() {
stats := iter.Cur()
if err := visitor(ctx, stats); err != nil {
return err
}
}
return nil
}
// NewTempContainerFromExistingStmtStats creates a new Container by ingesting a slice
// of serverpb.StatementsResponse_CollectedStatementStatistics sorted by
// Key.KeyData.App field.
// It consumes the first chunk of the slice where
// all entries in the chunk contains the identical appName. The remaining
// slice is returned as the result.
// It returns a nil slice once all entries in statistics are consumed.
func NewTempContainerFromExistingStmtStats(
statistics []serverpb.StatementsResponse_CollectedStatementStatistics,
) (
container *Container,
remaining []serverpb.StatementsResponse_CollectedStatementStatistics,
err error,
) {
if len(statistics) == 0 {
return nil, statistics, nil
}
appName := statistics[0].Key.KeyData.App
container = New(
nil, /* st */
nil, /* uniqueServerCount */
nil, /* mon */
appName,
nil, /* knobs */
nil, /* insights */
nil, /*latencyInformation */
)
for i := range statistics {
if currentAppName := statistics[i].Key.KeyData.App; currentAppName != appName {
return container, statistics[i:], nil
}
key := stmtKey{
sampledPlanKey: sampledPlanKey{
stmtNoConstants: statistics[i].Key.KeyData.Query,
implicitTxn: statistics[i].Key.KeyData.ImplicitTxn,
database: statistics[i].Key.KeyData.Database,
},
planHash: statistics[i].Key.KeyData.PlanHash,
transactionFingerprintID: statistics[i].Key.KeyData.TransactionFingerprintID,
}
stmtStats, _, throttled :=
container.getStatsForStmtWithKeyLocked(key, statistics[i].ID, true /* createIfNonexistent */)
if throttled {
return nil /* container */, nil /* remaining */, ErrFingerprintLimitReached
}
// This handles all the statistics fields.
stmtStats.mu.data.Add(&statistics[i].Stats)
// Setting all metadata fields.
if stmtStats.mu.data.SensitiveInfo.LastErr == "" {
stmtStats.mu.data.SensitiveInfo.LastErr = statistics[i].Stats.SensitiveInfo.LastErr
}
if stmtStats.mu.data.SensitiveInfo.MostRecentPlanTimestamp.Before(statistics[i].Stats.SensitiveInfo.MostRecentPlanTimestamp) {
stmtStats.mu.data.SensitiveInfo.MostRecentPlanDescription = statistics[i].Stats.SensitiveInfo.MostRecentPlanDescription
stmtStats.mu.data.SensitiveInfo.MostRecentPlanTimestamp = statistics[i].Stats.SensitiveInfo.MostRecentPlanTimestamp
}
stmtStats.mu.vectorized = statistics[i].Key.KeyData.Vec
stmtStats.mu.distSQLUsed = statistics[i].Key.KeyData.DistSQL
stmtStats.mu.fullScan = statistics[i].Key.KeyData.FullScan
stmtStats.mu.database = statistics[i].Key.KeyData.Database
stmtStats.mu.querySummary = statistics[i].Key.KeyData.QuerySummary
}
return container, nil /* remaining */, nil /* err */
}
func (s *Container) MaybeLogDiscardMessage(ctx context.Context) {
s.uniqueServerCount.maybeLogDiscardMessage(ctx)
}
// NewTempContainerFromExistingTxnStats creates a new Container by ingesting a slice
// of CollectedTransactionStatistics sorted by .StatsData.App field.
// It consumes the first chunk of the slice where all entries in the chunk
// contains the identical appName. The remaining slice is returned as the result.
// It returns a nil slice once all entries in statistics are consumed.
func NewTempContainerFromExistingTxnStats(
statistics []serverpb.StatementsResponse_ExtendedCollectedTransactionStatistics,
) (
container *Container,
remaining []serverpb.StatementsResponse_ExtendedCollectedTransactionStatistics,
err error,
) {
if len(statistics) == 0 {
return nil, statistics, nil
}
appName := statistics[0].StatsData.App
container = New(
nil, /* st */
nil, /* uniqueServerCount */
nil, /* mon */
appName,
nil, /* knobs */
nil, /* insights */
nil, /* latencyInformation */
)
for i := range statistics {
if currentAppName := statistics[i].StatsData.App; currentAppName != appName {
return container, statistics[i:], nil
}
txnStats, _, throttled :=
container.getStatsForTxnWithKeyLocked(
statistics[i].StatsData.TransactionFingerprintID,
statistics[i].StatsData.StatementFingerprintIDs,
true /* createIfNonexistent */)
if throttled {
return nil /* container */, nil /* remaining */, ErrFingerprintLimitReached
}
txnStats.mu.data.Add(&statistics[i].StatsData.Stats)
}
return container, nil /* remaining */, nil /* err */
}
// NewApplicationStatsWithInheritedOptions implements the
// sqlstats.ApplicationStats interface.
func (s *Container) NewApplicationStatsWithInheritedOptions() sqlstats.ApplicationStats {
s.mu.Lock()
defer s.mu.Unlock()
return New(
s.st,
// There is no need to constraint txn fingerprint limit since in temporary
// container, there will never be more than one transaction fingerprint.
nil, // uniqueServerCount
s.mon,
s.appName,
s.knobs,
s.insights,
s.latencyInformation,
)
}
type txnStats struct {
statementFingerprintIDs []appstatspb.StmtFingerprintID
mu struct {
syncutil.Mutex
data appstatspb.TransactionStatistics
}
}
func (t *txnStats) sizeUnsafe() int64 {
const txnStatsShallowSize = int64(unsafe.Sizeof(txnStats{}))
stmtFingerprintIDsSize := int64(cap(t.statementFingerprintIDs)) *
int64(unsafe.Sizeof(appstatspb.StmtFingerprintID(0)))
// t.mu.data might contain pointer types, so we subtract its shallow size
// and include the actual size.
dataSize := -int64(unsafe.Sizeof(appstatspb.TransactionStatistics{})) +
int64(t.mu.data.Size())
return txnStatsShallowSize + stmtFingerprintIDsSize + dataSize
}
func (t *txnStats) mergeStats(stats *appstatspb.TransactionStatistics) {
t.mu.Lock()
defer t.mu.Unlock()
t.mu.data.Add(stats)
}
// stmtStats holds per-statement statistics.
type stmtStats struct {
// ID is the statementFingerprintID constructed using the stmtKey fields.
ID appstatspb.StmtFingerprintID
// data contains all fields that are modified when new statements matching
// the stmtKey are executed, and therefore must be protected by a mutex.
mu struct {
syncutil.Mutex
// distSQLUsed records whether the last instance of this statement used
// distribution.
distSQLUsed bool
// vectorized records whether the last instance of this statement used
// vectorization.
vectorized bool
// fullScan records whether the last instance of this statement used a
// full table index scan.
fullScan bool
// database records the database from the session the statement
// was executed from.
database string
// querySummary records a summarized format of the query statement.
querySummary string
data appstatspb.StatementStatistics
}
}
func (s *stmtStats) sizeUnsafe() int64 {
const stmtStatsShallowSize = int64(unsafe.Sizeof(stmtStats{}))
databaseNameSize := int64(len(s.mu.database))
// s.mu.data might contain pointer tyeps, so we subtract its shallow size and
// include the actual size.
dataSize := -int64(unsafe.Sizeof(appstatspb.StatementStatistics{})) +
int64(s.mu.data.Size())
return stmtStatsShallowSize + databaseNameSize + dataSize
}
func (s *stmtStats) recordExecStats(stats execstats.QueryLevelStats) {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.data.ExecStats.Count++
count := s.mu.data.ExecStats.Count
s.mu.data.ExecStats.NetworkBytes.Record(count, float64(stats.NetworkBytesSent))
s.mu.data.ExecStats.MaxMemUsage.Record(count, float64(stats.MaxMemUsage))
s.mu.data.ExecStats.ContentionTime.Record(count, stats.ContentionTime.Seconds())
s.mu.data.ExecStats.NetworkMessages.Record(count, float64(stats.NetworkMessages))
s.mu.data.ExecStats.MaxDiskUsage.Record(count, float64(stats.MaxDiskUsage))
s.mu.data.ExecStats.CPUSQLNanos.Record(count, float64(stats.CPUTime.Nanoseconds()))
s.mu.data.ExecStats.MVCCIteratorStats.StepCount.Record(count, float64(stats.MvccSteps))
s.mu.data.ExecStats.MVCCIteratorStats.StepCountInternal.Record(count, float64(stats.MvccStepsInternal))
s.mu.data.ExecStats.MVCCIteratorStats.SeekCount.Record(count, float64(stats.MvccSeeks))
s.mu.data.ExecStats.MVCCIteratorStats.SeekCountInternal.Record(count, float64(stats.MvccSeeksInternal))
s.mu.data.ExecStats.MVCCIteratorStats.BlockBytes.Record(count, float64(stats.MvccBlockBytes))
s.mu.data.ExecStats.MVCCIteratorStats.BlockBytesInCache.Record(count, float64(stats.MvccBlockBytesInCache))
s.mu.data.ExecStats.MVCCIteratorStats.KeyBytes.Record(count, float64(stats.MvccKeyBytes))
s.mu.data.ExecStats.MVCCIteratorStats.ValueBytes.Record(count, float64(stats.MvccValueBytes))
s.mu.data.ExecStats.MVCCIteratorStats.PointCount.Record(count, float64(stats.MvccPointCount))
s.mu.data.ExecStats.MVCCIteratorStats.PointsCoveredByRangeTombstones.Record(count, float64(stats.MvccPointsCoveredByRangeTombstones))
s.mu.data.ExecStats.MVCCIteratorStats.RangeKeyCount.Record(count, float64(stats.MvccRangeKeyCount))
s.mu.data.ExecStats.MVCCIteratorStats.RangeKeyContainedPoints.Record(count, float64(stats.MvccRangeKeyContainedPoints))
s.mu.data.ExecStats.MVCCIteratorStats.RangeKeySkippedPoints.Record(count, float64(stats.MvccRangeKeySkippedPoints))
}
func (s *stmtStats) mergeStatsLocked(statistics *appstatspb.CollectedStatementStatistics) {
// This handles all the statistics fields.
s.mu.data.Add(&statistics.Stats)
// Setting all metadata fields.
if s.mu.data.SensitiveInfo.LastErr == "" {
s.mu.data.SensitiveInfo.LastErr = statistics.Stats.SensitiveInfo.LastErr
}
if s.mu.data.SensitiveInfo.MostRecentPlanTimestamp.Before(statistics.Stats.SensitiveInfo.MostRecentPlanTimestamp) {
s.mu.data.SensitiveInfo.MostRecentPlanDescription = statistics.Stats.SensitiveInfo.MostRecentPlanDescription
s.mu.data.SensitiveInfo.MostRecentPlanTimestamp = statistics.Stats.SensitiveInfo.MostRecentPlanTimestamp
}
s.mu.vectorized = statistics.Key.Vec
s.mu.distSQLUsed = statistics.Key.DistSQL
s.mu.fullScan = statistics.Key.FullScan
s.mu.database = statistics.Key.Database
s.mu.querySummary = statistics.Key.QuerySummary
}
// getStatsForStmt retrieves the per-stmt stat object. Regardless of if a valid
// stat object is returned or not, we always return the correct stmtFingerprintID
// for the given stmt.
func (s *Container) getStatsForStmt(
stmtNoConstants string,
implicitTxn bool,
database string,
planHash uint64,
transactionFingerprintID appstatspb.TransactionFingerprintID,
createIfNonexistent bool,
) (
stats *stmtStats,
key stmtKey,
stmtFingerprintID appstatspb.StmtFingerprintID,
created bool,
throttled bool,
) {
// Extend the statement key with various characteristics, so
// that we use separate buckets for the different situations.
key = stmtKey{
sampledPlanKey: sampledPlanKey{
stmtNoConstants: stmtNoConstants,
implicitTxn: implicitTxn,
database: database,
},
planHash: planHash,
transactionFingerprintID: transactionFingerprintID,
}
// We first try and see if we can get by without creating a new entry for this
// key, as this allows us to not construct the statementFingerprintID from scratch (which
// is an expensive operation)
stats, _, _ = s.getStatsForStmtWithKey(key, invalidStmtFingerprintID, false /* createIfNonexistent */)
if stats == nil {
stmtFingerprintID = constructStatementFingerprintIDFromStmtKey(key)
stats, created, throttled = s.getStatsForStmtWithKey(key, stmtFingerprintID, createIfNonexistent)
return stats, key, stmtFingerprintID, created, throttled
}
return stats, key, stats.ID, false /* created */, false /* throttled */
}
// getStatsForStmtWithKey returns an instance of stmtStats.
// If createIfNonexistent flag is set to true, then a new entry is created in
// the Container if it does not yet exist.
func (s *Container) getStatsForStmtWithKey(
key stmtKey, stmtFingerprintID appstatspb.StmtFingerprintID, createIfNonexistent bool,
) (stats *stmtStats, created, throttled bool) {
s.mu.Lock()
defer s.mu.Unlock()
return s.getStatsForStmtWithKeyLocked(key, stmtFingerprintID, createIfNonexistent)
}
func (s *Container) getStatsForStmtWithKeyLocked(
key stmtKey, stmtFingerprintID appstatspb.StmtFingerprintID, createIfNonexistent bool,
) (stats *stmtStats, created, throttled bool) {
// Retrieve the per-statement statistic object, and create it if it
// doesn't exist yet.
stats, ok := s.mu.stmts[key]
if !ok && createIfNonexistent {
// If the uniqueStmtFingerprintCount is nil, then we don't check for
// fingerprint limit.
if s.uniqueServerCount != nil && !s.uniqueServerCount.tryAddStmtFingerprint() {
return stats, false /* created */, true /* throttled */
}
stats = &stmtStats{}
stats.ID = stmtFingerprintID
s.mu.stmts[key] = stats
s.mu.sampledPlanMetadataCache[key.sampledPlanKey] = s.getTimeNow()
return stats, true /* created */, false /* throttled */
}
return stats, false /* created */, false /* throttled */
}
func (s *Container) getStatsForTxnWithKey(
key appstatspb.TransactionFingerprintID,
stmtFingerprintIDs []appstatspb.StmtFingerprintID,
createIfNonexistent bool,
) (stats *txnStats, created, throttled bool) {
s.mu.Lock()
defer s.mu.Unlock()
return s.getStatsForTxnWithKeyLocked(key, stmtFingerprintIDs, createIfNonexistent)
}
func (s *Container) getStatsForTxnWithKeyLocked(
key appstatspb.TransactionFingerprintID,
stmtFingerprintIDs []appstatspb.StmtFingerprintID,
createIfNonexistent bool,
) (stats *txnStats, created, throttled bool) {
// Retrieve the per-transaction statistic object, and create it if it doesn't
// exist yet.
stats, ok := s.mu.txns[key]
if !ok && createIfNonexistent {
// If the uniqueTxnFingerprintCount is nil, then we don't check for
// fingerprint limit.
if s.uniqueServerCount != nil && !s.uniqueServerCount.tryAddTxnFingerprint() {
return nil /* stats */, false /* created */, true /* throttled */
}
stats = &txnStats{}
stats.statementFingerprintIDs = stmtFingerprintIDs
s.mu.txns[key] = stats
return stats, true /* created */, false /* throttled */
}
return stats, false /* created */, false /* throttled */
}
// SaveToLog saves the existing statement stats into the info log.
func (s *Container) SaveToLog(ctx context.Context, appName string) {
s.mu.Lock()
defer s.mu.Unlock()
if len(s.mu.stmts) == 0 {
return
}
var buf bytes.Buffer
for key, stats := range s.mu.stmts {
json, err := func() ([]byte, error) {
stats.mu.Lock()
defer stats.mu.Unlock()
return json.Marshal(stats.mu.data)
}()
if err != nil {
log.Errorf(ctx, "error while marshaling stats for %q // %q: %v", appName, key.String(), err)
continue
}
fmt.Fprintf(&buf, "%q: %s\n", key.String(), json)
}
log.Infof(ctx, "statistics for %q:\n%s", appName, buf.String())
}
// PopAllStats returns all collected statement and transaction stats in memory to the caller and clears SQL stats
// make sure that new arriving stats won't be interfering with existing one.
func (s *Container) PopAllStats(
ctx context.Context,
) ([]*appstatspb.CollectedStatementStatistics, []*appstatspb.CollectedTransactionStatistics) {
statementStats := make([]*appstatspb.CollectedStatementStatistics, 0)
var stmts map[stmtKey]*stmtStats
transactionStats := make([]*appstatspb.CollectedTransactionStatistics, 0)
var txns map[appstatspb.TransactionFingerprintID]*txnStats
func() {
s.mu.Lock()
defer s.mu.Unlock()
stmts = s.mu.stmts
txns = s.mu.txns
// Reset statementStats and transactions after they're assigned to local variables.
s.mu.stmts = make(map[stmtKey]*stmtStats, len(s.mu.stmts)/2)
s.mu.txns = make(map[appstatspb.TransactionFingerprintID]*txnStats, len(s.mu.txns)/2)
s.mu.sampledPlanMetadataCache = make(map[sampledPlanKey]time.Time, len(s.mu.sampledPlanMetadataCache)/2)
s.freeLocked(ctx)
if s.knobs != nil && s.knobs.OnAfterClear != nil {
s.knobs.OnAfterClear()
}
}()
var data appstatspb.StatementStatistics
var distSQLUsed, vectorized, fullScan bool
var database, querySummary string
for key, stmt := range stmts {
func() {
stmt.mu.Lock()
defer stmt.mu.Unlock()
data = stmt.mu.data
distSQLUsed = stmt.mu.distSQLUsed
vectorized = stmt.mu.vectorized
fullScan = stmt.mu.fullScan
database = stmt.mu.database
querySummary = stmt.mu.querySummary
}()
statementStats = append(statementStats, &appstatspb.CollectedStatementStatistics{
Key: appstatspb.StatementStatisticsKey{
Query: key.stmtNoConstants,
QuerySummary: querySummary,
DistSQL: distSQLUsed,
Vec: vectorized,
ImplicitTxn: key.implicitTxn,
FullScan: fullScan,
App: s.appName,
Database: database,
PlanHash: key.planHash,
TransactionFingerprintID: key.transactionFingerprintID,
},
ID: constructStatementFingerprintIDFromStmtKey(key),
Stats: data,
})
}
for key, txnStats := range txns {
transactionStats = append(transactionStats, &appstatspb.CollectedTransactionStatistics{
StatementFingerprintIDs: txnStats.statementFingerprintIDs,
App: s.appName,
Stats: txnStats.mu.data,
TransactionFingerprintID: key,
})
}
return statementStats, transactionStats
}
// Clear clears the data stored in this Container and prepare the Container
// for reuse.
func (s *Container) Clear(ctx context.Context) {
s.mu.Lock()
defer s.mu.Unlock()
s.freeLocked(ctx)
// Clear the map, to release the memory; make the new map somewhat already
// large for the likely future workload.
s.mu.stmts = make(map[stmtKey]*stmtStats, len(s.mu.stmts)/2)
s.mu.txns = make(map[appstatspb.TransactionFingerprintID]*txnStats, len(s.mu.txns)/2)
s.mu.sampledPlanMetadataCache = make(map[sampledPlanKey]time.Time, len(s.mu.sampledPlanMetadataCache)/2)
if s.knobs != nil && s.knobs.OnAfterClear != nil {
s.knobs.OnAfterClear()
}
}
// Free frees the accounted resources from the Container. The Container is
// presumed to be no longer in use and its actual allocated memory will
// eventually be GC'd.
func (s *Container) Free(ctx context.Context) {
s.mu.Lock()
defer s.mu.Unlock()
s.freeLocked(ctx)
}
func (s *Container) freeLocked(ctx context.Context) {
if s.uniqueServerCount != nil {
s.uniqueServerCount.freeByCnt(int64(len(s.mu.stmts)), int64(len(s.mu.txns)))
}
s.mu.acc.Clear(ctx)
}
// MergeApplicationStatementStats implements the sqlstats.ApplicationStats interface.
func (s *Container) MergeApplicationStatementStats(
ctx context.Context,
other sqlstats.ApplicationStats,
transformer func(*appstatspb.CollectedStatementStatistics),
) (discardedStats uint64) {
if err := other.IterateStatementStats(
ctx,
sqlstats.IteratorOptions{},
func(ctx context.Context, statistics *appstatspb.CollectedStatementStatistics) error {
if transformer != nil {
transformer(statistics)
}
key := stmtKey{
sampledPlanKey: sampledPlanKey{
stmtNoConstants: statistics.Key.Query,
implicitTxn: statistics.Key.ImplicitTxn,
database: statistics.Key.Database,
},
planHash: statistics.Key.PlanHash,
transactionFingerprintID: statistics.Key.TransactionFingerprintID,
}
stmtStats, _, throttled :=
s.getStatsForStmtWithKey(key, statistics.ID, true /* createIfNoneExistent */)
if throttled {
discardedStats++
return nil
}
stmtStats.mu.Lock()
defer stmtStats.mu.Unlock()
stmtStats.mergeStatsLocked(statistics)
planLastSampled, _ := s.getLogicalPlanLastSampled(key.sampledPlanKey)
if planLastSampled.Before(stmtStats.mu.data.SensitiveInfo.MostRecentPlanTimestamp) {
s.setLogicalPlanLastSampled(key.sampledPlanKey, stmtStats.mu.data.SensitiveInfo.MostRecentPlanTimestamp)
}
return nil
},
); err != nil {
// Calling Iterate.*Stats() function with a visitor function that does not
// return error should not cause any error.
panic(
errors.NewAssertionErrorWithWrappedErrf(err, "unexpected error returned when iterating through application stats"),
)
}
return discardedStats
}
// MergeApplicationTransactionStats implements the sqlstats.ApplicationStats interface.
func (s *Container) MergeApplicationTransactionStats(
ctx context.Context, other sqlstats.ApplicationStats,
) (discardedStats uint64) {
if err := other.IterateTransactionStats(
ctx,
sqlstats.IteratorOptions{},
func(ctx context.Context, statistics *appstatspb.CollectedTransactionStatistics) error {
txnStats, _, throttled :=
s.getStatsForTxnWithKey(
statistics.TransactionFingerprintID,
statistics.StatementFingerprintIDs,
true, /* createIfNonexistent */
)
if throttled {
discardedStats++
return nil
}
txnStats.mergeStats(&statistics.Stats)
return nil
}); err != nil {
// Calling Iterate.*Stats() function with a visitor function that does not
// return error should not cause any error.
panic(
errors.NewAssertionErrorWithWrappedErrf(err, "unexpected error returned when iterating through application stats"),
)
}
return discardedStats
}
// Add combines one Container into another. Add manages locks on a, so taking
// a lock on a will cause a deadlock.
func (s *Container) Add(ctx context.Context, other *Container) (err error) {
statMap := func() map[stmtKey]*stmtStats {
other.mu.Lock()
defer other.mu.Unlock()
statMap := make(map[stmtKey]*stmtStats)
for k, v := range other.mu.stmts {
statMap[k] = v
}
return statMap
}()
// Copy the statement stats for each statement key.
for k, v := range statMap {
statCopy := func() *stmtStats {
v.mu.Lock()
defer v.mu.Unlock()
statCopy := &stmtStats{}
statCopy.mu.data = v.mu.data
return statCopy
}()
statCopy.ID = v.ID
statMap[k] = statCopy
}
// Merge the statement stats.
for k, v := range statMap {
stats, created, throttled := s.getStatsForStmtWithKey(k, v.ID, true /* createIfNonexistent */)
// If we have reached the limit of fingerprints, we skip this fingerprint.
// No cleanup necessary.
if throttled {
continue
}
func() {
stats.mu.Lock()
defer stats.mu.Unlock()
// If we created a new entry for the fingerprint, we check if we have
// exceeded our memory budget.
if created {
estimatedAllocBytes := stats.sizeUnsafe() + k.size() + 8 /* stmtKey hash */
// We still want to continue this loop to merge stats that are already
// present in our map that do not require allocation.
if latestErr := func() error {
s.mu.Lock()
defer s.mu.Unlock()
growErr := s.mu.acc.Grow(ctx, estimatedAllocBytes)
if growErr != nil {
delete(s.mu.stmts, k)
}
return growErr
}(); latestErr != nil {
// Instead of combining errors, we track the latest error occurred
// in this method. This is because currently the only type of error we
// can generate in this function is out of memory errors. Also since we
// do not abort after encountering such errors, combining many same
// errors is not helpful.
err = latestErr
return
}
}
// Note that we don't need to take a lock on v because
// no other thread knows about v yet.
stats.mu.data.Add(&v.mu.data)
}()
}
// Do what we did above for the statMap for the txn Map now.
txnMap := func() map[appstatspb.TransactionFingerprintID]*txnStats {
other.mu.Lock()
defer other.mu.Unlock()
txnMap := make(map[appstatspb.TransactionFingerprintID]*txnStats)
for k, v := range other.mu.txns {
txnMap[k] = v
}
return txnMap
}()
// Copy the transaction stats for each txn key
for k, v := range txnMap {
txnCopy := func() *txnStats {
v.mu.Lock()
defer v.mu.Unlock()
txnCopy := &txnStats{}
txnCopy.mu.data = v.mu.data
return txnCopy
}()
txnCopy.statementFingerprintIDs = v.statementFingerprintIDs
txnMap[k] = txnCopy
}
// Merge the txn stats
for k, v := range txnMap {
// We don't check if we have created a new entry here because we have
// already accounted for all the memory that we will be allocating in this
// function.
t, created, throttled := s.getStatsForTxnWithKey(k, v.statementFingerprintIDs, true /* createIfNonExistent */)
// If we have reached the unique fingerprint limit, we skip adding the
// current fingerprint. No cleanup is necessary.
if throttled {
continue
}
func() {
t.mu.Lock()
defer t.mu.Unlock()
if created {
estimatedAllocBytes := t.sizeUnsafe() + k.Size() + 8 /* TransactionFingerprintID hash */
// We still want to continue this loop to merge stats that are already
// present in our map that do not require allocation.
if latestErr := func() error {
s.mu.Lock()
defer s.mu.Unlock()
growErr := s.mu.acc.Grow(ctx, estimatedAllocBytes)
if growErr != nil {
delete(s.mu.txns, k)
}
return growErr
}(); latestErr != nil {
// We only track the latest error. See comment above for explanation.
err = latestErr
return
}
}
// Note that we don't need to take a lock on v because
// no other thread knows about v yet.
t.mu.data.Add(&v.mu.data)
}()
}
// Create a copy of the other's transactions statistics.
txnStats := func() appstatspb.TxnStats {
other.txnCounts.mu.Lock()
defer other.txnCounts.mu.Unlock()
return other.txnCounts.mu.TxnStats
}()
// Merge the transaction stats.
func(txnStats appstatspb.TxnStats) {
s.txnCounts.mu.Lock()
defer s.txnCounts.mu.Unlock()
s.txnCounts.mu.TxnStats.Add(txnStats)
}(txnStats)
return err
}
func (s *Container) getTimeNow() time.Time {
if s.knobs != nil && s.knobs.StubTimeNow != nil {
return s.knobs.StubTimeNow()
}
return timeutil.Now()
}
func (s *transactionCounts) recordTransactionCounts(
txnTimeSec float64, commit bool, implicit bool,
) {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.TxnCount++
s.mu.TxnTimeSec.Record(s.mu.TxnCount, txnTimeSec)
if commit {
s.mu.CommittedCount++
}
if implicit {
s.mu.ImplicitCount++
}
}
func (s *Container) getLogicalPlanLastSampled(
key sampledPlanKey,
) (lastSampled time.Time, found bool) {
s.mu.Lock()
defer s.mu.Unlock()
lastSampled, found = s.mu.sampledPlanMetadataCache[key]
return lastSampled, found
}
func (s *Container) setLogicalPlanLastSampled(key sampledPlanKey, time time.Time) {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.sampledPlanMetadataCache[key] = time
}
// shouldSaveLogicalPlanDescription returns whether we should save the sample
// logical plan based on the time it was last sampled. We use
// `logicalPlanCollectionPeriod` to assess how frequently to sample logical plans.
func (s *Container) shouldSaveLogicalPlanDescription(lastSampled time.Time) bool {
if !sqlstats.SampleLogicalPlans.Get(&s.st.SV) {
return false
}
now := s.getTimeNow()
period := sqlstats.LogicalPlanCollectionPeriod.Get(&s.st.SV)
return now.Sub(lastSampled) >= period
}
type transactionCounts struct {
mu struct {
syncutil.Mutex
// TODO(arul): Can we rename this without breaking stuff?
appstatspb.TxnStats
}
}
func constructStatementFingerprintIDFromStmtKey(key stmtKey) appstatspb.StmtFingerprintID {
return appstatspb.ConstructStatementFingerprintID(
key.stmtNoConstants, key.implicitTxn, key.database,
)
}