Skip to content

Commit a7aabe0

Browse files
authored
Fix ping-until-startup logic for InfluxDB 2.x (#485)
* Remove arbitrary timeout from influxd startup check * Skip booting init server when it's not needed
1 parent c8327f7 commit a7aabe0

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

influxdb/2.0/alpine/entrypoint.sh

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,21 @@ function upgrade_influxd () {
154154
set_config_path
155155
}
156156

157-
# Wait up to a minute for the DB to boot
158-
declare -r STARTUP_PING_WAIT_SECONDS=2
159-
declare -r STARTUP_PING_ATTEMPTS=30
160-
161-
# Ping influxd until it responds.
157+
# Ping influxd until it responds or crashes.
162158
# Used to block execution until the server is ready to process setup requests.
163159
function wait_for_influxd () {
160+
local -r influxd_pid=$1
164161
local ping_count=0
165-
while [ ${ping_count} -lt ${STARTUP_PING_ATTEMPTS} ]; do
166-
sleep ${STARTUP_PING_WAIT_SECONDS}
167-
log info "pinging influxd..."
162+
while kill -0 "${influxd_pid}"; do
163+
sleep 1
164+
log info "pinging influxd..." ping_attempt ${ping_count}
165+
ping_count=$((ping_count+1))
168166
if influx ping &> /dev/null; then
169-
log info "got response from influxd, proceeding"
167+
log info "got response from influxd, proceeding" total_pings ${ping_count}
170168
return
171169
fi
172-
ping_count=$((ping_count+1))
173170
done
174-
log error "failed to detect influxd startup" ping_attempts ${ping_count}
171+
log error "influxd crashed during startup" total_pings ${ping_count}
175172
exit 1
176173
}
177174

@@ -207,6 +204,14 @@ function set_init_resource_ids () {
207204
# for execution after initial setup/upgrade.
208205
declare -r USER_SCRIPT_DIR=/docker-entrypoint-initdb.d
209206

207+
# Check if user-defined setup scripts have been mounted into the container.
208+
function user_scripts_present () {
209+
if [ ! -d ${USER_SCRIPT_DIR} ]; then
210+
return 1
211+
fi
212+
test -n "$(find ${USER_SCRIPT_DIR} -name "*.sh" -type f -executable)"
213+
}
214+
210215
# Execute all shell files mounted into the expected path for user-defined startup scripts.
211216
function run_user_scripts () {
212217
if [ -d ${USER_SCRIPT_DIR} ]; then
@@ -238,6 +243,13 @@ function init_influxd () {
238243
upgrade_influxd
239244
fi
240245

246+
# Short-circuit if using upgrade mode and user didn't define any custom scripts,
247+
# to save startup time from booting & shutting down the server.
248+
if [ "${DOCKER_INFLUXDB_INIT_MODE}" = upgrade ] && ! user_scripts_present; then
249+
trap - EXIT
250+
return
251+
fi
252+
241253
# Capture final bind address, and check it is distinct from init addr
242254
local -r final_bind_addr="$(influxd print-config --key-name http-bind-address "${@}")"
243255
local -r init_bind_addr=":${INFLUXD_INIT_PORT}"
@@ -253,7 +265,7 @@ function init_influxd () {
253265
trap "handle_signal INT ${influxd_init_pid}" INT
254266

255267
export INFLUX_HOST="http://localhost:${INFLUXD_INIT_PORT}"
256-
wait_for_influxd
268+
wait_for_influxd "${influxd_init_pid}"
257269

258270
# Use the influx CLI to create an initial user/org/bucket.
259271
if [ "${DOCKER_INFLUXDB_INIT_MODE}" = setup ]; then

influxdb/2.0/entrypoint.sh

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,21 @@ function upgrade_influxd () {
154154
set_config_path
155155
}
156156

157-
# Wait up to a minute for the DB to boot
158-
declare -r STARTUP_PING_WAIT_SECONDS=2
159-
declare -r STARTUP_PING_ATTEMPTS=30
160-
161-
# Ping influxd until it responds.
157+
# Ping influxd until it responds or crashes.
162158
# Used to block execution until the server is ready to process setup requests.
163159
function wait_for_influxd () {
160+
local -r influxd_pid=$1
164161
local ping_count=0
165-
while [ ${ping_count} -lt ${STARTUP_PING_ATTEMPTS} ]; do
166-
sleep ${STARTUP_PING_WAIT_SECONDS}
167-
log info "pinging influxd..."
162+
while kill -0 "${influxd_pid}"; do
163+
sleep 1
164+
log info "pinging influxd..." ping_attempt ${ping_count}
165+
ping_count=$((ping_count+1))
168166
if influx ping &> /dev/null; then
169-
log info "got response from influxd, proceeding"
167+
log info "got response from influxd, proceeding" total_pings ${ping_count}
170168
return
171169
fi
172-
ping_count=$((ping_count+1))
173170
done
174-
log error "failed to detect influxd startup" ping_attempts ${ping_count}
171+
log error "influxd crashed during startup" total_pings ${ping_count}
175172
exit 1
176173
}
177174

@@ -207,6 +204,14 @@ function set_init_resource_ids () {
207204
# for execution after initial setup/upgrade.
208205
declare -r USER_SCRIPT_DIR=/docker-entrypoint-initdb.d
209206

207+
# Check if user-defined setup scripts have been mounted into the container.
208+
function user_scripts_present () {
209+
if [ ! -d ${USER_SCRIPT_DIR} ]; then
210+
return 1
211+
fi
212+
test -n "$(find ${USER_SCRIPT_DIR} -name "*.sh" -type f -executable)"
213+
}
214+
210215
# Execute all shell files mounted into the expected path for user-defined startup scripts.
211216
function run_user_scripts () {
212217
if [ -d ${USER_SCRIPT_DIR} ]; then
@@ -238,6 +243,13 @@ function init_influxd () {
238243
upgrade_influxd
239244
fi
240245

246+
# Short-circuit if using upgrade mode and user didn't define any custom scripts,
247+
# to save startup time from booting & shutting down the server.
248+
if [ "${DOCKER_INFLUXDB_INIT_MODE}" = upgrade ] && ! user_scripts_present; then
249+
trap - EXIT
250+
return
251+
fi
252+
241253
# Capture final bind address, and check it is distinct from init addr
242254
local -r final_bind_addr="$(influxd print-config --key-name http-bind-address "${@}")"
243255
local -r init_bind_addr=":${INFLUXD_INIT_PORT}"
@@ -253,7 +265,7 @@ function init_influxd () {
253265
trap "handle_signal INT ${influxd_init_pid}" INT
254266

255267
export INFLUX_HOST="http://localhost:${INFLUXD_INIT_PORT}"
256-
wait_for_influxd
268+
wait_for_influxd "${influxd_init_pid}"
257269

258270
# Use the influx CLI to create an initial user/org/bucket.
259271
if [ "${DOCKER_INFLUXDB_INIT_MODE}" = setup ]; then

0 commit comments

Comments
 (0)