-
Notifications
You must be signed in to change notification settings - Fork 19
/
user_data.sh
237 lines (199 loc) · 8.03 KB
/
user_data.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
#!/usr/bin/env bash
set -ex
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
cat <<'EOF' >/etc/security/limits.d/100-trino-nofile.conf
trino soft nofile 16384
trino hard nofile 16384
EOF
/usr/bin/printf "
node.environment=${environment_name}
node.id=$(hostname)
node.data-dir=/var/lib/trino/
" > /etc/trino/node.properties
/usr/bin/printf "-server
-Xmx${heap_size}G
-XX:-UseBiasedLocking
-XX:+UseG1GC
-XX:G1HeapRegionSize=32M
-XX:+ExplicitGCInvokesConcurrent
-XX:+HeapDumpOnOutOfMemoryError
-XX:+ExitOnOutOfMemoryError
-XX:+UseGCOverheadLimit
-XX:ReservedCodeCacheSize=512M
-Djdk.attach.allowAttachSelf=true
-Djdk.nio.maxCachedBufferSize=2000000
-Duser.timezone=UTC
" > /etc/trino/jvm.config
function setup_hive_metastore {
AV_ZONE="$(ec2metadata --availability-zone)"
ENVIRONMENT_NAME="$(aws ec2 describe-tags --region "${aws_region}" --filters Name=resource-id,Values=$(ec2metadata --instance-id) | jq -r '.Tags[] | select(.Key == "Environment") | .Value')"
echo "AV_ZONE: $AV_ZONE"
echo "ENVIRONMENT_NAME: $ENVIRONMENT_NAME"
while true; do
UNATTACHED_VOLUME_ID="$(aws ec2 describe-volumes --region ${aws_region} --filters Name=tag:Environment,Values=$ENVIRONMENT_NAME Name=tag:PrestoCoordinator,Values=true Name=availability-zone,Values=$AV_ZONE | jq -r '.Volumes[] | select(.Attachments | length == 0) | .VolumeId' | shuf -n 1)"
echo "UNATTACHED_VOLUME_ID: $UNATTACHED_VOLUME_ID"
aws ec2 attach-volume --device "/dev/xvdh" --instance-id=$(ec2metadata --instance-id) --volume-id "$UNATTACHED_VOLUME_ID" --region ${aws_region}
if [ "$?" != "0" ]; then
sleep 10
continue
fi
sleep 30
ATTACHMENTS_COUNT="$(aws ec2 describe-volumes --region "${aws_region}" --filters Name=volume-id,Values="$UNATTACHED_VOLUME_ID" | jq -r '.Volumes[0].Attachments | length')"
if [ "$ATTACHMENTS_COUNT" != "0" ]; then break; fi
done
echo 'Waiting for 30 seconds for the disk to become mountable...'
sleep 30
# Mount persistent storage and apply Hive Metastore schema if needed
DEVICE_NAME=$(lsblk -ip | tail -n +2 | awk '{print $1 " " ($7? "MOUNTEDPART" : "") }' | sed ':a;N;$!ba;s/\n`/ /g' | grep -v MOUNTEDPART | sed -e 's/[[:space:]]*$//')
MOUNT_PATH=/var/lib/mysql
sudo mv $MOUNT_PATH /tmp/mysql.backup
sudo mkdir -p $MOUNT_PATH
if sudo mount -o defaults -t ext4 "$DEVICE_NAME" $MOUNT_PATH; then
echo 'Successfully mounted existing disk'
else
echo 'Trying to mount a fresh disk'
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard "$DEVICE_NAME"
sudo mount -o defaults -t ext4 "$DEVICE_NAME" $MOUNT_PATH && echo 'Successfully mounted a fresh disk'
sudo cp -ar /tmp/mysql.backup/* $MOUNT_PATH/
fi
sudo chown mysql:mysql -R $MOUNT_PATH
sudo chmod 700 $MOUNT_PATH
service mysql start
systemctl enable mysql
. /etc/environment
export HADOOP_HOME=$HADOOP_HOME
if ! "$HIVE_HOME"/bin/schematool -validate -dbType mysql; then
echo "Mysql schema is not valid"
"$HIVE_HOME"/bin/schematool -dbType mysql -initSchema
fi
echo "Initializing Hive Metastore ($HIVE_HOME)..."
service hive-metastore start
systemctl enable hive-metastore
}
#
# Configure as COORDINATOR
#
if [[ "${mode_presto}" == "coordinator" ]]; then
echo "Configuring node as a [${mode_presto}]..."
/usr/bin/printf "
#
# coordinator
#
coordinator=true
discovery-server.enabled=true
discovery.uri=http://localhost:${http_port}
node-scheduler.include-coordinator=false
http-server.http.port=${http_port}
# query.max-memory-per-node has to be <= query.max-total-memory-per-node
#query.max-memory-per-node=${query_max_memory_per_node}GB
#query.max-total-memory-per-node=${query_max_total_memory_per_node}GB
query.max-memory=${query_max_memory}GB
# query.max-total-memory defaults to query.max-memory * 2 so we are good
${extra_worker_configs}
" > /etc/trino/config.properties
setup_hive_metastore
fi
#
# Configure as WORKER
#
if [[ "${mode_presto}" == "worker" ]]; then
echo "Configuring node as a [${mode_presto}]..."
/usr/bin/printf "
#
# worker
#
coordinator=false
discovery.uri=http://${address_presto_coordinator}:${http_port}
node-scheduler.include-coordinator=false
http-server.http.port=${http_port}
# query.max-memory-per-node has to be <= query.max-total-memory-per-node
#query.max-memory-per-node=${query_max_memory_per_node}GB
#query.max-total-memory-per-node=${query_max_total_memory_per_node}GB
query.max-memory=${query_max_memory}GB
# query.max-total-memory defaults to query.max-memory * 2 so we are good
${extra_worker_configs}
" > /etc/trino/config.properties
fi
#
# Configure as BOTH coordinator and worker
#
if [[ "${mode_presto}" == "coordinator-worker" ]]; then
echo "Configuring node as a [${mode_presto}]..."
/usr/bin/printf "
#
# coordinator-worker
#
coordinator=true
discovery-server.enabled=true
discovery.uri=http://localhost:${http_port}
node-scheduler.include-coordinator=true
http-server.http.port=${http_port}
# query.max-memory-per-node has to be <= query.max-total-memory-per-node
#query.max-memory-per-node=${query_max_memory_per_node}GB
#query.max-total-memory-per-node=${query_max_total_memory_per_node}GB
query.max-memory=${query_max_memory}GB
# query.max-total-memory defaults to query.max-memory * 2 so we are good
${extra_worker_configs}
" > /etc/trino/config.properties
setup_hive_metastore
fi
if [[ "${mode_presto}" == "worker" ]]; then
echo "Waiting for Presto Coordinator to come online at: http://${address_presto_coordinator}:${http_port}"
while ! nc -z ${address_presto_coordinator} ${http_port}; do
sleep 5
done
fi
if [ ! -z "${aws_access_key_id}" ] && [ ! -z "${aws_secret_access_key}" ]; then
# Update hive-site.xml
/usr/bin/printf "<configuration>
<property>
<name>fs.s3.impl</name>
<value>org.apache.hadoop.fs.s3native.NativeS3FileSystem</value>
</property>
<property>
<name>fs.s3.awsAccessKeyId</name>
<value>${aws_access_key_id}</value>
</property>
<property>
<name>fs.s3.awsSecretAccessKey</name>
<value>${aws_secret_access_key}</value>
</property>" > /tmp/hive-site-partial.txt
sudo sed -i "s/<configuration>/$(sed 's@[/\&]@\\&@g;$!s/$/\\/' /tmp/hive-site-partial.txt)/g" /usr/local/apache-hive-*-bin/conf/hive-site.xml
rm /tmp/hive-site-partial.txt
# Update hive.properties
/usr/bin/printf "\nhive.allow-drop-table=true" >> /etc/trino/catalog/hive.properties
/usr/bin/printf "\nhive.non-managed-table-writes-enabled=true" >> /etc/trino/catalog/hive.properties
/usr/bin/printf "\n#hive.time-zone=UTC" >> /etc/trino/catalog/hive.properties
/usr/bin/printf "\nhive.s3.aws-access-key=${aws_access_key_id}" >> /etc/trino/catalog/hive.properties
/usr/bin/printf "\nhive.s3.aws-secret-key=${aws_secret_access_key}" >> /etc/trino/catalog/hive.properties
/usr/bin/printf "\n" >> /etc/trino/catalog/hive.properties
/usr/bin/printf "\nhive.s3.aws-access-key=${aws_access_key_id}" >> /etc/trino/catalog/iceberg.properties
/usr/bin/printf "\nhive.s3.aws-secret-key=${aws_secret_access_key}" >> /etc/trino/catalog/iceberg.properties
/usr/bin/printf "\n" >> /etc/trino/catalog/iceberg.properties
fi
echo "Starting presto..."
systemctl enable trino.service
systemctl start trino.service
if [[ "${mode_presto}" == "coordinator" ]] || [[ "${mode_presto}" == "coordinator-worker" ]]; then
echo "Waiting for Presto Coordinator to start"
while ! presto --execute='select * from system.runtime.nodes'; do
sleep 10
done
echo "Presto Coordinator is now online"
fi
echo "Executing additional bootstrap scripts"
%{ for script in additional_bootstrap_scripts ~}
%{ if script.type == "s3" ~}
if [ ! -z "${aws_access_key_id}" ]; then
export AWS_ACCESS_KEY_ID=${aws_access_key_id}
export AWS_SECRET_ACCESS_KEY=${aws_secret_access_key}
fi
aws s3 cp "${script.script_url}" "/tmp/${script.script_name}"
%{ else ~}
curl "${script.script_url}" -o "/tmp/${script.script_name}"
%{ endif ~}
chmod +x "/tmp/${script.script_name}"
sh -c "/tmp/${script.script_name} %{ for param in script.params ~} ${param} %{ endfor ~}"
%{ endfor ~}
echo "Restarting Presto service"
systemctl restart trino