-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.go
923 lines (806 loc) · 25.3 KB
/
debug.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
package cli
import (
"bytes"
"fmt"
"os"
"sort"
"strconv"
"strings"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
"github.com/cockroachdb/cockroach/pkg/storage/storagebase"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/coreos/etcd/raft/raftpb"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/kr/pretty"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var debugKeysCmd = &cobra.Command{
Use: "keys [directory]",
Short: "dump all the keys in a store",
Long: `
Pretty-prints all keys in a store.
`,
RunE: MaybeDecorateGRPCError(runDebugKeys),
}
func parseRangeID(arg string) (roachpb.RangeID, error) {
rangeIDInt, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
return 0, err
}
if rangeIDInt < 1 {
return 0, fmt.Errorf("illegal RangeID: %d", rangeIDInt)
}
return roachpb.RangeID(rangeIDInt), nil
}
func openStore(cmd *cobra.Command, dir string, stopper *stop.Stopper) (*engine.RocksDB, error) {
cache := engine.NewRocksDBCache(server.DefaultCacheSize)
defer cache.Release()
maxOpenFiles, err := server.SetOpenFileLimitForOneStore()
if err != nil {
return nil, err
}
db, err := engine.NewRocksDB(
engine.RocksDBConfig{
Settings: serverCfg.Settings,
Dir: dir,
MaxOpenFiles: maxOpenFiles,
},
cache,
)
if err != nil {
return nil, err
}
stopper.AddCloser(db)
return db, nil
}
func printKey(kv engine.MVCCKeyValue) (bool, error) {
fmt.Printf("%s", kv.Key)
if debugCtx.sizes {
fmt.Printf(" %d %d", len(kv.Key.Key), len(kv.Value))
}
fmt.Printf("\n")
return false, nil
}
func printKeyValue(kv engine.MVCCKeyValue) (bool, error) {
if kv.Key.Timestamp != (hlc.Timestamp{}) {
fmt.Printf("%s %s: ", kv.Key.Timestamp, kv.Key.Key)
} else {
fmt.Printf("%s: ", kv.Key.Key)
}
if debugCtx.sizes {
fmt.Printf("%d %d: ", len(kv.Key.Key), len(kv.Value))
}
decoders := []func(kv engine.MVCCKeyValue) (string, error){
tryRaftLogEntry,
tryRangeDescriptor,
tryMeta,
tryTxn,
tryRangeIDKey,
}
for _, decoder := range decoders {
out, err := decoder(kv)
if err != nil {
continue
}
fmt.Println(out)
return false, nil
}
// No better idea, just print raw bytes and hope that folks use `less -S`.
fmt.Printf("%q\n\n", kv.Value)
return false, nil
}
func runDebugKeys(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
if len(args) != 1 {
return errors.New("one argument required: dir")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
printer := printKey
if debugCtx.values {
printer = printKeyValue
}
return db.Iterate(debugCtx.startKey, debugCtx.endKey, printer)
}
var debugRangeDataCmd = &cobra.Command{
Use: "range-data [directory] range-id",
Short: "dump all the data in a range",
Long: `
Pretty-prints all keys and values in a range. By default, includes unreplicated
state like the raft HardState. With --replicated, only includes data covered by
the consistency checker.
`,
RunE: MaybeDecorateGRPCError(runDebugRangeData),
}
func runDebugRangeData(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
if len(args) != 2 {
return errors.New("two arguments required: dir range_id")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
rangeID, err := parseRangeID(args[1])
if err != nil {
return err
}
desc, err := loadRangeDescriptor(db, rangeID)
if err != nil {
return err
}
iter := storage.NewReplicaDataIterator(&desc, db, debugCtx.replicated)
for ; ; iter.Next() {
if ok, err := iter.Valid(); err != nil {
return err
} else if !ok {
break
}
if _, err := printKeyValue(engine.MVCCKeyValue{
Key: iter.Key(),
Value: iter.Value(),
}); err != nil {
return err
}
}
return nil
}
var debugRangeDescriptorsCmd = &cobra.Command{
Use: "range-descriptors [directory]",
Short: "print all range descriptors in a store",
Long: `
Prints all range descriptors in a store with a history of changes.
`,
RunE: MaybeDecorateGRPCError(runDebugRangeDescriptors),
}
func descStr(desc roachpb.RangeDescriptor) string {
return fmt.Sprintf("[%s, %s)\n\tRaw:%s\n",
desc.StartKey, desc.EndKey, &desc)
}
func tryMeta(kv engine.MVCCKeyValue) (string, error) {
if !bytes.HasPrefix(kv.Key.Key, keys.Meta1Prefix) && !bytes.HasPrefix(kv.Key.Key, keys.Meta2Prefix) {
return "", errors.New("not a meta key")
}
value := roachpb.Value{
Timestamp: kv.Key.Timestamp,
RawBytes: kv.Value,
}
var desc roachpb.RangeDescriptor
if err := value.GetProto(&desc); err != nil {
return "", err
}
return descStr(desc), nil
}
func maybeUnmarshalInline(v []byte, dest proto.Message) error {
var meta enginepb.MVCCMetadata
if err := meta.Unmarshal(v); err != nil {
return err
}
value := roachpb.Value{
RawBytes: meta.RawBytes,
}
return value.GetProto(dest)
}
func tryTxn(kv engine.MVCCKeyValue) (string, error) {
var txn roachpb.Transaction
if err := maybeUnmarshalInline(kv.Value, &txn); err != nil {
return "", err
}
return txn.String() + "\n", nil
}
func tryRangeIDKey(kv engine.MVCCKeyValue) (string, error) {
if kv.Key.Timestamp != (hlc.Timestamp{}) {
return "", fmt.Errorf("range ID keys shouldn't have timestamps: %s", kv.Key)
}
_, _, suffix, _, err := keys.DecodeRangeIDKey(kv.Key.Key)
if err != nil {
return "", err
}
// All range ID keys are stored inline on the metadata.
var meta enginepb.MVCCMetadata
if err := meta.Unmarshal(kv.Value); err != nil {
return "", err
}
value := roachpb.Value{RawBytes: meta.RawBytes}
// Values encoded as protobufs set msg and continue outside the
// switch. Other types are handled inside the switch and return.
var msg proto.Message
switch {
case bytes.Equal(suffix, keys.LocalLeaseAppliedIndexSuffix):
fallthrough
case bytes.Equal(suffix, keys.LocalRaftAppliedIndexSuffix):
i, err := value.GetInt()
if err != nil {
return "", err
}
return strconv.FormatInt(i, 10), nil
case bytes.Equal(suffix, keys.LocalRangeFrozenStatusSuffix):
b, err := value.GetBool()
if err != nil {
return "", err
}
return strconv.FormatBool(b), nil
case bytes.Equal(suffix, keys.LocalAbortCacheSuffix):
msg = &roachpb.AbortCacheEntry{}
case bytes.Equal(suffix, keys.LocalRangeLastGCSuffix):
msg = &hlc.Timestamp{}
case bytes.Equal(suffix, keys.LocalRaftTombstoneSuffix):
msg = &roachpb.RaftTombstone{}
case bytes.Equal(suffix, keys.LocalRaftTruncatedStateSuffix):
msg = &roachpb.RaftTruncatedState{}
case bytes.Equal(suffix, keys.LocalRangeLeaseSuffix):
msg = &roachpb.Lease{}
case bytes.Equal(suffix, keys.LocalRangeStatsSuffix):
msg = &enginepb.MVCCStats{}
case bytes.Equal(suffix, keys.LocalRaftHardStateSuffix):
msg = &raftpb.HardState{}
case bytes.Equal(suffix, keys.LocalRaftLastIndexSuffix):
i, err := value.GetInt()
if err != nil {
return "", err
}
return strconv.FormatInt(i, 10), nil
case bytes.Equal(suffix, keys.LocalRangeLastVerificationTimestampSuffixDeprecated):
msg = &hlc.Timestamp{}
case bytes.Equal(suffix, keys.LocalRangeLastReplicaGCTimestampSuffix):
msg = &hlc.Timestamp{}
default:
return "", fmt.Errorf("unknown raft id key %s", suffix)
}
if err := value.GetProto(msg); err != nil {
return "", err
}
return msg.String(), nil
}
func checkRangeDescriptorKey(key engine.MVCCKey) error {
_, suffix, _, err := keys.DecodeRangeKey(key.Key)
if err != nil {
return err
}
if !bytes.Equal(suffix, keys.LocalRangeDescriptorSuffix) {
return fmt.Errorf("wrong suffix: %s", suffix)
}
return nil
}
func tryRangeDescriptor(kv engine.MVCCKeyValue) (string, error) {
if err := checkRangeDescriptorKey(kv.Key); err != nil {
return "", err
}
var desc roachpb.RangeDescriptor
if err := getProtoValue(kv.Value, &desc); err != nil {
return "", err
}
return descStr(desc), nil
}
func printRangeDescriptor(kv engine.MVCCKeyValue) (bool, error) {
if out, err := tryRangeDescriptor(kv); err == nil {
fmt.Printf("%s %q: %s\n", kv.Key.Timestamp, kv.Key.Key, out)
}
return false, nil
}
func getProtoValue(data []byte, msg proto.Message) error {
value := roachpb.Value{
RawBytes: data,
}
return value.GetProto(msg)
}
func loadRangeDescriptor(
db engine.Engine, rangeID roachpb.RangeID,
) (roachpb.RangeDescriptor, error) {
var desc roachpb.RangeDescriptor
handleKV := func(kv engine.MVCCKeyValue) (bool, error) {
if kv.Key.Timestamp == (hlc.Timestamp{}) {
// We only want values, not MVCCMetadata.
return false, nil
}
if err := checkRangeDescriptorKey(kv.Key); err != nil {
// Range descriptor keys are interleaved with others, so if it
// doesn't parse as a range descriptor just skip it.
return false, nil
}
if err := getProtoValue(kv.Value, &desc); err != nil {
return false, err
}
return desc.RangeID == rangeID, nil
}
// Range descriptors are stored by key, so we have to scan over the
// range-local data to find the one for this RangeID.
start := engine.MakeMVCCMetadataKey(keys.LocalRangePrefix)
end := engine.MakeMVCCMetadataKey(keys.LocalRangeMax)
if err := db.Iterate(start, end, handleKV); err != nil {
return roachpb.RangeDescriptor{}, err
}
if desc.RangeID == rangeID {
return desc, nil
}
return roachpb.RangeDescriptor{}, fmt.Errorf("range descriptor %d not found", rangeID)
}
func runDebugRangeDescriptors(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
if len(args) != 1 {
return errors.New("one argument required: dir")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
start := engine.MakeMVCCMetadataKey(keys.LocalRangePrefix)
end := engine.MakeMVCCMetadataKey(keys.LocalRangeMax)
return db.Iterate(start, end, printRangeDescriptor)
}
var debugRaftLogCmd = &cobra.Command{
Use: "raft-log [directory] [range id]",
Short: "print the raft log for a range",
Long: `
Prints all log entries in a store for the given range.
`,
RunE: MaybeDecorateGRPCError(runDebugRaftLog),
}
func tryRaftLogEntry(kv engine.MVCCKeyValue) (string, error) {
var ent raftpb.Entry
if err := maybeUnmarshalInline(kv.Value, &ent); err != nil {
return "", err
}
if ent.Type == raftpb.EntryNormal {
if len(ent.Data) > 0 {
_, cmdData := storage.DecodeRaftCommand(ent.Data)
var cmd storagebase.RaftCommand
if err := cmd.Unmarshal(cmdData); err != nil {
return "", err
}
ent.Data = nil
return fmt.Sprintf("%s by %s\n%s\n", &ent, cmd.ProposerLease, &cmd), nil
}
return fmt.Sprintf("%s: EMPTY\n", &ent), nil
} else if ent.Type == raftpb.EntryConfChange {
var cc raftpb.ConfChange
if err := cc.Unmarshal(ent.Data); err != nil {
return "", err
}
var ctx storage.ConfChangeContext
if err := ctx.Unmarshal(cc.Context); err != nil {
return "", err
}
var cmd storagebase.ReplicatedEvalResult
if err := cmd.Unmarshal(ctx.Payload); err != nil {
return "", err
}
ent.Data = nil
return fmt.Sprintf("%s\n%s\n", &ent, &cmd), nil
}
return "", fmt.Errorf("unknown log entry type: %s", &ent)
}
func printRaftLogEntry(kv engine.MVCCKeyValue) (bool, error) {
if out, err := tryRaftLogEntry(kv); err != nil {
fmt.Printf("%q: %v\n\n", kv.Key.Key, err)
} else {
fmt.Printf("%q: %s\n", kv.Key.Key, out)
}
return false, nil
}
func runDebugRaftLog(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
if len(args) != 2 {
return errors.New("two arguments required: dir range_id")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
rangeID, err := parseRangeID(args[1])
if err != nil {
return err
}
start := engine.MakeMVCCMetadataKey(keys.RaftLogPrefix(rangeID))
end := engine.MakeMVCCMetadataKey(keys.RaftLogPrefix(rangeID).PrefixEnd())
return db.Iterate(start, end, printRaftLogEntry)
}
var debugGCCmd = &cobra.Command{
Use: "estimate-gc [directory] [range id]",
Short: "find out what a GC run would do",
Long: `
Sets up (but does not run) a GC collection cycle, giving insight into how much
work would be done (assuming all intent resolution and pushes succeed).
Without a RangeID specified on the command line, runs the analysis for all
ranges individually.
Uses a hard-coded GC policy with a 24 hour TTL for old versions.
`,
RunE: MaybeDecorateGRPCError(runDebugGCCmd),
}
func runDebugGCCmd(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
var rangeID roachpb.RangeID
switch len(args) {
}
switch len(args) {
case 2:
var err error
if rangeID, err = parseRangeID(args[1]); err != nil {
return err
}
case 1:
default:
return errors.New("arguments: dir [range_id]")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
start := keys.RangeDescriptorKey(roachpb.RKeyMin)
end := keys.RangeDescriptorKey(roachpb.RKeyMax)
var descs []roachpb.RangeDescriptor
if _, err := engine.MVCCIterate(context.Background(), db, start, end, hlc.MaxTimestamp,
false /* !consistent */, nil, /* txn */
false /* !reverse */, func(kv roachpb.KeyValue) (bool, error) {
var desc roachpb.RangeDescriptor
_, suffix, _, err := keys.DecodeRangeKey(kv.Key)
if err != nil {
return false, err
}
if !bytes.Equal(suffix, keys.LocalRangeDescriptorSuffix) {
return false, nil
}
if err := kv.Value.GetProto(&desc); err != nil {
return false, err
}
if desc.RangeID == rangeID || rangeID == 0 {
descs = append(descs, desc)
}
return desc.RangeID == rangeID, nil
}); err != nil {
return err
}
if len(descs) == 0 {
return fmt.Errorf("no range matching the criteria found")
}
for _, desc := range descs {
snap := db.NewSnapshot()
defer snap.Close()
_, info, err := storage.RunGC(context.Background(), &desc, snap, hlc.Timestamp{WallTime: timeutil.Now().UnixNano()},
config.GCPolicy{TTLSeconds: 24 * 60 * 60 /* 1 day */}, func(_ hlc.Timestamp, _ *roachpb.Transaction, _ roachpb.PushTxnType) {
}, func(_ []roachpb.Intent, _ storage.ResolveOptions) error { return nil })
if err != nil {
return err
}
fmt.Printf("RangeID: %d [%s, %s):\n", desc.RangeID, desc.StartKey, desc.EndKey)
_, _ = pretty.Println(info)
}
return nil
}
var debugCheckStoreCmd = &cobra.Command{
Use: "check-store [directory]",
Short: "consistency check for a single store",
Long: `
Perform local consistency checks of a single store.
Capable of detecting the following errors:
* Raft logs that are inconsistent with their metadata
`,
RunE: MaybeDecorateGRPCError(runDebugCheckStoreCmd),
}
type replicaCheckInfo struct {
truncatedIndex uint64
appliedIndex uint64
firstIndex uint64
lastIndex uint64
}
func runDebugCheckStoreCmd(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
if len(args) != 1 {
return errors.New("one required argument: dir")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
// Iterate over the entire range-id-local space.
start := roachpb.Key(keys.LocalRangeIDPrefix)
end := start.PrefixEnd()
replicaInfo := map[roachpb.RangeID]*replicaCheckInfo{}
getReplicaInfo := func(rangeID roachpb.RangeID) *replicaCheckInfo {
if info, ok := replicaInfo[rangeID]; ok {
return info
}
replicaInfo[rangeID] = &replicaCheckInfo{}
return replicaInfo[rangeID]
}
if _, err := engine.MVCCIterate(context.Background(), db, start, end, hlc.MaxTimestamp,
false /* !consistent */, nil, /* txn */
false /* !reverse */, func(kv roachpb.KeyValue) (bool, error) {
rangeID, _, suffix, detail, err := keys.DecodeRangeIDKey(kv.Key)
if err != nil {
return false, err
}
switch {
case bytes.Equal(suffix, keys.LocalRaftTruncatedStateSuffix):
var trunc roachpb.RaftTruncatedState
if err := kv.Value.GetProto(&trunc); err != nil {
return false, err
}
getReplicaInfo(rangeID).truncatedIndex = trunc.Index
case bytes.Equal(suffix, keys.LocalRaftAppliedIndexSuffix):
idx, err := kv.Value.GetInt()
if err != nil {
return false, err
}
getReplicaInfo(rangeID).appliedIndex = uint64(idx)
case bytes.Equal(suffix, keys.LocalRaftLogSuffix):
_, index, err := encoding.DecodeUint64Ascending(detail)
if err != nil {
return false, err
}
ri := getReplicaInfo(rangeID)
if ri.firstIndex == 0 {
ri.firstIndex = index
ri.lastIndex = index
} else {
if index != ri.lastIndex+1 {
fmt.Printf("range %s: log index anomaly: %v followed by %v\n",
rangeID, ri.lastIndex, index)
}
ri.lastIndex = index
}
}
return false, nil
}); err != nil {
return err
}
for rangeID, info := range replicaInfo {
if info.truncatedIndex != info.firstIndex-1 {
fmt.Printf("range %s: truncated index %v should equal first index %v - 1\n",
rangeID, info.truncatedIndex, info.firstIndex)
}
if info.appliedIndex < info.firstIndex || info.appliedIndex > info.lastIndex {
fmt.Printf("range %s: applied index %v should be between first index %v and last index %v\n",
rangeID, info.appliedIndex, info.firstIndex, info.lastIndex)
}
}
return nil
}
var debugRocksDBCmd = &cobra.Command{
Use: "rocksdb",
Short: "run the RocksDB 'ldb' tool",
Long: `
Runs the RocksDB 'ldb' tool, which provides various subcommands for examining
raw store data. 'cockroach debug rocksdb' accepts the same arguments and flags
as 'ldb'.
https://github.com/facebook/rocksdb/wiki/Administration-and-Data-Access-Tool#ldb-tool
`,
// LDB does its own flag parsing.
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
engine.RunLDB(args)
},
}
var debugEnvCmd = &cobra.Command{
Use: "env",
Short: "output environment settings",
Long: `
Output environment variables that influence configuration.
`,
Run: func(cmd *cobra.Command, args []string) {
env := envutil.GetEnvReport()
fmt.Print(env)
},
}
var debugCompactCmd = &cobra.Command{
Use: "compact [directory]",
Short: "compact the sstables in a store",
Long: `
Compact the sstables in a store.
`,
RunE: MaybeDecorateGRPCError(runDebugCompact),
}
func runDebugCompact(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
if len(args) != 1 {
return errors.New("one argument is required")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
return db.Compact()
}
var debugSSTablesCmd = &cobra.Command{
Use: "sstables [directory]",
Short: "list the sstables in a store",
Long: `
List the sstables in a store. The output format is 1 or more lines of:
level [ total size #files ]: file sizes
Only non-empty levels are shown. For levels greater than 0, the files span
non-overlapping ranges of the key space. Level-0 is special in that sstables
are created there by flushing the mem-table, thus every level-0 sstable must be
consulted to see if it contains a particular key. Within a level, the file
sizes are displayed in decreasing order and bucketed by the number of files of
that size. The following example shows 3-level output. In Level-3, there are 19
total files and 14 files that are 129 MiB in size.
1 [ 8M 3 ]: 7M 1M 63K
2 [ 110M 7 ]: 31M 30M 13M[2] 10M 8M 5M
3 [ 2G 19 ]: 129M[14] 122M 93M 24M 18M 9M
The suffixes K, M, G and T are used for terseness to represent KiB, MiB, GiB
and TiB.
`,
RunE: MaybeDecorateGRPCError(runDebugSSTables),
}
func runDebugSSTables(cmd *cobra.Command, args []string) error {
stopper := stop.NewStopper()
defer stopper.Stop(stopperContext(stopper))
if len(args) != 1 {
return errors.New("one argument is required")
}
db, err := openStore(cmd, args[0], stopper)
if err != nil {
return err
}
fmt.Printf("%s", db.GetSSTables())
return nil
}
var debugGossipValuesCmd = &cobra.Command{
Use: "gossip-values [directory]",
Short: "dump all the values in a node's gossip instance",
Long: `
Pretty-prints the values in a node's gossip instance.
Can connect to a running server to get the values or can be provided with
a JSON file captured from a node's /_status/gossip/ debug endpoint.
`,
RunE: MaybeDecorateGRPCError(runDebugGossipValues),
}
func runDebugGossipValues(cmd *cobra.Command, args []string) error {
// If a file is provided, use it. Otherwise, try talking to the running node.
var gossipInfo *gossip.InfoStatus
if debugCtx.inputFile != "" {
file, err := os.Open(debugCtx.inputFile)
if err != nil {
return err
}
defer file.Close()
gossipInfo = new(gossip.InfoStatus)
if err := jsonpb.Unmarshal(file, gossipInfo); err != nil {
return errors.Wrap(err, "failed to parse provided file as gossip.InfoStatus")
}
} else {
conn, _, stopper, err := getClientGRPCConn()
if err != nil {
return err
}
ctx := stopperContext(stopper)
defer stopper.Stop(ctx)
status := serverpb.NewStatusClient(conn)
gossipInfo, err = status.Gossip(ctx, &serverpb.GossipRequest{})
if err != nil {
return errors.Wrap(err, "failed to retrieve gossip from server")
}
}
output, err := parseGossipValues(gossipInfo)
if err != nil {
return err
}
fmt.Println(output)
return nil
}
func parseGossipValues(gossipInfo *gossip.InfoStatus) (string, error) {
var output []string
for key, info := range gossipInfo.Infos {
bytes, err := info.Value.GetBytes()
if err != nil {
return "", errors.Wrapf(err, "failed to extract bytes for key %q", key)
}
if key == gossip.KeyClusterID || key == gossip.KeySentinel {
clusterID, err := uuid.FromBytes(bytes)
if err != nil {
return "", errors.Wrapf(err, "failed to parse value for key %q", key)
}
output = append(output, fmt.Sprintf("%q: %v", key, clusterID))
} else if key == gossip.KeySystemConfig {
if debugCtx.printSystemConfig {
var config config.SystemConfig
if err := proto.Unmarshal(bytes, &config); err != nil {
return "", errors.Wrapf(err, "failed to parse value for key %q", key)
}
output = append(output, fmt.Sprintf("%q: %+v", key, config))
} else {
output = append(output, fmt.Sprintf("%q: omitted", key))
}
} else if key == gossip.KeyFirstRangeDescriptor {
var desc roachpb.RangeDescriptor
if err := proto.Unmarshal(bytes, &desc); err != nil {
return "", errors.Wrapf(err, "failed to parse value for key %q", key)
}
output = append(output, fmt.Sprintf("%q: %v", key, desc))
} else if gossip.IsNodeIDKey(key) {
var desc roachpb.NodeDescriptor
if err := proto.Unmarshal(bytes, &desc); err != nil {
return "", errors.Wrapf(err, "failed to parse value for key %q", key)
}
output = append(output, fmt.Sprintf("%q: %+v", key, desc))
} else if strings.HasPrefix(key, gossip.KeyStorePrefix) {
var desc roachpb.StoreDescriptor
if err := proto.Unmarshal(bytes, &desc); err != nil {
return "", errors.Wrapf(err, "failed to parse value for key %q", key)
}
output = append(output, fmt.Sprintf("%q: %+v", key, desc))
} else if strings.HasPrefix(key, gossip.KeyNodeLivenessPrefix) {
var liveness storage.Liveness
if err := proto.Unmarshal(bytes, &liveness); err != nil {
return "", errors.Wrapf(err, "failed to parse value for key %q", key)
}
output = append(output, fmt.Sprintf("%q: %+v", key, liveness))
} else if strings.HasPrefix(key, gossip.KeyDeadReplicasPrefix) {
var deadReplicas roachpb.StoreDeadReplicas
if err := proto.Unmarshal(bytes, &deadReplicas); err != nil {
return "", errors.Wrapf(err, "failed to parse value for key %q", key)
}
output = append(output, fmt.Sprintf("%q: %+v", key, deadReplicas))
}
}
sort.Strings(output)
return strings.Join(output, "\n"), nil
}
func init() {
debugCmd.AddCommand(debugCmds...)
}
var debugCmds = []*cobra.Command{
debugKeysCmd,
debugRangeDataCmd,
debugRangeDescriptorsCmd,
debugRaftLogCmd,
debugGCCmd,
debugCheckStoreCmd,
debugRocksDBCmd,
debugCompactCmd,
debugSSTablesCmd,
debugGossipValuesCmd,
rangeCmd,
debugEnvCmd,
debugZipCmd,
}
var debugCmd = &cobra.Command{
Use: "debug [command]",
Short: "debugging commands",
Long: `Various commands for debugging.
These commands are useful for extracting data from the data files of a
process that has failed and cannot restart.
`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}