-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distributed Transaction: Action on commit prepared or redo prepared f…
…ailure (#16803) Signed-off-by: Harshit Gangal <[email protected]>
- Loading branch information
1 parent
670e141
commit 83b37b8
Showing
12 changed files
with
444 additions
and
87 deletions.
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
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
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,100 @@ | ||
/* | ||
Copyright 2024 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 endtoend | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
"vitess.io/vitess/go/vt/vttablet/endtoend/framework" | ||
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" | ||
"vitess.io/vitess/go/vt/vttest" | ||
|
||
vttestpb "vitess.io/vitess/go/vt/proto/vttest" | ||
) | ||
|
||
var ( | ||
connParams mysql.ConnParams | ||
connAppDebugParams mysql.ConnParams | ||
cluster vttest.LocalCluster | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() // Do not remove this comment, import into google3 depends on it | ||
tabletenv.Init() | ||
|
||
exitCode := func() int { | ||
// Launch MySQL. | ||
// We need a Keyspace in the topology, so the DbName is set. | ||
// We need a Shard too, so the database 'vttest' is created. | ||
cfg := vttest.Config{ | ||
Topology: &vttestpb.VTTestTopology{ | ||
Keyspaces: []*vttestpb.Keyspace{ | ||
{ | ||
Name: "vttest", | ||
Shards: []*vttestpb.Shard{ | ||
{ | ||
Name: "0", | ||
DbNameOverride: "vttest", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
OnlyMySQL: true, | ||
Charset: "utf8mb4_general_ci", | ||
} | ||
if err := cfg.InitSchemas("vttest", testSchema, nil); err != nil { | ||
fmt.Fprintf(os.Stderr, "InitSchemas failed: %v\n", err) | ||
return 1 | ||
} | ||
defer os.RemoveAll(cfg.SchemaDir) | ||
cluster = vttest.LocalCluster{ | ||
Config: cfg, | ||
} | ||
if err := cluster.Setup(); err != nil { | ||
fmt.Fprintf(os.Stderr, "could not launch mysql: %v\n", err) | ||
return 1 | ||
} | ||
|
||
defer cluster.TearDown() | ||
|
||
connParams = cluster.MySQLConnParams() | ||
connAppDebugParams = cluster.MySQLAppDebugConnParams() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
config := tabletenv.NewDefaultConfig() | ||
config.TwoPCEnable = true | ||
config.TwoPCAbandonAge = 1 | ||
err := framework.StartCustomServer(ctx, connParams, connAppDebugParams, cluster.DbName(), config) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%v", err) | ||
return 1 | ||
} | ||
defer framework.StopServer() | ||
|
||
return m.Run() | ||
}() | ||
os.Exit(exitCode) | ||
} | ||
|
||
var testSchema = `create table vitess_test(intval int default 0 primary key);` |
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,109 @@ | ||
/* | ||
Copyright 2024 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 endtoend | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/vt/vttablet/endtoend/framework" | ||
) | ||
|
||
// TestCommitPreparedFailNonRetryable tests the case where the commit_prepared fails trying to acquire update lock. | ||
// The transaction updates to failed state. | ||
func TestCommitPreparedFailNonRetryable(t *testing.T) { | ||
dbaConnector := framework.Server.Config().DB.DbaWithDB() | ||
conn, err := dbaConnector.Connect(context.Background()) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
|
||
_, err = conn.ExecuteFetch("set global innodb_lock_wait_timeout = 1", 1, false) | ||
require.NoError(t, err) | ||
defer conn.ExecuteFetch("set global innodb_lock_wait_timeout = default", 1, false) | ||
|
||
client := framework.NewClient() | ||
defer client.RollbackPrepared("bb", client.TransactionID()) | ||
|
||
_, err = client.BeginExecute(`insert into vitess_test (intval) values(50)`, nil, nil) | ||
require.NoError(t, err) | ||
err = client.Prepare("bb") | ||
require.NoError(t, err) | ||
|
||
client2 := framework.NewClient() | ||
_, err = client2.BeginExecute(`select * from _vt.redo_state where dtid = 'bb' for update`, nil, nil) | ||
require.NoError(t, err) | ||
|
||
ch := make(chan any) | ||
go func() { | ||
err := client.CommitPrepared("bb") | ||
ch <- nil | ||
require.ErrorContains(t, err, "commit_prepared") | ||
}() | ||
time.Sleep(1500 * time.Millisecond) | ||
|
||
client2.Release() | ||
<-ch | ||
|
||
qr, err := client2.Execute("select dtid, state, message from _vt.redo_state where dtid = 'bb'", nil) | ||
require.NoError(t, err) | ||
require.Equal(t, `[[VARBINARY("bb") INT64(0) TEXT("Lock wait timeout exceeded; try restarting transaction (errno 1205) (sqlstate HY000) during query: delete from _vt.redo_state where dtid = 'bb'")]]`, fmt.Sprintf("%v", qr.Rows)) | ||
} | ||
|
||
// TestCommitPreparedFailRetryable tests the case where the commit_prepared fails when the query is killed. | ||
// The transaction remains in the prepare state. | ||
func TestCommitPreparedFailRetryable(t *testing.T) { | ||
client := framework.NewClient() | ||
defer client.RollbackPrepared("aa", client.TransactionID()) | ||
|
||
_, err := client.BeginExecute(`insert into vitess_test (intval) values(40)`, nil, nil) | ||
require.NoError(t, err) | ||
connRes, err := client.Execute(`select connection_id()`, nil) | ||
require.NoError(t, err) | ||
err = client.Prepare("aa") | ||
require.NoError(t, err) | ||
|
||
client2 := framework.NewClient() | ||
_, err = client2.BeginExecute(`select * from _vt.redo_state where dtid = 'aa' for update`, nil, nil) | ||
require.NoError(t, err) | ||
|
||
ch := make(chan any) | ||
go func() { | ||
err := client.CommitPrepared("aa") | ||
ch <- nil | ||
require.ErrorContains(t, err, "commit_prepared") | ||
}() | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
dbaConnector := framework.Server.Config().DB.DbaWithDB() | ||
conn, err := dbaConnector.Connect(context.Background()) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
|
||
_, err = conn.ExecuteFetch(fmt.Sprintf("kill query %s", connRes.Rows[0][0].ToString()), 1, false) | ||
require.NoError(t, err) | ||
|
||
client2.Release() | ||
<-ch | ||
|
||
qr, err := client2.Execute("select dtid, state, message from _vt.redo_state where dtid = 'aa'", nil) | ||
require.NoError(t, err) | ||
require.Equal(t, `[[VARBINARY("aa") INT64(1) TEXT("Query execution was interrupted (errno 1317) (sqlstate 70100) during query: delete from _vt.redo_state where dtid = 'aa'")]]`, fmt.Sprintf("%v", qr.Rows)) | ||
} |
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
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
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
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
Oops, something went wrong.