forked from FudanSELab/train-ticket-auto-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-bursty-service.sh
executable file
·337 lines (287 loc) · 12.6 KB
/
prepare-bursty-service.sh
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
#!/bin/bash
# Function for consistent log formatting
log_info() {
echo -e "\n[INFO] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_error() {
echo -e "\n[ERROR] $(date '+%Y-%m-%d %H:%M:%S') - $1" >&2
}
log_success() {
echo -e "\n[SUCCESS] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# List of all bursty services
BURSTY_SERVICES=(
"ts-cancel-service"
"ts-basic-service"
"ts-travel-service"
# "ts-preserve-service"
"ts-seat-service"
)
# Initialize array for tracking failed services
failed_services=()
# Function to randomly select a service
select_random_service() {
local array_length=${#BURSTY_SERVICES[@]}
local random_index=$((RANDOM % array_length))
echo "${BURSTY_SERVICES[$random_index]}"
}
# Check arguments and handle --no-groundtruth flag
if [ "$1" = "--no-groundtruth" ]; then
if [ "$#" -ne 5 ]; then
log_error "Missing required arguments"
echo "Prerequiste: 1. install maven, 2.login to dockerhub use \"docker login\", 3. make sure train-ticket repo is already in cacti-exp branch"
echo "Usage: $0 --no-groundtruth <bursty-period> <burst-rate> <burst-duration> <tag-name>"
echo "Example: $0 --no-groundtruth 60 5 10 v1.0.0"
echo "Parameters:"
echo " - bursty-period: Time between bursts in seconds (e.g., 60 for 1 minute)"
echo " - burst-rate: Requests per second during burst (e.g., 5)"
echo " - burst-duration: Duration of each burst in seconds (e.g., 10)"
exit 1
fi
TARGET_SERVICE=$(select_random_service)
BURSTY_PERIOD_SECONDS=$2
BURST_REQUESTS_PER_SEC=$3
BURST_DURATION_SECONDS=$4
TAG_NAME=$5
log_info "Random target service selected: $TARGET_SERVICE"
else
if [ "$#" -ne 5 ]; then
log_error "Missing required arguments"
echo "Prerequiste: 1. install maven, 2.login to dockerhub use \"docker login\", 3. make sure train-ticket repo is already in cacti-exp branch"
echo "Usage: $0 <target-service-name> <bursty-period> <burst-rate> <burst-duration> <tag-name>"
echo "Note: the first parameter could be --no-groundtruth flag that randomly select a trigger serivce to enable burstness"
echo "Example: $0 ts-cancel-service 60 5 10 v1.0.0"
echo "Parameters:"
echo " - bursty-period: Time between bursts in seconds (e.g., 60 for 1 minute)"
echo " - burst-rate: Requests per second during burst (e.g., 5)"
echo " - burst-duration: Duration of each burst in seconds (e.g., 10)"
exit 1
fi
TARGET_SERVICE=$1
BURSTY_PERIOD_SECONDS=$2
BURST_REQUESTS_PER_SEC=$3
BURST_DURATION_SECONDS=$4
TAG_NAME=$5
fi
log_info "Starting service update process with following parameters:"
echo "Target Service: $TARGET_SERVICE"
echo "Bursty Period: $BURSTY_PERIOD_SECONDS seconds"
echo "Burst Rate: $BURST_REQUESTS_PER_SEC requests/second"
echo "Burst Duration: $BURST_DURATION_SECONDS seconds"
echo "Tag Name: $TAG_NAME"
# Function to get controller path based on service name
get_controller_path() {
local service=$1
local service_part=$(echo $service | sed 's/ts-\(.*\)-service/\1/')
local controller_name="$(tr '[:lower:]' '[:upper:]' <<< ${service_part:0:1})${service_part:1}Controller.java"
echo "${service}/src/main/java/${service_part}/controller/${controller_name}"
}
# Function to clean up any local changes and update to latest remote version
cleanup_and_update() {
log_info "Starting repository cleanup and update"
log_info "Starting repository cleanup and update"
echo "Cleaning up local changes..."
git restore . 2>/dev/null || true
git clean -fd 2>/dev/null || true
echo "Switching to cacti-exp branch..."
git switch cacti-exp
echo "Pulling latest changes..."
git pull origin cacti-exp --ff-only
if [ $? -ne 0 ]; then
log_error "Failed to update to latest version. Exiting."
log_error "Failed to update to latest version. Exiting."
exit 1
fi
log_success "Repository cleanup and update completed"
}
# Function to get path based on service name
get_service_path() {
local service=$1
# Special path and naming for basic service
if [ "$service" = "ts-basic-service" ]; then
echo "${service}/src/main/java/fdse/microservice/service/BasicServiceImpl.java"
elif [ "$service" = "ts-cancel-service" ]; then
echo "${service}/src/main/java/cancel/service/CancelServiceImpl.java"
elif [ "$service" = "ts-seat-service" ]; then
echo "${service}/src/main/java/seat/service/SeatServiceImpl.java"
# elif [ "$service" = "ts-travel-service" ]; then
# echo "${service}/src/main/java/travel/service/TravelServiceImpl.java"
else
# Controller path for other services
local service_part=$(echo $service | sed 's/ts-\(.*\)-service/\1/')
local controller_name="$(tr '[:lower:]' '[:upper:]' <<< ${service_part:0:1})${service_part:1}Controller.java"
echo "${service}/src/main/java/${service_part}/controller/${controller_name}"
fi
}
# Function to update burst parameters in a service
update_service_params() {
local service=$1
local period=$2
local rate=$3
local duration=$4
log_info "Updating parameters for service: $service"
local file_path=$(get_service_path "$service")
if [ ! -f "$file_path" ]; then
log_error "File not found at $file_path"
return 1
fi
echo "Target file path: $file_path"
echo "Applying changes:"
# Create backup of original file
cp "$file_path" "${file_path}.bak"
if [ "$service" = "ts-basic-service" ] || [ "$service" = "ts-cancel-service" ] || [ "$service" = "ts-seat-service" ]; then
# BasicServiceImpl parameters
echo " - BURST_PERIOD_SECONDS: $period"
echo " - BURST_REQUESTS_PER_SEC: $rate"
echo " - BURST_DURATION_SECONDS: $duration"
sed -i "s/private static final int BURST_PERIOD_SECONDS = [0-9]\+;/private static final int BURST_PERIOD_SECONDS = ${period};/" "$file_path"
sed -i "s/private static final int BURST_REQUESTS_PER_SEC = [0-9]\+;/private static final int BURST_REQUESTS_PER_SEC = ${rate};/" "$file_path"
sed -i "s/private static final int BURST_DURATION_SECONDS = [0-9]\+;/private static final int BURST_DURATION_SECONDS = ${duration};/" "$file_path"
# Verify changes for BasicServiceImpl
if grep -q "BURST_PERIOD_SECONDS = ${period}" "$file_path" && \
grep -q "BURST_REQUESTS_PER_SEC = ${rate}" "$file_path" && \
grep -q "BURST_DURATION_SECONDS = ${duration}" "$file_path"; then
log_success "Successfully updated parameters for $service"
rm "${file_path}.bak"
else
log_error "Failed to update all parameters for $service"
log_info "Restoring backup file"
mv "${file_path}.bak" "$file_path"
return 1
fi
else
# Controller parameters for other services
echo " - BURSTY_PERIOD_SECONDS: $period"
echo " - BURST_REQUESTS_PER_SEC: $rate"
echo " - BURST_DURATION_SECONDS: $duration"
sed -i "s/private static final int BURSTY_PERIOD_SECONDS = [0-9]\+;/private static final int BURSTY_PERIOD_SECONDS = ${period};/" "$file_path"
sed -i "s/private static final int BURST_REQUESTS_PER_SEC = [0-9]\+;/private static final int BURST_REQUESTS_PER_SEC = ${rate};/" "$file_path"
sed -i "s/private static final int BURST_DURATION_SECONDS = [0-9]\+;/private static final int BURST_DURATION_SECONDS = ${duration};/" "$file_path"
# Verify changes for Controllers
if grep -q "BURSTY_PERIOD_SECONDS = ${period}" "$file_path" && \
grep -q "BURST_REQUESTS_PER_SEC = ${rate}" "$file_path" && \
grep -q "BURST_DURATION_SECONDS = ${duration}" "$file_path"; then
log_success "Successfully updated parameters for $service"
rm "${file_path}.bak"
else
log_error "Failed to update all parameters for $service"
log_info "Restoring backup file"
mv "${file_path}.bak" "$file_path"
return 1
fi
fi
}
# Function to build and push Docker image
build_and_push_docker() {
local service=$1
local tag=$2
log_info "Building and pushing Docker image for $service"
# Navigate to the service directory
cd "${service}" || { log_error "Failed to navigate to ${service} directory"; return 1; }
# Build Docker image
log_info "Building Docker image for $service:$tag"
if ! sudo docker build -t "docclabgroup/${service}:${tag}" .; then
log_error "Docker build failed for $service"
cd ..
return 1
fi
# Push Docker image
log_info "Pushing Docker image for $service:$tag"
if ! sudo docker push "docclabgroup/${service}:${tag}"; then
log_error "Docker push failed for $service"
cd ..
return 1
fi
cd .. || { log_error "Failed to navigate back from ${service} directory"; return 1; }
log_success "Successfully built and pushed Docker image for $service"
}
# Function to check rollout status
check_all_rollouts() {
local services=("$@")
local failed_rollouts=()
local success=true
log_info "Checking rollout status for all services"
for service in "${services[@]}"; do
log_info "Checking rollout status for $service"
if ! kubectl rollout status "deployment/${service}" --timeout=300s; then
log_error "Rollout failed for $service"
failed_rollouts+=("$service")
success=false
else
log_success "Rollout completed successfully for $service"
fi
done
if [ "$success" = false ]; then
log_error "The following services failed to roll out: ${failed_rollouts[*]}"
return 1
fi
log_success "All services rolled out successfully"
return 0
}
# Main execution starts here
log_info "Starting main execution"
cd /local/train-ticket || { log_error "Failed to navigate to train-ticket directory"; exit 1; }
sudo chown -R $(whoami) .
# Initial cleanup and update
cleanup_and_update
# First Phase: Update Parameters
log_info "Phase 1: Updating service parameters"
echo "Total services to process: ${#BURSTY_SERVICES[@]}"
for service in "${BURSTY_SERVICES[@]}"; do
echo -e "\n----------------------------------------"
log_info "Updating parameters for service: $service"
if [ "$service" = "$TARGET_SERVICE" ]; then
log_info "$service is the target service - applying specified burst parameters"
update_service_params "$service" "$BURSTY_PERIOD_SECONDS" "$BURST_REQUESTS_PER_SEC" "$BURST_DURATION_SECONDS"
else
log_info "$service is not the target service - setting zero burst parameters"
update_service_params "$service" "0" "0" "0"
fi
echo "----------------------------------------"
done
# Second Phase: Maven Build
log_info "Phase 2: Building project with Maven"
if ! mvn clean install -DskipTests; then
log_error "Maven build failed"
exit 1
fi
log_success "Maven build completed"
# Third Phase: Build and Push Docker Images
log_info "Phase 3: Building and pushing Docker images"
for service in "${BURSTY_SERVICES[@]}"; do
echo -e "\n----------------------------------------"
if ! build_and_push_docker "$service" "$TAG_NAME"; then
failed_services+=("$service")
fi
echo "----------------------------------------"
done
if [ ${#failed_services[@]} -ne 0 ]; then
log_error "Failed to build/push the following services: ${failed_services[*]}"
exit 1
fi
# Fourth Phase: Update Kubernetes Deployments
log_info "Phase 4: Updating Kubernetes deployments"
echo "Updating all service images simultaneously..."
for service in "${BURSTY_SERVICES[@]}"; do
log_info "Setting new image for $service"
kubectl set image "deployment/${service}" "${service}=docclabgroup/${service}:${TAG_NAME}" &
done
# Wait for all kubectl set image commands to complete
log_info "Waiting for all image updates to complete"
wait
log_success "All deployment image updates initiated"
# Fifth Phase: Check Rollout Status
log_info "Phase 5: Checking rollout status for all services"
if ! check_all_rollouts "${BURSTY_SERVICES[@]}"; then
log_error "Some deployments failed to roll out properly"
exit 1
fi
log_success "All deployments completed successfully!"
echo -e "\nFinal Configuration Summary:"
echo "Target Service: ${TARGET_SERVICE}"
echo "Target Configuration:"
echo "- Bursty Period: ${BURSTY_PERIOD_SECONDS} seconds"
echo "- Burst Rate: ${BURST_REQUESTS_PER_SEC} requests per second"
echo "- Burst Duration: ${BURST_DURATION_SECONDS} seconds"
echo "- Image Tag: ${TAG_NAME}"
echo "All other services have been set to zero burst parameters"