@@ -403,6 +403,12 @@ type StatementContext struct {
403
403
useChunkAlloc bool
404
404
// Check if TiFlash read engine is removed due to strict sql mode.
405
405
TiFlashEngineRemovedDueToStrictSQLMode bool
406
+ // StaleTSOProvider is used to provide stale timestamp oracle for read-only transactions.
407
+ StaleTSOProvider struct {
408
+ sync.Mutex
409
+ value * uint64
410
+ eval func () (uint64 , error )
411
+ }
406
412
}
407
413
408
414
// StmtHints are SessionVars related sql hints.
@@ -1196,6 +1202,32 @@ func (sc *StatementContext) DetachMemDiskTracker() {
1196
1202
}
1197
1203
}
1198
1204
1205
+ // SetStaleTSOProvider sets the stale TSO provider.
1206
+ func (sc * StatementContext ) SetStaleTSOProvider (eval func () (uint64 , error )) {
1207
+ sc .StaleTSOProvider .Lock ()
1208
+ defer sc .StaleTSOProvider .Unlock ()
1209
+ sc .StaleTSOProvider .value = nil
1210
+ sc .StaleTSOProvider .eval = eval
1211
+ }
1212
+
1213
+ // GetStaleTSO returns the TSO for stale-read usage which calculate from PD's last response.
1214
+ func (sc * StatementContext ) GetStaleTSO () (uint64 , error ) {
1215
+ sc .StaleTSOProvider .Lock ()
1216
+ defer sc .StaleTSOProvider .Unlock ()
1217
+ if sc .StaleTSOProvider .value != nil {
1218
+ return * sc .StaleTSOProvider .value , nil
1219
+ }
1220
+ if sc .StaleTSOProvider .eval == nil {
1221
+ return 0 , nil
1222
+ }
1223
+ tso , err := sc .StaleTSOProvider .eval ()
1224
+ if err != nil {
1225
+ return 0 , err
1226
+ }
1227
+ sc .StaleTSOProvider .value = & tso
1228
+ return tso , nil
1229
+ }
1230
+
1199
1231
// CopTasksDetails collects some useful information of cop-tasks during execution.
1200
1232
type CopTasksDetails struct {
1201
1233
NumCopTasks int
0 commit comments