Materialize

<< MoveTables

This guide follows on from the Get Started guides. Please make sure that you have an Operator, local or Helm installation ready. Make sure you have only run the “101” step of the examples, for example 101_initial_cluster.sh in the local example. The commands in this guide also assumes you have setup the shell aliases from the example, e.g. env.sh in the local example.

Materialize is a new VReplication workflow in Vitess 6. It can be used as a more general way to achieve something similar to MoveTables, or as a way to generate materialized views of a table (or set of tables) in the same or different keyspace from the source table (or set of tables). In general, it can be used to create and maintain continually updated materialized views in Vitess, without having to resort to manual or trigger-based population of the view content.

Since Materialize uses VReplication, the view can be kept up-to-date very close to real-time, which enables use-cases like creating copies of the same table sharded different ways for the purposes of certain types of queries that would otherwise be prohibitively expensive on the original table. Materialize is also flexible enough to allow for you to pre-create the schema and vschema for the copied table, allowing you to, for example, maintain a copy of a table without some of the source table’s MySQL indexes. Alternatively, you could use Materialize to do certain schema changes (e.g. change the type of a table column) without having to use other tools like gh-ost.

All of the command options and parameters are listed in our reference page for Materialize.

In our example, we will be using Materialize to perform something similar to the MoveTables user guide, which will cover just the basics of what is possible using Materialize.

Let’s start by simulating this situation by loading sample data:

  1. # On helm and local installs:
  2. mysql < /usr/local/vitess/examples/common/insert_commerce_data.sql
  3. # With operator:
  4. mysql --table < insert_commerce_data.sql

We can look at what we just inserted:

  1. # On helm and local installs:
  2. mysql --table < /usr/local/vitess/examples/common/select_commerce_data.sql
  3. # With operator:
  4. mysql --table < select_commerce_data.sql
  5. Using commerce
  6. Customer
  7. +-------------+--------------------+
  8. | customer_id | email |
  9. +-------------+--------------------+
  10. | 1 | alice@domain.com |
  11. | 2 | bob@domain.com |
  12. | 3 | charlie@domain.com |
  13. | 4 | dan@domain.com |
  14. | 5 | eve@domain.com |
  15. +-------------+--------------------+
  16. Product
  17. +----------+-------------+-------+
  18. | sku | description | price |
  19. +----------+-------------+-------+
  20. | SKU-1001 | Monitor | 100 |
  21. | SKU-1002 | Keyboard | 30 |
  22. +----------+-------------+-------+
  23. COrder
  24. +----------+-------------+----------+-------+
  25. | order_id | customer_id | sku | price |
  26. +----------+-------------+----------+-------+
  27. | 1 | 1 | SKU-1001 | 100 |
  28. | 2 | 2 | SKU-1002 | 30 |
  29. | 3 | 3 | SKU-1002 | 30 |
  30. | 4 | 4 | SKU-1002 | 30 |
  31. | 5 | 5 | SKU-1002 | 30 |
  32. +----------+-------------+----------+-------+

Note that we are using keyspace commerce to select data from our tables.

Planning to use Materialize

In this scenario, we are going to make two copies of the corder table in the same keyspace using a different tablenames of corder_view and corder_view_redacted. The first copy will be identical to the source table, but for the corder_view_redacted copy, we will use the opportunity to drop the price column from the copy. Since we are doing the Materialize to the same keyspace, we do not need to create a new keyspace or tablets as we did for the MoveTables user guide.

Create the destination tables

In the case where we using Materialize to copy tables between keyspaces, we can use the "create_ddl": "copy" option in the Materialize json_spec table_settings to create the target table for us (similar to what MoveTables does). However, in our case where we are using Materialize with a target table name different from the source table name, we need to manually create the target tables. Let’s go ahead and do that:

  1. $ mysql -A
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. .
  4. .
  5. mysql> CREATE TABLE `corder_view` (
  6. `order_id` bigint NOT NULL,
  7. `customer_id` bigint DEFAULT NULL,
  8. `sku` varbinary(128) DEFAULT NULL,
  9. `price` bigint DEFAULT NULL,
  10. PRIMARY KEY (`order_id`)
  11. ) ENGINE=InnoDB;
  12. Query OK, 0 rows affected (0.13 sec)
  13. mysql> CREATE TABLE `corder_view_redacted` (
  14. `order_id` bigint NOT NULL,
  15. `customer_id` bigint DEFAULT NULL,
  16. `sku` varbinary(128) DEFAULT NULL,
  17. PRIMARY KEY (`order_id`)
  18. ) ENGINE=InnoDB;
  19. Query OK, 0 rows affected (0.09 sec)

Now we need to make sure Vitess’ view of our schema is up-to-date:

  1. $ vtctlclient ReloadSchemaKeyspace commerce

And now we can proceed to the Materialize step(s).

Start the Materialize (first copy)

We will run two Materialize operations, one for each copy/view of the corder table we will be creating. We could combine these two operations into a single Materialize operation, but we will keep them separate for clarity.

  1. $ vtctlclient Materialize '{"workflow": "copy_corder_1", "source_keyspace": "commerce", "target_keyspace": "commerce", "table_settings": [{"target_table": "corder_view", "source_expression": "select * from corder"}]}'

Now, we should see the materialized view table corder_view:

  1. $ echo "select * from corder_view;" | mysql --table commerce
  2. +----------+-------------+----------+-------+
  3. | order_id | customer_id | sku | price |
  4. +----------+-------------+----------+-------+
  5. | 1 | 1 | SKU-1001 | 100 |
  6. | 2 | 2 | SKU-1002 | 30 |
  7. | 3 | 3 | SKU-1002 | 30 |
  8. | 4 | 4 | SKU-1002 | 30 |
  9. | 5 | 5 | SKU-1002 | 30 |
  10. +----------+-------------+----------+-------+

And if we insert a row into the source table, it will be replicated to the materialized view:

  1. $ echo "insert into corder (order_id, customer_id, sku, price) values (6, 6, 'SKU-1002', 30);" | mysql commerce
  2. $ echo "select * from corder_view;" | mysql --table commerce
  3. +----------+-------------+----------+-------+
  4. | order_id | customer_id | sku | price |
  5. +----------+-------------+----------+-------+
  6. | 1 | 1 | SKU-1001 | 100 |
  7. | 2 | 2 | SKU-1002 | 30 |
  8. | 3 | 3 | SKU-1002 | 30 |
  9. | 4 | 4 | SKU-1002 | 30 |
  10. | 5 | 5 | SKU-1002 | 30 |
  11. | 6 | 6 | SKU-1002 | 30 |
  12. +----------+-------------+----------+-------+

Note that the target table is just a normal table, there is nothing that prevents you from writing to it directly. While you might not want to do that in this “view” use-case, in certain other use-cases, it might be completely acceptable to write to the table, as long as you don’t end up altering or removing rows in a fashion that would break the “replication” part of VReplication (e.g. removing a row in the target table directly that is later updated in the source table).

Viewing the workflow while in progress

While we can also see and manipulate the underlying VReplication streams created by Materialize; there are commands to show, stop, start and delete the operations associated with a Materialize workflow. For example, once we have started the Materialize command above, we can observe the status of the VReplication stream doing the materialization via the vtctlclient Workflow command:

  1. $ vtctlclient Workflow commerce.copy_corder_1 show
  2. {
  3. "Workflow": "copy_corder_1",
  4. "SourceLocation": {
  5. "Keyspace": "commerce",
  6. "Shards": [
  7. "0"
  8. ]
  9. },
  10. "TargetLocation": {
  11. "Keyspace": "commerce",
  12. "Shards": [
  13. "0"
  14. ]
  15. },
  16. "MaxVReplicationLag": 1599019410,
  17. "ShardStatuses": {
  18. "0/zone1-0000000100": {
  19. "MasterReplicationStatuses": [
  20. {
  21. "Shard": "0",
  22. "Tablet": "zone1-0000000100",
  23. "ID": 4,
  24. "Bls": {
  25. "keyspace": "commerce",
  26. "shard": "0",
  27. "filter": {
  28. "rules": [
  29. {
  30. "match": "corder_view",
  31. "filter": "select * from corder"
  32. }
  33. ]
  34. }
  35. },
  36. "Pos": "MySQL56/c0d82d27-ecd0-11ea-9840-001e677affd5:1-97978",
  37. "StopPos": "",
  38. "State": "Copying",
  39. "MaxReplicationLag": 9223372036854775807,
  40. "DBName": "vt_commerce",
  41. "TransactionTimestamp": 0,
  42. "TimeUpdated": 1599019408,
  43. "Message": "",
  44. "CopyState": [
  45. {
  46. "Table": "corder_view",
  47. "LastPK": "fields:<name:\"order_id\" type:INT64 > rows:<lengths:5 values:\"37014\" >"
  48. }
  49. ]
  50. }
  51. ],
  52. "TabletControls": null,
  53. "MasterIsServing": true
  54. }
  55. }
  56. }

Note the state of Copying, this will transition to Running when the bulk copying of rows is complete.

We can now also use the stop/start commands to temporarily stop the materialization workflow. E.g. stop:

  1. $ vtctlclient Workflow commerce.copy_corder_1 stop
  2. +------------------+--------------+
  3. | Tablet | RowsAffected |
  4. +------------------+--------------+
  5. | zone1-0000000100 | 1 |
  6. +------------------+--------------+

And start to start the workflow again and continue the materialization:

  1. $ vtctlclient Workflow commerce.copy_corder_1 start
  2. +------------------+--------------+
  3. | Tablet | RowsAffected |
  4. +------------------+--------------+
  5. | zone1-0000000100 | 1 |
  6. +------------------+--------------+

Eventually, when the copy is done, or we have materialized the data, and do not want to continue the copy of new source rows, we can delete the workflow via:

  1. $ vtctlclient Workflow commerce.copy_corder_1 delete
  2. +------------------+--------------+
  3. | Tablet | RowsAffected |
  4. +------------------+--------------+
  5. | zone1-0000000100 | 1 |
  6. +------------------+--------------+

Note that deleting the workflow will not drop the target table for the Materialize workflow, or any of the data already copied. The data in the target table will remain as it was at the moment the workflow was deleted (or previously stopped).

Start the Materialize (redacted copy)

Now, we can perform the copy to the corder_view_redacted table we created earlier. Note that we created this table without a price column; we will not be copying that column.

  1. $ vtctlclient Materialize '{"workflow": "copy_corder_2", "source_keyspace": "commerce", "target_keyspace": "commerce", "table_settings": [{"target_table": "corder_view_redacted", "source_expression": "select order_id, customer_id, sku from corder"}]}'

Again, looking the target table will show all the source table rows, this time without the sku column:

  1. $ echo "select * from corder_view_redacted;" | mysql --table commerce
  2. +----------+-------------+----------+
  3. | order_id | customer_id | sku |
  4. +----------+-------------+----------+
  5. | 1 | 1 | SKU-1001 |
  6. | 2 | 2 | SKU-1002 |
  7. | 3 | 3 | SKU-1002 |
  8. | 4 | 4 | SKU-1002 |
  9. | 5 | 5 | SKU-1002 |
  10. | 6 | 6 | SKU-1002 |
  11. +----------+-------------+----------+

Again, we can add a row to the source table, and see it replicated into the target table:

  1. $ echo "insert into corder (order_id, customer_id, sku, price) values (7, 7, 'SKU-1002', 30);" | mysql commerce
  2. $ echo "select * from corder_view_redacted;" | mysql --table commerce
  3. +----------+-------------+----------+
  4. | order_id | customer_id | sku |
  5. +----------+-------------+----------+
  6. | 1 | 1 | SKU-1001 |
  7. | 2 | 2 | SKU-1002 |
  8. | 3 | 3 | SKU-1002 |
  9. | 4 | 4 | SKU-1002 |
  10. | 5 | 5 | SKU-1002 |
  11. | 6 | 6 | SKU-1002 |
  12. | 7 | 7 | SKU-1002 |
  13. +----------+-------------+----------+

What happened under the covers

As with MoveTables, a VReplication stream was formed for each of the Materialize workflows we executed. We can see these by inspecting the VReplication table on the target keyspace master tablet, e.g. in this case:

  1. $ vtctlclient VReplicationExec zone1-0000000100 'select * from _vt.vreplication'
  2. +----+---------------+---------------------------------------------+----------------------------------------------------+----------+---------------------+---------------------+------+--------------+--------------+-----------------------+---------+---------+-------------+
  3. | id | workflow | source | pos | stop_pos | max_tps | max_replication_lag | cell | tablet_types | time_updated | transaction_timestamp | state | message | db_name |
  4. +----+---------------+---------------------------------------------+----------------------------------------------------+----------+---------------------+---------------------+------+--------------+--------------+-----------------------+---------+---------+-------------+
  5. | 1 | copy_corder_1 | keyspace:"commerce" shard:"0" | MySQL56/00a04e3a-e74d-11ea-a8c9-001e677affd5:1-926 | | 9223372036854775807 | 9223372036854775807 | | | 1598416592 | 1598416591 | Running | | vt_commerce |
  6. | | | filter:<rules:<match:"corder_view" | | | | | | | | | | | |
  7. | | | filter:"select * from corder" > > | | | | | | | | | | | |
  8. | 2 | copy_corder_2 | keyspace:"commerce" shard:"0" | MySQL56/00a04e3a-e74d-11ea-a8c9-001e677affd5:1-926 | | 9223372036854775807 | 9223372036854775807 | | | 1598416592 | 1598416591 | Running | | vt_commerce |
  9. | | | filter:<rules:<match:"corder_view_redacted" | | | | | | | | | | | |
  10. | | | filter:"select order_id, customer_id, sku | | | | | | | | | | | |
  11. | | | from corder" > > | | | | | | | | | | | |
  12. +----+---------------+---------------------------------------------+----------------------------------------------------+----------+---------------------+---------------------+------+--------------+--------------+-----------------------+---------+---------+-------------+

It is important to use the vtctlclient VReplicationExec command to inspect this table, since some of the fields are binary and might not render properly in a MySQL client (at least with default options). In the above output, you can see a summary of the VReplication streams that were setup (and are still Running) to copy and then do continuous replication of the source table (corder) to the two different target tables.

Cleanup

As seen earlier, you can easily use the vtctlclient Workflow ... stop and vtctlclient Workflow ... delete commands to clean up a materialize operation. If you like, you can instead use the VReplicationExec command to temporarily stop the VReplication streams that make up the Materialize process. For example, to stop both streams, you can do:

  1. $ vtctlclient VReplicationExec zone1-0000000100 'update _vt.vreplication set state = "Stopped" where id in (1,2)'
  2. +
  3. +
  4. $ vtctlclient VReplicationExec zone1-0000000100 'select * from _vt.vreplication'
  5. +----+---------------+---------------------------------------------+-----------------------------------------------------+----------+---------------------+---------------------+------+--------------+--------------+-----------------------+---------+---------+-------------+
  6. | id | workflow | source | pos | stop_pos | max_tps | max_replication_lag | cell | tablet_types | time_updated | transaction_timestamp | state | message | db_name |
  7. +----+---------------+---------------------------------------------+-----------------------------------------------------+----------+---------------------+---------------------+------+--------------+--------------+-----------------------+---------+---------+-------------+
  8. | 1 | copy_corder_1 | keyspace:"commerce" shard:"0" | MySQL56/00a04e3a-e74d-11ea-a8c9-001e677affd5:1-1218 | | 9223372036854775807 | 9223372036854775807 | | | 1598416861 | 1598416859 | Stopped | | vt_commerce |
  9. | | | filter:<rules:<match:"corder_view" | | | | | | | | | | | |
  10. | | | filter:"select * from corder" > > | | | | | | | | | | | |
  11. | 2 | copy_corder_2 | keyspace:"commerce" shard:"0" | MySQL56/00a04e3a-e74d-11ea-a8c9-001e677affd5:1-1218 | | 9223372036854775807 | 9223372036854775807 | | | 1598416861 | 1598416859 | Stopped | | vt_commerce |
  12. | | | filter:<rules:<match:"corder_view_redacted" | | | | | | | | | | | |
  13. | | | filter:"select order_id, customer_id, sku | | | | | | | | | | | |
  14. | | | from corder" > > | | | | | | | | | | | |
  15. +----+---------------+---------------------------------------------+-----------------------------------------------------+----------+---------------------+---------------------+------+--------------+--------------+-----------------------+---------+---------+-------------+

Any changes to the source tables will now not be applied to the target tables until you update the state column back to Running.

Lastly, you can clean up the Materialize process by just using VReplicationExec to delete the rows in the _vt.vreplication table. This will do the necessary runtime cleanup as well. E.g.:

  1. $ vtctlclient VReplicationExec zone1-0000000100 'delete from _vt.vreplication where id in (1,2)'
  2. +
  3. +
  4. $ vtctlclient VReplicationExec zone1-0000000100 'select * from _vt.vreplication'
  5. +----+----------+--------+-----+----------+---------+---------------------+------+--------------+--------------+-----------------------+-------+---------+---------+
  6. | id | workflow | source | pos | stop_pos | max_tps | max_replication_lag | cell | tablet_types | time_updated | transaction_timestamp | state | message | db_name |
  7. +----+----------+--------+-----+----------+---------+---------------------+------+--------------+--------------+-----------------------+-------+---------+---------+
  8. +----+----------+--------+-----+----------+---------+---------------------+------+--------------+--------------+-----------------------+-------+---------+---------+

Note that this just cleans up the VReplication streams; the actual source and target tables are left untouched and in the same state they were at the moment the VReplication streams were stopped or deleted.

Recap

As mentioned at the beginning, Materialize gives you finer control over the VReplication process without having to form VReplication rules by hand. For the ultimate flexibility, that is still possible, but you should be able to use Materialize together with other Vitess features like routing rules to cover a large set of potential migration and data maintenance use-cases without resorting to creating VReplication rules directly.


<< MoveTables