@@ -503,18 +503,59 @@ function waitEC2Ready() {
503
503
done
504
504
}
505
505
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
506
513
function createDockerMachine() {
507
514
echo " Attempt to create a docker machine..."
508
515
docker-machine create --driver=amazonec2 \
509
516
--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 ) &
516
523
}
517
524
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
+
518
559
if [[ " $RUN_ON " = " localhost" ]]; then
519
560
if [ -z ${CONTAINER_ID+x} ]; then
520
561
containerHash=$( docker run --name=" pg_nancy_${CURRENT_TS} " \
@@ -543,11 +584,22 @@ elif [[ "$RUN_ON" = "aws" ]]; then
543
584
echo " Increased price: $price "
544
585
EC2_PRICE=$price
545
586
546
- createDockerMachine;
587
+ createDockerMachine $DOCKER_MACHINE $AWS_EC2_TYPE $EC2_PRICE \
588
+ 60 $AWS_KEY_PAIR $AWS_KEY_PATH ;
547
589
status=$( waitEC2Ready " docker-machine create" " $DOCKER_MACHINE " 1)
548
590
if [ " $status " == " price-too-low" ]
549
591
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
+
551
603
corrrectPriceForLastFailedRequest=$( \
552
604
aws ec2 describe-spot-instance-requests \
553
605
--filters=" Name=launch.instance-type,Values=$AWS_EC2_TYPE " \
@@ -562,7 +614,8 @@ elif [[ "$RUN_ON" = "aws" ]]; then
562
614
DOCKER_MACHINE=" ${DOCKER_MACHINE// _/ -} "
563
615
# try start docker machine name with new price
564
616
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 ;
566
619
waitEC2Ready " docker-machine create" " $DOCKER_MACHINE " 0;
567
620
else
568
621
>&2 echo " ERROR: Cannot determine actual price for the instance $AWS_EC2_TYPE ."
@@ -592,28 +645,6 @@ else
592
645
exit 1
593
646
fi
594
647
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
-
617
648
alias docker_exec=' docker $dockerConfig exec -i ${containerHash} '
618
649
619
650
MACHINE_HOME=" /machine_home/nancy_${containerHash} "
@@ -751,10 +782,10 @@ echo -e "Report: $ARTIFACTS_DESTINATION/$ARTIFACTS_FILENAME.json"
751
782
echo -e " Query log: $ARTIFACTS_DESTINATION /$ARTIFACTS_FILENAME .log.gz"
752
783
echo -e " -------------------------------------------"
753
784
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' )
756
785
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' )
758
789
echo -e " -------------------------------------------"
759
790
760
791
sleep $DEBUG_TIMEOUT
0 commit comments