forked from awslabs/amazon-redshift-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptRunner.sh
More file actions
executable file
·114 lines (96 loc) · 2.62 KB
/
ScriptRunner.sh
File metadata and controls
executable file
·114 lines (96 loc) · 2.62 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
#!/bin/bash
# Starts multiple sessions/threads to execute a list of SQL scripts
# $1 Number of Concurrent Threads
# $2 List of scripts to run
: '
* Copyright 2018, Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
10/26/2016 : Initial Release.
'
export PGPORT=`ls /tmp/.s.PGSQL* | grep -v lock | cut -c15-`
# Functions
show_help()
{
echo "Usage: $0 <Number of Threads> [ <script list | sqls > ]"
}
run()
{
# Add the list of scripts to run on a file names sqls
#
# $1 = Concurrency
# $2 = run (to diferentiate runs)
# $3 = sqls file
# $4 = fork indicator - call came from the fork runner
sqls=$3
logdate=`date '+%Y%m%d%H%M%S'`
tot_sqls=`wc $sqls | awk '{print $1}'`
pos=0
if [ "$4" != "fork" ]; then
echo 0 > pos1; echo 0 > pos2;
fi
while [ $pos -le $tot_sqls ]
do
#share the sql queue across forks
safe="0"
while [ $safe -lt "1" ]
do
mkdir ./lock$2 2> /dev/null; lock_state=$?
# Make sure it is locked so there isn't race condition.
if [ "$lock_state" = "0" ]; then
pos=`cat pos$2`; pos=$((pos + 1)); echo $pos > pos$2
safe=2; rm -rf lock$2
else
sleep 1
fi
done
# Execute the SQL on Redshift via psql client
if [ $pos -le $tot_sqls ]; then
# Parse the next SQL Script
script=`cat $sqls | sed -n ${pos}p`
if [ -z "${script// }" ]; then
continue
fi
#log_out=log_out_thread_${2}_$1_$logdate
# if you want to send result-set to /dev/null
log_out=/dev/null
log_status=log_status_tread_${2}
# Uses the same DB os the original file
db=`basename $script | cut -d '-' -f 1`
echo "`date '+%Y%m%d%H%M%S'` " start $script >> $log_out
ds=$(date '+%s')
psql -v ON_ERROR_STOP=1 -f $script -p $PGPORT $db >> $log_out 2>&1
succ=$?
de=$(date '+%s')
# Dump some information such as exit status and runtime to log files
echo "`date '+%Y%m%d%H%M%S'` " done $script >> log_out
echo "$logdate $1 $script runtime " $succ $((de - ds)) >> $log_status
fi
done
}
# End Functions
if [ "$#" -le 0 ]; then
show_help
exit 0
fi
threads=${1:-1}
sqls=${2:-sqls}
echo -n STARTED $threads Thread\(s\) running $sqls on ' '
date
echo 0 > pos1; echo 0 > pos2;
for i in $(seq 1 $1)
do
echo "started instance no: $i"
run $i 1 $sqls fork 2> error_${1}_1 &
done
wait
echo -n ENDED ' '
date