Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit d45d6e3

Browse files
authored
Merge pull request #47 from startupturbo/price_too_low_cleanup
Cleanup spot requests with price-too-low status
2 parents 67dc0ce + d54a2ea commit d45d6e3

File tree

1 file changed

+65
-34
lines changed

1 file changed

+65
-34
lines changed

nancy_run.sh

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,59 @@ function waitEC2Ready() {
503503
done
504504
}
505505

506+
# Params:
507+
# 1) machine name
508+
# 2) AWS EC2 instance type
509+
# 3) price
510+
# 4) duration (minutes)
511+
# 5) key pair name
512+
# 6) key path
506513
function createDockerMachine() {
507514
echo "Attempt to create a docker machine..."
508515
docker-machine create --driver=amazonec2 \
509516
--amazonec2-request-spot-instance \
510-
--amazonec2-keypair-name="$AWS_KEY_PAIR" \
511-
--amazonec2-ssh-keypath="$AWS_KEY_PATH" \
512-
--amazonec2-block-duration-minutes=60 \
513-
--amazonec2-instance-type=$AWS_EC2_TYPE \
514-
--amazonec2-spot-price=$EC2_PRICE \
515-
$DOCKER_MACHINE &
517+
--amazonec2-keypair-name="$5" \
518+
--amazonec2-ssh-keypath="$6" \
519+
--amazonec2-block-duration-minutes=$4 \
520+
--amazonec2-instance-type=$2 \
521+
--amazonec2-spot-price=$3 \
522+
$1 2> >(grep -v "failed waiting for successful resource state" >&2) &
516523
}
517524

525+
function destroyDockerMachine() {
526+
# If spot request wasn't fulfilled, there is no associated instance,
527+
# so "docker-machine rm" will show an error, which is safe to ignore.
528+
# We better filter it out to avoid any confusions.
529+
# What is used here is called "process substitution",
530+
# see https://www.gnu.org/software/bash/manual/bash.html#Process-Substitution
531+
# The same trick is used in createDockerMachine to filter out errors
532+
# when we have "price-too-low" attempts, such errors come in few minutes
533+
# after an attempt and are generally unexpected by user.
534+
cmdout=$(docker-machine rm --force $1 2> >(grep -v "unknown instance" >&2) )
535+
echo "Termination requested for machine '$1', current status: $cmdout"
536+
}
537+
538+
function cleanupAndExit {
539+
echo "Remove temp files..." # if exists
540+
rm -f "$TMP_PATH/after_db_init_code_tmp.sql"
541+
rm -f "$TMP_PATH/workload_custom_sql_tmp.sql"
542+
rm -f "$TMP_PATH/target_ddl_do_tmp.sql"
543+
rm -f "$TMP_PATH/target_ddl_undo_tmp.sql"
544+
rm -f "$TMP_PATH/target_config_tmp.conf"
545+
rm -f "$TMP_PATH/pg_config_tmp.conf"
546+
if [ "$RUN_ON" = "localhost" ]; then
547+
rm -rf "$TMP_PATH/nancy_${containerHash}"
548+
echo "Remove docker container"
549+
docker container rm -f $containerHash
550+
elif [ "$RUN_ON" = "aws" ]; then
551+
destroyDockerMachine $DOCKER_MACHINE
552+
else
553+
>&2 echo "ASSERT: must not reach this point"
554+
exit 1
555+
fi
556+
}
557+
trap cleanupAndExit EXIT
558+
518559
if [[ "$RUN_ON" = "localhost" ]]; then
519560
if [ -z ${CONTAINER_ID+x} ]; then
520561
containerHash=$(docker run --name="pg_nancy_${CURRENT_TS}" \
@@ -543,11 +584,22 @@ elif [[ "$RUN_ON" = "aws" ]]; then
543584
echo "Increased price: $price"
544585
EC2_PRICE=$price
545586

546-
createDockerMachine;
587+
createDockerMachine $DOCKER_MACHINE $AWS_EC2_TYPE $EC2_PRICE \
588+
60 $AWS_KEY_PAIR $AWS_KEY_PATH;
547589
status=$(waitEC2Ready "docker-machine create" "$DOCKER_MACHINE" 1)
548590
if [ "$status" == "price-too-low" ]
549591
then
550-
echo "Price $price is too low for $AWS_EC2_TYPE instance. Try detect actual."
592+
echo "Price $price is too low for $AWS_EC2_TYPE instance. Getting the up-to-date value from the error message..."
593+
594+
#destroyDockerMachine $DOCKER_MACHINE
595+
# "docker-machine rm" doesn't work for "price-too-low" spot requests,
596+
# so we need to clean up them via aws cli interface directly
597+
aws ec2 describe-spot-instance-requests \
598+
--filters 'Name=status-code,Values=price-too-low' \
599+
| grep SpotInstanceRequestId | awk '{gsub(/[,"]/, "", $2); print $2}' \
600+
| xargs --no-run-if-empty aws ec2 cancel-spot-instance-requests \
601+
--spot-instance-request-ids
602+
551603
corrrectPriceForLastFailedRequest=$( \
552604
aws ec2 describe-spot-instance-requests \
553605
--filters="Name=launch.instance-type,Values=$AWS_EC2_TYPE" \
@@ -562,7 +614,8 @@ elif [[ "$RUN_ON" = "aws" ]]; then
562614
DOCKER_MACHINE="${DOCKER_MACHINE//_/-}"
563615
#try start docker machine name with new price
564616
echo "Attempt to create a new docker machine: $DOCKER_MACHINE with price: $EC2_PRICE."
565-
createDockerMachine;
617+
createDockerMachine $DOCKER_MACHINE $AWS_EC2_TYPE $EC2_PRICE \
618+
60 $AWS_KEY_PAIR $AWS_KEY_PATH;
566619
waitEC2Ready "docker-machine create" "$DOCKER_MACHINE" 0;
567620
else
568621
>&2 echo "ERROR: Cannot determine actual price for the instance $AWS_EC2_TYPE."
@@ -592,28 +645,6 @@ else
592645
exit 1
593646
fi
594647

595-
function cleanup {
596-
echo "Remove temp files..." # if exists
597-
rm -f "$TMP_PATH/after_db_init_code_tmp.sql"
598-
rm -f "$TMP_PATH/workload_custom_sql_tmp.sql"
599-
rm -f "$TMP_PATH/target_ddl_do_tmp.sql"
600-
rm -f "$TMP_PATH/target_ddl_undo_tmp.sql"
601-
rm -f "$TMP_PATH/target_config_tmp.conf"
602-
rm -f "$TMP_PATH/pg_config_tmp.conf"
603-
if [ "$RUN_ON" = "localhost" ]; then
604-
rm -rf "$TMP_PATH/nancy_${containerHash}"
605-
echo "Remove docker container"
606-
docker container rm -f $containerHash
607-
elif [ "$RUN_ON" = "aws" ]; then
608-
cmdout=$(docker-machine rm --force $DOCKER_MACHINE)
609-
echo "Finished working with machine $DOCKER_MACHINE, termination requested, current status: $cmdout"
610-
else
611-
>&2 echo "ASSERT: must not reach this point"
612-
exit 1
613-
fi
614-
}
615-
trap cleanup EXIT
616-
617648
alias docker_exec='docker $dockerConfig exec -i ${containerHash} '
618649

619650
MACHINE_HOME="/machine_home/nancy_${containerHash}"
@@ -751,10 +782,10 @@ echo -e "Report: $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json"
751782
echo -e "Query log: $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.log.gz"
752783
echo -e "-------------------------------------------"
753784
echo -e "Summary:"
754-
echo -e " Normalized queries number:\t\t" $(cat $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json | jq '.normalyzed_info| length')
755-
echo -e " Queries number:\t\t" $(cat $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json | jq '.overall_stat.queries_number')
756785
echo -e " Queries duration:\t\t" $(cat $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json | jq '.overall_stat.queries_duration') " ms"
757-
echo -e " Errors number:\t\t" $(cat $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json | jq '.overall_stat.errors_number')
786+
echo -e " Queries count:\t\t" $(cat $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json | jq '.overall_stat.queries_number')
787+
echo -e " Normalized queries count:\t" $(cat $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json | jq '.normalyzed_info| length')
788+
echo -e " Errors count:\t\t\t" $(cat $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json | jq '.overall_stat.errors_number')
758789
echo -e "-------------------------------------------"
759790

760791
sleep $DEBUG_TIMEOUT

0 commit comments

Comments
 (0)