-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mycnf_gen.go
182 lines (163 loc) · 6.24 KB
/
mycnf_gen.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
/*
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.
*/
// Generate my.cnf files from templates.
package mysqlctl
import (
"bytes"
"crypto/rand"
"fmt"
"io/ioutil"
"math/big"
"path"
"text/template"
"flag"
"vitess.io/vitess/go/vt/env"
)
// This files handles the creation of Mycnf objects for the default 'vt'
// file structure. These path are used by the mysqlctl commands.
//
// The default 'vt' file structure is as follows:
// - the root is specified by the environment variable VTDATAROOT,
// and defaults to /vt
// - each tablet with uid NNNNNNNNNN is located in <root>/vt_NNNNNNNNNN
// - in that tablet directory, there is a my.cnf file for the mysql instance,
// and 'data', 'innodb', 'relay-logs', 'bin-logs' directories.
// - these sub-directories might be symlinks to other places,
// see comment for createTopDir, to allow some data types to be on
// different disk partitions.
const (
dataDir = "data"
innodbDir = "innodb"
relayLogDir = "relay-logs"
binLogDir = "bin-logs"
innodbDataSubdir = "innodb/data"
innodbLogSubdir = "innodb/logs"
)
var (
tabletDir = flag.String("tablet_dir", "", "The directory within the vtdataroot to store vttablet/mysql files. Defaults to being generated by the tablet uid.")
)
// NewMycnf fills the Mycnf structure with vt root paths and derived values.
// This is used to fill out the cnfTemplate values and generate my.cnf.
// uid is a unique id for a particular tablet - it must be unique within the
// tabletservers deployed within a keyspace, lest there be collisions on disk.
// mysqldPort needs to be unique per instance per machine.
func NewMycnf(tabletUID uint32, mysqlPort int32) *Mycnf {
cnf := new(Mycnf)
cnf.path = MycnfFile(tabletUID)
tabletDir := TabletDir(tabletUID)
cnf.ServerID = tabletUID
cnf.MysqlPort = mysqlPort
cnf.DataDir = path.Join(tabletDir, dataDir)
cnf.InnodbDataHomeDir = path.Join(tabletDir, innodbDataSubdir)
cnf.InnodbLogGroupHomeDir = path.Join(tabletDir, innodbLogSubdir)
cnf.SocketFile = path.Join(tabletDir, "mysql.sock")
cnf.GeneralLogPath = path.Join(tabletDir, "general.log")
cnf.ErrorLogPath = path.Join(tabletDir, "error.log")
cnf.SlowLogPath = path.Join(tabletDir, "slow-query.log")
cnf.RelayLogPath = path.Join(tabletDir, relayLogDir,
fmt.Sprintf("vt-%010d-relay-bin", tabletUID))
cnf.RelayLogIndexPath = cnf.RelayLogPath + ".index"
cnf.RelayLogInfoPath = path.Join(tabletDir, relayLogDir, "relay-log.info")
cnf.BinLogPath = path.Join(tabletDir, binLogDir,
fmt.Sprintf("vt-%010d-bin", tabletUID))
cnf.MasterInfoFile = path.Join(tabletDir, "master.info")
cnf.PidFile = path.Join(tabletDir, "mysql.pid")
cnf.TmpDir = path.Join(tabletDir, "tmp")
cnf.SlaveLoadTmpDir = cnf.TmpDir
return cnf
}
// TabletDir returns the default directory for a tablet
func TabletDir(uid uint32) string {
if *tabletDir != "" {
return fmt.Sprintf("%s/%s", env.VtDataRoot(), *tabletDir)
}
return fmt.Sprintf("%s/vt_%010d", env.VtDataRoot(), uid)
}
// MycnfFile returns the default location of the my.cnf file.
func MycnfFile(uid uint32) string {
return path.Join(TabletDir(uid), "my.cnf")
}
// TopLevelDirs returns the list of directories in the toplevel tablet directory
// that might be located in a different place.
func TopLevelDirs() []string {
return []string{dataDir, innodbDir, relayLogDir, binLogDir}
}
// directoryList returns the list of directories to create in an empty
// mysql instance.
func (cnf *Mycnf) directoryList() []string {
return []string{
cnf.DataDir,
cnf.InnodbDataHomeDir,
cnf.InnodbLogGroupHomeDir,
cnf.TmpDir,
path.Dir(cnf.RelayLogPath),
path.Dir(cnf.BinLogPath),
}
}
// makeMycnf will join cnf files cnfPaths and substitute in the right values.
func (cnf *Mycnf) makeMycnf(cnfFiles []string) (string, error) {
myTemplateSource := new(bytes.Buffer)
myTemplateSource.WriteString("[mysqld]\n")
for _, path := range cnfFiles {
data, dataErr := ioutil.ReadFile(path)
if dataErr != nil {
return "", dataErr
}
myTemplateSource.WriteString("## " + path + "\n")
myTemplateSource.Write(data)
}
return cnf.fillMycnfTemplate(myTemplateSource.String())
}
// fillMycnfTemplate will fill in the passed in template with the values
// from Mycnf
func (cnf *Mycnf) fillMycnfTemplate(tmplSrc string) (string, error) {
myTemplate, err := template.New("").Parse(tmplSrc)
if err != nil {
return "", err
}
mycnfData := new(bytes.Buffer)
err = myTemplate.Execute(mycnfData, cnf)
if err != nil {
return "", err
}
return mycnfData.String(), nil
}
// RandomizeMysqlServerID generates a random MySQL server_id.
//
// The value assigned to ServerID will be in the range [100, 2^31):
// - It avoids 0 because that's reserved for mysqlbinlog dumps.
// - It also avoids 1-99 because low numbers are used for fake slave
// connections. See NewSlaveConnection() in binlog/slave_connection.go
// for more on that.
// - It avoids the 2^31 - 2^32-1 range, as there seems to be some
// confusion there. The main MySQL documentation at:
// http://dev.mysql.com/doc/refman/5.7/en/replication-options.html
// implies serverID is a full 32 bits number. The semi-sync log line
// at startup '[Note] Start semi-sync binlog_dump to slave ...'
// interprets the server_id as signed 32-bit (shows negative numbers
// for that range).
// Such an ID may also be responsible for a mysqld crash in semi-sync code,
// although we haven't been able to verify that yet. The issue for that is:
// https://github.com/vitessio/vitess/issues/2280
func (cnf *Mycnf) RandomizeMysqlServerID() error {
// rand.Int(_, max) returns a value in the range [0, max).
bigN, err := rand.Int(rand.Reader, big.NewInt(1<<31-100))
if err != nil {
return err
}
n := bigN.Uint64()
// n is in the range [0, 2^31 - 100).
// Add back 100 to put it in the range [100, 2^31).
cnf.ServerID = uint32(n + 100)
return nil
}