Applying, auditing, and controlling Online DDL

<< Online DDL strategies Declarative migrations >>

Vitess provides two interfaces to interacting with Online DDL:

  • SQL commands, via VTGate
  • Command line interface, via vtctl

Supported interactions are:

Running migrations

To run a managed schema migration, you should:

  • Formulate your DDLs (CREATE, ALTER, DROP) queries
  • Choose a ddl_strategy

When the user submits an online DDL, Vitess responds with a UUID, a job Id used to later track or control the migration. The migration does not start immediately. It is queued at the tablets and executed at some point in the future.

Via VTGate/SQL

  1. mysql> set @@ddl_strategy='online';
  2. mysql> alter table corder add column ts timestamp not null default current_timestamp;
  3. +--------------------------------------+
  4. | uuid |
  5. +--------------------------------------+
  6. | bf4598ab_8d55_11eb_815f_f875a4d24e90 |
  7. +--------------------------------------+
  8. mysql> drop table customer;
  9. +--------------------------------------+
  10. | uuid |
  11. +--------------------------------------+
  12. | 6848c1a4_8d57_11eb_815f_f875a4d24e90 |
  13. +--------------------------------------+
  • @@ddl_strategy behaves like a MySQL session variable, though is only recognized by VTGate. Setting @@ddl_strategy only applies to that same connection and does not affect other connections. The strategy applies to all migrations executed in that session. You may subsequently set @@ddl_strategy to different value.
  • If you run vtgate without -ddl_strategy, then @@ddl_strategy defaults to 'direct', which implies schema migrations are synchronous. You will need to set @@ddl_strategy='gh-ost' to run followup ALTER TABLE statements via gh-ost.
  • If you run vtgate -ddl_strategy "gh-ost", then @@ddl_strategy defaults to 'gh-ost' in each new session. Any ALTER TABLE will run via gh-ost. You may set @@ddl_strategy='pt-osc' to make migrations run through pt-online-schema-change, or set @@ddl_strategy='direct' to run migrations synchronously.

Via vtctl/ApplySchema

You may use vtctl or vtctlclient (the two are interchangeable for the purpose of this document) to apply schema changes. The ApplySchema command supports both synchronous and online schema migrations. To run an online schema migration you will supply the -ddl_strategy command line flag:

  1. $ vtctlclient ApplySchema -ddl_strategy "online" -sql "ALTER TABLE demo MODIFY id bigint UNSIGNED" commerce
  2. a2994c92_f1d4_11ea_afa3_f875a4d24e90

Tracking migrations

You may track the status of a single or of multiple migrations. Since migrations run asycnhronously, it is the user’s responsibility to audit the progress and state of submitted migrations. Users are likely to want to know when a migration is complete (or failed) so as to be able to deploy code changes or run other operations.

Common patterns are:

  • Show state of a specific migration
  • Show all running, complete or failed migrations
  • Show recent migrations

Via VTGate/SQL

Examples for a single shard cluster:

  1. mysql> show vitess_migrations like 'bf4598ab_8d55_11eb_815f_f875a4d24e90' \G
  2. *************************** 1. row ***************************
  3. id: 23
  4. migration_uuid: bf4598ab_8d55_11eb_815f_f875a4d24e90
  5. keyspace: commerce
  6. shard: 0
  7. mysql_schema: vt_commerce
  8. mysql_table: corder
  9. migration_statement: alter table corder add column ts timestamp not null default current_timestamp()
  10. strategy: online
  11. options:
  12. added_timestamp: 2021-03-25 12:35:01
  13. requested_timestamp: 2021-03-25 12:34:58
  14. ready_timestamp: 2021-03-25 12:35:04
  15. started_timestamp: 2021-03-25 12:35:04
  16. liveness_timestamp: 2021-03-25 12:35:06
  17. completed_timestamp: 2021-03-25 12:35:06
  18. cleanup_timestamp: NULL
  19. migration_status: complete
  20. log_path:
  21. artifacts: _bf4598ab_8d55_11eb_815f_f875a4d24e90_20210325123504_vrepl,
  22. retries: 0
  23. tablet: zone1-0000000100
  24. tablet_failure: 0
  25. progress: 100
  26. migration_context: vtgate:a8352418-8d55-11eb-815f-f875a4d24e90
  27. ddl_action: alter
  28. message:
  29. eta_seconds: 0
  1. mysql> show vitess_migrations like 'complete' \G
  2. ...
  3. *************************** 21. row ***************************
  4. id: 24
  5. migration_uuid: 6848c1a4_8d57_11eb_815f_f875a4d24e90
  6. keyspace: commerce
  7. shard: 0
  8. mysql_schema: vt_commerce
  9. mysql_table: customer
  10. migration_statement: drop table customer
  11. strategy: online
  12. options:
  13. added_timestamp: 2021-03-25 12:46:53
  14. requested_timestamp: 2021-03-25 12:46:51
  15. ready_timestamp: 2021-03-25 12:46:57
  16. started_timestamp: 2021-03-25 12:46:57
  17. liveness_timestamp: 2021-03-25 12:46:57
  18. completed_timestamp: 2021-03-25 12:46:57
  19. cleanup_timestamp: NULL
  20. migration_status: complete
  21. log_path:
  22. artifacts: _vt_HOLD_6848c1a48d5711eb815ff875a4d24e90_20210326104657,
  23. retries: 0
  24. tablet: zone1-0000000100
  25. tablet_failure: 0
  26. progress: 100
  27. migration_context: vtgate:a8352418-8d55-11eb-815f-f875a4d24e90
  28. ddl_action: drop
  29. message:
  30. eta_seconds: 0
  1. mysql> show vitess_migrations where completed_timestamp > now() - interval 1 day;
  2. +----+--------------------------------------+----------+-------+--------------+-------------+---------------------------------------------------------------------------------+----------+---------+---------------------+---------------------+---------------------+---------------------+---------------------+---------------------+-------------------+------------------+----------+-------------------------------------------------------------+---------+------------------+----------------+----------+---------------------------------------------+------------+---------+-------------+
  3. | id | migration_uuid | keyspace | shard | mysql_schema | mysql_table | migration_statement | strategy | options | added_timestamp | requested_timestamp | ready_timestamp | started_timestamp | liveness_timestamp | completed_timestamp | cleanup_timestamp | migration_status | log_path | artifacts | retries | tablet | tablet_failure | progress | migration_context | ddl_action | message | eta_seconds |
  4. +----+--------------------------------------+----------+-------+--------------+-------------+---------------------------------------------------------------------------------+----------+---------+---------------------+---------------------+---------------------+---------------------+---------------------+---------------------+-------------------+------------------+----------+-------------------------------------------------------------+---------+------------------+----------------+----------+---------------------------------------------+------------+---------+-------------+
  5. | 23 | bf4598ab_8d55_11eb_815f_f875a4d24e90 | commerce | 0 | vt_commerce | corder | alter table corder add column ts timestamp not null default current_timestamp() | online | | 2021-03-25 12:35:01 | 2021-03-25 12:34:58 | 2021-03-25 12:35:04 | 2021-03-25 12:35:04 | 2021-03-25 12:35:06 | 2021-03-25 12:35:06 | NULL | complete | | _bf4598ab_8d55_11eb_815f_f875a4d24e90_20210325123504_vrepl, | 0 | zone1-0000000100 | 0 | 100 | vtgate:a8352418-8d55-11eb-815f-f875a4d24e90 | alter | | 0 |
  6. | 24 | 6848c1a4_8d57_11eb_815f_f875a4d24e90 | commerce | 0 | vt_commerce | customer | drop table customer | online | | 2021-03-25 12:46:53 | 2021-03-25 12:46:51 | 2021-03-25 12:46:57 | 2021-03-25 12:46:57 | 2021-03-25 12:46:57 | 2021-03-25 12:46:57 | NULL | complete | | _vt_HOLD_6848c1a48d5711eb815ff875a4d24e90_20210326104657, | 0 | zone1-0000000100 | 0 | 100 | vtgate:a8352418-8d55-11eb-815f-f875a4d24e90 | drop | | 0 |
  7. | 25 | 6fd57dd3_8d57_11eb_815f_f875a4d24e90 | commerce | 0 | vt_commerce | customer | revert 6848c1a4_8d57_11eb_815f_f875a4d24e90 | online | | 2021-03-25 12:47:08 | 2021-03-25 12:47:04 | 2021-03-25 12:47:12 | 2021-03-25 12:47:12 | 2021-03-25 12:47:12 | 2021-03-25 12:47:12 | NULL | complete | | _vt_HOLD_6848c1a48d5711eb815ff875a4d24e90_20210326104657, | 0 | zone1-0000000100 | 0 | 100 | vtgate:a8352418-8d55-11eb-815f-f875a4d24e90 | create | | 0 |
  8. +----+--------------------------------------+----------+-------+--------------+-------------+---------------------------------------------------------------------------------+----------+---------+---------------------+---------------------+---------------------+---------------------+---------------------+---------------------+-------------------+------------------+----------+-------------------------------------------------------------+---------+------------------+----------------+----------+---------------------------------------------+------------+---------+-------------+
  • show vitess_migrations shows the entire history of migrations.
  • show vitess_migrations like ... filters migrations by migration_uuid, or migration_context, or migration_status.
  • show vitess_migrations where ... lets the user specify arbitrary conditions.
  • All commands return results for the keyspace (schema) in use.

Via vtctl/ApplySchema

Examples for a 4-shard cluster:

  1. $ vtctlclient OnlineDDL commerce show ab3ffdd5_f25c_11ea_bab4_0242c0a8b007
  2. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  3. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  4. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  5. | test-0000000201 | 40-80 | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:24:33 | 2020-09-09 05:24:34 | complete |
  6. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:25:13 | 2020-09-09 05:25:14 | complete |
  7. | test-0000000401 | c0- | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:25:13 | 2020-09-09 05:25:14 | complete |
  8. | test-0000000101 | -40 | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:25:13 | 2020-09-09 05:25:14 | complete |
  9. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  10. $ vtctlclient OnlineDDL commerce show 8a797518_f25c_11ea_bab4_0242c0a8b007
  11. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  12. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  13. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  14. | test-0000000401 | c0- | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | running |
  15. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | 2020-09-09 05:23:33 | complete |
  16. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | running |
  17. | test-0000000101 | -40 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | running |
  18. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  19. $ vtctlclient OnlineDDL commerce show 8a797518_f25c_11ea_bab4_0242c0a8b007
  20. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  21. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  22. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  23. | test-0000000401 | c0- | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  24. | test-0000000101 | -40 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  25. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  26. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | 2020-09-09 05:23:33 | complete |
  27. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  28. $ vtctlclient OnlineDDL commerce show recent
  29. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  30. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  31. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  32. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 63b5db0c_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:22:41 | 2020-09-09 05:22:42 | complete |
  33. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | 2020-09-09 05:23:33 | complete |
  34. | test-0000000201 | 40-80 | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:24:33 | 2020-09-09 05:24:34 | complete |
  35. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 63b5db0c_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:22:41 | 2020-09-09 05:22:42 | complete |
  36. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  37. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:25:13 | 2020-09-09 05:25:14 | complete |
  38. | test-0000000401 | c0- | vt_commerce | demo | alter | 63b5db0c_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:22:41 | 2020-09-09 05:22:42 | complete |
  39. | test-0000000401 | c0- | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  40. | test-0000000401 | c0- | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:25:13 | 2020-09-09 05:25:14 | complete |
  41. | test-0000000101 | -40 | vt_commerce | demo | alter | 63b5db0c_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:22:41 | 2020-09-09 05:22:42 | complete |
  42. | test-0000000101 | -40 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  43. | test-0000000101 | -40 | vt_commerce | demo | alter | ab3ffdd5_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:25:13 | 2020-09-09 05:25:14 | complete |
  44. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  45. $ vtctlclient OnlineDDL commerce show failed
  46. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  47. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  48. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  49. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  50. | test-0000000401 | c0- | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  51. | test-0000000101 | -40 | vt_commerce | demo | alter | 8a797518_f25c_11ea_bab4_0242c0a8b007 | online | 2020-09-09 05:23:32 | | failed |
  52. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+

The syntax for tracking migrations is:

  1. vtctlclient OnlineDDL <keyspace> show <migration_id|all|recent|queued|ready|running|complete|failed|cancelled>

Cancelling a migration

The user may cancel a migration, as follows:

  • If the migration hasn’t started yet (it is queued or ready), then it is removed from queue and will not be executed.
  • If the migration is running, then it is forcibly interrupted. The migration is expected to transition to failed state.
  • In all other cases, cancelling a migration has no effect.

Via VTGate/SQL

Examples for a single shard cluster:

  1. id: 28
  2. migration_uuid: aa89f255_8d68_11eb_815f_f875a4d24e90
  3. keyspace: commerce
  4. shard: 0
  5. mysql_schema: vt_commerce
  6. mysql_table: corder
  7. migration_statement: alter table corder add column handler_id int not null
  8. strategy: gh-ost
  9. options:
  10. added_timestamp: 2021-03-25 14:50:27
  11. requested_timestamp: 2021-03-25 14:50:24
  12. ready_timestamp: 2021-03-25 14:50:31
  13. started_timestamp: 2021-03-25 14:50:32
  14. liveness_timestamp: 2021-03-25 14:50:32
  15. completed_timestamp: NULL
  16. cleanup_timestamp: NULL
  17. migration_status: running
  18. ...
  19. mysql> alter vitess_migration 'aa89f255_8d68_11eb_815f_f875a4d24e90' cancel;
  20. Query OK, 1 row affected (0.01 sec)
  21. mysql> show vitess_migrations like 'aa89f255_8d68_11eb_815f_f875a4d24e90' \G
  22. *************************** 1. row ***************************
  23. id: 28
  24. migration_uuid: aa89f255_8d68_11eb_815f_f875a4d24e90
  25. keyspace: commerce
  26. shard: 0
  27. mysql_schema: vt_commerce
  28. mysql_table: corder
  29. migration_statement: alter table corder add column handler_id int not null
  30. strategy: gh-ost
  31. options: --throttle-flag-file=/tmp/throttle.flag
  32. added_timestamp: 2021-03-25 14:50:27
  33. requested_timestamp: 2021-03-25 14:50:24
  34. ready_timestamp: 2021-03-25 14:50:31
  35. started_timestamp: 2021-03-25 14:50:32
  36. liveness_timestamp: 2021-03-25 14:50:32
  37. completed_timestamp: NULL
  38. cleanup_timestamp: NULL
  39. migration_status: failed
  40. ...
  • alter vitess_migration ... cancel takes exactly one migration’s UUID.
  • alter vitess_migration ... cancel responds with number of affected migrations.

Via vtctl/ApplySchema

Examples for a 4-shard cluster:

  1. vtctlclient OnlineDDL <keyspace> cancel <migration_id>

Example:

  1. $ vtctlclient OnlineDDL commerce show 2201058f_f266_11ea_bab4_0242c0a8b007
  2. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  3. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  4. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  5. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | running |
  6. | test-0000000101 | -40 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | running |
  7. | test-0000000401 | c0- | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | running |
  8. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | running |
  9. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  10. $ vtctlclient OnlineDDL commerce cancel 2201058f_f266_11ea_bab4_0242c0a8b007
  11. +-----------------+--------------+
  12. | Tablet | RowsAffected |
  13. +-----------------+--------------+
  14. | test-0000000401 | 1 |
  15. | test-0000000101 | 1 |
  16. | test-0000000201 | 1 |
  17. | test-0000000301 | 1 |
  18. +-----------------+--------------+
  19. $ vtctlclient OnlineDDL commerce show 2201058f_f266_11ea_bab4_0242c0a8b007
  20. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  21. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  22. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  23. | test-0000000401 | c0- | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  24. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  25. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  26. | test-0000000101 | -40 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  27. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+

Cancelling all keyspace migrations

The user may cancel all migrations in a keyspace. A migration is cancellable if it is in queued, ready or running states, as described previously. It is a high impact operation and should be used with care.

Via VTGate/SQL

Examples for a single shard cluster:

  1. mysql> alter vitess_migration cancel all;
  2. Query OK, 1 row affected (0.02 sec)

Via vtctl/ApplySchema

Examples for a 4-shard cluster:

  1. vtctlclient OnlineDDL <keyspace> cancel-all

Example:

  1. $ vtctlclient OnlineDDL commerce show all
  2. +------------------+-------+--------------+-------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  3. | Tablet | shard | mysql_schema | mysql_table | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  4. +------------------+-------+--------------+-------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  5. | zone1-0000000100 | 0 | vt_commerce | corder | 2c581994_353a_11eb_8b72_f875a4d24e90 | online | | | queued |
  6. | zone1-0000000100 | 0 | vt_commerce | corder | 2c6420c9_353a_11eb_8b72_f875a4d24e90 | online | | | queued |
  7. | zone1-0000000100 | 0 | vt_commerce | corder | 2c7040df_353a_11eb_8b72_f875a4d24e90 | online | | | queued |
  8. | zone1-0000000100 | 0 | vt_commerce | corder | 2c7c0572_353a_11eb_8b72_f875a4d24e90 | online | | | queued |
  9. | zone1-0000000100 | 0 | vt_commerce | corder | 2c87f7cd_353a_11eb_8b72_f875a4d24e90 | online | | | queued |
  10. +------------------+-------+--------------+-------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  11. $ vtctlclient OnlineDDL commerce cancel-all
  12. +------------------+--------------+
  13. | Tablet | RowsAffected |
  14. +------------------+--------------+
  15. | zone1-0000000100 | 5 |
  16. +------------------+--------------+
  17. vtctlclient OnlineDDL commerce show all
  18. +------------------+-------+--------------+-------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  19. | Tablet | shard | mysql_schema | mysql_table | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  20. +------------------+-------+--------------+-------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  21. | zone1-0000000100 | 0 | vt_commerce | corder | 2c581994_353a_11eb_8b72_f875a4d24e90 | online | | | cancelled |
  22. | zone1-0000000100 | 0 | vt_commerce | corder | 2c6420c9_353a_11eb_8b72_f875a4d24e90 | online | | | cancelled |
  23. | zone1-0000000100 | 0 | vt_commerce | corder | 2c7040df_353a_11eb_8b72_f875a4d24e90 | online | | | cancelled |
  24. | zone1-0000000100 | 0 | vt_commerce | corder | 2c7c0572_353a_11eb_8b72_f875a4d24e90 | online | | | cancelled |
  25. | zone1-0000000100 | 0 | vt_commerce | corder | 2c87f7cd_353a_11eb_8b72_f875a4d24e90 | online | | | cancelled |
  26. +------------------+-------+--------------+-------------+--------------------------------------+----------+-------------------+---------------------+------------------+

Retrying a migration

The user may retry running a migration. If the migration is in failed or in cancelled state, Vitess will re-run the migration, with exact same arguments as previously intended. If the migration is in any other state, retry does nothing.

It is not possible to retry a migration with different options. e.g. if the user initially runs ALTER TABLE demo MODIFY id BIGINT with @@ddl_strategy='gh-ost --max-load Threads_running=200' and the migration fails, retrying it will use exact same options. It is not possible to retry with @@ddl_strategy='gh-ost --max-load Threads_running=500'.

Via VTGate/SQL

Examples for a single shard cluster:

  1. *************************** 1. row ***************************
  2. id: 28
  3. migration_uuid: aa89f255_8d68_11eb_815f_f875a4d24e90
  4. keyspace: commerce
  5. shard: 0
  6. mysql_schema: vt_commerce
  7. mysql_table: corder
  8. migration_statement: alter table corder add column handler_id int not null
  9. strategy: gh-ost
  10. options: --throttle-flag-file=/tmp/throttle.flag
  11. added_timestamp: 2021-03-25 14:50:27
  12. requested_timestamp: 2021-03-25 14:50:24
  13. ready_timestamp: 2021-03-25 14:56:22
  14. started_timestamp: 2021-03-25 14:56:22
  15. liveness_timestamp: 2021-03-25 14:56:22
  16. completed_timestamp: NULL
  17. cleanup_timestamp: NULL
  18. migration_status: failed
  19. ...
  20. mysql> alter vitess_migration 'aa89f255_8d68_11eb_815f_f875a4d24e90' retry;
  21. Query OK, 1 row affected (0.00 sec)
  22. mysql> show vitess_migrations like 'aa89f255_8d68_11eb_815f_f875a4d24e90' \G
  23. *************************** 1. row ***************************
  24. id: 28
  25. migration_uuid: aa89f255_8d68_11eb_815f_f875a4d24e90
  26. keyspace: commerce
  27. shard: 0
  28. mysql_schema: vt_commerce
  29. mysql_table: corder
  30. migration_statement: alter table corder add column handler_id int not null
  31. strategy: gh-ost
  32. options: --throttle-flag-file=/tmp/throttle.flag
  33. added_timestamp: 2021-03-25 14:50:27
  34. requested_timestamp: 2021-03-25 14:50:24
  35. ready_timestamp: 2021-03-25 14:56:42
  36. started_timestamp: 2021-03-25 14:56:42
  37. liveness_timestamp: 2021-03-25 14:56:42
  38. completed_timestamp: NULL
  39. cleanup_timestamp: NULL
  40. migration_status: running
  41. ...
  • alter vitess_migration ... cancel takes exactly one migration’s UUID.
  • alter vitess_migration ... cancel responds with number of affected migrations.

Via vtctl/ApplySchema

Examples for a 4-shard cluster:

  1. $ vtctlclient OnlineDDL commerce show 2201058f_f266_11ea_bab4_0242c0a8b007
  2. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  3. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  4. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  5. | test-0000000401 | c0- | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  6. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  7. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  8. | test-0000000101 | -40 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:32:31 | | failed |
  9. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  10. $ vtctlclient OnlineDDL commerce retry 2201058f_f266_11ea_bab4_0242c0a8b007
  11. +-----------------+--------------+
  12. | Tablet | RowsAffected |
  13. +-----------------+--------------+
  14. | test-0000000101 | 1 |
  15. | test-0000000201 | 1 |
  16. | test-0000000301 | 1 |
  17. | test-0000000401 | 1 |
  18. +-----------------+--------------+
  19. $ vtctlclient OnlineDDL commerce show 2201058f_f266_11ea_bab4_0242c0a8b007
  20. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  21. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  22. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  23. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | | | queued |
  24. | test-0000000101 | -40 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | | | queued |
  25. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | | | queued |
  26. | test-0000000401 | c0- | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | | | queued |
  27. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+-------------------+---------------------+------------------+
  28. $ vtctlclient OnlineDDL commerce show 2201058f_f266_11ea_bab4_0242c0a8b007
  29. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  30. | Tablet | shard | mysql_schema | mysql_table | ddl_action | migration_uuid | strategy | started_timestamp | completed_timestamp | migration_status |
  31. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+
  32. | test-0000000101 | -40 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:37:33 | | running |
  33. | test-0000000401 | c0- | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:37:33 | | running |
  34. | test-0000000201 | 40-80 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:37:33 | | running |
  35. | test-0000000301 | 80-c0 | vt_commerce | demo | alter | 2201058f_f266_11ea_bab4_0242c0a8b007 | online | 2020-09-09 06:37:33 | | running |
  36. +-----------------+-------+--------------+-------------+------------+--------------------------------------+----------+---------------------+---------------------+------------------+

Reverting a migration

Vitess offers lossless revert for online schema migrations: the user may regret a table migration after completion, and roll back the table’s schema to previous state without loss of data. See Revertible Migrations.

Via VTGate/SQL

Examples for a single shard cluster:

  1. mysql> show create table corder\G
  2. Create Table: CREATE TABLE `corder` (
  3. `order_id` bigint(20) NOT NULL AUTO_INCREMENT,
  4. `customer_id` bigint(20) DEFAULT NULL,
  5. `sku` varbinary(128) DEFAULT NULL,
  6. `price` bigint(20) DEFAULT NULL,
  7. `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  8. PRIMARY KEY (`order_id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  10. 1 row in set (0.01 sec)
  11. mysql> alter table corder drop column ts, add key customer_idx(customer_id);
  12. +--------------------------------------+
  13. | uuid |
  14. +--------------------------------------+
  15. | 1a689113_8d77_11eb_815f_f875a4d24e90 |
  16. +--------------------------------------+
  17. mysql> show create table corder\G
  18. Create Table: CREATE TABLE `corder` (
  19. `order_id` bigint(20) NOT NULL AUTO_INCREMENT,
  20. `customer_id` bigint(20) DEFAULT NULL,
  21. `sku` varbinary(128) DEFAULT NULL,
  22. `price` bigint(20) DEFAULT NULL,
  23. PRIMARY KEY (`order_id`),
  24. KEY `customer_idx` (`customer_id`)
  25. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  26. 1 row in set (0.00 sec)
  27. mysql> revert vitess_migration '1a689113_8d77_11eb_815f_f875a4d24e90';
  28. +--------------------------------------+
  29. | uuid |
  30. +--------------------------------------+
  31. | a02e6612_8d79_11eb_815f_f875a4d24e90 |
  32. +--------------------------------------+
  33. mysql> show create table corder\G
  34. Create Table: CREATE TABLE `corder` (
  35. `order_id` bigint(20) NOT NULL AUTO_INCREMENT,
  36. `customer_id` bigint(20) DEFAULT NULL,
  37. `sku` varbinary(128) DEFAULT NULL,
  38. `price` bigint(20) DEFAULT NULL,
  39. `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  40. PRIMARY KEY (`order_id`)
  41. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  • A revert is its own migration, hence has its own UUID

Via vtctl/ApplySchema

  1. $ vtctlclient OnlineDDL commerce revert 2201058f_f266_11ea_bab4_0242c0a8b007

VExec commands for greater control and visibility

vtctlclient OnlineDDL command should provide with most needs. However, Vitess gives the user greater control through the VExec command and via SQL queries.

For schema migrations, Vitess allows operations on the virtual table _vt.schema_migrations. Queries on this virtual table scatter to the underlying tablets and gather or manipulate data on their own, private backend tables (which incidentally are called by the same name). VExec only allows specific types of queries on that table.

  • SELECT: you may SELECT any column, or SELECT *. vtctlclient OnlineDDL show commands only present with a subset of columns, and so running VExec SELECT provides greater visibility. Some columns that are not shown are:
    • log_path: tablet server and path where migration logs are.
    • artifacts: tables created by the migration. This can be used to determine which tables need cleanup.
    • alter: the exact alter statement used by the migration
    • options: any options passed by the user (e.g. --max-load=Threads_running=200)
    • Various timestamps indicating the migration progress Aggregate functions do not work as expected and should be avoided. LIMIT and OFFSET are not supported.
  • UPDATE: you may directly update the status of a migration. You may only change status into cancel or retry, which Vitess interprets similarly to a vtctlclient OnlineDDL cancel/retry command. However, you get greater control as you may filter on a specific shard.
  • DELETE: unsupported
  • INSERT: unsupported, used internally only to advertise new migration requests to the tablets.

The syntax to run VExec queries is:

  1. vtctlclient VExec <keyspace>.<migration_id> "<sql query>"

Examples:

  1. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "select * from _vt.schema_migrations"
  2. $ vtctlclient VExec commerce.91b5c953-e1e2-11ea-a097-f875a4d24e90 "update _vt.schema_migrations set migration_status='retry'"
  3. $ vtctlclient VExec commerce.91b5c953-e1e2-11ea-a097-f875a4d24e90 "update _vt.schema_migrations set migration_status='retry' where shard='40-80'
  1. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "select shard, mysql_table, migration_uuid, started_timestamp, completed_timestamp, migration_status from _vt.schema_migrations"
  2. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  3. | Tablet | shard | mysql_table | migration_uuid | started_timestamp | completed_timestamp | migration_status |
  4. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  5. | test-0000000301 | 80-c0 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  6. | test-0000000101 | -40 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  7. | test-0000000201 | 40-80 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 08:31:47 | | failed |
  8. | test-0000000401 | c0- | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  9. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  10. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "update _vt.schema_migrations set migration_status='retry' where migration_uuid='2201058f_f266_11ea_bab4_0242c0a8b007' and shard='40-80'"
  11. +-----------------+--------------+
  12. | Tablet | RowsAffected |
  13. +-----------------+--------------+
  14. | test-0000000201 | 1 |
  15. +-----------------+--------------+
  16. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "select shard, mysql_table, migration_uuid, started_timestamp, completed_timestamp, migration_status from _vt.schema_migrations"
  17. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  18. | Tablet | shard | mysql_table | migration_uuid | started_timestamp | completed_timestamp | migration_status |
  19. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  20. | test-0000000301 | 80-c0 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  21. | test-0000000201 | 40-80 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 08:34:59 | | running |
  22. | test-0000000101 | -40 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  23. | test-0000000401 | c0- | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  24. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  25. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "update _vt.schema_migrations set migration_status='cancel' where migration_uuid='2201058f_f266_11ea_bab4_0242c0a8b007' and shard='40-80'"
  26. +-----------------+--------------+
  27. | Tablet | RowsAffected |
  28. +-----------------+--------------+
  29. | test-0000000201 | 1 |
  30. +-----------------+--------------+
  31. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "select shard, mysql_table, migration_uuid, started_timestamp, completed_timestamp, migration_status from _vt.schema_migrations"
  32. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  33. | Tablet | shard | mysql_table | migration_uuid | started_timestamp | completed_timestamp | migration_status |
  34. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  35. | test-0000000401 | c0- | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  36. | test-0000000101 | -40 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  37. | test-0000000201 | 40-80 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 08:34:59 | | failed |
  38. | test-0000000301 | 80-c0 | demo | 2201058f_f266_11ea_bab4_0242c0a8b007 | 2020-09-09 06:37:33 | | failed |
  39. +-----------------+-------+-------------+--------------------------------------+---------------------+---------------------+------------------+
  40. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "update _vt.schema_migrations set migration_status='cancel' where migration_uuid='2201058f_f266_11ea_bab4_0242c0a8b007' and shard='40-80'"
  41. <no result>
  42. $ vtctlclient VExec commerce.2201058f_f266_11ea_bab4_0242c0a8b007 "select shard, log_path from _vt.schema_migrations"
  43. +-----------------+-------+-----------------------------------------------------------------------------+
  44. | Tablet | shard | log_path |
  45. +-----------------+-------+-----------------------------------------------------------------------------+
  46. | test-0000000201 | 40-80 | 11ac2af6e63e:/tmp/online-ddl-2201058f_f266_11ea_bab4_0242c0a8b007-657478384 |
  47. | test-0000000101 | -40 | e779a82d35d7:/tmp/online-ddl-2201058f_f266_11ea_bab4_0242c0a8b007-901629215 |
  48. | test-0000000401 | c0- | 5aad1249ab91:/tmp/online-ddl-2201058f_f266_11ea_bab4_0242c0a8b007-039568897 |
  49. | test-0000000301 | 80-c0 | 5e7c662679d3:/tmp/online-ddl-2201058f_f266_11ea_bab4_0242c0a8b007-532703073 |
  50. +-----------------+-------+-----------------------------------------------------------------------------+

<< Online DDL strategies Declarative migrations >>