Skip to content

Commit 83ad222

Browse files
authored
Multitenant migrations execution (#431)
* feat: async tenant migrations * feat: monitoring with grafana & prometheus
1 parent 50e4298 commit 83ad222

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+10959
-479
lines changed

docker-compose-infra.yml renamed to .docker/docker-compose-infra.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ services:
4848
PGBOUNCER_STATS_USERS: postgres
4949

5050
supavisor:
51-
image: supabase/supavisor:1.1.6
51+
image: supabase/supavisor:1.1.23
5252
depends_on:
5353
multitenant_db:
5454
condition: service_healthy
@@ -78,7 +78,7 @@ services:
7878
command: sh -c "/app/bin/migrate && /app/bin/server"
7979

8080
supavisor_setup:
81-
image: supabase/supavisor:1.1.6
81+
image: supabase/supavisor:1.1.23
8282
command: |
8383
curl -X PUT \
8484
"http://supavisor:4000/api/tenants/bjhaohmqunupljrqypxz" \
@@ -102,7 +102,8 @@ services:
102102
\"db_password\": \"postgres\",
103103
\"mode_type\": \"transaction\",
104104
\"pool_size\": 15,
105-
\"pool_checkout_timeout\": 100
105+
\"max_clients\": 200,
106+
\"pool_checkout_timeout\": 5000
106107
}
107108
]
108109
}

.docker/docker-compose-monitoring.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: '3'
2+
services:
3+
pg_bouncer_exporter:
4+
image: spreaker/prometheus-pgbouncer-exporter
5+
environment:
6+
PGBOUNCER_EXPORTER_HOST: 0.0.0.0
7+
PGBOUNCER_EXPORTER_PORT: 9127
8+
PGBOUNCER_HOST: pg_bouncer
9+
PGBOUNCER_PORT: 6432
10+
PGBOUNCER_USER: postgres
11+
PGBOUNCER_PASS: postgres
12+
depends_on:
13+
- pg_bouncer
14+
ports:
15+
- 9127:9127
16+
17+
postgres_exporter:
18+
image: quay.io/prometheuscommunity/postgres-exporter
19+
ports:
20+
- "9187:9187"
21+
environment:
22+
DATA_SOURCE_NAME: postgres://postgres:postgres@tenant_db:5432/postgres?sslmode=disable
23+
24+
prometheus:
25+
image: prom/prometheus
26+
container_name: prometheus
27+
command:
28+
- '--config.file=/etc/prometheus/prometheus.yml'
29+
ports:
30+
- 9090:9090
31+
restart: unless-stopped
32+
volumes:
33+
- ../monitoring/prometheus:/etc/prometheus
34+
35+
grafana:
36+
image: grafana/grafana
37+
container_name: grafana
38+
ports:
39+
- 3000:3000
40+
restart: unless-stopped
41+
environment:
42+
- GF_SECURITY_ADMIN_USER=admin
43+
- GF_SECURITY_ADMIN_PASSWORD=grafana
44+
volumes:
45+
- ../monitoring/grafana/config:/etc/grafana/provisioning
46+
- ../monitoring/grafana/dashboards:/var/lib/grafana/dashboards

.env.sample

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SERVER_PORT=5000
66
SERVER_ADMIN_PORT=5001
77
SERVER_KEEP_ALIVE_TIMEOUT=61
88
SERVER_HEADERS_TIMEOUT=65
9-
SERVER_REGION=region-of-where-your-service-is-running
9+
SERVER_REGION=local
1010

1111

1212
#######################################
@@ -24,20 +24,13 @@ RESPONSE_S_MAXAGE=0
2424
AUTH_JWT_SECRET=f023d3db-39dc-4ac9-87b2-b2be72e9162b
2525
AUTH_JWT_ALGORITHM=HS256
2626

27-
28-
#######################################
29-
# Single Tenant
30-
#######################################
31-
TENANT_ID=bjhaohmqunupljrqypxz
32-
3327
#######################################
3428
# Multi Tenancy
3529
# uncomment MULTI_TENANT=true to enable multi tenancy
36-
# the [Single Tenant] configurations will be ignored
3730
#######################################
3831
# MULTI_TENANT=true
3932
DATABASE_MULTITENANT_URL=postgresql://postgres:[email protected]:5433/postgres
40-
REQUEST_X_FORWARDED_HOST_REGEXP=
33+
REQUEST_X_FORWARDED_HOST_REGEXP=^([a-z]{20}).local.(?:com|dev)$
4134
SERVER_ADMIN_API_KEYS=apikey
4235
AUTH_ENCRYPTION_KEY=encryptionkey
4336

@@ -85,7 +78,7 @@ STORAGE_BACKEND=s3
8578
#######################################
8679
# S3 Backend
8780
#######################################
88-
STORAGE_S3_BUCKET=name-of-your-s3-bucket
81+
STORAGE_S3_BUCKET=supa-storage-bucket
8982
STORAGE_S3_MAX_SOCKETS=200
9083
STORAGE_S3_ENDPOINT=http://127.0.0.1:9000
9184
STORAGE_S3_FORCE_PATH_STYLE=true

.env.test.sample

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
AUTHENTICATED_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwic3ViIjoiMzE3ZWFkY2UtNjMxYS00NDI5LWEwYmItZjE5YTdhNTE3YjRhIiwiZW1haWwiOiJpbmlhbit0ZXN0MUBzdXBhYmFzZS5pbyIsImV4cCI6MTkzOTEwNzk4NSwiYXBwX21ldGFkYXRhIjp7InByb3ZpZGVyIjoiZW1haWwifSwidXNlcl9tZXRhZGF0YSI6e30sInJvbGUiOiJhdXRoZW50aWNhdGVkIn0.E-x3oYcHIjFCdUO1M3wKDl1Ln32mik0xdHT2PjrvN70
2+
ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlhdCI6MTYxMzUzMTk4NSwiZXhwIjoxOTI5MTA3OTg1fQ.mqfi__KnQB4v6PkIjkhzfwWrYyF94MEbSC6LnuvVniE
3+
SERVICE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIiwiaWF0IjoxNjEzNTMxOTg1LCJleHAiOjE5MjkxMDc5ODV9.th84OKK0Iz8QchDyXZRrojmKSEZ-OuitQm_5DvLiSIc
4+
5+
TENANT_ID=bjhaohmqunupljrqypxz
6+
ENABLE_DEFAULT_METRICS=false
7+
PG_QUEUE_ENABLE=false
8+
MULTI_TENANT=false
9+
ADMIN_API_KEYS=apikey
10+
11+
DB_INSTALL_ROLES=true
12+
IMAGE_TRANSFORMATION_ENABLED=true
13+
214
AWS_ACCESS_KEY_ID=supa-storage
315
AWS_SECRET_ACCESS_KEY=secret1234
416
AWS_DEFAULT_REGION=ap-southeast-1
5-
GLOBAL_S3_ENDPOINT=http://127.0.0.1:9000
6-
GLOBAL_S3_PROTOCOL=http
7-
GLOBAL_S3_FORCE_PATH_STYLE=true
17+
18+
STORAGE_S3_ENDPOINT=http://127.0.0.1:9000
19+
STORAGE_S3_PROTOCOL=http
20+
STORAGE_S3_FORCE_PATH_STYLE=true

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ jobs:
7676
GLOBAL_S3_PROTOCOL: http
7777
GLOBAL_S3_FORCE_PATH_STYLE: true
7878
DB_INSTALL_ROLES: true
79+
ENABLE_DEFAULT_METRICS: false
80+
PG_QUEUE_ENABLE: false
81+
MULTI_TENANT: false
7982

8083
- name: Upload coverage results to Coveralls
8184
uses: coverallsapp/github-action@master

Dockerfile

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
FROM node:18-alpine
1+
# Base stage for shared environment setup
2+
FROM node:18-alpine as base
23
RUN apk add --no-cache g++ make python3
34
WORKDIR /app
45
COPY package.json package-lock.json ./
5-
RUN npm ci --production
66

7-
FROM node:18-alpine
8-
RUN apk add --no-cache g++ make python3
9-
WORKDIR /app
10-
COPY . .
7+
# Dependencies stage - install and cache all dependencies
8+
FROM base as dependencies
119
RUN npm ci
10+
# Cache the installed node_modules for later stages
11+
RUN cp -R node_modules /node_modules_cache
12+
13+
# Build stage - use cached node_modules for building the application
14+
FROM base as build
15+
COPY --from=dependencies /node_modules_cache ./node_modules
16+
COPY . .
1217
RUN npm run build
1318

14-
FROM node:18-alpine
19+
# Production dependencies stage - use npm cache to install only production dependencies
20+
FROM base as production-deps
21+
COPY --from=dependencies /node_modules_cache ./node_modules
22+
RUN npm ci --production
23+
24+
# Final stage - for the production build
25+
FROM base as final
1526
ARG VERSION
1627
ENV VERSION=$VERSION
17-
WORKDIR /app
1828
COPY migrations migrations
19-
COPY ecosystem.config.js package.json ./
20-
COPY --from=0 /app/node_modules node_modules
21-
COPY --from=1 /app/dist dist
29+
30+
# Copy production node_modules from the production dependencies stage
31+
COPY --from=production-deps /app/node_modules node_modules
32+
# Copy build artifacts from the build stage
33+
COPY --from=build /app/dist dist
34+
2235
EXPOSE 5000
23-
ENTRYPOINT ["docker-entrypoint.sh"]
24-
CMD ["node", "dist/server.js"]
36+
CMD ["node", "dist/server.js"]

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ A scalable, light-weight object storage service.
2323

2424
- Copy `.env.sample` to `.env` file.
2525
- Copy `.env.test.sample` to `.env.test`.
26-
- Change `GLOBAL_S3_BUCKET` and `REGION` to the name and region of a S3 bucket.
27-
- If you just want to run the tests and not develop locally, you can skip this step because S3 calls are mocked in our tests.
28-
- [Set up your AWS credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html). Your user must have permissions to `s3:PutObject, s3:GetObject, s3:DeleteObject` in the bucket you have chosen.
26+
27+
```bash
28+
cp .env.sample .env && cp .env.test.sample .env.test
29+
````
2930

3031
**Your root directory should now have both `.env` and `.env.test` files.**
3132

docker-compose-multi-tenant.yml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,51 +51,70 @@ services:
5151
IMGPROXY_URL: http://imgproxy:8080
5252
IMGPROXY_REQUEST_TIMEOUT: 15
5353

54+
PG_QUEUE_ENABLE: "true"
55+
5456
tenant_db:
5557
extends:
5658
service: tenant_db
57-
file: docker-compose-infra.yml
59+
file: ./.docker/docker-compose-infra.yml
5860

5961
multitenant_db:
6062
extends:
6163
service: multitenant_db
62-
file: docker-compose-infra.yml
64+
file: ./.docker/docker-compose-infra.yml
6365

6466
supavisor:
6567
extends:
6668
service: supavisor
67-
file: docker-compose-infra.yml
69+
file: ./.docker/docker-compose-infra.yml
6870

6971
supavisor_setup:
7072
depends_on:
7173
supavisor:
7274
condition: service_healthy
7375
extends:
7476
service: supavisor_setup
75-
file: docker-compose-infra.yml
77+
file: ./.docker/docker-compose-infra.yml
7678

7779
minio:
7880
extends:
7981
service: minio
80-
file: docker-compose-infra.yml
82+
file: ./.docker/docker-compose-infra.yml
8183

8284
minio_setup:
8385
extends:
8486
service: minio_setup
85-
file: docker-compose-infra.yml
87+
file: ./.docker/docker-compose-infra.yml
8688
depends_on:
8789
- minio
8890

8991
imgproxy:
9092
extends:
9193
service: imgproxy
92-
file: docker-compose-infra.yml
94+
file: ./.docker/docker-compose-infra.yml
9395

9496
# Optional for rate-limiting
9597
# redis:
9698
# extends:
9799
# service: redis
98-
# file: docker-compose-infra.yml
100+
# file: ./.docker/docker-compose-infra.yml
101+
102+
# Optional for monitoring:
103+
# grafana:
104+
# extends:
105+
# service: grafana
106+
# file: ./.docker/docker-compose-monitoring.yml
107+
#
108+
# prometheus:
109+
# extends:
110+
# service: prometheus
111+
# file: ./.docker/docker-compose-monitoring.yml
112+
#
113+
# postgres_exporter:
114+
# extends:
115+
# service: postgres_exporter
116+
# file: ./.docker/docker-compose-monitoring.yml
117+
99118

100119
configs:
101120
init.sql:

docker-compose.yml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,54 @@ services:
4747
tenant_db:
4848
extends:
4949
service: tenant_db
50-
file: docker-compose-infra.yml
50+
file: ./.docker/docker-compose-infra.yml
5151

5252
pg_bouncer:
5353
extends:
5454
service: pg_bouncer
55-
file: docker-compose-infra.yml
55+
file: ./.docker/docker-compose-infra.yml
5656

5757
minio:
5858
extends:
5959
service: minio
60-
file: docker-compose-infra.yml
60+
file: ./.docker/docker-compose-infra.yml
6161

6262
minio_setup:
6363
extends:
6464
service: minio_setup
65-
file: docker-compose-infra.yml
65+
file: ./.docker/docker-compose-infra.yml
6666
depends_on:
6767
minio:
6868
condition: service_healthy
6969

7070
imgproxy:
7171
extends:
7272
service: imgproxy
73-
file: docker-compose-infra.yml
73+
file: ./.docker/docker-compose-infra.yml
7474

7575
# Optional for rate-limiting
7676
# redis:
7777
# extends:
7878
# service: redis
79-
# file: docker-compose-infra.yml
79+
# file: ./.docker/docker-compose-infra.yml
80+
81+
# Optional for monitoring
82+
# pg_bouncer_exporter:
83+
# extends:
84+
# service: pg_bouncer_exporter
85+
# file: ./.docker/docker-compose-monitoring.yml
86+
87+
# postgres_exporter:
88+
# extends:
89+
# service: postgres_exporter
90+
# file: ./.docker/docker-compose-monitoring.yml
91+
#
92+
# grafana:
93+
# extends:
94+
# service: grafana
95+
# file: ./.docker/docker-compose-monitoring.yml
96+
#
97+
# prometheus:
98+
# extends:
99+
# service: prometheus
100+
# file: ./.docker/docker-compose-monitoring.yml

docker-entrypoint.sh

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)