github.com/matrixorigin/matrixone@v1.2.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  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestMutationControllerAppend(t *testing.T) {
    30  	defer testutils.AfterTest(t)()
    31  	testutils.EnsureNoLeak(t)
    32  	schema := catalog.MockSchema(1, 0)
    33  	c := catalog.MockCatalog()
    34  	defer c.Close()
    35  	db, _ := c.CreateDBEntry("db", "", "", nil)
    36  	table, _ := db.CreateTableEntry(schema, nil, nil)
    37  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
    38  	mc := NewAppendMVCCHandle(obj)
    39  
    40  	nodeCnt := 10000
    41  	rowsPerNode := uint32(5)
    42  	//ts := uint64(2)
    43  	//ts = 4
    44  
    45  	ts := types.NextGlobalTsForTest()
    46  	ts = ts.Next()
    47  	ts = ts.Next()
    48  	//queries := make([]uint64, 0)
    49  	//queries = append(queries, ts-1)
    50  	queries := make([]types.TS, 0)
    51  	queries = append(queries, ts.Prev())
    52  
    53  	for i := 0; i < nodeCnt; i++ {
    54  		txn := mockTxn()
    55  		txn.CommitTS = ts
    56  		txn.PrepareTS = ts
    57  		node, _ := mc.AddAppendNodeLocked(txn, rowsPerNode*uint32(i), rowsPerNode*(uint32(i)+1))
    58  		err := node.ApplyCommit()
    59  		assert.Nil(t, err)
    60  		//queries = append(queries, ts+1)
    61  		queries = append(queries, ts.Next())
    62  		//ts += 2
    63  		ts = ts.Next()
    64  		ts = ts.Next()
    65  	}
    66  
    67  	st := time.Now()
    68  	for i, qts := range queries {
    69  		row, ok, _, _ := mc.GetVisibleRowLocked(context.TODO(), MockTxnWithStartTS(qts))
    70  		if i == 0 {
    71  			assert.False(t, ok)
    72  		} else {
    73  			assert.True(t, ok)
    74  			assert.Equal(t, uint32(i)*rowsPerNode, row)
    75  		}
    76  	}
    77  	t.Logf("%s -- %d ops", time.Since(st), len(queries))
    78  }
    79  
    80  // AppendNode Start Prepare End Aborted
    81  // a1 1,1,1 false
    82  // a2 1,3,5 false
    83  // a3 1,4,4 false
    84  // a4 1,5,5 true
    85  func TestGetVisibleRow(t *testing.T) {
    86  	defer testutils.AfterTest(t)()
    87  	schema := catalog.MockSchema(1, 0)
    88  	c := catalog.MockCatalog()
    89  	defer c.Close()
    90  	db, _ := c.CreateDBEntry("db", "", "", nil)
    91  	table, _ := db.CreateTableEntry(schema, nil, nil)
    92  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
    93  	n := NewAppendMVCCHandle(obj)
    94  	an1, _ := n.AddAppendNodeLocked(nil, 0, 1)
    95  	an1.Start = types.BuildTS(1, 0)
    96  	an1.Prepare = types.BuildTS(1, 0)
    97  	an1.End = types.BuildTS(1, 0)
    98  	an2, _ := n.AddAppendNodeLocked(nil, 1, 2)
    99  	an2.Start = types.BuildTS(1, 0)
   100  	an2.Prepare = types.BuildTS(3, 0)
   101  	an2.End = types.BuildTS(5, 0)
   102  	an3, _ := n.AddAppendNodeLocked(nil, 2, 3)
   103  	an3.Start = types.BuildTS(1, 0)
   104  	an3.Prepare = types.BuildTS(4, 0)
   105  	an3.End = types.BuildTS(4, 0)
   106  	an4, _ := n.AddAppendNodeLocked(nil, 3, 4)
   107  	an4.Start = types.BuildTS(1, 0)
   108  	an4.Prepare = types.BuildTS(5, 0)
   109  	an4.End = types.BuildTS(5, 0)
   110  	an4.Aborted = true
   111  
   112  	// ts=1 maxrow=1, holes={}
   113  	maxrow, visible, holes, err := n.GetVisibleRowLocked(context.TODO(), MockTxnWithStartTS(types.BuildTS(1, 0)))
   114  	assert.NoError(t, err)
   115  	assert.Equal(t, uint32(1), maxrow)
   116  	assert.True(t, visible)
   117  	assert.Equal(t, 0, holes.GetCardinality())
   118  
   119  	// ts=4 maxrow=3, holes={1}
   120  	maxrow, visible, holes, err = n.GetVisibleRowLocked(context.TODO(), MockTxnWithStartTS(types.BuildTS(4, 0)))
   121  	assert.NoError(t, err)
   122  	assert.Equal(t, uint32(3), maxrow)
   123  	assert.True(t, visible)
   124  	assert.Equal(t, 1, holes.GetCardinality())
   125  	assert.True(t, holes.Contains(1))
   126  
   127  	// ts=5 maxrow=3, holes={}
   128  	maxrow, visible, holes, err = n.GetVisibleRowLocked(context.TODO(), MockTxnWithStartTS(types.BuildTS(5, 0)))
   129  	assert.NoError(t, err)
   130  	assert.Equal(t, uint32(3), maxrow)
   131  	assert.True(t, visible)
   132  	assert.Equal(t, 0, holes.GetCardinality())
   133  
   134  }