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