Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·337 lines (301 loc) · 13.2 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·337 lines (301 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORKSPACE="$(cd "${SCRIPT_DIR}/../.." && pwd)"
STATE_DIR="${SCRIPT_DIR}/.runtime"
LOG_DIR="${STATE_DIR}/logs"
BUILD_DIR="${STATE_DIR}/build"
SPARK_PID_FILE="/tmp/openrec-data-processor.pid"
SPARK_LOG_FILE="/tmp/openrec-data-processor.log"
LOCAL_MODE=false
usage() {
cat <<EOF
Usage: ${0##*/} [--local] [--reuse-images]
--local Use the mainland-China PyPI mirror; keep the official PyTorch base.
--reuse-images Run current source with existing local images; never build or pull images.
-h, --help Show this help.
Explicit RANK_BASE_IMAGE / RANK_PIP_INDEX_URL and RANK_TRAINING_BASE_IMAGE /
RANK_TRAINING_PIP_INDEX_URL values take precedence over --local defaults.
EOF
}
while (($#)); do
case "$1" in
--local) LOCAL_MODE=true ;;
--reuse-images) export OPENREC_REUSE_IMAGES=true ;;
-h|--help) usage; exit 0 ;;
*) usage >&2; echo "error: unknown option: $1" >&2; exit 2 ;;
esac
shift
done
if [[ "${LOCAL_MODE}" == true ]]; then
: "${RANK_BASE_IMAGE:=pytorch/pytorch:2.10.0-cuda12.8-cudnn9-runtime}"
: "${RANK_PIP_INDEX_URL:=https://pypi.tuna.tsinghua.edu.cn/simple}"
# Training copies /opt/conda into Spark; newer serving bases use system Python.
: "${RANK_TRAINING_BASE_IMAGE:=mirrors-ssl.aliyuncs.com/pytorch/pytorch:2.8.0-cuda12.9-cudnn9-devel}"
: "${RANK_TRAINING_PIP_INDEX_URL:=${RANK_PIP_INDEX_URL}}"
export RANK_BASE_IMAGE RANK_PIP_INDEX_URL
export RANK_TRAINING_BASE_IMAGE RANK_TRAINING_PIP_INDEX_URL
fi
note() { printf '\n\033[1m==> %s\033[0m\n' "$*"; }
die() { echo "error: $*" >&2; return 1; }
cleanup_on_error() {
local status=$?
trap - ERR
# Compose down removes containers and their logs. Save the evidence first so
# the CI artifact still contains graph node failures and rank-engine errors.
local failure_dir="${LOG_DIR}/failure-$(date -u +%Y%m%dT%H%M%SZ)-${BASHPID}"
mkdir -p "${failure_dir}" || true
docker compose -f "${SCRIPT_DIR}/docker-compose.yml" logs --no-color \
>"${failure_dir}/business.log" 2>&1 || true
docker logs --tail 2000 airflow-scheduler \
>"${failure_dir}/airflow.log" 2>&1 || true
docker exec spark-master tail -n 500 "${SPARK_LOG_FILE}" \
>"${failure_dir}/data-processor.log" 2>&1 || true
echo "startup failed; stopping services started by the OpenRec examples" >&2
"${SCRIPT_DIR}/stop.sh" || true
exit "${status}"
}
trap cleanup_on_error ERR
if [[ "${LOCAL_MODE}" == true ]]; then
note "Local mirrors enabled (rank base: ${RANK_BASE_IMAGE}, pip: ${RANK_PIP_INDEX_URL})"
fi
if command -v mvn >/dev/null 2>&1; then
MVN="$(command -v mvn)"
else
MVN="${WORKSPACE}/.tools/apache-maven-3.9.9/bin/mvn"
fi
source "${WORKSPACE}/openrec/scripts/lib/java.sh"
openrec_setup_java
[[ -x "${MVN}" ]] || die "Maven not found; install Maven 3.9+ or place it under .tools"
command -v docker >/dev/null 2>&1 || die "Docker is required"
docker compose version >/dev/null 2>&1 || die "Docker Compose v2 is required"
command -v curl >/dev/null 2>&1 || die "curl is required"
command -v rsync >/dev/null 2>&1 || die "rsync is required"
command -v python3 >/dev/null 2>&1 || die "Python 3 is required"
source "${SCRIPT_DIR}/init.sh"
openrec_prepare_init
source "${WORKSPACE}/openrec/scripts/lib/maven-repository.sh"
openrec_setup_maven_repository
MVN_ARGS=()
if [[ -n "${OPENREC_MAVEN_REPO:-}" ]]; then
MVN_ARGS+=("-Dmaven.repo.local=${OPENREC_MAVEN_REPO}")
fi
mkdir -p "${LOG_DIR}"
pid_file="${STATE_DIR}/web.pid"
if [[ -f "${pid_file}" ]]; then
pid="$(cat "${pid_file}")"
if [[ "${pid}" =~ ^[0-9]+$ ]] && kill -0 "${pid}" 2>/dev/null; then
die "cluster applications are already running; use ${SCRIPT_DIR}/stop.sh first"
fi
rm -f "${pid_file}"
fi
# Airflow (8091) belongs to the reusable cluster platform, not this app.
for required_port in 13579 12345 8123 8095; do
if (echo >/dev/tcp/127.0.0.1/"${required_port}") >/dev/null 2>&1; then
die "required port ${required_port} is occupied; run ${SCRIPT_DIR}/stop.sh and retry"
fi
done
wait_for_url() {
local name="$1" url="$2" attempts="${3:-90}" i
for ((i = 1; i <= attempts; i++)); do
if curl --noproxy '*' -fsS "${url}" >/dev/null 2>&1; then return 0; fi
sleep 1
done
die "${name} did not become ready: ${url}"
}
wait_for_es_documents() {
local name="$1" index="$2" attempts="${3:-30}" response="" i
for ((i = 1; i <= attempts; i++)); do
response="$(docker exec elasticsearch curl -fksS -u elastic:openrec-es-password \
"https://localhost:9200/${index}/_count" 2>/dev/null || true)"
if grep -Eq '"count"[[:space:]]*:[[:space:]]*[1-9][0-9]*' <<<"${response}"; then
return 0
fi
sleep 1
done
die "${name} is missing or empty after ${attempts}s: ${response:-no response}"
}
port_in_use() { (echo >/dev/tcp/127.0.0.1/"$1") >/dev/null 2>&1; }
start_jar() {
local name="$1" pid_file="$2" log_file="$3"
shift 3
# Detach from the invoking process group as well as its terminal when available.
if command -v setsid >/dev/null 2>&1; then
nohup setsid "$@" </dev/null >"${log_file}" 2>&1 &
else
nohup "$@" </dev/null >"${log_file}" 2>&1 &
fi
local pid=$!
echo "${pid}" >"${pid_file}"
note "${name} started (pid ${pid}, log ${log_file})"
}
run_airflow_dag() {
local dag_id="$1" run_id="$2" output state attempt loaded=false
note "Waiting for Airflow DAG ${dag_id}"
for attempt in {1..60}; do
if docker exec airflow-api-server airflow dags list -o plain 2>/dev/null \
| grep -Fq "${dag_id}"; then
loaded=true
break
fi
sleep 2
done
[[ "${loaded}" == true ]] || die "Airflow did not load DAG ${dag_id}"
docker exec airflow-api-server airflow dags unpause -y "${dag_id}" >/dev/null
docker exec airflow-api-server airflow dags trigger -r "${run_id}" "${dag_id}" >/dev/null
note "Waiting for Airflow run ${run_id}"
for attempt in {1..180}; do
# Airflow 3 can emit log records around the selected output format. Scan the mixed stream for
# the JSON value instead of assuming stdout is one clean JSON document.
output="$(docker exec airflow-api-server airflow dags list-runs "${dag_id}" -o json 2>&1)"
state="$(python3 -c '
import json, sys
payload = sys.stdin.read()
run_id = sys.argv[1]
decoder = json.JSONDecoder()
state = ""
for offset, char in enumerate(payload):
if char not in "[{":
continue
try:
value, _ = decoder.raw_decode(payload, offset)
except json.JSONDecodeError:
continue
runs = value if isinstance(value, list) else [value]
state = next((run.get("state", "") for run in runs
if isinstance(run, dict) and run.get("run_id") == run_id), "")
if state:
break
print(state)
' "${run_id}" <<<"${output}")"
case "${state}" in
success) return 0 ;;
failed)
echo "Airflow task states for failed run ${run_id}:" >&2
docker exec airflow-api-server \
airflow tasks states-for-dag-run "${dag_id}" "${run_id}" -o table >&2 || true
echo "Airflow task log excerpts for failed run ${run_id}:" >&2
docker logs --tail 1000 airflow-scheduler 2>&1 \
| grep -F "run_id=${run_id}" >&2 || true
die "Airflow DAG ${dag_id} failed (run ${run_id})"
;;
esac
sleep 2
done
die "Airflow DAG ${dag_id} timed out (run ${run_id}, last state ${state:-unknown})"
}
if [[ "${OPENREC_REUSE_IMAGES:-false}" != true ]]; then
note "Building the cluster infrastructure images"
"${WORKSPACE}/bigdata-platform/platform.sh" build cluster
fi
note "Starting the complete cluster platform"
export AIRFLOW_DAGS_PATH="${SCRIPT_DIR}/airflow/dags"
"${WORKSPACE}/bigdata-platform/platform.sh" up cluster
note "Waiting for the Airflow scheduler heartbeat"
airflow_scheduler_ready=false
for attempt in {1..60}; do
if docker exec airflow-scheduler airflow jobs check \
--job-type SchedulerJob --hostname airflow-scheduler >/dev/null 2>&1; then
airflow_scheduler_ready=true
break
fi
sleep 2
done
[[ "${airflow_scheduler_ready}" == true ]] || die "Airflow scheduler did not become ready"
"${WORKSPACE}/bigdata-platform/platform.sh" smoke cluster
note "Preparing isolated build workspaces"
mkdir -p "${BUILD_DIR}/rec-server" "${BUILD_DIR}/sdk/java-client" \
"${BUILD_DIR}/data-processor" "${BUILD_DIR}/openrec/init" "${BUILD_DIR}/openrec/web"
rsync -a --delete --exclude .git --exclude target/ "${WORKSPACE}/rec-server/" "${BUILD_DIR}/rec-server/"
rsync -a --delete --exclude .git --exclude target/ "${WORKSPACE}/sdk/java-client/" "${BUILD_DIR}/sdk/java-client/"
rsync -a --delete --exclude .git --exclude target/ "${WORKSPACE}/data-processor/" "${BUILD_DIR}/data-processor/"
rsync -a --delete --exclude target/ "${WORKSPACE}/openrec/init/" "${BUILD_DIR}/openrec/init/"
rsync -a --delete --exclude target/ "${WORKSPACE}/openrec/web/" "${BUILD_DIR}/openrec/web/"
note "Building SDK, feature processor, loader, and Web Demo"
"${MVN}" "${MVN_ARGS[@]}" -f "${BUILD_DIR}/rec-server/pom.xml" -pl proto -am clean install -DskipTests
"${MVN}" "${MVN_ARGS[@]}" -f "${BUILD_DIR}/sdk/java-client/pom.xml" clean install -DskipTests
"${MVN}" "${MVN_ARGS[@]}" -f "${BUILD_DIR}/data-processor/pom.xml" -pl spark -am clean package -DskipTests
"${MVN}" "${MVN_ARGS[@]}" -f "${BUILD_DIR}/openrec/init/pom.xml" clean package -DskipTests
"${MVN}" "${MVN_ARGS[@]}" -f "${BUILD_DIR}/openrec/web/pom.xml" clean package -DskipTests
note "Installing Hive daily-partition entity tables"
docker cp "${BUILD_DIR}/data-processor/sql/openrec_entities.sql" hiveserver2:/tmp/openrec_entities.sql
docker exec hiveserver2 beeline -u jdbc:hive2://hiveserver2:10000 -n hive \
-f /tmp/openrec_entities.sql >/dev/null
note "Starting Spark data-processor"
if docker exec spark-master test -f "${SPARK_PID_FILE}"; then
old_spark_pid="$(docker exec spark-master cat "${SPARK_PID_FILE}" | tr -d '\r')"
docker exec spark-master kill -0 "${old_spark_pid}" 2>/dev/null \
&& die "Spark data-processor is already running (container pid ${old_spark_pid})"
fi
docker cp "${BUILD_DIR}/data-processor/spark/target/rec-spark-0.2.0.jar" \
spark-master:/tmp/openrec-data-processor.jar
docker exec -d spark-master bash -lc \
"echo \$\$ >${SPARK_PID_FILE}; exec /opt/spark/bin/spark-submit --class com.openrec.dp.spark.SparkFeatureJob --master spark://spark-master:7077 --total-executor-cores ${OPENREC_SPARK_TOTAL_EXECUTOR_CORES:-4} /tmp/openrec-data-processor.jar >${SPARK_LOG_FILE} 2>&1"
for attempt in {1..30}; do
if docker exec spark-master test -s "${SPARK_PID_FILE}"; then
spark_pid="$(docker exec spark-master cat "${SPARK_PID_FILE}" | tr -d '\r')"
docker exec spark-master kill -0 "${spark_pid}" 2>/dev/null && break
fi
sleep 1
done
docker exec spark-master kill -0 "${spark_pid:-0}" 2>/dev/null \
|| die "Spark data-processor failed; inspect docker exec spark-master cat ${SPARK_LOG_FILE}"
if [[ "${EMBEDDING_ENABLED:-false}" == "true" ]]; then
note "Starting optional vLLM embedding infrastructure"
if [[ "${OPENREC_REUSE_IMAGES:-false}" != true ]]; then
"${WORKSPACE}/bigdata-platform/platform.sh" build vllm
fi
"${WORKSPACE}/bigdata-platform/platform.sh" up vllm
embedding_ready=false
for attempt in {1..60}; do
if "${WORKSPACE}/bigdata-platform/platform.sh" smoke vllm; then
embedding_ready=true
break
fi
sleep 5
done
[[ "${embedding_ready}" == "true" ]] || die "vLLM embedding service did not become ready"
python3 "${WORKSPACE}/openrec/scripts/runtime/verify_embedding.py" \
--items "${OPENREC_DATA_DIR}/item.csv" \
--vectors "${OPENREC_MODEL_ROOT}/recall/item/item_title_emb.csv"
fi
openrec_initialize
note "Starting rec-server, rank-engine, rec-algorithm runner, and rec-console containers"
if [[ "${OPENREC_REUSE_IMAGES:-false}" == true ]]; then
source "${WORKSPACE}/openrec/scripts/lib/local-source.sh"
openrec_start_local_source cluster
else
docker compose -f "${SCRIPT_DIR}/docker-compose.yml" up -d --build --wait --wait-timeout 300
fi
if [[ -n "${OPENREC_GRAPH_NODE_TIMEOUT_MS:-}" ]]; then
note "Configuring graph deadlines for functional acceptance"
python3 "${WORKSPACE}/openrec/scripts/runtime/configure-functional-graph.py" \
--node-timeout-ms "${OPENREC_GRAPH_NODE_TIMEOUT_MS}"
fi
"${SCRIPT_DIR}/verify_feature_parity_e2e.sh"
if [[ "${OPENREC_FIXTURE_INPUT}" == true ]]; then
run_airflow_dag "openrec_cluster_bootstrap" "openrec-start-$(date -u +%Y%m%dT%H%M%SZ)"
else
python3 "${WORKSPACE}/openrec/scripts/runtime/wait-recommendation-ready.py" \
--user-id "${smoke_user}" --scene "${smoke_scene}" \
--item-query "${smoke_item_query}" --user-query "${smoke_user_query}" --target item --target user
openrec_verify_custom_data cluster
fi
port_in_use 12345 && die "Web Demo port 12345 is already occupied"
start_jar "Web Demo" "${STATE_DIR}/web.pid" "${LOG_DIR}/web.log" \
"${JAVA}" -jar "${BUILD_DIR}/openrec/web/target/rec-example-web-0.2.0.jar" \
--server.port=12345 --demo.exposure-mode=viewport
wait_for_url "Web Demo" http://127.0.0.1:12345/
cat <<EOF
Cluster recommendation chain is ready.
Web Demo: http://127.0.0.1:12345
API health: http://127.0.0.1:13579/health
Rank health: http://127.0.0.1:8123/health
Console: http://127.0.0.1:8095 (or http://<host>:8095)
Console API: http://127.0.0.1:8095/docs
Airflow UI: http://127.0.0.1:8091
Spark UI: http://127.0.0.1:8083
Flink UI: http://127.0.0.1:8087
Stop apps: ${SCRIPT_DIR}/stop.sh
EOF
trap - ERR