forked from vitessio/vitess
/
local_cluster.go
328 lines (303 loc) · 7.87 KB
/
local_cluster.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package vttest provides the functionality to bring
// up a test cluster.
package vttest
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
log "github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/youtube/vitess/go/sqldb"
vttestpb "github.com/youtube/vitess/go/vt/proto/vttest"
)
// Handle allows you to interact with the processes launched by vttest.
type Handle struct {
Data map[string]interface{}
cmd *exec.Cmd
stdin io.WriteCloser
// dbname is valid only for LaunchMySQL.
dbname string
}
// VitessOption is the type for generic options to be passed in to LaunchVitess.
type VitessOption struct {
// beforeRun is executed before we start run_local_database.py.
beforeRun func(*Handle) error
// afterRun is executed after run_local_database.py has been
// started and is running (and is done reading and applying
// the schema).
afterRun func()
}
// Verbose makes the underlying local_cluster verbose.
func Verbose(verbose bool) VitessOption {
return VitessOption{
beforeRun: func(hdl *Handle) error {
if verbose {
hdl.cmd.Args = append(hdl.cmd.Args, "--verbose")
}
return nil
},
}
}
// SchemaDirectory is used to specify a directory to read schema from.
// It cannot be used at the same time as Schema.
func SchemaDirectory(dir string) VitessOption {
return VitessOption{
beforeRun: func(hdl *Handle) error {
if dir != "" {
hdl.cmd.Args = append(hdl.cmd.Args, "--schema_dir", dir)
}
return nil
},
}
}
// ProtoTopo is used to pass in the topology as a vttest proto definition.
// See vttest.proto for more information.
// It cannot be used at the same time as MySQLOnly.
func ProtoTopo(topo *vttestpb.VTTestTopology) VitessOption {
return VitessOption{
beforeRun: func(hdl *Handle) error {
hdl.cmd.Args = append(hdl.cmd.Args, "--proto_topo", proto.CompactTextString(topo))
return nil
},
}
}
// MySQLOnly is used to launch only a mysqld instance, with the specified db name.
// Use it before Schema option.
// It cannot be used at the same as ProtoTopo.
func MySQLOnly(dbName string) VitessOption {
return VitessOption{
beforeRun: func(hdl *Handle) error {
// the way to pass the dbname for creation in
// is to provide a topology
topo := &vttestpb.VTTestTopology{
Keyspaces: []*vttestpb.Keyspace{
{
Name: dbName,
Shards: []*vttestpb.Shard{
{
Name: "0",
DbNameOverride: dbName,
},
},
},
},
}
hdl.dbname = dbName
hdl.cmd.Args = append(hdl.cmd.Args,
"--mysql_only",
"--proto_topo", proto.CompactTextString(topo))
return nil
},
}
}
// Schema is used to specify SQL commands to run at startup.
// It conflicts with SchemaDirectory
func Schema(schema string) VitessOption {
schemaDir := ""
return VitessOption{
beforeRun: func(hdl *Handle) error {
if schema == "" {
return nil
}
if hdl.dbname == "" {
return fmt.Errorf("Schema option requires a previously passed MySQLOnly option")
}
var err error
schemaDir, err = ioutil.TempDir("", "vt")
if err != nil {
return err
}
ksDir := path.Join(schemaDir, hdl.dbname)
err = os.Mkdir(ksDir, os.ModeDir|0775)
if err != nil {
return err
}
fileName := path.Join(ksDir, "schema.sql")
f, err := os.Create(fileName)
if err != nil {
return err
}
n, err := f.WriteString(schema)
if n != len(schema) {
return errors.New("short write")
}
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
hdl.cmd.Args = append(hdl.cmd.Args, "--schema_dir", schemaDir)
return nil
},
afterRun: func() {
if schemaDir != "" {
os.RemoveAll(schemaDir)
}
},
}
}
// InitDataOptions contain the command line arguments that configure
// initialization of vttest with random data. See the documentation of
// the corresponding command line flags in py/vttest/run_local_database.py
// for the meaning of each field. If a field is nil, the flag will not be
// specified when running 'run_local_database.py' and the default value for
// the flag will be used.
type InitDataOptions struct {
rngSeed *int
minTableShardSize *int
maxTableShardSize *int
nullProbability *float64
}
// InitData returns a VitessOption that sets the InitDataOptions parameters.
func InitData(i *InitDataOptions) VitessOption {
return VitessOption{
beforeRun: func(hdl *Handle) error {
hdl.cmd.Args = append(hdl.cmd.Args, "--initialize_with_random_data")
if i.rngSeed != nil {
hdl.cmd.Args = append(hdl.cmd.Args, "--rng_seed", fmt.Sprintf("%v", *i.rngSeed))
}
if i.minTableShardSize != nil {
hdl.cmd.Args = append(hdl.cmd.Args, "--min_table_shard_size", fmt.Sprintf("%v", *i.minTableShardSize))
}
if i.maxTableShardSize != nil {
hdl.cmd.Args = append(hdl.cmd.Args, "--max_table_shard_size", fmt.Sprintf("%v", *i.maxTableShardSize))
}
if i.nullProbability != nil {
hdl.cmd.Args = append(hdl.cmd.Args, "--null_probability", fmt.Sprintf("%v", *i.nullProbability))
}
return nil
},
}
}
// LaunchVitess launches a vitess test cluster.
func LaunchVitess(
options ...VitessOption,
) (hdl *Handle, err error) {
hdl = &Handle{}
err = hdl.run(options...)
if err != nil {
return nil, err
}
return hdl, nil
}
// TearDown tears down the launched processes.
func (hdl *Handle) TearDown() error {
_, err := hdl.stdin.Write([]byte("\n"))
if err != nil {
return err
}
return hdl.cmd.Wait()
}
// MySQLConnParams builds the MySQL connection params.
// It's valid only if you used MySQLOnly option.
func (hdl *Handle) MySQLConnParams() (sqldb.ConnParams, error) {
params := sqldb.ConnParams{
Charset: "utf8",
DbName: hdl.dbname,
}
if hdl.Data == nil {
return params, errors.New("no data")
}
iuser, ok := hdl.Data["username"]
if !ok {
return params, errors.New("no username")
}
user, ok := iuser.(string)
if !ok {
return params, fmt.Errorf("invalid user type: %T", iuser)
}
params.Uname = user
if ipassword, ok := hdl.Data["password"]; ok {
password, ok := ipassword.(string)
if !ok {
return params, fmt.Errorf("invalid password type: %T", ipassword)
}
params.Pass = password
}
if ihost, ok := hdl.Data["host"]; ok {
host, ok := ihost.(string)
if !ok {
return params, fmt.Errorf("invalid host type: %T", ihost)
}
params.Host = host
}
if iport, ok := hdl.Data["port"]; ok {
port, ok := iport.(float64)
if !ok {
return params, fmt.Errorf("invalid port type: %T", iport)
}
params.Port = int(port)
}
if isocket, ok := hdl.Data["socket"]; ok {
socket, ok := isocket.(string)
if !ok {
return params, fmt.Errorf("invalid socket type: %T", isocket)
}
params.UnixSocket = socket
}
return params, nil
}
func (hdl *Handle) run(
options ...VitessOption,
) error {
launcher, err := launcherPath()
if err != nil {
return err
}
port := randomPort()
hdl.cmd = exec.Command(
launcher,
"--port", strconv.Itoa(port),
)
for _, option := range options {
if err := option.beforeRun(hdl); err != nil {
return err
}
if option.afterRun != nil {
defer option.afterRun()
}
}
log.Infof("executing: %v", strings.Join(hdl.cmd.Args, " "))
hdl.cmd.Stderr = os.Stderr
stdout, err := hdl.cmd.StdoutPipe()
if err != nil {
return err
}
decoder := json.NewDecoder(stdout)
hdl.stdin, err = hdl.cmd.StdinPipe()
if err != nil {
return err
}
err = hdl.cmd.Start()
if err != nil {
return err
}
err = decoder.Decode(&hdl.Data)
if err != nil {
err = fmt.Errorf(
"Error (%v) parsing JSON output from command: %v.", err, launcher)
}
return err
}
// randomPort returns a random number between 10k & 30k.
func randomPort() int {
v := rand.Int31n(20000)
return int(v + 10000)
}
func init() {
rand.Seed(time.Now().UnixNano())
}