Skip to content

Commit

Permalink
*: add autocommit-false-is-txn to set autocommit=0 start txn radondb#298
Browse files Browse the repository at this point in the history
  • Loading branch information
andyli029 committed Aug 29, 2019
1 parent ec27550 commit 71b88dc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type ProxyConfig struct {
LongQueryTime int `json:"long-query-time"`
StreamBufferSize int `json:"stream-buffer-size"`
IdleTxnTimeout uint32 `json:"kill-idle-transaction"` //is consistent with the official 8.0 kill_idle_transaction

//If autocommit-false-is-txn=true (false by default), a client connection with cmd: set autocommit=0
//is treated as start a transaction, e.g. begin, start transaction
AutocommitFalseIsTxn bool `json:"autocommit-false-is-txn"`
}

// DefaultProxyConfig returns default proxy config.
Expand Down
8 changes: 8 additions & 0 deletions src/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ func (p *Proxy) SetTwoPC(enable bool) {
p.conf.Proxy.TwopcEnable = enable
}

// SetAutocommitFalseIsTxn used to set autocommitFalseIsTxn to true or false.
func (p *Proxy) SetAutocommitFalseIsTxn(enable bool) {
p.mu.Lock()
defer p.mu.Unlock()
p.log.Info("proxy.SetAutocommitFalseIsTxn:[%v->%v]", p.conf.Proxy.AutocommitFalseIsTxn, enable)
p.conf.Proxy.AutocommitFalseIsTxn = enable
}

// SetAllowIP used to set allow ips.
func (p *Proxy) SetAllowIP(ips []string) {
p.mu.Lock()
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,14 @@ func (spanner *Spanner) ComQuery(session *driver.Session, query string, bindVari
spanner.auditLog(session, R, xbase.RADON, query, qr)
return returnQuery(qr, callback, err)
case *sqlparser.Set:
log.Warning("proxy.query.set.query:%s", query)
if qr, err = spanner.handleSet(session, query, node); err != nil {
log.Error("proxy.set[%s].from.session[%v].error:%+v", query, session.ID(), err)
}
spanner.auditLog(session, R, xbase.SET, query, qr)
return returnQuery(qr, callback, err)
case *sqlparser.Checksum:
log.Warning("proxy.query.checksum.query:%s", query)
if qr, err = spanner.handleChecksumTable(session, query, node); err != nil {
log.Error("proxy.checksum[%s].from.session[%v].error:%+v", query, session.ID(), err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/proxy/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (spanner *Spanner) handleSet(session *driver.Session, query string, node *s
txSession.setStreamingFetchVar(false)
}
}

case var_mysql_autocommit:
var autocommit = true

Expand All @@ -68,7 +69,7 @@ func (spanner *Spanner) handleSet(session *driver.Session, query string, node *s
}
}
}
if !autocommit {
if !autocommit && spanner.isAutocommitFalseIsTxn() {
query := "begin"
node := &sqlparser.Transaction{
Action: "begin",
Expand Down
31 changes: 30 additions & 1 deletion src/proxy/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ func TestProxySet(t *testing.T) {
}

func TestProxySetAutocommit(t *testing.T) {
log := xlog.NewStdLog(xlog.Level(xlog.DEBUG))
log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
fakedbs, proxy, cleanup := MockProxy(log)
defer cleanup()
address := proxy.Address()

// fakedbs.
{
proxy.conf.Proxy.TwopcEnable = true
proxy.conf.Proxy.AutocommitFalseIsTxn = true
fakedbs.AddQueryPattern("create .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("select .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("xa .*", &sqltypes.Result{})
Expand Down Expand Up @@ -119,4 +120,32 @@ func TestProxySetAutocommit(t *testing.T) {
assert.NotNil(t, err)
}
}

proxy.conf.Proxy.AutocommitFalseIsTxn = false
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
{
query := "set autocommit=0"
_, err := client.FetchAll(query, -1)
assert.Nil(t, err)

query = "select 1"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)

query = "commit"
_, err = client.FetchAll(query, -1)
assert.NotNil(t, err)
}
{
query := "set autocommit=1"
_, err := client.FetchAll(query, -1)
assert.Nil(t, err)

query = "commit"
_, err = client.FetchAll(query, -1)
assert.NotNil(t, err)
}
}
}
4 changes: 4 additions & 0 deletions src/proxy/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ func (spanner *Spanner) ServerVersion() string {
func (spanner *Spanner) isTwoPC() bool {
return spanner.conf.Proxy.TwopcEnable
}

func (spanner *Spanner) isAutocommitFalseIsTxn() bool {
return spanner.conf.Proxy.AutocommitFalseIsTxn
}

0 comments on commit 71b88dc

Please sign in to comment.