@@ -23,9 +23,13 @@ import (
2323 "github.com/pkg/errors"
2424
2525 "github.com/cockroachdb/cockroach/pkg/internal/client"
26+ "github.com/cockroachdb/cockroach/pkg/keys"
2627 "github.com/cockroachdb/cockroach/pkg/roachpb"
2728 "github.com/cockroachdb/cockroach/pkg/storage"
29+ "github.com/cockroachdb/cockroach/pkg/storage/batcheval/result"
30+ "github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
2831 "github.com/cockroachdb/cockroach/pkg/testutils"
32+ "github.com/cockroachdb/cockroach/pkg/util/hlc"
2933 "github.com/cockroachdb/cockroach/pkg/util/leaktest"
3034 "github.com/cockroachdb/cockroach/pkg/util/metric"
3135)
@@ -152,6 +156,84 @@ func verifyRocksDBStats(t *testing.T, s *storage.Store) {
152156 }
153157}
154158
159+ // TestStoreResolveMetrics verifies that metrics related to intent resolution
160+ // are tracked properly.
161+ func TestStoreResolveMetrics (t * testing.T ) {
162+ defer leaktest .AfterTest (t )()
163+
164+ // First prevent rot that would result from adding fields without handling
165+ // them everywhere.
166+ {
167+ act := fmt .Sprintf ("%+v" , result.Metrics {})
168+ exp := "{LeaseRequestSuccess:0 LeaseRequestError:0 LeaseTransferSuccess:0 LeaseTransferError:0 ResolveCommit:0 ResolveAbort:0 ResolvePoison:0}"
169+ if act != exp {
170+ t .Errorf ("need to update this test due to added fields: %v" , act )
171+ }
172+ }
173+
174+ mtc := & multiTestContext {}
175+ defer mtc .Stop ()
176+ mtc .Start (t , 1 )
177+
178+ ctx := context .Background ()
179+
180+ span := roachpb.Span {Key : roachpb .Key ("a" ), EndKey : roachpb .Key ("b" )}
181+
182+ txn := roachpb .MakeTransaction ("foo" , span .Key , roachpb .MinUserPriority , enginepb .SERIALIZABLE , hlc.Timestamp {WallTime : 123 }, 999 )
183+
184+ const resolveCommitCount = int64 (200 )
185+ const resolveAbortCount = int64 (800 )
186+ const resolvePoisonCount = int64 (2400 )
187+
188+ var ba roachpb.BatchRequest
189+ {
190+ repl := mtc .stores [0 ].LookupReplica (keys .MustAddr (span .Key ))
191+ var err error
192+ if ba .Replica , err = repl .GetReplicaDescriptor (); err != nil {
193+ t .Fatal (err )
194+ }
195+ ba .RangeID = repl .RangeID
196+ }
197+
198+ add := func (status roachpb.TransactionStatus , poison bool , n int64 ) {
199+ for i := int64 (0 ); i < n ; i ++ {
200+ key := span .Key
201+ endKey := span .EndKey
202+ if i > n / 2 {
203+ req := & roachpb.ResolveIntentRangeRequest {
204+ IntentTxn : txn .TxnMeta , Status : status , Poison : poison ,
205+ }
206+ req .Key , req .EndKey = key , endKey
207+ ba .Add (req )
208+ continue
209+ }
210+ req := & roachpb.ResolveIntentRequest {
211+ IntentTxn : txn .TxnMeta , Status : status , Poison : poison ,
212+ }
213+ req .Key = key
214+ ba .Add (req )
215+ }
216+ }
217+
218+ add (roachpb .COMMITTED , false , resolveCommitCount )
219+ add (roachpb .ABORTED , false , resolveAbortCount )
220+ add (roachpb .ABORTED , true , resolvePoisonCount )
221+
222+ if _ , pErr := mtc .senders [0 ].Send (ctx , ba ); pErr != nil {
223+ t .Fatal (pErr )
224+ }
225+
226+ if exp , act := resolveCommitCount , mtc .stores [0 ].Metrics ().ResolveCommitCount .Count (); act < exp || act > exp + 50 {
227+ t .Errorf ("expected around %d intent commits, saw %d" , exp , act )
228+ }
229+ if exp , act := resolveAbortCount , mtc .stores [0 ].Metrics ().ResolveAbortCount .Count (); act < exp || act > exp + 50 {
230+ t .Errorf ("expected around %d intent aborts, saw %d" , exp , act )
231+ }
232+ if exp , act := resolvePoisonCount , mtc .stores [0 ].Metrics ().ResolvePoisonCount .Count (); act < exp || act > exp + 50 {
233+ t .Errorf ("expected arounc %d abort span poisonings, saw %d" , exp , act )
234+ }
235+ }
236+
155237func TestStoreMetrics (t * testing.T ) {
156238 defer leaktest .AfterTest (t )()
157239
0 commit comments