github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/client_status_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_test 12 13 import ( 14 "context" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/kv" 18 "github.com/cockroachdb/cockroach/pkg/roachpb" 19 "github.com/cockroachdb/cockroach/pkg/testutils" 20 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 21 "github.com/cockroachdb/errors" 22 ) 23 24 func TestComputeStatsForKeySpan(t *testing.T) { 25 defer leaktest.AfterTest(t)() 26 mtc := &multiTestContext{} 27 defer mtc.Stop() 28 mtc.Start(t, 3) 29 30 // Create a number of ranges using splits. 31 splitKeys := []string{"a", "c", "e", "g", "i"} 32 for _, k := range splitKeys { 33 key := roachpb.Key(k) 34 repl := mtc.stores[0].LookupReplica(roachpb.RKey(key)) 35 args := adminSplitArgs(key) 36 header := roachpb.Header{ 37 RangeID: repl.RangeID, 38 } 39 if _, err := kv.SendWrappedWith(context.Background(), mtc.stores[0], header, args); err != nil { 40 t.Fatal(err) 41 } 42 } 43 44 // Wait for splits to finish. 45 testutils.SucceedsSoon(t, func() error { 46 repl := mtc.stores[0].LookupReplica(roachpb.RKey("z")) 47 if actualRSpan := repl.Desc().RSpan(); !actualRSpan.Key.Equal(roachpb.RKey("i")) { 48 return errors.Errorf("expected range %s to begin at key 'i'", repl) 49 } 50 return nil 51 }) 52 53 // Create some keys across the ranges. 54 incKeys := []string{"b", "bb", "bbb", "d", "dd", "h"} 55 for _, k := range incKeys { 56 if _, err := mtc.dbs[0].Inc(context.Background(), []byte(k), 5); err != nil { 57 t.Fatal(err) 58 } 59 } 60 61 // Verify stats across different spans. 62 for _, tcase := range []struct { 63 startKey string 64 endKey string 65 expectedRanges int 66 expectedKeys int64 67 }{ 68 {"a", "i", 4, 6}, 69 {"a", "c", 1, 3}, 70 {"b", "e", 2, 5}, 71 {"e", "i", 2, 1}, 72 } { 73 start, end := tcase.startKey, tcase.endKey 74 result, err := mtc.stores[0].ComputeStatsForKeySpan( 75 roachpb.RKey(start), roachpb.RKey(end)) 76 if err != nil { 77 t.Fatal(err) 78 } 79 if a, e := result.ReplicaCount, tcase.expectedRanges; a != e { 80 t.Errorf("Expected %d ranges in span [%s - %s], found %d", e, start, end, a) 81 } 82 if a, e := result.MVCC.LiveCount, tcase.expectedKeys; a != e { 83 t.Errorf("Expected %d keys in span [%s - %s], found %d", e, start, end, a) 84 } 85 } 86 }