github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/storage/rocksdb_iter_stats_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package storage
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    18  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    19  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    20  )
    21  
    22  func TestIterStats(t *testing.T) {
    23  	defer leaktest.AfterTest(t)()
    24  
    25  	ctx := context.Background()
    26  
    27  	db := setupMVCCInMemRocksDB(t, "test_iter_stats")
    28  	defer db.Close()
    29  
    30  	k := MakeMVCCMetadataKey(roachpb.Key("foo"))
    31  	if err := db.Put(k, []byte("abc")); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	if err := db.Clear(k); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	batch := db.NewBatch()
    40  	defer batch.Close()
    41  
    42  	testCases := []Iterator{
    43  		db.NewIterator(IterOptions{UpperBound: roachpb.KeyMax, WithStats: true}),
    44  		batch.NewIterator(IterOptions{UpperBound: roachpb.KeyMax, WithStats: true}),
    45  	}
    46  
    47  	defer func() {
    48  		for _, iter := range testCases {
    49  			iter.Close()
    50  		}
    51  	}()
    52  
    53  	for _, iter := range testCases {
    54  		t.Run("", func(t *testing.T) {
    55  			// Seeking past the tombstone manually counts it.
    56  			for i := 0; i < 10; i++ {
    57  				iter.SeekGE(NilKey)
    58  				iter.SeekGE(MVCCKeyMax)
    59  				stats := iter.Stats()
    60  				if e, a := i+1, stats.InternalDeleteSkippedCount; a != e {
    61  					t.Errorf("expected internal delete skipped count of %d, not %d", e, a)
    62  				}
    63  			}
    64  			// Scanning a key range containing the tombstone sees it.
    65  			for i := 0; i < 10; i++ {
    66  				if _, err := mvccScanToKvs(
    67  					ctx, iter, roachpb.KeyMin, roachpb.KeyMax, hlc.Timestamp{}, MVCCScanOptions{},
    68  				); err != nil {
    69  					t.Fatal(err)
    70  				}
    71  				stats := iter.Stats()
    72  				if e, a := i+11, stats.InternalDeleteSkippedCount; a != e {
    73  					t.Errorf("expected internal delete skipped count of %d, not %d", e, a)
    74  				}
    75  			}
    76  
    77  			// Getting the key with the tombstone sees it.
    78  			for i := 0; i < 10; i++ {
    79  				if _, _, err := mvccGet(ctx, iter, k.Key, hlc.Timestamp{}, MVCCGetOptions{}); err != nil {
    80  					t.Fatal(err)
    81  				}
    82  				stats := iter.Stats()
    83  				if e, a := i+21, stats.InternalDeleteSkippedCount; a != e {
    84  					t.Errorf("expected internal delete skipped count of %d, not %d", e, a)
    85  				}
    86  			}
    87  			// Getting KeyMax doesn't see it.
    88  			for i := 0; i < 10; i++ {
    89  				if _, _, err := mvccGet(ctx, iter, roachpb.KeyMax, hlc.Timestamp{}, MVCCGetOptions{}); err != nil {
    90  					t.Fatal(err)
    91  				}
    92  				stats := iter.Stats()
    93  				if e, a := 30, stats.InternalDeleteSkippedCount; a != e {
    94  					t.Errorf("expected internal delete skipped count of %d, not %d", e, a)
    95  				}
    96  			}
    97  
    98  		})
    99  	}
   100  }