Skip to content

Commit

Permalink
migrate events from mysql to clickhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
tudddorrr committed Aug 19, 2024
1 parent 6e2f5fc commit da0488f
Show file tree
Hide file tree
Showing 39 changed files with 753 additions and 342 deletions.
10 changes: 10 additions & 0 deletions clickhouse/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM clickhouse/clickhouse-server:24.6-alpine

RUN apk add --no-cache gettext

COPY /clickhouse/migrations /docker-entrypoint-initdb.d/
COPY /clickhouse/entrypoint.sh /usr/local/bin/entrypoint.sh

RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
21 changes: 21 additions & 0 deletions clickhouse/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
set -e

INIT_DIR="/docker-entrypoint-initdb.d"
FLAG_FILE="$INIT_DIR/.migrated"

if [ ! -f "$FLAG_FILE" ]; then
for file in ${INIT_DIR}/*.sql; do
if [ -f "$file" ]; then
envsubst < "$file" > "$file.processed" && mv "$file.processed" "$file"
echo "Processed migration $file"
fi
done

echo "Creating migrations lock"
touch "$FLAG_FILE"
else
echo "Migrations lock set; Skipping envsubst processing"
fi

exec /entrypoint.sh "$@"
16 changes: 16 additions & 0 deletions clickhouse/migrations/001CreateEventsTable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS ${CLICKHOUSE_DB}.events
(
id String,
name String,
game_id UInt32,
player_alias_id UInt32,
dev_build Boolean,
created_at DateTime,
updated_at DateTime,
PRIMARY KEY (id),
INDEX name_idx (name) TYPE bloom_filter(0.01) GRANULARITY 64,
INDEX game_id_idx (game_id) TYPE minmax GRANULARITY 64,
INDEX player_alias_id_idx (player_alias_id) TYPE minmax GRANULARITY 64
)
ENGINE = MergeTree()
ORDER BY (id, created_at, game_id, player_alias_id);
6 changes: 6 additions & 0 deletions clickhouse/migrations/002CreateEventPropsTable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS ${CLICKHOUSE_DB}.event_props (
event_id String,
prop_key String,
prop_value String
) ENGINE = MergeTree()
ORDER BY (event_id, prop_key);
38 changes: 1 addition & 37 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
services:
test-db:
image: mysql:8.4
command: --mysql-native-password=ON
environment:
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_ROOT_PASSWORD=${DB_PASS}
restart: always
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1"]
interval: 2s
timeout: 2s
retries: 10
ports:
- ${DB_PORT}:3306
volumes:
- test-data:/var/lib/mysql
networks:
- test-network

test-redis:
image: bitnami/redis:7.2
command:
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
ports:
- ${REDIS_PORT}:6379
depends_on:
test-db:
condition: service_healthy
networks:
- test-network

stripe-api:
image: stripe/stripe-mock:latest
ports:
- 12111:12111
- 12112:12112
networks:
- test-network

volumes:
test-data:

networks:
test-network:
5 changes: 3 additions & 2 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ services:
context: .
target: dev
image: backend
depends_on:
- db
ports:
- 3000:80
volumes:
- ./src:/usr/backend/src
- ./tests:/usr/backend/tests
depends_on:
- db

14 changes: 14 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ services:
networks:
- test-network

test-clickhouse:
build:
context: .
dockerfile: ./clickhouse/Dockerfile
environment:
CLICKHOUSE_USER: ${CLICKHOUSE_USER}
CLICKHOUSE_PASSWORD: ${CLICKHOUSE_PASSWORD}
CLICKHOUSE_DB: ${CLICKHOUSE_DB}
restart: unless-stopped
ports:
- ${CLICKHOUSE_PORT}:8123
networks:
- test-network

stripe-api:
image: stripe/stripe-mock:latest-arm64
ports:
Expand Down
18 changes: 17 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ services:
depends_on:
- db
- redis
- clickhouse

db:
image: mysql:8.4
command: --mysql-native-password=ON
environment:
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_ROOT_PASSWORD=${DB_PASS}
restart: always
restart: unless-stopped
ports:
- 3306:3306
volumes:
Expand All @@ -24,5 +25,20 @@ services:
ports:
- 6379:6379

clickhouse:
build:
context: .
dockerfile: ./clickhouse/Dockerfile
environment:
CLICKHOUSE_USER: ${CLICKHOUSE_USER}
CLICKHOUSE_PASSWORD: ${CLICKHOUSE_PASSWORD}
CLICKHOUSE_DB: ${CLICKHOUSE_DB}
restart: unless-stopped
ports:
- ${CLICKHOUSE_PORT}:8123
volumes:
- clickhouse-data:/var/lib/clickhouse

volumes:
data:
clickhouse-data:
Empty file added envs/.dockerignore
Empty file.
6 changes: 6 additions & 0 deletions envs/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ DB_PASS=password

REDIS_PASSWORD=password

CLICKHOUSE_HOST=clickhouse
CLICKHOUSE_PORT=8123
CLICKHOUSE_USER=gs_ch
CLICKHOUSE_PASSWORD=password
CLICKHOUSE_DB=gs_ch_dev

DEMO_ORGANISATION_NAME=Talo Demo

SENDGRID_KEY=
Expand Down
6 changes: 6 additions & 0 deletions envs/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ DB_PORT=3307
DB_NAME=gs_test
DB_PASS=password

CLICKHOUSE_HOST=127.0.0.1
CLICKHOUSE_PORT=8124
CLICKHOUSE_USER=
CLICKHOUSE_PASSWORD=
CLICKHOUSE_DB=gs_ch_test

SENDGRID_KEY=
SENTRY_DSN=

Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"vitest": "^1.5.2"
},
"dependencies": {
"@clickhouse/client": "^1.4.1",
"@dinero.js/currencies": "^2.0.0-alpha.14",
"@koa/cors": "^5.0.0",
"@mikro-orm/core": "^6.3.2",
Expand Down
58 changes: 54 additions & 4 deletions src/docs/event-api.docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,69 @@ const EventAPIDocs: APIDocs<EventAPIService> = {
events: [
{ name: 'Levelled up', timestamp: 1657063169020, props: [{ key: 'newLevel', value: '81' }] },
{ name: 'Quest completed', timestamp: 1657063169324, props: [{ key: 'questId', value: '122' }] },
{ name: 'Quested started', timestamp: 1657063169819, props: [{ key: 'questId', value: '128' }] }
{ name: 'Quested started', props: [{ key: 'questId', value: '128' }] }
]
}
},
{
title: 'Sample response',
sample: {
events: [
{ name: 'Levelled up', timestamp: 1657063169020, props: [{ key: 'newLevel', value: '81' }] },
{ name: 'Quest completed', timestamp: 1657063169324, props: [{ key: 'questId', value: '122' }] }
{
id: '77bdd78a-2292-40c0-8835-7e091adbced5',
name: 'Levelled up',
props: [{ key: 'newLevel', value: '81' }],
playerAlias: {
id: 1,
service: 'steam',
identifier: '11133645',
player: {
id: '7a4e70ec-6ee6-418e-923d-b3a45051b7f9',
props: [
{ key: 'xPos', value: '13.29' },
{ key: 'yPos', value: '26.44' }
],
aliases: [
'/* [Circular] */'
],
devBuild: false,
createdAt: '2022-01-15T13:20:32.133Z',
lastSeenAt: '2022-07-05T12:09:43.124Z'
}
},
gameId: 1,
createdAt: '2022-07-05T12:26:09.020Z'
},
{
id: 'd58b8a63-525b-4994-b04c-602485976245',
name: 'Quest completed',
props: [{ key: 'questId', value: '122' }],
playerAlias: {
id: 1,
service: 'steam',
identifier: '11133645',
player: {
id: '7a4e70ec-6ee6-418e-923d-b3a45051b7f9',
props: [
{ key: 'xPos', value: '13.29' },
{ key: 'yPos', value: '26.44' }
],
aliases: [
'/* [Circular] */'
],
devBuild: false,
createdAt: '2022-01-15T13:20:32.133Z',
lastSeenAt: '2022-07-05T12:09:43.124Z'
}
},
gameId: 1,
createdAt: '2022-07-05T12:26:09.324Z'
}
],
errors: [
'Event is missing the key: timestamp'
[],
[],
['Event is missing the key: timestamp']
]
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/docs/game-feedback-api.docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const GameFeedbackAPIDocs: APIDocs<GameFeedbackAPIService> = {
internalName: 'bugs',
name: 'Bugs',
description: 'Issues that hinder the player experience',
createdAt: '2024-06-01 21:36:02'
createdAt: '2024-06-01T21:36:02.008Z'
}
]
}
Expand Down Expand Up @@ -52,12 +52,12 @@ const GameFeedbackAPIDocs: APIDocs<GameFeedbackAPIService> = {
name: 'Bugs',
description: 'Issues that hinder the player experience',
anonymised: false,
createdAt: '2024-06-01 21:36:02'
createdAt: '2024-06-01T21:36:02.008Z'
},
comment: 'The new quest in the new zone cannot be completed!',
anonymised: true,
playerAlias: null,
createdAt: '2024-06-03 18:04:44'
createdAt: '2024-06-03T18:04:44.336Z'
}
}
},
Expand All @@ -72,7 +72,7 @@ const GameFeedbackAPIDocs: APIDocs<GameFeedbackAPIService> = {
name: 'Bugs',
description: 'Issues that hinder the player experience',
anonymised: false,
createdAt: '2024-06-01 21:36:02'
createdAt: '2024-06-01T21:36:02.008Z'
},
comment: 'The new quest in the new zone cannot be completed!',
anonymised: false,
Expand All @@ -90,11 +90,11 @@ const GameFeedbackAPIDocs: APIDocs<GameFeedbackAPIService> = {
'/* [Circular] */'
],
devBuild: false,
createdAt: '2022-01-15 13:20:32',
lastSeenAt: '2022-04-12 15:09:43'
createdAt: '2022-01-15T13:20:32.133Z',
lastSeenAt: '2022-04-12T15:09:43.066Z'
}
},
createdAt: '2024-06-03 18:04:44'
createdAt: '2024-06-03T18:04:44.336Z'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/docs/game-save-api.docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const sampleSave = {
}
]
},
updatedAt: '2022-06-07 14:58:13'
updatedAt: '2022-06-07T14:58:13.771Z'
}

const GameSaveAPIDocs: APIDocs<GameSaveAPIService> = {
Expand Down
8 changes: 4 additions & 4 deletions src/docs/game-stat-api.docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ const GameStatAPIDocs: APIDocs<GameStatAPIService> = {
minValue: 0,
maxValue: Infinity,
minTimeBetweenUpdates: 5,
createdAt: '2021-12-24 12:45:39',
updatedAt: '2021-12-24 12:49:14'
createdAt: '2021-12-24T12:45:39.409Z',
updatedAt: '2021-12-24T12:49:14.315Z'
},
value: 52,
createdAt: '2022-01-01 06:18:11',
updatedAt: '2022-01-03 08:32:46'
createdAt: '2022-01-01T06:18:11.881Z',
updatedAt: '2022-01-03T08:32:46.123Z'
}
}
}
Expand Down
Loading

0 comments on commit da0488f

Please sign in to comment.