|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +export OPENSEARCH_HOME=/usr/share/opensearch |
| 5 | +export OPENSEARCH_PATH_CONF=$OPENSEARCH_HOME/config |
| 6 | + |
| 7 | +# ---- openj9 jvm options (replaces jvm.options + JvmOptionsParser) ---- |
| 8 | +# we build the jvm flags ourselves instead of using JvmOptionsParser which |
| 9 | +# hardcodes hotspot flags and fights openj9. we read jvm.options for the |
| 10 | +# non-gc, non-logging lines (heap size, system properties, etc.) and |
| 11 | +# append our openj9-specific flags. |
| 12 | + |
| 13 | +build_jvm_opts() { |
| 14 | + local opts="" |
| 15 | + |
| 16 | + # read jvm.options, skip comments, blanks, and hotspot-specific lines |
| 17 | + if [ -f "$OPENSEARCH_PATH_CONF/jvm.options" ]; then |
| 18 | + while IFS= read -r line; do |
| 19 | + # skip comments and blanks |
| 20 | + [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue |
| 21 | + # skip version-gated lines (e.g. 8-10:, 11-:, 9-:) |
| 22 | + [[ "$line" =~ ^[0-9]+-?[0-9]*:- ]] && { |
| 23 | + # strip the version prefix to get the actual flag |
| 24 | + local flag="${line#*:-}" |
| 25 | + # skip all hotspot gc, gc tuning, and gc logging flags |
| 26 | + [[ "$flag" =~ UseG1GC|UseConcMarkSweepGC|UseSerialGC ]] && continue |
| 27 | + [[ "$flag" =~ G1ReservePercent|InitiatingHeapOccupancyPercent ]] && continue |
| 28 | + [[ "$flag" =~ CMSInitiatingOccupancyFraction|UseCMSInitiatingOccupancyOnly ]] && continue |
| 29 | + [[ "$flag" =~ CICompilerCount|TieredCompilation ]] && continue |
| 30 | + [[ "$flag" =~ InitialCodeCacheSize|InitialBootClassLoaderMetaspaceSize ]] && continue |
| 31 | + [[ "$flag" =~ Xlog: ]] && continue |
| 32 | + opts="$opts $flag" |
| 33 | + continue |
| 34 | + } |
| 35 | + # skip non-version-gated hotspot flags too |
| 36 | + [[ "$line" =~ UseG1GC|UseConcMarkSweepGC|UseSerialGC ]] && continue |
| 37 | + [[ "$line" =~ G1ReservePercent|InitiatingHeapOccupancyPercent ]] && continue |
| 38 | + [[ "$line" =~ CMSInitiatingOccupancyFraction|UseCMSInitiatingOccupancyOnly ]] && continue |
| 39 | + [[ "$line" =~ CICompilerCount|TieredCompilation ]] && continue |
| 40 | + [[ "$line" =~ InitialCodeCacheSize|InitialBootClassLoaderMetaspaceSize ]] && continue |
| 41 | + [[ "$line" =~ Xlog: ]] && continue |
| 42 | + # skip template variables that jvm.options uses (resolved by JvmOptionsParser normally) |
| 43 | + [[ "$line" =~ ^\$\{ ]] && continue |
| 44 | + opts="$opts $line" |
| 45 | + continue |
| 46 | + done < "$OPENSEARCH_PATH_CONF/jvm.options" |
| 47 | + fi |
| 48 | + |
| 49 | + # also read jvm.options.d/*.options if present |
| 50 | + if [ -d "$OPENSEARCH_PATH_CONF/jvm.options.d" ]; then |
| 51 | + for f in "$OPENSEARCH_PATH_CONF/jvm.options.d"/*.options; do |
| 52 | + [ -f "$f" ] || continue |
| 53 | + while IFS= read -r line; do |
| 54 | + [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue |
| 55 | + [[ "$line" =~ UseG1GC|UseConcMarkSweepGC|UseSerialGC ]] && continue |
| 56 | + [[ "$line" =~ Xlog: ]] && continue |
| 57 | + [[ "$line" =~ ^\$\{ ]] && continue |
| 58 | + opts="$opts $line" |
| 59 | + done < "$f" |
| 60 | + done |
| 61 | + fi |
| 62 | + |
| 63 | + # openj9 flags |
| 64 | + opts="$opts -XX:+IdleTuningGcOnIdle" |
| 65 | + opts="$opts -XX:IdleTuningMinIdleWaitTime=240" |
| 66 | + opts="$opts -Xshareclasses:none" |
| 67 | + opts="$opts -Xquickstart" |
| 68 | + opts="$opts -Xmns8m" |
| 69 | + opts="$opts -Xmnx64m" |
| 70 | + opts="$opts -Xsoftrefthreshold1" |
| 71 | + opts="$opts -XX:+ExitOnOutOfMemoryError" |
| 72 | + |
| 73 | + # resolve heap dump path and error file (normally done by JvmOptionsParser) |
| 74 | + opts="$opts -XX:+HeapDumpOnOutOfMemoryError" |
| 75 | + opts="$opts -XX:HeapDumpPath=data" |
| 76 | + opts="$opts -XX:ErrorFile=logs/hs_err_pid%p.log" |
| 77 | + |
| 78 | + # tmpdir |
| 79 | + export OPENSEARCH_TMPDIR=$(mktemp -d -t opensearch.XXXXXXXX) |
| 80 | + opts="$opts -Djava.io.tmpdir=$OPENSEARCH_TMPDIR" |
| 81 | + |
| 82 | + # user-provided opts (from k8s env) go last so they override |
| 83 | + opts="$opts ${OPENSEARCH_JAVA_OPTS:-}" |
| 84 | + |
| 85 | + echo "$opts" |
| 86 | +} |
| 87 | + |
| 88 | +# ---- security plugin setup ---- |
| 89 | +setup_security() { |
| 90 | + local SECURITY_PLUGIN="opensearch-security" |
| 91 | + if [ -d "$OPENSEARCH_HOME/plugins/$SECURITY_PLUGIN" ]; then |
| 92 | + if [ "$DISABLE_INSTALL_DEMO_CONFIG" = "true" ]; then |
| 93 | + echo "Disabling execution of install_demo_configuration.sh for OpenSearch Security Plugin" |
| 94 | + else |
| 95 | + echo "Enabling execution of install_demo_configuration.sh for OpenSearch Security Plugin" |
| 96 | + bash "$OPENSEARCH_HOME/plugins/$SECURITY_PLUGIN/tools/install_demo_configuration.sh" -y -i -s |
| 97 | + fi |
| 98 | + |
| 99 | + if [ "$DISABLE_SECURITY_PLUGIN" = "true" ]; then |
| 100 | + echo "Disabling OpenSearch Security Plugin" |
| 101 | + opensearch_opts+=("-Eplugins.security.disabled=true") |
| 102 | + fi |
| 103 | + fi |
| 104 | +} |
| 105 | + |
| 106 | +# ---- performance analyzer setup ---- |
| 107 | +setup_performance_analyzer() { |
| 108 | + local PA_PLUGIN="opensearch-performance-analyzer" |
| 109 | + if [ -d "$OPENSEARCH_HOME/plugins/$PA_PLUGIN" ]; then |
| 110 | + if [ "$DISABLE_PERFORMANCE_ANALYZER_AGENT_CLI" = "true" ]; then |
| 111 | + echo "Disabling execution of performance-analyzer-agent-cli for OpenSearch Performance Analyzer Plugin" |
| 112 | + else |
| 113 | + echo "Enabling execution of performance-analyzer-agent-cli for OpenSearch Performance Analyzer Plugin" |
| 114 | + "$OPENSEARCH_HOME/bin/opensearch-performance-analyzer/performance-analyzer-agent-cli" \ |
| 115 | + > "$OPENSEARCH_HOME/logs/performance-analyzer.log" 2>&1 & |
| 116 | + disown |
| 117 | + fi |
| 118 | + fi |
| 119 | +} |
| 120 | + |
| 121 | +# ---- main ---- |
| 122 | +run_opensearch() { |
| 123 | + local opensearch_opts=() |
| 124 | + |
| 125 | + # collect -E flags from environment variables |
| 126 | + while IFS='=' read -r envvar_key envvar_value; do |
| 127 | + if [[ "$envvar_key" =~ ^[a-z0-9_]+\.[a-z0-9_]+\.?[a-z0-9_]*$ ]]; then |
| 128 | + opensearch_opts+=("-E${envvar_key}=${envvar_value}") |
| 129 | + fi |
| 130 | + done < <(env) |
| 131 | + |
| 132 | + # save user's OPENSEARCH_JAVA_OPTS before cli tools clobber it |
| 133 | + local user_java_opts="${OPENSEARCH_JAVA_OPTS:-}" |
| 134 | + |
| 135 | + # cli tools (keystore, security setup) need a clean OPENSEARCH_JAVA_OPTS |
| 136 | + # without our openj9 flags — they use opensearch-cli which prepends its own flags |
| 137 | + export OPENSEARCH_JAVA_OPTS="" |
| 138 | + |
| 139 | + setup_security |
| 140 | + setup_performance_analyzer |
| 141 | + |
| 142 | + # now build the real jvm opts for the main opensearch process |
| 143 | + export OPENSEARCH_JAVA_OPTS="$user_java_opts" |
| 144 | + local jvm_opts |
| 145 | + jvm_opts=$(build_jvm_opts) |
| 146 | + |
| 147 | + # start opensearch directly with our jvm opts, bypassing JvmOptionsParser |
| 148 | + cd "$OPENSEARCH_HOME" |
| 149 | + exec "$OPENSEARCH_HOME/jdk/bin/java" \ |
| 150 | + $jvm_opts \ |
| 151 | + -Dopensearch.path.home="$OPENSEARCH_HOME" \ |
| 152 | + -Dopensearch.path.conf="$OPENSEARCH_PATH_CONF" \ |
| 153 | + -Dopensearch.distribution.type=docker \ |
| 154 | + -cp "$OPENSEARCH_HOME/lib/*:$OPENSEARCH_HOME/lib/tools/launchers/*" \ |
| 155 | + org.opensearch.bootstrap.OpenSearch \ |
| 156 | + "${opensearch_opts[@]}" |
| 157 | +} |
| 158 | + |
| 159 | +if [ $# -eq 0 ] || [ "${1:0:1}" = '-' ]; then |
| 160 | + set -- opensearch "$@" |
| 161 | +fi |
| 162 | + |
| 163 | +if [ "$1" = "opensearch" ]; then |
| 164 | + shift |
| 165 | + run_opensearch "$@" |
| 166 | +else |
| 167 | + exec "$@" |
| 168 | +fi |
0 commit comments