Skip to content

Instantly share code, notes, and snippets.

@nineinchnick
Last active March 22, 2024 09:44
Show Gist options
  • Save nineinchnick/399755ccfd10bba4cf8cbea9e8391221 to your computer and use it in GitHub Desktop.
Save nineinchnick/399755ccfd10bba4cf8cbea9e8391221 to your computer and use it in GitHub Desktop.
Install Trino and some plugins
#!/usr/bin/env bash
# This script:
# * installs Trino and Temurin JDK 21 in /home/trino
# * configures Trino to listen on port 8080, use it with a reverse proxy like Caddy
# * installs a SystemD service to run Trino on server startup
set -euo pipefail
usage() {
cat <<EOF >&2
Usage: $0 [-h] -t <VERSION>
Install and run Trino
-h Display help
-t Trino version
EOF
}
VERSION=443
while getopts ":s:t:h" OPTKEY; do
case "${OPTKEY}" in
t)
VERSION="$OPTARG"
;;
h)
usage
exit 0
;;
'?')
echo >&2 "ERROR: INVALID OPTION -- ${OPTARG}"
usage
exit 1
;;
':')
echo >&2 "MISSING ARGUMENT for option -- ${OPTARG}"
usage
exit 1
;;
*)
echo >&2 "ERROR: UNKNOWN OPTION -- ${OPTARG}"
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
[[ ${1:-} == "--" ]] && shift
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
cd "$SCRIPT_DIR" || exit 1
id trino || useradd --shell /bin/bash trino
passwd -d trino
mkdir -p /home/trino/{var/log,var/run,etc,bin}
#artifact=io.trino:trino-server:${VERSION}:tar.gz
#mvn -q -C dependency:get -Dtransitive=false "-Dartifact=$artifact"
url=https://repo1.maven.org/maven2/io/trino/trino-server/$VERSION/trino-server-$VERSION.tar.gz
base=$(basename "$url")
if [ ! -f "$base" ]; then
curl -fLsS -o "$base" "$url"
fi
if [ ! -d trino-server-$VERSION ]; then
tar xf "$base"
rm -rf /home/trino/{lib,plugin}
cp -a trino-server-$VERSION/* /home/trino
fi
jdk_version=21.0.2+13
url=https://github.com/adoptium/temurin21-binaries/releases/download/jdk-${jdk_version/+/%2B}/OpenJDK21U-jdk_x64_linux_hotspot_${jdk_version/+/_}.tar.gz
if [ ! -f jdk.tar.gz ]; then
curl -fLsS -o jdk.tar.gz "$url"
fi
if [ ! -d /home/trino/jdk ]; then
tar xf jdk.tar.gz
mv jdk-$jdk_version /home/trino/jdk
fi
# TODO download and install plugins
plugins=(
https://github.com/nineinchnick/trino-git/releases/download/v0.72/trino-git-0.72.zip
https://github.com/nineinchnick/trino-rest/releases/download/v0.138/trino-rest-github-0.138.zip
https://github.com/nineinchnick/trino-cloud/releases/download/v0.56/trino-cloud-aws-0.56.zip
https://github.com/nineinchnick/trino-faker/releases/download/v0.49/trino-faker-0.49.zip
https://github.com/nineinchnick/trino-openapi/releases/download/v1.42/trino-openapi-1.42.zip
https://github.com/snowlift/trino-storage/releases/download/v443/trino-storage-443.zip
)
for plugin in "${plugins[@]}"; do
base=$(basename "$plugin" .zip)
if [ ! -f "$base".zip ]; then
curl -fLsS -o "$base".zip "$plugin"
unzip "$base".zip
rm -rf /home/trino/plugin/${base//[0-9.]/}*
mv "$base" /home/trino/plugin/
fi
done
cat <<EOF > /lib/systemd/system/trino.service
[Unit]
Description=Trino
After=network.target
StartLimitIntervalSec=600
StartLimitBurst=3
[Service]
User=trino
Group=trino
Type=forking
RuntimeDirectory=trino
PIDFile=/home/trino/var/run/launcher.pid
LimitNOFILE=131072
LimitNPROC=131072
# Consinder exit on SIGTERM a successful exit
SuccessExitStatus=143
Restart=on-failure
RestartSec=5
ExecStart=/home/trino/bin/run-trino.sh start
ExecStop=/home/trino/bin/run-trino.sh stop
[Install]
WantedBy=multi-user.target
EOF
cat <<'EOF' > /home/trino/etc/env.sh
JAVA_HOME=/home/trino/jdk
EOF
cat <<'EOF' > /home/trino/bin/run-trino.sh
#!/usr/bin/env bash
# Launcher Config.
# Use data-dir from node.properties file (assumes it to be at /home/trino/etc). For other args use defaults from rpm install
NODE_PROPERTIES=/home/trino/etc/node.properties
DATA_DIR=$(grep -Po "(?<=^node.data-dir=).*" $NODE_PROPERTIES | tail -n 1)
SERVER_LOG_FILE=$(grep -Po "(?<=^node.server-log-file=).*" $NODE_PROPERTIES | tail -n 1)
LAUNCHER_LOG_FILE=$(grep -Po "(?<=^node.launcher-log-file=).*" $NODE_PROPERTIES | tail -n 1)
CONFIGURATION=(
--launcher-config /home/trino/bin/launcher.properties
--pid-file /home/trino/var/run/launcher.pid
--etc-dir /home/trino/etc
--data-dir "$DATA_DIR"
--node-config "$NODE_PROPERTIES"
--jvm-config /home/trino/etc/jvm.config
--config /home/trino/etc/config.properties
--launcher-log-file "${LAUNCHER_LOG_FILE:-/home/trino/var/log/launcher.log}"
--server-log-file "${SERVER_LOG_FILE:-/home/trino/var/log/server.log}"
)
declare -A CONFIG_ENV
source /home/trino/etc/env.sh
options=()
if [ -z "${JAVA_HOME}" ]; then
# Don't expand JAVA_HOME in the error message
# shellcheck disable=2016
echo 'Warning: No value found for $JAVA_HOME. Default Java will be used.' >&2
else
options+=("PATH=${JAVA_HOME}/bin:$PATH")
fi
for key in "${!CONFIG_ENV[@]}"; do
options+=("$key=${CONFIG_ENV[$key]}")
done
env "${options[@]}" /home/trino/bin/launcher "$1" "${CONFIGURATION[@]}"
EOF
chmod +x /home/trino/bin/run-trino.sh
cat <<EOF > /home/trino/etc/config.properties
#single node install config
coordinator=true
node-scheduler.include-coordinator=true
http-server.http.port=8080
http-server.process-forwarded=true
discovery.uri=http://localhost:8080
catalog.management=dynamic
EOF
cat <<EOF > /home/trino/etc/node.properties
node.environment=trino_rocks
node.data-dir=/home/trino/var
plugin.dir=/home/trino/plugin
EOF
cat <<EOF > /home/trino/etc/jvm.config
-server
-XX:InitialRAMPercentage=80
-XX:MaxRAMPercentage=80
-XX:G1HeapRegionSize=32M
-XX:+ExplicitGCInvokesConcurrent
-XX:+HeapDumpOnOutOfMemoryError
-XX:+ExitOnOutOfMemoryError
-XX:-OmitStackTraceInFastThrow
-XX:ReservedCodeCacheSize=256M
-XX:PerMethodRecompilationCutoff=10000
-XX:PerBytecodeRecompilationCutoff=10000
-Djdk.attach.allowAttachSelf=true
-Djdk.nio.maxCachedBufferSize=2000000
# Improve AES performance for S3, etc. on ARM64 (JDK-8271567)
-XX:+UnlockDiagnosticVMOptions
-XX:+UseAESCTRIntrinsics
# Reduce starvation of threads by GClocker (UnlockDiagnosticVMOptions dependent)
-XX:GCLockerRetryAllocationCount=100
# Prevent GC for performance reasons (JDK-8293861)
-XX:-G1UsePreventiveGC
# This flag is required by Snowflake connector
--add-opens=java.base/java.nio=ALL-UNNAMED
-Dfile.encoding=UTF-8
EOF
cat <<EOF > /home/trino/etc/log.properties
# Enable verbose logging from Trino
#io.trino=DEBUG
#pl.net.was=DEBUG
EOF
chown -R trino:trino /home/trino
systemctl daemon-reload
systemctl enable trino
systemctl restart trino
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment