Skip to content
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

Merged
merged 16 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split implementation of checks for stale read and snapshot read
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
  • Loading branch information
sticnarf committed Jun 24, 2021
commit f3e61ed34f8f6c0c8fe097cfa578ae73ef7fc2f7
7 changes: 6 additions & 1 deletion executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ func (e *SetExecutor) setSysVariable(ctx context.Context, name string, v *expres
newSnapshotTS := getSnapshotTSByName()
newSnapshotIsSet := newSnapshotTS > 0 && newSnapshotTS != oldSnapshotTS
if newSnapshotIsSet {
if err = sessionctx.ValidateReadTS(ctx, e.ctx, newSnapshotTS); err != nil {
if name == variable.TiDBTxnReadTS {
err = sessionctx.ValidateStaleReadTS(ctx, e.ctx, newSnapshotTS)
} else {
err = sessionctx.ValidateSnapshotReadTS(ctx, e.ctx, newSnapshotTS)
}
if err != nil {
fallbackOldSnapshotTS()
return err
}
Expand Down
2 changes: 1 addition & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2453,7 +2453,7 @@ func (b *PlanBuilder) buildSimple(ctx context.Context, node ast.StmtNode) (Plan,
if err != nil {
return nil, err
}
if err := sessionctx.ValidateReadTS(ctx, b.ctx, startTS); err != nil {
if err := sessionctx.ValidateStaleReadTS(ctx, b.ctx, startTS); err != nil {
return nil, err
}
p.StaleTxnStartTS = startTS
Expand Down
2 changes: 1 addition & 1 deletion planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ func (p *preprocessor) handleAsOfAndReadTS(node *ast.AsOfClause) {
if p.err != nil {
return
}
if err := sessionctx.ValidateReadTS(context.Background(), p.ctx, ts); err != nil {
if err := sessionctx.ValidateStaleReadTS(context.Background(), p.ctx, ts); err != nil {
p.err = errors.Trace(err)
return
}
Expand Down
23 changes: 21 additions & 2 deletions sessionctx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ const (
LastExecuteDDL basicCtxType = 3
)

// ValidateReadTS validates that readTS does not exceed the current timestamp
func ValidateReadTS(ctx context.Context, sctx Context, readTS uint64) error {
// 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()
Copy link
Contributor

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?

Copy link
Contributor Author

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.

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
Expand All @@ -170,3 +170,22 @@ func ValidateReadTS(ctx context.Context, sctx Context, readTS uint64) error {
}
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetStaleTimestamp is directly visit the cache in memory, so I think it's ok to use oracle.GlobalTxnScope.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, if you insist on using txnScope here, there should be config.GetTxnScopeFromConfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}