github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/tscache/tree_impl_test.go (about)

     1  // Copyright 2017 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 tscache
    12  
    13  import (
    14  	"testing"
    15  	"time"
    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  // TestTreeImplEviction verifies the eviction of timestamp cache entries after
    23  // MinRetentionWindow interval.
    24  func TestTreeImplEviction(t *testing.T) {
    25  	defer leaktest.AfterTest(t)()
    26  	manual := hlc.NewManualClock(123)
    27  	clock := hlc.NewClock(manual.UnixNano, time.Nanosecond)
    28  	tc := newTreeImpl(clock)
    29  	defer tc.clear(clock.Now())
    30  
    31  	tc.maxBytes = 0
    32  
    33  	// Increment time to the low water mark + 1.
    34  	manual.Increment(1)
    35  	aTS := clock.Now()
    36  	tc.Add(roachpb.Key("a"), nil, aTS, noTxnID)
    37  
    38  	// Increment time by the MinRetentionWindow and add another key.
    39  	manual.Increment(MinRetentionWindow.Nanoseconds())
    40  	tc.Add(roachpb.Key("b"), nil, clock.Now(), noTxnID)
    41  
    42  	// Verify looking up key "c" returns the new low water mark ("a"'s timestamp).
    43  	if rTS, rTxnID := tc.GetMax(roachpb.Key("c"), nil); rTS != aTS || rTxnID != noTxnID {
    44  		t.Errorf("expected low water mark %s, got %s; txnID=%s", aTS, rTS, rTxnID)
    45  	}
    46  }
    47  
    48  // TestTreeImplNoEviction verifies that even after the MinRetentionWindow
    49  // interval, if the cache has not hit its size threshold, it will not evict
    50  // entries.
    51  func TestTreeImplNoEviction(t *testing.T) {
    52  	defer leaktest.AfterTest(t)()
    53  	manual := hlc.NewManualClock(123)
    54  	clock := hlc.NewClock(manual.UnixNano, time.Nanosecond)
    55  	tc := newTreeImpl(clock)
    56  	defer tc.clear(clock.Now())
    57  
    58  	// Increment time to the low water mark + 1.
    59  	manual.Increment(1)
    60  	aTS := clock.Now()
    61  	tc.Add(roachpb.Key("a"), nil, aTS, noTxnID)
    62  
    63  	// Increment time by the MinRetentionWindow and add another key.
    64  	manual.Increment(MinRetentionWindow.Nanoseconds())
    65  	tc.Add(roachpb.Key("b"), nil, clock.Now(), noTxnID)
    66  
    67  	// Verify that the cache still has 2 entries in it
    68  	if l, want := tc.len(), 2; l != want {
    69  		t.Errorf("expected %d entries to remain, got %d", want, l)
    70  	}
    71  }