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

feat: read only transaction #303

Merged
merged 9 commits into from
Oct 9, 2023
Merged

Conversation

newborn22
Copy link
Collaborator

Description

Add two fields to Session: TransactionAccessMode and ReadOnlyTransactionPolicy.

If both ReadWriteSplittingPolicy and ReadOnlyTransactionPolicy are enabled, the function suggestTabletType will return TabletType_REPLICA for all SQLs during read-only transaction including the SQL "start transaction read only".

The function handleTransactions will mark or unmark read-only transactions on TransactionAccessMode field of the Session.

Related Issue(s)

#146

Checklist

  • Unit tests for the function suggestTabletType were added in read_write_splitter_test.go

@newborn22
Copy link
Collaborator Author

When both ReadWriteSplittingPolicy and ReadOnlyTransactionPolicy are enabled, the function suggestTabletType will return TabletType_REPLICA for all SQLs during read-only transaction. But I notice that there is another value of TabletType which is topodatapb.TabletType_RDONLY, and actually I don't know the exact difference between them.

@@ -607,6 +607,12 @@ func (e *Executor) handleBegin(ctx context.Context, safeSession *SafeSession, lo

begin := stmt.(*sqlparser.Begin)
err := e.txConn.Begin(ctx, safeSession, begin.TxAccessModes)

// verify read only tx
if len(begin.TxAccessModes) == 1 && begin.TxAccessModes[0] == sqlparser.ReadOnly {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible that begin.TxAccessModes > 1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah~ the transaction can start with both read only and with consistent snapshot!

@@ -127,3 +127,24 @@ func ToLoadBalancePolicy(s string) querypb.ExecuteOptions_LoadBalancePolicy {
return querypb.ExecuteOptions_RANDOM
}
}

type ReadOnlyTransactionPolicy string

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should use a better name.
Maybe EnableReadWriteSplitForReadOnlyTxn=true/false?

@@ -49,24 +62,33 @@ func randomPickTabletType(ratio int32) topodatapb.TabletType {
}

// IsReadOnly : whether the query should be routed to a read-only vttablet
func isReadOnly(query string) (bool, error) {
// return : -1 means error occurs; 0 means is not read-only; 1 means read-only tx; 2 means read-only but not in a read-only tx;
func isReadOnly(query string) (int, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is not a good idea that using int to replace bool as the return value type, it makes this function hard to understand.
In golang, we use error to represtent something wrong happens, we don't use int(-1).

// if the sql is in transaction and its transaction access mode is read-only, then suggest type should be read-only tablet
if isReadOnlyTx && schema.NewReadOnlyTransactionPolicy(readOnlyTransactionPolicy).IsEnable() {
return topodatapb.TabletType_REPLICA, nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these can be moved into isReadOnly

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Finally we found that it will be more readable if we leave this code in suggestTabletType function and let isSQLSupportReadWriteSplit (which is the new name of isReadOnly) to decide whether the SQL can be send to read-only nodes

…er read only transaction and should be routed to PRIMARY node

Signed-off-by: newborn22 <[email protected]>
…napshot both are enabled. And adjusted the order of the code to make it more readable

Signed-off-by: newborn22 <[email protected]>
…dWriteSplitForReadOnlyTxn to false

Signed-off-by: newborn22 <[email protected]>
@newborn22 newborn22 force-pushed the feature/read_only_transaction branch from 2228fc3 to 460faf0 Compare September 28, 2023 08:04
…out read only transaction begin sql

Signed-off-by: newborn22 <[email protected]>
@newborn22 newborn22 merged commit 70d885d into main Oct 9, 2023
5 checks passed
@newborn22 newborn22 deleted the feature/read_only_transaction branch October 9, 2023 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants