github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/updates/mvcc_test.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package updates 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 23 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestMutationControllerAppend(t *testing.T) { 28 defer testutils.AfterTest(t)() 29 testutils.EnsureNoLeak(t) 30 mc := NewMVCCHandle(nil) 31 32 nodeCnt := 10000 33 rowsPerNode := uint32(5) 34 //ts := uint64(2) 35 //ts = 4 36 ts := types.NextGlobalTsForTest().Next().Next() 37 //queries := make([]uint64, 0) 38 //queries = append(queries, ts-1) 39 queries := make([]types.TS, 0) 40 queries = append(queries, ts.Prev()) 41 42 for i := 0; i < nodeCnt; i++ { 43 txn := mockTxn() 44 txn.CommitTS = ts 45 txn.PrepareTS = ts 46 node, _ := mc.AddAppendNodeLocked(txn, rowsPerNode*uint32(i), rowsPerNode*(uint32(i)+1)) 47 err := node.ApplyCommit(nil) 48 assert.Nil(t, err) 49 //queries = append(queries, ts+1) 50 queries = append(queries, ts.Next()) 51 //ts += 2 52 ts = ts.Next().Next() 53 } 54 55 st := time.Now() 56 for i, qts := range queries { 57 row, ok, _, _ := mc.GetVisibleRowLocked(qts) 58 if i == 0 { 59 assert.False(t, ok) 60 } else { 61 assert.True(t, ok) 62 assert.Equal(t, uint32(i)*rowsPerNode, row) 63 } 64 } 65 t.Logf("%s -- %d ops", time.Since(st), len(queries)) 66 } 67 68 // AppendNode Start Prepare End Aborted 69 // a1 1,1,1 false 70 // a2 1,3,5 false 71 // a3 1,4,4 false 72 // a4 1,5,5 true 73 func TestGetVisibleRow(t *testing.T) { 74 defer testutils.AfterTest(t)() 75 n := NewMVCCHandle(nil) 76 an1, _ := n.AddAppendNodeLocked(nil, 0, 1) 77 an1.Start = types.BuildTS(1, 0) 78 an1.Prepare = types.BuildTS(1, 0) 79 an1.End = types.BuildTS(1, 0) 80 an2, _ := n.AddAppendNodeLocked(nil, 1, 2) 81 an2.Start = types.BuildTS(1, 0) 82 an2.Prepare = types.BuildTS(3, 0) 83 an2.End = types.BuildTS(5, 0) 84 an3, _ := n.AddAppendNodeLocked(nil, 2, 3) 85 an3.Start = types.BuildTS(1, 0) 86 an3.Prepare = types.BuildTS(4, 0) 87 an3.End = types.BuildTS(4, 0) 88 an4, _ := n.AddAppendNodeLocked(nil, 3, 4) 89 an4.Start = types.BuildTS(1, 0) 90 an4.Prepare = types.BuildTS(5, 0) 91 an4.End = types.BuildTS(5, 0) 92 an4.Aborted = true 93 94 // ts=1 maxrow=1, holes={} 95 maxrow, visible, holes, err := n.GetVisibleRowLocked(types.BuildTS(1, 0)) 96 assert.NoError(t, err) 97 assert.Equal(t, uint32(1), maxrow) 98 assert.True(t, visible) 99 assert.Equal(t, uint64(0), holes.GetCardinality()) 100 101 // ts=4 maxrow=3, holes={1} 102 maxrow, visible, holes, err = n.GetVisibleRowLocked(types.BuildTS(4, 0)) 103 assert.NoError(t, err) 104 assert.Equal(t, uint32(3), maxrow) 105 assert.True(t, visible) 106 assert.Equal(t, uint64(1), holes.GetCardinality()) 107 assert.True(t, holes.Contains(1)) 108 109 // ts=5 maxrow=3, holes={} 110 maxrow, visible, holes, err = n.GetVisibleRowLocked(types.BuildTS(5, 0)) 111 assert.NoError(t, err) 112 assert.Equal(t, uint32(3), maxrow) 113 assert.True(t, visible) 114 assert.Equal(t, uint64(0), holes.GetCardinality()) 115 116 }