-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4c1c14c
manage connections and force rollback
saifalharthi 4130289
few fixes, need tests
saifalharthi 7208a73
Add mod files
saifalharthi 6c979e9
Add go sum file
saifalharthi c7cb00c
Merge with upstream master to fix conflicts
saifalharthi df21380
Added test to ensure connections are rolledback
saifalharthi 263f2e5
Merge remote-tracking branch 'upstream/master' into sa-fix-5553
saifalharthi 5f69296
Add onClose Hook to ensure rollback when shutdown
saifalharthi 9ba30aa
Fix race test
saifalharthi 74914ea
vtgate: alternate approach for shutdown
sougou 913147f
vtgate: tweak rollback test for faster run
sougou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
go/test/endtoend/vtgate/transaction/rollback/txn_rollback_shutdown_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Need to hold mutex here, because you're accessing
connections
. However, you'll run into a deadlock becauseConnectionClosed
also obtains the mutex.I recommend implementing a
CloseAllConnections
instead ofConnectionClosed
, that obtains the mutex and closes all conections in a loop.And then, you can change
servenv.OnClose(rollbackAtShutdown)
->servenv.OnClose(vtgateHandler.CloseAllConnections)