forked from vitessio/vitess
/
mysqlctld.go
125 lines (105 loc) · 3.67 KB
/
mysqlctld.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
// Copyright 2014, 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.
// mysqlctld is a daemon that starts or initializes mysqld and provides an RPC
// interface for vttablet to stop and start mysqld from a different container
// without having to restart the container running mysqlctld.
package main
import (
"flag"
"os"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/exit"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/servenv"
"golang.org/x/net/context"
)
var (
// mysqld is used by the rpc implementation plugin.
mysqld *mysqlctl.Mysqld
mysqlPort = flag.Int("mysql_port", 3306, "mysql port")
tabletUID = flag.Uint("tablet_uid", 41983, "tablet uid")
mysqlSocket = flag.String("mysql_socket", "", "path to the mysql socket")
// mysqlctl init flags
waitTime = flag.Duration("wait_time", 5*time.Minute, "how long to wait for mysqld startup or shutdown")
initDBSQLFile = flag.String("init_db_sql_file", "", "path to .sql file to run after mysql_install_db")
)
func init() {
servenv.RegisterDefaultFlags()
servenv.RegisterDefaultSocketFileFlags()
}
func main() {
defer exit.Recover()
defer logutil.Flush()
// mysqlctld only starts and stops mysql, only needs dba.
dbconfigFlags := dbconfigs.DbaConfig
dbconfigs.RegisterFlags(dbconfigFlags)
flag.Parse()
// We'll register this OnTerm handler before mysqld starts, so we get notified
// if mysqld dies on its own without us (or our RPC client) telling it to.
mysqldTerminated := make(chan struct{})
onTermFunc := func() {
close(mysqldTerminated)
}
// Start or Init mysqld as needed.
ctx, cancel := context.WithTimeout(context.Background(), *waitTime)
mycnfFile := mysqlctl.MycnfFile(uint32(*tabletUID))
if _, statErr := os.Stat(mycnfFile); os.IsNotExist(statErr) {
// Generate my.cnf from scratch and use it to find mysqld.
log.Infof("mycnf file (%s) doesn't exist, initializing", mycnfFile)
var err error
mysqld, err = mysqlctl.CreateMysqld(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort), dbconfigFlags)
if err != nil {
log.Errorf("failed to initialize mysql config: %v", err)
exit.Return(1)
}
mysqld.OnTerm(onTermFunc)
if err := mysqld.Init(ctx, *initDBSQLFile); err != nil {
log.Errorf("failed to initialize mysql data dir and start mysqld: %v", err)
exit.Return(1)
}
} else {
// There ought to be an existing my.cnf, so use it to find mysqld.
log.Infof("mycnf file (%s) already exists, starting without init", mycnfFile)
var err error
mysqld, err = mysqlctl.OpenMysqld(uint32(*tabletUID), dbconfigFlags)
if err != nil {
log.Errorf("failed to find mysql config: %v", err)
exit.Return(1)
}
mysqld.OnTerm(onTermFunc)
err = mysqld.RefreshConfig()
if err != nil {
log.Errorf("failed to refresh config: %v", err)
exit.Return(1)
}
if err := mysqld.Start(ctx); err != nil {
log.Errorf("failed to start mysqld: %v", err)
exit.Return(1)
}
}
cancel()
servenv.Init()
defer servenv.Close()
// Take mysqld down with us on SIGTERM before entering lame duck.
servenv.OnTermSync(func() {
log.Infof("mysqlctl received SIGTERM, shutting down mysqld first")
ctx := context.Background()
mysqld.Shutdown(ctx, true)
})
// Start RPC server and wait for SIGTERM.
mysqlctldTerminated := make(chan struct{})
go func() {
servenv.RunDefault()
close(mysqlctldTerminated)
}()
select {
case <-mysqldTerminated:
log.Infof("mysqld shut down on its own, exiting mysqlctld")
case <-mysqlctldTerminated:
log.Infof("mysqlctld shut down gracefully")
}
}