github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/integration_tests/_utils/start_tidb_cluster_impl (about)

     1  #!/bin/bash
     2  
     3  # --workdir: work directory
     4  # --tidb-config: path to tidb config file
     5  # --multiple-upstream-pd: whether to deploy multiple pd severs in upstream
     6  
     7  set -e
     8  
     9  OUT_DIR=
    10  tidb_config=
    11  pd_config=
    12  tikv_config=
    13  multiple_upstream_pd=
    14  random_file_name=
    15  
    16  # Random generate the sockets config.
    17  # Make sure we dont use the same sock.
    18  randomGenSocketsConf() {
    19  	random_str=$(date '+%s%N')
    20  	if [ "$(uname)" == "Darwin" ]; then
    21  		random_str=$(cat /dev/random | LC_ALL=C tr -dc "a-zA-Z0-9" | head -c 10)
    22  	fi
    23  	random_file_name="$OUT_DIR/tidb-config-$random_str.toml"
    24  
    25  	cat "$OUT_DIR/tidb-config.toml" >"$random_file_name"
    26  	echo "socket = \"/tmp/tidb-$random_str.sock\"" >>"$random_file_name"
    27  }
    28  
    29  while [[ ${1} ]]; do
    30  	case "${1}" in
    31  	--workdir)
    32  		OUT_DIR=${2}
    33  		shift
    34  		;;
    35  	--tidb-config)
    36  		tidb_config=${2}
    37  		shift
    38  		;;
    39  	--pd-config)
    40  		pd_config=${2}
    41  		shift
    42  		;;
    43  	--tikv-config)
    44  		tikv_config=${2}
    45  		shift
    46  		;;
    47  	--multiple-upstream-pd)
    48  		multiple_upstream_pd=${2}
    49  		shift
    50  		;;
    51  	*)
    52  		echo "Unknown parameter: ${1}" >&2
    53  		exit 1
    54  		;;
    55  	esac
    56  
    57  	if ! shift; then
    58  		echo 'Missing parameter argument.' >&2
    59  		exit 1
    60  	fi
    61  done
    62  
    63  CUR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
    64  source $CUR/../_utils/test_prepare
    65  
    66  stop_tidb_cluster
    67  
    68  cd $OUT_DIR && echo "start tidb cluster in $OUT_DIR"
    69  
    70  # pd server config file
    71  if [[ "$pd_config" != "" ]]; then
    72  	cat $pd_config >$OUT_DIR/pd-config.toml
    73  else
    74  	cat - >"$OUT_DIR/pd-config.toml" <<EOF
    75  [replication]
    76  # Set it to 1 to make sure we have enough replicas to run placement-rules.
    77  max-replicas = 1
    78  enable-placement-rules = true
    79  EOF
    80  fi
    81  
    82  echo "Starting Upstream PD..."
    83  pd-server --version
    84  if [[ "$multiple_upstream_pd" == "true" ]]; then
    85  	pd_count=3
    86  	initial_cluster="pd1=http://${UP_PD_HOST_1}:${UP_PD_PEER_PORT_1},pd2=http://${UP_PD_HOST_2}:${UP_PD_PEER_PORT_2},pd3=http://${UP_PD_HOST_3}:${UP_PD_PEER_PORT_3}"
    87  else
    88  	pd_count=1
    89  	initial_cluster="pd1=http://${UP_PD_HOST_1}:${UP_PD_PEER_PORT_1}"
    90  fi
    91  for idx in $(seq 1 $pd_count); do
    92  	host="UP_PD_HOST_$idx"
    93  	port="UP_PD_PORT_$idx"
    94  	peer_port="UP_PD_PEER_PORT_$idx"
    95  	pd-server \
    96  		--advertise-client-urls http://${!host}:${!port} \
    97  		--client-urls http://0.0.0.0:${!port} \
    98  		--advertise-peer-urls http://${!host}:${!peer_port} \
    99  		--peer-urls http://0.0.0.0:${!peer_port} \
   100  		--config "$OUT_DIR/pd-config.toml" \
   101  		--log-file "$OUT_DIR/pd$idx.log" \
   102  		--data-dir "$OUT_DIR/pd$idx" \
   103  		--name="pd$idx" \
   104  		--initial-cluster=${initial_cluster} &
   105  done
   106  
   107  echo "Starting Downstream PD..."
   108  pd-server --version
   109  pd-server \
   110  	--advertise-client-urls http://${DOWN_PD_HOST}:${DOWN_PD_PORT} \
   111  	--client-urls http://0.0.0.0:${DOWN_PD_PORT} \
   112  	--advertise-peer-urls http://${DOWN_PD_HOST}:${DOWN_PD_PEER_PORT} \
   113  	--peer-urls http://0.0.0.0:${DOWN_PD_PEER_PORT} \
   114  	--config "$OUT_DIR/pd-config.toml" \
   115  	--log-file "$OUT_DIR/down_pd.log" \
   116  	--data-dir "$OUT_DIR/down_pd" &
   117  
   118  # wait until upstream PD is online...
   119  echo "Verifying upstream PD is started..."
   120  for idx in $(seq 1 $pd_count); do
   121  	host="UP_PD_HOST_$idx"
   122  	port="UP_PD_PORT_$idx"
   123  
   124  	i=0
   125  	while ! curl -o /dev/null -sf http://${!host}:${!port}/pd/api/v1/version; do
   126  		i=$((i + 1))
   127  		if [ "$i" -gt 60 ]; then
   128  			echo 'Failed to start upstream PD'
   129  			exit 1
   130  		fi
   131  		sleep 1
   132  	done
   133  
   134  	i=0
   135  	while [ -z "$(curl http://${!host}:${!port}/pd/api/v1/health 2>/dev/null | grep 'health' | grep 'true')" ]; do
   136  		i=$((i + 1))
   137  		if [ "$i" -gt 60 ]; then
   138  			echo 'Failed to start upstream PD'
   139  			exit 1
   140  		fi
   141  		sleep 1
   142  	done
   143  done
   144  
   145  # wait until downstream PD is online...
   146  echo "Verifying downstream PD is started..."
   147  i=0
   148  while ! curl -o /dev/null -sf http://${DOWN_PD_HOST}:${DOWN_PD_PORT}/pd/api/v1/version; do
   149  	i=$((i + 1))
   150  	if [ "$i" -gt 60 ]; then
   151  		echo 'Failed to start downstream PD'
   152  		exit 1
   153  	fi
   154  	sleep 1
   155  done
   156  
   157  i=0
   158  while [ -z "$(curl http://${DOWN_PD_HOST}:${DOWN_PD_PORT}/pd/api/v1/health 2>/dev/null | grep 'health' | grep 'true')" ]; do
   159  	i=$((i + 1))
   160  	if [ "$i" -gt 60 ]; then
   161  		echo 'Failed to start downstream PD'
   162  		exit 1
   163  	fi
   164  	sleep 1
   165  done
   166  
   167  # Tries to limit the max number of open files under the system limit
   168  if [[ "$tikv_config" != "" ]]; then
   169  	cat $tikv_config >$OUT_DIR/tikv-config.toml
   170  else
   171  	cat - >"$OUT_DIR/tikv-config.toml" <<EOF
   172  [storage]
   173  # Disable creating a large temp file.
   174  reserve-space = "0MB"
   175  [rocksdb]
   176  max-open-files = 4096
   177  [raftdb]
   178  max-open-files = 4096
   179  [raftstore]
   180  # true (default value) for high reliability, this can prevent data loss when power failure.
   181  sync-log = false
   182  [cdc]
   183  hibernate-regions-compatible = true
   184  EOF
   185  fi
   186  
   187  # tidb server config file
   188  if [[ "$tidb_config" != "" ]]; then
   189  	cat $tidb_config >$OUT_DIR/tidb-config.toml
   190  else
   191  	cat - >"$OUT_DIR/tidb-config.toml" <<EOF
   192  split-table = true
   193  new_collations_enabled_on_first_bootstrap = true
   194  EOF
   195  fi
   196  
   197  echo "Starting Upstream TiKV..."
   198  tikv-server --version
   199  for idx in $(seq 1 3); do
   200  	host="UP_TIKV_HOST_$idx"
   201  	port="UP_TIKV_PORT_$idx"
   202  	status_port="UP_TIKV_STATUS_PORT_$idx"
   203  	tikv-server \
   204  		--pd ${UP_PD_HOST_1}:${UP_PD_PORT_1} \
   205  		-A ${!host}:${!port} \
   206  		--status-addr ${!host}:${!status_port} \
   207  		--log-file "$OUT_DIR/tikv$idx.log" \
   208  		--log-level debug \
   209  		-C "$OUT_DIR/tikv-config.toml" \
   210  		-s "$OUT_DIR/tikv$idx" &
   211  done
   212  
   213  echo "Starting Downstream TiKV..."
   214  tikv-server --version
   215  tikv-server \
   216  	--pd ${DOWN_PD_HOST}:${DOWN_PD_PORT} \
   217  	-A ${DOWN_TIKV_HOST}:${DOWN_TIKV_PORT} \
   218  	--status-addr ${DOWN_TIKV_HOST}:${DOWN_TIKV_STATUS_PORT} \
   219  	--log-file "$OUT_DIR/tikv_down.log" \
   220  	--log-level debug \
   221  	-C "$OUT_DIR/tikv-config.toml" \
   222  	-s "$OUT_DIR/tikv_down" &
   223  
   224  sleep 2
   225  
   226  echo "Starting Upstream TiDB..."
   227  tidb-server -V
   228  randomGenSocketsConf
   229  tidb-server \
   230  	-P ${UP_TIDB_PORT} \
   231  	-config "$random_file_name" \
   232  	--store tikv \
   233  	--path ${UP_PD_HOST_1}:${UP_PD_PORT_1} \
   234  	--status=${UP_TIDB_STATUS} \
   235  	--log-file "$OUT_DIR/tidb.log" &
   236  
   237  randomGenSocketsConf
   238  tidb-server \
   239  	-P ${UP_TIDB_OTHER_PORT} \
   240  	-config "$random_file_name" \
   241  	--store tikv \
   242  	--path ${UP_PD_HOST_1}:${UP_PD_PORT_1} \
   243  	--status=${UP_TIDB_OTHER_STATUS} \
   244  	--log-file "$OUT_DIR/tidb_other.log" &
   245  
   246  echo "Starting Downstream TiDB..."
   247  tidb-server -V
   248  randomGenSocketsConf
   249  tidb-server \
   250  	-P ${DOWN_TIDB_PORT} \
   251  	-config "$random_file_name" \
   252  	--store tikv \
   253  	--path ${DOWN_PD_HOST}:${DOWN_PD_PORT} \
   254  	--status=${DOWN_TIDB_STATUS} \
   255  	--log-file "$OUT_DIR/tidb_down.log" &
   256  
   257  echo "Verifying Upstream TiDB is started..."
   258  i=0
   259  while ! mysql -uroot -h${UP_TIDB_HOST} -P${UP_TIDB_PORT} --default-character-set utf8mb4 -e 'select * from mysql.tidb;'; do
   260  	i=$((i + 1))
   261  	if [ "$i" -gt 60 ]; then
   262  		echo 'Failed to start upstream TiDB'
   263  		exit 2
   264  	fi
   265  	sleep 2
   266  done
   267  
   268  i=0
   269  while ! mysql -uroot -h${UP_TIDB_HOST} -P${UP_TIDB_OTHER_PORT} --default-character-set utf8mb4 -e 'select * from mysql.tidb;'; do
   270  	i=$((i + 1))
   271  	if [ "$i" -gt 60 ]; then
   272  		echo 'Failed to start upstream TiDB'
   273  		exit 2
   274  	fi
   275  	sleep 2
   276  done
   277  
   278  echo "Verifying Downstream TiDB is started..."
   279  i=0
   280  while ! mysql -uroot -h${DOWN_TIDB_HOST} -P${DOWN_TIDB_PORT} --default-character-set utf8mb4 -e 'select * from mysql.tidb;'; do
   281  	i=$((i + 1))
   282  	if [ "$i" -gt 60 ]; then
   283  		echo 'Failed to start downstream TiDB'
   284  		exit 1
   285  	fi
   286  	sleep 2
   287  done
   288  
   289  run_sql "update mysql.tidb set variable_value='60m' where variable_name='tikv_gc_life_time';" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
   290  run_sql "update mysql.tidb set variable_value='60m' where variable_name='tikv_gc_life_time';" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   291  run_sql "CREATE user 'normal'@'%' identified by '123456';" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   292  run_sql "GRANT select,insert,update,delete,index,create,drop,alter,create view,references ON *.* TO 'normal'@'%';" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   293  run_sql "FLUSH privileges" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   294  
   295  cat - >"$OUT_DIR/tiflash-config.toml" <<EOF
   296  tmp_path = "${OUT_DIR}/tiflash/tmp"
   297  display_name = "TiFlash"
   298  users_config = "${OUT_DIR}/tiflash/users.toml"
   299  path = "${OUT_DIR}/tiflash/db"
   300  mark_cache_size = 5368709120
   301  listen_host = "127.0.0.1"
   302  tcp_port = 5000
   303  http_port = 4500
   304  interserver_http_port = 5500
   305  
   306  [flash]
   307  tidb_status_addr = "127.0.0.1:8500"
   308  service_addr = "127.0.0.1:9500"
   309  
   310  [flash.proxy]
   311  addr = "127.0.0.1:9000"
   312  advertise-addr = "127.0.0.1:9000"
   313  data-dir = "${OUT_DIR}/tiflash/db/proxy"
   314  config = "${OUT_DIR}/tiflash-proxy.toml"
   315  log-file = "${OUT_DIR}/tiflash/log/proxy.log"
   316  
   317  [logger]
   318  level = "trace"
   319  log = "${OUT_DIR}/tiflash/log/server.log"
   320  errorlog = "${OUT_DIR}/tiflash/log/error.log"
   321  size = "4000M"
   322  count = 10
   323  
   324  [application]
   325  runAsDaemon = true
   326  
   327  [raft]
   328  pd_addr = "${UP_PD_HOST_1}:${UP_PD_PORT_1}"
   329  EOF
   330  
   331  cat - >"$OUT_DIR/tiflash-proxy.toml" <<EOF
   332  log-level = "info"
   333  
   334  [server]
   335  engine-addr = "127.0.0.1:9500"
   336  status-addr = "127.0.0.1:17000"
   337  
   338  [raftstore]
   339  sync-log = true
   340  capacity = "100GB"
   341  hibernate-regions = false
   342  
   343  [rocksdb]
   344  wal-dir = ""
   345  max-open-files = 1000
   346  
   347  [rocksdb.defaultcf]
   348  block-cache-size = "1GB"
   349  
   350  [rocksdb.lockcf]
   351  block-cache-size = "1GB"
   352  
   353  [rocksdb.writecf]
   354  block-cache-size = "1GB"
   355  
   356  [raftdb]
   357  max-open-files = 1000
   358  
   359  [raftdb.defaultcf]
   360  block-cache-size = "1GB"
   361  EOF
   362  
   363  echo "Starting Upstream TiFlash..."
   364  mkdir -p ${OUT_DIR}/tiflash/ && cp $CUR/tiflash-users.toml ${OUT_DIR}/tiflash/users.toml
   365  tiflash version
   366  tiflash server --config-file "$OUT_DIR/tiflash-config.toml" &
   367  
   368  echo "Verifying Upstream TiFlash is started..."
   369  # Make sure TiFlash is started.
   370  while ! curl -o /dev/null -sf http://127.0.0.1:17000/metrics 1>/dev/null 2>&1; do
   371  	i=$((i + 1))
   372  	if [ "$i" -gt 10 ]; then
   373  		cat ${OUT_DIR}/tiflash/log/proxy.log
   374  		cat ${OUT_DIR}/tiflash/log/server.log
   375  		cat ${OUT_DIR}/tiflash/log/error.log
   376  		echo 'Failed to start TiFlash'
   377  		exit 1
   378  	fi
   379  	sleep 2
   380  done