forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vttablet.go
145 lines (121 loc) · 4.48 KB
/
vttablet.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
/*
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.
*/
// vt tablet server: Serves queries and performs housekeeping jobs.
package main
import (
"flag"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/tableacl"
"vitess.io/vitess/go/vt/tableacl/simpleacl"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vttablet/tabletmanager"
"vitess.io/vitess/go/vt/vttablet/tabletserver"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
)
var (
enforceTableACLConfig = flag.Bool("enforce-tableacl-config", false, "if this flag is true, vttablet will fail to start if a valid tableacl config does not exist")
tableACLConfig = flag.String("table-acl-config", "", "path to table access checker config file; send SIGHUP to reload this file")
tabletPath = flag.String("tablet-path", "", "tablet alias")
agent *tabletmanager.ActionAgent
)
func init() {
servenv.RegisterDefaultFlags()
}
func main() {
dbconfigs.RegisterFlags(dbconfigs.All...)
mysqlctl.RegisterFlags()
servenv.ParseFlags("vttablet")
if err := tabletenv.VerifyConfig(); err != nil {
log.Exitf("invalid config: %v", err)
}
tabletenv.Init()
servenv.Init()
if *tabletPath == "" {
log.Exit("tabletPath required")
}
tabletAlias, err := topoproto.ParseTabletAlias(*tabletPath)
if err != nil {
log.Exitf("failed to parse -tablet-path: %v", err)
}
if tabletAlias.Uid < 0 {
log.Exitf("invalid tablet id: %d", tabletAlias.Uid)
}
var mycnf *mysqlctl.Mycnf
var socketFile string
// If no connection parameters were specified, load the mycnf file
// and use the socket from it. If connection parameters were specified,
// we assume that the mysql is not local, and we skip loading mycnf.
// This also means that backup and restore will not be allowed.
if !dbconfigs.HasConnectionParams() {
var err error
if mycnf, err = mysqlctl.NewMycnfFromFlags(tabletAlias.Uid); err != nil {
log.Exitf("mycnf read failed: %v", err)
}
socketFile = mycnf.SocketFile
} else {
log.Info("connection parameters were specified. Not loading my.cnf.")
}
// If connection parameters were specified, socketFile will be empty.
// Otherwise, the socketFile (read from mycnf) will be used to initialize
// dbconfigs.
dbcfgs, err := dbconfigs.Init(socketFile)
if err != nil {
log.Warning(err)
}
if *tableACLConfig != "" {
// To override default simpleacl, other ACL plugins must set themselves to be default ACL factory
tableacl.Register("simpleacl", &simpleacl.Factory{})
} else if *enforceTableACLConfig {
log.Exit("table acl config has to be specified with table-acl-config flag because enforce-tableacl-config is set.")
}
// creates and registers the query service
ts := topo.Open()
qsc := tabletserver.NewServer(ts, *tabletAlias)
servenv.OnRun(func() {
qsc.Register()
addStatusParts(qsc)
})
servenv.OnClose(func() {
// We now leave the queryservice running during lameduck,
// so stop it in OnClose(), after lameduck is over.
qsc.StopService()
})
qsc.InitACL(*tableACLConfig, *enforceTableACLConfig)
// Create mysqld and register the health reporter (needs to be done
// before initializing the agent, so the initial health check
// done by the agent has the right reporter)
mysqld := mysqlctl.NewMysqld(dbcfgs)
servenv.OnClose(mysqld.Close)
// Depends on both query and updateStream.
gRPCPort := int32(0)
if servenv.GRPCPort != nil {
gRPCPort = int32(*servenv.GRPCPort)
}
agent, err = tabletmanager.NewActionAgent(context.Background(), ts, mysqld, qsc, tabletAlias, dbcfgs, mycnf, int32(*servenv.Port), gRPCPort)
if err != nil {
log.Exitf("NewActionAgent() failed: %v", err)
}
servenv.OnClose(func() {
// stop the agent so that our topo entry gets pruned properly
agent.Close()
// We will still use the topo server during lameduck period
// to update our state, so closing it in OnClose()
ts.Close()
})
servenv.RunDefault()
}