## Module 2: Apply Read Write Workloads to TiDB Cluster

### Step 1. Review the script of the INSERT workload

+ In [Terminal 2](ssh://monitor1-ssh#2), review the INSERT workload script. It contains a Java program capable of printing messages to the console in case of any JDBC exceptions. In the final output, you might notice messages such as `Work ? discards connection and try to get a new connection`. It's essential to note that during the demonstration, you shouldn't encounter these messages. TiProxy can migrate the database client sessions before the upgrade process shuts down a TiDB server instance, ensuring uninterrupted operation.

  ```bash
  cat scripts/quick-demo-005-jdbc-endless-insert-dummy.sh
  ```

  ```output
  #!/bin/bash

  # Run ./quick-demo-005-jdbc-endless-insert-dummy.sh <LB_NAME>

  rm -f DemoJdbcEndlessInsertDummyCSPWithLBLongConn.class
  javac -cp .:misc/mysql-connector-java-5.1.36-bin.jar DemoJdbcEndlessInsertDummyCSPWithLBLongConn.java
  sleep 1
  java -cp .:misc/mysql-connector-java-5.1.36-bin.jar DemoJdbcEndlessInsertDummyCSPWithLBLongConn ${1}
  ```

  ```bash
  cat scripts/DemoJdbcEndlessInsertDummyCSPWithLBLongConn.java | grep -A10 catch | grep Work
  ```

  ```output
              System.out.println("Work 1 discards connection and try to get a new connection");
              System.out.println("Work 2 discards connection and try to get a new connection");
              System.out.println("Work 3 discards connection and try to get a new connection");
              System.out.println("Work 4 discards connection and try to get a new connection");
  ```

### Step 2. Run the script to apply the INSERT workload

+ Stay in [Terminal 2](ssh://monitor1-ssh#2), run the script to apply the sample INSERT workload with 4 workers. Keep the workload running and proceed to the next step.

  ```bash
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} -P 6000 -uroot << EOF
  DROP TABLE IF EXISTS test.dummy;
  CREATE TABLE test.dummy (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(80), event JSON, tidb_instance VARCHAR(30));
  EOF
  cd scripts
  ./quick-demo-005-jdbc-endless-insert-dummy.sh ${HOST_TIPROXY1_PRIVATE_IP}
  ```

  ```output
  Worker 3 -> TiDB:Thu May 16 09:38:55 UTC 2024 at 10.90.1.153
  Worker 4 -> TiDB:Thu May 16 09:38:56 UTC 2024 at 10.90.1.153
  Worker 2 -> TiDB:Thu May 16 09:38:56 UTC 2024 at 10.90.1.153
  Worker 1 -> TiDB:Thu May 16 09:38:56 UTC 2024 at 10.90.1.153
  Worker 3 -> TiDB:Thu May 16 09:38:56 UTC 2024 at 10.90.1.153
  Worker 4 -> TiDB:Thu May 16 09:38:57 UTC 2024 at 10.90.1.153
  Worker 2 -> TiDB:Thu May 16 09:38:57 UTC 2024 at 10.90.1.153
  Worker 1 -> TiDB:Thu May 16 09:38:57 UTC 2024 at 10.90.1.153
  Worker 3 -> TiDB:Thu May 16 09:38:57 UTC 2024 at 10.90.1.153
  Worker 4 -> TiDB:Thu May 16 09:38:58 UTC 2024 at 10.90.1.153
  Worker 2 -> TiDB:Thu May 16 09:38:58 UTC 2024 at 10.90.1.153
  ...
  ```

### Step 3. Run the script to apply the QUERY workload

+ In [Terminal 3](ssh://monitor1-ssh#3), run the script responsible for the query workload. The script checks how many requests were handled by each TiDB server instance. Keep the workload running and proceed to the next step.

  ```bash
  qdb(){
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} -P 6000 -uroot --connect-timeout 1 2>/dev/null << EOF
   SELECT tidb_instance, COUNT(event) FROM test.dummy GROUP BY tidb_instance ORDER BY tidb_instance;
  EOF
  }

  query(){
   echo;
   date;
   qdb
   sleep 2;
  }

  while true; do
   query;
  done;
  ```

  ```output
  Thu May 16 09:42:20 UTC 2024
  tidb_instance	COUNT(event)
  10.90.1.124:10080	466
  10.90.2.122:10080	466

  Thu May 16 09:42:22 UTC 2024
  tidb_instance	COUNT(event)
  10.90.1.124:10080	470
  10.90.2.122:10080	470

  Thu May 16 09:42:24 UTC 2024
  tidb_instance	COUNT(event)
  10.90.1.124:10080	474
  10.90.2.122:10080	474

  ...
  ```
