Completed for a grade of three

This commit is contained in:
2026-10-23 14:11:11 +03:00
parent a3dbcd6ba4
commit 92c2fdc254
54 changed files with 1221 additions and 92 deletions
+2 -1
View File
@@ -25,6 +25,7 @@ dependencies {
implementation(ktorLibs.server.statusPages)
implementation(ktorLibs.server.callLogging)
implementation(ktorLibs.server.defaultHeaders)
implementation(ktorLibs.server.cors)
implementation(ktorLibs.server.swagger)
implementation(ktorLibs.serialization.kotlinx.json)
@@ -67,7 +68,7 @@ ksp {
}
application {
mainClass = "io.ktor.server.netty.EngineMain"
mainClass = "com.mtusi.messenger.main.AppKt"
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=false")
}
+39 -3
View File
@@ -4,7 +4,6 @@ services:
postgres:
image: postgres:17.6-alpine3.22
container_name: mtusi-lab3-postgres
# pg_stat_statements counts executed queries, track 1 uses it to measure the cache stampede.
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all"]
environment:
@@ -25,7 +24,6 @@ services:
redis:
image: redis:8.4.0-alpine
container_name: mtusi-lab3-redis
# Session cache only, PostgreSQL is the source of truth: no RDB/AOF, a restart simply empties the cache.
# Every key carries a TTL, so under memory pressure volatile-lru evicts the least used sessions.
command:
@@ -48,11 +46,36 @@ services:
start_period: 5s
restart: unless-stopped
# One-shot: applies Flyway migrations once and exits. Replicas start only after it succeeded,
# so they never race each other on the schema.
migrate:
build: .
image: mtusi-messenger:lab3
command: ["migrate"]
healthcheck:
disable: true
depends_on:
postgres:
condition: service_healthy
environment:
DB_HOST: postgres
DB_PORT: "5432"
DB_NAME: messenger
DB_USER: messenger
DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
restart: "no"
api:
build: .
image: mtusi-messenger:lab3
container_name: mtusi-lab3-api
# Interchangeable replicas: no container_name and no published port, traffic only through `lb`.
deploy:
replicas: ${API_REPLICAS:-2}
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
redis:
@@ -65,14 +88,27 @@ services:
DB_NAME: messenger
DB_USER: messenger
DB_PASSWORD_FILE: /run/secrets/db_password
DB_MIGRATE_ON_START: "false"
REDIS_HOST: redis
REDIS_PORT: "6379"
REDIS_PASSWORD_FILE: /run/secrets/redis_password
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
SESSION_CACHE_SINGLE_FLIGHT: ${SESSION_CACHE_SINGLE_FLIGHT:-true}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
secrets:
- db_password
- redis_password
# SIGTERM: Ktor stops accepting, finishes requests in flight (shutdownGracePeriod 3 s, shutdownTimeout 10 s).
stop_grace_period: 15s
restart: unless-stopped
lb:
image: haproxy:3.2.4-alpine
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
api:
condition: service_healthy
ports:
- "127.0.0.1:8080:8080"
restart: unless-stopped
+24
View File
@@ -0,0 +1,24 @@
# before
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up About a minute (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 8 seconds (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 119, "failed_attempts": 1, "first_recovery_seconds": 6.508}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 exited Exited (143) 8 seconds ago
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 24 seconds (healthy)
# HAProxy log around the failure (non-2xx and state changes)
[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ).
Server api/api1 ('api') is UP/READY (resolves again).
[WARNING] (8) : Server api/api1 ('api') is UP/READY (resolves again).
172.24.0.1:55062 [22/Oct/2026:17:44:22.431] http api/api2 0/0/0/-1/93 502 209 - - SH-- 1/1/0/0/0 0/0 "POST /api/v1/chats/43312e52-5c0b-41af-9981-2379d372b382/messages HTTP/1.1"
[WARNING] (8) : Server api/api2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server api/api2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server api/api2 was DOWN and now enters maintenance (No IP for server ).
[WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ).
+24
View File
@@ -0,0 +1,24 @@
# before
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up 8 seconds (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up About a minute (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 120, "failed_attempts": 1, "first_recovery_seconds": 6.537}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up 24 seconds (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 exited Exited (143) 8 seconds ago
# HAProxy log around the failure (non-2xx and state changes)
[WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ).
Server api/api2 ('api') is UP/READY (resolves again).
[WARNING] (8) : Server api/api2 ('api') is UP/READY (resolves again).
172.24.0.1:62054 [22/Oct/2026:17:45:16.133] http api/api1 0/0/0/-1/87 502 209 - - SH-- 1/1/0/0/0 0/0 "POST /api/v1/chats/43529cee-e036-4e88-a744-c3fd0e23ce65/messages HTTP/1.1"
Server api/api1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[WARNING] (8) : Server api/api1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ).
Server api/api1 was DOWN and now enters maintenance (No IP for server ).
+84
View File
@@ -0,0 +1,84 @@
# ./gradlew test (Testcontainers: postgres:17.6-alpine3.22, redis:8.4.0-alpine)
total: 66 tests, 0 failed
infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 7.774 s
- logout revokes only the current token
- Bearer token cannot open a session
- password containing a colon and non-ASCII characters works
- unknown token returns 401
- session expires after SESSION_TTL_SECONDS
- each login issues a different token
- valid Basic credentials open a session
- missing or malformed Basic header returns 401
- wrong password and unknown user get the same 401
infrastructure.controllers.chat.ChatControllerTest: 6 tests, 0 failed, 2.101 s
- member can read own chat and members keep their order
- unknown member is rejected before anything is written
- foreign chat returns 404 for non-member
- cursor issued to another user is rejected
- chat list contains only own chats and is paginated
- invalid member lists are rejected with 400
infrastructure.controllers.health.HealthControllerTest: 2 tests, 0 failed, 0.134 s
- live returns 200
- ready returns 200 when database is reachable
infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.335 s
- parallel senders produce exactly N unique messages
infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.946 s
- foreign chat returns 404 when sending
- last page has null next_cursor
- pagination walks all messages exactly once
- foreign chat returns 404 when listing
- empty text returns 400 invalid_request
- history that is an exact multiple of limit ends with null next_cursor
- foreign cursor returns 400
infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 3.266 s
- expired key can be used for a new message
- key scope is user and chat
- same key with another body is a conflict
- missing or malformed key is rejected before anything is written
- retry with the same key and body returns the original message
- retry by a former outsider is still checked for access
- concurrent retries create exactly one message
infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 1.638 s
- password shorter than 12 code points returns 400
- concurrent registrations of one username create exactly one user
- X-User-Id alone gives no access
- create user returns 201 without password
- duplicate username returns 409 conflict
- missing token returns 401 with Bearer challenge
- invalid JSON body returns 400 invalid_request
- missing password returns 400
- Basic credentials are rejected on business endpoints
- me returns the session owner
- X-User-Id does not override bearer identity
infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: 3 tests, 0 failed, 0.102 s
- cached session round-trips and expires together with the session
- revoke replaces a cached session with a tombstone
- late cache fill cannot overwrite a tombstone
infrastructure.repositories.chat.ExposedChatRepositoryTest: 2 tests, 0 failed, 0.514 s
- chat row is rolled back when inserting members fails
- successful creation commits the chat and every member
infrastructure.services.authentication.SessionLookupSingleFlightTest: 4 tests, 0 failed, 1.120 s
- request arriving after logout does not join a lookup started before it
- without single-flight every concurrent miss queries the database
- with single-flight concurrent misses share one database lookup
- lookup that finished before a request arrived is not reused
infrastructure.services.hashing.DefaultHashingServiceTest: 4 tests, 0 failed, 0.421 s
- passwords differing only after 72 bytes are distinguished
- validates the right password and rejects a wrong one
- malformed hash is rejected instead of throwing
- hash is a salted Argon2id PHC string
plugins.DatabaseMigrationTest: 1 tests, 0 failed, 0.209 s
- re-running migrations applies nothing and keeps data
plugins.DependencyFailureTest: 5 tests, 0 failed, 13.326 s
- without PostgreSQL requests fail with a bounded 503
- logout during a Redis outage survives an API restart
- logout during a Redis outage is not undone by a stale cached copy
- without Redis requests bypass the cache
- hanging Redis costs one command timeout and recovers
plugins.InstanceIdTest: 1 tests, 0 failed, 0.231 s
- every response names the replica
plugins.SessionCacheTest: 3 tests, 0 failed, 0.928 s
- emptied cache falls back to PostgreSQL
- first authenticated request fills the cache
- logout leaves a tombstone and the token is rejected
+19
View File
@@ -0,0 +1,19 @@
$ python3 scripts/idempotency-race.py
{
"rounds": 20,
"parallel_requests_per_round": 32,
"statuses": {
"201": 640
},
"requests_per_instance": {
"236cb759e825": 320,
"787a253aa6a6": 320
},
"rounds_with_more_than_one_message_id": 0,
"messages_in_history": 20,
"distinct_texts_in_history": 20
}
PASS
$ psql: keys without a message (must be 0)
0
+21
View File
@@ -0,0 +1,21 @@
$ docker compose ps -a
NAME IMAGE STATE STATUS PORTS
mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 running Up 35 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 running Up 35 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine running Up 29 seconds 127.0.0.1:8080->8080/tcp
mtusi-messenger-lab3-migrate-1 mtusi-messenger:lab3 exited Exited (0) 36 seconds ago
mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 running Up 42 seconds (healthy) 5432/tcp
mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine running Up 42 seconds (healthy) 6379/tcp
$ for i in 1..6: curl -sD - http://localhost:8080/health/ready | grep X-Instance-Id
x-instance-id: 236cb759e825
x-instance-id: 787a253aa6a6
x-instance-id: 236cb759e825
x-instance-id: 787a253aa6a6
x-instance-id: 236cb759e825
x-instance-id: 787a253aa6a6
# X-Instance-Id = HOSTNAME = short container id of the replica
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up 35 seconds (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 35 seconds (healthy)
+24
View File
@@ -0,0 +1,24 @@
# before
NAME STATE STATUS
mtusi-messenger-lab3-api-1 running Up 35 seconds (healthy)
mtusi-messenger-lab3-api-2 running Up 8 seconds (healthy)
mtusi-messenger-lab3-lb-1 running Up 2 minutes
mtusi-messenger-lab3-migrate-1 exited Exited (0) 7 seconds ago
mtusi-messenger-lab3-postgres-1 running Up 2 minutes (healthy)
mtusi-messenger-lab3-redis-1 running Up 2 minutes (healthy)
/mtusi-messenger-lab3-api-1 StartedAt=2026-10-22T17:45:06.396826175Z
/mtusi-messenger-lab3-api-2 StartedAt=2026-10-22T17:45:33.184696798Z
/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-22T17:43:17.31840918Z
/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-22T17:43:04.928814758Z
/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-22T17:43:04.927049216Z
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: every container has a new StartedAt, volume postgres-data is kept
/mtusi-messenger-lab3-api-1 StartedAt=2026-10-22T17:45:53.47890296Z
/mtusi-messenger-lab3-api-2 StartedAt=2026-10-22T17:45:53.585460419Z
/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-22T17:45:59.18815863Z
/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-22T17:45:46.762570055Z
/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-22T17:45:46.760974013Z
mtusi-messenger-lab3_postgres-data
+2
View File
@@ -0,0 +1,2 @@
$ make test
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
+47
View File
@@ -0,0 +1,47 @@
# Single public entry point in front of the API replicas.
# Backends are discovered through Docker DNS (service name `api`) and receive traffic only while /health/ready is 200.
global
log stdout format raw local0 info
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor
timeout connect 1s
timeout client 30s
timeout server 10s
timeout http-request 10s
timeout http-keep-alive 5s
# Retried only when the connection to a replica fails, i.e. before the request was sent:
# a POST that reached a replica is never repeated by the balancer.
retries 2
option redispatch 1
resolvers docker
nameserver dns 127.0.0.11:53
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold valid 2s
hold other 2s
hold refused 2s
hold nx 2s
hold timeout 2s
hold obsolete 2s
accepted_payload_size 8192
frontend http
bind :8080
default_backend api
backend api
balance roundrobin
option httpchk GET /health/ready
http-check expect status 200
# A replica leaves rotation after 2 failed checks 1 s apart and returns after 2 successful ones.
default-server check inter 1s fall 2 rise 2 resolvers docker init-addr none
# Up to 4 replicas; slots without a resolved address stay in maintenance.
server-template api 4 api:8080
+23
View File
@@ -0,0 +1,23 @@
cors {
# Defaults to false
#allowAnyHost = true
# Swagger UI sends "Try it out" to the contract server http://localhost:8080; pages opened elsewhere need these.
allowedHosts = [
{ host = "localhost:8080", schemes = ["http"] }
{ host = "localhost:8081", schemes = ["http"] }
{ host = "127.0.0.1:8080", schemes = ["http"] }
{ host = "127.0.0.1:8081", schemes = ["http"] }
]
# Defaults to true
#allowAnyMethod = true
#allowedMethods = [GET, POST, PATCH, PUT, DELETE, OPTIONS]
allowAnyHeader = true
#allowedHeaders = []
exposedHeaders = [X-Request-Id, X-Instance-Id, WWW-Authenticate]
# Browsers send Origin on same-origin POST too, e.g. Swagger UI served by the API itself.
allowSameOrigin = true
}
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""Sends one message with the SAME Idempotency-Key many times in parallel through the balancer.
Expects every response to be 201 with the same message, at least two replicas answering (X-Instance-Id)
and exactly one message in the chat history. Synthetic users only; the token stays in memory.
"""
import argparse
import base64
import json
import threading
import urllib.error
import urllib.request
import uuid
from collections import Counter
parser = argparse.ArgumentParser()
parser.add_argument('--base-url', default='http://localhost:8080')
parser.add_argument('--parallel', type=int, default=32)
parser.add_argument('--rounds', type=int, default=20)
args = parser.parse_args()
def call(method, path, body=None, headers=None):
data = json.dumps(body).encode() if body is not None else None
request = urllib.request.Request(args.base_url + path, data=data, method=method, headers={
'Content-Type': 'application/json', **(headers or {})})
try:
with urllib.request.urlopen(request, timeout=10) as response:
return response.status, response.headers, json.loads(response.read() or b'null')
except urllib.error.HTTPError as error:
return error.code, error.headers, json.loads(error.read() or b'null')
username = 'race_' + uuid.uuid4().hex[:12]
password = 'race-password-' + uuid.uuid4().hex
status, _, user = call('POST', '/api/v1/users', {'username': username, 'display_name': 'race', 'password': password})
assert status == 201, status
basic = base64.b64encode(f'{username}:{password}'.encode()).decode()
status, _, session = call('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + basic})
assert status == 201, status
auth = {'Authorization': 'Bearer ' + session['token']}
status, _, chat = call('POST', '/api/v1/chats', {'title': 'race', 'member_ids': [user['id']]}, auth)
assert status == 201, status
path = f"/api/v1/chats/{chat['id']}/messages"
statuses, instances, violations = Counter(), Counter(), 0
for round_number in range(args.rounds):
key = uuid.uuid4().hex
results = []
barrier = threading.Barrier(args.parallel)
def send():
barrier.wait()
results.append(call('POST', path, {'text': f'race {round_number}'}, {**auth, 'Idempotency-Key': key}))
threads = [threading.Thread(target=send) for _ in range(args.parallel)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
ids = {body['id'] for status, _, body in results if status == 201}
for status, headers, _ in results:
statuses[status] += 1
instances[headers.get('X-Instance-Id', '-')] += 1
if len(ids) != 1:
violations += 1
status, _, page = call('GET', path + '?limit=100', headers=auth)
texts = [m['text'] for m in page['items']]
print(json.dumps({
'rounds': args.rounds,
'parallel_requests_per_round': args.parallel,
'statuses': dict(statuses),
'requests_per_instance': dict(instances),
'rounds_with_more_than_one_message_id': violations,
'messages_in_history': len(texts),
'distinct_texts_in_history': len(set(texts)),
}, indent=2))
ok = violations == 0 and len(texts) == args.rounds == len(set(texts)) and len(instances) >= 2 and set(statuses) == {201}
print('PASS' if ok else 'FAIL')
raise SystemExit(0 if ok else 1)
+3 -3
View File
@@ -1,7 +1,7 @@
#!/bin/sh
# Restarts the API, PostgreSQL and Redis, keeping the named volume (no `down -v`, no re-seed).
# Restarts the balancer, every API replica, PostgreSQL and Redis, keeping the named volume (no `down -v`, no re-seed).
set -eu
cd "$(dirname "$0")/.."
docker compose stop api redis postgres
docker compose up -d --wait postgres redis api
docker compose stop lb api redis postgres
docker compose up -d --wait
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
# Stops the ONE API replica named by TARGET_INSTANCE_ID and leaves it stopped (no DB/LB/volume changes).
# The replica's X-Instance-Id is its container hostname, i.e. the short container id.
# Restore afterwards with: docker compose start api
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
case "$TARGET_INSTANCE_ID" in
*[!0-9a-f]* | "") echo "unexpected instance id" >&2; exit 2 ;;
esac
# Only containers of the api service are candidates, so a wrong id can never stop the database or the balancer.
container=$(docker compose ps -q api | grep "^$TARGET_INSTANCE_ID" || true)
if [ -z "$container" ] || [ "$(printf '%s\n' "$container" | wc -l)" -ne 1 ]; then
echo "no single api replica matches $TARGET_INSTANCE_ID" >&2
exit 1
fi
docker stop --time 8 "$container" >/dev/null
@@ -3,17 +3,26 @@ package com.mtusi.messenger.main
import com.mtusi.messenger.main.plugins.authentication.configureAuthentication
import com.mtusi.messenger.main.plugins.configureCallLogging
import com.mtusi.messenger.main.plugins.configureContentNegotiation
import com.mtusi.messenger.main.plugins.configureCors
import com.mtusi.messenger.main.plugins.configureDatabase
import com.mtusi.messenger.main.plugins.configureDefaultHeaders
import com.mtusi.messenger.main.plugins.configureInstanceId
import com.mtusi.messenger.main.plugins.configureKoin
import com.mtusi.messenger.main.plugins.configureRedis
import com.mtusi.messenger.main.plugins.configureRequestId
import com.mtusi.messenger.main.plugins.configureRouting
import com.mtusi.messenger.main.plugins.configureStatusPages
import com.mtusi.messenger.main.plugins.migrateDatabase
import io.ktor.server.application.Application
import io.ktor.server.netty.EngineMain
fun main(args: Array<String>): Unit = EngineMain.main(args)
fun main(args: Array<String>) {
// `migrate` applies database migrations and exits; the API replicas start with migrations already in place.
if (args.firstOrNull() == "migrate") {
return migrateDatabase()
}
EngineMain.main(args)
}
fun Application.module() {
@@ -23,6 +32,8 @@ fun Application.module() {
configureContentNegotiation()
configureDefaultHeaders()
configureInstanceId()
configureCors()
configureRequestId()
configureCallLogging()
configureStatusPages()
@@ -0,0 +1,31 @@
package com.mtusi.messenger.main.compat.serializers
import io.ktor.http.HttpMethod
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
typealias SerialHttpMethod = @Serializable(HttpMethodSerializer::class) HttpMethod
object HttpMethodSerializer : KSerializer<HttpMethod> {
override val descriptor = PrimitiveSerialDescriptor(
serialName = "HttpMethod",
kind = PrimitiveKind.STRING
)
override fun serialize(
encoder: Encoder,
value: HttpMethod
) {
encoder.encodeString(value.value)
}
override fun deserialize(decoder: Decoder): HttpMethod {
return HttpMethod.parse(decoder.decodeString())
}
}
@@ -0,0 +1,100 @@
package com.mtusi.messenger.main.config.cors
import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod
import io.ktor.http.HttpMethod
import kotlinx.serialization.Serializable
import io.ktor.server.plugins.cors.CORSConfig as CorsPluginConfig
interface CorsConfig {
/**
* Whether any host should be allowed
*
* Must default to false
*/
val allowAnyHost: Boolean
val allowedHosts: List<AllowedHost>
/**
* Whether any [HttpMethod][io.ktor.http.HttpMethod] should be allowed
*
* Must default to true
*/
val allowAnyMethod: Boolean
val allowedMethods: List<SerialHttpMethod>
/**
* Whether any header should be allowed
*
* Must default to true
*/
val allowAnyHeader: Boolean
val allowedHeaders: List<String>
/**
* Response headers a browser page may read, e.g. X-Request-Id and X-Instance-Id in Swagger UI
*/
val exposedHeaders: List<String>
/**
* Whether requests from the API's own origin should be allowed
*
* Must default to false
*/
val allowSameOrigin: Boolean
/**
* Whether credentials should be allowed
*
* Must default to true
*/
val allowCredentials: Boolean
/**
* Whether non-simple content types should be allowed
*
* Must default to true
*
* @see [io.ktor.server.plugins.cors.CORSConfig.allowNonSimpleContentTypes]
*/
val allowNonSimpleContentTypes: Boolean
@Serializable
data class AllowedHost(
val host: String,
val schemes: List<String>
)
}
fun CorsPluginConfig.configure(config: CorsConfig) {
if (config.allowAnyHost) {
anyHost()
} else {
config.allowedHosts.forEach { (host, schemes) ->
allowHost(host, schemes)
}
}
if (config.allowAnyMethod) {
HttpMethod.DefaultMethods.forEach(::allowMethod)
} else {
config.allowedMethods.forEach(::allowMethod)
}
if (config.allowAnyHeader) {
allowHeaders { true }
} else {
config.allowedHeaders.forEach(::allowHeader)
}
config.exposedHeaders.forEach(::exposeHeader)
allowSameOrigin = config.allowSameOrigin
allowCredentials = config.allowCredentials
allowNonSimpleContentTypes = config.allowNonSimpleContentTypes
}
@@ -0,0 +1,11 @@
package com.mtusi.messenger.main.config.cors
import io.ktor.server.application.Application
import io.ktor.server.config.getAs
import org.koin.core.annotation.Provided
import org.koin.core.annotation.Single
@Single
class DefaultCorsConfig(
@Provided app: Application
) : CorsConfig by app.environment.config.property("cors").getAs<SerialCorsConfig>()
@@ -0,0 +1,18 @@
package com.mtusi.messenger.main.config.cors
import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod
import kotlinx.serialization.Serializable
@Serializable
data class SerialCorsConfig(
override val allowAnyHost: Boolean = false,
override val allowedHosts: List<CorsConfig.AllowedHost> = emptyList(),
override val allowAnyMethod: Boolean = true,
override val allowedMethods: List<SerialHttpMethod> = emptyList(),
override val allowAnyHeader: Boolean = true,
override val allowedHeaders: List<String> = emptyList(),
override val exposedHeaders: List<String> = emptyList(),
override val allowSameOrigin: Boolean = false,
override val allowCredentials: Boolean = true,
override val allowNonSimpleContentTypes: Boolean = true
) : CorsConfig
@@ -14,6 +14,8 @@ interface DatabaseConfig {
val connectionTimeoutMs: Long
val statementTimeoutMs: Long
val migrateOnStart: Boolean
val jdbcConnectionUrl: String
get() = "jdbc:postgresql://$host:$port/$name"
@@ -12,5 +12,6 @@ data class SerialDatabaseConfig(
override val passwordFile: String? = null,
override val maxPoolSize: Int,
override val connectionTimeoutMs: Long,
override val statementTimeoutMs: Long
override val statementTimeoutMs: Long,
override val migrateOnStart: Boolean = true
) : DatabaseConfig
@@ -0,0 +1,11 @@
package com.mtusi.messenger.main.config.message
import io.ktor.server.application.Application
import io.ktor.server.config.getAs
import org.koin.core.annotation.Provided
import org.koin.core.annotation.Single
@Single
class DefaultMessageConfig(
@Provided app: Application
) : MessageConfig by app.environment.config.propertyOrNull("messages")?.getAs<SerialMessageConfig>() ?: SerialMessageConfig()
@@ -0,0 +1,5 @@
package com.mtusi.messenger.main.config.message
interface MessageConfig {
val idempotencyTtlSeconds: Long
}
@@ -0,0 +1,8 @@
package com.mtusi.messenger.main.config.message
import kotlinx.serialization.Serializable
@Serializable
data class SerialMessageConfig(
override val idempotencyTtlSeconds: Long = 86400
) : MessageConfig
@@ -11,6 +11,7 @@ import com.mtusi.messenger.main.infrastructure.services.message.results.MessageP
import com.mtusi.messenger.main.infrastructure.types.ErrorCode
import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH
import com.mtusi.messenger.main.plugins.suspendedTransaction
import com.mtusi.messenger.main.utils.requireIdempotencyKey
import com.mtusi.messenger.main.utils.requireNonBlankBounded
import com.mtusi.messenger.main.utils.requireUuid
import io.ktor.http.HttpStatusCode
@@ -33,6 +34,7 @@ fun Routing.configureMessageRouting() = route("/api/v1/chats/{chat_id}/messages"
val actor = call.currentUser
val chatId = requireUuid(call.parameters["chat_id"], "chat_id")
val idempotencyKey = requireIdempotencyKey(call.request.headers["Idempotency-Key"])
val body = call.receive<CreateMessageRequest>()
requireNonBlankBounded(
@@ -46,13 +48,16 @@ fun Routing.configureMessageRouting() = route("/api/v1/chats/{chat_id}/messages"
messageService.send(
chatId = chatId,
senderId = actor.id,
text = body.text
text = body.text,
idempotencyKey = idempotencyKey
)
}
when (result) {
is MessageCreationResult.ChatNotFound ->
call.respondError(ErrorCode.NOT_FOUND, "chat not found")
is MessageCreationResult.IdempotencyKeyReused ->
call.respondError(ErrorCode.CONFLICT, "Idempotency-Key was used with a different message")
is MessageCreationResult.Success ->
call.respond(HttpStatusCode.Created, result.message)
}
@@ -0,0 +1,25 @@
package com.mtusi.messenger.main.infrastructure.models.message
import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel
import com.mtusi.messenger.main.infrastructure.models.user.UserModel
import org.jetbrains.exposed.v1.core.ReferenceOption
import org.jetbrains.exposed.v1.core.Table
import org.jetbrains.exposed.v1.datetime.timestamp
const val MAX_IDEMPOTENCY_KEY_LENGTH = 128
object MessageIdempotencyKeyModel : Table("message_idempotency_keys") {
val userId = reference("user_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_user_id_fkey")
val chatId = reference("chat_id", ChatModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_chat_id_fkey")
val key = varchar("key", MAX_IDEMPOTENCY_KEY_LENGTH)
val requestHash = varchar("request_hash", 64)
val messageId = optReference("message_id", MessageModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_message_id_fkey")
val createdAt = timestamp("created_at")
override val primaryKey = PrimaryKey(userId, chatId, key, name = "message_idempotency_keys_pkey")
init {
index("message_idempotency_keys_chat_id_idx", false, chatId)
index("message_idempotency_keys_message_id_idx", false, messageId)
}
}
@@ -4,28 +4,17 @@ import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
import com.mtusi.messenger.main.infrastructure.repositories.Repository
import kotlin.time.Instant
/**
* Cache-aside copy of [com.mtusi.messenger.main.infrastructure.repositories.authentication.session.SessionRepository].
* PostgreSQL stays the source of truth: losing every key only costs a database lookup.
*/
interface SessionCacheRepository : Repository<String, Session> {
suspend fun find(
tokenHash: String
): CachedSession?
/**
* Stores the session until [cachedUntil] unless the key already exists, so a lookup racing with a logout
* cannot overwrite the tombstone written by [revoke].
*/
suspend fun putIfAbsent(
session: Session,
cachedUntil: Instant
)
/**
* Replaces the entry with a tombstone that lives until [expiresAt], when the session would have expired.
*/
suspend fun revoke(
tokenHash: String,
expiresAt: Instant
@@ -4,18 +4,12 @@ import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocat
import com.mtusi.messenger.main.infrastructure.repositories.Repository
import kotlin.time.Instant
/**
* Logouts that still have to be written to the session cache as tombstones (outbox).
*/
interface SessionRevocationRepository : Repository<String, SessionRevocation> {
fun add(
revocation: SessionRevocation
)
/**
* Revocations of sessions that have not expired yet: an expired session is rejected even if its copy is cached.
*/
fun findPending(
now: Instant
): List<SessionRevocation>
@@ -21,9 +21,6 @@ interface ChatRepository : Repository<UUID, Chat> {
userId: UUID
): Boolean
/**
* Chats of [userId] ordered by `(created_at, id)`, strictly after [after].
*/
fun findPageByMemberId(
userId: UUID,
after: PageKey?,
@@ -42,6 +42,16 @@ class ExposedMessageRepository : MessageRepository {
)
}
override fun findById(
id: UUID
): Message? {
return MessageModel
.selectAll()
.where(MessageModel.id eq id)
.firstOrNull()
?.toMessage()
}
override fun findPageByChatId(
chatId: UUID,
after: PageKey?,
@@ -13,6 +13,10 @@ interface MessageRepository : Repository<UUID, Message> {
text: String
): Message
fun findById(
id: UUID
): Message?
fun findPageByChatId(
chatId: UUID,
after: PageKey?,
@@ -0,0 +1,83 @@
package com.mtusi.messenger.main.infrastructure.repositories.message.idempotency
import com.mtusi.messenger.main.infrastructure.models.message.MessageIdempotencyKeyModel
import org.jetbrains.exposed.v1.core.and
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.less
import org.jetbrains.exposed.v1.jdbc.deleteWhere
import org.jetbrains.exposed.v1.jdbc.insertIgnore
import org.jetbrains.exposed.v1.jdbc.selectAll
import org.jetbrains.exposed.v1.jdbc.update
import org.koin.core.annotation.Single
import java.util.UUID
import kotlin.time.Instant
@Single
class ExposedMessageIdempotencyRepository : MessageIdempotencyRepository {
override fun claim(
userId: UUID,
chatId: UUID,
key: String,
requestHash: String,
now: Instant
): Boolean {
// INSERT ... ON CONFLICT DO NOTHING
val statement = MessageIdempotencyKeyModel.insertIgnore {
it[this.userId] = userId
it[this.chatId] = chatId
it[this.key] = key
it[this.requestHash] = requestHash
it[this.createdAt] = now
}
return statement.insertedCount > 0
}
override fun find(
userId: UUID,
chatId: UUID,
key: String
): MessageIdempotencyRecord? {
return MessageIdempotencyKeyModel
.selectAll()
.where(scope(userId, chatId, key))
.firstOrNull()
?.let {
MessageIdempotencyRecord(
requestHash = it[MessageIdempotencyKeyModel.requestHash],
messageId = it[MessageIdempotencyKeyModel.messageId]?.value,
createdAt = it[MessageIdempotencyKeyModel.createdAt]
)
}
}
override fun attachMessage(
userId: UUID,
chatId: UUID,
key: String,
messageId: UUID
) {
MessageIdempotencyKeyModel.update({ scope(userId, chatId, key) }) {
it[this.messageId] = messageId
}
}
override fun deleteExpired(
userId: UUID,
chatId: UUID,
key: String,
createdBefore: Instant
) {
MessageIdempotencyKeyModel.deleteWhere {
scope(userId, chatId, key) and (MessageIdempotencyKeyModel.createdAt less createdBefore)
}
}
private fun scope(userId: UUID, chatId: UUID, key: String) =
(MessageIdempotencyKeyModel.userId eq userId) and
(MessageIdempotencyKeyModel.chatId eq chatId) and
(MessageIdempotencyKeyModel.key eq key)
}
@@ -0,0 +1,43 @@
package com.mtusi.messenger.main.infrastructure.repositories.message.idempotency
import com.mtusi.messenger.main.infrastructure.repositories.Repository
import java.util.UUID
import kotlin.time.Instant
data class MessageIdempotencyRecord(
val requestHash: String,
val messageId: UUID?,
val createdAt: Instant,
)
interface MessageIdempotencyRepository : Repository<String, MessageIdempotencyRecord> {
fun claim(
userId: UUID,
chatId: UUID,
key: String,
requestHash: String,
now: Instant
): Boolean
fun find(
userId: UUID,
chatId: UUID,
key: String
): MessageIdempotencyRecord?
fun attachMessage(
userId: UUID,
chatId: UUID,
key: String,
messageId: UUID
)
fun deleteExpired(
userId: UUID,
chatId: UUID,
key: String,
createdBefore: Instant
)
}
@@ -16,17 +16,10 @@ interface AuthenticationService {
userId: UUID
): IssuedSession
/**
* Opens its own transaction only on a cache miss, so it must not be called inside one.
*/
suspend fun findSessionByToken(
token: String
): Session?
/**
* Deletes the session in PostgreSQL, then replaces its cached copy with a tombstone. Without Redis only the
* database is updated: a copy cached before the outage can then live for at most the cache TTL.
*/
suspend fun revokeSession(
session: Session
): Boolean
@@ -52,22 +52,10 @@ class DefaultAuthenticationService(
private val random = SecureRandom()
/**
* Database lookups currently running per token hash. Concurrent cache misses for one token wait for the
* lookup already in flight instead of each sending their own query (cache stampede protection).
*/
private val inFlightLookups = ConcurrentHashMap<String, CompletableDeferred<Session?>>()
/**
* Only availability changes are logged: with Redis down every request would otherwise add a warning.
*/
private val isCacheAvailable = AtomicBoolean(true)
/**
* Number of failed cache operations. A failure may have lost a logout tombstone, so the cache is trusted only
* after the pending revocations were written to it with no failure since: [trustedAtFailures] equals this counter.
* A fresh process starts untrusted, as the previous one may have stopped with revocations still pending.
*/
private val cacheFailures = AtomicLong()
@Volatile
@@ -206,11 +194,6 @@ class DefaultAuthenticationService(
return withCache { sessionCacheRepository.find(tokenHash) }
}
/**
* Writes the tombstones of logouts that happened while Redis was unavailable. Until that succeeds the cache
* is bypassed: a copy cached before the outage could otherwise outlive the logout. One request restores,
* the others go to the database meanwhile; a failed attempt is repeated after [CACHE_RESTORE_BACKOFF].
*/
private suspend fun restoreCache(): Boolean {
if (!cacheRestoreMutex.tryLock()) {
@@ -277,11 +260,6 @@ class DefaultAuthenticationService(
}
}
/**
* Runs a cache operation, treating an unavailable Redis as a miss: the database stays the source of truth.
* A command already written when Redis drops the connection fails with the raw [IOException] (connection
* reset), not with a [RedisException]; it means the same.
*/
private suspend fun <T> withCache(
operation: suspend () -> T
): T? {
@@ -1,38 +1,85 @@
package com.mtusi.messenger.main.infrastructure.services.message
import com.mtusi.messenger.main.config.message.MessageConfig
import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository
import com.mtusi.messenger.main.infrastructure.repositories.message.MessageRepository
import com.mtusi.messenger.main.infrastructure.repositories.message.idempotency.MessageIdempotencyRepository
import com.mtusi.messenger.main.infrastructure.services.message.results.MessageCreationResult
import com.mtusi.messenger.main.infrastructure.services.message.results.MessagePageResult
import com.mtusi.messenger.main.utils.PageKey
import com.mtusi.messenger.main.utils.computeSha256
import com.mtusi.messenger.main.utils.decodeCursor
import com.mtusi.messenger.main.utils.nowMicros
import com.mtusi.messenger.main.utils.toPage
import org.koin.core.annotation.Single
import java.util.UUID
import kotlin.time.Clock
import kotlin.time.Duration.Companion.seconds
// One attempt to claim a free key, plus one more if the stored key had expired and was removed
private const val CLAIM_ATTEMPTS = 2
@Single
class DefaultMessageService(
private val chatRepository: ChatRepository,
private val messageRepository: MessageRepository
private val messageRepository: MessageRepository,
private val idempotencyRepository: MessageIdempotencyRepository,
private val config: MessageConfig
) : MessageService {
override fun send(
chatId: UUID,
senderId: UUID,
text: String
text: String,
idempotencyKey: String
): MessageCreationResult {
// Access is checked on every retry too: a replay must not outlive the membership.
if (!chatRepository.isMember(chatId, senderId)) {
return MessageCreationResult.ChatNotFound
}
val message = messageRepository.create(
chatId = chatId,
senderId = senderId,
text = text
)
// The body has a single field, so its hash is the hash of the text.
val requestHash = text.toByteArray().computeSha256()
val now = Clock.System.nowMicros()
return MessageCreationResult.Success(message)
repeat(CLAIM_ATTEMPTS) {
if (idempotencyRepository.claim(senderId, chatId, idempotencyKey, requestHash, now)) {
val message = messageRepository.create(
chatId = chatId,
senderId = senderId,
text = text
)
idempotencyRepository.attachMessage(senderId, chatId, idempotencyKey, message.id)
return MessageCreationResult.Success(message)
}
// The key is taken by a committed transaction (claim waits for an uncommitted one).
val record = idempotencyRepository.find(senderId, chatId, idempotencyKey)
?: return@repeat
val expiredBefore = now - config.idempotencyTtlSeconds.seconds
if (record.createdAt < expiredBefore) {
idempotencyRepository.deleteExpired(senderId, chatId, idempotencyKey, createdBefore = expiredBefore)
return@repeat
}
if (record.requestHash != requestHash) {
return MessageCreationResult.IdempotencyKeyReused
}
val messageId = checkNotNull(record.messageId) { "committed idempotency key without a message" }
return MessageCreationResult.Success(
checkNotNull(messageRepository.findById(messageId)) { "message of an idempotency key is missing" }
)
}
error("Idempotency key could not be claimed")
}
override fun findPageByChatId(
@@ -9,7 +9,8 @@ interface MessageService {
fun send(
chatId: UUID,
senderId: UUID,
text: String
text: String,
idempotencyKey: String
): MessageCreationResult
fun findPageByChatId(
@@ -8,6 +8,8 @@ sealed class MessageCreationResult {
data object ChatNotFound : Error()
data object IdempotencyKeyReused : Error()
data class Success(
val message: Message
) : MessageCreationResult()
@@ -0,0 +1,13 @@
package com.mtusi.messenger.main.plugins
import com.mtusi.messenger.main.config.cors.CorsConfig
import com.mtusi.messenger.main.config.cors.configure
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.plugins.cors.routing.CORS
import org.koin.ktor.ext.inject
fun Application.configureCors() = install(CORS) {
val config by this@configureCors.inject<CorsConfig>()
configure(config)
}
@@ -6,6 +6,7 @@ import com.mtusi.messenger.main.infrastructure.models.authentication.SessionMode
import com.mtusi.messenger.main.infrastructure.models.authentication.SessionRevocationModel
import com.mtusi.messenger.main.infrastructure.models.chat.ChatMemberModel
import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel
import com.mtusi.messenger.main.infrastructure.models.message.MessageIdempotencyKeyModel
import com.mtusi.messenger.main.infrastructure.models.message.MessageModel
import com.mtusi.messenger.main.infrastructure.models.user.UserModel
import com.mtusi.messenger.main.utils.resolveSecret
@@ -46,7 +47,11 @@ fun Application.configureDatabase() {
defaultMaxAttempts = 1
}
)
applyDatabaseMigrations(dataSource)
if (config.migrateOnStart) {
applyDatabaseMigrations(dataSource)
} else {
validateDatabaseMigrations(dataSource)
}
monitor.subscribe(ApplicationStopped) {
dataSource.close()
@@ -69,18 +74,38 @@ private fun DatabaseConfig.createDataSource(): HikariDataSource {
)
}
private fun flyway(dataSource: DataSource) = Flyway.configure()
.dataSource(dataSource)
.locations("classpath:migrations")
.load()
fun applyDatabaseMigrations(
dataSource: DataSource
) {
val flyway = Flyway.configure()
.dataSource(dataSource)
.locations("classpath:migrations")
.load()
val flyway = flyway(dataSource)
flyway.migrate()
flyway.validate()
}
/**
* Fails if a migration is pending or differs from the applied one, so a replica never serves an outdated schema.
*/
fun validateDatabaseMigrations(
dataSource: DataSource
) {
flyway(dataSource).validate()
}
/**
* Entry point of `java -jar messenger.jar migrate`: applies the migrations once and exits.
* Uses the `database` section of application.conf, environment variables included.
*/
fun migrateDatabase() {
val config = HoconApplicationConfig(ConfigFactory.load())
.property("database")
.getAs<SerialDatabaseConfig>()
config.createDataSource().use(::applyDatabaseMigrations)
}
fun main(args: Array<out String>) {
@@ -96,6 +121,7 @@ private fun generateMigration(description: String) {
ChatModel,
ChatMemberModel,
MessageModel,
MessageIdempotencyKeyModel,
SessionModel,
SessionRevocationModel
)
@@ -0,0 +1,28 @@
package com.mtusi.messenger.main.plugins
import io.ktor.server.application.Application
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.application.install
import io.ktor.server.application.log
import java.util.UUID
/**
* Identifies the replica that produced the response. Stable for the life of the process: `INSTANCE_ID` if set,
* otherwise the container hostname (Docker sets it to the short container id), otherwise a random alias.
*/
fun Application.configureInstanceId() {
val instanceId = environment.config.propertyOrNull("instance.id")?.getString()?.takeIf { it.isNotBlank() }
?: System.getenv("HOSTNAME")?.takeIf { it.isNotBlank() }
?: UUID.randomUUID().toString().take(12)
log.info("X-Instance-Id of this replica: $instanceId")
install(
createApplicationPlugin("InstanceId") {
onCall { call ->
call.response.headers.append("X-Instance-Id", instanceId, safeOnly = false)
}
}
)
}
@@ -10,9 +10,6 @@ import kotlin.time.Instant
private val json = Json { ignoreUnknownKeys = false }
/**
* Position in a `(created_at, id)` ordered list: the next page starts strictly after it.
*/
data class PageKey(
val createdAt: Instant,
val id: UUID,
@@ -30,9 +27,6 @@ fun encodeCursor(scope: String, key: PageKey): String {
return Base64.getUrlEncoder().withoutPadding().encodeToString(raw.toByteArray(Charsets.UTF_8))
}
/**
* @return page key, or null if the cursor is malformed or was issued for another resource
*/
fun decodeCursor(raw: String, expectedScope: String): PageKey? {
val cursor = try {
val bytes = Base64.getUrlDecoder().decode(raw)
@@ -48,9 +42,6 @@ fun decodeCursor(raw: String, expectedScope: String): PageKey? {
return PageKey(cursor.createdAt, cursor.id)
}
/**
* Builds a page from `limit + 1` rows fetched after the cursor: the extra row only signals that more data exists.
*/
fun <T : Any> List<T>.toPage(
limit: Int,
scope: String,
@@ -2,9 +2,6 @@ package com.mtusi.messenger.main.utils
import java.io.File
/**
* Secrets come either inline (local development) or as a mounted file (Docker secrets), the inline value wins.
*/
fun resolveSecret(
value: String?,
file: String?
@@ -5,10 +5,6 @@ import kotlin.time.Instant
private const val NANOS_PER_MICRO = 1_000
/**
* Current time truncated to microseconds: PostgreSQL timestamptz keeps exactly this precision,
* so the value returned to the client equals the one read back from the database.
*/
fun Clock.nowMicros(): Instant {
val now = now()
return Instant.fromEpochSeconds(
@@ -6,6 +6,8 @@ import java.util.UUID
private val USERNAME = Regex("^[a-z0-9_]{3,32}$")
private val IDEMPOTENCY_KEY = Regex("^[A-Za-z0-9._:-]{1,128}$")
private val UUID_REGEX = Regex(
"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
)
@@ -28,6 +30,13 @@ fun requireNonBlankBounded(value: String, min: Int, max: Int, field: String) {
}
}
fun requireIdempotencyKey(value: String?): String {
if (value == null || !IDEMPOTENCY_KEY.matches(value)) {
throw ApiException(ErrorCode.INVALID_REQUEST, "missing or invalid Idempotency-Key")
}
return value
}
fun requireUuid(value: String?, field: String): UUID {
if (value == null || !UUID_REGEX.matches(value)) {
throw ApiException(ErrorCode.INVALID_REQUEST, "invalid $field")
+36
View File
@@ -28,6 +28,9 @@ database {
maxPoolSize = ${?DB_MAX_POOL_SIZE}
connectionTimeoutMs = 2000
statementTimeoutMs = 5000
# Replicas run with false: compose applies migrations once in the `migrate` service.
migrateOnStart = true
migrateOnStart = ${?DB_MIGRATE_ON_START}
}
redis {
@@ -41,6 +44,39 @@ redis {
commandTimeoutMs = 500
}
cors {
# Defaults to false
#allowAnyHost = true
# Swagger UI sends "Try it out" to the contract server http://localhost:8080; pages opened elsewhere need these.
allowedHosts = [
{ host = "localhost:8080", schemes = ["http"] }
{ host = "localhost:8081", schemes = ["http"] }
{ host = "127.0.0.1:8080", schemes = ["http"] }
{ host = "127.0.0.1:8081", schemes = ["http"] }
]
# Defaults to true
#allowAnyMethod = true
#allowedMethods = [GET, POST, PATCH, PUT, DELETE, OPTIONS]
allowAnyHeader = true
#allowedHeaders = []
exposedHeaders = [X-Request-Id, X-Instance-Id, WWW-Authenticate]
# Browsers send Origin on same-origin POST too, e.g. Swagger UI served by the API itself.
allowSameOrigin = true
}
instance {
id = ${?INSTANCE_ID}
}
messages {
idempotencyTtlSeconds = 86400
idempotencyTtlSeconds = ${?IDEMPOTENCY_TTL_SECONDS}
}
authentication {
realm = "messenger"
sessionTtlSeconds = 3600
@@ -0,0 +1,19 @@
-- Idempotency-Key of POST /chats/{chat_id}/messages. Scope: user + chat (method and path are fixed) + key.
-- The row is inserted in the same transaction as the message, so a key exists only together with its message.
-- message_id is NULL only inside that transaction: the key is claimed first, the message is written next.
CREATE TABLE message_idempotency_keys (
user_id uuid NOT NULL,
chat_id uuid NOT NULL,
key varchar(128) NOT NULL,
request_hash varchar(64) NOT NULL,
message_id uuid NULL,
created_at timestamptz NOT NULL,
CONSTRAINT message_idempotency_keys_pkey PRIMARY KEY (user_id, chat_id, key),
CONSTRAINT message_idempotency_keys_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
CONSTRAINT message_idempotency_keys_chat_id_fkey FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE,
CONSTRAINT message_idempotency_keys_message_id_fkey FOREIGN KEY (message_id) REFERENCES messages (id) ON DELETE CASCADE
);
-- Foreign keys are not indexed automatically; the cascades from chats and messages need these.
CREATE INDEX message_idempotency_keys_chat_id_idx ON message_idempotency_keys (chat_id);
CREATE INDEX message_idempotency_keys_message_id_idx ON message_idempotency_keys (message_id);
@@ -5,6 +5,7 @@ import com.mtusi.messenger.main.infrastructure.dto.Message
import com.mtusi.messenger.main.infrastructure.dto.PageResponse
import com.mtusi.messenger.main.support.auth
import com.mtusi.messenger.main.support.createChat
import com.mtusi.messenger.main.support.idempotencyKey
import com.mtusi.messenger.main.support.register
import com.mtusi.messenger.main.support.runApp
import io.ktor.client.call.body
@@ -37,6 +38,7 @@ class MessageConcurrencyTest {
repeat(perWorker) { i ->
val response = client.post("/api/v1/chats/${chat.id}/messages") {
auth(alice)
idempotencyKey()
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = "w$w-i$i"))
}
@@ -6,6 +6,7 @@ import com.mtusi.messenger.main.infrastructure.dto.Message
import com.mtusi.messenger.main.infrastructure.dto.PageResponse
import com.mtusi.messenger.main.support.auth
import com.mtusi.messenger.main.support.createChat
import com.mtusi.messenger.main.support.idempotencyKey
import com.mtusi.messenger.main.support.register
import com.mtusi.messenger.main.support.runApp
import io.ktor.client.call.body
@@ -29,6 +30,7 @@ class MessageControllerTest {
val response = client.post("/api/v1/chats/${chat.id}/messages") {
auth(alice)
idempotencyKey()
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = ""))
}
@@ -45,6 +47,7 @@ class MessageControllerTest {
val response = client.post("/api/v1/chats/${chat.id}/messages") {
auth(bob)
idempotencyKey()
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = "hi"))
}
@@ -75,6 +78,7 @@ class MessageControllerTest {
repeat(total) { i ->
val r = client.post("/api/v1/chats/${chat.id}/messages") {
auth(alice)
idempotencyKey()
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = "m$i"))
}
@@ -115,6 +119,7 @@ class MessageControllerTest {
repeat(20) { i ->
client.post("/api/v1/chats/${chat.id}/messages") {
auth(alice)
idempotencyKey()
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = "m$i"))
}
@@ -164,6 +169,7 @@ class MessageControllerTest {
repeat(2) { i ->
client.post("/api/v1/chats/${chatA.id}/messages") {
auth(alice)
idempotencyKey()
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = "a$i"))
}
@@ -0,0 +1,157 @@
package com.mtusi.messenger.main.infrastructure.controllers.message
import com.mtusi.messenger.main.extensions.ErrorBody
import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest
import com.mtusi.messenger.main.infrastructure.dto.Chat
import com.mtusi.messenger.main.infrastructure.dto.Message
import com.mtusi.messenger.main.infrastructure.dto.PageResponse
import com.mtusi.messenger.main.support.TestUser
import com.mtusi.messenger.main.support.auth
import com.mtusi.messenger.main.support.createChat
import com.mtusi.messenger.main.support.idempotencyKey
import com.mtusi.messenger.main.support.register
import com.mtusi.messenger.main.support.runApp
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
class MessageIdempotencyTest {
private suspend fun HttpClient.send(user: TestUser, chat: Chat, text: String, key: String?): HttpResponse {
return post("/api/v1/chats/${chat.id}/messages") {
auth(user)
if (key != null) {
idempotencyKey(key)
}
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = text))
}
}
private suspend fun HttpClient.history(user: TestUser, chat: Chat): List<Message> {
return get("/api/v1/chats/${chat.id}/messages?limit=100") { auth(user) }.body<PageResponse<Message>>().items
}
@Test
fun `retry with the same key and body returns the original message`() = runApp { client ->
val alice = client.register("alice")
val chat = client.createChat(alice, "retry", listOf(alice.id))
val key = UUID.randomUUID().toString()
val first = client.send(alice, chat, "hello", key)
val retry = client.send(alice, chat, "hello", key)
assertEquals(HttpStatusCode.Created, first.status)
assertEquals(HttpStatusCode.Created, retry.status)
assertEquals(first.body<Message>(), retry.body<Message>())
assertEquals(1, client.history(alice, chat).size)
}
@Test
fun `same key with another body is a conflict`() = runApp { client ->
val alice = client.register("alice")
val chat = client.createChat(alice, "conflict", listOf(alice.id))
val key = UUID.randomUUID().toString()
client.send(alice, chat, "first", key)
val response = client.send(alice, chat, "second", key)
assertEquals(HttpStatusCode.Conflict, response.status)
assertEquals("conflict", response.body<ErrorBody>().error.code)
assertEquals(listOf("first"), client.history(alice, chat).map { it.text })
}
@Test
fun `missing or malformed key is rejected before anything is written`() = runApp { client ->
val alice = client.register("alice")
val chat = client.createChat(alice, "keys", listOf(alice.id))
for (key in listOf(null, "", "has space", "кириллица", "a".repeat(129))) {
val response = client.send(alice, chat, "text", key)
assertEquals(HttpStatusCode.BadRequest, response.status, "key: $key")
assertEquals("invalid_request", response.body<ErrorBody>().error.code)
}
assertEquals(HttpStatusCode.Created, client.send(alice, chat, "ok", "A-z_0.9:" + "x".repeat(120)).status)
assertEquals(1, client.history(alice, chat).size)
}
@Test
fun `key scope is user and chat`() = runApp { client ->
val alice = client.register("alice")
val bob = client.register("bob")
val first = client.createChat(alice, "one", listOf(alice.id, bob.id))
val second = client.createChat(alice, "two", listOf(alice.id, bob.id))
val key = UUID.randomUUID().toString()
val ids = listOf(
client.send(alice, first, "same", key),
client.send(bob, first, "same", key),
client.send(alice, second, "same", key)
).map {
assertEquals(HttpStatusCode.Created, it.status)
it.body<Message>().id
}
assertEquals(3, ids.toSet().size)
}
@Test
fun `retry by a former outsider is still checked for access`() = runApp { client ->
val alice = client.register("alice")
val eve = client.register("eve")
val chat = client.createChat(alice, "access", listOf(alice.id))
val key = UUID.randomUUID().toString()
client.send(alice, chat, "hello", key)
assertEquals(HttpStatusCode.NotFound, client.send(eve, chat, "hello", key).status)
}
@Test
fun `concurrent retries create exactly one message`() = runApp { client ->
val alice = client.register("alice")
val chat = client.createChat(alice, "concurrent", listOf(alice.id))
val key = UUID.randomUUID().toString()
val responses = withContext(Dispatchers.Default) {
coroutineScope {
(0 until 20).map { async { client.send(alice, chat, "once", key) } }.awaitAll()
}
}
assertEquals(List(20) { HttpStatusCode.Created }, responses.map { it.status })
assertEquals(1, responses.map { it.body<Message>() }.toSet().size)
assertEquals(1, client.history(alice, chat).size)
}
@Test
fun `expired key can be used for a new message`() = runApp(idempotencyTtlSeconds = 1) { client ->
val alice = client.register("alice")
val chat = client.createChat(alice, "ttl", listOf(alice.id))
val key = UUID.randomUUID().toString()
val first = client.send(alice, chat, "hello", key).body<Message>()
delay(1500)
val second = client.send(alice, chat, "hello again", key)
assertEquals(HttpStatusCode.Created, second.status)
assertNotEquals(first.id, second.body<Message>().id)
}
}
@@ -0,0 +1,26 @@
package com.mtusi.messenger.main.plugins
import com.mtusi.messenger.main.support.runApp
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.http.HttpStatusCode
import kotlin.test.Test
import kotlin.test.assertEquals
class CorsTest {
@Test
fun `page of the API itself may call it`() = runApp { client ->
val response = client.get("/health/ready") { header("Origin", "http://localhost") }
assertEquals(HttpStatusCode.OK, response.status)
}
@Test
fun `unknown origin is rejected`() = runApp { client ->
val response = client.get("/health/ready") { header("Origin", "http://evil.example") }
assertEquals(HttpStatusCode.Forbidden, response.status)
}
}
@@ -8,6 +8,7 @@ import com.mtusi.messenger.main.support.TestRedis
import com.mtusi.messenger.main.support.TestUser
import com.mtusi.messenger.main.support.auth
import com.mtusi.messenger.main.support.createChat
import com.mtusi.messenger.main.support.idempotencyKey
import com.mtusi.messenger.main.support.register
import com.mtusi.messenger.main.support.runApp
import com.mtusi.messenger.main.utils.computeSha256
@@ -176,6 +177,7 @@ class DependencyFailureTest {
HttpStatusCode.Unauthorized,
client.post("/api/v1/chats/${chat.id}/messages") {
auth(alice)
idempotencyKey()
contentType(ContentType.Application.Json)
setBody(CreateMessageRequest(text = "after logout"))
}.status
@@ -0,0 +1,30 @@
package com.mtusi.messenger.main.plugins
import com.mtusi.messenger.main.support.register
import com.mtusi.messenger.main.support.runApp
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.post
import kotlin.test.Test
import kotlin.test.assertEquals
class InstanceIdTest {
@Test
fun `every response names the replica`() = runApp { client ->
val alice = client.register("alice")
val responses = listOf(
client.get("/health/live"),
client.get("/health/ready"),
client.get("/api/v1/users/me") { header("Authorization", "Bearer ${alice.token}") },
client.get("/api/v1/users/me"),
client.get("/api/v1/chats/not-a-uuid") { header("Authorization", "Bearer ${alice.token}") },
client.post("/api/v1/chats") { header("Authorization", "Bearer ${alice.token}") },
client.get("/no/such/route")
)
assertEquals(List(responses.size) { "test-instance" }, responses.map { it.headers["X-Instance-Id"] })
}
}
@@ -13,6 +13,7 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.basicAuth
import io.ktor.client.request.bearerAuth
import io.ktor.client.request.header
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
@@ -56,6 +57,7 @@ private fun testConfig(
database: PostgreSQLContainer<*>,
databaseConnectionTimeoutMs: Long,
redis: RedisEndpoint,
idempotencyTtlSeconds: Long,
) = HoconApplicationConfig(
ConfigFactory.parseMap(
mapOf(
@@ -75,6 +77,9 @@ private fun testConfig(
"authentication.realm" to "messenger",
"authentication.sessionTtlSeconds" to sessionTtlSeconds,
"authentication.sessionCacheTtlSeconds" to 60,
"messages.idempotencyTtlSeconds" to idempotencyTtlSeconds,
"instance.id" to "test-instance",
"cors.allowSameOrigin" to true,
)
)
)
@@ -87,10 +92,11 @@ fun runApp(
database: PostgreSQLContainer<*> = TestDatabase.container,
databaseConnectionTimeoutMs: Long = 10000,
redis: RedisEndpoint = TestRedis.endpoint(),
idempotencyTtlSeconds: Long = 86400,
block: suspend ApplicationTestBuilder.(HttpClient) -> Unit
) = testApplication {
environment {
config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis)
config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds)
}
application {
module()
@@ -137,6 +143,11 @@ suspend fun HttpClient.register(prefix: String): TestUser {
fun HttpRequestBuilder.auth(user: TestUser) = bearerAuth(user.token)
/**
* Every message POST needs its own key since lab3; pass the same key to model a retry.
*/
fun HttpRequestBuilder.idempotencyKey(key: String = UUID.randomUUID().toString()) = header("Idempotency-Key", key)
suspend fun HttpClient.createChat(
creator: TestUser,
title: String,