

# Running a data movement operation

A data move changes which member cluster serves a collocation while your application keeps sending traffic. Run one under load before your application depends on it, so that you see what your application sees during the move and confirm that your workload keeps running. The example on this page rebalances a scaleout table. Its range routing function places each block of 1,000,000 IDs in one of four collocations, and all four start on member cluster A, so that member cluster A carries all of the load. You see the imbalance in CloudWatch, move two of the ID blocks to member cluster B while the workload runs, and confirm that the load is now split evenly.

The moves don't change the scaleout table or its routing function. Each move changes only the data mapping of one collocation, and the routing layer follows the data mappings, so the rows and the routing move together while the workload keeps using the same table.

The example uses `dbcg_load.lua` from [A sysbench script for a database cluster group for Aurora](databaseclustergroups-first-workload.md#databaseclustergroups-fw-sysbench-script), and its own database, `lt_w`, so that it doesn't change the dataset from [Preparing a test dataset](databaseclustergroups-fw-dataset.md). Each command runs on the load generator host. The commands are for sysbench 1.1 and Aurora MySQL 8.4.900.

## Before you start
<a name="databaseclustergroups-fw-data-movement-prereqs"></a>

Check the following before you start:
+ You use the current `dbcg_load.lua` from [A sysbench script for a database cluster group for Aurora](databaseclustergroups-first-workload.md#databaseclustergroups-fw-sysbench-script), which reopens the connection after an error.
+ Every command uses `--mysql-ignore-errors=63927,63923,63928,1412,1317,1180,1927,2013,2006`, the codes that a data movement cutover can return. On Aurora MySQL version 8.4.900, a refused read or write returns `63927`. Later engine versions return `63923` for the same error.
+ Your router user has `dbclustergroup_move_admin` or `dbclustergroup_super_admin` for the moves, and `dbclustergroup_metadata_admin` or `dbclustergroup_super_admin` to create the routing function and the scaleout table. For the privileges, see [ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-move-collocation.md) and [CREATE SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-create-scaleout-routing-function.md).
+ You run the example in a group that you use for testing, and you have taken a group snapshot, as described in [Creating a snapshot of a database cluster group for Aurora](databaseclustergroups-backups.md#databaseclustergroups-taking-a-group-snapshot). The example moves data.

## Creating the scaleout table
<a name="databaseclustergroups-fw-data-movement-layout"></a>

The example creates a scaleout table `lt_w.sbtest_w` over four member tables, `lt_w.sbtest_w0` to `lt_w.sbtest_w3`, each in its own collocation, `cg_w0` to `cg_w3`. A range routing function on `id` sends each row to the collocation whose range holds its ID. Each collocation holds one block of IDs, as the following table shows.


| Collocation | Member table | IDs | 
| --- | --- | --- | 
| cg\_w0 | sbtest\_w0 | 1 to 1,000,000 | 
| cg\_w1 | sbtest\_w1 | 1,000,001 to 2,000,000 | 
| cg\_w2 | sbtest\_w2 | 2,000,001 to 3,000,000 | 
| cg\_w3 | sbtest\_w3 | 3,000,001 and above. The example loads IDs up to 4,000,000. | 

All four collocations start on member cluster A. The following diagram shows the layout before the moves.

![Before the moves: all four collocations of scaleout table lt_w.sbtest_w are on member cluster A.](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/images/dbcg-fw-move-before.png)


The following table shows which member cluster holds each collocation before and after the moves.


| Member cluster | Before the moves | After the moves | 
| --- | --- | --- | 
| A | cg\_w0, cg\_w1, cg\_w2, cg\_w3 (IDs 1 and above) | cg\_w0, cg\_w1 (IDs 1 to 2,000,000) | 
| B | None | cg\_w2, cg\_w3 (IDs 2,000,001 and above) | 

In the following steps, replace {{ALIAS\_A}} and {{ALIAS\_B}} with the aliases of the two member clusters, as described in [Creating and mapping the dataset](databaseclustergroups-fw-dataset.md#databaseclustergroups-fw-dataset-create).

**To create the scaleout table**

1. Connect directly to each member cluster and create the database. Create it also on member cluster B, which starts empty, because a data move needs the database on its target.

   ```sql
   CREATE DATABASE lt_w;
   ```

1. From a load generator host, create and fill one table of 4,000,000 rows directly on member cluster A. The command creates `lt_w.sbtest1`, with IDs 1 to 4,000,000.

   ```bash
   sysbench dbcg_load.lua --mysql-host={{MEMBER_A_ENDPOINT}} --mysql-port=3306 --mysql-user={{MEMBER_USER}} \
     --mysql-password={{PASSWORD}} --mysql-ssl=REQUIRED --mysql-db=lt_w \
     --tables=1 --table_size=4000000 prepare
   ```

1. Connect directly to member cluster A. Copy each block of IDs into its member table, and then drop `lt_w.sbtest1`. Each member table holds 1,000,000 rows.

   ```sql
   CREATE TABLE lt_w.sbtest_w0 LIKE lt_w.sbtest1;
   CREATE TABLE lt_w.sbtest_w1 LIKE lt_w.sbtest1;
   CREATE TABLE lt_w.sbtest_w2 LIKE lt_w.sbtest1;
   CREATE TABLE lt_w.sbtest_w3 LIKE lt_w.sbtest1;
   INSERT INTO lt_w.sbtest_w0 SELECT * FROM lt_w.sbtest1 WHERE id BETWEEN 1 AND 1000000;
   INSERT INTO lt_w.sbtest_w1 SELECT * FROM lt_w.sbtest1 WHERE id BETWEEN 1000001 AND 2000000;
   INSERT INTO lt_w.sbtest_w2 SELECT * FROM lt_w.sbtest1 WHERE id BETWEEN 2000001 AND 3000000;
   INSERT INTO lt_w.sbtest_w3 SELECT * FROM lt_w.sbtest1 WHERE id BETWEEN 3000001 AND 4000000;
   DROP TABLE lt_w.sbtest1;
   ```

1. At the routing endpoint, with autocommit on, create one table-level data mapping for each member table, each in its own collocation on member cluster A.

   ```sql
   INSERT INTO dbclustergroup_sys.data_mappings
     (cluster_db_name, cluster_table_name, router_db_name, router_table_name, member_cluster_alias, router_collocation_group)
   VALUES ('lt_w', 'sbtest_w0', 'lt_w', 'sbtest_w0', '{{ALIAS_A}}', 'cg_w0'),
          ('lt_w', 'sbtest_w1', 'lt_w', 'sbtest_w1', '{{ALIAS_A}}', 'cg_w1'),
          ('lt_w', 'sbtest_w2', 'lt_w', 'sbtest_w2', '{{ALIAS_A}}', 'cg_w2'),
          ('lt_w', 'sbtest_w3', 'lt_w', 'sbtest_w3', '{{ALIAS_A}}', 'cg_w3');
   ```

1. At the routing endpoint, create a range routing function with one range for each collocation, and the scaleout table over the four member tables. `PARTITION BY COLUMN(id)` places each row by the value of its `id`. Each `VALUES IN RANGE` includes both of its bounds, and `MAXVALUE` leaves the top of the last range open.

   ```sql
   CREATE SCALEOUT ROUTING FUNCTION lt_w_range_fn(id BIGINT)
     AS PARTITION BY COLUMN(id)
     IN TARGET COLLOCATION (
       cg_w0 VALUES IN RANGE (1, 1000000),
       cg_w1 VALUES IN RANGE (1000001, 2000000),
       cg_w2 VALUES IN RANGE (2000001, 3000000),
       cg_w3 VALUES IN RANGE (3000001, MAXVALUE)
     );
   
   CREATE SCALEOUT TABLE lt_w.sbtest_w (
     ROUTE BY KEY(id POSITION(1)) USING ROUTING FUNCTION lt_w_range_fn
   )
   MEMBERS (
     MEMBER lt_w.sbtest_w0,
     MEMBER lt_w.sbtest_w1,
     MEMBER lt_w.sbtest_w2,
     MEMBER lt_w.sbtest_w3
   );
   ```

   A scaleout table name is unique across the whole group, not for each database, so this example uses `sbtest_w` rather than the `sbtest` of [Preparing a test dataset](databaseclustergroups-fw-dataset.md). A second `CREATE SCALEOUT TABLE` with a name already in use, in any database, fails with error `64020` (`Scaleout table 'sbtest' already exists`). For the scaleout table statement, see [CREATE SCALEOUT TABLE](databaseclustergroups-sql-create-scaleout-table.md).

1. At the routing endpoint, check that the first and last IDs return a row through the scaleout table. If a query returns error 1412, run it again. A data mapping change reaches the routing layer shortly after the statement returns.

   ```sql
   SELECT id FROM lt_w.sbtest_w WHERE id = 1;
   SELECT id FROM lt_w.sbtest_w WHERE id = 4000000;
   ```

## Starting the workload
<a name="databaseclustergroups-fw-data-movement-load"></a>

Start three processes through the routing endpoint, a few seconds apart: single-statement reads, read transactions with some writes, and writes. Each process queries the scaleout table with `--scaleout=sbtest_w`, which is `lt_w.sbtest_w` in the database that `--mysql-db=lt_w` selects, so the routing layer routes each statement by its `id`. The read transactions also use `--key_block=1000000`, so that the five reads in each transaction come from one block of IDs and stay in one collocation. Run the processes for longer than the two moves take, and save each one's output.

```bash
# reads
sysbench dbcg_load.lua --mysql-host={{ROUTING_ENDPOINT}} --mysql-port=3306 --mysql-user={{ROUTER_USER}} \
  --mysql-password={{PASSWORD}} --mysql-ssl=REQUIRED --mysql-db=lt_w \
  --tables=1 --table_size=4000000 --scaleout=sbtest_w --mysql-ignore-errors=63927,63923,63928,1412,1317,1180,1927,2013,2006 \
  --report-interval=10 --rate=150 --threads=8 --time=3600 run > read.log 2>&1 &
sleep 5
# read transactions, with 10 percent writes
sysbench dbcg_load.lua --mysql-host={{ROUTING_ENDPOINT}} --mysql-port=3306 --mysql-user={{ROUTER_USER}} \
  --mysql-password={{PASSWORD}} --mysql-ssl=REQUIRED --mysql-db=lt_w \
  --tables=1 --table_size=4000000 --scaleout=sbtest_w --mysql-ignore-errors=63927,63923,63928,1412,1317,1180,1927,2013,2006 \
  --report-interval=10 --rate=100 --point_selects=5 --key_block=1000000 --write_pct=10 --threads=8 --time=3600 run > trx.log 2>&1 &
sleep 5
# writes
sysbench dbcg_load.lua --mysql-host={{ROUTING_ENDPOINT}} --mysql-port=3306 --mysql-user={{ROUTER_USER}} \
  --mysql-password={{PASSWORD}} --mysql-ssl=REQUIRED --mysql-db=lt_w \
  --tables=1 --table_size=4000000 --scaleout=sbtest_w --mysql-ignore-errors=63927,63923,63928,1412,1317,1180,1927,2013,2006 \
  --report-interval=10 --rate=150 --write_pct=100 --threads=8 --time=3600 run > write.log 2>&1 &
```

Follow each process with `tail -f read.log`, and confirm that it reports no errors (`err/s: 0.00`) before you continue.

## Seeing the imbalance
<a name="databaseclustergroups-fw-data-movement-skew"></a>

Each sysbench process reports its throughput over the whole scaleout table, so its report doesn't show how the load splits across member clusters. Compare the member clusters instead.
+ At the routing endpoint, count the data mappings on each member cluster. The routing layer accepts `COUNT(*)` with one equality, so run one query for each alias.

  ```sql
  SELECT COUNT(*) FROM dbclustergroup_sys.data_mappings WHERE member_cluster_alias = '{{ALIAS_A}}';
  SELECT COUNT(*) FROM dbclustergroup_sys.data_mappings WHERE member_cluster_alias = '{{ALIAS_B}}';
  ```

  With only this dataset in the group, member cluster A holds 4 mappings and member cluster B holds 0.
+ In CloudWatch, compare `SelectThroughput`, `DMLThroughput`, and `CPUUtilization` for each member cluster's `DBClusterIdentifier`, with the same period and statistic, as described in [Monitoring member clusters side by side](databaseclustergroups-monitoring.md#databaseclustergroups-monitoring-member-clusters-side-by-side). Member cluster A serves all of the workload's reads and writes, and member cluster B serves none of them.

For more ways to find a busy member cluster, see [One member cluster is busier than the others](databaseclustergroups-ts-uneven-load.md#databaseclustergroups-one-member-cluster-is-busier-than-the-others).

## Moving two collocations under load
<a name="databaseclustergroups-fw-data-movement-move"></a>

Move two blocks of IDs from member cluster A to member cluster B, one after the other, while the three processes keep running: first collocation `cg_w2`, which holds IDs 2,000,001 to 3,000,000, and then collocation `cg_w3`, which holds IDs 3,000,001 and above. Use a manual cutover, so that you choose the moment of each cutover and can watch the report lines around it. Run each statement at the routing endpoint, in a session that isn't pinned.

The following diagram shows the steps of the first move, and what the workload sees at each one. The second move has the same steps.

![Timeline of moving collocation cg_w2 from member cluster A to member cluster B: preview, move phases, and cutover.](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/images/dbcg-fw-move-steps.png)


**To move two collocations while a workload runs**

1. Read the definition of the scaleout table, and save the output. You compare it with the definition after the moves.

   ```sql
   SHOW CREATE SCALEOUT TABLE lt_w.sbtest_w;
   ```

   Then read the data mappings of the dataset. The output has one row for each member table, `sbtest_w0` to `sbtest_w3`, and each row names {{ALIAS\_A}}.

   ```sql
   SELECT router_table_name, member_cluster_alias, router_collocation_group
     FROM dbclustergroup_sys.data_mappings WHERE router_db_name = 'lt_w';
   ```

1. Preview the move of `cg_w2`. Check that the preview reports `Collocation: cg_w2` and `Table data mapping updates: 1`, for the mapping of `lt_w.sbtest_w2`, and has no `Errors:` list. For the output, see [EXPLAIN ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-explain-move-collocation.md).

   ```sql
   EXPLAIN ALTER DBCLUSTERGROUP MOVE COLLOCATION cg_w2 FROM `{{ALIAS_A}}` TO `{{ALIAS_B}}` MANUAL_CUTOVER = 1;
   ```

   Quote each alias in backticks, because an alias can contain hyphens.

1. Start the move with a manual cutover. The statement returns the job ID (`JOB_ID`). Keep it for the next steps.

   ```sql
   ALTER DBCLUSTERGROUP MOVE COLLOCATION cg_w2 FROM `{{ALIAS_A}}` TO `{{ALIAS_B}}` MANUAL_CUTOVER = 1;
   ```

   For the options, see [ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-move-collocation.md).

1. Read the job's row every 30 seconds, until `phase` reads `Cdc`, which means that the job is ready for the cutover.

   ```sql
   SELECT job_id, status, phase, progress_details, errors, warnings
     FROM dbclustergroup_data_movement.move_info WHERE job_id = '{{JOB_ID}}';
   ```

   The job stays in `Initializing` for several minutes, and then passes through `Precheck` and `Seeding`. The workload continues without errors through these phases. While the job runs, a direct read of the copy on member cluster B is refused with a data movement error, so read the moving data through the routing endpoint. For the phases and the replication lag in `progress_details`, see [Reading move status](databaseclustergroups-sql-move-info.md) and [Reading move status](databaseclustergroups-running-a-move.md#databaseclustergroups-move-monitoring).

1. Note the time in UTC, and cut over.

   ```sql
   ALTER DBCLUSTERGROUP CUTOVER JOB '{{JOB_ID}}';
   ```

   A cutover that you send as soon as the job reaches `Cdc` can be refused with error `1221` (`Replication lag has not been reported for this job yet; retry once replication progress is available`). Wait until `progress_details` reports `replication_lag_seconds`, or send the statement again. The statement returns when the routing layer records the request, not when the cutover ends. For the replication lag bound, see [ALTER DBCLUSTERGROUP CUTOVER JOB](databaseclustergroups-sql-switch-job.md).

1. Read the job's row again until `status` reads `Completed`. Then read the data mappings of the dataset, as in step 1, and confirm that the row of `sbtest_w2` names {{ALIAS\_B}}. A routed read of an ID in the moved block returns a row.

   ```sql
   SELECT id FROM lt_w.sbtest_w WHERE id = 2000001;
   ```

1. Move `cg_w3` the same way, with steps 2 to 6 and `cg_w3` in place of `cg_w2`. Start the move after the first job reads `Completed`. A routed read of an ID in the second moved block returns a row.

   ```sql
   SELECT id FROM lt_w.sbtest_w WHERE id = 3000001;
   ```

1. Read the definition of the scaleout table again, and compare it with the output that you saved in step 1. The definition is the same. Then read the data mappings of the dataset, as in step 1. The rows of `sbtest_w0` and `sbtest_w1` name {{ALIAS\_A}}, and the rows of `sbtest_w2` and `sbtest_w3` name {{ALIAS\_B}}.

   ```sql
   SHOW CREATE SCALEOUT TABLE lt_w.sbtest_w;
   ```

1. Connect directly to member cluster B, and count the rows of a moved member table. It holds all 1,000,000 rows of `cg_w2`. On member cluster A, the source copy is gone, and the same query returns error `1146` (`Table 'lt_w.sbtest_w2' doesn't exist`).

   ```sql
   SELECT COUNT(*) FROM lt_w.sbtest_w2;
   ```

## What to look for
<a name="databaseclustergroups-fw-data-movement-look"></a>

In each process's report lines around each cutover, look for the following:
+ `err/s` rises above 0 for about 2 seconds at the cutover, and returns to 0. The errors are `63927` on reads and writes of the moving collocation. The message names the member table, not the scaleout table: `Cannot read `lt_w`.`sbtest_w2` while data movement job '{{JOB_ID}}' is in progress` for a read, and `Cannot write to `lt_w`.`sbtest_w2` while data movement job '{{JOB_ID}}' is in progress` for a write. Inside a read transaction, a statement can also get `63928`, after the routing layer has rolled the transaction back. A transaction that has written to the moving collocation loses its connection, with error `2013`. A cutover can also return `1412`, `1317`, `1180`, or `1927`, or close the connection, which some clients report as `2006`. sysbench retries each of these errors on a new connection, and each retry succeeds. sysbench reconnects by itself after `2013` and `2006`, so those show in `reconn/s`, not `err/s`. With `--report-interval=10`, they show in one or two report lines. For the meaning of each code, see [Cutover](databaseclustergroups-data-movement.md#databaseclustergroups-data-movement-cutover-writes) and [Data movement errors](databaseclustergroups-errors-movement.md).
+ Reads and writes of the collocations that don't move get no errors at a cutover. Every error names the moving member table.
+ No process stops. At the end of each run, the `ignored errors` total in the sysbench summary counts the errors that it retried.

Then confirm the new balance the same way that you saw the imbalance, as in [Seeing the imbalance](#databaseclustergroups-fw-data-movement-skew). Each member cluster now holds 2 of the dataset's mappings, and in CloudWatch the two member clusters serve about the same reads and writes, as the following diagram shows.

![After the moves: cg_w0 and cg_w1 on member cluster A, cg_w2 and cg_w3 on member cluster B.](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/images/dbcg-fw-move-after.png)


If a process stops, it prints a line that starts with `FATAL` and names the error code. Use the following table to find what to check.


| What stopped the process | What to check | 
| --- | --- | 
| An error code that isn't in --mysql-ignore-errors | Check the list on the command line, or in the file that you pass with --config-file. Look up the code in [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md). If your application retries it, add it to the list and run the workload again. | 
| Error 1235 in a read transaction | Check that the read transactions run with --key\_block=1000000, so that each transaction stays in one block of IDs, and that you run the current dbcg\_load.lua, whose hook reopens the connection before each retry. If both are true, check that each transaction in your workload uses one collocation. | 

If a move itself fails, its `move_info` row reads `Error`, and the `errors` column holds the reason. See [Data movement errors](databaseclustergroups-errors-movement.md).

## Moving the collocations back, or cleaning up
<a name="databaseclustergroups-fw-data-movement-cleanup"></a>

To run the test again, run another job to move `cg_w2` and `cg_w3` back from member cluster B to member cluster A, with the steps in [Moving two collocations under load](#databaseclustergroups-fw-data-movement-move) and the aliases swapped. Start each move after the previous job reads `Completed` and its `cleanup_status` reads `Complete`.

When you finish, including if you stop before the moves, stop the sysbench processes, or let them end at `--time`. Then remove the dataset in the following order.

**To remove the scaleout table and its dataset**

1. At the routing endpoint, drop the scaleout table and the routing function, and then delete the data mappings. Drop the scaleout table first, because the routing layer doesn't drop a routing function that a scaleout table uses.

   ```sql
   DROP SCALEOUT TABLE lt_w.sbtest_w;
   DROP SCALEOUT ROUTING FUNCTION lt_w_range_fn;
   DELETE FROM dbclustergroup_sys.data_mappings WHERE router_db_name = 'lt_w';
   ```

1. Connect directly to each member cluster and drop the database.

   ```sql
   DROP DATABASE lt_w;
   ```