## Module 2: Start the Read and Write Workloads

### Step 1. Create a table for test purposes

- In [Terminal 2](ssh://monitor1-ssh#2), connect to one of the TiDB server instances, create a testing table, and exit to the terminal.

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -uroot
  ```

  ```sql
  DROP TABLE IF EXISTS test.dummy;
  CREATE TABLE test.dummy (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(80), event JSON);
  EXIT;
  ```

### Step 2. Apply the insert workload

- In [Terminal 2](ssh://monitor1-ssh#2), run the sample Java program to initiate the INSERT workload.

- The program connects to a TiDB server instance using two workers to execute INSERT operations. If the connected TiDB server loses connectivity, the program will automatically switch to another available TiDB server. The output displays the private IP addresses of the TiDB servers handling the INSERT requests — there should be two. Leave the workload running and proceed to the next step.

  ```bash
  cd scripts
  javac -cp .:misc/mysql-connector-java-5.1.36-bin.jar DemoJdbcEndlessInsertDummyCSP.java
  java -cp .:misc/mysql-connector-java-5.1.36-bin.jar DemoJdbcEndlessInsertDummyCSP
  ```

  ```output
  Worker 2 -> TiDB:Thu May 09 07:05:33 UTC 2024 at ${LAB:HOST_DB1_PRIVATE_IP}
  Worker 1 -> TiDB:Thu May 09 07:05:33 UTC 2024 at ${LAB:HOST_DB2_PRIVATE_IP}
  Worker 1 -> TiDB:Thu May 09 07:05:34 UTC 2024 at ${LAB:HOST_DB1_PRIVATE_IP}
  Worker 2 -> TiDB:Thu May 09 07:05:34 UTC 2024 at ${LAB:HOST_DB2_PRIVATE_IP}
  Worker 1 -> TiDB:Thu May 09 07:05:35 UTC 2024 at ${LAB:HOST_DB2_PRIVATE_IP}
  Worker 2 -> TiDB:Thu May 09 07:05:35 UTC 2024 at ${LAB:HOST_DB1_PRIVATE_IP}
  Worker 1 -> TiDB:Thu May 09 07:05:36 UTC 2024 at ${LAB:HOST_DB1_PRIVATE_IP}
  ...
  ```

### Step 3. Apply the sample query workload

- In [Terminal 3](ssh://monitor1-ssh#3), run the script below. The script attempts to connect to two TiDB server instances to execute queries. If one TiDB server fails or is unable to respond, the script automatically switches to another available instance. Note that the TiDB server instances are given only 1 second to respond to connection requests, a setting similar to the Java program in Step 2. The output will show the row count in the test.dummy table steadily increasing. Leave the workload running and proceed to the next step.

  ```bash
  qdb1(){
  mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -uroot --connect-timeout 1 2>/dev/null << EOF
    SELECT COUNT(event) FROM test.dummy;
  EOF
  }

  qdb2(){
  mysql -h ${HOST_DB2_PRIVATE_IP} -P 4000 -uroot --connect-timeout 1 2>/dev/null << EOF
    SELECT COUNT(event) FROM test.dummy;
  EOF
  }

  query1(){
    echo;
    date;
    qdb1 || qdb2
    sleep 2;
  }

  query2(){
    echo;
    date;
    qdb2 || qdb1
    sleep 2;
  }

  while true; do
    query1;
    query2;
  done;
  ```

  ```output
  Mon May 13 05:27:33 UTC 2024
  COUNT(event)
  2608

  Mon May 13 05:27:35 UTC 2024
  COUNT(event)
  2612

  Mon May 13 05:27:37 UTC 2024
  COUNT(event)
  2616

  Mon May 13 05:27:39 UTC 2024
  COUNT(event)
  2620
  ...
  ```

### Step 4. Verify that the TiKV Region replicas setting is configured to three

- Return to [Terminal 1](ssh://monitor1-ssh#1), and verify that the TiKV Region replicas setting is configured to three. This is a core reason TiDB Cloud survives data center failures: if one TiKV server is lost when an availability zone fails, the remaining two replicas can still serve reads and writes.

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -uroot
  ```

  ```sql
  SHOW CONFIG WHERE type='pd' AND name='replication.max-replicas';
  EXIT
  ```

  ```output
  +------+---------------------+--------------------------+-------+
  | Type | Instance            | Name                     | Value |
  +------+---------------------+--------------------------+-------+
  | pd   | ${LAB:HOST_PD1_PUBLIC_IP}:2379   | replication.max-replicas | 3     |
  | pd   | ${LAB:HOST_PD2_PUBLIC_IP}:2379  | replication.max-replicas | 3     |
  | pd   | ${LAB:HOST_PD3_PUBLIC_IP}:2379 | replication.max-replicas | 3     |
  +------+---------------------+--------------------------+-------+
  3 rows in set (0.02 sec)
  Bye
  ```
