-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
zip_cluster_wide.go
225 lines (206 loc) · 7.53 KB
/
zip_cluster_wide.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
// Copyright 2021 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 cli
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
debugBase = "debug"
eventsName = debugBase + "/events"
livenessName = debugBase + "/liveness"
nodesPrefix = debugBase + "/nodes"
rangelogName = debugBase + "/rangelog"
reportsPrefix = debugBase + "/reports"
schemaPrefix = debugBase + "/schema"
settingsName = debugBase + "/settings"
problemRangesName = reportsPrefix + "/problemranges"
)
// makeClusterWideZipRequests defines the zipRequests that are to be
// performed just once for the entire cluster.
func makeClusterWideZipRequests(
admin serverpb.AdminClient, status serverpb.StatusClient,
) []zipRequest {
return []zipRequest{
// NB: we intentionally omit liveness since it's already pulled manually (we
// act on the output to special case decommissioned nodes).
{
fn: func(ctx context.Context) (interface{}, error) {
return admin.Events(ctx, &serverpb.EventsRequest{})
},
pathName: eventsName,
},
{
fn: func(ctx context.Context) (interface{}, error) {
return admin.RangeLog(ctx, &serverpb.RangeLogRequest{})
},
pathName: rangelogName,
},
{
fn: func(ctx context.Context) (interface{}, error) {
return admin.Settings(ctx, &serverpb.SettingsRequest{})
},
pathName: settingsName,
},
{
fn: func(ctx context.Context) (interface{}, error) {
return status.ProblemRanges(ctx, &serverpb.ProblemRangesRequest{})
},
pathName: problemRangesName,
},
}
}
// Tables containing cluster-wide info that are collected using SQL
// into a debug zip.
var debugZipTablesPerCluster = []string{
"crdb_internal.cluster_contention_events",
"crdb_internal.cluster_distsql_flows",
"crdb_internal.cluster_database_privileges",
"crdb_internal.cluster_queries",
"crdb_internal.cluster_sessions",
"crdb_internal.cluster_settings",
"crdb_internal.cluster_transactions",
"crdb_internal.default_privileges",
"crdb_internal.jobs",
"system.jobs", // get the raw, restorable jobs records too.
"system.descriptor", // descriptors also contain job-like mutation state.
"system.namespace",
"system.scheduled_jobs",
"system.settings", // get the raw settings to determine what's explicitly set.
"system.replication_stats",
"system.replication_critical_localities",
"system.replication_constraint_stats",
// The synthetic SQL CREATE statements for all tables.
// Note the "". to collect across all databases.
`"".crdb_internal.create_schema_statements`,
`"".crdb_internal.create_statements`,
// Ditto, for CREATE TYPE.
`"".crdb_internal.create_type_statements`,
"crdb_internal.kv_node_liveness",
"crdb_internal.kv_node_status",
"crdb_internal.kv_store_status",
"crdb_internal.regions",
"crdb_internal.schema_changes",
"crdb_internal.partitions",
"crdb_internal.zones",
"crdb_internal.invalid_objects",
"crdb_internal.index_usage_statistics",
"crdb_internal.table_indexes",
}
// nodesInfo holds node details pulled from a SQL or KV node.
// SQL only servers will only return nodeDetails for all SQL nodes.
// Storage servers will return both nodeDetails as well as nodeStatus
// for all KV nodes.
type nodesInfo struct {
nodeStatusResponse *serverpb.NodesResponse
nodeListResponse *serverpb.NodesListResponse
}
// collectClusterData runs the data collection that only needs to
// occur once for the entire cluster.
func (zc *debugZipContext) collectClusterData(
ctx context.Context, firstNodeDetails *serverpb.DetailsResponse,
) (ni nodesInfo, livenessByNodeID nodeLivenesses, err error) {
clusterWideZipRequests := makeClusterWideZipRequests(zc.admin, zc.status)
for _, r := range clusterWideZipRequests {
if err := zc.runZipRequest(ctx, zc.clusterPrinter, r); err != nil {
return nodesInfo{}, nil, err
}
}
for _, table := range debugZipTablesPerCluster {
query := fmt.Sprintf(`SELECT * FROM %s`, table)
if override, ok := customQuery[table]; ok {
query = override
}
if err := zc.dumpTableDataForZip(zc.clusterPrinter, zc.firstNodeSQLConn, debugBase, table, query); err != nil {
return nodesInfo{}, nil, errors.Wrapf(err, "fetching %s", table)
}
}
{
s := zc.clusterPrinter.start("requesting nodes")
err := zc.runZipFn(ctx, s, func(ctx context.Context) error {
ni, err = zc.nodesInfo(ctx)
return err
})
if ni.nodeStatusResponse != nil {
if cErr := zc.z.createJSONOrError(s, debugBase+"/nodes.json", ni.nodeStatusResponse, err); cErr != nil {
return nodesInfo{}, nil, cErr
}
} else {
if cErr := zc.z.createJSONOrError(s, debugBase+"/nodes.json", ni.nodeListResponse, err); cErr != nil {
return nodesInfo{}, nil, cErr
}
}
if ni.nodeListResponse == nil {
// In case nodes came up back empty (the Nodes()/NodesList() RPC failed), we
// still want to inspect the per-node endpoints on the head
// node. As per the above, we were able to connect at least to
// that.
ni.nodeListResponse = &serverpb.NodesListResponse{
Nodes: []serverpb.NodeDetails{{
NodeID: int32(firstNodeDetails.NodeID),
Address: firstNodeDetails.Address,
SQLAddress: firstNodeDetails.SQLAddress,
}},
}
}
// We'll want livenesses to decide whether a node is decommissioned.
var lresponse *serverpb.LivenessResponse
s = zc.clusterPrinter.start("requesting liveness")
err = zc.runZipFn(ctx, s, func(ctx context.Context) error {
lresponse, err = zc.admin.Liveness(ctx, &serverpb.LivenessRequest{})
return err
})
if cErr := zc.z.createJSONOrError(s, livenessName+".json", nodes, err); cErr != nil {
return nodesInfo{}, nil, cErr
}
livenessByNodeID = map[roachpb.NodeID]livenesspb.NodeLivenessStatus{}
if lresponse != nil {
livenessByNodeID = lresponse.Statuses
}
}
return ni, livenessByNodeID, nil
}
// nodesInfo constructs debug data for all nodes for the debug zip output.
// For SQL only servers, only the NodesListResponse is populated.
// For regular storage servers, the more detailed NodesResponse is
// returned along with the nodesListResponse.
func (zc *debugZipContext) nodesInfo(ctx context.Context) (ni nodesInfo, _ error) {
nodesResponse, err := zc.status.Nodes(ctx, &serverpb.NodesRequest{})
nodesList := &serverpb.NodesListResponse{}
if code := status.Code(errors.Cause(err)); code == codes.Unimplemented {
// Likely a SQL only server; try the NodesList endpoint.
nodesList, err = zc.status.NodesList(ctx, &serverpb.NodesListRequest{})
}
if err != nil {
return nodesInfo{}, err
}
if nodesResponse != nil {
// Build a nodesListResponse from the nodes data. nodesListResponse is needed
// further downstream to perform other debug zip related functionality such as
// collecting per node debug data.
for _, node := range nodesResponse.Nodes {
nodeDetails := serverpb.NodeDetails{
NodeID: int32(node.Desc.NodeID),
Address: node.Desc.Address,
SQLAddress: node.Desc.SQLAddress,
}
nodesList.Nodes = append(nodesList.Nodes, nodeDetails)
}
}
ni.nodeListResponse = nodesList
ni.nodeStatusResponse = nodesResponse
return ni, nil
}