-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
load.go
265 lines (234 loc) · 7.58 KB
/
load.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
// Copyright 2017 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package cliccl
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/blobs"
"github.com/cockroachdb/cockroach/pkg/ccl/backupccl"
"github.com/cockroachdb/cockroach/pkg/cli"
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/storage/cloud"
"github.com/cockroachdb/cockroach/pkg/storage/cloudimpl"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/spf13/cobra"
)
const (
descriptors = "descriptors"
files = "files"
spans = "spans"
metadata = "metadata"
)
var externalIODir string
func init() {
loadShowCmd := &cobra.Command{
Use: "show [descriptors|files|spans|metadata] <backup_path>",
Short: "show backups",
Long: "Shows subset(s) of meta information about a SQL backup.",
Args: cobra.MinimumNArgs(1),
RunE: cli.MaybeDecorateGRPCError(runLoadShow),
}
loadCmds := &cobra.Command{
Use: "load [command]",
Short: "loading commands",
Long: `Commands for bulk loading external files.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}
loadFlags := loadCmds.Flags()
loadFlags.StringVarP(
&externalIODir,
cliflags.ExternalIODir.Name,
cliflags.ExternalIODir.Shorthand,
"", /*value*/
cliflags.ExternalIODir.Usage())
cli.AddCmd(loadCmds)
loadCmds.AddCommand(loadShowCmd)
loadShowCmd.Flags().AddFlagSet(loadFlags)
}
func newBlobFactory(ctx context.Context, dialing roachpb.NodeID) (blobs.BlobClient, error) {
if dialing != 0 {
return nil, errors.Errorf(`only support nodelocal (0/self) under offline inspection`)
}
if externalIODir == "" {
externalIODir = filepath.Join(server.DefaultStorePath, "extern")
}
return blobs.NewLocalClient(externalIODir)
}
func parseShowArgs(args []string) (options map[string]bool, path string, err error) {
options = make(map[string]bool)
for _, arg := range args {
switch strings.ToLower(arg) {
case descriptors:
options[descriptors] = true
case files:
options[files] = true
case spans:
options[spans] = true
case metadata:
options[metadata] = true
default:
if path != "" {
return nil, "", errors.New("more than one path is specifiied")
}
path = arg
}
}
if len(options) == 0 {
options[descriptors] = true
options[files] = true
options[spans] = true
options[metadata] = true
}
if len(args) == len(options) {
return nil, "", errors.New("backup_path argument is required")
}
return options, path, nil
}
func runLoadShow(cmd *cobra.Command, args []string) error {
var options map[string]bool
var path string
var err error
if options, path, err = parseShowArgs(args); err != nil {
return err
}
var showHeaders bool
if len(options) > 1 {
showHeaders = true
}
ctx := context.Background()
if !strings.Contains(path, "://") {
path = cloudimpl.MakeLocalStorageURI(path)
}
externalStorageFromURI := func(ctx context.Context, uri string,
user security.SQLUsername) (cloud.ExternalStorage, error) {
return cloudimpl.ExternalStorageFromURI(ctx, uri, base.ExternalIODirConfig{},
cluster.NoSettings, newBlobFactory, user, nil /*Internal Executor*/, nil /*kvDB*/)
}
// This reads the raw backup descriptor (with table descriptors possibly not
// upgraded from the old FK representation, or even older formats). If more
// fields are added to the output, the table descriptors may need to be
// upgraded.
desc, err := backupccl.ReadBackupManifestFromURI(ctx, path, security.RootUserName(),
externalStorageFromURI, nil)
if err != nil {
return err
}
if _, ok := options[metadata]; ok {
showMeta(desc)
}
if _, ok := options[spans]; ok {
showSpans(desc, showHeaders)
}
if _, ok := options[files]; ok {
showFiles(desc, showHeaders)
}
if _, ok := options[descriptors]; ok {
err = showDescriptor(desc)
if err != nil {
return err
}
}
return nil
}
func showMeta(desc backupccl.BackupManifest) {
start := timeutil.Unix(0, desc.StartTime.WallTime).Format(time.RFC3339Nano)
end := timeutil.Unix(0, desc.EndTime.WallTime).Format(time.RFC3339Nano)
fmt.Printf("StartTime: %s (%s)\n", start, desc.StartTime)
fmt.Printf("EndTime: %s (%s)\n", end, desc.EndTime)
fmt.Printf("DataSize: %d (%s)\n", desc.EntryCounts.DataSize, humanizeutil.IBytes(desc.EntryCounts.DataSize))
fmt.Printf("Rows: %d\n", desc.EntryCounts.Rows)
fmt.Printf("IndexEntries: %d\n", desc.EntryCounts.IndexEntries)
fmt.Printf("FormatVersion: %d\n", desc.FormatVersion)
fmt.Printf("ClusterID: %s\n", desc.ClusterID)
fmt.Printf("NodeID: %s\n", desc.NodeID)
fmt.Printf("BuildInfo: %s\n", desc.BuildInfo.Short())
}
func showSpans(desc backupccl.BackupManifest, showHeaders bool) {
tabfmt := ""
if showHeaders {
fmt.Printf("Spans:\n")
tabfmt = "\t"
}
for _, s := range desc.Spans {
fmt.Printf("%s%s\n", tabfmt, s)
}
}
func showFiles(desc backupccl.BackupManifest, showHeaders bool) {
tabfmt := ""
if showHeaders {
fmt.Printf("Files:\n")
tabfmt = "\t"
}
for _, f := range desc.Files {
fmt.Printf("%s%s:\n", tabfmt, f.Path)
fmt.Printf(" Span: %s\n", f.Span)
fmt.Printf(" Sha512: %0128x\n", f.Sha512)
fmt.Printf(" DataSize: %d (%s)\n", f.EntryCounts.DataSize, humanizeutil.IBytes(f.EntryCounts.DataSize))
fmt.Printf(" Rows: %d\n", f.EntryCounts.Rows)
fmt.Printf(" IndexEntries: %d\n", f.EntryCounts.IndexEntries)
}
}
func showDescriptor(desc backupccl.BackupManifest) error {
// Note that these descriptors could be from any past version of the cluster,
// in case more fields need to be added to the output.
dbIDs := make([]descpb.ID, 0)
dbIDToName := make(map[descpb.ID]string)
schemaIDs := make([]descpb.ID, 0)
schemaIDs = append(schemaIDs, keys.PublicSchemaID)
schemaIDToName := make(map[descpb.ID]string)
schemaIDToName[keys.PublicSchemaID] = sessiondata.PublicSchemaName
for i := range desc.Descriptors {
d := &desc.Descriptors[i]
id := descpb.GetDescriptorID(d)
if d.GetDatabase() != nil {
dbIDToName[id] = descpb.GetDescriptorName(d)
dbIDs = append(dbIDs, id)
} else if d.GetSchema() != nil {
schemaIDToName[id] = descpb.GetDescriptorName(d)
schemaIDs = append(schemaIDs, id)
}
}
fmt.Printf("Databases:\n")
for _, id := range dbIDs {
fmt.Printf(" %s\n",
dbIDToName[id])
}
fmt.Printf("Schemas:\n")
for _, id := range schemaIDs {
fmt.Printf(" %s\n",
schemaIDToName[id])
}
fmt.Printf("Tables:\n")
for i := range desc.Descriptors {
d := &desc.Descriptors[i]
if descpb.TableFromDescriptor(d, hlc.Timestamp{}) != nil {
tbDesc := tabledesc.NewImmutable(*descpb.TableFromDescriptor(d, hlc.Timestamp{}))
dbName := dbIDToName[tbDesc.GetParentID()]
schemaName := schemaIDToName[tbDesc.GetParentSchemaID()]
fmt.Printf(" %s.%s.%s\n", dbName, schemaName, descpb.GetDescriptorName(d))
}
}
return nil
}