-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
774 lines (694 loc) · 22 KB
/
status.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
// Copyright 2014 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.
//
// Author: Bram Gruneir (bram+code@cockroachlabs.com)
package server
import (
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"regexp"
"runtime"
"strconv"
"sync"
"github.com/coreos/etcd/raft"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/pkg/errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
const (
// Default Maximum number of log entries returned.
defaultMaxLogEntries = 1000
// stackTraceApproxSize is the approximate size of a goroutine stack trace.
stackTraceApproxSize = 1024
// statusPrefix is the root of the cluster statistics and metrics API.
statusPrefix = "/_status/"
// statusVars exposes prometheus metrics for monitoring consumption.
statusVars = statusPrefix + "vars"
// rangeDebugEndpoint exposes an html page with information about a specific range.
rangeDebugEndpoint = "/debug/range"
// problemRangesDebugEndpoint exposes an html page with a list of all ranges
// that are experiencing problems.
problemRangesDebugEndpoint = "/debug/problemranges"
// raftStateDormant is used when there is no known raft state.
raftStateDormant = "StateDormant"
)
// Pattern for local used when determining the node ID.
var localRE = regexp.MustCompile(`(?i)local`)
type metricMarshaler interface {
json.Marshaler
PrintAsText(io.Writer) error
}
// A statusServer provides a RESTful status API.
type statusServer struct {
log.AmbientContext
db *client.DB
gossip *gossip.Gossip
metricSource metricMarshaler
nodeLiveness *storage.NodeLiveness
rpcCtx *rpc.Context
stores *storage.Stores
stopper *stop.Stopper
}
// newStatusServer allocates and returns a statusServer.
func newStatusServer(
ambient log.AmbientContext,
db *client.DB,
gossip *gossip.Gossip,
metricSource metricMarshaler,
nodeLiveness *storage.NodeLiveness,
rpcCtx *rpc.Context,
stores *storage.Stores,
stopper *stop.Stopper,
) *statusServer {
ambient.AddLogTag("status", nil)
server := &statusServer{
AmbientContext: ambient,
db: db,
gossip: gossip,
metricSource: metricSource,
nodeLiveness: nodeLiveness,
rpcCtx: rpcCtx,
stores: stores,
stopper: stopper,
}
return server
}
// RegisterService registers the GRPC service.
func (s *statusServer) RegisterService(g *grpc.Server) {
serverpb.RegisterStatusServer(g, s)
}
// RegisterGateway starts the gateway (i.e. reverse
// proxy) that proxies HTTP requests to the appropriate gRPC endpoints.
func (s *statusServer) RegisterGateway(
ctx context.Context, mux *gwruntime.ServeMux, conn *grpc.ClientConn,
) error {
ctx = s.AnnotateCtx(ctx)
return serverpb.RegisterStatusHandler(ctx, mux, conn)
}
func (s *statusServer) parseNodeID(nodeIDParam string) (roachpb.NodeID, bool, error) {
// No parameter provided or set to local.
if len(nodeIDParam) == 0 || localRE.MatchString(nodeIDParam) {
return s.gossip.NodeID.Get(), true, nil
}
id, err := strconv.ParseInt(nodeIDParam, 10, 64)
if err != nil {
return 0, false, errors.Wrap(err, "node id could not be parsed")
}
nodeID := roachpb.NodeID(id)
return nodeID, nodeID == s.gossip.NodeID.Get(), nil
}
func (s *statusServer) dialNode(nodeID roachpb.NodeID) (serverpb.StatusClient, error) {
addr, err := s.gossip.GetNodeIDAddress(nodeID)
if err != nil {
return nil, err
}
conn, err := s.rpcCtx.GRPCDial(addr.String())
if err != nil {
return nil, err
}
return serverpb.NewStatusClient(conn), nil
}
// Gossip returns gossip network status.
func (s *statusServer) Gossip(
ctx context.Context, req *serverpb.GossipRequest,
) (*gossip.InfoStatus, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if local {
infoStatus := s.gossip.GetInfoStatus()
return &infoStatus, nil
}
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.Gossip(ctx, req)
}
// Details returns node details.
func (s *statusServer) Details(
ctx context.Context, req *serverpb.DetailsRequest,
) (*serverpb.DetailsResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if local {
resp := &serverpb.DetailsResponse{
NodeID: s.gossip.NodeID.Get(),
BuildInfo: build.GetInfo(),
}
if addr, err := s.gossip.GetNodeIDAddress(s.gossip.NodeID.Get()); err == nil {
resp.Address = *addr
}
return resp, nil
}
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.Details(ctx, req)
}
// LogFilesList returns a list of available log files.
func (s *statusServer) LogFilesList(
ctx context.Context, req *serverpb.LogFilesListRequest,
) (*serverpb.LogFilesListResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if !local {
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.LogFilesList(ctx, req)
}
log.Flush()
logFiles, err := log.ListLogFiles()
if err != nil {
return nil, err
}
return &serverpb.LogFilesListResponse{Files: logFiles}, err
}
// LogFile returns a single log file.
func (s *statusServer) LogFile(
ctx context.Context, req *serverpb.LogFileRequest,
) (*serverpb.LogEntriesResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if !local {
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.LogFile(ctx, req)
}
log.Flush()
reader, err := log.GetLogReader(req.File, true /* restricted */)
if reader == nil || err != nil {
return nil, fmt.Errorf("log file %s could not be opened: %s", req.File, err)
}
defer reader.Close()
var entry log.Entry
var resp serverpb.LogEntriesResponse
decoder := log.NewEntryDecoder(reader)
for {
if err := decoder.Decode(&entry); err != nil {
if err == io.EOF {
break
}
return nil, err
}
resp.Entries = append(resp.Entries, entry)
}
return &resp, nil
}
// parseInt64WithDefault attempts to parse the passed in string. If an empty
// string is supplied or parsing results in an error the default value is
// returned. If an error does occur during parsing, the error is returned as
// well.
func parseInt64WithDefault(s string, defaultValue int64) (int64, error) {
if len(s) == 0 {
return defaultValue, nil
}
result, err := strconv.ParseInt(s, 10, 0)
if err != nil {
return defaultValue, err
}
return result, nil
}
// Logs returns the log entries parsed from the log files stored on
// the server. Log entries are returned in reverse chronological order. The
// following options are available:
// * "starttime" query parameter filters the log entries to only ones that
// occurred on or after the "starttime". Defaults to a day ago.
// * "endtime" query parameter filters the log entries to only ones that
// occurred before on on the "endtime". Defaults to the current time.
// * "pattern" query parameter filters the log entries by the provided regexp
// pattern if it exists. Defaults to nil.
// * "max" query parameter is the hard limit of the number of returned log
// entries. Defaults to defaultMaxLogEntries.
// To filter the log messages to only retrieve messages from a given level,
// use a pattern that excludes all messages at the undesired levels.
// (e.g. "^[^IW]" to only get errors, fatals and panics). An exclusive
// pattern is better because panics and some other errors do not use
// a prefix character.
func (s *statusServer) Logs(
ctx context.Context, req *serverpb.LogsRequest,
) (*serverpb.LogEntriesResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if !local {
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.Logs(ctx, req)
}
log.Flush()
startTimestamp, err := parseInt64WithDefault(
req.StartTime,
timeutil.Now().AddDate(0, 0, -1).UnixNano())
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "StartTime could not be parsed: %s", err)
}
endTimestamp, err := parseInt64WithDefault(req.EndTime, timeutil.Now().UnixNano())
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "EndTime could not be parsed: %s", err)
}
if startTimestamp > endTimestamp {
return nil, grpc.Errorf(codes.InvalidArgument, "StartTime: %d should not be greater than endtime: %d", startTimestamp, endTimestamp)
}
maxEntries, err := parseInt64WithDefault(req.Max, defaultMaxLogEntries)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "Max could not be parsed: %s", err)
}
if maxEntries < 1 {
return nil, grpc.Errorf(codes.InvalidArgument, "Max: %d should be set to a value greater than 0", maxEntries)
}
var regex *regexp.Regexp
if len(req.Pattern) > 0 {
if regex, err = regexp.Compile(req.Pattern); err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "regex pattern could not be compiled: %s", err)
}
}
entries, err := log.FetchEntriesFromFiles(startTimestamp, endTimestamp, int(maxEntries), regex)
if err != nil {
return nil, err
}
return &serverpb.LogEntriesResponse{Entries: entries}, nil
}
// TODO(tschottdorf): significant overlap with /debug/pprof/goroutine, except
// that this one allows querying by NodeID.
//
// Stacks handles returns goroutine stack traces.
func (s *statusServer) Stacks(
ctx context.Context, req *serverpb.StacksRequest,
) (*serverpb.JSONResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if !local {
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.Stacks(ctx, req)
}
bufSize := runtime.NumGoroutine() * stackTraceApproxSize
for {
buf := make([]byte, bufSize)
length := runtime.Stack(buf, true)
// If this wasn't large enough to accommodate the full set of
// stack traces, increase by 2 and try again.
if length == bufSize {
bufSize = bufSize * 2
continue
}
return &serverpb.JSONResponse{Data: buf[:length]}, nil
}
}
// Nodes returns all node statuses.
func (s *statusServer) Nodes(
ctx context.Context, req *serverpb.NodesRequest,
) (*serverpb.NodesResponse, error) {
ctx = s.AnnotateCtx(ctx)
startKey := keys.StatusNodePrefix
endKey := startKey.PrefixEnd()
b := &client.Batch{}
b.Scan(startKey, endKey)
if err := s.db.Run(ctx, b); err != nil {
log.Error(ctx, err)
return nil, grpc.Errorf(codes.Internal, err.Error())
}
rows := b.Results[0].Rows
resp := serverpb.NodesResponse{
Nodes: make([]status.NodeStatus, len(rows)),
}
for i, row := range rows {
if err := row.ValueProto(&resp.Nodes[i]); err != nil {
log.Error(ctx, err)
return nil, grpc.Errorf(codes.Internal, err.Error())
}
}
return &resp, nil
}
// handleNodeStatus handles GET requests for a single node's status.
func (s *statusServer) Node(
ctx context.Context, req *serverpb.NodeRequest,
) (*status.NodeStatus, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, _, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
key := keys.NodeStatusKey(nodeID)
b := &client.Batch{}
b.Get(key)
if err := s.db.Run(ctx, b); err != nil {
log.Error(ctx, err)
return nil, grpc.Errorf(codes.Internal, err.Error())
}
var nodeStatus status.NodeStatus
if err := b.Results[0].Rows[0].ValueProto(&nodeStatus); err != nil {
err = errors.Errorf("could not unmarshal NodeStatus from %s: %s", key, err)
log.Error(ctx, err)
return nil, grpc.Errorf(codes.Internal, err.Error())
}
return &nodeStatus, nil
}
// Metrics return metrics information for the server specified.
func (s *statusServer) Metrics(
ctx context.Context, req *serverpb.MetricsRequest,
) (*serverpb.JSONResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if !local {
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.Metrics(ctx, req)
}
return marshalJSONResponse(s.metricSource)
}
// RaftDebug returns raft debug information for all known nodes.
func (s *statusServer) RaftDebug(
ctx context.Context, req *serverpb.RaftDebugRequest,
) (*serverpb.RaftDebugResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodes, err := s.Nodes(ctx, nil)
if err != nil {
return nil, err
}
mu := struct {
syncutil.Mutex
resp serverpb.RaftDebugResponse
}{
resp: serverpb.RaftDebugResponse{
Ranges: make(map[roachpb.RangeID]serverpb.RaftRangeStatus),
},
}
// Subtract base.NetworkTimeout from the deadline so we have time to process
// the results and return them.
if deadline, ok := ctx.Deadline(); ok {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, deadline.Add(-base.NetworkTimeout))
defer cancel()
}
// Parallelize fetching of ranges to minimize total time.
var wg sync.WaitGroup
for _, node := range nodes.Nodes {
wg.Add(1)
nodeID := node.Desc.NodeID
go func() {
defer wg.Done()
ranges, err := s.Ranges(ctx, &serverpb.RangesRequest{NodeId: nodeID.String(), RangeIDs: req.RangeIDs})
mu.Lock()
defer mu.Unlock()
if err != nil {
err := errors.Wrapf(err, "failed to get ranges from %d", nodeID)
mu.resp.Errors = append(mu.resp.Errors, serverpb.RaftRangeError{Message: err.Error()})
return
}
for _, rng := range ranges.Ranges {
rangeID := rng.State.Desc.RangeID
status, ok := mu.resp.Ranges[rangeID]
if !ok {
status = serverpb.RaftRangeStatus{
RangeID: rangeID,
}
}
status.Nodes = append(status.Nodes, serverpb.RaftRangeNode{
NodeID: nodeID,
Range: rng,
})
mu.resp.Ranges[rangeID] = status
}
}()
}
wg.Wait()
mu.Lock()
defer mu.Unlock()
// Check for errors.
for i, rng := range mu.resp.Ranges {
for j, node := range rng.Nodes {
desc := node.Range.State.Desc
// Check for whether replica should be GCed.
containsNode := false
for _, replica := range desc.Replicas {
if replica.NodeID == node.NodeID {
containsNode = true
}
}
if !containsNode {
rng.Errors = append(rng.Errors, serverpb.RaftRangeError{
Message: fmt.Sprintf("node %d not in range descriptor and should be GCed", node.NodeID),
})
}
// Check for replica descs not matching.
if j > 0 {
prevDesc := rng.Nodes[j-1].Range.State.Desc
if !reflect.DeepEqual(&desc, &prevDesc) {
prevNodeID := rng.Nodes[j-1].NodeID
rng.Errors = append(rng.Errors, serverpb.RaftRangeError{
Message: fmt.Sprintf("node %d range descriptor does not match node %d", node.NodeID, prevNodeID),
})
}
}
mu.resp.Ranges[i] = rng
}
}
return &mu.resp, nil
}
func (s *statusServer) handleVars(w http.ResponseWriter, r *http.Request) {
w.Header().Set(httputil.ContentTypeHeader, httputil.PlaintextContentType)
err := s.metricSource.PrintAsText(w)
if err != nil {
log.Error(r.Context(), err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Ranges returns range info for the specified node.
func (s *statusServer) Ranges(
ctx context.Context, req *serverpb.RangesRequest,
) (*serverpb.RangesResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeId)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if !local {
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.Ranges(ctx, req)
}
output := serverpb.RangesResponse{
Ranges: make([]serverpb.RangeInfo, 0, s.stores.GetStoreCount()),
}
convertRaftStatus := func(raftStatus *raft.Status) serverpb.RaftState {
var state serverpb.RaftState
if raftStatus == nil {
state.State = raftStateDormant
return state
}
state.ReplicaID = raftStatus.ID
state.HardState = raftStatus.HardState
state.Applied = raftStatus.Applied
// Grab Lead and State, which together form the SoftState.
state.Lead = raftStatus.Lead
state.State = raftStatus.RaftState.String()
state.Progress = make(map[uint64]serverpb.RaftState_Progress)
for id, progress := range raftStatus.Progress {
state.Progress[id] = serverpb.RaftState_Progress{
Match: progress.Match,
Next: progress.Next,
Paused: progress.Paused,
PendingSnapshot: progress.PendingSnapshot,
State: progress.State.String(),
}
}
return state
}
constructRangeInfo := func(
desc roachpb.RangeDescriptor, rep *storage.Replica, storeID roachpb.StoreID, leaseHistory []roachpb.Lease, metrics storage.ReplicaMetrics,
) serverpb.RangeInfo {
return serverpb.RangeInfo{
Span: serverpb.PrettySpan{
StartKey: desc.StartKey.String(),
EndKey: desc.EndKey.String(),
},
RaftState: convertRaftStatus(rep.RaftStatus()),
State: rep.State(),
SourceNodeID: nodeID,
SourceStoreID: storeID,
LeaseHistory: leaseHistory,
Problems: serverpb.RangeProblems{
Unavailable: metrics.RangeCounter && metrics.Unavailable,
LeaderNotLeaseHolder: metrics.Leader && metrics.LeaseValid && !metrics.Leaseholder,
},
}
}
cfg, ok := s.gossip.GetSystemConfig()
if !ok {
return nil, grpc.Errorf(codes.Internal, "system config not yet available")
}
isLiveMap := s.nodeLiveness.GetIsLiveMap()
err = s.stores.VisitStores(func(store *storage.Store) error {
timestamp := store.Clock().Now()
if len(req.RangeIDs) == 0 {
// All ranges requested.
// Use IterateRangeDescriptors to read from the engine only
// because it's already exported.
err := storage.IterateRangeDescriptors(ctx, store.Engine(),
func(desc roachpb.RangeDescriptor) (bool, error) {
rep, err := store.GetReplica(desc.RangeID)
if err != nil {
return true, err
}
output.Ranges = append(output.Ranges,
constructRangeInfo(
desc,
rep,
store.Ident.StoreID,
rep.GetLeaseHistory(),
rep.Metrics(ctx, timestamp, cfg, isLiveMap),
))
return false, nil
})
return err
}
// Specific ranges requested:
for _, rid := range req.RangeIDs {
rep, err := store.GetReplica(rid)
if err != nil {
// Not found: continue.
continue
}
desc := rep.Desc()
output.Ranges = append(output.Ranges,
constructRangeInfo(
*desc,
rep,
store.Ident.StoreID,
rep.GetLeaseHistory(),
rep.Metrics(ctx, timestamp, cfg, isLiveMap),
))
}
return nil
})
if err != nil {
return nil, grpc.Errorf(codes.Internal, err.Error())
}
return &output, nil
}
// SpanStats requests the total statistics stored on a node for a given key
// span, which may include multiple ranges.
func (s *statusServer) SpanStats(
ctx context.Context, req *serverpb.SpanStatsRequest,
) (*serverpb.SpanStatsResponse, error) {
ctx = s.AnnotateCtx(ctx)
nodeID, local, err := s.parseNodeID(req.NodeID)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}
if !local {
status, err := s.dialNode(nodeID)
if err != nil {
return nil, err
}
return status.SpanStats(ctx, req)
}
output := &serverpb.SpanStatsResponse{}
err = s.stores.VisitStores(func(store *storage.Store) error {
stats, count := store.ComputeStatsForKeySpan(req.StartKey.Next(), req.EndKey)
output.TotalStats.Add(stats)
output.RangeCount += int32(count)
return nil
})
if err != nil {
return nil, err
}
return output, nil
}
// jsonWrapper provides a wrapper on any slice data type being
// marshaled to JSON. This prevents a security vulnerability
// where a phishing attack can trick a user's browser into
// requesting a document from Cockroach as an executable script,
// allowing the contents of the fetched document to be treated
// as executable javascript. More details here:
// http://haacked.com/archive/2009/06/25/json-hijacking.aspx/
type jsonWrapper struct {
Data interface{} `json:"d"`
}
// marshalToJSON marshals the given value into nicely indented JSON. If the
// value is an array or slice it is wrapped in jsonWrapper and then marshalled.
func marshalToJSON(value interface{}) ([]byte, error) {
switch reflect.ValueOf(value).Kind() {
case reflect.Array, reflect.Slice:
value = jsonWrapper{Data: value}
}
body, err := json.MarshalIndent(value, "", " ")
if err != nil {
return nil, errors.Errorf("unable to marshal %+v to json: %s", value, err)
}
return body, nil
}
// marshalJSONResponse converts an arbitrary value into a JSONResponse protobuf
// that can be sent via grpc.
func marshalJSONResponse(value interface{}) (*serverpb.JSONResponse, error) {
data, err := marshalToJSON(value)
if err != nil {
return nil, err
}
return &serverpb.JSONResponse{Data: data}, nil
}