diff --git a/go/vt/vtexplain/testdata/multi-output/deletesharded-output.txt b/go/vt/vtexplain/testdata/multi-output/deletesharded-output.txt index f6d341efb45..2a24f2f8736 100644 --- a/go/vt/vtexplain/testdata/multi-output/deletesharded-output.txt +++ b/go/vt/vtexplain/testdata/multi-output/deletesharded-output.txt @@ -56,17 +56,9 @@ delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from music_extra where extra='abc' ---------------------------------------------------------------------- delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from `ks_sharded[-]`.music_extra where extra='abc' LIMIT 10 -1 ks_sharded/-40: begin 1 ks_sharded/-40: delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from music_extra where extra = 'abc' limit 10 -1 ks_sharded/-40: commit -1 ks_sharded/40-80: begin 1 ks_sharded/40-80: delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from music_extra where extra = 'abc' limit 10 -1 ks_sharded/40-80: commit -1 ks_sharded/80-c0: begin 1 ks_sharded/80-c0: delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from music_extra where extra = 'abc' limit 10 -1 ks_sharded/80-c0: commit -1 ks_sharded/c0-: begin 1 ks_sharded/c0-: delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from music_extra where extra = 'abc' limit 10 -1 ks_sharded/c0-: commit ---------------------------------------------------------------------- diff --git a/go/vt/vtexplain/testdata/multi-output/insertsharded-output.txt b/go/vt/vtexplain/testdata/multi-output/insertsharded-output.txt index 1de476b4d5d..e9779e78b4b 100644 --- a/go/vt/vtexplain/testdata/multi-output/insertsharded-output.txt +++ b/go/vt/vtexplain/testdata/multi-output/insertsharded-output.txt @@ -65,11 +65,7 @@ insert into user (id, name, nickname, address) values(2, 'bob', 'bobby', '123 ma ---------------------------------------------------------------------- insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into music_extra (id, extra) values (1, 'a'), (2, 'b'), (3, 'c') -1 ks_sharded/-40: begin 1 ks_sharded/-40: insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into music_extra(id, extra) values (1, 'a'), (2, 'b') -1 ks_sharded/-40: commit -1 ks_sharded/40-80: begin 1 ks_sharded/40-80: insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into music_extra(id, extra) values (3, 'c') -1 ks_sharded/40-80: commit ---------------------------------------------------------------------- diff --git a/go/vt/vtexplain/testdata/multi-output/target-output.txt b/go/vt/vtexplain/testdata/multi-output/target-output.txt index abf70f5b57d..3e767f4dde3 100644 --- a/go/vt/vtexplain/testdata/multi-output/target-output.txt +++ b/go/vt/vtexplain/testdata/multi-output/target-output.txt @@ -11,8 +11,6 @@ select * from user where id in (1,2,3,4,5,6,7,8) ---------------------------------------------------------------------- insert into user (id, name) values (2, 'bob') -1 ks_sharded/40-80: begin 1 ks_sharded/40-80: insert into user(id, name) values (2, 'bob') -1 ks_sharded/40-80: commit ---------------------------------------------------------------------- diff --git a/go/vt/vtexplain/testdata/multi-output/unsharded-output.txt b/go/vt/vtexplain/testdata/multi-output/unsharded-output.txt index 98bdad579b9..86472e79a19 100644 --- a/go/vt/vtexplain/testdata/multi-output/unsharded-output.txt +++ b/go/vt/vtexplain/testdata/multi-output/unsharded-output.txt @@ -6,9 +6,7 @@ select * from t1 ---------------------------------------------------------------------- insert into t1 (id,intval,floatval) values (1,2,3.14) -1 ks_unsharded/-: begin 1 ks_unsharded/-: insert into t1(id, intval, floatval) values (1, 2, 3.14) -1 ks_unsharded/-: commit ---------------------------------------------------------------------- update t1 set intval = 10 @@ -34,9 +32,7 @@ delete from t1 where id = 100 ---------------------------------------------------------------------- insert into t1 (id,intval,floatval) values (1,2,3.14) on duplicate key update intval=3, floatval=3.14 -1 ks_unsharded/-: begin 1 ks_unsharded/-: insert into t1(id, intval, floatval) values (1, 2, 3.14) on duplicate key update intval = 3, floatval = 3.14 -1 ks_unsharded/-: commit ---------------------------------------------------------------------- select ID from t1 diff --git a/go/vt/vtgate/autocommit_test.go b/go/vt/vtgate/autocommit_test.go index 156e86162ef..9f00225a053 100644 --- a/go/vt/vtgate/autocommit_test.go +++ b/go/vt/vtgate/autocommit_test.go @@ -21,7 +21,7 @@ import ( "github.com/stretchr/testify/require" - "golang.org/x/net/context" + "context" "vitess.io/vitess/go/sqltypes" @@ -41,15 +41,13 @@ func TestAutocommitUpdateSharded(t *testing.T) { _, err := autocommitExec(executor, "update user set a=2 where id = 1") require.NoError(t, err) - testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{ + testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ Sql: "update user set a = 2 where id = 1", BindVariables: map[string]*querypb.BindVariable{}, - }) - testAsTransactionCount(t, "sbc1", sbc1, 1) + }}) testCommitCount(t, "sbc1", sbc1, 0) - testBatchQuery(t, "sbc2", sbc2, nil) - testAsTransactionCount(t, "sbc2", sbc2, 0) + testQueries(t, "sbc2", sbc2, nil) testCommitCount(t, "sbc1", sbc1, 0) } @@ -73,14 +71,12 @@ func TestAutocommitUpdateLookup(t *testing.T) { "music_id": vars, }, }}) - testAsTransactionCount(t, "sbclookup", sbclookup, 0) testCommitCount(t, "sbclookup", sbclookup, 1) testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ Sql: "update music set a = 2 where id = 2", BindVariables: map[string]*querypb.BindVariable{}, }}) - testAsTransactionCount(t, "sbc1", sbc1, 0) testCommitCount(t, "sbc1", sbc1, 1) } @@ -111,7 +107,6 @@ func TestAutocommitUpdateVindexChange(t *testing.T) { "keyspace_id_0": sqltypes.BytesBindVariable([]byte("\026k@\264J\272K\326")), }, }}) - testAsTransactionCount(t, "sbclookup", sbclookup, 0) testCommitCount(t, "sbclookup", sbclookup, 1) testQueries(t, "sbc", sbc, []*querypb.BoundQuery{{ @@ -121,7 +116,6 @@ func TestAutocommitUpdateVindexChange(t *testing.T) { Sql: "update user2 set name = 'myname', lastname = 'mylastname' where id = 1", BindVariables: map[string]*querypb.BindVariable{}, }}) - testAsTransactionCount(t, "sbc", sbc, 0) testCommitCount(t, "sbc", sbc, 1) } @@ -132,15 +126,13 @@ func TestAutocommitDeleteSharded(t *testing.T) { _, err := autocommitExec(executor, "delete from user_extra where user_id = 1") require.NoError(t, err) - testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{ + testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ Sql: "delete from user_extra where user_id = 1", BindVariables: map[string]*querypb.BindVariable{}, - }) - testAsTransactionCount(t, "sbc1", sbc1, 1) + }}) testCommitCount(t, "sbc1", sbc1, 0) - testBatchQuery(t, "sbc2", sbc2, nil) - testAsTransactionCount(t, "sbc2", sbc2, 0) + testQueries(t, "sbc2", sbc2, nil) testCommitCount(t, "sbc1", sbc1, 0) } @@ -174,7 +166,6 @@ func TestAutocommitDeleteLookup(t *testing.T) { "user_id": sqltypes.Uint64BindVariable(1), }, }}) - testAsTransactionCount(t, "sbclookup", sbclookup, 0) testCommitCount(t, "sbclookup", sbclookup, 1) testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ @@ -184,7 +175,6 @@ func TestAutocommitDeleteLookup(t *testing.T) { Sql: "delete from music where id = 1", BindVariables: map[string]*querypb.BindVariable{}, }}) - testAsTransactionCount(t, "sbc1", sbc1, 0) testCommitCount(t, "sbc1", sbc1, 1) } @@ -195,15 +185,13 @@ func TestAutocommitDeleteIn(t *testing.T) { _, err := autocommitExec(executor, "delete from user_extra where user_id in (1, 2)") require.NoError(t, err) - testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{ + testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ Sql: "delete from user_extra where user_id in (1, 2)", BindVariables: map[string]*querypb.BindVariable{}, - }) - testAsTransactionCount(t, "sbc1", sbc1, 1) + }}) testCommitCount(t, "sbc1", sbc1, 0) - testBatchQuery(t, "sbc2", sbc2, nil) - testAsTransactionCount(t, "sbc2", sbc2, 0) + testQueries(t, "sbc2", sbc2, nil) testCommitCount(t, "sbc2", sbc2, 0) } @@ -218,16 +206,12 @@ func TestAutocommitDeleteMultiShard(t *testing.T) { Sql: "delete from user_extra where user_id = user_id + 1", BindVariables: map[string]*querypb.BindVariable{}, }}) - testBatchQuery(t, "sbc1", sbc1, nil) - testAsTransactionCount(t, "sbc1", sbc1, 0) testCommitCount(t, "sbc1", sbc1, 1) testQueries(t, "sbc2", sbc2, []*querypb.BoundQuery{{ Sql: "delete from user_extra where user_id = user_id + 1", BindVariables: map[string]*querypb.BindVariable{}, }}) - testBatchQuery(t, "sbc2", sbc2, nil) - testAsTransactionCount(t, "sbc2", sbc2, 0) testCommitCount(t, "sbc2", sbc2, 1) } @@ -238,18 +222,16 @@ func TestAutocommitDeleteMultiShardAutoCommit(t *testing.T) { _, err := autocommitExec(executor, "delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from user_extra where user_id = user_id + 1") require.NoError(t, err) - testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{ + testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ Sql: "delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from user_extra where user_id = user_id + 1", BindVariables: map[string]*querypb.BindVariable{}, - }) - testAsTransactionCount(t, "sbc1", sbc1, 1) + }}) testCommitCount(t, "sbc1", sbc1, 0) - testBatchQuery(t, "sbc2", sbc2, &querypb.BoundQuery{ + testQueries(t, "sbc2", sbc2, []*querypb.BoundQuery{{ Sql: "delete /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ from user_extra where user_id = user_id + 1", BindVariables: map[string]*querypb.BindVariable{}, - }) - testAsTransactionCount(t, "sbc2", sbc2, 1) + }}) testCommitCount(t, "sbc1", sbc1, 0) } @@ -260,17 +242,15 @@ func TestAutocommitInsertSharded(t *testing.T) { _, err := autocommitExec(executor, "insert into user_extra(user_id, v) values (1, 2)") require.NoError(t, err) - testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{ + testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ Sql: "insert into user_extra(user_id, v) values (:_user_id_0, 2)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), }, - }) - testAsTransactionCount(t, "sbc1", sbc1, 1) + }}) testCommitCount(t, "sbc1", sbc1, 0) - testBatchQuery(t, "sbc2", sbc2, nil) - testAsTransactionCount(t, "sbc2", sbc2, 0) + testQueries(t, "sbc2", sbc2, nil) testCommitCount(t, "sbc1", sbc1, 0) } @@ -288,7 +268,6 @@ func TestAutocommitInsertLookup(t *testing.T) { "user_id_0": sqltypes.Uint64BindVariable(1), }, }}) - testAsTransactionCount(t, "sbclookup", sbclookup, 0) testCommitCount(t, "sbclookup", sbclookup, 1) testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ @@ -299,7 +278,6 @@ func TestAutocommitInsertLookup(t *testing.T) { "__seq0": sqltypes.Int64BindVariable(1), }, }}) - testAsTransactionCount(t, "sbc1", sbc1, 0) testCommitCount(t, "sbc1", sbc1, 1) } @@ -310,24 +288,22 @@ func TestAutocommitInsertMultishardAutoCommit(t *testing.T) { _, err := autocommitExec(executor, "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (1, 2), (3, 4)") require.NoError(t, err) - testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{ + testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{ Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_0, 2)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), "_user_id_1": sqltypes.Int64BindVariable(3), }, - }) - testAsTransactionCount(t, "sbc1", sbc1, 1) + }}) testCommitCount(t, "sbc1", sbc1, 0) - testBatchQuery(t, "sbc2", sbc2, &querypb.BoundQuery{ + testQueries(t, "sbc2", sbc2, []*querypb.BoundQuery{{ Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_1, 4)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), "_user_id_1": sqltypes.Int64BindVariable(3), }, - }) - testAsTransactionCount(t, "sbc2", sbc2, 1) + }}) testCommitCount(t, "sbc2", sbc2, 0) executor, sbc1, sbc2, _ = createExecutorEnv() @@ -336,20 +312,16 @@ func TestAutocommitInsertMultishardAutoCommit(t *testing.T) { _, err = autocommitExec(executor, "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (1, 2), (3, 4)") require.Error(t, err) require.Contains(t, err.Error(), "INVALID_ARGUMENT", "expected invalid argument error") - require.Empty(t, sbc1.Queries) - require.Empty(t, sbc1.BatchQueries) - testAsTransactionCount(t, "sbc1", sbc1, 1) testCommitCount(t, "sbc1", sbc1, 0) - testBatchQuery(t, "sbc2", sbc2, &querypb.BoundQuery{ + testQueries(t, "sbc2", sbc2, []*querypb.BoundQuery{{ Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_1, 4)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), "_user_id_1": sqltypes.Int64BindVariable(3), }, - }) - testAsTransactionCount(t, "sbc2", sbc2, 1) + }}) testCommitCount(t, "sbc2", sbc2, 0) } @@ -367,7 +339,6 @@ func TestAutocommitInsertMultishard(t *testing.T) { "_user_id_1": sqltypes.Int64BindVariable(3), }, }}) - testAsTransactionCount(t, "sbc1", sbc1, 0) testCommitCount(t, "sbc1", sbc1, 1) testQueries(t, "sbc2", sbc2, []*querypb.BoundQuery{{ @@ -377,7 +348,6 @@ func TestAutocommitInsertMultishard(t *testing.T) { "_user_id_1": sqltypes.Int64BindVariable(3), }, }}) - testAsTransactionCount(t, "sbc2", sbc2, 0) testCommitCount(t, "sbc2", sbc2, 1) } @@ -391,14 +361,12 @@ func TestAutocommitInsertAutoinc(t *testing.T) { testQueries(t, "sbclookup", sbclookup, []*querypb.BoundQuery{{ Sql: "select next :n values from user_seq", BindVariables: map[string]*querypb.BindVariable{"n": sqltypes.Int64BindVariable(1)}, - }}) - testBatchQuery(t, "sbclookup", sbclookup, &querypb.BoundQuery{ + }, { Sql: "insert into main1(id, name) values (:__seq0, 'myname')", BindVariables: map[string]*querypb.BindVariable{ "__seq0": sqltypes.Int64BindVariable(1), }, - }) - testAsTransactionCount(t, "sbclookup", sbclookup, 1) + }}) testCommitCount(t, "sbclookup", sbclookup, 0) } @@ -421,7 +389,6 @@ func TestAutocommitTransactionStarted(t *testing.T) { Sql: "update user set a = 2 where id = 1", BindVariables: map[string]*querypb.BindVariable{}, }}) - testAsTransactionCount(t, "sbc1", sbc1, 0) testCommitCount(t, "sbc1", sbc1, 0) } @@ -439,11 +406,10 @@ func TestAutocommitDirectTarget(t *testing.T) { _, err := executor.Execute(context.Background(), "TestExecute", NewSafeSession(session), sql, map[string]*querypb.BindVariable{}) require.NoError(t, err) - testBatchQuery(t, "sbclookup", sbclookup, &querypb.BoundQuery{ + testQueries(t, "sbclookup", sbclookup, []*querypb.BoundQuery{{ Sql: sql, BindVariables: map[string]*querypb.BindVariable{}, - }) - testAsTransactionCount(t, "sbclookup", sbclookup, 1) + }}) testCommitCount(t, "sbclookup", sbclookup, 0) } @@ -465,7 +431,6 @@ func TestAutocommitDirectRangeTarget(t *testing.T) { Sql: sql, BindVariables: map[string]*querypb.BindVariable{}, }}) - testAsTransactionCount(t, "sbc1", sbc1, 0) testCommitCount(t, "sbc1", sbc1, 1) } diff --git a/go/vt/vtgate/executor_dml_test.go b/go/vt/vtgate/executor_dml_test.go index 460cae3a4cf..dd4c471adcb 100644 --- a/go/vt/vtgate/executor_dml_test.go +++ b/go/vt/vtgate/executor_dml_test.go @@ -712,7 +712,7 @@ func TestInsertShardedAutocommitLookup(t *testing.T) { }, }} // autocommit should go as ExecuteBatch - if !reflect.DeepEqual(sbclookup.BatchQueries[0], wantQueries) { + if !reflect.DeepEqual(sbclookup.Queries, wantQueries) { t.Errorf("sbclookup.BatchQueries[0]: \n%+v, want \n%+v", sbclookup.BatchQueries[0], wantQueries) } } diff --git a/go/vt/vtgate/executor_framework_test.go b/go/vt/vtgate/executor_framework_test.go index 344c6688885..198d3779f75 100644 --- a/go/vt/vtgate/executor_framework_test.go +++ b/go/vt/vtgate/executor_framework_test.go @@ -437,26 +437,6 @@ func executorStream(executor *Executor, sql string) (qr *sqltypes.Result, err er return qr, nil } -// testBatchQuery verifies that a single (or no) query ExecuteBatch was performed on the SandboxConn. -func testBatchQuery(t *testing.T, sbcName string, sbc *sandboxconn.SandboxConn, boundQuery *querypb.BoundQuery) { - t.Helper() - - var wantQueries [][]*querypb.BoundQuery - if boundQuery != nil { - wantQueries = [][]*querypb.BoundQuery{{boundQuery}} - } - if !reflect.DeepEqual(sbc.BatchQueries, wantQueries) { - t.Errorf("%s.BatchQueries:\n%+v, want\n%+v\n", sbcName, sbc.BatchQueries, wantQueries) - } -} - -func testAsTransactionCount(t *testing.T, sbcName string, sbc *sandboxconn.SandboxConn, want int) { - t.Helper() - if got, want := sbc.AsTransactionCount.Get(), int64(want); got != want { - t.Errorf("%s.AsTransactionCount: %d, want %d\n", sbcName, got, want) - } -} - func testQueries(t *testing.T, sbcName string, sbc *sandboxconn.SandboxConn, wantQueries []*querypb.BoundQuery) { t.Helper() if !reflect.DeepEqual(sbc.Queries, wantQueries) { diff --git a/go/vt/vtgate/executor_test.go b/go/vt/vtgate/executor_test.go index afffc780639..62e37cfd11b 100644 --- a/go/vt/vtgate/executor_test.go +++ b/go/vt/vtgate/executor_test.go @@ -290,16 +290,10 @@ func TestExecutorAutocommit(t *testing.T) { t.Errorf("Commit count: %d, want %d", got, want) } - // In the following section, we look at AsTransaction count instead of CommitCount because - // the update results in a single round-trip ExecuteBatch call. - startCount = sbclookup.AsTransactionCount.Get() _, err = executor.Execute(ctx, "TestExecute", session, "update main1 set id=1", nil) require.NoError(t, err) wantSession = &vtgatepb.Session{Autocommit: true, TargetString: "@master", FoundRows: 1, RowCount: 1} utils.MustMatch(t, wantSession, session.Session, "session does not match for autocommit=1") - if got, want := sbclookup.AsTransactionCount.Get(), startCount+1; got != want { - t.Errorf("Commit count: %d, want %d", got, want) - } logStats = testQueryLog(t, logChan, "TestExecute", "UPDATE", "update main1 set id=1", 1) assert.NotEqual(t, time.Duration(0), logStats.CommitTime, "logstats: expected non-zero CommitTime") diff --git a/go/vt/vtgate/scatter_conn.go b/go/vt/vtgate/scatter_conn.go index cdefed7ceb5..80410d6efcb 100644 --- a/go/vt/vtgate/scatter_conn.go +++ b/go/vt/vtgate/scatter_conn.go @@ -168,7 +168,11 @@ func (stc *ScatterConn) Execute( ) switch { case autocommit: - innerqr, err = stc.executeAutocommit(ctx, rs, query, bindVars, opts) + // As this is auto-commit, the transactionID is supposed to be zero. + if transactionID != 0 { + return 0, nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "In autocommit mode, transactionID is non-zero: %d", transactionID) + } + innerqr, err = rs.Gateway.Execute(ctx, rs.Target, query, bindVars, 0, 0, opts) case shouldBegin: innerqr, transactionID, alias, err = rs.Gateway.BeginExecute(ctx, rs.Target, nil, query, bindVars, 0, options) default: @@ -245,8 +249,11 @@ func (stc *ScatterConn) ExecuteMultiShard( switch { case autocommit: - // tansactionID and alias are not used by this call, it is one round trip - innerqr, err = stc.executeAutocommit(ctx, rs, queries[i].Sql, queries[i].BindVariables, opts) + // As this is auto-commit, the transactionID is supposed to be zero. + if transactionID != 0 { + return 0, nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "In autocommit mode, transactionID is non-zero: %d", transactionID) + } + innerqr, err = rs.Gateway.Execute(ctx, rs.Target, queries[i].Sql, queries[i].BindVariables, 0, 0, opts) case shouldBegin: innerqr, transactionID, alias, err = rs.Gateway.BeginExecute(ctx, rs.Target, nil, queries[i].Sql, queries[i].BindVariables, 0, opts) default: @@ -282,20 +289,6 @@ func (stc *ScatterConn) ExecuteMultiShard( return qr, allErrors.GetErrors() } -func (stc *ScatterConn) executeAutocommit(ctx context.Context, rs *srvtopo.ResolvedShard, sql string, bindVariables map[string]*querypb.BindVariable, options *querypb.ExecuteOptions) (*sqltypes.Result, error) { - queries := []*querypb.BoundQuery{{ - Sql: sql, - BindVariables: bindVariables, - }} - // ExecuteBatch is a stop-gap because it's the only function that can currently do - // single round-trip commit. - qrs, err := rs.Gateway.ExecuteBatch(ctx, rs.Target, queries, true /* asTransaction */, 0, options) - if err != nil { - return nil, err - } - return &qrs[0], nil -} - func (stc *ScatterConn) processOneStreamingResult(mu *sync.Mutex, fieldSent *bool, qr *sqltypes.Result, callback func(*sqltypes.Result) error) error { mu.Lock() defer mu.Unlock() diff --git a/go/vt/vtgate/scatter_conn_test.go b/go/vt/vtgate/scatter_conn_test.go index 5249f0db99e..8a5a5f50478 100644 --- a/go/vt/vtgate/scatter_conn_test.go +++ b/go/vt/vtgate/scatter_conn_test.go @@ -22,6 +22,8 @@ import ( "strings" "testing" + "vitess.io/vitess/go/test/utils" + "github.com/stretchr/testify/assert" "golang.org/x/net/context" @@ -53,6 +55,67 @@ func TestScatterConnExecute(t *testing.T) { }) } +func TestExecuteFailOnAutocommit(t *testing.T) { + + createSandbox("TestExecuteFailOnAutocommit") + hc := discovery.NewFakeLegacyHealthCheck() + sc := newTestLegacyScatterConn(hc, new(sandboxTopo), "aa") + sbc0 := hc.AddTestTablet("aa", "0", 1, "TestExecuteFailOnAutocommit", "0", topodatapb.TabletType_MASTER, true, 1, nil) + sbc1 := hc.AddTestTablet("aa", "1", 1, "TestExecuteFailOnAutocommit", "1", topodatapb.TabletType_MASTER, true, 1, nil) + + rss := []*srvtopo.ResolvedShard{ + { + Target: &querypb.Target{ + Keyspace: "TestExecuteFailOnAutocommit", + Shard: "0", + TabletType: topodatapb.TabletType_MASTER, + }, + Gateway: sbc0, + }, + { + Target: &querypb.Target{ + Keyspace: "TestExecuteFailOnAutocommit", + Shard: "1", + TabletType: topodatapb.TabletType_MASTER, + }, + Gateway: sbc1, + }, + } + queries := []*querypb.BoundQuery{ + { + // This will fail to go to shard. It will be rejected at vtgate. + Sql: "query1", + BindVariables: map[string]*querypb.BindVariable{ + "bv0": sqltypes.Int64BindVariable(0), + }, + }, + { + // This will go to shard. + Sql: "query2", + BindVariables: map[string]*querypb.BindVariable{ + "bv1": sqltypes.Int64BindVariable(1), + }, + }, + } + // shard 0 - has transaction + // shard 1 - does not have transaction. + session := &vtgatepb.Session{ + InTransaction: true, + ShardSessions: []*vtgatepb.Session_ShardSession{ + { + Target: &querypb.Target{Keyspace: "TestExecuteFailOnAutocommit", Shard: "0", TabletType: topodatapb.TabletType_MASTER, Cell: "aa"}, + TransactionId: 123, + TabletAlias: nil, + }, + }, + Autocommit: false, + } + _, errs := sc.ExecuteMultiShard(ctx, rss, queries, NewSafeSession(session), false, true) + require.Error(t, vterrors.Aggregate(errs)) + utils.MustMatch(t, 0, len(sbc0.Queries), "") + utils.MustMatch(t, []*querypb.BoundQuery{queries[1]}, sbc1.Queries, "") +} + func TestScatterConnExecuteMulti(t *testing.T) { testScatterConnGeneric(t, "TestScatterConnExecuteMultiShard", func(sc *ScatterConn, shards []string) (*sqltypes.Result, error) { res := srvtopo.NewResolver(&sandboxTopo{}, sc.gateway, "aa")