github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/test/tables_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 test
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/testutil"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tables"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tables/jobs"
    33  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    34  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils/config"
    35  	"github.com/panjf2000/ants/v2"
    36  	"github.com/stretchr/testify/assert"
    37  )
    38  
    39  func TestTables1(t *testing.T) {
    40  	defer testutils.AfterTest(t)()
    41  	testutils.EnsureNoLeak(t)
    42  	ctx := context.Background()
    43  
    44  	db := testutil.InitTestDB(ctx, ModuleName, t, nil)
    45  	defer db.Close()
    46  	txn, _ := db.StartTxn(nil)
    47  	database, _ := txn.CreateDatabase("db", "", "")
    48  	schema := catalog.MockSchema(1, 0)
    49  	schema.BlockMaxRows = 1000
    50  	schema.ObjectMaxBlocks = 2
    51  	rel, _ := database.CreateRelation(schema)
    52  	tableMeta := rel.GetMeta().(*catalog.TableEntry)
    53  	dataFactory := tables.NewDataFactory(db.Runtime, db.Dir)
    54  	tableFactory := dataFactory.MakeTableFactory()
    55  	table := tableFactory(tableMeta)
    56  	handle := table.GetHandle()
    57  	_, err := handle.GetAppender()
    58  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrAppendableObjectNotFound))
    59  	obj, _ := rel.CreateObject(false)
    60  	id := obj.GetMeta().(*catalog.ObjectEntry).AsCommonID()
    61  	appender := handle.SetAppender(id)
    62  	assert.NotNil(t, appender)
    63  
    64  	blkCnt := 3
    65  	rows := schema.BlockMaxRows * uint32(blkCnt)
    66  	_, _, toAppend, err := appender.PrepareAppend(rows, nil)
    67  	assert.Equal(t, schema.BlockMaxRows, toAppend)
    68  	assert.Nil(t, err)
    69  	t.Log(toAppend)
    70  
    71  	_, _, toAppend, err = appender.PrepareAppend(rows-toAppend, nil)
    72  	assert.Nil(t, err)
    73  	assert.Equal(t, uint32(0), toAppend)
    74  
    75  	_, err = handle.GetAppender()
    76  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrAppendableObjectNotFound))
    77  
    78  	obj, _ = rel.CreateObject(false)
    79  	id = obj.GetMeta().(*catalog.ObjectEntry).AsCommonID()
    80  	appender = handle.SetAppender(id)
    81  
    82  	_, _, toAppend, err = appender.PrepareAppend(rows-toAppend, nil)
    83  	assert.Nil(t, err)
    84  	assert.Equal(t, schema.BlockMaxRows, toAppend)
    85  
    86  	_, err = handle.GetAppender()
    87  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrAppendableObjectNotFound))
    88  
    89  	obj, _ = rel.CreateObject(false)
    90  
    91  	id = obj.GetMeta().(*catalog.ObjectEntry).AsCommonID()
    92  	appender = handle.SetAppender(id)
    93  	_, _, toAppend, err = appender.PrepareAppend(rows-2*toAppend, nil)
    94  	assert.Nil(t, err)
    95  	assert.Equal(t, schema.BlockMaxRows, toAppend)
    96  	t.Log(db.Catalog.SimplePPString(common.PPL1))
    97  	err = txn.Rollback(context.Background())
    98  	assert.Nil(t, err)
    99  	t.Log(db.Catalog.SimplePPString(common.PPL1))
   100  }
   101  
   102  func TestTxn1(t *testing.T) {
   103  	defer testutils.AfterTest(t)()
   104  	testutils.EnsureNoLeak(t)
   105  	ctx := context.Background()
   106  
   107  	db := testutil.InitTestDB(ctx, ModuleName, t, nil)
   108  	defer db.Close()
   109  
   110  	schema := catalog.MockSchema(3, 2)
   111  	schema.BlockMaxRows = 10000
   112  	batchRows := schema.BlockMaxRows * 2 / 5
   113  	cnt := uint32(20)
   114  	bat := catalog.MockBatch(schema, int(batchRows*cnt))
   115  	defer bat.Close()
   116  	bats := bat.Split(20)
   117  	{
   118  		txn, _ := db.StartTxn(nil)
   119  		database, err := txn.CreateDatabase("db", "", "")
   120  		assert.Nil(t, err)
   121  		_, err = database.CreateRelation(schema)
   122  		assert.Nil(t, err)
   123  		err = txn.Commit(context.Background())
   124  		assert.Nil(t, err)
   125  	}
   126  	var wg sync.WaitGroup
   127  	now := time.Now()
   128  	doAppend := func(b *containers.Batch) func() {
   129  		return func() {
   130  			defer wg.Done()
   131  			txn, _ := db.StartTxn(nil)
   132  			database, _ := txn.GetDatabase("db")
   133  			rel, err := database.GetRelationByName(schema.Name)
   134  			assert.Nil(t, err)
   135  			err = rel.Append(context.Background(), b)
   136  			assert.Nil(t, err)
   137  			err = txn.Commit(context.Background())
   138  			assert.Nil(t, err)
   139  		}
   140  	}
   141  	p, err := ants.NewPool(4)
   142  	assert.Nil(t, err)
   143  	defer p.Release()
   144  	for _, toAppend := range bats {
   145  		wg.Add(1)
   146  		err := p.Submit(doAppend(toAppend))
   147  		assert.Nil(t, err)
   148  	}
   149  
   150  	wg.Wait()
   151  
   152  	t.Logf("Append takes: %s", time.Since(now))
   153  	// expectBlkCnt := (uint32(batchRows)*uint32(batchCnt)*uint32(loopCnt)-1)/schema.BlockMaxRows + 1
   154  	expectBlkCnt := (uint32(batchRows)*uint32(cnt)-1)/schema.BlockMaxRows + 1
   155  	expectObjCnt := expectBlkCnt
   156  	// t.Log(expectBlkCnt)
   157  	// t.Log(expectObjCnt)
   158  	{
   159  		txn, _ := db.StartTxn(nil)
   160  		database, _ := txn.GetDatabase("db")
   161  		rel, _ := database.GetRelationByName(schema.Name)
   162  		_, err = rel.CreateObject(false)
   163  		assert.Nil(t, err)
   164  	}
   165  	{
   166  		txn, _ := db.StartTxn(nil)
   167  		database, _ := txn.GetDatabase("db")
   168  		rel, _ := database.GetRelationByName(schema.Name)
   169  		objIt := rel.MakeObjectIt()
   170  		objCnt := uint32(0)
   171  		blkCnt := uint32(0)
   172  		for objIt.Valid() {
   173  			objCnt++
   174  			blkCnt += uint32(objIt.GetObject().BlkCnt())
   175  			objIt.Next()
   176  		}
   177  		assert.Equal(t, expectObjCnt, objCnt)
   178  		assert.Equal(t, expectBlkCnt, blkCnt)
   179  	}
   180  	t.Log(db.Catalog.SimplePPString(common.PPL1))
   181  }
   182  
   183  func TestTxn2(t *testing.T) {
   184  	defer testutils.AfterTest(t)()
   185  	testutils.EnsureNoLeak(t)
   186  	ctx := context.Background()
   187  
   188  	db := testutil.InitTestDB(ctx, ModuleName, t, nil)
   189  	defer db.Close()
   190  
   191  	var wg sync.WaitGroup
   192  	run := func() {
   193  		defer wg.Done()
   194  		txn, _ := db.StartTxn(nil)
   195  		if _, err := txn.CreateDatabase("db", "", ""); err != nil {
   196  			assert.Nil(t, txn.Rollback(context.Background()))
   197  		} else {
   198  			assert.Nil(t, txn.Commit(context.Background()))
   199  		}
   200  		t.Log(txn.String())
   201  	}
   202  	wg.Add(2)
   203  	go run()
   204  	go run()
   205  	wg.Wait()
   206  	t.Log(db.Catalog.SimplePPString(common.PPL1))
   207  }
   208  
   209  func TestTxn4(t *testing.T) {
   210  	defer testutils.AfterTest(t)()
   211  	testutils.EnsureNoLeak(t)
   212  	ctx := context.Background()
   213  
   214  	db := testutil.InitTestDB(ctx, ModuleName, t, nil)
   215  	defer db.Close()
   216  
   217  	schema := catalog.MockSchemaAll(4, 2)
   218  	schema.BlockMaxRows = 40000
   219  	schema.ObjectMaxBlocks = 8
   220  	{
   221  		txn, _ := db.StartTxn(nil)
   222  		database, _ := txn.CreateDatabase("db", "", "")
   223  		rel, _ := database.CreateRelation(schema)
   224  		pk := containers.MakeVector(schema.GetSingleSortKey().Type, common.DefaultAllocator)
   225  		defer pk.Close()
   226  		pk.AppendMany([]any{int32(1), int32(2), int32(1)}, []bool{false, false, false})
   227  		provider := containers.NewMockDataProvider()
   228  		provider.AddColumnProvider(schema.GetSingleSortKeyIdx(), pk)
   229  		bat := containers.MockBatchWithAttrs(schema.Types(), schema.Attrs(), 3, schema.GetSingleSortKeyIdx(), provider)
   230  		defer bat.Close()
   231  		err := rel.Append(context.Background(), bat)
   232  		t.Log(err)
   233  		assert.NotNil(t, err)
   234  		assert.Nil(t, txn.Commit(context.Background()))
   235  	}
   236  }
   237  
   238  func TestTxn5(t *testing.T) {
   239  	defer testutils.AfterTest(t)()
   240  	testutils.EnsureNoLeak(t)
   241  	ctx := context.Background()
   242  
   243  	db := testutil.InitTestDB(ctx, ModuleName, t, nil)
   244  	defer db.Close()
   245  
   246  	schema := catalog.MockSchemaAll(4, 2)
   247  	schema.BlockMaxRows = 20
   248  	schema.ObjectMaxBlocks = 4
   249  	cnt := uint32(10)
   250  	rows := schema.BlockMaxRows / 2 * cnt
   251  	bat := catalog.MockBatch(schema, int(rows))
   252  	defer bat.Close()
   253  	bats := bat.Split(int(cnt))
   254  	{
   255  		txn, _ := db.StartTxn(nil)
   256  		database, _ := txn.CreateDatabase("db", "", "")
   257  		_, err := database.CreateRelation(schema)
   258  		assert.Nil(t, err)
   259  		assert.Nil(t, txn.Commit(context.Background()))
   260  	}
   261  	{
   262  		txn, _ := db.StartTxn(nil)
   263  		database, _ := txn.GetDatabase("db")
   264  		rel, _ := database.GetRelationByName(schema.Name)
   265  		err := rel.Append(context.Background(), bats[0])
   266  		assert.Nil(t, err)
   267  		err = rel.Append(context.Background(), bats[0])
   268  		assert.NotNil(t, err)
   269  		assert.Nil(t, txn.Rollback(context.Background()))
   270  	}
   271  
   272  	{
   273  		txn, _ := db.StartTxn(nil)
   274  		database, _ := txn.GetDatabase("db")
   275  		rel, _ := database.GetRelationByName(schema.Name)
   276  		err := rel.Append(context.Background(), bats[0])
   277  		assert.Nil(t, err)
   278  		assert.Nil(t, txn.Commit(context.Background()))
   279  	}
   280  	{
   281  		txn, _ := db.StartTxn(nil)
   282  		database, _ := txn.GetDatabase("db")
   283  		rel, _ := database.GetRelationByName(schema.Name)
   284  		err := rel.Append(context.Background(), bats[0])
   285  		assert.NotNil(t, err)
   286  		assert.Nil(t, txn.Rollback(context.Background()))
   287  	}
   288  	{
   289  		txn, _ := db.StartTxn(nil)
   290  		database, _ := txn.GetDatabase("db")
   291  		rel, _ := database.GetRelationByName(schema.Name)
   292  		err := rel.Append(context.Background(), bats[1])
   293  		assert.Nil(t, err)
   294  
   295  		txn2, _ := db.StartTxn(nil)
   296  		db2, _ := txn2.GetDatabase("db")
   297  		rel2, _ := db2.GetRelationByName(schema.Name)
   298  		err = rel2.Append(context.Background(), bats[1])
   299  		assert.Nil(t, err)
   300  		err = rel2.Append(context.Background(), bats[2])
   301  		assert.Nil(t, err)
   302  
   303  		assert.Nil(t, txn2.Commit(context.Background()))
   304  		assert.Error(t, txn.Commit(context.Background()))
   305  		t.Log(txn2.String())
   306  		t.Log(txn.String())
   307  	}
   308  	t.Log(db.Catalog.SimplePPString(common.PPL1))
   309  }
   310  
   311  func TestTxn6(t *testing.T) {
   312  	defer testutils.AfterTest(t)()
   313  	testutils.EnsureNoLeak(t)
   314  	ctx := context.Background()
   315  
   316  	db := testutil.InitTestDB(ctx, ModuleName, t, nil)
   317  	defer db.Close()
   318  
   319  	schema := catalog.MockSchemaAll(4, 2)
   320  	schema.BlockMaxRows = 20
   321  	schema.ObjectMaxBlocks = 4
   322  	cnt := uint32(10)
   323  	rows := schema.BlockMaxRows / 2 * cnt
   324  	bat := catalog.MockBatch(schema, int(rows))
   325  	defer bat.Close()
   326  	bats := bat.Split(int(cnt))
   327  	{
   328  		txn, _ := db.StartTxn(nil)
   329  		database, _ := txn.CreateDatabase("db", "", "")
   330  		rel, _ := database.CreateRelation(schema)
   331  		err := rel.Append(context.Background(), bats[0])
   332  		assert.Nil(t, err)
   333  		assert.Nil(t, txn.Commit(context.Background()))
   334  	}
   335  	{
   336  		txn, _ := db.StartTxn(nil)
   337  		database, _ := txn.GetDatabase("db")
   338  		rel, _ := database.GetRelationByName(schema.Name)
   339  		filter := new(handle.Filter)
   340  		filter.Op = handle.FilterEq
   341  		filter.Val = int32(5)
   342  
   343  		err := rel.UpdateByFilter(context.Background(), filter, uint16(3), int64(33), false)
   344  		assert.NoError(t, err)
   345  
   346  		err = rel.UpdateByFilter(context.Background(), filter, uint16(3), int64(44), false)
   347  		assert.NoError(t, err)
   348  		v, _, err := rel.GetValueByFilter(context.Background(), filter, 3)
   349  		assert.NoError(t, err)
   350  		assert.Equal(t, int64(44), v)
   351  
   352  		filter.Val = int32(6)
   353  		err = rel.UpdateByFilter(context.Background(), filter, uint16(3), int64(77), false)
   354  		assert.NoError(t, err)
   355  
   356  		err = rel.DeleteByFilter(context.Background(), filter)
   357  		assert.NoError(t, err)
   358  
   359  		// Double delete in a same txn -- FAIL
   360  		err = rel.DeleteByFilter(context.Background(), filter)
   361  		assert.Error(t, err)
   362  
   363  		{
   364  			txn, _ := db.StartTxn(nil)
   365  			database, _ := txn.GetDatabase("db")
   366  			rel, _ := database.GetRelationByName(schema.Name)
   367  
   368  			filter.Val = int32(5)
   369  			v, _, err := rel.GetValueByFilter(context.Background(), filter, 3)
   370  			assert.NoError(t, err)
   371  			assert.NotEqual(t, int64(44), v)
   372  
   373  			err = rel.UpdateByFilter(context.Background(), filter, uint16(3), int64(55), false)
   374  			assert.Error(t, err)
   375  
   376  			filter.Val = int32(7)
   377  			err = rel.UpdateByFilter(context.Background(), filter, uint16(3), int64(88), false)
   378  			assert.NoError(t, err)
   379  
   380  			// Update row that has uncommitted delete -- FAIL
   381  			filter.Val = int32(6)
   382  			err = rel.UpdateByFilter(context.Background(), filter, uint16(3), int64(55), false)
   383  			assert.Error(t, err)
   384  			_, _, err = rel.GetValueByFilter(context.Background(), filter, 3)
   385  			assert.NoError(t, err)
   386  			err = txn.Rollback(context.Background())
   387  			assert.NoError(t, err)
   388  		}
   389  		filter.Val = int32(7)
   390  		err = rel.UpdateByFilter(context.Background(), filter, uint16(3), int64(99), false)
   391  		assert.NoError(t, err)
   392  
   393  		assert.NoError(t, txn.Commit(context.Background()))
   394  
   395  		{
   396  			txn, _ := db.StartTxn(nil)
   397  			database, _ := txn.GetDatabase("db")
   398  			rel, _ := database.GetRelationByName(schema.Name)
   399  
   400  			filter.Val = int32(5)
   401  			v, _, err := rel.GetValueByFilter(context.Background(), filter, 3)
   402  			assert.NoError(t, err)
   403  			assert.Equal(t, int64(44), v)
   404  
   405  			filter.Val = int32(7)
   406  			v, _, err = rel.GetValueByFilter(context.Background(), filter, 3)
   407  			assert.NoError(t, err)
   408  			assert.Equal(t, int64(99), v)
   409  
   410  			filter.Val = int32(6)
   411  			_, _, err = rel.GetValueByFilter(context.Background(), filter, 3)
   412  			assert.Error(t, err)
   413  
   414  			it := rel.MakeObjectIt()
   415  			for it.Valid() {
   416  				obj := it.GetObject()
   417  				for j := 0; j < obj.BlkCnt(); j++ {
   418  					view, err := obj.GetColumnDataByName(context.Background(), uint16(j), schema.ColDefs[3].Name, common.DefaultAllocator)
   419  					assert.Nil(t, err)
   420  					defer view.Close()
   421  					assert.NotEqual(t, bats[0].Length(), view.Length())
   422  					t.Log(view.DeleteMask.String())
   423  					assert.Equal(t, bats[0].Length()-1, view.ApplyDeletes().Length())
   424  				}
   425  				it.Next()
   426  			}
   427  		}
   428  	}
   429  }
   430  
   431  func TestFlushAblkMerge(t *testing.T) {
   432  	defer testutils.AfterTest(t)()
   433  	testutils.EnsureNoLeak(t)
   434  	ctx := context.Background()
   435  
   436  	opts := new(options.Options)
   437  	db := testutil.InitTestDB(ctx, ModuleName, t, opts)
   438  	defer db.Close()
   439  	schema := catalog.MockSchemaAll(13, 2)
   440  	schema.BlockMaxRows = 5
   441  	schema.ObjectMaxBlocks = 8
   442  	col3Data := []int64{10, 8, 1, 6, 15, 7, 3, 12, 11, 4, 9, 5, 14, 13, 2}
   443  	// col3Data := []int64{2, 9, 11, 13, 15, 1, 4, 7, 10, 14, 3, 5, 6, 8, 12}
   444  	pkData := []int32{2, 9, 11, 13, 15, 1, 4, 7, 10, 14, 3, 5, 6, 8, 12}
   445  	pk := containers.MakeVector(schema.GetSingleSortKey().Type, common.DefaultAllocator)
   446  	defer pk.Close()
   447  	col3 := containers.MakeVector(schema.ColDefs[3].Type, common.DefaultAllocator)
   448  	defer col3.Close()
   449  	mapping := make(map[int32]int64)
   450  	for i, v := range pkData {
   451  		pk.Append(v, false)
   452  		col3.Append(col3Data[i], false)
   453  		mapping[v] = col3Data[i]
   454  	}
   455  
   456  	provider := containers.NewMockDataProvider()
   457  	provider.AddColumnProvider(schema.GetSingleSortKeyIdx(), pk)
   458  	provider.AddColumnProvider(3, col3)
   459  	bat := containers.MockBatchWithAttrs(schema.Types(), schema.Attrs(), int(schema.BlockMaxRows*3), schema.GetSingleSortKeyIdx(), provider)
   460  	defer bat.Close()
   461  	{
   462  		txn, _ := db.StartTxn(nil)
   463  		database, _ := txn.CreateDatabase("db", "", "")
   464  		rel, _ := database.CreateRelation(schema)
   465  		err := rel.Append(context.Background(), bat)
   466  		assert.Nil(t, err)
   467  		assert.Nil(t, txn.Commit(context.Background()))
   468  	}
   469  
   470  	{
   471  		txn, _ := db.StartTxn(nil)
   472  		database, _ := txn.GetDatabase("db")
   473  		rel, _ := database.GetRelationByName(schema.Name)
   474  		blks := make([]*catalog.ObjectEntry, 0)
   475  		it := rel.MakeObjectIt()
   476  		for it.Valid() {
   477  			blk := it.GetObject()
   478  			meta := blk.GetMeta().(*catalog.ObjectEntry)
   479  			blks = append(blks, meta)
   480  			it.Next()
   481  		}
   482  		{
   483  			txn, _ := db.StartTxn(nil)
   484  			database, _ := txn.GetDatabase("db")
   485  			rel, _ := database.GetRelationByName(schema.Name)
   486  			it := rel.MakeObjectIt()
   487  			blk := it.GetObject()
   488  			err := blk.RangeDelete(0, 4, 4, handle.DT_Normal, common.DefaultAllocator)
   489  			assert.Nil(t, err)
   490  			assert.Nil(t, txn.Commit(context.Background()))
   491  		}
   492  		start := time.Now()
   493  		{
   494  			task, err := jobs.NewFlushTableTailTask(nil, txn, blks, db.Runtime, types.MaxTs())
   495  			assert.Nil(t, err)
   496  			err = task.OnExec(context.Background())
   497  			assert.Nil(t, err)
   498  		}
   499  		assert.Nil(t, txn.Commit(context.Background()))
   500  		t.Logf("MergeSort takes: %s", time.Since(start))
   501  		t.Log(db.Catalog.SimplePPString(common.PPL1))
   502  	}
   503  	{
   504  		txn, _ := db.StartTxn(nil)
   505  		database, _ := txn.GetDatabase("db")
   506  		rel, _ := database.GetRelationByName(schema.Name)
   507  		it := rel.MakeObjectIt()
   508  		for it.Valid() {
   509  			blk := it.GetObject()
   510  			for j := 0; j < blk.BlkCnt(); j++ {
   511  				view, _ := blk.GetColumnDataById(context.Background(), uint16(j), 3, common.DefaultAllocator)
   512  				assert.NotNil(t, view)
   513  				defer view.Close()
   514  				if view.DeleteMask != nil {
   515  					t.Log(view.DeleteMask.String())
   516  				}
   517  				pkView, _ := blk.GetColumnDataById(context.Background(), uint16(j), schema.GetSingleSortKeyIdx(), common.DefaultAllocator)
   518  				defer pkView.Close()
   519  				for i := 0; i < pkView.Length(); i++ {
   520  					pkv, _ := pkView.GetValue(i)
   521  					colv, _ := view.GetValue(i)
   522  					assert.Equal(t, mapping[pkv.(int32)], colv)
   523  				}
   524  
   525  			}
   526  			it.Next()
   527  		}
   528  	}
   529  	// testutils.WaitExpect(1000, func() bool {
   530  	// 	return db.Wal.GetPenddingCnt() == 0
   531  	// })
   532  	// assert.Equal(t, uint64(0), db.Wal.GetPenddingCnt())
   533  	t.Logf("Checkpointed: %d", db.Wal.GetCheckpointed())
   534  	t.Logf("PendingCnt: %d", db.Wal.GetPenddingCnt())
   535  }
   536  
   537  func TestMergeBlocks2(t *testing.T) {
   538  	defer testutils.AfterTest(t)()
   539  	testutils.EnsureNoLeak(t)
   540  	ctx := context.Background()
   541  
   542  	opts := config.WithQuickScanAndCKPOpts(nil)
   543  	tae := testutil.InitTestDB(ctx, ModuleName, t, opts)
   544  	schema := catalog.MockSchemaAll(13, 2)
   545  	schema.BlockMaxRows = 5
   546  	schema.ObjectMaxBlocks = 2
   547  	col3Data := []int64{10, 8, 1, 6, 15, 7, 3, 12, 11, 4, 9, 5, 14, 13, 2}
   548  	// col3Data := []int64{2, 9, 11, 13, 15, 1, 4, 7, 10, 14, 3, 5, 6, 8, 12}
   549  	pkData := []int32{2, 9, 11, 13, 15, 1, 4, 7, 10, 14, 3, 5, 6, 8, 12}
   550  
   551  	pk := containers.MakeVector(schema.GetSingleSortKey().Type, common.DefaultAllocator)
   552  	col3 := containers.MakeVector(schema.ColDefs[3].Type, common.DefaultAllocator)
   553  	mapping := make(map[int32]int64)
   554  	for i, v := range pkData {
   555  		pk.Append(v, false)
   556  		col3.Append(col3Data[i], false)
   557  		mapping[v] = col3Data[i]
   558  	}
   559  
   560  	provider := containers.NewMockDataProvider()
   561  	provider.AddColumnProvider(schema.GetSingleSortKeyIdx(), pk)
   562  	provider.AddColumnProvider(3, col3)
   563  	bat := containers.MockBatchWithAttrs(schema.Types(), schema.Attrs(), int(schema.BlockMaxRows*3), schema.GetSingleSortKeyIdx(), provider)
   564  	{
   565  		txn, _ := tae.StartTxn(nil)
   566  		database, _ := txn.CreateDatabase("db", "", "")
   567  		rel, _ := database.CreateRelation(schema)
   568  		err := rel.Append(context.Background(), bat)
   569  		assert.Nil(t, err)
   570  		assert.Nil(t, txn.Commit(context.Background()))
   571  	}
   572  	pk.Close()
   573  	col3.Close()
   574  	bat.Close()
   575  	start := time.Now()
   576  	testutils.WaitExpect(2000, func() bool {
   577  		return tae.Wal.GetPenddingCnt() == 0
   578  	})
   579  	t.Logf("Wait %s", time.Since(start))
   580  	// assert.Equal(t, uint64(0), tae.Wal.GetPenddingCnt())
   581  	t.Logf("Checkpointed: %d", tae.Wal.GetCheckpointed())
   582  	t.Logf("PendingCnt: %d", tae.Wal.GetPenddingCnt())
   583  	tae.Close()
   584  }
   585  
   586  func TestCompaction1(t *testing.T) {
   587  	defer testutils.AfterTest(t)()
   588  	testutils.EnsureNoLeak(t)
   589  	ctx := context.Background()
   590  
   591  	db := testutil.InitTestDB(ctx, ModuleName, t, nil)
   592  	defer db.Close()
   593  
   594  	schema := catalog.MockSchemaAll(4, 2)
   595  	schema.BlockMaxRows = 20
   596  	schema.ObjectMaxBlocks = 4
   597  	cnt := uint32(2)
   598  	rows := schema.BlockMaxRows / 2 * cnt
   599  	bat := catalog.MockBatch(schema, int(rows))
   600  	defer bat.Close()
   601  	bats := bat.Split(int(cnt))
   602  	{
   603  		txn, _ := db.StartTxn(nil)
   604  		database, _ := txn.CreateDatabase("db", "", "")
   605  		rel, _ := database.CreateRelation(schema)
   606  		err := rel.Append(context.Background(), bats[0])
   607  		assert.Nil(t, err)
   608  		assert.Nil(t, txn.Commit(context.Background()))
   609  	}
   610  	{
   611  		txn, _ := db.StartTxn(nil)
   612  		database, _ := txn.GetDatabase("db")
   613  		rel, _ := database.GetRelationByName(schema.Name)
   614  		it := rel.MakeObjectIt()
   615  		for it.Valid() {
   616  			blk := it.GetObject()
   617  			for j := 0; j < blk.BlkCnt(); j++ {
   618  				view, _ := blk.GetColumnDataById(context.Background(), uint16(3), 3, common.DefaultAllocator)
   619  				assert.NotNil(t, view)
   620  				view.Close()
   621  				assert.True(t, blk.GetMeta().(*catalog.ObjectEntry).GetObjectData().IsAppendable())
   622  			}
   623  			it.Next()
   624  		}
   625  	}
   626  	{
   627  		txn, _ := db.StartTxn(nil)
   628  		database, _ := txn.GetDatabase("db")
   629  		rel, _ := database.GetRelationByName(schema.Name)
   630  		err := rel.Append(context.Background(), bats[1])
   631  		assert.Nil(t, err)
   632  		assert.Nil(t, txn.Commit(context.Background()))
   633  	}
   634  	{
   635  		txn, _ := db.StartTxn(nil)
   636  		database, _ := txn.GetDatabase("db")
   637  		rel, _ := database.GetRelationByName(schema.Name)
   638  		it := rel.MakeObjectIt()
   639  		for it.Valid() {
   640  			blk := it.GetObject()
   641  			for j := 0; j < blk.BlkCnt(); j++ {
   642  				view, _ := blk.GetColumnDataById(context.Background(), uint16(0), 3, common.DefaultAllocator)
   643  				assert.NotNil(t, view)
   644  				view.Close()
   645  				assert.False(t, blk.GetMeta().(*catalog.ObjectEntry).GetObjectData().IsAppendable())
   646  			}
   647  			it.Next()
   648  		}
   649  	}
   650  }
   651  
   652  func TestCompaction2(t *testing.T) {
   653  	defer testutils.AfterTest(t)()
   654  	testutils.EnsureNoLeak(t)
   655  	ctx := context.Background()
   656  
   657  	opts := config.WithQuickScanAndCKPOpts(nil)
   658  	db := testutil.InitTestDB(ctx, ModuleName, t, opts)
   659  	defer db.Close()
   660  
   661  	schema := catalog.MockSchemaAll(4, 2)
   662  	schema.BlockMaxRows = 21
   663  	schema.ObjectMaxBlocks = 4
   664  	cnt := uint32(3)
   665  	rows := schema.BlockMaxRows
   666  	bat := catalog.MockBatch(schema, int(rows))
   667  	defer bat.Close()
   668  	bats := bat.Split(int(cnt))
   669  	{
   670  		txn, _ := db.StartTxn(nil)
   671  		database, _ := txn.CreateDatabase("db", "", "")
   672  		rel, _ := database.CreateRelation(schema)
   673  		err := rel.Append(context.Background(), bats[0])
   674  		assert.Nil(t, err)
   675  		assert.Nil(t, txn.Commit(context.Background()))
   676  	}
   677  
   678  	testutils.WaitExpect(5000, func() bool {
   679  		dirty := db.BGCheckpointRunner.GetDirtyCollector().ScanInRangePruned(types.TS{}, types.MaxTs())
   680  		return dirty.GetTree().Compact()
   681  	})
   682  	{
   683  		txn, _ := db.TxnMgr.StartTxn(nil)
   684  		database, _ := txn.GetDatabase("db")
   685  		rel, _ := database.GetRelationByName(schema.Name)
   686  		it := rel.MakeObjectIt()
   687  		for it.Valid() {
   688  			blk := it.GetObject()
   689  			for j := 0; j < blk.BlkCnt(); j++ {
   690  				view, _ := blk.GetColumnDataById(context.Background(), uint16(j), 3, common.DefaultAllocator)
   691  				assert.NotNil(t, view)
   692  				view.Close()
   693  				assert.False(t, blk.GetMeta().(*catalog.ObjectEntry).IsAppendable())
   694  				assert.False(t, blk.GetMeta().(*catalog.ObjectEntry).GetObjectData().IsAppendable())
   695  			}
   696  			it.Next()
   697  		}
   698  	}
   699  	{
   700  		txn, _ := db.TxnMgr.StartTxn(nil)
   701  		database, _ := txn.GetDatabase("db")
   702  		rel, _ := database.GetRelationByName(schema.Name)
   703  		it := rel.MakeObjectIt()
   704  		for it.Valid() {
   705  			blk := it.GetObject()
   706  			for j := 0; j < blk.BlkCnt(); j++ {
   707  				view, _ := blk.GetColumnDataById(context.Background(), uint16(j), 3, common.DefaultAllocator)
   708  				assert.NotNil(t, view)
   709  				view.Close()
   710  				assert.False(t, blk.GetMeta().(*catalog.ObjectEntry).IsAppendable())
   711  				assert.False(t, blk.GetMeta().(*catalog.ObjectEntry).GetObjectData().IsAppendable())
   712  			}
   713  			it.Next()
   714  		}
   715  	}
   716  }
   717  
   718  // TestCompaction3 is a case for testing block refcount,
   719  // which requires modification of the data block to test.
   720  /*func TestCompaction3(t *testing.T) {
   721  	defer testutils.AfterTest(t)()
   722  	testutils.EnsureNoLeak(t)
   723  	opts := config.WithQuickScanAndCKPOpts(nil)
   724  	db := initDB(t, opts)
   725  	defer db.Close()
   726  
   727  	schema := catalog.MockSchemaAll(4, 2)
   728  	schema.BlockMaxRows = 21
   729  	schema.ObjectMaxBlocks = 4
   730      schema.Name = tables.ForTestBlockRefName
   731  	cnt := uint32(3)
   732  	rows := schema.BlockMaxRows / 3 * cnt
   733  	bat := catalog.MockBatch(schema, int(rows))
   734  	defer bat.Close()
   735  	bats := bat.Split(int(cnt))
   736  	{
   737  		txn, _ := db.StartTxn(nil)
   738  		database, _ := txn.CreateDatabase("db")
   739  		rel, _ := database.CreateRelation(schema)
   740  		err := rel.Append(context.Background(), bats[0])
   741  		assert.Nil(t, err)
   742  		assert.Nil(t, txn.Commit(context.Background()))
   743  	}
   744  	go func() {
   745  		txn, _ := db.StartTxn(nil)
   746  		database, _ := txn.GetDatabase("db")
   747  		rel, _ := database.GetRelationByName(schema.Name)
   748  		err := rel.Append(context.Background(), bats[1])
   749  		assert.Nil(t, err)
   750  		assert.Nil(t, txn.Commit(context.Background()))
   751  	}()
   752  	{
   753  		txn, _ := db.StartTxn(nil)
   754  		database, _ := txn.GetDatabase("db")
   755  		rel, _ := database.GetRelationByName(schema.Name)
   756  		it := rel.MakeBlockIt()
   757  		for it.Valid() {
   758  			blk := it.GetBlock()
   759  			view, _ := blk.GetColumnDataById(context.Background(), 3)
   760  			assert.NotNil(t, view)
   761  			view.Close()
   762  			assert.True(t, blk.GetMeta().(*catalog.BlockEntry).IsAppendable())
   763  			it.Next()
   764  		}
   765  	}
   766  	time.Sleep(400 * time.Millisecond)
   767  	{
   768  		txn, _ := db.StartTxn(nil)
   769  		database, _ := txn.GetDatabase("db")
   770  		rel, _ := database.GetRelationByName(schema.Name)
   771  		it := rel.MakeBlockIt()
   772  		for it.Valid() {
   773  			blk := it.GetBlock()
   774  			view, _ := blk.GetColumnDataById(context.Background(), 3)
   775  			assert.NotNil(t, view)
   776  			view.Close()
   777  			assert.False(t, blk.GetMeta().(*catalog.BlockEntry).IsAppendable())
   778  			assert.False(t, blk.GetMeta().(*catalog.BlockEntry).GetObjectData().IsAppendable())
   779  			it.Next()
   780  		}
   781  	}
   782  }*/