github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/model/transfer_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 model
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/objectio"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestTransferPage(t *testing.T) {
    28  	sid := objectio.NewSegmentid()
    29  	src := common.ID{
    30  		BlockID: *objectio.NewBlockid(sid, 1, 0),
    31  	}
    32  	dest := common.ID{
    33  		BlockID: *objectio.NewBlockid(sid, 2, 0),
    34  	}
    35  
    36  	memo1 := NewTransferHashPage(&src, time.Now(), false)
    37  	assert.Zero(t, memo1.RefCount())
    38  
    39  	for i := 0; i < 10; i++ {
    40  		memo1.Train(uint32(i), *objectio.NewRowid(&dest.BlockID, uint32(i)))
    41  	}
    42  
    43  	pinned := memo1.Pin()
    44  	assert.Equal(t, int64(1), memo1.RefCount())
    45  	pinned.Close()
    46  	assert.Zero(t, memo1.RefCount())
    47  
    48  	ttl := time.Millisecond * 10
    49  	now := time.Now()
    50  	memo2 := NewTransferHashPage(&src, now, false)
    51  	defer memo2.Close()
    52  	assert.Zero(t, memo2.RefCount())
    53  
    54  	for i := 0; i < 10; i++ {
    55  		memo2.Train(uint32(i), *objectio.NewRowid(&dest.BlockID, uint32(i)))
    56  	}
    57  
    58  	assert.False(t, memo2.TTL(now.Add(ttl-time.Duration(1)), ttl))
    59  	assert.True(t, memo2.TTL(now.Add(ttl+time.Duration(1)), ttl))
    60  
    61  	for i := 0; i < 10; i++ {
    62  		rowID, ok := memo2.Transfer(uint32(i))
    63  		assert.True(t, ok)
    64  		blockId, offset := rowID.Decode()
    65  		assert.Equal(t, dest.BlockID, blockId)
    66  		assert.Equal(t, uint32(i), offset)
    67  	}
    68  }
    69  
    70  func TestTransferTable(t *testing.T) {
    71  	ttl := time.Minute
    72  	table := NewTransferTable[*TransferHashPage](ttl)
    73  	defer table.Close()
    74  	sid := objectio.NewSegmentid()
    75  
    76  	id1 := common.ID{BlockID: *objectio.NewBlockid(sid, 1, 0)}
    77  	id2 := common.ID{BlockID: *objectio.NewBlockid(sid, 2, 0)}
    78  
    79  	now := time.Now()
    80  	page1 := NewTransferHashPage(&id1, now, false)
    81  	for i := 0; i < 10; i++ {
    82  		rowID := *objectio.NewRowid(&id2.BlockID, uint32(i))
    83  		page1.Train(uint32(i), rowID)
    84  	}
    85  
    86  	assert.False(t, table.AddPage(page1))
    87  	assert.True(t, table.AddPage(page1))
    88  	assert.Equal(t, int64(1), page1.RefCount())
    89  
    90  	_, err := table.Pin(id2)
    91  	assert.True(t, moerr.IsMoErrCode(err, moerr.OkExpectedEOB))
    92  	pinned, err := table.Pin(id1)
    93  	assert.NoError(t, err)
    94  
    95  	assert.Equal(t, int64(2), pinned.Item().RefCount())
    96  
    97  	table.RunTTL(now.Add(ttl - time.Duration(1)))
    98  	assert.Equal(t, 1, table.Len())
    99  	table.RunTTL(now.Add(ttl + time.Duration(1)))
   100  	assert.Equal(t, 0, table.Len())
   101  
   102  	assert.Equal(t, int64(1), pinned.Item().RefCount())
   103  	pinned.Close()
   104  	assert.Equal(t, int64(0), pinned.Item().RefCount())
   105  }