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

     1  // Copyright 2016 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 kvserver
    12  
    13  import (
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    18  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    19  )
    20  
    21  func TestReplicaGCShouldQueue(t *testing.T) {
    22  	defer leaktest.AfterTest(t)()
    23  
    24  	ts := func(t time.Duration) hlc.Timestamp {
    25  		return hlc.Timestamp{WallTime: t.Nanoseconds()}
    26  	}
    27  
    28  	base := 2 * (ReplicaGCQueueSuspectTimeout + ReplicaGCQueueInactivityThreshold)
    29  	var (
    30  		z       = ts(0)
    31  		bTS     = ts(base)
    32  		cTS     = ts(base + ReplicaGCQueueSuspectTimeout)
    33  		cTSnext = ts(base + ReplicaGCQueueSuspectTimeout + 1)
    34  		iTSprev = ts(base + ReplicaGCQueueInactivityThreshold - 1)
    35  		iTS     = ts(base + ReplicaGCQueueInactivityThreshold)
    36  	)
    37  
    38  	for i, test := range []struct {
    39  		now, lastCheck, lastActivity hlc.Timestamp
    40  		isCandidate                  bool
    41  
    42  		shouldQ  bool
    43  		priority float64
    44  	}{
    45  		// Test outcomes when range is in candidate state.
    46  
    47  		// All timestamps current: candidacy plays no role.
    48  		{now: z, lastCheck: z, lastActivity: z, isCandidate: true, shouldQ: false, priority: 0},
    49  		// Threshold: no action taken.
    50  		{now: cTS, lastCheck: z, lastActivity: bTS, isCandidate: true, shouldQ: false, priority: 0},
    51  		// Queue with priority.
    52  		{now: cTSnext, lastCheck: z, lastActivity: bTS, isCandidate: true, shouldQ: true, priority: 1},
    53  		// Last processed recently: candidate still gets processed eagerly.
    54  		{now: cTSnext, lastCheck: bTS, lastActivity: z, isCandidate: true, shouldQ: true, priority: 1},
    55  		// Last processed recently: non-candidate stays put.
    56  		{now: cTSnext, lastCheck: bTS, lastActivity: z, isCandidate: false, shouldQ: false, priority: 0},
    57  		// Still no effect until iTS reached.
    58  		{now: iTSprev, lastCheck: bTS, lastActivity: z, isCandidate: false, shouldQ: false, priority: 0},
    59  		{now: iTS, lastCheck: bTS, lastActivity: z, isCandidate: true, shouldQ: true, priority: 1},
    60  		// Verify again that candidacy increases priority.
    61  		{now: iTS, lastCheck: bTS, lastActivity: z, isCandidate: false, shouldQ: true, priority: 0},
    62  	} {
    63  		if sq, pr := replicaGCShouldQueueImpl(
    64  			test.now, test.lastCheck, test.lastActivity, test.isCandidate,
    65  		); sq != test.shouldQ || pr != test.priority {
    66  			t.Errorf("%d: %+v: got (%t,%f)", i, test, sq, pr)
    67  		}
    68  	}
    69  }