forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vtexplain.go
179 lines (154 loc) · 4.79 KB
/
vtexplain.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
/*
Copyright 2017 Google Inc.
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.
*/
package main
import (
"flag"
"fmt"
"io/ioutil"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vtexplain"
)
var (
sqlFlag = flag.String("sql", "", "A list of semicolon-delimited SQL commands to analyze")
sqlFileFlag = flag.String("sql-file", "", "Identifies the file that contains the SQL commands to analyze")
schemaFlag = flag.String("schema", "", "The SQL table schema")
schemaFileFlag = flag.String("schema-file", "", "Identifies the file that contains the SQL table schema")
vschemaFlag = flag.String("vschema", "", "Identifies the VTGate routing schema")
vschemaFileFlag = flag.String("vschema-file", "", "Identifies the VTGate routing schema file")
numShards = flag.Int("shards", 2, "Number of shards per keyspace")
executionMode = flag.String("execution-mode", "multi", "The execution mode to simulate -- must be set to multi, legacy-autocommit, or twopc")
replicationMode = flag.String("replication-mode", "ROW", "The replication mode to simulate -- must be set to either ROW or STATEMENT")
normalize = flag.Bool("normalize", false, "Whether to enable vtgate normalization")
outputMode = flag.String("output-mode", "text", "Output in human-friendly text or json")
// vtexplainFlags lists all the flags that should show in usage
vtexplainFlags = []string{
"output-mode",
"normalize",
"shards",
"replication-mode",
"schema",
"schema-file",
"sql",
"sql-file",
"vschema",
"vschema-file",
}
)
func usage() {
fmt.Printf("usage of vtexplain:\n")
for _, name := range vtexplainFlags {
f := flag.Lookup(name)
if f == nil {
panic("unkown flag " + name)
}
flagUsage(f)
}
}
// Cloned from the source to print out the usage for a given flag
func flagUsage(f *flag.Flag) {
s := fmt.Sprintf(" -%s", f.Name) // Two spaces before -; see next two comments.
name, usage := flag.UnquoteUsage(f)
if len(name) > 0 {
s += " " + name
}
// Boolean flags of one ASCII letter are so common we
// treat them specially, putting their usage on the same line.
if len(s) <= 4 { // space, space, '-', 'x'.
s += "\t"
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
s += "\n \t"
}
s += usage
if name == "string" {
// put quotes on the value
s += fmt.Sprintf(" (default %q)", f.DefValue)
} else {
s += fmt.Sprintf(" (default %v)", f.DefValue)
}
fmt.Printf(s + "\n")
}
func init() {
logger := logutil.NewConsoleLogger()
flag.CommandLine.SetOutput(logutil.NewLoggerWriter(logger))
flag.Usage = usage
}
// getFileParam returns a string containing either flag is not "",
// or the content of the file named flagFile
func getFileParam(flag, flagFile, name string) (string, error) {
if flag != "" {
if flagFile != "" {
return "", fmt.Errorf("action requires only one of %v or %v-file", name, name)
}
return flag, nil
}
if flagFile == "" {
return "", fmt.Errorf("action requires one of %v or %v-file", name, name)
}
data, err := ioutil.ReadFile(flagFile)
if err != nil {
return "", fmt.Errorf("Cannot read file %v: %v", flagFile, err)
}
return string(data), nil
}
func main() {
defer exit.RecoverAll()
defer logutil.Flush()
servenv.ParseFlags("vtexplain")
err := parseAndRun()
if err != nil {
fmt.Printf("ERROR: %s\n", err)
exit.Return(1)
}
}
func parseAndRun() error {
sql, err := getFileParam(*sqlFlag, *sqlFileFlag, "sql")
if err != nil {
return err
}
schema, err := getFileParam(*schemaFlag, *schemaFileFlag, "schema")
if err != nil {
return err
}
vschema, err := getFileParam(*vschemaFlag, *vschemaFileFlag, "vschema")
if err != nil {
return err
}
opts := &vtexplain.Options{
ExecutionMode: *executionMode,
ReplicationMode: *replicationMode,
NumShards: *numShards,
Normalize: *normalize,
}
log.V(100).Infof("sql %s\n", sql)
log.V(100).Infof("schema %s\n", schema)
log.V(100).Infof("vschema %s\n", vschema)
err = vtexplain.Init(vschema, schema, opts)
if err != nil {
return err
}
plans, err := vtexplain.Run(sql)
if err != nil {
return err
}
if *outputMode == "text" {
fmt.Print(vtexplain.ExplainsAsText(plans))
} else {
fmt.Print(vtexplain.ExplainsAsJSON(plans))
}
return nil
}