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

Enforce rollback for each connection when shutdown occurs #5659

Merged
merged 11 commits into from
Jan 15, 2020
2 changes: 1 addition & 1 deletion go/test/endtoend/cluster/vtgate_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (vtgate *VtgateProcess) TearDown() error {
vtgate.proc = nil
return err

case <-time.After(10 * time.Second):
case <-time.After(15 * time.Second):
vtgate.proc.Process.Kill()
vtgate.proc = nil
return <-vtgate.exit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright 2019 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rollback

import (
"context"
"flag"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
vtParams mysql.ConnParams
keyspaceName = "ks"
cell = "zone1"
hostname = "localhost"
sqlSchema = `
create table buffer(
id BIGINT NOT NULL,
msg VARCHAR(64) NOT NULL,
PRIMARY KEY (id)
) Engine=InnoDB;`
)

func TestMain(m *testing.M) {
flag.Parse()

exitcode, err := func() (int, error) {
clusterInstance = cluster.NewCluster(cell, hostname)
defer clusterInstance.Teardown()

// Reserve vtGate port in order to pass it to vtTablet
clusterInstance.VtgateGrpcPort = clusterInstance.GetAndReservePort()

// Start topo server
if err := clusterInstance.StartTopo(); err != nil {
return 1, err
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: keyspaceName,
SchemaSQL: sqlSchema,
}
if err := clusterInstance.StartUnshardedKeyspace(*keyspace, 1, false); err != nil {
return 1, err
}

// Starting Vtgate in SINGLE transaction mode
clusterInstance.VtGateExtraArgs = []string{"-transaction_mode", "SINGLE"}
if err := clusterInstance.StartVtgate(); err != nil {
return 1, err
}
vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}

return m.Run(), nil
}()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
} else {
os.Exit(exitcode)
}
}

func exec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result {
t.Helper()
qr, err := conn.ExecuteFetch(query, 1000, true)
if err != nil {
t.Fatal(err)
}
return qr
}

func TestTransactionRollBackWhenShutDown(t *testing.T) {
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

exec(t, conn, "insert into buffer(id, msg) values(3,'mark')")
exec(t, conn, "insert into buffer(id, msg) values(4,'doug')")

// start an incomplete transaction
exec(t, conn, "begin")
exec(t, conn, "insert into buffer(id, msg) values(33,'mark')")

// Enforce a restart to enforce rollback
if err = clusterInstance.ReStartVtgate(); err != nil {
t.Errorf("Fail to re-start vtgate: %v", err)
}

want := ""

// Make a new mysql connection to vtGate
vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}
conn2, err := mysql.Connect(ctx, &vtParams)
if err != nil {
t.Fatal(err)
}
defer conn2.Close()

vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}

// Verify that rollback worked
qr := exec(t, conn2, "select id from buffer where msg='mark'")
got := fmt.Sprintf("%v", qr.Rows)
want = `[[INT64(3)]]`
assert.Equal(t, want, got)
}
34 changes: 29 additions & 5 deletions go/vt/vtgate/plugin_mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net"
"os"
"regexp"
"sync"
"sync/atomic"
"syscall"
"time"
Expand Down Expand Up @@ -72,16 +73,23 @@ var (
// vtgateHandler implements the Listener interface.
// It stores the Session in the ClientData of a Connection.
type vtgateHandler struct {
vtg *VTGate
mu sync.Mutex

vtg *VTGate
connections map[*mysql.Conn]bool
}

func newVtgateHandler(vtg *VTGate) *vtgateHandler {
return &vtgateHandler{
vtg: vtg,
vtg: vtg,
connections: make(map[*mysql.Conn]bool),
}
}

func (vh *vtgateHandler) NewConnection(c *mysql.Conn) {
vh.mu.Lock()
defer vh.mu.Unlock()
vh.connections[c] = true
}

func (vh *vtgateHandler) ComResetConnection(c *mysql.Conn) {
Expand All @@ -98,6 +106,12 @@ func (vh *vtgateHandler) ComResetConnection(c *mysql.Conn) {

func (vh *vtgateHandler) ConnectionClosed(c *mysql.Conn) {
// Rollback if there is an ongoing transaction. Ignore error.
defer func() {
vh.mu.Lock()
defer vh.mu.Unlock()
delete(vh.connections, c)
}()

var ctx context.Context
var cancel context.CancelFunc
if *mysqlQueryTimeout != 0 {
Expand Down Expand Up @@ -310,6 +324,8 @@ func (vh *vtgateHandler) session(c *mysql.Conn) *vtgatepb.Session {
var mysqlListener *mysql.Listener
var mysqlUnixListener *mysql.Listener

var vtgateHandle *vtgateHandler

// initiMySQLProtocol starts the mysql protocol.
// It should be called only once in a process.
func initMySQLProtocol() {
Expand Down Expand Up @@ -338,9 +354,9 @@ func initMySQLProtocol() {

// Create a Listener.
var err error
vh := newVtgateHandler(rpcVTGate)
vtgateHandle = newVtgateHandler(rpcVTGate)
if *mysqlServerPort >= 0 {
mysqlListener, err = mysql.NewListener(*mysqlTCPVersion, net.JoinHostPort(*mysqlServerBindAddress, fmt.Sprintf("%v", *mysqlServerPort)), authServer, vh, *mysqlConnReadTimeout, *mysqlConnWriteTimeout, *mysqlProxyProtocol)
mysqlListener, err = mysql.NewListener(*mysqlTCPVersion, net.JoinHostPort(*mysqlServerBindAddress, fmt.Sprintf("%v", *mysqlServerPort)), authServer, vtgateHandle, *mysqlConnReadTimeout, *mysqlConnWriteTimeout, *mysqlProxyProtocol)
if err != nil {
log.Exitf("mysql.NewListener failed: %v", err)
}
Expand Down Expand Up @@ -369,7 +385,7 @@ func initMySQLProtocol() {
// Let's create this unix socket with permissions to all users. In this way,
// clients can connect to vtgate mysql server without being vtgate user
oldMask := syscall.Umask(000)
mysqlUnixListener, err = newMysqlUnixSocket(*mysqlServerSocketPath, authServer, vh)
mysqlUnixListener, err = newMysqlUnixSocket(*mysqlServerSocketPath, authServer, vtgateHandle)
_ = syscall.Umask(oldMask)
if err != nil {
log.Exitf("mysql.NewListener failed: %v", err)
Expand Down Expand Up @@ -436,9 +452,17 @@ func shutdownMysqlProtocolAndDrain() {
}
}

func rollbackAtShutdown() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to hold mutex here, because you're accessing connections. However, you'll run into a deadlock because ConnectionClosed also obtains the mutex.

I recommend implementing a CloseAllConnections instead of ConnectionClosed, that obtains the mutex and closes all conections in a loop.

And then, you can change servenv.OnClose(rollbackAtShutdown) -> servenv.OnClose(vtgateHandler.CloseAllConnections)

for c := range vtgateHandle.connections {
log.Warningf("Rolling back transactions associated with connection ID: %v", c.ConnectionID)
vtgateHandle.ConnectionClosed(c)
}
}

func init() {
servenv.OnRun(initMySQLProtocol)
servenv.OnTermSync(shutdownMysqlProtocolAndDrain)
servenv.OnClose(rollbackAtShutdown)
}

var pluginInitializers []func()
Expand Down