github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/db/scheduler_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 db
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tables/jobs"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils/config"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestCheckpoint1(t *testing.T) {
    32  	defer testutils.AfterTest(t)()
    33  	testutils.EnsureNoLeak(t)
    34  	opts := config.WithQuickScanAndCKPOpts(nil)
    35  	db := initDB(t, opts)
    36  	defer db.Close()
    37  	schema := catalog.MockSchema(13, 12)
    38  	schema.BlockMaxRows = 1000
    39  	schema.SegmentMaxBlocks = 2
    40  	bat := catalog.MockBatch(schema, int(schema.BlockMaxRows))
    41  	defer bat.Close()
    42  	{
    43  		txn, _ := db.StartTxn(nil)
    44  		database, _ := txn.CreateDatabase("db", "")
    45  		rel, _ := database.CreateRelation(schema)
    46  		err := rel.Append(bat)
    47  		assert.Nil(t, err)
    48  		assert.Nil(t, txn.Commit())
    49  	}
    50  	{
    51  		txn, _ := db.StartTxn(nil)
    52  		database, _ := txn.GetDatabase("db")
    53  		rel, _ := database.GetRelationByName(schema.Name)
    54  		it := rel.MakeBlockIt()
    55  		blk := it.GetBlock()
    56  		err := blk.RangeDelete(3, 3, handle.DT_Normal)
    57  		assert.Nil(t, err)
    58  		assert.Nil(t, txn.Commit())
    59  	}
    60  
    61  	blockCnt := 0
    62  	fn := func() bool {
    63  		blockCnt = 0
    64  		blockFn := func(entry *catalog.BlockEntry) error {
    65  			blockCnt++
    66  			return nil
    67  		}
    68  		processor := new(catalog.LoopProcessor)
    69  		processor.BlockFn = blockFn
    70  		err := db.Opts.Catalog.RecurLoop(processor)
    71  		assert.NoError(t, err)
    72  		return blockCnt == 2+3
    73  	}
    74  	testutils.WaitExpect(1000, fn)
    75  	fn()
    76  	assert.Equal(t, 2+3, blockCnt)
    77  }
    78  
    79  func TestCheckpoint2(t *testing.T) {
    80  	defer testutils.AfterTest(t)()
    81  	testutils.EnsureNoLeak(t)
    82  	opts := new(options.Options)
    83  	opts.CacheCfg = new(options.CacheCfg)
    84  	opts.CacheCfg.IndexCapacity = 1000000
    85  	opts.CacheCfg.TxnCapacity = 1000000
    86  	opts.CacheCfg.InsertCapacity = 400
    87  	// opts.CheckpointCfg = new(options.CheckpointCfg)
    88  	// opts.CheckpointCfg.ScannerInterval = 10
    89  	// opts.CheckpointCfg.ExecutionLevels = 2
    90  	// opts.CheckpointCfg.ExecutionInterval = 1
    91  	tae := initDB(t, opts)
    92  	defer tae.Close()
    93  	schema1 := catalog.MockSchema(4, 2)
    94  	schema1.BlockMaxRows = 10
    95  	schema1.SegmentMaxBlocks = 2
    96  	schema2 := catalog.MockSchema(4, 2)
    97  	schema2.BlockMaxRows = 10
    98  	schema2.SegmentMaxBlocks = 2
    99  	bat := catalog.MockBatch(schema1, int(schema1.BlockMaxRows*2))
   100  	defer bat.Close()
   101  	bats := bat.Split(10)
   102  	var (
   103  		meta1 *catalog.TableEntry
   104  		meta2 *catalog.TableEntry
   105  	)
   106  	{
   107  		txn, _ := tae.StartTxn(nil)
   108  		db, _ := txn.CreateDatabase("db", "")
   109  		rel1, _ := db.CreateRelation(schema1)
   110  		rel2, _ := db.CreateRelation(schema2)
   111  		meta1 = rel1.GetMeta().(*catalog.TableEntry)
   112  		meta2 = rel2.GetMeta().(*catalog.TableEntry)
   113  		t.Log(meta1.String())
   114  		t.Log(meta2.String())
   115  		assert.Nil(t, txn.Commit())
   116  	}
   117  	for i, data := range bats[0:8] {
   118  		var name string
   119  		if i%2 == 0 {
   120  			name = schema1.Name
   121  		} else {
   122  			name = schema2.Name
   123  		}
   124  		appendClosure(t, data, name, tae, nil)()
   125  	}
   126  	var meta *catalog.BlockEntry
   127  	testutils.WaitExpect(1000, func() bool {
   128  		return tae.Wal.GetPenddingCnt() == 9
   129  	})
   130  	assert.Equal(t, uint64(9), tae.Wal.GetPenddingCnt())
   131  	t.Log(tae.Wal.GetPenddingCnt())
   132  	appendClosure(t, bats[8], schema1.Name, tae, nil)()
   133  	// t.Log(tae.MTBufMgr.String())
   134  
   135  	{
   136  		txn, _ := tae.StartTxn(nil)
   137  		db, err := txn.GetDatabase("db")
   138  		assert.Nil(t, err)
   139  		rel, err := db.GetRelationByName(schema1.Name)
   140  		assert.Nil(t, err)
   141  		it := rel.MakeBlockIt()
   142  		blk := it.GetBlock()
   143  		meta = blk.GetMeta().(*catalog.BlockEntry)
   144  		assert.Equal(t, 10, blk.Rows())
   145  		task, err := jobs.NewCompactBlockTask(tasks.WaitableCtx, txn, meta, tae.Scheduler)
   146  		assert.Nil(t, err)
   147  		err = tae.Scheduler.Schedule(task)
   148  		assert.Nil(t, err)
   149  		err = task.WaitDone()
   150  		assert.Nil(t, err)
   151  		assert.Nil(t, txn.Commit())
   152  	}
   153  
   154  	// testutils.WaitExpect(1000, func() bool {
   155  	// 	return tae.Wal.GetPenddingCnt() == 1
   156  	// })
   157  	// t.Log(tae.Wal.GetPenddingCnt())
   158  	// err := meta.GetBlockData().Destroy()
   159  	// assert.Nil(t, err)
   160  	// task, err := tae.Scheduler.ScheduleScopedFn(tasks.WaitableCtx, tasks.CheckpointTask, nil, tae.Catalog.CheckpointClosure(tae.Scheduler.GetCheckpointTS()))
   161  	// assert.Nil(t, err)
   162  	// err = task.WaitDone()
   163  	// assert.Nil(t, err)
   164  	// testutils.WaitExpect(1000, func() bool {
   165  	// 	return tae.Wal.GetPenddingCnt() == 4
   166  	// })
   167  	// t.Log(tae.Wal.GetPenddingCnt())
   168  	// assert.Equal(t, uint64(4), tae.Wal.GetPenddingCnt())
   169  }
   170  
   171  func TestSchedule1(t *testing.T) {
   172  	defer testutils.AfterTest(t)()
   173  	testutils.EnsureNoLeak(t)
   174  	db := initDB(t, nil)
   175  	schema := catalog.MockSchema(13, 12)
   176  	schema.BlockMaxRows = 10
   177  	schema.SegmentMaxBlocks = 2
   178  	bat := catalog.MockBatch(schema, int(schema.BlockMaxRows))
   179  	defer bat.Close()
   180  	{
   181  		txn, _ := db.StartTxn(nil)
   182  		database, _ := txn.CreateDatabase("db", "")
   183  		rel, _ := database.CreateRelation(schema)
   184  		err := rel.Append(bat)
   185  		assert.Nil(t, err)
   186  		assert.Nil(t, txn.Commit())
   187  	}
   188  	compactBlocks(t, 0, db, "db", schema, false)
   189  	t.Log(db.Opts.Catalog.SimplePPString(common.PPL1))
   190  	db.Close()
   191  }