github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/disttae/logtailreplay/partition_state_test.go (about)

     1  // Copyright 2022 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 logtailreplay
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/objectio"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func BenchmarkPartitionStateConcurrentWriteAndIter(b *testing.B) {
    26  	partition := NewPartition()
    27  	end := make(chan struct{})
    28  	defer func() {
    29  		close(end)
    30  	}()
    31  
    32  	// concurrent writer
    33  	go func() {
    34  		for {
    35  			select {
    36  			case <-end:
    37  				return
    38  			default:
    39  			}
    40  			state, end := partition.MutateState()
    41  			_ = state
    42  			end()
    43  		}
    44  	}()
    45  
    46  	b.ResetTimer()
    47  	b.RunParallel(func(pb *testing.PB) {
    48  		for pb.Next() {
    49  			state := partition.state.Load()
    50  			iter := state.NewRowsIter(types.BuildTS(0, 0), nil, false)
    51  			iter.Close()
    52  		}
    53  	})
    54  
    55  }
    56  
    57  func TestTruncate(t *testing.T) {
    58  	partition := NewPartitionState(true)
    59  	addObject(partition, types.BuildTS(1, 0), types.BuildTS(2, 0))
    60  	addObject(partition, types.BuildTS(1, 0), types.BuildTS(3, 0))
    61  	addObject(partition, types.BuildTS(1, 0), types.TS{})
    62  
    63  	partition.truncate([2]uint64{0, 0}, types.BuildTS(1, 0))
    64  	assert.Equal(t, 5, partition.objectIndexByTS.Len())
    65  
    66  	partition.truncate([2]uint64{0, 0}, types.BuildTS(2, 0))
    67  	assert.Equal(t, 3, partition.objectIndexByTS.Len())
    68  
    69  	partition.truncate([2]uint64{0, 0}, types.BuildTS(3, 0))
    70  	assert.Equal(t, 1, partition.objectIndexByTS.Len())
    71  
    72  	partition.truncate([2]uint64{0, 0}, types.BuildTS(4, 0))
    73  	assert.Equal(t, 1, partition.objectIndexByTS.Len())
    74  }
    75  
    76  func addObject(p *PartitionState, create, delete types.TS) {
    77  	blkID := objectio.NewBlockid(objectio.NewSegmentid(), 0, 0)
    78  	objShortName := objectio.ShortName(blkID)
    79  	objIndex1 := ObjectIndexByTSEntry{
    80  		Time:         create,
    81  		ShortObjName: *objShortName,
    82  		IsDelete:     false,
    83  	}
    84  	p.objectIndexByTS.Set(objIndex1)
    85  	if !delete.IsEmpty() {
    86  		objIndex2 := ObjectIndexByTSEntry{
    87  			Time:         delete,
    88  			ShortObjName: *objShortName,
    89  			IsDelete:     true,
    90  		}
    91  		p.objectIndexByTS.Set(objIndex2)
    92  	}
    93  
    94  }