-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
client_test.go
191 lines (174 loc) · 5.33 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2014 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
/* Package storage_test provides a means of testing store
functionality which depends on a fully-functional KV client. This
cannot be done within the storage package because of circular
dependencies.
By convention, tests in package storage_test have names of the form
client_*.go.
*/
package kvserver_test
import (
"context"
"fmt"
"testing"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/rditer"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/stateloader"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
"github.com/kr/pretty"
)
// getArgs returns a GetRequest and GetResponse pair addressed to
// the default replica for the specified key.
func getArgs(key roachpb.Key) *roachpb.GetRequest {
return &roachpb.GetRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
}
}
// putArgs returns a PutRequest and PutResponse pair addressed to
// the default replica for the specified key / value.
func putArgs(key roachpb.Key, value []byte) *roachpb.PutRequest {
return &roachpb.PutRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
Value: roachpb.MakeValueFromBytes(value),
}
}
// cPutArgs returns a ConditionPutRequest to the default replica
// for the specified key and value, with the given expected value.
func cPutArgs(key roachpb.Key, value, expValue []byte) *roachpb.ConditionalPutRequest {
var expBytes []byte
if expValue != nil {
var e roachpb.Value
e.SetBytes(expValue)
expBytes = e.TagAndDataBytes()
}
return &roachpb.ConditionalPutRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
Value: roachpb.MakeValueFromBytes(value),
ExpBytes: expBytes,
}
}
// incrementArgs returns an IncrementRequest addressed to the default replica
// for the specified key.
func incrementArgs(key roachpb.Key, inc int64) *roachpb.IncrementRequest {
return &roachpb.IncrementRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
Increment: inc,
}
}
func truncateLogArgs(index uint64, rangeID roachpb.RangeID) *roachpb.TruncateLogRequest {
return &roachpb.TruncateLogRequest{
Index: index,
RangeID: rangeID,
}
}
func heartbeatArgs(
txn *roachpb.Transaction, now hlc.Timestamp,
) (*roachpb.HeartbeatTxnRequest, roachpb.Header) {
return &roachpb.HeartbeatTxnRequest{
RequestHeader: roachpb.RequestHeader{
Key: txn.Key,
},
Now: now,
}, roachpb.Header{Txn: txn}
}
func pushTxnArgs(
pusher, pushee *roachpb.Transaction, pushType roachpb.PushTxnType,
) *roachpb.PushTxnRequest {
return &roachpb.PushTxnRequest{
RequestHeader: roachpb.RequestHeader{
Key: pushee.Key,
},
PushTo: pusher.WriteTimestamp.Next(),
PusherTxn: *pusher,
PusheeTxn: pushee.TxnMeta,
PushType: pushType,
}
}
func migrateArgs(start, end roachpb.Key, version roachpb.Version) *roachpb.MigrateRequest {
return &roachpb.MigrateRequest{
RequestHeader: roachpb.RequestHeader{
Key: start,
EndKey: end,
},
Version: version,
}
}
func adminTransferLeaseArgs(key roachpb.Key, target roachpb.StoreID) roachpb.Request {
return &roachpb.AdminTransferLeaseRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
Target: target,
}
}
func verifyRangeStats(
reader storage.Reader, rangeID roachpb.RangeID, expMS enginepb.MVCCStats,
) error {
ms, err := stateloader.Make(rangeID).LoadMVCCStats(context.Background(), reader)
if err != nil {
return err
}
// When used with a real wall clock these will not be the same, since it
// takes time to load stats.
expMS.AgeTo(ms.LastUpdateNanos)
// Clear system counts as these are expected to vary.
ms.SysBytes, ms.SysCount, ms.AbortSpanBytes = 0, 0, 0
if ms != expMS {
return errors.Errorf("expected and actual stats differ:\n%s", pretty.Diff(expMS, ms))
}
return nil
}
func verifyRecomputedStats(
reader storage.Reader, d *roachpb.RangeDescriptor, expMS enginepb.MVCCStats, nowNanos int64,
) error {
ms, err := rditer.ComputeStatsForRange(d, reader, nowNanos)
if err != nil {
return err
}
// When used with a real wall clock these will not be the same, since it
// takes time to load stats.
expMS.AgeTo(ms.LastUpdateNanos)
if expMS != ms {
return fmt.Errorf("expected range's stats to agree with recomputation: got\n%+v\nrecomputed\n%+v", expMS, ms)
}
return nil
}
func waitForTombstone(
t *testing.T, reader storage.Reader, rangeID roachpb.RangeID,
) (tombstone roachpb.RangeTombstone) {
testutils.SucceedsSoon(t, func() error {
tombstoneKey := keys.RangeTombstoneKey(rangeID)
ok, err := storage.MVCCGetProto(
context.Background(), reader, tombstoneKey, hlc.Timestamp{}, &tombstone, storage.MVCCGetOptions{},
)
if err != nil {
t.Fatalf("failed to read tombstone: %v", err)
}
if !ok {
return fmt.Errorf("tombstone not found for range %d", rangeID)
}
return nil
})
return tombstone
}