-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
key_visualizer_server.go
230 lines (190 loc) · 6.68 KB
/
key_visualizer_server.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
// Copyright 2022 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 server
import (
"context"
"sort"
"strings"
"time"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/keyvisualizer/keyvispb"
"github.com/cockroachdb/cockroach/pkg/keyvisualizer/keyvissettings"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/rpc/nodedialer"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
// KeyVisualizerServer is a concrete implementation of the keyvispb.KeyVisualizerServer interface.
type KeyVisualizerServer struct {
ie *sql.InternalExecutor
settings *cluster.Settings
nodeDialer *nodedialer.Dialer
status *systemStatusServer
node *Node
}
var _ keyvispb.KeyVisualizerServer = &KeyVisualizerServer{}
func (s *KeyVisualizerServer) saveBoundaries(
ctx context.Context, req *keyvispb.UpdateBoundariesRequest,
) error {
encoded, err := protoutil.Marshal(req)
if err != nil {
return err
}
// Nodes are notified about boundary changes via the keyvissubscriber.BoundarySubscriber.
_, err = s.ie.ExecEx(
ctx,
"upsert tenant boundaries",
nil,
sessiondata.InternalExecutorOverride{User: username.RootUserName()},
`UPSERT INTO system.span_stats_tenant_boundaries(
tenant_id,
boundaries
) VALUES ($1, $2)
`,
roachpb.SystemTenantID.ToUint64(),
encoded,
)
return err
}
func (s *KeyVisualizerServer) getSamplesFromFanOut(
ctx context.Context, timestamp time.Time,
) (*keyvispb.GetSamplesResponse, error) {
samplePeriod := keyvissettings.SampleInterval.Get(&s.settings.SV)
dialFn := func(ctx context.Context, nodeID roachpb.NodeID) (interface{}, error) {
conn, err := s.nodeDialer.Dial(ctx, nodeID, rpc.DefaultClass)
return keyvispb.NewKeyVisualizerClient(conn), err
}
nodeFn := func(ctx context.Context, client interface{}, nodeID roachpb.NodeID) (interface{}, error) {
samples, err := client.(keyvispb.KeyVisualizerClient).GetSamples(ctx,
&keyvispb.GetSamplesRequest{
NodeID: nodeID,
CollectedOnOrAfter: timestamp,
})
if err != nil {
return nil, err
}
return samples, err
}
globalSamples := make(map[int64][]keyvispb.Sample)
responseFn := func(nodeID roachpb.NodeID, resp interface{}) {
nodeResponse := resp.(*keyvispb.GetSamplesResponse)
// Collection is spread across each node, so samples that belong to the
// same sample period should be aggregated together.
for _, sampleFragment := range nodeResponse.Samples {
tNanos := sampleFragment.SampleTime.Truncate(samplePeriod).UnixNano()
globalSamples[tNanos] = append(globalSamples[tNanos], sampleFragment)
}
}
errorFn := func(nodeID roachpb.NodeID, err error) {
log.Errorf(ctx, "could not get key visualizer sample for node %d: %v",
nodeID, err)
}
err := s.status.iterateNodes(ctx,
"iterating nodes for key visualizer samples", dialFn, nodeFn,
responseFn, errorFn)
if err != nil {
return nil, err
}
var samples []keyvispb.Sample
for sampleTimeNanos, sampleFragments := range globalSamples {
if !verifySampleBoundariesEqual(sampleFragments) {
log.Warningf(ctx, "key visualizer sample boundaries differ between nodes")
}
samples = append(samples, keyvispb.Sample{
SampleTime: timeutil.Unix(0, sampleTimeNanos),
SpanStats: cumulativeStats(sampleFragments),
})
}
return &keyvispb.GetSamplesResponse{Samples: samples}, nil
}
// verifySampleBoundariesEqual returns true if all the samples collected
// from across the cluster belonging to the same sample period have identical
// spans.
func verifySampleBoundariesEqual(fragments []keyvispb.Sample) bool {
f0 := fragments[0]
sort.Slice(f0.SpanStats, func(a, b int) bool {
return f0.SpanStats[a].Span.Key.Compare(f0.SpanStats[b].Span.Key) == -1
})
for i := 1; i < len(fragments); i++ {
fi := fragments[i]
if len(f0.SpanStats) != len(fi.SpanStats) {
return false
}
sort.Slice(fi.SpanStats, func(a, b int) bool {
return fi.SpanStats[a].Span.Key.Compare(fi.SpanStats[b].Span.Key) == -1
})
for b, bucket := range f0.SpanStats {
if !bucket.Span.Equal(fi.SpanStats[b].Span) {
return false
}
}
}
return true
}
// unsafeBytesToString constructs a string from a byte slice. It is
// critical that the byte slice not be modified.
func unsafeBytesToString(data []byte) string {
return *(*string)(unsafe.Pointer(&data))
}
// cumulativeStats uniques and accumulates all of a sample's
// keyvispb.SpanStats from across the cluster. Stores collect statistics for
// the same spans, and the caller wants the cumulative statistics for those spans.
func cumulativeStats(fragments []keyvispb.Sample) []keyvispb.SpanStats {
var stats []keyvispb.SpanStats
for _, sampleFragment := range fragments {
stats = append(stats, sampleFragment.SpanStats...)
}
unique := make(map[string]keyvispb.SpanStats)
for _, stat := range stats {
var sb strings.Builder
sb.WriteString(unsafeBytesToString(stat.Span.Key))
sb.WriteString(unsafeBytesToString(stat.Span.EndKey))
spanAsString := sb.String()
if uniqueStat, ok := unique[spanAsString]; ok {
uniqueStat.Requests += stat.Requests
} else {
unique[spanAsString] = keyvispb.SpanStats{
Span: stat.Span,
Requests: stat.Requests,
}
}
}
ret := make([]keyvispb.SpanStats, 0, len(unique))
for _, stat := range unique {
ret = append(ret, stat)
}
return ret
}
// GetSamples implements the keyvispb.KeyVisualizerServer interface.
func (s *KeyVisualizerServer) GetSamples(
ctx context.Context, req *keyvispb.GetSamplesRequest,
) (*keyvispb.GetSamplesResponse, error) {
if req.NodeID == 0 {
return s.getSamplesFromFanOut(ctx, req.CollectedOnOrAfter)
}
samples := s.node.spanStatsCollector.GetSamples(
req.CollectedOnOrAfter)
return &keyvispb.GetSamplesResponse{Samples: samples}, nil
}
// UpdateBoundaries implements the keyvispb.KeyVisualizerServer interface.
func (s *KeyVisualizerServer) UpdateBoundaries(
ctx context.Context, req *keyvispb.UpdateBoundariesRequest,
) (*keyvispb.UpdateBoundariesResponse, error) {
if err := s.saveBoundaries(ctx, req); err != nil {
return nil, err
}
return &keyvispb.UpdateBoundariesResponse{}, nil
}