|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2022 The Databend Authors. |
| 3 | +# SPDX-License-Identifier: Apache-2.0. |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +SCRIPT_PATH="$(cd "$(dirname "$0")" >/dev/null 2>&1 && pwd)" |
| 8 | +cd "$SCRIPT_PATH/../../.." || exit |
| 9 | +BUILD_PROFILE=${BUILD_PROFILE:-debug} |
| 10 | + |
| 11 | +if [ $# -eq 1 ]; then |
| 12 | + num=$1 |
| 13 | + node_group="" |
| 14 | +elif [ $# -eq 2 ]; then |
| 15 | + num=$1 |
| 16 | + node_group=$2 |
| 17 | +else |
| 18 | + echo "Usage: $0 <number> - Start number of databend-query with system-managed mode" |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +if ! [[ "$num" =~ ^[0-9]*$ ]]; then |
| 23 | + echo "Error: Argument must be an integer." |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# Caveat: has to kill query first. |
| 28 | +# `query` tries to remove its liveness record from meta before shutting down. |
| 29 | +# If meta is stopped, `query` will receive an error that hangs graceful |
| 30 | +# shutdown. |
| 31 | +killall databend-query || true |
| 32 | +sleep 3 |
| 33 | + |
| 34 | +killall databend-meta || true |
| 35 | +sleep 3 |
| 36 | + |
| 37 | +for bin in databend-query databend-meta; do |
| 38 | + if test -n "$(pgrep $bin)"; then |
| 39 | + echo "The $bin is not killed. force killing." |
| 40 | + killall -9 $bin || true |
| 41 | + fi |
| 42 | +done |
| 43 | + |
| 44 | +# Wait for killed process to cleanup resources |
| 45 | +sleep 1 |
| 46 | + |
| 47 | +echo 'Start Meta service HA cluster(3 nodes)...' |
| 48 | + |
| 49 | +mkdir -p ./.databend/ |
| 50 | + |
| 51 | +nohup ./target/${BUILD_PROFILE}/databend-meta -c scripts/ci/deploy/config/databend-meta-node-1.toml >./.databend/meta-1.out 2>&1 & |
| 52 | +python3 scripts/ci/wait_tcp.py --timeout 30 --port 9191 |
| 53 | + |
| 54 | +# wait for cluster formation to complete. |
| 55 | +sleep 1 |
| 56 | + |
| 57 | +nohup ./target/${BUILD_PROFILE}/databend-meta -c scripts/ci/deploy/config/databend-meta-node-2.toml >./.databend/meta-2.out 2>&1 & |
| 58 | +python3 scripts/ci/wait_tcp.py --timeout 30 --port 28202 |
| 59 | + |
| 60 | +# wait for cluster formation to complete. |
| 61 | +sleep 1 |
| 62 | + |
| 63 | +nohup ./target/${BUILD_PROFILE}/databend-meta -c scripts/ci/deploy/config/databend-meta-node-3.toml >./.databend/meta-3.out 2>&1 & |
| 64 | +python3 scripts/ci/wait_tcp.py --timeout 30 --port 28302 |
| 65 | + |
| 66 | +# wait for cluster formation to complete. |
| 67 | +sleep 1 |
| 68 | + |
| 69 | +find_available_port() { |
| 70 | + local base_port=20000 |
| 71 | + local max_port=65535 |
| 72 | + local attempts=10 |
| 73 | + |
| 74 | + for ((i=0; i<attempts; i++)); do |
| 75 | + port=$(( RANDOM % (max_port - base_port + 1) + base_port )) |
| 76 | + if ! lsof -i :$port >/dev/null 2>&1; then |
| 77 | + echo $port |
| 78 | + return |
| 79 | + fi |
| 80 | + done |
| 81 | + |
| 82 | + echo "Unable to find an available port after $attempts attempts" >&2 |
| 83 | + exit 1 |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +start_databend_query() { |
| 88 | + local http_port=$1 |
| 89 | + local mysql_port=$2 |
| 90 | + local log_dir=$3 |
| 91 | + local node_group=$4 |
| 92 | + system_managed_config="./scripts/ci/deploy/config/databend-query-node-system-managed.toml" |
| 93 | + |
| 94 | + temp_file=$(mktemp) |
| 95 | + |
| 96 | + if [ -f "$system_managed_config" ]; then |
| 97 | + sed -e "s/flight_port/$(find_available_port)/g" \ |
| 98 | + -e "s/admin_api_port/$(find_available_port)/g" \ |
| 99 | + -e "s/metric_api_port/$(find_available_port)/g" \ |
| 100 | + -e "s/mysql_port/${mysql_port}/g" \ |
| 101 | + -e "s/clickhouse_port/$(find_available_port)/g" \ |
| 102 | + -e "s/http_port/${http_port}/g" \ |
| 103 | + -e "s/flight_sql_port/$(find_available_port)/g" \ |
| 104 | + -e "s/query_logs/${log_dir}/g" \ |
| 105 | + -e "s/node_group/node_group=\"${node_group}\"/g" \ |
| 106 | + "$system_managed_config" > "$temp_file" |
| 107 | + |
| 108 | + if [ $? -eq 0 ]; then |
| 109 | + echo "Start databend-query on port $http_port..." |
| 110 | + nohup target/${BUILD_PROFILE}/databend-query -c $temp_file --internal-enable-sandbox-tenant & |
| 111 | + |
| 112 | + echo "Waiting on databend-query 10 seconds..." |
| 113 | + python3 scripts/ci/wait_tcp.py --timeout 30 --port $http_port |
| 114 | + else |
| 115 | + echo "Error occurred during port replacement." |
| 116 | + rm -f "$temp_file" |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + else |
| 120 | + echo "Error: system-managed config file is not exists." |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | +} |
| 124 | + |
| 125 | +if ! lsof -i :8000 >/dev/null 2>&1; then |
| 126 | + start_databend_query 8000 3307 "logs_1" $node_group |
| 127 | + num=$(( num - 1 )) |
| 128 | +fi |
| 129 | + |
| 130 | +for (( i=0; i<$num; i++ )) |
| 131 | +do |
| 132 | + http_port=$(find_available_port) |
| 133 | + mysql_port=$(find_available_port) |
| 134 | + start_databend_query $http_port $mysql_port "logs_$http_port" $node_group |
| 135 | +done |
0 commit comments