github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/updates/chain_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  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  const (
    32  	ModuleName = "TAEUPDATES"
    33  )
    34  
    35  func commitTxn(txn *txnbase.Txn) {
    36  	txn.CommitTS = types.NextGlobalTsForTest()
    37  }
    38  
    39  func TestDeleteChain1(t *testing.T) {
    40  	defer testutils.AfterTest(t)()
    41  	testutils.EnsureNoLeak(t)
    42  	schema := catalog.MockSchema(1, 0)
    43  	c := catalog.MockCatalog()
    44  	defer c.Close()
    45  
    46  	db, _ := c.CreateDBEntry("db", "", "", nil)
    47  	table, _ := db.CreateTableEntry(schema, nil, nil)
    48  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
    49  	objHandle := NewObjectMVCCHandle(obj)
    50  
    51  	controller := NewMVCCHandle(objHandle, 0)
    52  	chain := NewDeleteChain(nil, controller)
    53  	txn1 := new(txnbase.Txn)
    54  	txn1.TxnCtx = txnbase.NewTxnCtx(common.NewTxnIDAllocator().Alloc(), types.NextGlobalTsForTest(), types.TS{})
    55  	n1 := chain.AddNodeLocked(txn1, handle.DeleteType(handle.DT_Normal)).(*DeleteNode)
    56  	assert.Equal(t, 1, chain.Depth())
    57  	mockPK := containers.MakeVector(types.New(types.T_uint8, 0, 0), common.DefaultAllocator)
    58  	for i := 0; i < 33; i++ {
    59  		mockPK.Append(uint8(i), false)
    60  	}
    61  
    62  	// 1. Txn1 delete from 1 to 10 -- PASS
    63  	err := chain.PrepareRangeDelete(1, 10, txn1.GetStartTS())
    64  	assert.Nil(t, err)
    65  	n1.RangeDeleteLocked(1, 10, mockPK.Window(1, 10), common.DefaultAllocator)
    66  	assert.Equal(t, uint32(10), n1.GetCardinalityLocked())
    67  	t.Log(n1.mask.String())
    68  
    69  	// 2. Txn1 delete 10 -- FAIL
    70  	err = chain.PrepareRangeDelete(10, 10, txn1.GetStartTS())
    71  	assert.NotNil(t, err)
    72  
    73  	txn2 := mockTxn()
    74  	// 3. Txn2 delete 2 -- FAIL
    75  	err = chain.PrepareRangeDelete(2, 2, txn2.GetStartTS())
    76  	assert.NotNil(t, err)
    77  
    78  	// 4. Txn2 delete from 21 to 30 -- PASS
    79  	err = chain.PrepareRangeDelete(20, 30, txn2.GetStartTS())
    80  	assert.Nil(t, err)
    81  	n2 := chain.AddNodeLocked(txn2, handle.DeleteType(handle.DT_Normal)).(*DeleteNode)
    82  	n2.RangeDeleteLocked(20, 30, mockPK.Window(20, 11), common.DefaultAllocator)
    83  	assert.Equal(t, uint32(11), n2.GetCardinalityLocked())
    84  	t.Log(n2.mask.String())
    85  
    86  	merged := chain.AddMergeNode().(*DeleteNode)
    87  	assert.Nil(t, merged)
    88  	assert.Equal(t, 2, chain.Depth())
    89  
    90  	collected, err := chain.CollectDeletesLocked(txn1, nil)
    91  	assert.NoError(t, err)
    92  	assert.Equal(t, 10, collected.GetCardinality())
    93  	collected, err = chain.CollectDeletesLocked(txn2, nil)
    94  	assert.NoError(t, err)
    95  	assert.Equal(t, 11, collected.GetCardinality())
    96  
    97  	var startTs1 types.TS
    98  	collected, err = chain.CollectDeletesLocked(MockTxnWithStartTS(startTs1), nil)
    99  	assert.NoError(t, err)
   100  	assert.True(t, collected.IsEmpty())
   101  	collected, err = chain.CollectDeletesLocked(MockTxnWithStartTS(types.NextGlobalTsForTest()), nil)
   102  	assert.NoError(t, err)
   103  	assert.True(t, collected.IsEmpty())
   104  
   105  	commitTxn(txn1)
   106  	assert.Nil(t, n1.PrepareCommit())
   107  	assert.Nil(t, n1.ApplyCommit())
   108  	t.Log(chain.StringLocked())
   109  
   110  	var startTs2 types.TS
   111  	collected, err = chain.CollectDeletesLocked(MockTxnWithStartTS(startTs2), nil)
   112  	assert.NoError(t, err)
   113  	assert.True(t, collected.IsEmpty())
   114  	collected, err = chain.CollectDeletesLocked(MockTxnWithStartTS(types.NextGlobalTsForTest()), nil)
   115  	assert.NoError(t, err)
   116  	assert.Equal(t, 10, collected.GetCardinality())
   117  	collected, err = chain.CollectDeletesLocked(txn2, nil)
   118  	assert.NoError(t, err)
   119  	assert.Equal(t, 11, collected.GetCardinality())
   120  
   121  	txn3 := mockTxn()
   122  	err = chain.PrepareRangeDelete(5, 5, txn3.GetStartTS())
   123  	assert.NotNil(t, err)
   124  	err = chain.PrepareRangeDelete(31, 33, txn3.GetStartTS())
   125  	assert.Nil(t, err)
   126  	n3 := chain.AddNodeLocked(txn3, handle.DeleteType(handle.DT_Normal))
   127  	n3.RangeDeleteLocked(31, 33, mockPK.Window(31, 4), common.DefaultAllocator)
   128  
   129  	collected, err = chain.CollectDeletesLocked(txn3, nil)
   130  	assert.NoError(t, err)
   131  	assert.Equal(t, 13, collected.GetCardinality())
   132  	t.Log(chain.StringLocked())
   133  
   134  	merged = chain.AddMergeNode().(*DeleteNode)
   135  	assert.NotNil(t, merged)
   136  	t.Log(chain.StringLocked())
   137  	assert.Equal(t, 4, chain.DepthLocked())
   138  
   139  	cmd, err := merged.MakeCommand(1)
   140  	assert.Nil(t, err)
   141  	assert.NotNil(t, cmd)
   142  
   143  	buf, err := cmd.MarshalBinary()
   144  	assert.Nil(t, err)
   145  
   146  	vcmd, err := txnbase.BuildCommandFrom(buf)
   147  	assert.Nil(t, err)
   148  	cmd2 := vcmd.(*UpdateCmd)
   149  	assert.Equal(t, IOET_WALTxnCommand_DeleteNode, cmd2.GetType())
   150  	assert.Equal(t, IOET_WALTxnCommand_DeleteNode, cmd2.cmdType)
   151  	assert.True(t, cmd2.delete.mask.Equals(merged.mask))
   152  }
   153  
   154  func TestDeleteChain2(t *testing.T) {
   155  	defer testutils.AfterTest(t)()
   156  	testutils.EnsureNoLeak(t)
   157  	schema := catalog.MockSchema(1, 0)
   158  	c := catalog.MockCatalog()
   159  	defer c.Close()
   160  	db, _ := c.CreateDBEntry("db", "", "", nil)
   161  	table, _ := db.CreateTableEntry(schema, nil, nil)
   162  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
   163  	objHandle := NewObjectMVCCHandle(obj)
   164  	controller := NewMVCCHandle(objHandle, 0)
   165  	chain := NewDeleteChain(nil, controller)
   166  	mockPK := containers.MakeVector(types.New(types.T_uint8, 0, 0), common.DefaultAllocator)
   167  	for i := 0; i < 13; i++ {
   168  		mockPK.Append(uint8(i), false)
   169  	}
   170  
   171  	txn1 := mockTxn()
   172  	n1 := chain.AddNodeLocked(txn1, handle.DeleteType(handle.DT_Normal)).(*DeleteNode)
   173  	err := chain.PrepareRangeDelete(1, 4, txn1.GetStartTS())
   174  	assert.Nil(t, err)
   175  	n1.RangeDeleteLocked(1, 4, mockPK.Window(1, 4), common.DefaultAllocator)
   176  	commitTxn(txn1)
   177  	err = n1.PrepareCommit()
   178  	assert.Nil(t, err)
   179  	err = n1.ApplyCommit()
   180  	assert.Nil(t, err)
   181  	t.Log(chain.StringLocked())
   182  
   183  	txn2 := mockTxn()
   184  	n2 := chain.AddNodeLocked(txn2, handle.DeleteType(handle.DT_Normal)).(*DeleteNode)
   185  	err = chain.PrepareRangeDelete(5, 8, txn2.GetStartTS())
   186  	assert.Nil(t, err)
   187  	n2.RangeDeleteLocked(5, 8, mockPK.Window(5, 4), common.DefaultAllocator)
   188  	t.Log(chain.StringLocked())
   189  
   190  	txn3 := mockTxn()
   191  	n3 := chain.AddNodeLocked(txn3, handle.DeleteType(handle.DT_Normal)).(*DeleteNode)
   192  	err = chain.PrepareRangeDelete(9, 12, txn3.GetStartTS())
   193  	assert.Nil(t, err)
   194  	n3.RangeDeleteLocked(9, 12, mockPK.Window(9, 4), common.DefaultAllocator)
   195  	commitTxn(txn3)
   196  	err = n3.PrepareCommit()
   197  	assert.Nil(t, err)
   198  	err = n3.ApplyCommit()
   199  	assert.Nil(t, err)
   200  	t.Log(chain.StringLocked())
   201  
   202  	mask, err := chain.CollectDeletesLocked(MockTxnWithStartTS(types.NextGlobalTsForTest()), nil)
   203  	assert.NoError(t, err)
   204  	assert.Equal(t, 8, mask.GetCardinality())
   205  	mask, err = chain.CollectDeletesLocked(MockTxnWithStartTS(txn3.GetCommitTS()), nil)
   206  	assert.NoError(t, err)
   207  	assert.Equal(t, 8, mask.GetCardinality())
   208  	mask, err = chain.CollectDeletesLocked(MockTxnWithStartTS(txn1.GetCommitTS()), nil)
   209  	assert.NoError(t, err)
   210  	assert.Equal(t, 4, mask.GetCardinality())
   211  	mask, err = chain.CollectDeletesLocked(MockTxnWithStartTS(txn1.GetCommitTS().Prev()), nil)
   212  	assert.NoError(t, err)
   213  	assert.True(t, mask.IsEmpty())
   214  
   215  	var startTs1 types.TS
   216  	mask, err = chain.CollectDeletesInRangeWithLock(startTs1, txn3.GetCommitTS(), nil)
   217  	assert.NoError(t, err)
   218  	t.Log(mask.GetBitmap().String())
   219  	assert.Equal(t, 8, mask.GetCardinality())
   220  
   221  	var startTs2 types.TS
   222  	commitTS := txn3.GetCommitTS()
   223  	mask, err = chain.CollectDeletesInRangeWithLock(startTs2, commitTS.Next(), nil)
   224  	assert.NoError(t, err)
   225  	t.Log(mask.GetBitmap().String())
   226  	assert.Equal(t, 8, mask.GetCardinality())
   227  
   228  	commitTS = txn3.GetCommitTS()
   229  	mask, err = chain.CollectDeletesInRangeWithLock(txn1.GetCommitTS(), commitTS.Next(), nil)
   230  	assert.NoError(t, err)
   231  	t.Log(mask.GetBitmap().String())
   232  	assert.Equal(t, 4, mask.GetCardinality())
   233  }