Skip to content

Commit

Permalink
v3: instant-commit for autocommit
Browse files Browse the repository at this point in the history
If we are in autocommit mode and vtgate does not break a DML into
smaller parts, then it has the opportunity to send that statement
through to a vttablet as autocommit in a single round-trip.

Reviewer instructions:
@demmer: I'm not too sure about the vtexplain fixes, or if additional
tests are required there. Extra scrutiny may be required there.

Implementation notes:
* SafeSession has a state machine for tracking autocommit state.
* The autocommit state is initialized by executor as needed.
* VCursor API has been changed for ExecMultiShard. It now accepts
  an extra canCommit flag that should be set to true if the engine
  is executing its final DML. This, combined with the autocommit
  state will decide if an instant autocommit is possible.
  • Loading branch information
sougou committed Jan 24, 2018
1 parent fccc652 commit 3f86922
Show file tree
Hide file tree
Showing 15 changed files with 527 additions and 43 deletions.
4 changes: 2 additions & 2 deletions data/test/vtexplain/multi-output/deletesharded-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ delete from music_extra where id=1

1 ks_sharded/-40: begin
1 ks_sharded/-40: delete from music_extra where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */
2 ks_sharded/-40: commit
1 ks_sharded/-40: commit

----------------------------------------------------------------------
delete from music_extra where id=1 and extra='abc'

1 ks_sharded/-40: begin
1 ks_sharded/-40: select id from music_extra where id = 1 and extra = 'abc' limit 10001 for update /* vtgate:: keyspace_id:166b40b44aba4bd6 */
1 ks_sharded/-40: delete from music_extra where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */
2 ks_sharded/-40: commit
1 ks_sharded/-40: commit

----------------------------------------------------------------------
delete from user where id=1
Expand Down
10 changes: 5 additions & 5 deletions data/test/vtexplain/multi-output/unsharded-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ 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)
2 ks_unsharded/-: commit
1 ks_unsharded/-: commit

----------------------------------------------------------------------
update t1 set intval = 10

1 ks_unsharded/-: begin
1 ks_unsharded/-: select id from t1 limit 10001 for update
1 ks_unsharded/-: update t1 set intval = 10 where id in (1)
2 ks_unsharded/-: commit
1 ks_unsharded/-: commit

----------------------------------------------------------------------
update t1 set floatval = 9.99

1 ks_unsharded/-: begin
1 ks_unsharded/-: select id from t1 limit 10001 for update
1 ks_unsharded/-: update t1 set floatval = 9.99 where id in (1)
2 ks_unsharded/-: commit
1 ks_unsharded/-: commit

----------------------------------------------------------------------
delete from t1 where id = 100

1 ks_unsharded/-: begin
1 ks_unsharded/-: delete from t1 where id in (100)
2 ks_unsharded/-: commit
1 ks_unsharded/-: commit

----------------------------------------------------------------------
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
2 ks_unsharded/-: commit
1 ks_unsharded/-: commit

----------------------------------------------------------------------
4 changes: 2 additions & 2 deletions data/test/vtexplain/multi-output/updatesharded-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ update user set nickname='alice' where id=1

1 ks_sharded/-40: begin
1 ks_sharded/-40: update user set nickname = 'alice' where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */
2 ks_sharded/-40: commit
1 ks_sharded/-40: commit

----------------------------------------------------------------------
update user set nickname='alice' where name='alice'
Expand All @@ -21,7 +21,7 @@ update user set pet='fido' where id=1

1 ks_sharded/-40: begin
1 ks_sharded/-40: update user set pet = 'fido' where id in (1) /* vtgate:: keyspace_id:166b40b44aba4bd6 */
2 ks_sharded/-40: commit
1 ks_sharded/-40: commit

----------------------------------------------------------------------
update user set name='alicia' where id=1
Expand Down
20 changes: 20 additions & 0 deletions go/vt/vtexplain/vtexplain_vttablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@ func (t *explainTablet) ReadTransaction(ctx context.Context, target *querypb.Tar
return t.tsv.ReadTransaction(ctx, target, dtid)
}

// ExecuteBatch is part of the QueryService interface.
func (t *explainTablet) ExecuteBatch(ctx context.Context, target *querypb.Target, queries []*querypb.BoundQuery, asTransaction bool, transactionID int64, options *querypb.ExecuteOptions) ([]sqltypes.Result, error) {
t.mu.Lock()
t.currentTime = batchTime.Wait()

// Since the query is simulated being "sent" over the wire we need to
// copy the bindVars into the executor to avoid a data race.
for _, query := range queries {
bindVariables := sqltypes.CopyBindVariables(query.BindVariables)
t.tabletQueries = append(t.tabletQueries, &TabletQuery{
Time: t.currentTime,
SQL: query.Sql,
BindVars: bindVariables,
})
}
t.mu.Unlock()

return t.tsv.ExecuteBatch(ctx, target, queries, asTransaction, transactionID, options)
}

// BeginExecute is part of the QueryService interface.
func (t *explainTablet) BeginExecute(ctx context.Context, target *querypb.Target, sql string, bindVariables map[string]*querypb.BindVariable, options *querypb.ExecuteOptions) (*sqltypes.Result, int64, error) {
t.mu.Lock()
Expand Down
Loading

0 comments on commit 3f86922

Please sign in to comment.