-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
main.go
352 lines (303 loc) · 11.8 KB
/
main.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright 2019 The Vitess Authors.
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.
*/
// vtcombo: a single binary that contains:
// - a ZK topology server based on an in-memory map.
// - one vtgate instance.
// - many vttablet instances.
// - a vtctld instance so it's easy to see the topology.
package main
import (
"context"
"os"
"strings"
"time"
"github.com/spf13/pflag"
"google.golang.org/protobuf/proto"
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/vtcombo"
"vitess.io/vitess/go/vt/vtctld"
"vitess.io/vitess/go/vt/vtgate"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/vt/vttest"
"vitess.io/vitess/go/vt/wrangler"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vttestpb "vitess.io/vitess/go/vt/proto/vttest"
)
var (
flags = pflag.NewFlagSet("vtcombo", pflag.ContinueOnError)
schemaDir = flags.String("schema_dir", "", "Schema base directory. Should contain one directory per keyspace, with a vschema.json file if necessary.")
startMysql = flags.Bool("start_mysql", false, "Should vtcombo also start mysql")
mysqlPort = flags.Int("mysql_port", 3306, "mysql port")
externalTopoServer = flags.Bool("external_topo_server", false, "Should vtcombo use an external topology server instead of starting its own in-memory topology server. "+
"If true, vtcombo will use the flags defined in topo/server.go to open topo server")
plannerName = flags.String("planner-version", "", "Sets the default planner to use when the session has not changed it. Valid values are: V3, V3Insert, Gen4, Gen4Greedy and Gen4Fallback. Gen4Fallback tries the gen4 planner and falls back to the V3 planner if the gen4 fails.")
tpb vttestpb.VTTestTopology
ts *topo.Server
resilientServer *srvtopo.ResilientServer
)
func init() {
flags.Var(vttest.TextTopoData(&tpb), "proto_topo", "vttest proto definition of the topology, encoded in compact text format. See vttest.proto for more information.")
flags.Var(vttest.JSONTopoData(&tpb), "json_topo", "vttest proto definition of the topology, encoded in json format. See vttest.proto for more information.")
servenv.RegisterDefaultFlags()
servenv.RegisterFlags()
servenv.RegisterGRPCServerFlags()
servenv.RegisterGRPCServerAuthFlags()
servenv.RegisterServiceMapFlag()
}
func startMysqld(uid uint32) (*mysqlctl.Mysqld, *mysqlctl.Mycnf) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
mycnfFile := mysqlctl.MycnfFile(uid)
var mysqld *mysqlctl.Mysqld
var cnf *mysqlctl.Mycnf
var err error
if _, statErr := os.Stat(mycnfFile); os.IsNotExist(statErr) {
mysqld, cnf, err = mysqlctl.CreateMysqldAndMycnf(uid, "", *mysqlPort)
if err != nil {
log.Errorf("failed to initialize mysql config :%v", err)
exit.Return(1)
}
if err := mysqld.Init(ctx, cnf, ""); err != nil {
log.Errorf("failed to initialize mysql :%v", err)
exit.Return(1)
}
} else {
mysqld, cnf, err = mysqlctl.OpenMysqldAndMycnf(uid)
if err != nil {
log.Errorf("failed to find mysql config: %v", err)
exit.Return(1)
}
err = mysqld.RefreshConfig(ctx, cnf)
if err != nil {
log.Errorf("failed to refresh config: %v", err)
exit.Return(1)
}
if err := mysqld.Start(ctx, cnf); err != nil {
log.Errorf("Failed to start mysqld: %v", err)
exit.Return(1)
}
}
cancel()
return mysqld, cnf
}
func main() {
defer exit.Recover()
// flag parsing
var globalFlags *pflag.FlagSet
dbconfigs.RegisterFlags(dbconfigs.All...)
mysqlctl.RegisterFlags()
servenv.OnParseFor("vtcombo", func(fs *pflag.FlagSet) {
// We're going to force the value later, so don't even bother letting
// the user know about this flag.
fs.MarkHidden("tablet_protocol")
// Add the vtcombo flags declared above in var/init sections to the
// global flags.
fs.AddFlagSet(flags)
// Save for later -- see comment directly after ParseFlags for why.
globalFlags = fs
acl.RegisterFlags(fs)
})
servenv.ParseFlags("vtcombo")
// At this point, servenv.ParseFlags has invoked _flag.Parse, which has
// combined all the flags everywhere into the globalFlags variable we
// stashed a reference to earlier in our OnParseFor callback function.
//
// We now take those flags and make them available to our `flags` instance,
// which we call `Set` on various flags to force their values further down
// in main().
//
// N.B.: we could just as easily call Set on globalFlags on everything
// (including our local flags), but we need to save a reference either way,
// and that in particular (globalFlags.Set on a local flag) feels more
// potentially confusing than its inverse (flags.Set on a global flag), so
// we go this way.
flags.AddFlagSet(globalFlags)
// Stash away a copy of the topology that vtcombo was started with.
//
// We will use this to determine the shard structure when keyspaces
// get recreated.
originalTopology := proto.Clone(&tpb).(*vttestpb.VTTestTopology)
// default cell to "test" if unspecified
if len(tpb.Cells) == 0 {
tpb.Cells = append(tpb.Cells, "test")
}
flags.Set("cells_to_watch", strings.Join(tpb.Cells, ","))
// vtctld UI requires the cell flag
flags.Set("cell", tpb.Cells[0])
if flags.Lookup("log_dir") == nil {
flags.Set("log_dir", "$VTDATAROOT/tmp")
}
if *externalTopoServer {
// Open topo server based on the command line flags defined at topo/server.go
// do not create cell info as it should be done by whoever sets up the external topo server
ts = topo.Open()
} else {
// Create topo server. We use a 'memorytopo' implementation.
ts = memorytopo.NewServer(tpb.Cells...)
}
// attempt to load any routing rules specified by tpb
if err := vtcombo.InitRoutingRules(context.Background(), ts, tpb.GetRoutingRules()); err != nil {
log.Errorf("Failed to load routing rules: %v", err)
exit.Return(1)
}
servenv.Init()
tabletenv.Init()
mysqld := &vtcomboMysqld{}
var cnf *mysqlctl.Mycnf
if *startMysql {
mysqld.Mysqld, cnf = startMysqld(1)
servenv.OnClose(func() {
mysqld.Shutdown(context.TODO(), cnf, true)
})
// We want to ensure we can write to this database
mysqld.SetReadOnly(false)
} else {
dbconfigs.GlobalDBConfigs.InitWithSocket("")
mysqld.Mysqld = mysqlctl.NewMysqld(&dbconfigs.GlobalDBConfigs)
servenv.OnClose(mysqld.Close)
}
// Tablet configuration and init.
// Send mycnf as nil because vtcombo won't do backups and restores.
//
// Also force the `--tablet_manager_protocol` and `--tablet_protocol` flags
// to be the "internal" protocol that InitTabletMap registers.
flags.Set("tablet_manager_protocol", "internal")
flags.Set("tablet_protocol", "internal")
uid, err := vtcombo.InitTabletMap(ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, *schemaDir, *startMysql)
if err != nil {
log.Errorf("initTabletMapProto failed: %v", err)
// ensure we start mysql in the event we fail here
if *startMysql {
mysqld.Shutdown(context.TODO(), cnf, true)
}
exit.Return(1)
}
globalCreateDb = func(ctx context.Context, ks *vttestpb.Keyspace) error {
// Check if we're recreating a keyspace that was previously deleted by looking
// at the original topology definition.
//
// If we find a matching keyspace, we create it with the same sharding
// configuration. This ensures that dropping and recreating a keyspace
// will end up with the same number of shards.
for _, originalKs := range originalTopology.Keyspaces {
if originalKs.Name == ks.Name {
ks = proto.Clone(originalKs).(*vttestpb.Keyspace)
}
}
wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil)
newUID, err := vtcombo.CreateKs(ctx, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, *schemaDir, ks, true, uid, wr)
if err != nil {
return err
}
uid = newUID
tpb.Keyspaces = append(tpb.Keyspaces, ks)
return nil
}
globalDropDb = func(ctx context.Context, ksName string) error {
if err := vtcombo.DeleteKs(ctx, ts, ksName, mysqld, &tpb); err != nil {
return err
}
// Rebuild the SrvVSchema object
if err := ts.RebuildSrvVSchema(ctx, tpb.Cells); err != nil {
return err
}
return nil
}
// Now that we have fully initialized the tablets, rebuild the keyspace graph.
for _, ks := range tpb.Keyspaces {
err := topotools.RebuildKeyspace(context.Background(), logutil.NewConsoleLogger(), ts, ks.GetName(), tpb.Cells, false)
if err != nil {
if *startMysql {
mysqld.Shutdown(context.TODO(), cnf, true)
}
log.Fatalf("Couldn't build srv keyspace for (%v: %v). Got error: %v", ks, tpb.Cells, err)
}
}
// vtgate configuration and init
resilientServer = srvtopo.NewResilientServer(ts, "ResilientSrvTopoServer")
tabletTypesToWait := []topodatapb.TabletType{
topodatapb.TabletType_PRIMARY,
topodatapb.TabletType_REPLICA,
topodatapb.TabletType_RDONLY,
}
plannerVersion, _ := plancontext.PlannerNameToVersion(*plannerName)
vtgate.QueryLogHandler = "/debug/vtgate/querylog"
vtgate.QueryLogzHandler = "/debug/vtgate/querylogz"
vtgate.QueryzHandler = "/debug/vtgate/queryz"
// pass nil for healthcheck, it will get created
vtg := vtgate.Init(context.Background(), nil, resilientServer, tpb.Cells[0], tabletTypesToWait, plannerVersion)
// vtctld configuration and init
err = vtctld.InitVtctld(ts)
if err != nil {
exit.Return(1)
}
servenv.OnRun(func() {
addStatusParts(vtg)
})
servenv.OnTerm(func() {
log.Error("Terminating")
// FIXME(alainjobart): stop vtgate
})
servenv.OnClose(func() {
// We will still use the topo server during lameduck period
// to update our state, so closing it in OnClose()
ts.Close()
})
servenv.RunDefault()
}
// vtcomboMysqld is a wrapper on top of mysqlctl.Mysqld.
// We need this wrapper because vtcombo runs with a single MySQL instance
// which all the tablets connect to. (replica, primary, all). This means that we shouldn't
// be trying to run any replication related commands on it, otherwise they fail.
type vtcomboMysqld struct {
*mysqlctl.Mysqld
}
// SetReplicationSource implements the MysqlDaemon interface
func (mysqld *vtcomboMysqld) SetReplicationSource(ctx context.Context, host string, port int32, stopReplicationBefore bool, startReplicationAfter bool) error {
return nil
}
// StartReplication implements the MysqlDaemon interface
func (mysqld *vtcomboMysqld) StartReplication(hookExtraEnv map[string]string) error {
return nil
}
// RestartReplication implements the MysqlDaemon interface
func (mysqld *vtcomboMysqld) RestartReplication(hookExtraEnv map[string]string) error {
return nil
}
// StartReplicationUntilAfter implements the MysqlDaemon interface
func (mysqld *vtcomboMysqld) StartReplicationUntilAfter(ctx context.Context, pos mysql.Position) error {
return nil
}
// StopReplication implements the MysqlDaemon interface
func (mysqld *vtcomboMysqld) StopReplication(hookExtraEnv map[string]string) error {
return nil
}
// SetSemiSyncEnabled implements the MysqlDaemon interface
func (mysqld *vtcomboMysqld) SetSemiSyncEnabled(source, replica bool) error {
return nil
}
// SemiSyncExtensionLoaded implements the MysqlDaemon interface
func (mysqld *vtcomboMysqld) SemiSyncExtensionLoaded() (bool, error) {
return true, nil
}