Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(insights): launch funnels as a Clickhouse UDF behind a feature flag #23587

Merged
merged 201 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
201 commits
Select commit Hold shift + click to select a range
f351508
udf
aspicer Jul 4, 2024
e4a0979
working
aspicer Jul 4, 2024
5fd74bf
merge
aspicer Jul 5, 2024
2322db9
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Jul 5, 2024
2cb95db
funnels
aspicer Jul 5, 2024
69674bc
sorted group array
aspicer Jul 5, 2024
e6a4e6d
working query
aspicer Jul 6, 2024
1401ffa
3 level funnels
aspicer Jul 6, 2024
a70ffc2
loading conversion window limit
aspicer Jul 6, 2024
abbb018
working with array
aspicer Jul 6, 2024
d8b319b
timestamps
aspicer Jul 6, 2024
fdd66e7
timestamps
aspicer Jul 6, 2024
9bf5dee
working
aspicer Jul 6, 2024
2af6b78
passing breakdown through
aspicer Jul 6, 2024
e497417
passing
aspicer Jul 6, 2024
c41cd48
breakdowns passing through
aspicer Jul 6, 2024
2bd965b
string breakdown
aspicer Jul 6, 2024
1c45b7b
breakdowns and stesting
aspicer Jul 6, 2024
17ac128
time tracking
aspicer Jul 7, 2024
6288035
need to make timestamps
aspicer Jul 7, 2024
cb1962f
returning values
aspicer Jul 8, 2024
f27e4b7
timings working
aspicer Jul 8, 2024
4e69b14
median
aspicer Jul 8, 2024
89dceb0
nan
aspicer Jul 8, 2024
3e43a8f
working array breakdowns
aspicer Jul 8, 2024
dc9a0f4
hm
aspicer Jul 8, 2024
91d8c4e
test case
aspicer Jul 8, 2024
9e5ce4c
step
aspicer Jul 8, 2024
d13e5c8
36 tests failing
aspicer Jul 8, 2024
65de38a
order by
aspicer Jul 8, 2024
10aa1dc
hm
aspicer Jul 8, 2024
afa3149
mm
aspicer Jul 8, 2024
7d25857
30
aspicer Jul 8, 2024
7ad5736
mm
aspicer Jul 9, 2024
594ac94
exclusions
aspicer Jul 9, 2024
1a2288c
exclusions
aspicer Jul 9, 2024
2045839
results
aspicer Jul 9, 2024
b9558a9
8 failing
aspicer Jul 9, 2024
0aec55c
udf
aspicer Jul 4, 2024
9197277
working
aspicer Jul 4, 2024
c30b651
merge
aspicer Jul 5, 2024
2ccbd0f
funnels
aspicer Jul 5, 2024
99199f7
sorted group array
aspicer Jul 5, 2024
dd681a7
working query
aspicer Jul 6, 2024
aee3589
3 level funnels
aspicer Jul 6, 2024
4d8351d
loading conversion window limit
aspicer Jul 6, 2024
4f85e01
working with array
aspicer Jul 6, 2024
18a0e50
timestamps
aspicer Jul 6, 2024
57da140
timestamps
aspicer Jul 6, 2024
07af8e4
working
aspicer Jul 6, 2024
c78a4f7
passing breakdown through
aspicer Jul 6, 2024
44bffbb
passing
aspicer Jul 6, 2024
615b06f
breakdowns passing through
aspicer Jul 6, 2024
c796027
string breakdown
aspicer Jul 6, 2024
72d4d56
breakdowns and stesting
aspicer Jul 6, 2024
5bac5f5
time tracking
aspicer Jul 7, 2024
1312dfa
need to make timestamps
aspicer Jul 7, 2024
5ade8a9
returning values
aspicer Jul 8, 2024
80288c3
timings working
aspicer Jul 8, 2024
5e8c2f1
median
aspicer Jul 8, 2024
b594037
nan
aspicer Jul 8, 2024
23f0442
working array breakdowns
aspicer Jul 8, 2024
df85473
hm
aspicer Jul 8, 2024
8f4789c
test case
aspicer Jul 8, 2024
41bdd2d
step
aspicer Jul 8, 2024
42a5af8
36 tests failing
aspicer Jul 8, 2024
f609f6a
order by
aspicer Jul 8, 2024
d72f923
hm
aspicer Jul 8, 2024
a0db6b5
mm
aspicer Jul 8, 2024
034b79a
30
aspicer Jul 8, 2024
9e6c501
mm
aspicer Jul 9, 2024
bd90420
exclusions
aspicer Jul 9, 2024
0535c5e
exclusions
aspicer Jul 9, 2024
8699b7e
results
aspicer Jul 9, 2024
9833c93
8 failing
aspicer Jul 9, 2024
7116889
false
aspicer Jul 9, 2024
4228165
cohorts
aspicer Jul 9, 2024
d7784f7
5 failing
aspicer Jul 9, 2024
e262538
funnel no step
aspicer Jul 9, 2024
49e9177
prop vals
aspicer Jul 9, 2024
cc263e4
3 tests failing
aspicer Jul 9, 2024
21886d2
working
aspicer Jul 9, 2024
63c981c
hm
aspicer Jul 9, 2024
5814d9a
permutations
aspicer Jul 9, 2024
62eafe5
working funnel
aspicer Jul 9, 2024
fd1f4ec
tests
aspicer Jul 9, 2024
feec377
Update query snapshots
github-actions[bot] Jul 9, 2024
773264b
mypy fixes and ast literal eval
aspicer Jul 9, 2024
5319398
removed outer loop
aspicer Jul 9, 2024
a8db5a8
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Jul 9, 2024
79426b4
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Jul 9, 2024
84170a8
cleanup
aspicer Jul 10, 2024
ec4ff23
Update UI snapshots for `chromium` (2)
github-actions[bot] Jul 10, 2024
4e0ed71
remove extra
aspicer Jul 10, 2024
1b022e5
tests
aspicer Jul 10, 2024
910f37f
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Jul 10, 2024
eacfee6
Update query snapshots
github-actions[bot] Jul 10, 2024
3d269bc
Update query snapshots
github-actions[bot] Jul 10, 2024
ca30e00
making waves
aspicer Jul 10, 2024
2d3ba27
py
aspicer Jul 10, 2024
bdf6cc2
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Jul 10, 2024
256b423
strict funnels
aspicer Jul 10, 2024
e5bc5bb
Update query snapshots
github-actions[bot] Jul 10, 2024
18e5eed
Update query snapshots
github-actions[bot] Jul 10, 2024
10fa2dd
max step
aspicer Jul 10, 2024
ad83187
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Jul 10, 2024
3bd07a5
typing
aspicer Jul 10, 2024
e5f99af
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Jul 10, 2024
91df7b1
Cleanup
aspicer Jul 10, 2024
f21eecd
revert celery threads
aspicer Jul 10, 2024
b6e1a86
hm
aspicer Jul 10, 2024
88ae18d
clean up
aspicer Jul 29, 2024
81b8ed0
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Jul 29, 2024
8525554
merge
aspicer Aug 14, 2024
fb650be
Update UI snapshots for `chromium` (1)
github-actions[bot] Aug 14, 2024
08e4f6a
Update UI snapshots for `chromium` (2)
github-actions[bot] Aug 14, 2024
e559aac
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Aug 15, 2024
ba6a9b3
mm
aspicer Aug 16, 2024
4c9cdf7
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Aug 16, 2024
d00c4f2
bad merge
aspicer Aug 16, 2024
92d9203
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Aug 16, 2024
4b13a4e
tests
aspicer Aug 16, 2024
b671b69
Update query snapshots
github-actions[bot] Aug 16, 2024
50a20e7
docker compose full
aspicer Aug 16, 2024
2054fc4
tests
aspicer Aug 19, 2024
9ad6666
entrypoint
aspicer Aug 19, 2024
031b4fe
Update query snapshots
github-actions[bot] Aug 19, 2024
dd8dea0
fix mypy
aspicer Aug 19, 2024
4824c3c
fix mypy
aspicer Aug 19, 2024
d7c2c94
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Aug 19, 2024
de2a2cb
having
aspicer Aug 21, 2024
35f2ad0
Update query snapshots
github-actions[bot] Aug 21, 2024
65f5944
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Aug 21, 2024
519ec76
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Aug 21, 2024
e7b0ef4
Update query snapshots
github-actions[bot] Aug 21, 2024
0acb3b5
Update UI snapshots for `chromium` (1)
github-actions[bot] Aug 21, 2024
fbd5848
Update query snapshots
github-actions[bot] Aug 21, 2024
6e8cc02
Update UI snapshots for `chromium` (1)
github-actions[bot] Aug 21, 2024
47e59ca
trends wip
aspicer Aug 21, 2024
a336eb0
udf returning timestamps and success / failure
aspicer Aug 21, 2024
d2115ff
start of interval logic
aspicer Aug 22, 2024
2d34776
returning trends
aspicer Aug 22, 2024
64afdd3
funnels query runner
aspicer Aug 23, 2024
4f3c247
max steps ordered
aspicer Aug 23, 2024
db59c81
min
aspicer Aug 23, 2024
9f4228a
new test files
aspicer Aug 23, 2024
2f61308
breakdowns, working on join
aspicer Aug 23, 2024
187d37d
breakdowns working
aspicer Aug 23, 2024
a97fefd
undo test rig
aspicer Aug 23, 2024
aef455c
breakdowns working, 5 tests failing for funnel trends
aspicer Aug 23, 2024
42bed52
Update query snapshots
github-actions[bot] Aug 23, 2024
041ee84
Update query snapshots
github-actions[bot] Aug 23, 2024
ce8ca2e
timezone parsing correct
aspicer Aug 23, 2024
89b0371
Merge branch 'aspicer/trends-udf' of github.com:PostHog/posthog into …
aspicer Aug 23, 2024
ebadbdf
qq
aspicer Aug 23, 2024
f3731be
Update query snapshots
github-actions[bot] Aug 23, 2024
cb253ec
testing
aspicer Aug 23, 2024
93d69db
Merge branch 'aspicer/trends-udf' of github.com:PostHog/posthog into …
aspicer Aug 23, 2024
96bde73
Update query snapshots
github-actions[bot] Aug 23, 2024
3057194
switch to json
aspicer Aug 27, 2024
3430f4a
lala
aspicer Aug 27, 2024
f97d8f5
Update query snapshots
github-actions[bot] Aug 27, 2024
39b7220
Update query snapshots
github-actions[bot] Aug 27, 2024
2b3dbf6
reduce files
aspicer Aug 27, 2024
112444a
Merge branch 'aspicer/trends-udf' of github.com:PostHog/posthog into …
aspicer Aug 27, 2024
b7ac533
timezones working
aspicer Aug 27, 2024
6dfb5aa
Update query snapshots
github-actions[bot] Aug 27, 2024
8c07c63
Update query snapshots
github-actions[bot] Aug 27, 2024
a8f250b
merge
aspicer Aug 27, 2024
71f6177
remove pypy
aspicer Aug 27, 2024
775c2e5
fix test
aspicer Aug 27, 2024
03a0617
Update query snapshots
github-actions[bot] Aug 28, 2024
d940fc5
i c
aspicer Aug 28, 2024
8dc53dc
Merge branch 'aspicer/trends-udf' of github.com:PostHog/posthog into …
aspicer Aug 28, 2024
bcc3cbd
Update query snapshots
github-actions[bot] Aug 28, 2024
52c6056
Update query snapshots
github-actions[bot] Aug 28, 2024
38ce36e
Update query snapshots
github-actions[bot] Aug 28, 2024
fbfd7b1
add the ability to toggle a udf
aspicer Aug 28, 2024
fb5ee7c
remove test function
aspicer Aug 28, 2024
e65617c
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Aug 28, 2024
062b660
mypy
aspicer Aug 28, 2024
839274d
udf
aspicer Aug 28, 2024
2e2521d
fix typo
aspicer Aug 28, 2024
18c9ccf
Update query snapshots
github-actions[bot] Aug 28, 2024
fbb1407
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Aug 29, 2024
6ec5de8
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Aug 29, 2024
aad3b15
tmate
aspicer Aug 29, 2024
a981ea9
timeout
aspicer Aug 29, 2024
62d07b7
tet
aspicer Aug 29, 2024
0fd6128
bump temporalio
aspicer Aug 29, 2024
ded3751
always wait for temporal
aspicer Aug 29, 2024
209a722
Update UI snapshots for `chromium` (2)
github-actions[bot] Aug 29, 2024
cd25fa1
Update UI snapshots for `chromium` (2)
github-actions[bot] Aug 29, 2024
5b8313d
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Sep 3, 2024
ebf6c56
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Sep 3, 2024
f245dfc
clean up actions and rename
aspicer Sep 4, 2024
a6f0c21
staged
aspicer Sep 4, 2024
afacb91
Update query snapshots
github-actions[bot] Sep 4, 2024
329cd4a
Merge remote-tracking branch 'origin/master' into aspicer/udf
aspicer Sep 5, 2024
f6fa7ab
Merge branch 'aspicer/udf' of github.com:PostHog/posthog into aspicer…
aspicer Sep 5, 2024
32fd423
Update query snapshots
github-actions[bot] Sep 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/actions/run-backend-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ runs:
--durations=100 --durations-min=1.0 --store-durations \
$PYTEST_ARGS

# Uncomment this code to create an ssh-able console so you can debug issues with github actions
# (Consider changing the timeout in ci-backend.yml to have more time)
# - name: Setup tmate session
# if: failure()
# uses: mxschmitt/action-tmate@v3

- name: Run /decide read replica tests
id: run-decide-read-replica-tests
if: ${{ inputs.segment == 'Core' && inputs.group == 1 && inputs.person-on-events != 'true' }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ jobs:

django:
needs: changes
# increase for tmate testing
timeout-minutes: 30

name: Django tests – ${{ matrix.segment }} (persons-on-events ${{ matrix.person-on-events && 'on' || 'off' }}), Py ${{ matrix.python-version }}, ${{ matrix.clickhouse-server-image }} (${{matrix.group}}/${{ matrix.concurrency }})
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.dev-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ services:
- ./docker/clickhouse/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./docker/clickhouse/config.xml:/etc/clickhouse-server/config.xml
- ./docker/clickhouse/users-dev.xml:/etc/clickhouse-server/users.xml
- ./docker/clickhouse/user_defined_function.xml:/etc/clickhouse-server/user_defined_function.xml
- ./posthog/user_scripts:/var/lib/clickhouse/user_scripts
depends_on:
- kafka
- zookeeper
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ services:
- '9440:9440'
- '9009:9009'
volumes:
# this new entrypoint file is to fix a bug detailed here https://github.com/ClickHouse/ClickHouse/pull/59991
# revert this when we upgrade clickhouse
- ./docker/clickhouse/entrypoint.sh:/entrypoint.sh
- ./posthog/idl:/idl
- ./docker/clickhouse/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./docker/clickhouse/config.xml:/etc/clickhouse-server/config.xml
- ./docker/clickhouse/users-dev.xml:/etc/clickhouse-server/users.xml
- ./docker/clickhouse/user_defined_function.xml:/etc/clickhouse-server/user_defined_function.xml
- ./posthog/user_scripts:/var/lib/clickhouse/user_scripts
extra_hosts:
- 'host.docker.internal:host-gateway'
depends_on:
Expand Down
1 change: 1 addition & 0 deletions docker/clickhouse/docker-entrypoint-initdb.d/init-db.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

apk add python3
cp -r /idl/* /var/lib/clickhouse/format_schemas/
207 changes: 207 additions & 0 deletions docker/clickhouse/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#!/bin/bash

set -eo pipefail
shopt -s nullglob

DO_CHOWN=1
if [ "${CLICKHOUSE_DO_NOT_CHOWN:-0}" = "1" ]; then
DO_CHOWN=0
fi

CLICKHOUSE_UID="${CLICKHOUSE_UID:-"$(id -u clickhouse)"}"
CLICKHOUSE_GID="${CLICKHOUSE_GID:-"$(id -g clickhouse)"}"

# support --user
if [ "$(id -u)" = "0" ]; then
USER=$CLICKHOUSE_UID
GROUP=$CLICKHOUSE_GID
else
USER="$(id -u)"
GROUP="$(id -g)"
DO_CHOWN=0
fi

# set some vars
CLICKHOUSE_CONFIG="${CLICKHOUSE_CONFIG:-/etc/clickhouse-server/config.xml}"

# get CH directories locations
DATA_DIR="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=path || true)"
TMP_DIR="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=tmp_path || true)"
USER_PATH="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=user_files_path || true)"
LOG_PATH="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=logger.log || true)"
LOG_DIR=""
if [ -n "$LOG_PATH" ]; then LOG_DIR="$(dirname "$LOG_PATH")"; fi
ERROR_LOG_PATH="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=logger.errorlog || true)"
ERROR_LOG_DIR=""
if [ -n "$ERROR_LOG_PATH" ]; then ERROR_LOG_DIR="$(dirname "$ERROR_LOG_PATH")"; fi
FORMAT_SCHEMA_PATH="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=format_schema_path || true)"

# There could be many disks declared in config
readarray -t DISKS_PATHS < <(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key='storage_configuration.disks.*.path' || true)
readarray -t DISKS_METADATA_PATHS < <(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key='storage_configuration.disks.*.metadata_path' || true)

CLICKHOUSE_USER="${CLICKHOUSE_USER:-default}"
CLICKHOUSE_PASSWORD="${CLICKHOUSE_PASSWORD:-}"
CLICKHOUSE_DB="${CLICKHOUSE_DB:-}"
CLICKHOUSE_ACCESS_MANAGEMENT="${CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT:-0}"

for dir in "$DATA_DIR" \
"$ERROR_LOG_DIR" \
"$LOG_DIR" \
"$TMP_DIR" \
"$USER_PATH" \
"$FORMAT_SCHEMA_PATH" \
"${DISKS_PATHS[@]}" \
"${DISKS_METADATA_PATHS[@]}"
do
# check if variable not empty
[ -z "$dir" ] && continue
# ensure directories exist
if [ "$DO_CHOWN" = "1" ]; then
mkdir="mkdir"
else
# if DO_CHOWN=0 it means that the system does not map root user to "admin" permissions
# it mainly happens on NFS mounts where root==nobody for security reasons
# thus mkdir MUST run with user id/gid and not from nobody that has zero permissions
mkdir="/usr/bin/clickhouse su "${USER}:${GROUP}" mkdir"
fi
if ! $mkdir -p "$dir"; then
echo "Couldn't create necessary directory: $dir"
exit 1
fi

if [ "$DO_CHOWN" = "1" ]; then
# ensure proper directories permissions
# but skip it for if directory already has proper premissions, cause recursive chown may be slow
if [ "$(stat -c %u "$dir")" != "$USER" ] || [ "$(stat -c %g "$dir")" != "$GROUP" ]; then
chown -R "$USER:$GROUP" "$dir"
fi
fi
done

# if clickhouse user is defined - create it (user "default" already exists out of box)
if [ -n "$CLICKHOUSE_USER" ] && [ "$CLICKHOUSE_USER" != "default" ] || [ -n "$CLICKHOUSE_PASSWORD" ] || [ "$CLICKHOUSE_ACCESS_MANAGEMENT" != "0" ]; then
echo "$0: create new user '$CLICKHOUSE_USER' instead 'default'"
cat <<EOT > /etc/clickhouse-server/users.d/default-user.xml
<clickhouse>
<!-- Docs: <https://clickhouse.com/docs/en/operations/settings/settings_users/> -->
<users>
<!-- Remove default user -->
<default remove="remove">
</default>

<${CLICKHOUSE_USER}>
<profile>default</profile>
<networks>
<ip>::/0</ip>
</networks>
<password>${CLICKHOUSE_PASSWORD}</password>
<quota>default</quota>
<access_management>${CLICKHOUSE_ACCESS_MANAGEMENT}</access_management>
</${CLICKHOUSE_USER}>
</users>
</clickhouse>
EOT
fi

CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS="${CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS:-}"

# checking $DATA_DIR for initialization
if [ -d "${DATA_DIR%/}/data" ]; then
DATABASE_ALREADY_EXISTS='true'
fi

# run initialization if flag CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS is not empty or data directory is empty
if [[ -n "${CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS}" || -z "${DATABASE_ALREADY_EXISTS}" ]]; then
RUN_INITDB_SCRIPTS='true'
fi

if [ -n "${RUN_INITDB_SCRIPTS}" ]; then
if [ -n "$(ls /docker-entrypoint-initdb.d/)" ] || [ -n "$CLICKHOUSE_DB" ]; then
# port is needed to check if clickhouse-server is ready for connections
HTTP_PORT="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=http_port --try)"
HTTPS_PORT="$(clickhouse extract-from-config --config-file "$CLICKHOUSE_CONFIG" --key=https_port --try)"

if [ -n "$HTTP_PORT" ]; then
URL="http://127.0.0.1:$HTTP_PORT/ping"
else
URL="https://127.0.0.1:$HTTPS_PORT/ping"
fi

# Listen only on localhost until the initialization is done
/usr/bin/clickhouse su "${USER}:${GROUP}" /usr/bin/clickhouse-server --config-file="$CLICKHOUSE_CONFIG" -- --listen_host=127.0.0.1 &
pid="$!"

# check if clickhouse is ready to accept connections
# will try to send ping clickhouse via http_port (max 1000 retries by default, with 1 sec timeout and 1 sec delay between retries)
tries=${CLICKHOUSE_INIT_TIMEOUT:-1000}
while ! wget --spider --no-check-certificate -T 1 -q "$URL" 2>/dev/null; do
if [ "$tries" -le "0" ]; then
echo >&2 'ClickHouse init process failed.'
exit 1
fi
tries=$(( tries-1 ))
sleep 1
done

clickhouseclient=( clickhouse-client --multiquery --host "127.0.0.1" -u "$CLICKHOUSE_USER" --password "$CLICKHOUSE_PASSWORD" )

echo

# create default database, if defined
if [ -n "$CLICKHOUSE_DB" ]; then
echo "$0: create database '$CLICKHOUSE_DB'"
"${clickhouseclient[@]}" -q "CREATE DATABASE IF NOT EXISTS $CLICKHOUSE_DB";
fi

for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh)
if [ -x "$f" ]; then
echo "$0: running $f"
"$f"
else
echo "$0: sourcing $f"
# shellcheck source=/dev/null
. "$f"
fi
;;
*.sql) echo "$0: running $f"; "${clickhouseclient[@]}" < "$f" ; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${clickhouseclient[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done

if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'Finishing of ClickHouse init process failed.'
exit 1
fi
fi
else
echo "ClickHouse Database directory appears to contain a database; Skipping initialization"
fi

# if no args passed to `docker run` or first argument start with `--`, then the user is passing clickhouse-server arguments
if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then
# Watchdog is launched by default, but does not send SIGINT to the main process,
# so the container can't be finished by ctrl+c
CLICKHOUSE_WATCHDOG_ENABLE=${CLICKHOUSE_WATCHDOG_ENABLE:-0}
export CLICKHOUSE_WATCHDOG_ENABLE

# An option for easy restarting and replacing clickhouse-server in a container, especially in Kubernetes.
# For example, you can replace the clickhouse-server binary to another and restart it while keeping the container running.
if [[ "${CLICKHOUSE_DOCKER_RESTART_ON_EXIT:-0}" -eq "1" ]]; then
while true; do
# This runs the server as a child process of the shell script:
/usr/bin/clickhouse su "${USER}:${GROUP}" /usr/bin/clickhouse-server --config-file="$CLICKHOUSE_CONFIG" "$@" ||:
echo >&2 'ClickHouse Server exited, and the environment variable CLICKHOUSE_DOCKER_RESTART_ON_EXIT is set to 1. Restarting the server.'
done
else
# This replaces the shell script with the server:
exec /usr/bin/clickhouse su "${USER}:${GROUP}" /usr/bin/clickhouse-server --config-file="$CLICKHOUSE_CONFIG" "$@"
fi
fi

# Otherwise, we assume the user want to run his own process, for example a `bash` shell to explore this image
exec "$@"
Loading
Loading