## Module 1: Initialization of TiDB Cluster and data

### Step 1. Connect to the TiUP control machine

+ Click here - [Terminal 1](ssh://monitor1-ssh#1) to open a new terminal connected to the virtual machine assigned for your hands-on lab.

### Step 2. Create a new TiDB cluster

+ The commands in the following code block will execute a series of operations in sequence. Which are: 1. Change [TiUP mirror](https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup) setting, 2. Check system settings, 3. Fix the failed checks, 4. Deploy the TiDB cluster, and finally 5. Start the TiDB cluster. It takes approximately 3 minutes to complete.

  ```bash
  export PATH="$PATH:/home/ec2-user/.tiup/bin"
  tiup mirror set tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  tiup cluster check ./topology-with-tiproxy.yaml \
   --user ec2-user \
   -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
   --apply
  tiup cluster deploy tidb-demo v${LAB:TIDB_VERSION} ./topology-with-tiproxy.yaml \
   --user ec2-user \
   -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
   --yes
  tiup cluster start tidb-demo
  sleep 5;
  tiup cluster display tidb-demo
  ```

  ```output
  Successfully set mirror to /home/ec2-user/tidb-community-server-v${LAB:TIDB_VERSION}-linux-amd64
  /home/ec2-user/tidb-community-server-v${LAB:TIDB_VERSION}-linux-amd64
  ...
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://52.27.45.10:2379/dashboard
  Grafana URL:        http://10.90.4.40:3000
  ID                 Role          Host         Ports                            OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----                            -------       ------  --------                      ----------
  10.90.4.40:9093    alertmanager  10.90.4.40   9093/9094                        linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.40:3000    grafana       10.90.4.40   3000                             linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.2.51:2379    pd            10.90.2.51   2379/2380                        linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.40:9090    prometheus    10.90.4.40   9090/12020                       linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.2.14:4000    tidb          10.90.2.14   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.2.43:4000    tidb          10.90.2.43   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.4.243:9000   tiflash       10.90.4.243  9000/3930/20170/20292/8234/8123  linux/x86_64  Down    /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
  10.90.1.208:20160  tikv          10.90.1.208  20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.30:20160   tikv          10.90.1.30   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.93:20160   tikv          10.90.1.93   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.4.154:6000   tiproxy       10.90.4.154  6000/3080                        linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
  Total nodes: 11
  ```

### Step 3. Connect to the TiDB cluster, create tables and prepare date

+ Connect to TiDB cluster.

  ```bash
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} --port 6000 -u root
  ```

  ```output
  mysql> 
  ```

+ Set up two test tables named `ddl_test1`,  `ddl_test2`,  `ddl_test3` and  `ddl_test4`  with the same schema.

  ```sql
  DROP TABLE IF EXISTS test.ddl_test1;
  DROP TABLE IF EXISTS test.ddl_test2;
  DROP TABLE IF EXISTS test.ddl_test3;
  DROP TABLE IF EXISTS test.ddl_test4;
  CREATE TABLE test.ddl_test1 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  CREATE TABLE test.ddl_test2 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  CREATE TABLE test.ddl_test3 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  CREATE TABLE test.ddl_test4 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  ```

+ Insert one row into 4 tables.

  ```sql
  INSERT INTO test.ddl_test1 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  INSERT INTO test.ddl_test2 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  INSERT INTO test.ddl_test3 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  INSERT INTO test.ddl_test4 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  SELECT * FROM test.ddl_test1;
  SELECT * FROM test.ddl_test2;
  SELECT * FROM test.ddl_test3;
  SELECT * FROM test.ddl_test4;
  EXIT
  ```

+ Expand 4 tables with 4,194,304 rows each. It takes approximately 2 minutes to complete.

  ```bash
  cat > double.sql <<EOF
  INSERT INTO test.ddl_test1 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test1;
  INSERT INTO test.ddl_test2 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test2;
  INSERT INTO test.ddl_test3 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test3;
  INSERT INTO test.ddl_test4 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test4;
  EOF
  cat > double.sh <<EOF
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} --port 6000 -u root < double.sql
  EOF
  chmod +x double.sh
  for c in {1..22}; do
  ./double.sh
  done;
  ```

  ```bash
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} --port 6000 -u root
  ```

  ```sql
  SELECT count(*) FROM test.ddl_test1;
  SELECT count(*) FROM test.ddl_test2;
  SELECT count(*) FROM test.ddl_test3;
  SELECT count(*) FROM test.ddl_test4;
  EXIT
  ```

### Step 4. Prepare Stress Test Using TiUP Bench Component

+ Prepare stress test using TiUP bench component, more information about TiUP bench component. It takes approximately 1 minute to complete. please refer to：<https://docs.pingcap.com/tidb/stable/tiup-bench/>

   ```bash
   export PATH="$PATH:/home/ec2-user/.tiup/bin"
   tiup bench tpcc -H ${HOST_DB1_PRIVATE_IP},${HOST_DB2_PRIVATE_IP} -P 4000 -D tpcc --warehouses 4 --parts 4  --threads 20 prepare
   ```

   ```output
   A new version of bench is available:  -> v1.12.0

      To update this component:   tiup update bench
      To update all components:   tiup update --all

   The component `bench` version  is not installed; downloading from repository.
   Starting component bench: /home/ec2-user/.tiup/components/bench/v1.12.0/tiup-bench tpcc -H 10.90.2.91,10.90.2.63 -P 4000 -D tpcc --warehouses 4 --parts 4 --threads 20 prepare
   creating table warehouse
   creating table district
   creating table customer
   creating table history
   creating table new_order
   creating table orders
   creating table order_line
   creating table stock
   creating table item
   load to item
   load to warehouse in warehouse 3
   load to customer in warehouse 1 district 10
   ...
   begin to check warehouse 1 at condition 3.3.2.6
   begin to check warehouse 2 at condition 3.3.2.9
   begin to check warehouse 2 at condition 3.3.2.10
   begin to check warehouse 4 at condition 3.3.2.11
   begin to check warehouse 4 at condition 3.3.2.12
   Finished
   ```

+ At this point, the TiDB cluster and data have been prepared, and you can start learning `ADMIN PAUSE DDL` and `ADMIN RESUME DDL`.

+ This is the end of Module 1.
