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

sql: put connExecutor's AutoRetry fields into txnState's mutex #82561

Merged

Conversation

RichardJCai
Copy link
Contributor

@RichardJCai RichardJCai commented Jun 7, 2022

Auto retry variables could be used outside the connExecutor's goroutine in calls to
serialize. If this is the case, the field should be in txnState's mutex struct.

Release note: None

Fixes #82506
Fixes #78178

@RichardJCai RichardJCai requested review from rafiss and a team June 7, 2022 23:05
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@RichardJCai RichardJCai requested a review from ajwerner June 7, 2022 23:05
pkg/sql/conn_executor.go Outdated Show resolved Hide resolved
@RichardJCai RichardJCai force-pushed the conn_executor_auto_retry_mutex branch 3 times, most recently from 7e330cf to 03d96f1 Compare June 8, 2022 22:38
@RichardJCai RichardJCai changed the title sql: put connExecutor's ExtraTxnState auto retry variables behind mutex sql: put connExecutor's AutoRetry fields into txnState's mutex Jun 8, 2022
@RichardJCai RichardJCai requested a review from ZhouXing19 June 8, 2022 22:42
@andreimatei
Copy link
Contributor

don't forget to update the PR message

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

i like the direction, but have questions

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @RichardJCai, and @ZhouXing19)


pkg/sql/conn_executor.go line 2726 at r2 (raw file):

	// data which may be after the schema change when we retry.
	var minTSErr *roachpb.MinTimestampBoundUnsatisfiableError
	if err := ex.state.mu.autoRetryReason; err != nil && errors.As(err, &minTSErr) {

are these functions allowed to access ex.state.mu whenever? i don't believe the lock is held here right?


pkg/sql/conn_executor.go line 3033 at r2 (raw file):

	var autoRetryReasonStr string

	if ex.state.mu.autoRetryReason != nil {

ditto


pkg/sql/conn_executor.go line 3044 at r2 (raw file):

			NumStatementsExecuted: int32(ex.state.mu.stmtCount),
			NumRetries:            int32(txn.Epoch()),
			NumAutoRetries:        ex.state.mu.autoRetryCounter,

ditto


pkg/sql/conn_executor_exec.go line 1062 at r2 (raw file):

			ctx,
			ex.executorType,
			int(ex.state.mu.autoRetryCounter),

ditto


pkg/sql/conn_executor_exec.go line 1140 at r2 (raw file):

	}

	ex.sessionTracing.TraceRetryInformation(ctx, int(ex.state.mu.autoRetryCounter), ex.state.mu.autoRetryReason)

ditto


pkg/sql/conn_executor_exec.go line 1177 at r2 (raw file):

	ex.recordStatementSummary(
		ctx, planner,
		int(ex.state.mu.autoRetryCounter), res.RowsAffected(), res.Err(), stats,

ditto


pkg/sql/conn_executor_exec.go line 2251 at r2 (raw file):

		Committed:               ev.eventType == txnCommit,
		ImplicitTxn:             implicit,
		RetryCount:              int64(ex.state.mu.autoRetryCounter),

ditto


pkg/sql/conn_fsm.go line 538 at r2 (raw file):

func prepareTxnForRetryWithRewind(args fsm.Args) error {
	pl := args.Payload.(eventRetriableErrPayload)

is this safe or should we use pl, ok := ...

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 5 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss, @RichardJCai, and @ZhouXing19)


pkg/sql/conn_executor.go line 2726 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

are these functions allowed to access ex.state.mu whenever? i don't believe the lock is held here right?

See this for this and the dittos

// Note that reads of mu.txn from the session's main goroutine do not require
// acquiring a read lock - since only that goroutine will ever write to
// mu.txn. Writes to mu.txn do require a write lock to guarantee safety with
// reads by other goroutines.


pkg/sql/conn_fsm.go line 469 at r2 (raw file):

	ts.mu.Lock()
	ts.mu.autoRetryCounter = 0

why not inside resetForNewSQLTxn?

Code quote:

	ts.mu.autoRetryCounter = 0
	ts.mu.autoRetryReason = nil
	ts.mu.Unlock()

pkg/sql/conn_fsm.go line 538 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

is this safe or should we use pl, ok := ...

this is safe, see below


pkg/sql/conn_fsm.go line 548 at r2 (raw file):

	ts.setAdvanceInfo(
		rewind,
		args.Payload.(eventRetriableErrPayload).rewCap,

We already did the type assertion, you can use pl.rewCap

Auto retry variables could be used outside the connExecutor's goroutine in calls to
serialize. If this is the case, the field should be in txnState's mutex struct.

Release note: None
@RichardJCai RichardJCai force-pushed the conn_executor_auto_retry_mutex branch from 03d96f1 to b6d3be5 Compare June 9, 2022 15:53
Copy link
Contributor Author

@RichardJCai RichardJCai left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @rafiss, @RichardJCai, and @ZhouXing19)


pkg/sql/conn_executor.go line 2726 at r2 (raw file):

Previously, ajwerner wrote…

See this for this and the dittos

// Note that reads of mu.txn from the session's main goroutine do not require
// acquiring a read lock - since only that goroutine will ever write to
// mu.txn. Writes to mu.txn do require a write lock to guarantee safety with
// reads by other goroutines.

Resolved.


pkg/sql/conn_fsm.go line 469 at r2 (raw file):

Previously, ajwerner wrote…

why not inside resetForNewSQLTxn?

Good call, done.


pkg/sql/conn_fsm.go line 538 at r2 (raw file):

Previously, ajwerner wrote…

this is safe, see below

Done.

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rafiss, @RichardJCai, and @ZhouXing19)

@RichardJCai
Copy link
Contributor Author

Thanks for the reviews!

bors r=ajwerner

@craig
Copy link
Contributor

craig bot commented Jun 9, 2022

Build succeeded:

@craig craig bot merged commit e09ce91 into cockroachdb:master Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants