From a3dbcd6ba4c5b2f57e4b20b02998ae371abc0fe2 Mon Sep 17 00:00:00 2001 From: Mraksimus Date: Thu, 22 Oct 2026 20:21:52 +0300 Subject: [PATCH 1/4] Move lab2 to lab3 --- .dockerignore | 18 + .gitignore | 4 + Dockerfile | 19 ++ build.gradle.kts | 115 +++++++ compose.yaml | 87 +++++ gradle.properties | 1 + gradle/libs.versions.toml | 54 +++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 43764 bytes gradle/wrapper/gradle-wrapper.properties | 7 + gradlew | 251 ++++++++++++++ gradlew.bat | 94 ++++++ run/_template/config/application.conf | 12 + run/_template/config/authentication.conf | 4 + run/_template/config/database.conf | 9 + run/_template/config/redis.conf | 6 + scripts/explain-history.sh | 10 + scripts/init-secrets.sh | 19 ++ scripts/restart.sh | 7 + scripts/sql/explain-history.sql | 100 ++++++ settings.gradle.kts | 17 + .../kotlin/com/mtusi/messenger/main/App.kt | 33 ++ .../main/compat/serializers/UUIDSerializer.kt | 31 ++ .../messenger/main/config/ConfigModule.kt | 8 + .../authentication/AuthenticationConfig.kt | 10 + .../DefaultAuthenticationConfig.kt | 11 + .../SerialAuthenticationConfig.kt | 11 + .../main/config/database/DatabaseConfig.kt | 20 ++ .../config/database/DefaultDatabaseConfig.kt | 11 + .../config/database/SerialDatabaseConfig.kt | 16 + .../main/config/redis/DefaultRedisConfig.kt | 11 + .../main/config/redis/RedisConfig.kt | 15 + .../main/config/redis/SerialRedisConfig.kt | 14 + .../main/extensions/ApplicationCall.kt | 37 +++ .../_common/parameters/PageParameters.kt | 31 ++ .../AuthenticationController.kt | 53 +++ .../responses/SessionResponse.kt | 13 + .../controllers/chat/ChatController.kt | 112 +++++++ .../chat/requests/CreateChatRequest.kt | 9 + .../controllers/health/HealthController.kt | 41 +++ .../health/responses/HealthResponse.kt | 6 + .../controllers/message/MessageController.kt | 88 +++++ .../message/requests/CreateMessageRequest.kt | 8 + .../controllers/user/UserController.kt | 83 +++++ .../user/requests/CreateUserRequest.kt | 10 + .../messenger/main/infrastructure/dto/Chat.kt | 13 + .../main/infrastructure/dto/Message.kt | 14 + .../main/infrastructure/dto/PageResponse.kt | 9 + .../messenger/main/infrastructure/dto/User.kt | 13 + .../dto/authentication/IssuedSession.kt | 6 + .../dto/authentication/Session.kt | 12 + .../dto/authentication/SessionRevocation.kt | 8 + .../dto/authentication/UserCredentials.kt | 8 + .../models/authentication/SessionModel.kt | 17 + .../authentication/SessionRevocationModel.kt | 11 + .../models/chat/ChatMemberModel.kt | 17 + .../infrastructure/models/chat/ChatModel.kt | 12 + .../models/message/MessageModel.kt | 20 ++ .../infrastructure/models/user/UserModel.kt | 14 + .../repositories/RepositoriesModule.kt | 8 + .../infrastructure/repositories/Repository.kt | 3 + .../repositories/_common/RedisConnection.kt | 88 +++++ .../session/ExposedSessionRepository.kt | 73 ++++ .../session/SessionRepository.kt | 26 ++ .../session/cache/CachedSession.kt | 11 + .../cache/RedisSessionCacheRepository.kt | 87 +++++ .../session/cache/SessionCacheRepository.kt | 34 ++ .../ExposedSessionRevocationRepository.kt | 52 +++ .../revocation/SessionRevocationRepository.kt | 31 ++ .../repositories/chat/ChatRepository.kt | 33 ++ .../chat/ExposedChatRepository.kt | 134 ++++++++ .../message/ExposedMessageRepository.kt | 81 +++++ .../repositories/message/MessageRepository.kt | 22 ++ .../user/ExposedUserRepository.kt | 82 +++++ .../repositories/user/UserRepository.kt | 28 ++ .../infrastructure/services/ServicesModule.kt | 8 + .../authentication/AuthenticationService.kt | 34 ++ .../DefaultAuthenticationService.kt | 311 ++++++++++++++++++ .../services/chat/ChatService.kt | 27 ++ .../services/chat/DefaultChatService.kt | 79 +++++ .../chat/results/ChatCreationResult.kt | 17 + .../services/chat/results/ChatPageResult.kt | 16 + .../services/hashing/DefaultHashingService.kt | 123 +++++++ .../services/hashing/HashingService.kt | 18 + .../hashing/types/HashingAlgorithm.kt | 5 + .../services/message/DefaultMessageService.kt | 68 ++++ .../services/message/MessageService.kt | 22 ++ .../message/results/MessageCreationResult.kt | 15 + .../message/results/MessagePageResult.kt | 17 + .../services/user/DefaultUserService.kt | 43 +++ .../services/user/UserService.kt | 19 ++ .../user/results/UserCreationResult.kt | 15 + .../main/infrastructure/types/ApiException.kt | 3 + .../infrastructure/types/AuthChallenge.kt | 6 + .../main/infrastructure/types/ErrorCode.kt | 15 + .../messenger/main/plugins/CallLogging.kt | 14 + .../main/plugins/ContentNegotiation.kt | 21 ++ .../mtusi/messenger/main/plugins/Database.kt | 149 +++++++++ .../messenger/main/plugins/DefaultHeaders.kt | 7 + .../com/mtusi/messenger/main/plugins/Koin.kt | 27 ++ .../com/mtusi/messenger/main/plugins/Redis.kt | 16 + .../mtusi/messenger/main/plugins/RequestId.kt | 20 ++ .../mtusi/messenger/main/plugins/Routing.kt | 18 + .../messenger/main/plugins/StatusPages.kt | 78 +++++ .../mtusi/messenger/main/plugins/Swagger.kt | 8 + .../plugins/authentication/Authentication.kt | 43 +++ .../authentication/CredentialsPrincipal.kt | 7 + .../plugins/authentication/UserPrincipal.kt | 13 + .../providers/BasicCredentialsProvider.kt | 66 ++++ .../providers/BearerSessionProvider.kt | 71 ++++ .../com/mtusi/messenger/main/utils/Hashing.kt | 10 + .../mtusi/messenger/main/utils/Pagination.kt | 64 ++++ .../com/mtusi/messenger/main/utils/Secrets.kt | 13 + .../com/mtusi/messenger/main/utils/Time.kt | 18 + .../mtusi/messenger/main/utils/Validation.kt | 36 ++ .../utils/exposed/operations/RowGreater.kt | 32 ++ src/main/resources/application.conf | 52 +++ src/main/resources/logback.xml | 12 + src/main/resources/migrations/V1__init.sql | 50 +++ .../migrations/V2__session_revocations.sql | 6 + .../AuthenticationControllerTest.kt | 151 +++++++++ .../controllers/chat/ChatControllerTest.kt | 152 +++++++++ .../health/HealthControllerTest.kt | 22 ++ .../message/MessageConcurrencyTest.kt | 75 +++++ .../message/MessageControllerTest.kt | 191 +++++++++++ .../controllers/user/UserControllerTest.kt | 180 ++++++++++ .../cache/RedisSessionCacheRepositoryTest.kt | 83 +++++ .../chat/ExposedChatRepositoryTest.kt | 62 ++++ .../SessionLookupSingleFlightTest.kt | 174 ++++++++++ .../hashing/DefaultHashingServiceTest.kt | 45 +++ .../main/plugins/DatabaseMigrationTest.kt | 49 +++ .../main/plugins/DependencyFailureTest.kt | 217 ++++++++++++ .../main/plugins/SessionCacheTest.kt | 65 ++++ .../mtusi/messenger/main/support/TestApp.kt | 152 +++++++++ .../messenger/main/support/TestDatabase.kt | 30 ++ .../mtusi/messenger/main/support/TestRedis.kt | 54 +++ 135 files changed, 5897 insertions(+) create mode 100644 Dockerfile create mode 100644 build.gradle.kts create mode 100644 compose.yaml create mode 100644 gradle.properties create mode 100644 gradle/libs.versions.toml create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 run/_template/config/application.conf create mode 100644 run/_template/config/authentication.conf create mode 100644 run/_template/config/database.conf create mode 100644 run/_template/config/redis.conf create mode 100755 scripts/explain-history.sh create mode 100755 scripts/init-secrets.sh create mode 100755 scripts/restart.sh create mode 100644 scripts/sql/explain-history.sql create mode 100644 settings.gradle.kts create mode 100644 src/main/kotlin/com/mtusi/messenger/main/App.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/compat/serializers/UUIDSerializer.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/ConfigModule.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/authentication/DefaultAuthenticationConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/database/DefaultDatabaseConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/redis/DefaultRedisConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/redis/RedisConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/redis/SerialRedisConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/extensions/ApplicationCall.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/_common/parameters/PageParameters.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationController.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/responses/SessionResponse.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/requests/CreateChatRequest.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/responses/HealthResponse.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/requests/CreateUserRequest.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Chat.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/PageResponse.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/User.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/IssuedSession.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/Session.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/SessionRevocation.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/UserCredentials.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionRevocationModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatMemberModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/user/UserModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/Repository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/_common/RedisConnection.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/SessionRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/CachedSession.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/ExposedSessionRevocationRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/UserRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/ChatService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatCreationResult.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatPageResult.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/HashingService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/types/HashingAlgorithm.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessagePageResult.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/DefaultUserService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/UserService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/results/UserCreationResult.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ApiException.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/AuthChallenge.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ErrorCode.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/CallLogging.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/ContentNegotiation.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/DefaultHeaders.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/Redis.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/RequestId.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/StatusPages.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/Swagger.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/Authentication.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/CredentialsPrincipal.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/UserPrincipal.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BasicCredentialsProvider.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BearerSessionProvider.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/utils/Hashing.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt create mode 100644 src/main/resources/application.conf create mode 100644 src/main/resources/logback.xml create mode 100644 src/main/resources/migrations/V1__init.sql create mode 100644 src/main/resources/migrations/V2__session_revocations.sql create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationControllerTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatControllerTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthControllerTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserControllerTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepositoryTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingServiceTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/plugins/DatabaseMigrationTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/plugins/SessionCacheTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/support/TestRedis.kt diff --git a/.dockerignore b/.dockerignore index bc16e556..c3256588 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,21 @@ __pycache__ node_modules evidence .DS_Store +.gradle +.kotlin +build +.idea +*.iml +tests +scripts +examples +run +COURSE.md +CONTRIBUTING.md +MAINTAINERS.md +REPORT.md +README.md +requirements-dev.txt +Dockerfile.example +compose.example.yaml +Makefile diff --git a/.gitignore b/.gitignore index b5a71896..6137e904 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,9 @@ coverage/ .DS_Store .idea/ .vscode/ +.gradle/ +.kotlin/ +!gradle/wrapper/ +/run/development/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..096e5a7a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM gradle:8.14-jdk21 AS build +WORKDIR /src +COPY settings.gradle.kts build.gradle.kts gradle.properties ./ +COPY gradle ./gradle +COPY gradlew ./ +COPY src ./src +COPY contracts ./contracts +RUN --mount=type=cache,target=/home/gradle/.gradle gradle --no-daemon shadowJar + +FROM eclipse-temurin:21-jre-alpine +RUN apk add --no-cache wget && addgroup -S app && adduser -S -G app app +WORKDIR /app +COPY --from=build /src/build/libs/messenger.jar /app/messenger.jar +USER app +ENV APP_HOST=0.0.0.0 APP_PORT=8080 +EXPOSE 8080 +HEALTHCHECK --interval=10s --timeout=3s --start-period=15s --retries=3 \ + CMD wget -qO- http://127.0.0.1:8080/health/ready || exit 1 +ENTRYPOINT ["java","-XX:MaxRAMPercentage=75.0","-jar","/app/messenger.jar"] diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 00000000..0a5b5720 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,115 @@ +plugins { + alias(libs.plugins.kotlin) + alias(libs.plugins.kotlin.serialization) + alias(libs.plugins.ksp) + alias(libs.plugins.shadow) + id("application") +} + +group = "com.mtusi.messenger" +version = "2.0.0" + +repositories { + mavenCentral() +} + +dependencies { + implementation(libs.kotlinx.serialization.json) + implementation(libs.kotlinx.serialization.hocon) + implementation(libs.logback.classic) + + implementation(ktorLibs.server.core) + implementation(ktorLibs.server.netty) + implementation(ktorLibs.server.auth) + implementation(ktorLibs.server.contentNegotiation) + implementation(ktorLibs.server.statusPages) + implementation(ktorLibs.server.callLogging) + implementation(ktorLibs.server.defaultHeaders) + implementation(ktorLibs.server.swagger) + implementation(ktorLibs.serialization.kotlinx.json) + + implementation(libs.koin.core) + implementation(libs.koin.ktor) + implementation(libs.koin.annotations) + ksp(libs.koin.compiler) + + implementation(libs.exposed.core) + implementation(libs.exposed.jdbc) + implementation(libs.exposed.kotlin.datetime) + implementation(libs.exposed.migration.core) + implementation(libs.exposed.migration.jdbc) + + implementation(libs.postgres) + implementation(libs.hikaricp) + implementation(libs.flyway) + implementation(libs.flyway.postgresql) + + implementation(libs.lettuce) + + implementation(libs.bouncycastle) + + testImplementation(kotlin("test")) + testImplementation(ktorLibs.server.testHost) + testImplementation(ktorLibs.client.contentNegotiation) + testImplementation(libs.kotlinx.coroutines.test) + testImplementation(libs.koin.test) + testImplementation(libs.testContainers) + testImplementation(libs.testContainers.postgresql) +} + +kotlin { + jvmToolchain(21) +} + +ksp { + arg("KOIN_CONFIG_CHECK", "true") + arg("KOIN_DEFAULT_MODULE", "false") +} + +application { + mainClass = "io.ktor.server.netty.EngineMain" + applicationDefaultJvmArgs = listOf("-Dio.ktor.development=false") +} + +sourceSets { + named("main") { + resources { + srcDir(layout.projectDirectory.dir("contracts")) + } + } +} + +tasks { + test { + useJUnitPlatform() + } + + shadowJar { + archiveBaseName = "messenger" + archiveClassifier = "" + archiveVersion = "" + mergeServiceFiles() + } + + getByName("run") { + + val configArgs = File("run/development/config") + .listFiles() + ?.filter { it.extension == "conf" } + ?.map { + "-config=config/${it.name}" + } + ?: emptyList() + + args = configArgs + workingDir = file("run/development") + } + + register("generateMigration") { + group = "database" + description = "Generates a new migration" + + classpath = sourceSets["main"].runtimeClasspath + mainClass.set("com.mtusi.messenger.main.plugins.DatabaseKt") + } +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..42c56457 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,87 @@ +name: mtusi-messenger-lab3 + +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: + POSTGRES_DB: messenger + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/db_password + secrets: + - db_password + volumes: + - postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 10s + restart: unless-stopped + + 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: + - sh + - -c + - >- + exec docker-entrypoint.sh redis-server + --requirepass "$$(cat /run/secrets/redis_password)" + --save "" + --appendonly no + --maxmemory 64mb + --maxmemory-policy volatile-lru + secrets: + - redis_password + healthcheck: + test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping | grep -q PONG"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + restart: unless-stopped + + api: + build: . + image: mtusi-messenger:lab3 + container_name: mtusi-lab3-api + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + environment: + APP_HOST: "0.0.0.0" + APP_PORT: "8080" + DB_HOST: postgres + DB_PORT: "5432" + DB_NAME: messenger + DB_USER: messenger + DB_PASSWORD_FILE: /run/secrets/db_password + 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} + secrets: + - db_password + - redis_password + ports: + - "127.0.0.1:8080:8080" + restart: unless-stopped + +secrets: + db_password: + file: ./secrets/db_password + redis_password: + file: ./secrets/redis_password + +volumes: + postgres-data: diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..7fc6f1ff --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 00000000..ec020b4d --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,54 @@ +[versions] +kotlin = "2.3.20" +ksp = "2.3.0" +kotlinx-serialization = "1.10.0" +kotlinx-coroutines = "1.9.0" +logback = "1.5.13" +shadow = "8.3.5" +koin = "4.1.0-RC1" +koin-ktor = "4.1.0-Beta8" +koin-compiler = "2.0.0" +exposed = "1.0.0" +postgres = "42.7.3" +hikaricp = "6.3.0" +flyway = "11.9.0" +lettuce = "6.7.1.RELEASE" +bouncycastle = "1.81" +test-containers = "1.21.0" + +[plugins] +kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } +shadow = { id = "com.gradleup.shadow", version.ref = "shadow" } + +[libraries] +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } +kotlinx-serialization-hocon = { module = "org.jetbrains.kotlinx:kotlinx-serialization-hocon", version.ref = "kotlinx-serialization" } +kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" } + +logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } + +koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" } +koin-ktor = { module = "io.insert-koin:koin-ktor3", version.ref = "koin-ktor" } +koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin-compiler" } +koin-compiler = { module = "io.insert-koin:koin-ksp-compiler", version.ref = "koin-compiler" } +koin-test = { module = "io.insert-koin:koin-test", version.ref = "koin" } + +exposed-core = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" } +exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "exposed" } +exposed-kotlin-datetime = { module = "org.jetbrains.exposed:exposed-kotlin-datetime", version.ref = "exposed" } +exposed-migration-core = { module = "org.jetbrains.exposed:exposed-migration-core", version.ref = "exposed" } +exposed-migration-jdbc = { module = "org.jetbrains.exposed:exposed-migration-jdbc", version.ref = "exposed" } + +postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" } +hikaricp = { module = "com.zaxxer:HikariCP", version.ref = "hikaricp" } +flyway = { module = "org.flywaydb:flyway-core", version.ref = "flyway" } +flyway-postgresql = { module = "org.flywaydb:flyway-database-postgresql", version.ref = "flyway" } + +lettuce = { module = "io.lettuce:lettuce-core", version.ref = "lettuce" } + +bouncycastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bouncycastle" } + +testContainers = { module = "org.testcontainers:testcontainers", version.ref = "test-containers" } +testContainers-postgresql = { module = "org.testcontainers:postgresql", version.ref = "test-containers" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000000000000000000000000000000000..1b33c55baabb587c669f562ae36f953de2481846 GIT binary patch literal 43764 zcma&OWmKeVvL#I6?i3D%6z=Zs?ofE*?rw#G$eqJB ziT4y8-Y@s9rkH0Tz>ll(^xkcTl)CY?rS&9VNd66Yc)g^6)JcWaY(5$5gt z8gr3SBXUTN;~cBgz&})qX%#!Fxom2Yau_`&8)+6aSN7YY+pS410rRUU*>J}qL0TnJ zRxt*7QeUqTh8j)Q&iavh<}L+$Jqz))<`IfKussVk%%Ah-Ti?Eo0hQH!rK%K=#EAw0 zwq@@~XNUXRnv8$;zv<6rCR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N$8TsTavXs)tLDFJtY9b)y~Tl@7z4^I8oUQq4JckH@~CVQ;FoK(+e0XAM>1O z(ei}h?)JQp>)d=6ng-BZF1Z5hsAKW@mXq+hU?r8I(*%`tnIIOXw7V6ZK(T9RFJJe@ zZS!aC+p)Gf2Ujc=a6hx4!A1Th%YH!Lb^xpI!Eu` zmJO{9rw){B1Ql18d%F%da+Tbu1()?o(zT7StYqK6_w`e+fjXq5L^y(0 z09QA6H4oFj59c2wR~{~>jUoDzDdKz}5#onYPJRwa`SUO)Pd4)?(ENBaFVLJr6Kvz= zhTtXqbx09C1z~~iZt;g^9_2nCZ{};-b4dQJbv8HsWHXPVg^@(*!@xycp#R?a|L!+` zY5w))JWV`Gls(=}shH0#r*;~>_+-P5Qc978+QUd>J%`fyn{*TsiG-dWMiJXNgwBaT zJ=wgYFt+1ACW)XwtNx)Q9tA2LPoB&DkL16P)ERWQlY4%Y`-5aM9mZ{eKPUgI!~J3Z zkMd5A_p&v?V-o-6TUa8BndiX?ooviev(DKw=*bBVOW|=zps9=Yl|-R5@yJe*BPzN}a0mUsLn{4LfjB_oxpv(mwq# zSY*%E{iB)sNvWfzg-B!R!|+x(Q|b@>{-~cFvdDHA{F2sFGA5QGiIWy#3?P2JIpPKg6ncI^)dvqe`_|N=8 '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..db3a6ac2 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/run/_template/config/application.conf b/run/_template/config/application.conf new file mode 100644 index 00000000..d9373003 --- /dev/null +++ b/run/_template/config/application.conf @@ -0,0 +1,12 @@ +ktor { + + application { + modules = [com.mtusi.messenger.main.AppKt.module] + } + + deployment { + host = "127.0.0.1" + port = 8080 + } + +} diff --git a/run/_template/config/authentication.conf b/run/_template/config/authentication.conf new file mode 100644 index 00000000..8debebf3 --- /dev/null +++ b/run/_template/config/authentication.conf @@ -0,0 +1,4 @@ +authentication { + realm = "messenger" + sessionTtlSeconds = 3600 +} diff --git a/run/_template/config/database.conf b/run/_template/config/database.conf new file mode 100644 index 00000000..6de16a67 --- /dev/null +++ b/run/_template/config/database.conf @@ -0,0 +1,9 @@ +database { + host = "localhost" + port = 5432 + name = "messenger" + username = "username" + password = "password" + maxPoolSize = 10 + connectionTimeoutMs = 2000 +} diff --git a/run/_template/config/redis.conf b/run/_template/config/redis.conf new file mode 100644 index 00000000..f6a9b183 --- /dev/null +++ b/run/_template/config/redis.conf @@ -0,0 +1,6 @@ +redis { + host = "localhost" + port = 6379 + connectTimeoutMs = 500 + commandTimeoutMs = 500 +} diff --git a/scripts/explain-history.sh b/scripts/explain-history.sh new file mode 100755 index 00000000..fa0c07e0 --- /dev/null +++ b/scripts/explain-history.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# Runs EXPLAIN (ANALYZE, BUFFERS) of the chat history query against the Compose PostgreSQL on 70 000 synthetic +# messages. The data is inserted and rolled back in one transaction; the plans are saved to evidence/. +set -eu +cd "$(dirname "$0")/.." + +mkdir -p evidence + +docker compose exec -T postgres psql -U messenger -d messenger -X -q < scripts/sql/explain-history.sql \ + | tee evidence/explain-history.txt diff --git a/scripts/init-secrets.sh b/scripts/init-secrets.sh new file mode 100755 index 00000000..70d9c47b --- /dev/null +++ b/scripts/init-secrets.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Creates local secrets for the Compose stack. Never overwrites an existing secret: +# the PostgreSQL volume is initialised with the first password and keeps it. +set -eu +cd "$(dirname "$0")/.." + +mkdir -p secrets + +# Readable by the non-root users inside the containers (bind-mounted as is). +umask 022 + +for name in db_password redis_password; do + if [ ! -s "secrets/$name" ]; then + openssl rand -hex 24 > "secrets/$name" + echo "created secrets/$name" + else + echo "secrets/$name already exists, keeping it" + fi +done diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..76c2bb0c --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# Restarts the API, 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 diff --git a/scripts/sql/explain-history.sql b/scripts/sql/explain-history.sql new file mode 100644 index 00000000..8467e137 --- /dev/null +++ b/scripts/sql/explain-history.sql @@ -0,0 +1,100 @@ +-- EXPLAIN (ANALYZE, BUFFERS) of the chat history query on synthetic data. +-- Everything runs in one transaction that is rolled back: the database is left untouched. +-- The queries are the ones ExposedMessageRepository.findPageByChatId generates (page size 50 + 1 look-ahead row). + +\set ON_ERROR_STOP on +\pset pager off + +BEGIN; + +INSERT INTO users (id, username, display_name, password_hash, created_at) +VALUES ('00000000-0000-0000-0000-000000000001', 'explain_alice', 'Alice', 'not-a-hash', now()), + ('00000000-0000-0000-0000-000000000002', 'explain_bob', 'Bob', 'not-a-hash', now()); + +-- One large chat (20 000 messages) and 50 neighbours with 1 000 messages each: 70 000 rows in total, +-- so the plan has to skip foreign chats instead of reading a table that only holds the target chat. +INSERT INTO chats (id, title, created_at) +SELECT ('00000000-0000-0000-0001-' || lpad(n::text, 12, '0'))::uuid, 'explain chat ' || n, now() +FROM generate_series(0, 50) AS n; + +INSERT INTO chat_members (chat_id, user_id, position) +SELECT c.id, u.id, u.position +FROM chats c +CROSS JOIN (VALUES ('00000000-0000-0000-0000-000000000001'::uuid, 0), + ('00000000-0000-0000-0000-000000000002'::uuid, 1)) AS u (id, position) +WHERE c.title LIKE 'explain chat %'; + +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), + '00000000-0000-0000-0001-000000000000', + CASE WHEN n % 2 = 0 THEN '00000000-0000-0000-0000-000000000001'::uuid + ELSE '00000000-0000-0000-0000-000000000002'::uuid END, + 'message #' || n, + timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second' +FROM generate_series(1, 20000) AS n; + +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), + ('00000000-0000-0000-0001-' || lpad(c::text, 12, '0'))::uuid, + '00000000-0000-0000-0000-000000000001', + 'neighbour message #' || n, + timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second' +FROM generate_series(1, 50) AS c, generate_series(1, 1000) AS n; + +ANALYZE users, chats, chat_members, messages; + +\echo +\echo '== Data volume' +SELECT count(*) AS total_messages, + count(*) FILTER (WHERE chat_id = '00000000-0000-0000-0001-000000000000') AS target_chat_messages +FROM messages; + +-- Cursor of the 15 000th message: a deep page, where OFFSET pagination would have to skip 15 000 rows. +SELECT created_at AS cursor_created_at, id AS cursor_id +FROM messages +WHERE chat_id = '00000000-0000-0000-0001-000000000000' +ORDER BY created_at, id +OFFSET 14999 LIMIT 1 \gset + +\echo +\echo '== 1. First page: WHERE chat_id = ? ORDER BY created_at, id LIMIT 51' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000' +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51; + +\echo +\echo '== 2. Deep page after the 15 000th message: (created_at, id) > (?, ?)' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000') + AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id')) +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51; + +\echo +\echo '== 3. Same deep page with OFFSET instead of a cursor (for comparison, not used by the API)' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000' +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51 OFFSET 15000; + +\echo +\echo '== 4. Deep page without messages_chat_id_created_at_id_idx (index scans disabled for this query only)' +SET LOCAL enable_indexscan = off; +SET LOCAL enable_bitmapscan = off; +SET LOCAL enable_indexonlyscan = off; +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000') + AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id')) +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51; + +ROLLBACK; diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 00000000..7e9d725d --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,17 @@ +dependencyResolutionManagement { + repositories { + mavenCentral() + } + + versionCatalogs { + create("ktorLibs") { + from("io.ktor:ktor-version-catalog:3.3.0") + } + } +} + +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0" +} + +rootProject.name = "mtusi-messenger-lab3" diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt new file mode 100644 index 00000000..8ec647e2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -0,0 +1,33 @@ +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.configureDatabase +import com.mtusi.messenger.main.plugins.configureDefaultHeaders +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 io.ktor.server.application.Application +import io.ktor.server.netty.EngineMain + +fun main(args: Array): Unit = EngineMain.main(args) + +fun Application.module() { + + configureKoin() + configureDatabase() + configureRedis() + + configureContentNegotiation() + configureDefaultHeaders() + configureRequestId() + configureCallLogging() + configureStatusPages() + + configureAuthentication() + configureRouting() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/UUIDSerializer.kt b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/UUIDSerializer.kt new file mode 100644 index 00000000..5a1ccff8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/UUIDSerializer.kt @@ -0,0 +1,31 @@ +package com.mtusi.messenger.main.compat.serializers + +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 +import java.util.UUID + +typealias SerialUUID = @Serializable(UUIDSerializer::class) UUID + +object UUIDSerializer : KSerializer { + + override val descriptor = PrimitiveSerialDescriptor( + serialName = "UUID", + kind = PrimitiveKind.STRING + ) + + override fun deserialize(decoder: Decoder): UUID { + return decoder.decodeString().run(UUID::fromString) + } + + override fun serialize( + encoder: Encoder, + value: UUID + ) { + encoder.encodeString(value.toString()) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/ConfigModule.kt b/src/main/kotlin/com/mtusi/messenger/main/config/ConfigModule.kt new file mode 100644 index 00000000..e4763008 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/ConfigModule.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.config + +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module + +@Module(createdAtStart = true) +@ComponentScan +class ConfigModule diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt new file mode 100644 index 00000000..7d948f5b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.config.authentication + +interface AuthenticationConfig { + + val realm: String + val sessionTtlSeconds: Long + val sessionCacheTtlSeconds: Long + val sessionCacheSingleFlight: Boolean + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/DefaultAuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/DefaultAuthenticationConfig.kt new file mode 100644 index 00000000..7be2ab3f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/DefaultAuthenticationConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.authentication + +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 DefaultAuthenticationConfig( + @Provided app: Application +) : AuthenticationConfig by app.environment.config.property("authentication").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt new file mode 100644 index 00000000..82431688 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.authentication + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialAuthenticationConfig( + override val realm: String, + override val sessionTtlSeconds: Long, + override val sessionCacheTtlSeconds: Long, + override val sessionCacheSingleFlight: Boolean = true +) : AuthenticationConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt new file mode 100644 index 00000000..40d48c28 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.config.database + +interface DatabaseConfig { + + val host: String + val port: Int + val name: String + + val username: String + val password: String? + val passwordFile: String? + + val maxPoolSize: Int + val connectionTimeoutMs: Long + val statementTimeoutMs: Long + + val jdbcConnectionUrl: String + get() = "jdbc:postgresql://$host:$port/$name" + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/DefaultDatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/DefaultDatabaseConfig.kt new file mode 100644 index 00000000..72a6c044 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/DefaultDatabaseConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.database + +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 DefaultDatabaseConfig( + @Provided app: Application +) : DatabaseConfig by app.environment.config.property("database").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt new file mode 100644 index 00000000..00c4890e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.config.database + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialDatabaseConfig( + override val host: String, + override val port: Int, + override val name: String, + override val username: String, + override val password: String? = null, + override val passwordFile: String? = null, + override val maxPoolSize: Int, + override val connectionTimeoutMs: Long, + override val statementTimeoutMs: Long +) : DatabaseConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/redis/DefaultRedisConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/redis/DefaultRedisConfig.kt new file mode 100644 index 00000000..9b7b168e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/redis/DefaultRedisConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.redis + +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 DefaultRedisConfig( + @Provided app: Application +) : RedisConfig by app.environment.config.property("redis").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/redis/RedisConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/redis/RedisConfig.kt new file mode 100644 index 00000000..4393403d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/redis/RedisConfig.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.config.redis + +interface RedisConfig { + + val host: String + val port: Int + + val username: String? + val password: String? + val passwordFile: String? + + val connectTimeoutMs: Long + val commandTimeoutMs: Long + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/redis/SerialRedisConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/redis/SerialRedisConfig.kt new file mode 100644 index 00000000..fddb8ed2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/redis/SerialRedisConfig.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.config.redis + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialRedisConfig( + override val host: String, + override val port: Int, + override val username: String? = null, + override val password: String? = null, + override val passwordFile: String? = null, + override val connectTimeoutMs: Long, + override val commandTimeoutMs: Long +) : RedisConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/extensions/ApplicationCall.kt b/src/main/kotlin/com/mtusi/messenger/main/extensions/ApplicationCall.kt new file mode 100644 index 00000000..0fc40818 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/extensions/ApplicationCall.kt @@ -0,0 +1,37 @@ +package com.mtusi.messenger.main.extensions + +import com.mtusi.messenger.main.infrastructure.types.AuthChallenge +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.RequestIdKey +import com.mtusi.messenger.main.plugins.authentication.UserPrincipal +import io.ktor.http.HttpHeaders +import io.ktor.server.application.ApplicationCall +import io.ktor.server.auth.principal +import io.ktor.server.response.header +import io.ktor.server.response.respond +import kotlinx.serialization.Serializable + +@Serializable +data class ErrorBody(val error: ErrorDetails) + +@Serializable +data class ErrorDetails( + val code: String, + val message: String, + val requestId: String, +) + +suspend fun ApplicationCall.respondError( + code: ErrorCode, + message: String, + challenge: String = AuthChallenge.BEARER, +) { + val requestId = attributes[RequestIdKey] + if (code == ErrorCode.UNAUTHORIZED) { + response.header(HttpHeaders.WWWAuthenticate, challenge) + } + respond(code.status, ErrorBody(ErrorDetails(code.wire, message, requestId))) +} + +val ApplicationCall.currentUser: UserPrincipal + get() = principal()!! diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/_common/parameters/PageParameters.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/_common/parameters/PageParameters.kt new file mode 100644 index 00000000..043b686e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/_common/parameters/PageParameters.kt @@ -0,0 +1,31 @@ +package com.mtusi.messenger.main.infrastructure.controllers._common.parameters + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.server.application.ApplicationCall + +private const val DEFAULT_LIMIT = 50 +private const val MAX_LIMIT = 100 + +data class PageParameters( + val limit: Int, + val cursor: String? +) + +fun ApplicationCall.pageParameters(): PageParameters { + + val query = request.queryParameters + + val limit = query["limit"] + ?.let { it.toIntOrNull() ?: throw ApiException(ErrorCode.INVALID_REQUEST, "invalid limit") } + ?: DEFAULT_LIMIT + + if (limit !in 1..MAX_LIMIT) { + throw ApiException(ErrorCode.INVALID_REQUEST, "limit out of range") + } + + val cursor = query["cursor"] + ?.takeIf { it.isNotEmpty() } + + return PageParameters(limit, cursor) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationController.kt new file mode 100644 index 00000000..757f629b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationController.kt @@ -0,0 +1,53 @@ +package com.mtusi.messenger.main.infrastructure.controllers.authentication + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse +import com.mtusi.messenger.main.infrastructure.services.authentication.AuthenticationService +import com.mtusi.messenger.main.plugins.authentication.CREDENTIALS_AUTH +import com.mtusi.messenger.main.plugins.authentication.CredentialsPrincipal +import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH +import com.mtusi.messenger.main.plugins.suspendedTransaction +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.auth.principal +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.delete +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +fun Routing.configureAuthenticationRouting() = route("/api/v1/auth/sessions") { + + val authenticationService by inject() + + authenticate(CREDENTIALS_AUTH) { + post { + + val (user) = call.principal()!! + + val issued = suspendedTransaction { + authenticationService.createSession(user.id) + } + + call.respond( + HttpStatusCode.Created, + SessionResponse( + token = issued.token, + expiresAt = issued.session.expiresAt, + user = user + ) + ) + } + } + + authenticate(SESSION_AUTH) { + delete("current") { + + authenticationService.revokeSession(call.currentUser.session) + + call.respond(HttpStatusCode.NoContent) + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/responses/SessionResponse.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/responses/SessionResponse.kt new file mode 100644 index 00000000..f9c4e343 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/responses/SessionResponse.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.controllers.authentication.responses + +import com.mtusi.messenger.main.infrastructure.dto.User +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class SessionResponse( + val token: String, + val tokenType: String = "Bearer", + val expiresAt: Instant, + val user: User, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt new file mode 100644 index 00000000..e2fd839a --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt @@ -0,0 +1,112 @@ +package com.mtusi.messenger.main.infrastructure.controllers.chat + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers._common.parameters.pageParameters +import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest +import com.mtusi.messenger.main.infrastructure.models.chat.MAX_CHAT_MEMBERS +import com.mtusi.messenger.main.infrastructure.models.chat.MAX_CHAT_TITLE_LENGTH +import com.mtusi.messenger.main.infrastructure.services.chat.ChatService +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatCreationResult +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatPageResult +import com.mtusi.messenger.main.infrastructure.types.ApiException +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.requireNonBlankBounded +import com.mtusi.messenger.main.utils.requireUuid +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +fun Routing.configureChatRouting() = route("/api/v1/chats") { + + val chatService by inject() + + authenticate(SESSION_AUTH) { + + post { + + val actor = call.currentUser + val body = call.receive() + + requireNonBlankBounded( + body.title, + min = 1, + max = MAX_CHAT_TITLE_LENGTH, + field = "title" + ) + + if (body.memberIds.isEmpty() || body.memberIds.size > MAX_CHAT_MEMBERS) { + throw ApiException(ErrorCode.INVALID_REQUEST, "member_ids size out of range") + } + + val memberIds = body.memberIds.map { requireUuid(it, "member_ids") } + + val result = suspendedTransaction { + chatService.create( + creatorId = actor.id, + title = body.title, + memberIds = memberIds + ) + } + + when (result) { + is ChatCreationResult.DuplicateMembers -> + call.respondError(ErrorCode.INVALID_REQUEST, "member_ids must be unique") + is ChatCreationResult.CreatorNotMember -> + call.respondError(ErrorCode.INVALID_REQUEST, "creator must be in member ids") + is ChatCreationResult.UnknownMembers -> + call.respondError(ErrorCode.INVALID_REQUEST, "unknown member") + is ChatCreationResult.Success -> + call.respond(HttpStatusCode.Created, result.chat) + } + } + + get { + + val actor = call.currentUser + val (limit, cursor) = call.pageParameters() + + val result = suspendedTransaction { + chatService.findPageAccessible( + userId = actor.id, + limit = limit, + cursor = cursor + ) + } + + when (result) { + is ChatPageResult.InvalidCursor -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid cursor") + is ChatPageResult.Success -> + call.respond(HttpStatusCode.OK, result.page) + } + } + + get("{chat_id}") { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + + val chat = suspendedTransaction { + chatService.findAccessible(actor.id, chatId) + } + + if (chat == null) { + call.respondError(ErrorCode.NOT_FOUND, "chat not found") + return@get + } + + call.respond(HttpStatusCode.OK, chat) + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/requests/CreateChatRequest.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/requests/CreateChatRequest.kt new file mode 100644 index 00000000..d0505b9d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/requests/CreateChatRequest.kt @@ -0,0 +1,9 @@ +package com.mtusi.messenger.main.infrastructure.controllers.chat.requests + +import kotlinx.serialization.Serializable + +@Serializable +data class CreateChatRequest( + val title: String, + val memberIds: List, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt new file mode 100644 index 00000000..51465719 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt @@ -0,0 +1,41 @@ +package com.mtusi.messenger.main.infrastructure.controllers.health + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers.health.responses.HealthResponse +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.suspendedTransaction +import io.ktor.http.HttpStatusCode +import io.ktor.server.application.log +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.route + +fun Routing.configureHealthRouting() = route("/health") { + + get("live") { + call.respond(HttpStatusCode.OK, HealthResponse()) + } + + // PostgreSQL is the only dependency of the synchronous path: without it nothing can be served. + get("ready") { + + val isDatabaseReachable = try { + suspendedTransaction { + exec("SELECT 1") + } + true + } catch (cause: Exception) { + call.application.log.warn("Readiness check failed: ${cause.message}") + false + } + + if (!isDatabaseReachable) { + call.respondError(ErrorCode.UNAVAILABLE, "database unavailable") + return@get + } + + call.respond(HttpStatusCode.OK, HealthResponse()) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/responses/HealthResponse.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/responses/HealthResponse.kt new file mode 100644 index 00000000..a6b56097 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/responses/HealthResponse.kt @@ -0,0 +1,6 @@ +package com.mtusi.messenger.main.infrastructure.controllers.health.responses + +import kotlinx.serialization.Serializable + +@Serializable +data class HealthResponse(val status: String = "ok") diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt new file mode 100644 index 00000000..d1b02be5 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt @@ -0,0 +1,88 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers._common.parameters.pageParameters +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.models.message.MAX_MESSAGE_TEXT_LENGTH +import com.mtusi.messenger.main.infrastructure.services.message.MessageService +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.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.requireNonBlankBounded +import com.mtusi.messenger.main.utils.requireUuid +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +fun Routing.configureMessageRouting() = route("/api/v1/chats/{chat_id}/messages") { + + val messageService by inject() + + authenticate(SESSION_AUTH) { + + post { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + val body = call.receive() + + requireNonBlankBounded( + body.text, + min = 1, + max = MAX_MESSAGE_TEXT_LENGTH, + field = "text" + ) + + val result = suspendedTransaction { + messageService.send( + chatId = chatId, + senderId = actor.id, + text = body.text + ) + } + + when (result) { + is MessageCreationResult.ChatNotFound -> + call.respondError(ErrorCode.NOT_FOUND, "chat not found") + is MessageCreationResult.Success -> + call.respond(HttpStatusCode.Created, result.message) + } + } + + get { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + val (limit, cursor) = call.pageParameters() + + val result = suspendedTransaction { + messageService.findPageByChatId( + chatId = chatId, + actorId = actor.id, + limit = limit, + cursor = cursor + ) + } + + when (result) { + is MessagePageResult.ChatNotFound -> + call.respondError(ErrorCode.NOT_FOUND, "chat not found") + is MessagePageResult.InvalidCursor -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid cursor") + is MessagePageResult.Success -> + call.respond(HttpStatusCode.OK, result.page) + } + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt new file mode 100644 index 00000000..8a9a3c39 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message.requests + +import kotlinx.serialization.Serializable + +@Serializable +data class CreateMessageRequest( + val text: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt new file mode 100644 index 00000000..d49bc9a9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt @@ -0,0 +1,83 @@ +package com.mtusi.messenger.main.infrastructure.controllers.user + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest +import com.mtusi.messenger.main.infrastructure.models.user.MAX_DISPLAY_NAME_LENGTH +import com.mtusi.messenger.main.infrastructure.services.user.UserService +import com.mtusi.messenger.main.infrastructure.services.user.results.UserCreationResult +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.requireNonBlankBounded +import com.mtusi.messenger.main.utils.requireUsername +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +private const val PASSWORD_MIN = 12 +private const val PASSWORD_MAX = 128 + +fun Routing.configureUserRouting() = route("/api/v1/users") { + + val userService by inject() + + post { + + val body = call.receive() + + requireUsername(body.username) + + requireNonBlankBounded( + body.displayName, + min = 1, + max = MAX_DISPLAY_NAME_LENGTH, + field = "display_name" + ) + + requireNonBlankBounded( + body.password, + min = PASSWORD_MIN, + max = PASSWORD_MAX, + field = "password" + ) + + val result = suspendedTransaction { + userService.create( + username = body.username, + displayName = body.displayName, + password = body.password + ) + } + + when (result) { + is UserCreationResult.UsernameTaken -> + call.respondError(ErrorCode.CONFLICT, "username already taken") + is UserCreationResult.Success -> + call.respond(HttpStatusCode.Created, result.user) + } + } + + authenticate(SESSION_AUTH) { + get("me") { + + val user = suspendedTransaction { + userService.findById(call.currentUser.id) + } + + if (user == null) { + call.respondError(ErrorCode.UNAUTHORIZED, "unknown user") + return@get + } + + call.respond(HttpStatusCode.OK, user) + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/requests/CreateUserRequest.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/requests/CreateUserRequest.kt new file mode 100644 index 00000000..0e7bb113 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/requests/CreateUserRequest.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.infrastructure.controllers.user.requests + +import kotlinx.serialization.Serializable + +@Serializable +data class CreateUserRequest( + val username: String, + val displayName: String, + val password: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Chat.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Chat.kt new file mode 100644 index 00000000..846d6c82 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Chat.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class Chat( + val id: SerialUUID, + val title: String, + val memberIds: List, + val createdAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt new file mode 100644 index 00000000..15e30304 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class Message( + val id: SerialUUID, + val chatId: SerialUUID, + val senderId: SerialUUID, + val text: String, + val createdAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/PageResponse.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/PageResponse.kt new file mode 100644 index 00000000..28fa6dce --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/PageResponse.kt @@ -0,0 +1,9 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import kotlinx.serialization.Serializable + +@Serializable +data class PageResponse( + val items: List, + val nextCursor: String?, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/User.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/User.kt new file mode 100644 index 00000000..4f768822 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/User.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class User( + val id: SerialUUID, + val username: String, + val displayName: String, + val createdAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/IssuedSession.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/IssuedSession.kt new file mode 100644 index 00000000..480607bf --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/IssuedSession.kt @@ -0,0 +1,6 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +data class IssuedSession( + val session: Session, + val token: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/Session.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/Session.kt new file mode 100644 index 00000000..a56336f7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/Session.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +import java.util.UUID +import kotlin.time.Instant + +data class Session( + val id: UUID, + val userId: UUID, + val tokenHash: String, + val createdAt: Instant, + val expiresAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/SessionRevocation.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/SessionRevocation.kt new file mode 100644 index 00000000..a02cf062 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/SessionRevocation.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +import kotlin.time.Instant + +data class SessionRevocation( + val tokenHash: String, + val expiresAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/UserCredentials.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/UserCredentials.kt new file mode 100644 index 00000000..2533ff71 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/UserCredentials.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +import com.mtusi.messenger.main.infrastructure.dto.User + +data class UserCredentials( + val user: User, + val passwordHash: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionModel.kt new file mode 100644 index 00000000..6b847e59 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionModel.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.models.authentication + +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +object SessionModel : UUIDTable("sessions") { + val userId = reference("user_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "sessions_user_id_fkey") + val tokenHash = varchar("token_hash", 64).uniqueIndex("sessions_token_hash_key") + val createdAt = timestamp("created_at") + val expiresAt = timestamp("expires_at") + + init { + index("sessions_user_id_idx", false, userId) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionRevocationModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionRevocationModel.kt new file mode 100644 index 00000000..13e791dc --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionRevocationModel.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.infrastructure.models.authentication + +import org.jetbrains.exposed.v1.core.Table +import org.jetbrains.exposed.v1.datetime.timestamp + +object SessionRevocationModel : Table("session_revocations") { + val tokenHash = varchar("token_hash", 64) + val expiresAt = timestamp("expires_at") + + override val primaryKey = PrimaryKey(tokenHash, name = "session_revocations_pkey") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatMemberModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatMemberModel.kt new file mode 100644 index 00000000..cd48d84d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatMemberModel.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.models.chat + +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.Table + +object ChatMemberModel : Table("chat_members") { + val chatId = reference("chat_id", ChatModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "chat_members_chat_id_fkey") + val userId = reference("user_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "chat_members_user_id_fkey") + val position = integer("position") + + override val primaryKey = PrimaryKey(chatId, userId) + + init { + index("chat_members_user_id_idx", false, userId) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt new file mode 100644 index 00000000..984fbfd8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.infrastructure.models.chat + +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_CHAT_TITLE_LENGTH = 100 +const val MAX_CHAT_MEMBERS = 100 + +object ChatModel : UUIDTable("chats") { + val title = varchar("title", MAX_CHAT_TITLE_LENGTH) + val createdAt = timestamp("created_at") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageModel.kt new file mode 100644 index 00000000..301c59f6 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageModel.kt @@ -0,0 +1,20 @@ +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.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_MESSAGE_TEXT_LENGTH = 4000 + +object MessageModel : UUIDTable("messages") { + val chatId = reference("chat_id", ChatModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "messages_chat_id_fkey") + val senderId = reference("sender_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "messages_sender_id_fkey") + val text = varchar("text", MAX_MESSAGE_TEXT_LENGTH) + val createdAt = timestamp("created_at") + + init { + index("messages_chat_id_created_at_id_idx", false, chatId, createdAt, id) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/user/UserModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/user/UserModel.kt new file mode 100644 index 00000000..8dfa1664 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/user/UserModel.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.infrastructure.models.user + +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_USERNAME_LENGTH = 32 +const val MAX_DISPLAY_NAME_LENGTH = 100 + +object UserModel : UUIDTable("users") { + val username = varchar("username", MAX_USERNAME_LENGTH).uniqueIndex("users_username_key") + val displayName = varchar("display_name", MAX_DISPLAY_NAME_LENGTH) + val passwordHash = varchar("password_hash", 255) + val createdAt = timestamp("created_at") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt new file mode 100644 index 00000000..b5d76471 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.repositories + +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module + +@Module +@ComponentScan +class RepositoriesModule diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/Repository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/Repository.kt new file mode 100644 index 00000000..7d841e3e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/Repository.kt @@ -0,0 +1,3 @@ +package com.mtusi.messenger.main.infrastructure.repositories + +interface Repository diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/_common/RedisConnection.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/_common/RedisConnection.kt new file mode 100644 index 00000000..03d38375 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/_common/RedisConnection.kt @@ -0,0 +1,88 @@ +package com.mtusi.messenger.main.infrastructure.repositories._common + +import com.mtusi.messenger.main.config.redis.RedisConfig +import com.mtusi.messenger.main.utils.resolveSecret +import io.lettuce.core.ClientOptions +import io.lettuce.core.RedisClient +import io.lettuce.core.RedisConnectionException +import io.lettuce.core.RedisURI +import io.lettuce.core.SocketOptions +import io.lettuce.core.TimeoutOptions +import io.lettuce.core.api.StatefulRedisConnection +import io.lettuce.core.api.async.RedisAsyncCommands +import io.lettuce.core.codec.StringCodec +import kotlinx.coroutines.future.await +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import org.koin.core.annotation.Single +import java.time.Duration +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +private val RECONNECT_BACKOFF = 1.seconds + +/** + * Lazily connected Redis client. The application must start and serve requests without Redis, so the connection + * is opened on first use, a failed attempt is not retried for [RECONNECT_BACKOFF], and commands issued while + * disconnected fail immediately instead of queueing behind the reconnect. + */ +@Single +class RedisConnection( + config: RedisConfig +) : AutoCloseable { + + private val uri = RedisURI.builder() + .withHost(config.host) + .withPort(config.port) + .withTimeout(Duration.ofMillis(config.commandTimeoutMs)) + .apply { + val password = resolveSecret(config.password, config.passwordFile) + if (password != null) { + if (config.username != null) withAuthentication(config.username, password) else withPassword(password.toCharArray()) + } + } + .build() + + private val client = RedisClient.create().apply { + options = ClientOptions.builder() + .autoReconnect(true) + .disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS) + .timeoutOptions(TimeoutOptions.enabled(Duration.ofMillis(config.commandTimeoutMs))) + .socketOptions(SocketOptions.builder().connectTimeout(Duration.ofMillis(config.connectTimeoutMs)).build()) + .build() + } + + private val mutex = Mutex() + + @Volatile + private var connection: StatefulRedisConnection? = null + + @Volatile + private var nextAttempt = TimeSource.Monotonic.markNow() + + suspend fun commands(): RedisAsyncCommands { + return (connection ?: connect()).async() + } + + private suspend fun connect(): StatefulRedisConnection = mutex.withLock { + + connection?.let { return it } + + if (nextAttempt.hasNotPassedNow()) { + throw RedisConnectionException("Redis is unavailable, next connection attempt is postponed") + } + + return try { + client.connectAsync(StringCodec.UTF8, uri).await().also { connection = it } + } catch (e: Exception) { + nextAttempt = TimeSource.Monotonic.markNow() + RECONNECT_BACKOFF + throw e as? RedisConnectionException ?: RedisConnectionException("Unable to connect to Redis", e) + } + } + + override fun close() { + connection?.close() + client.shutdown() + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt new file mode 100644 index 00000000..080b14e3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt @@ -0,0 +1,73 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.models.authentication.SessionModel +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.greater +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock +import kotlin.time.Duration +import kotlin.time.Instant + +@Single +class ExposedSessionRepository : SessionRepository { + + override fun create( + userId: UUID, + tokenHash: String, + ttl: Duration + ): Session { + + val createdAt = Clock.System.nowMicros() + val expiresAt = createdAt + ttl + + val insertedRow = SessionModel.insert { + it[this.userId] = userId + it[this.tokenHash] = tokenHash + it[this.createdAt] = createdAt + it[this.expiresAt] = expiresAt + } + + return Session( + id = insertedRow[SessionModel.id].value, + userId = userId, + tokenHash = tokenHash, + createdAt = createdAt, + expiresAt = expiresAt + ) + } + + override fun findActiveByTokenHash( + tokenHash: String, + now: Instant + ): Session? { + return SessionModel + .selectAll() + .where((SessionModel.tokenHash eq tokenHash) and (SessionModel.expiresAt greater now)) + .firstOrNull() + ?.toSession() + } + + override fun deleteById( + id: UUID + ): Boolean { + val deletedRows = SessionModel.deleteWhere { SessionModel.id eq id } + return deletedRows > 0 + } + + private fun ResultRow.toSession() = Session( + id = this[SessionModel.id].value, + userId = this[SessionModel.userId].value, + tokenHash = this[SessionModel.tokenHash], + createdAt = this[SessionModel.createdAt], + expiresAt = this[SessionModel.expiresAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/SessionRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/SessionRepository.kt new file mode 100644 index 00000000..e693c339 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/SessionRepository.kt @@ -0,0 +1,26 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import java.util.UUID +import kotlin.time.Duration +import kotlin.time.Instant + +interface SessionRepository : Repository { + + fun create( + userId: UUID, + tokenHash: String, + ttl: Duration + ): Session + + fun findActiveByTokenHash( + tokenHash: String, + now: Instant + ): Session? + + fun deleteById( + id: UUID + ): Boolean + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/CachedSession.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/CachedSession.kt new file mode 100644 index 00000000..86be49d5 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/CachedSession.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session + +sealed class CachedSession { + + data class Active(val session: Session) : CachedSession() + + data object Revoked : CachedSession() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepository.kt new file mode 100644 index 00000000..5cd22fc3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepository.kt @@ -0,0 +1,87 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import io.lettuce.core.SetArgs +import kotlinx.coroutines.future.await +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json +import org.koin.core.annotation.Single +import kotlin.time.Instant + +private const val KEY_PREFIX = "session:" +private const val REVOKED = "revoked" + +@Single +class RedisSessionCacheRepository( + private val redis: RedisConnection +) : SessionCacheRepository { + + override suspend fun find( + tokenHash: String + ): CachedSession? { + + val value = redis.commands().get(key(tokenHash)).await() ?: return null + + if (value == REVOKED) { + return CachedSession.Revoked + } + + return CachedSession.Active( + Json.decodeFromString(value).toSession(tokenHash) + ) + } + + override suspend fun putIfAbsent( + session: Session, + cachedUntil: Instant + ) { + redis.commands().set( + key(session.tokenHash), + Json.encodeToString(SessionEntry.from(session)), + SetArgs.Builder.nx().pxAt(cachedUntil.toEpochMilliseconds()) + ).await() + } + + override suspend fun revoke( + tokenHash: String, + expiresAt: Instant + ) { + redis.commands().set( + key(tokenHash), + REVOKED, + SetArgs.Builder.pxAt(expiresAt.toEpochMilliseconds()) + ).await() + } + + private fun key(tokenHash: String) = KEY_PREFIX + tokenHash + + @Serializable + private data class SessionEntry( + val id: SerialUUID, + val userId: SerialUUID, + val createdAt: Instant, + val expiresAt: Instant + ) { + + fun toSession(tokenHash: String) = Session( + id = id, + userId = userId, + tokenHash = tokenHash, + createdAt = createdAt, + expiresAt = expiresAt + ) + + companion object { + fun from(session: Session) = SessionEntry( + id = session.id, + userId = session.userId, + createdAt = session.createdAt, + expiresAt = session.expiresAt + ) + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt new file mode 100644 index 00000000..c244cc95 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt @@ -0,0 +1,34 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +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 { + + 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 + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/ExposedSessionRevocationRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/ExposedSessionRevocationRepository.kt new file mode 100644 index 00000000..9ca4227a --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/ExposedSessionRevocationRepository.kt @@ -0,0 +1,52 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation + +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +import com.mtusi.messenger.main.infrastructure.models.authentication.SessionRevocationModel +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.greater +import org.jetbrains.exposed.v1.core.lessEq +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import kotlin.time.Instant + +@Single +class ExposedSessionRevocationRepository : SessionRevocationRepository { + + override fun add( + revocation: SessionRevocation + ) { + SessionRevocationModel.insertIgnore { + it[tokenHash] = revocation.tokenHash + it[expiresAt] = revocation.expiresAt + } + } + + override fun findPending( + now: Instant + ): List { + return SessionRevocationModel + .selectAll() + .where(SessionRevocationModel.expiresAt greater now) + .map { + SessionRevocation( + tokenHash = it[SessionRevocationModel.tokenHash], + expiresAt = it[SessionRevocationModel.expiresAt] + ) + } + } + + override fun delete( + tokenHash: String + ) { + SessionRevocationModel.deleteWhere { SessionRevocationModel.tokenHash eq tokenHash } + } + + override fun deleteExpired( + now: Instant + ) { + SessionRevocationModel.deleteWhere { SessionRevocationModel.expiresAt lessEq now } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt new file mode 100644 index 00000000..8612e876 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt @@ -0,0 +1,31 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation + +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +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 { + + 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 + + fun delete( + tokenHash: String + ) + + fun deleteExpired( + now: Instant + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt new file mode 100644 index 00000000..956e9dfc --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt @@ -0,0 +1,33 @@ +package com.mtusi.messenger.main.infrastructure.repositories.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import com.mtusi.messenger.main.utils.PageKey +import java.util.UUID + +interface ChatRepository : Repository { + + fun create( + title: String, + memberIds: List + ): Chat + + fun findById( + id: UUID + ): Chat? + + fun isMember( + chatId: UUID, + userId: UUID + ): Boolean + + /** + * Chats of [userId] ordered by `(created_at, id)`, strictly after [after]. + */ + fun findPageByMemberId( + userId: UUID, + after: PageKey?, + limit: Int + ): List + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt new file mode 100644 index 00000000..2afe352d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt @@ -0,0 +1,134 @@ +package com.mtusi.messenger.main.infrastructure.repositories.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.models.chat.ChatMemberModel +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.exposed.operations.RowGreater +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.QueryParameter +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.SortOrder +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.dao.id.EntityID +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.jdbc.batchInsert +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock + +@Single +class ExposedChatRepository : ChatRepository { + + override fun create( + title: String, + memberIds: List + ): Chat { + + val insertedRow = ChatModel.insert { + it[this.title] = title + it[this.createdAt] = Clock.System.nowMicros() + } + + val chatId = insertedRow[ChatModel.id].value + + ChatMemberModel.batchInsert(memberIds.withIndex()) { (position, userId) -> + this[ChatMemberModel.chatId] = chatId + this[ChatMemberModel.userId] = userId + this[ChatMemberModel.position] = position + } + + return Chat( + id = chatId, + title = title, + memberIds = memberIds, + createdAt = insertedRow[ChatModel.createdAt] + ) + } + + override fun findById( + id: UUID + ): Chat? { + + val row = ChatModel + .selectAll() + .where(ChatModel.id eq id) + .firstOrNull() + ?: return null + + return row.toChat(findMemberIds(listOf(id))[id].orEmpty()) + } + + override fun isMember( + chatId: UUID, + userId: UUID + ): Boolean { + return ChatMemberModel + .selectAll() + .where((ChatMemberModel.chatId eq chatId) and (ChatMemberModel.userId eq userId)) + .empty() + .not() + } + + override fun findPageByMemberId( + userId: UUID, + after: PageKey?, + limit: Int + ): List { + + var condition = ChatMemberModel.userId eq userId + + if (after != null) { + condition = condition and RowGreater( + left = listOf(ChatModel.createdAt, ChatModel.id), + right = listOf( + QueryParameter(after.createdAt, ChatModel.createdAt.columnType), + QueryParameter(EntityID(after.id, ChatModel), ChatModel.id.columnType) + ) + ) + } + + val rows = ChatModel + .innerJoin(ChatMemberModel) + .selectAll() + .where(condition) + .orderBy(ChatModel.createdAt to SortOrder.ASC, ChatModel.id to SortOrder.ASC) + .limit(limit) + .toList() + + val memberIds = findMemberIds(rows.map { it[ChatModel.id].value }) + + return rows.map { + it.toChat(memberIds[it[ChatModel.id].value].orEmpty()) + } + } + + private fun findMemberIds( + chatIds: List + ): Map> { + + if (chatIds.isEmpty()) { + return emptyMap() + } + + return ChatMemberModel + .selectAll() + .where(ChatMemberModel.chatId inList chatIds) + .orderBy(ChatMemberModel.position to SortOrder.ASC) + .groupBy( + keySelector = { it[ChatMemberModel.chatId].value }, + valueTransform = { it[ChatMemberModel.userId].value } + ) + } + + private fun ResultRow.toChat(memberIds: List) = Chat( + id = this[ChatModel.id].value, + title = this[ChatModel.title], + memberIds = memberIds, + createdAt = this[ChatModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt new file mode 100644 index 00000000..19dc19c4 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt @@ -0,0 +1,81 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message + +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.models.message.MessageModel +import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.exposed.operations.RowGreater +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.QueryParameter +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.SortOrder +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.dao.id.EntityID +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock + +@Single +class ExposedMessageRepository : MessageRepository { + + override fun create( + chatId: UUID, + senderId: UUID, + text: String + ): Message { + + val insertedRow = MessageModel.insert { + it[this.chatId] = chatId + it[this.senderId] = senderId + it[this.text] = text + it[this.createdAt] = Clock.System.nowMicros() + } + + return Message( + id = insertedRow[MessageModel.id].value, + chatId = chatId, + senderId = senderId, + text = text, + createdAt = insertedRow[MessageModel.createdAt] + ) + } + + override fun findPageByChatId( + chatId: UUID, + after: PageKey?, + limit: Int + ): List { + + var condition = MessageModel.chatId eq chatId + + if (after != null) { + condition = condition and RowGreater( + left = listOf(MessageModel.createdAt, MessageModel.id), + right = listOf( + QueryParameter(after.createdAt, MessageModel.createdAt.columnType), + QueryParameter(EntityID(after.id, MessageModel), MessageModel.id.columnType) + ) + ) + } + + return MessageModel + .selectAll() + .where(condition) + .orderBy(MessageModel.createdAt to SortOrder.ASC, MessageModel.id to SortOrder.ASC) + .limit(limit) + .map { + it.toMessage() + } + } + + private fun ResultRow.toMessage() = Message( + id = this[MessageModel.id].value, + chatId = this[MessageModel.chatId].value, + senderId = this[MessageModel.senderId].value, + text = this[MessageModel.text], + createdAt = this[MessageModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt new file mode 100644 index 00000000..80a130d2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt @@ -0,0 +1,22 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message + +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import com.mtusi.messenger.main.utils.PageKey +import java.util.UUID + +interface MessageRepository : Repository { + + fun create( + chatId: UUID, + senderId: UUID, + text: String + ): Message + + fun findPageByChatId( + chatId: UUID, + after: PageKey?, + limit: Int + ): List + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt new file mode 100644 index 00000000..404a333f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt @@ -0,0 +1,82 @@ +package com.mtusi.messenger.main.infrastructure.repositories.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.UserCredentials +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock + +@Single +class ExposedUserRepository : UserRepository { + + override fun create( + username: String, + displayName: String, + passwordHash: String + ): User? { + + val statement = UserModel.insertIgnore { + it[this.username] = username + it[this.displayName] = displayName + it[this.passwordHash] = passwordHash + it[this.createdAt] = Clock.System.nowMicros() + } + + if (statement.insertedCount == 0) { + return null + } + + return statement.resultedValues + ?.singleOrNull() + ?.toUser() + } + + override fun findById( + id: UUID + ): User? { + return UserModel + .selectAll() + .where(UserModel.id eq id) + .firstOrNull() + ?.toUser() + } + + override fun findCredentialsByUsername( + username: String + ): UserCredentials? { + return UserModel + .selectAll() + .where(UserModel.username eq username) + .firstOrNull() + ?.let { + UserCredentials( + user = it.toUser(), + passwordHash = it[UserModel.passwordHash] + ) + } + } + + override fun countByIds( + ids: Collection + ): Long { + return UserModel + .selectAll() + .where(UserModel.id inList ids) + .count() + } + + private fun ResultRow.toUser() = User( + id = this[UserModel.id].value, + username = this[UserModel.username], + displayName = this[UserModel.displayName], + createdAt = this[UserModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/UserRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/UserRepository.kt new file mode 100644 index 00000000..0773d071 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/UserRepository.kt @@ -0,0 +1,28 @@ +package com.mtusi.messenger.main.infrastructure.repositories.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.UserCredentials +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import java.util.UUID + +interface UserRepository : Repository { + + fun create( + username: String, + displayName: String, + passwordHash: String + ): User? + + fun findById( + id: UUID + ): User? + + fun findCredentialsByUsername( + username: String + ): UserCredentials? + + fun countByIds( + ids: Collection + ): Long + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt new file mode 100644 index 00000000..d7d75fc9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.services + +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module + +@Module +@ComponentScan +class ServicesModule diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt new file mode 100644 index 00000000..dd86fe0d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt @@ -0,0 +1,34 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.IssuedSession +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import java.util.UUID + +interface AuthenticationService { + + fun validateCredentials( + username: String, + password: String + ): User? + + fun createSession( + 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 + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt new file mode 100644 index 00000000..9bfc3df9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt @@ -0,0 +1,311 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.config.authentication.AuthenticationConfig +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.IssuedSession +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.SessionRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.CachedSession +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.SessionCacheRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation.SessionRevocationRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.HashingService +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.utils.computeSha256 +import io.lettuce.core.RedisException +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.withContext +import org.koin.core.annotation.Single +import org.slf4j.LoggerFactory +import java.io.IOException +import java.security.SecureRandom +import java.util.Base64 +import java.util.UUID +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicLong +import kotlin.time.Clock +import kotlin.time.Instant +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +private const val TOKEN_BYTES = 32 + +private val CACHE_RESTORE_BACKOFF = 1.seconds + +@Single +class DefaultAuthenticationService( + private val userRepository: UserRepository, + private val sessionRepository: SessionRepository, + private val sessionCacheRepository: SessionCacheRepository, + private val sessionRevocationRepository: SessionRevocationRepository, + private val hashingService: HashingService, + private val config: AuthenticationConfig +) : AuthenticationService { + + private val logger = LoggerFactory.getLogger(DefaultAuthenticationService::class.java) + + 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>() + + /** + * 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 + private var trustedAtFailures = -1L + + private val cacheRestoreMutex = Mutex() + + @Volatile + private var nextCacheRestoreAttempt = TimeSource.Monotonic.markNow() + + private val dummyPasswordHash by lazy { + hashingService.computeHash(HashingAlgorithm.Argon2id, "dummy-password-for-timing") + } + + override fun validateCredentials( + username: String, + password: String + ): User? { + + val credentials = userRepository.findCredentialsByUsername(username) + + val isPasswordValid = hashingService.validateHash( + algorithm = HashingAlgorithm.Argon2id, + input = password, + hashed = credentials?.passwordHash ?: dummyPasswordHash + ) + + if (credentials == null || !isPasswordValid) { + return null + } + + return credentials.user + } + + override fun createSession( + userId: UUID + ): IssuedSession { + + val token = ByteArray(TOKEN_BYTES) + .also(random::nextBytes) + .let(Base64.getUrlEncoder().withoutPadding()::encodeToString) + + val session = sessionRepository.create( + userId = userId, + tokenHash = token.toByteArray().computeSha256(), + ttl = config.sessionTtlSeconds.seconds + ) + + return IssuedSession( + session = session, + token = token + ) + } + + override suspend fun findSessionByToken( + token: String + ): Session? { + + val tokenHash = token.toByteArray().computeSha256() + val now = Clock.System.now() + + when (val cached = findCachedSession(tokenHash)) { + is CachedSession.Revoked -> return null + is CachedSession.Active -> return cached.session.takeIf { it.expiresAt > now } + null -> Unit + } + + if (!config.sessionCacheSingleFlight) { + return selectSession(tokenHash, now)?.also { cacheSession(it, now) } + } + + val flight = CompletableDeferred() + + inFlightLookups.putIfAbsent(tokenHash, flight)?.let { leader -> + return leader.await() + } + + try { + // The query is shared with the followers, so the leader's own cancellation must not abort it. + val session = withContext(NonCancellable) { + selectSession(tokenHash, now) + } + + flight.complete(session) + + // Requests arriving until the cache write lands reuse this result instead of starting a new lookup. + // A logout drops the flight first (see revokeSession), so a revoked session is never handed out here. + session?.let { cacheSession(it, now) } + + return session + } catch (e: Throwable) { + flight.completeExceptionally(e) + throw e + } finally { + inFlightLookups.remove(tokenHash, flight) + } + } + + private suspend fun selectSession( + tokenHash: String, + now: Instant + ): Session? { + return suspendedTransaction { + sessionRepository.findActiveByTokenHash(tokenHash, now) + } + } + + override suspend fun revokeSession( + session: Session + ): Boolean { + + // The revocation is kept in PostgreSQL until its tombstone is in Redis: if Redis is unavailable now, + // the tombstone is written when the cache is trusted again (see restoreCache), even after a restart. + val isDeleted = suspendedTransaction { + sessionRevocationRepository.add(SessionRevocation(session.tokenHash, session.expiresAt)) + sessionRepository.deleteById(session.id) + } + + // A lookup that started before the delete must not be joined by requests arriving after the logout. + inFlightLookups.remove(session.tokenHash) + + if (withCache { sessionCacheRepository.revoke(session.tokenHash, session.expiresAt) } != null) { + // Best effort: a leftover row only costs one more tombstone write on the next restore. + runCatchingDatabase { sessionRevocationRepository.delete(session.tokenHash) } + } + + return isDeleted + } + + private suspend fun findCachedSession( + tokenHash: String + ): CachedSession? { + if (trustedAtFailures != cacheFailures.get() && !restoreCache()) { + return null + } + 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()) { + return false + } + + try { + val failures = cacheFailures.get() + + if (trustedAtFailures == failures) { + return true + } + if (nextCacheRestoreAttempt.hasNotPassedNow()) { + return false + } + nextCacheRestoreAttempt = TimeSource.Monotonic.markNow() + CACHE_RESTORE_BACKOFF + + val now = Clock.System.now() + + val pending = runCatchingDatabase { + sessionRevocationRepository.deleteExpired(now) + sessionRevocationRepository.findPending(now) + } ?: return false + + for (revocation in pending) { + withCache { sessionCacheRepository.revoke(revocation.tokenHash, revocation.expiresAt) } ?: return false + } + + if (pending.isNotEmpty()) { + runCatchingDatabase { pending.forEach { sessionRevocationRepository.delete(it.tokenHash) } } + logger.info("Wrote ${pending.size} pending session revocations to the cache") + } + + // A failure while restoring changed the counter, so the cache stays untrusted. + trustedAtFailures = failures + return trustedAtFailures == cacheFailures.get() + } finally { + cacheRestoreMutex.unlock() + } + } + + private suspend fun runCatchingDatabase( + statement: () -> T + ): T? { + return try { + suspendedTransaction { statement() } + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + logger.warn("Unable to update pending session revocations: ${e.message}") + null + } + } + + private suspend fun cacheSession( + session: Session, + now: Instant + ) { + withCache { + sessionCacheRepository.putIfAbsent( + session = session, + cachedUntil = minOf(session.expiresAt, now + config.sessionCacheTtlSeconds.seconds) + ) + } + } + + /** + * 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 withCache( + operation: suspend () -> T + ): T? { + return try { + operation().also { + if (isCacheAvailable.compareAndSet(false, true)) { + logger.info("Session cache is available again") + } + } + } catch (e: RedisException) { + onCacheUnavailable(e) + } catch (e: IOException) { + onCacheUnavailable(e) + } + } + + private fun onCacheUnavailable( + cause: Exception + ): Nothing? { + cacheFailures.incrementAndGet() + if (isCacheAvailable.compareAndSet(true, false)) { + logger.warn("Session cache is unavailable, sessions are read from the database: ${cause.message}") + } + return null + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/ChatService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/ChatService.kt new file mode 100644 index 00000000..603abc9f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/ChatService.kt @@ -0,0 +1,27 @@ +package com.mtusi.messenger.main.infrastructure.services.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatCreationResult +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatPageResult +import java.util.UUID + +interface ChatService { + + fun create( + creatorId: UUID, + title: String, + memberIds: List + ): ChatCreationResult + + fun findAccessible( + userId: UUID, + chatId: UUID + ): Chat? + + fun findPageAccessible( + userId: UUID, + limit: Int, + cursor: String? + ): ChatPageResult + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt new file mode 100644 index 00000000..b38620c5 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt @@ -0,0 +1,79 @@ +package com.mtusi.messenger.main.infrastructure.services.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatCreationResult +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatPageResult +import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.decodeCursor +import com.mtusi.messenger.main.utils.toPage +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class DefaultChatService( + private val chatRepository: ChatRepository, + private val userRepository: UserRepository +) : ChatService { + + override fun create( + creatorId: UUID, + title: String, + memberIds: List + ): ChatCreationResult { + + if (memberIds.toSet().size != memberIds.size) { + return ChatCreationResult.DuplicateMembers + } + + if (creatorId !in memberIds) { + return ChatCreationResult.CreatorNotMember + } + + if (userRepository.countByIds(memberIds) != memberIds.size.toLong()) { + return ChatCreationResult.UnknownMembers + } + + val chat = chatRepository.create( + title = title, + memberIds = memberIds + ) + + return ChatCreationResult.Success(chat) + } + + override fun findAccessible( + userId: UUID, + chatId: UUID + ): Chat? { + return chatRepository.findById(chatId) + ?.takeIf { userId in it.memberIds } + } + + override fun findPageAccessible( + userId: UUID, + limit: Int, + cursor: String? + ): ChatPageResult { + + val scope = "chats:$userId" + + val after = cursor?.let { + decodeCursor(it, scope) ?: return ChatPageResult.InvalidCursor + } + + val page = chatRepository + .findPageByMemberId( + userId = userId, + after = after, + limit = limit + 1 + ) + .toPage(limit, scope) { + PageKey(it.createdAt, it.id) + } + + return ChatPageResult.Success(page) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatCreationResult.kt new file mode 100644 index 00000000..61c35511 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatCreationResult.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.services.chat.results + +import com.mtusi.messenger.main.infrastructure.dto.Chat + +sealed class ChatCreationResult { + + sealed class Error : ChatCreationResult() + + data object DuplicateMembers : Error() + data object CreatorNotMember : Error() + data object UnknownMembers : Error() + + data class Success( + val chat: Chat + ) : ChatCreationResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatPageResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatPageResult.kt new file mode 100644 index 00000000..d347bad6 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatPageResult.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.infrastructure.services.chat.results + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.PageResponse + +sealed class ChatPageResult { + + sealed class Error : ChatPageResult() + + data object InvalidCursor : Error() + + data class Success( + val page: PageResponse + ) : ChatPageResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingService.kt new file mode 100644 index 00000000..d18110ac --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingService.kt @@ -0,0 +1,123 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing + +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import org.bouncycastle.crypto.generators.Argon2BytesGenerator +import org.bouncycastle.crypto.params.Argon2Parameters +import org.koin.core.annotation.Single +import java.security.MessageDigest +import java.security.SecureRandom +import java.util.Base64 + +// TODO: Move to config later +private const val ARGON2_MEMORY_KIB = 19 * 1024 +private const val ARGON2_ITERATIONS = 2 +private const val ARGON2_PARALLELISM = 1 +private const val ARGON2_SALT_LENGTH = 16 +private const val ARGON2_HASH_LENGTH = 32 + +@Single +class DefaultHashingService : HashingService { + + private val random = SecureRandom() + private val encoder = Base64.getEncoder().withoutPadding() + private val decoder = Base64.getDecoder() + + override fun computeHash( + algorithm: HashingAlgorithm, + input: String + ): String { + return when (algorithm) { + HashingAlgorithm.Argon2id -> computeHashArgon2id(input) + } + } + + private fun computeHashArgon2id(input: String): String { + + val salt = ByteArray(ARGON2_SALT_LENGTH).also(random::nextBytes) + + val hash = argon2id( + input = input, + salt = salt, + memoryKib = ARGON2_MEMORY_KIB, + iterations = ARGON2_ITERATIONS, + parallelism = ARGON2_PARALLELISM, + length = ARGON2_HASH_LENGTH + ) + + return "\$argon2id\$v=19\$m=$ARGON2_MEMORY_KIB,t=$ARGON2_ITERATIONS,p=$ARGON2_PARALLELISM" + + "\$${encoder.encodeToString(salt)}\$${encoder.encodeToString(hash)}" + } + + override fun validateHash( + algorithm: HashingAlgorithm, + input: String, + hashed: String + ): Boolean { + return when (algorithm) { + HashingAlgorithm.Argon2id -> validateHashArgon2id(input, hashed) + } + } + + private fun validateHashArgon2id( + input: String, + hashed: String + ): Boolean { + return try { + + val parts = hashed.split('$') + if (parts.size != 6 || parts[1] != "argon2id" || parts[2] != "v=19") { + return false + } + + val parameters = parts[3] + .split(',') + .associate { + val (key, value) = it.split('=') + key to value.toInt() + } + + val salt = decoder.decode(parts[4]) + val expected = decoder.decode(parts[5]) + + val actual = argon2id( + input = input, + salt = salt, + memoryKib = parameters.getValue("m"), + iterations = parameters.getValue("t"), + parallelism = parameters.getValue("p"), + length = expected.size + ) + + MessageDigest.isEqual(expected, actual) + } catch (_: Exception) { + false + } + } + + private fun argon2id( + input: String, + salt: ByteArray, + memoryKib: Int, + iterations: Int, + parallelism: Int, + length: Int + ): ByteArray { + + val parameters = Argon2Parameters.Builder(Argon2Parameters.ARGON2_id) + .withVersion(Argon2Parameters.ARGON2_VERSION_13) + .withMemoryAsKB(memoryKib) + .withIterations(iterations) + .withParallelism(parallelism) + .withSalt(salt) + .build() + + val generator = Argon2BytesGenerator().apply { + init(parameters) + } + + return ByteArray(length).also { + generator.generateBytes(input.toByteArray(Charsets.UTF_8), it) + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/HashingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/HashingService.kt new file mode 100644 index 00000000..0de59beb --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/HashingService.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing + +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm + +interface HashingService { + + fun computeHash( + algorithm: HashingAlgorithm, + input: String + ): String + + fun validateHash( + algorithm: HashingAlgorithm, + input: String, + hashed: String + ): Boolean + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/types/HashingAlgorithm.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/types/HashingAlgorithm.kt new file mode 100644 index 00000000..8f39402b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/types/HashingAlgorithm.kt @@ -0,0 +1,5 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing.types + +sealed class HashingAlgorithm { + data object Argon2id : HashingAlgorithm() +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt new file mode 100644 index 00000000..800af6fe --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt @@ -0,0 +1,68 @@ +package com.mtusi.messenger.main.infrastructure.services.message + +import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository +import com.mtusi.messenger.main.infrastructure.repositories.message.MessageRepository +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.decodeCursor +import com.mtusi.messenger.main.utils.toPage +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class DefaultMessageService( + private val chatRepository: ChatRepository, + private val messageRepository: MessageRepository +) : MessageService { + + override fun send( + chatId: UUID, + senderId: UUID, + text: String + ): MessageCreationResult { + + if (!chatRepository.isMember(chatId, senderId)) { + return MessageCreationResult.ChatNotFound + } + + val message = messageRepository.create( + chatId = chatId, + senderId = senderId, + text = text + ) + + return MessageCreationResult.Success(message) + } + + override fun findPageByChatId( + chatId: UUID, + actorId: UUID, + limit: Int, + cursor: String? + ): MessagePageResult { + + if (!chatRepository.isMember(chatId, actorId)) { + return MessagePageResult.ChatNotFound + } + + val scope = "messages:$chatId" + + val after = cursor?.let { + decodeCursor(it, scope) ?: return MessagePageResult.InvalidCursor + } + + val page = messageRepository + .findPageByChatId( + chatId = chatId, + after = after, + limit = limit + 1 + ) + .toPage(limit, scope) { + PageKey(it.createdAt, it.id) + } + + return MessagePageResult.Success(page) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt new file mode 100644 index 00000000..075c35d7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt @@ -0,0 +1,22 @@ +package com.mtusi.messenger.main.infrastructure.services.message + +import com.mtusi.messenger.main.infrastructure.services.message.results.MessageCreationResult +import com.mtusi.messenger.main.infrastructure.services.message.results.MessagePageResult +import java.util.UUID + +interface MessageService { + + fun send( + chatId: UUID, + senderId: UUID, + text: String + ): MessageCreationResult + + fun findPageByChatId( + chatId: UUID, + actorId: UUID, + limit: Int, + cursor: String? + ): MessagePageResult + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt new file mode 100644 index 00000000..d5ac9756 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.infrastructure.services.message.results + +import com.mtusi.messenger.main.infrastructure.dto.Message + +sealed class MessageCreationResult { + + sealed class Error : MessageCreationResult() + + data object ChatNotFound : Error() + + data class Success( + val message: Message + ) : MessageCreationResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessagePageResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessagePageResult.kt new file mode 100644 index 00000000..254b1d52 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessagePageResult.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.services.message.results + +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.dto.PageResponse + +sealed class MessagePageResult { + + sealed class Error : MessagePageResult() + + data object ChatNotFound : Error() + data object InvalidCursor : Error() + + data class Success( + val page: PageResponse + ) : MessagePageResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/DefaultUserService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/DefaultUserService.kt new file mode 100644 index 00000000..618ca129 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/DefaultUserService.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.infrastructure.services.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.HashingService +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import com.mtusi.messenger.main.infrastructure.services.user.results.UserCreationResult +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class DefaultUserService( + private val userRepository: UserRepository, + private val hashingService: HashingService +) : UserService { + + override fun create( + username: String, + displayName: String, + password: String + ): UserCreationResult { + + val passwordHash = hashingService.computeHash( + algorithm = HashingAlgorithm.Argon2id, + input = password + ) + + val user = userRepository.create( + username = username, + displayName = displayName, + passwordHash = passwordHash + ) ?: return UserCreationResult.UsernameTaken + + return UserCreationResult.Success(user) + } + + override fun findById( + id: UUID + ): User? { + return userRepository.findById(id) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/UserService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/UserService.kt new file mode 100644 index 00000000..da9528ed --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/UserService.kt @@ -0,0 +1,19 @@ +package com.mtusi.messenger.main.infrastructure.services.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.services.user.results.UserCreationResult +import java.util.UUID + +interface UserService { + + fun create( + username: String, + displayName: String, + password: String + ): UserCreationResult + + fun findById( + id: UUID + ): User? + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/results/UserCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/results/UserCreationResult.kt new file mode 100644 index 00000000..ae51bf5d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/results/UserCreationResult.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.infrastructure.services.user.results + +import com.mtusi.messenger.main.infrastructure.dto.User + +sealed class UserCreationResult { + + sealed class Error : UserCreationResult() + + data object UsernameTaken : Error() + + data class Success( + val user: User + ) : UserCreationResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ApiException.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ApiException.kt new file mode 100644 index 00000000..90021402 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ApiException.kt @@ -0,0 +1,3 @@ +package com.mtusi.messenger.main.infrastructure.types + +class ApiException(val code: ErrorCode, message: String) : RuntimeException(message) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/AuthChallenge.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/AuthChallenge.kt new file mode 100644 index 00000000..d8f23b56 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/AuthChallenge.kt @@ -0,0 +1,6 @@ +package com.mtusi.messenger.main.infrastructure.types + +object AuthChallenge { + const val BASIC = "Basic realm=\"messenger\", charset=\"UTF-8\"" + const val BEARER = "Bearer realm=\"messenger\"" +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ErrorCode.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ErrorCode.kt new file mode 100644 index 00000000..d71aef95 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ErrorCode.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.infrastructure.types + +import io.ktor.http.HttpStatusCode + +enum class ErrorCode(val wire: String, val status: HttpStatusCode) { + INVALID_REQUEST("invalid_request", HttpStatusCode.BadRequest), + UNAUTHORIZED("unauthorized", HttpStatusCode.Unauthorized), + NOT_FOUND("not_found", HttpStatusCode.NotFound), + CONFLICT("conflict", HttpStatusCode.Conflict), + PAYLOAD_TOO_LARGE("payload_too_large", HttpStatusCode.PayloadTooLarge), + UNSUPPORTED_MEDIA_TYPE("unsupported_media_type", HttpStatusCode.UnsupportedMediaType), + RATE_LIMITED("rate_limited", HttpStatusCode.TooManyRequests), + UNAVAILABLE("unavailable", HttpStatusCode.ServiceUnavailable), + INTERNAL_ERROR("internal_error", HttpStatusCode.InternalServerError), +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/CallLogging.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/CallLogging.kt new file mode 100644 index 00000000..c9148e6b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/CallLogging.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.calllogging.CallLogging +import org.slf4j.event.Level + +fun Application.configureCallLogging() = install(CallLogging) { + level = Level.INFO + filter { call -> + val uri = call.request.local.uri + uri.startsWith("/api") || uri.startsWith("/health") + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/ContentNegotiation.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/ContentNegotiation.kt new file mode 100644 index 00000000..beb9fffb --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/ContentNegotiation.kt @@ -0,0 +1,21 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy + +@OptIn(ExperimentalSerializationApi::class) +val json = Json { + ignoreUnknownKeys = false + explicitNulls = true + encodeDefaults = true + namingStrategy = JsonNamingStrategy.SnakeCase +} + +fun Application.configureContentNegotiation() = install(ContentNegotiation) { + json(json) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt new file mode 100644 index 00000000..32001ed3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt @@ -0,0 +1,149 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.config.database.DatabaseConfig +import com.mtusi.messenger.main.config.database.SerialDatabaseConfig +import com.mtusi.messenger.main.infrastructure.models.authentication.SessionModel +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.MessageModel +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import com.mtusi.messenger.main.utils.resolveSecret +import com.typesafe.config.ConfigFactory +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopped +import io.ktor.server.config.HoconApplicationConfig +import io.ktor.server.config.getAs +import kotlinx.coroutines.Dispatchers +import org.flywaydb.core.Flyway +import org.jetbrains.exposed.v1.core.ExperimentalDatabaseMigrationApi +import org.jetbrains.exposed.v1.core.Table +import org.jetbrains.exposed.v1.core.DatabaseConfig as ExposedDatabaseConfig +import org.jetbrains.exposed.v1.jdbc.Database +import org.jetbrains.exposed.v1.jdbc.JdbcTransaction +import org.jetbrains.exposed.v1.jdbc.transactions.experimental.newSuspendedTransaction +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import org.jetbrains.exposed.v1.migration.jdbc.MigrationUtils +import org.koin.ktor.ext.inject +import java.io.File +import javax.sql.DataSource + +private const val MIGRATIONS_DIRECTORY = "src/main/resources/migrations" +private const val DEVELOPMENT_DATABASE_CONFIG = "run/development/config/database.conf" +private const val VALIDATION_TIMEOUT_MS = 1000L + +fun Application.configureDatabase() { + + val config by inject() + + val dataSource = config.createDataSource() + + Database.connect( + datasource = dataSource, + databaseConfig = ExposedDatabaseConfig { + defaultMaxAttempts = 1 + } + ) + applyDatabaseMigrations(dataSource) + + monitor.subscribe(ApplicationStopped) { + dataSource.close() + } + +} + +private fun DatabaseConfig.createDataSource(): HikariDataSource { + return HikariDataSource( + HikariConfig().apply { + poolName = "messenger" + jdbcUrl = jdbcConnectionUrl + username = this@createDataSource.username + password = resolveSecret(this@createDataSource.password, passwordFile).orEmpty() + maximumPoolSize = maxPoolSize + connectionTimeout = connectionTimeoutMs + validationTimeout = minOf(VALIDATION_TIMEOUT_MS, connectionTimeoutMs - 1) + addDataSourceProperty("options", "-c statement_timeout=$statementTimeoutMs") + } + ) +} + +fun applyDatabaseMigrations( + dataSource: DataSource +) { + + val flyway = Flyway.configure() + .dataSource(dataSource) + .locations("classpath:migrations") + .load() + + flyway.migrate() + flyway.validate() + +} + +fun main(args: Array) { + generateMigration(args.joinToString("_")) +} + +@OptIn(ExperimentalDatabaseMigrationApi::class) +private fun generateMigration(description: String) { + require(description.isNotEmpty()) { "Description cannot be empty" } + + val tables: Array = arrayOf( + UserModel, + ChatModel, + ChatMemberModel, + MessageModel, + SessionModel, + SessionRevocationModel + ) + + val config = HoconApplicationConfig(ConfigFactory.parseFile(File(DEVELOPMENT_DATABASE_CONFIG)).resolve()) + .property("database") + .getAs() + + config.createDataSource().use { dataSource -> + + Database.connect(dataSource) + applyDatabaseMigrations(dataSource) + + val migrations = File(MIGRATIONS_DIRECTORY).listFiles { it.extension == "sql" }.orEmpty() + + val latestVersion = migrations.maxOfOrNull { + it.name + .substringBefore("__") + .drop(1) + .toInt() + } + + val version = latestVersion?.inc() ?: 1 + + transaction { + + val statements = MigrationUtils.statementsRequiredForDatabaseMigration(*tables) + + if (statements.isEmpty()) { + println("Database schema is up to date, no migration generated") + return@transaction + } + + val script = MigrationUtils.generateMigrationScript( + tables = tables, + scriptDirectory = MIGRATIONS_DIRECTORY, + scriptName = "V${version}__$description" + ) + + println("Generated migration ${script.path}") + + } + + } + +} + +suspend fun suspendedTransaction(statement: JdbcTransaction.() -> R) = newSuspendedTransaction( + context = Dispatchers.IO, + statement = statement +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/DefaultHeaders.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/DefaultHeaders.kt new file mode 100644 index 00000000..e7be1a56 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/DefaultHeaders.kt @@ -0,0 +1,7 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.defaultheaders.DefaultHeaders + +fun Application.configureDefaultHeaders() = install(DefaultHeaders) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt new file mode 100644 index 00000000..6702609f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt @@ -0,0 +1,27 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.config.ConfigModule +import com.mtusi.messenger.main.infrastructure.repositories.RepositoriesModule +import com.mtusi.messenger.main.infrastructure.services.ServicesModule +import io.ktor.server.application.Application +import io.ktor.server.application.install +import org.koin.dsl.module +import org.koin.ksp.generated.module +import org.koin.ktor.plugin.Koin + +fun Application.configureKoin() { + + val applicationModule = module { + single { this@configureKoin } + } + + install(Koin) { + modules( + applicationModule, + ConfigModule().module, + RepositoriesModule().module, + ServicesModule().module + ) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Redis.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Redis.kt new file mode 100644 index 00000000..d0aaea25 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Redis.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopped +import org.koin.ktor.ext.get + +fun Application.configureRedis() { + + val redisConnection = get() + + monitor.subscribe(ApplicationStopped) { + redisConnection.close() + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/RequestId.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/RequestId.kt new file mode 100644 index 00000000..e22ef712 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/RequestId.kt @@ -0,0 +1,20 @@ +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.util.AttributeKey +import java.util.UUID + +val RequestIdKey: AttributeKey = AttributeKey("X-Request-Id") + +private val RequestIdPlugin = createApplicationPlugin("RequestId") { + onCall { call -> + val incoming = call.request.headers["X-Request-Id"]?.takeIf { it.isNotBlank() } + val id = incoming ?: UUID.randomUUID().toString() + call.attributes.put(RequestIdKey, id) + call.response.headers.append("X-Request-Id", id, safeOnly = false) + } +} + +fun Application.configureRequestId() = install(RequestIdPlugin) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt new file mode 100644 index 00000000..5329f729 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.controllers.authentication.configureAuthenticationRouting +import com.mtusi.messenger.main.infrastructure.controllers.chat.configureChatRouting +import com.mtusi.messenger.main.infrastructure.controllers.health.configureHealthRouting +import com.mtusi.messenger.main.infrastructure.controllers.message.configureMessageRouting +import com.mtusi.messenger.main.infrastructure.controllers.user.configureUserRouting +import io.ktor.server.application.Application +import io.ktor.server.routing.routing + +fun Application.configureRouting() = routing { + configureSwaggerRouting() + configureHealthRouting() + configureUserRouting() + configureAuthenticationRouting() + configureChatRouting() + configureMessageRouting() +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/StatusPages.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/StatusPages.kt new file mode 100644 index 00000000..3b39bc99 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/StatusPages.kt @@ -0,0 +1,78 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.http.HttpStatusCode +import io.ktor.serialization.JsonConvertException +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.application.log +import io.ktor.server.plugins.BadRequestException +import io.ktor.server.plugins.NotFoundException +import io.ktor.server.plugins.UnsupportedMediaTypeException +import io.ktor.server.plugins.statuspages.StatusPages +import io.lettuce.core.RedisException +import kotlinx.serialization.SerializationException +import java.sql.SQLException +import java.sql.SQLTransientConnectionException + +private const val CONNECTION_ERROR_SQL_STATE_CLASS = "08" + +// admin_shutdown, crash_shutdown, cannot_connect_now: the server is going down or still starting. +private const val SERVER_SHUTDOWN_SQL_STATE_PREFIX = "57P" + +fun Application.configureStatusPages() = install(StatusPages) { + exception { call, cause -> + call.respondError(cause.code, cause.message ?: cause.code.wire) + } + exception { call, _ -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid JSON body") + } + exception { call, _ -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid JSON body") + } + exception { call, _ -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid request") + } + exception { call, _ -> + call.respondError(ErrorCode.NOT_FOUND, "not found") + } + exception { call, _ -> + call.respondError(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "unsupported media type") + } + exception { call, cause -> + if (cause.isConnectionFailure()) { + call.application.log.warn("Database unavailable: ${cause.message}") + call.respondError(ErrorCode.UNAVAILABLE, "service temporarily unavailable") + } else { + call.application.log.error("Database error", cause) + call.respondError(ErrorCode.INTERNAL_ERROR, "internal error") + } + } + exception { call, cause -> + call.application.log.warn("Redis unavailable: ${cause.message}") + call.respondError(ErrorCode.UNAVAILABLE, "service temporarily unavailable") + } + exception { call, cause -> + call.application.log.error("Unhandled error", cause) + call.respondError(ErrorCode.INTERNAL_ERROR, "internal error") + } + status(HttpStatusCode.NotFound) { call, _ -> + call.respondError(ErrorCode.NOT_FOUND, "not found") + } + status(HttpStatusCode.MethodNotAllowed) { call, _ -> + call.respondError(ErrorCode.NOT_FOUND, "not found") + } +} + +private fun SQLException.isConnectionFailure(): Boolean { + return generateSequence(this) { it.cause }.any { + it is SQLTransientConnectionException || + (it is SQLException && it.sqlState.isUnavailableState()) + } +} + +private fun String?.isUnavailableState(): Boolean { + return this != null && (startsWith(CONNECTION_ERROR_SQL_STATE_CLASS) || startsWith(SERVER_SHUTDOWN_SQL_STATE_PREFIX)) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Swagger.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Swagger.kt new file mode 100644 index 00000000..8f1d4f61 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Swagger.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.plugins.swagger.swaggerUI +import io.ktor.server.routing.Routing + +fun Routing.configureSwaggerRouting() { + swaggerUI(path = "docs", swaggerFile = "openapi.json") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/Authentication.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/Authentication.kt new file mode 100644 index 00000000..378524eb --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/Authentication.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.plugins.authentication + +import com.mtusi.messenger.main.infrastructure.services.authentication.AuthenticationService +import com.mtusi.messenger.main.plugins.authentication.providers.basicCredentials +import com.mtusi.messenger.main.plugins.authentication.providers.bearerSession +import com.mtusi.messenger.main.plugins.suspendedTransaction +import io.ktor.server.application.Application +import io.ktor.server.auth.authentication +import org.koin.ktor.ext.inject + +const val CREDENTIALS_AUTH = "credentials" +const val SESSION_AUTH = "session" + +fun Application.configureAuthentication() { + + val authenticationService by inject() + + authentication { + + basicCredentials(CREDENTIALS_AUTH) { + validate { credential -> + val user = suspendedTransaction { + authenticationService.validateCredentials( + username = credential.name, + password = credential.password + ) + } ?: return@validate null + + return@validate CredentialsPrincipal(user) + } + } + + bearerSession(SESSION_AUTH) { + authenticate { token -> + val session = authenticationService.findSessionByToken(token) ?: return@authenticate null + + return@authenticate UserPrincipal(session) + } + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/CredentialsPrincipal.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/CredentialsPrincipal.kt new file mode 100644 index 00000000..9a5684a3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/CredentialsPrincipal.kt @@ -0,0 +1,7 @@ +package com.mtusi.messenger.main.plugins.authentication + +import com.mtusi.messenger.main.infrastructure.dto.User + +data class CredentialsPrincipal( + val user: User +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/UserPrincipal.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/UserPrincipal.kt new file mode 100644 index 00000000..4ac42fa8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/UserPrincipal.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.plugins.authentication + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import java.util.UUID + +data class UserPrincipal( + val session: Session +) { + + val id: UUID + get() = session.userId + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BasicCredentialsProvider.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BasicCredentialsProvider.kt new file mode 100644 index 00000000..f8ab4179 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BasicCredentialsProvider.kt @@ -0,0 +1,66 @@ +package com.mtusi.messenger.main.plugins.authentication.providers + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.types.AuthChallenge +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.server.application.ApplicationCall +import io.ktor.server.auth.AuthenticationConfig +import io.ktor.server.auth.AuthenticationContext +import io.ktor.server.auth.AuthenticationFailedCause +import io.ktor.server.auth.AuthenticationProvider +import io.ktor.server.auth.UserPasswordCredential +import io.ktor.server.auth.basicAuthenticationCredentials +import io.ktor.server.plugins.BadRequestException + +private const val CHALLENGE_KEY = "BasicCredentials" + +class BasicCredentialsProvider(config: Config) : AuthenticationProvider(config) { + + private val validate = config.validate + + override suspend fun onAuthenticate(context: AuthenticationContext) { + + val credential = try { + context.call.request.basicAuthenticationCredentials(Charsets.UTF_8) + } catch (_: BadRequestException) { + null + } + + val principal = credential?.let { validate(context.call, it) } + + if (principal == null) { + + val cause = if (credential == null) { + AuthenticationFailedCause.NoCredentials + } else { + AuthenticationFailedCause.InvalidCredentials + } + + context.challenge(CHALLENGE_KEY, cause) { challenge, call -> + call.respondError(ErrorCode.UNAUTHORIZED, "invalid credentials", AuthChallenge.BASIC) + challenge.complete() + } + return + } + + context.principal(principal) + } + + class Config(name: String?) : AuthenticationProvider.Config(name) { + + internal var validate: suspend ApplicationCall.(UserPasswordCredential) -> Any? = { null } + + fun validate(body: suspend ApplicationCall.(UserPasswordCredential) -> Any?) { + validate = body + } + + } + +} + +fun AuthenticationConfig.basicCredentials( + name: String, + configure: BasicCredentialsProvider.Config.() -> Unit +) { + register(BasicCredentialsProvider(BasicCredentialsProvider.Config(name).apply(configure))) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BearerSessionProvider.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BearerSessionProvider.kt new file mode 100644 index 00000000..9d3ad728 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BearerSessionProvider.kt @@ -0,0 +1,71 @@ +package com.mtusi.messenger.main.plugins.authentication.providers + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.types.AuthChallenge +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.http.auth.HttpAuthHeader +import io.ktor.server.application.ApplicationCall +import io.ktor.server.auth.AuthenticationConfig +import io.ktor.server.auth.AuthenticationContext +import io.ktor.server.auth.AuthenticationFailedCause +import io.ktor.server.auth.AuthenticationProvider +import io.ktor.server.auth.parseAuthorizationHeader +import io.ktor.server.plugins.BadRequestException + +private const val CHALLENGE_KEY = "BearerSession" +private const val BEARER_SCHEME = "Bearer" + +class BearerSessionProvider(config: Config) : AuthenticationProvider(config) { + + private val authenticate = config.authenticate + + override suspend fun onAuthenticate(context: AuthenticationContext) { + + val header = try { + context.call.request.parseAuthorizationHeader() + } catch (_: BadRequestException) { + null + } + + val token = (header as? HttpAuthHeader.Single) + ?.takeIf { it.authScheme.equals(BEARER_SCHEME, ignoreCase = true) } + ?.blob + + val principal = token?.let { authenticate(context.call, it) } + + if (principal == null) { + + val cause = if (token == null) { + AuthenticationFailedCause.NoCredentials + } else { + AuthenticationFailedCause.InvalidCredentials + } + + context.challenge(CHALLENGE_KEY, cause) { challenge, call -> + call.respondError(ErrorCode.UNAUTHORIZED, "authentication required", AuthChallenge.BEARER) + challenge.complete() + } + return + } + + context.principal(principal) + } + + class Config(name: String?) : AuthenticationProvider.Config(name) { + + internal var authenticate: suspend ApplicationCall.(String) -> Any? = { null } + + fun authenticate(body: suspend ApplicationCall.(String) -> Any?) { + authenticate = body + } + + } + +} + +fun AuthenticationConfig.bearerSession( + name: String, + configure: BearerSessionProvider.Config.() -> Unit +) { + register(BearerSessionProvider(BearerSessionProvider.Config(name).apply(configure))) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Hashing.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Hashing.kt new file mode 100644 index 00000000..824b9059 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Hashing.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.utils + +import java.security.MessageDigest + +@OptIn(ExperimentalStdlibApi::class) +fun ByteArray.computeSha256(): String { + return MessageDigest.getInstance("SHA-256") + .digest(this) + .toHexString() +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt new file mode 100644 index 00000000..cd84ad28 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt @@ -0,0 +1,64 @@ +package com.mtusi.messenger.main.utils + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json +import java.util.Base64 +import java.util.UUID +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, +) + +@Serializable +private data class Cursor( + val scope: String, + val createdAt: Instant, + val id: SerialUUID, +) + +fun encodeCursor(scope: String, key: PageKey): String { + val raw = json.encodeToString(Cursor.serializer(), Cursor(scope, key.createdAt, key.id)) + 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) + json.decodeFromString(Cursor.serializer(), String(bytes, Charsets.UTF_8)) + } catch (_: Exception) { + return null + } + + if (cursor.scope != expectedScope) { + return null + } + + 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 List.toPage( + limit: Int, + scope: String, + key: (T) -> PageKey, +): PageResponse { + + val items = take(limit) + val nextCursor = if (size > limit) encodeCursor(scope, key(items.last())) else null + + return PageResponse(items, nextCursor) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt new file mode 100644 index 00000000..e0b30312 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt @@ -0,0 +1,13 @@ +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? +): String? { + return value ?: file?.let { File(it).readText().trim() } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt new file mode 100644 index 00000000..7c8c8d43 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.utils + +import kotlin.time.Clock +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( + epochSeconds = now.epochSeconds, + nanosecondAdjustment = now.nanosecondsOfSecond / NANOS_PER_MICRO * NANOS_PER_MICRO + ) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt new file mode 100644 index 00000000..395e8fb6 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt @@ -0,0 +1,36 @@ +package com.mtusi.messenger.main.utils + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import java.util.UUID + +private val USERNAME = Regex("^[a-z0-9_]{3,32}$") + +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}$" +) + +fun codePoints(value: String): Int = value.codePointCount(0, value.length) + +fun requireUsername(value: String) { + if (!USERNAME.matches(value)) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid username") + } +} + +fun requireNonBlankBounded(value: String, min: Int, max: Int, field: String) { + if (value.isBlank()) { + throw ApiException(ErrorCode.INVALID_REQUEST, "$field must not be blank") + } + val n = codePoints(value) + if (n !in min..max) { + throw ApiException(ErrorCode.INVALID_REQUEST, "$field length out of range") + } +} + +fun requireUuid(value: String?, field: String): UUID { + if (value == null || !UUID_REGEX.matches(value)) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid $field") + } + return UUID.fromString(value) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt new file mode 100644 index 00000000..190d93fa --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt @@ -0,0 +1,32 @@ +package com.mtusi.messenger.main.utils.exposed.operations + +import org.jetbrains.exposed.v1.core.Expression +import org.jetbrains.exposed.v1.core.Op +import org.jetbrains.exposed.v1.core.QueryBuilder + +class RowGreater( + private val left: List>, + private val right: List> +) : Op() { + + init { + require(left.size == right.size) { "Rows must have the same arity" } + } + + override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { + appendRow(left) + append(" > ") + appendRow(right) + } + + private fun QueryBuilder.appendRow(expressions: List>) { + append("(") + expressions.forEachIndexed { index, expression -> + if (index > 0) { + append(", ") + } + append(expression) + } + append(")") + } +} diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf new file mode 100644 index 00000000..0f2d4033 --- /dev/null +++ b/src/main/resources/application.conf @@ -0,0 +1,52 @@ +ktor { + application { + modules = [com.mtusi.messenger.main.AppKt.module] + } + + deployment { + host = "0.0.0.0" + host = ${?APP_HOST} + port = 8080 + port = ${?APP_PORT} + shutdownGracePeriod = 3000 + shutdownTimeout = 10000 + } +} + +database { + host = "localhost" + host = ${?DB_HOST} + port = 5432 + port = ${?DB_PORT} + name = "messenger" + name = ${?DB_NAME} + username = "messenger" + username = ${?DB_USER} + password = ${?DB_PASSWORD} + passwordFile = ${?DB_PASSWORD_FILE} + maxPoolSize = 10 + maxPoolSize = ${?DB_MAX_POOL_SIZE} + connectionTimeoutMs = 2000 + statementTimeoutMs = 5000 +} + +redis { + host = "localhost" + host = ${?REDIS_HOST} + port = 6379 + port = ${?REDIS_PORT} + password = ${?REDIS_PASSWORD} + passwordFile = ${?REDIS_PASSWORD_FILE} + connectTimeoutMs = 500 + commandTimeoutMs = 500 +} + +authentication { + realm = "messenger" + sessionTtlSeconds = 3600 + sessionTtlSeconds = ${?SESSION_TTL_SECONDS} + sessionCacheTtlSeconds = 60 + # Collapse concurrent cache misses for one token into a single database lookup (track 1 compares on/off). + sessionCacheSingleFlight = true + sessionCacheSingleFlight = ${?SESSION_CACHE_SINGLE_FLIGHT} +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 00000000..5416c44d --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,12 @@ + + + + %d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %-5level [%thread] %logger{36} - %msg%n + + + + + + + + diff --git a/src/main/resources/migrations/V1__init.sql b/src/main/resources/migrations/V1__init.sql new file mode 100644 index 00000000..b580eefd --- /dev/null +++ b/src/main/resources/migrations/V1__init.sql @@ -0,0 +1,50 @@ +-- Users, chats, membership, messages and sessions. PostgreSQL is the source of truth. +-- Timestamps are timestamptz: an absolute instant, independent of server/JVM time zone. + +CREATE TABLE users ( + id uuid PRIMARY KEY, + username varchar(32) NOT NULL, + display_name varchar(100) NOT NULL, + password_hash varchar(255) NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT users_username_key UNIQUE (username), + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$') +); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title varchar(100) NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + position integer NOT NULL, + PRIMARY KEY (chat_id, user_id) +); + +-- "Chats of a user": the PK leads with chat_id, so the reverse lookup needs its own index. +CREATE INDEX chat_members_user_id_idx ON chat_members (user_id); + +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + sender_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + text varchar(4000) NOT NULL, + created_at timestamptz NOT NULL +); + +-- Keyset pagination of a chat history: WHERE chat_id = ? AND (created_at, id) > (?, ?) ORDER BY created_at, id. +CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id); + +CREATE TABLE sessions ( + id uuid PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + token_hash varchar(64) NOT NULL, + created_at timestamptz NOT NULL, + expires_at timestamptz NOT NULL, + CONSTRAINT sessions_token_hash_key UNIQUE (token_hash) +); + +CREATE INDEX sessions_user_id_idx ON sessions (user_id); diff --git a/src/main/resources/migrations/V2__session_revocations.sql b/src/main/resources/migrations/V2__session_revocations.sql new file mode 100644 index 00000000..4b15c00c --- /dev/null +++ b/src/main/resources/migrations/V2__session_revocations.sql @@ -0,0 +1,6 @@ +-- Logouts whose tombstone has not reached Redis yet. Written in the same transaction as the session delete and +-- removed once the tombstone is stored, so a copy cached before an outage cannot outlive the logout. +CREATE TABLE session_revocations ( + token_hash varchar(64) PRIMARY KEY, + expires_at timestamptz NOT NULL +); diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationControllerTest.kt new file mode 100644 index 00000000..67734ad9 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationControllerTest.kt @@ -0,0 +1,151 @@ +package com.mtusi.messenger.main.infrastructure.controllers.authentication + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse +import com.mtusi.messenger.main.support.TEST_PASSWORD +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createUser +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.call.body +import io.ktor.client.request.basicAuth +import io.ktor.client.request.bearerAuth +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import kotlinx.coroutines.delay +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertTrue +import kotlin.time.Clock +import kotlin.time.Duration.Companion.minutes +import kotlin.time.Duration.Companion.seconds + +private const val BASIC_CHALLENGE = "Basic realm=\"messenger\", charset=\"UTF-8\"" + +class AuthenticationControllerTest { + + @Test + fun `valid Basic credentials open a session`() = runApp { client -> + val user = client.createUser("alice") + + val response = client.post("/api/v1/auth/sessions") { + basicAuth(user.username, TEST_PASSWORD) + } + + assertEquals(HttpStatusCode.Created, response.status) + + val session = response.body() + assertEquals("Bearer", session.tokenType) + assertEquals(user, session.user) + assertTrue(session.token.length >= 43, "token must carry at least 256 bits") + assertTrue(session.expiresAt > Clock.System.now() + 59.minutes) + } + + @Test + fun `each login issues a different token`() = runApp { client -> + val user = client.createUser("alice") + + val first = client.post("/api/v1/auth/sessions") { basicAuth(user.username, TEST_PASSWORD) } + val second = client.post("/api/v1/auth/sessions") { basicAuth(user.username, TEST_PASSWORD) } + + assertNotEquals(first.body().token, second.body().token) + } + + @Test + fun `password containing a colon and non-ASCII characters works`() = runApp { client -> + val password = "пароль:с-двоеточием" + val user = client.createUser("colon", password = password) + + val response = client.post("/api/v1/auth/sessions") { + basicAuth(user.username, password) + } + + assertEquals(HttpStatusCode.Created, response.status) + } + + @Test + fun `wrong password and unknown user get the same 401`() = runApp { client -> + val user = client.createUser("alice") + + val wrongPassword = client.post("/api/v1/auth/sessions") { + basicAuth(user.username, "wrong-password-123") + } + val unknownUser = client.post("/api/v1/auth/sessions") { + basicAuth("nobody_${user.username}".take(32), TEST_PASSWORD) + } + + for (response in listOf(wrongPassword, unknownUser)) { + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals(BASIC_CHALLENGE, response.headers[HttpHeaders.WWWAuthenticate]) + assertEquals("unauthorized", response.body().error.code) + } + } + + @Test + fun `missing or malformed Basic header returns 401`() = runApp { client -> + val missing = client.post("/api/v1/auth/sessions") + val malformed = client.post("/api/v1/auth/sessions") { + header(HttpHeaders.Authorization, "Basic !!!not-base64") + } + + for (response in listOf(missing, malformed)) { + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals(BASIC_CHALLENGE, response.headers[HttpHeaders.WWWAuthenticate]) + } + } + + @Test + fun `Bearer token cannot open a session`() = runApp { client -> + val alice = client.register("alice") + + val response = client.post("/api/v1/auth/sessions") { + auth(alice) + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + } + + @Test + fun `logout revokes only the current token`() = runApp { client -> + val alice = client.register("alice") + val secondSession = client.post("/api/v1/auth/sessions") { + basicAuth(alice.user.username, alice.password) + }.body() + + val logout = client.delete("/api/v1/auth/sessions/current") { auth(alice) } + assertEquals(HttpStatusCode.NoContent, logout.status) + + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + + val other = client.get("/api/v1/users/me") { bearerAuth(secondSession.token) } + assertEquals(HttpStatusCode.OK, other.status) + } + + @Test + fun `unknown token returns 401`() = runApp { client -> + val response = client.get("/api/v1/users/me") { + bearerAuth("this-token-was-never-issued-by-the-server-at-all") + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + } + + @Test + fun `session expires after SESSION_TTL_SECONDS`() = runApp(sessionTtlSeconds = 2) { client -> + val alice = client.register("alice") + + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + delay(2.5.seconds) + + val expired = client.get("/api/v1/users/me") { auth(alice) } + assertEquals(HttpStatusCode.Unauthorized, expired.status) + assertEquals("Bearer realm=\"messenger\"", expired.headers[HttpHeaders.WWWAuthenticate]) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatControllerTest.kt new file mode 100644 index 00000000..f58f9053 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatControllerTest.kt @@ -0,0 +1,152 @@ +package com.mtusi.messenger.main.infrastructure.controllers.chat + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +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.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 org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class ChatControllerTest { + + private suspend fun HttpClient.postChat(actor: TestUser, title: String, memberIds: List): HttpResponse = + post("/api/v1/chats") { + auth(actor) + contentType(ContentType.Application.Json) + setBody(CreateChatRequest(title = title, memberIds = memberIds)) + } + + @Test + fun `member can read own chat and members keep their order`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val created = client.createChat(alice, title = "notes", memberIds = listOf(bob.id, alice.id)) + + val response = client.get("/api/v1/chats/${created.id}") { + auth(bob) + } + + assertEquals(HttpStatusCode.OK, response.status) + assertEquals(created, response.body()) + assertEquals(listOf(bob.id, alice.id), created.memberIds) + } + + @Test + fun `foreign chat returns 404 for non-member`() = runApp { client -> + val alice = client.register("alice") + val eve = client.register("eve") + val chat = client.createChat(alice, title = "secret", memberIds = listOf(alice.id)) + + val response = client.get("/api/v1/chats/${chat.id}") { + auth(eve) + } + + assertEquals(HttpStatusCode.NotFound, response.status) + assertEquals("not_found", response.body().error.code) + } + + @Test + fun `invalid member lists are rejected with 400`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + + val cases = listOf( + listOf(bob.id.toString()), // creator is not a member + listOf(alice.id.toString(), alice.id.toString()), // duplicates + listOf(alice.id.toString(), "not-a-uuid"), // malformed id + listOf(alice.id.toString(), UUID.randomUUID().toString()), // unknown user + emptyList(), + ) + + for (memberIds in cases) { + val response = client.postChat(alice, "bad", memberIds) + assertEquals(HttpStatusCode.BadRequest, response.status, "member_ids=$memberIds") + } + } + + @Test + fun `unknown member is rejected before anything is written`() = runApp { client -> + val alice = client.register("alice") + val title = "atomic-${UUID.randomUUID()}" + + val response = client.postChat(alice, title, listOf(alice.id.toString(), UUID.randomUUID().toString())) + assertEquals(HttpStatusCode.BadRequest, response.status) + + val chats = transaction { + ChatModel.selectAll().where(ChatModel.title eq title).count() + } + assertEquals(0, chats) + } + + @Test + fun `chat list contains only own chats and is paginated`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val eve = client.register("eve") + + val created = (0 until 5).map { + client.createChat(alice, title = "c$it", memberIds = listOf(alice.id, bob.id)) + } + client.createChat(eve, title = "eve only", memberIds = listOf(eve.id)) + + val collected = mutableListOf() + var cursor: String? = null + + do { + val page = client.get("/api/v1/chats") { + auth(bob) + url { + parameters.append("limit", "2") + cursor?.let { parameters.append("cursor", it) } + } + }.body>() + + collected += page.items + cursor = page.nextCursor + } while (cursor != null) + + assertEquals(created, collected) + + val evePage = client.get("/api/v1/chats") { auth(eve) }.body>() + assertEquals(listOf("eve only"), evePage.items.map { it.title }) + assertNull(evePage.nextCursor) + } + + @Test + fun `cursor issued to another user is rejected`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + repeat(2) { client.createChat(alice, title = "c$it", memberIds = listOf(alice.id)) } + + val cursor = client.get("/api/v1/chats?limit=1") { auth(alice) } + .body>() + .nextCursor!! + + val response = client.get("/api/v1/chats") { + auth(bob) + url { parameters.append("cursor", cursor) } + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthControllerTest.kt new file mode 100644 index 00000000..66a36f3f --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthControllerTest.kt @@ -0,0 +1,22 @@ +package com.mtusi.messenger.main.infrastructure.controllers.health + +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.request.get +import io.ktor.http.HttpStatusCode +import kotlin.test.Test +import kotlin.test.assertEquals + +class HealthControllerTest { + + @Test + fun `live returns 200`() = runApp { client -> + val response = client.get("/health/live") + assertEquals(HttpStatusCode.OK, response.status) + } + + @Test + fun `ready returns 200 when database is reachable`() = runApp { client -> + val response = client.get("/health/ready") + assertEquals(HttpStatusCode.OK, response.status) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt new file mode 100644 index 00000000..a91cb3d8 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt @@ -0,0 +1,75 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +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.register +import com.mtusi.messenger.main.support.runApp +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.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import kotlin.test.Test +import kotlin.test.assertEquals + +class MessageConcurrencyTest { + + @Test + fun `parallel senders produce exactly N unique messages`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "load", memberIds = listOf(alice.id)) + + val workers = 10 + val perWorker = 10 + val total = workers * perWorker + + coroutineScope { + (0 until workers).map { w -> + async { + repeat(perWorker) { i -> + val response = client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "w$w-i$i")) + } + assertEquals(HttpStatusCode.Created, response.status) + } + } + }.awaitAll() + } + + val collected = mutableListOf() + var cursor: String? = null + + do { + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { + parameters.append("limit", "100") + if (cursor != null) parameters.append("cursor", cursor!!) + } + } + assertEquals(HttpStatusCode.OK, response.status) + + val page = response.body>() + collected += page.items.map { it.text } + cursor = page.nextCursor + } while (cursor != null) + + assertEquals(total, collected.size) + assertEquals(total, collected.toSet().size) + + val expected = buildSet { + for (w in 0 until workers) for (i in 0 until perWorker) add("w$w-i$i") + } + assertEquals(expected, collected.toSet()) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt new file mode 100644 index 00000000..ac72e82c --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt @@ -0,0 +1,191 @@ +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.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.register +import com.mtusi.messenger.main.support.runApp +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.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +class MessageControllerTest { + + @Test + fun `empty text returns 400 invalid_request`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "")) + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + assertEquals("invalid_request", response.body().error.code) + } + + @Test + fun `foreign chat returns 404 when sending`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.post("/api/v1/chats/${chat.id}/messages") { + auth(bob) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "hi")) + } + + assertEquals(HttpStatusCode.NotFound, response.status) + assertEquals("not_found", response.body().error.code) + } + + @Test + fun `foreign chat returns 404 when listing`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(bob) + } + + assertEquals(HttpStatusCode.NotFound, response.status) + } + + @Test + fun `pagination walks all messages exactly once`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val total = 25 + repeat(total) { i -> + val r = client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "m$i")) + } + assertEquals(HttpStatusCode.Created, r.status) + } + + val collected = mutableListOf() + var cursor: String? = null + var pages = 0 + + do { + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { + parameters.append("limit", "10") + if (cursor != null) parameters.append("cursor", cursor!!) + } + } + assertEquals(HttpStatusCode.OK, response.status) + + val page = response.body>() + collected += page.items.map { it.text } + cursor = page.nextCursor + pages++ + } while (cursor != null) + + assertEquals(3, pages) + assertEquals(total, collected.size) + assertEquals(total, collected.toSet().size) + assertEquals((0 until total).map { "m$it" }, collected) + } + + @Test + fun `history that is an exact multiple of limit ends with null next_cursor`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + repeat(20) { i -> + client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "m$i")) + } + } + + val first = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { parameters.append("limit", "10") } + }.body>() + + val cursor = first.nextCursor + assertNotNull(cursor) + + val second = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { + parameters.append("limit", "10") + parameters.append("cursor", cursor) + } + }.body>() + + assertEquals((10 until 20).map { "m$it" }, second.items.map { it.text }) + assertNull(second.nextCursor) + } + + @Test + fun `last page has null next_cursor`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + } + + assertEquals(HttpStatusCode.OK, response.status) + val page = response.body>() + assertEquals(0, page.items.size) + assertNull(page.nextCursor) + } + + @Test + fun `foreign cursor returns 400`() = runApp { client -> + val alice = client.register("alice") + val chatA = client.createChat(alice, title = "a", memberIds = listOf(alice.id)) + val chatB = client.createChat(alice, title = "b", memberIds = listOf(alice.id)) + + repeat(2) { i -> + client.post("/api/v1/chats/${chatA.id}/messages") { + auth(alice) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "a$i")) + } + } + + val firstA = client.get("/api/v1/chats/${chatA.id}/messages") { + auth(alice) + url { parameters.append("limit", "1") } + }.body>() + + val cursor = firstA.nextCursor + assertNotNull(cursor) + + val response = client.get("/api/v1/chats/${chatB.id}/messages") { + auth(alice) + url { + parameters.append("limit", "1") + parameters.append("cursor", cursor) + } + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + assertEquals("invalid_request", response.body().error.code) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserControllerTest.kt new file mode 100644 index 00000000..b2d7dfea --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserControllerTest.kt @@ -0,0 +1,180 @@ +package com.mtusi.messenger.main.infrastructure.controllers.user + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.support.TEST_PASSWORD +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.support.uniqueUsername +import io.ktor.client.call.body +import io.ktor.client.request.basicAuth +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.statement.bodyAsText +import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull + +class UserControllerTest { + + @Test + fun `create user returns 201 without password`() = runApp { client -> + val username = uniqueUsername("alice") + + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = username, displayName = "Alice", password = TEST_PASSWORD)) + } + + assertEquals(HttpStatusCode.Created, response.status) + + val body = response.bodyAsText() + assertFalse("password" in body) + assertFalse(TEST_PASSWORD in body) + assertEquals(username, response.body().username) + } + + @Test + fun `invalid JSON body returns 400 invalid_request`() = runApp { client -> + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody("{not json") + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + + val body = response.body() + assertEquals("invalid_request", body.error.code) + assertNotNull(body.error.requestId) + } + + @Test + fun `missing password returns 400`() = runApp { client -> + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody("""{"username":"${uniqueUsername("nopass")}","display_name":"x"}""") + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + } + + @Test + fun `password shorter than 12 code points returns 400`() = runApp { client -> + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = uniqueUsername("short"), displayName = "x", password = "elevenchars")) + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + } + + @Test + fun `duplicate username returns 409 conflict`() = runApp { client -> + val username = uniqueUsername("bob") + + val request = CreateUserRequest(username = username, displayName = "Bob", password = TEST_PASSWORD) + + val first = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(request) + } + assertEquals(HttpStatusCode.Created, first.status) + + val second = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(request.copy(displayName = "Bob 2")) + } + + assertEquals(HttpStatusCode.Conflict, second.status) + assertEquals("conflict", second.body().error.code) + } + + @Test + fun `concurrent registrations of one username create exactly one user`() = runApp { client -> + val username = uniqueUsername("race") + val attempts = 10 + + val statuses = coroutineScope { + (0 until attempts).map { + async { + client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = username, displayName = "r$it", password = TEST_PASSWORD)) + }.status + } + }.awaitAll() + } + + assertEquals(1, statuses.count { it == HttpStatusCode.Created }) + assertEquals(attempts - 1, statuses.count { it == HttpStatusCode.Conflict }) + } + + @Test + fun `me returns the session owner`() = runApp { client -> + val alice = client.register("alice") + + val response = client.get("/api/v1/users/me") { + auth(alice) + } + + assertEquals(HttpStatusCode.OK, response.status) + assertEquals(alice.user, response.body()) + } + + @Test + fun `missing token returns 401 with Bearer challenge`() = runApp { client -> + val response = client.get("/api/v1/users/me") + + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals("unauthorized", response.body().error.code) + assertEquals("Bearer realm=\"messenger\"", response.headers[HttpHeaders.WWWAuthenticate]) + } + + @Test + fun `X-User-Id alone gives no access`() = runApp { client -> + val alice = client.register("alice") + + val response = client.get("/api/v1/users/me") { + header("X-User-Id", alice.id.toString()) + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + } + + @Test + fun `X-User-Id does not override bearer identity`() = runApp { client -> + val alice = client.register("alice") + val eve = client.register("eve") + + val response = client.get("/api/v1/users/me") { + auth(alice) + header("X-User-Id", eve.id.toString()) + } + + assertEquals(alice.id, response.body().id) + } + + @Test + fun `Basic credentials are rejected on business endpoints`() = runApp { client -> + val alice = client.register("alice") + + val response = client.get("/api/v1/users/me") { + basicAuth(alice.user.username, alice.password) + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals("Bearer realm=\"messenger\"", response.headers[HttpHeaders.WWWAuthenticate]) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepositoryTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepositoryTest.kt new file mode 100644 index 00000000..8f27e66f --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepositoryTest.kt @@ -0,0 +1,83 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +import com.mtusi.messenger.main.config.redis.SerialRedisConfig +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import com.mtusi.messenger.main.support.TestRedis +import com.mtusi.messenger.main.utils.nowMicros +import kotlinx.coroutines.test.runTest +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.time.Clock +import kotlin.time.Duration +import kotlin.time.Duration.Companion.minutes + +class RedisSessionCacheRepositoryTest { + + private fun withRepository(block: suspend (RedisSessionCacheRepository) -> Unit) = runTest { + val endpoint = TestRedis.endpoint() + val connection = RedisConnection( + SerialRedisConfig( + host = endpoint.host, + port = endpoint.port, + password = TestRedis.PASSWORD, + connectTimeoutMs = 2000, + commandTimeoutMs = 2000 + ) + ) + connection.use { block(RedisSessionCacheRepository(it)) } + } + + private fun session(ttl: Duration = 10.minutes): Session { + val now = Clock.System.nowMicros() + return Session( + id = UUID.randomUUID(), + userId = UUID.randomUUID(), + tokenHash = UUID.randomUUID().toString().replace("-", ""), + createdAt = now, + expiresAt = now + ttl + ) + } + + private fun ttlSeconds(session: Session): Long { + return TestRedis.cli(TestRedis.container, "TTL", "session:${session.tokenHash}").toLong() + } + + @Test + fun `cached session round-trips and expires together with the session`() = withRepository { repository -> + val session = session() + + assertNull(repository.find(session.tokenHash)) + + repository.putIfAbsent(session, cachedUntil = session.expiresAt) + + assertEquals(CachedSession.Active(session), repository.find(session.tokenHash)) + assertTrue(ttlSeconds(session) in 590..600, "key must expire at cachedUntil") + } + + @Test + fun `revoke replaces a cached session with a tombstone`() = withRepository { repository -> + val session = session() + repository.putIfAbsent(session, cachedUntil = session.expiresAt) + + repository.revoke(session.tokenHash, session.expiresAt) + + assertEquals(CachedSession.Revoked, repository.find(session.tokenHash)) + assertTrue(ttlSeconds(session) in 590..600, "tombstone must live until the session would expire") + } + + @Test + fun `late cache fill cannot overwrite a tombstone`() = withRepository { repository -> + val session = session() + + // A lookup read the session from PostgreSQL before logout and stores it after the logout finished. + repository.revoke(session.tokenHash, session.expiresAt) + repository.putIfAbsent(session, cachedUntil = session.expiresAt) + + assertEquals(CachedSession.Revoked, repository.find(session.tokenHash)) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt new file mode 100644 index 00000000..e6964807 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt @@ -0,0 +1,62 @@ +package com.mtusi.messenger.main.infrastructure.repositories.chat + +import com.mtusi.messenger.main.infrastructure.models.chat.ChatMemberModel +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.exceptions.ExposedSQLException +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +private const val FOREIGN_KEY_VIOLATION = "23503" + +/** + * The service validates members before writing, so the rollback is exercised here directly: the chat row is + * inserted, the membership batch then fails on the foreign key, and the whole transaction must disappear. + */ +class ExposedChatRepositoryTest { + + private val repository = ExposedChatRepository() + + @Test + fun `chat row is rolled back when inserting members fails`() = runApp { client -> + val alice = client.register("alice") + val title = "rollback-${UUID.randomUUID()}" + + val error = assertFailsWith { + transaction { + repository.create(title = title, memberIds = listOf(alice.id, UUID.randomUUID())) + } + } + assertEquals(FOREIGN_KEY_VIOLATION, error.sqlState) + + val (chats, memberships) = transaction { + val chats = ChatModel.selectAll().where(ChatModel.title eq title).count() + val memberships = ChatMemberModel.selectAll().where(ChatMemberModel.userId eq alice.id).count() + chats to memberships + } + assertEquals(0, chats) + assertEquals(0, memberships) + } + + @Test + fun `successful creation commits the chat and every member`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + + val chat = transaction { + repository.create(title = "committed", memberIds = listOf(alice.id, bob.id)) + } + + val memberships = transaction { + ChatMemberModel.selectAll().where(ChatMemberModel.chatId eq chat.id).count() + } + assertEquals(2, memberships) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt new file mode 100644 index 00000000..01df4eed --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt @@ -0,0 +1,174 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.config.authentication.SerialAuthenticationConfig +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.SessionRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.CachedSession +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.SessionCacheRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation.SessionRevocationRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.ExposedUserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.DefaultHashingService +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.utils.computeSha256 +import com.mtusi.messenger.main.utils.nowMicros +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import org.jetbrains.exposed.v1.jdbc.Database +import java.util.UUID +import java.util.concurrent.atomic.AtomicInteger +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.time.Clock +import kotlin.time.Duration +import kotlin.time.Duration.Companion.hours +import kotlin.time.Instant + +private const val CONCURRENT_REQUESTS = 50 +private const val SLOW_QUERY_MS = 200L + +/** + * Cold cache and a deliberately slow lookup, so every request is guaranteed to overlap with the first one. + */ +class SessionLookupSingleFlightTest { + + private val token = "token-${UUID.randomUUID()}" + private val tokenHash = token.toByteArray().computeSha256() + + private val session = Clock.System.nowMicros().let { + Session( + id = UUID.randomUUID(), + userId = UUID.randomUUID(), + tokenHash = tokenHash, + createdAt = it, + expiresAt = it + 1.hours + ) + } + + private class CountingSessionRepository(private val session: Session) : SessionRepository { + + val lookups = AtomicInteger() + + override fun findActiveByTokenHash(tokenHash: String, now: Instant): Session? { + lookups.incrementAndGet() + Thread.sleep(SLOW_QUERY_MS) + return session.takeIf { it.tokenHash == tokenHash } + } + + override fun create(userId: UUID, tokenHash: String, ttl: Duration) = error("not used") + + override fun deleteById(id: UUID) = true + + } + + private class ColdCache : SessionCacheRepository { + + val writes = AtomicInteger() + + override suspend fun find(tokenHash: String): CachedSession? = null + + override suspend fun putIfAbsent(session: Session, cachedUntil: Instant) { + writes.incrementAndGet() + } + + override suspend fun revoke(tokenHash: String, expiresAt: Instant) = Unit + + } + + private object NoRevocations : SessionRevocationRepository { + + override fun add(revocation: SessionRevocation) = Unit + + override fun findPending(now: Instant) = emptyList() + + override fun delete(tokenHash: String) = Unit + + override fun deleteExpired(now: Instant) = Unit + + } + + @BeforeTest + fun connect() { + // The fakes do not touch the database, but the service still opens a transaction around the lookup. + TestDatabase.container.let { Database.connect(it.jdbcUrl, user = it.username, password = it.password) } + } + + private fun service(singleFlight: Boolean, repository: SessionRepository, cache: SessionCacheRepository) = + DefaultAuthenticationService( + userRepository = ExposedUserRepository(), + sessionRepository = repository, + sessionCacheRepository = cache, + sessionRevocationRepository = NoRevocations, + hashingService = DefaultHashingService(), + config = SerialAuthenticationConfig( + realm = "messenger", + sessionTtlSeconds = 3600, + sessionCacheTtlSeconds = 60, + sessionCacheSingleFlight = singleFlight + ) + ) + + private fun stampede(service: AuthenticationService): List = runBlocking(Dispatchers.Default) { + (0 until CONCURRENT_REQUESTS).map { + async { service.findSessionByToken(token) } + }.awaitAll() + } + + @Test + fun `without single-flight every concurrent miss queries the database`() { + val repository = CountingSessionRepository(session) + val cache = ColdCache() + + val results = stampede(service(singleFlight = false, repository, cache)) + + assertTrue(results.all { it == session }) + assertEquals(CONCURRENT_REQUESTS, repository.lookups.get()) + assertEquals(CONCURRENT_REQUESTS, cache.writes.get()) + } + + @Test + fun `with single-flight concurrent misses share one database lookup`() { + val repository = CountingSessionRepository(session) + val cache = ColdCache() + + val results = stampede(service(singleFlight = true, repository, cache)) + + assertTrue(results.all { it == session }) + assertEquals(1, repository.lookups.get()) + assertEquals(1, cache.writes.get()) + } + + @Test + fun `request arriving after logout does not join a lookup started before it`() = runBlocking(Dispatchers.Default) { + val repository = CountingSessionRepository(session) + val service = service(singleFlight = true, repository, ColdCache()) + + // The leader's query is still running (SLOW_QUERY_MS) when the logout completes. + val leader = async { service.findSessionByToken(token) } + delay(SLOW_QUERY_MS / 4) + service.revokeSession(session) + + val afterLogout = async { service.findSessionByToken(token) } + leader.await() + afterLogout.await() + + assertEquals(2, repository.lookups.get(), "the request after logout must run its own lookup") + } + + @Test + fun `lookup that finished before a request arrived is not reused`() = runBlocking { + val repository = CountingSessionRepository(session) + val service = service(singleFlight = true, repository, ColdCache()) + + service.findSessionByToken(token) + service.findSessionByToken(token) + + assertEquals(2, repository.lookups.get()) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingServiceTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingServiceTest.kt new file mode 100644 index 00000000..384e053c --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingServiceTest.kt @@ -0,0 +1,45 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing + +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertNotEquals +import kotlin.test.assertTrue + +class DefaultHashingServiceTest { + + private val service = DefaultHashingService() + + @Test + fun `hash is a salted Argon2id PHC string`() { + val first = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery") + val second = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery") + + assertTrue(first.startsWith("\$argon2id\$v=19\$m=19456,t=2,p=1\$")) + assertFalse("correct-horse-battery" in first) + assertNotEquals(first, second, "same password must produce different hashes (random salt)") + } + + @Test + fun `validates the right password and rejects a wrong one`() { + val hash = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery") + + assertTrue(service.validateHash(HashingAlgorithm.Argon2id, "correct-horse-battery", hash)) + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "correct-horse-batterY", hash)) + } + + @Test + fun `passwords differing only after 72 bytes are distinguished`() { + // bcrypt silently truncates input to 72 bytes; Argon2id must not. + val prefix = "ж".repeat(40) // 80 bytes in UTF-8 + val hash = service.computeHash(HashingAlgorithm.Argon2id, prefix + "a") + + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, prefix + "b", hash)) + } + + @Test + fun `malformed hash is rejected instead of throwing`() { + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "whatever", "not-a-hash")) + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "whatever", "\$argon2id\$v=19\$m=x\$a\$b")) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DatabaseMigrationTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DatabaseMigrationTest.kt new file mode 100644 index 00000000..93ccd43f --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DatabaseMigrationTest.kt @@ -0,0 +1,49 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.support.createUser +import com.mtusi.messenger.main.support.runApp +import com.zaxxer.hikari.HikariDataSource +import org.flywaydb.core.Flyway +import kotlin.test.Test +import kotlin.test.assertEquals + +class DatabaseMigrationTest { + + @Test + fun `re-running migrations applies nothing and keeps data`() = runApp { client -> + client.createUser("keep") + + HikariDataSource().apply { + jdbcUrl = TestDatabase.container.jdbcUrl + username = TestDatabase.USERNAME + password = TestDatabase.PASSWORD + }.use { dataSource -> + + val usersBefore = countUsers(dataSource) + + applyDatabaseMigrations(dataSource) + applyDatabaseMigrations(dataSource) + + val flyway = Flyway.configure() + .dataSource(dataSource) + .locations("classpath:migrations") + .load() + + assertEquals(0, flyway.info().pending().size) + assertEquals(flyway.info().applied().size, flyway.info().all().size) + assertEquals(usersBefore, countUsers(dataSource)) + } + } + + private fun countUsers(dataSource: HikariDataSource): Long { + return dataSource.connection.use { connection -> + connection.createStatement().use { statement -> + statement.executeQuery("SELECT count(*) FROM users").use { + it.next() + it.getLong(1) + } + } + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt new file mode 100644 index 00000000..2a171457 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt @@ -0,0 +1,217 @@ +package com.mtusi.messenger.main.plugins + +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.support.TestDatabase +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.register +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.utils.computeSha256 +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.basicAuth +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.client.request.header +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 org.testcontainers.containers.GenericContainer +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +class DependencyFailureTest { + + /** + * An unknown token can only be checked against PostgreSQL, so without it the answer is 503, never a success. + */ + private suspend fun HttpClient.assertAnonymousRejected( + unknownTokenStatus: HttpStatusCode = HttpStatusCode.Unauthorized + ) { + assertEquals(HttpStatusCode.Unauthorized, get("/api/v1/users/me").status) + assertEquals(HttpStatusCode.Unauthorized, get("/api/v1/users/me") { header("X-User-Id", "1") }.status) + assertEquals( + unknownTokenStatus, + get("/api/v1/users/me") { header("Authorization", "Bearer never-issued-token") }.status + ) + } + + private suspend fun assertUnavailable(response: HttpResponse) { + assertEquals(HttpStatusCode.ServiceUnavailable, response.status) + assertEquals("unavailable", response.body().error.code) + } + + @Test + fun `without Redis requests bypass the cache`() = runApp( + redis = TestRedis.unreachableEndpoint() + ) { client -> + assertEquals(HttpStatusCode.OK, client.get("/health/ready").status) + + val alice = client.register("alice") + val bob = client.register("bob") + + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + client.createChat(alice, "no redis", listOf(alice.id, bob.id)) + + client.assertAnonymousRejected() + + // PostgreSQL is the source of truth: logout still works and is visible immediately. + assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + } + + @Test + fun `hanging Redis costs one command timeout and recovers`() { + val redis = TestRedis.create().also { it.start() } + val docker = redis.dockerClient + + try { + runApp(redis = TestRedis.endpoint(redis)) { client -> + val alice = client.register("alice") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + docker.pauseContainerCmd(redis.containerId).exec() + + val started = TimeSource.Monotonic.markNow() + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + assertTrue(started.elapsedNow() < 2.seconds, "cache lookup must be bounded by the command timeout") + + client.assertAnonymousRejected() + + assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + + docker.unpauseContainerCmd(redis.containerId).exec() + + // The copy cached before the pause is replaced by the delayed tombstone, not resurrected. + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + + val bob = client.register("bob") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(bob) }.status) + assertTrue( + TestRedis.cli(redis, "EXISTS", "session:${bob.token.toByteArray().computeSha256()}") == "1", + "cache is used again after recovery" + ) + } + } finally { + redis.stop() + } + } + + /** + * The tombstone written by a logout during an outage can be lost while the copy cached before it survives + * (here: put back by hand). The revocation kept in PostgreSQL must still win once Redis is reachable again. + */ + private fun loseTombstone(redis: GenericContainer<*>, user: TestUser, cachedCopy: String) { + TestRedis.cli(redis, "SET", "session:${user.token.toByteArray().computeSha256()}", cachedCopy, "PX", "60000") + } + + private fun cachedCopy(redis: GenericContainer<*>, user: TestUser): String { + return TestRedis.cli(redis, "GET", "session:${user.token.toByteArray().computeSha256()}") + .also { assertTrue(it.contains(user.id.toString()), "session must be cached before the outage") } + } + + @Test + fun `logout during a Redis outage is not undone by a stale cached copy`() { + val redis = TestRedis.create().also { it.start() } + val docker = redis.dockerClient + + try { + runApp(redis = TestRedis.endpoint(redis)) { client -> + val alice = client.register("alice") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + val copy = cachedCopy(redis, alice) + + docker.pauseContainerCmd(redis.containerId).exec() + assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + docker.unpauseContainerCmd(redis.containerId).exec() + + loseTombstone(redis, alice, copy) + + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + assertEquals("revoked", TestRedis.cli(redis, "GET", "session:${alice.token.toByteArray().computeSha256()}")) + } + } finally { + redis.stop() + } + } + + @Test + fun `logout during a Redis outage survives an API restart`() { + val redis = TestRedis.create().also { it.start() } + val docker = redis.dockerClient + + lateinit var alice: TestUser + lateinit var chat: Chat + lateinit var copy: String + + try { + runApp(redis = TestRedis.endpoint(redis)) { client -> + alice = client.register("alice") + val bob = client.register("bob") + chat = client.createChat(alice, "restart", listOf(alice.id, bob.id)) + copy = cachedCopy(redis, alice) + + docker.pauseContainerCmd(redis.containerId).exec() + assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + } + + docker.unpauseContainerCmd(redis.containerId).exec() + loseTombstone(redis, alice, copy) + + runApp(redis = TestRedis.endpoint(redis)) { client -> + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/chats/${chat.id}/messages") { auth(alice) }.status) + assertEquals( + HttpStatusCode.Unauthorized, + client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "after logout")) + }.status + ) + } + } finally { + redis.stop() + } + } + + @Test + fun `without PostgreSQL requests fail with a bounded 503`() { + val database = TestDatabase.create().also { it.start() } + + try { + runApp(database = database, databaseConnectionTimeoutMs = 2000) { client -> + val alice = client.register("alice") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + database.dockerClient.stopContainerCmd(database.containerId).exec() + + val started = TimeSource.Monotonic.markNow() + + // The session itself is still served from Redis, the user lookup needs PostgreSQL. + assertUnavailable(client.get("/api/v1/users/me") { auth(alice) }) + assertUnavailable(client.post("/api/v1/auth/sessions") { basicAuth(alice.user.username, alice.password) }) + assertEquals(HttpStatusCode.ServiceUnavailable, client.get("/health/ready").status) + assertEquals(HttpStatusCode.OK, client.get("/health/live").status) + + assertTrue(started.elapsedNow() < 15.seconds, "every request must fail within the pool timeout") + + client.assertAnonymousRejected(unknownTokenStatus = HttpStatusCode.ServiceUnavailable) + } + } finally { + database.stop() + } + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/SessionCacheTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/SessionCacheTest.kt new file mode 100644 index 00000000..d4523641 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/SessionCacheTest.kt @@ -0,0 +1,65 @@ +package com.mtusi.messenger.main.plugins + +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.register +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.utils.computeSha256 +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.http.HttpStatusCode +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class SessionCacheTest { + + private val redis = TestRedis.container + + private fun cached(user: TestUser): String { + return TestRedis.cli(redis, "GET", "session:${user.token.toByteArray().computeSha256()}") + } + + @Test + fun `first authenticated request fills the cache`() = runApp { client -> + val alice = client.register("alice") + + assertEquals("", cached(alice), "login does not write to the cache") + + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + assertTrue(cached(alice).contains(alice.id.toString())) + + // The session lives for an hour, its cached copy at most sessionCacheTtlSeconds. + val ttl = TestRedis.cli(redis, "TTL", "session:${alice.token.toByteArray().computeSha256()}").toLong() + assertTrue(ttl in 1..60, "cached copy TTL was $ttl") + } + + @Test + fun `logout leaves a tombstone and the token is rejected`() = runApp { client -> + val alice = client.register("alice") + client.get("/api/v1/users/me") { auth(alice) } + + assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + + assertEquals("revoked", cached(alice)) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + } + + @Test + fun `emptied cache falls back to PostgreSQL`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + client.get("/api/v1/users/me") { auth(alice) } + client.delete("/api/v1/auth/sessions/current") { auth(bob) } + + TestRedis.cli(redis, "FLUSHALL") + + // PostgreSQL is the source of truth: the live session survives, the revoked one stays revoked. + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(bob) }.status) + assertTrue(cached(alice).contains(alice.id.toString()), "cache is filled again") + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt new file mode 100644 index 00000000..c502993b --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt @@ -0,0 +1,152 @@ +package com.mtusi.messenger.main.support + +import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse +import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest +import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.module +import com.typesafe.config.ConfigFactory +import io.ktor.client.HttpClient +import io.ktor.client.call.body +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.post +import io.ktor.client.request.setBody +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.config.HoconApplicationConfig +import io.ktor.server.testing.ApplicationTestBuilder +import io.ktor.server.testing.testApplication +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy +import org.testcontainers.containers.PostgreSQLContainer +import java.util.UUID + +@OptIn(ExperimentalSerializationApi::class) +val testJson = Json { + ignoreUnknownKeys = true + encodeDefaults = true + namingStrategy = JsonNamingStrategy.SnakeCase +} + +const val TEST_PASSWORD = "correct-horse-battery" + +data class TestUser( + val user: User, + val password: String, + val token: String, +) { + val id: UUID + get() = user.id +} + +data class RedisEndpoint( + val host: String, + val port: Int, +) + +private fun testConfig( + sessionTtlSeconds: Long, + database: PostgreSQLContainer<*>, + databaseConnectionTimeoutMs: Long, + redis: RedisEndpoint, +) = HoconApplicationConfig( + ConfigFactory.parseMap( + mapOf( + "database.host" to database.host, + "database.port" to database.firstMappedPort, + "database.name" to database.databaseName, + "database.username" to database.username, + "database.password" to database.password, + "database.maxPoolSize" to 10, + "database.connectionTimeoutMs" to databaseConnectionTimeoutMs, + "database.statementTimeoutMs" to 5000, + "redis.host" to redis.host, + "redis.port" to redis.port, + "redis.password" to TestRedis.PASSWORD, + "redis.connectTimeoutMs" to 500, + "redis.commandTimeoutMs" to 500, + "authentication.realm" to "messenger", + "authentication.sessionTtlSeconds" to sessionTtlSeconds, + "authentication.sessionCacheTtlSeconds" to 60, + ) + ) +) + +/** + * Docker Desktop can be slow to accept the first connection, hence the generous default database timeout. + */ +fun runApp( + sessionTtlSeconds: Long = 3600, + database: PostgreSQLContainer<*> = TestDatabase.container, + databaseConnectionTimeoutMs: Long = 10000, + redis: RedisEndpoint = TestRedis.endpoint(), + block: suspend ApplicationTestBuilder.(HttpClient) -> Unit +) = testApplication { + environment { + config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis) + } + application { + module() + } + val client = createClient { + install(ContentNegotiation) { + json(testJson) + } + } + block(client) +} + +/** + * Usernames are unique per call: all tests share one database. + */ +fun uniqueUsername(prefix: String): String = "${prefix}_${UUID.randomUUID().toString().replace("-", "").take(12)}" + +suspend fun HttpClient.createUser(prefix: String, password: String = TEST_PASSWORD): User { + val username = uniqueUsername(prefix) + val response = post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = username, displayName = prefix, password = password)) + } + check(response.status == HttpStatusCode.Created) { "createUser failed: ${response.status}" } + return response.body() +} + +suspend fun HttpClient.login(username: String, password: String): SessionResponse { + val response = post("/api/v1/auth/sessions") { + basicAuth(username, password) + } + check(response.status == HttpStatusCode.Created) { "login failed: ${response.status}" } + return response.body() +} + +/** + * Registers a user and opens a session for it. + */ +suspend fun HttpClient.register(prefix: String): TestUser { + val user = createUser(prefix) + val session = login(user.username, TEST_PASSWORD) + return TestUser(user, TEST_PASSWORD, session.token) +} + +fun HttpRequestBuilder.auth(user: TestUser) = bearerAuth(user.token) + +suspend fun HttpClient.createChat( + creator: TestUser, + title: String, + memberIds: List, +): Chat { + val response = post("/api/v1/chats") { + auth(creator) + contentType(ContentType.Application.Json) + setBody(CreateChatRequest(title = title, memberIds = memberIds.map(UUID::toString))) + } + check(response.status == HttpStatusCode.Created) { "createChat failed: ${response.status}" } + return response.body() +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt new file mode 100644 index 00000000..e9a29cd4 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt @@ -0,0 +1,30 @@ +package com.mtusi.messenger.main.support + +import org.testcontainers.containers.PostgreSQLContainer +import org.testcontainers.utility.DockerImageName + +/** + * One PostgreSQL container per test JVM, same image as compose.yaml. + * Tests share it and stay independent by using unique usernames. + */ +object TestDatabase { + + const val NAME = "messenger" + const val USERNAME = "messenger" + const val PASSWORD = "messenger" + + val container: PostgreSQLContainer<*> by lazy { + create().also { it.start() } + } + + /** + * A separate, not yet started container for tests that stop the database. + */ + fun create(): PostgreSQLContainer<*> { + return PostgreSQLContainer(DockerImageName.parse("postgres:17.6-alpine3.22").asCompatibleSubstituteFor("postgres")) + .withDatabaseName(NAME) + .withUsername(USERNAME) + .withPassword(PASSWORD) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestRedis.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestRedis.kt new file mode 100644 index 00000000..d5a5b6ef --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestRedis.kt @@ -0,0 +1,54 @@ +package com.mtusi.messenger.main.support + +import org.testcontainers.containers.GenericContainer +import org.testcontainers.utility.DockerImageName +import java.net.ServerSocket + +/** + * One Redis container per test JVM, same image and memory settings as compose.yaml. + */ +object TestRedis { + + const val PASSWORD = "messenger" + + private const val PORT = 6379 + + val container: GenericContainer<*> by lazy { + create().also { it.start() } + } + + /** + * A separate, not yet started container for tests that pause or flush Redis. + */ + fun create(): GenericContainer<*> { + return GenericContainer(DockerImageName.parse("redis:8.4.0-alpine")) + .withExposedPorts(PORT) + .withCommand( + "redis-server", + "--requirepass", PASSWORD, + "--save", "", + "--appendonly", "no", + "--maxmemory-policy", "volatile-lru" + ) + } + + fun endpoint(container: GenericContainer<*> = this.container) = RedisEndpoint( + host = container.host, + port = container.getMappedPort(PORT) + ) + + /** + * An endpoint nobody listens on: connections are refused immediately. + */ + fun unreachableEndpoint(): RedisEndpoint { + val port = ServerSocket(0).use { it.localPort } + return RedisEndpoint(host = "127.0.0.1", port = port) + } + + fun cli(container: GenericContainer<*>, vararg command: String): String { + val result = container.execInContainer("redis-cli", "--no-auth-warning", "-a", PASSWORD, *command) + check(result.exitCode == 0) { "redis-cli failed: ${result.stderr}" } + return result.stdout.trim() + } + +} -- 2.54.0 From 92c2fdc254c9c0ab59adfb90764f53738ceff218 Mon Sep 17 00:00:00 2001 From: Mraksimus Date: Fri, 23 Oct 2026 14:11:11 +0300 Subject: [PATCH 2/4] Completed for a grade of three --- build.gradle.kts | 3 +- compose.yaml | 42 ++++- evidence/failover-api-1.txt | 24 +++ evidence/failover-api-2.txt | 24 +++ evidence/gradle-tests.txt | 84 ++++++++++ evidence/idempotency-race.txt | 19 +++ evidence/instances.txt | 21 +++ evidence/persistence.txt | 24 +++ evidence/smoke.txt | 2 + infra/haproxy/haproxy.cfg | 47 ++++++ run/_template/config/cors.conf | 23 +++ scripts/idempotency-race.py | 84 ++++++++++ scripts/restart.sh | 6 +- scripts/stop-one.sh | 22 +++ .../kotlin/com/mtusi/messenger/main/App.kt | 13 +- .../serializers/HttpMethodSerializer.kt | 31 ++++ .../messenger/main/config/cors/CorsConfig.kt | 100 +++++++++++ .../main/config/cors/DefaultCorsConfig.kt | 11 ++ .../main/config/cors/SerialCorsConfig.kt | 18 ++ .../main/config/database/DatabaseConfig.kt | 2 + .../config/database/SerialDatabaseConfig.kt | 3 +- .../config/message/DefaultMessageConfig.kt | 11 ++ .../main/config/message/MessageConfig.kt | 5 + .../config/message/SerialMessageConfig.kt | 8 + .../controllers/message/MessageController.kt | 7 +- .../message/MessageIdempotencyKeyModel.kt | 25 +++ .../session/cache/SessionCacheRepository.kt | 11 -- .../revocation/SessionRevocationRepository.kt | 6 - .../repositories/chat/ChatRepository.kt | 3 - .../message/ExposedMessageRepository.kt | 10 ++ .../repositories/message/MessageRepository.kt | 4 + .../ExposedMessageIdempotencyRepository.kt | 83 +++++++++ .../MessageIdempotencyRepository.kt | 43 +++++ .../authentication/AuthenticationService.kt | 7 - .../DefaultAuthenticationService.kt | 22 --- .../services/message/DefaultMessageService.kt | 63 ++++++- .../services/message/MessageService.kt | 3 +- .../message/results/MessageCreationResult.kt | 2 + .../com/mtusi/messenger/main/plugins/Cors.kt | 13 ++ .../mtusi/messenger/main/plugins/Database.kt | 40 ++++- .../messenger/main/plugins/InstanceId.kt | 28 ++++ .../mtusi/messenger/main/utils/Pagination.kt | 9 - .../com/mtusi/messenger/main/utils/Secrets.kt | 3 - .../com/mtusi/messenger/main/utils/Time.kt | 4 - .../mtusi/messenger/main/utils/Validation.kt | 9 + src/main/resources/application.conf | 36 ++++ .../V3__message_idempotency_keys.sql | 19 +++ .../message/MessageConcurrencyTest.kt | 2 + .../message/MessageControllerTest.kt | 6 + .../message/MessageIdempotencyTest.kt | 157 ++++++++++++++++++ .../mtusi/messenger/main/plugins/CorsTest.kt | 26 +++ .../main/plugins/DependencyFailureTest.kt | 2 + .../messenger/main/plugins/InstanceIdTest.kt | 30 ++++ .../mtusi/messenger/main/support/TestApp.kt | 13 +- 54 files changed, 1221 insertions(+), 92 deletions(-) create mode 100644 evidence/failover-api-1.txt create mode 100644 evidence/failover-api-2.txt create mode 100644 evidence/gradle-tests.txt create mode 100644 evidence/idempotency-race.txt create mode 100644 evidence/instances.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/smoke.txt create mode 100644 infra/haproxy/haproxy.cfg create mode 100644 run/_template/config/cors.conf create mode 100755 scripts/idempotency-race.py create mode 100755 scripts/stop-one.sh create mode 100644 src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt create mode 100644 src/main/resources/migrations/V3__message_idempotency_keys.sql create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 0a5b5720..52fda1bc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") } diff --git a/compose.yaml b/compose.yaml index 42c56457..163a6261 100644 --- a/compose.yaml +++ b/compose.yaml @@ -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 diff --git a/evidence/failover-api-1.txt b/evidence/failover-api-1.txt new file mode 100644 index 00000000..3ef92aef --- /dev/null +++ b/evidence/failover-api-1.txt @@ -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 ). diff --git a/evidence/failover-api-2.txt b/evidence/failover-api-2.txt new file mode 100644 index 00000000..232bad25 --- /dev/null +++ b/evidence/failover-api-2.txt @@ -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 ). diff --git a/evidence/gradle-tests.txt b/evidence/gradle-tests.txt new file mode 100644 index 00000000..7411d123 --- /dev/null +++ b/evidence/gradle-tests.txt @@ -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 diff --git a/evidence/idempotency-race.txt b/evidence/idempotency-race.txt new file mode 100644 index 00000000..2720d6fa --- /dev/null +++ b/evidence/idempotency-race.txt @@ -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 diff --git a/evidence/instances.txt b/evidence/instances.txt new file mode 100644 index 00000000..440fc6ba --- /dev/null +++ b/evidence/instances.txt @@ -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) diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..d33c8104 --- /dev/null +++ b/evidence/persistence.txt @@ -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 diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..b049e9a0 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +$ make test +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. diff --git a/infra/haproxy/haproxy.cfg b/infra/haproxy/haproxy.cfg new file mode 100644 index 00000000..b3abf039 --- /dev/null +++ b/infra/haproxy/haproxy.cfg @@ -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 diff --git a/run/_template/config/cors.conf b/run/_template/config/cors.conf new file mode 100644 index 00000000..4551a034 --- /dev/null +++ b/run/_template/config/cors.conf @@ -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 +} diff --git a/scripts/idempotency-race.py b/scripts/idempotency-race.py new file mode 100755 index 00000000..cdcc6af6 --- /dev/null +++ b/scripts/idempotency-race.py @@ -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) diff --git a/scripts/restart.sh b/scripts/restart.sh index 76c2bb0c..97789e1d 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -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 diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..b848e733 --- /dev/null +++ b/scripts/stop-one.sh @@ -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 diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt index 8ec647e2..4115fd77 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/App.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -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): Unit = EngineMain.main(args) +fun main(args: Array) { + // `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() diff --git a/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt new file mode 100644 index 00000000..59bc7bd8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt @@ -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 { + + 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()) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt new file mode 100644 index 00000000..a6aa0185 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt @@ -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 + + /** + * Whether any [HttpMethod][io.ktor.http.HttpMethod] should be allowed + * + * Must default to true + */ + val allowAnyMethod: Boolean + val allowedMethods: List + + /** + * Whether any header should be allowed + * + * Must default to true + */ + val allowAnyHeader: Boolean + val allowedHeaders: List + + /** + * Response headers a browser page may read, e.g. X-Request-Id and X-Instance-Id in Swagger UI + */ + val exposedHeaders: List + + /** + * 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 + ) + +} + +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 + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt new file mode 100644 index 00000000..5dab8651 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt @@ -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() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt new file mode 100644 index 00000000..93918a29 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt @@ -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 = emptyList(), + override val allowAnyMethod: Boolean = true, + override val allowedMethods: List = emptyList(), + override val allowAnyHeader: Boolean = true, + override val allowedHeaders: List = emptyList(), + override val exposedHeaders: List = emptyList(), + override val allowSameOrigin: Boolean = false, + override val allowCredentials: Boolean = true, + override val allowNonSimpleContentTypes: Boolean = true +) : CorsConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt index 40d48c28..1a971655 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt @@ -14,6 +14,8 @@ interface DatabaseConfig { val connectionTimeoutMs: Long val statementTimeoutMs: Long + val migrateOnStart: Boolean + val jdbcConnectionUrl: String get() = "jdbc:postgresql://$host:$port/$name" diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt index 00c4890e..1a2ebc92 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt @@ -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 diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt new file mode 100644 index 00000000..8a62d41d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt @@ -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() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt new file mode 100644 index 00000000..ccf1808f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt @@ -0,0 +1,5 @@ +package com.mtusi.messenger.main.config.message + +interface MessageConfig { + val idempotencyTtlSeconds: Long +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt new file mode 100644 index 00000000..6c0752e7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt @@ -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 diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt index d1b02be5..c5c5594b 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt @@ -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() 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) } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt new file mode 100644 index 00000000..dd2698e2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt @@ -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) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt index c244cc95..31ef6897 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt @@ -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 { 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 diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt index 8612e876..ab9d69b9 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt @@ -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 { 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 diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt index 956e9dfc..4e49658c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt @@ -21,9 +21,6 @@ interface ChatRepository : Repository { userId: UUID ): Boolean - /** - * Chats of [userId] ordered by `(created_at, id)`, strictly after [after]. - */ fun findPageByMemberId( userId: UUID, after: PageKey?, diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt index 19dc19c4..f687fd95 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt @@ -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?, diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt index 80a130d2..86bddb81 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt @@ -13,6 +13,10 @@ interface MessageRepository : Repository { text: String ): Message + fun findById( + id: UUID + ): Message? + fun findPageByChatId( chatId: UUID, after: PageKey?, diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt new file mode 100644 index 00000000..0363fc04 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt @@ -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) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt new file mode 100644 index 00000000..a3069dd0 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt @@ -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 { + + 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 + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt index dd86fe0d..b3db613b 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt @@ -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 diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt index 9bfc3df9..c6a0fa14 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt @@ -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>() - /** - * 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 withCache( operation: suspend () -> T ): T? { diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt index 800af6fe..34b11a2c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt @@ -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( diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt index 075c35d7..5ec42822 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt @@ -9,7 +9,8 @@ interface MessageService { fun send( chatId: UUID, senderId: UUID, - text: String + text: String, + idempotencyKey: String ): MessageCreationResult fun findPageByChatId( diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt index d5ac9756..1f260a32 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt @@ -8,6 +8,8 @@ sealed class MessageCreationResult { data object ChatNotFound : Error() + data object IdempotencyKeyReused : Error() + data class Success( val message: Message ) : MessageCreationResult() diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt new file mode 100644 index 00000000..de5c66b1 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt @@ -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() + configure(config) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt index 32001ed3..623f3dd0 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt @@ -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() + + config.createDataSource().use(::applyDatabaseMigrations) } fun main(args: Array) { @@ -96,6 +121,7 @@ private fun generateMigration(description: String) { ChatModel, ChatMemberModel, MessageModel, + MessageIdempotencyKeyModel, SessionModel, SessionRevocationModel ) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt new file mode 100644 index 00000000..34066029 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt @@ -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) + } + } + ) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt index cd84ad28..bc605e09 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt @@ -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 List.toPage( limit: Int, scope: String, diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt index e0b30312..8288a810 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt @@ -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? diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt index 7c8c8d43..86b8e3c7 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt @@ -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( diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt index 395e8fb6..aad25ee1 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt @@ -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") diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 0f2d4033..7460517c 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -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 diff --git a/src/main/resources/migrations/V3__message_idempotency_keys.sql b/src/main/resources/migrations/V3__message_idempotency_keys.sql new file mode 100644 index 00000000..9ddedfee --- /dev/null +++ b/src/main/resources/migrations/V3__message_idempotency_keys.sql @@ -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); diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt index a91cb3d8..5cc3bc1b 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt @@ -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")) } diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt index ac72e82c..61ce8052 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt @@ -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")) } diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt new file mode 100644 index 00000000..eb9728c5 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt @@ -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 { + return get("/api/v1/chats/${chat.id}/messages?limit=100") { auth(user) }.body>().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(), retry.body()) + 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().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().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().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() }.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() + delay(1500) + val second = client.send(alice, chat, "hello again", key) + + assertEquals(HttpStatusCode.Created, second.status) + assertNotEquals(first.id, second.body().id) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt new file mode 100644 index 00000000..50fb3b11 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt @@ -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) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt index 2a171457..e221736d 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt @@ -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 diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt new file mode 100644 index 00000000..e3141f41 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt @@ -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"] }) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt index c502993b..669b36c6 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt @@ -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, -- 2.54.0 From af184cabe30c61eccd74e24932f3a28aaf929224 Mon Sep 17 00:00:00 2001 From: Mraksimus Date: Fri, 23 Oct 2026 16:08:53 +0300 Subject: [PATCH 3/4] Completed for a grade of four --- Dockerfile | 6 +- REPORT.md | 189 ++++++++++++++++-- compose.yaml | 39 +++- evidence/clean-start.txt | 20 ++ evidence/failover-api-1.txt | 24 +-- evidence/failover-api-2.txt | 22 +- evidence/gradle-tests.txt | 35 ++-- evidence/idempotency-race.txt | 4 +- evidence/instances.txt | 28 +-- evidence/limits.txt | 44 ++++ evidence/load/README.md | 20 ++ evidence/load/failover-kill-api-1/events.json | 2 + evidence/load/failover-kill-api-1/k6.txt | 26 +++ .../load/failover-kill-api-1/per-second.csv | 46 +++++ .../load/failover-kill-api-1/summary.json | 26 +++ evidence/load/failover-kill-api-2/events.json | 2 + evidence/load/failover-kill-api-2/k6.txt | 26 +++ .../load/failover-kill-api-2/per-second.csv | 46 +++++ .../load/failover-kill-api-2/summary.json | 26 +++ evidence/load/failover-stop-api-1/events.json | 2 + evidence/load/failover-stop-api-1/k6.txt | 25 +++ .../load/failover-stop-api-1/per-second.csv | 46 +++++ .../load/failover-stop-api-1/summary.json | 26 +++ evidence/load/failover-stop-api-2/events.json | 2 + evidence/load/failover-stop-api-2/k6.txt | 26 +++ .../load/failover-stop-api-2/per-second.csv | 46 +++++ .../load/failover-stop-api-2/summary.json | 26 +++ evidence/load/rolling-update/events.json | 1 + evidence/load/rolling-update/k6.txt | 25 +++ evidence/load/rolling-update/per-second.csv | 71 +++++++ evidence/load/rolling-update/summary.json | 20 ++ evidence/persistence.txt | 28 +-- infra/haproxy/haproxy.cfg | 10 + run/_template/config/authentication.conf | 3 + run/_template/config/database.conf | 3 + run/_template/config/instance.conf | 4 + run/_template/config/messages.conf | 4 + run/_template/config/redis.conf | 4 +- scripts/load/failover-under-load.sh | 55 +++++ scripts/load/mixed.js | 67 +++++++ scripts/load/rolling-under-load.sh | 34 ++++ scripts/load/summarize.py | 110 ++++++++++ scripts/rolling-update.sh | 40 ++++ scripts/stop-one.sh | 2 +- .../kotlin/com/mtusi/messenger/main/App.kt | 2 + .../controllers/health/HealthController.kt | 7 + .../main/plugins/GracefulShutdown.kt | 38 ++++ src/main/resources/application.conf | 12 +- .../main/plugins/DependencyFailureTest.kt | 1 + .../messenger/main/support/TestDatabase.kt | 18 ++ 50 files changed, 1297 insertions(+), 92 deletions(-) create mode 100644 evidence/clean-start.txt create mode 100644 evidence/limits.txt create mode 100644 evidence/load/README.md create mode 100644 evidence/load/failover-kill-api-1/events.json create mode 100644 evidence/load/failover-kill-api-1/k6.txt create mode 100644 evidence/load/failover-kill-api-1/per-second.csv create mode 100644 evidence/load/failover-kill-api-1/summary.json create mode 100644 evidence/load/failover-kill-api-2/events.json create mode 100644 evidence/load/failover-kill-api-2/k6.txt create mode 100644 evidence/load/failover-kill-api-2/per-second.csv create mode 100644 evidence/load/failover-kill-api-2/summary.json create mode 100644 evidence/load/failover-stop-api-1/events.json create mode 100644 evidence/load/failover-stop-api-1/k6.txt create mode 100644 evidence/load/failover-stop-api-1/per-second.csv create mode 100644 evidence/load/failover-stop-api-1/summary.json create mode 100644 evidence/load/failover-stop-api-2/events.json create mode 100644 evidence/load/failover-stop-api-2/k6.txt create mode 100644 evidence/load/failover-stop-api-2/per-second.csv create mode 100644 evidence/load/failover-stop-api-2/summary.json create mode 100644 evidence/load/rolling-update/events.json create mode 100644 evidence/load/rolling-update/k6.txt create mode 100644 evidence/load/rolling-update/per-second.csv create mode 100644 evidence/load/rolling-update/summary.json create mode 100644 run/_template/config/instance.conf create mode 100644 run/_template/config/messages.conf create mode 100755 scripts/load/failover-under-load.sh create mode 100644 scripts/load/mixed.js create mode 100755 scripts/load/rolling-under-load.sh create mode 100755 scripts/load/summarize.py create mode 100755 scripts/rolling-update.sh create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/GracefulShutdown.kt diff --git a/Dockerfile b/Dockerfile index 096e5a7a..2f60fd5f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,12 +8,16 @@ COPY contracts ./contracts RUN --mount=type=cache,target=/home/gradle/.gradle gradle --no-daemon shadowJar FROM eclipse-temurin:21-jre-alpine +# Distinguishes builds of the same code, e.g. the image rolled out by scripts/rolling-update.sh. +ARG BUILD_ID=dev +LABEL org.opencontainers.image.revision=$BUILD_ID RUN apk add --no-cache wget && addgroup -S app && adduser -S -G app app WORKDIR /app COPY --from=build /src/build/libs/messenger.jar /app/messenger.jar USER app ENV APP_HOST=0.0.0.0 APP_PORT=8080 EXPOSE 8080 -HEALTHCHECK --interval=10s --timeout=3s --start-period=15s --retries=3 \ +# Docker's own view of the replica (compose depends_on, rolling update). The balancer runs its own checks every 1 s. +HEALTHCHECK --interval=10s --timeout=3s --start-period=30s --start-interval=1s --retries=3 \ CMD wget -qO- http://127.0.0.1:8080/health/ready || exit 1 ENTRYPOINT ["java","-XX:MaxRAMPercentage=75.0","-jar","/app/messenger.jar"] diff --git a/REPORT.md b/REPORT.md index c2679005..92584380 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,198 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO +- Автор, группа: Кучерявенко Максим, БВТ2303 +- Целевая оценка; выбранный трек 5, если нужен: 4 - Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Использованные источники, библиотеки и помощь ИИ: всё из lab2 (Kotlin 2.3.20, Ktor 3.3.0, Koin 4.1, Exposed 1.0, HikariCP, Flyway 11.9, Lettuce 6.7, BouncyCastle, Testcontainers) плюс Ktor CORS. Балансировщик HAProxy 3.2.4, нагрузка k6 2.3. Документация: [Compose services](https://docs.docker.com/reference/compose-file/services/), [HAProxy 3.2 configuration manual](https://docs.haproxy.org/3.2/configuration.html) (server-template, resolvers, httpchk, retries/redispatch), [PostgreSQL INSERT … ON CONFLICT](https://www.postgresql.org/docs/17/sql-insert.html). ИИ (Claude) использовал как парного разработчика. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker 24+ с Compose v2, Python 3.9+ (публичные тесты), `openssl` (секреты). Для `./gradlew test` ещё JDK 21+ и запущенный Docker, для нагрузочных прогонов — k6. Я всё запускал на Docker 28.4, Compose 2.39, JDK 24, k6 2.3. + +```sh +# один раз: пароли PostgreSQL и Redis в secrets/ (папка в .gitignore, существующие файлы не перезаписываются) +./scripts/init-secrets.sh + +# сборка образа и запуск: postgres, redis -> migrate (один раз применяет миграции и выходит) -> api x2 -> lb +# --wait ждёт healthcheck всех сервисов и успешного завершения migrate +docker compose up -d --build --wait + +# публичные проверки, всё через балансировщик http://localhost:8080 +make check +make test +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +docker compose start api # вернуть остановленную реплику + +# свои тесты (Testcontainers) +./gradlew test + +# одновременные повторы одного Idempotency-Key через обе реплики +python3 scripts/idempotency-race.py + +# уровень 4: отказ каждой реплики под нагрузкой и rolling update (нужен k6) +MODE=stop TARGET=api-1 scripts/load/failover-under-load.sh # также MODE=kill, TARGET=api-2 +scripts/load/rolling-under-load.sh # сам update без нагрузки: scripts/rolling-update.sh + +# остановка с сохранением данных +docker compose stop # или docker compose down, volume postgres-data остаётся + +# намеренное удаление всех учебных данных +docker compose down -v +``` + +`.env` не обязателен: у всех переменных в `compose.yaml` есть значения по умолчанию. `docker compose --env-file .env.example config --quiet` проходит. + +`make test-failover` останавливает ту реплику, которая ответила на первое сообщение теста. Тест делает одинаковое число запросов, и round robin каждый раз приводит его к одной и той же реплике. Чтобы проверить вторую, перед повторным запуском я делаю один лишний запрос (`curl -s localhost:8080/health/live`), он сдвигает очередь. + +После `scripts/rolling-update.sh` контейнеры называются `api-3`, `api-4` (Compose нумерует новые дальше). `stop-one.sh` от имён не зависит. `failover-under-load.sh` принимает имя (`TARGET=api-3`), прежние имена возвращает `docker compose down && docker compose up -d --wait` без `-v`. + +Локально без Docker: `./gradlew run`, конфиг из `run/development/config/*.conf`, порт 8081, один процесс сам применяет миграции. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +``` +клиент ──HTTP──▶ lb: HAProxy 3.2.4 (127.0.0.1:8080, единственный опубликованный порт) + │ round robin, проверка GET /health/ready раз в 1 с, адреса реплик из Docker DNS + ├──▶ api-1 (Ktor) ─┐ + └──▶ api-2 (Ktor) ─┼──JDBC, пул ≤10 на реплику──▶ postgres (volume postgres-data) ← источник истины + └──RESP──▶ redis (кэш сессий, без persistence) +migrate (тот же образ, `migrate`) ── один раз до старта реплик ──▶ postgres +``` + +| Роль | Service | Образ | Health/readiness | Volume | Порт | Credentials | Лимиты | +| --- | --- | --- | --- | --- | --- | --- | --- | +| Балансировщик | `lb` | `haproxy:3.2.4-alpine` | `wget http://127.0.0.1:8404/healthz` (внутренний порт) | — | `127.0.0.1:8080` | нет | 0.5 CPU, 128 МБ | +| API ×2 | `api` | `mtusi-messenger:lab3` (свой Dockerfile) | Docker: `/health/ready` раз в 10 с; HAProxy: `/health/ready` раз в 1 с | — | 8080 только в сети compose | secrets `db_password`, `redis_password` | 1 CPU, 512 МБ каждая | +| Миграции | `migrate` | тот же образ, команда `migrate` | разовый, healthcheck выключен | — | — | `db_password` | 0.5 CPU, 384 МБ | +| БД | `postgres` | `postgres:17.6-alpine3.22` | `pg_isready` | `postgres-data` | 5432 в сети | `db_password` | 1 CPU, 1 ГБ | +| Кэш сессий | `redis` | `redis:8.4.0-alpine` | `redis-cli ping` | — | 6379 в сети | `redis_password` | 0.5 CPU, 128 МБ | + +**Что перенёс из lab2 без изменений.** Весь API и контракт, PostgreSQL как источник истины, Bearer-сессии в PostgreSQL с кэшем в Redis (single-flight, tombstone при logout, outbox `session_revocations`), курсорную пагинацию, Argon2id, Docker secrets, 503 при недоступной базе. У API и раньше не было состояния в памяти, от которого зависит корректность: сессии в PostgreSQL, кэш в общем Redis, курсор пагинации не подписан ключом процесса. Поэтому реплики взаимозаменяемы без доработок. + +**Что поменял в этой работе.** +- **`X-Instance-Id`** (`plugins/InstanceId.kt`) — в каждом ответе API, включая ошибки и health. Значение: `INSTANCE_ID`, если задан, иначе `HOSTNAME` (в Docker это короткий id контейнера), иначе случайная строка. Задаётся при старте процесса и не меняется. Благодаря тому, что это id контейнера, `stop-one.sh` находит нужный контейнер без таблицы соответствий. +- **`Idempotency-Key`** на `POST /chats/{id}/messages`, ключ обязателен, формат `[A-Za-z0-9._:-]{1,128}`, иначе 400. Таблица `message_idempotency_keys` (миграция `V3`) с первичным ключом `(user_id, chat_id, key)` — это область уникальности из контракта: метод и путь у этого запроса фиксированы, остаются пользователь, чат и ключ. В той же транзакции, что и сообщение: + 1. Проверка членства в чате, в том числе при повторе: постороннему — 404. + 2. `INSERT … ON CONFLICT DO NOTHING` занимает ключ. + 3. Ключ свободен — создаём сообщение и записываем его id в строку ключа. + 4. Ключ занят — тот же SHA-256 тела даёт то же сообщение с 201, другой — 409. + 5. Ключ старше `IDEMPOTENCY_TTL_SECONDS` (86400) удаляется и занимается заново. + + Если тот же ключ сейчас вставляет другая транзакция (другая реплика), PostgreSQL на шаге 2 ждёт её конца. Поэтому одновременные повторы создают одно сообщение. При ошибке 5xx транзакция откатывается вместе с ключом, и неудачная попытка не запоминается как результат. +- **Миграции отдельно от реплик.** `java -jar messenger.jar migrate` применяет Flyway и выходит (main-класс jar теперь `AppKt`, а не `EngineMain`). В compose это сервис `migrate`. Реплики запускаются с `DB_MIGRATE_ON_START=false` после его успешного завершения (`service_completed_successfully`) и делают только `flyway.validate()`: при неприменённой миграции реплика не стартует. Так реплики не устраивают гонку за схему. Локальный процесс и тесты по-прежнему мигрируют сами. +- **Балансировщик HAProxy** (`infra/haproxy/haproxy.cfg`): + - адреса реплик берутся из Docker DNS (`server-template api 4 api:8080`, resolvers `127.0.0.11`, `hold` 2 с), так что масштабирование и замена контейнеров не требуют правки конфига; + - активная проверка `GET /health/ready`: `inter 1s fall 2 rise 2`; + - `balance roundrobin`; + - `retries 2` + `option redispatch` — только при ошибке соединения, то есть до отправки запроса. POST, который дошёл до реплики, балансировщик не повторяет; + - `maxconn 4096` в `global` (без него HAProxy занимал ~100 МБ из 128); + - внутренний frontend `:8404` с `/healthz` и `/stats` наружу не опубликован. + + Sticky sessions нет. +- **Плавная остановка** (`plugins/GracefulShutdown.kt`). По SIGTERM `/health/ready` сразу отвечает 503, процесс ещё 3 с (`SHUTDOWN_DRAIN_DELAY_MS`) обслуживает запросы. За это время HAProxy выводит реплику из ротации. Потом Ktor закрывает сокет и дожидается начатых запросов (`shutdownGracePeriod` 1 с, `shutdownTimeout` 5 с). Задержка сделана в обработчике `ApplicationStopPreparing`: Ktor вызывает его синхронно до закрытия каналов. До этой правки (на уровне 3) каждая остановка давала один 502 `SH--`: реплика закрывала соединение посреди POST. +- **Rolling update** (`scripts/rolling-update.sh`): + 1. Собирает образ с `BUILD_ID` (Dockerfile `ARG` + `LABEL`, чтобы новая версия отличалась даже при том же коде). + 2. Один раз применяет миграции: `docker compose run --rm migrate`. + 3. Для каждой старой реплики: `up --no-recreate --scale api=N+1` поднимает новую, скрипт ждёт `healthy` и состояние UP в `/stats` HAProxy, затем `docker stop` (с drain) и `docker rm` старой. + + Работающих реплик ни в какой момент не меньше двух. +- **Ресурсы.** CPU/RAM-лимиты у всех контейнеров, `container_name` убран у всех, порт опубликован только у `lb` и только на `127.0.0.1`. +- **CORS** (`plugins/Cors.kt`, `config/cors/`). Нужен для Swagger UI на `/docs`, открытого с другого адреса (например, dev-порт 8081). Разрешены только `localhost`/`127.0.0.1:8080/8081`, наружу отдаются `X-Request-Id`, `X-Instance-Id`, `WWW-Authenticate`. На API без браузера не влияет. + +**Транзакционные границы.** Отправка сообщения: проверка членства, ключ идемпотентности и само сообщение — одна транзакция PostgreSQL. Ключ без сообщения снаружи не виден (проверено запросом: 0 строк с `message_id IS NULL`). Остальное как в lab2. + +**Совместимость миграций с работающей версией.** Схема меняется только расширением. `V3` добавляет таблицу и индексы, старая версия кода её не трогает. Новая реплика проверяет схему через `validate()`. Старая реплика при `validate()` не падает от неизвестной ей новой миграции: Flyway по умолчанию пропускает «будущие» миграции (`ignoreMigrationPatterns=*:future`). Удаление или переименование колонок пришлось бы делать в два релиза (expand/contract); в этой работе таких изменений нет. + +**Переменные `.env.example` → реальная конфигурация.** + +| `.env.example` | У меня | +| --- | --- | +| `APP_HOST`, `APP_PORT` | так же, задаются в `compose.yaml` (`0.0.0.0:8080` внутри сети) | +| `APP_ENV` | не используется | +| `SESSION_TTL_SECONDS` | так же, по умолчанию 3600 | +| `DATABASE_URL_FILE` | разбит на `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER`, `DB_PASSWORD_FILE=/run/secrets/db_password` | +| `SESSION_STORE=postgres` | так и есть: сессии в PostgreSQL, Redis — кэш | +| `CACHE_URL_FILE` | `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD_FILE=/run/secrets/redis_password` | +| `API_REPLICAS` | так же, `deploy.replicas: ${API_REPLICAS:-2}` (HAProxy рассчитан максимум на 4) | +| `IDEMPOTENCY_TTL_SECONDS` | так же, по умолчанию 86400 | +| — | `DB_MIGRATE_ON_START` (false у реплик), `SHUTDOWN_DRAIN_DELAY_MS` (3000), `INSTANCE_ID` (не задаю) | + +**Таймауты и проверки — одна цепочка.** + +| Что | Значение | Почему так | +| --- | --- | --- | +| Проверка готовности в HAProxy | каждую 1 с; DOWN после 2 неудач, UP после 2 успехов | обнаружение ≤ 2 с | +| Обновление адресов из DNS | `hold` 2 с, `timeout resolve` 1 с | упавший контейнер исчезает из DNS, слот уходит в MAINT | +| Подключение HAProxy → реплика | 1 с, затем повтор на другую реплику (`retries 2`, `redispatch`) | до отправки запроса повтор безопасен и для POST | +| Ответ реплики | 10 с | больше, чем ожидание пула 2 с + `statement_timeout` 5 с | +| Drain после SIGTERM | 3 с | больше, чем 2 с на обнаружение | +| Остановка Ktor | 1 с тишины, максимум 5 с | дожать начатые запросы | +| `stop_grace_period` | 10 с | 3 + 5 = 8 с < 10 с, SIGKILL не нужен | +| `stop-one.sh` | `docker stop --time 9` | укладывается в 10 с, которые даёт тест | + +Максимальное время обнаружения отказа — около 2 с: при остановке по проверке готовности, при падении процесса по DNS или проверке. Измерено 1,1–1,9 с, см. ниже. ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test` | PASS, профиль lab3 | evidence/smoke.txt | +| Запуск из чистого clone | копия рабочего дерева без игнорируемых файлов, новые секреты, свежий volume, все `make`-проверки | все PASS | evidence/clean-start.txt | +| Стенд в compose: ≥2 API, один вход, общие БД и сессии | `docker compose ps` | `api-1`, `api-2`, `lb` с портом `127.0.0.1:8080`, `postgres`, `redis`; `migrate` завершился с 0 | evidence/instances.txt, evidence/limits.txt | +| Разные реплики без повторного входа, видно по `X-Instance-Id` | 6 запросов через LB; failover-тест сам проверяет ≥2 реплики | два id чередуются и совпадают с id контейнеров `api-1`, `api-2`; `observed_instances_before: 2` | evidence/instances.txt, evidence/failover-api-*.txt | +| Остановка любой одной реплики: новые чтения/записи ≤ 10 с, подтверждённое на месте | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` для api-1, потом для api-2 | PASS оба раза; 121 запись, 0 неудачных; остановлен именно контейнер с id из теста (`Exited (143)`) | evidence/failover-api-1.txt, evidence/failover-api-2.txt | +| Повтор POST с тем же ключом без дубля, в том числе одновременно и через другую реплику | `scripts/idempotency-race.py`: 20 раундов по 32 одновременных запроса с одним ключом; тесты `MessageIdempotencyTest` | 640 ответов 201 (320 + 320 по репликам), в каждом раунде один id, в истории 20 сообщений; 7 тестов: повтор, 409, 400, область ключа, доступ, 20 одновременных запросов, TTL | evidence/idempotency-race.txt, evidence/gradle-tests.txt | +| Неподтверждённую запись можно повторить с тем же ключом | failover-тест повторяет неопределённый POST с исходным ключом | дублей нет, проверка текстов прошла | evidence/failover-api-*.txt | +| `make test`, persistence, failover | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; у lb, обеих api, postgres, redis новый `StartedAt`, volume тот же | evidence/persistence.txt | +| Уровень 4: health/readiness, таймауты, SIGTERM согласованы, LB исключает недоступную реплику | таблица таймаутов выше; отказ под нагрузкой | после SIGTERM реплика получает последний запрос через 1,1–1,7 с, ошибок 0 | evidence/load/failover-stop-*/ | +| Уровень 4: поочерёдная остановка каждой реплики, RPS, ошибки, восстановление | `scripts/load/failover-under-load.sh`, 150 RPS, stop и kill для api-1 и api-2 | 4 прогона: ~150 RPS, 0 ошибок; таблица ниже | evidence/load/ | +| Уровень 4: параллельные запросы с одним ключом | см. строку про идемпотентность | 0 дублей | evidence/idempotency-race.txt | +| Уровень 4: лимиты CPU/RAM, API без портов наружу, секреты только где нужно, нет `container_name` | `docker inspect`, `docker compose ps`, `docker compose config` | лимиты у всех; порт только у `lb`; `lb` без секретов, `migrate` и `postgres` только `db_password`, `redis` только `redis_password` | evidence/limits.txt | +| Уровень 4: rolling update под нагрузкой, миграции однократно | `scripts/load/rolling-under-load.sh` | 10 501 запрос, 0 ошибок, p99 11,7 мс; обе реплики заменены на образ с новым `BUILD_ID` | evidence/load/rolling-update/ | +| Свои автотесты | `./gradlew test` | 68 тестов, все проходят | evidence/gradle-tests.txt | +| Секреты не в Git | `secrets/` и `.env` в `.gitignore`; в evidence нет токенов (k6 их не пишет, сырые запросы не сохраняются) | — | .gitignore | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Стенд: Apple M4 (10 ядер), 16 ГБ, macOS 27, Docker Desktop 28.4 (VM 10 CPU, 8 ГБ), лимиты контейнеров как в таблице выше. k6 2.3 на той же машине. + +Профиль (`scripts/load/mixed.js`): +- открытая модель, 150 запросов/с независимо от времени ответа, поэтому медленные ответы не прячут ошибки; +- 70 % `GET /users/me`, 30 % отправка сообщения с уникальным ключом; +- 10 пользователей, один чат, таймаут 5 с, без повторов. + +В каждом прогоне 45 с нагрузки: на 10-й секунде реплику останавливаю, на 25-й запускаю обратно. Ошибка — любой ответ не 2xx, включая обрыв. Сводки считает `scripts/load/summarize.py`. Время «LB вывел» и «LB вернул» берётся из лога HAProxy, «получила трафик» — по `X-Instance-Id` в ответах. Каждый вариант прогнан один раз, повторов нет. + +Гипотеза: благодаря drain плавная остановка реплики проходит без ошибок, а падение процесса стоит только задержки до 1 с (таймаут подключения), но не ошибок, потому что HAProxy повторяет неудавшееся подключение на другой реплике. | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| `docker stop` api-1 (SIGTERM) под 150 RPS | 0 ошибок, реплика выходит из ротации за ≤ 2 с | 6 750 запросов, 0 ошибок, 150 RPS, p99 48 мс; последний ответ остановленной реплики через 1,07 с, HAProxy пометил DOWN через 1,08 с | вернувшаяся реплика получила трафик через 3,8 с после `docker start`; потерь нет | +| `docker stop` api-2 | то же | 6 743 запроса, 0 ошибок, 149,8 RPS, p99 162 мс; DOWN через 1,69 с | трафик через 2,4 с; потерь нет | +| `docker kill` api-1 (SIGKILL, падение) | 0 ошибок, задержка до 1 с у части запросов | 6 726 запросов, 0 ошибок, 149,5 RPS, p99 1 004 мс; DOWN через 1,74 с | трафик через 3,9 с; потерь нет | +| `docker kill` api-2 | то же | 6 722 запроса, 0 ошибок, 149,4 RPS, p99 1 004 мс; DOWN через 1,92 с | трафик через 3,9 с; потерь нет | +| Rolling update двух реплик под 150 RPS | 0 ошибок | 10 501 запрос, 0 ошибок, p99 11,7 мс, max 625 мс; замена двух реплик заняла 26 с | — | +| `make test-failover`, api-1 и api-2 | ≤ 10 с до записи, без дублей | PASS, 0 неудачных из ~121; `first_recovery_seconds` 5,1–5,4 | это время самого `docker stop` (drain 3 с + остановка), записи всё это время шли через вторую реплику | +| `scripts/restart.sh` (lb, api, redis, postgres) | данные и токен на месте | PASS, у всех контейнеров новый `StartedAt` | тест сам ждёт готовности и проверяет данные тем же токеном; потерь нет | + +Что из этого следует: +- **SIGTERM.** Drain работает: реплика получает запросы ещё 1–1,7 с после SIGTERM, а сокет закрывается только через 3 с. Начатых запросов, которые обрывались бы, нет. +- **SIGKILL.** Ошибок тоже нет, но в первые 1–2 с запросы к мёртвой реплике ждут таймаут подключения 1 с и уходят на живую. Отсюда p99 около 1 с. Адреса упавшего контейнера в сети больше нет, поэтому отказ в соединении (RST) не приходит — только таймаут. +- **Обратный ввод в ротацию.** Сначала нужно 2–2,5 с на старт JVM и Ktor, потом 2 успешные проверки (≈2 с). +- **Один прогон на вариант.** Число ошибок (0) от повторов, скорее всего, не изменится, а p99 и времена — оценка, а не распределение. +- **Измерение.** k6 запускался на той же машине. Хвосты p99 в «спокойных» прогонах (до 160 мс) похожи на паузы Docker Desktop и GC, а не на работу балансировщика. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- **Выдерживается** остановка или падение одной API-реплики, в том числе под нагрузкой, и поочерёдная замена всех реплик. **Не выдерживается** отказ `lb`, `postgres` или всего хоста: они в одном экземпляре на одной машине. Это единые точки отказа, HA для них в этой работе не заявляю. Отказ Redis, как и в lab2, не ломает API: запросы идут в PostgreSQL. +- **Хвост задержки при падении.** При внезапном падении реплики p99 около 1 с из-за `timeout connect 1s`. В сети Docker таймаут подключения можно сделать 200–300 мс, это должно сократить хвост, но я это не проверял. +- **Недоверие к кэшу после сбоя Redis** (из lab2) живёт в памяти одной реплики. Если logout пришёлся на сбой Redis, реплика, которая сама ошибку Redis не видела, может до 60 с отдавать отозванную сессию из кэша. С двумя репликами за одним Redis это маловероятно: ошибку Redis увидят обе. Но строгой гарантии нет, нужен общий признак (например, номер поколения кэша в PostgreSQL). +- **Single-flight** (защита от одновременных промахов кэша) работает внутри процесса: на N реплик до N запросов к базе на один токен. +- **`make test-failover` всегда выбирает api-1** при одинаковом числе запросов. Вторую реплику проверяю после одного лишнего запроса, это описано в разделе запуска. +- **Без очистки.** Старые ключи идемпотентности (старше 24 ч) удаляются только при повторе того же ключа, а не отдельной задачей. Таблица растёт на одну строку на сообщение; для учебного объёма это нормально, в продакшене нужна периодическая чистка по `created_at`. Просроченные сессии тоже не удаляются (как в lab2). +- **Ограничения rolling update.** + - Скрипт для Compose, не Swarm. + - Совместимость новой версии со старой держится на правиле «миграции только расширяют схему», автоматически это не проверяется. + - После обновления имена контейнеров `api-3`/`api-4`. +- **HAProxy** рассчитан на 4 реплики (`server-template api 4`), для большего числа нужно поменять число в конфиге. diff --git a/compose.yaml b/compose.yaml index 163a6261..4fd5074c 100644 --- a/compose.yaml +++ b/compose.yaml @@ -4,7 +4,6 @@ services: postgres: image: postgres:17.6-alpine3.22 - # 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: POSTGRES_DB: messenger @@ -20,6 +19,11 @@ services: timeout: 3s retries: 10 start_period: 10s + deploy: + resources: + limits: + cpus: "1.0" + memory: 1g restart: unless-stopped redis: @@ -44,6 +48,11 @@ services: timeout: 3s retries: 10 start_period: 5s + deploy: + resources: + limits: + cpus: "0.5" + memory: 128m restart: unless-stopped # One-shot: applies Flyway migrations once and exits. Replicas start only after it succeeded, @@ -53,7 +62,7 @@ services: image: mtusi-messenger:lab3 command: ["migrate"] healthcheck: - disable: true + test: ["NONE"] depends_on: postgres: condition: service_healthy @@ -65,6 +74,11 @@ services: DB_PASSWORD_FILE: /run/secrets/db_password secrets: - db_password + deploy: + resources: + limits: + cpus: "0.5" + memory: 384m restart: "no" api: @@ -73,6 +87,10 @@ services: # Interchangeable replicas: no container_name and no published port, traffic only through `lb`. deploy: replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: "1.0" + memory: 512m depends_on: migrate: condition: service_completed_successfully @@ -95,11 +113,13 @@ services: SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} SESSION_CACHE_SINGLE_FLIGHT: ${SESSION_CACHE_SINGLE_FLIGHT:-true} IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} + SHUTDOWN_DRAIN_DELAY_MS: "3000" secrets: - db_password - redis_password - # SIGTERM: Ktor stops accepting, finishes requests in flight (shutdownGracePeriod 3 s, shutdownTimeout 10 s). - stop_grace_period: 15s + # SIGTERM: readiness 503 and 3 s drain (HAProxy needs <= 2 s to notice), then Ktor closes the socket and + # finishes requests in flight within 5 s. 3 + 5 = 8 s < 10 s before Docker sends SIGKILL. + stop_grace_period: 10s restart: unless-stopped lb: @@ -111,6 +131,17 @@ services: condition: service_healthy ports: - "127.0.0.1:8080:8080" + # Port 8404 (health and stats) stays inside the compose network. + healthcheck: + test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8404/healthz"] + interval: 5s + timeout: 2s + retries: 3 + deploy: + resources: + limits: + cpus: "0.5" + memory: 128m restart: unless-stopped secrets: diff --git a/evidence/clean-start.txt b/evidence/clean-start.txt new file mode 100644 index 00000000..0fe8d405 --- /dev/null +++ b/evidence/clean-start.txt @@ -0,0 +1,20 @@ +# clean copy of the working tree (git ls-files -co --exclude-standard), fresh secrets and volume, COMPOSE_PROJECT_NAME=lab3-clean +# 2026-10-23T12:49Z, base commit 92c2fdc + uncommitted lab3 grade-4 changes +$ ./scripts/init-secrets.sh && docker compose up -d --build --wait && make check && make test && make test-persistence ACTION_SCRIPT=scripts/restart.sh && make test-failover ACTION_SCRIPT=scripts/stop-one.sh +created secrets/db_password +created secrets/redis_password +config ok + Container lab3-clean-api-2 Healthy + Container lab3-clean-api-1 Healthy + Container lab3-clean-lb-1 Healthy +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +{"observed_instances_before": 2, "successful_writes": 125, "failed_attempts": 0, "first_recovery_seconds": 4.859} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. +lab3-clean-api-1 running +lab3-clean-api-2 exited +lab3-clean-lb-1 running +lab3-clean-migrate-1 exited +lab3-clean-postgres-1 running +lab3-clean-redis-1 running diff --git a/evidence/failover-api-1.txt b/evidence/failover-api-1.txt index 3ef92aef..4a54361b 100644 --- a/evidence/failover-api-1.txt +++ b/evidence/failover-api-1.txt @@ -1,24 +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) +mtusi-messenger-lab3-api-1 d1ff13561537 running Up 24 seconds (healthy) +mtusi-messenger-lab3-api-2 ac9723acebaa running Up 24 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} +{"observed_instances_before": 2, "successful_writes": 121, "failed_attempts": 0, "first_recovery_seconds": 5.385} 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) +mtusi-messenger-lab3-api-1 d1ff13561537 exited Exited (143) 9 seconds ago +mtusi-messenger-lab3-api-2 ac9723acebaa running Up 40 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 ). +172.24.0.1:57032 [23/Oct/2026:11:37:38.388] http api/api2 0/0/0/97/97 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:58552 [23/Oct/2026:11:37:53.795] http api/api2 0/0/0/86/86 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:58572 [23/Oct/2026:11:37:53.942] http api/api2 0/0/0/78/78 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:58586 [23/Oct/2026:11:37:54.055] http api/api2 0/0/0/81/81 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 5ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +[WARNING] (8) : Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 5ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. [WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ). +Server api/api2 was DOWN and now enters maintenance (No IP for server ). diff --git a/evidence/failover-api-2.txt b/evidence/failover-api-2.txt index 232bad25..8e1cc861 100644 --- a/evidence/failover-api-2.txt +++ b/evidence/failover-api-2.txt @@ -1,24 +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) +mtusi-messenger-lab3-api-1 d1ff13561537 running Up 8 seconds (healthy) +mtusi-messenger-lab3-api-2 ac9723acebaa 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} +{"observed_instances_before": 2, "successful_writes": 121, "failed_attempts": 0, "first_recovery_seconds": 5.125} 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 +mtusi-messenger-lab3-api-1 d1ff13561537 running Up 24 seconds (healthy) +mtusi-messenger-lab3-api-2 ac9723acebaa exited Exited (143) 10 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/api2 is UP, reason: Layer7 check passed, code: 200, check duration: 9ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue. +172.24.0.1:64862 [23/Oct/2026:11:38:51.915] http api/api1 0/0/0/69/69 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:64886 [23/Oct/2026:11:38:52.074] http api/api1 0/0/0/26/26 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:64892 [23/Oct/2026:11:38:52.179] http api/api1 0/0/0/24/24 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +[WARNING] (8) : Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 3ms. 1 active and 0 backup servers left. 1 sessions active, 0 requeued, 0 remaining in queue. +Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 3ms. 1 active and 0 backup servers left. 1 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 ). diff --git a/evidence/gradle-tests.txt b/evidence/gradle-tests.txt index 7411d123..7bba1aa6 100644 --- a/evidence/gradle-tests.txt +++ b/evidence/gradle-tests.txt @@ -1,7 +1,7 @@ # ./gradlew test (Testcontainers: postgres:17.6-alpine3.22, redis:8.4.0-alpine) -total: 66 tests, 0 failed +total: 68 tests, 0 failed -infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 7.774 s +infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 7.132 s - logout revokes only the current token - Bearer token cannot open a session - password containing a colon and non-ASCII characters works @@ -11,19 +11,19 @@ infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, - 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 +infrastructure.controllers.chat.ChatControllerTest: 6 tests, 0 failed, 2.173 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 +infrastructure.controllers.health.HealthControllerTest: 2 tests, 0 failed, 0.127 s - live returns 200 - ready returns 200 when database is reachable -infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.335 s +infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.285 s - parallel senders produce exactly N unique messages -infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.946 s +infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.826 s - foreign chat returns 404 when sending - last page has null next_cursor - pagination walks all messages exactly once @@ -31,7 +31,7 @@ infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.9 - 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 +infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 3.269 s - expired key can be used for a new message - key scope is user and chat - same key with another body is a conflict @@ -39,7 +39,7 @@ infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 3. - 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 +infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 1.668 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 @@ -51,34 +51,37 @@ infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 1.638 s - 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 +infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: 3 tests, 0 failed, 0.091 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 +infrastructure.repositories.chat.ExposedChatRepositoryTest: 2 tests, 0 failed, 0.498 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 +infrastructure.services.authentication.SessionLookupSingleFlightTest: 4 tests, 0 failed, 1.122 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 +infrastructure.services.hashing.DefaultHashingServiceTest: 4 tests, 0 failed, 0.418 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 +plugins.CorsTest: 2 tests, 0 failed, 0.119 s + - page of the API itself may call it + - unknown origin is rejected +plugins.DatabaseMigrationTest: 1 tests, 0 failed, 0.188 s - re-running migrations applies nothing and keeps data -plugins.DependencyFailureTest: 5 tests, 0 failed, 13.326 s +plugins.DependencyFailureTest: 5 tests, 0 failed, 13.637 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 +plugins.InstanceIdTest: 1 tests, 0 failed, 0.245 s - every response names the replica -plugins.SessionCacheTest: 3 tests, 0 failed, 0.928 s +plugins.SessionCacheTest: 3 tests, 0 failed, 0.953 s - emptied cache falls back to PostgreSQL - first authenticated request fills the cache - logout leaves a tombstone and the token is rejected diff --git a/evidence/idempotency-race.txt b/evidence/idempotency-race.txt index 2720d6fa..2aa14a6d 100644 --- a/evidence/idempotency-race.txt +++ b/evidence/idempotency-race.txt @@ -6,8 +6,8 @@ $ python3 scripts/idempotency-race.py "201": 640 }, "requests_per_instance": { - "236cb759e825": 320, - "787a253aa6a6": 320 + "ac9723acebaa": 320, + "d1ff13561537": 320 }, "rounds_with_more_than_one_message_id": 0, "messages_in_history": 20, diff --git a/evidence/instances.txt b/evidence/instances.txt index 440fc6ba..489d5fc8 100644 --- a/evidence/instances.txt +++ b/evidence/instances.txt @@ -1,21 +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 +mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 running Up 10 seconds (healthy) 8080/tcp +mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 running Up 9 seconds (healthy) 8080/tcp +mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine running Up 7 seconds (healthy) 127.0.0.1:8080->8080/tcp +mtusi-messenger-lab3-migrate-1 mtusi-messenger:lab3 exited Exited (0) 10 seconds ago +mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 running Up 20 seconds (healthy) 5432/tcp +mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine running Up 20 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: ac9723acebaa +x-instance-id: d1ff13561537 +x-instance-id: ac9723acebaa +x-instance-id: d1ff13561537 +x-instance-id: ac9723acebaa +x-instance-id: d1ff13561537 # 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) +mtusi-messenger-lab3-api-1 d1ff13561537 running Up 10 seconds (healthy) +mtusi-messenger-lab3-api-2 ac9723acebaa running Up 10 seconds (healthy) diff --git a/evidence/limits.txt b/evidence/limits.txt new file mode 100644 index 00000000..cf52a9dd --- /dev/null +++ b/evidence/limits.txt @@ -0,0 +1,44 @@ +$ docker compose ps +NAME IMAGE STATUS PORTS +mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 Up 57 seconds (healthy) 8080/tcp +mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 Up 57 seconds (healthy) 8080/tcp +mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine Up 54 seconds (healthy) 127.0.0.1:8080->8080/tcp +mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 Up About a minute (healthy) 5432/tcp +mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine Up About a minute (healthy) 6379/tcp + +# limits (NanoCpus / Memory) from docker inspect +/mtusi-messenger-lab3-api-1 cpus=1000000000 memory=536870912 restart=unless-stopped +/mtusi-messenger-lab3-api-2 cpus=1000000000 memory=536870912 restart=unless-stopped +/mtusi-messenger-lab3-lb-1 cpus=500000000 memory=134217728 restart=unless-stopped +/mtusi-messenger-lab3-postgres-1 cpus=1000000000 memory=1073741824 restart=unless-stopped +/mtusi-messenger-lab3-redis-1 cpus=500000000 memory=134217728 restart=unless-stopped + +$ docker stats --no-stream +NAME CPU % MEM USAGE / LIMIT +mtusi-messenger-lab3-lb-1 0.21% 13.65MiB / 128MiB +mtusi-messenger-lab3-api-1 2.36% 183.7MiB / 512MiB +mtusi-messenger-lab3-api-2 1.42% 184.4MiB / 512MiB +mtusi-messenger-lab3-postgres-1 0.13% 67.29MiB / 1GiB +mtusi-messenger-lab3-redis-1 0.26% 12.44MiB / 128MiB + +# published ports: only the balancer +mtusi-messenger-lab3-api-1 [{ 8080 0 tcp}] +mtusi-messenger-lab3-api-2 [{ 8080 0 tcp}] +mtusi-messenger-lab3-lb-1 [{127.0.0.1 8080 8080 tcp}] +mtusi-messenger-lab3-postgres-1 [{ 5432 0 tcp}] +mtusi-messenger-lab3-redis-1 [{ 6379 0 tcp}] + +# secrets per service +api ['db_password', 'redis_password'] +lb [] +migrate ['db_password'] +postgres ['db_password'] +redis ['redis_password'] + +# HAProxy backend table +# pxname,svname,status,check_status +api,api1,UP,L7OK +api,api2,UP,L7OK +api,api3,MAINT (resolution), +api,api4,MAINT (resolution), +api,BACKEND,UP, diff --git a/evidence/load/README.md b/evidence/load/README.md new file mode 100644 index 00000000..a21780fe --- /dev/null +++ b/evidence/load/README.md @@ -0,0 +1,20 @@ +# Нагрузочные прогоны + +Дата: 2026-10-23. Код: commit 92c2fdc + незакоммиченные изменения уровня 4 (тот же код, что в PR). + +Стенд: Apple M4 (10 ядер), 16 ГБ, macOS 27; Docker Desktop 28.4 (VM 10 CPU, 8 ГБ), Compose 2.39; k6 2.3 на той же машине. +Лимиты контейнеров — как в `compose.yaml`: api 1 CPU / 512 МБ ×2, postgres 1 CPU / 1 ГБ, redis 0.5 CPU / 128 МБ, lb 0.5 CPU / 128 МБ. + +Профиль (`scripts/load/mixed.js`): открытая модель, 150 запросов/с через `localhost:8080`, 10 пользователей, один общий чат. +70 % `GET /api/v1/users/me`, 30 % `POST /api/v1/chats/{id}/messages` с уникальным `Idempotency-Key`. Таймаут запроса 5 с, повторов нет. +Ошибка — любой ответ не 2xx, включая status 0 (обрыв, таймаут). + +| Каталог | Команда | Что происходит | +| --- | --- | --- | +| `failover-stop-api-1`, `failover-stop-api-2` | `MODE=stop TARGET=api-N scripts/load/failover-under-load.sh` | 45 с нагрузки, на 10-й с `docker stop` реплики (SIGTERM), на 25-й `docker start` | +| `failover-kill-api-1`, `failover-kill-api-2` | `MODE=kill TARGET=api-N scripts/load/failover-under-load.sh` | то же, но `docker kill` (SIGKILL, падение процесса) | +| `rolling-update` | `scripts/load/rolling-under-load.sh` | 70 с нагрузки, с 10-й с `scripts/rolling-update.sh` | + +В каждом каталоге: `k6.txt` (итог k6), `events.json` (моменты действий, epoch ms), `haproxy.log` (смены состояния backend и ответы 5xx), +`per-second.csv` (по секундам: успешные, ошибки, p99, max, сколько ответила каждая реплика), `summary.json` (итог, считает `scripts/load/summarize.py`). +Сырые данные по каждому запросу в Git не кладутся; `KEEP_RAW=1` оставляет `requests.csv` локально. diff --git a/evidence/load/failover-kill-api-1/events.json b/evidence/load/failover-kill-api-1/events.json new file mode 100644 index 00000000..41dbbbd7 --- /dev/null +++ b/evidence/load/failover-kill-api-1/events.json @@ -0,0 +1,2 @@ +{"mode": "kill", "target": "api-1", "target_instance": "7b89cf61597b", "restored_instance": "7b89cf61597b", + "load_start": 1792755242253, "fault": 1792755252286, "fault_done": 1792755252443, "restore_begin": 1792755267305, "restore_healthy": 1792755269813} diff --git a/evidence/load/failover-kill-api-1/k6.txt b/evidence/load/failover-kill-api-1/k6.txt new file mode 100644 index 00000000..e6635170 --- /dev/null +++ b/evidence/load/failover-kill-api-1/k6.txt @@ -0,0 +1,26 @@ + + + █ TOTAL RESULTS + + CUSTOM + served_by......................: 6726 146.724721/s + + HTTP + http_req_duration..............: avg=24.62ms med=1.73ms p(95)=6.23ms p(99)=1s max=1.01s + { expected_response:true }...: avg=24.62ms med=1.73ms p(95)=6.23ms p(99)=1s max=1.01s + http_req_failed................: 0.00% 0 out of 6747 + http_reqs......................: 6747 147.182826/s + + EXECUTION + dropped_iterations.............: 25 0.545364/s + iteration_duration.............: avg=24.77ms med=1.92ms p(95)=6.16ms p(99)=1s max=1.01s + iterations.....................: 6726 146.724721/s + vus............................: 0 min=0 max=66 + vus_max........................: 75 min=50 max=75 + + NETWORK + data_received..................: 2.6 MB 57 kB/s + data_sent......................: 1.3 MB 29 kB/s + + + diff --git a/evidence/load/failover-kill-api-1/per-second.csv b/evidence/load/failover-kill-api-1/per-second.csv new file mode 100644 index 00000000..8e5d98b5 --- /dev/null +++ b/evidence/load/failover-kill-api-1/per-second.csv @@ -0,0 +1,46 @@ +second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b +0,151,0,11.9,12.3,75,76 +1,149,0,5.9,6.7,75,74 +2,150,0,9.8,10.5,75,75 +3,150,0,6.4,7.3,75,75 +4,150,0,7.0,7.9,75,75 +5,151,0,5.8,7.8,75,76 +6,150,0,3.9,7.1,75,75 +7,149,0,6.2,6.5,75,74 +8,150,0,6.2,6.4,75,75 +9,151,0,6.1,7.3,75,76 +10,97,0,7.6,7.6,77,20 +11,104,0,1007.5,1009.4,104,0 +12,224,0,1006.6,1011.5,224,0 +13,150,0,7.1,15.8,150,0 +14,150,0,8.9,22.4,150,0 +15,150,0,4.3,5.1,150,0 +16,150,0,6.2,7.4,150,0 +17,150,0,5.2,6.2,150,0 +18,149,0,5.1,5.9,149,0 +19,151,0,5.4,6.8,151,0 +20,150,0,5.7,5.8,150,0 +21,150,0,5.6,5.7,150,0 +22,150,0,11.5,14.7,150,0 +23,150,0,5.7,6.0,150,0 +24,150,0,5.5,6.2,150,0 +25,150,0,34.2,43.0,150,0 +26,150,0,4.1,4.4,150,0 +27,149,0,5.9,6.1,149,0 +28,136,0,7.4,8.0,136,0 +29,164,0,714.7,777.3,75,89 +30,151,0,8.4,20.8,75,76 +31,149,0,32.0,41.6,74,75 +32,151,0,12.8,15.2,76,75 +33,150,0,5.6,5.8,75,75 +34,150,0,5.4,5.5,75,75 +35,150,0,10.5,12.2,75,75 +36,150,0,5.7,6.5,75,75 +37,150,0,5.5,5.9,75,75 +38,150,0,5.4,6.6,75,75 +39,150,0,6.4,6.5,75,75 +40,150,0,5.7,6.2,75,75 +41,149,0,5.2,5.5,74,75 +42,150,0,26.9,29.4,75,75 +43,151,0,6.9,7.1,76,75 +44,150,0,17.8,18.8,75,75 diff --git a/evidence/load/failover-kill-api-1/summary.json b/evidence/load/failover-kill-api-1/summary.json new file mode 100644 index 00000000..e6f71a3f --- /dev/null +++ b/evidence/load/failover-kill-api-1/summary.json @@ -0,0 +1,26 @@ +{ + "requests": 6726, + "ok": 6726, + "errors": 0, + "error_codes": {}, + "ok_rps": 149.5, + "p50_ms": 1.7, + "p95_ms": 5.9, + "p99_ms": 1003.5, + "max_ms": 1011.5, + "requests_per_instance": { + "7b89cf61597b": 1986, + "4fa49e9e1a53": 4740 + }, + "event_load_start_s": 0.22, + "event_fault_s": 10.26, + "event_fault_done_s": 10.41, + "event_restore_begin_s": 25.27, + "event_restore_healthy_s": 27.78, + "errors_after_fault": 0, + "last_error_after_fault_s": null, + "target_last_response_after_fault_s": 0.01, + "restored_replica_first_response_after_start_s": 3.85, + "lb_marked_down_after_fault_s": 1.74, + "lb_marked_up_after_start_s": 3.52 +} \ No newline at end of file diff --git a/evidence/load/failover-kill-api-2/events.json b/evidence/load/failover-kill-api-2/events.json new file mode 100644 index 00000000..23365925 --- /dev/null +++ b/evidence/load/failover-kill-api-2/events.json @@ -0,0 +1,2 @@ +{"mode": "kill", "target": "api-2", "target_instance": "4fa49e9e1a53", "restored_instance": "4fa49e9e1a53", + "load_start": 1792755292111, "fault": 1792755302165, "fault_done": 1792755302329, "restore_begin": 1792755317163, "restore_healthy": 1792755319645} diff --git a/evidence/load/failover-kill-api-2/k6.txt b/evidence/load/failover-kill-api-2/k6.txt new file mode 100644 index 00000000..d9541d25 --- /dev/null +++ b/evidence/load/failover-kill-api-2/k6.txt @@ -0,0 +1,26 @@ + + + █ TOTAL RESULTS + + CUSTOM + served_by......................: 6722 146.537002/s + + HTTP + http_req_duration..............: avg=25.2ms med=1.69ms p(95)=5.68ms p(99)=1s max=1.01s + { expected_response:true }...: avg=25.2ms med=1.69ms p(95)=5.68ms p(99)=1s max=1.01s + http_req_failed................: 0.00% 0 out of 6743 + http_reqs......................: 6743 146.994794/s + + EXECUTION + dropped_iterations.............: 29 0.632189/s + iteration_duration.............: avg=25.35ms med=1.88ms p(95)=5.77ms p(99)=1s max=1.01s + iterations.....................: 6722 146.537002/s + vus............................: 1 min=0 max=78 + vus_max........................: 79 min=50 max=79 + + NETWORK + data_received..................: 2.6 MB 57 kB/s + data_sent......................: 1.3 MB 29 kB/s + + + diff --git a/evidence/load/failover-kill-api-2/per-second.csv b/evidence/load/failover-kill-api-2/per-second.csv new file mode 100644 index 00000000..d811ae40 --- /dev/null +++ b/evidence/load/failover-kill-api-2/per-second.csv @@ -0,0 +1,46 @@ +second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b +0,151,0,4.9,6.7,76,75 +1,150,0,6.1,6.7,75,75 +2,150,0,8.3,9.0,75,75 +3,150,0,5.8,6.2,75,75 +4,150,0,5.5,6.4,75,75 +5,150,0,5.4,5.7,75,75 +6,150,0,5.4,6.1,75,75 +7,150,0,5.5,6.1,75,75 +8,150,0,6.8,7.2,75,75 +9,150,0,7.0,7.8,75,75 +10,96,0,9.1,9.1,19,77 +11,104,0,1010.4,1012.0,0,104 +12,209,0,1006.0,1006.3,0,209 +13,162,0,1004.9,1005.5,0,162 +14,149,0,5.8,6.0,0,149 +15,151,0,3.7,3.8,0,151 +16,150,0,4.6,6.1,0,150 +17,150,0,5.4,6.2,0,150 +18,150,0,5.5,7.0,0,150 +19,150,0,6.0,7.1,0,150 +20,149,0,5.8,7.3,0,149 +21,151,0,6.5,6.9,0,151 +22,150,0,5.5,5.7,0,150 +23,149,0,6.4,6.6,0,149 +24,151,0,11.2,13.3,0,151 +25,150,0,11.7,14.0,0,150 +26,150,0,4.3,5.2,0,150 +27,150,0,6.2,6.2,0,150 +28,133,0,6.0,7.3,0,133 +29,167,0,694.4,717.1,92,75 +30,150,0,5.9,7.7,75,75 +31,148,0,8.0,33.2,74,74 +32,152,0,6.2,23.0,76,76 +33,150,0,5.5,6.5,75,75 +34,150,0,29.7,35.8,75,75 +35,150,0,4.9,6.4,75,75 +36,150,0,5.1,5.2,75,75 +37,150,0,4.9,5.2,75,75 +38,150,0,4.9,5.7,75,75 +39,149,0,5.3,5.7,75,74 +40,151,0,5.9,6.7,75,76 +41,149,0,4.4,4.4,75,74 +42,151,0,9.9,10.7,75,76 +43,150,0,8.5,8.9,75,75 +44,150,0,5.5,6.2,75,75 diff --git a/evidence/load/failover-kill-api-2/summary.json b/evidence/load/failover-kill-api-2/summary.json new file mode 100644 index 00000000..e3c7eba0 --- /dev/null +++ b/evidence/load/failover-kill-api-2/summary.json @@ -0,0 +1,26 @@ +{ + "requests": 6722, + "ok": 6722, + "errors": 0, + "error_codes": {}, + "ok_rps": 149.4, + "p50_ms": 1.7, + "p95_ms": 5.4, + "p99_ms": 1003.5, + "max_ms": 1012.0, + "requests_per_instance": { + "4fa49e9e1a53": 1987, + "7b89cf61597b": 4735 + }, + "event_load_start_s": 0.18, + "event_fault_s": 10.23, + "event_fault_done_s": 10.4, + "event_restore_begin_s": 25.23, + "event_restore_healthy_s": 27.71, + "errors_after_fault": 0, + "last_error_after_fault_s": null, + "target_last_response_after_fault_s": 0.02, + "restored_replica_first_response_after_start_s": 3.86, + "lb_marked_down_after_fault_s": 1.92, + "lb_marked_up_after_start_s": 3.54 +} \ No newline at end of file diff --git a/evidence/load/failover-stop-api-1/events.json b/evidence/load/failover-stop-api-1/events.json new file mode 100644 index 00000000..1018d8e1 --- /dev/null +++ b/evidence/load/failover-stop-api-1/events.json @@ -0,0 +1,2 @@ +{"mode": "stop", "target": "api-1", "target_instance": "7b89cf61597b", "restored_instance": "7b89cf61597b", + "load_start": 1792755143191, "fault": 1792755153238, "fault_done": 1792755158479, "restore_begin": 1792755168242, "restore_healthy": 1792755170683} diff --git a/evidence/load/failover-stop-api-1/k6.txt b/evidence/load/failover-stop-api-1/k6.txt new file mode 100644 index 00000000..3a89b661 --- /dev/null +++ b/evidence/load/failover-stop-api-1/k6.txt @@ -0,0 +1,25 @@ + + + █ TOTAL RESULTS + + CUSTOM + served_by......................: 6750 143.513142/s + + HTTP + http_req_duration..............: avg=5.21ms med=2.03ms p(95)=5.28ms p(99)=66.29ms max=608.68ms + { expected_response:true }...: avg=5.21ms med=2.03ms p(95)=5.28ms p(99)=66.29ms max=608.68ms + http_req_failed................: 0.00% 0 out of 6771 + http_reqs......................: 6771 143.959628/s + + EXECUTION + iteration_duration.............: avg=5.13ms med=2.21ms p(95)=5.47ms p(99)=48.43ms max=608.87ms + iterations.....................: 6750 143.513142/s + vus............................: 2 min=0 max=26 + vus_max........................: 50 min=50 max=50 + + NETWORK + data_received..................: 2.6 MB 56 kB/s + data_sent......................: 1.4 MB 29 kB/s + + + diff --git a/evidence/load/failover-stop-api-1/per-second.csv b/evidence/load/failover-stop-api-1/per-second.csv new file mode 100644 index 00000000..48d500a8 --- /dev/null +++ b/evidence/load/failover-stop-api-1/per-second.csv @@ -0,0 +1,46 @@ +second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b +0,153,0,205.2,220.8,77,76 +1,150,0,7.0,7.7,75,75 +2,150,0,7.3,16.6,75,75 +3,150,0,5.6,6.1,75,75 +4,150,0,6.3,6.6,75,75 +5,150,0,30.2,41.5,75,75 +6,150,0,7.4,7.8,75,75 +7,150,0,7.0,7.3,75,75 +8,150,0,58.5,73.3,75,75 +9,150,0,5.7,7.0,75,75 +10,150,0,5.7,7.6,75,75 +11,150,0,5.8,6.1,75,75 +12,150,0,25.5,29.9,134,16 +13,150,0,5.8,6.6,150,0 +14,150,0,4.9,5.5,150,0 +15,150,0,4.6,5.3,150,0 +16,150,0,11.3,14.1,150,0 +17,150,0,4.4,5.2,150,0 +18,150,0,5.0,6.8,150,0 +19,150,0,6.2,7.2,150,0 +20,150,0,6.6,6.9,150,0 +21,150,0,6.7,8.5,150,0 +22,151,0,7.8,12.3,151,0 +23,150,0,5.0,11.3,150,0 +24,149,0,20.5,27.7,149,0 +25,150,0,5.7,6.3,150,0 +26,150,0,7.8,8.4,150,0 +27,150,0,6.6,10.4,150,0 +28,151,0,4.4,8.2,151,0 +29,120,0,543.2,554.2,103,17 +30,180,0,602.9,608.7,75,105 +31,149,0,5.9,8.8,74,75 +32,150,0,18.6,31.4,75,75 +33,150,0,5.2,5.6,75,75 +34,150,0,5.9,6.0,75,75 +35,150,0,7.6,9.4,75,75 +36,150,0,4.6,4.7,75,75 +37,150,0,4.0,5.1,75,75 +38,150,0,5.5,7.1,75,75 +39,151,0,4.9,5.0,76,75 +40,149,0,7.3,7.6,74,75 +41,150,0,7.4,16.1,75,75 +42,150,0,6.8,13.7,75,75 +43,150,0,5.7,9.3,75,75 +44,147,0,45.7,53.5,74,73 diff --git a/evidence/load/failover-stop-api-1/summary.json b/evidence/load/failover-stop-api-1/summary.json new file mode 100644 index 00000000..39fd1562 --- /dev/null +++ b/evidence/load/failover-stop-api-1/summary.json @@ -0,0 +1,26 @@ +{ + "requests": 6750, + "ok": 6750, + "errors": 0, + "error_codes": {}, + "ok_rps": 150.0, + "p50_ms": 2.0, + "p95_ms": 5.2, + "p99_ms": 48.3, + "max_ms": 608.7, + "requests_per_instance": { + "7b89cf61597b": 2087, + "4fa49e9e1a53": 4663 + }, + "event_load_start_s": 1.08, + "event_fault_s": 11.13, + "event_fault_done_s": 16.37, + "event_restore_begin_s": 26.13, + "event_restore_healthy_s": 28.57, + "errors_after_fault": 0, + "last_error_after_fault_s": null, + "target_last_response_after_fault_s": 1.07, + "restored_replica_first_response_after_start_s": 3.8, + "lb_marked_down_after_fault_s": 1.08, + "lb_marked_up_after_start_s": 3.24 +} \ No newline at end of file diff --git a/evidence/load/failover-stop-api-2/events.json b/evidence/load/failover-stop-api-2/events.json new file mode 100644 index 00000000..8da27b82 --- /dev/null +++ b/evidence/load/failover-stop-api-2/events.json @@ -0,0 +1,2 @@ +{"mode": "stop", "target": "api-2", "target_instance": "4fa49e9e1a53", "restored_instance": "4fa49e9e1a53", + "load_start": 1792755193214, "fault": 1792755203249, "fault_done": 1792755207464, "restore_begin": 1792755218279, "restore_healthy": 1792755220771} diff --git a/evidence/load/failover-stop-api-2/k6.txt b/evidence/load/failover-stop-api-2/k6.txt new file mode 100644 index 00000000..3e5f1084 --- /dev/null +++ b/evidence/load/failover-stop-api-2/k6.txt @@ -0,0 +1,26 @@ + + + █ TOTAL RESULTS + + CUSTOM + served_by......................: 6743 145.744963/s + + HTTP + http_req_duration..............: avg=8.51ms med=1.69ms p(95)=5.01ms p(99)=161.63ms max=1.04s + { expected_response:true }...: avg=8.51ms med=1.69ms p(95)=5.01ms p(99)=161.63ms max=1.04s + http_req_failed................: 0.00% 0 out of 6764 + http_reqs......................: 6764 146.198862/s + + EXECUTION + dropped_iterations.............: 8 0.172914/s + iteration_duration.............: avg=8.54ms med=1.88ms p(95)=5.16ms p(99)=156.59ms max=1.04s + iterations.....................: 6743 145.744963/s + vus............................: 0 min=0 max=28 + vus_max........................: 58 min=50 max=58 + + NETWORK + data_received..................: 2.6 MB 56 kB/s + data_sent......................: 1.4 MB 29 kB/s + + + diff --git a/evidence/load/failover-stop-api-2/per-second.csv b/evidence/load/failover-stop-api-2/per-second.csv new file mode 100644 index 00000000..e0c8fef4 --- /dev/null +++ b/evidence/load/failover-stop-api-2/per-second.csv @@ -0,0 +1,46 @@ +second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b +0,151,0,5.3,5.3,76,75 +1,150,0,5.6,6.0,75,75 +2,150,0,10.9,11.6,75,75 +3,150,0,9.9,10.0,75,75 +4,150,0,10.2,12.3,75,75 +5,150,0,6.3,7.4,75,75 +6,150,0,5.7,6.3,75,75 +7,150,0,8.4,16.2,75,75 +8,149,0,6.0,7.9,74,75 +9,151,0,4.2,5.4,76,75 +10,150,0,4.3,4.4,75,75 +11,150,0,8.1,12.1,75,75 +12,150,0,6.8,10.4,44,106 +13,150,0,5.7,5.9,0,150 +14,150,0,5.6,6.2,0,150 +15,150,0,4.9,5.9,0,150 +16,150,0,4.4,4.9,0,150 +17,150,0,4.4,4.8,0,150 +18,150,0,5.9,6.1,0,150 +19,150,0,5.7,10.8,0,150 +20,150,0,5.8,5.8,0,150 +21,149,0,6.2,7.7,0,149 +22,150,0,8.5,11.4,0,150 +23,151,0,5.0,5.4,0,151 +24,150,0,5.1,5.1,0,150 +25,150,0,6.0,8.8,0,150 +26,150,0,4.9,6.9,0,150 +27,119,0,4.0,5.5,0,119 +28,172,0,1038.9,1043.4,101,71 +29,151,0,53.1,64.8,76,75 +30,150,0,6.5,7.6,75,75 +31,150,0,6.1,7.2,75,75 +32,150,0,7.6,8.6,75,75 +33,150,0,5.7,11.9,75,75 +34,150,0,6.1,8.2,75,75 +35,150,0,7.8,10.4,75,75 +36,150,0,5.1,5.1,75,75 +37,150,0,5.0,6.6,75,75 +38,150,0,7.3,8.7,75,75 +39,150,0,5.8,5.8,75,75 +40,150,0,5.3,5.5,75,75 +41,150,0,4.2,4.6,75,75 +42,150,0,10.8,11.4,75,75 +43,150,0,5.5,5.6,75,75 +44,150,0,7.1,8.6,75,75 diff --git a/evidence/load/failover-stop-api-2/summary.json b/evidence/load/failover-stop-api-2/summary.json new file mode 100644 index 00000000..35a7c355 --- /dev/null +++ b/evidence/load/failover-stop-api-2/summary.json @@ -0,0 +1,26 @@ +{ + "requests": 6743, + "ok": 6743, + "errors": 0, + "error_codes": {}, + "ok_rps": 149.8, + "p50_ms": 1.7, + "p95_ms": 4.9, + "p99_ms": 161.6, + "max_ms": 1043.4, + "requests_per_instance": { + "4fa49e9e1a53": 2247, + "7b89cf61597b": 4496 + }, + "event_load_start_s": 0.87, + "event_fault_s": 10.9, + "event_fault_done_s": 15.12, + "event_restore_begin_s": 25.93, + "event_restore_healthy_s": 28.43, + "errors_after_fault": 0, + "last_error_after_fault_s": null, + "target_last_response_after_fault_s": 1.68, + "restored_replica_first_response_after_start_s": 2.41, + "lb_marked_down_after_fault_s": 1.69, + "lb_marked_up_after_start_s": null +} \ No newline at end of file diff --git a/evidence/load/rolling-update/events.json b/evidence/load/rolling-update/events.json new file mode 100644 index 00000000..0cf24cf1 --- /dev/null +++ b/evidence/load/rolling-update/events.json @@ -0,0 +1 @@ +{"load_start": 1792755359464, "rolling_begin": 1792755369523, "rolling_end": 1792755395558} diff --git a/evidence/load/rolling-update/k6.txt b/evidence/load/rolling-update/k6.txt new file mode 100644 index 00000000..41182e99 --- /dev/null +++ b/evidence/load/rolling-update/k6.txt @@ -0,0 +1,25 @@ + + + █ TOTAL RESULTS + + CUSTOM + served_by......................: 10501 148.101961/s + + HTTP + http_req_duration..............: avg=3.73ms med=1.88ms p(95)=4.77ms p(99)=14.85ms max=625.43ms + { expected_response:true }...: avg=3.73ms med=1.88ms p(95)=4.77ms p(99)=14.85ms max=625.43ms + http_req_failed................: 0.00% 0 out of 10522 + http_reqs......................: 10522 148.398137/s + + EXECUTION + iteration_duration.............: avg=3.84ms med=2.06ms p(95)=4.98ms p(99)=12ms max=625.53ms + iterations.....................: 10501 148.101961/s + vus............................: 0 min=0 max=27 + vus_max........................: 50 min=50 max=50 + + NETWORK + data_received..................: 4.1 MB 57 kB/s + data_sent......................: 2.1 MB 30 kB/s + + + diff --git a/evidence/load/rolling-update/per-second.csv b/evidence/load/rolling-update/per-second.csv new file mode 100644 index 00000000..057e1193 --- /dev/null +++ b/evidence/load/rolling-update/per-second.csv @@ -0,0 +1,71 @@ +second,ok,errors,p99_ms,max_ms,1239a2b734c0,4fa49e9e1a53,7b89cf61597b,d23287559687 +0,151,0,5.5,9.0,0,75,76,0 +1,150,0,5.1,7.0,0,75,75,0 +2,150,0,9.8,10.3,0,75,75,0 +3,150,0,5.7,6.4,0,75,75,0 +4,150,0,6.9,7.0,0,75,75,0 +5,150,0,6.3,9.4,0,75,75,0 +6,150,0,4.6,6.8,0,75,75,0 +7,150,0,5.6,6.0,0,75,75,0 +8,150,0,17.0,22.8,0,75,75,0 +9,150,0,7.7,9.3,0,75,75,0 +10,150,0,4.6,5.3,0,75,75,0 +11,150,0,4.5,6.2,0,75,75,0 +12,150,0,4.3,4.4,0,75,75,0 +13,150,0,6.1,10.7,0,75,75,0 +14,150,0,17.3,25.9,0,75,75,0 +15,150,0,9.2,16.0,0,75,75,0 +16,150,0,4.2,4.5,0,75,75,0 +17,149,0,13.0,21.3,0,75,74,0 +18,151,0,13.5,13.8,0,75,76,0 +19,150,0,4.6,4.6,0,75,75,0 +20,149,0,6.1,11.1,0,75,74,0 +21,129,0,8.7,11.5,0,64,65,0 +22,172,0,586.0,625.4,0,50,50,72 +23,150,0,8.1,10.9,0,68,14,68 +24,150,0,5.0,5.4,0,75,0,75 +25,150,0,7.6,12.0,0,75,0,75 +26,150,0,9.6,10.2,0,75,0,75 +27,150,0,20.6,22.0,0,75,0,75 +28,150,0,5.1,7.4,0,75,0,75 +29,150,0,24.8,27.6,0,75,0,75 +30,149,0,354.8,406.4,28,61,0,60 +31,151,0,6.2,6.3,51,50,0,50 +32,150,0,6.2,7.9,74,1,0,75 +33,150,0,5.6,6.0,75,0,0,75 +34,150,0,6.7,7.1,75,0,0,75 +35,150,0,11.7,13.0,75,0,0,75 +36,150,0,20.0,28.9,75,0,0,75 +37,150,0,5.5,6.0,75,0,0,75 +38,150,0,6.0,6.4,75,0,0,75 +39,150,0,5.5,5.8,75,0,0,75 +40,150,0,5.1,5.3,75,0,0,75 +41,150,0,24.6,37.4,75,0,0,75 +42,150,0,4.6,7.6,75,0,0,75 +43,150,0,5.0,6.1,75,0,0,75 +44,150,0,6.6,7.5,75,0,0,75 +45,150,0,7.7,11.6,75,0,0,75 +46,150,0,5.8,5.9,75,0,0,75 +47,150,0,34.1,39.4,75,0,0,75 +48,150,0,5.3,5.5,75,0,0,75 +49,150,0,5.2,6.0,75,0,0,75 +50,150,0,5.3,5.5,75,0,0,75 +51,150,0,5.7,6.7,75,0,0,75 +52,150,0,5.8,5.9,75,0,0,75 +53,150,0,5.7,6.7,75,0,0,75 +54,150,0,5.5,7.1,75,0,0,75 +55,150,0,6.5,6.7,75,0,0,75 +56,149,0,6.7,6.9,75,0,0,74 +57,151,0,8.0,11.5,75,0,0,76 +58,150,0,5.3,5.4,75,0,0,75 +59,150,0,5.6,5.7,75,0,0,75 +60,150,0,4.3,4.6,75,0,0,75 +61,150,0,5.9,6.2,75,0,0,75 +62,150,0,6.1,7.5,75,0,0,75 +63,150,0,4.5,4.8,75,0,0,75 +64,150,0,5.5,6.9,75,0,0,75 +65,150,0,7.2,7.7,75,0,0,75 +66,150,0,5.0,5.2,75,0,0,75 +67,150,0,4.2,4.9,75,0,0,75 +68,150,0,8.6,8.9,75,0,0,75 +69,150,0,5.8,7.4,75,0,0,75 diff --git a/evidence/load/rolling-update/summary.json b/evidence/load/rolling-update/summary.json new file mode 100644 index 00000000..735fae5a --- /dev/null +++ b/evidence/load/rolling-update/summary.json @@ -0,0 +1,20 @@ +{ + "requests": 10501, + "ok": 10501, + "errors": 0, + "error_codes": {}, + "ok_rps": 150.0, + "p50_ms": 1.9, + "p95_ms": 4.7, + "p99_ms": 11.7, + "max_ms": 625.4, + "requests_per_instance": { + "7b89cf61597b": 1704, + "4fa49e9e1a53": 2319, + "d23287559687": 3550, + "1239a2b734c0": 2928 + }, + "event_load_start_s": 0.33, + "event_rolling_begin_s": 10.38, + "event_rolling_end_s": 36.42 +} \ No newline at end of file diff --git a/evidence/persistence.txt b/evidence/persistence.txt index d33c8104..4ba9d2f7 100644 --- a/evidence/persistence.txt +++ b/evidence/persistence.txt @@ -1,24 +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-api-1 running Up 37 seconds (healthy) +mtusi-messenger-lab3-api-2 running Up 7 seconds (healthy) +mtusi-messenger-lab3-lb-1 running Up About a minute (healthy) +mtusi-messenger-lab3-migrate-1 exited Exited (0) 4 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 +/mtusi-messenger-lab3-api-1 StartedAt=2026-10-23T11:38:43.02725971Z +/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T11:39:12.251618502Z +/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T11:37:31.902111428Z +/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T11:37:18.99589288Z +/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T11:37:18.995508005Z $ 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-api-1 StartedAt=2026-10-23T11:39:37.089275763Z +/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T11:39:37.18606118Z +/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T11:39:39.805556042Z +/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T11:39:26.389951592Z +/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T11:39:26.38860105Z mtusi-messenger-lab3_postgres-data diff --git a/infra/haproxy/haproxy.cfg b/infra/haproxy/haproxy.cfg index b3abf039..66edd0a1 100644 --- a/infra/haproxy/haproxy.cfg +++ b/infra/haproxy/haproxy.cfg @@ -3,6 +3,8 @@ global log stdout format raw local0 info + # Without it HAProxy sizes buffers from the file descriptor limit and uses ~100 MB. + maxconn 4096 defaults mode http @@ -37,6 +39,14 @@ frontend http bind :8080 default_backend api +# Not published: container healthcheck and the backend table (wget -qO- "http://127.0.0.1:8404/stats;csv"). +frontend internal + bind :8404 + monitor-uri /healthz + stats enable + stats uri /stats + stats refresh 5s + backend api balance roundrobin option httpchk GET /health/ready diff --git a/run/_template/config/authentication.conf b/run/_template/config/authentication.conf index 8debebf3..83968198 100644 --- a/run/_template/config/authentication.conf +++ b/run/_template/config/authentication.conf @@ -1,4 +1,7 @@ authentication { realm = "messenger" sessionTtlSeconds = 3600 + sessionCacheTtlSeconds = 60 + # Collapse concurrent cache misses for one token into a single database lookup + sessionCacheSingleFlight = true } diff --git a/run/_template/config/database.conf b/run/_template/config/database.conf index 6de16a67..2f27e6f8 100644 --- a/run/_template/config/database.conf +++ b/run/_template/config/database.conf @@ -6,4 +6,7 @@ database { password = "password" maxPoolSize = 10 connectionTimeoutMs = 2000 + statementTimeoutMs = 5000 + # A single local process migrates itself; replicas in compose leave it to the `migrate` service + migrateOnStart = true } diff --git a/run/_template/config/instance.conf b/run/_template/config/instance.conf new file mode 100644 index 00000000..1029f35d --- /dev/null +++ b/run/_template/config/instance.conf @@ -0,0 +1,4 @@ +instance { + # X-Instance-Id of this process. Defaults to HOSTNAME (the short container id in Docker), else a random alias + #id = "dev" +} diff --git a/run/_template/config/messages.conf b/run/_template/config/messages.conf new file mode 100644 index 00000000..ced68696 --- /dev/null +++ b/run/_template/config/messages.conf @@ -0,0 +1,4 @@ +messages { + # How long a stored Idempotency-Key answers a retry with the original message (contract: at least 24 h) + idempotencyTtlSeconds = 86400 +} diff --git a/run/_template/config/redis.conf b/run/_template/config/redis.conf index f6a9b183..d2f835f0 100644 --- a/run/_template/config/redis.conf +++ b/run/_template/config/redis.conf @@ -1,6 +1,8 @@ -redis { + redis { host = "localhost" port = 6379 + # Only if the local Redis requires AUTH + #password = "" connectTimeoutMs = 500 commandTimeoutMs = 500 } diff --git a/scripts/load/failover-under-load.sh b/scripts/load/failover-under-load.sh new file mode 100755 index 00000000..b142f6e4 --- /dev/null +++ b/scripts/load/failover-under-load.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# One replica fault under constant load through the balancer, then its restoration. +# MODE=stop docker stop (SIGTERM: drain, graceful shutdown) MODE=kill docker kill (SIGKILL: crash) +# TARGET=api-1|api-2 RATE=150 req/s DURATION=45 s fault at 10 s, restore at 25 s +# Output: evidence/load/failover--/{k6.txt,events.json,haproxy.log,per-second.csv,summary.json} +set -eu +cd "$(dirname "$0")/../.." + +MODE=${MODE:-stop} +TARGET=${TARGET:-api-1} +RATE=${RATE:-150} +DURATION=${DURATION:-45} +FAULT_AT=${FAULT_AT:-10} +RESTORE_AT=${RESTORE_AT:-25} + +out=evidence/load/failover-$MODE-$TARGET +rm -rf "$out" && mkdir -p "$out" +now() { python3 -c 'import time; print(int(time.time() * 1000))'; } +# Sleeps until after the load started, so the schedule does not drift with slow docker commands. +sleep_until() { python3 -c "import time; time.sleep(max(0, $start / 1000 + $1 - time.time()))"; } +container=mtusi-messenger-lab3-$TARGET +target_instance=$(docker inspect -f '{{.Config.Hostname}}' "$container") + +since=$(date -u +%Y-%m-%dT%H:%M:%SZ) +K6_CSV_TIME_FORMAT=unix_milli k6 run --quiet --out csv="$out/requests.csv" \ + -e RATE="$RATE" -e DURATION="${DURATION}s" scripts/load/mixed.js > "$out/k6.txt" 2>&1 & +k6_pid=$! + +# Load starts after k6 setup (users, sessions, chat): wait for the first request of the scenario. +until grep -q ',load,' "$out/requests.csv" 2>/dev/null; do sleep 0.2; done +start=$(now) + +sleep_until "$FAULT_AT" +fault=$(now) +if [ "$MODE" = kill ]; then docker kill "$container" >/dev/null; else docker stop --time 9 "$container" >/dev/null; fi +fault_done=$(now) + +sleep_until "$RESTORE_AT" +restore_begin=$(now) +docker start "$container" >/dev/null +until [ "$(docker inspect -f '{{.State.Health.Status}}' "$container")" = healthy ]; do sleep 0.2; done +restore_healthy=$(now) +restored_instance=$(docker inspect -f '{{.Config.Hostname}}' "$container") + +wait "$k6_pid" || true +docker compose logs --timestamps --no-log-prefix --since "$since" lb 2>/dev/null | grep -E 'Server api/|SH--|5[0-9][0-9] ' > "$out/haproxy.log" || true + +cat > "$out/events.json" < /dev/null +# Raw per-request data stays out of Git (evidence keeps aggregates only); KEEP_RAW=1 keeps it for analysis. +[ "${KEEP_RAW:-0}" = 1 ] || rm -f "$out/requests.csv" +cat "$out/summary.json" diff --git a/scripts/load/mixed.js b/scripts/load/mixed.js new file mode 100644 index 00000000..1cdf57dc --- /dev/null +++ b/scripts/load/mixed.js @@ -0,0 +1,67 @@ +// Open-model load through the balancer: RATE requests per second regardless of response time. +// 70 % GET /users/me (session lookup), 30 % POST message with a unique Idempotency-Key. +// Every response adds a `served_by` sample tagged with X-Instance-Id and status, so the summary can show +// which replica answered each second. Tokens live only in this process. +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import { Counter } from 'k6/metrics'; + +const BASE = __ENV.BASE_URL || 'http://localhost:8080'; +const RATE = Number(__ENV.RATE || 150); +const DURATION = __ENV.DURATION || '40s'; +const USERS = 10; +const PASSWORD = 'load-test-password'; + +const servedBy = new Counter('served_by'); + +export const options = { + scenarios: { + load: { + executor: 'constant-arrival-rate', + rate: RATE, + timeUnit: '1s', + duration: DURATION, + preAllocatedVUs: 50, + maxVUs: 300, + }, + }, + summaryTrendStats: ['avg', 'med', 'p(95)', 'p(99)', 'max'], +}; + +export function setup() { + const json = { headers: { 'Content-Type': 'application/json' } }; + const users = []; + for (let i = 0; i < USERS; i++) { + const username = `load_${Date.now().toString(36)}_${i}`; + const user = http.post(`${BASE}/api/v1/users`, JSON.stringify({ username, display_name: 'load', password: PASSWORD }), json).json(); + const basic = encoding.b64encode(`${username}:${PASSWORD}`); + const session = http.post(`${BASE}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${basic}` } }).json(); + users.push({ id: user.id, token: session.token }); + } + const chat = http.post(`${BASE}/api/v1/chats`, JSON.stringify({ title: 'load', member_ids: users.map((u) => u.id) }), { + headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${users[0].token}` }, + }).json(); + return { users, chatId: chat.id }; +} + +export default function (data) { + const user = data.users[Math.floor(Math.random() * data.users.length)]; + const auth = { Authorization: `Bearer ${user.token}` }; + let response; + + if (Math.random() < 0.7) { + response = http.get(`${BASE}/api/v1/users/me`, { headers: auth, tags: { name: 'me' }, timeout: '5s' }); + } else { + response = http.post( + `${BASE}/api/v1/chats/${data.chatId}/messages`, + JSON.stringify({ text: `load ${__VU}-${__ITER}` }), + { + headers: { ...auth, 'Content-Type': 'application/json', 'Idempotency-Key': `load-${__VU}-${__ITER}-${Date.now()}` }, + tags: { name: 'send' }, + timeout: '5s', + }, + ); + } + + servedBy.add(1, { instance: response.headers['X-Instance-Id'] || 'none', code: String(response.status) }); +} diff --git a/scripts/load/rolling-under-load.sh b/scripts/load/rolling-under-load.sh new file mode 100755 index 00000000..b326bb04 --- /dev/null +++ b/scripts/load/rolling-under-load.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# scripts/rolling-update.sh while k6 keeps RATE req/s through the balancer. +# Output: evidence/load/rolling-update/{k6.txt,rolling.log,events.json,per-second.csv,summary.json} +set -eu +cd "$(dirname "$0")/../.." + +RATE=${RATE:-150} +DURATION=${DURATION:-70} +out=evidence/load/rolling-update +rm -rf "$out" && mkdir -p "$out" +now() { python3 -c 'import time; print(int(time.time() * 1000))'; } + +# Build outside the measured window: compiling would compete with the API for CPU. +docker compose build --quiet api + +{ echo "# before"; docker compose ps api --format '{{.Name}} {{.ID}} {{.Image}}'; } > "$out/rolling.log" +K6_CSV_TIME_FORMAT=unix_milli k6 run --quiet --out csv="$out/requests.csv" \ + -e RATE="$RATE" -e DURATION="${DURATION}s" scripts/load/mixed.js > "$out/k6.txt" 2>&1 & +k6_pid=$! +until grep -q ',load,' "$out/requests.csv" 2>/dev/null; do sleep 0.2; done +start=$(now) + +sleep 10 +begin=$(now) +scripts/rolling-update.sh >> "$out/rolling.log" 2>&1 +end=$(now) +wait "$k6_pid" || true + +{ echo "# after"; docker compose ps api --format '{{.Name}} {{.ID}} {{.Image}}'; } >> "$out/rolling.log" +echo "{\"load_start\": $start, \"rolling_begin\": $begin, \"rolling_end\": $end}" > "$out/events.json" +python3 scripts/load/summarize.py "$out" > /dev/null +# Raw per-request data stays out of Git (evidence keeps aggregates only); KEEP_RAW=1 keeps it for analysis. +[ "${KEEP_RAW:-0}" = 1 ] || rm -f "$out/requests.csv" +cat "$out/rolling.log" "$out/summary.json" diff --git a/scripts/load/summarize.py b/scripts/load/summarize.py new file mode 100755 index 00000000..a3a2d04b --- /dev/null +++ b/scripts/load/summarize.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +"""Per-second view of one k6 run (requests.csv) plus the events of the experiment (events.json). + +Writes per-second.csv and summary.json next to the input. An error is any response that is not 2xx +(including k6 status 0: connection reset or timeout). Times are seconds from the start of the load. +""" +import csv +import json +import math +import sys +from collections import Counter, defaultdict +from pathlib import Path +from urllib.parse import parse_qsl + +run = Path(sys.argv[1]) +events = json.loads((run / 'events.json').read_text()) if (run / 'events.json').exists() else {} + +durations = [] # (t_ms, duration_ms, status) +served = [] # (t_ms, instance, status) +with open(run / 'requests.csv') as f: + for row in csv.DictReader(f): + if row['metric_name'] == 'http_req_duration' and row['scenario'] == 'load': + durations.append((int(row['timestamp']), float(row['metric_value']), int(row['status'] or 0))) + elif row['metric_name'] == 'served_by': + tags = dict(parse_qsl(row['extra_tags'])) + served.append((int(row['timestamp']), tags.get('instance', 'none'), int(tags.get('code', 0)))) + +t0 = min(t for t, _, _ in durations) +rel = lambda ms: (ms - t0) / 1000 + + +def percentile(values, p): + if not values: + return None + values = sorted(values) + return values[min(len(values) - 1, math.ceil(p / 100 * len(values)) - 1)] + + +per_second = defaultdict(lambda: {'ok': 0, 'errors': 0, 'durations': [], 'instances': Counter()}) +for t, d, status in durations: + second = per_second[math.floor(rel(t))] + second['durations'].append(d) + if 200 <= status < 300: + second['ok'] += 1 + else: + second['errors'] += 1 +for t, instance, status in served: + if 200 <= status < 300: + per_second[math.floor(rel(t))]['instances'][instance] += 1 + +instances = sorted({i for s in per_second.values() for i in s['instances']}) +with open(run / 'per-second.csv', 'w', newline='') as f: + writer = csv.writer(f) + writer.writerow(['second', 'ok', 'errors', 'p99_ms', 'max_ms'] + instances) + for second in sorted(per_second): + s = per_second[second] + writer.writerow([second, s['ok'], s['errors'], round(percentile(s['durations'], 99), 1), round(max(s['durations']), 1)] + + [s['instances'].get(i, 0) for i in instances]) + +errors = sorted(rel(t) for t, _, status in durations if not 200 <= status < 300) +seconds = max(per_second) + 1 +summary = { + 'requests': len(durations), + 'ok': len(durations) - len(errors), + 'errors': len(errors), + 'error_codes': dict(Counter(str(status) for _, _, status in durations if not 200 <= status < 300)), + 'ok_rps': round((len(durations) - len(errors)) / seconds, 1), + 'p50_ms': round(percentile([d for _, d, _ in durations], 50), 1), + 'p95_ms': round(percentile([d for _, d, _ in durations], 95), 1), + 'p99_ms': round(percentile([d for _, d, _ in durations], 99), 1), + 'max_ms': round(max(d for _, d, _ in durations), 1), + 'requests_per_instance': dict(Counter(i for _, i, status in served if 200 <= status < 300)), +} +for name, value in events.items(): + if isinstance(value, (int, float)): + summary[f'event_{name}_s'] = round(rel(value), 2) +if 'fault' in events: + fault = rel(events['fault']) + after = [e - fault for e in errors if e >= fault - 0.5] + summary['errors_after_fault'] = len(after) + summary['last_error_after_fault_s'] = round(max(after), 2) if after else None + target = events.get('target_instance') + end = rel(events['restore_begin']) if 'restore_begin' in events else float('inf') + last = [rel(t) - fault for t, i, status in served if i == target and fault <= rel(t) < end] + summary['target_last_response_after_fault_s'] = round(max(last), 2) if last else None +if 'restore_begin' in events and events.get('restored_instance'): + begin = rel(events['restore_begin']) + first = [rel(t) - begin for t, i, status in served + if i == events['restored_instance'] and 200 <= status < 300 and rel(t) >= begin] + summary['restored_replica_first_response_after_start_s'] = round(min(first), 2) if first else None + +# HAProxy state changes (docker log timestamps): when the balancer took the target out and put it back. +log = run / 'haproxy.log' +if log.exists() and 'fault' in events: + from datetime import datetime + + def at(line): + stamp = line.split()[0].rstrip('Z') + whole, _, fraction = stamp.partition('.') + return datetime.fromisoformat(whole + '+00:00').timestamp() * 1000 + float('0.' + (fraction or '0')) * 1000 + + changes = [(at(line), line) for line in log.read_text().splitlines() if 'Server api/' in line and '[WARNING]' not in line] + down = [t for t, line in changes if ('is DOWN' in line or 'going DOWN' in line) and t >= events['fault']] + summary['lb_marked_down_after_fault_s'] = round((min(down) - events['fault']) / 1000, 2) if down else None + if 'restore_begin' in events: + up = [t for t, line in changes if 'is UP, reason' in line and t >= events['restore_begin']] + summary['lb_marked_up_after_start_s'] = round((min(up) - events['restore_begin']) / 1000, 2) if up else None + +(run / 'summary.json').write_text(json.dumps(summary, indent=2, ensure_ascii=False)) +print(json.dumps(summary, indent=2, ensure_ascii=False)) diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..7952b143 --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Replaces the API replicas one by one with a freshly built image while the balancer keeps serving. +# 1. build the new image, 2. apply migrations once (they must be compatible with the running version), +# 3. for every old replica: start one new replica, wait until HAProxy has it UP, then stop (drain) and remove the old. +# The number of serving replicas never drops below the configured count. +set -euo pipefail +cd "$(dirname "$0")/.." + +build_id=${BUILD_ID:-$(git rev-parse --short HEAD)-$(date +%s)} +log() { echo "$(date -u +%H:%M:%S) $*"; } + +up_in_lb() { + docker compose exec -T lb wget -qO- 'http://127.0.0.1:8404/stats;csv' | awk -F, '$1 == "api" && $2 != "BACKEND" && $18 == "UP"' | wc -l +} + +log "build image (BUILD_ID=$build_id)" +docker compose build --quiet --build-arg BUILD_ID="$build_id" api + +log "migrate" +docker compose run --rm --no-deps migrate > /dev/null + +old=$(docker compose ps -q api) +count=$(echo "$old" | wc -l | tr -d ' ') + +for container in $old; do + before=$(docker compose ps -q api | sort) + docker compose up -d --no-deps --no-recreate --scale api=$((count + 1)) api 2> /dev/null + new=$(comm -13 <(echo "$before") <(docker compose ps -q api | sort)) + log "started $(docker inspect -f '{{.Name}}' "$new") ($(echo "$new" | cut -c1-12))" + + until [ "$(docker inspect -f '{{.State.Health.Status}}' "$new")" = healthy ]; do sleep 0.5; done + until [ "$(up_in_lb)" -ge $((count + 1)) ]; do sleep 0.5; done + log "new replica is UP in HAProxy" + + docker stop --time 9 "$container" > /dev/null + docker rm "$container" > /dev/null + log "stopped and removed $(echo "$container" | cut -c1-12)" +done + +log "done: $(docker compose ps -q api | while read -r c; do docker inspect -f '{{.Name}} {{index .Config.Labels "org.opencontainers.image.revision"}}' "$c"; done | tr '\n' ' ')" diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh index b848e733..f8e65527 100755 --- a/scripts/stop-one.sh +++ b/scripts/stop-one.sh @@ -19,4 +19,4 @@ if [ -z "$container" ] || [ "$(printf '%s\n' "$container" | wc -l)" -ne 1 ]; the exit 1 fi -docker stop --time 8 "$container" >/dev/null +docker stop --time 9 "$container" >/dev/null diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt index 4115fd77..69bd4634 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/App.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -6,6 +6,7 @@ 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.configureGracefulShutdown import com.mtusi.messenger.main.plugins.configureInstanceId import com.mtusi.messenger.main.plugins.configureKoin import com.mtusi.messenger.main.plugins.configureRedis @@ -29,6 +30,7 @@ fun Application.module() { configureKoin() configureDatabase() configureRedis() + configureGracefulShutdown() configureContentNegotiation() configureDefaultHeaders() diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt index 51465719..543b30e4 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt @@ -3,6 +3,7 @@ package com.mtusi.messenger.main.infrastructure.controllers.health import com.mtusi.messenger.main.extensions.respondError import com.mtusi.messenger.main.infrastructure.controllers.health.responses.HealthResponse import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.isDraining import com.mtusi.messenger.main.plugins.suspendedTransaction import io.ktor.http.HttpStatusCode import io.ktor.server.application.log @@ -20,6 +21,12 @@ fun Routing.configureHealthRouting() = route("/health") { // PostgreSQL is the only dependency of the synchronous path: without it nothing can be served. get("ready") { + // SIGTERM received: leave the balancer's rotation while requests in flight are still being finished. + if (call.application.isDraining) { + call.respondError(ErrorCode.UNAVAILABLE, "shutting down") + return@get + } + val isDatabaseReachable = try { suspendedTransaction { exec("SELECT 1") diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/GracefulShutdown.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/GracefulShutdown.kt new file mode 100644 index 00000000..9e6eec03 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/GracefulShutdown.kt @@ -0,0 +1,38 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopPreparing +import io.ktor.server.application.log +import io.ktor.util.AttributeKey +import java.util.concurrent.atomic.AtomicBoolean + +private val DrainingKey = AttributeKey("Draining") + +/** + * True once the application received SIGTERM: /health/ready answers 503 from then on. + */ +val Application.isDraining: Boolean + get() = attributes.getOrNull(DrainingKey)?.get() == true + +/** + * On SIGTERM the replica first fails its readiness check and keeps serving for `shutdown.drainDelayMs`, so the + * balancer takes it out of rotation while it still answers. Only then Ktor closes the listening socket and waits + * for requests in flight (`shutdownGracePeriod` / `shutdownTimeout`). + * + * ApplicationStopPreparing is raised synchronously before the engine closes its channels, so blocking here delays + * the close itself. + */ +fun Application.configureGracefulShutdown() { + + val drainDelayMs = environment.config.propertyOrNull("shutdown.drainDelayMs")?.getString()?.toLong() ?: 0 + val draining = AtomicBoolean(false) + + attributes.put(DrainingKey, draining) + + monitor.subscribe(ApplicationStopPreparing) { + draining.set(true) + log.info("Shutting down: readiness is 503, still serving for $drainDelayMs ms") + Thread.sleep(drainDelayMs) + log.info("Drain delay is over, closing the listening socket") + } +} diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 7460517c..956da0f5 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -8,11 +8,19 @@ ktor { host = ${?APP_HOST} port = 8080 port = ${?APP_PORT} - shutdownGracePeriod = 3000 - shutdownTimeout = 10000 + # After the drain (see `shutdown`): Netty waits for 1 s without new activity, at most 5 s in total. + shutdownGracePeriod = 1000 + shutdownTimeout = 5000 } } +shutdown { + # Time between SIGTERM and closing the socket while /health/ready is already 503. + # Behind HAProxy (check every 1 s, 2 failures) 3 s is enough to leave rotation; a single local process needs none. + drainDelayMs = 0 + drainDelayMs = ${?SHUTDOWN_DRAIN_DELAY_MS} +} + database { host = "localhost" host = ${?DB_HOST} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt index e221736d..4db94965 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt @@ -191,6 +191,7 @@ class DependencyFailureTest { @Test fun `without PostgreSQL requests fail with a bounded 503`() { val database = TestDatabase.create().also { it.start() } + TestDatabase.awaitConnectable(database) try { runApp(database = database, databaseConnectionTimeoutMs = 2000) { client -> diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt index e9a29cd4..9453a1ef 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt @@ -1,6 +1,8 @@ package com.mtusi.messenger.main.support import org.testcontainers.containers.PostgreSQLContainer +import java.sql.DriverManager +import java.sql.SQLException import org.testcontainers.utility.DockerImageName /** @@ -20,6 +22,22 @@ object TestDatabase { /** * A separate, not yet started container for tests that stop the database. */ + /** + * Docker Desktop may refuse the first connections through a fresh port mapping for a moment after the container + * reported ready; tests with a short pool timeout wait for a real connection first. + */ + fun awaitConnectable(container: PostgreSQLContainer<*>, timeoutMs: Long = 15000) { + val deadline = System.currentTimeMillis() + timeoutMs + while (true) { + try { + DriverManager.getConnection(container.jdbcUrl, container.username, container.password).use { return } + } catch (e: SQLException) { + if (System.currentTimeMillis() > deadline) throw e + Thread.sleep(200) + } + } + } + fun create(): PostgreSQLContainer<*> { return PostgreSQLContainer(DockerImageName.parse("postgres:17.6-alpine3.22").asCompatibleSubstituteFor("postgres")) .withDatabaseName(NAME) -- 2.54.0 From 4abbe84847fef84e4b70492b4555df2456b3795c Mon Sep 17 00:00:00 2001 From: Mraksimus Date: Fri, 23 Oct 2026 22:33:12 +0300 Subject: [PATCH 4/4] Fix: reject revoked tokens on all replicas when Redis writes fail --- REPORT.md | 44 +++-- evidence/clean-start.txt | 22 +-- evidence/failover-api-1.txt | 20 +-- evidence/failover-api-2.txt | 24 +-- evidence/gradle-tests.txt | 37 +++-- evidence/idempotency-race.txt | 4 +- evidence/instances.txt | 18 +-- evidence/load/README.md | 2 +- evidence/load/failover-kill-api-1/events.json | 4 +- evidence/load/failover-kill-api-1/k6.txt | 22 +-- .../load/failover-kill-api-1/per-second.csv | 92 +++++------ .../load/failover-kill-api-1/summary.json | 36 ++--- evidence/load/failover-kill-api-2/events.json | 4 +- evidence/load/failover-kill-api-2/k6.txt | 22 +-- .../load/failover-kill-api-2/per-second.csv | 93 +++++------ .../load/failover-kill-api-2/summary.json | 36 ++--- evidence/load/failover-stop-api-1/events.json | 4 +- evidence/load/failover-stop-api-1/k6.txt | 18 +-- .../load/failover-stop-api-1/per-second.csv | 92 +++++------ .../load/failover-stop-api-1/summary.json | 30 ++-- evidence/load/failover-stop-api-2/events.json | 4 +- evidence/load/failover-stop-api-2/k6.txt | 19 ++- .../load/failover-stop-api-2/per-second.csv | 92 +++++------ .../load/failover-stop-api-2/summary.json | 36 ++--- evidence/load/rolling-update/events.json | 2 +- evidence/load/rolling-update/k6.txt | 16 +- evidence/load/rolling-update/per-second.csv | 142 ++++++++-------- evidence/load/rolling-update/summary.json | 24 +-- evidence/persistence.txt | 26 +-- evidence/revocation-pause.txt | 53 ++++++ run/_template/config/authentication.conf | 2 + scripts/revocation-pause.sh | 44 +++++ .../kotlin/com/mtusi/messenger/main/App.kt | 2 + .../authentication/AuthenticationConfig.kt | 5 + .../SerialAuthenticationConfig.kt | 3 +- .../authentication/AuthenticationService.kt | 11 ++ .../DefaultAuthenticationService.kt | 40 ++++- .../main/plugins/SessionRevocationSync.kt | 36 +++++ src/main/resources/application.conf | 4 + .../CrossReplicaRevocationTest.kt | 152 ++++++++++++++++++ .../main/plugins/DependencyFailureTest.kt | 35 ++-- .../mtusi/messenger/main/support/TestApp.kt | 5 +- 42 files changed, 876 insertions(+), 501 deletions(-) create mode 100644 evidence/revocation-pause.txt create mode 100755 scripts/revocation-pause.sh create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt diff --git a/REPORT.md b/REPORT.md index 92584380..2e0af768 100644 --- a/REPORT.md +++ b/REPORT.md @@ -30,6 +30,10 @@ docker compose start api # вернуть остановле # одновременные повторы одного Idempotency-Key через обе реплики python3 scripts/idempotency-race.py +# отзыв токена при паузе записи в Redis (сценарий из замечания на PR): 204 через ~5 с, потом 401 от обеих реплик +PAUSE_MS=5000 scripts/revocation-pause.sh +PAUSE_MS=12000 scripts/revocation-pause.sh # дольше срока logout: 503, токен всё равно 401 + # уровень 4: отказ каждой реплики под нагрузкой и rolling update (нужен k6) MODE=stop TARGET=api-1 scripts/load/failover-under-load.sh # также MODE=kill, TARGET=api-2 scripts/load/rolling-under-load.sh # сам update без нагрузки: scripts/rolling-update.sh @@ -97,6 +101,11 @@ migrate (тот же образ, `migrate`) ── один раз до стар 3. Для каждой старой реплики: `up --no-recreate --scale api=N+1` поднимает новую, скрипт ждёт `healthy` и состояние UP в `/stats` HAProxy, затем `docker stop` (с drain) и `docker rm` старой. Работающих реплик ни в какой момент не меньше двух. +- **Отзыв сессии виден всем репликам.** По замечанию на PR. Раньше logout отвечал 204 сразу после удаления сессии в PostgreSQL, даже если метку `revoked` не удалось записать в Redis. Признак «кэшу нельзя верить» после сбоя Redis жил в памяти только той реплики, которая сама увидела ошибку. Воспроизведение: `CLIENT PAUSE 5000 WRITE` (чтения из Redis идут, запись метки зависает), logout через LB — 204. После этого реплика, обработавшая logout, давала 401, а вторая — 200 на чтение и 201 на повтор сообщения. Теперь в `DefaultAuthenticationService`: + 1. **204 только после записи метки.** Logout по-прежнему сначала удаляет сессию и пишет строку в `session_revocations` (одна транзакция). Затем пишет `revoked` в Redis и повторяет попытку каждые 250 мс до `SESSION_REVOKE_TIMEOUT_MS` (8 с, меньше `timeout server` HAProxy). Получилось — строка удаляется, ответ 204: метка уже в общем Redis, и любая реплика при чтении кэша видит `revoked`. Не получилось за 8 с — ответ 503 `unavailable`: logout не подтверждён. + 2. **Общий сигнал через PostgreSQL** (`plugins/SessionRevocationSync.kt`). Каждая реплика раз в секунду проверяет `session_revocations`. Строка там значит, что какой-то logout ещё не дописал метку. Реплика тогда перестаёт доверять кэшу: сессии читаются из базы, где сессии уже нет, а значит 401. Как только Redis принимает запись, недописанные метки дописывает любая реплика. Это закрывает и случай с 503, и сбой Redis во время logout на другой реплике. + + Цена решения: если Redis недоступен на запись дольше 8 с, logout отвечает 503 вместо 204. В lab2 при остановленном Redis logout отвечал 204. Это сознательный выбор: 204 обещает, что токен больше нигде не примут, а без записи в общий кэш это обещание проверить нельзя. - **Ресурсы.** CPU/RAM-лимиты у всех контейнеров, `container_name` убран у всех, порт опубликован только у `lb` и только на `127.0.0.1`. - **CORS** (`plugins/Cors.kt`, `config/cors/`). Нужен для Swagger UI на `/docs`, открытого с другого адреса (например, dev-порт 8081). Разрешены только `localhost`/`127.0.0.1:8080/8081`, наружу отдаются `X-Request-Id`, `X-Instance-Id`, `WWW-Authenticate`. На API без браузера не влияет. @@ -116,7 +125,7 @@ migrate (тот же образ, `migrate`) ── один раз до стар | `CACHE_URL_FILE` | `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD_FILE=/run/secrets/redis_password` | | `API_REPLICAS` | так же, `deploy.replicas: ${API_REPLICAS:-2}` (HAProxy рассчитан максимум на 4) | | `IDEMPOTENCY_TTL_SECONDS` | так же, по умолчанию 86400 | -| — | `DB_MIGRATE_ON_START` (false у реплик), `SHUTDOWN_DRAIN_DELAY_MS` (3000), `INSTANCE_ID` (не задаю) | +| — | `DB_MIGRATE_ON_START` (false у реплик), `SHUTDOWN_DRAIN_DELAY_MS` (3000), `SESSION_REVOKE_TIMEOUT_MS` (8000), `INSTANCE_ID` (не задаю) | **Таймауты и проверки — одна цепочка.** @@ -130,8 +139,10 @@ migrate (тот же образ, `migrate`) ── один раз до стар | Остановка Ktor | 1 с тишины, максимум 5 с | дожать начатые запросы | | `stop_grace_period` | 10 с | 3 + 5 = 8 с < 10 с, SIGKILL не нужен | | `stop-one.sh` | `docker stop --time 9` | укладывается в 10 с, которые даёт тест | +| Подтверждение logout в Redis | повтор каждые 250 мс, максимум 8 с, затем 503 | меньше `timeout server` 10 с; пауза записи Redis до 8 с даёт 204 | +| Проверка `session_revocations` | раз в 1 с на каждой реплике | реплика, не видевшая сбоя Redis, перестаёт доверять кэшу ≤ 1 с | -Максимальное время обнаружения отказа — около 2 с: при остановке по проверке готовности, при падении процесса по DNS или проверке. Измерено 1,1–1,9 с, см. ниже. +Максимальное время обнаружения отказа — около 2 с: при остановке по проверке готовности, при падении процесса по DNS или проверке. Измерено 1,5–2,0 с, см. ниже. ## Доказательства критериев @@ -141,16 +152,17 @@ migrate (тот же образ, `migrate`) ── один раз до стар | Запуск из чистого clone | копия рабочего дерева без игнорируемых файлов, новые секреты, свежий volume, все `make`-проверки | все PASS | evidence/clean-start.txt | | Стенд в compose: ≥2 API, один вход, общие БД и сессии | `docker compose ps` | `api-1`, `api-2`, `lb` с портом `127.0.0.1:8080`, `postgres`, `redis`; `migrate` завершился с 0 | evidence/instances.txt, evidence/limits.txt | | Разные реплики без повторного входа, видно по `X-Instance-Id` | 6 запросов через LB; failover-тест сам проверяет ≥2 реплики | два id чередуются и совпадают с id контейнеров `api-1`, `api-2`; `observed_instances_before: 2` | evidence/instances.txt, evidence/failover-api-*.txt | -| Остановка любой одной реплики: новые чтения/записи ≤ 10 с, подтверждённое на месте | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` для api-1, потом для api-2 | PASS оба раза; 121 запись, 0 неудачных; остановлен именно контейнер с id из теста (`Exited (143)`) | evidence/failover-api-1.txt, evidence/failover-api-2.txt | +| Остановка любой одной реплики: новые чтения/записи ≤ 10 с, подтверждённое на месте | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` для api-1, потом для api-2 | PASS оба раза; 124–125 записей, 0 неудачных; остановлен именно контейнер с id из теста (`Exited (143)`) | evidence/failover-api-1.txt, evidence/failover-api-2.txt | | Повтор POST с тем же ключом без дубля, в том числе одновременно и через другую реплику | `scripts/idempotency-race.py`: 20 раундов по 32 одновременных запроса с одним ключом; тесты `MessageIdempotencyTest` | 640 ответов 201 (320 + 320 по репликам), в каждом раунде один id, в истории 20 сообщений; 7 тестов: повтор, 409, 400, область ключа, доступ, 20 одновременных запросов, TTL | evidence/idempotency-race.txt, evidence/gradle-tests.txt | | Неподтверждённую запись можно повторить с тем же ключом | failover-тест повторяет неопределённый POST с исходным ключом | дублей нет, проверка текстов прошла | evidence/failover-api-*.txt | | `make test`, persistence, failover | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; у lb, обеих api, postgres, redis новый `StartedAt`, volume тот же | evidence/persistence.txt | -| Уровень 4: health/readiness, таймауты, SIGTERM согласованы, LB исключает недоступную реплику | таблица таймаутов выше; отказ под нагрузкой | после SIGTERM реплика получает последний запрос через 1,1–1,7 с, ошибок 0 | evidence/load/failover-stop-*/ | +| Отзыв токена при сбое записи в Redis виден всем репликам (замечание на PR) | `PAUSE_MS=5000 scripts/revocation-pause.sh`, `PAUSE_MS=12000 …` через LB; тест `CrossReplicaRevocationTest` | пауза 5 с: logout 204 через 5,1 с, затем обе реплики: чтение 401, повтор сообщения с тем же ключом 401. Пауза 12 с: logout 503 через 8,6 с, обе реплики всё равно 401, после паузы в Redis `revoked`. Тест: две реплики на одних PostgreSQL и Redis, вторая отклоняет токен | evidence/revocation-pause.txt, evidence/gradle-tests.txt | +| Уровень 4: health/readiness, таймауты, SIGTERM согласованы, LB исключает недоступную реплику | таблица таймаутов выше; отказ под нагрузкой | после SIGTERM реплика получает последний запрос через 1,5–1,6 с, ошибок 0 | evidence/load/failover-stop-*/ | | Уровень 4: поочерёдная остановка каждой реплики, RPS, ошибки, восстановление | `scripts/load/failover-under-load.sh`, 150 RPS, stop и kill для api-1 и api-2 | 4 прогона: ~150 RPS, 0 ошибок; таблица ниже | evidence/load/ | | Уровень 4: параллельные запросы с одним ключом | см. строку про идемпотентность | 0 дублей | evidence/idempotency-race.txt | | Уровень 4: лимиты CPU/RAM, API без портов наружу, секреты только где нужно, нет `container_name` | `docker inspect`, `docker compose ps`, `docker compose config` | лимиты у всех; порт только у `lb`; `lb` без секретов, `migrate` и `postgres` только `db_password`, `redis` только `redis_password` | evidence/limits.txt | -| Уровень 4: rolling update под нагрузкой, миграции однократно | `scripts/load/rolling-under-load.sh` | 10 501 запрос, 0 ошибок, p99 11,7 мс; обе реплики заменены на образ с новым `BUILD_ID` | evidence/load/rolling-update/ | -| Свои автотесты | `./gradlew test` | 68 тестов, все проходят | evidence/gradle-tests.txt | +| Уровень 4: rolling update под нагрузкой, миграции однократно | `scripts/load/rolling-under-load.sh` | 10 500 запросов, 0 ошибок, p99 46,7 мс; обе реплики заменены на образ с новым `BUILD_ID` | evidence/load/rolling-update/ | +| Свои автотесты | `./gradlew test` | 70 тестов, все проходят | evidence/gradle-tests.txt | | Секреты не в Git | `secrets/` и `.env` в `.gitignore`; в evidence нет токенов (k6 их не пишет, сырые запросы не сохраняются) | — | .gitignore | ## Эксперименты и отказы @@ -168,26 +180,26 @@ migrate (тот же образ, `migrate`) ── один раз до стар | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| `docker stop` api-1 (SIGTERM) под 150 RPS | 0 ошибок, реплика выходит из ротации за ≤ 2 с | 6 750 запросов, 0 ошибок, 150 RPS, p99 48 мс; последний ответ остановленной реплики через 1,07 с, HAProxy пометил DOWN через 1,08 с | вернувшаяся реплика получила трафик через 3,8 с после `docker start`; потерь нет | -| `docker stop` api-2 | то же | 6 743 запроса, 0 ошибок, 149,8 RPS, p99 162 мс; DOWN через 1,69 с | трафик через 2,4 с; потерь нет | -| `docker kill` api-1 (SIGKILL, падение) | 0 ошибок, задержка до 1 с у части запросов | 6 726 запросов, 0 ошибок, 149,5 RPS, p99 1 004 мс; DOWN через 1,74 с | трафик через 3,9 с; потерь нет | -| `docker kill` api-2 | то же | 6 722 запроса, 0 ошибок, 149,4 RPS, p99 1 004 мс; DOWN через 1,92 с | трафик через 3,9 с; потерь нет | -| Rolling update двух реплик под 150 RPS | 0 ошибок | 10 501 запрос, 0 ошибок, p99 11,7 мс, max 625 мс; замена двух реплик заняла 26 с | — | -| `make test-failover`, api-1 и api-2 | ≤ 10 с до записи, без дублей | PASS, 0 неудачных из ~121; `first_recovery_seconds` 5,1–5,4 | это время самого `docker stop` (drain 3 с + остановка), записи всё это время шли через вторую реплику | +| `docker stop` api-1 (SIGTERM) под 150 RPS | 0 ошибок, реплика выходит из ротации за ≤ 2 с | 6 751 запрос, 0 ошибок, 150 RPS, p99 24 мс; последний ответ остановленной реплики через 1,6 с, HAProxy пометил DOWN через 1,6 с | вернувшаяся реплика получила трафик через 4,1 с после `docker start`; потерь нет | +| `docker stop` api-2 | то же | 6 750 запросов, 0 ошибок, 150 RPS, p99 14 мс; DOWN через 1,5 с | трафик через 4,0 с; потерь нет | +| `docker kill` api-1 (SIGKILL, падение) | 0 ошибок, задержка до 1 с у части запросов | 6 713 запросов, 0 ошибок, 149,2 RPS, p99 1 005 мс; DOWN через 1,89 с; k6 не запустил 38 итераций (VU ждали таймаут подключения) | трафик через 4,0 с; потерь нет | +| `docker kill` api-2 | то же | 6 697 запросов, 0 ошибок, 145,6 RPS, p99 1 004 мс; DOWN через 1,98 с; не запущено 54 итерации | трафик через 2,3 с; потерь нет | +| Rolling update двух реплик под 150 RPS | 0 ошибок | 10 500 запросов, 0 ошибок, p99 46,7 мс, max 390 мс; замена двух реплик заняла 31 с (вместе со сборкой образа) | — | +| `make test-failover`, api-1 и api-2 | ≤ 10 с до записи, без дублей | PASS, 0 неудачных из ~124; `first_recovery_seconds` 4,9–5,4 | это время самого `docker stop` (drain 3 с + остановка), записи всё это время шли через вторую реплику | | `scripts/restart.sh` (lb, api, redis, postgres) | данные и токен на месте | PASS, у всех контейнеров новый `StartedAt` | тест сам ждёт готовности и проверяет данные тем же токеном; потерь нет | Что из этого следует: -- **SIGTERM.** Drain работает: реплика получает запросы ещё 1–1,7 с после SIGTERM, а сокет закрывается только через 3 с. Начатых запросов, которые обрывались бы, нет. -- **SIGKILL.** Ошибок тоже нет, но в первые 1–2 с запросы к мёртвой реплике ждут таймаут подключения 1 с и уходят на живую. Отсюда p99 около 1 с. Адреса упавшего контейнера в сети больше нет, поэтому отказ в соединении (RST) не приходит — только таймаут. +- **SIGTERM.** Drain работает: реплика получает запросы ещё ~1,5 с после SIGTERM, а сокет закрывается только через 3 с. Начатых запросов, которые обрывались бы, нет. +- **SIGKILL.** Ошибок тоже нет, но в первые 1–2 с запросы к мёртвой реплике ждут таймаут подключения 1 с и уходят на живую. Отсюда p99 около 1 с. Адреса упавшего контейнера в сети больше нет, поэтому отказ в соединении (RST) не приходит — только таймаут. Пока виртуальные пользователи k6 ждут эту секунду, часть итераций (38–54 из ~6 750) k6 не успевает запустить, отсюда 145–149 RPS. - **Обратный ввод в ротацию.** Сначала нужно 2–2,5 с на старт JVM и Ktor, потом 2 успешные проверки (≈2 с). - **Один прогон на вариант.** Число ошибок (0) от повторов, скорее всего, не изменится, а p99 и времена — оценка, а не распределение. -- **Измерение.** k6 запускался на той же машине. Хвосты p99 в «спокойных» прогонах (до 160 мс) похожи на паузы Docker Desktop и GC, а не на работу балансировщика. +- **Измерение.** k6 запускался на той же машине. Хвосты p99 в «спокойных» прогонах (до 50 мс) похожи на паузы Docker Desktop и GC, а не на работу балансировщика. ## Ограничения и следующие шаги - **Выдерживается** остановка или падение одной API-реплики, в том числе под нагрузкой, и поочерёдная замена всех реплик. **Не выдерживается** отказ `lb`, `postgres` или всего хоста: они в одном экземпляре на одной машине. Это единые точки отказа, HA для них в этой работе не заявляю. Отказ Redis, как и в lab2, не ломает API: запросы идут в PostgreSQL. - **Хвост задержки при падении.** При внезапном падении реплики p99 около 1 с из-за `timeout connect 1s`. В сети Docker таймаут подключения можно сделать 200–300 мс, это должно сократить хвост, но я это не проверял. -- **Недоверие к кэшу после сбоя Redis** (из lab2) живёт в памяти одной реплики. Если logout пришёлся на сбой Redis, реплика, которая сама ошибку Redis не видела, может до 60 с отдавать отозванную сессию из кэша. С двумя репликами за одним Redis это маловероятно: ошибку Redis увидят обе. Но строгой гарантии нет, нужен общий признак (например, номер поколения кэша в PostgreSQL). +- **Logout при Redis, который дольше 8 с не принимает запись, отвечает 503.** Сессия при этом уже удалена в PostgreSQL. В течение ~1 с, пока другие реплики не увидели строку в `session_revocations`, они ещё могут принять токен из кэша. Подтверждения (204) клиент в этом случае не получил; повтор logout вернёт 401, потому что сессии в базе уже нет. - **Single-flight** (защита от одновременных промахов кэша) работает внутри процесса: на N реплик до N запросов к базе на один токен. - **`make test-failover` всегда выбирает api-1** при одинаковом числе запросов. Вторую реплику проверяю после одного лишнего запроса, это описано в разделе запуска. - **Без очистки.** Старые ключи идемпотентности (старше 24 ч) удаляются только при повторе того же ключа, а не отдельной задачей. Таблица растёт на одну строку на сообщение; для учебного объёма это нормально, в продакшене нужна периодическая чистка по `created_at`. Просроченные сессии тоже не удаляются (как в lab2). diff --git a/evidence/clean-start.txt b/evidence/clean-start.txt index 0fe8d405..c385e599 100644 --- a/evidence/clean-start.txt +++ b/evidence/clean-start.txt @@ -1,20 +1,20 @@ # clean copy of the working tree (git ls-files -co --exclude-standard), fresh secrets and volume, COMPOSE_PROJECT_NAME=lab3-clean -# 2026-10-23T12:49Z, base commit 92c2fdc + uncommitted lab3 grade-4 changes -$ ./scripts/init-secrets.sh && docker compose up -d --build --wait && make check && make test && make test-persistence ACTION_SCRIPT=scripts/restart.sh && make test-failover ACTION_SCRIPT=scripts/stop-one.sh +# 2026-10-23T19:07Z, base commit af184ca + fix of session revocation across replicas +$ ./scripts/init-secrets.sh && docker compose up -d --build --wait && make check && make test && make test-persistence ACTION_SCRIPT=scripts/restart.sh && make test-failover ACTION_SCRIPT=scripts/stop-one.sh && PAUSE_MS=5000 scripts/revocation-pause.sh created secrets/db_password created secrets/redis_password -config ok Container lab3-clean-api-2 Healthy - Container lab3-clean-api-1 Healthy Container lab3-clean-lb-1 Healthy PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. -{"observed_instances_before": 2, "successful_writes": 125, "failed_attempts": 0, "first_recovery_seconds": 4.859} +{"observed_instances_before": 2, "successful_writes": 125, "failed_attempts": 0, "first_recovery_seconds": 4.945} PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. -lab3-clean-api-1 running -lab3-clean-api-2 exited -lab3-clean-lb-1 running -lab3-clean-migrate-1 exited -lab3-clean-postgres-1 running -lab3-clean-redis-1 running + GET /users/me -> 200 e4b51cb6e956 + GET /users/me -> 200 20b26306c56f + GET /users/me -> 200 e4b51cb6e956 + GET /users/me -> 200 20b26306c56f +DELETE /auth/sessions/current -> 204 e4b51cb6e956 after 5.09 s + GET /users/me -> 401 20b26306c56f + GET /users/me -> 401 e4b51cb6e956 + GET /users/me -> 401 20b26306c56f diff --git a/evidence/failover-api-1.txt b/evidence/failover-api-1.txt index 4a54361b..2ba13d78 100644 --- a/evidence/failover-api-1.txt +++ b/evidence/failover-api-1.txt @@ -1,24 +1,24 @@ # before NAME CONTAINER ID STATE STATUS -mtusi-messenger-lab3-api-1 d1ff13561537 running Up 24 seconds (healthy) -mtusi-messenger-lab3-api-2 ac9723acebaa running Up 24 seconds (healthy) +mtusi-messenger-lab3-api-1 73b8fff02245 running Up 22 seconds (healthy) +mtusi-messenger-lab3-api-2 01d14766531f running Up 22 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": 121, "failed_attempts": 0, "first_recovery_seconds": 5.385} +{"observed_instances_before": 2, "successful_writes": 125, "failed_attempts": 0, "first_recovery_seconds": 5.401} 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 d1ff13561537 exited Exited (143) 9 seconds ago -mtusi-messenger-lab3-api-2 ac9723acebaa running Up 40 seconds (healthy) +mtusi-messenger-lab3-api-1 73b8fff02245 exited Exited (143) 10 seconds ago +mtusi-messenger-lab3-api-2 01d14766531f running Up 38 seconds (healthy) # HAProxy log around the failure (non-2xx and state changes) -172.24.0.1:57032 [23/Oct/2026:11:37:38.388] http api/api2 0/0/0/97/97 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" -172.24.0.1:58552 [23/Oct/2026:11:37:53.795] http api/api2 0/0/0/86/86 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" -172.24.0.1:58572 [23/Oct/2026:11:37:53.942] http api/api2 0/0/0/78/78 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" -172.24.0.1:58586 [23/Oct/2026:11:37:54.055] http api/api2 0/0/0/81/81 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" -Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 5ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +172.24.0.1:59474 [23/Oct/2026:19:02:09.419] http api/api2 0/0/0/55/55 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:56176 [23/Oct/2026:19:02:23.431] http api/api2 0/0/0/73/73 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:56204 [23/Oct/2026:19:02:23.611] http api/api2 0/0/0/27/27 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:56220 [23/Oct/2026:19:02:23.714] http api/api2 0/0/0/50/50 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" [WARNING] (8) : Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 5ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 5ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. [WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ). Server api/api2 was DOWN and now enters maintenance (No IP for server ). diff --git a/evidence/failover-api-2.txt b/evidence/failover-api-2.txt index 8e1cc861..7c427599 100644 --- a/evidence/failover-api-2.txt +++ b/evidence/failover-api-2.txt @@ -1,24 +1,24 @@ # before NAME CONTAINER ID STATE STATUS -mtusi-messenger-lab3-api-1 d1ff13561537 running Up 8 seconds (healthy) -mtusi-messenger-lab3-api-2 ac9723acebaa running Up About a minute (healthy) +mtusi-messenger-lab3-api-1 73b8fff02245 running Up 7 seconds (healthy) +mtusi-messenger-lab3-api-2 01d14766531f 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": 121, "failed_attempts": 0, "first_recovery_seconds": 5.125} +{"observed_instances_before": 2, "successful_writes": 124, "failed_attempts": 0, "first_recovery_seconds": 4.882} 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 d1ff13561537 running Up 24 seconds (healthy) -mtusi-messenger-lab3-api-2 ac9723acebaa exited Exited (143) 10 seconds ago +mtusi-messenger-lab3-api-1 73b8fff02245 running Up 24 seconds (healthy) +mtusi-messenger-lab3-api-2 01d14766531f exited Exited (143) 10 seconds ago # HAProxy log around the failure (non-2xx and state changes) -[WARNING] (8) : Server api/api2 is UP, reason: Layer7 check passed, code: 200, check duration: 9ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue. -172.24.0.1:64862 [23/Oct/2026:11:38:51.915] http api/api1 0/0/0/69/69 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" -172.24.0.1:64886 [23/Oct/2026:11:38:52.074] http api/api1 0/0/0/26/26 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" -172.24.0.1:64892 [23/Oct/2026:11:38:52.179] http api/api1 0/0/0/24/24 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" -[WARNING] (8) : Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 3ms. 1 active and 0 backup servers left. 1 sessions active, 0 requeued, 0 remaining in queue. -Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 3ms. 1 active and 0 backup servers left. 1 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/api2 is UP, reason: Layer7 check passed, code: 200, check duration: 5ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue. +172.24.0.1:57320 [23/Oct/2026:19:03:21.353] http api/api1 0/0/0/77/77 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:57342 [23/Oct/2026:19:03:21.513] http api/api1 0/0/0/105/105 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +172.24.0.1:57356 [23/Oct/2026:19:03:21.724] http api/api1 0/0/0/85/85 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1" +[WARNING] (8) : Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 1ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 1ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. Server api/api1 was DOWN and now enters maintenance (No IP for server ). +[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ). diff --git a/evidence/gradle-tests.txt b/evidence/gradle-tests.txt index 7bba1aa6..1747287f 100644 --- a/evidence/gradle-tests.txt +++ b/evidence/gradle-tests.txt @@ -1,7 +1,7 @@ # ./gradlew test (Testcontainers: postgres:17.6-alpine3.22, redis:8.4.0-alpine) -total: 68 tests, 0 failed +total: 70 tests, 0 failed -infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 7.132 s +infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 3.509 s - logout revokes only the current token - Bearer token cannot open a session - password containing a colon and non-ASCII characters works @@ -11,19 +11,19 @@ infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, - 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.173 s +infrastructure.controllers.chat.ChatControllerTest: 6 tests, 0 failed, 1.078 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.127 s +infrastructure.controllers.health.HealthControllerTest: 2 tests, 0 failed, 0.102 s - live returns 200 - ready returns 200 when database is reachable -infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.285 s +infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.230 s - parallel senders produce exactly N unique messages -infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.826 s +infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.020 s - foreign chat returns 404 when sending - last page has null next_cursor - pagination walks all messages exactly once @@ -31,7 +31,7 @@ infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.8 - 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.269 s +infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 2.500 s - expired key can be used for a new message - key scope is user and chat - same key with another body is a conflict @@ -39,7 +39,7 @@ infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 3. - 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.668 s +infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 0.917 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 @@ -51,37 +51,40 @@ infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 1.668 s - 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.091 s +infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: 3 tests, 0 failed, 0.124 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.498 s +infrastructure.repositories.chat.ExposedChatRepositoryTest: 2 tests, 0 failed, 0.233 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.122 s +infrastructure.services.authentication.CrossReplicaRevocationTest: 2 tests, 0 failed, 11.634 s + - unconfirmed logout makes every replica bypass the cache + - logout waits for paused Redis writes and then no replica accepts the token +infrastructure.services.authentication.SessionLookupSingleFlightTest: 4 tests, 0 failed, 1.091 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.418 s +infrastructure.services.hashing.DefaultHashingServiceTest: 4 tests, 0 failed, 0.170 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.CorsTest: 2 tests, 0 failed, 0.119 s +plugins.CorsTest: 2 tests, 0 failed, 0.129 s - page of the API itself may call it - unknown origin is rejected -plugins.DatabaseMigrationTest: 1 tests, 0 failed, 0.188 s +plugins.DatabaseMigrationTest: 1 tests, 0 failed, 0.185 s - re-running migrations applies nothing and keeps data -plugins.DependencyFailureTest: 5 tests, 0 failed, 13.637 s +plugins.DependencyFailureTest: 5 tests, 0 failed, 25.701 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.245 s +plugins.InstanceIdTest: 1 tests, 0 failed, 0.087 s - every response names the replica -plugins.SessionCacheTest: 3 tests, 0 failed, 0.953 s +plugins.SessionCacheTest: 3 tests, 0 failed, 0.624 s - emptied cache falls back to PostgreSQL - first authenticated request fills the cache - logout leaves a tombstone and the token is rejected diff --git a/evidence/idempotency-race.txt b/evidence/idempotency-race.txt index 2aa14a6d..3c8d7c11 100644 --- a/evidence/idempotency-race.txt +++ b/evidence/idempotency-race.txt @@ -6,8 +6,8 @@ $ python3 scripts/idempotency-race.py "201": 640 }, "requests_per_instance": { - "ac9723acebaa": 320, - "d1ff13561537": 320 + "73b8fff02245": 320, + "01d14766531f": 320 }, "rounds_with_more_than_one_message_id": 0, "messages_in_history": 20, diff --git a/evidence/instances.txt b/evidence/instances.txt index 489d5fc8..4b2cea3e 100644 --- a/evidence/instances.txt +++ b/evidence/instances.txt @@ -1,6 +1,6 @@ $ docker compose ps -a NAME IMAGE STATE STATUS PORTS -mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 running Up 10 seconds (healthy) 8080/tcp +mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 running Up 9 seconds (healthy) 8080/tcp mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 running Up 9 seconds (healthy) 8080/tcp mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine running Up 7 seconds (healthy) 127.0.0.1:8080->8080/tcp mtusi-messenger-lab3-migrate-1 mtusi-messenger:lab3 exited Exited (0) 10 seconds ago @@ -8,14 +8,14 @@ mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 running Up 20 sec mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine running Up 20 seconds (healthy) 6379/tcp $ for i in 1..6: curl -sD - http://localhost:8080/health/ready | grep X-Instance-Id -x-instance-id: ac9723acebaa -x-instance-id: d1ff13561537 -x-instance-id: ac9723acebaa -x-instance-id: d1ff13561537 -x-instance-id: ac9723acebaa -x-instance-id: d1ff13561537 +x-instance-id: 01d14766531f +x-instance-id: 73b8fff02245 +x-instance-id: 01d14766531f +x-instance-id: 73b8fff02245 +x-instance-id: 01d14766531f +x-instance-id: 73b8fff02245 # X-Instance-Id = HOSTNAME = short container id of the replica NAME CONTAINER ID STATE STATUS -mtusi-messenger-lab3-api-1 d1ff13561537 running Up 10 seconds (healthy) -mtusi-messenger-lab3-api-2 ac9723acebaa running Up 10 seconds (healthy) +mtusi-messenger-lab3-api-1 73b8fff02245 running Up 10 seconds (healthy) +mtusi-messenger-lab3-api-2 01d14766531f running Up 10 seconds (healthy) diff --git a/evidence/load/README.md b/evidence/load/README.md index a21780fe..42d67baf 100644 --- a/evidence/load/README.md +++ b/evidence/load/README.md @@ -1,6 +1,6 @@ # Нагрузочные прогоны -Дата: 2026-10-23. Код: commit 92c2fdc + незакоммиченные изменения уровня 4 (тот же код, что в PR). +Дата: 2026-10-23. Код: commit af184ca + исправление отзыва сессий по замечанию на PR (тот же код, что в PR). Стенд: Apple M4 (10 ядер), 16 ГБ, macOS 27; Docker Desktop 28.4 (VM 10 CPU, 8 ГБ), Compose 2.39; k6 2.3 на той же машине. Лимиты контейнеров — как в `compose.yaml`: api 1 CPU / 512 МБ ×2, postgres 1 CPU / 1 ГБ, redis 0.5 CPU / 128 МБ, lb 0.5 CPU / 128 МБ. diff --git a/evidence/load/failover-kill-api-1/events.json b/evidence/load/failover-kill-api-1/events.json index 41dbbbd7..8f4f1f3a 100644 --- a/evidence/load/failover-kill-api-1/events.json +++ b/evidence/load/failover-kill-api-1/events.json @@ -1,2 +1,2 @@ -{"mode": "kill", "target": "api-1", "target_instance": "7b89cf61597b", "restored_instance": "7b89cf61597b", - "load_start": 1792755242253, "fault": 1792755252286, "fault_done": 1792755252443, "restore_begin": 1792755267305, "restore_healthy": 1792755269813} +{"mode": "kill", "target": "api-1", "target_instance": "ef8f943d313b", "restored_instance": "ef8f943d313b", + "load_start": 1792781931991, "fault": 1792781942042, "fault_done": 1792781942234, "restore_begin": 1792781957033, "restore_healthy": 1792781959476} diff --git a/evidence/load/failover-kill-api-1/k6.txt b/evidence/load/failover-kill-api-1/k6.txt index e6635170..2bbe3211 100644 --- a/evidence/load/failover-kill-api-1/k6.txt +++ b/evidence/load/failover-kill-api-1/k6.txt @@ -3,23 +3,23 @@ █ TOTAL RESULTS CUSTOM - served_by......................: 6726 146.724721/s + served_by......................: 6713 145.257079/s HTTP - http_req_duration..............: avg=24.62ms med=1.73ms p(95)=6.23ms p(99)=1s max=1.01s - { expected_response:true }...: avg=24.62ms med=1.73ms p(95)=6.23ms p(99)=1s max=1.01s - http_req_failed................: 0.00% 0 out of 6747 - http_reqs......................: 6747 147.182826/s + http_req_duration..............: avg=42.08ms med=2.01ms p(95)=171.07ms p(99)=1s max=1.29s + { expected_response:true }...: avg=42.08ms med=2.01ms p(95)=171.07ms p(99)=1s max=1.29s + http_req_failed................: 0.00% 0 out of 6734 + http_reqs......................: 6734 145.711481/s EXECUTION - dropped_iterations.............: 25 0.545364/s - iteration_duration.............: avg=24.77ms med=1.92ms p(95)=6.16ms p(99)=1s max=1.01s - iterations.....................: 6726 146.724721/s - vus............................: 0 min=0 max=66 - vus_max........................: 75 min=50 max=75 + dropped_iterations.............: 38 0.822251/s + iteration_duration.............: avg=42.25ms med=2.21ms p(95)=171.3ms p(99)=1s max=1.29s + iterations.....................: 6713 145.257079/s + vus............................: 1 min=0 max=85 + vus_max........................: 88 min=50 max=88 NETWORK - data_received..................: 2.6 MB 57 kB/s + data_received..................: 2.6 MB 56 kB/s data_sent......................: 1.3 MB 29 kB/s diff --git a/evidence/load/failover-kill-api-1/per-second.csv b/evidence/load/failover-kill-api-1/per-second.csv index 8e5d98b5..d417f273 100644 --- a/evidence/load/failover-kill-api-1/per-second.csv +++ b/evidence/load/failover-kill-api-1/per-second.csv @@ -1,46 +1,46 @@ -second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b -0,151,0,11.9,12.3,75,76 -1,149,0,5.9,6.7,75,74 -2,150,0,9.8,10.5,75,75 -3,150,0,6.4,7.3,75,75 -4,150,0,7.0,7.9,75,75 -5,151,0,5.8,7.8,75,76 -6,150,0,3.9,7.1,75,75 -7,149,0,6.2,6.5,75,74 -8,150,0,6.2,6.4,75,75 -9,151,0,6.1,7.3,75,76 -10,97,0,7.6,7.6,77,20 -11,104,0,1007.5,1009.4,104,0 -12,224,0,1006.6,1011.5,224,0 -13,150,0,7.1,15.8,150,0 -14,150,0,8.9,22.4,150,0 -15,150,0,4.3,5.1,150,0 -16,150,0,6.2,7.4,150,0 -17,150,0,5.2,6.2,150,0 -18,149,0,5.1,5.9,149,0 -19,151,0,5.4,6.8,151,0 -20,150,0,5.7,5.8,150,0 -21,150,0,5.6,5.7,150,0 -22,150,0,11.5,14.7,150,0 -23,150,0,5.7,6.0,150,0 -24,150,0,5.5,6.2,150,0 -25,150,0,34.2,43.0,150,0 -26,150,0,4.1,4.4,150,0 -27,149,0,5.9,6.1,149,0 -28,136,0,7.4,8.0,136,0 -29,164,0,714.7,777.3,75,89 -30,151,0,8.4,20.8,75,76 -31,149,0,32.0,41.6,74,75 -32,151,0,12.8,15.2,76,75 -33,150,0,5.6,5.8,75,75 -34,150,0,5.4,5.5,75,75 -35,150,0,10.5,12.2,75,75 -36,150,0,5.7,6.5,75,75 -37,150,0,5.5,5.9,75,75 -38,150,0,5.4,6.6,75,75 -39,150,0,6.4,6.5,75,75 -40,150,0,5.7,6.2,75,75 -41,149,0,5.2,5.5,74,75 -42,150,0,26.9,29.4,75,75 -43,151,0,6.9,7.1,76,75 -44,150,0,17.8,18.8,75,75 +second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b +0,151,0,13.2,16.3,76,75 +1,150,0,4.8,6.2,75,75 +2,150,0,5.2,5.5,75,75 +3,150,0,7.9,10.4,75,75 +4,150,0,6.3,7.3,75,75 +5,150,0,8.1,8.9,75,75 +6,150,0,6.0,6.1,75,75 +7,150,0,4.6,4.9,75,75 +8,150,0,4.2,4.4,75,75 +9,150,0,5.1,6.0,75,75 +10,150,0,13.5,13.6,77,73 +11,68,0,10.1,10.1,68,0 +12,146,0,1006.5,1006.6,146,0 +13,213,0,1012.8,1013.9,213,0 +14,150,0,7.0,7.6,150,0 +15,150,0,4.5,4.5,150,0 +16,150,0,4.3,8.0,150,0 +17,150,0,4.7,7.5,150,0 +18,150,0,4.7,5.4,150,0 +19,150,0,6.0,6.3,150,0 +20,150,0,5.6,5.8,150,0 +21,150,0,6.2,6.5,150,0 +22,150,0,6.1,6.2,150,0 +23,150,0,11.8,24.3,150,0 +24,150,0,7.8,14.1,150,0 +25,150,0,5.3,6.8,150,0 +26,150,0,4.2,5.5,150,0 +27,150,0,5.3,5.7,150,0 +28,150,0,7.0,11.0,150,0 +29,145,0,546.0,550.9,100,45 +30,155,0,135.4,179.9,75,80 +31,128,0,53.6,57.2,64,64 +32,89,0,926.0,926.0,69,20 +33,218,0,1266.2,1293.6,84,134 +34,150,0,6.1,7.9,75,75 +35,150,0,6.8,7.7,75,75 +36,150,0,8.0,10.8,75,75 +37,150,0,29.3,34.7,75,75 +38,150,0,8.0,8.9,75,75 +39,150,0,5.2,5.4,75,75 +40,150,0,4.9,6.1,75,75 +41,150,0,6.6,7.0,75,75 +42,150,0,5.7,6.2,75,75 +43,150,0,9.2,15.0,75,75 +44,150,0,7.0,8.1,75,75 diff --git a/evidence/load/failover-kill-api-1/summary.json b/evidence/load/failover-kill-api-1/summary.json index e6f71a3f..eec27cf8 100644 --- a/evidence/load/failover-kill-api-1/summary.json +++ b/evidence/load/failover-kill-api-1/summary.json @@ -1,26 +1,26 @@ { - "requests": 6726, - "ok": 6726, + "requests": 6713, + "ok": 6713, "errors": 0, "error_codes": {}, - "ok_rps": 149.5, - "p50_ms": 1.7, - "p95_ms": 5.9, - "p99_ms": 1003.5, - "max_ms": 1011.5, + "ok_rps": 149.2, + "p50_ms": 2.0, + "p95_ms": 171.4, + "p99_ms": 1005.3, + "max_ms": 1293.6, "requests_per_instance": { - "7b89cf61597b": 1986, - "4fa49e9e1a53": 4740 + "33ea33bfe1fd": 4722, + "ef8f943d313b": 1991 }, - "event_load_start_s": 0.22, - "event_fault_s": 10.26, - "event_fault_done_s": 10.41, - "event_restore_begin_s": 25.27, - "event_restore_healthy_s": 27.78, + "event_load_start_s": 0.89, + "event_fault_s": 10.94, + "event_fault_done_s": 11.13, + "event_restore_begin_s": 25.93, + "event_restore_healthy_s": 28.38, "errors_after_fault": 0, "last_error_after_fault_s": null, - "target_last_response_after_fault_s": 0.01, - "restored_replica_first_response_after_start_s": 3.85, - "lb_marked_down_after_fault_s": 1.74, - "lb_marked_up_after_start_s": 3.52 + "target_last_response_after_fault_s": 0.02, + "restored_replica_first_response_after_start_s": 3.95, + "lb_marked_down_after_fault_s": 1.89, + "lb_marked_up_after_start_s": 3.39 } \ No newline at end of file diff --git a/evidence/load/failover-kill-api-2/events.json b/evidence/load/failover-kill-api-2/events.json index 23365925..2f73e43c 100644 --- a/evidence/load/failover-kill-api-2/events.json +++ b/evidence/load/failover-kill-api-2/events.json @@ -1,2 +1,2 @@ -{"mode": "kill", "target": "api-2", "target_instance": "4fa49e9e1a53", "restored_instance": "4fa49e9e1a53", - "load_start": 1792755292111, "fault": 1792755302165, "fault_done": 1792755302329, "restore_begin": 1792755317163, "restore_healthy": 1792755319645} +{"mode": "kill", "target": "api-2", "target_instance": "33ea33bfe1fd", "restored_instance": "33ea33bfe1fd", + "load_start": 1792781980971, "fault": 1792781991007, "fault_done": 1792781991173, "restore_begin": 1792782006018, "restore_healthy": 1792782008497} diff --git a/evidence/load/failover-kill-api-2/k6.txt b/evidence/load/failover-kill-api-2/k6.txt index d9541d25..de8c6d03 100644 --- a/evidence/load/failover-kill-api-2/k6.txt +++ b/evidence/load/failover-kill-api-2/k6.txt @@ -3,23 +3,23 @@ █ TOTAL RESULTS CUSTOM - served_by......................: 6722 146.537002/s + served_by......................: 6697 145.107345/s HTTP - http_req_duration..............: avg=25.2ms med=1.69ms p(95)=5.68ms p(99)=1s max=1.01s - { expected_response:true }...: avg=25.2ms med=1.69ms p(95)=5.68ms p(99)=1s max=1.01s - http_req_failed................: 0.00% 0 out of 6743 - http_reqs......................: 6743 146.994794/s + http_req_duration..............: avg=44.22ms med=1.98ms p(95)=297.87ms p(99)=1s max=1.12s + { expected_response:true }...: avg=44.22ms med=1.98ms p(95)=297.87ms p(99)=1s max=1.12s + http_req_failed................: 0.00% 0 out of 6718 + http_reqs......................: 6718 145.562363/s EXECUTION - dropped_iterations.............: 29 0.632189/s - iteration_duration.............: avg=25.35ms med=1.88ms p(95)=5.77ms p(99)=1s max=1.01s - iterations.....................: 6722 146.537002/s - vus............................: 1 min=0 max=78 - vus_max........................: 79 min=50 max=79 + dropped_iterations.............: 54 1.170046/s + iteration_duration.............: avg=44.43ms med=2.16ms p(95)=298.99ms p(99)=1s max=1.12s + iterations.....................: 6697 145.107345/s + vus............................: 97 min=0 max=97 + vus_max........................: 97 min=50 max=97 NETWORK - data_received..................: 2.6 MB 57 kB/s + data_received..................: 2.6 MB 56 kB/s data_sent......................: 1.3 MB 29 kB/s diff --git a/evidence/load/failover-kill-api-2/per-second.csv b/evidence/load/failover-kill-api-2/per-second.csv index d811ae40..cf602e11 100644 --- a/evidence/load/failover-kill-api-2/per-second.csv +++ b/evidence/load/failover-kill-api-2/per-second.csv @@ -1,46 +1,47 @@ -second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b -0,151,0,4.9,6.7,76,75 -1,150,0,6.1,6.7,75,75 -2,150,0,8.3,9.0,75,75 -3,150,0,5.8,6.2,75,75 -4,150,0,5.5,6.4,75,75 -5,150,0,5.4,5.7,75,75 -6,150,0,5.4,6.1,75,75 -7,150,0,5.5,6.1,75,75 -8,150,0,6.8,7.2,75,75 -9,150,0,7.0,7.8,75,75 -10,96,0,9.1,9.1,19,77 -11,104,0,1010.4,1012.0,0,104 -12,209,0,1006.0,1006.3,0,209 -13,162,0,1004.9,1005.5,0,162 -14,149,0,5.8,6.0,0,149 -15,151,0,3.7,3.8,0,151 -16,150,0,4.6,6.1,0,150 -17,150,0,5.4,6.2,0,150 -18,150,0,5.5,7.0,0,150 -19,150,0,6.0,7.1,0,150 -20,149,0,5.8,7.3,0,149 -21,151,0,6.5,6.9,0,151 -22,150,0,5.5,5.7,0,150 -23,149,0,6.4,6.6,0,149 -24,151,0,11.2,13.3,0,151 -25,150,0,11.7,14.0,0,150 -26,150,0,4.3,5.2,0,150 -27,150,0,6.2,6.2,0,150 -28,133,0,6.0,7.3,0,133 -29,167,0,694.4,717.1,92,75 -30,150,0,5.9,7.7,75,75 -31,148,0,8.0,33.2,74,74 -32,152,0,6.2,23.0,76,76 -33,150,0,5.5,6.5,75,75 -34,150,0,29.7,35.8,75,75 -35,150,0,4.9,6.4,75,75 -36,150,0,5.1,5.2,75,75 -37,150,0,4.9,5.2,75,75 -38,150,0,4.9,5.7,75,75 -39,149,0,5.3,5.7,75,74 -40,151,0,5.9,6.7,75,76 -41,149,0,4.4,4.4,75,74 -42,151,0,9.9,10.7,75,76 -43,150,0,8.5,8.9,75,75 -44,150,0,5.5,6.2,75,75 +second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b +0,152,0,6.1,8.1,76,76 +1,150,0,9.8,13.4,75,75 +2,150,0,4.5,4.5,75,75 +3,150,0,96.5,99.6,75,75 +4,149,0,391.0,395.5,74,75 +5,151,0,21.4,21.6,76,75 +6,150,0,5.1,6.4,75,75 +7,150,0,5.3,5.9,75,75 +8,149,0,5.6,7.3,74,75 +9,148,0,36.3,40.2,75,73 +10,89,0,28.8,28.8,13,76 +11,110,0,1006.8,1008.0,0,110 +12,214,0,1006.9,1008.4,0,214 +13,159,0,1004.7,1005.4,0,159 +14,149,0,6.3,7.3,0,149 +15,151,0,9.9,12.8,0,151 +16,150,0,36.1,43.2,0,150 +17,149,0,5.0,5.0,0,149 +18,151,0,6.2,8.4,0,151 +19,150,0,5.4,7.6,0,150 +20,150,0,5.5,5.8,0,150 +21,150,0,6.0,8.0,0,150 +22,150,0,6.2,8.0,0,150 +23,149,0,8.0,9.0,0,149 +24,151,0,5.1,5.4,0,151 +25,150,0,4.1,4.5,0,150 +26,136,0,4.1,4.5,0,136 +27,154,0,954.6,986.7,79,75 +28,160,0,148.6,161.1,85,75 +29,150,0,21.8,36.3,75,75 +30,149,0,4.5,5.4,75,74 +31,151,0,5.7,6.1,75,76 +32,150,0,5.4,5.8,75,75 +33,150,0,9.0,21.0,75,75 +34,150,0,48.5,55.4,75,75 +35,149,0,4.7,5.7,75,74 +36,151,0,7.1,7.5,75,76 +37,150,0,4.5,6.0,75,75 +38,150,0,5.8,6.3,75,75 +39,150,0,4.8,5.5,75,75 +40,150,0,4.7,5.8,75,75 +41,149,0,11.5,11.8,75,74 +42,151,0,10.9,20.2,75,76 +43,123,0,471.7,504.0,54,69 +44,56,0,588.3,588.3,25,31 +45,97,0,1125.0,1125.0,59,38 diff --git a/evidence/load/failover-kill-api-2/summary.json b/evidence/load/failover-kill-api-2/summary.json index e3c7eba0..53b8e22e 100644 --- a/evidence/load/failover-kill-api-2/summary.json +++ b/evidence/load/failover-kill-api-2/summary.json @@ -1,26 +1,26 @@ { - "requests": 6722, - "ok": 6722, + "requests": 6697, + "ok": 6697, "errors": 0, "error_codes": {}, - "ok_rps": 149.4, - "p50_ms": 1.7, - "p95_ms": 5.4, - "p99_ms": 1003.5, - "max_ms": 1012.0, + "ok_rps": 145.6, + "p50_ms": 2.0, + "p95_ms": 299.1, + "p99_ms": 1003.8, + "max_ms": 1125.0, "requests_per_instance": { - "4fa49e9e1a53": 1987, - "7b89cf61597b": 4735 + "ef8f943d313b": 4582, + "33ea33bfe1fd": 2115 }, - "event_load_start_s": 0.18, - "event_fault_s": 10.23, - "event_fault_done_s": 10.4, - "event_restore_begin_s": 25.23, - "event_restore_healthy_s": 27.71, + "event_load_start_s": 0.1, + "event_fault_s": 10.13, + "event_fault_done_s": 10.3, + "event_restore_begin_s": 25.14, + "event_restore_healthy_s": 27.62, "errors_after_fault": 0, "last_error_after_fault_s": null, - "target_last_response_after_fault_s": 0.02, - "restored_replica_first_response_after_start_s": 3.86, - "lb_marked_down_after_fault_s": 1.92, - "lb_marked_up_after_start_s": 3.54 + "target_last_response_after_fault_s": 0.03, + "restored_replica_first_response_after_start_s": 2.27, + "lb_marked_down_after_fault_s": 1.98, + "lb_marked_up_after_start_s": null } \ No newline at end of file diff --git a/evidence/load/failover-stop-api-1/events.json b/evidence/load/failover-stop-api-1/events.json index 1018d8e1..ef44bdd1 100644 --- a/evidence/load/failover-stop-api-1/events.json +++ b/evidence/load/failover-stop-api-1/events.json @@ -1,2 +1,2 @@ -{"mode": "stop", "target": "api-1", "target_instance": "7b89cf61597b", "restored_instance": "7b89cf61597b", - "load_start": 1792755143191, "fault": 1792755153238, "fault_done": 1792755158479, "restore_begin": 1792755168242, "restore_healthy": 1792755170683} +{"mode": "stop", "target": "api-1", "target_instance": "ef8f943d313b", "restored_instance": "ef8f943d313b", + "load_start": 1792781831378, "fault": 1792781841419, "fault_done": 1792781845703, "restore_begin": 1792781856431, "restore_healthy": 1792781858936} diff --git a/evidence/load/failover-stop-api-1/k6.txt b/evidence/load/failover-stop-api-1/k6.txt index 3a89b661..9d4af3ad 100644 --- a/evidence/load/failover-stop-api-1/k6.txt +++ b/evidence/load/failover-stop-api-1/k6.txt @@ -3,23 +3,23 @@ █ TOTAL RESULTS CUSTOM - served_by......................: 6750 143.513142/s + served_by......................: 6751 146.192761/s HTTP - http_req_duration..............: avg=5.21ms med=2.03ms p(95)=5.28ms p(99)=66.29ms max=608.68ms - { expected_response:true }...: avg=5.21ms med=2.03ms p(95)=5.28ms p(99)=66.29ms max=608.68ms - http_req_failed................: 0.00% 0 out of 6771 - http_reqs......................: 6771 143.959628/s + http_req_duration..............: avg=4.7ms med=1.97ms p(95)=5.34ms p(99)=43.67ms max=610.04ms + { expected_response:true }...: avg=4.7ms med=1.97ms p(95)=5.34ms p(99)=43.67ms max=610.04ms + http_req_failed................: 0.00% 0 out of 6772 + http_reqs......................: 6772 146.647516/s EXECUTION - iteration_duration.............: avg=5.13ms med=2.21ms p(95)=5.47ms p(99)=48.43ms max=608.87ms - iterations.....................: 6750 143.513142/s - vus............................: 2 min=0 max=26 + iteration_duration.............: avg=4.75ms med=2.17ms p(95)=5.5ms p(99)=23.83ms max=610.26ms + iterations.....................: 6751 146.192761/s + vus............................: 1 min=0 max=22 vus_max........................: 50 min=50 max=50 NETWORK data_received..................: 2.6 MB 56 kB/s - data_sent......................: 1.4 MB 29 kB/s + data_sent......................: 1.3 MB 29 kB/s diff --git a/evidence/load/failover-stop-api-1/per-second.csv b/evidence/load/failover-stop-api-1/per-second.csv index 48d500a8..7ae1350c 100644 --- a/evidence/load/failover-stop-api-1/per-second.csv +++ b/evidence/load/failover-stop-api-1/per-second.csv @@ -1,46 +1,46 @@ -second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b -0,153,0,205.2,220.8,77,76 -1,150,0,7.0,7.7,75,75 -2,150,0,7.3,16.6,75,75 -3,150,0,5.6,6.1,75,75 -4,150,0,6.3,6.6,75,75 -5,150,0,30.2,41.5,75,75 -6,150,0,7.4,7.8,75,75 -7,150,0,7.0,7.3,75,75 -8,150,0,58.5,73.3,75,75 -9,150,0,5.7,7.0,75,75 -10,150,0,5.7,7.6,75,75 -11,150,0,5.8,6.1,75,75 -12,150,0,25.5,29.9,134,16 -13,150,0,5.8,6.6,150,0 -14,150,0,4.9,5.5,150,0 -15,150,0,4.6,5.3,150,0 -16,150,0,11.3,14.1,150,0 -17,150,0,4.4,5.2,150,0 -18,150,0,5.0,6.8,150,0 -19,150,0,6.2,7.2,150,0 -20,150,0,6.6,6.9,150,0 -21,150,0,6.7,8.5,150,0 -22,151,0,7.8,12.3,151,0 -23,150,0,5.0,11.3,150,0 -24,149,0,20.5,27.7,149,0 -25,150,0,5.7,6.3,150,0 -26,150,0,7.8,8.4,150,0 -27,150,0,6.6,10.4,150,0 -28,151,0,4.4,8.2,151,0 -29,120,0,543.2,554.2,103,17 -30,180,0,602.9,608.7,75,105 -31,149,0,5.9,8.8,74,75 -32,150,0,18.6,31.4,75,75 -33,150,0,5.2,5.6,75,75 -34,150,0,5.9,6.0,75,75 -35,150,0,7.6,9.4,75,75 -36,150,0,4.6,4.7,75,75 -37,150,0,4.0,5.1,75,75 -38,150,0,5.5,7.1,75,75 -39,151,0,4.9,5.0,76,75 -40,149,0,7.3,7.6,74,75 -41,150,0,7.4,16.1,75,75 -42,150,0,6.8,13.7,75,75 -43,150,0,5.7,9.3,75,75 -44,147,0,45.7,53.5,74,73 +second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b +0,152,0,5.8,12.1,76,76 +1,150,0,8.8,19.5,75,75 +2,150,0,4.7,4.9,75,75 +3,150,0,18.4,19.9,75,75 +4,150,0,5.2,5.5,75,75 +5,150,0,4.7,4.8,75,75 +6,149,0,5.0,5.6,74,75 +7,151,0,6.3,6.4,76,75 +8,150,0,13.9,14.1,75,75 +9,150,0,10.2,10.8,75,75 +10,150,0,6.7,7.9,75,75 +11,150,0,4.1,4.6,75,75 +12,150,0,4.7,6.2,112,38 +13,150,0,14.8,18.6,150,0 +14,150,0,5.5,6.8,150,0 +15,150,0,5.3,7.5,150,0 +16,150,0,5.2,5.4,150,0 +17,150,0,4.4,5.0,150,0 +18,150,0,4.9,5.3,150,0 +19,149,0,5.6,5.8,149,0 +20,151,0,5.2,6.1,151,0 +21,150,0,4.4,6.5,150,0 +22,150,0,6.2,7.0,150,0 +23,150,0,16.9,17.2,150,0 +24,150,0,4.8,9.1,150,0 +25,150,0,5.2,6.1,150,0 +26,150,0,7.4,16.9,150,0 +27,150,0,4.8,5.6,150,0 +28,150,0,4.8,4.9,150,0 +29,115,0,6.0,6.2,115,0 +30,185,0,588.5,610.0,75,110 +31,150,0,7.2,8.2,75,75 +32,150,0,8.4,11.9,75,75 +33,150,0,26.3,30.7,75,75 +34,150,0,6.9,7.3,75,75 +35,150,0,7.0,8.7,75,75 +36,150,0,5.2,5.9,75,75 +37,150,0,5.4,6.5,75,75 +38,150,0,7.9,11.6,75,75 +39,150,0,4.9,5.1,75,75 +40,149,0,6.5,7.2,74,75 +41,151,0,6.4,15.9,76,75 +42,150,0,11.7,14.7,75,75 +43,147,0,20.3,28.0,73,74 +44,152,0,26.7,42.2,76,76 diff --git a/evidence/load/failover-stop-api-1/summary.json b/evidence/load/failover-stop-api-1/summary.json index 39fd1562..ca2aa48e 100644 --- a/evidence/load/failover-stop-api-1/summary.json +++ b/evidence/load/failover-stop-api-1/summary.json @@ -1,26 +1,26 @@ { - "requests": 6750, - "ok": 6750, + "requests": 6751, + "ok": 6751, "errors": 0, "error_codes": {}, "ok_rps": 150.0, "p50_ms": 2.0, "p95_ms": 5.2, - "p99_ms": 48.3, - "max_ms": 608.7, + "p99_ms": 24.0, + "max_ms": 610.0, "requests_per_instance": { - "7b89cf61597b": 2087, - "4fa49e9e1a53": 4663 + "33ea33bfe1fd": 4652, + "ef8f943d313b": 2099 }, - "event_load_start_s": 1.08, - "event_fault_s": 11.13, - "event_fault_done_s": 16.37, - "event_restore_begin_s": 26.13, - "event_restore_healthy_s": 28.57, + "event_load_start_s": 0.86, + "event_fault_s": 10.9, + "event_fault_done_s": 15.18, + "event_restore_begin_s": 25.91, + "event_restore_healthy_s": 28.41, "errors_after_fault": 0, "last_error_after_fault_s": null, - "target_last_response_after_fault_s": 1.07, - "restored_replica_first_response_after_start_s": 3.8, - "lb_marked_down_after_fault_s": 1.08, - "lb_marked_up_after_start_s": 3.24 + "target_last_response_after_fault_s": 1.6, + "restored_replica_first_response_after_start_s": 4.11, + "lb_marked_down_after_fault_s": 1.6, + "lb_marked_up_after_start_s": 3.62 } \ No newline at end of file diff --git a/evidence/load/failover-stop-api-2/events.json b/evidence/load/failover-stop-api-2/events.json index 8da27b82..543a4b38 100644 --- a/evidence/load/failover-stop-api-2/events.json +++ b/evidence/load/failover-stop-api-2/events.json @@ -1,2 +1,2 @@ -{"mode": "stop", "target": "api-2", "target_instance": "4fa49e9e1a53", "restored_instance": "4fa49e9e1a53", - "load_start": 1792755193214, "fault": 1792755203249, "fault_done": 1792755207464, "restore_begin": 1792755218279, "restore_healthy": 1792755220771} +{"mode": "stop", "target": "api-2", "target_instance": "33ea33bfe1fd", "restored_instance": "33ea33bfe1fd", + "load_start": 1792781881945, "fault": 1792781891997, "fault_done": 1792781897229, "restore_begin": 1792781906997, "restore_healthy": 1792781909498} diff --git a/evidence/load/failover-stop-api-2/k6.txt b/evidence/load/failover-stop-api-2/k6.txt index 3e5f1084..6fbdd569 100644 --- a/evidence/load/failover-stop-api-2/k6.txt +++ b/evidence/load/failover-stop-api-2/k6.txt @@ -3,23 +3,22 @@ █ TOTAL RESULTS CUSTOM - served_by......................: 6743 145.744963/s + served_by......................: 6750 146.705085/s HTTP - http_req_duration..............: avg=8.51ms med=1.69ms p(95)=5.01ms p(99)=161.63ms max=1.04s - { expected_response:true }...: avg=8.51ms med=1.69ms p(95)=5.01ms p(99)=161.63ms max=1.04s - http_req_failed................: 0.00% 0 out of 6764 - http_reqs......................: 6764 146.198862/s + http_req_duration..............: avg=4.21ms med=1.89ms p(95)=5.05ms p(99)=25.58ms max=638.21ms + { expected_response:true }...: avg=4.21ms med=1.89ms p(95)=5.05ms p(99)=25.58ms max=638.21ms + http_req_failed................: 0.00% 0 out of 6771 + http_reqs......................: 6771 147.161501/s EXECUTION - dropped_iterations.............: 8 0.172914/s - iteration_duration.............: avg=8.54ms med=1.88ms p(95)=5.16ms p(99)=156.59ms max=1.04s - iterations.....................: 6743 145.744963/s + iteration_duration.............: avg=4.27ms med=2.09ms p(95)=5.24ms p(99)=13.83ms max=638.37ms + iterations.....................: 6750 146.705085/s vus............................: 0 min=0 max=28 - vus_max........................: 58 min=50 max=58 + vus_max........................: 50 min=50 max=50 NETWORK - data_received..................: 2.6 MB 56 kB/s + data_received..................: 2.6 MB 57 kB/s data_sent......................: 1.4 MB 29 kB/s diff --git a/evidence/load/failover-stop-api-2/per-second.csv b/evidence/load/failover-stop-api-2/per-second.csv index e0c8fef4..8ab1ce41 100644 --- a/evidence/load/failover-stop-api-2/per-second.csv +++ b/evidence/load/failover-stop-api-2/per-second.csv @@ -1,46 +1,46 @@ -second,ok,errors,p99_ms,max_ms,4fa49e9e1a53,7b89cf61597b -0,151,0,5.3,5.3,76,75 -1,150,0,5.6,6.0,75,75 -2,150,0,10.9,11.6,75,75 -3,150,0,9.9,10.0,75,75 -4,150,0,10.2,12.3,75,75 -5,150,0,6.3,7.4,75,75 -6,150,0,5.7,6.3,75,75 -7,150,0,8.4,16.2,75,75 -8,149,0,6.0,7.9,74,75 -9,151,0,4.2,5.4,76,75 -10,150,0,4.3,4.4,75,75 -11,150,0,8.1,12.1,75,75 -12,150,0,6.8,10.4,44,106 -13,150,0,5.7,5.9,0,150 -14,150,0,5.6,6.2,0,150 -15,150,0,4.9,5.9,0,150 -16,150,0,4.4,4.9,0,150 -17,150,0,4.4,4.8,0,150 -18,150,0,5.9,6.1,0,150 -19,150,0,5.7,10.8,0,150 -20,150,0,5.8,5.8,0,150 -21,149,0,6.2,7.7,0,149 -22,150,0,8.5,11.4,0,150 -23,151,0,5.0,5.4,0,151 -24,150,0,5.1,5.1,0,150 -25,150,0,6.0,8.8,0,150 -26,150,0,4.9,6.9,0,150 -27,119,0,4.0,5.5,0,119 -28,172,0,1038.9,1043.4,101,71 -29,151,0,53.1,64.8,76,75 -30,150,0,6.5,7.6,75,75 -31,150,0,6.1,7.2,75,75 -32,150,0,7.6,8.6,75,75 -33,150,0,5.7,11.9,75,75 -34,150,0,6.1,8.2,75,75 -35,150,0,7.8,10.4,75,75 -36,150,0,5.1,5.1,75,75 -37,150,0,5.0,6.6,75,75 -38,150,0,7.3,8.7,75,75 -39,150,0,5.8,5.8,75,75 -40,150,0,5.3,5.5,75,75 -41,150,0,4.2,4.6,75,75 -42,150,0,10.8,11.4,75,75 -43,150,0,5.5,5.6,75,75 -44,150,0,7.1,8.6,75,75 +second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b +0,150,0,5.0,5.7,75,75 +1,151,0,5.3,6.4,75,76 +2,150,0,6.1,11.8,75,75 +3,150,0,8.1,14.1,75,75 +4,150,0,8.7,9.4,75,75 +5,150,0,7.5,9.7,75,75 +6,150,0,18.3,20.1,75,75 +7,150,0,6.8,8.4,75,75 +8,150,0,5.0,5.2,75,75 +9,150,0,5.5,6.6,75,75 +10,150,0,5.6,12.9,75,75 +11,150,0,5.4,5.6,75,75 +12,150,0,5.2,5.5,44,106 +13,150,0,7.1,10.4,0,150 +14,150,0,5.9,6.2,0,150 +15,150,0,4.8,8.6,0,150 +16,150,0,10.2,11.1,0,150 +17,150,0,7.6,9.1,0,150 +18,150,0,7.7,9.7,0,150 +19,150,0,5.3,12.9,0,150 +20,150,0,5.0,5.1,0,150 +21,150,0,5.3,7.2,0,150 +22,150,0,6.1,6.2,0,150 +23,150,0,6.3,6.4,0,150 +24,150,0,5.6,6.0,0,150 +25,150,0,5.4,5.5,0,150 +26,150,0,7.9,9.0,0,150 +27,150,0,5.4,5.7,0,150 +28,150,0,22.6,37.2,0,150 +29,121,0,5.0,5.3,0,121 +30,179,0,478.3,638.2,104,75 +31,150,0,37.6,40.7,75,75 +32,150,0,7.7,10.6,75,75 +33,150,0,9.0,10.3,75,75 +34,150,0,6.3,7.2,75,75 +35,150,0,5.7,7.3,75,75 +36,150,0,8.2,9.2,75,75 +37,150,0,4.9,6.1,75,75 +38,150,0,4.2,5.7,75,75 +39,150,0,6.0,6.1,75,75 +40,150,0,11.3,12.2,75,75 +41,150,0,10.6,11.8,75,75 +42,150,0,8.5,12.1,75,75 +43,150,0,9.0,15.2,75,75 +44,149,0,5.9,6.4,75,74 diff --git a/evidence/load/failover-stop-api-2/summary.json b/evidence/load/failover-stop-api-2/summary.json index 35a7c355..aa6ac5e2 100644 --- a/evidence/load/failover-stop-api-2/summary.json +++ b/evidence/load/failover-stop-api-2/summary.json @@ -1,26 +1,26 @@ { - "requests": 6743, - "ok": 6743, + "requests": 6750, + "ok": 6750, "errors": 0, "error_codes": {}, - "ok_rps": 149.8, - "p50_ms": 1.7, - "p95_ms": 4.9, - "p99_ms": 161.6, - "max_ms": 1043.4, + "ok_rps": 150.0, + "p50_ms": 1.9, + "p95_ms": 5.0, + "p99_ms": 14.1, + "max_ms": 638.2, "requests_per_instance": { - "4fa49e9e1a53": 2247, - "7b89cf61597b": 4496 + "ef8f943d313b": 4652, + "33ea33bfe1fd": 2098 }, - "event_load_start_s": 0.87, - "event_fault_s": 10.9, - "event_fault_done_s": 15.12, - "event_restore_begin_s": 25.93, - "event_restore_healthy_s": 28.43, + "event_load_start_s": 1.03, + "event_fault_s": 11.08, + "event_fault_done_s": 16.32, + "event_restore_begin_s": 26.08, + "event_restore_healthy_s": 28.59, "errors_after_fault": 0, "last_error_after_fault_s": null, - "target_last_response_after_fault_s": 1.68, - "restored_replica_first_response_after_start_s": 2.41, - "lb_marked_down_after_fault_s": 1.69, - "lb_marked_up_after_start_s": null + "target_last_response_after_fault_s": 1.49, + "restored_replica_first_response_after_start_s": 3.99, + "lb_marked_down_after_fault_s": 1.5, + "lb_marked_up_after_start_s": 3.52 } \ No newline at end of file diff --git a/evidence/load/rolling-update/events.json b/evidence/load/rolling-update/events.json index 0cf24cf1..d0b2fefe 100644 --- a/evidence/load/rolling-update/events.json +++ b/evidence/load/rolling-update/events.json @@ -1 +1 @@ -{"load_start": 1792755359464, "rolling_begin": 1792755369523, "rolling_end": 1792755395558} +{"load_start": 1792782033873, "rolling_begin": 1792782043905, "rolling_end": 1792782074163} diff --git a/evidence/load/rolling-update/k6.txt b/evidence/load/rolling-update/k6.txt index 41182e99..d7e2275f 100644 --- a/evidence/load/rolling-update/k6.txt +++ b/evidence/load/rolling-update/k6.txt @@ -3,18 +3,18 @@ █ TOTAL RESULTS CUSTOM - served_by......................: 10501 148.101961/s + served_by......................: 10500 148.071162/s HTTP - http_req_duration..............: avg=3.73ms med=1.88ms p(95)=4.77ms p(99)=14.85ms max=625.43ms - { expected_response:true }...: avg=3.73ms med=1.88ms p(95)=4.77ms p(99)=14.85ms max=625.43ms - http_req_failed................: 0.00% 0 out of 10522 - http_reqs......................: 10522 148.398137/s + http_req_duration..............: avg=4.42ms med=1.91ms p(95)=6.51ms p(99)=49.84ms max=390.14ms + { expected_response:true }...: avg=4.42ms med=1.91ms p(95)=6.51ms p(99)=49.84ms max=390.14ms + http_req_failed................: 0.00% 0 out of 10521 + http_reqs......................: 10521 148.367304/s EXECUTION - iteration_duration.............: avg=3.84ms med=2.06ms p(95)=4.98ms p(99)=12ms max=625.53ms - iterations.....................: 10501 148.101961/s - vus............................: 0 min=0 max=27 + iteration_duration.............: avg=4.52ms med=2.08ms p(95)=6.57ms p(99)=46.81ms max=390.44ms + iterations.....................: 10500 148.071162/s + vus............................: 0 min=0 max=4 vus_max........................: 50 min=50 max=50 NETWORK diff --git a/evidence/load/rolling-update/per-second.csv b/evidence/load/rolling-update/per-second.csv index 057e1193..107582d0 100644 --- a/evidence/load/rolling-update/per-second.csv +++ b/evidence/load/rolling-update/per-second.csv @@ -1,71 +1,71 @@ -second,ok,errors,p99_ms,max_ms,1239a2b734c0,4fa49e9e1a53,7b89cf61597b,d23287559687 -0,151,0,5.5,9.0,0,75,76,0 -1,150,0,5.1,7.0,0,75,75,0 -2,150,0,9.8,10.3,0,75,75,0 -3,150,0,5.7,6.4,0,75,75,0 -4,150,0,6.9,7.0,0,75,75,0 -5,150,0,6.3,9.4,0,75,75,0 -6,150,0,4.6,6.8,0,75,75,0 -7,150,0,5.6,6.0,0,75,75,0 -8,150,0,17.0,22.8,0,75,75,0 -9,150,0,7.7,9.3,0,75,75,0 -10,150,0,4.6,5.3,0,75,75,0 -11,150,0,4.5,6.2,0,75,75,0 -12,150,0,4.3,4.4,0,75,75,0 -13,150,0,6.1,10.7,0,75,75,0 -14,150,0,17.3,25.9,0,75,75,0 -15,150,0,9.2,16.0,0,75,75,0 -16,150,0,4.2,4.5,0,75,75,0 -17,149,0,13.0,21.3,0,75,74,0 -18,151,0,13.5,13.8,0,75,76,0 -19,150,0,4.6,4.6,0,75,75,0 -20,149,0,6.1,11.1,0,75,74,0 -21,129,0,8.7,11.5,0,64,65,0 -22,172,0,586.0,625.4,0,50,50,72 -23,150,0,8.1,10.9,0,68,14,68 -24,150,0,5.0,5.4,0,75,0,75 -25,150,0,7.6,12.0,0,75,0,75 -26,150,0,9.6,10.2,0,75,0,75 -27,150,0,20.6,22.0,0,75,0,75 -28,150,0,5.1,7.4,0,75,0,75 -29,150,0,24.8,27.6,0,75,0,75 -30,149,0,354.8,406.4,28,61,0,60 -31,151,0,6.2,6.3,51,50,0,50 -32,150,0,6.2,7.9,74,1,0,75 -33,150,0,5.6,6.0,75,0,0,75 -34,150,0,6.7,7.1,75,0,0,75 -35,150,0,11.7,13.0,75,0,0,75 -36,150,0,20.0,28.9,75,0,0,75 -37,150,0,5.5,6.0,75,0,0,75 -38,150,0,6.0,6.4,75,0,0,75 -39,150,0,5.5,5.8,75,0,0,75 -40,150,0,5.1,5.3,75,0,0,75 -41,150,0,24.6,37.4,75,0,0,75 -42,150,0,4.6,7.6,75,0,0,75 -43,150,0,5.0,6.1,75,0,0,75 -44,150,0,6.6,7.5,75,0,0,75 -45,150,0,7.7,11.6,75,0,0,75 -46,150,0,5.8,5.9,75,0,0,75 -47,150,0,34.1,39.4,75,0,0,75 -48,150,0,5.3,5.5,75,0,0,75 -49,150,0,5.2,6.0,75,0,0,75 -50,150,0,5.3,5.5,75,0,0,75 -51,150,0,5.7,6.7,75,0,0,75 -52,150,0,5.8,5.9,75,0,0,75 -53,150,0,5.7,6.7,75,0,0,75 -54,150,0,5.5,7.1,75,0,0,75 -55,150,0,6.5,6.7,75,0,0,75 -56,149,0,6.7,6.9,75,0,0,74 -57,151,0,8.0,11.5,75,0,0,76 -58,150,0,5.3,5.4,75,0,0,75 -59,150,0,5.6,5.7,75,0,0,75 -60,150,0,4.3,4.6,75,0,0,75 -61,150,0,5.9,6.2,75,0,0,75 -62,150,0,6.1,7.5,75,0,0,75 -63,150,0,4.5,4.8,75,0,0,75 -64,150,0,5.5,6.9,75,0,0,75 -65,150,0,7.2,7.7,75,0,0,75 -66,150,0,5.0,5.2,75,0,0,75 -67,150,0,4.2,4.9,75,0,0,75 -68,150,0,8.6,8.9,75,0,0,75 -69,150,0,5.8,7.4,75,0,0,75 +second,ok,errors,p99_ms,max_ms,05feed80156e,33ea33bfe1fd,5bd75b122d67,ef8f943d313b +0,151,0,7.0,7.5,0,75,0,76 +1,150,0,4.9,5.2,0,75,0,75 +2,150,0,8.2,10.0,0,75,0,75 +3,150,0,7.2,8.1,0,75,0,75 +4,150,0,4.1,4.3,0,75,0,75 +5,150,0,5.1,5.3,0,75,0,75 +6,150,0,5.6,6.1,0,75,0,75 +7,148,0,42.6,46.7,0,74,0,74 +8,152,0,26.0,32.5,0,76,0,76 +9,150,0,30.0,39.6,0,75,0,75 +10,150,0,20.6,25.4,0,75,0,75 +11,149,0,382.2,390.1,0,75,0,74 +12,151,0,49.4,52.9,0,75,0,76 +13,150,0,9.2,12.4,0,75,0,75 +14,149,0,7.4,8.7,0,75,0,74 +15,151,0,43.0,44.2,0,75,0,76 +16,150,0,8.3,11.0,0,75,0,75 +17,149,0,23.8,26.5,0,74,0,75 +18,151,0,99.7,111.5,0,76,0,75 +19,150,0,11.7,17.6,0,75,0,75 +20,150,0,8.7,9.3,0,75,0,75 +21,150,0,4.6,4.7,0,75,0,75 +22,150,0,11.3,14.6,0,75,0,75 +23,150,0,4.7,4.8,0,75,0,75 +24,150,0,4.3,7.6,0,75,0,75 +25,145,0,263.8,284.0,0,65,16,64 +26,155,0,182.9,259.5,0,50,55,50 +27,150,0,6.7,7.0,0,59,60,31 +28,150,0,13.8,15.4,0,75,75,0 +29,150,0,6.1,6.5,0,75,75,0 +30,150,0,6.7,9.6,0,75,75,0 +31,150,0,8.1,9.0,0,75,75,0 +32,150,0,6.5,7.3,0,75,75,0 +33,150,0,4.3,4.4,0,75,75,0 +34,135,0,7.2,10.3,0,68,67,0 +35,165,0,325.1,348.3,65,50,50,0 +36,150,0,6.7,8.2,54,42,54,0 +37,149,0,4.8,11.0,75,0,74,0 +38,150,0,16.4,18.0,75,0,75,0 +39,151,0,10.1,14.6,75,0,76,0 +40,150,0,12.2,12.3,75,0,75,0 +41,150,0,6.4,7.1,75,0,75,0 +42,150,0,5.2,9.0,75,0,75,0 +43,150,0,7.9,8.0,75,0,75,0 +44,150,0,4.4,4.9,75,0,75,0 +45,150,0,4.9,8.7,75,0,75,0 +46,150,0,11.5,15.5,75,0,75,0 +47,150,0,7.4,9.3,75,0,75,0 +48,150,0,5.7,7.4,75,0,75,0 +49,150,0,5.0,5.3,75,0,75,0 +50,150,0,8.5,9.1,75,0,75,0 +51,150,0,5.7,6.6,75,0,75,0 +52,150,0,5.0,6.1,75,0,75,0 +53,150,0,5.0,5.3,75,0,75,0 +54,150,0,11.3,13.3,75,0,75,0 +55,150,0,5.7,6.7,75,0,75,0 +56,150,0,5.1,7.2,75,0,75,0 +57,150,0,5.6,5.7,75,0,75,0 +58,150,0,4.2,4.4,75,0,75,0 +59,150,0,5.0,5.1,75,0,75,0 +60,150,0,8.9,9.7,75,0,75,0 +61,150,0,8.0,8.2,75,0,75,0 +62,150,0,5.4,5.4,75,0,75,0 +63,150,0,4.0,5.5,75,0,75,0 +64,150,0,3.5,4.1,75,0,75,0 +65,150,0,5.4,5.5,75,0,75,0 +66,150,0,5.6,5.6,75,0,75,0 +67,150,0,5.4,6.0,75,0,75,0 +68,150,0,5.5,6.5,75,0,75,0 +69,149,0,5.0,5.6,75,0,74,0 diff --git a/evidence/load/rolling-update/summary.json b/evidence/load/rolling-update/summary.json index 735fae5a..ed474f37 100644 --- a/evidence/load/rolling-update/summary.json +++ b/evidence/load/rolling-update/summary.json @@ -1,20 +1,20 @@ { - "requests": 10501, - "ok": 10501, + "requests": 10500, + "ok": 10500, "errors": 0, "error_codes": {}, "ok_rps": 150.0, "p50_ms": 1.9, - "p95_ms": 4.7, - "p99_ms": 11.7, - "max_ms": 625.4, + "p95_ms": 6.3, + "p99_ms": 46.7, + "max_ms": 390.1, "requests_per_instance": { - "7b89cf61597b": 1704, - "4fa49e9e1a53": 2319, - "d23287559687": 3550, - "1239a2b734c0": 2928 + "ef8f943d313b": 2021, + "33ea33bfe1fd": 2659, + "5bd75b122d67": 3226, + "05feed80156e": 2594 }, - "event_load_start_s": 0.33, - "event_rolling_begin_s": 10.38, - "event_rolling_end_s": 36.42 + "event_load_start_s": 0.11, + "event_rolling_begin_s": 10.14, + "event_rolling_end_s": 40.4 } \ No newline at end of file diff --git a/evidence/persistence.txt b/evidence/persistence.txt index 4ba9d2f7..2ca57e55 100644 --- a/evidence/persistence.txt +++ b/evidence/persistence.txt @@ -1,24 +1,24 @@ # before NAME STATE STATUS -mtusi-messenger-lab3-api-1 running Up 37 seconds (healthy) +mtusi-messenger-lab3-api-1 running Up 36 seconds (healthy) mtusi-messenger-lab3-api-2 running Up 7 seconds (healthy) mtusi-messenger-lab3-lb-1 running Up About a minute (healthy) mtusi-messenger-lab3-migrate-1 exited Exited (0) 4 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-23T11:38:43.02725971Z -/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T11:39:12.251618502Z -/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T11:37:31.902111428Z -/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T11:37:18.99589288Z -/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T11:37:18.995508005Z +mtusi-messenger-lab3-postgres-1 running Up About a minute (healthy) +mtusi-messenger-lab3-redis-1 running Up About a minute (healthy) +/mtusi-messenger-lab3-api-1 StartedAt=2026-10-23T19:03:13.056739134Z +/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T19:03:42.093218675Z +/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T19:02:03.051437129Z +/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T19:01:50.139036512Z +/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T19:01:50.133911762Z $ 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-23T11:39:37.089275763Z -/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T11:39:37.18606118Z -/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T11:39:39.805556042Z -/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T11:39:26.389951592Z -/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T11:39:26.38860105Z +/mtusi-messenger-lab3-api-1 StartedAt=2026-10-23T19:04:06.150995464Z +/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T19:04:06.224890922Z +/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T19:04:08.799986382Z +/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T19:03:55.446885876Z +/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T19:03:55.448259126Z mtusi-messenger-lab3_postgres-data diff --git a/evidence/revocation-pause.txt b/evidence/revocation-pause.txt new file mode 100644 index 00000000..e1a4347c --- /dev/null +++ b/evidence/revocation-pause.txt @@ -0,0 +1,53 @@ +# 2026-10-23T18:55Z scripts/revocation-pause.sh against the compose stack (2 replicas behind HAProxy) +# replica ids: ef8f943d313b 33ea33bfe1fd + +$ PAUSE_MS=5000 scripts/revocation-pause.sh +send message with Idempotency-Key: 201 33ea33bfe1fd +warm the session on both replicas: + GET /users/me -> 200 ef8f943d313b + GET /users/me -> 200 33ea33bfe1fd + GET /users/me -> 200 ef8f943d313b + GET /users/me -> 200 33ea33bfe1fd +redis: CLIENT PAUSE 5000 WRITE -> OK +DELETE /auth/sessions/current -> 204 ef8f943d313b after 5.1 s +old token after logout (redis writes still paused: no): + GET /users/me -> 401 33ea33bfe1fd + GET /users/me -> 401 ef8f943d313b + GET /users/me -> 401 33ea33bfe1fd + GET /users/me -> 401 ef8f943d313b + POST message, same key -> 401 33ea33bfe1fd + POST message, same key -> 401 ef8f943d313b +after the pause: session key in redis = revoked + GET /users/me -> 401 33ea33bfe1fd + GET /users/me -> 401 ef8f943d313b + +$ PAUSE_MS=12000 scripts/revocation-pause.sh +send message with Idempotency-Key: 201 33ea33bfe1fd +warm the session on both replicas: + GET /users/me -> 200 ef8f943d313b + GET /users/me -> 200 33ea33bfe1fd + GET /users/me -> 200 ef8f943d313b + GET /users/me -> 200 33ea33bfe1fd +redis: CLIENT PAUSE 12000 WRITE -> OK +DELETE /auth/sessions/current -> 503 ef8f943d313b after 8.63 s +old token after logout (redis writes still paused: yes): + GET /users/me -> 401 33ea33bfe1fd + GET /users/me -> 401 ef8f943d313b + GET /users/me -> 401 33ea33bfe1fd + GET /users/me -> 401 ef8f943d313b + POST message, same key -> 401 33ea33bfe1fd + POST message, same key -> 401 ef8f943d313b +after the pause: session key in redis = revoked + GET /users/me -> 401 33ea33bfe1fd + GET /users/me -> 401 ef8f943d313b + +# API log: pending revocation seen by the replicas +api-1 2026-10-23T18:55:37.498Z Pending session revocations found, cached sessions are not trusted until they are written +api-1 2026-10-23T18:55:42.419Z Wrote 1 pending session revocations to the cache +api-1 2026-10-23T18:55:45.442Z Pending session revocations found, cached sessions are not trusted until they are written +api-1 2026-10-23T18:55:57.192Z Wrote 1 pending session revocations to the cache +api-2 2026-10-23T18:55:23.552Z Pending session revocations found, cached sessions are not trusted until they are written +api-2 2026-10-23T18:55:37.425Z Pending session revocations found, cached sessions are not trusted until they are written +api-2 2026-10-23T18:55:42.420Z Wrote 1 pending session revocations to the cache +api-2 2026-10-23T18:55:45.442Z Pending session revocations found, cached sessions are not trusted until they are written +api-2 2026-10-23T18:55:57.193Z Wrote 1 pending session revocations to the cache diff --git a/run/_template/config/authentication.conf b/run/_template/config/authentication.conf index 83968198..44d5a6d7 100644 --- a/run/_template/config/authentication.conf +++ b/run/_template/config/authentication.conf @@ -4,4 +4,6 @@ authentication { sessionCacheTtlSeconds = 60 # Collapse concurrent cache misses for one token into a single database lookup sessionCacheSingleFlight = true + # A logout answers 204 only after its tombstone is in Redis; retries up to this long, then 503 + sessionRevokeTimeoutMs = 8000 } diff --git a/scripts/revocation-pause.sh b/scripts/revocation-pause.sh new file mode 100755 index 00000000..9c3a5880 --- /dev/null +++ b/scripts/revocation-pause.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# Reproduces the review scenario on the running stack, everything through the balancer: +# session + message, warm the session on both replicas, CLIENT PAUSE WRITE on Redis (reads keep working), +# logout, then read and repeat the original message with the old token and key. +# Expected: logout is 204 only once the tombstone is in Redis; afterwards every replica answers 401. +# PAUSE_MS=5000 (default): the logout waits for the pause and answers 204. +# PAUSE_MS=12000: longer than the logout deadline (8 s): the logout answers 503 and every replica still rejects the token. +set -euo pipefail +cd "$(dirname "$0")/.." + +PAUSE_MS=${PAUSE_MS:-5000} +B=${BASE_URL:-http://localhost:8080}/api/v1 +U=pause_$RANDOM$RANDOM +PW=pause-test-password +KEY=pause-$RANDOM$RANDOM +redis() { docker compose exec -T redis redis-cli --no-auth-warning -a "$(cat secrets/redis_password)" "$@"; } +now() { python3 -c 'import time; print(time.time())'; } +status() { curl -s -o /dev/null -w '%{http_code} %header{x-instance-id}' "$@"; } + +curl -s -o /dev/null -X POST "$B/users" -H 'Content-Type: application/json' \ + -d "{\"username\":\"$U\",\"display_name\":\"pause\",\"password\":\"$PW\"}" +T=$(curl -s -X POST "$B/auth/sessions" -u "$U:$PW" | python3 -c 'import sys,json;print(json.load(sys.stdin)["token"])') +ME=$(curl -s "$B/users/me" -H "Authorization: Bearer $T" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])') +CHAT=$(curl -s -X POST "$B/chats" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \ + -d "{\"title\":\"pause\",\"member_ids\":[\"$ME\"]}" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])') +send() { status -X POST "$B/chats/$CHAT/messages" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \ + -H "Idempotency-Key: $KEY" -d '{"text":"original message"}'; } + +echo "send message with Idempotency-Key: $(send)" +echo "warm the session on both replicas:" +for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done + +echo "redis: CLIENT PAUSE $PAUSE_MS WRITE -> $(redis CLIENT PAUSE "$PAUSE_MS" WRITE)" +start=$(now) +echo "DELETE /auth/sessions/current -> $(status -X DELETE "$B/auth/sessions/current" -H "Authorization: Bearer $T") after $(python3 -c "print(round($(now) - $start, 2))") s" +sleep 1.5 # replicas check pending revocations once a second; only matters for the unconfirmed (503) case + +echo "old token after logout (redis writes still paused: $(python3 -c "print('yes' if $(now) - $start < $PAUSE_MS / 1000 else 'no')")):" +for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done +for i in 1 2; do echo " POST message, same key -> $(send)"; done + +python3 -c "import time; time.sleep(max(0, $start + $PAUSE_MS / 1000 + 2 - time.time()))" +echo "after the pause: session key in redis = $(redis GET "session:$(printf %s "$T" | shasum -a 256 | cut -d' ' -f1)")" +for i in 1 2; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt index 69bd4634..e53a125c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/App.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -11,6 +11,7 @@ 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.configureSessionRevocationSync import com.mtusi.messenger.main.plugins.configureRouting import com.mtusi.messenger.main.plugins.configureStatusPages import com.mtusi.messenger.main.plugins.migrateDatabase @@ -31,6 +32,7 @@ fun Application.module() { configureDatabase() configureRedis() configureGracefulShutdown() + configureSessionRevocationSync() configureContentNegotiation() configureDefaultHeaders() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt index 7d948f5b..ce78889c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt @@ -7,4 +7,9 @@ interface AuthenticationConfig { val sessionCacheTtlSeconds: Long val sessionCacheSingleFlight: Boolean + /** + * How long a logout keeps trying to write its tombstone to Redis before it answers 503 instead of 204. + */ + val sessionRevokeTimeoutMs: Long + } diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt index 82431688..7ba9c360 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt @@ -7,5 +7,6 @@ data class SerialAuthenticationConfig( override val realm: String, override val sessionTtlSeconds: Long, override val sessionCacheTtlSeconds: Long, - override val sessionCacheSingleFlight: Boolean = true + override val sessionCacheSingleFlight: Boolean = true, + override val sessionRevokeTimeoutMs: Long = 8000 ) : AuthenticationConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt index b3db613b..26d91bab 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt @@ -20,8 +20,19 @@ interface AuthenticationService { token: String ): Session? + /** + * Deletes the session and confirms the revocation in the shared session cache, so that no replica accepts the + * token afterwards. Throws [com.mtusi.messenger.main.infrastructure.types.ApiException] (503) if the cache did + * not accept the tombstone in time: the logout is then not confirmed. + */ suspend fun revokeSession( session: Session ): Boolean + /** + * Called periodically on every replica: a revocation still waiting for its tombstone (written by any replica) + * means cached sessions cannot be trusted until it is written. + */ + suspend fun syncPendingRevocations() + } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt index c6a0fa14..c8953367 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt @@ -12,12 +12,15 @@ import com.mtusi.messenger.main.infrastructure.repositories.authentication.sessi import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository import com.mtusi.messenger.main.infrastructure.services.hashing.HashingService import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode import com.mtusi.messenger.main.plugins.suspendedTransaction import com.mtusi.messenger.main.utils.computeSha256 import io.lettuce.core.RedisException import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.delay import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.withContext import org.koin.core.annotation.Single @@ -31,6 +34,7 @@ import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicLong import kotlin.time.Clock import kotlin.time.Instant +import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds import kotlin.time.TimeSource @@ -38,6 +42,8 @@ private const val TOKEN_BYTES = 32 private val CACHE_RESTORE_BACKOFF = 1.seconds +private val REVOKE_RETRY_DELAY = 250.milliseconds + @Single class DefaultAuthenticationService( private val userRepository: UserRepository, @@ -177,14 +183,42 @@ class DefaultAuthenticationService( // A lookup that started before the delete must not be joined by requests arriving after the logout. inFlightLookups.remove(session.tokenHash) - if (withCache { sessionCacheRepository.revoke(session.tokenHash, session.expiresAt) } != null) { - // Best effort: a leftover row only costs one more tombstone write on the next restore. - runCatchingDatabase { sessionRevocationRepository.delete(session.tokenHash) } + // Other replicas may hold the session in the shared cache: 204 is only allowed once the tombstone is there. + // Redis that rejects or delays writes (CLIENT PAUSE WRITE, reconnect) gets retried until the deadline. + val deadline = TimeSource.Monotonic.markNow() + config.sessionRevokeTimeoutMs.milliseconds + + while (withCache { sessionCacheRepository.revoke(session.tokenHash, session.expiresAt) } == null) { + if (deadline.hasPassedNow()) { + // The row stays in session_revocations: every replica stops trusting the cache within a second + // (syncPendingRevocations) and the tombstone is written as soon as Redis accepts writes again. + throw ApiException(ErrorCode.UNAVAILABLE, "logout is not confirmed: session cache is unavailable") + } + delay(REVOKE_RETRY_DELAY) } + // Best effort: a leftover row only costs one more tombstone write on the next restore. + runCatchingDatabase { sessionRevocationRepository.delete(session.tokenHash) } + return isDeleted } + override suspend fun syncPendingRevocations() { + + val hasPending = runCatchingDatabase { + sessionRevocationRepository.findPending(Clock.System.now()).isNotEmpty() + } ?: return + + if (hasPending && trustedAtFailures == cacheFailures.get()) { + // Reuses the failure counter: an untrusted cache is bypassed until restoreCache writes the tombstones. + cacheFailures.incrementAndGet() + logger.warn("Pending session revocations found, cached sessions are not trusted until they are written") + } + + if (hasPending) { + restoreCache() + } + } + private suspend fun findCachedSession( tokenHash: String ): CachedSession? { diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt new file mode 100644 index 00000000..b0c7343e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt @@ -0,0 +1,36 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.services.authentication.AuthenticationService +import io.ktor.server.application.Application +import io.ktor.server.application.log +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import org.koin.ktor.ext.inject +import kotlin.time.Duration.Companion.seconds + +private val SYNC_INTERVAL = 1.seconds + +/** + * Shared signal between replicas: a logout that could not write its tombstone leaves a row in session_revocations + * (PostgreSQL). Every replica checks for such rows once a second and stops trusting cached sessions until the + * tombstones are written, so a replica that never saw the Redis failure itself does not keep accepting the token. + */ +fun Application.configureSessionRevocationSync() { + + val authenticationService by inject() + + launch { + while (isActive) { + delay(SYNC_INTERVAL) + try { + authenticationService.syncPendingRevocations() + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + log.warn("Pending revocation check failed: ${e.message}") + } + } + } +} diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 956da0f5..68da88bd 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -93,4 +93,8 @@ authentication { # Collapse concurrent cache misses for one token into a single database lookup (track 1 compares on/off). sessionCacheSingleFlight = true sessionCacheSingleFlight = ${?SESSION_CACHE_SINGLE_FLIGHT} + # A logout answers 204 only after its tombstone is in Redis; it retries up to this long, then answers 503. + # Below HAProxy `timeout server` (10 s). + sessionRevokeTimeoutMs = 8000 + sessionRevokeTimeoutMs = ${?SESSION_REVOKE_TIMEOUT_MS} } diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt new file mode 100644 index 00000000..0024b4dc --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt @@ -0,0 +1,152 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.config.authentication.SerialAuthenticationConfig +import com.mtusi.messenger.main.config.redis.SerialRedisConfig +import com.mtusi.messenger.main.infrastructure.dto.authentication.IssuedSession +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.ExposedSessionRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation.ExposedSessionRevocationRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.ExposedUserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.DefaultHashingService +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.applyDatabaseMigrations +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.support.TestRedis +import com.mtusi.messenger.main.support.uniqueUsername +import com.mtusi.messenger.main.utils.computeSha256 +import com.zaxxer.hikari.HikariDataSource +import kotlinx.coroutines.runBlocking +import org.jetbrains.exposed.v1.jdbc.Database +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import org.testcontainers.containers.GenericContainer +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +/** + * Two services sharing PostgreSQL and Redis model two API replicas. Redis keeps serving reads while writes are + * paused (CLIENT PAUSE ... WRITE), so the replica that did not handle the logout still finds the cached session. + */ +class CrossReplicaRevocationTest { + + private lateinit var redis: GenericContainer<*> + private val connections = mutableListOf() + + @BeforeTest + fun start() { + redis = TestRedis.create().also { it.start() } + HikariDataSource().apply { + jdbcUrl = TestDatabase.container.jdbcUrl + username = TestDatabase.USERNAME + password = TestDatabase.PASSWORD + }.use(::applyDatabaseMigrations) + TestDatabase.container.let { Database.connect(it.jdbcUrl, user = it.username, password = it.password) } + } + + @AfterTest + fun stop() { + connections.forEach { it.close() } + redis.stop() + } + + private fun replica(revokeTimeoutMs: Long): DefaultAuthenticationService { + val endpoint = TestRedis.endpoint(redis) + val connection = RedisConnection( + SerialRedisConfig( + host = endpoint.host, + port = endpoint.port, + password = TestRedis.PASSWORD, + connectTimeoutMs = 500, + commandTimeoutMs = 500 + ) + ).also(connections::add) + + return DefaultAuthenticationService( + userRepository = ExposedUserRepository(), + sessionRepository = ExposedSessionRepository(), + sessionCacheRepository = RedisSessionCacheRepository(connection), + sessionRevocationRepository = ExposedSessionRevocationRepository(), + hashingService = DefaultHashingService(), + config = SerialAuthenticationConfig( + realm = "messenger", + sessionTtlSeconds = 3600, + sessionCacheTtlSeconds = 60, + sessionRevokeTimeoutMs = revokeTimeoutMs + ) + ) + } + + private fun login(service: DefaultAuthenticationService): IssuedSession = transaction { + val user = checkNotNull(ExposedUserRepository().create(uniqueUsername("replica"), "replica", "not-a-real-hash")) + service.createSession(user.id) + } + + private fun cached(issued: IssuedSession): String { + return TestRedis.cli(redis, "GET", "session:${issued.token.toByteArray().computeSha256()}") + } + + private fun pauseWrites(ms: Long) { + assertEquals("OK", TestRedis.cli(redis, "CLIENT", "PAUSE", ms.toString(), "WRITE")) + } + + @Test + fun `logout waits for paused Redis writes and then no replica accepts the token`() = runBlocking { + val a = replica(revokeTimeoutMs = 8000) + val b = replica(revokeTimeoutMs = 8000) + val issued = login(a) + + // Both replicas have read the session, Redis holds the active copy. + assertNotNull(a.findSessionByToken(issued.token)) + assertNotNull(b.findSessionByToken(issued.token)) + assertTrue(cached(issued).contains(issued.session.id.toString())) + + pauseWrites(3000) + + val started = TimeSource.Monotonic.markNow() + a.revokeSession(issued.session) + assertTrue(started.elapsedNow() > 2.seconds, "the logout must wait for the tombstone, not answer early") + + assertEquals("revoked", cached(issued)) + assertNull(b.findSessionByToken(issued.token), "the other replica must reject the token") + assertNull(a.findSessionByToken(issued.token)) + } + + @Test + fun `unconfirmed logout makes every replica bypass the cache`() = runBlocking { + val a = replica(revokeTimeoutMs = 1000) + val b = replica(revokeTimeoutMs = 1000) + val issued = login(a) + + assertNotNull(a.findSessionByToken(issued.token)) + assertNotNull(b.findSessionByToken(issued.token)) + + pauseWrites(6000) + val paused = TimeSource.Monotonic.markNow() + + val error = assertFailsWith { a.revokeSession(issued.session) } + assertEquals(ErrorCode.UNAVAILABLE, error.code) + + // The periodic check (once a second on every replica) sees the pending revocation in PostgreSQL. + b.syncPendingRevocations() + assertNull(b.findSessionByToken(issued.token), "a replica that saw no Redis failure must not trust the cache") + assertNull(a.findSessionByToken(issued.token)) + + // Once Redis accepts writes again the tombstone is written and the cache is trusted again. + while (paused.elapsedNow() < 7.seconds) { + Thread.sleep(200) + } + b.syncPendingRevocations() + assertEquals("revoked", cached(issued)) + assertNull(b.findSessionByToken(issued.token)) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt index 4db94965..867d8e08 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt @@ -31,6 +31,11 @@ import kotlin.test.assertTrue import kotlin.time.Duration.Companion.seconds import kotlin.time.TimeSource +/** + * Short logout deadline: with Redis paused or down the tombstone cannot be written and the logout answers 503. + */ +private const val REVOKE_TIMEOUT_MS = 1000L + class DependencyFailureTest { /** @@ -54,7 +59,8 @@ class DependencyFailureTest { @Test fun `without Redis requests bypass the cache`() = runApp( - redis = TestRedis.unreachableEndpoint() + redis = TestRedis.unreachableEndpoint(), + sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS ) { client -> assertEquals(HttpStatusCode.OK, client.get("/health/ready").status) @@ -66,8 +72,9 @@ class DependencyFailureTest { client.assertAnonymousRejected() - // PostgreSQL is the source of truth: logout still works and is visible immediately. - assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + // Without the tombstone another replica could still accept the token from the cache, so the logout is not + // confirmed (503). The session is gone from PostgreSQL, so this replica rejects the token anyway. + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) } @@ -77,7 +84,7 @@ class DependencyFailureTest { val docker = redis.dockerClient try { - runApp(redis = TestRedis.endpoint(redis)) { client -> + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> val alice = client.register("alice") assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) @@ -89,7 +96,7 @@ class DependencyFailureTest { client.assertAnonymousRejected() - assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) docker.unpauseContainerCmd(redis.containerId).exec() @@ -128,19 +135,25 @@ class DependencyFailureTest { val docker = redis.dockerClient try { - runApp(redis = TestRedis.endpoint(redis)) { client -> + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> val alice = client.register("alice") assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) val copy = cachedCopy(redis, alice) docker.pauseContainerCmd(redis.containerId).exec() - assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) docker.unpauseContainerCmd(redis.containerId).exec() loseTombstone(redis, alice, copy) assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) - assertEquals("revoked", TestRedis.cli(redis, "GET", "session:${alice.token.toByteArray().computeSha256()}")) + + // The background check writes the pending tombstone once Redis accepts writes (retry every 1 s). + val deadline = TimeSource.Monotonic.markNow() + 3.seconds + while (TestRedis.cli(redis, "GET", "session:${alice.token.toByteArray().computeSha256()}") != "revoked") { + assertTrue(deadline.hasNotPassedNow(), "pending tombstone was not written after recovery") + Thread.sleep(200) + } } } finally { redis.stop() @@ -157,20 +170,20 @@ class DependencyFailureTest { lateinit var copy: String try { - runApp(redis = TestRedis.endpoint(redis)) { client -> + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> alice = client.register("alice") val bob = client.register("bob") chat = client.createChat(alice, "restart", listOf(alice.id, bob.id)) copy = cachedCopy(redis, alice) docker.pauseContainerCmd(redis.containerId).exec() - assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) } docker.unpauseContainerCmd(redis.containerId).exec() loseTombstone(redis, alice, copy) - runApp(redis = TestRedis.endpoint(redis)) { client -> + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/chats/${chat.id}/messages") { auth(alice) }.status) assertEquals( diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt index 669b36c6..9bc63210 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt @@ -58,6 +58,7 @@ private fun testConfig( databaseConnectionTimeoutMs: Long, redis: RedisEndpoint, idempotencyTtlSeconds: Long, + sessionRevokeTimeoutMs: Long, ) = HoconApplicationConfig( ConfigFactory.parseMap( mapOf( @@ -77,6 +78,7 @@ private fun testConfig( "authentication.realm" to "messenger", "authentication.sessionTtlSeconds" to sessionTtlSeconds, "authentication.sessionCacheTtlSeconds" to 60, + "authentication.sessionRevokeTimeoutMs" to sessionRevokeTimeoutMs, "messages.idempotencyTtlSeconds" to idempotencyTtlSeconds, "instance.id" to "test-instance", "cors.allowSameOrigin" to true, @@ -93,10 +95,11 @@ fun runApp( databaseConnectionTimeoutMs: Long = 10000, redis: RedisEndpoint = TestRedis.endpoint(), idempotencyTtlSeconds: Long = 86400, + sessionRevokeTimeoutMs: Long = 8000, block: suspend ApplicationTestBuilder.(HttpClient) -> Unit ) = testApplication { environment { - config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds) + config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds, sessionRevokeTimeoutMs) } application { module() -- 2.54.0