github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/processor/sourcemanager/sorter/pebble/db_test.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package pebble
    15  
    16  import (
    17  	"path/filepath"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/cockroachdb/pebble"
    22  	"github.com/pingcap/tiflow/cdc/model"
    23  	"github.com/pingcap/tiflow/cdc/processor/sourcemanager/sorter"
    24  	"github.com/pingcap/tiflow/cdc/processor/sourcemanager/sorter/pebble/encoding"
    25  	"github.com/pingcap/tiflow/pkg/config"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestIteratorWithTableFilter(t *testing.T) {
    30  	dbPath := filepath.Join(t.TempDir(), t.Name())
    31  	db, err := OpenPebble(
    32  		1, dbPath, &config.DBConfig{Count: 1},
    33  		nil,
    34  		nil,
    35  		// Disable auto compactions to make the case more stable.
    36  		func(opts *pebble.Options) { opts.DisableAutomaticCompactions = true },
    37  	)
    38  	require.Nil(t, err)
    39  	defer func() { _ = db.Close() }()
    40  
    41  	writeOpts := &pebble.WriteOptions{Sync: false}
    42  
    43  	// Put 7 table keys with CRTS=1, and then flush it to L0. The flush is required for generating table properties.
    44  	for tableID := 1; tableID <= 7; tableID++ {
    45  		key := encoding.EncodeTsKey(1, uint64(tableID), 1, 0)
    46  		b := db.NewBatch()
    47  		b.Set(key, []byte{'x'}, writeOpts)
    48  		if err := b.Commit(writeOpts); err != nil {
    49  			t.Errorf("Put failed: %v", err)
    50  		}
    51  	}
    52  	if err = db.Flush(); err != nil {
    53  		t.Errorf("Flush failed: %v", err)
    54  	}
    55  
    56  	// Put 9 table keys with CRTS=3, and then flush it to L0.
    57  	for tableID := 1; tableID <= 9; tableID++ {
    58  		key := encoding.EncodeTsKey(1, uint64(tableID), 3, 0)
    59  		b := db.NewBatch()
    60  		b.Set(key, []byte{'x'}, writeOpts)
    61  		if err := b.Commit(writeOpts); err != nil {
    62  			t.Errorf("Put failed: %v", err)
    63  		}
    64  	}
    65  	if err = db.Flush(); err != nil {
    66  		t.Errorf("Flush failed: %v", err)
    67  	}
    68  
    69  	// Sleep a while. Automatic compactions shouldn't be triggered.
    70  	time.Sleep(time.Second)
    71  
    72  	// There should be no any compactions but 2 tables at L0.
    73  	stats := db.Metrics()
    74  	require.Equal(t, int64(0), stats.Compact.Count)
    75  	require.Equal(t, int64(2), stats.Levels[0].NumFiles)
    76  	// 7 is a pebble internal constant.
    77  	// See: https://github.com/cockroachdb/pebble/blob/
    78  	// 71d17c2a007bfad5111a229ba325d30251b88a41/internal/manifest/version.go#L579
    79  	for level := 1; level < 7; level++ {
    80  		require.Equal(t, int64(0), stats.Levels[level].NumFiles)
    81  	}
    82  
    83  	for _, x := range []struct {
    84  		lowerTs, upperTs uint64
    85  		expectedCount    int
    86  	}{
    87  		{lowerTs: 0, upperTs: 1, expectedCount: 7},
    88  		{lowerTs: 1, upperTs: 2, expectedCount: 7},
    89  		{lowerTs: 2, upperTs: 3, expectedCount: 9},
    90  		{lowerTs: 3, upperTs: 4, expectedCount: 9},
    91  		{lowerTs: 0, upperTs: 10, expectedCount: 16},
    92  		{lowerTs: 10, upperTs: 20, expectedCount: 0},
    93  	} {
    94  		count := 0
    95  		for tableID := 0; tableID <= 9; tableID++ {
    96  			iter := iterTable(db, 1, model.TableID(tableID),
    97  				sorter.Position{CommitTs: x.lowerTs},
    98  				sorter.Position{CommitTs: x.upperTs}.Next())
    99  			valid := iter.Valid()
   100  			for valid {
   101  				count += 1
   102  				valid = iter.Next()
   103  			}
   104  			require.Nil(t, iter.Close())
   105  		}
   106  		require.Equal(t, x.expectedCount, count)
   107  	}
   108  }