Skip to content

Commit 809908c

Browse files
author
Shlomi Noach
authored
Merge branch 'master' into ipv6
2 parents 6284a34 + d5c374a commit 809908c

File tree

195 files changed

+13064
-4233
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+13064
-4233
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# http://docs.travis-ci.com/user/languages/go/
22
language: go
33

4-
go: 1.9
4+
go:
5+
- "1.9"
6+
- "1.10"
57

68
os:
79
- linux

RELEASE_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.46
1+
1.0.47

doc/command-line-flags.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ This is somewhat similar to a Nagios `n`-times test, where `n` in our case is al
6969

7070
Optional. Default is `safe`. See more discussion in [`cut-over`](cut-over.md)
7171

72+
### cut-over-lock-timeout-seconds
73+
74+
Default `3`. Max number of seconds to hold locks on tables while attempting to cut-over (retry attempted when lock exceeds timeout).
75+
7276
### discard-foreign-keys
7377

7478
**Danger**: this flag will _silently_ discard any foreign keys existing on your table.
@@ -107,6 +111,10 @@ While the ongoing estimated number of rows is still heuristic, it's almost exact
107111

108112
Without this parameter, migration is a _noop_: testing table creation and validity of migration, but not touching data.
109113

114+
### force-table-names
115+
116+
Table name prefix to be used on the temporary tables.
117+
110118
### gcp
111119

112120
Add this flag when executing on a 1st generation Google Cloud Platform (GCP).
@@ -125,6 +133,10 @@ We think `gh-ost` should not take chances or make assumptions about the user's t
125133

126134
See [`initially-drop-ghost-table`](#initially-drop-ghost-table)
127135

136+
### initially-drop-socket-file
137+
138+
Default False. Should `gh-ost` forcibly delete an existing socket file. Be careful: this might drop the socket file of a running migration!
139+
128140
### max-lag-millis
129141

130142
On a replication topology, this is perhaps the most important migration throttling factor: the maximum lag allowed for migration to work. If lag exceeds this value, migration throttles.
@@ -169,6 +181,10 @@ See [`approve-renamed-columns`](#approve-renamed-columns)
169181

170182
Issue the migration on a replica; do not modify data on master. Useful for validating, testing and benchmarking. See [`testing-on-replica`](testing-on-replica.md)
171183

184+
### test-on-replica-skip-replica-stop
185+
186+
Default `False`. When `--test-on-replica` is enabled, do not issue commands stop replication (requires `--test-on-replica`).
187+
172188
### throttle-control-replicas
173189

174190
Provide a command delimited list of replicas; `gh-ost` will throttle when any of the given replicas lag beyond [`--max-lag-millis`](#max-lag-millis). The list can be queried and updated dynamically via [interactive commands](interactive-commands.md)

doc/hooks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ The following variables are available on all hooks:
6969
- `GH_OST_INSPECTED_HOST`
7070
- `GH_OST_EXECUTING_HOST`
7171
- `GH_OST_HOOKS_HINT` - copy of `--hooks-hint` value
72+
- `GH_OST_DRY_RUN` - whether or not the `gh-ost` run is a dry run
7273

7374
The following variable are available on particular hooks:
7475

doc/shared-key.md

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Shared key
22

3-
A requirement for a migration to run is that the two _before_ and _after_ tables have a shared unique key. This is to elaborate and illustrate on the matter.
3+
gh-ost requires for every migration that both the _before_ and _after_ versions of the table share the same unique not-null key columns. This page illustrates this rule.
44

55
### Introduction
66

7-
Consider a classic, simple migration. The table is any normal:
7+
Consider a simple migration, with a normal table,
88

9-
```
9+
```sql
1010
CREATE TABLE tbl (
1111
id bigint unsigned not null auto_increment,
1212
data varchar(255),
@@ -15,54 +15,72 @@ CREATE TABLE tbl (
1515
)
1616
```
1717

18-
And the migration is a simple `add column ts timestamp`.
18+
and the migration `add column ts timestamp`. The _after_ table version would be:
1919

20-
In such migration there is no change in indexes, and in particular no change to any unique key, and specifically no change to the `PRIMARY KEY`. To run this migration, `gh-ost` would iterate the `tbl` table using the primary key, copy rows from `tbl` to the _ghost_ table `_tbl_gho` by order of `id`, and then apply binlog events onto `_tbl_gho`.
20+
```sql
21+
CREATE TABLE tbl (
22+
id bigint unsigned not null auto_increment,
23+
data varchar(255),
24+
more_data int,
25+
ts timestamp,
26+
PRIMARY KEY(id)
27+
)
28+
```
2129

22-
Applying the binlog events assumes the existence of a shared unique key. For example, an `UPDATE` statement in the binary log translate to a `REPLACE` statement which `gh-ost` applies to the _ghost_ table. Such statement expects to add or replace an existing row based on given row data. In particular, it would _replace_ an existing row if a unique key violation is met.
30+
(This is also the definition of the _ghost_ table, except that that table would be called `_tbl_gho`).
2331

24-
So `gh-ost` correlates `tbl` and `_tbl_gho` rows using a unique key. In the above example that would be the `PRIMARY KEY`.
32+
In this migration, the _before_ and _after_ versions contain the same unique not-null key (the PRIMARY KEY). To run this migration, `gh-ost` would iterate through the `tbl` table using the primary key, copy rows from `tbl` to the _ghost_ table `_tbl_gho` in primary key order, while also applying the binlog event writes from `tble` onto `_tbl_gho`.
2533

26-
### Rules
34+
The applying of the binlog events is what requires the shared unique key. For example, an `UPDATE` statement to `tbl` translates to a `REPLACE` statement which `gh-ost` applies to `_tbl_gho`. A `REPLACE` statement expects to insert or replace an existing row based on its row's values and the table's unique key constraints. In particular, if inserting that row would result in a unique key violation (e.g., a row with that primary key already exists), it would _replace_ that existing row with the new values.
2735

28-
There must be a shared set of not-null columns for which there is a unique constraint in both the original table and the migration (_ghost_) table.
36+
So `gh-ost` correlates `tbl` and `_tbl_gho` rows one to one using a unique key. In the above example that would be the `PRIMARY KEY`.
2937

30-
### Interpreting the rules
38+
### Interpreting the rule
3139

32-
The same columns must be covered by a unique key in both tables. This doesn't have to be the `PRIMARY KEY`. This doesn't have to be a key of the same name.
40+
The _before_ and _after_ versions of the table share the same unique not-null key, but:
41+
- the key doesn't have to be the PRIMARY KEY
42+
- the key can have a different name between the _before_ and _after_ versions (e.g., renamed via DROP INDEX and ADD INDEX) so long as it contains the exact same column(s)
3343

34-
Upon migration, `gh-ost` inspects both the original and _ghost_ table and attempts to find at least one such unique key (or rather, a set of columns) that is shared between the two. Typically this would just be the `PRIMARY KEY`, but sometimes you may change the `PRIMARY KEY` itself, in which case `gh-ost` will look for other options.
44+
At the start of the migration, `gh-ost` inspects both the original and _ghost_ table it created, and attempts to find at least one such unique key (or rather, a set of columns) that is shared between the two. Typically this would just be the `PRIMARY KEY`, but some tables don't have primary keys, or sometimes it is the primary key that is being modified by the migration. In these cases `gh-ost` will look for other options.
3545

36-
`gh-ost` expects unique keys where no `NULL` values are found, i.e. all columns covered by the unique key are defined as `NOT NULL`. This is implicitly true for `PRIMARY KEY`s. If no such key can be found, `gh-ost` bails out. In the event there is no such key, but you happen to _know_ your columns have no `NULL` values even though they're `NULL`-able, you may take responsibility and pass the `--allow-nullable-unique-key`. The migration will run well as long as no `NULL` values are found in the unique key's columns. Any actual `NULL`s may corrupt the migration.
46+
`gh-ost` expects unique keys where no `NULL` values are found, i.e. all columns contained in the unique key are defined as `NOT NULL`. This is implicitly true for primary keys. If no such key can be found, `gh-ost` bails out.
3747

38-
### Examples: allowed and not allowed
48+
If the table contains a unique key with nullable columns, but you know your columns contain no `NULL` values, use the `--allow-nullable-unique-key` option. The migration will run well as long as no `NULL` values are found in the unique key's columns. **Any actual `NULL`s may corrupt the migration.**
3949

40-
```
50+
### Examples: Allowed and Not Allowed
51+
52+
```sql
4153
create table some_table (
42-
id int auto_increment,
54+
id int not null auto_increment,
4355
ts timestamp,
4456
name varchar(128) not null,
4557
owner_id int not null,
46-
loc_id int,
58+
loc_id int not null,
4759
primary key(id),
4860
unique key name_uidx(name)
4961
)
5062
```
5163

52-
Following are examples of migrations that are _good to run_:
64+
Note the two unique, not-null indexes: the primary key and `name_uidx`.
65+
66+
Allowed migrations:
5367

5468
- `add column i int`
55-
- `add key owner_idx(owner_id)`
56-
- `add unique key owner_name_idx(owner_id, name)` - though you need to make sure to not write conflicting rows while this migration runs
69+
- `add key owner_idx (owner_id)`
70+
- `add unique key owner_name_idx (owner_id, name)` - **be careful not to write conflicting rows while this migration runs**
5771
- `drop key name_uidx` - `primary key` is shared between the tables
58-
- `drop primary key, add primary key(owner_id, loc_id)` - `name_uidx` is shared between the tables and is used for migration
59-
- `change id bigint unsigned` - the `'primary key` is used. The change of type still makes the `primary key` workable.
60-
- `drop primary key, drop key name_uidx, create primary key(name), create unique key id_uidx(id)` - swapping the two keys. `gh-ost` is still happy because `id` is still unique in both tables. So is `name`.
72+
- `drop primary key, add primary key(owner_id, loc_id)` - `name_uidx` is shared between the tables
73+
- `change id bigint unsigned not null auto_increment` - the `primary key` changes datatype but not value, and can be used
74+
- `drop primary key, drop key name_uidx, add primary key(name), add unique key id_uidx(id)` - swapping the two keys. Either `id` or `name` could be used
75+
76+
Not allowed:
6177

78+
- `drop primary key, drop key name_uidx` - the _ghost_ table has no unique key
79+
- `drop primary key, drop key name_uidx, create primary key(name, owner_id)` - no shared columns to the unique keys on both tables. Even though `name` exists in the _ghost_ table's `primary key`, it is only part of the key and in itself does not guarantee uniqueness in the _ghost_ table.
6280

63-
Following are examples of migrations that _cannot run_:
6481

65-
- `drop primary key, drop key name_uidx` - no unique key to _ghost_ table, so clearly cannot run
66-
- `drop primary key, drop key name_uidx, create primary key(name, owner_id)` - no shared columns to both tables. Even though `name` exists in the _ghost_ table's `primary key`, it is only part of the key and in itself does not guarantee uniqueness in the _ghost_ table.
82+
### Workarounds
6783

68-
Also, you cannot run a migration on a table that doesn't have some form of `unique key` in the first place, such as `some_table (id int, ts timestamp)`
84+
If you need to change your primary key or only not-null unique index to use different columns, you will want to do it as two separate migrations:
85+
1. `ADD UNIQUE KEY temp_pk (temp_pk_column,...)`
86+
1. `DROP PRIMARY KEY, DROP KEY temp_pk, ADD PRIMARY KEY (temp_pk_column,...)`

doc/throttle.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ Note that you may dynamically change both `--max-lag-millis` and the `throttle-c
4646

4747
An example query could be: `--throttle-query="select hour(now()) between 8 and 17"` which implies throttling auto-starts `8:00am` and migration auto-resumes at `18:00pm`.
4848

49+
#### HTTP Throttle
50+
51+
The `--throttle-http` flag allows for throttling via HTTP. Every 100ms `gh-ost` issues a `HEAD` request to the provided URL. If the response status code is not `200` throttling will kick in until a `200` response status code is returned.
52+
53+
If no URL is provided or the URL provided doesn't contain the scheme then the HTTP check will be disabled. For example `--throttle-http="http://1.2.3.4:6789/throttle"` will enable the HTTP check/throttling, but `--throttle-http="1.2.3.4:6789/throttle"` will not.
54+
55+
The URL can be queried and updated dynamically via [interactive interface](interactive-commands.md).
56+
4957
#### Manual control
5058

5159
In addition to the above, you are able to take control and throttle the operation any time you like.

go/binlog/gomysql_reader.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ func NewGoMySQLReader(migrationContext *base.MigrationContext) (binlogReader *Go
4040
serverId := uint32(migrationContext.ReplicaServerId)
4141

4242
binlogSyncerConfig := replication.BinlogSyncerConfig{
43-
ServerID: serverId,
44-
Flavor: "mysql",
45-
Host: binlogReader.connectionConfig.Key.Hostname,
46-
Port: uint16(binlogReader.connectionConfig.Key.Port),
47-
User: binlogReader.connectionConfig.User,
48-
Password: binlogReader.connectionConfig.Password,
43+
ServerID: serverId,
44+
Flavor: "mysql",
45+
Host: binlogReader.connectionConfig.Key.Hostname,
46+
Port: uint16(binlogReader.connectionConfig.Key.Port),
47+
User: binlogReader.connectionConfig.User,
48+
Password: binlogReader.connectionConfig.Password,
49+
UseDecimal: true,
4950
}
5051
binlogReader.binlogSyncer = replication.NewBinlogSyncer(binlogSyncerConfig)
5152

go/logic/hooks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (this *HooksExecutor) applyEnvironmentVariables(extraVariables ...string) [
6464
env = append(env, fmt.Sprintf("GH_OST_INSPECTED_HOST=%s", this.migrationContext.GetInspectorHostname()))
6565
env = append(env, fmt.Sprintf("GH_OST_EXECUTING_HOST=%s", this.migrationContext.Hostname))
6666
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT=%s", this.migrationContext.HooksHintMessage))
67+
env = append(env, fmt.Sprintf("GH_OST_DRY_RUN=%t", this.migrationContext.Noop))
6768

6869
for _, variable := range extraVariables {
6970
env = append(env, variable)

go/logic/inspect.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ func (this *Inspector) inspectOriginalAndGhostTables() (err error) {
173173
// This additional step looks at which columns are unsigned. We could have merged this within
174174
// the `getTableColumns()` function, but it's a later patch and introduces some complexity; I feel
175175
// comfortable in doing this as a separate step.
176-
this.applyColumnTypes(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, this.migrationContext.OriginalTableColumns, this.migrationContext.SharedColumns)
177-
this.applyColumnTypes(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, &this.migrationContext.UniqueKey.Columns)
176+
this.applyColumnTypes(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, this.migrationContext.OriginalTableColumns, this.migrationContext.SharedColumns, &this.migrationContext.UniqueKey.Columns)
178177
this.applyColumnTypes(this.migrationContext.DatabaseName, this.migrationContext.GetGhostTableName(), this.migrationContext.GhostTableColumns, this.migrationContext.MappedSharedColumns)
179178

180179
for i := range this.migrationContext.SharedColumns.Columns() {
@@ -233,6 +232,9 @@ func (this *Inspector) validateGrants() error {
233232
if strings.Contains(grant, fmt.Sprintf("GRANT ALL PRIVILEGES ON `%s`.*", this.migrationContext.DatabaseName)) {
234233
foundDBAll = true
235234
}
235+
if strings.Contains(grant, fmt.Sprintf("GRANT ALL PRIVILEGES ON `%s`.*", strings.Replace(this.migrationContext.DatabaseName, "_", "\\_", -1))) {
236+
foundDBAll = true
237+
}
236238
if base.StringContainsAll(grant, `ALTER`, `CREATE`, `DELETE`, `DROP`, `INDEX`, `INSERT`, `LOCK TABLES`, `SELECT`, `TRIGGER`, `UPDATE`, ` ON *.*`) {
237239
foundDBAll = true
238240
}
@@ -552,44 +554,35 @@ func (this *Inspector) applyColumnTypes(databaseName, tableName string, columnsL
552554
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
553555
columnName := m.GetString("COLUMN_NAME")
554556
columnType := m.GetString("COLUMN_TYPE")
555-
if strings.Contains(columnType, "unsigned") {
556-
for _, columnsList := range columnsLists {
557-
columnsList.SetUnsigned(columnName)
557+
for _, columnsList := range columnsLists {
558+
column := columnsList.GetColumn(columnName)
559+
if column == nil {
560+
continue
558561
}
559-
}
560-
if strings.Contains(columnType, "mediumint") {
561-
for _, columnsList := range columnsLists {
562-
columnsList.GetColumn(columnName).Type = sql.MediumIntColumnType
562+
563+
if strings.Contains(columnType, "unsigned") {
564+
column.IsUnsigned = true
563565
}
564-
}
565-
if strings.Contains(columnType, "timestamp") {
566-
for _, columnsList := range columnsLists {
567-
columnsList.GetColumn(columnName).Type = sql.TimestampColumnType
566+
if strings.Contains(columnType, "mediumint") {
567+
column.Type = sql.MediumIntColumnType
568568
}
569-
}
570-
if strings.Contains(columnType, "datetime") {
571-
for _, columnsList := range columnsLists {
572-
columnsList.GetColumn(columnName).Type = sql.DateTimeColumnType
569+
if strings.Contains(columnType, "timestamp") {
570+
column.Type = sql.TimestampColumnType
573571
}
574-
}
575-
if strings.Contains(columnType, "json") {
576-
for _, columnsList := range columnsLists {
577-
columnsList.GetColumn(columnName).Type = sql.JSONColumnType
572+
if strings.Contains(columnType, "datetime") {
573+
column.Type = sql.DateTimeColumnType
578574
}
579-
}
580-
if strings.Contains(columnType, "float") {
581-
for _, columnsList := range columnsLists {
582-
columnsList.GetColumn(columnName).Type = sql.FloatColumnType
575+
if strings.Contains(columnType, "json") {
576+
column.Type = sql.JSONColumnType
583577
}
584-
}
585-
if strings.HasPrefix(columnType, "enum") {
586-
for _, columnsList := range columnsLists {
587-
columnsList.GetColumn(columnName).Type = sql.EnumColumnType
578+
if strings.Contains(columnType, "float") {
579+
column.Type = sql.FloatColumnType
588580
}
589-
}
590-
if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" {
591-
for _, columnsList := range columnsLists {
592-
columnsList.SetCharset(columnName, charset)
581+
if strings.HasPrefix(columnType, "enum") {
582+
column.Type = sql.EnumColumnType
583+
}
584+
if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" {
585+
column.Charset = charset
593586
}
594587
}
595588
return nil

localtests/bit-add/create.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
drop table if exists gh_ost_test;
2+
create table gh_ost_test (
3+
id int auto_increment,
4+
i int not null,
5+
primary key(id)
6+
) auto_increment=1;
7+
8+
drop event if exists gh_ost_test;
9+
delimiter ;;
10+
create event gh_ost_test
11+
on schedule every 1 second
12+
starts current_timestamp
13+
ends current_timestamp + interval 60 second
14+
on completion not preserve
15+
enable
16+
do
17+
begin
18+
insert into gh_ost_test values (null, 11);
19+
insert into gh_ost_test values (null, 13);
20+
end ;;

0 commit comments

Comments
 (0)