-
Notifications
You must be signed in to change notification settings - Fork 12
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
Conversation
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. |
go/vt/vtgate/executor.go
Outdated
@@ -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 { |
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.
Is it possible that begin.TxAccessModes
> 1?
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.
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 | |||
|
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.
I think you should use a better name.
Maybe EnableReadWriteSplitForReadOnlyTxn=true/false
?
go/vt/vtgate/read_write_splitter.go
Outdated
@@ -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) { |
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.
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)
.
go/vt/vtgate/read_write_splitter.go
Outdated
// 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 | ||
} |
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.
I think these can be moved into isReadOnly
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.
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
Signed-off-by: newborn22 <[email protected]>
Signed-off-by: newborn22 <[email protected]>
…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]>
Signed-off-by: newborn22 <[email protected]>
Signed-off-by: newborn22 <[email protected]>
Signed-off-by: newborn22 <[email protected]>
…dWriteSplitForReadOnlyTxn to false Signed-off-by: newborn22 <[email protected]>
2228fc3
to
460faf0
Compare
…out read only transaction begin sql Signed-off-by: newborn22 <[email protected]>
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