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

Reset Session for Reserved Connection when the connection id is not found #7539

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go/vt/vtgate/scatter_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (stc *ScatterConn) ExecuteMultiShard(
return qr, allErrors.GetErrors()
}

var errRegx = regexp.MustCompile("transaction ([a-z0-9:]+) ended")
var errRegx = regexp.MustCompile("transaction ([a-z0-9:]+) (?:ended|not found)")

func checkAndResetShardSession(info *shardActionInfo, err error, session *SafeSession) bool {
if info.reservedID != 0 && info.transactionID == 0 && wasConnectionClosed(err) {
Expand Down
52 changes: 52 additions & 0 deletions go/vt/vtgate/scatter_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,64 @@ func TestReservedConnFail(t *testing.T) {

session := NewSafeSession(&vtgatepb.Session{InTransaction: false, InReservedConn: true})
destinations := []key.Destination{key.DestinationShard("0")}

executeOnShards(t, res, keyspace, sc, session, destinations)
assert.Equal(t, 1, len(session.ShardSessions))
oldRId := session.Session.ShardSessions[0].ReservedId

sbc0.EphemeralShardErr = mysql.NewSQLError(mysql.CRServerGone, mysql.SSUnknownSQLState, "lost connection")
_ = executeOnShardsReturnsErr(t, res, keyspace, sc, session, destinations)
assert.Equal(t, 3, len(sbc0.Queries), "1 for the successful run, one for the failed attempt, and one for the retry")
require.Equal(t, 1, len(session.ShardSessions))
assert.NotEqual(t, oldRId, session.Session.ShardSessions[0].ReservedId, "should have recreated a reserved connection since the last connection was lost")
oldRId = session.Session.ShardSessions[0].ReservedId

sbc0.Queries = nil
sbc0.EphemeralShardErr = mysql.NewSQLError(mysql.ERQueryInterrupted, mysql.SSUnknownSQLState, "transaction 123 not found")
_ = executeOnShardsReturnsErr(t, res, keyspace, sc, session, destinations)
assert.Equal(t, 2, len(sbc0.Queries), "one for the failed attempt, and one for the retry")
require.Equal(t, 1, len(session.ShardSessions))
assert.NotEqual(t, oldRId, session.Session.ShardSessions[0].ReservedId, "should have recreated a reserved connection since the last connection was lost")
oldRId = session.Session.ShardSessions[0].ReservedId

sbc0.Queries = nil
sbc0.EphemeralShardErr = mysql.NewSQLError(mysql.ERQueryInterrupted, mysql.SSUnknownSQLState, "transaction 123 ended at 2020-01-20")
_ = executeOnShardsReturnsErr(t, res, keyspace, sc, session, destinations)
assert.Equal(t, 2, len(sbc0.Queries), "one for the failed attempt, and one for the retry")
require.Equal(t, 1, len(session.ShardSessions))
assert.NotEqual(t, oldRId, session.Session.ShardSessions[0].ReservedId, "should have recreated a reserved connection since the last connection was lost")
}

func TestIsConnClosed(t *testing.T) {
var testCases = []struct {
name string
err error
conClosed bool
}{{
"server gone",
mysql.NewSQLError(mysql.CRServerGone, mysql.SSServerShutdown, ""),
true,
}, {
"connection lost",
mysql.NewSQLError(mysql.CRServerLost, mysql.SSServerShutdown, ""),
true,
}, {
"tx ended",
mysql.NewSQLError(mysql.ERQueryInterrupted, mysql.SSUnknownSQLState, "transaction 111 ended at ..."),
true,
}, {
"tx not found",
mysql.NewSQLError(mysql.ERQueryInterrupted, mysql.SSUnknownSQLState, "transaction 111 not found ..."),
true,
}, {
"tx not found missing tx id",
mysql.NewSQLError(mysql.ERQueryInterrupted, mysql.SSUnknownSQLState, "transaction not found"),
false,
}}

for _, tCase := range testCases {
t.Run(tCase.name, func(t *testing.T) {
assert.Equal(t, tCase.conClosed, wasConnectionClosed(tCase.err))
})
}
}