@@ -812,7 +812,7 @@ func TestTxnBundleCollection(t *testing.T) {
812
812
sqlConn := sqlutils .MakeSQLRunner (tc .ServerConn (0 ))
813
813
814
814
// Execute to transaction so that it is in transaction_statistics
815
- executeTransactions (t , sqlConn , txnStatements )
815
+ executeTransaction (t , sqlConn , txnStatements )
816
816
817
817
// Get the fingerprint id and statement fingerprint ids for the transaction
818
818
// to make the txn diagnostics request
@@ -928,9 +928,7 @@ LIMIT 1`)
928
928
require .NoError (t , err )
929
929
930
930
// Ensure that the request shows up in the system.transaction_diagnostics_requests table
931
- var count int
932
- sqlConn .QueryRow (t , "SELECT count(*) FROM system.transaction_diagnostics_requests WHERE completed=false and id=$1" , reqId ).Scan (& count )
933
- require .Equal (t , 1 , count )
931
+ require .False (t , getRequestCompletedStatus (t , sqlConn , reqId ))
934
932
935
933
// Execute the transaction until the request is marked as complete.
936
934
// We use a connection to a different server than the diagnostics request
@@ -1016,18 +1014,15 @@ LIMIT 1`)
1016
1014
require .NoError (t , err )
1017
1015
1018
1016
// Ensure that the request shows up in the system.transaction_diagnostics_requests table
1019
- var count int
1020
- sqlConn .QueryRow (t , "SELECT count(*) FROM system.transaction_diagnostics_requests WHERE completed=false and id=$1" , reqId ).Scan (& count )
1021
- require .Equal (t , 1 , count )
1017
+ require .False (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1022
1018
runTxnUntilDiagnosticsCollected (t , tc , sqlConn , txnStatements , reqId )
1023
- // Ensure there are no more active transaction diagnostics requests
1024
- sqlConn .QueryRow (t , "SELECT count(*) FROM system.transaction_diagnostics_requests WHERE completed=false and id=$1" , reqId ).Scan (& count )
1025
- require .Equal (t , 0 , count )
1019
+ // Ensure the request is complete
1020
+ require .True (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1026
1021
// make sure the statement diagnostics request is still incomplete
1027
1022
sqlConn .QueryRow (t , "SELECT count(*) FROM system.statement_diagnostics_requests WHERE completed=false and id=$1" , stmtReqId ).Scan (& stmtcount )
1028
1023
require .Equal (t , 1 , stmtcount )
1029
1024
1030
- executeTransactions (t , sqlConn , txnStatements )
1025
+ executeTransaction (t , sqlConn , txnStatements )
1031
1026
// should be complete now that there is no transaction diagnostics request
1032
1027
sqlConn .QueryRow (t , "SELECT count(*) FROM system.statement_diagnostics_requests WHERE completed=true and id=$1" , stmtReqId ).Scan (& stmtcount )
1033
1028
require .Equal (t , 1 , stmtcount )
@@ -1048,9 +1043,7 @@ LIMIT 1`)
1048
1043
require .NoError (t , err )
1049
1044
1050
1045
// Ensure that the request shows up in the system.transaction_diagnostics_requests table
1051
- var count int
1052
- sqlConn .QueryRow (t , "SELECT count(*) FROM system.transaction_diagnostics_requests WHERE completed=false and id=$1" , reqId ).Scan (& count )
1053
- require .Equal (t , 1 , count )
1046
+ require .False (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1054
1047
1055
1048
conn2 := sqlutils .MakeSQLRunner (tc .ServerConn (1 ))
1056
1049
conn3 := sqlutils .MakeSQLRunner (tc .ServerConn (2 ))
@@ -1078,16 +1071,79 @@ LIMIT 1`)
1078
1071
conn3 .Exec (t , "COMMIT" )
1079
1072
conn2 .Exec (t , "COMMIT" )
1080
1073
1081
- sqlConn .QueryRow (t , "SELECT count(*) FROM system.transaction_diagnostics_requests WHERE completed=true and id=$1" , reqId ).Scan (& count )
1082
- require .Equal (t , 1 , count )
1074
+ require .True (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1083
1075
1076
+ var count int
1084
1077
sqlConn .QueryRow (t , "" +
1085
1078
"SELECT count(*) FROM system.statement_diagnostics sd " +
1086
1079
"JOIN system.transaction_diagnostics_requests tdr on tdr.transaction_diagnostics_id = sd.transaction_diagnostics_id " +
1087
1080
"WHERE tdr.id=$1" , reqId ).Scan (& count )
1088
1081
1089
1082
require .Equal (t , 3 , count )
1090
1083
})
1084
+ t .Run ("partial match transaction" , func (t * testing.T ) {
1085
+ reqId , err := registry .InsertTxnRequestInternal (
1086
+ ctx ,
1087
+ txnFingerprintId ,
1088
+ statementFingerprintIds ,
1089
+ "testuser" ,
1090
+ 0 ,
1091
+ 0 ,
1092
+ 0 ,
1093
+ false ,
1094
+ )
1095
+ require .NoError (t , err )
1096
+
1097
+ // Ensure that the request shows up in the system.transaction_diagnostics_requests table
1098
+ require .False (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1099
+
1100
+ executeTransaction (t , sqlConn , []string {txnStatements [0 ]})
1101
+ require .False (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1102
+
1103
+ sqlConn .Exec (t , txnStatements [0 ])
1104
+ require .False (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1105
+
1106
+ executeTransaction (t , sqlConn , txnStatements )
1107
+ runTxnUntilDiagnosticsCollected (t , tc , sqlConn , txnStatements , reqId )
1108
+ })
1109
+
1110
+ t .Run ("implicit txn" , func (t * testing.T ) {
1111
+ sqlConn .Exec (t , "CREATE DATABASE mydb" )
1112
+ sqlConn .Exec (t , "USE mydb" )
1113
+ sqlConn .Exec (t , "CREATE TABLE my_table(a int, b int)" )
1114
+ sqlConn .Exec (t , "INSERT INTO my_table VALUES (1,1)" )
1115
+ var fingerprintIdBytes []byte
1116
+ var txnFingerprintIdBytes []byte
1117
+ sqlConn .QueryRow (t , `
1118
+ SELECT fingerprint_id, transaction_fingerprint_id
1119
+ FROM crdb_internal.statement_statistics
1120
+ WHERE metadata->>'db' = 'mydb'
1121
+ AND metadata->>'query'
1122
+ LIKE 'INSERT INTO my_table%'
1123
+ LIMIT 1` ).Scan (& fingerprintIdBytes , & txnFingerprintIdBytes )
1124
+ _ , fingerprintId , err := encoding .DecodeUint64Ascending (fingerprintIdBytes )
1125
+ require .NoError (t , err )
1126
+ _ , txnFingerprintId , err := encoding .DecodeUint64Ascending (txnFingerprintIdBytes )
1127
+ require .NoError (t , err )
1128
+
1129
+ // Insert a request for the transaction fingerprint
1130
+ reqId , err := registry .InsertTxnRequestInternal (
1131
+ ctx ,
1132
+ txnFingerprintId ,
1133
+ []uint64 {fingerprintId },
1134
+ "testuser" ,
1135
+ 0 ,
1136
+ 0 ,
1137
+ 0 ,
1138
+ false ,
1139
+ )
1140
+ require .NoError (t , err )
1141
+ require .False (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1142
+
1143
+ sqlConn .Exec (t , "INSERT INTO my_table VALUES (1,1)" )
1144
+ require .True (t , getRequestCompletedStatus (t , sqlConn , reqId ))
1145
+ })
1146
+
1091
1147
}
1092
1148
1093
1149
func TestRequestTxnBundleBuiltin (t * testing.T ) {
@@ -1111,7 +1167,7 @@ func TestRequestTxnBundleBuiltin(t *testing.T) {
1111
1167
// The built-in requires that the transaction fingerprint exists in
1112
1168
// the system.transaction_statistics table, so we execute the transaction
1113
1169
// once.
1114
- executeTransactions (t , runner , txnStatements )
1170
+ executeTransaction (t , runner , txnStatements )
1115
1171
1116
1172
// Get the fingerprint id and statement fingerprint ids for the transaction
1117
1173
// to make the txn diagnostics request
@@ -1334,7 +1390,7 @@ LIMIT 1`)
1334
1390
})
1335
1391
}
1336
1392
1337
- func executeTransactions (t * testing.T , runner * sqlutils.SQLRunner , statements []string ) {
1393
+ func executeTransaction (t * testing.T , runner * sqlutils.SQLRunner , statements []string ) {
1338
1394
t .Helper ()
1339
1395
runner .Exec (t , "BEGIN" )
1340
1396
for _ , stmt := range statements {
@@ -1352,18 +1408,10 @@ func runTxnUntilDiagnosticsCollected(
1352
1408
) {
1353
1409
t .Helper ()
1354
1410
testutils .SucceedsSoon (t , func () error {
1355
- executeTransactions (t , sqlConn , statements )
1356
- r := tc .ServerConn (0 ).QueryRow ("SELECT count(*) FROM system.transaction_diagnostics_requests WHERE completed=true and id=$1" , reqId )
1357
- if r .Err () != nil {
1358
- return r .Err ()
1359
- }
1360
- var count int
1361
- err := r .Scan (& count )
1362
- if err != nil {
1363
- return err
1364
- }
1365
- if count != 1 {
1366
- return errors .Newf ("expected 1 completed bundle, got %d" , count )
1411
+ executeTransaction (t , sqlConn , statements )
1412
+ completed := getRequestCompletedStatus (t , sqlConn , reqId )
1413
+ if ! completed {
1414
+ return errors .New ("expected request to be completed" )
1367
1415
}
1368
1416
return nil
1369
1417
})
@@ -1379,3 +1427,12 @@ func runTxnUntilDiagnosticsCollected(
1379
1427
return nil
1380
1428
})
1381
1429
}
1430
+
1431
+ func getRequestCompletedStatus (
1432
+ t * testing.T , runner * sqlutils.SQLRunner , reqId stmtdiagnostics.RequestID ,
1433
+ ) bool {
1434
+ t .Helper ()
1435
+ var completed bool
1436
+ runner .QueryRow (t , "SELECT completed FROM system.transaction_diagnostics_requests WHERE id=$1" , reqId ).Scan (& completed )
1437
+ return completed
1438
+ }
0 commit comments