Skip to content

Commit 6d9e584

Browse files
author
laodouya
authored
Merge pull request #271 from CovenantSQL/bugfix/isolation_level
Fix isolation level auto rollback bug
2 parents 2f8f5df + 896e060 commit 6d9e584

3 files changed

Lines changed: 94 additions & 14 deletions

File tree

xenomint/state.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ func (s *State) write(
404404
}
405405
defer s.executer.Exec(`ROLLBACK TO "?"`, lastSeq)
406406
}
407+
if s.level != sql.LevelReadUncommitted {
408+
// NOTE(leventeliu): this will cancel any uncommitted transaction, and do not harm to
409+
// committed ones.
410+
defer s.executer.Exec(`ROLLBACK`)
411+
}
407412
for i, v := range req.Payload.Queries {
408413
var res sql.Result
409414
if res, ierr = s.writeSingle(ctx, &v); ierr != nil {
@@ -426,10 +431,6 @@ func (s *State) write(
426431
return
427432
}
428433
}
429-
} else {
430-
// NOTE(leventeliu): this will cancel any uncommitted transaction, and do not harm to
431-
// committed ones.
432-
s.executer.Exec(`ROLLBACK`)
433434
}
434435
// Try to commit if the ongoing tx is too large or schema is changed
435436
if s.getSeq()-s.getLastCommitPoint() > s.maxTx ||

xenomint/state_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,82 @@ func TestSerializableState(t *testing.T) {
818818
})
819819
}
820820
})
821+
Convey("The state should see changes", FailureContinues, func(c C) {
822+
// Build transaction query
823+
var (
824+
count = 1000
825+
queries = make([]types.Query, count+2)
826+
req *types.Request
827+
)
828+
queries[0] = buildQuery(`BEGIN`)
829+
for i := 0; i < count; i++ {
830+
queries[i+1] = buildQuery(
831+
`INSERT INTO t1(k, v) VALUES (?, ?)`, i, fmt.Sprintf("v%d", i),
832+
)
833+
}
834+
queries[count+1] = buildQuery(`COMMIT`)
835+
req = buildRequest(types.WriteQuery, queries)
836+
// Send uncommitted transaction on background
837+
var _, resp, err = state.Query(req, true)
838+
c.So(err, ShouldBeNil)
839+
c.So(resp.Header.RowCount, ShouldEqual, 0)
840+
841+
// Test isolation level
842+
for i := 0; i < count; i++ {
843+
_, resp, err = state.Query(buildRequest(types.ReadQuery, []types.Query{
844+
buildQuery(`SELECT COUNT(1) AS cnt FROM t1`),
845+
}), true)
846+
So(resp.Payload, ShouldResemble, types.ResponsePayload{
847+
Columns: []string{"cnt"},
848+
DeclTypes: []string{""},
849+
Rows: []types.ResponseRow{{Values: []interface{}{int64(count)}}},
850+
})
851+
}
852+
853+
req = buildRequest(types.WriteQuery, []types.Query{
854+
buildQuery("DELETE FROM t1"),
855+
})
856+
_, resp, err = state.Query(req, true)
857+
c.So(err, ShouldBeNil)
858+
})
859+
Convey("The state should not see changes because of failure query content", FailureContinues, func(c C) {
860+
// Build transaction query
861+
var (
862+
count = 1000
863+
queries = make([]types.Query, count+3)
864+
req *types.Request
865+
)
866+
queries[0] = buildQuery(`BEGIN`)
867+
for i := 0; i < count; i++ {
868+
queries[i+1] = buildQuery(
869+
`INSERT INTO t1(k, v) VALUES (?, ?)`, i, fmt.Sprintf("v%d", i),
870+
)
871+
}
872+
queries[count+1] = buildQuery(`HAHA`)
873+
queries[count+2] = buildQuery(`COMMIT`)
874+
req = buildRequest(types.WriteQuery, queries)
875+
// Send uncommitted transaction on background
876+
var _, resp, err = state.Query(req, true)
877+
c.So(err, ShouldNotBeNil)
878+
879+
// Test isolation level
880+
for i := 0; i < count; i++ {
881+
_, resp, err = state.Query(buildRequest(types.ReadQuery, []types.Query{
882+
buildQuery(`SELECT COUNT(1) AS cnt FROM t1`),
883+
}), true)
884+
So(resp.Payload, ShouldResemble, types.ResponsePayload{
885+
Columns: []string{"cnt"},
886+
DeclTypes: []string{""},
887+
Rows: []types.ResponseRow{{Values: []interface{}{int64(0)}}},
888+
})
889+
}
890+
891+
req = buildRequest(types.WriteQuery, []types.Query{
892+
buildQuery("DELETE FROM t1"),
893+
})
894+
_, resp, err = state.Query(req, true)
895+
c.So(err, ShouldBeNil)
896+
})
821897
})
822898
})
823899
}

xenomint/xxx_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"math/rand"
2222
"os"
2323
"path"
24+
"runtime"
2425
"sync"
2526
"sync/atomic"
2627
"syscall"
@@ -198,16 +199,18 @@ func setup() {
198199

199200
rand.Seed(time.Now().UnixNano())
200201

201-
// Set NOFILE limit
202-
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lmt); err != nil {
203-
panic(err)
204-
}
205-
if lmt.Max < minNoFile {
206-
panic("insufficient max RLIMIT_NOFILE")
207-
}
208-
lmt.Cur = lmt.Max
209-
if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lmt); err != nil {
210-
panic(err)
202+
if runtime.GOOS == "linux" {
203+
// Set NOFILE limit
204+
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lmt); err != nil {
205+
panic(err)
206+
}
207+
if lmt.Max < minNoFile {
208+
panic("insufficient max RLIMIT_NOFILE")
209+
}
210+
lmt.Cur = lmt.Max
211+
if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lmt); err != nil {
212+
panic(err)
213+
}
211214
}
212215

213216
// Initialze kms

0 commit comments

Comments
 (0)