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

vitessdriver: add support for DistributedTx #9451

Merged
merged 7 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
122 changes: 121 additions & 1 deletion go/vt/vitessdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ import (
"context"
"database/sql"
"database/sql/driver"
"encoding/base64"
"encoding/json"
"errors"

"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"

"vitess.io/vitess/go/vt/vtgate/grpcvtgateconn"
"vitess.io/vitess/go/vt/vtgate/vtgateconn"
Expand Down Expand Up @@ -115,6 +120,13 @@ func (d drv) Open(name string) (driver.Conn, error) {

c.setDefaults()

if c.Configuration.SessionToken != "" {
c.Configuration.sessionFromToken, err = sessionTokenToSession(c.Configuration.SessionToken)
if err != nil {
return nil, err
}
}

if c.convert, err = newConverter(&c.Configuration); err != nil {
return nil, err
}
Expand Down Expand Up @@ -167,6 +179,10 @@ type Configuration struct {
//
// Default: "vitess"
DriverName string `json:"-"`

// allows for some magic
SessionToken string
sessionFromToken *vtgatepb.Session
}

// toJSON converts Configuration to the JSON string which is required by the
Expand Down Expand Up @@ -205,7 +221,11 @@ func (c *conn) dial() error {
if err != nil {
return err
}
c.session = c.conn.Session(c.Target, nil)
if c.Configuration.sessionFromToken != nil {
c.session = c.conn.SessionFromPb(c.Configuration.sessionFromToken)
} else {
c.session = c.conn.Session(c.Target, nil)
}
return nil
}

Expand All @@ -231,7 +251,88 @@ func (c *conn) Close() error {
return nil
}

// DistributedTxFromSessionToken allows users to send serialized sessions over the wire and
// reconnect to an existing transaction. Setting the sessionToken and address on the
// supplied configuration is the minimum required
func DistributedTxFromSessionToken(ctx context.Context, c Configuration) (*sql.Tx, error) {
if c.SessionToken == "" {
return nil, errors.New("c.SessionToken is required")
}
if c.Address == "" {
derekperkins marked this conversation as resolved.
Show resolved Hide resolved
return nil, errors.New("c.Address is required")
}

db, err := OpenWithConfiguration(c)
if err != nil {
return nil, err
}

// this should return the only connection associated with the db
return db.BeginTx(ctx, nil)
}

// SessionTokenFromTx serializes the sessionFromToken on the tx, which can be reconstituted
// into a *sql.Tx using DistributedTxFromSessionToken
func SessionTokenFromTx(ctx context.Context, tx *sql.Tx) (string, error) {
var sessionToken string

err := tx.QueryRowContext(ctx, "vt_session_token").Scan(&sessionToken)
if err != nil {
return "", err
}

return sessionToken, nil
}

func newSessionTokenRow(session *vtgatepb.Session, c *converter) (driver.Rows, error) {
sessionToken, err := sessionToSessionToken(session)
if err != nil {
return nil, err
}

qr := sqltypes.Result{
Fields: []*querypb.Field{{
Name: "vt_session_token",
Type: sqltypes.VarBinary,
}},
Rows: [][]sqltypes.Value{{
sqltypes.NewVarBinary(sessionToken),
}},
}

return newRows(&qr, c), nil
}

func sessionToSessionToken(session *vtgatepb.Session) (string, error) {
b, err := proto.Marshal(session)
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(b), nil
}

func sessionTokenToSession(sessionToken string) (*vtgatepb.Session, error) {
b, err := base64.StdEncoding.DecodeString(sessionToken)
if err != nil {
return nil, err
}

session := &vtgatepb.Session{}
err = proto.Unmarshal(b, session)
if err != nil {
return nil, err
}

return session, nil
}

func (c *conn) Begin() (driver.Tx, error) {
// if we're loading from an existing session, we need to avoid starting a new transaction
if c.Configuration.SessionToken != "" {
return c, nil
}

if _, err := c.Exec("begin", nil); err != nil {
return nil, err
}
Expand All @@ -248,11 +349,25 @@ func (c *conn) BeginTx(_ context.Context, opts driver.TxOptions) (driver.Tx, err
}

func (c *conn) Commit() error {
// if we're loading from an existing session, disallow committing/rolling back the transaction
// this isn't a technical limitation, but is enforced to prevent misuse, so that only
// the original creator of the transaction can commit/rollback
if c.Configuration.SessionToken != "" {
return errors.New("calling Commit from a distributed tx is not allowed")
}

_, err := c.Exec("commit", nil)
return err
}

func (c *conn) Rollback() error {
// if we're loading from an existing session, disallow committing/rolling back the transaction
// this isn't a technical limitation, but is enforced to prevent misuse, so that only
// the original creator of the transaction can commit/rollback
if c.Configuration.SessionToken != "" {
return errors.New("calling Rollback from a distributed tx is not allowed")
}

_, err := c.Exec("rollback", nil)
return err
}
Expand Down Expand Up @@ -314,6 +429,11 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
}

func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
// special case for serializing the current sessionFromToken state
if query == "vt_session_token" {
return newSessionTokenRow(c.session.SessionPb(), c.convert)
}

bv, err := c.convert.bindVarsFromNamedValues(args)
if err != nil {
return nil, err
Expand Down
85 changes: 85 additions & 0 deletions go/vt/vitessdriver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,88 @@ func TestTxExecStreamingNotAllowed(t *testing.T) {
t.Errorf("err: %v, does not contain %s", err, want)
}
}

func TestSessionToken(t *testing.T) {
c := Configuration{
Protocol: "grpc",
Address: testAddress,
Target: "@primary",
}

ctx := context.Background()

db, err := OpenWithConfiguration(c)
if err != nil {
t.Fatal(err)
}
defer db.Close()

tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}

s, err := tx.Prepare("txRequest")
if err != nil {
t.Fatal(err)
}

_, err = s.Exec(int64(0))
if err != nil {
t.Fatal(err)
}

sessionToken, err := SessionTokenFromTx(ctx, tx)
if err != nil {
t.Fatal(err)
}

distributedTxConfig := Configuration{
Address: testAddress,
Target: "@primary",
SessionToken: sessionToken,
}

sameTx, err := DistributedTxFromSessionToken(ctx, distributedTxConfig)
if err != nil {
t.Fatal(err)
}

newS, err := sameTx.Prepare("distributedTxRequest")
if err != nil {
t.Fatal(err)
}

_, err = newS.Exec(int64(1))
if err != nil {
t.Fatal(err)
}

// enforce that Rollback can't be called on the distributed tx
noRollbackTx, err := DistributedTxFromSessionToken(ctx, distributedTxConfig)
if err != nil {
t.Fatal(err)
}

err = noRollbackTx.Rollback()
if err != nil && err.Error() != "calling Rollback from a distributed tx is not allowed" {
t.Fatal(err)
}

// enforce that Commit can't be called on the distributed tx
noCommitTx, err := DistributedTxFromSessionToken(ctx, distributedTxConfig)
if err != nil {
t.Fatal(err)
}

err = noCommitTx.Commit()
if err != nil && err.Error() != "calling Commit from a distributed tx is not allowed" {
t.Fatal(err)
}

// finally commit the original tx
err = tx.Commit()
if err != nil {
t.Fatal(err)
}
}
28 changes: 25 additions & 3 deletions go/vt/vitessdriver/fakeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ limitations under the License.
package vitessdriver

import (
"context"
"errors"
"fmt"
"reflect"

"context"

"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/vtgate/vtgateservice"

Expand Down Expand Up @@ -228,6 +226,30 @@ var execMap = map[string]struct {
result: &sqltypes.Result{},
session: session2,
},
"distributedTxRequest": {
execQuery: &queryExecute{
SQL: "distributedTxRequest",
BindVariables: map[string]*querypb.BindVariable{
"v1": sqltypes.Int64BindVariable(1),
},
Session: &vtgatepb.Session{
InTransaction: true,
ShardSessions: []*vtgatepb.Session_ShardSession{
{
Target: &querypb.Target{
Keyspace: "ks",
Shard: "1",
TabletType: topodatapb.TabletType_PRIMARY,
},
TransactionId: 1,
},
},
TargetString: "@rdonly",
},
},
result: &sqltypes.Result{},
session: session2,
},
"begin": {
execQuery: &queryExecute{
SQL: "begin",
Expand Down
16 changes: 14 additions & 2 deletions go/vt/vtgate/vtgateconn/vtgateconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ limitations under the License.
package vtgateconn

import (
"context"
"flag"
"fmt"

"context"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/log"

Expand Down Expand Up @@ -55,6 +54,19 @@ func (conn *VTGateConn) Session(targetString string, options *querypb.ExecuteOpt
}
}

// SessionPb returns the underlying proto session.
func (sn *VTGateSession) SessionPb() *vtgatepb.Session {
return sn.session
}

// SessionFromPb returns a VTGateSession based on the provided proto session.
func (conn *VTGateConn) SessionFromPb(sn *vtgatepb.Session) *VTGateSession {
return &VTGateSession{
session: sn,
impl: conn.impl,
}
}

derekperkins marked this conversation as resolved.
Show resolved Hide resolved
// ResolveTransaction resolves the 2pc transaction.
func (conn *VTGateConn) ResolveTransaction(ctx context.Context, dtid string) error {
return conn.impl.ResolveTransaction(ctx, dtid)
Expand Down