-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mysqlctl_process.go
216 lines (191 loc) · 7.21 KB
/
mysqlctl_process.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
/*
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.
*/
package cluster
import (
"context"
"fmt"
"html/template"
"os"
"os/exec"
"path"
"strings"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/tlstest"
)
// MysqlctlProcess is a generic handle for a running mysqlctl command .
// It can be spawned manually
type MysqlctlProcess struct {
Name string
Binary string
LogDirectory string
TabletUID int
MySQLPort int
InitDBFile string
ExtraArgs []string
InitMysql bool
SecureTransport bool
}
// InitDb executes mysqlctl command to add cell info
func (mysqlctl *MysqlctlProcess) InitDb() (err error) {
args := []string{"-log_dir", mysqlctl.LogDirectory,
"-tablet_uid", fmt.Sprintf("%d", mysqlctl.TabletUID),
"-mysql_port", fmt.Sprintf("%d", mysqlctl.MySQLPort),
"init",
"-init_db_sql_file", mysqlctl.InitDBFile}
if *isCoverage {
args = append([]string{"-test.coverprofile=" + getCoveragePath("mysql-initdb.out"), "-test.v"}, args...)
}
tmpProcess := exec.Command(
mysqlctl.Binary,
args...)
return tmpProcess.Run()
}
// Start executes mysqlctl command to start mysql instance
func (mysqlctl *MysqlctlProcess) Start() (err error) {
tmpProcess, err := mysqlctl.StartProcess()
if err != nil {
return err
}
return tmpProcess.Wait()
}
// StartProcess starts the mysqlctl and returns the process reference
func (mysqlctl *MysqlctlProcess) StartProcess() (*exec.Cmd, error) {
tmpProcess := exec.Command(
mysqlctl.Binary,
"-log_dir", mysqlctl.LogDirectory,
"-tablet_uid", fmt.Sprintf("%d", mysqlctl.TabletUID),
"-mysql_port", fmt.Sprintf("%d", mysqlctl.MySQLPort),
)
if *isCoverage {
tmpProcess.Args = append(tmpProcess.Args, []string{"-test.coverprofile=" + getCoveragePath("mysql-start.out")}...)
}
if len(mysqlctl.ExtraArgs) > 0 {
tmpProcess.Args = append(tmpProcess.Args, mysqlctl.ExtraArgs...)
}
if mysqlctl.InitMysql {
if mysqlctl.SecureTransport {
// Set up EXTRA_MY_CNF for ssl
sslPath := path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/ssl_%010d", mysqlctl.TabletUID))
sslPathData := struct {
Dir string
}{
Dir: sslPath,
}
os.MkdirAll(sslPath, 0755)
extraMyCNF := path.Join(sslPath, "ssl.cnf")
fout, err := os.Create(extraMyCNF)
if err != nil {
log.Error(err)
return nil, err
}
template.Must(template.New(fmt.Sprintf("%010d", mysqlctl.TabletUID)).Parse(`
ssl_ca={{.Dir}}/ca-cert.pem
ssl_cert={{.Dir}}/server-001-cert.pem
ssl_key={{.Dir}}/server-001-key.pem
`)).Execute(fout, sslPathData)
if err := fout.Close(); err != nil {
return nil, err
}
tlstest.CreateClientServerCertPairs(sslPath)
tmpProcess.Env = append(tmpProcess.Env, "EXTRA_MY_CNF="+extraMyCNF)
tmpProcess.Env = append(tmpProcess.Env, "VTDATAROOT="+os.Getenv("VTDATAROOT"))
}
tmpProcess.Args = append(tmpProcess.Args, "init",
"-init_db_sql_file", mysqlctl.InitDBFile)
}
tmpProcess.Args = append(tmpProcess.Args, "start")
log.Infof("Starting mysqlctl with command: %v", tmpProcess.Args)
return tmpProcess, tmpProcess.Start()
}
// Stop executes mysqlctl command to stop mysql instance
func (mysqlctl *MysqlctlProcess) Stop() (err error) {
log.Infof("Shutting down MySQL: %d", mysqlctl.TabletUID)
defer log.Infof("MySQL shutdown complete: %d", mysqlctl.TabletUID)
tmpProcess, err := mysqlctl.StopProcess()
if err != nil {
return err
}
return tmpProcess.Wait()
}
// StopProcess executes mysqlctl command to stop mysql instance and returns process reference
func (mysqlctl *MysqlctlProcess) StopProcess() (*exec.Cmd, error) {
tmpProcess := exec.Command(
mysqlctl.Binary,
"-tablet_uid", fmt.Sprintf("%d", mysqlctl.TabletUID),
)
if *isCoverage {
tmpProcess.Args = append(tmpProcess.Args, []string{"-test.coverprofile=" + getCoveragePath("mysql-stop.out")}...)
}
if len(mysqlctl.ExtraArgs) > 0 {
tmpProcess.Args = append(tmpProcess.Args, mysqlctl.ExtraArgs...)
}
tmpProcess.Args = append(tmpProcess.Args, "shutdown")
return tmpProcess, tmpProcess.Start()
}
// CleanupFiles clean the mysql files to make sure we can start the same process again
func (mysqlctl *MysqlctlProcess) CleanupFiles(tabletUID int) {
os.RemoveAll(path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d/data", tabletUID)))
os.RemoveAll(path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d/relay-logs", tabletUID)))
os.RemoveAll(path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d/tmp", tabletUID)))
os.RemoveAll(path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d/bin-logs", tabletUID)))
os.RemoveAll(path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d/innodb", tabletUID)))
}
// MysqlCtlProcessInstanceOptionalInit returns a Mysqlctl handle for mysqlctl process
// configured with the given Config.
func MysqlCtlProcessInstanceOptionalInit(tabletUID int, mySQLPort int, tmpDirectory string, initMySQL bool) *MysqlctlProcess {
mysqlctl := &MysqlctlProcess{
Name: "mysqlctl",
Binary: "mysqlctl",
LogDirectory: tmpDirectory,
InitDBFile: path.Join(os.Getenv("VTROOT"), "/config/init_db.sql"),
}
mysqlctl.MySQLPort = mySQLPort
mysqlctl.TabletUID = tabletUID
mysqlctl.InitMysql = initMySQL
mysqlctl.SecureTransport = false
return mysqlctl
}
// MysqlCtlProcessInstance returns a Mysqlctl handle for mysqlctl process
// configured with the given Config.
func MysqlCtlProcessInstance(tabletUID int, mySQLPort int, tmpDirectory string) *MysqlctlProcess {
return MysqlCtlProcessInstanceOptionalInit(tabletUID, mySQLPort, tmpDirectory, true)
}
// StartMySQL starts mysqlctl process
func StartMySQL(ctx context.Context, tablet *Vttablet, username string, tmpDirectory string) error {
tablet.MysqlctlProcess = *MysqlCtlProcessInstance(tablet.TabletUID, tablet.MySQLPort, tmpDirectory)
return tablet.MysqlctlProcess.Start()
}
// StartMySQLAndGetConnection create a connection to tablet mysql
func StartMySQLAndGetConnection(ctx context.Context, tablet *Vttablet, username string, tmpDirectory string) (*mysql.Conn, error) {
tablet.MysqlctlProcess = *MysqlCtlProcessInstance(tablet.TabletUID, tablet.MySQLPort, tmpDirectory)
err := tablet.MysqlctlProcess.Start()
if err != nil {
return nil, err
}
params := mysql.ConnParams{
Uname: username,
UnixSocket: path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d", tablet.TabletUID), "/mysql.sock"),
}
return mysql.Connect(ctx, ¶ms)
}
// ExecuteCommandWithOutput executes any mysqlctl command and returns output
func (mysqlctl *MysqlctlProcess) ExecuteCommandWithOutput(args ...string) (result string, err error) {
tmpProcess := exec.Command(
mysqlctl.Binary,
args...,
)
log.Info(fmt.Sprintf("Executing mysqlctl with arguments %v", strings.Join(tmpProcess.Args, " ")))
resultByte, err := tmpProcess.CombinedOutput()
return string(resultByte), err
}