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

in sharding mode, support local transaction #255

Merged
merged 3 commits into from
Sep 5, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: add local transaction integration test cases
dk-lockdown committed Aug 22, 2022
commit b21cd62da9a8e2e39c5804bfdd81e58bf037df0c
3 changes: 3 additions & 0 deletions pkg/group/transaction.go
Original file line number Diff line number Diff line change
@@ -94,6 +94,7 @@ func (tx *ComplexTx) Begin(ctx context.Context, executor proto.DBGroupExecutor)
if err != nil {
return nil, err
}
log.Debugf("DBGroup %s has begun local transaction!", executor.GroupName())
tx.txs[executor.GroupName()] = childTx
return childTx, nil
}
@@ -117,6 +118,7 @@ func (tx *ComplexTx) Commit(ctx context.Context) (result proto.Result, err error
log.Errorf("commit failed, db group: %s, err: %v", group, err)
return err
}
log.Debugf("DBGroup %s has committed local transaction!", group)
return nil
})
}
@@ -148,6 +150,7 @@ func (tx *ComplexTx) Rollback(ctx context.Context) (result proto.Result, err err
log.Errorf("rollback failed, db group: %s, err: %v", group, err)
return err
}
log.Debugf("DBGroup %s has rollbacked local transaction!", group)
return nil
})
}
57 changes: 41 additions & 16 deletions test/shd/sharding_test.go
Original file line number Diff line number Diff line change
@@ -263,22 +263,47 @@ func (suite *_ShardingSuite) TestUpdateCity() {
suite.Assert().Nil(err)
suite.Assert().Equal(int64(11), affectedRows)

rows, err := suite.db.Query(selectCityOrderBy1, 200, 210)
if suite.NoErrorf(err, "select row error: %v", err) {
var (
id int64
name string
countryCode string
district string
population int
)
for rows.Next() {
err := rows.Scan(&id, &name, &countryCode, &district, &population)
suite.NoError(err)
suite.T().Logf("id: %d, name: %s, country code: %s, district: %s, population: %d",
id, name, countryCode, district, population)
}
}
suite.TestSelectOrderBy()
}

func (suite *_ShardingSuite) TestLocalTransaction_2_Commit() {
suite.TestSelectOrderBy()
tx, err := suite.db.Begin()
suite.Assert().Nil(err)
result, err := tx.Exec(updateCity, 200, 210)
suite.Assert().Nil(err)
affectedRows, err := result.RowsAffected()
suite.Assert().Nil(err)
suite.Assert().Equal(int64(11), affectedRows)

result, err = tx.Exec(deleteCity, 30, 40)
suite.Assert().Nil(err)
affectedRows, err = result.RowsAffected()
suite.Assert().Nil(err)
suite.Assert().Equal(int64(11), affectedRows)
err = tx.Commit()
suite.Assert().Nil(err)
suite.TestSelectOrderBy()
}

func (suite *_ShardingSuite) TestLocalTransaction_1_Rollback() {
suite.TestSelectOrderBy()
tx, err := suite.db.Begin()
suite.Assert().Nil(err)
result, err := tx.Exec(updateCity, 200, 210)
suite.Assert().Nil(err)
affectedRows, err := result.RowsAffected()
suite.Assert().Nil(err)
suite.Assert().Equal(int64(11), affectedRows)

result, err = tx.Exec(deleteCity, 30, 40)
suite.Assert().Nil(err)
affectedRows, err = result.RowsAffected()
suite.Assert().Nil(err)
suite.Assert().Equal(int64(11), affectedRows)
err = tx.Rollback()
suite.Assert().Nil(err)
suite.TestSelectOrderBy()
}

func (suite *_ShardingSuite) TearDownSuite() {