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