-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
executor: reject setting read ts to a future time #25732
Changes from 11 commits
dd7c613
e8daeba
60b8961
bb0315f
1b66665
36581c8
bd102bc
f3e61ed
63b0be8
f238dc9
9359d6f
c6bf0c8
7272660
1afdc3f
0a93d8c
b61ad75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,17 @@ import ( | |
"context" | ||
"fmt" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/metrics" | ||
"github.com/pingcap/tidb/owner" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
"github.com/pingcap/tidb/util" | ||
"github.com/pingcap/tidb/util/kvcache" | ||
"github.com/pingcap/tidb/util/sli" | ||
"github.com/pingcap/tipb/go-binlog" | ||
"github.com/tikv/client-go/v2/oracle" | ||
) | ||
|
||
// InfoschemaMetaVersion is a workaround. Due to circular dependency, | ||
|
@@ -149,3 +152,40 @@ const ( | |
// LastExecuteDDL is the key for whether the session execute a ddl command last time. | ||
LastExecuteDDL basicCtxType = 3 | ||
) | ||
|
||
// ValidateSnapshotReadTS strictly validates that readTS does not exceed the PD timestamp | ||
func ValidateSnapshotReadTS(ctx context.Context, sctx Context, readTS uint64) error { | ||
txnScope := sctx.GetSessionVars().CheckAndGetTxnScope() | ||
latestTS, err := sctx.GetStore().GetOracle().GetLowResolutionTimestamp(ctx, &oracle.Option{TxnScope: txnScope}) | ||
// If we fail to get latestTS or the readTS exceeds it, get a timestamp from PD to double check | ||
if err != nil || readTS > latestTS { | ||
metrics.ValidateReadTSFromPDCount.Inc() | ||
currentVer, err := sctx.GetStore().CurrentVersion(txnScope) | ||
nolouch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return errors.Errorf("fail to validate read timestamp: %v", err) | ||
} | ||
if readTS > currentVer.Ver { | ||
return errors.Errorf("cannot set read timestamp to a future time") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateStaleReadTS validates that readTS does not exceed the current time not strictly. | ||
func ValidateStaleReadTS(ctx context.Context, sctx Context, readTS uint64) error { | ||
txnScope := sctx.GetSessionVars().CheckAndGetTxnScope() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the check for stale read need to use global scope? This can be not strict so I guess a close PD would suffice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, if you insist on using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I change to use global scope. I think there's little difference. |
||
currentTS, err := sctx.GetStore().GetOracle().GetStaleTimestamp(ctx, txnScope, 0) | ||
// If we fail to calculate currentTS from local time, fallback to get a timestamp from PD | ||
if err != nil { | ||
metrics.ValidateReadTSFromPDCount.Inc() | ||
currentVer, err := sctx.GetStore().CurrentVersion(txnScope) | ||
if err != nil { | ||
return errors.Errorf("fail to validate read timestamp: %v", err) | ||
} | ||
currentTS = currentVer.Ver | ||
} | ||
if readTS > currentTS { | ||
return errors.Errorf("cannot set read timestamp to a future time") | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should use
oracle.GlobalTxnScope
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. This looks safer.