

# Running your first workload through the routing endpoint

Run a test workload through the routing endpoint before you send application traffic to it. Use the test workload to confirm that each statement reaches the member cluster that you expect. You can also see how your connections behave and try operations, such as data movement, while traffic flows. You can use any load generator, including your own application's test harness. The examples in this section use Aurora MySQL and sysbench 1.1.0.

A workload tells you the most when it connects and sends statements the way your application will. Set up your load generator with the same practices as your application: [Keeping transactions on one member cluster](databaseclustergroups-queries.md#databaseclustergroups-bp-keeping-transactions-on-one-member-cluster), [Writing keyed statements](databaseclustergroups-queries.md#databaseclustergroups-queries-keyed-statements), [Connection best practices at a glance](databaseclustergroups-connection-management.md#databaseclustergroups-best-practices), and [Retrying after an error](databaseclustergroups-queries.md#databaseclustergroups-queries-retries). Run your workloads in a group that you use for testing.

You can run sysbench through the routing endpoint by setting a few options. Its built-in scripts work for single-statement tests. For transactions, data movement, and scaleout tables, use a short Lua script that follows your data mappings, such as the one on this page.

## Connection options
<a name="databaseclustergroups-fw-sysbench-connection"></a>

Pass the connection options on each sysbench command. The routing endpoint requires TLS, so set `--mysql-ssl=REQUIRED`. The following options connect to the routing endpoint.

```bash
--mysql-host={{ROUTING_ENDPOINT}} --mysql-port=3306 --mysql-user={{ROUTER_USER}} \
  --mysql-password={{PASSWORD}} --mysql-ssl=REQUIRED --mysql-db=lt
```

To load data directly on a member cluster, set `--mysql-host` to its cluster endpoint and `--mysql-user` to a user of that member cluster. `--mysql-password` puts the password on the command line, where other users of the host can see it, so run sysbench on a host that only you use.

## Options for the routing endpoint
<a name="databaseclustergroups-fw-sysbench-options"></a>

Use the following options for every run through the routing endpoint.


| Option | Why | 
| --- | --- | 
| --db-ps-mode=disable | In preview, the routing endpoint doesn't support prepared BEGIN and COMMIT statements, so sysbench must send them as text. This option makes the built-in scripts send every statement as text. The script on this page always sends text statements. | 
| --mysql-ignore-errors=63927,63923,63928,1412,1317,1180,1927,2013,2006 | Counts these errors as ignored errors and runs the event again, instead of stopping. After any error in the list, the hook in [A sysbench script for a database cluster group for Aurora](#databaseclustergroups-fw-sysbench-script) reopens the connection, so that the retry starts on a new connection with no open transaction. The option replaces the sysbench default list, 1213 (deadlock), 1205 (lock wait timeout), and 1020 (record changed). Add those codes to the list if your workload can get them. Any other error stops the thread and the run. | 
| --reconnect={{N}} | With the built-in scripts, closes and reopens each thread's connection after every {{N}} events, the way a connection pool recycles its connections. Each closed connection holds a local port on the load generator host for about 60 seconds, so a low {{N}} with many threads can use up the host's local ports. Keep the reconnections in any 60 seconds, which is 60 times the throughput divided by {{N}}, well below the number of ports in the host's local port range. The script on this page recycles its connections on its own and doesn't take this option. | 
| --threads={{N}} | Sets the number of connections, which all open when the run starts. Keep each process to a few hundred threads, and start several processes a few seconds apart. | 
| --report-interval=10 | Prints throughput, latency, and errors every 10 seconds, so that you can match the run to CloudWatch metrics. | 

## Using the built-in scripts
<a name="databaseclustergroups-fw-sysbench-builtin"></a>

The built-in scripts create tables `sbtest1` to `sbtest{{N}}`, each with the same IDs. They pick a table for each group of statements, so a transaction in `oltp_read_only` or `oltp_read_write` can read tables on more than one member cluster. The following table describes how to use them with the routing endpoint.


| Script | How to use it | 
| --- | --- | 
| oltp\_point\_select, oltp\_update\_index, oltp\_update\_non\_index | Run as they are, with --db-ps-mode=disable. Each event is one statement in its own transaction. | 
| oltp\_read\_only, oltp\_read\_write | Use the script on this page, which keeps each transaction on one table. Or add --skip\_trx=on, which sends each statement in its own transaction. Without either, a transaction can read tables on two member clusters and return error 1235. | 

The built-in scripts use their own tables, in which every table holds IDs 1 to `--table_size`. To use them, run their `prepare` command directly on each member cluster, drop the tables that the member cluster doesn't hold, and map each remaining table to its member cluster, as [Creating and mapping the dataset](databaseclustergroups-fw-dataset.md#databaseclustergroups-fw-dataset-create) shows for the script on this page.

The following example runs point selects through the routing endpoint for 10 minutes.

```bash
sysbench oltp_point_select --mysql-host={{ROUTING_ENDPOINT}} --mysql-port=3306 \
  --mysql-user={{ROUTER_USER}} --mysql-password={{PASSWORD}} --mysql-ssl=REQUIRED --mysql-db=lt \
  --tables=4 --table_size=1000000 \
  --db-ps-mode=disable --mysql-ignore-errors=63927,63923,63928,1412,1317,1180,1927,2013,2006 \
  --threads=16 --time=600 --report-interval=10 run
```

## A sysbench script for a database cluster group for Aurora
<a name="databaseclustergroups-fw-sysbench-script"></a>

The following Lua script, `dbcg_load.lua`, sends load the way an application in a database cluster group for Aurora does:
+ Table `sbtest{{N}}` holds its own range of IDs, ({{N}} - 1) × `table_size` \+ 1 to {{N}} × `table_size`, so a range routing function can route a scaleout table over the same tables.
+ Each transaction uses one table, and so one collocation and one member cluster. On a scaleout table with a range routing function, `--key_block` keeps each read transaction in one block of IDs, and so in one collocation.
+ Every statement is a text statement, with the ID as a literal.
+ Each thread closes and reopens its connection every 90 to 120 seconds, between events. The random part spreads the reconnects over time, because reconnects that happen together slow every routed statement. With a few hundred threads, that stays well within the load generator host's local ports.
+ After an error in `--mysql-ignore-errors`, the script reopens its connection, and sysbench runs the event again on the new connection, which has no open transaction.

The script has the following options.


| Option | Description | 
| --- | --- | 
| --tables, --table\_size | The number of tables, and the rows in each table. Use the same values for prepare and run. | 
| --table\_set | Limits the tables that a command uses. With prepare, run the script on each member cluster for the tables that member cluster holds. With run, send more load to some tables than to others. | 
| --point\_selects | The point selects in each read transaction. With 1, the default, each read is one statement with no transaction. | 
| --write\_pct | The percentage of events that update one row. | 
| --scaleout | A scaleout table to query instead of sbtest{{N}}. | 
| --key\_block | With --scaleout and a range routing function, the size of each block of IDs, such as 1000000. Every ID in a read transaction then comes from one block (1 to 1,000,000, 1,000,001 to 2,000,000, and so on), so the transaction stays in one collocation when each range of the function holds whole blocks. With 0, the default, the IDs in a transaction are random within the table's range. | 

**Note**  
A table holds at most 4,294,967,295 rows (`--table_size`), because the script draws IDs with sysbench's 32-bit random numbers.

```lua
-- dbcg_load.lua: sysbench load for the routing endpoint of a database cluster group.
--
-- Data:    table sbtestN holds IDs (N-1)*table_size+1 .. N*table_size. Each table has its own ID range,
--          so a range routing function can route a scaleout table over the same tables.
-- Load:    every statement is a text statement with the ID as a literal; every transaction stays on
--          one table (one collocation, so one member cluster); each thread reconnects every 90 to 120 s.
-- Retry:   after an error in --mysql-ignore-errors, reset the connection, and run the event again.
-- Usage:   sysbench dbcg_load.lua [connection options] [options] prepare   (on a member cluster, directly)
--          sysbench dbcg_load.lua [connection options] [options] run       (through the routing endpoint)

sysbench.cmdline.options = {
  tables        = {"Number of tables, sbtest1..N", 4},
  table_size    = {"Rows per table", 1000000},
  table_set     = {"Comma-separated table numbers to create or query (default: all)", ""},
  point_selects = {"Point selects per read transaction (1: a single statement, no BEGIN/COMMIT)", 1},
  write_pct     = {"Percent of events that update one row", 0},
  scaleout      = {"Scaleout table to query instead of sbtestN", ""},
  key_block     = {"With --scaleout and a range routing function: IDs per block, so a read transaction stays in one block (0: off)", 0},
}

local RECONNECT_S, RECONNECT_JITTER_S = 90, 30     -- reconnect every 90 to 120 s; match your pool's maximum lifetime
local BATCH = 1000                                 -- rows per INSERT in prepare

-- The table numbers in --table_set, or 1..--tables.
local function table_list()
  local t = {}
  for n in sysbench.opt.table_set:gmatch("%d+") do t[#t + 1] = tonumber(n) end
  if #t == 0 then for n = 1, sysbench.opt.tables do t[#t + 1] = n end end
  return t
end

-- prepare: create and fill the listed tables on the endpoint in the configuration file.
function cmd_prepare()
  local con, size = sysbench.sql.driver():connect(), sysbench.opt.table_size
  for _, n in ipairs(table_list()) do
    con:query(string.format([[CREATE TABLE IF NOT EXISTS sbtest%d (
      id BIGINT UNSIGNED NOT NULL PRIMARY KEY, k BIGINT UNSIGNED NOT NULL DEFAULT 0,
      c CHAR(120) NOT NULL DEFAULT '', KEY k (k))]], n))
    local lo = (n - 1) * size                       -- IDs lo+1 .. lo+size
    for first = 1, size, BATCH do
      con:bulk_insert_init("INSERT INTO sbtest" .. n .. " (id, k, c) VALUES")
      for i = first, math.min(first + BATCH - 1, size) do
        con:bulk_insert_next(string.format("(%d,%d,'%s')", lo + i,
          sysbench.rand.uniform(1, 1000000), sysbench.rand.string("####################")))
      end
      con:bulk_insert_done()
    end
    print(string.format("sbtest%d: ids %d..%d", n, lo + 1, lo + size))
  end
end

-- run: per-thread state.
local drv, con, reconnect_at, tables, opt

local function next_reconnect()
  return os.time() + RECONNECT_S + sysbench.rand.uniform(0, RECONNECT_JITTER_S)
end

function thread_init()
  opt = sysbench.opt
  tables = table_list()
  drv = sysbench.sql.driver()
  con = drv:connect()
  reconnect_at = next_reconnect()
end

function thread_done()
  con:disconnect()
end

-- After an error in --mysql-ignore-errors, reset the connection.
function sysbench.hooks.before_restart_event(err)
  pcall(con.reconnect, con)
end

-- One event: an update, a single point select, or a read transaction, all on one table.
function event()
  if os.time() >= reconnect_at then                -- recycle the connection between events
    con:disconnect()
    con = drv:connect()
    reconnect_at = next_reconnect()
  end

  local n = tables[sysbench.rand.uniform(1, #tables)]
  local name = opt.scaleout ~= "" and opt.scaleout or "sbtest" .. n
  local lo = (n - 1) * opt.table_size              -- keep every ID in table n's range

  if sysbench.rand.uniform(1, 100) <= opt.write_pct then
    con:query(string.format("UPDATE %s SET k = k + 1 WHERE id = %d", name,
                            lo + sysbench.rand.uniform(1, opt.table_size)))
  elseif opt.point_selects == 1 then
    con:query(string.format("SELECT c FROM %s WHERE id = %d", name,
                            lo + sysbench.rand.uniform(1, opt.table_size)))
  else
    local first, last = lo + 1, lo + opt.table_size
    if opt.key_block > 0 then                       -- one block of IDs, so one collocation of a range function
      local b = math.floor((lo + sysbench.rand.uniform(1, opt.table_size) - 1) / opt.key_block)
      first = math.max(first, b * opt.key_block + 1)
      last = math.min(last, (b + 1) * opt.key_block)
    end
    con:query("BEGIN")
    for _ = 1, opt.point_selects do
      con:query(string.format("SELECT c FROM %s WHERE id = %d", name,
                              first + sysbench.rand.uniform(0, last - first)))
    end
    con:query("COMMIT")
  end
end

sysbench.cmdline.commands = {
  prepare = {cmd_prepare},
}
```

The hook `sysbench.hooks.before_restart_event` reopens the connection after each error in `--mysql-ignore-errors`, so that sysbench retries the event on a new connection.

Save the script on each load generator host. Run the commands on these pages from the directory where you saved it. To change how often connections are recycled, edit `RECONNECT_S` and `RECONNECT_JITTER_S`. Match it to the maximum lifetime of your application's connection pool.

**Topics**
+ [Connection options](#databaseclustergroups-fw-sysbench-connection)
+ [Options for the routing endpoint](#databaseclustergroups-fw-sysbench-options)
+ [Using the built-in scripts](#databaseclustergroups-fw-sysbench-builtin)
+ [A sysbench script for a database cluster group for Aurora](#databaseclustergroups-fw-sysbench-script)
+ [Preparing a test dataset](databaseclustergroups-fw-dataset.md)
+ [Example workloads](databaseclustergroups-fw-examples.md)
+ [Running a data movement operation](databaseclustergroups-fw-data-movement.md)