From cd84a6fd36456083fb0120a93694b2330ed86f78 Mon Sep 17 00:00:00 2001 From: Mraksimus Date: Mon, 16 Nov 2026 19:54:38 +0300 Subject: [PATCH 1/3] Move lab4 to lab5 --- .../skills/messenger-kotlin-exposed/SKILL.md | 30 ++ .../agents/openai.yaml | 4 + .../references/project-patterns.md | 50 +++ .dockerignore | 20 ++ .env.example | 4 +- .gitignore | 4 + AGENTS.md | 31 ++ Dockerfile | 23 ++ REPORT.md | 105 ++++-- build.gradle.kts | 125 +++++++ compose.yaml | 216 ++++++++++++ evidence/transfer-checks.txt | 29 ++ 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 +++++ infra/haproxy/haproxy.cfg | 57 ++++ infra/s3/bootstrap.sh | 20 ++ run/_template/config/application.conf | 12 + run/_template/config/authentication.conf | 9 + run/_template/config/cors.conf | 23 ++ run/_template/config/database.conf | 12 + run/_template/config/instance.conf | 4 + run/_template/config/messages.conf | 4 + run/_template/config/redis.conf | 8 + run/_template/config/storage.conf | 6 + scripts/attachment-failover.py | 59 ++++ scripts/attachment-interrupt.py | 39 +++ scripts/attachment-load.py | 84 +++++ scripts/check-s3-permissions.sh | 21 ++ scripts/explain-history.sh | 10 + scripts/idempotency-race.py | 84 +++++ scripts/init-secrets.sh | 19 ++ 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/restart.sh | 7 + scripts/revocation-pause.sh | 44 +++ scripts/rolling-update.sh | 40 +++ scripts/sql/explain-history.sql | 100 ++++++ scripts/stop-one.sh | 22 ++ settings.gradle.kts | 21 ++ .../kotlin/com/mtusi/messenger/main/App.kt | 50 +++ .../serializers/HttpMethodSerializer.kt | 31 ++ .../main/compat/serializers/UUIDSerializer.kt | 31 ++ .../messenger/main/config/ConfigModule.kt | 8 + .../authentication/AuthenticationConfig.kt | 15 + .../DefaultAuthenticationConfig.kt | 11 + .../SerialAuthenticationConfig.kt | 12 + .../messenger/main/config/cors/CorsConfig.kt | 100 ++++++ .../main/config/cors/DefaultCorsConfig.kt | 11 + .../main/config/cors/SerialCorsConfig.kt | 18 + .../main/config/database/DatabaseConfig.kt | 22 ++ .../config/database/DefaultDatabaseConfig.kt | 11 + .../config/database/SerialDatabaseConfig.kt | 17 + .../config/message/DefaultMessageConfig.kt | 11 + .../main/config/message/MessageConfig.kt | 5 + .../config/message/SerialMessageConfig.kt | 8 + .../main/config/redis/DefaultRedisConfig.kt | 11 + .../main/config/redis/RedisConfig.kt | 15 + .../main/config/redis/SerialRedisConfig.kt | 14 + .../config/storage/DefaultStorageConfig.kt | 12 + .../config/storage/SerialStorageConfig.kt | 13 + .../main/config/storage/StorageConfig.kt | 10 + .../main/extensions/ApplicationCall.kt | 37 ++ .../_common/parameters/PageParameters.kt | 31 ++ .../attachment/AttachmentController.kt | 106 ++++++ .../AuthenticationController.kt | 53 +++ .../responses/SessionResponse.kt | 13 + .../controllers/chat/ChatController.kt | 112 ++++++ .../chat/requests/CreateChatRequest.kt | 9 + .../controllers/health/HealthController.kt | 67 ++++ .../health/responses/HealthResponse.kt | 6 + .../controllers/message/MessageController.kt | 94 +++++ .../message/requests/CreateMessageRequest.kt | 9 + .../controllers/user/UserController.kt | 83 +++++ .../user/requests/CreateUserRequest.kt | 10 + .../main/infrastructure/dto/Attachment.kt | 21 ++ .../messenger/main/infrastructure/dto/Chat.kt | 13 + .../main/infrastructure/dto/Message.kt | 15 + .../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/attachment/AttachmentModel.kt | 16 + .../attachment/MessageAttachmentModel.kt | 11 + .../models/attachment/UploadJournalModel.kt | 18 + .../models/authentication/SessionModel.kt | 17 + .../authentication/SessionRevocationModel.kt | 11 + .../models/chat/ChatMemberModel.kt | 17 + .../infrastructure/models/chat/ChatModel.kt | 12 + .../message/MessageIdempotencyKeyModel.kt | 25 ++ .../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 +++++ .../attachment/AttachmentRepository.kt | 20 ++ .../attachment/ExposedAttachmentRepository.kt | 88 +++++ .../ExposedUploadJournalRepository.kt | 77 +++++ .../attachment/UploadJournalRepository.kt | 13 + .../storage/ObjectStorageRepository.kt | 19 ++ .../storage/S3ObjectStorageRepository.kt | 116 +++++++ .../session/ExposedSessionRepository.kt | 73 ++++ .../session/SessionRepository.kt | 26 ++ .../session/cache/CachedSession.kt | 11 + .../cache/RedisSessionCacheRepository.kt | 87 +++++ .../session/cache/SessionCacheRepository.kt | 23 ++ .../ExposedSessionRevocationRepository.kt | 52 +++ .../revocation/SessionRevocationRepository.kt | 25 ++ .../repositories/chat/ChatRepository.kt | 30 ++ .../chat/ExposedChatRepository.kt | 134 ++++++++ .../message/ExposedMessageRepository.kt | 99 ++++++ .../repositories/message/MessageRepository.kt | 26 ++ .../ExposedMessageIdempotencyRepository.kt | 83 +++++ .../MessageIdempotencyRepository.kt | 43 +++ .../user/ExposedUserRepository.kt | 82 +++++ .../repositories/user/UserRepository.kt | 28 ++ .../infrastructure/services/ServicesModule.kt | 8 + .../attachment/AttachmentCleanupService.kt | 34 ++ .../services/attachment/AttachmentService.kt | 28 ++ .../attachment/DefaultAttachmentService.kt | 94 +++++ .../services/attachment/ImageValidator.kt | 127 +++++++ .../authentication/AuthenticationService.kt | 38 +++ .../DefaultAuthenticationService.kt | 323 ++++++++++++++++++ .../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 | 136 ++++++++ .../services/message/MessageService.kt | 24 ++ .../message/results/MessageCreationResult.kt | 17 + .../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 + .../main/plugins/AttachmentCleanup.kt | 29 ++ .../messenger/main/plugins/CallLogging.kt | 14 + .../main/plugins/ContentNegotiation.kt | 21 ++ .../com/mtusi/messenger/main/plugins/Cors.kt | 13 + .../mtusi/messenger/main/plugins/Database.kt | 181 ++++++++++ .../messenger/main/plugins/DefaultHeaders.kt | 7 + .../main/plugins/GracefulShutdown.kt | 38 +++ .../messenger/main/plugins/InstanceId.kt | 28 ++ .../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 | 20 ++ .../main/plugins/SessionRevocationSync.kt | 36 ++ .../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 | 55 +++ .../com/mtusi/messenger/main/utils/Secrets.kt | 10 + .../messenger/main/utils/TemporaryUpload.kt | 17 + .../com/mtusi/messenger/main/utils/Time.kt | 14 + .../mtusi/messenger/main/utils/Validation.kt | 45 +++ .../exposed/operations/InstantPlusSeconds.kt | 25 ++ .../utils/exposed/operations/RowGreater.kt | 32 ++ src/main/resources/application.conf | 112 ++++++ src/main/resources/logback.xml | 12 + src/main/resources/migrations/V1__init.sql | 50 +++ .../migrations/V2__session_revocations.sql | 6 + .../V3__message_idempotency_keys.sql | 19 ++ .../resources/migrations/V4__attachments.sql | 19 ++ .../migrations/V5__attachment_uploads.sql | 7 + .../attachment/AttachmentControllerTest.kt | 139 ++++++++ .../AuthenticationControllerTest.kt | 151 ++++++++ .../controllers/chat/ChatControllerTest.kt | 152 +++++++++ .../health/HealthControllerTest.kt | 22 ++ .../message/MessageConcurrencyTest.kt | 77 +++++ .../message/MessageControllerTest.kt | 197 +++++++++++ .../message/MessageIdempotencyTest.kt | 158 +++++++++ .../controllers/user/UserControllerTest.kt | 180 ++++++++++ .../cache/RedisSessionCacheRepositoryTest.kt | 83 +++++ .../chat/ExposedChatRepositoryTest.kt | 62 ++++ .../message/MessageAttachmentsQueryTest.kt | 58 ++++ .../attachment/AttachmentCleanupTest.kt | 124 +++++++ .../services/attachment/ImageValidatorTest.kt | 84 +++++ .../CrossReplicaRevocationTest.kt | 156 +++++++++ .../SessionLookupSingleFlightTest.kt | 175 ++++++++++ .../hashing/DefaultHashingServiceTest.kt | 45 +++ .../mtusi/messenger/main/plugins/CorsTest.kt | 26 ++ .../main/plugins/DatabaseMigrationTest.kt | 49 +++ .../main/plugins/DependencyFailureTest.kt | 237 +++++++++++++ .../messenger/main/plugins/InstanceIdTest.kt | 30 ++ .../main/plugins/SessionCacheTest.kt | 65 ++++ .../mtusi/messenger/main/support/TestApp.kt | 170 +++++++++ .../messenger/main/support/TestDatabase.kt | 48 +++ .../mtusi/messenger/main/support/TestRedis.kt | 54 +++ .../messenger/main/support/TestStorage.kt | 45 +++ .../main/utils/TemporaryUploadTest.kt | 43 +++ 208 files changed, 9611 insertions(+), 25 deletions(-) create mode 100644 .agents/skills/messenger-kotlin-exposed/SKILL.md create mode 100644 .agents/skills/messenger-kotlin-exposed/agents/openai.yaml create mode 100644 .agents/skills/messenger-kotlin-exposed/references/project-patterns.md create mode 100644 AGENTS.md create mode 100644 Dockerfile create mode 100644 build.gradle.kts create mode 100644 compose.yaml create mode 100644 evidence/transfer-checks.txt 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 infra/haproxy/haproxy.cfg create mode 100644 infra/s3/bootstrap.sh create mode 100644 run/_template/config/application.conf create mode 100644 run/_template/config/authentication.conf create mode 100644 run/_template/config/cors.conf create mode 100644 run/_template/config/database.conf create mode 100644 run/_template/config/instance.conf create mode 100644 run/_template/config/messages.conf create mode 100644 run/_template/config/redis.conf create mode 100644 run/_template/config/storage.conf create mode 100644 scripts/attachment-failover.py create mode 100644 scripts/attachment-interrupt.py create mode 100644 scripts/attachment-load.py create mode 100755 scripts/check-s3-permissions.sh create mode 100755 scripts/explain-history.sh create mode 100755 scripts/idempotency-race.py create mode 100755 scripts/init-secrets.sh 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/restart.sh create mode 100755 scripts/revocation-pause.sh create mode 100755 scripts/rolling-update.sh create mode 100644 scripts/sql/explain-history.sql create mode 100755 scripts/stop-one.sh 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/HttpMethodSerializer.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/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/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/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/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/config/storage/DefaultStorageConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/storage/SerialStorageConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/storage/StorageConfig.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/attachment/AttachmentController.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/Attachment.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/attachment/AttachmentModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/MessageAttachmentModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/UploadJournalModel.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/MessageIdempotencyKeyModel.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/attachment/AttachmentRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedUploadJournalRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/UploadJournalRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.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/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/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/attachment/AttachmentCleanupService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.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/AttachmentCleanup.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/Cors.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/GracefulShutdown.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.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/SessionRevocationSync.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/TemporaryUpload.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/InstantPlusSeconds.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/main/resources/migrations/V3__message_idempotency_keys.sql create mode 100644 src/main/resources/migrations/V4__attachments.sql create mode 100644 src/main/resources/migrations/V5__attachment_uploads.sql create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt 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/message/MessageIdempotencyTest.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/repositories/message/MessageAttachmentsQueryTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidatorTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.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/CorsTest.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/InstanceIdTest.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 create mode 100644 src/test/kotlin/com/mtusi/messenger/main/support/TestStorage.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/utils/TemporaryUploadTest.kt diff --git a/.agents/skills/messenger-kotlin-exposed/SKILL.md b/.agents/skills/messenger-kotlin-exposed/SKILL.md new file mode 100644 index 00000000..8998de94 --- /dev/null +++ b/.agents/skills/messenger-kotlin-exposed/SKILL.md @@ -0,0 +1,30 @@ +--- +name: messenger-kotlin-exposed +description: Implement and review Kotlin repository, service, transaction, DI and library-DSL changes in this messenger project using its existing Exposed JDBC conventions. Use when editing persistence or its Kotlin callers, rather than for unrelated infrastructure or contract-only work. +--- + +# Messenger Kotlin and Exposed + +Use the project's existing implementations to choose APIs, layout and transaction boundaries. Repository-root paths below identify live sources; inspect the relevant ones instead of copying snippets or rereading the whole application. + +## Choose the smallest useful context + +- For CRUD, start with the affected interface/model and `ExposedUserRepository` or `ExposedSessionRepository`. +- For multi-write operations or pagination, use `ExposedChatRepository` and its controller/test. +- For services/DI, use `DefaultChatService`, `RepositoriesModule` and `plugins/Koin.kt`. +- Read only the relevant section of [project patterns](references/project-patterns.md) for source links, transaction ownership, pinned dependencies and checks. +- The attachment/upload/storage implementations are not default style anchors merely because they are nearby; compare their changes with the established examples above. + +## Implement in the project's idiom + +- Follow the chosen analogue's multiline signatures and constructor parameters, named arguments, vertical query chains, small mapping functions and blank lines between logical stages. +- Keep Exposed **1.0.0** and its `org.jetbrains.exposed.v1.*` imports; recheck `gradle/libs.versions.toml` if dependencies have changed. +- Use the Table/column DSL for CRUD and expressions. Check `utils/exposed/operations` before introducing a separate SQL path. +- Before raw SQL, establish which required operation the pinned DSL and existing extensions cannot express; explain that limitation and parameterize values in the smallest fallback. Migration SQL is a separate, existing workflow. +- Follow the caller's transaction boundary. Ordinary repositories operate inside it so related writes commit/roll back together; reuse `plugins.suspendedTransaction` at the existing orchestration layer rather than wrapping each repository method. +- Use Kotlin library DSLs only where the dependency provides them. The project has Ktor/Koin/Exposed DSLs and the AWS SDK for Kotlin; builders in Java-based integrations can still be appropriate. Check the dependency and local wrapper before deciding. +- Preserve `@Single`, constructor injection, interface-based dependencies and existing Koin/KSP module discovery. + +## Finish the change + +Compare the complete diff with the chosen analogues, including other code affected by the user's applicable corrections. Run the relevant checks from the reference, scale them to the change, and state which ran. A passing compile alone does not establish query semantics or transaction atomicity. diff --git a/.agents/skills/messenger-kotlin-exposed/agents/openai.yaml b/.agents/skills/messenger-kotlin-exposed/agents/openai.yaml new file mode 100644 index 00000000..b51e1962 --- /dev/null +++ b/.agents/skills/messenger-kotlin-exposed/agents/openai.yaml @@ -0,0 +1,4 @@ +interface: + display_name: "Messenger Kotlin/Exposed" + short_description: "Project Kotlin DSL, Exposed and transaction patterns" + default_prompt: "Use $messenger-kotlin-exposed to implement this change using the messenger project conventions." diff --git a/.agents/skills/messenger-kotlin-exposed/references/project-patterns.md b/.agents/skills/messenger-kotlin-exposed/references/project-patterns.md new file mode 100644 index 00000000..18279adf --- /dev/null +++ b/.agents/skills/messenger-kotlin-exposed/references/project-patterns.md @@ -0,0 +1,50 @@ +# Project patterns + +Read the section relevant to the change. Source links point to the repository's current files, not frozen examples. + +## Dependencies and build + +- [Version catalog](../../../../gradle/libs.versions.toml): Kotlin **2.3.20**, Exposed **1.0.0** (`core`, `jdbc`, `kotlin-datetime`, migration modules). Preserve `org.jetbrains.exposed.v1.*` imports and follow the affected model's types. +- [Settings](../../../../settings.gradle.kts): Ktor catalog **3.3.0**, AWS SDK for Kotlin catalog **1.6.41**. Do not substitute MinIO Java builders for the installed Kotlin SDK. +- [Build](../../../../build.gradle.kts): JVM **21**, Koin annotations/KSP with configuration checking, JUnit Platform. There is no configured ktlint/detekt task to assume or claim ran. + +The catalogs/build files remain the source of truth if versions or plugins change. Check API details against those versions when the local examples do not establish an operation. + +## CRUD, layout and query expressions + +- [ExposedUserRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt): multiline function parameters, `insertIgnore`, `selectAll().where(...)` split into vertical chains, nullable results and `ResultRow` mapping. +- [ExposedSessionRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt): `insert`, `deleteWhere`, typed time predicates and result mapping. +- [ExposedChatRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt): related writes via `insert`/`batchInsert`, joins, keyset pagination and grouped member loading. Keep result ordering and transaction atomicity intact. +- [ExposedMessageIdempotencyRepository](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt): conflict handling, `update`, `deleteWhere` and reusable typed conditions. +- [RowGreater](../../../../src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt) and [InstantPlusSeconds](../../../../src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt): existing typed expression extensions. Inspect the relevant operation before implementing another SQL escape hatch. +- [ChatModel](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt): the model/table namespace used by queries. Read the affected feature's own model before changing its columns or mapping. + +These are focused references, not a requirement to open every listed file on every task. Prefer User/Chat for established layout; newer attachment/storage code is not automatically the style authority. + +## Transaction ownership + +- [Database.kt](../../../../src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt) defines `suspendedTransaction` using `newSuspendedTransaction` on `Dispatchers.IO`. The database configuration sets `defaultMaxAttempts = 1`; do not silently introduce retries around side effects. +- [ChatController](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt) and [UserController](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt) open transactions around their service calls. [DefaultChatService](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt) and ordinary Exposed repositories assume that active transaction. +- Authentication and attachment orchestration also own transactions in services. Inspect the actual caller of the method you change; do not impose a controller-only rule or create nested/per-operation boundaries. +- [ExposedChatRepositoryTest](../../../../src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt) demonstrates caller-owned transactions and rollback when a related write fails. +- Flyway migrations live in `src/main/resources/migrations`. Database.kt's `generateMigration` connects to the development database and applies existing migrations before generating SQL; it is not a harmless formatting/check command. + +## DI and library API choice + +- [RepositoriesModule](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt) and [ServicesModule](../../../../src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt) use `@Module`/`@ComponentScan`. +- [Koin.kt](../../../../src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt) installs the generated modules; concrete implementations use `@Single` and constructor dependencies. Reuse this flow instead of adding a parallel registry. +- Ktor routing, Koin modules, Exposed queries and the installed AWS Kotlin SDK expose Kotlin DSLs. Check the affected dependency's real API: Java integrations such as Hikari/Lettuce already use builders/configuration objects, so a blanket builder ban would conflict with the project. + +## Focused verification + +From the repository root, choose the checks that exercise the change: + +```sh +./gradlew compileKotlin +./gradlew compileTestKotlin +./gradlew test --tests '*ExposedChatRepositoryTest' +``` + +Replace the test selector with the affected class; use `./gradlew test` when broader coverage is warranted. [TestApp](../../../../src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt) and [TestDatabase](../../../../src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt) show that API/database tests use Testcontainers and require Docker; those API helpers also start Redis and S3 test dependencies. + +[Makefile](../../../../Makefile) provides `make check` for offline structure/contract checks and `make test` for smoke checks against a running API (`BASE_URL` defaults to `http://localhost:8080`). Use the existing Gradle wrapper; do not invent lint tasks or count an unavailable/skipped check as passing. Instruction-only changes do not need an application build or test suite. diff --git a/.dockerignore b/.dockerignore index bc16e556..28532a6d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,23 @@ __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 + +archive diff --git a/.env.example b/.env.example index 119384b5..3d65fa79 100644 --- a/.env.example +++ b/.env.example @@ -14,8 +14,8 @@ IDEMPOTENCY_TTL_SECONDS=86400 S3_ENDPOINT=http://s3:9000 S3_REGION=us-east-1 S3_BUCKET=messenger-attachments -S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id -S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key +S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_api_access_key_id +S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_api_secret_access_key MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 BROKER_URL_FILE=/run/secrets/broker_url 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/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..66442be8 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,31 @@ +# Project working rules + +## Start with the relevant code + +- This is a Kotlin/JVM messenger: Ktor, Koin/KSP, Exposed JDBC, PostgreSQL, Redis and S3. +- Before implementing, read the nearest interface, a good analogous implementation and its caller/test. Expand the search only as needed. +- Use `ExposedUserRepository`, `ExposedChatRepository` and `DefaultChatService` as established style anchors; proximity alone does not make new code an example to copy. +- For Kotlin persistence, transaction boundaries, DI or library DSL changes, use [messenger-kotlin-exposed](.agents/skills/messenger-kotlin-exposed/SKILL.md). + +## Match the project + +- Keep the existing controller → service → repository separation and reuse local helpers before adding abstractions. +- Follow the anchors' multiline declarations, named arguments, vertically chained calls and spacing between logical steps. Do not compress them into dense one-liners. +- Prefer the library DSLs and project wrappers actually available in the pinned dependencies. Do not invent a DSL or replace a library just to avoid a builder. +- Exposed is **1.0.0** with `org.jetbrains.exposed.v1.*` imports; `gradle/libs.versions.toml` is the source of truth. +- Express ordinary database operations with Exposed DSL. Before introducing raw SQL, check the pinned API and existing extensions, and identify the concrete operation they cannot express. Keep a necessary fallback small and parameterized; SQL migration files remain appropriate. +- Repositories use the caller's current transaction. Preserve the existing controller/service ownership of `suspendedTransaction`; do not open a separate transaction for each repository operation. +- Reuse Koin `@Single`, constructor injection and existing component-scanned modules; follow the affected feature's registration pattern. +- Apply a user's correction to other relevant code in the current change, not only to the reported line. Preserve the scope of the requested task. +- Preserve unrelated API contracts and test expectations. Update them when the requested behavior requires it, rather than to hide an implementation failure. + +## Verify the actual change + +- Use the Gradle wrapper and JVM 21. Inspect `build.gradle.kts` when build behavior matters. +- Compile application changes with `./gradlew compileKotlin`; include `compileTestKotlin` when test sources change. +- Run the relevant tests, for example `./gradlew test --tests '*ExposedChatRepositoryTest'`; database/API tests use Testcontainers and need Docker. +- Use `./gradlew test` when the change warrants the broader Kotlin suite. +- `make check` validates contract/repository structure offline; `make test` runs smoke checks against a running API. They serve different purposes. +- Documentation/instruction-only edits need link/content checks, not an application build or full test suite. +- Review the complete diff for behavior, consistency with the selected analogues, accidental SQL/builders, formatting and unrelated edits before finishing. +- Report what changed and which checks actually ran, including any check blocked by the local environment. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..2f60fd5f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +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 +# 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 +# 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 e98f75e9..33ade9d4 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,96 @@ # Отчёт — лабораторная 5 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Кучерявенко Максим, БВТ2303. +- Текущий этап: перенос работающей основы lab4 для просмотра перед выполнением lab5. +- Целевая оценка: будет выбрана при переходе к реализации; работа ведётся поэтапно, начиная с уровня 3. +- Commit/PR: после просмотра и публикации. +- Основа: Kotlin 2.3.20, Ktor 3.3.0, JDK 21, Koin/KSP, Exposed 1.0.0, PostgreSQL, Redis и MinIO. Существующие архитектура controller → service → repository, DSL и стиль кода сохранены; ориентир — проекты Pixefy. +- Источники: исходники собственного lab4, задание и контракты lab5, проекты Pixefy. ИИ использован для переноса и проверки структуры проекта. -## Запуск из чистого clone +## Что перенесено -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +- Исходники приложения и 80 JVM-тестов, конфигурации, Gradle wrapper и зависимости. +- Миграции Flyway V1–V5: пользователи, чаты, сообщения, сессии, идемпотентность, вложения и журнал загрузок. +- Bearer-аутентификация, Redis-кэш с fallback в PostgreSQL, keyset-пагинация и идемпотентная отправка сообщений. +- Синхронные upload/download PNG/JPEG, приватный bucket, проверки доступа и изображений, временные файлы и очистка по журналу. +- Dockerfile, Compose с двумя API-репликами, HAProxy, PostgreSQL, Redis, MinIO и повторяемым bootstrap. +- Скрипты запуска, проверки S3-прав, прерывания загрузки, нагрузки, переключения реплик и перезапуска стенда. +- Проектные инструкции AGENTS.md и локальный skill Kotlin/Exposed для дальнейшего соблюдения архитектуры и стиля. -## Архитектура и решения +Имя Compose/Gradle-проекта изменено на `mtusi-messenger-lab5`, образ приложения — на `mtusi-messenger:lab5`. Это отделяет контейнеры, сети, named volumes и образ от lab4. Оригиналы и записи БД из работающего стенда lab4 не переносились: lab5 получает собственные volumes при запуске. -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Исходные README задания, lab.json, contracts/, публичные tests/, examples/, CI и шаблон PR лабораторной 5 сохранены. В `.env.example` сохранены новые настройки broker/worker/jobs/staging; пути S3 credentials согласованы с отдельным пользователем API. -## Доказательства критериев +Исторические материалы lab4 находятся в [archive/lab4](archive/lab4/README.md): исходный отчёт, evidence и документы к защите. Их результаты не являются доказательствами выполнения lab5. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +## Запуск перенесённой основы из чистого clone -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Нужны Docker с Compose v2, Python 3.9+ и openssl. Для Gradle-проверок — JDK 21 и Docker для Testcontainers. -## Эксперименты и отказы +```sh +./scripts/init-secrets.sh -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +# Отдельный порт, чтобы не мешать lab4 на 8084 и другим лабораторным. +export LAB_HTTP_PORT=8085 +export BASE_URL=http://localhost:8085 -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +docker compose config --quiet +docker compose up -d --build --wait +docker compose ps -## Ограничения и следующие шаги +make check +./gradlew test -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +# Безопасная остановка с сохранением данных: +docker compose down +# Только для намеренного удаления всех учебных данных lab5: +# docker compose down -v +``` + +Секреты создаются заново локальным скриптом; секреты lab4, локальная `.env`, runtime-конфигурации, build/cache и `.git` не копировались. `./gradlew run` создаёт недостающие конфигурации из `run/_template`, сохраняя существующие локальные настройки. Для запуска вне Compose необходимо отдельно настроить доступные с host PostgreSQL, Redis и S3. + +### Настройки инфраструктуры + +- `LAB_HTTP_PORT` меняет только опубликованный порт HAProxy; внутри API остаётся 8080. В Compose по умолчанию две API-реплики; их число задаётся `API_REPLICAS`. +- `SESSION_TTL_SECONDS`, `IDEMPOTENCY_TTL_SECONDS` и `S3_BUCKET` передаются через Compose. Доступ к PostgreSQL и Redis использует существующие `DB_*` и `REDIS_*`, а не предложенные skeleton `DATABASE_URL_FILE` и `CACHE_URL_FILE`. +- API обращается к `http://s3:9000`; `S3_ACCESS_KEY_ID_FILE` и `S3_SECRET_ACCESS_KEY_FILE` указывают на secret-файлы прикладного S3-пользователя. Административные credentials доступны только MinIO и bootstrap. +- Образы и зависимости сохранены из lab4: PostgreSQL 17.6, Redis 8.4.0, HAProxy 3.2.4; MinIO RELEASE.2025-09-07T16-13-09Z и mc RELEASE.2025-08-13T08-35-41Z закреплены digest. API имеет лимиты 1 CPU / 1 GiB, временный `/tmp` — tmpfs 32 MiB. +- `S3_REGION=us-east-1`, пределы 10 MiB и 40 млн пикселей закреплены в реализации. `APP_ENV` и переключатель `SESSION_STORE` не меняют реализацию. +- Переменные `BROKER_URL_FILE`, `WORKER_CONCURRENCY`, `JOB_MAX_ATTEMPTS`, `JOB_TIMEOUT_SECONDS`, `STAGING_RETENTION_SECONDS` пока являются частью skeleton и не используются перенесённым приложением. + +## Архитектура текущего этапа + +Клиент → HAProxy → API × 2 → PostgreSQL + Redis + MinIO. + +Это основа lab4: upload возвращает **201 ready** после S3 PUT и COMMIT метаданных. Фоновой обработки изображений нет. Из фоновых задач перенесён только существующий уборщик незавершённых загрузок, который не является worker lab5. + +## Проверки переноса + +Проверки переноса выполнены 16.11.2026: + +| Проверка | Результат | +| --- | --- | +| `rtk gradlew compileKotlin compileTestKotlin --console=plain` | BUILD SUCCESSFUL; исходники приложения и всех JVM-тестов компилируются. | +| `rtk gradlew test --tests '*ImageValidatorTest' --tests '*TemporaryUploadTest' --tests '*DefaultHashingServiceTest' --console=plain` | 9 тестов, 0 failures/errors/skipped. | +| `make check` | PASS: структура skeleton, локальные ссылки и Python-синтаксис. | +| `docker compose config --quiet` | Конфигурация корректна; это не запуск контейнеров. | +| Сравнение с исходным lab4 | 176 файлов src/, gradle/, run/_template/, infra/haproxy/ и infra/s3/ совпадают побайтно; executable bit скриптов сохранён. | +| Сохранность skeleton lab5 | 32 исходных файла задания, контрактов, публичных проверок и связанных материалов не изменены. | + +Краткий протокол: [evidence/transfer-checks.txt](evidence/transfer-checks.txt). + +Docker daemon сейчас недоступен. Полный набор Testcontainers-тестов и сам Compose-стенд в этой проверке не запускались. Исторические 80 успешных тестов в archive/lab4/evidence относятся к lab4, а не к новому прогону lab5. Эти проверки переноса не подтверждают асинхронный pipeline lab5. + +`make test` по неизменённому lab.json запускает публичный профиль **lab5**. Он требует 202 queued и thumbnail, поэтому пока не используется как успешная проверка перенесённой основы. После начала выполнения lab5 проверяться будет именно этот контракт; менять его на профиль lab4 не требуется. + +## Следующий этап после просмотра + +Реализация ещё не начата: предстоит выбрать durable broker, добавить отдельный worker, устойчивый staging и задания, состояния queued/processing/ready/failed, thumbnail, ограниченные повторы и идемпотентность обработки. Затем будут выполнены проверки уровня 3. Пункты уровней 4 и 5 рассматриваются отдельно после предыдущего этапа. + +## Ограничения + +- Контракт lab5 ещё не реализован; готовность на оценку 3/4/5 не заявляется. +- Унаследованные attachment load/failover-скрипты ожидают синхронный 201. При переходе на 202 их потребуется адаптировать вместе с реализацией. +- Валидация использует ограниченный массив bytes и полный raster ImageIO; крайний случай памяти очень широких PNG остаётся ограничением исходной основы. +- PostgreSQL, MinIO и HAProxy одноузловые. Volumes обеспечивают сохранность при restart, но не заменяют backup и репликацию. +- Журнал lab4 очищает учтённые незавершённые загрузки, а не выполняет глобальную сверку bucket. Записи abandoned сохраняются для удаления поздних PUT и могут накапливаться. diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 00000000..97d10253 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,125 @@ +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.cors) + 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(awssdk.services.s3) + + 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 = "com.mtusi.messenger.main.AppKt" + 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") { + + workingDir = file("run/development") + + doFirst { + val configDir = file("run/development/config") + configDir.mkdirs() + file("run/_template/config").listFiles() + ?.filter { it.isFile && it.extension == "conf" } + ?.forEach { template -> + val target = configDir.resolve(template.name) + if (!target.exists()) template.copyTo(target) + } + + args = configDir.listFiles() + ?.filter { it.extension == "conf" } + ?.sortedBy { it.name } + ?.map { "-config=config/${it.name}" } + ?: emptyList() + } + } + + 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..c8ce9309 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,216 @@ +name: mtusi-messenger-lab5 + +services: + + postgres: + image: postgres:17.6-alpine3.22 + 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 + deploy: + resources: + limits: + cpus: "1.0" + memory: 1g + restart: unless-stopped + + redis: + image: redis:8.4.0-alpine + # 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 + deploy: + resources: + limits: + cpus: "0.5" + memory: 128m + restart: unless-stopped + + s3: + # MinIO RELEASE.2025-09-07T16-13-09Z, pinned multi-platform image. + image: quay.io/minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + command: ["server", "/data", "--console-address", "127.0.0.1:9001"] + environment: + MINIO_ROOT_USER_FILE: /run/secrets/s3_access_key_id + MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_secret_access_key + secrets: + - s3_access_key_id + - s3_secret_access_key + volumes: + - s3-data:/data + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] + interval: 5s + timeout: 3s + retries: 10 + deploy: + resources: + limits: + cpus: "1.0" + memory: 512m + restart: unless-stopped + + s3-init: + # mc RELEASE.2025-08-13T08-35-41Z + image: quay.io/minio/mc@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727 + depends_on: + s3: + condition: service_healthy + entrypoint: ["/bin/sh", "/bootstrap.sh"] + environment: + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + secrets: + - s3_access_key_id + - s3_secret_access_key + - s3_api_access_key_id + - s3_api_secret_access_key + volumes: + - ./infra/s3/bootstrap.sh:/bootstrap.sh:ro + restart: "no" + + # One-shot: applies Flyway migrations once and exits. Replicas start only after it succeeded, + # so they never race each other on the schema. + migrate: + image: mtusi-messenger:lab5 + command: ["migrate"] + healthcheck: + test: ["NONE"] + 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 + deploy: + resources: + limits: + cpus: "0.5" + memory: 384m + restart: "no" + + api: + build: . + image: mtusi-messenger:lab5 + # Interchangeable replicas: no container_name and no published port, traffic only through `lb`. + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: "1.0" + memory: 1g + depends_on: + migrate: + condition: service_completed_successfully + postgres: + condition: service_healthy + redis: + condition: service_healthy + s3-init: + condition: service_completed_successfully + 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 + 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} + SHUTDOWN_DRAIN_DELAY_MS: "3000" + S3_ENDPOINT: http://s3:9000 + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret_access_key + secrets: + - db_password + - redis_password + - s3_api_access_key_id + - s3_api_secret_access_key + # 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. + tmpfs: + - /tmp:size=32m,mode=1777 + stop_grace_period: 10s + 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:${LAB_HTTP_PORT:-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: + db_password: + file: ./secrets/db_password + redis_password: + file: ./secrets/redis_password + + s3_access_key_id: + file: ./secrets/s3_access_key_id + s3_secret_access_key: + file: ./secrets/s3_secret_access_key + + s3_api_access_key_id: + file: ./secrets/s3_api_access_key_id + s3_api_secret_access_key: + file: ./secrets/s3_api_secret_access_key + +volumes: + postgres-data: + s3-data: diff --git a/evidence/transfer-checks.txt b/evidence/transfer-checks.txt new file mode 100644 index 00000000..c6a81542 --- /dev/null +++ b/evidence/transfer-checks.txt @@ -0,0 +1,29 @@ +Date: 2026-11-16 +Stage: lab4 baseline transfer only; lab5 pipeline not implemented. + +Command: rtk gradlew compileKotlin compileTestKotlin --console=plain +BUILD SUCCESSFUL in 21s +6 actionable tasks: 6 executed + +Command: rtk gradlew test --tests '*ImageValidatorTest' --tests '*TemporaryUploadTest' --tests '*DefaultHashingServiceTest' --console=plain +BUILD SUCCESSFUL in 2s +Tests from JUnit XML: 9; failures: 0; errors: 0; skipped: 0 +ImageValidatorTest: 3 +TemporaryUploadTest: 2 +DefaultHashingServiceTest: 4 + +Command: make check +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. + +Command: docker compose config --quiet +Exit code: 0 + +Content comparisons: +32 protected lab5 skeleton files unchanged. +176 source/application/template/infra files identical to lab4. +Executable bits preserved for Gradle wrapper and operational scripts. + +Runtime limitation: +Docker daemon unavailable. Full Testcontainers suite and Compose runtime were not run. +Lab5 public smoke was not run: implementation still has the inherited lab4 synchronous 201 ready flow. +Historical lab4 evidence is stored separately in archive/lab4/. 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<6rCRJ6fPD^hfrh;0K?n z=p!u^3xOgWZ%f3+?+>H)9+w^$Tn1e;?UpVMJb!!;f)`6f&4|8mr+g)^@x>_rvnL0< zvD0Hu_N>$(Li7|Jgu0mRh&MV+<}`~Wi*+avM01E)Jtg=)-vViQKax!GeDc!xv$^mL z{#OVBA$U{(Zr8~Xm|cP@odkHC*1R8z6hcLY#N@3E-A8XEvpt066+3t9L_6Zg6j@9Q zj$$%~yO-OS6PUVrM2s)(T4#6=JpI_@Uz+!6=GdyVU?`!F=d;8#ZB@(5g7$A0(`eqY z8_i@3w$0*es5mrSjhW*qzrl!_LQWs4?VfLmo1Sd@Ztt53+etwzAT^8ow_*7Jp`Y|l z*UgSEwvxq+FYO!O*aLf-PinZYne7Ib6ny3u>MjQz=((r3NTEeU4=-i0LBq3H-VJH< z^>1RE3_JwrclUn9vb7HcGUaFRA0QHcnE;6)hnkp%lY1UII#WPAv?-;c?YH}LWB8Nl z{sx-@Z;QxWh9fX8SxLZk8;kMFlGD3Jc^QZVL4nO)1I$zQwvwM&_!kW+LMf&lApv#< zur|EyC|U@5OQuph$TC_ZU`{!vJp`13e9alaR0Dbn5ikLFH7>eIz4QbV|C=%7)F=qo z_>M&5N)d)7G(A%c>}UCrW!Ql_6_A{?R7&CL`;!KOb3 z8Z=$YkV-IF;c7zs{3-WDEFJzuakFbd*4LWd<_kBE8~BFcv}js_2OowRNzWCtCQ6&k z{&~Me92$m*@e0ANcWKuz)?YjB*VoSTx??-3Cc0l2U!X^;Bv@m87eKHukAljrD54R+ zE;@_w4NPe1>3`i5Qy*3^E9x#VB6?}v=~qIprrrd5|DFkg;v5ixo0IsBmik8=Y;zv2 z%Bcf%NE$a44bk^`i4VwDLTbX=q@j9;JWT9JncQ!+Y%2&HHk@1~*L8-{ZpY?(-a9J-1~<1ltr9i~D9`P{XTIFWA6IG8c4;6bFw*lzU-{+?b&%OcIoCiw00n>A1ra zFPE$y@>ebbZlf(sN_iWBzQKDV zmmaLX#zK!@ZdvCANfwV}9@2O&w)!5gSgQzHdk2Q`jG6KD7S+1R5&F)j6QTD^=hq&7 zHUW+r^da^%V(h(wonR(j?BOiC!;y=%nJvz?*aW&5E87qq;2z`EI(f zBJNNSMFF9U{sR-af5{IY&AtoGcoG)Iq-S^v{7+t0>7N(KRoPj;+2N5;9o_nxIGjJ@ z7bYQK)bX)vEhy~VL%N6g^NE@D5VtV+Q8U2%{ji_=6+i^G%xeskEhH>Sqr194PJ$fB zu1y^){?9Vkg(FY2h)3ZHrw0Z<@;(gd_dtF#6y_;Iwi{yX$?asr?0N0_B*CifEi7<6 zq`?OdQjCYbhVcg+7MSgIM|pJRu~`g?g3x?Tl+V}#$It`iD1j+!x+!;wS0+2e>#g?Z z*EA^k7W{jO1r^K~cD#5pamp+o@8&yw6;%b|uiT?{Wa=4+9<}aXWUuL#ZwN1a;lQod zW{pxWCYGXdEq9qAmvAB904}?97=re$>!I%wxPV#|f#@A*Y=qa%zHlDv^yWbR03%V0 zprLP+b(#fBqxI%FiF*-n8HtH6$8f(P6!H3V^ysgd8de-N(@|K!A< z^qP}jp(RaM9kQ(^K(U8O84?D)aU(g?1S8iWwe)gqpHCaFlJxb*ilr{KTnu4_@5{K- z)n=CCeCrPHO0WHz)dDtkbZfUfVBd?53}K>C5*-wC4hpDN8cGk3lu-ypq+EYpb_2H; z%vP4@&+c2p;thaTs$dc^1CDGlPG@A;yGR5@$UEqk6p58qpw#7lc<+W(WR;(vr(D>W z#(K$vE#uBkT=*q&uaZwzz=P5mjiee6>!lV?c}QIX%ZdkO1dHg>Fa#xcGT6~}1*2m9 zkc7l3ItD6Ie~o_aFjI$Ri=C!8uF4!Ky7iG9QTrxVbsQroi|r)SAon#*B*{}TB-?=@ z8~jJs;_R2iDd!$+n$%X6FO&PYS{YhDAS+U2o4su9x~1+U3z7YN5o0qUK&|g^klZ6X zj_vrM5SUTnz5`*}Hyts9ADwLu#x_L=nv$Z0`HqN`Zo=V>OQI)fh01n~*a%01%cx%0 z4LTFVjmW+ipVQv5rYcn3;d2o4qunWUY!p+?s~X~(ost@WR@r@EuDOSs8*MT4fiP>! zkfo^!PWJJ1MHgKS2D_hc?Bs?isSDO61>ebl$U*9*QY(b=i&rp3@3GV@z>KzcZOxip z^dzA~44;R~cnhWz7s$$v?_8y-k!DZys}Q?4IkSyR!)C0j$(Gm|t#e3|QAOFaV2}36 z?dPNY;@I=FaCwylc_;~kXlZsk$_eLkNb~TIl8QQ`mmH&$*zwwR8zHU*sId)rxHu*K z;yZWa8UmCwju%aSNLwD5fBl^b0Ux1%q8YR*uG`53Mi<`5uA^Dc6Ync)J3N7;zQ*75)hf%a@{$H+%S?SGT)ks60)?6j$ zspl|4Ad6@%-r1t*$tT(en!gIXTUDcsj?28ZEzz)dH)SV3bZ+pjMaW0oc~rOPZP@g! zb9E+ndeVO_Ib9c_>{)`01^`ZS198 z)(t=+{Azi11$eu%aU7jbwuQrO`vLOixuh~%4z@mKr_Oc;F%Uq01fA)^W&y+g16e?rkLhTxV!EqC%2}sx_1u7IBq|}Be&7WI z4I<;1-9tJsI&pQIhj>FPkQV9{(m!wYYV@i5h?A0#BN2wqlEwNDIq06|^2oYVa7<~h zI_OLan0Do*4R5P=a3H9`s5*>xU}_PSztg`+2mv)|3nIy=5#Z$%+@tZnr> zLcTI!Mxa`PY7%{;KW~!=;*t)R_sl<^b>eNO@w#fEt(tPMg_jpJpW$q_DoUlkY|uo> z0-1{ouA#;t%spf*7VjkK&$QrvwUERKt^Sdo)5@?qAP)>}Y!h4(JQ!7{wIdkA+|)bv z&8hBwoX4v|+fie}iTslaBX^i*TjwO}f{V)8*!dMmRPi%XAWc8<_IqK1jUsApk)+~R zNFTCD-h>M5Y{qTQ&0#j@I@tmXGj%rzhTW5%Bkh&sSc=$Fv;M@1y!zvYG5P2(2|(&W zlcbR1{--rJ&s!rB{G-sX5^PaM@3EqWVz_y9cwLR9xMig&9gq(voeI)W&{d6j1jh&< zARXi&APWE1FQWh7eoZjuP z;vdgX>zep^{{2%hem;e*gDJhK1Hj12nBLIJoL<=0+8SVEBx7!4Ea+hBY;A1gBwvY<)tj~T=H`^?3>zeWWm|LAwo*S4Z%bDVUe z6r)CH1H!(>OH#MXFJ2V(U(qxD{4Px2`8qfFLG+=a;B^~Te_Z!r3RO%Oc#ZAHKQxV5 zRYXxZ9T2A%NVJIu5Pu7!Mj>t%YDO$T@M=RR(~mi%sv(YXVl`yMLD;+WZ{vG9(@P#e zMo}ZiK^7^h6TV%cG+;jhJ0s>h&VERs=tuZz^Tlu~%d{ZHtq6hX$V9h)Bw|jVCMudd zwZ5l7In8NT)qEPGF$VSKg&fb0%R2RnUnqa){)V(X(s0U zkCdVZe6wy{+_WhZh3qLp245Y2RR$@g-!9PjJ&4~0cFSHMUn=>dapv)hy}|y91ZWTV zCh=z*!S3_?`$&-eZ6xIXUq8RGl9oK0BJw*TdU6A`LJqX9eS3X@F)g$jLkBWFscPhR zpCv8#KeAc^y>>Y$k^=r|K(DTC}T$0#jQBOwB#@`P6~*IuW_8JxCG}J4va{ zsZzt}tt+cv7=l&CEuVtjD6G2~_Meh%p4RGuY?hSt?(sreO_F}8r7Kp$qQdvCdZnDQ zxzc*qchE*E2=WK)^oRNa>Ttj`fpvF-JZ5tu5>X1xw)J@1!IqWjq)ESBG?J|ez`-Tc zi5a}GZx|w-h%5lNDE_3ho0hEXMoaofo#Z;$8|2;EDF&*L+e$u}K=u?pb;dv$SXeQM zD-~7P0i_`Wk$#YP$=hw3UVU+=^@Kuy$>6?~gIXx636jh{PHly_a2xNYe1l60`|y!7 z(u%;ILuW0DDJ)2%y`Zc~hOALnj1~txJtcdD#o4BCT68+8gZe`=^te6H_egxY#nZH&P*)hgYaoJ^qtmpeea`35Fw)cy!w@c#v6E29co8&D9CTCl%^GV|X;SpneSXzV~LXyRn-@K0Df z{tK-nDWA!q38M1~`xUIt_(MO^R(yNY#9@es9RQbY@Ia*xHhD&=k^T+ zJi@j2I|WcgW=PuAc>hs`(&CvgjL2a9Rx zCbZyUpi8NWUOi@S%t+Su4|r&UoU|ze9SVe7p@f1GBkrjkkq)T}X%Qo1g!SQ{O{P?m z-OfGyyWta+UCXH+-+(D^%kw#A1-U;?9129at7MeCCzC{DNgO zeSqsV>W^NIfTO~4({c}KUiuoH8A*J!Cb0*sp*w-Bg@YfBIPZFH!M}C=S=S7PLLcIG zs7K77g~W)~^|+mx9onzMm0qh(f~OsDTzVmRtz=aZTllgR zGUn~_5hw_k&rll<4G=G+`^Xlnw;jNYDJz@bE?|r866F2hA9v0-8=JO3g}IHB#b`hy zA42a0>{0L7CcabSD+F7?pGbS1KMvT{@1_@k!_+Ki|5~EMGt7T%u=79F)8xEiL5!EJ zzuxQ`NBliCoJMJdwu|);zRCD<5Sf?Y>U$trQ-;xj6!s5&w=9E7)%pZ+1Nh&8nCCwM zv5>Ket%I?cxr3vVva`YeR?dGxbG@pi{H#8@kFEf0Jq6~K4>kt26*bxv=P&jyE#e$| zDJB_~imk^-z|o!2njF2hL*|7sHCnzluhJjwLQGDmC)Y9 zr9ZN`s)uCd^XDvn)VirMgW~qfn1~SaN^7vcX#K1G`==UGaDVVx$0BQnubhX|{e z^i0}>k-;BP#Szk{cFjO{2x~LjK{^Upqd&<+03_iMLp0$!6_$@TbX>8U-f*-w-ew1?`CtD_0y_Lo|PfKi52p?`5$Jzx0E8`M0 zNIb?#!K$mM4X%`Ry_yhG5k@*+n4||2!~*+&pYLh~{`~o(W|o64^NrjP?-1Lgu?iK^ zTX6u3?#$?R?N!{599vg>G8RGHw)Hx&=|g4599y}mXNpM{EPKKXB&+m?==R3GsIq?G zL5fH={=zawB(sMlDBJ+{dgb)Vx3pu>L=mDV0{r1Qs{0Pn%TpopH{m(By4;{FBvi{I z$}x!Iw~MJOL~&)p93SDIfP3x%ROjg}X{Sme#hiJ&Yk&a;iR}V|n%PriZBY8SX2*;6 z4hdb^&h;Xz%)BDACY5AUsV!($lib4>11UmcgXKWpzRL8r2Srl*9Y(1uBQsY&hO&uv znDNff0tpHlLISam?o(lOp#CmFdH<6HmA0{UwfU#Y{8M+7od8b8|B|7ZYR9f<#+V|ZSaCQvI$~es~g(Pv{2&m_rKSB2QQ zMvT}$?Ll>V+!9Xh5^iy3?UG;dF-zh~RL#++roOCsW^cZ&({6q|?Jt6`?S8=16Y{oH zp50I7r1AC1(#{b`Aq5cw>ypNggHKM9vBx!W$eYIzD!4KbLsZGr2o8>g<@inmS3*>J zx8oG((8f!ei|M@JZB`p7+n<Q}?>h249<`7xJ?u}_n;Gq(&km#1ULN87CeTO~FY zS_Ty}0TgQhV zOh3T7{{x&LSYGQfKR1PDIkP!WnfC1$l+fs@Di+d4O=eVKeF~2fq#1<8hEvpwuqcaH z4A8u~r^gnY3u6}zj*RHjk{AHhrrDqaj?|6GaVJbV%o-nATw}ASFr!f`Oz|u_QPkR# z0mDudY1dZRlk@TyQ?%Eti=$_WNFtLpSx9=S^be{wXINp%MU?a`F66LNU<c;0&ngifmP9i;bj6&hdGMW^Kf8e6ZDXbQD&$QAAMo;OQ)G zW(qlHh;}!ZP)JKEjm$VZjTs@hk&4{?@+NADuYrr!R^cJzU{kGc1yB?;7mIyAWwhbeA_l_lw-iDVi7wcFurf5 z#Uw)A@a9fOf{D}AWE%<`s1L_AwpZ?F!Vac$LYkp<#A!!`XKaDC{A%)~K#5z6>Hv@V zBEqF(D5?@6r3Pwj$^krpPDCjB+UOszqUS;b2n>&iAFcw<*im2(b3|5u6SK!n9Sg4I z0KLcwA6{Mq?p%t>aW0W!PQ>iUeYvNjdKYqII!CE7SsS&Rj)eIw-K4jtI?II+0IdGq z2WT|L3RL?;GtGgt1LWfI4Ka`9dbZXc$TMJ~8#Juv@K^1RJN@yzdLS8$AJ(>g!U9`# zx}qr7JWlU+&m)VG*Se;rGisutS%!6yybi%B`bv|9rjS(xOUIvbNz5qtvC$_JYY+c& za*3*2$RUH8p%pSq>48xR)4qsp!Q7BEiJ*`^>^6INRbC@>+2q9?x(h0bpc>GaNFi$K zPH$6!#(~{8@0QZk=)QnM#I=bDx5vTvjm$f4K}%*s+((H2>tUTf==$wqyoI`oxI7>C z&>5fe)Yg)SmT)eA(|j@JYR1M%KixxC-Eceknf-;N=jJTwKvk#@|J^&5H0c+%KxHUI z6dQbwwVx3p?X<_VRVb2fStH?HH zFR@Mp=qX%#L3XL)+$PXKV|o|#DpHAoqvj6uQKe@M-mnhCSou7Dj4YuO6^*V`m)1lf z;)@e%1!Qg$10w8uEmz{ENb$^%u}B;J7sDd zump}onoD#!l=agcBR)iG!3AF0-63%@`K9G(CzKrm$VJ{v7^O9Ps7Zej|3m= zVXlR&yW6=Y%mD30G@|tf=yC7-#L!16Q=dq&@beWgaIL40k0n% z)QHrp2Jck#evLMM1RGt3WvQ936ZC9vEje0nFMfvmOHVI+&okB_K|l-;|4vW;qk>n~ z+|kk8#`K?x`q>`(f6A${wfw9Cx(^)~tX7<#TpxR#zYG2P+FY~mG{tnEkv~d6oUQA+ z&hNTL=~Y@rF`v-RZlts$nb$3(OL1&@Y11hhL9+zUb6)SP!;CD)^GUtUpCHBE`j1te zAGud@miCVFLk$fjsrcpjsadP__yj9iEZUW{Ll7PPi<$R;m1o!&Xdl~R_v0;oDX2z^!&8}zNGA}iYG|k zmehMd1%?R)u6R#<)B)1oe9TgYH5-CqUT8N7K-A-dm3hbm_W21p%8)H{O)xUlBVb+iUR}-v5dFaCyfSd zC6Bd7=N4A@+Bna=!-l|*_(nWGDpoyU>nH=}IOrLfS+-d40&(Wo*dDB9nQiA2Tse$R z;uq{`X7LLzP)%Y9aHa4YQ%H?htkWd3Owv&UYbr5NUDAH^<l@Z0Cx%`N+B*i!!1u>D8%;Qt1$ zE5O0{-`9gdDxZ!`0m}ywH!;c{oBfL-(BH<&SQ~smbcobU!j49O^f4&IIYh~f+hK*M zZwTp%{ZSAhMFj1qFaOA+3)p^gnXH^=)`NTYgTu!CLpEV2NF=~-`(}7p^Eof=@VUbd z_9U|8qF7Rueg&$qpSSkN%%%DpbV?8E8ivu@ensI0toJ7Eas^jyFReQ1JeY9plb^{m z&eQO)qPLZQ6O;FTr*aJq=$cMN)QlQO@G&%z?BKUs1&I^`lq>=QLODwa`(mFGC`0H< zOlc*|N?B5&!U6BuJvkL?s1&nsi$*5cCv7^j_*l&$-sBmRS85UIrE--7eD8Gr3^+o? zqG-Yl4S&E;>H>k^a0GdUI(|n1`ws@)1%sq2XBdK`mqrNq_b4N{#VpouCXLzNvjoFv zo9wMQ6l0+FT+?%N(ka*;%m~(?338bu32v26!{r)|w8J`EL|t$}TA4q_FJRX5 zCPa{hc_I(7TGE#@rO-(!$1H3N-C0{R$J=yPCXCtGk{4>=*B56JdXU9cQVwB`6~cQZ zf^qK21x_d>X%dT!!)CJQ3mlHA@ z{Prkgfs6=Tz%63$6Zr8CO0Ak3A)Cv#@BVKr&aiKG7RYxY$Yx>Bj#3gJk*~Ps-jc1l z;4nltQwwT4@Z)}Pb!3xM?+EW0qEKA)sqzw~!C6wd^{03-9aGf3Jmt=}w-*!yXupLf z;)>-7uvWN4Unn8b4kfIza-X=x*e4n5pU`HtgpFFd))s$C@#d>aUl3helLom+RYb&g zI7A9GXLRZPl}iQS*d$Azxg-VgcUr*lpLnbPKUV{QI|bsG{8bLG<%CF( zMoS4pRDtLVYOWG^@ox^h8xL~afW_9DcE#^1eEC1SVSb1BfDi^@g?#f6e%v~Aw>@w- zIY0k+2lGWNV|aA*e#`U3=+oBDmGeInfcL)>*!w|*;mWiKNG6wP6AW4-4imN!W)!hE zA02~S1*@Q`fD*+qX@f3!2yJX&6FsEfPditB%TWo3=HA;T3o2IrjS@9SSxv%{{7&4_ zdS#r4OU41~GYMiib#z#O;zohNbhJknrPPZS6sN$%HB=jUnlCO_w5Gw5EeE@KV>soy z2EZ?Y|4RQDDjt5y!WBlZ(8M)|HP<0YyG|D%RqD+K#e7-##o3IZxS^wQ5{Kbzb6h(i z#(wZ|^ei>8`%ta*!2tJzwMv+IFHLF`zTU8E^Mu!R*45_=ccqI};Zbyxw@U%a#2}%f zF>q?SrUa_a4H9l+uW8JHh2Oob>NyUwG=QH~-^ZebU*R@67DcXdz2{HVB4#@edz?B< z5!rQH3O0>A&ylROO%G^fimV*LX7>!%re{_Sm6N>S{+GW1LCnGImHRoF@csnFzn@P0 zM=jld0z%oz;j=>c7mMwzq$B^2mae7NiG}%>(wtmsDXkWk{?BeMpTrIt3Mizq?vRsf zi_WjNp+61uV(%gEU-Vf0;>~vcDhe(dzWdaf#4mH3o^v{0EWhj?E?$5v02sV@xL0l4 zX0_IMFtQ44PfWBbPYN#}qxa%=J%dlR{O!KyZvk^g5s?sTNycWYPJ^FK(nl3k?z-5t z39#hKrdO7V(@!TU)LAPY&ngnZ1MzLEeEiZznn7e-jLCy8LO zu^7_#z*%I-BjS#Pg-;zKWWqX-+Ly$T!4`vTe5ZOV0j?TJVA*2?*=82^GVlZIuH%9s zXiV&(T(QGHHah=s&7e|6y?g+XxZGmK55`wGV>@1U)Th&=JTgJq>4mI&Av2C z)w+kRoj_dA!;SfTfkgMPO>7Dw6&1*Hi1q?54Yng`JO&q->^CX21^PrU^JU#CJ_qhV zSG>afB%>2fx<~g8p=P8Yzxqc}s@>>{g7}F!;lCXvF#RV)^fyYb_)iKVCz1xEq=fJ| z0a7DMCK*FuP=NM*5h;*D`R4y$6cpW-E&-i{v`x=Jbk_xSn@2T3q!3HoAOB`@5Vg6) z{PW|@9o!e;v1jZ2{=Uw6S6o{g82x6g=k!)cFSC*oemHaVjg?VpEmtUuD2_J^A~$4* z3O7HsbA6wxw{TP5Kk)(Vm?gKo+_}11vbo{Tp_5x79P~#F)ahQXT)tSH5;;14?s)On zel1J>1x>+7;g1Iz2FRpnYz;sD0wG9Q!vuzE9yKi3@4a9Nh1!GGN?hA)!mZEnnHh&i zf?#ZEN2sFbf~kV;>K3UNj1&vFhc^sxgj8FCL4v>EOYL?2uuT`0eDH}R zmtUJMxVrV5H{L53hu3#qaWLUa#5zY?f5ozIn|PkMWNP%n zWB5!B0LZB0kLw$k39=!akkE9Q>F4j+q434jB4VmslQ;$ zKiO#FZ`p|dKS716jpcvR{QJkSNfDVhr2%~eHrW;fU45>>snr*S8Vik-5eN5k*c2Mp zyxvX&_cFbB6lODXznHHT|rsURe2!swomtrqc~w5 zymTM8!w`1{04CBprR!_F{5LB+2_SOuZN{b*!J~1ZiPpP-M;);!ce!rOPDLtgR@Ie1 zPreuqm4!H)hYePcW1WZ0Fyaqe%l}F~Orr)~+;mkS&pOhP5Ebb`cnUt!X_QhP4_4p( z8YKQCDKGIy>?WIFm3-}Br2-N`T&FOi?t)$hjphB9wOhBXU#Hb+zm&We_-O)s(wc`2 z8?VsvU;J>Ju7n}uUb3s1yPx_F*|FlAi=Ge=-kN?1;`~6szP%$3B0|8Sqp%ebM)F8v zADFrbeT0cgE>M0DMV@_Ze*GHM>q}wWMzt|GYC%}r{OXRG3Ij&<+nx9;4jE${Fj_r* z`{z1AW_6Myd)i6e0E-h&m{{CvzH=Xg!&(bLYgRMO_YVd8JU7W+7MuGWNE=4@OvP9+ zxi^vqS@5%+#gf*Z@RVyU9N1sO-(rY$24LGsg1>w>s6ST^@)|D9>cT50maXLUD{Fzf zt~tp{OSTEKg3ZSQyQQ5r51){%=?xlZ54*t1;Ow)zLe3i?8tD8YyY^k%M)e`V*r+vL zPqUf&m)U+zxps+NprxMHF{QSxv}>lE{JZETNk1&F+R~bp{_T$dbXL2UGnB|hgh*p4h$clt#6;NO~>zuyY@C-MD@)JCc5XrYOt`wW7! z_ti2hhZBMJNbn0O-uTxl_b6Hm313^fG@e;RrhIUK9@# z+DHGv_Ow$%S8D%RB}`doJjJy*aOa5mGHVHz0e0>>O_%+^56?IkA5eN+L1BVCp4~m=1eeL zb;#G!#^5G%6Mw}r1KnaKsLvJB%HZL)!3OxT{k$Yo-XrJ?|7{s4!H+S2o?N|^Z z)+?IE9H7h~Vxn5hTis^3wHYuOU84+bWd)cUKuHapq=&}WV#OxHpLab`NpwHm8LmOo zjri+!k;7j_?FP##CpM+pOVx*0wExEex z@`#)K<-ZrGyArK;a%Km`^+We|eT+#MygHOT6lXBmz`8|lyZOwL1+b+?Z$0OhMEp3R z&J=iRERpv~TC=p2-BYLC*?4 zxvPs9V@g=JT0>zky5Poj=fW_M!c)Xxz1<=&_ZcL=LMZJqlnO1P^xwGGW*Z+yTBvbV z-IFe6;(k1@$1;tS>{%pXZ_7w+i?N4A2=TXnGf=YhePg8bH8M|Lk-->+w8Y+FjZ;L=wSGwxfA`gqSn)f(XNuSm>6Y z@|#e-)I(PQ^G@N`%|_DZSb4_pkaEF0!-nqY+t#pyA>{9^*I-zw4SYA1_z2Bs$XGUZbGA;VeMo%CezHK0lO={L%G)dI-+8w?r9iexdoB{?l zbJ}C?huIhWXBVs7oo{!$lOTlvCLZ_KN1N+XJGuG$rh<^eUQIqcI7^pmqhBSaOKNRq zrx~w^?9C?*&rNwP_SPYmo;J-#!G|{`$JZK7DxsM3N^8iR4vvn>E4MU&Oe1DKJvLc~ zCT>KLZ1;t@My zRj_2hI^61T&LIz)S!+AQIV23n1>ng+LUvzv;xu!4;wpqb#EZz;F)BLUzT;8UA1x*6vJ zicB!3Mj03s*kGV{g`fpC?V^s(=JG-k1EMHbkdP4P*1^8p_TqO|;!Zr%GuP$8KLxuf z=pv*H;kzd;P|2`JmBt~h6|GxdU~@weK5O=X&5~w$HpfO}@l-T7@vTCxVOwCkoPQv8 z@aV_)I5HQtfs7^X=C03zYmH4m0S!V@JINm6#(JmZRHBD?T!m^DdiZJrhKpBcur2u1 zf9e4%k$$vcFopK5!CC`;ww(CKL~}mlxK_Pv!cOsFgVkNIghA2Au@)t6;Y3*2gK=5d z?|@1a)-(sQ%uFOmJ7v2iG&l&m^u&^6DJM#XzCrF%r>{2XKyxLD2rgWBD;i(!e4InDQBDg==^z;AzT2z~OmV0!?Z z0S9pX$+E;w3WN;v&NYT=+G8hf=6w0E1$0AOr61}eOvE8W1jX%>&Mjo7&!ulawgzLH zbcb+IF(s^3aj12WSi#pzIpijJJzkP?JzRawnxmNDSUR#7!29vHULCE<3Aa#be}ie~d|!V+ z%l~s9Odo$G&fH!t!+`rUT0T9DulF!Yq&BfQWFZV1L9D($r4H(}Gnf6k3^wa7g5|Ws zj7%d`!3(0bb55yhC6@Q{?H|2os{_F%o=;-h{@Yyyn*V7?{s%Grvpe!H^kl6tF4Zf5 z{Jv1~yZ*iIWL_9C*8pBMQArfJJ0d9Df6Kl#wa}7Xa#Ef_5B7=X}DzbQXVPfCwTO@9+@;A^Ti6il_C>g?A-GFwA0#U;t4;wOm-4oS})h z5&on>NAu67O?YCQr%7XIzY%LS4bha9*e*4bU4{lGCUmO2UQ2U)QOqClLo61Kx~3dI zmV3*(P6F_Tr-oP%x!0kTnnT?Ep5j;_IQ^pTRp=e8dmJtI4YgWd0}+b2=ATkOhgpXe z;jmw+FBLE}UIs4!&HflFr4)vMFOJ19W4f2^W(=2)F%TAL)+=F>IE$=e=@j-*bFLSg z)wf|uFQu+!=N-UzSef62u0-C8Zc7 zo6@F)c+nZA{H|+~7i$DCU0pL{0Ye|fKLuV^w!0Y^tT$isu%i1Iw&N|tX3kwFKJN(M zXS`k9js66o$r)x?TWL}Kxl`wUDUpwFx(w4Yk%49;$sgVvT~n8AgfG~HUcDt1TRo^s zdla@6heJB@JV z!vK;BUMznhzGK6PVtj0)GB=zTv6)Q9Yt@l#fv7>wKovLobMV-+(8)NJmyF8R zcB|_K7=FJGGn^X@JdFaat0uhKjp3>k#^&xE_}6NYNG?kgTp>2Iu?ElUjt4~E-?`Du z?mDCS9wbuS%fU?5BU@Ijx>1HG*N?gIP+<~xE4u=>H`8o((cS5M6@_OK%jSjFHirQK zN9@~NXFx*jS{<|bgSpC|SAnA@I)+GB=2W|JJChLI_mx+-J(mSJ!b)uUom6nH0#2^(L@JBlV#t zLl?j54s`Y3vE^c_3^Hl0TGu*tw_n?@HyO@ZrENxA+^!)OvUX28gDSF*xFtQzM$A+O zCG=n#6~r|3zt=8%GuG} z<#VCZ%2?3Q(Ad#Y7GMJ~{U3>E{5e@z6+rgZLX{Cxk^p-7dip^d29;2N1_mm4QkASo z-L`GWWPCq$uCo;X_BmGIpJFBlhl<8~EG{vOD1o|X$aB9KPhWO_cKiU*$HWEgtf=fn zsO%9bp~D2c@?*K9jVN@_vhR03>M_8h!_~%aN!Cnr?s-!;U3SVfmhRwk11A^8Ns`@KeE}+ zN$H}a1U6E;*j5&~Og!xHdfK5M<~xka)x-0N)K_&e7AjMz`toDzasH+^1bZlC!n()crk9kg@$(Y{wdKvbuUd04N^8}t1iOgsKF zGa%%XWx@WoVaNC1!|&{5ZbkopFre-Lu(LCE5HWZBoE#W@er9W<>R=^oYxBvypN#x3 zq#LC8&q)GFP=5^-bpHj?LW=)-g+3_)Ylps!3^YQ{9~O9&K)xgy zMkCWaApU-MI~e^cV{Je75Qr7eF%&_H)BvfyKL=gIA>;OSq(y z052BFz3E(Prg~09>|_Z@!qj}@;8yxnw+#Ej0?Rk<y}4ghbD569B{9hSFr*^ygZ zr6j7P#gtZh6tMk6?4V$*Jgz+#&ug;yOr>=qdI#9U&^am2qoh4Jy}H2%a|#Fs{E(5r z%!ijh;VuGA6)W)cJZx+;9Bp1LMUzN~x_8lQ#D3+sL{be-Jyeo@@dv7XguJ&S5vrH` z>QxOMWn7N-T!D@1(@4>ZlL^y5>m#0!HKovs12GRav4z!>p(1~xok8+_{| z#Ae4{9#NLh#Vj2&JuIn5$d6t@__`o}umFo(n0QxUtd2GKCyE+erwXY?`cm*h&^9*8 zJ+8x6fRZI-e$CRygofIQN^dWysCxgkyr{(_oBwwSRxZora1(%(aC!5BTtj^+YuevI zx?)H#(xlALUp6QJ!=l9N__$cxBZ5p&7;qD3PsXRFVd<({Kh+mShFWJNpy`N@ab7?9 zv5=klvCJ4bx|-pvOO2-+G)6O?$&)ncA#Urze2rlBfp#htudhx-NeRnJ@u%^_bfw4o z4|{b8SkPV3b>Wera1W(+N@p9H>dc6{cnkh-sgr?e%(YkWvK+0YXVwk0=d`)}*47*B z5JGkEdVix!w7-<%r0JF~`ZMMPe;f0EQHuYHxya`puazyph*ZSb1mJAt^k4549BfS; zK7~T&lRb=W{s&t`DJ$B}s-eH1&&-wEOH1KWsKn0a(ZI+G!v&W4A*cl>qAvUv6pbUR z#(f#EKV8~hk&8oayBz4vaswc(?qw1vn`yC zZQDl2PCB-&Uu@g9ZQHhO+v(W0bNig{-k0;;`+wM@#@J)8r?qOYs#&vUna8ILxN7S{ zp1s41KnR8miQJtJtOr|+qk}wrLt+N*z#5o`TmD1)E&QD(Vh&pjZJ_J*0!8dy_ z>^=@v=J)C`x&gjqAYu`}t^S=DFCtc0MkBU2zf|69?xW`Ck~(6zLD)gSE{7n~6w8j_ zoH&~$ED2k5-yRa0!r8fMRy z;QjBYUaUnpd}mf%iVFPR%Dg9!d>g`01m~>2s))`W|5!kc+_&Y>wD@@C9%>-lE`WB0 zOIf%FVD^cj#2hCkFgi-fgzIfOi+ya)MZK@IZhHT5FVEaSbv-oDDs0W)pA0&^nM0TW zmgJmd7b1R7b0a`UwWJYZXp4AJPteYLH>@M|xZFKwm!t3D3&q~av?i)WvAKHE{RqpD{{%OhYkK?47}+}` zrR2(Iv9bhVa;cDzJ%6ntcSbx7v7J@Y4x&+eWSKZ*eR7_=CVIUSB$^lfYe@g+p|LD{ zPSpQmxx@b$%d!05|H}WzBT4_cq?@~dvy<7s&QWtieJ9)hd4)$SZz}#H2UTi$CkFWW|I)v_-NjuH!VypONC=1`A=rm_jfzQ8Fu~1r8i{q-+S_j$ z#u^t&Xnfi5tZtl@^!fUJhx@~Cg0*vXMK}D{>|$#T*+mj(J_@c{jXBF|rm4-8%Z2o! z2z0o(4%8KljCm^>6HDK!{jI7p+RAPcty_~GZ~R_+=+UzZ0qzOwD=;YeZt*?3%UGdr z`c|BPE;yUbnyARUl&XWSNJ<+uRt%!xPF&K;(l$^JcA_CMH6)FZt{>6ah$|(9$2fc~ z=CD00uHM{qv;{Zk9FR0~u|3|Eiqv9?z2#^GqylT5>6JNZwKqKBzzQpKU2_pmtD;CT zi%Ktau!Y2Tldfu&b0UgmF(SSBID)15*r08eoUe#bT_K-G4VecJL2Pa=6D1K6({zj6 za(2Z{r!FY5W^y{qZ}08+h9f>EKd&PN90f}Sc0ejf%kB4+f#T8Q1=Pj=~#pi$U zp#5rMR%W25>k?<$;$x72pkLibu1N|jX4cWjD3q^Pk3js!uK6h7!dlvw24crL|MZs_ zb%Y%?Fyp0bY0HkG^XyS76Ts*|Giw{31LR~+WU5NejqfPr73Rp!xQ1mLgq@mdWncLy z%8}|nzS4P&`^;zAR-&nm5f;D-%yNQPwq4N7&yULM8bkttkD)hVU>h>t47`{8?n2&4 zjEfL}UEagLUYwdx0sB2QXGeRmL?sZ%J!XM`$@ODc2!y|2#7hys=b$LrGbvvjx`Iqi z&RDDm3YBrlKhl`O@%%&rhLWZ*ABFz2nHu7k~3@e4)kO3%$=?GEFUcCF=6-1n!x^vmu+Ai*amgXH+Rknl6U>#9w;A} zn2xanZSDu`4%%x}+~FG{Wbi1jo@wqBc5(5Xl~d0KW(^Iu(U3>WB@-(&vn_PJt9{1`e9Iic@+{VPc`vP776L*viP{wYB2Iff8hB%E3|o zGMOu)tJX!`qJ}ZPzq7>=`*9TmETN7xwU;^AmFZ-ckZjV5B2T09pYliaqGFY|X#E-8 z20b>y?(r-Fn5*WZ-GsK}4WM>@TTqsxvSYWL6>18q8Q`~JO1{vLND2wg@58OaU!EvT z1|o+f1mVXz2EKAbL!Q=QWQKDZpV|jznuJ}@-)1&cdo z^&~b4Mx{*1gurlH;Vhk5g_cM&6LOHS2 zRkLfO#HabR1JD4Vc2t828dCUG#DL}f5QDSBg?o)IYYi@_xVwR2w_ntlpAW0NWk$F1 z$If?*lP&Ka1oWfl!)1c3fl`g*lMW3JOn#)R1+tfwrs`aiFUgz3;XIJ>{QFxLCkK30 zNS-)#DON3yb!7LBHQJ$)4y%TN82DC2-9tOIqzhZ27@WY^<6}vXCWcR5iN{LN8{0u9 zNXayqD=G|e?O^*ms*4P?G%o@J1tN9_76e}E#66mr89%W_&w4n66~R;X_vWD(oArwj z4CpY`)_mH2FvDuxgT+akffhX0b_slJJ*?Jn3O3~moqu2Fs1oL*>7m=oVek2bnprnW zixkaIFU%+3XhNA@@9hyhFwqsH2bM|`P?G>i<-gy>NflhrN{$9?LZ1ynSE_Mj0rADF zhOz4FnK}wpLmQuV zgO4_Oz9GBu_NN>cPLA=`SP^$gxAnj;WjJnBi%Q1zg`*^cG;Q)#3Gv@c^j6L{arv>- zAW%8WrSAVY1sj$=umcAf#ZgC8UGZGoamK}hR7j6}i8#np8ruUlvgQ$j+AQglFsQQq zOjyHf22pxh9+h#n$21&$h?2uq0>C9P?P=Juw0|;oE~c$H{#RGfa>| zj)Iv&uOnaf@foiBJ}_;zyPHcZt1U~nOcNB{)og8Btv+;f@PIT*xz$x!G?u0Di$lo7 zOugtQ$Wx|C($fyJTZE1JvR~i7LP{ zbdIwqYghQAJi9p}V&$=*2Azev$6K@pyblphgpv8^9bN!?V}{BkC!o#bl&AP!3DAjM zmWFsvn2fKWCfjcAQmE+=c3Y7j@#7|{;;0f~PIodmq*;W9Fiak|gil6$w3%b_Pr6K_ zJEG@&!J%DgBZJDCMn^7mk`JV0&l07Bt`1ymM|;a)MOWz*bh2#d{i?SDe9IcHs7 zjCrnyQ*Y5GzIt}>`bD91o#~5H?4_nckAgotN{2%!?wsSl|LVmJht$uhGa+HiH>;av z8c?mcMYM7;mvWr6noUR{)gE!=i7cZUY7e;HXa221KkRoc2UB>s$Y(k%NzTSEr>W(u z<(4mcc)4rB_&bPzX*1?*ra%VF}P1nwiP5cykJ&W{!OTlz&Td0pOkVp+wc z@k=-Hg=()hNg=Q!Ub%`BONH{ z_=ZFgetj@)NvppAK2>8r!KAgi>#%*7;O-o9MOOfQjV-n@BX6;Xw;I`%HBkk20v`qoVd0)}L6_49y1IhR z_OS}+eto}OPVRn*?UHC{eGyFU7JkPz!+gX4P>?h3QOwGS63fv4D1*no^6PveUeE5% zlehjv_3_^j^C({a2&RSoVlOn71D8WwMu9@Nb@=E_>1R*ve3`#TF(NA0?d9IR_tm=P zOP-x;gS*vtyE1Cm zG0L?2nRUFj#aLr-R1fX*$sXhad)~xdA*=hF3zPZhha<2O$Ps+F07w*3#MTe?)T8|A!P!v+a|ot{|^$q(TX`35O{WI0RbU zCj?hgOv=Z)xV?F`@HKI11IKtT^ocP78cqHU!YS@cHI@{fPD?YXL)?sD~9thOAv4JM|K8OlQhPXgnevF=F7GKD2#sZW*d za}ma31wLm81IZxX(W#A9mBvLZr|PoLnP>S4BhpK8{YV_}C|p<)4#yO{#ISbco92^3 zv&kCE(q9Wi;9%7>>PQ!zSkM%qqqLZW7O`VXvcj;WcJ`2~v?ZTYB@$Q&^CTfvy?1r^ z;Cdi+PTtmQwHX_7Kz?r#1>D zS5lWU(Mw_$B&`ZPmqxpIvK<~fbXq?x20k1~9az-Q!uR78mCgRj*eQ>zh3c$W}>^+w^dIr-u{@s30J=)1zF8?Wn|H`GS<=>Om|DjzC{}Jt?{!fSJe*@$H zg>wFnlT)k#T?LslW zu$^7Uy~$SQ21cE?3Ijl+bLfuH^U5P^$@~*UY#|_`uvAIe(+wD2eF}z_y!pvomuVO; zS^9fbdv)pcm-B@CW|Upm<7s|0+$@@<&*>$a{aW+oJ%f+VMO<#wa)7n|JL5egEgoBv zl$BY(NQjE0#*nv=!kMnp&{2Le#30b)Ql2e!VkPLK*+{jv77H7)xG7&=aPHL7LK9ER z5lfHxBI5O{-3S?GU4X6$yVk>lFn;ApnwZybdC-GAvaznGW-lScIls-P?Km2mF>%B2 zkcrXTk+__hj-3f48U%|jX9*|Ps41U_cd>2QW81Lz9}%`mTDIhE)jYI$q$ma7Y-`>% z8=u+Oftgcj%~TU}3nP8&h7k+}$D-CCgS~wtWvM|UU77r^pUw3YCV80Ou*+bH0!mf0 zxzUq4ed6y>oYFz7+l18PGGzhB^pqSt)si=9M>~0(Bx9*5r~W7sa#w+_1TSj3Jn9mW zMuG9BxN=}4645Cpa#SVKjFst;9UUY@O<|wpnZk$kE+to^4!?0@?Cwr3(>!NjYbu?x z1!U-?0_O?k!NdM^-rIQ8p)%?M+2xkhltt*|l=%z2WFJhme7*2xD~@zk#`dQR$6Lmd zb3LOD4fdt$Cq>?1<%&Y^wTWX=eHQ49Xl_lFUA(YQYHGHhd}@!VpYHHm=(1-O=yfK#kKe|2Xc*9}?BDFN zD7FJM-AjVi)T~OG)hpSWqH>vlb41V#^G2B_EvYlWhDB{Z;Q9-0)ja(O+By`31=biA zG&Fs#5!%_mHi|E4Nm$;vVQ!*>=_F;ZC=1DTPB#CICS5fL2T3XmzyHu?bI;m7D4@#; ztr~;dGYwb?m^VebuULtS4lkC_7>KCS)F@)0OdxZIFZp@FM_pHnJes8YOvwB|++#G( z&dm*OP^cz95Wi15vh`Q+yB>R{8zqEhz5of>Po$9LNE{xS<)lg2*roP*sQ}3r3t<}; zPbDl{lk{pox~2(XY5=qg0z!W-x^PJ`VVtz$git7?)!h>`91&&hESZy1KCJ2nS^yMH z!=Q$eTyRi68rKxdDsdt+%J_&lapa{ds^HV9Ngp^YDvtq&-Xp}60B_w@Ma>_1TTC;^ zpbe!#gH}#fFLkNo#|`jcn?5LeUYto%==XBk6Ik0kc4$6Z+L3x^4=M6OI1=z5u#M%0 z0E`kevJEpJjvvN>+g`?gtnbo$@p4VumliZV3Z%CfXXB&wPS^5C+7of2tyVkMwNWBiTE2 z8CdPu3i{*vR-I(NY5syRR}I1TJOV@DJy-Xmvxn^IInF>Tx2e)eE9jVSz69$6T`M9-&om!T+I znia!ZWJRB28o_srWlAxtz4VVft8)cYloIoVF=pL zugnk@vFLXQ_^7;%hn9x;Vq?lzg7%CQR^c#S)Oc-8d=q_!2ZVH764V z!wDKSgP}BrVV6SfCLZnYe-7f;igDs9t+K*rbMAKsp9L$Kh<6Z;e7;xxced zn=FGY<}CUz31a2G}$Q(`_r~75PzM4l_({Hg&b@d8&jC}B?2<+ed`f#qMEWi z`gm!STV9E4sLaQX+sp5Nu9*;9g12naf5?=P9p@H@f}dxYprH+3ju)uDFt^V{G0APn zS;16Dk{*fm6&BCg#2vo?7cbkkI4R`S9SSEJ=#KBk3rl69SxnCnS#{*$!^T9UUmO#&XXKjHKBqLdt^3yVvu8yn|{ zZ#%1CP)8t-PAz(+_g?xyq;C2<9<5Yy<~C74Iw(y>uUL$+$mp(DRcCWbCKiGCZw@?_ zdomfp+C5xt;j5L@VfhF*xvZdXwA5pcdsG>G<8II-|1dhAgzS&KArcb0BD4ZZ#WfiEY{hkCq5%z9@f|!EwTm;UEjKJsUo696V>h zy##eXYX}GUu%t{Gql8vVZKkNhQeQ4C%n|RmxL4ee5$cgwlU+?V7a?(jI#&3wid+Kz5+x^G!bb#$q>QpR#BZ}Xo5UW^ zD&I`;?(a}Oys7-`I^|AkN?{XLZNa{@27Dv^s4pGowuyhHuXc zuctKG2x0{WCvg_sGN^n9myJ}&FXyGmUQnW7fR$=bj$AHR88-q$D!*8MNB{YvTTEyS zn22f@WMdvg5~o_2wkjItJN@?mDZ9UUlat2zCh(zVE=dGi$rjXF7&}*sxac^%HFD`Y zTM5D3u5x**{bW!68DL1A!s&$2XG@ytB~dX-?BF9U@XZABO`a|LM1X3HWCllgl0+uL z04S*PX$%|^WAq%jkzp~%9HyYIF{Ym?k)j3nMwPZ=hlCg9!G+t>tf0o|J2%t1 ztC+`((dUplgm3`+0JN~}&FRRJ3?l*>Y&TfjS>!ShS`*MwO{WIbAZR#<%M|4c4^dY8 z{Rh;-!qhY=dz5JthbWoovLY~jNaw>%tS4gHVlt5epV8ekXm#==Po$)}mh^u*cE>q7*kvX&gq)(AHoItMYH6^s6f(deNw%}1=7O~bTHSj1rm2|Cq+3M z93djjdomWCTCYu!3Slx2bZVy#CWDozNedIHbqa|otsUl+ut?>a;}OqPfQA05Yim_2 zs@^BjPoFHOYNc6VbNaR5QZfSMh2S*`BGwcHMM(1@w{-4jVqE8Eu0Bi%d!E*^Rj?cR z7qgxkINXZR)K^=fh{pc0DCKtrydVbVILI>@Y0!Jm>x-xM!gu%dehm?cC6ok_msDVA*J#{75%4IZt}X|tIVPReZS#aCvuHkZxc zHVMtUhT(wp09+w9j9eRqz~LtuSNi2rQx_QgQ(}jBt7NqyT&ma61ldD(s9x%@q~PQl zp6N*?=N$BtvjQ_xIT{+vhb1>{pM0Arde0!X-y))A4znDrVx8yrP3B1(7bKPE5jR@5 zwpzwT4cu~_qUG#zYMZ_!2Tkl9zP>M%cy>9Y(@&VoB84#%>amTAH{(hL4cDYt!^{8L z645F>BWO6QaFJ-{C-i|-d%j7#&7)$X7pv#%9J6da#9FB5KyDhkA+~)G0^87!^}AP>XaCSScr;kL;Z%RSPD2CgoJ;gpYT5&6NUK$86$T?jRH=w8nI9Z534O?5fk{kd z`(-t$8W|#$3>xoMfXvV^-A(Q~$8SKDE^!T;J+rQXP71XZ(kCCbP%bAQ1|%$%Ov9_a zyC`QP3uPvFoBqr_+$HenHklqyIr>PU_Fk5$2C+0eYy^~7U&(!B&&P2%7#mBUhM!z> z_B$Ko?{Pf6?)gpYs~N*y%-3!1>o-4;@1Zz9VQHh)j5U1aL-Hyu@1d?X;jtDBNk*vMXPn@ z+u@wxHN*{uHR!*g*4Xo&w;5A+=Pf9w#PeZ^x@UD?iQ&${K2c}UQgLRik-rKM#Y5rdDphdcNTF~cCX&9ViRP}`>L)QA4zNXeG)KXFzSDa6 zd^St;inY6J_i=5mcGTx4_^Ys`M3l%Q==f>{8S1LEHn{y(kbxn5g1ezt4CELqy)~TV6{;VW>O9?5^ ztcoxHRa0jQY7>wwHWcxA-BCwzsP>63Kt&3fy*n#Cha687CQurXaRQnf5wc9o8v7Rw zNwGr2fac;Wr-Ldehn7tF^(-gPJwPt@VR1f;AmKgxN&YPL;j=0^xKM{!wuU|^mh3NE zy35quf}MeL!PU;|{OW_x$TBothLylT-J>_x6p}B_jW1L>k)ps6n%7Rh z96mPkJIM0QFNYUM2H}YF5bs%@Chs6#pEnloQhEl?J-)es!(SoJpEPoMTdgA14-#mC zghayD-DJWtUu`TD8?4mR)w5E`^EHbsz2EjH5aQLYRcF{l7_Q5?CEEvzDo(zjh|BKg z3aJl_n#j&eFHsUw4~lxqnr!6NL*se)6H=A+T1e3xUJGQrd}oSPwSy5+$tt{2t5J5@(lFxl43amsARG74iyNC}uuS zd2$=(r6RdamdGx^eatX@F2D8?U23tDpR+Os?0Gq2&^dF+$9wiWf?=mDWfjo4LfRwL zI#SRV9iSz>XCSgEj!cW&9H-njJopYiYuq|2w<5R2!nZ27DyvU4UDrHpoNQZiGPkp@ z1$h4H46Zn~eqdj$pWrv;*t!rTYTfZ1_bdkZmVVIRC21YeU$iS-*XMNK`#p8Z_DJx| zk3Jssf^XP7v0X?MWFO{rACltn$^~q(M9rMYoVxG$15N;nP)A98k^m3CJx8>6}NrUd@wp-E#$Q0uUDQT5GoiK_R{ z<{`g;8s>UFLpbga#DAf%qbfi`WN1J@6IA~R!YBT}qp%V-j!ybkR{uY0X|x)gmzE0J z&)=eHPjBxJvrZSOmt|)hC+kIMI;qgOnuL3mbNR0g^<%|>9x7>{}>a2qYSZAGPt4it?8 zNcLc!Gy0>$jaU?}ZWxK78hbhzE+etM`67*-*x4DN>1_&{@5t7_c*n(qz>&K{Y?10s zXsw2&nQev#SUSd|D8w7ZD2>E<%g^; zV{yE_O}gq?Q|zL|jdqB^zcx7vo(^})QW?QKacx$yR zhG|XH|8$vDZNIfuxr-sYFR{^csEI*IM#_gd;9*C+SysUFejP0{{z7@P?1+&_o6=7V|EJLQun^XEMS)w(=@eMi5&bbH*a0f;iC~2J74V2DZIlLUHD&>mlug5+v z6xBN~8-ovZylyH&gG#ptYsNlT?-tzOh%V#Y33zlsJ{AIju`CjIgf$@gr8}JugRq^c zAVQ3;&uGaVlVw}SUSWnTkH_6DISN&k2QLMBe9YU=sA+WiX@z)FoSYX`^k@B!j;ZeC zf&**P?HQG6Rk98hZ*ozn6iS-dG}V>jQhb3?4NJB*2F?6N7Nd;EOOo;xR7acylLaLy z9)^lykX39d@8@I~iEVar4jmjjLWhR0d=EB@%I;FZM$rykBNN~jf>#WbH4U{MqhhF6 zU??@fSO~4EbU4MaeQ_UXQcFyO*Rae|VAPLYMJEU`Q_Q_%s2*>$#S^)&7er+&`9L=1 z4q4ao07Z2Vsa%(nP!kJ590YmvrWg+YrgXYs_lv&B5EcoD`%uL79WyYA$0>>qi6ov7 z%`ia~J^_l{p39EY zv>>b}Qs8vxsu&WcXEt8B#FD%L%ZpcVtY!rqVTHe;$p9rbb5O{^rFMB>auLn-^;s+-&P1#h~mf~YLg$8M9 zZ4#87;e-Y6x6QO<{McUzhy(%*6| z)`D~A(TJ$>+0H+mct(jfgL4x%^oC^T#u(bL)`E2tBI#V1kSikAWmOOYrO~#-cc_8! zCe|@1&mN2{*ceeiBldHCdrURk4>V}79_*TVP3aCyV*5n@jiNbOm+~EQ_}1#->_tI@ zqXv+jj2#8xJtW508rzFrYcJxoek@iW6SR@1%a%Bux&;>25%`j3UI`0DaUr7l79`B1 zqqUARhW1^h6=)6?;@v>xrZNM;t}{yY3P@|L}ey@gG( z9r{}WoYN(9TW&dE2dEJIXkyHA4&pU6ki=rx&l2{DLGbVmg4%3Dlfvn!GB>EVaY_%3+Df{fBiqJV>~Xf8A0aqUjgpa} zoF8YXO&^_x*Ej}nw-$-F@(ddB>%RWoPUj?p8U{t0=n>gAI83y<9Ce@Q#3&(soJ{64 z37@Vij1}5fmzAuIUnXX`EYe;!H-yTVTmhAy;y8VZeB#vD{vw9~P#DiFiKQ|kWwGFZ z=jK;JX*A;Jr{#x?n8XUOLS;C%f|zj-7vXtlf_DtP7bpurBeX%Hjwr z4lI-2TdFpzkjgiv!8Vfv`=SP+s=^i3+N~1ELNWUbH|ytVu>EyPN_3(4TM^QE1swRo zoV7Y_g)a>28+hZG0e7g%@2^s>pzR4^fzR-El}ARTmtu!zjZLuX%>#OoU3}|rFjJg} zQ2TmaygxJ#sbHVyiA5KE+yH0LREWr%^C*yR|@gM$nK2P zo}M}PV0v))uJh&33N>#aU376@ZH79u(Yw`EQ2hM3SJs9f99+cO6_pNW$j$L-CtAfe zYfM)ccwD!P%LiBk!eCD?fHCGvgMQ%Q2oT_gmf?OY=A>&PaZQOq4eT=lwbaf}33LCH zFD|)lu{K7$8n9gX#w4~URjZxWm@wlH%oL#G|I~Fb-v^0L0TWu+`B+ZG!yII)w05DU z>GO?n(TN+B=>HdxVDSlIH76pta$_LhbBg;eZ`M7OGcqt||qi zogS72W1IN%=)5JCyOHWoFP7pOFK0L*OAh=i%&VW&4^LF@R;+K)t^S!96?}^+5QBIs zjJNTCh)?)4k^H^g1&jc>gysM`y^8Rm3qsvkr$9AeWwYpa$b22=yAd1t<*{ zaowSEFP+{y?Ob}8&cwfqoy4Pb9IA~VnM3u!trIK$&&0Op#Ql4j>(EW?UNUv#*iH1$ z^j>+W{afcd`{e&`-A{g}{JnIzYib)!T56IT@YEs{4|`sMpW3c8@UCoIJv`XsAw!XC z34|Il$LpW}CIHFC5e*)}00I5{%OL*WZRGzC0?_}-9{#ue?-ug^ zLE|uv-~6xnSs_2_&CN9{9vyc!Xgtn36_g^wI0C4s0s^;8+p?|mm;Odt3`2ZjwtK;l zfd6j)*Fr#53>C6Y8(N5?$H0ma;BCF3HCjUs7rpb2Kf*x3Xcj#O8mvs#&33i+McX zQpBxD8!O{5Y8D&0*QjD=Yhl9%M0)&_vk}bmN_Ud^BPN;H=U^bn&(csl-pkA+GyY0Z zKV7sU_4n;}uR78ouo8O%g*V;79KY?3d>k6%gpcmQsKk&@Vkw9yna_3asGt`0Hmj59 z%0yiF*`jXhByBI9QsD=+>big5{)BGe&+U2gAARGe3ID)xrid~QN_{I>k}@tzL!Md_ z&=7>TWciblF@EMC3t4-WX{?!m!G6$M$1S?NzF*2KHMP3Go4=#ZHkeIv{eEd;s-yD# z_jU^Ba06TZqvV|Yd;Z_sN%$X=!T+&?#p+OQIHS%!LO`Hx0q_Y0MyGYFNoM{W;&@0@ zLM^!X4KhdtsET5G<0+|q0oqVXMW~-7LW9Bg}=E$YtNh1#1D^6Mz(V9?2g~I1( zoz9Cz=8Hw98zVLwC2AQvp@pBeKyidn6Xu0-1SY1((^Hu*-!HxFUPs)yJ+i`^BC>PC zjwd0mygOVK#d2pRC9LxqGc6;Ui>f{YW9Bvb>33bp^NcnZoH~w9(lM5@JiIlfa-6|k ziy31UoMN%fvQfhi8^T+=yrP{QEyb-jK~>$A4SZT-N56NYEbpvO&yUme&pWKs3^94D zH{oXnUTb3T@H+RgzML*lejx`WAyw*?K7B-I(VJx($2!NXYm%3`=F~TbLv3H<{>D?A zJo-FDYdSA-(Y%;4KUP2SpHKAIcv9-ld(UEJE7=TKp|Gryn;72?0LHqAN^fk6%8PCW z{g_-t)G5uCIf0I`*F0ZNl)Z>))MaLMpXgqWgj-y;R+@A+AzDjsTqw2Mo9ULKA3c70 z!7SOkMtZb+MStH>9MnvNV0G;pwSW9HgP+`tg}e{ij0H6Zt5zJ7iw`hEnvye!XbA@!~#%vIkzowCOvq5I5@$3wtc*w2R$7!$*?}vg4;eDyJ_1=ixJuEp3pUS27W?qq(P^8$_lU!mRChT}ctvZz4p!X^ zOSp|JOAi~f?UkwH#9k{0smZ7-#=lK6X3OFEMl7%)WIcHb=#ZN$L=aD`#DZKOG4p4r zwlQ~XDZ`R-RbF&hZZhu3(67kggsM-F4Y_tI^PH8PMJRcs7NS9ogF+?bZB*fcpJ z=LTM4W=N9yepVvTj&Hu~0?*vR1HgtEvf8w%Q;U0^`2@e8{SwgX5d(cQ|1(!|i$km! zvY03MK}j`sff;*-%mN~ST>xU$6Bu?*Hm%l@0dk;j@%>}jsgDcQ)Hn*UfuThz9(ww_ zasV`rSrp_^bp-0sx>i35FzJwA!d6cZ5#5#nr@GcPEjNnFHIrtUYm1^Z$;{d&{hQV9 z6EfFHaIS}46p^5I-D_EcwwzUUuO}mqRh&T7r9sfw`)G^Q%oHxEs~+XoM?8e*{-&!7 z7$m$lg9t9KP9282eke608^Q2E%H-xm|oJ8=*SyEo} z@&;TQ3K)jgspgKHyGiKVMCz>xmC=H5Fy3!=TP)-R3|&1S-B)!6q50wfLHKM@7Bq6E z44CY%G;GY>tC`~yh!qv~YdXw! zSkquvYNs6k1r7>Eza?Vkkxo6XRS$W7EzL&A`o>=$HXgBp{L(i^$}t`NcnAxzbH8Ht z2!;`bhKIh`f1hIFcI5bHI=ueKdzmB9)!z$s-BT4ItyY|NaA_+o=jO%MU5as9 zc2)aLP>N%u>wlaXTK!p)r?+~)L+0eCGb5{8WIk7K52$nufnQ+m8YF+GQc&{^(zh-$ z#wyWV*Zh@d!b(WwXqvfhQX)^aoHTBkc;4ossV3&Ut*k>AI|m+{#kh4B!`3*<)EJVj zwrxK>99v^k4&Y&`Awm>|exo}NvewV%E+@vOc>5>%H#BK9uaE2$vje zWYM5fKuOTtn96B_2~~!xJPIcXF>E_;yO8AwpJ4)V`Hht#wbO3Ung~@c%%=FX4)q+9 z99#>VC2!4l`~0WHs9FI$Nz+abUq# zz`Of97})Su=^rGp2S$)7N3rQCj#0%2YO<R&p>$<#lgXcUj=4H_{oAYiT3 z44*xDn-$wEzRw7#@6aD)EGO$0{!C5Z^7#yl1o;k0PhN=aVUQu~eTQ^Xy{z8Ow6tk83 z4{5xe%(hx)%nD&|e*6sTWH`4W&U!Jae#U4TnICheJmsw{l|CH?UA{a6?2GNgpZLyzU2UlFu1ZVwlALmh_DOs03J^Cjh1im`E3?9&zvNmg(MuMw&0^Lu$(#CJ*q6DjlKsY-RMJ^8yIY|{SQZ*9~CH|u9L z`R78^r=EbbR*_>5?-)I+$6i}G)%mN(`!X72KaV(MNUP7Nv3MS9S|Pe!%N2AeOt5zG zVJ;jI4HZ$W->Ai_4X+`9c(~m=@ek*m`ZQbv3ryI-AD#AH=`x$~WeW~M{Js57(K7(v ze5`};LG|%C_tmd>bkufMWmAo&B+DT9ZV~h(4jg0>^aeAqL`PEUzJJtI8W1M!bQWpv zvN(d}E1@nlYa!L!!A*RN!(Q3F%J?5PvQ0udu?q-T)j3JKV~NL>KRb~w-lWc685uS6 z=S#aR&B8Sc8>cGJ!!--?kwsJTUUm`Jk?7`H z7PrO~xgBrSW2_tTlCq1LH8*!o?pj?qxy8}(=r_;G18POrFh#;buWR0qU24+XUaVZ0 z?(sXcr@-YqvkCmHr{U2oPogHL{r#3r49TeR<{SJX1pcUqyWPrkYz^X8#QW~?F)R5i z>p^!i<;qM8Nf{-fd6!_&V*e_9qP6q(s<--&1Ttj01j0w>bXY7y1W*%Auu&p|XSOH=)V7Bd4fUKh&T1)@cvqhuD-d=?w}O zjI%i(f|thk0Go*!d7D%0^ztBfE*V=(ZIN84f5HU}T9?ulmEYzT5usi=DeuI*d|;M~ zp_=Cx^!4k#=m_qSPBr5EK~E?3J{dWWPH&oCcNepYVqL?nh4D5ynfWip$m*YlZ8r^Z zuFEUL-nW!3qjRCLIWPT0x)FDL7>Yt7@8dA?R2kF@WE>ysMY+)lTsgNM#3VbXVGL}F z1O(>q>2a+_`6r5Xv$NZAnp=Kgnr3)cL(^=8ypEeOf3q8(HGe@7Tt59;yFl||w|mnO zHDxg2G3z8=(6wjj9kbcEY@Z0iOd7Gq5GiPS5% z*sF1J<#daxDV2Z8H>wxOF<;yKzMeTaSOp_|XkS9Sfn6Mpe9UBi1cSTieGG5$O;ZLIIJ60Y>SN4vC?=yE_CWlo(EEE$e4j?z&^FM%kNmRtlbEL^dPPgvs9sbK5fGw*r@ z+!EU@u$T8!nZh?Fdf_qk$VuHk^yVw`h`_#KoS*N%epIIOfQUy_&V}VWDGp3tplMbf z5Se1sJUC$7N0F1-9jdV2mmGK{-}fu|Nv;12jDy0<-kf^AmkDnu6j~TPWOgy1MT68|D z=4=50jVbUKdKaQgD`eWGr3I&^<6uhkjz$YwItY8%Yp9{z4-{6g{73<_b*@XJ4Nm3-3z z?BW3{aY_ccRjb@W1)i5nLg|7BnWS!B`_Uo9CWaE`Ij327QH?i)9A}4Ug4wmxVVa^b z-4+m%-wwOl7cKH7+=x&nrCrbEC)Q$fpg&V83#uEH;C=GNMz`ps@^RxK%T*8%OPnC` z{WO~J%nxYJ`x|N%?&i7?;{_8t^jM&=50HlaOQj8fS}_`moH$c;vI<|cruPFnpT8yU zS%rPOCUSd5Zdb(zwk`hqwTQn)*&n)uYsP*F_(~xEWq}C= zv30kFmZFwJZ@ELVX3?$dXQh|icO7UrL*_5G=I^xXjImz`ZPp>?g#tf(ej~KaIU0algsG!IS09;>?MvqGg#c{i+}qY|{P8W~O%#>|gFd z<1dr$-oxyRGN17yZo1OwLnzwYs0|;IS_nymNB0IlSzPQ%-r`?T=;_XQ^~&#}b|AB} zkNbN5uB?-sUB-T5QLlg%Uk3)uHB;>VIzGe9_J9 zaeISkQm!v(9d(0ML^b9fR^sfHFlH?7Mvddt37OuR{|O0{uv)(&-6<87W4 zyO>s!=cPgP3O&7xxU5DlIPw_o3O>6o6Qb?JWs3qw#p3sBc3g$?Dx zi(6D+DYgV;GrUis-CL%Qe{nvZnwaVXmbhH(|GFh|Q)k=1uvA$I@1DXI7bKlQ@8D6P zS?(*?><>)G49q0wr;NajpxP4W2G)kHl6^=Z>hrNEI4Mwd_$O6$1dXF;Q#hE(-eeW6 zz03GJF%Wl?HO=_ztv5*zRlcU~{+{k%#N59mgm~eK>P!QZ6E?#Cu^2)+K8m@ySvZ*5 z|HDT}BkF@3!l(0%75G=1u2hETXEj!^1Z$!)!lyGXlWD!_vqGE$Z)#cUVBqlORW>0^ zDjyVTxwKHKG|0}j-`;!R-p>}qQfBl(?($7pP<+Y8QE#M8SCDq~k<+>Q^Zf@cT_WdX3~BSe z+|KK|7OL5Hm5(NFP~j>Ct3*$wi0n0!xl=(C61`q&cec@mFlH(sy%+RH<=s)8aAPN`SfJdkAQjdv82G5iRdv8 zh{9wHUZaniSEpslXl^_ODh}mypC?b*9FzLjb~H@3DFSe;D(A-K3t3eOTB(m~I6C;(-lKAvit(70k`%@+O*Ztdz;}|_TS~B?Tpmi=QKC^m_ z2YpEaT3iiz*;T~ap1yiA)a`dKMwu`^UhIUeltNQ1Yjo=q@bI@&3zH?rVUg=IxLy-ni zyxDu%-Fr{H6owTjZU2O5>nDb=q&Jz_TjeSq%!2m40x&U6w~GQ({quPL73IsJS;f`$ zsuhioqCBj(gJ>2hoo)Gou7(WP*pX)f=Y=!=k!&1K?EYY%jJ~X&DnK{^saPQK<1BJ z_A`_{%ZozcB(3w$z^To^6d|XuT@=X~wtW!+{4ID@N{AB~J6AL5vuY>JwvWCNFKsKh zd}@>q@_WV#QZ&UJ0#?X(pXR!oyXOEG3rqzHbCzGLONDb042i$})fM@XF)uSP(DHUc z^&{|$*xe{cs?Gp8=B%RY3L7#$ve$?TWh>MZdxF1zH1v}1z+$Ov#G7?%D)bBCyDe*% zSeKSpETC2V1){II>@UwJi>4uBN+iAx+82E~gb|Cr&8E^i&)A!uv-g?jzH99wU}8+# z$nh>yvb;TwZmS@7LrvuCu_d0-WxFNI&C7%sWuTL%YU!l|I1{|->=dlOeHOCtUO#zkS3ESO8LHV4hTdQL5EdV zuWD33fFPH}HPrW^s$Qn1Xgp&AT6<-He{{4%eIu3rN=iK|9mURdKXfB&Q?qGok%!cs ze53UP{Z!TO-Y@q2;;k2avA3`lm4OoN4@S*k=UA)7H;qZ`d8`XaYFCv?Ba+uGW@r5v z&&{nf(24WSBOhc7!qF^@0cz;XcUynNaj6w2349;s!K{KVqs5yS{ z7VubS`2OzT^5#1~6Tt^RTvt9-J|D2F>y~>2;jeF>g`hx5l%B3H=aLExQihuYngzlnBTYOTHJQMzl>kwqN5JYs)Ej zblA@ntkUS~xi+}y6|(81helS}Q~&VB37qyV|S3Y=><^1wh%msQM?fz z<58MX(=|PSUKCF#)dbhR%D&xgCD?$aR0qen+wpp6 zst}vX18!Be96TD??j1HsHTUx(a&@F?=gT`Q$oJFFyrh^;zgz!(NlAHGn0cJy@us=w zNhC#l5G;H}+>49Nsh12=ZPO2r*2OBQe5kpb&1?*PIBFitK8}FUfb~S-#hKfF0o#&d z#3aPkB$9scYku&kA6{0xHnBV#&Wei5J>5T-XX-gUXEPo+9b7WL=*XESc(3BshL`aj zXp}QIp*40}oWJt*l043e8_5;H5PI5c)U&IEw5dF(4zjX0y_lk9 zAp@!mK>WUqHo)-jop=DoK>&no>kAD=^qIE7qis&_*4~ z6q^EF$D@R~3_xseCG>Ikb6Gfofb$g|75PPyyZN&tiRxqovo_k zO|HA|sgy#B<32gyU9x^&)H$1jvw@qp+1b(eGAb)O%O!&pyX@^nQd^9BQ4{(F8<}|A zhF&)xusQhtoXOOhic=8#Xtt5&slLia3c*a?dIeczyTbC#>FTfiLST57nc3@Y#v_Eg#VUv zT8cKH#f3=1PNj!Oroz_MAR*pow%Y0*6YCYmUy^7`^r|j23Q~^*TW#cU7CHf0eAD_0 zEWEVddxFgQ7=!nEBQ|ibaScslvhuUk^*%b#QUNrEB{3PG@uTxNwW}Bs4$nS9wc(~O zG7Iq>aMsYkcr!9#A;HNsJrwTDYkK8ikdj{M;N$sN6BqJ<8~z>T20{J8Z2rRUuH7~3 z=tgS`AgxbBOMg87UT4Lwge`*Y=01Dvk>)^{Iu+n6fuVX4%}>?3czOGR$0 zpp*wp>bsFFSV`V;r_m+TZns$ZprIi`OUMhe^cLE$2O+pP3nP!YB$ry}2THx2QJs3< za1;>d-AggCarrQ>&Z!d@;mW+!q6eXhb&`GbzUDSxpl8AJ#Cm#tuc)_xh(2NV=5XMs zrf_ozRYO$NkC=pKFX5OH8v1>0i9Z$ec`~Mf+_jQ68spn(CJwclDhEEkH2Qw;${J$clv__nUjn5jA0wCLEnu1j;v!0vB>Ri6m9`;R{JMS%^)4FC zU0Z44+u$I$w=Bj|iu4DT5h~sS`C*zbmX?@-crY}E+hy>}2~C0Nn(EKk@5^qO4@l@! z6O0lr%tzGC`D^)8xU3FnMZVm0kX1sBWhaQyzVoXFWwr%Ny?=2M{5s#5i7fTu3gEkG zc{(Pr$v=;`Y#&`y*J}#M9ux>0?xu!`$9cUKm#Bdd_&S#LPTS?ZPV6zN6>W6JTS~-LfjL{mB=b(KMk3 z2HjBSlJeyUVqDd=Mt!=hpYsvby2GL&3~zm;0{^nZJq+4vb?5HH4wufvr}IX42sHeK zm@x?HN$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/infra/haproxy/haproxy.cfg b/infra/haproxy/haproxy.cfg new file mode 100644 index 00000000..98071432 --- /dev/null +++ b/infra/haproxy/haproxy.cfg @@ -0,0 +1,57 @@ +# 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 + # Without it HAProxy sizes buffers from the file descriptor limit and uses ~100 MB. + maxconn 4096 + +defaults + mode http + log global + option httplog + option dontlognull + option forwardfor + timeout connect 1s + timeout client 30s + timeout server 30s + 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 + +# 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 + 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/infra/s3/bootstrap.sh b/infra/s3/bootstrap.sh new file mode 100644 index 00000000..5ca311de --- /dev/null +++ b/infra/s3/bootstrap.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -eu +# No tracing: credentials must not appear in logs. +mc alias set local http://s3:9000 "$(cat /run/secrets/s3_access_key_id)" "$(cat /run/secrets/s3_secret_access_key)" >/dev/null +mc mb --ignore-existing "local/$S3_BUCKET" +mc anonymous set none "local/$S3_BUCKET" + +api_user=$(cat /run/secrets/s3_api_access_key_id) +mc admin user add local "$api_user" "$(cat /run/secrets/s3_api_secret_access_key)" >/dev/null +cat > /tmp/attachment-policy.json </dev/null +mc admin policy attach local attachments-api --user "$api_user" >/dev/null 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..44d5a6d7 --- /dev/null +++ b/run/_template/config/authentication.conf @@ -0,0 +1,9 @@ +authentication { + realm = "messenger" + sessionTtlSeconds = 3600 + 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/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/run/_template/config/database.conf b/run/_template/config/database.conf new file mode 100644 index 00000000..2f27e6f8 --- /dev/null +++ b/run/_template/config/database.conf @@ -0,0 +1,12 @@ +database { + host = "localhost" + port = 5432 + name = "messenger" + username = "username" + 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 new file mode 100644 index 00000000..d2f835f0 --- /dev/null +++ b/run/_template/config/redis.conf @@ -0,0 +1,8 @@ + redis { + host = "localhost" + port = 6379 + # Only if the local Redis requires AUTH + #password = "" + connectTimeoutMs = 500 + commandTimeoutMs = 500 +} diff --git a/run/_template/config/storage.conf b/run/_template/config/storage.conf new file mode 100644 index 00000000..2fb4828d --- /dev/null +++ b/run/_template/config/storage.conf @@ -0,0 +1,6 @@ +storage { + endpoint = "http://localhost:9000" + bucket = "messenger-attachments" + accessKeyFile = "../../secrets/s3_api_access_key_id" + secretKeyFile = "../../secrets/s3_api_secret_access_key" +} diff --git a/scripts/attachment-failover.py b/scripts/attachment-failover.py new file mode 100644 index 00000000..de8cdd7d --- /dev/null +++ b/scripts/attachment-failover.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +"""Stop the exact API instance which accepted the upload, then verify the original. +Creates synthetic data; restores the stopped replica in finally. No credentials in output. +""" +import hashlib +import os +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, Failure, check + + +def main(): + c = Client(timeout=10) + alice, bob, eve, chat = c.bootstrap() + content = (ROOT / 'tests/fixtures/sample.png').read_bytes() + result = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png', + 201, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment') + attachment = result['json'] + target = result['headers']['X-Instance-Id'] + c.message(alice, chat, '', attachments=[attachment['id']]) + print('Upload: 201 ready; instance=' + target, flush=True) + # Probe the real known object anonymously from the internal network, not an unknown key. + probe = subprocess.run(['docker', 'compose', 'exec', '-T', 'lb', 'wget', '-S', '-O', '/dev/null', + 'http://s3:9000/' + os.environ.get('S3_BUCKET', 'messenger-attachments') + '/originals/' + attachment['id']], + cwd=ROOT, capture_output=True, text=True) + check(probe.returncode != 0 and '403' in probe.stderr, 'private bucket did not reject anonymous GET') + print('Anonymous S3 GET of known object: 403', flush=True) + path = '/api/v1/attachments/' + attachment['id'] + c.call('GET', path, 404, actor=eve) + c.call('GET', path + '/content', 404, actor=eve) + print('Outsider metadata/content: 404', flush=True) + try: + subprocess.run([str(ROOT / 'scripts/stop-one.sh')], cwd=ROOT, + env=dict(os.environ, TARGET_INSTANCE_ID=target), check=True, capture_output=True) + deadline = time.monotonic() + 15 + while True: + try: + download = c.call('GET', path + '/content', actor=bob) + check(download['headers']['X-Instance-Id'] != target, 'stopped instance returned content') + check(download['bytes'] == content, 'download differs from original') + print('Download: 200; instance=' + download['headers']['X-Instance-Id'], flush=True) + print('SHA-256: ' + hashlib.sha256(content).hexdigest(), flush=True) + break + except Failure: + if time.monotonic() >= deadline: + raise + time.sleep(0.2) + finally: + subprocess.run(['docker', 'compose', 'start', 'api'], cwd=ROOT, check=True, capture_output=True) + print('PASS: uploading replica stopped; original served by another replica; stopped replica restored') + + +if __name__ == '__main__': + main() diff --git a/scripts/attachment-interrupt.py b/scripts/attachment-interrupt.py new file mode 100644 index 00000000..f95385cf --- /dev/null +++ b/scripts/attachment-interrupt.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +"""Abort a binary request mid-body and verify temporary files and metadata are absent.""" +import os +from pathlib import Path +import socket +import subprocess +import sys +import time +import urllib.parse +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, check + +c = Client() +alice, bob, eve, chat = c.bootstrap() +name = 'interrupted-' + uuid.uuid4().hex + '.png' +base = urllib.parse.urlsplit(c.base) +body = (ROOT / 'tests/fixtures/sample.png').read_bytes() +headers = ('POST /api/v1/chats/' + chat['id'] + '/attachments?filename=' + name + ' HTTP/1.1\r\n' + 'Host: localhost\r\nAuthorization: Bearer ' + alice['token'] + '\r\n' + 'Content-Type: image/png\r\nContent-Length: 1048576\r\n\r\n') +with socket.create_connection((base.hostname, base.port or 80)) as connection: + connection.sendall(headers.encode() + body[:len(body)//2]) + time.sleep(0.5) +# Let the server observe EOF, or the bounded receive timeout. +ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api'], cwd=ROOT, text=True).split() +deadline = time.monotonic() + 20 +while True: + files = [subprocess.check_output(['docker', 'exec', cid, 'find', '/tmp', '-name', 'messenger-upload-*'], text=True).strip() for cid in ids] + if not any(files): + break + check(time.monotonic() < deadline, 'temporary upload file survived interruption/timeout') + time.sleep(0.2) +count = subprocess.check_output(['docker', 'compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', + "SELECT count(*) FROM attachments WHERE filename = '" + name + "'"], cwd=ROOT, text=True).strip() +check(count == '0', 'interrupted upload published metadata') +print('PASS: connection closed mid-body; temporary files removed on both replicas; metadata absent') diff --git a/scripts/attachment-load.py b/scripts/attachment-load.py new file mode 100644 index 00000000..ff021fbe --- /dev/null +++ b/scripts/attachment-load.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +"""Ten simultaneous binary uploads and sampled API cgroup memory, without tokens in output.""" +import concurrent.futures +import hashlib +import json +import os +from pathlib import Path +import random +import struct +import subprocess +import sys +import threading +import time +import zlib + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, check + + +def chunk(kind, data): + return struct.pack('!I', len(data)) + kind + data + struct.pack('!I', zlib.crc32(kind + data)) + + +def fixture(): + width = height = 1600 + rng = random.Random(42) + pixels = b''.join(b'\0' + rng.randbytes(width * 3) for _ in range(height)) + return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!IIBBBBB', width, height, 8, 2, 0, 0, 0)) + + chunk(b'IDAT', zlib.compress(pixels, 1)) + chunk(b'IEND', b'')) + + +def memory(ids): + result = {} + for container in ids: + value = subprocess.check_output(['docker', 'exec', container, 'cat', '/sys/fs/cgroup/memory.current'], text=True) + result[container[:12]] = int(value) + return result + + +def main(): + c = Client(timeout=60) + alice, bob, eve, chat = c.bootstrap() + data = fixture() + expected = hashlib.sha256(data).hexdigest() + ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api'], cwd=ROOT, text=True).split() + baseline = memory(ids) + samples = [] + stop = threading.Event() + start = time.monotonic() + + def sample(): + while not stop.is_set(): + samples.append({'seconds': round(time.monotonic() - start, 3), 'bytes': memory(ids)}) + stop.wait(0.2) + + barrier = threading.Barrier(10) + + def upload(index): + client = Client(timeout=60) + barrier.wait(timeout=30) + result = client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=load.png', + 201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment') + check(result['json']['sha256'] == expected, 'uploaded checksum mismatch') + downloaded = client.call('GET', '/api/v1/attachments/' + result['json']['id'] + '/content', actor=bob) + check(hashlib.sha256(downloaded['bytes']).hexdigest() == expected, 'download checksum mismatch') + return {'upload': index, 'seconds': round(result['elapsed'], 3), 'instance': result['headers']['X-Instance-Id']} + + sampler = threading.Thread(target=sample) + sampler.start() + try: + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool: + results = list(pool.map(upload, range(10))) + finally: + stop.set() + sampler.join() + peak = {key: max([baseline[key]] + [s['bytes'][key] for s in samples]) for key in baseline} + print(json.dumps({'concurrency': 10, 'file_bytes': len(data), 'pixels': 1600 * 1600, + 'sha256': expected, 'baseline_bytes': baseline, 'sampled_peak_bytes': peak, + 'results': results, 'samples': samples}, indent=2)) + + +if __name__ == '__main__': + main() diff --git a/scripts/check-s3-permissions.sh b/scripts/check-s3-permissions.sh new file mode 100755 index 00000000..416bdeb3 --- /dev/null +++ b/scripts/check-s3-permissions.sh @@ -0,0 +1,21 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +# Bootstrap is deliberately repeated; existing objects must survive both runs. +docker compose run --rm s3-init +docker compose run --rm s3-init +docker compose run --rm --entrypoint /bin/sh s3-init -ec ' +mc alias set app http://s3:9000 "$(cat /run/secrets/s3_api_access_key_id)" "$(cat /run/secrets/s3_api_secret_access_key)" >/dev/null +if mc admin info app >/dev/null 2>&1; then exit 1; fi +if mc mb app/forbidden-bucket >/dev/null 2>&1; then exit 1; fi +if mc anonymous set download "app/$S3_BUCKET" >/dev/null 2>&1; then exit 1; fi +printf test > /tmp/test-object +if mc cp /tmp/test-object "app/$S3_BUCKET/forbidden-prefix/object" >/dev/null 2>&1; then exit 1; fi +mc stat "app/$S3_BUCKET" >/dev/null +printf "PASS: app cannot administer MinIO, create buckets, change policy or write outside originals/\n" +' +if docker compose exec -T lb wget -q -T 2 -O /dev/null http://s3:9001; then + echo 'FAIL: console reachable from shared network' + exit 1 +fi +echo 'PASS: console unavailable from shared network' 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/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/init-secrets.sh b/scripts/init-secrets.sh new file mode 100755 index 00000000..7a4f6848 --- /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 s3_access_key_id s3_secret_access_key s3_api_access_key_id s3_api_secret_access_key; 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/load/failover-under-load.sh b/scripts/load/failover-under-load.sh new file mode 100755 index 00000000..972e46cf --- /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-lab5-$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/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..88dbc5d9 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# Restarts the balancer, every API replica, PostgreSQL, Redis and S3, keeping the named volume (no `down -v`, no re-seed). +set -eu +cd "$(dirname "$0")/.." + +docker compose stop lb api redis postgres s3 +docker compose up -d --wait 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/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/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/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..f8e65527 --- /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 9 "$container" >/dev/null diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 00000000..534ce9e4 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,21 @@ +dependencyResolutionManagement { + repositories { + mavenCentral() + } + + versionCatalogs { + create("awssdk") { + from("aws.sdk.kotlin:version-catalog:1.6.41") + } + + 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-lab5" 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..d2214cd9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -0,0 +1,50 @@ +package com.mtusi.messenger.main + +import com.mtusi.messenger.main.plugins.authentication.configureAuthentication +import com.mtusi.messenger.main.plugins.configureAttachmentCleanup +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.configureGracefulShutdown +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 +import io.ktor.server.application.Application +import io.ktor.server.netty.EngineMain + +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() { + + configureKoin() + configureDatabase() + configureRedis() + configureGracefulShutdown() + configureSessionRevocationSync() + configureAttachmentCleanup() + + configureContentNegotiation() + configureDefaultHeaders() + configureInstanceId() + configureCors() + configureRequestId() + configureCallLogging() + configureStatusPages() + + configureAuthentication() + configureRouting() + +} 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/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..ce78889c --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.config.authentication + +interface AuthenticationConfig { + + val realm: String + val sessionTtlSeconds: Long + 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/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..7ba9c360 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt @@ -0,0 +1,12 @@ +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, + override val sessionRevokeTimeoutMs: Long = 8000 +) : AuthenticationConfig 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 new file mode 100644 index 00000000..1a971655 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt @@ -0,0 +1,22 @@ +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 migrateOnStart: Boolean + + 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..1a2ebc92 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt @@ -0,0 +1,17 @@ +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, + 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/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/config/storage/DefaultStorageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/storage/DefaultStorageConfig.kt new file mode 100644 index 00000000..f3fd2456 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/storage/DefaultStorageConfig.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.config.storage + +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 DefaultStorageConfig( + @Provided app: Application +) : StorageConfig by + (app.environment.config.propertyOrNull("storage")?.getAs() ?: SerialStorageConfig()) diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/storage/SerialStorageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/storage/SerialStorageConfig.kt new file mode 100644 index 00000000..93c3a1ab --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/storage/SerialStorageConfig.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.config.storage + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialStorageConfig( + override val endpoint: String = "http://localhost:9000", + override val bucket: String = "attachments", + override val accessKey: String? = null, + override val secretKey: String? = null, + override val accessKeyFile: String? = null, + override val secretKeyFile: String? = null, +) : StorageConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/storage/StorageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/storage/StorageConfig.kt new file mode 100644 index 00000000..c79f9746 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/storage/StorageConfig.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.config.storage + +interface StorageConfig { + val endpoint: String + val bucket: String + val accessKey: String? + val secretKey: String? + val accessKeyFile: String? + val secretKeyFile: String? +} 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/attachment/AttachmentController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt new file mode 100644 index 00000000..6ae1f5a8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt @@ -0,0 +1,106 @@ +package com.mtusi.messenger.main.infrastructure.controllers.attachment + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentService +import com.mtusi.messenger.main.infrastructure.services.attachment.MAX_ATTACHMENT_BYTES +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.utils.requireUuid +import io.ktor.http.* +import io.ktor.server.auth.authenticate +import io.ktor.server.request.* +import io.ktor.server.response.* +import io.ktor.server.routing.* +import io.ktor.utils.io.readAvailable +import kotlinx.coroutines.withTimeoutOrNull +import org.koin.ktor.ext.inject +import com.mtusi.messenger.main.utils.withTemporaryUpload +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import kotlinx.coroutines.sync.Semaphore +import kotlinx.coroutines.sync.withPermit +import kotlin.time.Duration.Companion.milliseconds + +fun Routing.configureAttachmentRouting() { + + val service by inject() + val uploads = Semaphore(1) + + authenticate(SESSION_AUTH) { + + post("/api/v1/chats/{chat_id}/attachments") { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + + service.requireMember(chatId, actor.id) + + val filename = call.request.queryParameters["filename"] + ?: throw ApiException(ErrorCode.INVALID_REQUEST, "filename is required") + + if (filename.isBlank() || filename.codePointCount(0, filename.length) !in 1..255 || filename.any { it == '/' || it == '\\' || it.isISOControl() }) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid filename") + } + + val type = try { + call.request.contentType().withoutParameters().toString().lowercase() + } catch (_: BadContentTypeFormatException) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid Content-Type") + } + if (type !in setOf("image/png", "image/jpeg")) { + throw ApiException(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "expected PNG or JPEG") + } + + if ((call.request.contentLength() ?: 0) > MAX_ATTACHMENT_BYTES) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") + } + + uploads.withPermit { + withTemporaryUpload { file -> + withTimeoutOrNull(15_000.milliseconds) { + withContext(Dispatchers.IO) { + val input = call.receiveChannel() + val buffer = ByteArray(8192) + var size = 0L + file.outputStream().use { output -> + while (true) { + val count = input.readAvailable(buffer) + if (count == -1) break + size += count + if (size > MAX_ATTACHMENT_BYTES) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") + } + output.write(buffer, 0, count) + } + } + } + } ?: throw ApiException(ErrorCode.INVALID_REQUEST, "upload timed out") + + val attachment = service.upload(chatId, actor.id, filename, type, file) + call.respond(HttpStatusCode.Created, attachment) + } + } + + } + + get("/api/v1/attachments/{id}") { + call.respond(service.find(requireUuid(call.parameters["id"], "id"), call.currentUser.id)) + } + + get("/api/v1/attachments/{id}/content") { + + val attachment = service.find(requireUuid(call.parameters["id"], "id"), call.currentUser.id) + + if ((call.request.queryParameters["variant"] ?: "original") != "original") { + throw ApiException(ErrorCode.NOT_FOUND, "variant not found") + } + + call.response.header(HttpHeaders.CacheControl, "private, no-store") + call.response.header("X-Content-Type-Options", "nosniff") + + call.respondBytes(service.content(attachment), ContentType.parse(attachment.contentType)) + } + + } +} 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..d17fd116 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt @@ -0,0 +1,67 @@ +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 com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.koin.ktor.ext.inject +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 +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.ensureActive + +fun Routing.configureHealthRouting() = route("/health") { + + get("live") { + call.respond(HttpStatusCode.OK, HealthResponse()) + } + + val storage by inject() + + // PostgreSQL and S3 are dependencies of the synchronous path; Redis has a DB fallback. + 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") + } + true + } catch (cause: Exception) { + + if (cause is CancellationException) { + currentCoroutineContext().ensureActive() + } + + call.application.log.warn("Readiness check failed: ${cause.message}") + false + } + + if (!isDatabaseReachable) { + call.respondError(ErrorCode.UNAVAILABLE, "database unavailable") + return@get + } + + if (!withContext(Dispatchers.IO) { storage.ready() }) { + call.respondError(ErrorCode.UNAVAILABLE, "object storage 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..0fd89ada --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt @@ -0,0 +1,94 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.infrastructure.types.ApiException +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.requireIdempotencyKey +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 idempotencyKey = requireIdempotencyKey(call.request.headers["Idempotency-Key"]) + val body = call.receive() + + val attachmentIds = body.attachmentIds.map { requireUuid(it, "attachment_ids") } + if (attachmentIds.size > 10 || attachmentIds.distinct().size != attachmentIds.size || + body.text.codePointCount(0, body.text.length) > MAX_MESSAGE_TEXT_LENGTH || + (body.text.isBlank() && attachmentIds.isEmpty())) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid message") + } + + val result = suspendedTransaction { + messageService.send( + chatId = chatId, + senderId = actor.id, + text = body.text, + idempotencyKey = idempotencyKey, + attachmentIds = attachmentIds + ) + } + + 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) + } + } + + 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..417ec578 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt @@ -0,0 +1,9 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message.requests + +import kotlinx.serialization.Serializable + +@Serializable +data class CreateMessageRequest( + val text: String, + val attachmentIds: List = emptyList(), +) 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/Attachment.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt new file mode 100644 index 00000000..e65ac4cd --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt @@ -0,0 +1,21 @@ +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 Attachment( + val id: SerialUUID, + val chatId: SerialUUID, + val ownerId: SerialUUID, + val filename: String, + val contentType: String, + val sizeBytes: Long, + val sha256: String, + val createdAt: Instant, + val updatedAt: Instant = createdAt, + val status: String = "ready", + val variants: List = emptyList(), + val errorCode: String? = null, +) 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..01b2ced1 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt @@ -0,0 +1,15 @@ +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, + val attachmentIds: List = emptyList(), +) 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/attachment/AttachmentModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt new file mode 100644 index 00000000..2a26edd0 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +object AttachmentModel : UUIDTable("attachments") { + val chatId = reference("chat_id", ChatModel) + val ownerId = reference("owner_id", UserModel) + val filename = varchar("filename", 255) + val contentType = varchar("content_type", 32) + val sizeBytes = long("size_bytes") + val sha256 = varchar("sha256", 64) + val createdAt = timestamp("created_at") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/MessageAttachmentModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/MessageAttachmentModel.kt new file mode 100644 index 00000000..507fe240 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/MessageAttachmentModel.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import com.mtusi.messenger.main.infrastructure.models.message.MessageModel +import org.jetbrains.exposed.v1.core.Table + +object MessageAttachmentModel : Table("message_attachments") { + val messageId = reference("message_id", MessageModel) + val attachmentId = reference("attachment_id", AttachmentModel) + val position = integer("position") + override val primaryKey = PrimaryKey(messageId, attachmentId) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/UploadJournalModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/UploadJournalModel.kt new file mode 100644 index 00000000..ae55f759 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/UploadJournalModel.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import com.mtusi.messenger.main.utils.exposed.operations.plus +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.datetime.timestamp + +object UploadJournalModel : UUIDTable("attachment_uploads") { + + val expiresAt = timestamp("expires_at").defaultExpression(CurrentTimestamp + longLiteral(3600)) + val abandoned = bool("abandoned").default(false) + val nextAttemptAt = timestamp("next_attempt_at").defaultExpression(CurrentTimestamp + longLiteral(3600)) + + init { + index("attachment_uploads_next_attempt_idx", false, nextAttemptAt) + } +} 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/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/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/attachment/AttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt new file mode 100644 index 00000000..1487d761 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import java.util.UUID + +interface AttachmentRepository { + + fun create(attachment: Attachment) + + fun findById(id: UUID): Attachment? + + fun attach( + messageId: UUID, + ids: List + ) + + fun idsForMessage(messageId: UUID): List + + fun idsForMessages(messageIds: List): Map> +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt new file mode 100644 index 00000000..024c6330 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt @@ -0,0 +1,88 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.SortOrder +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class ExposedAttachmentRepository : AttachmentRepository { + + override fun create(attachment: Attachment) { + AttachmentModel.insert { + it[id] = attachment.id + it[chatId] = attachment.chatId + it[ownerId] = attachment.ownerId + it[filename] = attachment.filename + it[contentType] = attachment.contentType + it[sizeBytes] = attachment.sizeBytes + it[sha256] = attachment.sha256 + it[createdAt] = attachment.createdAt + } + } + + override fun findById( + id: UUID + ): Attachment? { + return AttachmentModel + .selectAll() + .where(AttachmentModel.id eq id) + .firstOrNull() + ?.toAttachment() + } + + override fun attach( + messageId: UUID, + ids: List + ) { + ids.forEachIndexed { index, id -> + MessageAttachmentModel.insert { + it[this.messageId] = messageId + it[attachmentId] = id + it[position] = index + } + } + } + + override fun idsForMessage( + messageId: UUID + ): List { + return idsForMessages(listOf(messageId))[messageId].orEmpty() + } + + override fun idsForMessages( + messageIds: List + ): Map> { + if (messageIds.isEmpty()) { + return emptyMap() + } + + return MessageAttachmentModel + .selectAll() + .where(MessageAttachmentModel.messageId inList messageIds) + .orderBy(MessageAttachmentModel.position to SortOrder.ASC) + .groupBy( + keySelector = { it[MessageAttachmentModel.messageId].value }, + valueTransform = { it[MessageAttachmentModel.attachmentId].value } + ) + } + + private fun ResultRow.toAttachment() = Attachment( + id = this[AttachmentModel.id].value, + chatId = this[AttachmentModel.chatId].value, + ownerId = this[AttachmentModel.ownerId].value, + filename = this[AttachmentModel.filename], + contentType = this[AttachmentModel.contentType], + sizeBytes = this[AttachmentModel.sizeBytes], + sha256 = this[AttachmentModel.sha256], + createdAt = this[AttachmentModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedUploadJournalRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedUploadJournalRepository.kt new file mode 100644 index 00000000..edd2ef92 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedUploadJournalRepository.kt @@ -0,0 +1,77 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel +import com.mtusi.messenger.main.utils.exposed.operations.plus +import org.jetbrains.exposed.v1.core.SortOrder +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.core.inList +import org.jetbrains.exposed.v1.core.lessEq +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.core.vendors.ForUpdateOption +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.select +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.update +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class ExposedUploadJournalRepository : UploadJournalRepository { + + override fun register( + id: UUID + ) { + UploadJournalModel.insert { + it[this.id] = id + } + } + + override fun lockForCompletion( + id: UUID + ): Boolean { + val canComplete = (UploadJournalModel.abandoned eq false) and + (UploadJournalModel.expiresAt greater CurrentTimestamp) + + return UploadJournalModel + .select(canComplete) + .where(UploadJournalModel.id eq id) + .forUpdate() + .firstOrNull() + ?.get(canComplete) + ?: false + } + + override fun complete( + id: UUID + ) { + UploadJournalModel.deleteWhere { + UploadJournalModel.id eq id + } + } + + override fun claimExpired(): List { + + val ids = UploadJournalModel + .selectAll() + .where(UploadJournalModel.nextAttemptAt lessEq CurrentTimestamp) + .orderBy(UploadJournalModel.nextAttemptAt to SortOrder.ASC) + .limit(100) + .forUpdate(ForUpdateOption.PostgreSQL.ForUpdate(ForUpdateOption.PostgreSQL.MODE.SKIP_LOCKED)) + .map { + it[UploadJournalModel.id].value + } + + if (ids.isNotEmpty()) { + UploadJournalModel.update({ UploadJournalModel.id inList ids }) { + it[abandoned] = true + it[nextAttemptAt] = CurrentTimestamp + longLiteral(60) + } + } + + return ids + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/UploadJournalRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/UploadJournalRepository.kt new file mode 100644 index 00000000..09fff459 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/UploadJournalRepository.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import java.util.UUID + +interface UploadJournalRepository { + fun register(id: UUID) + + fun lockForCompletion(id: UUID): Boolean + + fun complete(id: UUID) + + fun claimExpired(): List +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt new file mode 100644 index 00000000..42154b81 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt @@ -0,0 +1,19 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.storage + +import java.io.File +import java.util.UUID + +interface ObjectStorageRepository { + + suspend fun put( + id: UUID, + file: File, + contentType: String + ) + + suspend fun get(id: UUID): ByteArray + + suspend fun delete(id: UUID) + + suspend fun ready(): Boolean +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt new file mode 100644 index 00000000..72afac5f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt @@ -0,0 +1,116 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.storage + +import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider +import aws.sdk.kotlin.services.s3.S3Client +import aws.sdk.kotlin.services.s3.deleteObject +import aws.sdk.kotlin.services.s3.model.GetObjectRequest +import aws.sdk.kotlin.services.s3.headBucket +import aws.sdk.kotlin.services.s3.putObject +import aws.smithy.kotlin.runtime.content.asByteStream +import aws.smithy.kotlin.runtime.content.toByteArray +import aws.smithy.kotlin.runtime.net.url.Url +import com.mtusi.messenger.main.config.storage.StorageConfig +import com.mtusi.messenger.main.infrastructure.services.attachment.MAX_ATTACHMENT_BYTES +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.utils.resolveSecret +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.withTimeoutOrNull +import org.koin.core.annotation.Single +import java.io.File +import java.util.UUID +import kotlin.time.Duration.Companion.milliseconds + +@Single +class S3ObjectStorageRepository( + private val config: StorageConfig +) : ObjectStorageRepository { + + private val client by lazy { + S3Client { + endpointUrl = Url.parse(config.endpoint) + region = "us-east-1" + forcePathStyle = true + + credentialsProvider = StaticCredentialsProvider { + accessKeyId = requireNotNull(resolveSecret(config.accessKey, config.accessKeyFile)) + secretAccessKey = requireNotNull(resolveSecret(config.secretKey, config.secretKeyFile)) + } + } + } + + override suspend fun put( + id: UUID, + file: File, + contentType: String + ) { + request { + client.putObject { + bucket = config.bucket + key = getObjectKey(id) + body = file.asByteStream() + this.contentType = contentType + contentLength = file.length() + } + } + } + + override suspend fun get( + id: UUID + ): ByteArray = request { + client.getObject(GetObjectRequest { + bucket = config.bucket + key = getObjectKey(id) + }) { response -> + + val contentLength = requireNotNull(response.contentLength) + + check(contentLength >= 1L && contentLength <= MAX_ATTACHMENT_BYTES.toLong()) + + val bytes = requireNotNull(response.body?.toByteArray()) + + check(bytes.size <= MAX_ATTACHMENT_BYTES) + + bytes + } + } + + override suspend fun delete( + id: UUID + ) { + request { + client.deleteObject { + bucket = config.bucket + key = getObjectKey(id) + } + } + } + + override suspend fun ready(): Boolean { + return try { + request { + client.headBucket { + bucket = config.bucket + } + } + true + } catch (_: ApiException) { + false + } + } + + private fun getObjectKey(id: UUID) = "originals/$id" + + private suspend fun request(block: suspend () -> T): T { + return try { + withTimeoutOrNull(8000.milliseconds) { + block() + } ?: throw ApiException(ErrorCode.UNAVAILABLE, "object storage unavailable") + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + throw ApiException(ErrorCode.UNAVAILABLE, "object storage unavailable") + } + } + +} 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..31ef6897 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt @@ -0,0 +1,23 @@ +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 + +interface SessionCacheRepository : Repository { + + suspend fun find( + tokenHash: String + ): CachedSession? + + suspend fun putIfAbsent( + session: Session, + cachedUntil: Instant + ) + + 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..ab9d69b9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt @@ -0,0 +1,25 @@ +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 + +interface SessionRevocationRepository : Repository { + + fun add( + revocation: SessionRevocation + ) + + 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..4e49658c --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt @@ -0,0 +1,30 @@ +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 + + 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..0595efa7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt @@ -0,0 +1,99 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message + +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +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( + private val attachments: AttachmentRepository +) : 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 findById( + id: UUID + ): Message? { + return MessageModel + .selectAll() + .where(MessageModel.id eq id) + .firstOrNull() + ?.toMessage(attachments.idsForMessage(id)) + } + + 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) + ) + ) + } + + val rows = MessageModel + .selectAll() + .where(condition) + .orderBy(MessageModel.createdAt to SortOrder.ASC, MessageModel.id to SortOrder.ASC) + .limit(limit) + .toList() + + val attachmentIds = attachments.idsForMessages(rows.map { it[MessageModel.id].value }) + + return rows.map { + it.toMessage(attachmentIds[it[MessageModel.id].value].orEmpty()) + } + } + + private fun ResultRow.toMessage(attachmentIds: List) = Message( + id = this[MessageModel.id].value, + chatId = this[MessageModel.chatId].value, + senderId = this[MessageModel.senderId].value, + text = this[MessageModel.text], + attachmentIds = attachmentIds, + 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..86bddb81 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt @@ -0,0 +1,26 @@ +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 findById( + id: UUID + ): Message? + + fun findPageByChatId( + chatId: UUID, + after: PageKey?, + limit: Int + ): List + +} 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/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/attachment/AttachmentCleanupService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt new file mode 100644 index 00000000..125ac525 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt @@ -0,0 +1,34 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.plugins.suspendedTransaction +import kotlinx.coroutines.CancellationException +import org.koin.core.annotation.Single + +@Single +class AttachmentCleanupService( + private val journalRepository: UploadJournalRepository, + private val objectStorageRepository: ObjectStorageRepository +) { + suspend fun cleanup(): Int { + + val ids = suspendedTransaction { + journalRepository.claimExpired() + } + + var deleted = 0 + ids.forEach { id -> + try { + objectStorageRepository.delete(id) + deleted++ + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + // The durable tombstone remains eligible for the next retry. + } + } + + return deleted + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt new file mode 100644 index 00000000..b4fffe56 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt @@ -0,0 +1,28 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import java.io.File +import java.util.UUID + +interface AttachmentService { + + suspend fun requireMember( + chatId: UUID, + actorId: UUID + ) + + suspend fun upload( + chatId: UUID, + actorId: UUID, + filename: String, + contentType: String, + file: File + ): Attachment + + suspend fun find( + id: UUID, + actorId: UUID + ): Attachment + + suspend fun content(attachment: Attachment): ByteArray +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt new file mode 100644 index 00000000..ea95a32c --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt @@ -0,0 +1,94 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository +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 com.mtusi.messenger.main.utils.nowMicros +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.koin.core.annotation.Single +import java.io.File +import java.util.UUID +import kotlin.time.Clock + +@Single +class DefaultAttachmentService( + private val journalRepository: UploadJournalRepository, + private val attachmentRepository: AttachmentRepository, + private val chatRepository: ChatRepository, + private val objectStorageRepository: ObjectStorageRepository, +) : AttachmentService { + + override suspend fun requireMember( + chatId: UUID, + actorId: UUID + ) { + if (!suspendedTransaction { chatRepository.isMember(chatId, actorId) }) { + notFound() + } + } + + override suspend fun upload( + chatId: UUID, + actorId: UUID, + filename: String, + contentType: String, + file: File + ): Attachment { + + requireMember(chatId, actorId) + + val (type, hash) = withContext(Dispatchers.IO) { + val bytes = file.readBytes() + ImageValidator.validate(bytes, contentType) to bytes.computeSha256() + } + + val attachment = Attachment( + id = UUID.randomUUID(), + chatId = chatId, + ownerId = actorId, + filename = filename, + contentType = type, + sizeBytes = file.length(), + sha256 = hash, + createdAt = Clock.System.nowMicros() + ) + + suspendedTransaction { + journalRepository.register(attachment.id) + } + objectStorageRepository.put(attachment.id, file, type) + + suspendedTransaction { + if (!journalRepository.lockForCompletion(attachment.id)) { + throw ApiException(ErrorCode.UNAVAILABLE, "upload expired") + } + attachmentRepository.create(attachment) + journalRepository.complete(attachment.id) + } + return attachment + } + + override suspend fun find( + id: UUID, + actorId: UUID + ): Attachment = suspendedTransaction { + val attachment = attachmentRepository.findById(id) ?: notFound() + if (!chatRepository.isMember(attachment.chatId, actorId)) { + notFound() + } + attachment + } + + override suspend fun content(attachment: Attachment): ByteArray = withContext(Dispatchers.IO) { + objectStorageRepository.get(attachment.id) + } + + private fun notFound(): Nothing = throw ApiException(ErrorCode.NOT_FOUND, "attachment or chat not found") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt new file mode 100644 index 00000000..c693f2c2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt @@ -0,0 +1,127 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import java.nio.ByteBuffer +import java.util.zip.CRC32 +import java.io.ByteArrayInputStream +import javax.imageio.ImageIO +import javax.imageio.stream.MemoryCacheImageInputStream + +const val MAX_ATTACHMENT_BYTES = 10 * 1024 * 1024 +private const val MAX_PIXELS = 40_000_000L + +object ImageValidator { + + fun validate( + bytes: ByteArray, + declaredType: String + ): String { + + if (bytes.size > MAX_ATTACHMENT_BYTES) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") + } + + try { + MemoryCacheImageInputStream(ByteArrayInputStream(bytes)).use { input -> + val readers = ImageIO.getImageReaders(input) + if (!readers.hasNext()) { + invalid() + } + + val reader = readers.next() + + try { + // Ignore optional compressed text metadata to bound memory usage. + reader.setInput(input, false, true) + + val type = when (reader.formatName.lowercase()) { + "png" -> "image/png" + "jpeg", "jpg" -> "image/jpeg" + else -> invalid() + } + + if (declaredType != type) { + invalid() + } + + if (type == "image/png") { + validatePngChunks(bytes) + } + + if (type == "image/jpeg" && ( + bytes.size < 4 || + bytes[bytes.lastIndex - 1] != 0xff.toByte() || + bytes.last() != 0xd9.toByte() + )) { + invalid() + } + + val width = reader.getWidth(0) + val height = reader.getHeight(0) + + if (width <= 0 || height <= 0) { + invalid() + } + + if (width.toLong() * height > MAX_PIXELS) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "image exceeds 40 million pixels") + } + + var warning = false + reader.addIIOReadWarningListener { _, _ -> warning = true } + + val image = reader.read(0) ?: invalid() + image.flush() + + if (warning) { + invalid() + } + + return type + } finally { + reader.dispose() + } + } + } catch (cause: ApiException) { + throw cause + } catch (_: Exception) { + invalid() + } + + } + + private fun validatePngChunks(bytes: ByteArray) { + + var offset = 8 + + while (offset + 12 <= bytes.size) { + val length = ByteBuffer.wrap(bytes, offset, 4).int + + if (length < 0 || length.toLong() + offset + 12 > bytes.size) { + invalid() + } + + val type = String(bytes, offset + 4, 4, Charsets.US_ASCII) + + val crc = CRC32().apply { update(bytes, offset + 4, length + 4) }.value + val expected = ByteBuffer.wrap(bytes, offset + 8 + length, 4).int.toLong() and 0xffffffffL + if (crc != expected) { + invalid() + } + + offset += length + 12 + if (type == "IEND") { + if (length != 0 || offset != bytes.size) { + invalid() + } + return + } + } + invalid() + } + + private fun invalid(): Nothing = throw ApiException( + ErrorCode.UNSUPPORTED_MEDIA_TYPE, "invalid PNG/JPEG or MIME mismatch" + ) +} 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..26d91bab --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt @@ -0,0 +1,38 @@ +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 + + suspend fun findSessionByToken( + 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 new file mode 100644 index 00000000..c8953367 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt @@ -0,0 +1,323 @@ +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.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 +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.milliseconds +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +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, + 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() + + private val inFlightLookups = ConcurrentHashMap>() + + private val isCacheAvailable = AtomicBoolean(true) + + 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) + + // 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? { + if (trustedAtFailures != cacheFailures.get() && !restoreCache()) { + return null + } + return withCache { sessionCacheRepository.find(tokenHash) } + } + + 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) + ) + } + } + + 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..c91ca0ae --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt @@ -0,0 +1,136 @@ +package com.mtusi.messenger.main.infrastructure.services.message + +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +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 idempotencyRepository: MessageIdempotencyRepository, + private val attachments: AttachmentRepository, + private val config: MessageConfig +) : MessageService { + + override fun send( + chatId: UUID, + senderId: UUID, + text: String, + idempotencyKey: String, + attachmentIds: List + ): MessageCreationResult { + + // Access is checked on every retry too: a replay must not outlive the membership. + if (!chatRepository.isMember(chatId, senderId)) { + return MessageCreationResult.ChatNotFound + } + + attachmentIds.forEach { id -> + if (attachments.findById(id)?.chatId != chatId) + throw ApiException( + ErrorCode.NOT_FOUND, "attachment not found") + } + + val requestHash = (text + if (attachmentIds.isEmpty()) "" else "\u0000attachments:" + attachmentIds.joinToString(",")) + .toByteArray().computeSha256() + + val now = Clock.System.nowMicros() + + repeat(CLAIM_ATTEMPTS) { + + if (idempotencyRepository.claim(senderId, chatId, idempotencyKey, requestHash, now)) { + + val message = messageRepository.create( + chatId = chatId, + senderId = senderId, + text = text + ) + attachments.attach(message.id, attachmentIds) + idempotencyRepository.attachMessage(senderId, chatId, idempotencyKey, message.id) + + return MessageCreationResult.Success(message.copy(attachmentIds = attachmentIds)) + } + + // 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" + } + + val original = checkNotNull(messageRepository.findById(messageId)) { + "message of an idempotency key is missing" + } + + if (original.text != text || original.attachmentIds != attachmentIds) { + return MessageCreationResult.IdempotencyKeyReused + } + + return MessageCreationResult.Success(original) + } + + error("Idempotency key could not be claimed") + } + + 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..17fdabc2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt @@ -0,0 +1,24 @@ +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, + idempotencyKey: String, + attachmentIds: List = emptyList() + ): 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..1f260a32 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt @@ -0,0 +1,17 @@ +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 object IdempotencyKeyReused : 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/AttachmentCleanup.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt new file mode 100644 index 00000000..76f69853 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt @@ -0,0 +1,29 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentCleanupService +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 + +fun Application.configureAttachmentCleanup() { + + val cleanupService by inject() + + launch { + while (isActive) { + delay(30.seconds) + try { + cleanupService.cleanup() + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + log.warn("Attachment cleanup journal is unavailable") + } + } + } +} 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/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 new file mode 100644 index 00000000..d54e0b83 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt @@ -0,0 +1,181 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +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.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 +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 + } + ) + if (config.migrateOnStart) { + applyDatabaseMigrations(dataSource) + } else { + validateDatabaseMigrations(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") + } + ) +} + +private fun flyway(dataSource: DataSource) = Flyway.configure() + .dataSource(dataSource) + .locations("classpath:migrations") + .load() + +fun applyDatabaseMigrations( + dataSource: DataSource +) { + 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) { + generateMigration(args.joinToString("_")) +} + +@OptIn(ExperimentalDatabaseMigrationApi::class) +private fun generateMigration(description: String) { + require(description.isNotEmpty()) { "Description cannot be empty" } + + val tables: Array = arrayOf( + AttachmentModel, + UploadJournalModel, + MessageAttachmentModel, + UserModel, + ChatModel, + ChatMemberModel, + MessageModel, + MessageIdempotencyKeyModel, + 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/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/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/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..437b6ad7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt @@ -0,0 +1,20 @@ +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 com.mtusi.messenger.main.infrastructure.controllers.attachment.configureAttachmentRouting +import io.ktor.server.application.Application +import io.ktor.server.routing.routing + +fun Application.configureRouting() = routing { + configureSwaggerRouting() + configureHealthRouting() + configureUserRouting() + configureAuthenticationRouting() + configureChatRouting() + configureMessageRouting() + configureAttachmentRouting() +} 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/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..bc605e09 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt @@ -0,0 +1,55 @@ +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 } + +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)) +} + +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) +} + +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..8288a810 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.utils + +import java.io.File + +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/TemporaryUpload.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/TemporaryUpload.kt new file mode 100644 index 00000000..9bee2e7e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/TemporaryUpload.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.utils + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.File +import java.nio.file.Files + +suspend fun withTemporaryUpload(block: suspend (File) -> T): T = withContext(Dispatchers.IO) { + // Own creation and cleanup in the same context: cancellation during dispatch back + // must not lose a newly created file before a caller can enter its finally block. + val file = Files.createTempFile("messenger-upload-", ".bin").toFile() + try { + block(file) + } finally { + Files.deleteIfExists(file.toPath()) + } +} 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..86b8e3c7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.utils + +import kotlin.time.Clock +import kotlin.time.Instant + +private const val NANOS_PER_MICRO = 1_000 + +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..aad25ee1 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt @@ -0,0 +1,45 @@ +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 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}$" +) + +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 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") + } + return UUID.fromString(value) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt new file mode 100644 index 00000000..4c0abd5d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt @@ -0,0 +1,25 @@ +package com.mtusi.messenger.main.utils.exposed.operations + +import org.jetbrains.exposed.v1.core.Expression +import org.jetbrains.exposed.v1.core.ExpressionWithColumnType +import org.jetbrains.exposed.v1.core.QueryBuilder +import org.jetbrains.exposed.v1.datetime.KotlinInstantColumnType +import kotlin.time.Instant + +class InstantPlusSeconds( + private val instant: Expression, + private val seconds: Expression +) : ExpressionWithColumnType() { + + override val columnType = KotlinInstantColumnType() + + override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { + append("(") + append(instant) + append(" + (") + append(seconds) + append(" * INTERVAL '1 second'))") + } +} + +operator fun Expression.plus(seconds: Expression) = InstantPlusSeconds(this, seconds) 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..4d51dca4 --- /dev/null +++ b/src/main/resources/application.conf @@ -0,0 +1,112 @@ +ktor { + application { + modules = [com.mtusi.messenger.main.AppKt.module] + } + + deployment { + host = "0.0.0.0" + host = ${?APP_HOST} + port = 8080 + port = ${?APP_PORT} + # 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} + 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 + # Replicas run with false: compose applies migrations once in the `migrate` service. + migrateOnStart = true + migrateOnStart = ${?DB_MIGRATE_ON_START} +} + +redis { + host = "localhost" + host = ${?REDIS_HOST} + port = 6379 + port = ${?REDIS_PORT} + password = ${?REDIS_PASSWORD} + passwordFile = ${?REDIS_PASSWORD_FILE} + connectTimeoutMs = 500 + 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 + 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} + # 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} +} + +# S3 credentials are local files, never source-controlled values. +storage { + endpoint = "http://localhost:9000" + endpoint = ${?S3_ENDPOINT} + bucket = "messenger-attachments" + bucket = ${?S3_BUCKET} + accessKey = ${?S3_ACCESS_KEY_ID} + secretKey = ${?S3_SECRET_ACCESS_KEY} + accessKeyFile = ${?S3_ACCESS_KEY_ID_FILE} + secretKeyFile = ${?S3_SECRET_ACCESS_KEY_FILE} +} 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/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/main/resources/migrations/V4__attachments.sql b/src/main/resources/migrations/V4__attachments.sql new file mode 100644 index 00000000..07b752ab --- /dev/null +++ b/src/main/resources/migrations/V4__attachments.sql @@ -0,0 +1,19 @@ +CREATE TABLE attachments ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id), + owner_id UUID NOT NULL REFERENCES users(id), + filename VARCHAR(255) NOT NULL, + content_type VARCHAR(32) NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes BIGINT NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 VARCHAR(64) NOT NULL, + created_at TIMESTAMP WITH TIME ZONE NOT NULL +); +CREATE INDEX attachments_chat_id_idx ON attachments(chat_id); +CREATE TABLE message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id UUID NOT NULL REFERENCES attachments(id), + position INTEGER NOT NULL CHECK (position BETWEEN 0 AND 9), + PRIMARY KEY (message_id, attachment_id), + UNIQUE (message_id, position) +); +CREATE INDEX message_attachments_attachment_id_idx ON message_attachments(attachment_id); diff --git a/src/main/resources/migrations/V5__attachment_uploads.sql b/src/main/resources/migrations/V5__attachment_uploads.sql new file mode 100644 index 00000000..881c8711 --- /dev/null +++ b/src/main/resources/migrations/V5__attachment_uploads.sql @@ -0,0 +1,7 @@ +CREATE TABLE attachment_uploads ( + id UUID PRIMARY KEY, + expires_at TIMESTAMPTZ NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL '1 hour'), + abandoned BOOLEAN NOT NULL DEFAULT FALSE, + next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL '1 hour') +); +CREATE INDEX attachment_uploads_next_attempt_idx ON attachment_uploads(next_attempt_at); diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt new file mode 100644 index 00000000..c8181d20 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt @@ -0,0 +1,139 @@ +package com.mtusi.messenger.main.infrastructure.controllers.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.support.* +import io.ktor.client.call.body +import io.ktor.client.request.* +import io.ktor.http.* +import io.ktor.http.content.OutgoingContent +import java.awt.image.BufferedImage +import java.io.ByteArrayOutputStream +import javax.imageio.ImageIO +import kotlin.test.* + +class AttachmentControllerTest { + + @Test + + fun `upload originals ACL message links and idempotency`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val outsider = client.register("outsider") + val chat = client.createChat(alice, "files", listOf(alice.id, bob.id)) + for (format in listOf("png", "jpeg")) { + val bytes = image(format) + val upload = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.$format") { + auth(alice) + contentType(ContentType.parse("image/$format")) + setBody(bytes) + } + assertEquals(HttpStatusCode.Created, upload.status) + val attachment = upload.body() + assertEquals("ready", attachment.status) + assertEquals(bytes.size.toLong(), attachment.sizeBytes) + val path = "/api/v1/attachments/${attachment.id}" + val download = client.get("$path/content") { auth(bob) } + assertEquals(HttpStatusCode.OK, download.status) + assertContentEquals(bytes, download.body()) + assertEquals(HttpStatusCode.NotFound, client.get(path) { auth(outsider) }.status) + assertEquals(HttpStatusCode.NotFound, client.get("$path/content") { auth(outsider) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("$path/content").status) + assertEquals(HttpStatusCode.NotFound, client.get("$path/content?variant=thumbnail") { auth(bob) }.status) + val outsiderChat = client.createChat(outsider, "outsider", listOf(outsider.id)) + assertEquals(HttpStatusCode.NotFound, client.post("/api/v1/chats/${outsiderChat.id}/messages") { + auth(outsider) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest("", listOf(attachment.id.toString()))) + }.status) + val key = "file-$format" + suspend fun send(ids: List) = client.post("/api/v1/chats/${chat.id}/messages") { + auth(bob) + idempotencyKey(key) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest("", ids)) + } + val first = send(listOf(attachment.id.toString())) + assertEquals(HttpStatusCode.Created, first.status) + val message = first.body() + assertEquals(listOf(attachment.id), message.attachmentIds) + assertEquals(message, send(listOf(attachment.id.toString())).body()) + val otherChat = client.createChat(bob, "other", listOf(bob.id)) + assertEquals(HttpStatusCode.NotFound, client.post("/api/v1/chats/${otherChat.id}/messages") { + auth(bob) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest("", listOf(attachment.id.toString()))) + }.status) + } + } + + @Test + + fun `invalid images filenames and byte limit return client errors`() = runApp { client -> + val user = client.register("upload") + val chat = client.createChat(user, "files", listOf(user.id)) + suspend fun upload( + bytes: ByteArray, + type: String = "image/png", + name: String = "photo.png" + ) = + client.post("/api/v1/chats/${chat.id}/attachments") { + auth(user) + parameter("filename", name) + contentType(ContentType.parse(type)) + setBody(bytes) + } + val malformedMime = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.png") { + auth(user) + // Keep the invalid header in raw content so the client does not parse it first. + setBody(object : OutgoingContent.ByteArrayContent() { + override val headers = Headers.build { + append(HttpHeaders.ContentType, "image") + } + override fun bytes() = image("png") + }) + } + assertEquals(HttpStatusCode.BadRequest, malformedMime.status) + assertEquals(HttpStatusCode.UnsupportedMediaType, upload("not an image".toByteArray()).status) + assertEquals(HttpStatusCode.UnsupportedMediaType, upload(image("png"), "image/jpeg").status) + assertEquals(HttpStatusCode.UnsupportedMediaType, upload(image("png").copyOf(40)).status) + for (name in listOf("", " ", "\u2003", "../x.png", "a\\b.png", "a\n.png", "a\u0000.png", "x".repeat(256))) + assertEquals(HttpStatusCode.BadRequest, upload(image("png"), name = name).status) + assertEquals(HttpStatusCode.PayloadTooLarge, upload(ByteArray(10 * 1024 * 1024 + 1)).status) + } + + @Test + + fun `S3 outage fails upload download and readiness without losing the original`() = runApp { client -> + val user = client.register("outage") + val chat = client.createChat(user, "files", listOf(user.id)) + val bytes = image("png") + suspend fun upload() = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.png") { + auth(user) + contentType(ContentType.Image.PNG) + setBody(bytes) + } + val attachment = upload().body() + val path = "/api/v1/attachments/${attachment.id}/content" + val container = TestStorage.container + container.dockerClient.pauseContainerCmd(container.containerId).exec() + try { + assertEquals(HttpStatusCode.ServiceUnavailable, upload().status) + assertEquals(HttpStatusCode.ServiceUnavailable, client.get(path) { auth(user) }.status) + assertEquals(HttpStatusCode.ServiceUnavailable, client.get("/health/ready").status) + assertEquals(HttpStatusCode.OK, client.get("/health/live").status) + } finally { + container.dockerClient.unpauseContainerCmd(container.containerId).exec() + } + assertContentEquals(bytes, client.get(path) { auth(user) }.body()) + assertEquals(HttpStatusCode.OK, client.get("/health/ready").status) + } + + private fun image(format: String): ByteArray = ByteArrayOutputStream().use { + ImageIO.write(BufferedImage(32, 20, BufferedImage.TYPE_INT_RGB), format, it) + it.toByteArray() + } +} 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..5cc3bc1b --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt @@ -0,0 +1,77 @@ +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.idempotencyKey +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) + idempotencyKey() + 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..61ce8052 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt @@ -0,0 +1,197 @@ +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.idempotencyKey +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) + idempotencyKey() + 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) + idempotencyKey() + 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) + idempotencyKey() + 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) + idempotencyKey() + 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) + idempotencyKey() + 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/message/MessageIdempotencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt new file mode 100644 index 00000000..e3d32e19 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt @@ -0,0 +1,158 @@ +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 +import kotlin.time.Duration.Companion.milliseconds + +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.milliseconds) + 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/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/repositories/message/MessageAttachmentsQueryTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageAttachmentsQueryTest.kt new file mode 100644 index 00000000..306d23ff --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageAttachmentsQueryTest.kt @@ -0,0 +1,58 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedAttachmentRepository +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.time.Clock + +class MessageAttachmentsQueryTest { + + @Test + fun `history loads ordered attachment links in one query for the entire page`() = runApp { client -> + val user = client.register("historyfiles") + val chat = client.createChat(user, "history", listOf(user.id)) + val attachments = ExposedAttachmentRepository() + val messages = ExposedMessageRepository(attachments) + + suspendedTransaction { + val ids = List(2) { + val id = UUID.randomUUID() + attachments.create(Attachment( + id = id, + chatId = chat.id, + ownerId = user.id, + filename = "file.png", + contentType = "image/png", + sizeBytes = 1, + sha256 = "0".repeat(64), + createdAt = Clock.System.now() + )) + id + } + val created = List(100) { index -> + val message = messages.create(chat.id, user.id, "message $index") + if (index % 2 == 0) { + attachments.attach(message.id, ids.reversed()) + } + message + } + val before = statementCount + val page = messages.findPageByChatId(chat.id, null, 100) + assertEquals(2, statementCount - before) + assertEquals(100, page.size) + val expected = created.mapIndexed { index, message -> + message.id to if (index % 2 == 0) ids.reversed() else emptyList() + }.toMap() + page.forEach { message -> + assertEquals(expected[message.id], message.attachmentIds) + } + assertEquals(ids.reversed(), messages.findById(created.first().id)?.attachmentIds) + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt new file mode 100644 index 00000000..9f1a4eae --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt @@ -0,0 +1,124 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.config.storage.SerialStorageConfig +import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedAttachmentRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedUploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.S3ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.chat.ExposedChatRepository +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.support.TestStorage +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.infrastructure.models.attachment.UploadJournalModel +import com.mtusi.messenger.main.utils.exposed.operations.plus +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.update +import java.awt.image.BufferedImage +import java.io.File +import java.nio.file.Files +import java.util.UUID +import javax.imageio.ImageIO +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class AttachmentCleanupTest { + + @Test + fun `orphan cleanup respects grace and fences late publishers`() = runApp { client -> + + val user = client.register("cleanup") + val chat = client.createChat(user, "cleanup", listOf(user.id)) + val journal = ExposedUploadJournalRepository() + val attachments = ExposedAttachmentRepository() + val storage = S3ObjectStorageRepository(SerialStorageConfig( + endpoint = TestStorage.endpoint, + bucket = TestStorage.BUCKET, + accessKey = TestStorage.ACCESS_KEY, + secretKey = TestStorage.SECRET_KEY + )) + val cleanup = AttachmentCleanupService(journal, storage) + + val file = withContext(Dispatchers.IO) { + Files.createTempFile("cleanup-test-", ".png") + }.toFile() + + withContext(Dispatchers.IO) { + ImageIO.write(BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "png", file) + } + + var orphanId: UUID? = null + val failingStorage = object : ObjectStorageRepository by storage { + override suspend fun put(id: UUID, file: File, contentType: String) { + storage.put(id, file, contentType) + orphanId = id + error("injected failure after PUT, before metadata COMMIT") + } + } + try { + val failingService = DefaultAttachmentService(journal, attachments, ExposedChatRepository(), failingStorage) + + assertFailsWith { + failingService.upload(chat.id, user.id, "file.png", "image/png", file) + } + + val id = requireNotNull(orphanId) + assertEquals(null, suspendedTransaction { attachments.findById(id) }) + assertContentEquals(file.readBytes(), storage.get(id)) + cleanup.cleanup() + assertContentEquals(file.readBytes(), storage.get(id), "fresh upload must survive grace") + + val service = DefaultAttachmentService(journal, attachments, ExposedChatRepository(), storage) + val published = service.upload(chat.id, user.id, "ready.png", "image/png", file) + suspendedTransaction { + UploadJournalModel.update({ UploadJournalModel.id eq id }) { + it[expiresAt] = CurrentTimestamp + longLiteral(-1) + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + } + + val unavailableStorage = object : ObjectStorageRepository by storage { + override suspend fun delete(id: UUID) { + error("injected S3 delete outage") + } + } + + assertEquals(0, AttachmentCleanupService(journal, unavailableStorage).cleanup()) + assertContentEquals(file.readBytes(), storage.get(id)) + suspendedTransaction { + UploadJournalModel.update({ UploadJournalModel.id eq id }) { + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + } + + assertTrue(cleanup.cleanup() >= 1) + assertFailsWith { storage.get(id) } + assertFalse(suspendedTransaction { journal.lockForCompletion(id) }) + assertContentEquals(file.readBytes(), storage.get(published.id)) + + // A process suspended longer than grace can complete a late PUT, but cannot publish metadata. + storage.put(id, file, "image/png") + suspendedTransaction { + UploadJournalModel.update({ UploadJournalModel.id eq id }) { + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + } + + assertTrue(cleanup.cleanup() >= 1) + assertFailsWith { storage.get(id) } + assertContentEquals(file.readBytes(), storage.get(published.id)) + } finally { + file.delete() + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidatorTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidatorTest.kt new file mode 100644 index 00000000..9d627861 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidatorTest.kt @@ -0,0 +1,84 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import java.awt.image.BufferedImage +import java.io.ByteArrayOutputStream +import java.io.DataOutputStream +import java.util.zip.DeflaterOutputStream +import java.nio.ByteBuffer +import java.util.zip.CRC32 +import javax.imageio.ImageIO +import kotlin.test.* + +class ImageValidatorTest { + + private fun png() = ByteArrayOutputStream().also { + ImageIO.write(BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB), "png", it) + }.toByteArray() + + @Test + fun `pixel limit is enforced on header before decoding`() { + val bytes = png() + ByteBuffer.wrap(bytes, 16, 8).putInt(10000).putInt(4001) + val crc = CRC32().apply { update(bytes, 12, 17) }.value + ByteBuffer.wrap(bytes, 29, 4).putInt(crc.toInt()) + assertEquals(ErrorCode.PAYLOAD_TOO_LARGE, assertFailsWith { + ImageValidator.validate(bytes, "image/png") + }.code) + } + @Test + fun `corrupted CRC and missing end chunk are rejected`() { + val good = png() + for (bytes in listOf(good.copyOf(good.size - 12), good.clone().apply { this[29] = (this[29].toInt() xor 1).toByte() })) { + assertEquals(ErrorCode.UNSUPPORTED_MEDIA_TYPE, assertFailsWith { + ImageValidator.validate(bytes, "image/png") + }.code) + } + } + @Test + fun `compressed text metadata does not consume the decoded image memory budget`() { + val compressed = ByteArrayOutputStream().also { output -> + DeflaterOutputStream(output).use { + it.write(ByteArray(262144) { 65 }) + } + }.toByteArray() + + for (imageType in listOf(BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_BYTE_INDEXED)) { + val original = ByteArrayOutputStream().also { + ImageIO.write(BufferedImage(2, 2, imageType), "png", it) + }.toByteArray() + val text = "Comment".toByteArray() + byteArrayOf(0, 0) + compressed + val internationalText = "Comment".toByteArray() + byteArrayOf(0, 1, 0, 0, 0) + compressed + val bytes = ByteArrayOutputStream().also { output -> + output.write(original, 0, 33) + repeat(2500) { + output.write(chunk("zTXt", text)) + output.write(chunk("iTXt", internationalText)) + } + output.write(original, 33, original.size - 33) + }.toByteArray() + + // Less than 2 MiB on the wire, over 1 GiB if all text is retained after inflation. + assertTrue(bytes.size < MAX_ATTACHMENT_BYTES) + assertEquals("image/png", ImageValidator.validate(bytes, "image/png")) + } + } + + private fun chunk(type: String, data: ByteArray): ByteArray { + val name = type.toByteArray(Charsets.US_ASCII) + val crc = CRC32().apply { + update(name) + update(data) + } + return ByteArrayOutputStream().also { output -> + DataOutputStream(output).use { + it.writeInt(data.size) + it.write(name) + it.write(data) + it.writeInt(crc.value.toInt()) + } + }.toByteArray() + } + +} 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..8a8df748 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt @@ -0,0 +1,156 @@ +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.Dispatchers +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +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) { + withContext(Dispatchers.IO) { + Thread.sleep(200) + } + } + b.syncPendingRevocations() + assertEquals("revoked", cached(issued)) + assertNull(b.findSessionByToken(issued.token)) + } + +} 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..7e55cdca --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt @@ -0,0 +1,175 @@ +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.Duration.Companion.milliseconds +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).milliseconds) + 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/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/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..51842a86 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt @@ -0,0 +1,237 @@ +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.idempotencyKey +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 kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +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 + +/** + * 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 { + + /** + * 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(), + sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS + ) { 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() + + // 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) + } + + @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), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { 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() + + 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() + + // 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), 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() + 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) + + // 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") + withContext(Dispatchers.IO) { + Thread.sleep(200) + } + } + } + } 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), 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() + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) + } + + docker.unpauseContainerCmd(redis.containerId).exec() + loseTombstone(redis, alice, copy) + + 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( + HttpStatusCode.Unauthorized, + client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + idempotencyKey() + 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() } + TestDatabase.awaitConnectable(database) + + 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/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/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..0ba00322 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt @@ -0,0 +1,170 @@ +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.header +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, + idempotencyTtlSeconds: Long, + sessionRevokeTimeoutMs: Long, +) = HoconApplicationConfig( + ConfigFactory.parseMap( + mapOf( + "storage.endpoint" to TestStorage.endpoint, + "storage.bucket" to TestStorage.BUCKET, + "storage.accessKey" to TestStorage.ACCESS_KEY, + "storage.secretKey" to TestStorage.SECRET_KEY, + "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, + "authentication.sessionRevokeTimeoutMs" to sessionRevokeTimeoutMs, + "messages.idempotencyTtlSeconds" to idempotencyTtlSeconds, + "instance.id" to "test-instance", + "cors.allowSameOrigin" to true, + ) + ) +) + +/** + * 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(), + idempotencyTtlSeconds: Long = 86400, + sessionRevokeTimeoutMs: Long = 8000, + block: suspend ApplicationTestBuilder.(HttpClient) -> Unit +) = testApplication { + environment { + config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds, sessionRevokeTimeoutMs) + } + 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) + +/** + * 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, + 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..9453a1ef --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt @@ -0,0 +1,48 @@ +package com.mtusi.messenger.main.support + +import org.testcontainers.containers.PostgreSQLContainer +import java.sql.DriverManager +import java.sql.SQLException +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. + */ + /** + * 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) + .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() + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestStorage.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestStorage.kt new file mode 100644 index 00000000..710fdebe --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestStorage.kt @@ -0,0 +1,45 @@ +package com.mtusi.messenger.main.support + +import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider +import aws.sdk.kotlin.services.s3.S3Client +import aws.sdk.kotlin.services.s3.createBucket +import aws.smithy.kotlin.runtime.net.url.Url +import kotlinx.coroutines.runBlocking +import org.testcontainers.containers.GenericContainer +import org.testcontainers.containers.wait.strategy.Wait + +object TestStorage { + const val ACCESS_KEY = "test-access-key" + const val SECRET_KEY = "test-secret-key-for-local-tests" + const val BUCKET = "test-attachments" + val container by lazy { + GenericContainer("quay.io/minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e") + .withEnv("MINIO_ROOT_USER", ACCESS_KEY) + .withEnv("MINIO_ROOT_PASSWORD", SECRET_KEY) + .withCommand("server", "/data") + .withExposedPorts(9000) + .waitingFor(Wait.forHttp("/minio/health/live").forPort(9000)) + .also { + it.start() + } + } + val endpoint: String by lazy { + val url = "http://${container.host}:${container.getMappedPort(9000)}" + runBlocking { + S3Client { + endpointUrl = Url.parse(url) + region = "us-east-1" + forcePathStyle = true + credentialsProvider = StaticCredentialsProvider { + accessKeyId = ACCESS_KEY + secretAccessKey = SECRET_KEY + } + }.use { client -> + client.createBucket { + bucket = BUCKET + } + } + } + url + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/utils/TemporaryUploadTest.kt b/src/test/kotlin/com/mtusi/messenger/main/utils/TemporaryUploadTest.kt new file mode 100644 index 00000000..2f942b40 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/utils/TemporaryUploadTest.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.utils + +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.cancelAndJoin +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import java.io.File +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertFailsWith + +class TemporaryUploadTest { + + @Test + fun `temporary file is removed when upload is cancelled`() = runBlocking { + val created = CompletableDeferred() + val job = launch { + withTemporaryUpload { file -> + created.complete(file) + awaitCancellation() + } + } + val file = created.await() + job.cancelAndJoin() + assertFalse(file.exists()) + } + + @Test + fun `temporary file is removed on success and failure`() = runBlocking { + val completed = withTemporaryUpload { it } + assertFalse(completed.exists()) + + val failure = assertFailsWith { + withTemporaryUpload { + throw UploadFailure(it) + } + } + assertFalse(failure.file.exists()) + } + + private class UploadFailure(val file: File) : IllegalStateException("injected failure") +} -- 2.54.0 From 248f8a5cb0f2cd42864c5fa5f4c5b91722898f50 Mon Sep 17 00:00:00 2001 From: Mraksimus Date: Wed, 18 Nov 2026 13:55:24 +0300 Subject: [PATCH 2/3] Completed for a grade of three --- .env.example | 8 +- REPORT.md | 184 ++++--- build.gradle.kts | 2 + compose.yaml | 86 ++++ evidence/attachment-failover.txt | 6 + evidence/check.txt | 4 + evidence/compose.txt | 13 + evidence/gradle-tests.txt | 45 ++ evidence/persistence.txt | 23 + evidence/s3-permissions.txt | 10 + evidence/smoke.txt | 2 + evidence/worker-recovery.txt | 3 + gradle/libs.versions.toml | 6 + infra/rabbitmq/bootstrap.sh | 6 + infra/rabbitmq/rabbitmq.conf | 3 + infra/s3/bootstrap.sh | 2 +- run/_template/config/broker.conf | 15 + run/_template/config/worker.conf | 6 + scripts/attachment-failover.py | 6 +- scripts/attachment-load.py | 3 +- scripts/attachment-worker-recovery.py | 66 +++ scripts/init-secrets.sh | 2 +- scripts/restart.sh | 4 +- .../kotlin/com/mtusi/messenger/main/App.kt | 19 +- .../main/config/broker/BrokerConfig.kt | 16 + .../main/config/broker/DefaultBrokerConfig.kt | 16 + .../main/config/broker/SerialBrokerConfig.kt | 15 + .../main/config/worker/DefaultWorkerConfig.kt | 16 + .../main/config/worker/SerialWorkerConfig.kt | 20 + .../main/config/worker/WorkerConfig.kt | 10 + .../attachment/AttachmentController.kt | 106 +++- .../controllers/health/HealthController.kt | 27 +- .../main/infrastructure/dto/Attachment.kt | 2 +- .../main/infrastructure/dto/AttachmentJob.kt | 25 + .../infrastructure/dto/AttachmentVariant.kt | 13 + .../main/infrastructure/dto/ProcessedImage.kt | 39 ++ .../models/attachment/AttachmentJobModel.kt | 24 + .../models/attachment/AttachmentModel.kt | 4 + .../attachment/AttachmentVariantModel.kt | 23 + .../attachment/AttachmentRepository.kt | 37 ++ .../attachment/ExposedAttachmentRepository.kt | 166 +++++- .../jobs/AttachmentJobRepository.kt | 59 +++ .../jobs/ExposedAttachmentJobRepository.kt | 250 ++++++++++ .../storage/ObjectStorageRepository.kt | 19 - .../attachment/AttachmentCleanupService.kt | 42 +- .../services/attachment/AttachmentService.kt | 6 +- .../DefaultAttachmentProcessingService.kt | 171 +++++++ .../attachment/DefaultAttachmentService.kt | 133 ++++- .../DefaultImageProcessingService.kt | 206 ++++++++ .../attachment/ImageProcessingService.kt | 12 + .../services/attachment/ImageValidator.kt | 20 +- .../attachment/jobs/JobBrokerService.kt | 20 + .../jobs/LegacyAttachmentDispatchService.kt | 28 ++ .../jobs/RabbitMqJobBrokerService.kt | 314 ++++++++++++ .../storage/ObjectStorageService.kt | 46 ++ .../storage/S3ObjectStorageService.kt} | 126 ++++- .../services/message/DefaultMessageService.kt | 59 ++- .../main/plugins/AttachmentCleanup.kt | 9 +- .../main/plugins/AttachmentWorker.kt | 50 ++ .../mtusi/messenger/main/plugins/Database.kt | 26 +- .../mtusi/messenger/main/plugins/JobBroker.kt | 16 + .../main/plugins/SessionRevocationSync.kt | 9 +- .../com/mtusi/messenger/main/worker/Worker.kt | 38 ++ src/main/resources/application.conf | 26 + .../migrations/V6__attachment_processing.sql | 29 ++ .../V7__attachment_retry_metadata.sql | 3 + .../V8__backfill_attachment_thumbnails.sql | 19 + src/main/resources/worker.conf | 2 + .../attachment/AttachmentControllerTest.kt | 174 ++++++- .../dto/AttachmentJobContractTest.kt | 54 ++ .../attachment/AttachmentCleanupTest.kt | 204 ++++++-- .../attachment/AttachmentPipelineTest.kt | 471 ++++++++++++++++++ .../attachment/ImageProcessingServiceTest.kt | 284 +++++++++++ .../LegacyAttachmentMigrationTest.kt | 129 +++++ .../jobs/RabbitMqJobBrokerServiceTest.kt | 137 +++++ .../mtusi/messenger/main/support/TestApp.kt | 25 +- .../messenger/main/support/TestBroker.kt | 52 ++ 77 files changed, 4100 insertions(+), 251 deletions(-) create mode 100644 evidence/attachment-failover.txt create mode 100644 evidence/check.txt create mode 100644 evidence/compose.txt create mode 100644 evidence/gradle-tests.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/s3-permissions.txt create mode 100644 evidence/smoke.txt create mode 100644 evidence/worker-recovery.txt create mode 100644 infra/rabbitmq/bootstrap.sh create mode 100644 infra/rabbitmq/rabbitmq.conf create mode 100644 run/_template/config/broker.conf create mode 100644 run/_template/config/worker.conf create mode 100644 scripts/attachment-worker-recovery.py create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/broker/BrokerConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/broker/DefaultBrokerConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/broker/SerialBrokerConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/worker/DefaultWorkerConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/worker/SerialWorkerConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/config/worker/WorkerConfig.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJob.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentVariant.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/ProcessedImage.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentVariantModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt delete mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/JobBrokerService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt rename src/main/kotlin/com/mtusi/messenger/main/infrastructure/{repositories/attachment/storage/S3ObjectStorageRepository.kt => services/attachment/storage/S3ObjectStorageService.kt} (61%) create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/plugins/JobBroker.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt create mode 100644 src/main/resources/migrations/V6__attachment_processing.sql create mode 100644 src/main/resources/migrations/V7__attachment_retry_metadata.sql create mode 100644 src/main/resources/migrations/V8__backfill_attachment_thumbnails.sql create mode 100644 src/main/resources/worker.conf create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJobContractTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerServiceTest.kt create mode 100644 src/test/kotlin/com/mtusi/messenger/main/support/TestBroker.kt diff --git a/.env.example b/.env.example index 3d65fa79..a0b3ae35 100644 --- a/.env.example +++ b/.env.example @@ -19,7 +19,13 @@ S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_api_secret_access_key MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 BROKER_URL_FILE=/run/secrets/broker_url -WORKER_CONCURRENCY=2 +# The implementation uses structured fields instead of a credential-bearing URL. +BROKER_HOST=broker +BROKER_PORT=5672 +BROKER_USER=messenger +BROKER_PASSWORD_FILE=/run/secrets/broker_password +BROKER_QUEUE=attachment.process.v1 +WORKER_CONCURRENCY=1 JOB_MAX_ATTEMPTS=5 JOB_TIMEOUT_SECONDS=60 STAGING_RETENTION_SECONDS=86400 diff --git a/REPORT.md b/REPORT.md index 33ade9d4..f0cbd128 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,36 +1,18 @@ # Отчёт — лабораторная 5 - Автор, группа: Кучерявенко Максим, БВТ2303. -- Текущий этап: перенос работающей основы lab4 для просмотра перед выполнением lab5. -- Целевая оценка: будет выбрана при переходе к реализации; работа ведётся поэтапно, начиная с уровня 3. +- Целевая оценка: **3**. Уровни 4 и 5 не заявляются. +- Основа: собственный проект lab4 на Kotlin 2.3.20, Ktor 3.3.0 и JDK 21. Архитектура controller → service → repository, Koin/KSP и Exposed 1.0.0 сохранены; стиль сверяется с существующими репозиториями проекта и Pixefy. +- Новые зависимости: RabbitMQ Java Client 5.26.0 и metadata-extractor 2.19.0. Библиотека Java Client предоставляет AMQP API, поэтому здесь используются её Java configuration objects; операции PostgreSQL написаны на Exposed DSL, S3 — на AWS SDK for Kotlin DSL. - Commit/PR: после просмотра и публикации. -- Основа: Kotlin 2.3.20, Ktor 3.3.0, JDK 21, Koin/KSP, Exposed 1.0.0, PostgreSQL, Redis и MinIO. Существующие архитектура controller → service → repository, DSL и стиль кода сохранены; ориентир — проекты Pixefy. -- Источники: исходники собственного lab4, задание и контракты lab5, проекты Pixefy. ИИ использован для переноса и проверки структуры проекта. +- ИИ использован как парный разработчик при реализации, ревью, подготовке тестов и отчёта. -## Что перенесено +## Запуск из чистого clone -- Исходники приложения и 80 JVM-тестов, конфигурации, Gradle wrapper и зависимости. -- Миграции Flyway V1–V5: пользователи, чаты, сообщения, сессии, идемпотентность, вложения и журнал загрузок. -- Bearer-аутентификация, Redis-кэш с fallback в PostgreSQL, keyset-пагинация и идемпотентная отправка сообщений. -- Синхронные upload/download PNG/JPEG, приватный bucket, проверки доступа и изображений, временные файлы и очистка по журналу. -- Dockerfile, Compose с двумя API-репликами, HAProxy, PostgreSQL, Redis, MinIO и повторяемым bootstrap. -- Скрипты запуска, проверки S3-прав, прерывания загрузки, нагрузки, переключения реплик и перезапуска стенда. -- Проектные инструкции AGENTS.md и локальный skill Kotlin/Exposed для дальнейшего соблюдения архитектуры и стиля. - -Имя Compose/Gradle-проекта изменено на `mtusi-messenger-lab5`, образ приложения — на `mtusi-messenger:lab5`. Это отделяет контейнеры, сети, named volumes и образ от lab4. Оригиналы и записи БД из работающего стенда lab4 не переносились: lab5 получает собственные volumes при запуске. - -Исходные README задания, lab.json, contracts/, публичные tests/, examples/, CI и шаблон PR лабораторной 5 сохранены. В `.env.example` сохранены новые настройки broker/worker/jobs/staging; пути S3 credentials согласованы с отдельным пользователем API. - -Исторические материалы lab4 находятся в [archive/lab4](archive/lab4/README.md): исходный отчёт, evidence и документы к защите. Их результаты не являются доказательствами выполнения lab5. - -## Запуск перенесённой основы из чистого clone - -Нужны Docker с Compose v2, Python 3.9+ и openssl. Для Gradle-проверок — JDK 21 и Docker для Testcontainers. +Нужны Docker с Compose v2, Python 3.9+, openssl. JVM-тесты требуют JDK 21 и работающий Docker: Testcontainers поднимает отдельные PostgreSQL, Redis, MinIO и RabbitMQ. ```sh ./scripts/init-secrets.sh - -# Отдельный порт, чтобы не мешать lab4 на 8084 и другим лабораторным. export LAB_HTTP_PORT=8085 export BASE_URL=http://localhost:8085 @@ -40,57 +22,145 @@ docker compose ps make check ./gradlew test +make test -# Безопасная остановка с сохранением данных: +# Основной сценарий lab5: worker остановлен, 202 принят, +# обе API и broker перезапущены, тот же файл доходит до ready. +python3 scripts/attachment-worker-recovery.py + +# Доступ через другую API-реплику и приватность объекта. +python3 scripts/attachment-failover.py + +# Сохранность после перезапуска всех компонентов, включая worker/broker. +make test-persistence ACTION_SCRIPT=scripts/restart.sh + +# Безопасная остановка с сохранением volumes: docker compose down # Только для намеренного удаления всех учебных данных lab5: # docker compose down -v ``` -Секреты создаются заново локальным скриптом; секреты lab4, локальная `.env`, runtime-конфигурации, build/cache и `.git` не копировались. `./gradlew run` создаёт недостающие конфигурации из `run/_template`, сохраняя существующие локальные настройки. Для запуска вне Compose необходимо отдельно настроить доступные с host PostgreSQL, Redis и S3. +Секреты генерируются локально, существующие файлы сохраняются. В Git не включаются `.env`, `secrets/`, `run/development/`, данные volumes и build/cache. Проект Compose называется `mtusi-messenger-lab5`: его контейнеры, сеть и volumes отделены от lab4. Наружу опубликован только HAProxy на loopback; PostgreSQL, Redis, S3 и RabbitMQ доступны внутри сети стенда. -### Настройки инфраструктуры +Для запуска через `./gradlew run` нужны отдельные локальные настройки подключений в `run/development/config`. Шаблон не является готовой конфигурацией существующего PostgreSQL на компьютере; роль `username` необходимо заменить реальной. Автоматически менять локальную БД или её пользователей задача `run` не будет. Воспроизводимый путь проверки этой работы — Compose. -- `LAB_HTTP_PORT` меняет только опубликованный порт HAProxy; внутри API остаётся 8080. В Compose по умолчанию две API-реплики; их число задаётся `API_REPLICAS`. -- `SESSION_TTL_SECONDS`, `IDEMPOTENCY_TTL_SECONDS` и `S3_BUCKET` передаются через Compose. Доступ к PostgreSQL и Redis использует существующие `DB_*` и `REDIS_*`, а не предложенные skeleton `DATABASE_URL_FILE` и `CACHE_URL_FILE`. -- API обращается к `http://s3:9000`; `S3_ACCESS_KEY_ID_FILE` и `S3_SECRET_ACCESS_KEY_FILE` указывают на secret-файлы прикладного S3-пользователя. Административные credentials доступны только MinIO и bootstrap. -- Образы и зависимости сохранены из lab4: PostgreSQL 17.6, Redis 8.4.0, HAProxy 3.2.4; MinIO RELEASE.2025-09-07T16-13-09Z и mc RELEASE.2025-08-13T08-35-41Z закреплены digest. API имеет лимиты 1 CPU / 1 GiB, временный `/tmp` — tmpfs 32 MiB. -- `S3_REGION=us-east-1`, пределы 10 MiB и 40 млн пикселей закреплены в реализации. `APP_ENV` и переключатель `SESSION_STORE` не меняют реализацию. -- Переменные `BROKER_URL_FILE`, `WORKER_CONCURRENCY`, `JOB_MAX_ATTEMPTS`, `JOB_TIMEOUT_SECONDS`, `STAGING_RETENTION_SECONDS` пока являются частью skeleton и не используются перенесённым приложением. +## Сервисы и настройки -## Архитектура текущего этапа +| Роль | Compose service | Версия / хранение | +| --- | --- | --- | +| Балансировщик | lb | HAProxy 3.2.4; внешний порт LAB_HTTP_PORT, по умолчанию 8080 | +| HTTP API | api × 2 | Образ mtusi-messenger:lab5, собранный из исходников | +| Обработчик | worker | Тот же образ, отдельный процесс с командой `worker` | +| Метаданные и задания | postgres | PostgreSQL 17.6; postgres-data | +| Кэш сессий | redis | Redis 8.4.0; без persistence, источник истины — PostgreSQL | +| Объектное хранилище | s3 | MinIO RELEASE.2025-09-07T16-13-09Z; s3-data | +| Durable очередь | broker | RabbitMQ 4.1.8-alpine; broker-data, стабильный hostname broker | -Клиент → HAProxy → API × 2 → PostgreSQL + Redis + MinIO. +MinIO и mc закреплены digest из lab4. RabbitMQ также закреплён multi-platform digest `sha256:cd4009416f83d7e5895020cd71b0963f3b8a4dfc3de74bbb6fb3bfe5f81f01e0`. Одноразовый `migrate` применяет миграции Flyway V1–V8 до старта API и worker. `s3-init` создаёт приватный bucket и policy и допускает повторный запуск. -Это основа lab4: upload возвращает **201 ready** после S3 PUT и COMMIT метаданных. Фоновой обработки изображений нет. Из фоновых задач перенесён только существующий уборщик незавершённых загрузок, который не является worker lab5. +Переменные `.env.example` сопоставлены реализации так: -## Проверки переноса +- `API_REPLICAS`, `SESSION_TTL_SECONDS`, `IDEMPOTENCY_TTL_SECONDS`, `S3_BUCKET` и `LAB_HTTP_PORT` управляют существующим стендом. +- PostgreSQL и Redis используют `DB_*` и `REDIS_*` с password-файлами. `DATABASE_URL_FILE`, `CACHE_URL_FILE`, `APP_ENV` и переключатель `SESSION_STORE` из skeleton не управляют выбранной реализацией. +- `BROKER_HOST`, `BROKER_PORT`, `BROKER_USER`, `BROKER_PASSWORD_FILE`, `BROKER_QUEUE` задают RabbitMQ. Вместо `BROKER_URL_FILE` используются отдельные поля, чтобы URL с credentials не попадал в логи. Имя очереди по умолчанию — `attachment.process.v1`. +- `WORKER_CONCURRENCY=1` задаёт число последовательных consumer-циклов worker; допустимы 1–4. Каждый цикл держит не более одной unacked delivery. Проверенный профиль использует 1; увеличение требует отдельной оценки памяти. +- `JOB_MAX_ATTEMPTS=5` ограничивает количество захватов работы, `JOB_TIMEOUT_SECONDS=60` задаёт coroutine timeout, lease устанавливается на timeout + 15 секунд. +- `STAGING_RETENTION_SECONDS=86400` задаёт задержку удаления staging для terminal-состояний; минимум 3600. Для queued/processing исходник не удаляется по возрасту. +- Регион S3 фиксирован `us-east-1`; пределы 10 MiB и 40 млн пикселей закреплены в коде и не увеличиваются переменными окружения. -Проверки переноса выполнены 16.11.2026: +## Архитектура и граница 202 -| Проверка | Результат | -| --- | --- | -| `rtk gradlew compileKotlin compileTestKotlin --console=plain` | BUILD SUCCESSFUL; исходники приложения и всех JVM-тестов компилируются. | -| `rtk gradlew test --tests '*ImageValidatorTest' --tests '*TemporaryUploadTest' --tests '*DefaultHashingServiceTest' --console=plain` | 9 тестов, 0 failures/errors/skipped. | -| `make check` | PASS: структура skeleton, локальные ссылки и Python-синтаксис. | -| `docker compose config --quiet` | Конфигурация корректна; это не запуск контейнеров. | -| Сравнение с исходным lab4 | 176 файлов src/, gradle/, run/_template/, infra/haproxy/ и infra/s3/ совпадают побайтно; executable bit скриптов сохранён. | -| Сохранность skeleton lab5 | 32 исходных файла задания, контрактов, публичных проверок и связанных материалов не изменены. | +Клиент → HAProxy → API → PostgreSQL + приватный staging S3 → RabbitMQ → worker → final S3 + PostgreSQL. -Краткий протокол: [evidence/transfer-checks.txt](evidence/transfer-checks.txt). +API принимает весь файл, проверяет доступ к чату, filename, MIME, размер и корректность изображения. HTTP-тело читается порциями по 8192 bytes во временный файл с лимитом 10 MiB и таймаутом 15 секунд; семафор допускает одну активную загрузку на API-реплику. Для сохранения обязательных проверок lab4 перед 202 выполняется полная валидация ImageIO, включая ограничения пикселей, CRC/IEND PNG и EOI JPEG. Resize и публикация вариантов выполняются только worker. -Docker daemon сейчас недоступен. Полный набор Testcontainers-тестов и сам Compose-стенд в этой проверке не запускались. Исторические 80 успешных тестов в archive/lab4/evidence относятся к lab4, а не к новому прогону lab5. Эти проверки переноса не подтверждают асинхронный pipeline lab5. +Далее API: -`make test` по неизменённому lab.json запускает публичный профиль **lab5**. Он требует 202 queued и thumbnail, поэтому пока не используется как успешная проверка перенесённой основы. После начала выполнения lab5 проверяться будет именно этот контракт; менять его на профиль lab4 не требуется. +1. Регистрирует намерение загрузки в унаследованном журнале `attachment_uploads`. +2. Сохраняет неизменённые bytes в `staging/` в MinIO. +3. В одной DB-транзакции создаёт Attachment со статусом queued и строку `attachment_jobs`, затем удаляет завершённое намерение из журнала. +4. Публикует persistent JSON в durable очередь и ожидает publisher confirm. +5. Возвращает **202** со снимком queued, пустыми variants и error_code=null. -## Следующий этап после просмотра +Worker может успеть завершить работу до первого GET, но ответ POST остаётся снимком queued. Подтверждённая 202 работа уже имеет staging, метаданные, запись задания и подтверждённое брокером сообщение. Локальный временный файл после этого не нужен. -Реализация ещё не начата: предстоит выбрать durable broker, добавить отдельный worker, устойчивый staging и задания, состояния queued/processing/ready/failed, thumbnail, ограниченные повторы и идемпотентность обработки. Затем будут выполнены проверки уровня 3. Пункты уровней 4 и 5 рассматриваются отдельно после предыдущего этапа. +При ошибке публикации 202 не возвращается. Если строка ещё queued, она переводится в failed с `enqueue_failed`; уже захваченная или завершённая работа не откатывается. После жёсткого crash между COMMIT и publish может остаться неподтверждённая queued-запись без сообщения: автоматическое восстановление этого окна относится к outbox уровня 4 и здесь не заявляется. -## Ограничения +## Договор producer/worker и состояния -- Контракт lab5 ещё не реализован; готовность на оценку 3/4/5 не заявляется. -- Унаследованные attachment load/failover-скрипты ожидают синхронный 201. При переходе на 202 их потребуется адаптировать вместе с реализацией. -- Валидация использует ограниченный массив bytes и полный raster ImageIO; крайний случай памяти очень широких PNG остаётся ограничением исходной основы. -- PostgreSQL, MinIO и HAProxy одноузловые. Volumes обеспечивают сохранность при restart, но не заменяют backup и репликацию. -- Журнал lab4 очищает учтённые незавершённые загрузки, а не выполняет глобальную сверку bucket. Записи abandoned сохраняются для удаления поздних PUT и могут накапливаться. +`AttachmentJob` соответствует обязательным полям `contracts/jobs.schema.json`: schema_version=1, job_id, type=attachment.process.v1, attachment_id, pipeline_version=1, attempt и created_at. В сообщении нет bytes, credentials, пользовательского пути или URL. Worker получает доверенные метаданные по ID из PostgreSQL. + +Логическая job_id стабильна. Broker redelivery сохраняет envelope. Управляемый retry публикует сообщение той же job с увеличенным attempt. Worker подтверждает старую доставку после подтверждения новой публикации. Если lease ещё действует, worker отправляет тот же envelope в конец очереди, сохраняя номер попытки. Если старый envelope приходит после изменения DB-счётчика, worker публикует актуальную попытку без повторной обработки. + +- queued → processing: worker блокирует запись задания, проверяет статус и lease, увеличивает счётчик попыток и записывает случайный token. +- processing → ready: после записи original и thumbnail одна DB-транзакция публикует вариант и состояние ready, затем снимает lease. +- processing → queued: при временной ошибке с оставшимися попытками PostgreSQL сохраняет внутренний код причины и время следующего запуска. Повтор можно запустить сразу, поэтому используется время БД в момент перехода. Затем worker публикует retry и ждёт подтверждения. +- processing → failed: постоянная ошибка изображения или исчерпание попыток. В API остаётся безопасный error_code, без stack trace и секретов. +- ready/failed + повторная delivery: no-op с ack. Старый token не может опубликовать результат после нового захвата. + +Default consumer использует manual `basicGet`: следующий запрос выполняется после завершения предыдущего handler. Это явно ограничивает число delivery в работе; значение basicQos(1) не используется как единственная гарантия для polling API. Ack отправляется после устойчивого результата или подтверждённой передачи следующей попытки. При остановке/обрыве unacked delivery возвращается в очередь. Истёкший lease допускает новый захват; попытки по-прежнему ограничены DB-счётчиком. + +Это доставка **at least once** с идемпотентным эффектом, а не exactly-once. Объекты имеют детерминированные ключи, а вариант в БД — первичный ключ `(attachment_id, name)`. В этом этапе используется только pipeline_version=1. + +## Worker и изображения + +Worker запускается отдельным процессом и не принимает бизнес-HTTP-запросы. Служебные health endpoints нужны Compose. Остановка worker не переводит resize в API: новые вложения остаются queued, текстовые сообщения продолжают работать, queued-вложение разрешено прикреплять к сообщению. + +Worker проверяет размер и SHA-256 staging, один раз декодирует изображение и строит thumbnail. Для записи оригинала в S3 worker использует уже проверенные байты: + +- `originals/` — исходные bytes без перекодирования; +- `variants//v1/thumbnail.jpg` — JPEG, вписанный в 256×256 без увеличения; размеры округляются вниз, минимум 1 px. + +EXIF orientation 1–8 применяется при отрисовке. Alpha компонуется на белый. JPEG кодируется стандартным ImageIO writer; чужие metadata в новый JPEG не переносятся. Fixture 320×200 даёт thumbnail 256×160. Метаданные варианта содержат собственные MIME, размеры, размер bytes и SHA-256. + +Для queued/processing/failed скачивание content возвращает 409. Для ready доступен original и thumbnail, отсутствующий вариант получает 404. Чужой пользователь получает 404 до выдачи метаданных или содержимого. Failed-вложение нельзя добавить в новое сообщение, но уже созданное сообщение корректно воспроизводится по прежнему Idempotency-Key, даже если вложение позднее стало failed. + +При обновлении с lab4 миграция V8 переводит вложения без thumbnail в queued и создаёт задания для уже сохранённых оригиналов. Worker снимает отметку об ожидающей публикации после подтверждения RabbitMQ. Если процесс остановится раньше, он повторит публикацию того же задания. Существующий объект читается по прежнему ключу и сохраняет исходные байты. Скачивание снова доступно после готовности thumbnail. + +## Сохранность, cleanup и безопасность + +Очередь durable classic, сообщения persistent, публикации используют mandatory routing и publisher confirms. RabbitMQ сохраняет данные в отдельном volume. Producer confirm означает приём брокером, consumer ack — устойчивый эффект worker; эти подтверждения не взаимозаменяемы. + +Унаследованный журнал закрывает orphan-окно между staging PUT и записью метаданных. После часового grace period уборщик помечает просроченные намерения abandoned и удаляет связанные ключи. Записи сохраняются для повторного удаления позднего PUT. По истечении retention уборщик удаляет staging для ready и все записанные файлы для failed. У вложений из lab4 сохраняется оригинал. Отметка staging_cleaned фиксирует завершение уборки. Очистка обходит только terminal-состояния. + +Root S3 credentials доступны только MinIO/bootstrap. Прикладная policy дополнена разрешениями для staging/* и variants/*, сохраняя запрет административных операций и записи вне рабочих prefixes. S3 и console не опубликованы на host, console привязана к loopback внутри контейнера. Пароль RabbitMQ читается из secret-файла; брокер также не опубликован наружу. + +При завершении приложения явно отменяются consumer, cleanup и синхронизация revocation, затем закрываются подключения. Это предотвращает работу старых фоновых циклов после закрытия DI/БД и их вмешательство в другие экземпляры тестового приложения. + +## Проверки уровня 3 + +Стендовые проверки из таблицы выполнены **17 ноября 2026 года** на порту 8085. После исправлений **18 ноября** отдельно собран стенд на порту 8086, где повторно прошли публичный smoke и сценарий восстановления worker. Полный JVM-набор также запущен повторно после правок. + +| Проверка | Команда / сценарий | Результат и доказательство | +| --- | --- | --- | +| Структура и контракт | `make check` | PASS; [вывод](evidence/check.txt) | +| Конфигурация и запуск Compose | `docker compose config --quiet`, `docker compose up -d --build --wait` | Все восемь рабочих контейнеров healthy; [конфигурация и состояние стенда](evidence/compose.txt) | +| JVM-регрессии и новый pipeline | `rtk gradlew compileKotlin compileTestKotlin test --console=plain` | **94 теста, 0 ошибок и пропусков**, включая обновление с lab4, очистку частичного результата и обработку следующей задачи при действующем lease; [сводка по JUnit XML](evidence/gradle-tests.txt) | +| Публичный сценарий lab5 | `make test BASE_URL=http://localhost:8085` | PASS: upload 202, ready, оригинал и настоящий JPEG thumbnail; [вывод](evidence/smoke.txt) | +| Worker остановлен, API и broker перезапущены | `python3 scripts/attachment-worker-recovery.py` | 202 queued → сообщение создано → content 409 → restart обеих API и broker с volumes → запуск worker → та же запись ready, неизменённый оригинал и JPEG 256×160; [вывод с attachment_id](evidence/worker-recovery.txt) | +| Durable доставка и повтор одной job | `RabbitMqJobBrokerServiceTest`, `AttachmentPipelineTest` в JVM-наборе | Реальный restart контейнера RabbitMQ сохраняет подтверждённое сообщение; nack возвращает тот же envelope; повтор завершённой job сохраняет прежний результат | +| Ограниченные попытки и failed | `AttachmentPipelineTest` в JVM-наборе | Busy lease, постоянная ошибка изображения и исчерпание временных попыток; failed запрещает content и новое сообщение, прежний Idempotency-Key воспроизводит созданное сообщение | +| Правила thumbnail | `ImageProcessingServiceTest` в JVM-наборе | Шесть тестов: JPEG/PNG EXIF orientation, alpha на белом, размеры с округлением вниз, отсутствие увеличения и удаление orientation из результата | +| Переключение API-реплики | `python3 scripts/attachment-failover.py` | Загрузившая реплика остановлена, другая отдаёт оригинал с тем же SHA-256; анонимный S3 GET — 403, чужой пользователь — 404; [вывод](evidence/attachment-failover.txt) | +| Привилегии S3 и повторный bootstrap | `scripts/check-s3-permissions.sh` | Запрещены административные операции, изменение policy и запись вне рабочих prefixes; console недоступна из общей сети; [вывод](evidence/s3-permissions.txt) | +| Полный restart с сохранением данных | `make test-persistence BASE_URL=http://localhost:8085 ACTION_SCRIPT=scripts/restart.sh` | PASS: подтверждённые данные и сессия сохранены; StartedAt изменился у обеих API, worker, broker, LB, PostgreSQL, Redis и MinIO; [вывод и времена до/после](evidence/persistence.txt) | + +Для сценариев со стендом установлены `LAB_HTTP_PORT=8085` и `BASE_URL=http://localhost:8085`, как в инструкции запуска выше. Полный JVM-прогон также проверяет сохранённые возможности lab1–lab4: авторизацию, чаты, конкурентную запись, идемпотентность сообщений, межрепличный отзыв сессий, миграции и отказы зависимостей. Исторические материалы lab4 находятся в `archive/lab4/`; они не являются доказательствами lab5. + +## Ограничения и границы следующего этапа + +- Outbox/автоматическое восстановление разрыва COMMIT/publish, backoff с jitter, DLQ/operator replay, crop и optimized WebP относятся к уровню 4 и не реализованы. +- Валидация использует массив ≤10 MiB и полный raster ImageIO; это не полностью потоковый декодер. Очень широкие изображения остаются краевым случаем памяти исходной реализации. Worker ограничен 1 GiB контейнерной памяти; при JVM OOM включён аварийный выход с последующим restart, а число попыток сохраняется в БД. +- Coroutine timeout кооперативный и не гарантирует принудительное прерывание блокирующего декодера в середине вызова. Lease/token предотвращает публикацию устаревшим обработчиком, но не заменяет жёсткое ограничение CPU-времени отдельного декодера. +- PostgreSQL, MinIO, RabbitMQ и HAProxy одноузловые. Volumes сохраняют данные при restart; отказоустойчивость узла и backup не заявляются. +- После crash незавершённая processing-работа может ждать истечения lease. Секундного SLA на восстановление при любом сбое не заявляется. +- Сравнение нескольких pipeline versions и защита от перезаписи результата старой версией относятся к исследовательскому треку; сейчас применяется версия 1. + +## Источники + +- Задание, contracts/README.md, contracts/attachment-states.md и contracts/jobs.schema.json этого репозитория. +- [RabbitMQ reliability](https://www.rabbitmq.com/docs/reliability). +- [Publisher confirms и consumer acknowledgements](https://www.rabbitmq.com/docs/confirms). +- [RabbitMQ Java Client API](https://www.rabbitmq.com/client-libraries/java-api-guide). +- [AWS SDK for Kotlin](https://github.com/awslabs/aws-sdk-kotlin). +- [metadata-extractor](https://github.com/drewnoakes/metadata-extractor). diff --git a/build.gradle.kts b/build.gradle.kts index 97d10253..2f3348ba 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -47,6 +47,8 @@ dependencies { implementation(libs.lettuce) implementation(awssdk.services.s3) + implementation(libs.rabbitmq) + implementation(libs.metadata.extractor) implementation(libs.bouncycastle) diff --git a/compose.yaml b/compose.yaml index c8ce9309..6a3c3a67 100644 --- a/compose.yaml +++ b/compose.yaml @@ -55,6 +55,30 @@ services: memory: 128m restart: unless-stopped + broker: + # RabbitMQ 4.1.8-alpine, pinned multi-platform image. + image: rabbitmq@sha256:cd4009416f83d7e5895020cd71b0963f3b8a4dfc3de74bbb6fb3bfe5f81f01e0 + hostname: broker + entrypoint: ["/bin/sh", "/bootstrap.sh"] + secrets: + - broker_password + volumes: + - broker-data:/var/lib/rabbitmq + - ./infra/rabbitmq/bootstrap.sh:/bootstrap.sh:ro + - ./infra/rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro + healthcheck: + test: ["CMD", "rabbitmq-diagnostics", "-q", "check_running"] + interval: 5s + timeout: 5s + retries: 20 + start_period: 20s + deploy: + resources: + limits: + cpus: "1.0" + memory: 512m + restart: unless-stopped + s3: # MinIO RELEASE.2025-09-07T16-13-09Z, pinned multi-platform image. image: quay.io/minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e @@ -141,6 +165,8 @@ services: condition: service_healthy s3-init: condition: service_completed_successfully + broker: + condition: service_healthy environment: APP_HOST: "0.0.0.0" APP_PORT: "8080" @@ -155,17 +181,24 @@ services: REDIS_PASSWORD_FILE: /run/secrets/redis_password SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} SESSION_CACHE_SINGLE_FLIGHT: ${SESSION_CACHE_SINGLE_FLIGHT:-true} + STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} SHUTDOWN_DRAIN_DELAY_MS: "3000" S3_ENDPOINT: http://s3:9000 S3_BUCKET: ${S3_BUCKET:-messenger-attachments} S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_access_key_id S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret_access_key + BROKER_HOST: broker + BROKER_PORT: "5672" + BROKER_USER: messenger + BROKER_PASSWORD_FILE: /run/secrets/broker_password + BROKER_QUEUE: ${BROKER_QUEUE:-attachment.process.v1} secrets: - db_password - redis_password - s3_api_access_key_id - s3_api_secret_access_key + - broker_password # 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. tmpfs: @@ -173,6 +206,56 @@ services: stop_grace_period: 10s restart: unless-stopped + worker: + image: mtusi-messenger:lab5 + command: ["worker"] + depends_on: + migrate: + condition: service_completed_successfully + postgres: + condition: service_healthy + s3-init: + condition: service_completed_successfully + broker: + 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 + DB_MIGRATE_ON_START: "false" + S3_ENDPOINT: http://s3:9000 + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret_access_key + BROKER_HOST: broker + BROKER_PORT: "5672" + BROKER_USER: messenger + BROKER_PASSWORD_FILE: /run/secrets/broker_password + BROKER_QUEUE: ${BROKER_QUEUE:-attachment.process.v1} + JAVA_TOOL_OPTIONS: "-XX:+ExitOnOutOfMemoryError" + WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-1} + JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} + JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60} + STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} + secrets: + - db_password + - s3_api_access_key_id + - s3_api_secret_access_key + - broker_password + tmpfs: + - /tmp:size=32m,mode=1777 + stop_grace_period: 10s + deploy: + resources: + limits: + cpus: "1.0" + memory: 1g + restart: unless-stopped + lb: image: haproxy:3.2.4-alpine volumes: @@ -200,6 +283,8 @@ secrets: file: ./secrets/db_password redis_password: file: ./secrets/redis_password + broker_password: + file: ./secrets/broker_password s3_access_key_id: file: ./secrets/s3_access_key_id @@ -214,3 +299,4 @@ secrets: volumes: postgres-data: s3-data: + broker-data: diff --git a/evidence/attachment-failover.txt b/evidence/attachment-failover.txt new file mode 100644 index 00000000..96df8612 --- /dev/null +++ b/evidence/attachment-failover.txt @@ -0,0 +1,6 @@ +Upload: 202 queued, then ready; instance=7a70d3b286da +Anonymous S3 GET of known object: 403 +Outsider metadata/content: 404 +Download: 200; instance=f000ea12f8bd +SHA-256: 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 +PASS: uploading replica stopped; original served by another replica; stopped replica restored diff --git a/evidence/check.txt b/evidence/check.txt new file mode 100644 index 00000000..060802d0 --- /dev/null +++ b/evidence/check.txt @@ -0,0 +1,4 @@ +$ make check +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +Exit code: 0 diff --git a/evidence/compose.txt b/evidence/compose.txt new file mode 100644 index 00000000..c89ebc6b --- /dev/null +++ b/evidence/compose.txt @@ -0,0 +1,13 @@ +$ docker compose config --quiet +Exit code: 0 + +$ docker compose ps +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi-messenger-lab5-api-1 mtusi-messenger:lab5 "java -XX:MaxRAMPerc…" api 8 minutes ago Up 2 minutes (healthy) 8080/tcp +mtusi-messenger-lab5-api-2 mtusi-messenger:lab5 "java -XX:MaxRAMPerc…" api 8 minutes ago Up 2 minutes (healthy) 8080/tcp +mtusi-messenger-lab5-broker-1 rabbitmq@sha256:cd4009416f83d7e5895020cd71b0963f3b8a4dfc3de74bbb6fb3bfe5f81f01e0 "/bin/sh /bootstrap.…" broker 19 hours ago Up 2 minutes (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp +mtusi-messenger-lab5-lb-1 haproxy:3.2.4-alpine "docker-entrypoint.s…" lb 19 hours ago Up About a minute (healthy) 127.0.0.1:8085->8080/tcp +mtusi-messenger-lab5-postgres-1 postgres:17.6-alpine3.22 "docker-entrypoint.s…" postgres 19 hours ago Up 2 minutes (healthy) 5432/tcp +mtusi-messenger-lab5-redis-1 redis:8.4.0-alpine "docker-entrypoint.s…" redis 19 hours ago Up 2 minutes (healthy) 6379/tcp +mtusi-messenger-lab5-s3-1 quay.io/minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e "/usr/bin/docker-ent…" s3 19 hours ago Up 2 minutes (healthy) 9000/tcp +mtusi-messenger-lab5-worker-1 mtusi-messenger:lab5 "java -XX:MaxRAMPerc…" worker 8 minutes ago Up 2 minutes (healthy) 8080/tcp diff --git a/evidence/gradle-tests.txt b/evidence/gradle-tests.txt new file mode 100644 index 00000000..56fa37a2 --- /dev/null +++ b/evidence/gradle-tests.txt @@ -0,0 +1,45 @@ +Command: rtk gradlew compileKotlin compileTestKotlin test --console=plain (JDK 21) +BUILD SUCCESSFUL +Tests: 94; failures: 0; errors: 0; skipped: 0 +com.mtusi.messenger.main.infrastructure.controllers.attachment.AttachmentControllerTest: tests=3 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.authentication.AuthenticationControllerTest: tests=9 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.chat.ChatControllerTest: tests=6 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.health.HealthControllerTest: tests=2 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.message.MessageConcurrencyTest: tests=1 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.message.MessageControllerTest: tests=7 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.message.MessageIdempotencyTest: tests=7 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.user.UserControllerTest: tests=11 failures=0 +com.mtusi.messenger.main.infrastructure.dto.AttachmentJobContractTest: tests=1 failures=0 + job envelope requires every field mandated by the public schema() +com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: tests=3 failures=0 +com.mtusi.messenger.main.infrastructure.repositories.chat.ExposedChatRepositoryTest: tests=2 failures=0 +com.mtusi.messenger.main.infrastructure.repositories.message.MessageAttachmentsQueryTest: tests=1 failures=0 +com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentCleanupTest: tests=2 failures=0 + expired failed attachments lose partially published files() + orphan cleanup respects grace and fences late publishers() +com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentPipelineTest: tests=4 failures=0 + attempts are bounded and failed attachments reject content and message links() + busy redelivery lets the next attachment finish before lease expiration() + worker distinguishes permanent image errors from bounded transient failures() + stopped worker leaves a durable queued attachment and redelivery preserves its result() +com.mtusi.messenger.main.infrastructure.services.attachment.ImageProcessingServiceTest: tests=6 failures=0 + PNG EXIF orientation is applied before resizing() + transparent and semitransparent pixels are composited on white() + invalid images and MIME mismatches are rejected before publishing a thumbnail() + missing or unknown EXIF orientation preserves the normal image() + all JPEG EXIF orientations rotate or mirror pixels and strip orientation from the thumbnail() + thumbnail fits the box floors dimensions and never enlarges the source() +com.mtusi.messenger.main.infrastructure.services.attachment.ImageValidatorTest: tests=3 failures=0 +com.mtusi.messenger.main.infrastructure.services.attachment.LegacyAttachmentMigrationTest: tests=1 failures=0 + migration schedules a thumbnail for an existing lab4 original() +com.mtusi.messenger.main.infrastructure.services.attachment.jobs.RabbitMqJobBrokerServiceTest: tests=1 failures=0 + confirmed job survives broker restart and negative acknowledgement preserves its identity() +com.mtusi.messenger.main.infrastructure.services.authentication.CrossReplicaRevocationTest: tests=2 failures=0 +com.mtusi.messenger.main.infrastructure.services.authentication.SessionLookupSingleFlightTest: tests=4 failures=0 +com.mtusi.messenger.main.infrastructure.services.hashing.DefaultHashingServiceTest: tests=4 failures=0 +com.mtusi.messenger.main.plugins.CorsTest: tests=2 failures=0 +com.mtusi.messenger.main.plugins.DatabaseMigrationTest: tests=1 failures=0 +com.mtusi.messenger.main.plugins.DependencyFailureTest: tests=5 failures=0 +com.mtusi.messenger.main.plugins.InstanceIdTest: tests=1 failures=0 +com.mtusi.messenger.main.plugins.SessionCacheTest: tests=3 failures=0 +com.mtusi.messenger.main.utils.TemporaryUploadTest: tests=2 failures=0 diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..a380f1d9 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,23 @@ +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8085" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + + +Before restart: +/mtusi-messenger-lab5-api-1 2026-11-17T11:48:25.6974278Z +/mtusi-messenger-lab5-api-2 2026-11-17T11:48:25.799164633Z +/mtusi-messenger-lab5-broker-1 2026-11-17T11:48:15.954373295Z +/mtusi-messenger-lab5-lb-1 2026-11-17T11:48:28.901775676Z +/mtusi-messenger-lab5-postgres-1 2026-11-17T11:48:15.943166503Z +/mtusi-messenger-lab5-redis-1 2026-11-17T11:48:15.95361167Z +/mtusi-messenger-lab5-s3-1 2026-11-17T11:48:15.94897392Z +/mtusi-messenger-lab5-worker-1 2026-11-17T11:48:25.696508175Z +After restart: +/mtusi-messenger-lab5-api-1 2026-11-17T11:52:14.708903753Z +/mtusi-messenger-lab5-api-2 2026-11-17T11:52:14.820903586Z +/mtusi-messenger-lab5-broker-1 2026-11-17T11:52:04.990389887Z +/mtusi-messenger-lab5-lb-1 2026-11-17T11:52:17.919454796Z +/mtusi-messenger-lab5-postgres-1 2026-11-17T11:52:04.990403929Z +/mtusi-messenger-lab5-redis-1 2026-11-17T11:52:04.984496471Z +/mtusi-messenger-lab5-s3-1 2026-11-17T11:52:04.992642512Z +/mtusi-messenger-lab5-worker-1 2026-11-17T11:52:14.708902794Z diff --git a/evidence/s3-permissions.txt b/evidence/s3-permissions.txt new file mode 100644 index 00000000..59841603 --- /dev/null +++ b/evidence/s3-permissions.txt @@ -0,0 +1,10 @@ + Container mtusi-messenger-lab5-s3-1 Running +Bucket created successfully `local/messenger-attachments`. +Access permission for `local/messenger-attachments` is set to `private` + Container mtusi-messenger-lab5-s3-1 Running +Bucket created successfully `local/messenger-attachments`. +Access permission for `local/messenger-attachments` is set to `private` + Container mtusi-messenger-lab5-s3-1 Running +PASS: app cannot administer MinIO, create buckets, change policy or write outside originals/ +wget: can't connect to remote host (172.26.0.2): Connection refused +PASS: console unavailable from shared network diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..2448312f --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8085" +PASS: lab5 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/worker-recovery.txt b/evidence/worker-recovery.txt new file mode 100644 index 00000000..bcac0aeb --- /dev/null +++ b/evidence/worker-recovery.txt @@ -0,0 +1,3 @@ +PASS: worker stopped; upload 202 queued; message accepted; content 409; attachment_id=0305bfe5-e30e-47cc-b81e-4f2c578273a6 +PASS: both APIs and broker restarted with existing volumes; same attachment remains queued; attachment_id=0305bfe5-e30e-47cc-b81e-4f2c578273a6 +PASS: same accepted job reached ready; original unchanged; thumbnail JPEG 256x160 with matching checksum; attachment_id=0305bfe5-e30e-47cc-b81e-4f2c578273a6 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ec020b4d..b51abcf0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,6 +16,9 @@ lettuce = "6.7.1.RELEASE" bouncycastle = "1.81" test-containers = "1.21.0" +rabbitmq = "5.26.0" +metadata-extractor = "2.19.0" + [plugins] kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } @@ -52,3 +55,6 @@ bouncycastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "boun testContainers = { module = "org.testcontainers:testcontainers", version.ref = "test-containers" } testContainers-postgresql = { module = "org.testcontainers:postgresql", version.ref = "test-containers" } + +rabbitmq = { module = "com.rabbitmq:amqp-client", version.ref = "rabbitmq" } +metadata-extractor = { module = "com.drewnoakes:metadata-extractor", version.ref = "metadata-extractor" } diff --git a/infra/rabbitmq/bootstrap.sh b/infra/rabbitmq/bootstrap.sh new file mode 100644 index 00000000..2796c6c6 --- /dev/null +++ b/infra/rabbitmq/bootstrap.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -eu +# The password is read only inside the container and is never written to logs. +export RABBITMQ_DEFAULT_USER=messenger +export RABBITMQ_DEFAULT_PASS="$(cat /run/secrets/broker_password)" +exec docker-entrypoint.sh rabbitmq-server diff --git a/infra/rabbitmq/rabbitmq.conf b/infra/rabbitmq/rabbitmq.conf new file mode 100644 index 00000000..0c013283 --- /dev/null +++ b/infra/rabbitmq/rabbitmq.conf @@ -0,0 +1,3 @@ +# Job envelopes contain identifiers only, never image bytes. +max_message_size = 16384 +consumer_timeout = 300000 diff --git a/infra/s3/bootstrap.sh b/infra/s3/bootstrap.sh index 5ca311de..45dd2296 100644 --- a/infra/s3/bootstrap.sh +++ b/infra/s3/bootstrap.sh @@ -12,7 +12,7 @@ cat > /tmp/attachment-policy.json < "secrets/$name" echo "created secrets/$name" diff --git a/scripts/restart.sh b/scripts/restart.sh index 88dbc5d9..3b64ec43 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -1,7 +1,7 @@ #!/bin/sh -# Restarts the balancer, every API replica, PostgreSQL, Redis and S3, keeping the named volume (no `down -v`, no re-seed). +# Restarts the balancer, every API replica, PostgreSQL, Redis, S3 and broker, keeping the named volume (no `down -v`, no re-seed). set -eu cd "$(dirname "$0")/.." -docker compose stop lb api redis postgres s3 +docker compose stop lb api worker redis postgres s3 broker docker compose up -d --wait diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt index d2214cd9..97e9e9a9 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/App.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -9,21 +9,35 @@ 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.configureJobBroker 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.configureSessionRevocationSync 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) { + + val command = args.firstOrNull() + // `migrate` applies database migrations and exits; the API replicas start with migrations already in place. - if (args.firstOrNull() == "migrate") { + if (command == "migrate") { return migrateDatabase() } + + if (command == "worker") { + val workerArgs = arrayOf( + "-config=worker.conf", + *args.drop(1).toTypedArray() + ) + + return EngineMain.main(workerArgs) + } + EngineMain.main(args) } @@ -31,6 +45,7 @@ fun Application.module() { configureKoin() configureDatabase() + configureJobBroker() configureRedis() configureGracefulShutdown() configureSessionRevocationSync() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/broker/BrokerConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/broker/BrokerConfig.kt new file mode 100644 index 00000000..e5c1acc8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/broker/BrokerConfig.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.config.broker + +interface BrokerConfig { + + val host: String + val port: Int + val queue: String + + val username: String + val password: String? + val passwordFile: String? + + val connectionTimeoutMs: Int + val operationTimeoutMs: Int + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/broker/DefaultBrokerConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/broker/DefaultBrokerConfig.kt new file mode 100644 index 00000000..22b157d7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/broker/DefaultBrokerConfig.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.config.broker + +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 DefaultBrokerConfig( + @Provided app: Application +) : BrokerConfig by ( + app.environment.config + .propertyOrNull("broker") + ?.getAs() + ?: SerialBrokerConfig() +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/broker/SerialBrokerConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/broker/SerialBrokerConfig.kt new file mode 100644 index 00000000..3d66b573 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/broker/SerialBrokerConfig.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.config.broker + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialBrokerConfig( + override val host: String = "localhost", + override val port: Int = 5672, + override val username: String = "messenger", + override val password: String? = null, + override val passwordFile: String? = null, + override val queue: String = "attachment.process.v1", + override val connectionTimeoutMs: Int = 3000, + override val operationTimeoutMs: Int = 8000 +) : BrokerConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/worker/DefaultWorkerConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/worker/DefaultWorkerConfig.kt new file mode 100644 index 00000000..f32fd270 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/worker/DefaultWorkerConfig.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.config.worker + +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 DefaultWorkerConfig( + @Provided app: Application +) : WorkerConfig by ( + app.environment.config + .propertyOrNull("worker") + ?.getAs() + ?: SerialWorkerConfig() +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/worker/SerialWorkerConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/worker/SerialWorkerConfig.kt new file mode 100644 index 00000000..7aad65f3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/worker/SerialWorkerConfig.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.config.worker + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialWorkerConfig( + override val concurrency: Int = 1, + override val maxAttempts: Int = 5, + override val timeoutSeconds: Long = 60, + override val stagingRetentionSeconds: Long = 86400 +) : WorkerConfig { + + init { + require(concurrency in 1..4) + require(maxAttempts in 1..100) + require(timeoutSeconds in 1..3600) + require(stagingRetentionSeconds >= 3600) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/worker/WorkerConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/worker/WorkerConfig.kt new file mode 100644 index 00000000..f5c31ad8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/worker/WorkerConfig.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.config.worker + +interface WorkerConfig { + + val concurrency: Int + val maxAttempts: Int + val timeoutSeconds: Long + val stagingRetentionSeconds: Long + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt index 6ae1f5a8..556094e2 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt @@ -7,19 +7,28 @@ 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.utils.requireUuid -import io.ktor.http.* -import io.ktor.server.auth.authenticate -import io.ktor.server.request.* -import io.ktor.server.response.* -import io.ktor.server.routing.* -import io.ktor.utils.io.readAvailable -import kotlinx.coroutines.withTimeoutOrNull -import org.koin.ktor.ext.inject import com.mtusi.messenger.main.utils.withTemporaryUpload +import io.ktor.http.BadContentTypeFormatException +import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.request.contentLength +import io.ktor.server.request.contentType +import io.ktor.server.request.receiveChannel +import io.ktor.server.response.header +import io.ktor.server.response.respond +import io.ktor.server.response.respondBytes +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.utils.io.readAvailable import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.withPermit +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeoutOrNull +import org.koin.ktor.ext.inject import kotlin.time.Duration.Companion.milliseconds fun Routing.configureAttachmentRouting() { @@ -34,20 +43,32 @@ fun Routing.configureAttachmentRouting() { val actor = call.currentUser val chatId = requireUuid(call.parameters["chat_id"], "chat_id") - service.requireMember(chatId, actor.id) + service.requireMember( + chatId = chatId, + actorId = actor.id + ) val filename = call.request.queryParameters["filename"] ?: throw ApiException(ErrorCode.INVALID_REQUEST, "filename is required") - if (filename.isBlank() || filename.codePointCount(0, filename.length) !in 1..255 || filename.any { it == '/' || it == '\\' || it.isISOControl() }) { + if ( + filename.isBlank() || + filename.codePointCount(0, filename.length) !in 1..255 || + filename.any { it == '/' || it == '\\' || it.isISOControl() } + ) { throw ApiException(ErrorCode.INVALID_REQUEST, "invalid filename") } val type = try { - call.request.contentType().withoutParameters().toString().lowercase() + call.request + .contentType() + .withoutParameters() + .toString() + .lowercase() } catch (_: BadContentTypeFormatException) { throw ApiException(ErrorCode.INVALID_REQUEST, "invalid Content-Type") } + if (type !in setOf("image/png", "image/jpeg")) { throw ApiException(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "expected PNG or JPEG") } @@ -63,44 +84,89 @@ fun Routing.configureAttachmentRouting() { val input = call.receiveChannel() val buffer = ByteArray(8192) var size = 0L + file.outputStream().use { output -> while (true) { val count = input.readAvailable(buffer) - if (count == -1) break + + if (count == -1) { + break + } + size += count + if (size > MAX_ATTACHMENT_BYTES) { throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") } + output.write(buffer, 0, count) } } } } ?: throw ApiException(ErrorCode.INVALID_REQUEST, "upload timed out") - val attachment = service.upload(chatId, actor.id, filename, type, file) - call.respond(HttpStatusCode.Created, attachment) + val attachment = service.upload( + chatId = chatId, + actorId = actor.id, + filename = filename, + contentType = type, + file = file + ) + + call.respond(HttpStatusCode.Accepted, attachment) } } } get("/api/v1/attachments/{id}") { - call.respond(service.find(requireUuid(call.parameters["id"], "id"), call.currentUser.id)) + + val id = requireUuid(call.parameters["id"], "id") + val attachment = service.find( + id = id, + actorId = call.currentUser.id + ) + + call.respond(attachment) } get("/api/v1/attachments/{id}/content") { - val attachment = service.find(requireUuid(call.parameters["id"], "id"), call.currentUser.id) + val id = requireUuid(call.parameters["id"], "id") + val attachment = service.find( + id = id, + actorId = call.currentUser.id + ) - if ((call.request.queryParameters["variant"] ?: "original") != "original") { - throw ApiException(ErrorCode.NOT_FOUND, "variant not found") + val variant = call.request.queryParameters["variant"] ?: "original" + + if (attachment.status != "ready") { + throw ApiException(ErrorCode.CONFLICT, "attachment is not ready") + } + + val contentType = if (variant == "original") { + attachment.contentType + } else { + attachment.variants + .firstOrNull { it.name == variant } + ?.contentType + ?: throw ApiException(ErrorCode.NOT_FOUND, "variant not found") } call.response.header(HttpHeaders.CacheControl, "private, no-store") call.response.header("X-Content-Type-Options", "nosniff") - call.respondBytes(service.content(attachment), ContentType.parse(attachment.contentType)) + val bytes = service.content( + attachment = attachment, + variant = variant + ) + + call.respondBytes( + bytes = bytes, + contentType = ContentType.parse(contentType) + ) } } + } 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 d17fd116..5ebfb74e 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 @@ -2,13 +2,11 @@ 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.services.attachment.jobs.JobBrokerService +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService import com.mtusi.messenger.main.infrastructure.types.ErrorCode import com.mtusi.messenger.main.plugins.isDraining import com.mtusi.messenger.main.plugins.suspendedTransaction -import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import org.koin.ktor.ext.inject import io.ktor.http.HttpStatusCode import io.ktor.server.application.log import io.ktor.server.response.respond @@ -16,8 +14,11 @@ import io.ktor.server.routing.Routing import io.ktor.server.routing.get import io.ktor.server.routing.route import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.withContext +import org.koin.ktor.ext.inject fun Routing.configureHealthRouting() = route("/health") { @@ -25,9 +26,10 @@ fun Routing.configureHealthRouting() = route("/health") { call.respond(HttpStatusCode.OK, HealthResponse()) } - val storage by inject() + val storage by inject() + val broker by inject() - // PostgreSQL and S3 are dependencies of the synchronous path; Redis has a DB fallback. + // Readiness checks PostgreSQL, S3 and RabbitMQ. Redis has a DB fallback. get("ready") { // SIGTERM received: leave the balancer's rotation while requests in flight are still being finished. @@ -40,6 +42,7 @@ fun Routing.configureHealthRouting() = route("/health") { suspendedTransaction { exec("SELECT 1") } + true } catch (cause: Exception) { @@ -48,6 +51,7 @@ fun Routing.configureHealthRouting() = route("/health") { } call.application.log.warn("Readiness check failed: ${cause.message}") + false } @@ -56,11 +60,20 @@ fun Routing.configureHealthRouting() = route("/health") { return@get } - if (!withContext(Dispatchers.IO) { storage.ready() }) { + val isStorageReachable = withContext(Dispatchers.IO) { + storage.ready() + } + + if (!isStorageReachable) { call.respondError(ErrorCode.UNAVAILABLE, "object storage unavailable") return@get } + if (!broker.ready()) { + call.respondError(ErrorCode.UNAVAILABLE, "job broker unavailable") + return@get + } + call.respond(HttpStatusCode.OK, HealthResponse()) } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt index e65ac4cd..f2aeccf6 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt @@ -16,6 +16,6 @@ data class Attachment( val createdAt: Instant, val updatedAt: Instant = createdAt, val status: String = "ready", - val variants: List = emptyList(), + val variants: List = emptyList(), val errorCode: String? = null, ) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJob.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJob.kt new file mode 100644 index 00000000..08a39ede --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJob.kt @@ -0,0 +1,25 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Required +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class AttachmentJob( + val jobId: SerialUUID, + val attachmentId: SerialUUID, + val createdAt: Instant, + + @Required + val schemaVersion: Int = 1, + + @Required + val type: String = "attachment.process.v1", + + @Required + val pipelineVersion: Int = 1, + + @Required + val attempt: Int = 1, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentVariant.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentVariant.kt new file mode 100644 index 00000000..7b65ef86 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentVariant.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import kotlinx.serialization.Serializable + +@Serializable +data class AttachmentVariant( + val name: String, + val contentType: String, + val sizeBytes: Long, + val width: Int, + val height: Int, + val sha256: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/ProcessedImage.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/ProcessedImage.kt new file mode 100644 index 00000000..dcaddab8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/ProcessedImage.kt @@ -0,0 +1,39 @@ +package com.mtusi.messenger.main.infrastructure.dto + +data class ProcessedImage( + val bytes: ByteArray, + val width: Int, + val height: Int, + val contentType: String = "image/jpeg" +) { + + override fun equals( + other: Any? + ): Boolean { + + if (this === other) { + return true + } + + if (javaClass != other?.javaClass) { + return false + } + + other as ProcessedImage + + return width == other.width && + height == other.height && + bytes.contentEquals(other.bytes) && + contentType == other.contentType + } + + override fun hashCode(): Int { + var result = width + result = 31 * result + height + result = 31 * result + bytes.contentHashCode() + result = 31 * result + contentType.hashCode() + + return result + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt new file mode 100644 index 00000000..2e64b26d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt @@ -0,0 +1,24 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.core.java.javaUUID +import org.jetbrains.exposed.v1.datetime.timestamp + +object AttachmentJobModel : UUIDTable("attachment_jobs") { + + val attachmentId = reference( + name = "attachment_id", + foreign = AttachmentModel + ).uniqueIndex() + + val pipelineVersion = integer("pipeline_version").default(1) + val attempts = integer("attempts").default(0) + val leaseToken = javaUUID("lease_token").nullable() + val leaseUntil = timestamp("lease_until").nullable() + val lastErrorCode = varchar("last_error_code", 64).nullable() + val nextAttemptAt = timestamp("next_attempt_at").nullable() + val createdAt = timestamp("created_at") + val legacyOriginal = bool("legacy_original").default(false) + val legacyDispatchPending = bool("legacy_dispatch_pending").default(false) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt index 2a26edd0..027f3b7e 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt @@ -13,4 +13,8 @@ object AttachmentModel : UUIDTable("attachments") { val sizeBytes = long("size_bytes") val sha256 = varchar("sha256", 64) val createdAt = timestamp("created_at") + val updatedAt = timestamp("updated_at") + val status = varchar("status", 16).default("ready") + val stagingCleaned = bool("staging_cleaned").default(false) + val errorCode = varchar("error_code", 64).nullable() } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentVariantModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentVariantModel.kt new file mode 100644 index 00000000..0bcc0479 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentVariantModel.kt @@ -0,0 +1,23 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.Table + +object AttachmentVariantModel : Table("attachment_variants") { + + val attachmentId = reference( + name = "attachment_id", + foreign = AttachmentModel, + onDelete = ReferenceOption.CASCADE + ) + + val name = varchar("name", 16) + val contentType = varchar("content_type", 32) + val sizeBytes = long("size_bytes") + val width = integer("width") + val height = integer("height") + val sha256 = varchar("sha256", 64) + + override val primaryKey = PrimaryKey(attachmentId, name) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt index 1487d761..fd4086db 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt @@ -1,7 +1,9 @@ package com.mtusi.messenger.main.infrastructure.repositories.attachment import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant import java.util.UUID +import kotlin.time.Instant interface AttachmentRepository { @@ -9,6 +11,35 @@ interface AttachmentRepository { fun findById(id: UUID): Attachment? + fun findStatusesInChat( + chatId: UUID, + ids: List, + lockForUpdate: Boolean = false + ): List + + fun setState( + id: UUID, + status: String, + errorCode: String? = null + ) + + fun markEnqueueFailed( + id: UUID + ) + + fun markStagingCleaned( + id: UUID + ) + + fun findExpiredStorage( + before: Instant + ): List + + fun publish( + id: UUID, + variant: AttachmentVariant + ) + fun attach( messageId: UUID, ids: List @@ -18,3 +49,9 @@ interface AttachmentRepository { fun idsForMessages(messageIds: List): Map> } + +data class ExpiredAttachmentStorage( + val id: UUID, + val failed: Boolean, + val legacyOriginal: Boolean +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt index 024c6330..f1144526 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt @@ -1,21 +1,34 @@ package com.mtusi.messenger.main.infrastructure.repositories.attachment import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobModel import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentVariantModel import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel -import org.jetbrains.exposed.v1.jdbc.insert -import org.jetbrains.exposed.v1.jdbc.selectAll +import com.mtusi.messenger.main.utils.nowMicros import org.jetbrains.exposed.v1.core.ResultRow -import org.jetbrains.exposed.v1.core.inList -import org.jetbrains.exposed.v1.core.eq import org.jetbrains.exposed.v1.core.SortOrder +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.core.less +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.select +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.Clock +import kotlin.time.Instant @Single class ExposedAttachmentRepository : AttachmentRepository { - override fun create(attachment: Attachment) { + override fun create( + attachment: Attachment + ) { AttachmentModel.insert { it[id] = attachment.id it[chatId] = attachment.chatId @@ -25,17 +38,129 @@ class ExposedAttachmentRepository : AttachmentRepository { it[sizeBytes] = attachment.sizeBytes it[sha256] = attachment.sha256 it[createdAt] = attachment.createdAt + it[updatedAt] = attachment.updatedAt + it[status] = attachment.status + it[errorCode] = attachment.errorCode } } override fun findById( id: UUID ): Attachment? { - return AttachmentModel + + val rows = AttachmentModel + .leftJoin(AttachmentVariantModel) .selectAll() .where(AttachmentModel.id eq id) - .firstOrNull() - ?.toAttachment() + .orderBy(AttachmentVariantModel.name to SortOrder.ASC) + .toList() + + val attachment = rows.firstOrNull() + ?: return null + + val variants = rows.mapNotNull { it.toAttachmentVariantOrNull() } + + return attachment.toAttachment(variants) + } + + override fun findStatusesInChat( + chatId: UUID, + ids: List, + lockForUpdate: Boolean + ): List { + + if (ids.isEmpty()) { + return emptyList() + } + + val query = AttachmentModel + .select(AttachmentModel.status) + .where((AttachmentModel.chatId eq chatId) and (AttachmentModel.id inList ids)) + + if (lockForUpdate) { + // Requests with overlapping attachments take locks in the same order. + query + .orderBy(AttachmentModel.id to SortOrder.ASC) + .forUpdate() + } + + return query.map { it[AttachmentModel.status] } + } + + override fun setState( + id: UUID, + status: String, + errorCode: String? + ) { + AttachmentModel.update(where = { AttachmentModel.id eq id }) { + it[this.status] = status + it[this.errorCode] = errorCode + it[updatedAt] = Clock.System.nowMicros() + } + } + + override fun markEnqueueFailed( + id: UUID + ) { + AttachmentModel.update( + where = { + (AttachmentModel.id eq id) and (AttachmentModel.status eq "queued") + } + ) { + it[status] = "failed" + it[errorCode] = "enqueue_failed" + it[updatedAt] = Clock.System.nowMicros() + } + } + + override fun markStagingCleaned( + id: UUID + ) { + AttachmentModel.update(where = { AttachmentModel.id eq id }) { + it[stagingCleaned] = true + } + } + + override fun findExpiredStorage( + before: Instant + ): List { + return AttachmentModel + .leftJoin(AttachmentJobModel) + .select(AttachmentModel.id, AttachmentModel.status, AttachmentJobModel.legacyOriginal) + .where( + (AttachmentModel.status inList listOf("ready", "failed")) + .and(AttachmentModel.updatedAt less before) + .and(AttachmentModel.stagingCleaned eq false) + ) + .orderBy(AttachmentModel.updatedAt to SortOrder.ASC) + .limit(100) + .map { + ExpiredAttachmentStorage( + id = it[AttachmentModel.id].value, + failed = it[AttachmentModel.status] == "failed", + legacyOriginal = it.getOrNull(AttachmentJobModel.legacyOriginal) == true + ) + } + } + + override fun publish( + id: UUID, + variant: AttachmentVariant + ) { + AttachmentVariantModel.insertIgnore { + it[attachmentId] = id + it[name] = variant.name + it[contentType] = variant.contentType + it[sizeBytes] = variant.sizeBytes + it[width] = variant.width + it[height] = variant.height + it[sha256] = variant.sha256 + } + + setState( + id = id, + status = "ready" + ) } override fun attach( @@ -74,7 +199,9 @@ class ExposedAttachmentRepository : AttachmentRepository { ) } - private fun ResultRow.toAttachment() = Attachment( + private fun ResultRow.toAttachment( + variants: List + ) = Attachment( id = this[AttachmentModel.id].value, chatId = this[AttachmentModel.chatId].value, ownerId = this[AttachmentModel.ownerId].value, @@ -82,7 +209,26 @@ class ExposedAttachmentRepository : AttachmentRepository { contentType = this[AttachmentModel.contentType], sizeBytes = this[AttachmentModel.sizeBytes], sha256 = this[AttachmentModel.sha256], - createdAt = this[AttachmentModel.createdAt] + createdAt = this[AttachmentModel.createdAt], + updatedAt = this[AttachmentModel.updatedAt], + status = this[AttachmentModel.status], + errorCode = this[AttachmentModel.errorCode], + variants = variants ) + private fun ResultRow.toAttachmentVariantOrNull(): AttachmentVariant? { + + val name = getOrNull(AttachmentVariantModel.name) + ?: return null + + return AttachmentVariant( + name = name, + contentType = this[AttachmentVariantModel.contentType], + sizeBytes = this[AttachmentVariantModel.sizeBytes], + width = this[AttachmentVariantModel.width], + height = this[AttachmentVariantModel.height], + sha256 = this[AttachmentVariantModel.sha256] + ) + } + } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt new file mode 100644 index 00000000..270d47e2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt @@ -0,0 +1,59 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs + +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant +import java.util.UUID + +interface AttachmentJobRepository { + + fun create( + job: AttachmentJob + ) + + fun claim( + job: AttachmentJob, + token: UUID, + maxAttempts: Int, + leaseSeconds: Long + ): JobClaim + + fun complete( + job: AttachmentJob, + token: UUID, + variant: AttachmentVariant + ): Boolean + + fun fail( + job: AttachmentJob, + token: UUID, + errorCode: String, + permanent: Boolean, + maxAttempts: Int + ): Boolean + + fun findPendingLegacyJobs( + limit: Int + ): List + + fun markLegacyJobDispatched( + id: UUID + ) + +} + +sealed interface JobClaim { + + data object Complete : JobClaim + + data object Busy : JobClaim + + data class Retry( + val attempt: Int + ) : JobClaim + + data class Acquired( + val attempt: Int, + val legacyOriginal: Boolean = false + ) : JobClaim + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt new file mode 100644 index 00000000..7de5e212 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt @@ -0,0 +1,250 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs + +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.utils.exposed.operations.plus +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.eq +import org.jetbrains.exposed.v1.core.greater +import org.jetbrains.exposed.v1.core.isNotNull +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.select +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.update +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class ExposedAttachmentJobRepository( + private val attachmentRepository: AttachmentRepository +) : AttachmentJobRepository { + + override fun create( + job: AttachmentJob + ) { + AttachmentJobModel.insert { + it[id] = job.jobId + it[attachmentId] = job.attachmentId + it[pipelineVersion] = job.pipelineVersion + it[createdAt] = job.createdAt + } + } + + override fun claim( + job: AttachmentJob, + token: UUID, + maxAttempts: Int, + leaseSeconds: Long + ): JobClaim { + + val row = lock(job) ?: return JobClaim.Complete + + val status = AttachmentModel + .select(AttachmentModel.status) + .where(AttachmentModel.id eq job.attachmentId) + .forUpdate() + .single()[AttachmentModel.status] + + if (status in setOf("ready", "failed")) { + return JobClaim.Complete + } + + val activeLease = AttachmentJobModel.leaseUntil.isNotNull() + .and(AttachmentJobModel.leaseUntil greater CurrentTimestamp) + + val busy = AttachmentJobModel + .select(activeLease) + .where(AttachmentJobModel.id eq job.jobId) + .single() + .get(activeLease) == true + + if (busy) { + return JobClaim.Busy + } + + val attempts = row[AttachmentJobModel.attempts] + + if (attempts >= maxAttempts) { + attachmentRepository.setState( + id = job.attachmentId, + status = "failed", + errorCode = "retry_exhausted" + ) + + clearLease(job.jobId) + + return JobClaim.Complete + } + + val nextAttempt = attempts + 1 + + if (job.attempt != nextAttempt) { + if (row[AttachmentJobModel.leaseToken] != null) { + queueRetry( + id = job.jobId, + attachmentId = job.attachmentId, + errorCode = "lease_expired" + ) + } + + return JobClaim.Retry(attempt = nextAttempt) + } + + AttachmentJobModel.update(where = { AttachmentJobModel.id eq job.jobId }) { + it[this.attempts] = nextAttempt + it[leaseToken] = token + it[leaseUntil] = CurrentTimestamp + longLiteral(leaseSeconds) + it[nextAttemptAt] = null + } + + attachmentRepository.setState( + id = job.attachmentId, + status = "processing" + ) + + return JobClaim.Acquired( + attempt = nextAttempt, + legacyOriginal = row[AttachmentJobModel.legacyOriginal] + ) + } + + override fun complete( + job: AttachmentJob, + token: UUID, + variant: AttachmentVariant + ): Boolean { + + val row = lock(job) ?: return false + + if (row[AttachmentJobModel.leaseToken] != token) { + return false + } + + attachmentRepository.publish( + id = job.attachmentId, + variant = variant + ) + + clearLease(job.jobId) + + return true + } + + override fun fail( + job: AttachmentJob, + token: UUID, + errorCode: String, + permanent: Boolean, + maxAttempts: Int + ): Boolean { + + val row = lock(job) ?: return true + + if (row[AttachmentJobModel.leaseToken] != token) { + return false + } + + val terminal = permanent || row[AttachmentJobModel.attempts] >= maxAttempts + + if (!terminal) { + queueRetry( + id = job.jobId, + attachmentId = job.attachmentId, + errorCode = errorCode + ) + + return false + } + + attachmentRepository.setState( + id = job.attachmentId, + status = "failed", + errorCode = if (permanent) errorCode else "retry_exhausted" + ) + + AttachmentJobModel.update(where = { AttachmentJobModel.id eq job.jobId }) { + it[leaseToken] = null + it[leaseUntil] = null + it[lastErrorCode] = errorCode + it[nextAttemptAt] = null + } + + return true + } + + private fun queueRetry( + id: UUID, + attachmentId: UUID, + errorCode: String + ) { + attachmentRepository.setState( + id = attachmentId, + status = "queued" + ) + + AttachmentJobModel.update(where = { AttachmentJobModel.id eq id }) { + it[leaseToken] = null + it[leaseUntil] = null + it[lastErrorCode] = errorCode + it[nextAttemptAt] = CurrentTimestamp + } + } + + override fun findPendingLegacyJobs( + limit: Int + ): List { + return AttachmentJobModel + .selectAll() + .where(AttachmentJobModel.legacyDispatchPending eq true) + .orderBy(AttachmentJobModel.createdAt to SortOrder.ASC, AttachmentJobModel.id to SortOrder.ASC) + .limit(limit) + .map { + AttachmentJob( + jobId = it[AttachmentJobModel.id].value, + attachmentId = it[AttachmentJobModel.attachmentId].value, + pipelineVersion = it[AttachmentJobModel.pipelineVersion], + createdAt = it[AttachmentJobModel.createdAt] + ) + } + } + + override fun markLegacyJobDispatched( + id: UUID + ) { + AttachmentJobModel.update(where = { AttachmentJobModel.id eq id }) { + it[legacyDispatchPending] = false + } + } + + private fun lock( + job: AttachmentJob + ): ResultRow? { + return AttachmentJobModel + .selectAll() + .where(AttachmentJobModel.id eq job.jobId) + .forUpdate() + .firstOrNull() + ?.takeIf { + it[AttachmentJobModel.attachmentId].value == job.attachmentId && + it[AttachmentJobModel.pipelineVersion] == job.pipelineVersion + } + } + + private fun clearLease( + id: UUID + ) { + AttachmentJobModel.update(where = { AttachmentJobModel.id eq id }) { + it[leaseToken] = null + it[leaseUntil] = null + it[nextAttemptAt] = null + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt deleted file mode 100644 index 42154b81..00000000 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.mtusi.messenger.main.infrastructure.repositories.attachment.storage - -import java.io.File -import java.util.UUID - -interface ObjectStorageRepository { - - suspend fun put( - id: UUID, - file: File, - contentType: String - ) - - suspend fun get(id: UUID): ByteArray - - suspend fun delete(id: UUID) - - suspend fun ready(): Boolean -} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt index 125ac525..d7b41db8 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt @@ -1,16 +1,23 @@ package com.mtusi.messenger.main.infrastructure.services.attachment +import com.mtusi.messenger.main.config.worker.WorkerConfig +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository -import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService import com.mtusi.messenger.main.plugins.suspendedTransaction import kotlinx.coroutines.CancellationException import org.koin.core.annotation.Single +import kotlin.time.Clock +import kotlin.time.Duration.Companion.seconds @Single class AttachmentCleanupService( private val journalRepository: UploadJournalRepository, - private val objectStorageRepository: ObjectStorageRepository + private val objectStorageService: ObjectStorageService, + private val attachmentRepository: AttachmentRepository, + private val config: WorkerConfig ) { + suspend fun cleanup(): Int { val ids = suspendedTransaction { @@ -18,9 +25,10 @@ class AttachmentCleanupService( } var deleted = 0 + ids.forEach { id -> try { - objectStorageRepository.delete(id) + objectStorageService.delete(id) deleted++ } catch (cause: CancellationException) { throw cause @@ -29,6 +37,34 @@ class AttachmentCleanupService( } } + val expired = suspendedTransaction { + attachmentRepository.findExpiredStorage( + before = Clock.System.now() - config.stagingRetentionSeconds.seconds + ) + } + + expired.forEach { attachment -> + try { + when { + attachment.failed && !attachment.legacyOriginal -> + objectStorageService.delete(attachment.id) + attachment.failed -> + objectStorageService.deleteThumbnail(attachment.id) + !attachment.legacyOriginal -> + objectStorageService.deleteStaging(attachment.id) + } + + suspendedTransaction { + attachmentRepository.markStagingCleaned(attachment.id) + } + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + // Terminal metadata remains available for a later cleanup retry. + } + } + return deleted } + } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt index b4fffe56..cdc0e0b9 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt @@ -24,5 +24,9 @@ interface AttachmentService { actorId: UUID ): Attachment - suspend fun content(attachment: Attachment): ByteArray + suspend fun content( + attachment: Attachment, + variant: String = "original" + ): ByteArray + } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt new file mode 100644 index 00000000..29812a17 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt @@ -0,0 +1,171 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.config.worker.WorkerConfig +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.JobClaim +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService +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 kotlinx.coroutines.CancellationException +import kotlinx.coroutines.delay +import kotlinx.coroutines.withTimeoutOrNull +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds + +@Single +class DefaultAttachmentProcessingService( + private val jobRepository: AttachmentJobRepository, + private val attachmentRepository: AttachmentRepository, + private val objectStorageService: ObjectStorageService, + private val imageProcessingService: ImageProcessingService, + private val jobBrokerService: JobBrokerService, + private val config: WorkerConfig +) { + + suspend fun process( + job: AttachmentJob + ): Boolean { + + val token = UUID.randomUUID() + val claim = suspendedTransaction { + jobRepository.claim( + job = job, + token = token, + maxAttempts = config.maxAttempts, + leaseSeconds = config.timeoutSeconds + 15 + ) + } + + when (claim) { + JobClaim.Complete -> return true + + JobClaim.Busy -> { + // Pace redeliveries while the lease is active. Publish to the tail and + // confirm before acknowledging the original delivery. + delay(250.milliseconds) + jobBrokerService.publish(job) + + return true + } + + is JobClaim.Retry -> { + jobBrokerService.publish(job.copy(attempt = claim.attempt)) + + return true + } + + is JobClaim.Acquired -> Unit + } + + try { + val completed = withTimeoutOrNull(config.timeoutSeconds.seconds) { + processAttachment( + job = job, + token = token, + legacyOriginal = claim.legacyOriginal + ) + } ?: throw ProcessingTimeoutException() + + return completed + } catch (cause: CancellationException) { + // Leave the delivery unacknowledged; a new worker can reclaim the expired lease. + throw cause + } catch (cause: Exception) { + val isPermanent = cause is ApiException && cause.code in setOf( + ErrorCode.UNSUPPORTED_MEDIA_TYPE, + ErrorCode.PAYLOAD_TOO_LARGE + ) + + val errorCode = when { + isPermanent -> "invalid_image" + cause is ProcessingTimeoutException -> "processing_timeout" + cause is ApiException && cause.code == ErrorCode.UNAVAILABLE -> "storage_unavailable" + else -> "processing_failed" + } + + val isTerminal = suspendedTransaction { + jobRepository.fail( + job = job, + token = token, + errorCode = errorCode, + permanent = isPermanent, + maxAttempts = config.maxAttempts + ) + } + + if (!isTerminal) { + jobBrokerService.publish(job.copy(attempt = claim.attempt + 1)) + } + + return true + } + } + + private class ProcessingTimeoutException : RuntimeException() + + private suspend fun processAttachment( + job: AttachmentJob, + token: UUID, + legacyOriginal: Boolean + ): Boolean { + + val attachment = suspendedTransaction { + checkNotNull(attachmentRepository.findById(job.attachmentId)) + } + + val bytes = if (legacyOriginal) { + objectStorageService.get(attachment.id) + } else { + objectStorageService.getStaging(attachment.id) + } + + if (bytes.size.toLong() != attachment.sizeBytes || bytes.computeSha256() != attachment.sha256) { + throw ApiException( + code = ErrorCode.UNSUPPORTED_MEDIA_TYPE, + message = "invalid source image" + ) + } + + val thumbnail = imageProcessingService.thumbnail( + bytes = bytes, + declaredType = attachment.contentType + ) + + if (!legacyOriginal) { + objectStorageService.put( + id = attachment.id, + bytes = bytes, + contentType = attachment.contentType + ) + } + + objectStorageService.putThumbnail( + id = attachment.id, + bytes = thumbnail.bytes + ) + + return suspendedTransaction { + jobRepository.complete( + job = job, + token = token, + variant = AttachmentVariant( + name = "thumbnail", + contentType = thumbnail.contentType, + sizeBytes = thumbnail.bytes.size.toLong(), + width = thumbnail.width, + height = thumbnail.height, + sha256 = thumbnail.bytes.computeSha256() + ) + ) + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt index ea95a32c..8782680c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt @@ -1,16 +1,21 @@ package com.mtusi.messenger.main.infrastructure.services.attachment import com.mtusi.messenger.main.infrastructure.dto.Attachment -import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository -import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService 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 com.mtusi.messenger.main.utils.nowMicros +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.withContext import org.koin.core.annotation.Single import java.io.File @@ -22,14 +27,24 @@ class DefaultAttachmentService( private val journalRepository: UploadJournalRepository, private val attachmentRepository: AttachmentRepository, private val chatRepository: ChatRepository, - private val objectStorageRepository: ObjectStorageRepository, + private val objectStorageService: ObjectStorageService, + private val jobRepository: AttachmentJobRepository, + private val jobBrokerService: JobBrokerService ) : AttachmentService { override suspend fun requireMember( chatId: UUID, actorId: UUID ) { - if (!suspendedTransaction { chatRepository.isMember(chatId, actorId) }) { + + val isMember = suspendedTransaction { + chatRepository.isMember( + chatId = chatId, + userId = actorId + ) + } + + if (!isMember) { notFound() } } @@ -42,11 +57,16 @@ class DefaultAttachmentService( file: File ): Attachment { - requireMember(chatId, actorId) + requireMember( + chatId = chatId, + actorId = actorId + ) - val (type, hash) = withContext(Dispatchers.IO) { + val (validatedType, sha256) = withContext(Dispatchers.IO) { val bytes = file.readBytes() - ImageValidator.validate(bytes, contentType) to bytes.computeSha256() + val type = ImageValidator.validate(bytes, contentType) + + type to bytes.computeSha256() } val attachment = Attachment( @@ -54,24 +74,62 @@ class DefaultAttachmentService( chatId = chatId, ownerId = actorId, filename = filename, - contentType = type, + contentType = validatedType, sizeBytes = file.length(), - sha256 = hash, - createdAt = Clock.System.nowMicros() + sha256 = sha256, + createdAt = Clock.System.nowMicros(), + status = "queued" ) suspendedTransaction { journalRepository.register(attachment.id) } - objectStorageRepository.put(attachment.id, file, type) - suspendedTransaction { - if (!journalRepository.lockForCompletion(attachment.id)) { - throw ApiException(ErrorCode.UNAVAILABLE, "upload expired") + objectStorageService.putStaging( + id = attachment.id, + file = file, + contentType = validatedType + ) + + val job = AttachmentJob( + jobId = UUID.randomUUID(), + attachmentId = attachment.id, + createdAt = attachment.createdAt + ) + + try { + suspendedTransaction { + if (!journalRepository.lockForCompletion(attachment.id)) { + throw ApiException( + code = ErrorCode.UNAVAILABLE, + message = "upload expired" + ) + } + + attachmentRepository.create(attachment) + jobRepository.create(job) + journalRepository.complete(attachment.id) } - attachmentRepository.create(attachment) - journalRepository.complete(attachment.id) + + jobBrokerService.publish(job) + } catch (cause: CancellationException) { + withContext(NonCancellable) { + runCatching { + suspendedTransaction { + attachmentRepository.markEnqueueFailed(attachment.id) + } + } + } + + throw cause + } catch (cause: Exception) { + suspendedTransaction { + attachmentRepository.markEnqueueFailed(attachment.id) + } + + throw cause } + return attachment } @@ -79,16 +137,49 @@ class DefaultAttachmentService( id: UUID, actorId: UUID ): Attachment = suspendedTransaction { - val attachment = attachmentRepository.findById(id) ?: notFound() - if (!chatRepository.isMember(attachment.chatId, actorId)) { + + val attachment = attachmentRepository.findById(id) + ?: notFound() + + val isMember = chatRepository.isMember( + chatId = attachment.chatId, + userId = actorId + ) + + if (!isMember) { notFound() } + attachment } - override suspend fun content(attachment: Attachment): ByteArray = withContext(Dispatchers.IO) { - objectStorageRepository.get(attachment.id) + override suspend fun content( + attachment: Attachment, + variant: String + ): ByteArray = withContext(Dispatchers.IO) { + + if (attachment.status != "ready") { + throw ApiException( + code = ErrorCode.CONFLICT, + message = "attachment is not ready" + ) + } + + when (variant) { + "original" -> objectStorageService.get(attachment.id) + "thumbnail" -> objectStorageService.getThumbnail(attachment.id) + else -> throw ApiException( + code = ErrorCode.NOT_FOUND, + message = "variant not found" + ) + } + } + + private fun notFound(): Nothing { + throw ApiException( + code = ErrorCode.NOT_FOUND, + message = "attachment or chat not found" + ) } - private fun notFound(): Nothing = throw ApiException(ErrorCode.NOT_FOUND, "attachment or chat not found") } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt new file mode 100644 index 00000000..8dd1a869 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt @@ -0,0 +1,206 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.drew.imaging.jpeg.JpegMetadataReader +import com.drew.lang.ByteArrayReader +import com.drew.metadata.Metadata +import com.drew.metadata.exif.ExifIFD0Directory +import com.drew.metadata.exif.ExifReader +import com.mtusi.messenger.main.infrastructure.dto.ProcessedImage +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.withContext +import org.koin.core.annotation.Single +import java.awt.Color +import java.awt.RenderingHints +import java.awt.geom.AffineTransform +import java.awt.image.BufferedImage +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.nio.ByteBuffer +import javax.imageio.ImageIO + +private const val THUMBNAIL_SIZE = 256 + +@Single +class DefaultImageProcessingService : ImageProcessingService { + + override suspend fun thumbnail( + bytes: ByteArray, + declaredType: String + ): ProcessedImage = withContext(Dispatchers.IO) { + + currentCoroutineContext().ensureActive() + + val original = ImageValidator.decode( + bytes = bytes, + declaredType = declaredType + ) + + try { + currentCoroutineContext().ensureActive() + + val orientation = readOrientation(bytes, declaredType) + val swapDimensions = orientation in 5..8 + val orientedWidth = if (swapDimensions) original.height else original.width + val orientedHeight = if (swapDimensions) original.width else original.height + val longestSide = maxOf(orientedWidth, orientedHeight) + + val width = if (longestSide <= THUMBNAIL_SIZE) { + orientedWidth + } else { + maxOf(1, (orientedWidth.toLong() * THUMBNAIL_SIZE / longestSide).toInt()) + } + + val height = if (longestSide <= THUMBNAIL_SIZE) { + orientedHeight + } else { + maxOf(1, (orientedHeight.toLong() * THUMBNAIL_SIZE / longestSide).toInt()) + } + + val thumbnail = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) + + try { + val transform = AffineTransform.getScaleInstance( + width.toDouble() / orientedWidth, + height.toDouble() / orientedHeight + ).apply { + val orientationCorrection = orientationTransform( + orientation = orientation, + width = original.width, + height = original.height + ) + + concatenate(orientationCorrection) + } + + drawThumbnail( + original = original, + thumbnail = thumbnail, + transform = transform + ) + + currentCoroutineContext().ensureActive() + + val encoded = ByteArrayOutputStream().use { output -> + check(ImageIO.write(thumbnail, "jpeg", output)) { + "JPEG encoder is unavailable" + } + + output.toByteArray() + } + + ProcessedImage( + bytes = encoded, + width = width, + height = height + ) + } finally { + thumbnail.flush() + } + } finally { + original.flush() + } + } + + private fun drawThumbnail( + original: BufferedImage, + thumbnail: BufferedImage, + transform: AffineTransform + ) { + + val graphics = thumbnail.createGraphics() + + try { + graphics.color = Color.WHITE + graphics.fillRect(0, 0, thumbnail.width, thumbnail.height) + + graphics.setRenderingHint( + RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR + ) + + graphics.setRenderingHint( + RenderingHints.KEY_RENDERING, + RenderingHints.VALUE_RENDER_QUALITY + ) + + graphics.drawImage(original, transform, null) + } finally { + graphics.dispose() + } + } + + private fun readOrientation( + bytes: ByteArray, + declaredType: String + ): Int { + + // Read only EXIF: generic PNG metadata readers can inflate optional text chunks. + val metadata = if (declaredType == "image/jpeg") { + ByteArrayInputStream(bytes).use { input -> + JpegMetadataReader.readMetadata(input, listOf(ExifReader())) + } + } else { + readPngExif(bytes) + } + + return metadata + .getFirstDirectoryOfType(ExifIFD0Directory::class.java) + ?.getInteger(ExifIFD0Directory.TAG_ORIENTATION) + ?.takeIf { it in 1..8 } + ?: 1 + } + + private fun readPngExif( + bytes: ByteArray + ): Metadata { + + val metadata = Metadata() + var offset = 8 + + while (offset + 12 <= bytes.size) { + val length = ByteBuffer.wrap(bytes, offset, 4).int + val type = String(bytes, offset + 4, 4, Charsets.US_ASCII) + + if (type == "eXIf") { + val dataOffset = offset + 8 + val exif = ByteArrayReader( + bytes.copyOfRange( + fromIndex = dataOffset, + toIndex = dataOffset + length + ) + ) + + ExifReader().extract(exif, metadata) + break + } + + offset += length + 12 + } + + return metadata + } + + private fun orientationTransform( + orientation: Int, + width: Int, + height: Int + ): AffineTransform { + + val sourceWidth = width.toDouble() + val sourceHeight = height.toDouble() + + return when (orientation) { + 2 -> AffineTransform(-1.0, 0.0, 0.0, 1.0, sourceWidth, 0.0) + 3 -> AffineTransform(-1.0, 0.0, 0.0, -1.0, sourceWidth, sourceHeight) + 4 -> AffineTransform(1.0, 0.0, 0.0, -1.0, 0.0, sourceHeight) + 5 -> AffineTransform(0.0, 1.0, 1.0, 0.0, 0.0, 0.0) + 6 -> AffineTransform(0.0, 1.0, -1.0, 0.0, sourceHeight, 0.0) + 7 -> AffineTransform(0.0, -1.0, -1.0, 0.0, sourceHeight, sourceWidth) + 8 -> AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth) + else -> AffineTransform() + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt new file mode 100644 index 00000000..581cc459 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.dto.ProcessedImage + +interface ImageProcessingService { + + suspend fun thumbnail( + bytes: ByteArray, + declaredType: String + ): ProcessedImage + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt index c693f2c2..02f92abf 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt @@ -2,9 +2,10 @@ package com.mtusi.messenger.main.infrastructure.services.attachment import com.mtusi.messenger.main.infrastructure.types.ApiException import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import java.awt.image.BufferedImage +import java.io.ByteArrayInputStream import java.nio.ByteBuffer import java.util.zip.CRC32 -import java.io.ByteArrayInputStream import javax.imageio.ImageIO import javax.imageio.stream.MemoryCacheImageInputStream @@ -18,6 +19,19 @@ object ImageValidator { declaredType: String ): String { + decode( + bytes = bytes, + declaredType = declaredType + ).flush() + + return declaredType + } + + fun decode( + bytes: ByteArray, + declaredType: String + ): BufferedImage { + if (bytes.size > MAX_ATTACHMENT_BYTES) { throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") } @@ -72,13 +86,13 @@ object ImageValidator { reader.addIIOReadWarningListener { _, _ -> warning = true } val image = reader.read(0) ?: invalid() - image.flush() if (warning) { + image.flush() invalid() } - return type + return image } finally { reader.dispose() } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/JobBrokerService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/JobBrokerService.kt new file mode 100644 index 00000000..38187dd9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/JobBrokerService.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment.jobs + +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob + +interface JobBrokerService { + + suspend fun publish( + job: AttachmentJob + ) + + /** A true result acknowledges a durable effect; false requeues the delivery. */ + suspend fun consume( + handler: suspend (AttachmentJob) -> Boolean + ) + + suspend fun ready(): Boolean + + fun close() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt new file mode 100644 index 00000000..2027d4b4 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt @@ -0,0 +1,28 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment.jobs + +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository +import com.mtusi.messenger.main.plugins.suspendedTransaction +import org.koin.core.annotation.Single + +@Single +class LegacyAttachmentDispatchService( + private val jobRepository: AttachmentJobRepository, + private val jobBrokerService: JobBrokerService +) { + + suspend fun publishPending() { + + val jobs = suspendedTransaction { + jobRepository.findPendingLegacyJobs(limit = 32) + } + + for (job in jobs) { + jobBrokerService.publish(job) + + suspendedTransaction { + jobRepository.markLegacyJobDispatched(job.jobId) + } + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerService.kt new file mode 100644 index 00000000..b934a114 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerService.kt @@ -0,0 +1,314 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment.jobs + +import com.mtusi.messenger.main.config.broker.BrokerConfig +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.utils.resolveSecret +import com.rabbitmq.client.AMQP +import com.rabbitmq.client.Channel +import com.rabbitmq.client.Connection +import com.rabbitmq.client.ConnectionFactory +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.delay +import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.isActive +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import kotlinx.coroutines.withContext +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy +import org.koin.core.annotation.Single +import org.slf4j.LoggerFactory +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds + +private const val MAX_JOB_BYTES = 16 * 1024 + +@Single +class RabbitMqJobBrokerService( + private val config: BrokerConfig +) : JobBrokerService { + + private val logger = LoggerFactory.getLogger(RabbitMqJobBrokerService::class.java) + private val closed = AtomicBoolean(false) + private val publisherMutex = Mutex() + private val consumers = ConcurrentHashMap.newKeySet() + + @Volatile + private var publisher: Connection? = null + + @OptIn(ExperimentalSerializationApi::class) + private val json = Json { + encodeDefaults = true + namingStrategy = JsonNamingStrategy.SnakeCase + } + + private val factory by lazy { + ConnectionFactory().apply { + host = config.host + port = config.port + username = config.username + password = requireNotNull( + resolveSecret( + value = config.password, + file = config.passwordFile + ) + ) + + connectionTimeout = config.connectionTimeoutMs + handshakeTimeout = config.operationTimeoutMs + channelRpcTimeout = config.operationTimeoutMs + requestedHeartbeat = 10 + isAutomaticRecoveryEnabled = true + networkRecoveryInterval = 1000 + } + } + + override suspend fun publish( + job: AttachmentJob + ) { + + require(valid(job)) { "Unsupported attachment job" } + + try { + publisherMutex.withLock { + withContext(Dispatchers.IO) { + check(!closed.get()) + + val connection = publisherConnection() + + connection.createChannel().use { channel -> + declareQueue(channel) + channel.confirmSelect() + + val returned = AtomicBoolean(false) + channel.addReturnListener { _ -> + returned.set(true) + } + + val properties = AMQP.BasicProperties.Builder() + .contentType("application/json") + .contentEncoding("utf-8") + .deliveryMode(2) + .messageId(job.jobId.toString()) + .build() + + val body = json.encodeToString(job).toByteArray(Charsets.UTF_8) + + channel.basicPublish( + "", + config.queue, + true, + properties, + body + ) + channel.waitForConfirmsOrDie(config.operationTimeoutMs.toLong()) + + check(!returned.get()) { "Attachment job was not routed" } + currentCoroutineContext().ensureActive() + } + } + } + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + currentCoroutineContext().ensureActive() + + throw ApiException( + code = ErrorCode.UNAVAILABLE, + message = "job broker unavailable" + ) + } + } + + override suspend fun consume( + handler: suspend (AttachmentJob) -> Boolean + ) { + withContext(Dispatchers.IO) { + while (currentCoroutineContext().isActive && !closed.get()) { + try { + val connection = connect("messenger-jobs-worker") + consumers.add(connection) + + try { + connection.createChannel().use { channel -> + declareQueue(channel) + channel.basicQos(1) + + consumeDeliveries( + connection = connection, + channel = channel, + handler = handler + ) + } + } finally { + consumers.remove(connection) + runCatching { connection.abort() } + } + } catch (cause: CancellationException) { + throw cause + } catch (cause: Exception) { + currentCoroutineContext().ensureActive() + + if (!closed.get()) { + logger.warn( + "Attachment job consumer disconnected ({}); retrying", + cause.javaClass.simpleName + ) + delay(1.seconds) + } + } + } + } + } + + override suspend fun ready(): Boolean { + return try { + publisherMutex.withLock { + withContext(Dispatchers.IO) { + check(!closed.get()) + + val connection = publisherConnection() + connection.createChannel().use(::declareQueue) + + true + } + } + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + currentCoroutineContext().ensureActive() + + false + } + } + + override fun close() { + closed.set(true) + + runCatching { publisher?.abort() } + + consumers.forEach { connection -> + runCatching { connection.abort() } + } + } + + private suspend fun consumeDeliveries( + connection: Connection, + channel: Channel, + handler: suspend (AttachmentJob) -> Boolean + ) { + + // Waiting for the handler before the next basicGet keeps one delivery in flight. + // Channel or connection closure requeues an unacknowledged job. + while ( + currentCoroutineContext().isActive && + !closed.get() && + connection.isOpen && + channel.isOpen + ) { + val delivery = channel.basicGet(config.queue, false) + + if (delivery == null) { + delay(250.milliseconds) + continue + } + + val job = decode(delivery.body) + val deliveryTag = delivery.envelope.deliveryTag + + if (job == null) { + channel.basicReject(deliveryTag, false) + logger.warn("Rejected unsupported attachment job envelope") + + continue + } + + if (handler(job)) { + channel.basicAck(deliveryTag, false) + } else { + channel.basicNack(deliveryTag, false, true) + delay(500.milliseconds) + } + } + } + + private fun connect( + name: String + ): Connection { + check(!closed.get()) + + return factory.newConnection(name).also { connection -> + if (closed.get()) { + connection.abort() + error("Attachment job broker is closed") + } + } + } + + /** Called under publisherMutex so reconnect does not retain an unused recovery loop. */ + private fun publisherConnection(): Connection { + + publisher?.let { connection -> + if (connection.isOpen) { + return connection + } + + runCatching { connection.abort() } + publisher = null + } + + val connection = connect("messenger-jobs-publisher") + publisher = connection + + if (closed.get()) { + connection.abort() + error("Attachment job broker is closed") + } + + return connection + } + + private fun declareQueue( + channel: Channel + ) { + channel.queueDeclare( + config.queue, + true, + false, + false, + mapOf("x-queue-type" to "classic") + ) + } + + private fun decode( + bytes: ByteArray + ): AttachmentJob? { + + if (bytes.size > MAX_JOB_BYTES) { + return null + } + + return runCatching { + json.decodeFromString(bytes.toString(Charsets.UTF_8)) + } + .getOrNull() + ?.takeIf(::valid) + } + + private fun valid( + job: AttachmentJob + ): Boolean { + return job.schemaVersion == 1 && + job.type == "attachment.process.v1" && + job.pipelineVersion == 1 && + job.attempt >= 1 + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt new file mode 100644 index 00000000..2246ffc6 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt @@ -0,0 +1,46 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment.storage + +import java.io.File +import java.util.UUID + +interface ObjectStorageService { + + suspend fun put( + id: UUID, + bytes: ByteArray, + contentType: String + ) + + suspend fun putStaging( + id: UUID, + file: File, + contentType: String + ) + + suspend fun getStaging( + id: UUID + ): ByteArray + + suspend fun deleteStaging( + id: UUID + ) + + suspend fun putThumbnail( + id: UUID, + bytes: ByteArray + ) + + suspend fun getThumbnail( + id: UUID + ): ByteArray + + suspend fun deleteThumbnail( + id: UUID + ) + + suspend fun get(id: UUID): ByteArray + + suspend fun delete(id: UUID) + + suspend fun ready(): Boolean +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/S3ObjectStorageService.kt similarity index 61% rename from src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt rename to src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/S3ObjectStorageService.kt index 72afac5f..63e3a0fd 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/S3ObjectStorageService.kt @@ -1,4 +1,4 @@ -package com.mtusi.messenger.main.infrastructure.repositories.attachment.storage +package com.mtusi.messenger.main.infrastructure.services.attachment.storage import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider import aws.sdk.kotlin.services.s3.S3Client @@ -22,9 +22,9 @@ import java.util.UUID import kotlin.time.Duration.Companion.milliseconds @Single -class S3ObjectStorageRepository( +class S3ObjectStorageService( private val config: StorageConfig -) : ObjectStorageRepository { +) : ObjectStorageService { private val client by lazy { S3Client { @@ -41,13 +41,33 @@ class S3ObjectStorageRepository( override suspend fun put( id: UUID, - file: File, + bytes: ByteArray, contentType: String ) { request { client.putObject { bucket = config.bucket key = getObjectKey(id) + body = bytes.asByteStream() + this.contentType = contentType + contentLength = bytes.size.toLong() + } + } + } + + override suspend fun get( + id: UUID + ): ByteArray = getBytes(getObjectKey(id)) + + override suspend fun putStaging( + id: UUID, + file: File, + contentType: String + ) { + request { + client.putObject { + bucket = config.bucket + key = getStagingKey(id) body = file.asByteStream() this.contentType = contentType contentLength = file.length() @@ -55,13 +75,72 @@ class S3ObjectStorageRepository( } } - override suspend fun get( + override suspend fun getStaging( id: UUID + ): ByteArray = getBytes(getStagingKey(id)) + + override suspend fun deleteStaging( + id: UUID + ) { + deleteKey(getStagingKey(id)) + } + + override suspend fun putThumbnail( + id: UUID, + bytes: ByteArray + ) { + request { + client.putObject { + bucket = config.bucket + key = getThumbnailKey(id) + body = bytes.asByteStream() + contentType = "image/jpeg" + contentLength = bytes.size.toLong() + } + } + } + + override suspend fun getThumbnail( + id: UUID + ): ByteArray = getBytes(getThumbnailKey(id)) + + override suspend fun deleteThumbnail( + id: UUID + ) { + deleteKey(getThumbnailKey(id)) + } + + override suspend fun delete( + id: UUID + ) { + deleteKey(getObjectKey(id)) + deleteStaging(id) + deleteThumbnail(id) + } + + override suspend fun ready(): Boolean { + return try { + request { + client.headBucket { + bucket = config.bucket + } + } + + true + } catch (_: ApiException) { + false + } + } + + private suspend fun getBytes( + key: String ): ByteArray = request { - client.getObject(GetObjectRequest { - bucket = config.bucket - key = getObjectKey(id) - }) { response -> + client.getObject( + GetObjectRequest { + bucket = config.bucket + this.key = key + } + ) { response -> val contentLength = requireNotNull(response.contentLength) @@ -75,31 +154,28 @@ class S3ObjectStorageRepository( } } - override suspend fun delete( - id: UUID + private suspend fun deleteKey( + key: String ) { request { client.deleteObject { bucket = config.bucket - key = getObjectKey(id) + this.key = key } } } - override suspend fun ready(): Boolean { - return try { - request { - client.headBucket { - bucket = config.bucket - } - } - true - } catch (_: ApiException) { - false - } - } + private fun getObjectKey( + id: UUID + ) = "originals/$id" - private fun getObjectKey(id: UUID) = "originals/$id" + private fun getStagingKey( + id: UUID + ) = "staging/$id" + + private fun getThumbnailKey( + id: UUID + ) = "variants/$id/v1/thumbnail.jpg" private suspend fun request(block: suspend () -> T): T { return try { 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 c91ca0ae..1bfd581e 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,14 +1,14 @@ package com.mtusi.messenger.main.infrastructure.services.message -import com.mtusi.messenger.main.infrastructure.types.ErrorCode -import com.mtusi.messenger.main.infrastructure.types.ApiException -import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository import com.mtusi.messenger.main.config.message.MessageConfig +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository 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.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode import com.mtusi.messenger.main.utils.PageKey import com.mtusi.messenger.main.utils.computeSha256 import com.mtusi.messenger.main.utils.decodeCursor @@ -44,14 +44,20 @@ class DefaultMessageService( return MessageCreationResult.ChatNotFound } - attachmentIds.forEach { id -> - if (attachments.findById(id)?.chatId != chatId) - throw ApiException( - ErrorCode.NOT_FOUND, "attachment not found") + requireAttachmentStatuses( + chatId = chatId, + attachmentIds = attachmentIds + ) + + val attachmentSuffix = if (attachmentIds.isEmpty()) { + "" + } else { + "\u0000attachments:" + attachmentIds.joinToString(",") } - val requestHash = (text + if (attachmentIds.isEmpty()) "" else "\u0000attachments:" + attachmentIds.joinToString(",")) - .toByteArray().computeSha256() + val requestHash = (text + attachmentSuffix) + .toByteArray() + .computeSha256() val now = Clock.System.nowMicros() @@ -59,6 +65,19 @@ class DefaultMessageService( if (idempotencyRepository.claim(senderId, chatId, idempotencyKey, requestHash, now)) { + val statuses = requireAttachmentStatuses( + chatId = chatId, + attachmentIds = attachmentIds, + lockForUpdate = true + ) + + if ("failed" in statuses) { + throw ApiException( + code = ErrorCode.CONFLICT, + message = "attachment processing failed" + ) + } + val message = messageRepository.create( chatId = chatId, senderId = senderId, @@ -103,6 +122,28 @@ class DefaultMessageService( error("Idempotency key could not be claimed") } + private fun requireAttachmentStatuses( + chatId: UUID, + attachmentIds: List, + lockForUpdate: Boolean = false + ): List { + + val statuses = attachments.findStatusesInChat( + chatId = chatId, + ids = attachmentIds, + lockForUpdate = lockForUpdate + ) + + if (statuses.size != attachmentIds.size) { + throw ApiException( + code = ErrorCode.NOT_FOUND, + message = "attachment not found" + ) + } + + return statuses + } + override fun findPageByChatId( chatId: UUID, actorId: UUID, diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt index 76f69853..1c39666d 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt @@ -2,6 +2,7 @@ package com.mtusi.messenger.main.plugins import com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentCleanupService import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopping import io.ktor.server.application.log import kotlinx.coroutines.CancellationException import kotlinx.coroutines.delay @@ -14,9 +15,10 @@ fun Application.configureAttachmentCleanup() { val cleanupService by inject() - launch { + val cleanup = launch { while (isActive) { delay(30.seconds) + try { cleanupService.cleanup() } catch (cause: CancellationException) { @@ -26,4 +28,9 @@ fun Application.configureAttachmentCleanup() { } } } + + monitor.subscribe(ApplicationStopping) { + cleanup.cancel() + } + } diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt new file mode 100644 index 00000000..d2c94b20 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt @@ -0,0 +1,50 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.config.worker.WorkerConfig +import com.mtusi.messenger.main.infrastructure.services.attachment.DefaultAttachmentProcessingService +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.LegacyAttachmentDispatchService +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopping +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.get +import kotlin.time.Duration.Companion.seconds + +fun Application.configureAttachmentWorker() { + + val broker = get() + val processingService = get() + val legacyDispatchService = get() + val config = get() + + val workers = List(config.concurrency) { + launch { + broker.consume(processingService::process) + } + } + + val legacyDispatch = launch { + while (isActive) { + try { + legacyDispatchService.publishPending() + } catch (cause: CancellationException) { + throw cause + } catch (cause: Exception) { + log.warn("Legacy attachment dispatch failed ({}); retrying", cause.javaClass.simpleName) + } + + delay(1.seconds) + } + } + + monitor.subscribe(ApplicationStopping) { + workers.forEach { it.cancel() } + legacyDispatch.cancel() + broker.close() + } + +} 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 d54e0b83..9a55309e 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt @@ -1,10 +1,12 @@ package com.mtusi.messenger.main.plugins -import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel -import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel -import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel import com.mtusi.messenger.main.config.database.DatabaseConfig import com.mtusi.messenger.main.config.database.SerialDatabaseConfig +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentVariantModel +import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel 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 @@ -21,18 +23,19 @@ import io.ktor.server.application.ApplicationStopped import io.ktor.server.config.HoconApplicationConfig import io.ktor.server.config.getAs import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext 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.suspendTransaction 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 +import org.jetbrains.exposed.v1.core.DatabaseConfig as ExposedDatabaseConfig private const val MIGRATIONS_DIRECTORY = "src/main/resources/migrations" private const val DEVELOPMENT_DATABASE_CONFIG = "run/development/config/database.conf" @@ -121,6 +124,8 @@ private fun generateMigration(description: String) { val tables: Array
= arrayOf( AttachmentModel, + AttachmentVariantModel, + AttachmentJobModel, UploadJournalModel, MessageAttachmentModel, UserModel, @@ -175,7 +180,10 @@ private fun generateMigration(description: String) { } -suspend fun suspendedTransaction(statement: JdbcTransaction.() -> R) = newSuspendedTransaction( - context = Dispatchers.IO, - statement = statement -) +suspend fun suspendedTransaction( + statement: JdbcTransaction.() -> R +): R = withContext(Dispatchers.IO) { + suspendTransaction { + statement() + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/JobBroker.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/JobBroker.kt new file mode 100644 index 00000000..536caadb --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/JobBroker.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopping +import org.koin.ktor.ext.get + +fun Application.configureJobBroker() { + + val broker = get() + + monitor.subscribe(ApplicationStopping) { + broker.close() + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt index b0c7343e..6f389c0d 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt @@ -2,6 +2,7 @@ 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.ApplicationStopping import io.ktor.server.application.log import kotlinx.coroutines.CancellationException import kotlinx.coroutines.delay @@ -21,9 +22,10 @@ fun Application.configureSessionRevocationSync() { val authenticationService by inject() - launch { + val synchronization = launch { while (isActive) { delay(SYNC_INTERVAL) + try { authenticationService.syncPendingRevocations() } catch (e: CancellationException) { @@ -33,4 +35,9 @@ fun Application.configureSessionRevocationSync() { } } } + + monitor.subscribe(ApplicationStopping) { + synchronization.cancel() + } + } diff --git a/src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt b/src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt new file mode 100644 index 00000000..a326856a --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt @@ -0,0 +1,38 @@ +package com.mtusi.messenger.main.worker + +import com.mtusi.messenger.main.infrastructure.controllers.health.configureHealthRouting +import com.mtusi.messenger.main.plugins.configureAttachmentCleanup +import com.mtusi.messenger.main.plugins.configureAttachmentWorker +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.configureGracefulShutdown +import com.mtusi.messenger.main.plugins.configureInstanceId +import com.mtusi.messenger.main.plugins.configureJobBroker +import com.mtusi.messenger.main.plugins.configureKoin +import com.mtusi.messenger.main.plugins.configureRequestId +import com.mtusi.messenger.main.plugins.configureStatusPages +import io.ktor.server.application.Application +import io.ktor.server.routing.routing + +fun Application.workerModule() { + + configureKoin() + configureDatabase() + configureGracefulShutdown() + + configureContentNegotiation() + configureDefaultHeaders() + configureInstanceId() + configureRequestId() + configureStatusPages() + + configureJobBroker() + configureAttachmentWorker() + configureAttachmentCleanup() + + routing { + configureHealthRouting() + } + +} diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 4d51dca4..c504aa1a 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -110,3 +110,29 @@ storage { accessKeyFile = ${?S3_ACCESS_KEY_ID_FILE} secretKeyFile = ${?S3_SECRET_ACCESS_KEY_FILE} } + +broker { + host = "localhost" + host = ${?BROKER_HOST} + port = 5672 + port = ${?BROKER_PORT} + username = "messenger" + username = ${?BROKER_USER} + password = ${?BROKER_PASSWORD} + passwordFile = ${?BROKER_PASSWORD_FILE} + queue = "attachment.process.v1" + queue = ${?BROKER_QUEUE} + connectionTimeoutMs = 3000 + operationTimeoutMs = 8000 +} + +worker { + concurrency = 1 + concurrency = ${?WORKER_CONCURRENCY} + maxAttempts = 5 + maxAttempts = ${?JOB_MAX_ATTEMPTS} + timeoutSeconds = 60 + timeoutSeconds = ${?JOB_TIMEOUT_SECONDS} + stagingRetentionSeconds = 86400 + stagingRetentionSeconds = ${?STAGING_RETENTION_SECONDS} +} diff --git a/src/main/resources/migrations/V6__attachment_processing.sql b/src/main/resources/migrations/V6__attachment_processing.sql new file mode 100644 index 00000000..e2808304 --- /dev/null +++ b/src/main/resources/migrations/V6__attachment_processing.sql @@ -0,0 +1,29 @@ +ALTER TABLE attachments ADD COLUMN status VARCHAR(16) NOT NULL DEFAULT 'ready' + CHECK (status IN ('queued', 'processing', 'ready', 'failed')); +ALTER TABLE attachments ADD COLUMN staging_cleaned BOOLEAN NOT NULL DEFAULT FALSE; +ALTER TABLE attachments ADD COLUMN error_code VARCHAR(64); +ALTER TABLE attachments ADD COLUMN updated_at TIMESTAMPTZ; +UPDATE attachments SET updated_at = created_at; +ALTER TABLE attachments ALTER COLUMN updated_at SET NOT NULL; + +CREATE TABLE attachment_variants ( + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + name VARCHAR(16) NOT NULL, + content_type VARCHAR(32) NOT NULL, + size_bytes BIGINT NOT NULL CHECK (size_bytes > 0), + width INTEGER NOT NULL CHECK (width > 0), + height INTEGER NOT NULL CHECK (height > 0), + sha256 VARCHAR(64) NOT NULL, + PRIMARY KEY (attachment_id, name) +); + +CREATE TABLE attachment_jobs ( + id UUID PRIMARY KEY, + attachment_id UUID NOT NULL UNIQUE REFERENCES attachments(id), + pipeline_version INTEGER NOT NULL DEFAULT 1, + attempts INTEGER NOT NULL DEFAULT 0, + lease_token UUID, + lease_until TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL, + CHECK ((lease_token IS NULL) = (lease_until IS NULL)) +); diff --git a/src/main/resources/migrations/V7__attachment_retry_metadata.sql b/src/main/resources/migrations/V7__attachment_retry_metadata.sql new file mode 100644 index 00000000..b6c6c92d --- /dev/null +++ b/src/main/resources/migrations/V7__attachment_retry_metadata.sql @@ -0,0 +1,3 @@ +ALTER TABLE attachment_jobs + ADD COLUMN last_error_code VARCHAR(64), + ADD COLUMN next_attempt_at TIMESTAMPTZ; diff --git a/src/main/resources/migrations/V8__backfill_attachment_thumbnails.sql b/src/main/resources/migrations/V8__backfill_attachment_thumbnails.sql new file mode 100644 index 00000000..f3997be3 --- /dev/null +++ b/src/main/resources/migrations/V8__backfill_attachment_thumbnails.sql @@ -0,0 +1,19 @@ +ALTER TABLE attachment_jobs ADD COLUMN legacy_original BOOLEAN NOT NULL DEFAULT FALSE; +ALTER TABLE attachment_jobs ADD COLUMN legacy_dispatch_pending BOOLEAN NOT NULL DEFAULT FALSE; + +CREATE INDEX attachment_jobs_legacy_dispatch_idx ON attachment_jobs (created_at, id) + WHERE legacy_dispatch_pending; + +INSERT INTO attachment_jobs (id, attachment_id, created_at, legacy_original, legacy_dispatch_pending) +SELECT a.id, a.id, CURRENT_TIMESTAMP, TRUE, TRUE +FROM attachments a +WHERE a.status = 'ready' + AND NOT EXISTS (SELECT 1 FROM attachment_jobs j WHERE j.attachment_id = a.id) + AND NOT EXISTS ( + SELECT 1 FROM attachment_variants v WHERE v.attachment_id = a.id AND v.name = 'thumbnail' + ); + +UPDATE attachments a +SET status = 'queued', error_code = NULL, updated_at = CURRENT_TIMESTAMP, staging_cleaned = FALSE +FROM attachment_jobs j +WHERE j.attachment_id = a.id AND j.legacy_dispatch_pending; diff --git a/src/main/resources/worker.conf b/src/main/resources/worker.conf new file mode 100644 index 00000000..b3da2833 --- /dev/null +++ b/src/main/resources/worker.conf @@ -0,0 +1,2 @@ +include "application.conf" +ktor.application.modules = [com.mtusi.messenger.main.worker.WorkerKt.workerModule] diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt index c8181d20..400a8e2e 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt @@ -1,27 +1,53 @@ package com.mtusi.messenger.main.infrastructure.controllers.attachment +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest import com.mtusi.messenger.main.infrastructure.dto.Attachment import com.mtusi.messenger.main.infrastructure.dto.Message -import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest -import com.mtusi.messenger.main.support.* +import com.mtusi.messenger.main.support.TestStorage +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.* -import io.ktor.http.* +import io.ktor.client.request.get +import io.ktor.client.request.parameter +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.http.ContentType +import io.ktor.http.Headers +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType import io.ktor.http.content.OutgoingContent +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout import java.awt.image.BufferedImage import java.io.ByteArrayOutputStream +import java.nio.ByteBuffer +import java.util.UUID +import java.util.zip.CRC32 import javax.imageio.ImageIO -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds class AttachmentControllerTest { @Test - fun `upload originals ACL message links and idempotency`() = runApp { client -> + val alice = client.register("alice") val bob = client.register("bob") val outsider = client.register("outsider") val chat = client.createChat(alice, "files", listOf(alice.id, bob.id)) + for (format in listOf("png", "jpeg")) { val bytes = image(format) val upload = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.$format") { @@ -29,52 +55,101 @@ class AttachmentControllerTest { contentType(ContentType.parse("image/$format")) setBody(bytes) } - assertEquals(HttpStatusCode.Created, upload.status) + + assertEquals(HttpStatusCode.Accepted, upload.status) + val attachment = upload.body() - assertEquals("ready", attachment.status) + + assertEquals("queued", attachment.status) + assertEquals(emptyList(), attachment.variants) assertEquals(bytes.size.toLong(), attachment.sizeBytes) + + val ready = client.awaitReady(attachment.id, bob) + + assertEquals(listOf("thumbnail"), ready.variants.map { it.name }) + val path = "/api/v1/attachments/${attachment.id}" val download = client.get("$path/content") { auth(bob) } + assertEquals(HttpStatusCode.OK, download.status) assertContentEquals(bytes, download.body()) assertEquals(HttpStatusCode.NotFound, client.get(path) { auth(outsider) }.status) assertEquals(HttpStatusCode.NotFound, client.get("$path/content") { auth(outsider) }.status) assertEquals(HttpStatusCode.Unauthorized, client.get("$path/content").status) - assertEquals(HttpStatusCode.NotFound, client.get("$path/content?variant=thumbnail") { auth(bob) }.status) + + val thumbnail = client.get("$path/content?variant=thumbnail") { auth(bob) } + + assertEquals(HttpStatusCode.OK, thumbnail.status) + assertEquals(ContentType.Image.JPEG, thumbnail.contentType()?.withoutParameters()) + + val thumbnailBytes = thumbnail.body() + val decoded = withContext(Dispatchers.IO) { + thumbnailBytes.inputStream().use { input -> + ImageIO.read(input) + } + } + val missingVariant = client.get("$path/content?variant=crop") { auth(bob) } + + assertEquals(32, decoded.width) + assertEquals(20, decoded.height) + assertEquals(HttpStatusCode.NotFound, missingVariant.status) + val outsiderChat = client.createChat(outsider, "outsider", listOf(outsider.id)) - assertEquals(HttpStatusCode.NotFound, client.post("/api/v1/chats/${outsiderChat.id}/messages") { + val rejectedByMembership = client.post("/api/v1/chats/${outsiderChat.id}/messages") { auth(outsider) idempotencyKey() contentType(ContentType.Application.Json) - setBody(CreateMessageRequest("", listOf(attachment.id.toString()))) - }.status) + setBody( + CreateMessageRequest( + text = "", + attachmentIds = listOf(attachment.id.toString()) + ) + ) + } + + assertEquals(HttpStatusCode.NotFound, rejectedByMembership.status) + val key = "file-$format" + suspend fun send(ids: List) = client.post("/api/v1/chats/${chat.id}/messages") { auth(bob) idempotencyKey(key) contentType(ContentType.Application.Json) - setBody(CreateMessageRequest("", ids)) + setBody(CreateMessageRequest(text = "", attachmentIds = ids)) } + val first = send(listOf(attachment.id.toString())) + assertEquals(HttpStatusCode.Created, first.status) + val message = first.body() + assertEquals(listOf(attachment.id), message.attachmentIds) assertEquals(message, send(listOf(attachment.id.toString())).body()) + val otherChat = client.createChat(bob, "other", listOf(bob.id)) - assertEquals(HttpStatusCode.NotFound, client.post("/api/v1/chats/${otherChat.id}/messages") { + val rejectedByChat = client.post("/api/v1/chats/${otherChat.id}/messages") { auth(bob) idempotencyKey() contentType(ContentType.Application.Json) - setBody(CreateMessageRequest("", listOf(attachment.id.toString()))) - }.status) + setBody( + CreateMessageRequest( + text = "", + attachmentIds = listOf(attachment.id.toString()) + ) + ) + } + + assertEquals(HttpStatusCode.NotFound, rejectedByChat.status) } } @Test - fun `invalid images filenames and byte limit return client errors`() = runApp { client -> + val user = client.register("upload") val chat = client.createChat(user, "files", listOf(user.id)) + suspend fun upload( bytes: ByteArray, type: String = "image/png", @@ -86,6 +161,7 @@ class AttachmentControllerTest { contentType(ContentType.parse(type)) setBody(bytes) } + val malformedMime = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.png") { auth(user) // Keep the invalid header in raw content so the client does not parse it first. @@ -96,30 +172,73 @@ class AttachmentControllerTest { override fun bytes() = image("png") }) } + assertEquals(HttpStatusCode.BadRequest, malformedMime.status) assertEquals(HttpStatusCode.UnsupportedMediaType, upload("not an image".toByteArray()).status) assertEquals(HttpStatusCode.UnsupportedMediaType, upload(image("png"), "image/jpeg").status) assertEquals(HttpStatusCode.UnsupportedMediaType, upload(image("png").copyOf(40)).status) - for (name in listOf("", " ", "\u2003", "../x.png", "a\\b.png", "a\n.png", "a\u0000.png", "x".repeat(256))) + + val corrupt = image("png") + var offset = 8 + + while (offset + 12 <= corrupt.size) { + val length = ByteBuffer.wrap(corrupt, offset, 4).int + val type = String(corrupt, offset + 4, 4, Charsets.US_ASCII) + + if (type == "IDAT") { + corrupt[offset + 8] = 0 // Invalid zlib header, but a correct PNG chunk CRC. + val crc = CRC32().apply { + update(corrupt, offset + 4, length + 4) + } + + ByteBuffer.wrap(corrupt, offset + 8 + length, 4) + .putInt(crc.value.toInt()) + break + } + + offset += length + 12 + } + + assertEquals(HttpStatusCode.UnsupportedMediaType, upload(corrupt).status) + + val invalidNames = listOf( + "", + " ", + "\u2003", + "../x.png", + "a\\b.png", + "a\n.png", + "a\u0000.png", + "x".repeat(256) + ) + + for (name in invalidNames) { assertEquals(HttpStatusCode.BadRequest, upload(image("png"), name = name).status) + } + assertEquals(HttpStatusCode.PayloadTooLarge, upload(ByteArray(10 * 1024 * 1024 + 1)).status) } @Test - fun `S3 outage fails upload download and readiness without losing the original`() = runApp { client -> + val user = client.register("outage") val chat = client.createChat(user, "files", listOf(user.id)) val bytes = image("png") + suspend fun upload() = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.png") { auth(user) contentType(ContentType.Image.PNG) setBody(bytes) } + val attachment = upload().body() + client.awaitReady(attachment.id, user) + val path = "/api/v1/attachments/${attachment.id}/content" val container = TestStorage.container container.dockerClient.pauseContainerCmd(container.containerId).exec() + try { assertEquals(HttpStatusCode.ServiceUnavailable, upload().status) assertEquals(HttpStatusCode.ServiceUnavailable, client.get(path) { auth(user) }.status) @@ -128,10 +247,27 @@ class AttachmentControllerTest { } finally { container.dockerClient.unpauseContainerCmd(container.containerId).exec() } + assertContentEquals(bytes, client.get(path) { auth(user) }.body()) assertEquals(HttpStatusCode.OK, client.get("/health/ready").status) } + private suspend fun HttpClient.awaitReady( + id: UUID, + user: TestUser + ): Attachment = withTimeout(30.seconds) { + val path = "/api/v1/attachments/$id" + var attachment = get(path) { auth(user) }.body() + + while (attachment.status != "ready") { + check(attachment.status != "failed") { "attachment failed: ${attachment.errorCode}" } + delay(50.milliseconds) + attachment = get(path) { auth(user) }.body() + } + + attachment + } + private fun image(format: String): ByteArray = ByteArrayOutputStream().use { ImageIO.write(BufferedImage(32, 20, BufferedImage.TYPE_INT_RGB), format, it) it.toByteArray() diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJobContractTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJobContractTest.kt new file mode 100644 index 00000000..5fcb07ca --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/dto/AttachmentJobContractTest.kt @@ -0,0 +1,54 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.SerializationException +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.jsonObject +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.time.Clock + +class AttachmentJobContractTest { + + @OptIn(ExperimentalSerializationApi::class) + @Test + fun `job envelope requires every field mandated by the public schema`() { + + val json = Json { + encodeDefaults = true + namingStrategy = JsonNamingStrategy.SnakeCase + } + val job = AttachmentJob( + jobId = UUID.randomUUID(), + attachmentId = UUID.randomUUID(), + createdAt = Clock.System.now() + ) + + val encoded = json.encodeToString(job) + + assertEquals(job, json.decodeFromString(encoded)) + + val fields = json.parseToJsonElement(encoded).jsonObject + val requiredFields = listOf( + "schema_version", + "job_id", + "type", + "attachment_id", + "pipeline_version", + "attempt", + "created_at" + ) + + for (key in requiredFields) { + val incomplete = JsonObject(fields.filterKeys { it != key }).toString() + + assertFailsWith("Missing required field: $key") { + json.decodeFromString(incomplete) + } + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt index 9f1a4eae..32d6ca01 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt @@ -1,25 +1,25 @@ package com.mtusi.messenger.main.infrastructure.services.attachment import com.mtusi.messenger.main.config.storage.SerialStorageConfig +import com.mtusi.messenger.main.config.worker.SerialWorkerConfig +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedAttachmentRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedUploadJournalRepository -import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository -import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.S3ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.ExposedAttachmentJobRepository import com.mtusi.messenger.main.infrastructure.repositories.chat.ExposedChatRepository +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.S3ObjectStorageService import com.mtusi.messenger.main.infrastructure.types.ApiException import com.mtusi.messenger.main.plugins.suspendedTransaction import com.mtusi.messenger.main.support.TestStorage 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.infrastructure.models.attachment.UploadJournalModel import com.mtusi.messenger.main.utils.exposed.operations.plus -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import org.jetbrains.exposed.v1.core.eq -import org.jetbrains.exposed.v1.core.longLiteral -import org.jetbrains.exposed.v1.datetime.CurrentTimestamp -import org.jetbrains.exposed.v1.jdbc.update +import com.mtusi.messenger.main.utils.computeSha256 import java.awt.image.BufferedImage import java.io.File import java.nio.file.Files @@ -31,23 +31,103 @@ import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertTrue +import kotlin.time.Clock +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.update +import org.koin.ktor.ext.get class AttachmentCleanupTest { + @Test + fun `expired failed attachments lose partially published files`() = runApp(startWorker = false) { client -> + + val user = client.register("failed_cleanup") + val chat = client.createChat( + creator = user, + title = "failed cleanup", + memberIds = listOf(user.id) + ) + + val storage = application.get() + val cleanup = application.get() + val bytes = byteArrayOf(1, 2, 3) + val attachment = Attachment( + id = UUID.randomUUID(), + chatId = chat.id, + ownerId = user.id, + filename = "partial.png", + contentType = "image/png", + sizeBytes = bytes.size.toLong(), + sha256 = bytes.computeSha256(), + createdAt = Clock.System.now(), + status = "failed", + errorCode = "retry_exhausted" + ) + + suspendedTransaction { + ExposedAttachmentRepository().create(attachment) + } + + storage.put( + id = attachment.id, + bytes = bytes, + contentType = attachment.contentType + ) + storage.putThumbnail(id = attachment.id, bytes = bytes) + + cleanup.cleanup() + + assertContentEquals(bytes, storage.get(attachment.id)) + assertContentEquals(bytes, storage.getThumbnail(attachment.id)) + + suspendedTransaction { + AttachmentModel.update(where = { AttachmentModel.id eq attachment.id }) { + it[updatedAt] = CurrentTimestamp + longLiteral(-90_000) + } + } + + cleanup.cleanup() + + assertTrue(storage.ready()) + assertFailsWith { storage.get(attachment.id) } + assertFailsWith { storage.getThumbnail(attachment.id) } + } + @Test fun `orphan cleanup respects grace and fences late publishers`() = runApp { client -> val user = client.register("cleanup") val chat = client.createChat(user, "cleanup", listOf(user.id)) + val journal = ExposedUploadJournalRepository() val attachments = ExposedAttachmentRepository() - val storage = S3ObjectStorageRepository(SerialStorageConfig( - endpoint = TestStorage.endpoint, - bucket = TestStorage.BUCKET, - accessKey = TestStorage.ACCESS_KEY, - secretKey = TestStorage.SECRET_KEY - )) - val cleanup = AttachmentCleanupService(journal, storage) + val storage = S3ObjectStorageService( + SerialStorageConfig( + endpoint = TestStorage.endpoint, + bucket = TestStorage.BUCKET, + accessKey = TestStorage.ACCESS_KEY, + secretKey = TestStorage.SECRET_KEY + ) + ) + + val workerConfig = SerialWorkerConfig() + val cleanup = AttachmentCleanupService( + journalRepository = journal, + objectStorageService = storage, + attachmentRepository = attachments, + config = workerConfig + ) + + val jobs = ExposedAttachmentJobRepository(attachments) + val broker = application.get() val file = withContext(Dispatchers.IO) { Files.createTempFile("cleanup-test-", ".png") @@ -58,28 +138,78 @@ class AttachmentCleanupTest { } var orphanId: UUID? = null - val failingStorage = object : ObjectStorageRepository by storage { - override suspend fun put(id: UUID, file: File, contentType: String) { - storage.put(id, file, contentType) + val failingStorage = object : ObjectStorageService by storage { + override suspend fun putStaging( + id: UUID, + file: File, + contentType: String + ) { + storage.putStaging( + id = id, + file = file, + contentType = contentType + ) + orphanId = id error("injected failure after PUT, before metadata COMMIT") } } + try { - val failingService = DefaultAttachmentService(journal, attachments, ExposedChatRepository(), failingStorage) + val failingService = DefaultAttachmentService( + journalRepository = journal, + attachmentRepository = attachments, + chatRepository = ExposedChatRepository(), + objectStorageService = failingStorage, + jobRepository = jobs, + jobBrokerService = broker + ) assertFailsWith { - failingService.upload(chat.id, user.id, "file.png", "image/png", file) + failingService.upload( + chatId = chat.id, + actorId = user.id, + filename = "file.png", + contentType = "image/png", + file = file + ) } val id = requireNotNull(orphanId) - assertEquals(null, suspendedTransaction { attachments.findById(id) }) - assertContentEquals(file.readBytes(), storage.get(id)) - cleanup.cleanup() - assertContentEquals(file.readBytes(), storage.get(id), "fresh upload must survive grace") - val service = DefaultAttachmentService(journal, attachments, ExposedChatRepository(), storage) - val published = service.upload(chat.id, user.id, "ready.png", "image/png", file) + assertEquals(null, suspendedTransaction { attachments.findById(id) }) + assertContentEquals(file.readBytes(), storage.getStaging(id)) + + cleanup.cleanup() + + assertContentEquals( + file.readBytes(), + storage.getStaging(id), + "fresh upload must survive grace" + ) + + val service = DefaultAttachmentService( + journalRepository = journal, + attachmentRepository = attachments, + chatRepository = ExposedChatRepository(), + objectStorageService = storage, + jobRepository = jobs, + jobBrokerService = broker + ) + val published = service.upload( + chatId = chat.id, + actorId = user.id, + filename = "ready.png", + contentType = "image/png", + file = file + ) + + withTimeout(30.seconds) { + while (suspendedTransaction { attachments.findById(published.id)?.status } != "ready") { + delay(50.milliseconds) + } + } + suspendedTransaction { UploadJournalModel.update({ UploadJournalModel.id eq id }) { it[expiresAt] = CurrentTimestamp + longLiteral(-1) @@ -87,14 +217,22 @@ class AttachmentCleanupTest { } } - val unavailableStorage = object : ObjectStorageRepository by storage { + val unavailableStorage = object : ObjectStorageService by storage { override suspend fun delete(id: UUID) { error("injected S3 delete outage") } } - assertEquals(0, AttachmentCleanupService(journal, unavailableStorage).cleanup()) - assertContentEquals(file.readBytes(), storage.get(id)) + val unavailableCleanup = AttachmentCleanupService( + journalRepository = journal, + objectStorageService = unavailableStorage, + attachmentRepository = attachments, + config = workerConfig + ) + + assertEquals(0, unavailableCleanup.cleanup()) + assertContentEquals(file.readBytes(), storage.getStaging(id)) + suspendedTransaction { UploadJournalModel.update({ UploadJournalModel.id eq id }) { it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) @@ -102,12 +240,12 @@ class AttachmentCleanupTest { } assertTrue(cleanup.cleanup() >= 1) - assertFailsWith { storage.get(id) } + assertFailsWith { storage.getStaging(id) } assertFalse(suspendedTransaction { journal.lockForCompletion(id) }) assertContentEquals(file.readBytes(), storage.get(published.id)) // A process suspended longer than grace can complete a late PUT, but cannot publish metadata. - storage.put(id, file, "image/png") + storage.putStaging(id, file, "image/png") suspendedTransaction { UploadJournalModel.update({ UploadJournalModel.id eq id }) { it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) @@ -115,7 +253,7 @@ class AttachmentCleanupTest { } assertTrue(cleanup.cleanup() >= 1) - assertFailsWith { storage.get(id) } + assertFailsWith { storage.getStaging(id) } assertContentEquals(file.readBytes(), storage.get(published.id)) } finally { file.delete() diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt new file mode 100644 index 00000000..241d2653 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt @@ -0,0 +1,471 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.config.worker.SerialWorkerConfig +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.dto.ProcessedImage +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobModel +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.JobClaim +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService +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.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 +import com.mtusi.messenger.main.utils.exposed.operations.plus +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.Dispatchers +import kotlinx.coroutines.cancelAndJoin +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout +import java.awt.image.BufferedImage +import java.io.ByteArrayOutputStream +import java.util.UUID +import java.util.concurrent.atomic.AtomicInteger +import javax.imageio.ImageIO +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.update +import org.koin.ktor.ext.get + +class AttachmentPipelineTest { + + @Test + fun `stopped worker leaves a durable queued attachment and redelivery preserves its result`() = runApp(startWorker = false) { client -> + + val alice = client.register("pipeline") + val bob = client.register("reader") + val chat = client.createChat( + creator = alice, + title = "pipeline", + memberIds = listOf(alice.id, bob.id) + ) + val original = image() + + val upload = client.post("/api/v1/chats/${chat.id}/attachments?filename=sample.png") { + auth(alice) + contentType(ContentType.Image.PNG) + setBody(original) + } + + assertEquals(HttpStatusCode.Accepted, upload.status) + + val queued = upload.body() + + assertEquals("queued", queued.status) + assertEquals(emptyList(), queued.variants) + assertEquals(null, queued.errorCode) + assertEquals(original.computeSha256(), queued.sha256) + + val path = "/api/v1/attachments/${queued.id}" + val metadata = client.get(path) { auth(bob) }.body() + val unavailableOriginal = client.get("$path/content") { auth(bob) } + val unavailableThumbnail = client.get("$path/content?variant=thumbnail") { auth(bob) } + + assertEquals("queued", metadata.status) + assertEquals(HttpStatusCode.Conflict, unavailableOriginal.status) + assertEquals(HttpStatusCode.Conflict, unavailableThumbnail.status) + + val sent = client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody( + CreateMessageRequest( + text = "", + attachmentIds = listOf(queued.id.toString()) + ) + ) + } + + assertEquals(HttpStatusCode.Created, sent.status) + + val job = jobFor(queued.id) + val processing = application.get() + + assertTrue(processing.process(job)) + + val ready = client.get(path) { auth(bob) }.body() + + assertEquals("ready", ready.status) + assertEquals(null, ready.errorCode) + + val variant = ready.variants.single() + + assertEquals("thumbnail", variant.name) + assertEquals("image/jpeg", variant.contentType) + assertEquals(256, variant.width) + assertEquals(160, variant.height) + + val thumbnail = client.get("$path/content?variant=thumbnail") { auth(bob) }.body() + + assertEquals(variant.sizeBytes, thumbnail.size.toLong()) + assertEquals(variant.sha256, thumbnail.computeSha256()) + + val decoded = withContext(Dispatchers.IO) { + thumbnail.inputStream().use { input -> + ImageIO.read(input) + } + } + val downloaded = client.get("$path/content") { auth(bob) }.body() + + assertEquals(256, decoded.width) + assertEquals(160, decoded.height) + assertContentEquals(original, downloaded) + + assertTrue( + processing.process(job), + "a ready redelivery must be acknowledged without another effect" + ) + + val replayedMetadata = client.get(path) { auth(bob) }.body() + val replayedThumbnail = client.get("$path/content?variant=thumbnail") { auth(bob) }.body() + val attempts = suspendedTransaction { + AttachmentJobModel + .selectAll() + .where(AttachmentJobModel.id eq job.jobId) + .single()[AttachmentJobModel.attempts] + } + + assertEquals(ready, replayedMetadata) + assertContentEquals(thumbnail, replayedThumbnail) + assertEquals(1, attempts) + } + + @Test + fun `attempts are bounded and failed attachments reject content and message links`() = runApp(startWorker = false) { client -> + + val user = client.register("attempts") + val chat = client.createChat( + creator = user, + title = "attempts", + memberIds = listOf(user.id) + ) + + val queued = client.post("/api/v1/chats/${chat.id}/attachments?filename=sample.png") { + auth(user) + contentType(ContentType.Image.PNG) + setBody(image()) + }.body() + + val path = "/api/v1/attachments/${queued.id}" + val key = UUID.randomUUID().toString() + + suspend fun send(key: String) = client.post("/api/v1/chats/${chat.id}/messages") { + auth(user) + idempotencyKey(key) + contentType(ContentType.Application.Json) + setBody( + CreateMessageRequest( + text = "", + attachmentIds = listOf(queued.id.toString()) + ) + ) + } + + val sent = send(key) + + assertEquals(HttpStatusCode.Created, sent.status) + + val message = sent.body() + val job = jobFor(queued.id) + val jobs = application.get() + val firstToken = UUID.randomUUID() + + val firstClaim = suspendedTransaction { + jobs.claim( + job = job, + token = firstToken, + maxAttempts = 2, + leaseSeconds = 60 + ) + } + val processing = client.get(path) { auth(user) }.body() + val competingClaim = suspendedTransaction { + jobs.claim( + job = job, + token = UUID.randomUUID(), + maxAttempts = 2, + leaseSeconds = 60 + ) + } + + assertEquals(JobClaim.Acquired(1), firstClaim) + assertEquals("processing", processing.status) + assertEquals(JobClaim.Busy, competingClaim) + + val terminalFirstAttempt = suspendedTransaction { + jobs.fail( + job = job, + token = firstToken, + errorCode = "storage_unavailable", + permanent = false, + maxAttempts = 2 + ) + } + val pendingRetry = client.get(path) { auth(user) }.body() + + assertFalse(terminalFirstAttempt) + assertEquals("queued", pendingRetry.status) + assertNull(pendingRetry.errorCode) + + val pendingJob = suspendedTransaction { + AttachmentJobModel + .selectAll() + .where(AttachmentJobModel.id eq job.jobId) + .single() + } + + assertEquals("storage_unavailable", pendingJob[AttachmentJobModel.lastErrorCode]) + assertNotNull(pendingJob[AttachmentJobModel.nextAttemptAt]) + + val retry = job.copy(attempt = 2) + val secondToken = UUID.randomUUID() + val staleClaim = suspendedTransaction { + jobs.claim( + job = job, + token = UUID.randomUUID(), + maxAttempts = 2, + leaseSeconds = 60 + ) + } + val retryClaim = suspendedTransaction { + jobs.claim( + job = retry, + token = secondToken, + maxAttempts = 2, + leaseSeconds = 60 + ) + } + + assertEquals(JobClaim.Retry(2), staleClaim) + assertEquals(JobClaim.Acquired(2), retryClaim) + + val claimedJob = suspendedTransaction { + AttachmentJobModel + .selectAll() + .where(AttachmentJobModel.id eq job.jobId) + .single() + } + + assertNull(claimedJob[AttachmentJobModel.nextAttemptAt]) + + val terminalRetry = suspendedTransaction { + jobs.fail( + job = retry, + token = secondToken, + errorCode = "retry_exhausted", + permanent = false, + maxAttempts = 2 + ) + } + + assertTrue(terminalRetry) + + val failed = client.get(path) { auth(user) }.body() + + assertEquals("failed", failed.status) + assertEquals("retry_exhausted", failed.errorCode) + assertEquals(emptyList(), failed.variants) + + val replay = send(key) + + assertEquals(HttpStatusCode.Created, replay.status) + assertEquals(message, replay.body(), "failure after sending must not invalidate idempotent replay") + + val unavailableContent = client.get("$path/content") { auth(user) } + val rejectedMessage = send(UUID.randomUUID().toString()) + + assertEquals(HttpStatusCode.Conflict, unavailableContent.status) + assertEquals(HttpStatusCode.Conflict, rejectedMessage.status) + + assertTrue(application.get().process(job)) + assertEquals(failed, client.get(path) { auth(user) }.body()) + } + + @Test + fun `busy redelivery lets the next attachment finish before lease expiration`() = runApp(startWorker = false) { client -> + + val user = client.register("busyredelivery") + val chat = client.createChat( + creator = user, + title = "busy redelivery", + memberIds = listOf(user.id) + ) + + suspend fun upload(name: String): Attachment = + client.post("/api/v1/chats/${chat.id}/attachments?filename=$name.png") { + auth(user) + contentType(ContentType.Image.PNG) + setBody(image()) + }.body() + + val busy = upload("busy") + val busyJob = jobFor(busy.id) + val jobs = application.get() + + val claim = suspendedTransaction { + jobs.claim( + job = busyJob, + token = UUID.randomUUID(), + maxAttempts = 5, + leaseSeconds = 60 + ) + } + + assertEquals(JobClaim.Acquired(1), claim) + + val next = upload("next") + val broker = application.get() + val worker = application.get() + + suspend fun metadata(attachment: Attachment): Attachment { + return client.get("/api/v1/attachments/${attachment.id}") { auth(user) } + .body() + } + + suspend fun awaitReady(attachment: Attachment) { + withTimeout(15.seconds) { + while (metadata(attachment).status != "ready") { + delay(50.milliseconds) + } + } + } + + coroutineScope { + val consumer = launch { + broker.consume(worker::process) + } + + try { + awaitReady(next) + assertEquals("processing", metadata(busy).status) + + suspendedTransaction { + AttachmentJobModel.update(where = { AttachmentJobModel.id eq busyJob.jobId }) { + it[leaseUntil] = CurrentTimestamp + longLiteral(-1) + } + } + + awaitReady(busy) + } finally { + consumer.cancelAndJoin() + } + } + } + + @Test + fun `worker distinguishes permanent image errors from bounded transient failures`() = runApp(startWorker = false) { client -> + + val user = client.register("workererrors") + val chat = client.createChat( + creator = user, + title = "errors", + memberIds = listOf(user.id) + ) + + for (permanent in listOf(false, true)) { + val queued = client.post("/api/v1/chats/${chat.id}/attachments?filename=sample.png") { + auth(user) + contentType(ContentType.Image.PNG) + setBody(image()) + }.body() + + val path = "/api/v1/attachments/${queued.id}" + val job = jobFor(queued.id) + val calls = AtomicInteger() + + val processor = object : ImageProcessingService { + override suspend fun thumbnail( + bytes: ByteArray, + declaredType: String + ): ProcessedImage { + calls.incrementAndGet() + + if (permanent) { + throw ApiException(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "invalid image") + } + + error("injected temporary processing failure") + } + } + + val service = DefaultAttachmentProcessingService( + jobRepository = application.get(), + attachmentRepository = application.get(), + objectStorageService = application.get(), + imageProcessingService = processor, + jobBrokerService = application.get(), + config = SerialWorkerConfig(maxAttempts = 2) + ) + + assertTrue(service.process(job)) + + if (!permanent) { + val pendingRetry = client.get(path) { auth(user) }.body() + + assertEquals("queued", pendingRetry.status) + assertTrue(service.process(job.copy(attempt = 2))) + } + + val failed = client.get(path) { auth(user) }.body() + val expectedError = if (permanent) "invalid_image" else "retry_exhausted" + val expectedCalls = if (permanent) 1 else 2 + + assertEquals("failed", failed.status) + assertEquals(expectedError, failed.errorCode) + assertEquals(expectedCalls, calls.get()) + + assertTrue(service.process(job)) + assertEquals(expectedCalls, calls.get(), "terminal redelivery must not run the processor again") + } + } + + private suspend fun jobFor(attachmentId: UUID): AttachmentJob = suspendedTransaction { + val row = AttachmentJobModel + .selectAll() + .where(AttachmentJobModel.attachmentId eq attachmentId) + .single() + + AttachmentJob( + jobId = row[AttachmentJobModel.id].value, + attachmentId = attachmentId, + createdAt = row[AttachmentJobModel.createdAt], + pipelineVersion = row[AttachmentJobModel.pipelineVersion] + ) + } + + private fun image(): ByteArray = ByteArrayOutputStream().use { output -> + ImageIO.write(BufferedImage(320, 200, BufferedImage.TYPE_INT_RGB), "png", output) + output.toByteArray() + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt new file mode 100644 index 00000000..65783a11 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt @@ -0,0 +1,284 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.drew.imaging.jpeg.JpegMetadataReader +import com.drew.metadata.exif.ExifIFD0Directory +import com.drew.metadata.exif.ExifReader +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import java.awt.Color +import java.awt.image.BufferedImage +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.DataOutputStream +import java.nio.ByteBuffer +import java.nio.ByteOrder +import java.util.zip.CRC32 +import javax.imageio.ImageIO +import kotlin.math.abs +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class ImageProcessingServiceTest { + + private val service = DefaultImageProcessingService() + private val colors = listOf(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW) + + @Test + fun `thumbnail fits the box floors dimensions and never enlarges the source`() = runBlocking { + + val cases = listOf( + Triple(320 to 200, 256 to 160, "png"), + Triple(200 to 320, 160 to 256, "jpeg"), + Triple(501 to 300, 256 to 153, "png"), + Triple(63 to 41, 63 to 41, "jpeg"), + Triple(600 to 1, 256 to 1, "png"), + Triple(1 to 600, 1 to 256, "png") + ) + + for ((size, expected, format) in cases) { + val source = BufferedImage(size.first, size.second, BufferedImage.TYPE_INT_RGB) + val bytes = encode(source, format) + val original = bytes.copyOf() + + val thumbnail = service.thumbnail(bytes, "image/$format") + val decoded = decode(thumbnail.bytes) + + assertEquals(expected.first, thumbnail.width) + assertEquals(expected.second, thumbnail.height) + assertEquals(expected.first, decoded.width) + assertEquals(expected.second, decoded.height) + assertEquals("image/jpeg", thumbnail.contentType) + assertEquals(0xff, thumbnail.bytes[0].toInt() and 0xff) + assertEquals(0xd8, thumbnail.bytes[1].toInt() and 0xff) + assertContentEquals(original, bytes, "thumbnail generation must not modify the original") + } + } + + @Test + fun `transparent and semitransparent pixels are composited on white`() = runBlocking { + + val source = BufferedImage(80, 40, BufferedImage.TYPE_INT_ARGB) + val graphics = source.createGraphics() + + try { + graphics.color = Color(255, 0, 0, 128) + graphics.fillRect(0, 0, 40, 40) + } finally { + graphics.dispose() + } + + val bytes = encode(source, "png") + val thumbnail = service.thumbnail(bytes, "image/png") + val decoded = decode(thumbnail.bytes) + + assertColor(Color.WHITE, Color(decoded.getRGB(60, 20))) + assertColor(Color(255, 127, 127), Color(decoded.getRGB(20, 20))) + assertFalse(decoded.colorModel.hasAlpha()) + } + + @Test + fun `all JPEG EXIF orientations rotate or mirror pixels and strip orientation from the thumbnail`() = runBlocking { + + val encoded = encode(quadrants(), "jpeg") + val expectedCorners = listOf( + listOf(0, 1, 2, 3), + listOf(1, 0, 3, 2), + listOf(3, 2, 1, 0), + listOf(2, 3, 0, 1), + listOf(0, 2, 1, 3), + listOf(2, 0, 3, 1), + listOf(3, 1, 2, 0), + listOf(1, 3, 0, 2) + ) + + for (orientation in 1..8) { + val bytes = withJpegExif(encoded, orientation) + val original = bytes.copyOf() + val thumbnail = service.thumbnail(bytes, "image/jpeg") + val decoded = decode(thumbnail.bytes) + + assertEquals(if (orientation >= 5) 40 else 80, decoded.width) + assertEquals(if (orientation >= 5) 80 else 40, decoded.height) + assertCorners(decoded, expectedCorners[orientation - 1]) + assertContentEquals(original, bytes) + + val metadata = ByteArrayInputStream(thumbnail.bytes).use { + JpegMetadataReader.readMetadata(it, listOf(ExifReader())) + } + + assertNull(metadata.getFirstDirectoryOfType(ExifIFD0Directory::class.java)) + } + } + + @Test + fun `PNG EXIF orientation is applied before resizing`() = runBlocking { + + val source = BufferedImage(320, 200, BufferedImage.TYPE_INT_RGB) + val bytes = withPngExif(encode(source, "png"), 6) + val thumbnail = service.thumbnail(bytes, "image/png") + + assertEquals(160, thumbnail.width) + assertEquals(256, thumbnail.height) + + val orientedQuadrants = withPngExif(encode(quadrants(), "png"), 7) + val small = service.thumbnail(orientedQuadrants, "image/png") + val decoded = decode(small.bytes) + + assertCorners(decoded, listOf(3, 1, 2, 0)) + } + + @Test + fun `missing or unknown EXIF orientation preserves the normal image`() = runBlocking { + + val bytes = encode(quadrants(), "jpeg") + + for (original in listOf(bytes, withJpegExif(bytes, 9))) { + val thumbnail = service.thumbnail(original, "image/jpeg") + val decoded = decode(thumbnail.bytes) + + assertEquals(80, thumbnail.width) + assertEquals(40, thumbnail.height) + assertCorners(decoded, listOf(0, 1, 2, 3)) + } + } + + @Test + fun `invalid images and MIME mismatches are rejected before publishing a thumbnail`() = runBlocking { + + val invalidImages = listOf( + "not an image".toByteArray() to "image/png", + encode(quadrants(), "png") to "image/jpeg", + encode(quadrants(), "jpeg").copyOf(40) to "image/jpeg" + ) + + for ((bytes, type) in invalidImages) { + val failure = assertFailsWith { + service.thumbnail(bytes, type) + } + + assertEquals(ErrorCode.UNSUPPORTED_MEDIA_TYPE, failure.code) + } + } + + private fun quadrants(): BufferedImage { + + val image = BufferedImage(80, 40, BufferedImage.TYPE_INT_RGB) + val graphics = image.createGraphics() + + try { + colors.forEachIndexed { index, color -> + graphics.color = color + graphics.fillRect((index % 2) * 40, (index / 2) * 20, 40, 20) + } + } finally { + graphics.dispose() + } + + return image + } + + private fun assertCorners( + image: BufferedImage, + expected: List + ) { + + expected.forEachIndexed { index, color -> + val x = if (index % 2 == 0) image.width / 4 else image.width * 3 / 4 + val y = if (index / 2 == 0) image.height / 4 else image.height * 3 / 4 + assertColor(colors[color], Color(image.getRGB(x, y))) + } + } + + private fun assertColor( + expected: Color, + actual: Color + ) { + assertTrue(abs(expected.red - actual.red) <= 12, "red: expected $expected, got $actual") + assertTrue(abs(expected.green - actual.green) <= 12, "green: expected $expected, got $actual") + assertTrue(abs(expected.blue - actual.blue) <= 12, "blue: expected $expected, got $actual") + } + + private fun encode( + image: BufferedImage, + format: String + ): ByteArray = ByteArrayOutputStream().use { output -> + check(ImageIO.write(image, format, output)) + output.toByteArray() + } + + private suspend fun decode( + bytes: ByteArray + ): BufferedImage = withContext(Dispatchers.IO) { + ByteArrayInputStream(bytes).use { input -> + ImageIO.read(input) + } + } + + private fun tiff(orientation: Int): ByteArray { + return ByteBuffer.allocate(26) + .order(ByteOrder.LITTLE_ENDIAN) + .put('I'.code.toByte()) + .put('I'.code.toByte()) + .putShort(42) + .putInt(8) + .putShort(1) + .putShort(0x0112) + .putShort(3) + .putInt(1) + .putShort(orientation.toShort()) + .putShort(0) + .putInt(0) + .array() + } + + private fun withJpegExif( + bytes: ByteArray, + orientation: Int + ): ByteArray { + + val exif = byteArrayOf(69, 120, 105, 102, 0, 0) + tiff(orientation) + return ByteArrayOutputStream().use { output -> + output.write(bytes, 0, 2) + DataOutputStream(output).apply { + writeShort(0xffe1) + writeShort(exif.size + 2) + write(exif) + write(bytes, 2, bytes.size - 2) + } + output.toByteArray() + } + } + + private fun withPngExif( + bytes: ByteArray, + orientation: Int + ): ByteArray { + + val type = "eXIf".toByteArray(Charsets.US_ASCII) + val data = tiff(orientation) + val crc = CRC32().apply { + update(type) + update(data) + } + + return ByteArrayOutputStream().use { output -> + output.write(bytes, 0, 33) + DataOutputStream(output).apply { + writeInt(data.size) + write(type) + write(data) + writeInt(crc.value.toInt()) + write(bytes, 33, bytes.size - 33) + } + output.toByteArray() + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt new file mode 100644 index 00000000..ca127059 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt @@ -0,0 +1,129 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.config.storage.SerialStorageConfig +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.infrastructure.services.attachment.storage.S3ObjectStorageService +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.support.TestStorage +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.utils.computeSha256 +import io.ktor.client.request.get +import io.ktor.http.HttpStatusCode +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import org.flywaydb.core.Flyway +import org.koin.ktor.ext.get +import org.testcontainers.containers.PostgreSQLContainer +import java.awt.image.BufferedImage +import java.io.ByteArrayOutputStream +import java.sql.DriverManager +import java.util.UUID +import javax.imageio.ImageIO +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.time.Duration.Companion.milliseconds + +class LegacyAttachmentMigrationTest { + + @Test + fun `migration schedules a thumbnail for an existing lab4 original`() { + + val id = UUID.randomUUID() + val original = ByteArrayOutputStream().use { output -> + ImageIO.write(BufferedImage(320, 200, BufferedImage.TYPE_INT_RGB), "png", output) + output.toByteArray() + } + + TestDatabase.create().use { database -> + database.start() + TestDatabase.awaitConnectable(database) + seedLab4Attachment(database, id, original) + + val storage = S3ObjectStorageService( + SerialStorageConfig( + endpoint = TestStorage.endpoint, + bucket = TestStorage.BUCKET, + accessKey = TestStorage.ACCESS_KEY, + secretKey = TestStorage.SECRET_KEY + ) + ) + + runBlocking { + storage.put( + id = id, + bytes = original, + contentType = "image/png" + ) + } + + runApp(database = database) { client -> + assertEquals(HttpStatusCode.OK, client.get("/health/live").status) + + val attachments = application.get() + val ready = withTimeout(30_000.milliseconds) { + var attachment = suspendedTransaction { checkNotNull(attachments.findById(id)) } + + while (attachment.status != "ready") { + check(attachment.status != "failed") { attachment.errorCode.orEmpty() } + delay(50.milliseconds) + attachment = suspendedTransaction { checkNotNull(attachments.findById(id)) } + } + + attachment + } + + val variant = ready.variants.single() + val thumbnail = storage.getThumbnail(id) + + assertContentEquals(original, storage.get(id)) + assertEquals("thumbnail", variant.name) + assertEquals(256, variant.width) + assertEquals(160, variant.height) + assertEquals(variant.sha256, thumbnail.computeSha256()) + } + } + } + + private fun seedLab4Attachment( + database: PostgreSQLContainer<*>, + id: UUID, + original: ByteArray + ) { + + Flyway.configure() + .dataSource(database.jdbcUrl, database.username, database.password) + .locations("classpath:migrations") + .target("5") + .load() + .migrate() + + DriverManager.getConnection(database.jdbcUrl, database.username, database.password).use { connection -> + connection.prepareStatement( + """ + WITH owner AS ( + INSERT INTO users VALUES (?, 'legacy', 'Legacy', 'unused', CURRENT_TIMESTAMP) + RETURNING id + ), chat AS ( + INSERT INTO chats VALUES (?, 'Legacy', CURRENT_TIMESTAMP) + RETURNING id + ) + INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, created_at) + SELECT ?, chat.id, owner.id, 'legacy.png', 'image/png', ?, ?, CURRENT_TIMESTAMP + FROM owner CROSS JOIN chat + """.trimIndent() + ).use { insert -> + insert.setObject(1, UUID.randomUUID()) + insert.setObject(2, UUID.randomUUID()) + insert.setObject(3, id) + insert.setLong(4, original.size.toLong()) + insert.setString(5, original.computeSha256()) + assertEquals(1, insert.executeUpdate()) + } + } + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerServiceTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerServiceTest.kt new file mode 100644 index 00000000..f496bad8 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/RabbitMqJobBrokerServiceTest.kt @@ -0,0 +1,137 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment.jobs + +import com.mtusi.messenger.main.config.broker.SerialBrokerConfig +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import com.mtusi.messenger.main.support.TestBroker +import com.rabbitmq.client.ConnectionFactory +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.cancelAndJoin +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.time.Clock +import kotlin.time.Duration.Companion.milliseconds + +class RabbitMqJobBrokerServiceTest { + + @Test + fun `confirmed job survives broker restart and negative acknowledgement preserves its identity`() = runBlocking { + + val config = TestBroker.config().copy( + connectionTimeoutMs = 1000, + operationTimeoutMs = 2000 + ) + val job = AttachmentJob( + jobId = UUID.randomUUID(), + attachmentId = UUID.randomUUID(), + createdAt = Clock.System.now() + ) + val marker = job.copy( + jobId = UUID.randomUUID(), + attachmentId = UUID.randomUUID() + ) + val publisher = RabbitMqJobBrokerService(config) + + try { + // No producer remains to republish after restart: the next delivery must + // come from the broker's persisted data. + publisher.publish(job) + } finally { + publisher.close() + } + + val recoveredConfig = withTimeout(30_000.milliseconds) { + val container = TestBroker.container + val containerId = container.containerId + + withContext(Dispatchers.IO) { + container.dockerClient.restartContainerCmd(containerId).exec() + } + + assertEquals(containerId, TestBroker.container.containerId) + + // Docker may assign another ephemeral host port when the container restarts. + val recoveredConfig = config.copy(port = TestBroker.port) + val broker = RabbitMqJobBrokerService(recoveredConfig) + + try { + val firstDelivery = CompletableDeferred() + val redelivery = CompletableDeferred() + val markerDelivery = CompletableDeferred() + + val consumer = launch { + broker.consume { delivered -> + when { + delivered.jobId == marker.jobId -> { + markerDelivery.complete(delivered) + awaitCancellation() + } + + firstDelivery.complete(delivered) -> false + + else -> { + redelivery.complete(delivered) + true + } + } + } + } + + try { + assertEquals(job, firstDelivery.await()) + assertEquals(job, redelivery.await()) + + // The marker synchronizes with the receive loop and stays unacknowledged. + broker.publish(marker) + assertEquals(marker, markerDelivery.await()) + } finally { + consumer.cancelAndJoin() + } + } finally { + broker.close() + } + + recoveredConfig + } + + // The closed channel must requeue the marker and only the marker. + assertEquals( + listOf(marker.jobId.toString()), + drainMessageIds(recoveredConfig) + ) + } + + private suspend fun drainMessageIds( + config: SerialBrokerConfig + ): List = withContext(Dispatchers.IO) { + + val factory = ConnectionFactory().apply { + host = config.host + port = config.port + username = config.username + password = requireNotNull(config.password) + connectionTimeout = config.connectionTimeoutMs + handshakeTimeout = config.operationTimeoutMs + channelRpcTimeout = config.operationTimeoutMs + } + + factory.newConnection().use { connection -> + connection.createChannel().use { channel -> + buildList { + while (true) { + val delivery = channel.basicGet(config.queue, true) ?: break + + add(delivery.props.messageId) + } + } + } + } + } + +} 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 0ba00322..d630f0df 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt @@ -6,6 +6,7 @@ import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateU import com.mtusi.messenger.main.infrastructure.dto.Chat import com.mtusi.messenger.main.infrastructure.dto.User import com.mtusi.messenger.main.module +import com.mtusi.messenger.main.plugins.configureAttachmentWorker import com.typesafe.config.ConfigFactory import io.ktor.client.HttpClient import io.ktor.client.call.body @@ -62,6 +63,12 @@ private fun testConfig( ) = HoconApplicationConfig( ConfigFactory.parseMap( mapOf( + "broker.host" to TestBroker.host, + "broker.port" to TestBroker.port, + "broker.username" to TestBroker.USERNAME, + "broker.password" to TestBroker.PASSWORD, + "broker.queue" to "test-${UUID.randomUUID()}", + "storage.endpoint" to TestStorage.endpoint, "storage.bucket" to TestStorage.BUCKET, "storage.accessKey" to TestStorage.ACCESS_KEY, @@ -100,19 +107,35 @@ fun runApp( redis: RedisEndpoint = TestRedis.endpoint(), idempotencyTtlSeconds: Long = 86400, sessionRevokeTimeoutMs: Long = 8000, + startWorker: Boolean = true, block: suspend ApplicationTestBuilder.(HttpClient) -> Unit ) = testApplication { + environment { - config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds, sessionRevokeTimeoutMs) + config = testConfig( + sessionTtlSeconds = sessionTtlSeconds, + database = database, + databaseConnectionTimeoutMs = databaseConnectionTimeoutMs, + redis = redis, + idempotencyTtlSeconds = idempotencyTtlSeconds, + sessionRevokeTimeoutMs = sessionRevokeTimeoutMs + ) } + application { module() + + if (startWorker) { + configureAttachmentWorker() + } } + val client = createClient { install(ContentNegotiation) { json(testJson) } } + block(client) } diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestBroker.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestBroker.kt new file mode 100644 index 00000000..0652e4dc --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestBroker.kt @@ -0,0 +1,52 @@ +package com.mtusi.messenger.main.support + +import com.github.dockerjava.api.model.ExposedPort +import com.mtusi.messenger.main.config.broker.SerialBrokerConfig +import org.testcontainers.containers.GenericContainer +import org.testcontainers.containers.wait.strategy.Wait +import java.util.UUID + +object TestBroker { + + const val USERNAME = "test-worker" + const val PASSWORD = "test-broker-password" + + val container by lazy { + GenericContainer("rabbitmq@sha256:cd4009416f83d7e5895020cd71b0963f3b8a4dfc3de74bbb6fb3bfe5f81f01e0") + .withEnv("RABBITMQ_DEFAULT_USER", USERNAME) + .withEnv("RABBITMQ_DEFAULT_PASS", PASSWORD) + .withExposedPorts(5672) + .waitingFor(Wait.forLogMessage(".*Server startup complete.*\\n", 1)) + .also { + it.start() + } + } + + val host: String + get() = container.host + + val port: Int + get() { + val bindings = container.dockerClient + .inspectContainerCmd(container.containerId) + .exec() + .networkSettings + .ports + .bindings + + return bindings.getValue(ExposedPort.tcp(5672)) + .first() + .hostPortSpec + .toInt() + } + + fun config(queue: String = "attachments-test-${UUID.randomUUID()}"): SerialBrokerConfig { + return SerialBrokerConfig( + host = host, + port = port, + username = USERNAME, + password = PASSWORD, + queue = queue + ) + } +} -- 2.54.0 From be2abbb7344ca6527066a2f6aa52159740b20e27 Mon Sep 17 00:00:00 2001 From: Mraksimus Date: Thu, 19 Nov 2026 16:48:52 +0300 Subject: [PATCH 3/3] Completed for a grade of four --- Dockerfile | 2 +- REPORT.md | 174 ++++------ build.gradle.kts | 1 + compose.yaml | 4 +- evidence/attachment-failover.txt | 4 +- evidence/compose.txt | 24 +- evidence/gradle-tests.txt | 8 +- evidence/level4-recovery.txt | 5 + evidence/persistence.txt | 34 +- evidence/s3-permissions.txt | 10 +- evidence/smoke.txt | 2 +- evidence/worker-recovery.txt | 6 +- scripts/attachment-level4-recovery.py | 187 +++++++++++ scripts/attachment-worker-recovery.py | 2 +- scripts/check-s3-permissions.sh | 2 +- .../kotlin/com/mtusi/messenger/main/App.kt | 5 + .../controllers/health/HealthController.kt | 8 +- .../models/attachment/AttachmentJobModel.kt | 2 +- .../attachment/AttachmentJobReplayModel.kt | 18 ++ .../models/attachment/AttachmentModel.kt | 1 + .../attachment/AttachmentOutboxModel.kt | 25 ++ .../attachment/AttachmentRepository.kt | 11 +- .../attachment/ExposedAttachmentRepository.kt | 53 +-- .../jobs/AttachmentJobRepository.kt | 42 ++- .../jobs/AttachmentOutboxRepository.kt | 33 ++ .../jobs/ExposedAttachmentJobRepository.kt | 297 +++++++++++------ .../jobs/ExposedAttachmentOutboxRepository.kt | 110 +++++++ .../attachment/AttachmentCleanupService.kt | 22 +- .../attachment/AttachmentImageProcessor.kt | 137 ++++++++ .../DefaultAttachmentProcessingService.kt | 87 ++--- .../attachment/DefaultAttachmentService.kt | 54 ++-- .../DefaultImageProcessingService.kt | 301 +++++++++++++----- .../services/attachment/ImageProcess.kt | 64 ++++ .../attachment/ImageProcessingService.kt | 4 +- .../jobs/AttachmentOutboxDispatchService.kt | 49 +++ .../jobs/LegacyAttachmentDispatchService.kt | 28 -- .../storage/ObjectStorageService.kt | 10 +- .../storage/S3ObjectStorageService.kt | 38 ++- .../main/operator/AttachmentJobsCommand.kt | 105 ++++++ .../main/plugins/AttachmentWorker.kt | 12 +- .../mtusi/messenger/main/plugins/Database.kt | 6 +- .../com/mtusi/messenger/main/worker/Worker.kt | 2 +- .../migrations/V10__attachment_job_replay.sql | 13 + .../V11__complete_attachment_variants.sql | 36 +++ .../migrations/V9__attachment_outbox.sql | 23 ++ .../attachment/AttachmentControllerTest.kt | 10 +- .../attachment/AttachmentCleanupTest.kt | 19 +- .../attachment/AttachmentPipelineTest.kt | 139 +++++++- .../attachment/ImageProcessingServiceTest.kt | 130 ++++++++ .../LegacyAttachmentMigrationTest.kt | 4 +- 50 files changed, 1831 insertions(+), 532 deletions(-) create mode 100644 evidence/level4-recovery.txt create mode 100644 scripts/attachment-level4-recovery.py create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobReplayModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentOutboxModel.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentOutboxRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentOutboxRepository.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentImageProcessor.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcess.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/AttachmentOutboxDispatchService.kt delete mode 100644 src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt create mode 100644 src/main/kotlin/com/mtusi/messenger/main/operator/AttachmentJobsCommand.kt create mode 100644 src/main/resources/migrations/V10__attachment_job_replay.sql create mode 100644 src/main/resources/migrations/V11__complete_attachment_variants.sql create mode 100644 src/main/resources/migrations/V9__attachment_outbox.sql diff --git a/Dockerfile b/Dockerfile index 2f60fd5f..1b618079 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ 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 +RUN apk add --no-cache wget libwebp-tools && addgroup -S app && adduser -S -G app app WORKDIR /app COPY --from=build /src/build/libs/messenger.jar /app/messenger.jar USER app diff --git a/REPORT.md b/REPORT.md index f0cbd128..2795f0bb 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,15 +1,13 @@ # Отчёт — лабораторная 5 -- Автор, группа: Кучерявенко Максим, БВТ2303. -- Целевая оценка: **3**. Уровни 4 и 5 не заявляются. -- Основа: собственный проект lab4 на Kotlin 2.3.20, Ktor 3.3.0 и JDK 21. Архитектура controller → service → repository, Koin/KSP и Exposed 1.0.0 сохранены; стиль сверяется с существующими репозиториями проекта и Pixefy. -- Новые зависимости: RabbitMQ Java Client 5.26.0 и metadata-extractor 2.19.0. Библиотека Java Client предоставляет AMQP API, поэтому здесь используются её Java configuration objects; операции PostgreSQL написаны на Exposed DSL, S3 — на AWS SDK for Kotlin DSL. -- Commit/PR: после просмотра и публикации. -- ИИ использован как парный разработчик при реализации, ревью, подготовке тестов и отчёта. +- Автор, группа — Кучерявенко Максим, БВТ2303. +- Целевая оценка — **4**. +- Основа — Kotlin 2.3.20, Ktor 3.3.0, JDK 21, Koin и Exposed 1.0.0. Из предыдущих работ сохранены авторизация, чаты и сообщения, балансировка, идемпотентность отправки и приватное хранение оригиналов. +- ИИ использован для реализации, ревью, тестов и подготовки отчёта. -## Запуск из чистого clone +## Запуск -Нужны Docker с Compose v2, Python 3.9+, openssl. JVM-тесты требуют JDK 21 и работающий Docker: Testcontainers поднимает отдельные PostgreSQL, Redis, MinIO и RabbitMQ. +Для стенда нужны Docker с Compose v2, Python 3.9+ и OpenSSL. Для локального прогона JVM-тестов нужны JDK 21, Docker и `cwebp` с `dwebp` в PATH. На macOS эти утилиты входят в пакет `webp` Homebrew. Образ приложения содержит `libwebp-tools`. ```sh ./scripts/init-secrets.sh @@ -18,149 +16,91 @@ export BASE_URL=http://localhost:8085 docker compose config --quiet docker compose up -d --build --wait -docker compose ps - make check ./gradlew test make test - -# Основной сценарий lab5: worker остановлен, 202 принят, -# обе API и broker перезапущены, тот же файл доходит до ready. python3 scripts/attachment-worker-recovery.py -# Доступ через другую API-реплику и приватность объекта. +# Сценарии остановки сервисов запускайте на локальном учебном стенде. +python3 scripts/attachment-level4-recovery.py python3 scripts/attachment-failover.py - -# Сохранность после перезапуска всех компонентов, включая worker/broker. make test-persistence ACTION_SCRIPT=scripts/restart.sh - -# Безопасная остановка с сохранением volumes: -docker compose down -# Только для намеренного удаления всех учебных данных lab5: -# docker compose down -v ``` -Секреты генерируются локально, существующие файлы сохраняются. В Git не включаются `.env`, `secrets/`, `run/development/`, данные volumes и build/cache. Проект Compose называется `mtusi-messenger-lab5`: его контейнеры, сеть и volumes отделены от lab4. Наружу опубликован только HAProxy на loopback; PostgreSQL, Redis, S3 и RabbitMQ доступны внутри сети стенда. +Flyway применяет V1–V11 до запуска API и worker. HAProxy публикует только loopback-порт. API, worker, PostgreSQL, Redis, RabbitMQ и MinIO общаются по внутренней сети Compose. Для PostgreSQL, RabbitMQ и MinIO выделены именованные volumes. Повторный запуск bootstrap сохраняет bucket приватным. Секреты генерируются в `secrets/` и монтируются как файлы. `docker compose down` сохраняет volumes. -Для запуска через `./gradlew run` нужны отдельные локальные настройки подключений в `run/development/config`. Шаблон не является готовой конфигурацией существующего PostgreSQL на компьютере; роль `username` необходимо заменить реальной. Автоматически менять локальную БД или её пользователей задача `run` не будет. Воспроизводимый путь проверки этой работы — Compose. +## Что сохраняется к моменту 202 -## Сервисы и настройки +API проверяет доступ к чату, имя, MIME, размер и корректность PNG/JPEG. Тело загружается с лимитом 10 MiB и общим таймаутом чтения 15 секунд. Проверка заголовка ограничивает изображение 40 миллионами пикселей до декодирования. Полная валидация перед 202 сохраняет поведение lab4 для битых изображений. -| Роль | Compose service | Версия / хранение | -| --- | --- | --- | -| Балансировщик | lb | HAProxy 3.2.4; внешний порт LAB_HTTP_PORT, по умолчанию 8080 | -| HTTP API | api × 2 | Образ mtusi-messenger:lab5, собранный из исходников | -| Обработчик | worker | Тот же образ, отдельный процесс с командой `worker` | -| Метаданные и задания | postgres | PostgreSQL 17.6; postgres-data | -| Кэш сессий | redis | Redis 8.4.0; без persistence, источник истины — PostgreSQL | -| Объектное хранилище | s3 | MinIO RELEASE.2025-09-07T16-13-09Z; s3-data | -| Durable очередь | broker | RabbitMQ 4.1.8-alpine; broker-data, стабильный hostname broker | +1. API создаёт намерение в `attachment_uploads` и записывает исходные байты в приватный `staging/` в MinIO. +2. Одна транзакция PostgreSQL создаёт метаданные queued, строку `attachment_jobs` и запись `attachment_outbox`, затем закрывает намерение загрузки. +3. После COMMIT API отвечает `202` со снимком queued. За загрузку отвечает API, за доставку задания в RabbitMQ отвечает процесс worker. -MinIO и mc закреплены digest из lab4. RabbitMQ также закреплён multi-platform digest `sha256:cd4009416f83d7e5895020cd71b0963f3b8a4dfc3de74bbb6fb3bfe5f81f01e0`. Одноразовый `migrate` применяет миграции Flyway V1–V8 до старта API и worker. `s3-init` создаёт приватный bucket и policy и допускает повторный запуск. +Фоновый диспетчер выбирает готовую строку outbox через `FOR UPDATE SKIP LOCKED` и фиксирует аренду на 30 секунд. Затем вне транзакции публикует persistent JSON с mandatory routing и ждёт publisher confirm. После подтверждения удаляет строку с проверкой токена аренды. При отказе публикации строка снова доступна через секунду. После падения диспетчера её подберёт следующий процесс по истечении аренды. Повторная публикация между confirm и удалением строки допустима. -Переменные `.env.example` сопоставлены реализации так: +Если RabbitMQ остановлен, API остаётся ready при доступных PostgreSQL и MinIO. Подтверждённая загрузка ждёт в outbox. Worker начинает работу после восстановления RabbitMQ. В очереди хранятся идентификаторы и служебные поля из `contracts/jobs.schema.json`. -- `API_REPLICAS`, `SESSION_TTL_SECONDS`, `IDEMPOTENCY_TTL_SECONDS`, `S3_BUCKET` и `LAB_HTTP_PORT` управляют существующим стендом. -- PostgreSQL и Redis используют `DB_*` и `REDIS_*` с password-файлами. `DATABASE_URL_FILE`, `CACHE_URL_FILE`, `APP_ENV` и переключатель `SESSION_STORE` из skeleton не управляют выбранной реализацией. -- `BROKER_HOST`, `BROKER_PORT`, `BROKER_USER`, `BROKER_PASSWORD_FILE`, `BROKER_QUEUE` задают RabbitMQ. Вместо `BROKER_URL_FILE` используются отдельные поля, чтобы URL с credentials не попадал в логи. Имя очереди по умолчанию — `attachment.process.v1`. -- `WORKER_CONCURRENCY=1` задаёт число последовательных consumer-циклов worker; допустимы 1–4. Каждый цикл держит не более одной unacked delivery. Проверенный профиль использует 1; увеличение требует отдельной оценки памяти. -- `JOB_MAX_ATTEMPTS=5` ограничивает количество захватов работы, `JOB_TIMEOUT_SECONDS=60` задаёт coroutine timeout, lease устанавливается на timeout + 15 секунд. -- `STAGING_RETENTION_SECONDS=86400` задаёт задержку удаления staging для terminal-состояний; минимум 3600. Для queued/processing исходник не удаляется по возрасту. -- Регион S3 фиксирован `us-east-1`; пределы 10 MiB и 40 млн пикселей закреплены в коде и не увеличиваются переменными окружения. +## Worker и варианты -## Архитектура и граница 202 +RabbitMQ хранит durable classic очередь в volume. Каждый цикл worker последовательно берёт одну manual-ack доставку через `basicGet`, поэтому в работе не более одной доставки на цикл. Параллелизм по умолчанию 1, конфигурация допускает 1–4. Декодирование сериализовано внутри процесса worker. -Клиент → HAProxy → API → PostgreSQL + приватный staging S3 → RabbitMQ → worker → final S3 + PostgreSQL. +Worker проверяет размер и SHA-256 исходника. Дочерняя JVM декодирует его один раз и создаёт все варианты. Она ограничена heap 384 MiB и дедлайном 25 секунд. По дедлайну родитель завершает процесс и его потомков. `cwebp` получает уже уменьшенный растр и имеет отдельный лимит 15 секунд. Основная JVM worker ограничена heap 320 MiB, контейнер — 1 GiB памяти и 1 CPU. Таймаут задания по умолчанию 60 секунд, аренда — таймаут плюс 15 секунд. -API принимает весь файл, проверяет доступ к чату, filename, MIME, размер и корректность изображения. HTTP-тело читается порциями по 8192 bytes во временный файл с лимитом 10 MiB и таймаутом 15 секунд; семафор допускает одну активную загрузку на API-реплику. Для сохранения обязательных проверок lab4 перед 202 выполняется полная валидация ImageIO, включая ограничения пикселей, CRC/IEND PNG и EOI JPEG. Resize и публикация вариантов выполняются только worker. +| Вариант | Результат | +| --- | --- | +| original | Исходные байты и MIME, ключ `originals/` | +| thumbnail | JPEG, вписанный в 256×256 без увеличения, для 320×200 получается 256×160 | +| crop | JPEG, центральный квадрат 128×128. Маленькие изображения при необходимости увеличиваются | +| optimized | WebP, вписанный в 1280×1280 без увеличения | -Далее API: +Для thumbnail и optimized размеры округляются вниз до целого пикселя, минимум 1. EXIF orientation 1–8 учитывается для всех вариантов. JPEG использует качество 0,85 и белый фон под прозрачностью. WebP кодируется `cwebp` с параметрами `-q 82 -m 4 -alpha_q 100 -metadata none` и сохраняет альфа-канал. Варианты строятся из отрисованного растра, поэтому EXIF и другие метаданные исходника не копируются. -1. Регистрирует намерение загрузки в унаследованном журнале `attachment_uploads`. -2. Сохраняет неизменённые bytes в `staging/` в MinIO. -3. В одной DB-транзакции создаёт Attachment со статусом queued и строку `attachment_jobs`, затем удаляет завершённое намерение из журнала. -4. Публикует persistent JSON в durable очередь и ожидает publisher confirm. -5. Возвращает **202** со снимком queued, пустыми variants и error_code=null. +Worker записывает оригинал и три варианта по детерминированным ключам. Затем одна транзакция публикует метаданные вариантов и статус ready под проверкой lease token. ACK следует после фиксации результата или устойчивого сохранения следующей попытки в outbox. Повтор завершённой работы подтверждается без обработки. Ключ результата — вложение, версия pipeline 1 и имя варианта. -Worker может успеть завершить работу до первого GET, но ответ POST остаётся снимком queued. Подтверждённая 202 работа уже имеет staging, метаданные, запись задания и подтверждённое брокером сообщение. Локальный временный файл после этого не нужен. +Готовые варианты читаются через авторизованный API. Для queued, processing и failed контент отвечает 409. Незнакомый вариант готового вложения и обращение постороннего к вложению возвращают 404. История сообщений и повторы по Idempotency-Key сохраняют прежнюю семантику. -При ошибке публикации 202 не возвращается. Если строка ещё queued, она переводится в failed с `enqueue_failed`; уже захваченная или завершённая работа не откатывается. После жёсткого crash между COMMIT и publish может остаться неподтверждённая queued-запись без сообщения: автоматическое восстановление этого окна относится к outbox уровня 4 и здесь не заявляется. +V8 сохраняет старые оригиналы lab4 и ставит их в очередь на обработку. V9 переносит ожидающие задачи в outbox. V11 ставит в очередь вложения уровня 3, у которых ещё нет crop и optimized. Эти работы читают оригинал по существующему ключу. На время генерации контент отвечает 409. -## Договор producer/worker и состояния +## Повторы и операторский replay -`AttachmentJob` соответствует обязательным полям `contracts/jobs.schema.json`: schema_version=1, job_id, type=attachment.process.v1, attachment_id, pipeline_version=1, attempt и created_at. В сообщении нет bytes, credentials, пользовательского пути или URL. Worker получает доверенные метаданные по ID из PostgreSQL. +Временные ошибки хранилища, таймауты и ошибки обработки повторяются с экспоненциальной задержкой и jitter в диапазоне от половины до полного интервала. Верхняя граница интервала начинается с 1 секунды и ограничена 30 секундами. PostgreSQL хранит причину и время следующей попытки. Retry и запись outbox фиксируются вместе. Лимит по умолчанию — пять попыток на запуск. Невалидное изображение и превышение лимитов исходника приводят к failed с безопасным кодом. -Логическая job_id стабильна. Broker redelivery сохраняет envelope. Управляемый retry публикует сообщение той же job с увеличенным attempt. Worker подтверждает старую доставку после подтверждения новой публикации. Если lease ещё действует, worker отправляет тот же envelope в конец очереди, сохраняя номер попытки. Если старый envelope приходит после изменения DB-счётчика, worker публикует актуальную попытку без повторной обработки. +Если доставка возвращается при действующей аренде, worker планирует её в outbox на момент истечения lease. Очередь продолжает обслуживать остальные задания. После истечения lease job получает новую попытку в пределах бюджета. Успешное завершение или запись следующей попытки по старому токену блокируются после смены аренды. -- queued → processing: worker блокирует запись задания, проверяет статус и lease, увеличивает счётчик попыток и записывает случайный token. -- processing → ready: после записи original и thumbnail одна DB-транзакция публикует вариант и состояние ready, затем снимает lease. -- processing → queued: при временной ошибке с оставшимися попытками PostgreSQL сохраняет внутренний код причины и время следующего запуска. Повтор можно запустить сразу, поэтому используется время БД в момент перехода. Затем worker публикует retry и ждёт подтверждения. -- processing → failed: постоянная ошибка изображения или исчерпание попыток. В API остаётся безопасный error_code, без stack trace и секретов. -- ready/failed + повторная delivery: no-op с ack. Старый token не может опубликовать результат после нового захвата. +Реестр failed хранится в PostgreSQL. Команды доступны пользователю с доступом к локальному Docker и базе: -Default consumer использует manual `basicGet`: следующий запрос выполняется после завершения предыдущего handler. Это явно ограничивает число delivery в работе; значение basicQos(1) не используется как единственная гарантия для polling API. Ack отправляется после устойчивого результата или подтверждённой передачи следующей попытки. При остановке/обрыве unacked delivery возвращается в очередь. Истёкший lease допускает новый захват; попытки по-прежнему ограничены DB-счётчиком. +```sh +docker compose run --rm --no-deps -T worker attachment-jobs failed +docker compose run --rm --no-deps -T worker attachment-jobs replay JOB_ID OPERATOR +``` -Это доставка **at least once** с идемпотентным эффектом, а не exactly-once. Объекты имеют детерминированные ключи, а вариант в БД — первичный ключ `(attachment_id, name)`. В этом этапе используется только pipeline_version=1. +Первая команда выводит до 100 последних отказов. Replay сохраняет ID задания, открывает новый бюджет попыток и пишет аудит с операторской меткой, прежним кодом ошибки, временем и номером попытки. Метка передаётся оператором и используется для аудита, предоставление доступа к команде остаётся на стороне Docker. Replay отклоняет постоянную ошибку изображения, успешно завершённую или выполняющуюся задачу и задачу после начала очистки. Поэтому повтор команды для уже восстановленного задания не запускает дубль. -## Worker и изображения +Уборщик удаляет осиротевшие исходники после grace period. Срок хранения staging для терминальных состояний по умолчанию 86400 секунд. У готового вложения сохраняются оригинал и варианты. У failed нового вложения по истечении срока удаляются исходник из staging и частичные результаты. У импортированных вложений сохраняется прежний оригинал. Очистка помечает строку перед удалением, чтобы replay и удаление исходника не могли начаться одновременно. -Worker запускается отдельным процессом и не принимает бизнес-HTTP-запросы. Служебные health endpoints нужны Compose. Остановка worker не переводит resize в API: новые вложения остаются queued, текстовые сообщения продолжают работать, queued-вложение разрешено прикреплять к сообщению. +## Результаты проверок -Worker проверяет размер и SHA-256 staging, один раз декодирует изображение и строит thumbnail. Для записи оригинала в S3 worker использует уже проверенные байты: +Контрольный прогон выполнен **18 ноября 2026 года** на отдельном Compose-стенде `mtusi-messenger-lab5-level4` с портом 8086. Все результаты получены на сборке уровня 4. -- `originals/` — исходные bytes без перекодирования; -- `variants//v1/thumbnail.jpg` — JPEG, вписанный в 256×256 без увеличения; размеры округляются вниз, минимум 1 px. +| Проверка | Результат | +| --- | --- | +| Сборка и JVM-тесты | `rtk gradlew compileKotlin compileTestKotlin test --console=plain` на JDK 21. Все **98 тестов** прошли. Покрыты backoff, истечение lease, повторная доставка после рестарта RabbitMQ, аудит replay, миграции, очистка, EXIF, декодирование WebP и завершение дочернего процесса по дедлайну. [Сводка JUnit](evidence/gradle-tests.txt) | +| Структура, стенд и smoke | `make check`, `docker compose config --quiet`, `docker compose up -d --build --wait` и `make test` прошли. [Структура](evidence/check.txt), [контейнеры и лимиты](evidence/compose.txt), [smoke](evidence/smoke.txt) | +| Падение producer после COMMIT | RabbitMQ и worker остановлены. API принял файл, запись outbox осталась после SIGKILL принявшей реплики. После восстановления тот же ID стал ready со всеми вариантами. [Лог проверки](evidence/level4-recovery.txt) | +| Падение worker после записи S3 | Одноразовый worker завершился с кодом 86 до фиксации результата и ACK. API ответил 409. После истечения аренды вторая попытка завершила то же вложение, сохранив оригинал и контрольные суммы всех трёх вариантов. [Лог проверки](evidence/level4-recovery.txt) | +| Реестр и replay | При недоступном MinIO задание исчерпало бюджет. CLI показал failed. После восстановления хранилища replay довёл то же задание до ready на второй попытке. Повторная команда получила отказ. [Лог проверки](evidence/level4-recovery.txt) | +| Перезапуск worker, API и RabbitMQ | Задание осталось queued при остановленном worker и сохранилось после перезапуска API и RabbitMQ. После старта worker доступны оригинал и thumbnail. [Лог восстановления](evidence/worker-recovery.txt) | +| Переключение API и приватность S3 | После остановки принявшей реплики другая вернула тот же оригинал. Чужой пользователь получил 404, анонимный S3 GET получил 403. Служебная учётная запись ограничена рабочими префиксами. [Переключение](evidence/attachment-failover.txt), [права S3](evidence/s3-permissions.txt) | +| Перезапуск всего стенда | Подтверждённые данные и действующая сессия сохранились. `StartedAt` изменился у всех восьми контейнеров. [Сохранность и времена запуска](evidence/persistence.txt) | -EXIF orientation 1–8 применяется при отрисовке. Alpha компонуется на белый. JPEG кодируется стандартным ImageIO writer; чужие metadata в новый JPEG не переносятся. Fixture 320×200 даёт thumbnail 256×160. Метаданные варианта содержат собственные MIME, размеры, размер bytes и SHA-256. +## Ограничения и источники -Для queued/processing/failed скачивание content возвращает 409. Для ready доступен original и thumbnail, отсутствующий вариант получает 404. Чужой пользователь получает 404 до выдачи метаданных или содержимого. Failed-вложение нельзя добавить в новое сообщение, но уже созданное сообщение корректно воспроизводится по прежнему Idempotency-Key, даже если вложение позднее стало failed. +PostgreSQL, MinIO, RabbitMQ и HAProxy работают в одном экземпляре. Проверяется сохранность при рестарте с volumes. Восстановление задачи после падения worker может занять время аренды и задержки новой попытки. API перед 202 по-прежнему полностью декодирует ограниченное изображение. Жёсткий таймаут дочернего декодера применяется к worker. -При обновлении с lab4 миграция V8 переводит вложения без thumbnail в queued и создаёт задания для уже сохранённых оригиналов. Worker снимает отметку об ожидающей публикации после подтверждения RabbitMQ. Если процесс остановится раньше, он повторит публикацию того же задания. Существующий объект читается по прежнему ключу и сохраняет исходные байты. Скачивание снова доступно после готовности thumbnail. +Семантика outbox и повторной доставки рассчитана на at least once. Схема защищает эффект в рамках pipeline версии 1. Итоговый размер WebP зависит от исходника и не обязан быть меньше размера маленького PNG или JPEG. -## Сохранность, cleanup и безопасность - -Очередь durable classic, сообщения persistent, публикации используют mandatory routing и publisher confirms. RabbitMQ сохраняет данные в отдельном volume. Producer confirm означает приём брокером, consumer ack — устойчивый эффект worker; эти подтверждения не взаимозаменяемы. - -Унаследованный журнал закрывает orphan-окно между staging PUT и записью метаданных. После часового grace period уборщик помечает просроченные намерения abandoned и удаляет связанные ключи. Записи сохраняются для повторного удаления позднего PUT. По истечении retention уборщик удаляет staging для ready и все записанные файлы для failed. У вложений из lab4 сохраняется оригинал. Отметка staging_cleaned фиксирует завершение уборки. Очистка обходит только terminal-состояния. - -Root S3 credentials доступны только MinIO/bootstrap. Прикладная policy дополнена разрешениями для staging/* и variants/*, сохраняя запрет административных операций и записи вне рабочих prefixes. S3 и console не опубликованы на host, console привязана к loopback внутри контейнера. Пароль RabbitMQ читается из secret-файла; брокер также не опубликован наружу. - -При завершении приложения явно отменяются consumer, cleanup и синхронизация revocation, затем закрываются подключения. Это предотвращает работу старых фоновых циклов после закрытия DI/БД и их вмешательство в другие экземпляры тестового приложения. - -## Проверки уровня 3 - -Стендовые проверки из таблицы выполнены **17 ноября 2026 года** на порту 8085. После исправлений **18 ноября** отдельно собран стенд на порту 8086, где повторно прошли публичный smoke и сценарий восстановления worker. Полный JVM-набор также запущен повторно после правок. - -| Проверка | Команда / сценарий | Результат и доказательство | -| --- | --- | --- | -| Структура и контракт | `make check` | PASS; [вывод](evidence/check.txt) | -| Конфигурация и запуск Compose | `docker compose config --quiet`, `docker compose up -d --build --wait` | Все восемь рабочих контейнеров healthy; [конфигурация и состояние стенда](evidence/compose.txt) | -| JVM-регрессии и новый pipeline | `rtk gradlew compileKotlin compileTestKotlin test --console=plain` | **94 теста, 0 ошибок и пропусков**, включая обновление с lab4, очистку частичного результата и обработку следующей задачи при действующем lease; [сводка по JUnit XML](evidence/gradle-tests.txt) | -| Публичный сценарий lab5 | `make test BASE_URL=http://localhost:8085` | PASS: upload 202, ready, оригинал и настоящий JPEG thumbnail; [вывод](evidence/smoke.txt) | -| Worker остановлен, API и broker перезапущены | `python3 scripts/attachment-worker-recovery.py` | 202 queued → сообщение создано → content 409 → restart обеих API и broker с volumes → запуск worker → та же запись ready, неизменённый оригинал и JPEG 256×160; [вывод с attachment_id](evidence/worker-recovery.txt) | -| Durable доставка и повтор одной job | `RabbitMqJobBrokerServiceTest`, `AttachmentPipelineTest` в JVM-наборе | Реальный restart контейнера RabbitMQ сохраняет подтверждённое сообщение; nack возвращает тот же envelope; повтор завершённой job сохраняет прежний результат | -| Ограниченные попытки и failed | `AttachmentPipelineTest` в JVM-наборе | Busy lease, постоянная ошибка изображения и исчерпание временных попыток; failed запрещает content и новое сообщение, прежний Idempotency-Key воспроизводит созданное сообщение | -| Правила thumbnail | `ImageProcessingServiceTest` в JVM-наборе | Шесть тестов: JPEG/PNG EXIF orientation, alpha на белом, размеры с округлением вниз, отсутствие увеличения и удаление orientation из результата | -| Переключение API-реплики | `python3 scripts/attachment-failover.py` | Загрузившая реплика остановлена, другая отдаёт оригинал с тем же SHA-256; анонимный S3 GET — 403, чужой пользователь — 404; [вывод](evidence/attachment-failover.txt) | -| Привилегии S3 и повторный bootstrap | `scripts/check-s3-permissions.sh` | Запрещены административные операции, изменение policy и запись вне рабочих prefixes; console недоступна из общей сети; [вывод](evidence/s3-permissions.txt) | -| Полный restart с сохранением данных | `make test-persistence BASE_URL=http://localhost:8085 ACTION_SCRIPT=scripts/restart.sh` | PASS: подтверждённые данные и сессия сохранены; StartedAt изменился у обеих API, worker, broker, LB, PostgreSQL, Redis и MinIO; [вывод и времена до/после](evidence/persistence.txt) | - -Для сценариев со стендом установлены `LAB_HTTP_PORT=8085` и `BASE_URL=http://localhost:8085`, как в инструкции запуска выше. Полный JVM-прогон также проверяет сохранённые возможности lab1–lab4: авторизацию, чаты, конкурентную запись, идемпотентность сообщений, межрепличный отзыв сессий, миграции и отказы зависимостей. Исторические материалы lab4 находятся в `archive/lab4/`; они не являются доказательствами lab5. - -## Ограничения и границы следующего этапа - -- Outbox/автоматическое восстановление разрыва COMMIT/publish, backoff с jitter, DLQ/operator replay, crop и optimized WebP относятся к уровню 4 и не реализованы. -- Валидация использует массив ≤10 MiB и полный raster ImageIO; это не полностью потоковый декодер. Очень широкие изображения остаются краевым случаем памяти исходной реализации. Worker ограничен 1 GiB контейнерной памяти; при JVM OOM включён аварийный выход с последующим restart, а число попыток сохраняется в БД. -- Coroutine timeout кооперативный и не гарантирует принудительное прерывание блокирующего декодера в середине вызова. Lease/token предотвращает публикацию устаревшим обработчиком, но не заменяет жёсткое ограничение CPU-времени отдельного декодера. -- PostgreSQL, MinIO, RabbitMQ и HAProxy одноузловые. Volumes сохраняют данные при restart; отказоустойчивость узла и backup не заявляются. -- После crash незавершённая processing-работа может ждать истечения lease. Секундного SLA на восстановление при любом сбое не заявляется. -- Сравнение нескольких pipeline versions и защита от перезаписи результата старой версией относятся к исследовательскому треку; сейчас применяется версия 1. - -## Источники - -- Задание, contracts/README.md, contracts/attachment-states.md и contracts/jobs.schema.json этого репозитория. -- [RabbitMQ reliability](https://www.rabbitmq.com/docs/reliability). -- [Publisher confirms и consumer acknowledgements](https://www.rabbitmq.com/docs/confirms). -- [RabbitMQ Java Client API](https://www.rabbitmq.com/client-libraries/java-api-guide). +- [Контракт API](contracts/README.md) и [состояния вложений](contracts/attachment-states.md). +- [RabbitMQ reliability](https://www.rabbitmq.com/docs/reliability) и [publisher confirms / consumer ack](https://www.rabbitmq.com/docs/confirms). - [AWS SDK for Kotlin](https://github.com/awslabs/aws-sdk-kotlin). -- [metadata-extractor](https://github.com/drewnoakes/metadata-extractor). +- [Параметры cwebp](https://developers.google.com/speed/webp/docs/cwebp). diff --git a/build.gradle.kts b/build.gradle.kts index 2f3348ba..352567ac 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -86,6 +86,7 @@ sourceSets { tasks { test { useJUnitPlatform() + systemProperty("messenger.image.classpath", sourceSets["main"].runtimeClasspath.asPath) } shadowJar { diff --git a/compose.yaml b/compose.yaml index 6a3c3a67..bea573ce 100644 --- a/compose.yaml +++ b/compose.yaml @@ -165,8 +165,6 @@ services: condition: service_healthy s3-init: condition: service_completed_successfully - broker: - condition: service_healthy environment: APP_HOST: "0.0.0.0" APP_PORT: "8080" @@ -236,7 +234,7 @@ services: BROKER_USER: messenger BROKER_PASSWORD_FILE: /run/secrets/broker_password BROKER_QUEUE: ${BROKER_QUEUE:-attachment.process.v1} - JAVA_TOOL_OPTIONS: "-XX:+ExitOnOutOfMemoryError" + JAVA_TOOL_OPTIONS: "-Xmx320m -XX:+ExitOnOutOfMemoryError" WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-1} JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60} diff --git a/evidence/attachment-failover.txt b/evidence/attachment-failover.txt index 96df8612..9943eede 100644 --- a/evidence/attachment-failover.txt +++ b/evidence/attachment-failover.txt @@ -1,6 +1,6 @@ -Upload: 202 queued, then ready; instance=7a70d3b286da +Upload: 202 queued, then ready; instance=da6189641548 Anonymous S3 GET of known object: 403 Outsider metadata/content: 404 -Download: 200; instance=f000ea12f8bd +Download: 200; instance=b89ba5975865 SHA-256: 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 PASS: uploading replica stopped; original served by another replica; stopped replica restored diff --git a/evidence/compose.txt b/evidence/compose.txt index c89ebc6b..1bbd8b79 100644 --- a/evidence/compose.txt +++ b/evidence/compose.txt @@ -1,13 +1,11 @@ -$ docker compose config --quiet -Exit code: 0 - -$ docker compose ps -NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS -mtusi-messenger-lab5-api-1 mtusi-messenger:lab5 "java -XX:MaxRAMPerc…" api 8 minutes ago Up 2 minutes (healthy) 8080/tcp -mtusi-messenger-lab5-api-2 mtusi-messenger:lab5 "java -XX:MaxRAMPerc…" api 8 minutes ago Up 2 minutes (healthy) 8080/tcp -mtusi-messenger-lab5-broker-1 rabbitmq@sha256:cd4009416f83d7e5895020cd71b0963f3b8a4dfc3de74bbb6fb3bfe5f81f01e0 "/bin/sh /bootstrap.…" broker 19 hours ago Up 2 minutes (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp -mtusi-messenger-lab5-lb-1 haproxy:3.2.4-alpine "docker-entrypoint.s…" lb 19 hours ago Up About a minute (healthy) 127.0.0.1:8085->8080/tcp -mtusi-messenger-lab5-postgres-1 postgres:17.6-alpine3.22 "docker-entrypoint.s…" postgres 19 hours ago Up 2 minutes (healthy) 5432/tcp -mtusi-messenger-lab5-redis-1 redis:8.4.0-alpine "docker-entrypoint.s…" redis 19 hours ago Up 2 minutes (healthy) 6379/tcp -mtusi-messenger-lab5-s3-1 quay.io/minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e "/usr/bin/docker-ent…" s3 19 hours ago Up 2 minutes (healthy) 9000/tcp -mtusi-messenger-lab5-worker-1 mtusi-messenger:lab5 "java -XX:MaxRAMPerc…" worker 8 minutes ago Up 2 minutes (healthy) 8080/tcp +api Up 10 seconds (healthy) 8080/tcp +api Up 10 seconds (healthy) 8080/tcp +broker Up 19 seconds (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp +lb Up 6 seconds (healthy) 127.0.0.1:8086->8080/tcp +postgres Up 19 seconds (healthy) 5432/tcp +redis Up 19 seconds (healthy) 6379/tcp +s3 Up 19 seconds (healthy) 9000/tcp +worker Up 10 seconds (healthy) 8080/tcp +Worker memory bytes=1073741824; NanoCPUs=1000000000 +1.6.0 +libsharpyuv: 0.4.2 diff --git a/evidence/gradle-tests.txt b/evidence/gradle-tests.txt index 56fa37a2..57834776 100644 --- a/evidence/gradle-tests.txt +++ b/evidence/gradle-tests.txt @@ -1,6 +1,6 @@ Command: rtk gradlew compileKotlin compileTestKotlin test --console=plain (JDK 21) BUILD SUCCESSFUL -Tests: 94; failures: 0; errors: 0; skipped: 0 +Tests: 98; failures: 0; errors: 0; skipped: 0 com.mtusi.messenger.main.infrastructure.controllers.attachment.AttachmentControllerTest: tests=3 failures=0 com.mtusi.messenger.main.infrastructure.controllers.authentication.AuthenticationControllerTest: tests=9 failures=0 com.mtusi.messenger.main.infrastructure.controllers.chat.ChatControllerTest: tests=6 failures=0 @@ -22,12 +22,16 @@ com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentPipelineTe busy redelivery lets the next attachment finish before lease expiration() worker distinguishes permanent image errors from bounded transient failures() stopped worker leaves a durable queued attachment and redelivery preserves its result() -com.mtusi.messenger.main.infrastructure.services.attachment.ImageProcessingServiceTest: tests=6 failures=0 +com.mtusi.messenger.main.infrastructure.services.attachment.ImageProcessingServiceTest: tests=10 failures=0 + worker subprocess preserves permanent image failures() PNG EXIF orientation is applied before resizing() transparent and semitransparent pixels are composited on white() invalid images and MIME mismatches are rejected before publishing a thumbnail() + published variants apply orientation and preserve the centered crop() missing or unknown EXIF orientation preserves the normal image() all JPEG EXIF orientations rotate or mirror pixels and strip orientation from the thumbnail() + optimized WebP preserves alpha and never enlarges a small source() + image subprocess stops at its deadline() thumbnail fits the box floors dimensions and never enlarges the source() com.mtusi.messenger.main.infrastructure.services.attachment.ImageValidatorTest: tests=3 failures=0 com.mtusi.messenger.main.infrastructure.services.attachment.LegacyAttachmentMigrationTest: tests=1 failures=0 diff --git a/evidence/level4-recovery.txt b/evidence/level4-recovery.txt new file mode 100644 index 00000000..a7873962 --- /dev/null +++ b/evidence/level4-recovery.txt @@ -0,0 +1,5 @@ +PASS: producer SIGKILL after PostgreSQL commit and before publish; outbox survived; id=dfeea134-7def-4f55-a601-825dc8a56208 +PASS: outbox recovered the same attachment with all three variants; id=dfeea134-7def-4f55-a601-825dc8a56208 +PASS: worker exited 86 after writing S3 objects, before result commit and ack; id=cf5b21c7-ecc8-4c43-8a28-a3f3761d1378 +PASS: redelivery recovered the same id, original and three variants on attempt 2; id=cf5b21c7-ecc8-4c43-8a28-a3f3761d1378 +PASS: failed job listed, replayed with same id on attempt 2, and replay refused after ready; id=99a5cbcd-5cde-482b-860c-c375d8a42af9 diff --git a/evidence/persistence.txt b/evidence/persistence.txt index a380f1d9..ec7c20e5 100644 --- a/evidence/persistence.txt +++ b/evidence/persistence.txt @@ -1,23 +1,23 @@ -python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8085" +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8086" Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. Before restart: -/mtusi-messenger-lab5-api-1 2026-11-17T11:48:25.6974278Z -/mtusi-messenger-lab5-api-2 2026-11-17T11:48:25.799164633Z -/mtusi-messenger-lab5-broker-1 2026-11-17T11:48:15.954373295Z -/mtusi-messenger-lab5-lb-1 2026-11-17T11:48:28.901775676Z -/mtusi-messenger-lab5-postgres-1 2026-11-17T11:48:15.943166503Z -/mtusi-messenger-lab5-redis-1 2026-11-17T11:48:15.95361167Z -/mtusi-messenger-lab5-s3-1 2026-11-17T11:48:15.94897392Z -/mtusi-messenger-lab5-worker-1 2026-11-17T11:48:25.696508175Z +/mtusi-messenger-lab5-level4-api-1 2026-11-18T11:08:43.347539047Z +/mtusi-messenger-lab5-level4-api-2 2026-11-18T11:08:17.102104132Z +/mtusi-messenger-lab5-level4-broker-1 2026-11-18T11:08:13.123424672Z +/mtusi-messenger-lab5-level4-lb-1 2026-11-18T11:05:22.404306926Z +/mtusi-messenger-lab5-level4-postgres-1 2026-11-18T11:05:06.926939794Z +/mtusi-messenger-lab5-level4-redis-1 2026-11-18T11:05:06.922586211Z +/mtusi-messenger-lab5-level4-s3-1 2026-11-18T11:07:45.175138173Z +/mtusi-messenger-lab5-level4-worker-1 2026-11-18T11:08:24.920627553Z After restart: -/mtusi-messenger-lab5-api-1 2026-11-17T11:52:14.708903753Z -/mtusi-messenger-lab5-api-2 2026-11-17T11:52:14.820903586Z -/mtusi-messenger-lab5-broker-1 2026-11-17T11:52:04.990389887Z -/mtusi-messenger-lab5-lb-1 2026-11-17T11:52:17.919454796Z -/mtusi-messenger-lab5-postgres-1 2026-11-17T11:52:04.990403929Z -/mtusi-messenger-lab5-redis-1 2026-11-17T11:52:04.984496471Z -/mtusi-messenger-lab5-s3-1 2026-11-17T11:52:04.992642512Z -/mtusi-messenger-lab5-worker-1 2026-11-17T11:52:14.708902794Z +/mtusi-messenger-lab5-level4-api-1 2026-11-18T11:09:23.075147802Z +/mtusi-messenger-lab5-level4-api-2 2026-11-18T11:09:22.927327177Z +/mtusi-messenger-lab5-level4-broker-1 2026-11-18T11:09:13.628215922Z +/mtusi-messenger-lab5-level4-lb-1 2026-11-18T11:09:26.709695053Z +/mtusi-messenger-lab5-level4-postgres-1 2026-11-18T11:09:13.629343672Z +/mtusi-messenger-lab5-level4-redis-1 2026-11-18T11:09:13.621602214Z +/mtusi-messenger-lab5-level4-s3-1 2026-11-18T11:09:13.629312422Z +/mtusi-messenger-lab5-level4-worker-1 2026-11-18T11:09:22.928541802Z diff --git a/evidence/s3-permissions.txt b/evidence/s3-permissions.txt index 59841603..ca044e59 100644 --- a/evidence/s3-permissions.txt +++ b/evidence/s3-permissions.txt @@ -1,10 +1,10 @@ - Container mtusi-messenger-lab5-s3-1 Running + Container mtusi-messenger-lab5-level4-s3-1 Running Bucket created successfully `local/messenger-attachments`. Access permission for `local/messenger-attachments` is set to `private` - Container mtusi-messenger-lab5-s3-1 Running + Container mtusi-messenger-lab5-level4-s3-1 Running Bucket created successfully `local/messenger-attachments`. Access permission for `local/messenger-attachments` is set to `private` - Container mtusi-messenger-lab5-s3-1 Running -PASS: app cannot administer MinIO, create buckets, change policy or write outside originals/ -wget: can't connect to remote host (172.26.0.2): Connection refused + Container mtusi-messenger-lab5-level4-s3-1 Running +PASS: app cannot administer MinIO, create buckets, change policy or write to an unrelated prefix +wget: can't connect to remote host (172.27.0.4): Connection refused PASS: console unavailable from shared network diff --git a/evidence/smoke.txt b/evidence/smoke.txt index 2448312f..cbe5f5a5 100644 --- a/evidence/smoke.txt +++ b/evidence/smoke.txt @@ -1,2 +1,2 @@ -python3 tests/smoke.py --base-url "http://localhost:8085" +python3 tests/smoke.py --base-url "http://localhost:8086" PASS: lab5 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/worker-recovery.txt b/evidence/worker-recovery.txt index bcac0aeb..61d37898 100644 --- a/evidence/worker-recovery.txt +++ b/evidence/worker-recovery.txt @@ -1,3 +1,3 @@ -PASS: worker stopped; upload 202 queued; message accepted; content 409; attachment_id=0305bfe5-e30e-47cc-b81e-4f2c578273a6 -PASS: both APIs and broker restarted with existing volumes; same attachment remains queued; attachment_id=0305bfe5-e30e-47cc-b81e-4f2c578273a6 -PASS: same accepted job reached ready; original unchanged; thumbnail JPEG 256x160 with matching checksum; attachment_id=0305bfe5-e30e-47cc-b81e-4f2c578273a6 +PASS: worker stopped; upload 202 queued; message accepted; content 409; attachment_id=5fba0009-e3d4-4c96-b77d-472ead517cda +PASS: both APIs and broker restarted with existing volumes; same attachment remains queued; attachment_id=5fba0009-e3d4-4c96-b77d-472ead517cda +PASS: same accepted job reached ready; original unchanged; thumbnail JPEG 256x160 with matching checksum; attachment_id=5fba0009-e3d4-4c96-b77d-472ead517cda diff --git a/scripts/attachment-level4-recovery.py b/scripts/attachment-level4-recovery.py new file mode 100644 index 00000000..3ae5953d --- /dev/null +++ b/scripts/attachment-level4-recovery.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python3 +"""Prove the producer and worker crash boundaries on a local Compose stand.""" +import hashlib +from pathlib import Path +import subprocess +import sys +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tests")) +from client import Client, Failure, check +from image_probe import jpeg_size + +FIXTURE = (ROOT / "tests/fixtures/sample.png").read_bytes() + + +def docker(*args, timeout=45, expected=0): + result = subprocess.run( + ["docker", *args], + cwd=ROOT, + capture_output=True, + text=True, + timeout=timeout, + ) + check(result.returncode == expected, f"docker {args[0]} returned {result.returncode}, expected {expected}") + return (result.stdout if expected == 0 else result.stdout + result.stderr).strip() + + +def compose(*args, **kwargs): + return docker("compose", *args, **kwargs) + + +def sql(query): + return compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-Atc", query) + + +def metadata_state(attachment_id): + return sql( + "SELECT a.status, j.attempts, EXISTS " + "(SELECT 1 FROM attachment_outbox o WHERE o.job_id = j.id) " + "FROM attachments a JOIN attachment_jobs j ON j.attachment_id = a.id " + f"WHERE a.id = '{uuid.UUID(attachment_id)}'" + ) + + +def await_api(client): + deadline = time.monotonic() + 30 + while True: + try: + client.call("GET", "/health/ready") + return + except Failure: + check(time.monotonic() < deadline, "API did not recover") + time.sleep(0.25) + + +def upload(client, actor, chat): + return client.call( + "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png", + 202, actor=actor, raw=FIXTURE, headers={"Content-Type": "image/png"}, schema="Attachment", + ) + + +def verify_result(client, actor, queued): + attachment = client.wait_ready(actor, queued, wait_seconds=75) + path = f"/api/v1/attachments/{queued['id']}/content" + original = client.call("GET", path, actor=actor)["bytes"] + check(original == FIXTURE, "original bytes changed") + + variants = {variant["name"]: variant for variant in attachment["variants"]} + check(set(variants) == {"thumbnail", "crop", "optimized"}, "variant set differs") + for name, variant in variants.items(): + response = client.call("GET", f"{path}?variant={name}", actor=actor) + data = response["bytes"] + check(hashlib.sha256(data).hexdigest() == variant["sha256"], f"{name} checksum differs") + check(len(data) == variant["size_bytes"], f"{name} size differs") + check(response["headers"].get_content_type() == variant["content_type"], f"{name} MIME differs") + if name == "optimized": + check(data[:4] == b"RIFF" and data[8:12] == b"WEBP", "optimized is not WebP") + check((variant["width"], variant["height"]) == (320, 200), "optimized dimensions differ") + else: + expected = (128, 128) if name == "crop" else (256, 160) + check(jpeg_size(data) == expected, f"{name} dimensions differ") + + +def producer_crash(client, actor, reader, chat): + compose("stop", "worker") + compose("stop", "broker") + try: + result = upload(client, actor, chat) + queued = result["json"] + check(metadata_state(queued["id"]) == "queued|0|t", "outbox did not commit with the upload") + + instance = result["headers"]["X-Instance-Id"] + matches = [cid for cid in compose("ps", "-q", "api").splitlines() if cid.startswith(instance)] + check(len(matches) == 1, "uploading API replica was not identified") + replica = matches[0] + before = docker("inspect", "-f", "{{.State.StartedAt}}", replica) + docker("kill", "--signal", "KILL", replica) + docker("start", replica) + await_api(client) + check(before != docker("inspect", "-f", "{{.State.StartedAt}}", replica), "uploading API did not restart") + check(metadata_state(queued["id"]) == "queued|0|t", "pending outbox lost after API crash") + print(f"PASS: producer SIGKILL after PostgreSQL commit and before publish; outbox survived; id={queued['id']}", flush=True) + + compose("start", "broker", "worker") + verify_result(client, reader, queued) + print(f"PASS: outbox recovered the same attachment with all three variants; id={queued['id']}", flush=True) + finally: + compose("start", "broker", "api", "worker") + + +def worker_crash(client, actor, reader, chat): + compose("stop", "worker") + temporary = "lab5-worker-fault-" + uuid.uuid4().hex[:12] + try: + queued = upload(client, actor, chat)["json"] + compose( + "run", "--rm", "--no-deps", "-T", "--name", temporary, + "-e", f"LAB5_TEST_CRASH_AFTER_OBJECTS={queued['id']}", + "-e", "JOB_TIMEOUT_SECONDS=15", "worker", + timeout=70, expected=86, + ) + check(metadata_state(queued["id"]).startswith("processing|1|"), "worker did not stop before committing results") + client.call("GET", f"/api/v1/attachments/{queued['id']}/content", 409, actor=reader) + print(f"PASS: worker exited 86 after writing S3 objects, before result commit and ack; id={queued['id']}", flush=True) + + compose("start", "worker") + verify_result(client, reader, queued) + check(metadata_state(queued["id"]).startswith("ready|2|"), "recovery did not use exactly one new attempt") + print(f"PASS: redelivery recovered the same id, original and three variants on attempt 2; id={queued['id']}", flush=True) + finally: + subprocess.run(["docker", "rm", "-f", temporary], capture_output=True, cwd=ROOT) + compose("start", "worker") + + +def operator_replay(client, actor, reader, chat): + compose("stop", "worker") + temporary = "lab5-worker-replay-" + uuid.uuid4().hex[:12] + try: + queued = upload(client, actor, chat)["json"] + job = sql(f"SELECT id FROM attachment_jobs WHERE attachment_id = '{uuid.UUID(queued['id'])}'") + job = str(uuid.UUID(job)) + compose("stop", "s3") + compose( + "run", "-d", "--rm", "--no-deps", "--name", temporary, + "-e", "JOB_MAX_ATTEMPTS=1", "worker", + ) + + deadline = time.monotonic() + 40 + while not metadata_state(queued["id"]).startswith("failed|1|"): + check(time.monotonic() < deadline, "one-attempt worker did not record the storage failure") + time.sleep(0.25) + + docker("stop", "--time", "6", temporary) + compose("up", "-d", "--wait", "s3") + await_api(client) + + command = ("run", "--rm", "--no-deps", "-T", "worker", "attachment-jobs") + listed = compose(*command, "failed") + check(any(line.startswith(job + "\t") for line in listed.splitlines()), "failed job was not listed") + check(f"QUEUED {job}" in compose(*command, "replay", job, "local-verification"), "replay was not queued") + check(metadata_state(queued["id"]) == "queued|1|t", "operator replay was not committed with outbox") + compose("start", "worker") + verify_result(client, reader, queued) + + refused = compose(*command, "replay", job, "local-verification", expected=2) + check("REJECTED" in refused, "successful job accepted a duplicate replay") + check(metadata_state(queued["id"]).startswith("ready|2|"), "successful result changed on duplicate replay") + print(f"PASS: failed job listed, replayed with same id on attempt 2, and replay refused after ready; id={queued['id']}", flush=True) + finally: + subprocess.run(["docker", "rm", "-f", temporary], capture_output=True, cwd=ROOT) + compose("start", "s3", "worker") + + +def main(): + client = Client(timeout=15) + check(client.base.startswith(("http://localhost:", "http://127.0.0.1:")), "use a local Compose stand") + actor, reader, _, chat = client.bootstrap() + producer_crash(client, actor, reader, chat) + worker_crash(client, actor, reader, chat) + operator_replay(client, actor, reader, chat) + + +if __name__ == "__main__": + main() diff --git a/scripts/attachment-worker-recovery.py b/scripts/attachment-worker-recovery.py index 59559b6b..0a259886 100644 --- a/scripts/attachment-worker-recovery.py +++ b/scripts/attachment-worker-recovery.py @@ -54,7 +54,7 @@ def main(): thumbnail = client.call('GET', path + '/content?variant=thumbnail', actor=bob)['bytes'] check(downloaded == original, 'original changed') check(jpeg_size(thumbnail) == (256, 160), 'incorrect thumbnail dimensions') - variant = attachment['variants'][0] + variant = next(item for item in attachment['variants'] if item['name'] == 'thumbnail') check(variant['sha256'] == hashlib.sha256(thumbnail).hexdigest(), 'thumbnail checksum mismatch') client.call('GET', path, 404, actor=eve) print(f"PASS: same accepted job reached ready; original unchanged; thumbnail JPEG 256x160 with matching checksum; attachment_id={attachment['id']}", flush=True) diff --git a/scripts/check-s3-permissions.sh b/scripts/check-s3-permissions.sh index 416bdeb3..e0db025d 100755 --- a/scripts/check-s3-permissions.sh +++ b/scripts/check-s3-permissions.sh @@ -12,7 +12,7 @@ if mc anonymous set download "app/$S3_BUCKET" >/dev/null 2>&1; then exit 1; fi printf test > /tmp/test-object if mc cp /tmp/test-object "app/$S3_BUCKET/forbidden-prefix/object" >/dev/null 2>&1; then exit 1; fi mc stat "app/$S3_BUCKET" >/dev/null -printf "PASS: app cannot administer MinIO, create buckets, change policy or write outside originals/\n" +printf "PASS: app cannot administer MinIO, create buckets, change policy or write to an unrelated prefix\n" ' if docker compose exec -T lb wget -q -T 2 -O /dev/null http://s3:9001; then echo 'FAIL: console reachable from shared network' diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt index 97e9e9a9..f769402f 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/App.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -1,5 +1,6 @@ package com.mtusi.messenger.main +import com.mtusi.messenger.main.operator.runAttachmentJobsCommand import com.mtusi.messenger.main.plugins.authentication.configureAuthentication import com.mtusi.messenger.main.plugins.configureAttachmentCleanup import com.mtusi.messenger.main.plugins.configureCallLogging @@ -29,6 +30,10 @@ fun main(args: Array) { return migrateDatabase() } + if (command == "attachment-jobs") { + return runAttachmentJobsCommand(args.drop(1)) + } + if (command == "worker") { val workerArgs = arrayOf( "-config=worker.conf", 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 5ebfb74e..02a5ba46 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 @@ -20,7 +20,9 @@ import kotlinx.coroutines.ensureActive import kotlinx.coroutines.withContext import org.koin.ktor.ext.inject -fun Routing.configureHealthRouting() = route("/health") { +fun Routing.configureHealthRouting( + requireBroker: Boolean = false +) = route("/health") { get("live") { call.respond(HttpStatusCode.OK, HealthResponse()) @@ -29,7 +31,7 @@ fun Routing.configureHealthRouting() = route("/health") { val storage by inject() val broker by inject() - // Readiness checks PostgreSQL, S3 and RabbitMQ. Redis has a DB fallback. + // API uploads persist in PostgreSQL and S3. Only the worker requires RabbitMQ. get("ready") { // SIGTERM received: leave the balancer's rotation while requests in flight are still being finished. @@ -69,7 +71,7 @@ fun Routing.configureHealthRouting() = route("/health") { return@get } - if (!broker.ready()) { + if (requireBroker && !broker.ready()) { call.respondError(ErrorCode.UNAVAILABLE, "job broker unavailable") return@get } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt index 2e64b26d..2e035ba0 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobModel.kt @@ -13,12 +13,12 @@ object AttachmentJobModel : UUIDTable("attachment_jobs") { val pipelineVersion = integer("pipeline_version").default(1) val attempts = integer("attempts").default(0) + val replayBaseAttempt = integer("replay_base_attempt").default(0) val leaseToken = javaUUID("lease_token").nullable() val leaseUntil = timestamp("lease_until").nullable() val lastErrorCode = varchar("last_error_code", 64).nullable() val nextAttemptAt = timestamp("next_attempt_at").nullable() val createdAt = timestamp("created_at") val legacyOriginal = bool("legacy_original").default(false) - val legacyDispatchPending = bool("legacy_dispatch_pending").default(false) } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobReplayModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobReplayModel.kt new file mode 100644 index 00000000..09f2348f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentJobReplayModel.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +object AttachmentJobReplayModel : UUIDTable("attachment_job_replays") { + + val jobId = reference("job_id", AttachmentJobModel) + val operator = varchar("operator", 80) + val previousErrorCode = varchar("previous_error_code", 64) + val attempt = integer("attempt") + val createdAt = timestamp("created_at") + + init { + uniqueIndex(jobId, attempt) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt index 027f3b7e..e65e3cb2 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt @@ -16,5 +16,6 @@ object AttachmentModel : UUIDTable("attachments") { val updatedAt = timestamp("updated_at") val status = varchar("status", 16).default("ready") val stagingCleaned = bool("staging_cleaned").default(false) + val storageCleanupStarted = bool("storage_cleanup_started").default(false) val errorCode = varchar("error_code", 64).nullable() } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentOutboxModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentOutboxModel.kt new file mode 100644 index 00000000..13a91a25 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentOutboxModel.kt @@ -0,0 +1,25 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.core.java.javaUUID +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.datetime.timestamp + +object AttachmentOutboxModel : UUIDTable("attachment_outbox") { + + val jobId = reference("job_id", AttachmentJobModel) + val attempt = integer("attempt") + val availableAt = timestamp("available_at").defaultExpression(CurrentTimestamp) + val leaseToken = javaUUID("lease_token").nullable() + val leaseUntil = timestamp("lease_until").nullable() + + init { + index( + customIndexName = "attachment_outbox_due_idx", + isUnique = false, + availableAt, + id + ) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt index fd4086db..9b46beb9 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt @@ -23,10 +23,6 @@ interface AttachmentRepository { errorCode: String? = null ) - fun markEnqueueFailed( - id: UUID - ) - fun markStagingCleaned( id: UUID ) @@ -35,9 +31,14 @@ interface AttachmentRepository { before: Instant ): List + fun claimExpiredStorage( + attachment: ExpiredAttachmentStorage, + before: Instant + ): Boolean + fun publish( id: UUID, - variant: AttachmentVariant + variants: List ) fun attach( diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt index f1144526..df8c3b43 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt @@ -14,7 +14,7 @@ import org.jetbrains.exposed.v1.core.eq import org.jetbrains.exposed.v1.core.inList import org.jetbrains.exposed.v1.core.less import org.jetbrains.exposed.v1.jdbc.insert -import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.batchUpsert import org.jetbrains.exposed.v1.jdbc.select import org.jetbrains.exposed.v1.jdbc.selectAll import org.jetbrains.exposed.v1.jdbc.update @@ -99,20 +99,6 @@ class ExposedAttachmentRepository : AttachmentRepository { } } - override fun markEnqueueFailed( - id: UUID - ) { - AttachmentModel.update( - where = { - (AttachmentModel.id eq id) and (AttachmentModel.status eq "queued") - } - ) { - it[status] = "failed" - it[errorCode] = "enqueue_failed" - it[updatedAt] = Clock.System.nowMicros() - } - } - override fun markStagingCleaned( id: UUID ) { @@ -145,16 +131,16 @@ class ExposedAttachmentRepository : AttachmentRepository { override fun publish( id: UUID, - variant: AttachmentVariant + variants: List ) { - AttachmentVariantModel.insertIgnore { - it[attachmentId] = id - it[name] = variant.name - it[contentType] = variant.contentType - it[sizeBytes] = variant.sizeBytes - it[width] = variant.width - it[height] = variant.height - it[sha256] = variant.sha256 + AttachmentVariantModel.batchUpsert(variants) { variant -> + this[AttachmentVariantModel.attachmentId] = id + this[AttachmentVariantModel.name] = variant.name + this[AttachmentVariantModel.contentType] = variant.contentType + this[AttachmentVariantModel.sizeBytes] = variant.sizeBytes + this[AttachmentVariantModel.width] = variant.width + this[AttachmentVariantModel.height] = variant.height + this[AttachmentVariantModel.sha256] = variant.sha256 } setState( @@ -163,6 +149,25 @@ class ExposedAttachmentRepository : AttachmentRepository { ) } + override fun claimExpiredStorage( + attachment: ExpiredAttachmentStorage, + before: Instant + ): Boolean { + + val status = if (attachment.failed) "failed" else "ready" + + return AttachmentModel.update( + where = { + (AttachmentModel.id eq attachment.id) + .and(AttachmentModel.status eq status) + .and(AttachmentModel.updatedAt less before) + .and(AttachmentModel.stagingCleaned eq false) + } + ) { + it[storageCleanupStarted] = true + } == 1 + } + override fun attach( messageId: UUID, ids: List diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt index 270d47e2..64ea2c2c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentJobRepository.kt @@ -3,6 +3,8 @@ package com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant import java.util.UUID +import kotlin.time.Duration +import kotlin.time.Instant interface AttachmentJobRepository { @@ -14,13 +16,14 @@ interface AttachmentJobRepository { job: AttachmentJob, token: UUID, maxAttempts: Int, - leaseSeconds: Long + leaseSeconds: Long, + retryDelay: (Int) -> Duration = { Duration.ZERO } ): JobClaim fun complete( job: AttachmentJob, token: UUID, - variant: AttachmentVariant + variants: List ): Boolean fun fail( @@ -28,19 +31,39 @@ interface AttachmentJobRepository { token: UUID, errorCode: String, permanent: Boolean, - maxAttempts: Int + maxAttempts: Int, + retryDelay: Duration = Duration.ZERO ): Boolean - fun findPendingLegacyJobs( + fun findFailed( limit: Int - ): List + ): List - fun markLegacyJobDispatched( - id: UUID - ) + fun replay( + id: UUID, + operator: String + ): JobReplayResult } +data class FailedAttachmentJob( + val jobId: UUID, + val attachmentId: UUID, + val attempts: Int, + val errorCode: String, + val lastErrorCode: String?, + val failedAt: Instant, + val replayable: Boolean +) + +enum class JobReplayResult { + QUEUED, + NOT_FOUND, + NOT_FAILED, + PERMANENT_FAILURE, + SOURCE_EXPIRED +} + sealed interface JobClaim { data object Complete : JobClaim @@ -53,7 +76,8 @@ sealed interface JobClaim { data class Acquired( val attempt: Int, - val legacyOriginal: Boolean = false + val legacyOriginal: Boolean = false, + val attemptInRun: Int = attempt ) : JobClaim } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentOutboxRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentOutboxRepository.kt new file mode 100644 index 00000000..8da72c51 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/AttachmentOutboxRepository.kt @@ -0,0 +1,33 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs + +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import java.util.UUID +import kotlin.time.Instant + +interface AttachmentOutboxRepository { + + fun enqueue( + job: AttachmentJob, + availableAt: Instant = job.createdAt + ) + + fun claimDue( + token: UUID + ): PendingAttachmentPublish? + + fun complete( + id: UUID, + token: UUID + ) + + fun reschedule( + id: UUID, + token: UUID + ) + +} + +data class PendingAttachmentPublish( + val id: UUID, + val job: AttachmentJob +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt index 7de5e212..91551a43 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentJobRepository.kt @@ -3,16 +3,12 @@ package com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobReplayModel import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository -import com.mtusi.messenger.main.utils.exposed.operations.plus 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.eq -import org.jetbrains.exposed.v1.core.greater -import org.jetbrains.exposed.v1.core.isNotNull -import org.jetbrains.exposed.v1.core.longLiteral import org.jetbrains.exposed.v1.datetime.CurrentTimestamp import org.jetbrains.exposed.v1.jdbc.insert import org.jetbrains.exposed.v1.jdbc.select @@ -20,10 +16,14 @@ 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.Duration +import kotlin.time.Duration.Companion.seconds +import kotlin.time.Instant @Single class ExposedAttachmentJobRepository( - private val attachmentRepository: AttachmentRepository + private val attachmentRepository: AttachmentRepository, + private val outboxRepository: AttachmentOutboxRepository ) : AttachmentJobRepository { override fun create( @@ -41,67 +41,81 @@ class ExposedAttachmentJobRepository( job: AttachmentJob, token: UUID, maxAttempts: Int, - leaseSeconds: Long + leaseSeconds: Long, + retryDelay: (Int) -> Duration ): JobClaim { val row = lock(job) ?: return JobClaim.Complete - val status = AttachmentModel - .select(AttachmentModel.status) - .where(AttachmentModel.id eq job.attachmentId) - .forUpdate() - .single()[AttachmentModel.status] + val attachment = lockAttachment(job.attachmentId) + val status = attachment[AttachmentModel.status] if (status in setOf("ready", "failed")) { return JobClaim.Complete } - val activeLease = AttachmentJobModel.leaseUntil.isNotNull() - .and(AttachmentJobModel.leaseUntil greater CurrentTimestamp) - - val busy = AttachmentJobModel - .select(activeLease) - .where(AttachmentJobModel.id eq job.jobId) - .single() - .get(activeLease) == true - - if (busy) { - return JobClaim.Busy - } - val attempts = row[AttachmentJobModel.attempts] + val attemptsInRun = attempts - row[AttachmentJobModel.replayBaseAttempt] + val leaseUntil = row[AttachmentJobModel.leaseUntil] + val now = row[CurrentTimestamp] + val nextAttempt = attempts + 1 - if (attempts >= maxAttempts) { - attachmentRepository.setState( - id = job.attachmentId, - status = "failed", - errorCode = "retry_exhausted" + if (leaseUntil != null) { + // The delivery for the live claim is the only one that can schedule recovery. + if (job.attempt != attempts) { + return JobClaim.Complete + } + + if (leaseUntil > now) { + outboxRepository.enqueue( + job = job, + availableAt = leaseUntil + ) + + return JobClaim.Busy + } + + if (attemptsInRun >= maxAttempts) { + markFailed( + job = job, + cause = "lease_expired", + permanent = false + ) + + return JobClaim.Complete + } + + queueRetry( + job = job.copy(attempt = nextAttempt), + errorCode = "lease_expired", + availableAt = now + retryDelay(attemptsInRun) ) - clearLease(job.jobId) + return JobClaim.Retry(attempt = nextAttempt) + } + // Once an attempt moves on, its old deliveries cannot skip backoff or restart a replay. + val nextAttemptAt = row[AttachmentJobModel.nextAttemptAt] + + if (job.attempt != nextAttempt || (nextAttemptAt != null && nextAttemptAt > now)) { return JobClaim.Complete } - val nextAttempt = attempts + 1 + if (attemptsInRun >= maxAttempts) { + markFailed( + job = job, + cause = row[AttachmentJobModel.lastErrorCode] ?: "retry_exhausted", + permanent = false + ) - if (job.attempt != nextAttempt) { - if (row[AttachmentJobModel.leaseToken] != null) { - queueRetry( - id = job.jobId, - attachmentId = job.attachmentId, - errorCode = "lease_expired" - ) - } - - return JobClaim.Retry(attempt = nextAttempt) + return JobClaim.Complete } AttachmentJobModel.update(where = { AttachmentJobModel.id eq job.jobId }) { it[this.attempts] = nextAttempt it[leaseToken] = token - it[leaseUntil] = CurrentTimestamp + longLiteral(leaseSeconds) - it[nextAttemptAt] = null + it[this.leaseUntil] = now + leaseSeconds.seconds + it[this.nextAttemptAt] = null } attachmentRepository.setState( @@ -111,14 +125,15 @@ class ExposedAttachmentJobRepository( return JobClaim.Acquired( attempt = nextAttempt, - legacyOriginal = row[AttachmentJobModel.legacyOriginal] + legacyOriginal = row[AttachmentJobModel.legacyOriginal], + attemptInRun = attemptsInRun + 1 ) } override fun complete( job: AttachmentJob, token: UUID, - variant: AttachmentVariant + variants: List ): Boolean { val row = lock(job) ?: return false @@ -129,7 +144,7 @@ class ExposedAttachmentJobRepository( attachmentRepository.publish( id = job.attachmentId, - variant = variant + variants = variants ) clearLease(job.jobId) @@ -142,7 +157,8 @@ class ExposedAttachmentJobRepository( token: UUID, errorCode: String, permanent: Boolean, - maxAttempts: Int + maxAttempts: Int, + retryDelay: Duration ): Boolean { val row = lock(job) ?: return true @@ -151,90 +167,181 @@ class ExposedAttachmentJobRepository( return false } - val terminal = permanent || row[AttachmentJobModel.attempts] >= maxAttempts + val attempts = row[AttachmentJobModel.attempts] + val attemptsInRun = attempts - row[AttachmentJobModel.replayBaseAttempt] + val terminal = permanent || attemptsInRun >= maxAttempts if (!terminal) { queueRetry( - id = job.jobId, - attachmentId = job.attachmentId, - errorCode = errorCode + job = job.copy(attempt = attempts + 1), + errorCode = errorCode, + availableAt = row[CurrentTimestamp] + retryDelay ) return false } + markFailed( + job = job, + cause = errorCode, + permanent = permanent + ) + + return true + } + + private fun queueRetry( + job: AttachmentJob, + errorCode: String, + availableAt: Instant + ) { attachmentRepository.setState( id = job.attachmentId, - status = "failed", - errorCode = if (permanent) errorCode else "retry_exhausted" + status = "queued" ) AttachmentJobModel.update(where = { AttachmentJobModel.id eq job.jobId }) { it[leaseToken] = null it[leaseUntil] = null it[lastErrorCode] = errorCode - it[nextAttemptAt] = null + it[nextAttemptAt] = availableAt } - return true + outboxRepository.enqueue( + job = job, + availableAt = availableAt + ) } - private fun queueRetry( - id: UUID, - attachmentId: UUID, - errorCode: String + private fun markFailed( + job: AttachmentJob, + cause: String, + permanent: Boolean ) { + attachmentRepository.setState( + id = job.attachmentId, + status = "failed", + errorCode = if (permanent) cause else "retry_exhausted" + ) + + AttachmentJobModel.update(where = { AttachmentJobModel.id eq job.jobId }) { + it[leaseToken] = null + it[leaseUntil] = null + it[lastErrorCode] = cause + it[nextAttemptAt] = null + } + } + + override fun findFailed( + limit: Int + ): List { + return AttachmentJobModel + .innerJoin(AttachmentModel) + .selectAll() + .where(AttachmentModel.status eq "failed") + .orderBy(AttachmentModel.updatedAt to SortOrder.DESC, AttachmentJobModel.id to SortOrder.ASC) + .limit(limit) + .map { row -> + FailedAttachmentJob( + jobId = row[AttachmentJobModel.id].value, + attachmentId = row[AttachmentJobModel.attachmentId].value, + attempts = row[AttachmentJobModel.attempts], + errorCode = checkNotNull(row[AttachmentModel.errorCode]), + lastErrorCode = row[AttachmentJobModel.lastErrorCode], + failedAt = row[AttachmentModel.updatedAt], + replayable = replayRestriction(row) == null + ) + } + } + + override fun replay( + id: UUID, + operator: String + ): JobReplayResult { + val row = lockById(id) ?: return JobReplayResult.NOT_FOUND + val attachmentId = row[AttachmentJobModel.attachmentId].value + val attachment = lockAttachment(attachmentId) + val restriction = replayRestriction(attachment) + + if (restriction != null) { + return restriction + } + + val attempts = row[AttachmentJobModel.attempts] + val now = row[CurrentTimestamp] + val job = AttachmentJob( + jobId = id, + attachmentId = attachmentId, + createdAt = row[AttachmentJobModel.createdAt], + pipelineVersion = row[AttachmentJobModel.pipelineVersion], + attempt = attempts + 1 + ) + + AttachmentJobModel.update(where = { AttachmentJobModel.id eq id }) { + it[replayBaseAttempt] = attempts + it[leaseToken] = null + it[leaseUntil] = null + it[lastErrorCode] = null + it[nextAttemptAt] = now + } + + AttachmentJobReplayModel.insert { + it[jobId] = id + it[this.operator] = operator + it[previousErrorCode] = checkNotNull(attachment[AttachmentModel.errorCode]) + it[attempt] = job.attempt + it[createdAt] = now + } + attachmentRepository.setState( id = attachmentId, status = "queued" ) - AttachmentJobModel.update(where = { AttachmentJobModel.id eq id }) { - it[leaseToken] = null - it[leaseUntil] = null - it[lastErrorCode] = errorCode - it[nextAttemptAt] = CurrentTimestamp - } + outboxRepository.enqueue(job, availableAt = now) + + return JobReplayResult.QUEUED } - override fun findPendingLegacyJobs( - limit: Int - ): List { - return AttachmentJobModel - .selectAll() - .where(AttachmentJobModel.legacyDispatchPending eq true) - .orderBy(AttachmentJobModel.createdAt to SortOrder.ASC, AttachmentJobModel.id to SortOrder.ASC) - .limit(limit) - .map { - AttachmentJob( - jobId = it[AttachmentJobModel.id].value, - attachmentId = it[AttachmentJobModel.attachmentId].value, - pipelineVersion = it[AttachmentJobModel.pipelineVersion], - createdAt = it[AttachmentJobModel.createdAt] - ) - } - } - - override fun markLegacyJobDispatched( - id: UUID - ) { - AttachmentJobModel.update(where = { AttachmentJobModel.id eq id }) { - it[legacyDispatchPending] = false + private fun replayRestriction( + attachment: ResultRow + ): JobReplayResult? { + return when { + attachment[AttachmentModel.status] != "failed" -> JobReplayResult.NOT_FAILED + attachment[AttachmentModel.errorCode] == "invalid_image" -> JobReplayResult.PERMANENT_FAILURE + attachment[AttachmentModel.stagingCleaned] || attachment[AttachmentModel.storageCleanupStarted] -> + JobReplayResult.SOURCE_EXPIRED + else -> null } } private fun lock( job: AttachmentJob + ): ResultRow? { + return lockById(job.jobId)?.takeIf { + it[AttachmentJobModel.attachmentId].value == job.attachmentId && + it[AttachmentJobModel.pipelineVersion] == job.pipelineVersion + } + } + + private fun lockById( + id: UUID ): ResultRow? { return AttachmentJobModel - .selectAll() - .where(AttachmentJobModel.id eq job.jobId) + .select(AttachmentJobModel.columns + CurrentTimestamp) + .where(AttachmentJobModel.id eq id) .forUpdate() .firstOrNull() - ?.takeIf { - it[AttachmentJobModel.attachmentId].value == job.attachmentId && - it[AttachmentJobModel.pipelineVersion] == job.pipelineVersion - } + } + + private fun lockAttachment( + id: UUID + ): ResultRow { + return AttachmentModel + .selectAll() + .where(AttachmentModel.id eq id) + .forUpdate() + .single() } private fun clearLease( diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentOutboxRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentOutboxRepository.kt new file mode 100644 index 00000000..983ffa4d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/jobs/ExposedAttachmentOutboxRepository.kt @@ -0,0 +1,110 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs + +import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentOutboxModel +import com.mtusi.messenger.main.utils.exposed.operations.plus +import org.jetbrains.exposed.v1.core.SortOrder +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.isNull +import org.jetbrains.exposed.v1.core.lessEq +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.core.or +import org.jetbrains.exposed.v1.core.vendors.ForUpdateOption +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insert +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 + +private const val PUBLISH_LEASE_SECONDS = 30L +private const val PUBLISH_RETRY_SECONDS = 1L + +@Single +class ExposedAttachmentOutboxRepository : AttachmentOutboxRepository { + + override fun enqueue( + job: AttachmentJob, + availableAt: Instant + ) { + AttachmentOutboxModel.insert { + it[jobId] = job.jobId + it[attempt] = job.attempt + it[this.availableAt] = availableAt + } + } + + override fun claimDue( + token: UUID + ): PendingAttachmentPublish? { + + val row = AttachmentOutboxModel + .selectAll() + .where( + (AttachmentOutboxModel.availableAt lessEq CurrentTimestamp) and + (AttachmentOutboxModel.leaseUntil.isNull() or + (AttachmentOutboxModel.leaseUntil lessEq CurrentTimestamp)) + ) + .orderBy( + AttachmentOutboxModel.availableAt to SortOrder.ASC, + AttachmentOutboxModel.id to SortOrder.ASC + ) + .limit(1) + .forUpdate(ForUpdateOption.PostgreSQL.ForUpdate(ForUpdateOption.PostgreSQL.MODE.SKIP_LOCKED)) + .firstOrNull() + ?: return null + + val id = row[AttachmentOutboxModel.id].value + val jobId = row[AttachmentOutboxModel.jobId].value + + AttachmentOutboxModel.update(where = { AttachmentOutboxModel.id eq id }) { + it[leaseToken] = token + it[leaseUntil] = CurrentTimestamp + longLiteral(PUBLISH_LEASE_SECONDS) + } + + val job = AttachmentJobModel + .selectAll() + .where(AttachmentJobModel.id eq jobId) + .single() + + return PendingAttachmentPublish( + id = id, + job = AttachmentJob( + jobId = jobId, + attachmentId = job[AttachmentJobModel.attachmentId].value, + attempt = row[AttachmentOutboxModel.attempt], + pipelineVersion = job[AttachmentJobModel.pipelineVersion], + createdAt = job[AttachmentJobModel.createdAt] + ) + ) + } + + override fun complete( + id: UUID, + token: UUID + ) { + AttachmentOutboxModel.deleteWhere { + (AttachmentOutboxModel.id eq id) and (leaseToken eq token) + } + } + + override fun reschedule( + id: UUID, + token: UUID + ) { + AttachmentOutboxModel.update( + where = { + (AttachmentOutboxModel.id eq id) and (AttachmentOutboxModel.leaseToken eq token) + } + ) { + it[availableAt] = CurrentTimestamp + longLiteral(PUBLISH_RETRY_SECONDS) + it[leaseToken] = null + it[leaseUntil] = null + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt index d7b41db8..e110b7ce 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt @@ -37,19 +37,27 @@ class AttachmentCleanupService( } } - val expired = suspendedTransaction { - attachmentRepository.findExpiredStorage( - before = Clock.System.now() - config.stagingRetentionSeconds.seconds - ) - } + val before = Clock.System.now() - config.stagingRetentionSeconds.seconds + val expired = suspendedTransaction { attachmentRepository.findExpiredStorage(before) } - expired.forEach { attachment -> + for (attachment in expired) { try { + val claimed = suspendedTransaction { + attachmentRepository.claimExpiredStorage( + attachment = attachment, + before = before + ) + } + + if (!claimed) { + continue + } + when { attachment.failed && !attachment.legacyOriginal -> objectStorageService.delete(attachment.id) attachment.failed -> - objectStorageService.deleteThumbnail(attachment.id) + objectStorageService.deleteVariants(attachment.id) !attachment.legacyOriginal -> objectStorageService.deleteStaging(attachment.id) } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentImageProcessor.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentImageProcessor.kt new file mode 100644 index 00000000..c8b98182 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentImageProcessor.kt @@ -0,0 +1,137 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.dto.ProcessedImage +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import kotlinx.coroutines.runBlocking +import java.io.DataInputStream +import java.io.DataOutputStream +import java.nio.file.Path +import kotlin.system.exitProcess +import kotlin.time.Duration.Companion.seconds + +private const val INVALID_IMAGE = 64 +private const val IMAGE_LIMIT = 65 +private const val ENCODER_TIMEOUT = 66 +private const val JVM_OUT_OF_MEMORY = 3 +private val OUTPUT_TYPES = linkedMapOf( + "thumbnail" to "image/jpeg", + "crop" to "image/jpeg", + "optimized" to "image/webp" +) + +internal object AttachmentImageProcessor { + + suspend fun process( + bytes: ByteArray, + declaredType: String + ): Map { + + if (bytes.size > MAX_ATTACHMENT_BYTES) { + throw ApiException( + code = ErrorCode.PAYLOAD_TOO_LARGE, + message = "file exceeds 10 MiB" + ) + } + + val java = Path.of(System.getProperty("java.home"), "bin", "java") + val classpath = System.getProperty("messenger.image.classpath") + ?: System.getProperty("java.class.path") + + val response = runImageProcess( + command = listOf( + java.toString(), + "-Xmx384m", + "-XX:+UseSerialGC", + "-XX:ActiveProcessorCount=1", + "-XX:+ExitOnOutOfMemoryError", + "-Djava.awt.headless=true", + "-cp", classpath, + AttachmentImageProcessor::class.java.name, + declaredType + ), + input = bytes, + outputLimit = MAX_ATTACHMENT_BYTES + 1024 * 1024, + timeout = 25.seconds + ) + + when (response.exitCode) { + INVALID_IMAGE -> throw ApiException( + code = ErrorCode.UNSUPPORTED_MEDIA_TYPE, + message = "invalid source image" + ) + IMAGE_LIMIT, JVM_OUT_OF_MEMORY -> throw ApiException( + code = ErrorCode.PAYLOAD_TOO_LARGE, + message = "image exceeds processing limits" + ) + ENCODER_TIMEOUT -> throw ImageProcessTimeoutException() + else -> check(response.exitCode == 0) { "image processor failed" } + } + + return readVariants(response.output) + } + + @JvmStatic + fun main( + args: Array + ) { + + val declaredType = args.single() + val bytes = System.`in`.readNBytes(MAX_ATTACHMENT_BYTES + 1) + + val variants = try { + runBlocking { + DefaultImageProcessingService().generateVariants( + bytes = bytes, + declaredType = declaredType + ) + } + } catch (cause: ApiException) { + when (cause.code) { + ErrorCode.UNSUPPORTED_MEDIA_TYPE -> exitProcess(INVALID_IMAGE) + ErrorCode.PAYLOAD_TOO_LARGE -> exitProcess(IMAGE_LIMIT) + else -> throw cause + } + } catch (_: ImageProcessTimeoutException) { + exitProcess(ENCODER_TIMEOUT) + } + + DataOutputStream(System.out).use { output -> + for (name in OUTPUT_TYPES.keys) { + val variant = variants.getValue(name) + output.writeInt(variant.width) + output.writeInt(variant.height) + output.writeInt(variant.bytes.size) + output.write(variant.bytes) + } + } + } + + private fun readVariants( + bytes: ByteArray + ): Map = DataInputStream(bytes.inputStream()).use { input -> + + val variants = OUTPUT_TYPES.mapValues { (_, contentType) -> + val width = input.readInt() + val height = input.readInt() + val size = input.readInt() + + check(width in 1..1280 && height in 1..1280 && size in 1..MAX_ATTACHMENT_BYTES) + + val image = ByteArray(size) + input.readFully(image) + + ProcessedImage( + bytes = image, + width = width, + height = height, + contentType = contentType + ) + } + + check(input.read() == -1) { "unexpected image process output" } + + variants + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt index 29812a17..e4a8ef85 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentProcessingService.kt @@ -6,17 +6,18 @@ import com.mtusi.messenger.main.infrastructure.dto.AttachmentVariant import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.JobClaim -import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService 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 kotlinx.coroutines.CancellationException -import kotlinx.coroutines.delay import kotlinx.coroutines.withTimeoutOrNull import org.koin.core.annotation.Single import java.util.UUID +import kotlin.random.Random +import kotlin.time.Duration +import kotlin.system.exitProcess import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds @@ -26,7 +27,6 @@ class DefaultAttachmentProcessingService( private val attachmentRepository: AttachmentRepository, private val objectStorageService: ObjectStorageService, private val imageProcessingService: ImageProcessingService, - private val jobBrokerService: JobBrokerService, private val config: WorkerConfig ) { @@ -40,28 +40,13 @@ class DefaultAttachmentProcessingService( job = job, token = token, maxAttempts = config.maxAttempts, - leaseSeconds = config.timeoutSeconds + 15 + leaseSeconds = config.timeoutSeconds + 15, + retryDelay = ::retryDelay ) } when (claim) { - JobClaim.Complete -> return true - - JobClaim.Busy -> { - // Pace redeliveries while the lease is active. Publish to the tail and - // confirm before acknowledging the original delivery. - delay(250.milliseconds) - jobBrokerService.publish(job) - - return true - } - - is JobClaim.Retry -> { - jobBrokerService.publish(job.copy(attempt = claim.attempt)) - - return true - } - + JobClaim.Complete, JobClaim.Busy, is JobClaim.Retry -> return true is JobClaim.Acquired -> Unit } @@ -86,31 +71,37 @@ class DefaultAttachmentProcessingService( val errorCode = when { isPermanent -> "invalid_image" - cause is ProcessingTimeoutException -> "processing_timeout" + cause is ProcessingTimeoutException || cause is ImageProcessTimeoutException -> "processing_timeout" cause is ApiException && cause.code == ErrorCode.UNAVAILABLE -> "storage_unavailable" else -> "processing_failed" } - val isTerminal = suspendedTransaction { + suspendedTransaction { jobRepository.fail( job = job, token = token, errorCode = errorCode, permanent = isPermanent, - maxAttempts = config.maxAttempts + maxAttempts = config.maxAttempts, + retryDelay = retryDelay(claim.attemptInRun) ) } - if (!isTerminal) { - jobBrokerService.publish(job.copy(attempt = claim.attempt + 1)) - } - return true } } private class ProcessingTimeoutException : RuntimeException() + private fun retryDelay( + attempts: Int + ): Duration { + val exponent = (attempts - 1).coerceIn(0, 5) + val upperMillis = (1000L shl exponent).coerceAtMost(30_000L) + + return Random.nextLong(upperMillis / 2, upperMillis + 1).milliseconds + } + private suspend fun processAttachment( job: AttachmentJob, token: UUID, @@ -134,11 +125,15 @@ class DefaultAttachmentProcessingService( ) } - val thumbnail = imageProcessingService.thumbnail( + val images = imageProcessingService.variants( bytes = bytes, declaredType = attachment.contentType ) + check(images.keys == setOf("thumbnail", "crop", "optimized")) { + "Image processor did not produce all required variants" + } + if (!legacyOriginal) { objectStorageService.put( id = attachment.id, @@ -147,23 +142,33 @@ class DefaultAttachmentProcessingService( ) } - objectStorageService.putThumbnail( - id = attachment.id, - bytes = thumbnail.bytes - ) + val variants = images.map { (name, image) -> + objectStorageService.putVariant( + id = attachment.id, + name = name, + bytes = image.bytes + ) + + AttachmentVariant( + name = name, + contentType = image.contentType, + sizeBytes = image.bytes.size.toLong(), + width = image.width, + height = image.height, + sha256 = image.bytes.computeSha256() + ) + } + + // Enabled only on a disposable worker used by the recovery check. + if (System.getenv("LAB5_TEST_CRASH_AFTER_OBJECTS") == attachment.id.toString()) { + exitProcess(86) + } return suspendedTransaction { jobRepository.complete( job = job, token = token, - variant = AttachmentVariant( - name = "thumbnail", - contentType = thumbnail.contentType, - sizeBytes = thumbnail.bytes.size.toLong(), - width = thumbnail.width, - height = thumbnail.height, - sha256 = thumbnail.bytes.computeSha256() - ) + variants = variants ) } } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt index 8782680c..181d0229 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt @@ -5,17 +5,15 @@ import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentOutboxRepository import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository -import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService 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 com.mtusi.messenger.main.utils.nowMicros -import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.withContext import org.koin.core.annotation.Single import java.io.File @@ -29,7 +27,7 @@ class DefaultAttachmentService( private val chatRepository: ChatRepository, private val objectStorageService: ObjectStorageService, private val jobRepository: AttachmentJobRepository, - private val jobBrokerService: JobBrokerService + private val outboxRepository: AttachmentOutboxRepository ) : AttachmentService { override suspend fun requireMember( @@ -97,37 +95,18 @@ class DefaultAttachmentService( createdAt = attachment.createdAt ) - try { - suspendedTransaction { - if (!journalRepository.lockForCompletion(attachment.id)) { - throw ApiException( - code = ErrorCode.UNAVAILABLE, - message = "upload expired" - ) - } - - attachmentRepository.create(attachment) - jobRepository.create(job) - journalRepository.complete(attachment.id) + suspendedTransaction { + if (!journalRepository.lockForCompletion(attachment.id)) { + throw ApiException( + code = ErrorCode.UNAVAILABLE, + message = "upload expired" + ) } - jobBrokerService.publish(job) - } catch (cause: CancellationException) { - withContext(NonCancellable) { - runCatching { - suspendedTransaction { - attachmentRepository.markEnqueueFailed(attachment.id) - } - } - } - - throw cause - } catch (cause: Exception) { - suspendedTransaction { - attachmentRepository.markEnqueueFailed(attachment.id) - } - - throw cause + attachmentRepository.create(attachment) + jobRepository.create(job) + outboxRepository.enqueue(job) + journalRepository.complete(attachment.id) } return attachment @@ -165,9 +144,12 @@ class DefaultAttachmentService( ) } - when (variant) { - "original" -> objectStorageService.get(attachment.id) - "thumbnail" -> objectStorageService.getThumbnail(attachment.id) + when { + variant == "original" -> objectStorageService.get(attachment.id) + attachment.variants.any { it.name == variant } -> objectStorageService.getVariant( + id = attachment.id, + name = variant + ) else -> throw ApiException( code = ErrorCode.NOT_FOUND, message = "variant not found" diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt index 8dd1a869..9ab66acc 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultImageProcessingService.kt @@ -9,6 +9,8 @@ import com.mtusi.messenger.main.infrastructure.dto.ProcessedImage import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.sync.Semaphore +import kotlinx.coroutines.sync.withPermit import kotlinx.coroutines.withContext import org.koin.core.annotation.Single import java.awt.Color @@ -18,20 +20,212 @@ import java.awt.image.BufferedImage import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.nio.ByteBuffer +import javax.imageio.IIOImage import javax.imageio.ImageIO +import javax.imageio.ImageWriteParam +import javax.imageio.stream.MemoryCacheImageOutputStream +import kotlin.time.Duration.Companion.seconds private const val THUMBNAIL_SIZE = 256 +private const val CROP_SIZE = 128 +private const val OPTIMIZED_SIZE = 1280 +private const val JPEG_QUALITY = 0.85f @Single class DefaultImageProcessingService : ImageProcessingService { - override suspend fun thumbnail( + private val decoder = Semaphore(1) + + override suspend fun variants( bytes: ByteArray, declaredType: String - ): ProcessedImage = withContext(Dispatchers.IO) { + ): Map = decoder.withPermit { + AttachmentImageProcessor.process( + bytes = bytes, + declaredType = declaredType + ) + } + + internal suspend fun thumbnail( + bytes: ByteArray, + declaredType: String + ): ProcessedImage = withSource(bytes, declaredType, ::thumbnail) + + internal suspend fun generateVariants( + bytes: ByteArray, + declaredType: String + ): Map = withSource(bytes, declaredType) { source -> + linkedMapOf( + "thumbnail" to thumbnail(source), + "crop" to crop(source), + "optimized" to optimized(source) + ) + } + + private suspend fun thumbnail( + source: SourceImage + ): ProcessedImage { + val (width, height) = source.fit(THUMBNAIL_SIZE) + + return render( + source = source, + width = width, + height = height, + transform = source.scaleTo(width, height), + contentType = "image/jpeg", + encode = ::encodeJpeg + ) + } + + private suspend fun crop( + source: SourceImage + ): ProcessedImage { + + val scale = CROP_SIZE.toDouble() / minOf(source.width, source.height) + val transform = AffineTransform.getTranslateInstance( + (CROP_SIZE - source.width * scale) / 2, + (CROP_SIZE - source.height * scale) / 2 + ).apply { + scale(scale, scale) + } + + return render( + source = source, + width = CROP_SIZE, + height = CROP_SIZE, + transform = transform, + contentType = "image/jpeg", + encode = ::encodeJpeg + ) + } + + private suspend fun optimized( + source: SourceImage + ): ProcessedImage { + val (width, height) = source.fit(OPTIMIZED_SIZE) + + return render( + source = source, + width = width, + height = height, + transform = source.scaleTo(width, height), + contentType = "image/webp", + encode = ::encodeWebp + ) + } + + private suspend fun render( + source: SourceImage, + width: Int, + height: Int, + transform: AffineTransform, + contentType: String, + encode: suspend (BufferedImage) -> ByteArray + ): ProcessedImage { currentCoroutineContext().ensureActive() + val preserveAlpha = contentType == "image/webp" + val imageType = if (preserveAlpha) BufferedImage.TYPE_INT_ARGB else BufferedImage.TYPE_INT_RGB + val rendered = BufferedImage(width, height, imageType) + + try { + val graphics = rendered.createGraphics() + + try { + if (!preserveAlpha) { + graphics.color = Color.WHITE + graphics.fillRect(0, 0, width, height) + } + + graphics.setRenderingHint( + RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR + ) + graphics.setRenderingHint( + RenderingHints.KEY_RENDERING, + RenderingHints.VALUE_RENDER_QUALITY + ) + + transform.concatenate(source.orientation) + graphics.drawImage(source.original, transform, null) + } finally { + graphics.dispose() + } + + return ProcessedImage( + bytes = encode(rendered), + width = width, + height = height, + contentType = contentType + ) + } finally { + rendered.flush() + } + } + + private fun encodeJpeg( + image: BufferedImage + ): ByteArray { + + val output = ByteArrayOutputStream() + val writer = ImageIO.getImageWritersByFormatName("jpeg").next() + + try { + MemoryCacheImageOutputStream(output).use { stream -> + writer.output = stream + + val parameters = writer.defaultWriteParam.apply { + compressionMode = ImageWriteParam.MODE_EXPLICIT + compressionQuality = JPEG_QUALITY + } + + writer.write(null, IIOImage(image, null, null), parameters) + } + } finally { + writer.dispose() + } + + return output.toByteArray() + } + + private suspend fun encodeWebp( + image: BufferedImage + ): ByteArray { + + val png = ByteArrayOutputStream() + + MemoryCacheImageOutputStream(png).use { output -> + check(ImageIO.write(image, "png", output)) { "PNG encoder is unavailable" } + } + + val encoded = runImageProcess( + command = listOf( + "cwebp", + "-quiet", + "-q", "82", + "-m", "4", + "-alpha_q", "100", + "-metadata", "none", + "-o", "-", + "--", "-" + ), + input = png.toByteArray(), + outputLimit = MAX_ATTACHMENT_BYTES, + timeout = 15.seconds + ) + + check(encoded.exitCode == 0 && encoded.output.isNotEmpty()) { "WebP encode failed" } + + return encoded.output + } + + private suspend fun withSource( + bytes: ByteArray, + declaredType: String, + block: suspend (SourceImage) -> R + ): R = withContext(Dispatchers.IO) { + val original = ImageValidator.decode( bytes = bytes, declaredType = declaredType @@ -41,94 +235,57 @@ class DefaultImageProcessingService : ImageProcessingService { currentCoroutineContext().ensureActive() val orientation = readOrientation(bytes, declaredType) - val swapDimensions = orientation in 5..8 - val orientedWidth = if (swapDimensions) original.height else original.width - val orientedHeight = if (swapDimensions) original.width else original.height - val longestSide = maxOf(orientedWidth, orientedHeight) - val width = if (longestSide <= THUMBNAIL_SIZE) { - orientedWidth - } else { - maxOf(1, (orientedWidth.toLong() * THUMBNAIL_SIZE / longestSide).toInt()) - } - - val height = if (longestSide <= THUMBNAIL_SIZE) { - orientedHeight - } else { - maxOf(1, (orientedHeight.toLong() * THUMBNAIL_SIZE / longestSide).toInt()) - } - - val thumbnail = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) - - try { - val transform = AffineTransform.getScaleInstance( - width.toDouble() / orientedWidth, - height.toDouble() / orientedHeight - ).apply { - val orientationCorrection = orientationTransform( + block( + SourceImage( + original = original, + width = if (orientation in 5..8) original.height else original.width, + height = if (orientation in 5..8) original.width else original.height, + orientation = orientationTransform( orientation = orientation, width = original.width, height = original.height ) - - concatenate(orientationCorrection) - } - - drawThumbnail( - original = original, - thumbnail = thumbnail, - transform = transform ) - - currentCoroutineContext().ensureActive() - - val encoded = ByteArrayOutputStream().use { output -> - check(ImageIO.write(thumbnail, "jpeg", output)) { - "JPEG encoder is unavailable" - } - - output.toByteArray() - } - - ProcessedImage( - bytes = encoded, - width = width, - height = height - ) - } finally { - thumbnail.flush() - } + ) } finally { original.flush() } } - private fun drawThumbnail( - original: BufferedImage, - thumbnail: BufferedImage, - transform: AffineTransform + private class SourceImage( + val original: BufferedImage, + val width: Int, + val height: Int, + val orientation: AffineTransform ) { - val graphics = thumbnail.createGraphics() + fun fit( + size: Int + ): Pair { - try { - graphics.color = Color.WHITE - graphics.fillRect(0, 0, thumbnail.width, thumbnail.height) + val longest = maxOf(width, height) - graphics.setRenderingHint( - RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_BILINEAR - ) + if (longest <= size) { + return width to height + } - graphics.setRenderingHint( - RenderingHints.KEY_RENDERING, - RenderingHints.VALUE_RENDER_QUALITY - ) + val scaledWidth = maxOf(1, (width.toLong() * size / longest).toInt()) + val scaledHeight = maxOf(1, (height.toLong() * size / longest).toInt()) - graphics.drawImage(original, transform, null) - } finally { - graphics.dispose() + return scaledWidth to scaledHeight } + + fun scaleTo( + width: Int, + height: Int + ): AffineTransform { + return AffineTransform.getScaleInstance( + width.toDouble() / this.width, + height.toDouble() / this.height + ) + } + } private fun readOrientation( diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcess.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcess.kt new file mode 100644 index 00000000..5ab8178b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcess.kt @@ -0,0 +1,64 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.future.await +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeoutOrNull +import java.util.concurrent.TimeUnit +import kotlin.time.Duration + +internal class ImageProcessTimeoutException : RuntimeException("image processing deadline exceeded") + +internal class ImageProcessResult( + val exitCode: Int, + val output: ByteArray +) + +internal suspend fun runImageProcess( + command: List, + input: ByteArray, + outputLimit: Int, + timeout: Duration +): ImageProcessResult = withContext(Dispatchers.IO) { + + val process = ProcessBuilder(command) + .redirectError(ProcessBuilder.Redirect.DISCARD) + .start() + + coroutineScope { + val writer = async { + process.outputStream.use { it.write(input) } + } + + val reader = async { + process.inputStream.use { it.readNBytes(outputLimit + 1) } + } + + try { + withTimeoutOrNull(timeout) { + val exitCode = process.onExit().await().exitValue() + val output = reader.await() + writer.await() + + check(output.size <= outputLimit) { "image process output exceeded limit" } + + ImageProcessResult( + exitCode = exitCode, + output = output + ) + } ?: throw ImageProcessTimeoutException() + } finally { + // Kill before the scope joins children that may be waiting on the pipes. + if (process.isAlive) { + process.descendants().use { children -> + children.forEach { it.destroyForcibly() } + } + + process.destroyForcibly() + process.waitFor(1, TimeUnit.SECONDS) + } + } + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt index 581cc459..a7fd2487 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingService.kt @@ -4,9 +4,9 @@ import com.mtusi.messenger.main.infrastructure.dto.ProcessedImage interface ImageProcessingService { - suspend fun thumbnail( + suspend fun variants( bytes: ByteArray, declaredType: String - ): ProcessedImage + ): Map } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/AttachmentOutboxDispatchService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/AttachmentOutboxDispatchService.kt new file mode 100644 index 00000000..37a69388 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/AttachmentOutboxDispatchService.kt @@ -0,0 +1,49 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment.jobs + +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentOutboxRepository +import com.mtusi.messenger.main.plugins.suspendedTransaction +import kotlinx.coroutines.CancellationException +import org.koin.core.annotation.Single +import java.util.UUID + +private const val PUBLISH_BATCH_SIZE = 32 + +@Single +class AttachmentOutboxDispatchService( + private val outboxRepository: AttachmentOutboxRepository, + private val jobBrokerService: JobBrokerService +) { + + suspend fun publishPending() { + + repeat(PUBLISH_BATCH_SIZE) { + val token = UUID.randomUUID() + val pending = suspendedTransaction { + outboxRepository.claimDue(token) + } ?: return + + try { + jobBrokerService.publish(pending.job) + } catch (cause: CancellationException) { + throw cause + } catch (cause: Exception) { + suspendedTransaction { + outboxRepository.reschedule( + id = pending.id, + token = token + ) + } + + throw cause + } + + suspendedTransaction { + outboxRepository.complete( + id = pending.id, + token = token + ) + } + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt deleted file mode 100644 index 2027d4b4..00000000 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/jobs/LegacyAttachmentDispatchService.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.mtusi.messenger.main.infrastructure.services.attachment.jobs - -import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository -import com.mtusi.messenger.main.plugins.suspendedTransaction -import org.koin.core.annotation.Single - -@Single -class LegacyAttachmentDispatchService( - private val jobRepository: AttachmentJobRepository, - private val jobBrokerService: JobBrokerService -) { - - suspend fun publishPending() { - - val jobs = suspendedTransaction { - jobRepository.findPendingLegacyJobs(limit = 32) - } - - for (job in jobs) { - jobBrokerService.publish(job) - - suspendedTransaction { - jobRepository.markLegacyJobDispatched(job.jobId) - } - } - } - -} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt index 2246ffc6..b87f3e67 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/ObjectStorageService.kt @@ -25,16 +25,18 @@ interface ObjectStorageService { id: UUID ) - suspend fun putThumbnail( + suspend fun putVariant( id: UUID, + name: String, bytes: ByteArray ) - suspend fun getThumbnail( - id: UUID + suspend fun getVariant( + id: UUID, + name: String ): ByteArray - suspend fun deleteThumbnail( + suspend fun deleteVariants( id: UUID ) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/S3ObjectStorageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/S3ObjectStorageService.kt index 63e3a0fd..e7d82505 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/S3ObjectStorageService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/storage/S3ObjectStorageService.kt @@ -85,29 +85,33 @@ class S3ObjectStorageService( deleteKey(getStagingKey(id)) } - override suspend fun putThumbnail( + override suspend fun putVariant( id: UUID, + name: String, bytes: ByteArray ) { request { client.putObject { bucket = config.bucket - key = getThumbnailKey(id) + key = getVariantKey(id, name) body = bytes.asByteStream() - contentType = "image/jpeg" + contentType = if (name == "optimized") "image/webp" else "image/jpeg" contentLength = bytes.size.toLong() } } } - override suspend fun getThumbnail( - id: UUID - ): ByteArray = getBytes(getThumbnailKey(id)) + override suspend fun getVariant( + id: UUID, + name: String + ): ByteArray = getBytes(getVariantKey(id, name)) - override suspend fun deleteThumbnail( + override suspend fun deleteVariants( id: UUID ) { - deleteKey(getThumbnailKey(id)) + for (name in listOf("thumbnail", "crop", "optimized")) { + deleteKey(getVariantKey(id, name)) + } } override suspend fun delete( @@ -115,7 +119,7 @@ class S3ObjectStorageService( ) { deleteKey(getObjectKey(id)) deleteStaging(id) - deleteThumbnail(id) + deleteVariants(id) } override suspend fun ready(): Boolean { @@ -173,9 +177,19 @@ class S3ObjectStorageService( id: UUID ) = "staging/$id" - private fun getThumbnailKey( - id: UUID - ) = "variants/$id/v1/thumbnail.jpg" + private fun getVariantKey( + id: UUID, + name: String + ): String { + val filename = when (name) { + "thumbnail" -> "thumbnail.jpg" + "crop" -> "crop.jpg" + "optimized" -> "optimized.webp" + else -> error("Unknown attachment variant") + } + + return "variants/$id/v1/$filename" + } private suspend fun request(block: suspend () -> T): T { return try { diff --git a/src/main/kotlin/com/mtusi/messenger/main/operator/AttachmentJobsCommand.kt b/src/main/kotlin/com/mtusi/messenger/main/operator/AttachmentJobsCommand.kt new file mode 100644 index 00000000..32811a64 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/operator/AttachmentJobsCommand.kt @@ -0,0 +1,105 @@ +package com.mtusi.messenger.main.operator + +import com.mtusi.messenger.main.config.database.SerialDatabaseConfig +import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedAttachmentRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.ExposedAttachmentJobRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.ExposedAttachmentOutboxRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.JobReplayResult +import com.mtusi.messenger.main.plugins.createDataSource +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.plugins.validateDatabaseMigrations +import com.typesafe.config.ConfigFactory +import io.ktor.server.config.HoconApplicationConfig +import io.ktor.server.config.getAs +import kotlinx.coroutines.runBlocking +import org.jetbrains.exposed.v1.core.DatabaseConfig +import org.jetbrains.exposed.v1.jdbc.Database +import java.util.UUID +import kotlin.system.exitProcess + +private val operatorPattern = Regex("[A-Za-z0-9_.@-]{1,80}") + +fun runAttachmentJobsCommand( + args: List +) { + + val subcommand = args.firstOrNull() + val replayJobId = if (subcommand == "replay" && args.size == 3) { + runCatching { UUID.fromString(args[1]) } + .getOrNull() + ?.takeIf { it.toString().equals(args[1], ignoreCase = true) } + } else { + null + } + val isList = args == listOf("failed") + val isReplay = replayJobId != null && operatorPattern.matches(args[2]) + + if (!isList && !isReplay) { + System.err.println("Usage: attachment-jobs failed | attachment-jobs replay JOB_ID OPERATOR") + exitProcess(2) + } + + val config = HoconApplicationConfig(ConfigFactory.load()) + .property("database") + .getAs() + + val exitCode = config.createDataSource().use { dataSource -> + validateDatabaseMigrations(dataSource) + Database.connect( + datasource = dataSource, + databaseConfig = DatabaseConfig { + defaultMaxAttempts = 1 + } + ) + + val jobs = ExposedAttachmentJobRepository( + attachmentRepository = ExposedAttachmentRepository(), + outboxRepository = ExposedAttachmentOutboxRepository() + ) + + runBlocking { + if (isList) { + val failed = suspendedTransaction { + jobs.findFailed(limit = 100) + } + + println("job_id\tattachment_id\tattempts\terror_code\tlast_error_code\tfailed_at\treplayable") + + for (job in failed) { + println( + listOf( + job.jobId, + job.attachmentId, + job.attempts, + job.errorCode, + job.lastErrorCode.orEmpty(), + job.failedAt, + job.replayable + ).joinToString("\t") + ) + } + 0 + } else { + val id = checkNotNull(replayJobId) + val result = suspendedTransaction { + jobs.replay( + id = id, + operator = args[2] + ) + } + + if (result == JobReplayResult.QUEUED) { + println("QUEUED ${id}") + 0 + } else { + System.err.println("REJECTED ${result.name}") + 2 + } + } + } + } + + if (exitCode != 0) { + exitProcess(exitCode) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt index d2c94b20..702aedee 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentWorker.kt @@ -2,8 +2,8 @@ package com.mtusi.messenger.main.plugins import com.mtusi.messenger.main.config.worker.WorkerConfig import com.mtusi.messenger.main.infrastructure.services.attachment.DefaultAttachmentProcessingService +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.AttachmentOutboxDispatchService import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService -import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.LegacyAttachmentDispatchService import io.ktor.server.application.Application import io.ktor.server.application.ApplicationStopping import io.ktor.server.application.log @@ -18,7 +18,7 @@ fun Application.configureAttachmentWorker() { val broker = get() val processingService = get() - val legacyDispatchService = get() + val dispatchService = get() val config = get() val workers = List(config.concurrency) { @@ -27,14 +27,14 @@ fun Application.configureAttachmentWorker() { } } - val legacyDispatch = launch { + val dispatch = launch { while (isActive) { try { - legacyDispatchService.publishPending() + dispatchService.publishPending() } catch (cause: CancellationException) { throw cause } catch (cause: Exception) { - log.warn("Legacy attachment dispatch failed ({}); retrying", cause.javaClass.simpleName) + log.warn("Attachment outbox dispatch failed ({}); retrying", cause.javaClass.simpleName) } delay(1.seconds) @@ -43,7 +43,7 @@ fun Application.configureAttachmentWorker() { monitor.subscribe(ApplicationStopping) { workers.forEach { it.cancel() } - legacyDispatch.cancel() + dispatch.cancel() broker.close() } 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 9a55309e..b2b3b0d1 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt @@ -3,7 +3,9 @@ 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.attachment.AttachmentJobModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobReplayModel import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentOutboxModel import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentVariantModel import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel @@ -65,7 +67,7 @@ fun Application.configureDatabase() { } -private fun DatabaseConfig.createDataSource(): HikariDataSource { +internal fun DatabaseConfig.createDataSource(): HikariDataSource { return HikariDataSource( HikariConfig().apply { poolName = "messenger" @@ -126,6 +128,8 @@ private fun generateMigration(description: String) { AttachmentModel, AttachmentVariantModel, AttachmentJobModel, + AttachmentJobReplayModel, + AttachmentOutboxModel, UploadJournalModel, MessageAttachmentModel, UserModel, diff --git a/src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt b/src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt index a326856a..e448c301 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/worker/Worker.kt @@ -32,7 +32,7 @@ fun Application.workerModule() { configureAttachmentCleanup() routing { - configureHealthRouting() + configureHealthRouting(requireBroker = true) } } diff --git a/src/main/resources/migrations/V10__attachment_job_replay.sql b/src/main/resources/migrations/V10__attachment_job_replay.sql new file mode 100644 index 00000000..4cfb918f --- /dev/null +++ b/src/main/resources/migrations/V10__attachment_job_replay.sql @@ -0,0 +1,13 @@ +ALTER TABLE attachment_jobs + ADD COLUMN replay_base_attempt INTEGER NOT NULL DEFAULT 0 + CHECK (replay_base_attempt >= 0 AND replay_base_attempt <= attempts); + +CREATE TABLE attachment_job_replays ( + id UUID PRIMARY KEY, + job_id UUID NOT NULL REFERENCES attachment_jobs(id), + operator VARCHAR(80) NOT NULL, + previous_error_code VARCHAR(64) NOT NULL, + attempt INTEGER NOT NULL CHECK (attempt > 0), + created_at TIMESTAMPTZ NOT NULL, + UNIQUE (job_id, attempt) +); diff --git a/src/main/resources/migrations/V11__complete_attachment_variants.sql b/src/main/resources/migrations/V11__complete_attachment_variants.sql new file mode 100644 index 00000000..5e68fc6a --- /dev/null +++ b/src/main/resources/migrations/V11__complete_attachment_variants.sql @@ -0,0 +1,36 @@ +ALTER TABLE attachments ADD COLUMN storage_cleanup_started BOOLEAN NOT NULL DEFAULT FALSE; + +-- Reuse existing images when upgrading a level-three deployment. +WITH upgraded_jobs AS ( + UPDATE attachment_jobs j + SET legacy_original = TRUE, + replay_base_attempt = j.attempts, + last_error_code = NULL, + next_attempt_at = CURRENT_TIMESTAMP + FROM attachments a + WHERE j.attachment_id = a.id + AND a.status = 'ready' + AND ( + SELECT COUNT(*) + FROM attachment_variants v + WHERE v.attachment_id = a.id AND v.name IN ('thumbnail', 'crop', 'optimized') + ) < 3 + RETURNING j.id, j.attachment_id, j.attempts +), queued_attachments AS ( + UPDATE attachments a + SET status = 'queued', + error_code = NULL, + updated_at = CURRENT_TIMESTAMP, + staging_cleaned = FALSE + FROM upgraded_jobs j + WHERE a.id = j.attachment_id + RETURNING a.id +) +INSERT INTO attachment_outbox (id, job_id, attempt) +SELECT gen_random_uuid(), j.id, j.attempts + 1 +FROM upgraded_jobs j +JOIN queued_attachments a ON a.id = j.attachment_id; + +DELETE FROM attachment_variants v +USING attachments a +WHERE v.attachment_id = a.id AND a.status = 'queued'; diff --git a/src/main/resources/migrations/V9__attachment_outbox.sql b/src/main/resources/migrations/V9__attachment_outbox.sql new file mode 100644 index 00000000..81b1be8d --- /dev/null +++ b/src/main/resources/migrations/V9__attachment_outbox.sql @@ -0,0 +1,23 @@ +CREATE TABLE attachment_outbox ( + id UUID PRIMARY KEY, + job_id UUID NOT NULL REFERENCES attachment_jobs(id), + attempt INTEGER NOT NULL CHECK (attempt > 0), + available_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + lease_token UUID, + lease_until TIMESTAMPTZ, + CHECK ((lease_token IS NULL) = (lease_until IS NULL)) +); + +CREATE INDEX attachment_outbox_due_idx ON attachment_outbox (available_at, id); + +-- Recover both pending lab4 imports and unfinished work already accepted by lab5. +INSERT INTO attachment_outbox (id, job_id, attempt, available_at) +SELECT gen_random_uuid(), + j.id, + CASE WHEN j.lease_until IS NULL THEN j.attempts + 1 ELSE j.attempts END, + GREATEST(CURRENT_TIMESTAMP, j.next_attempt_at, j.lease_until) +FROM attachment_jobs j +JOIN attachments a ON a.id = j.attachment_id +WHERE a.status IN ('queued', 'processing'); + +ALTER TABLE attachment_jobs DROP COLUMN legacy_dispatch_pending; diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt index 400a8e2e..5a1f20f6 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt @@ -66,7 +66,7 @@ class AttachmentControllerTest { val ready = client.awaitReady(attachment.id, bob) - assertEquals(listOf("thumbnail"), ready.variants.map { it.name }) + assertEquals(setOf("thumbnail", "crop", "optimized"), ready.variants.map { it.name }.toSet()) val path = "/api/v1/attachments/${attachment.id}" val download = client.get("$path/content") { auth(bob) } @@ -88,10 +88,16 @@ class AttachmentControllerTest { ImageIO.read(input) } } - val missingVariant = client.get("$path/content?variant=crop") { auth(bob) } + val crop = client.get("$path/content?variant=crop") { auth(bob) } + val optimized = client.get("$path/content?variant=optimized") { auth(bob) } + val missingVariant = client.get("$path/content?variant=unknown") { auth(bob) } assertEquals(32, decoded.width) assertEquals(20, decoded.height) + assertEquals(HttpStatusCode.OK, crop.status) + assertEquals(ContentType.Image.JPEG, crop.contentType()?.withoutParameters()) + assertEquals(HttpStatusCode.OK, optimized.status) + assertEquals(ContentType.parse("image/webp"), optimized.contentType()?.withoutParameters()) assertEquals(HttpStatusCode.NotFound, missingVariant.status) val outsiderChat = client.createChat(outsider, "outsider", listOf(outsider.id)) diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt index 32d6ca01..21a2213c 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt @@ -7,9 +7,9 @@ import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedAttachmentRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedUploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentOutboxRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.ExposedAttachmentJobRepository import com.mtusi.messenger.main.infrastructure.repositories.chat.ExposedChatRepository -import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService import com.mtusi.messenger.main.infrastructure.services.attachment.storage.S3ObjectStorageService import com.mtusi.messenger.main.infrastructure.types.ApiException @@ -81,12 +81,12 @@ class AttachmentCleanupTest { bytes = bytes, contentType = attachment.contentType ) - storage.putThumbnail(id = attachment.id, bytes = bytes) + storage.putVariant(id = attachment.id, name = "thumbnail", bytes = bytes) cleanup.cleanup() assertContentEquals(bytes, storage.get(attachment.id)) - assertContentEquals(bytes, storage.getThumbnail(attachment.id)) + assertContentEquals(bytes, storage.getVariant(attachment.id, "thumbnail")) suspendedTransaction { AttachmentModel.update(where = { AttachmentModel.id eq attachment.id }) { @@ -98,7 +98,7 @@ class AttachmentCleanupTest { assertTrue(storage.ready()) assertFailsWith { storage.get(attachment.id) } - assertFailsWith { storage.getThumbnail(attachment.id) } + assertFailsWith { storage.getVariant(attachment.id, "thumbnail") } } @Test @@ -126,8 +126,11 @@ class AttachmentCleanupTest { config = workerConfig ) - val jobs = ExposedAttachmentJobRepository(attachments) - val broker = application.get() + val outbox = application.get() + val jobs = ExposedAttachmentJobRepository( + attachmentRepository = attachments, + outboxRepository = outbox + ) val file = withContext(Dispatchers.IO) { Files.createTempFile("cleanup-test-", ".png") @@ -162,7 +165,7 @@ class AttachmentCleanupTest { chatRepository = ExposedChatRepository(), objectStorageService = failingStorage, jobRepository = jobs, - jobBrokerService = broker + outboxRepository = outbox ) assertFailsWith { @@ -194,7 +197,7 @@ class AttachmentCleanupTest { chatRepository = ExposedChatRepository(), objectStorageService = storage, jobRepository = jobs, - jobBrokerService = broker + outboxRepository = outbox ) val published = service.upload( chatId = chat.id, diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt index 241d2653..be297788 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentPipelineTest.kt @@ -7,9 +7,14 @@ import com.mtusi.messenger.main.infrastructure.dto.AttachmentJob import com.mtusi.messenger.main.infrastructure.dto.Message import com.mtusi.messenger.main.infrastructure.dto.ProcessedImage import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentJobReplayModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentOutboxModel import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.AttachmentJobRepository import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.JobClaim +import com.mtusi.messenger.main.infrastructure.repositories.attachment.jobs.JobReplayResult +import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.AttachmentOutboxDispatchService import com.mtusi.messenger.main.infrastructure.services.attachment.jobs.JobBrokerService import com.mtusi.messenger.main.infrastructure.services.attachment.storage.ObjectStorageService import com.mtusi.messenger.main.infrastructure.types.ApiException @@ -51,8 +56,10 @@ import kotlin.test.assertTrue import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.and import org.jetbrains.exposed.v1.core.longLiteral import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.select import org.jetbrains.exposed.v1.jdbc.selectAll import org.jetbrains.exposed.v1.jdbc.update import org.koin.ktor.ext.get @@ -119,7 +126,9 @@ class AttachmentPipelineTest { assertEquals("ready", ready.status) assertEquals(null, ready.errorCode) - val variant = ready.variants.single() + assertEquals(setOf("thumbnail", "crop", "optimized"), ready.variants.map { it.name }.toSet()) + + val variant = ready.variants.single { it.name == "thumbnail" } assertEquals("thumbnail", variant.name) assertEquals("image/jpeg", variant.contentType) @@ -229,7 +238,8 @@ class AttachmentPipelineTest { token = firstToken, errorCode = "storage_unavailable", permanent = false, - maxAttempts = 2 + maxAttempts = 2, + retryDelay = 5.seconds ) } val pendingRetry = client.get(path) { auth(user) }.body() @@ -258,7 +268,23 @@ class AttachmentPipelineTest { leaseSeconds = 60 ) } + val earlyClaim = suspendedTransaction { + jobs.claim( + job = retry, + token = UUID.randomUUID(), + maxAttempts = 2, + leaseSeconds = 60 + ) + } + + assertEquals(JobClaim.Complete, staleClaim) + assertEquals(JobClaim.Complete, earlyClaim) + val retryClaim = suspendedTransaction { + AttachmentJobModel.update(where = { AttachmentJobModel.id eq job.jobId }) { + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + jobs.claim( job = retry, token = secondToken, @@ -267,7 +293,6 @@ class AttachmentPipelineTest { ) } - assertEquals(JobClaim.Retry(2), staleClaim) assertEquals(JobClaim.Acquired(2), retryClaim) val claimedJob = suspendedTransaction { @@ -308,8 +333,53 @@ class AttachmentPipelineTest { assertEquals(HttpStatusCode.Conflict, unavailableContent.status) assertEquals(HttpStatusCode.Conflict, rejectedMessage.status) - assertTrue(application.get().process(job)) + val worker = application.get() + + assertTrue(worker.process(job)) assertEquals(failed, client.get(path) { auth(user) }.body()) + + val failedJob = suspendedTransaction { + jobs.findFailed(limit = 100).single { it.jobId == job.jobId } + } + + assertTrue(failedJob.replayable) + assertEquals("retry_exhausted", failedJob.errorCode) + assertEquals(2, failedJob.attempts) + + val replayResult = suspendedTransaction { jobs.replay(job.jobId, "integration-test") } + + assertEquals(JobReplayResult.QUEUED, replayResult) + assertTrue(worker.process(retry)) + assertEquals("queued", client.get(path) { auth(user) }.body().status) + + // The new run gets a fresh budget, while the absolute envelope number keeps increasing. + val replayedJob = job.copy(attempt = 3) + val replayToken = UUID.randomUUID() + val replayClaim = suspendedTransaction { + jobs.claim( + job = replayedJob, + token = replayToken, + maxAttempts = 1, + leaseSeconds = 60 + ) + } + + assertEquals(JobClaim.Acquired(3, attemptInRun = 1), replayClaim) + assertEquals( + JobReplayResult.NOT_FAILED, + suspendedTransaction { jobs.replay(job.jobId, "integration-test") } + ) + + val audit = suspendedTransaction { + AttachmentJobReplayModel + .selectAll() + .where(AttachmentJobReplayModel.jobId eq job.jobId) + .single() + } + + assertEquals("integration-test", audit[AttachmentJobReplayModel.operator]) + assertEquals(3, audit[AttachmentJobReplayModel.attempt]) + assertEquals("retry_exhausted", audit[AttachmentJobReplayModel.previousErrorCode]) } @Test @@ -347,6 +417,7 @@ class AttachmentPipelineTest { val next = upload("next") val broker = application.get() val worker = application.get() + val dispatcher = application.get() suspend fun metadata(attachment: Attachment): Attachment { return client.get("/api/v1/attachments/${attachment.id}") { auth(user) } @@ -362,6 +433,12 @@ class AttachmentPipelineTest { } coroutineScope { + val publisher = launch { + while (true) { + dispatcher.publishPending() + delay(50.milliseconds) + } + } val consumer = launch { broker.consume(worker::process) } @@ -374,11 +451,20 @@ class AttachmentPipelineTest { AttachmentJobModel.update(where = { AttachmentJobModel.id eq busyJob.jobId }) { it[leaseUntil] = CurrentTimestamp + longLiteral(-1) } + AttachmentOutboxModel.update( + where = { + (AttachmentOutboxModel.jobId eq busyJob.jobId) and + (AttachmentOutboxModel.attempt eq 1) + } + ) { + it[availableAt] = CurrentTimestamp + longLiteral(-1) + } } awaitReady(busy) } finally { consumer.cancelAndJoin() + publisher.cancelAndJoin() } } } @@ -405,10 +491,10 @@ class AttachmentPipelineTest { val calls = AtomicInteger() val processor = object : ImageProcessingService { - override suspend fun thumbnail( + override suspend fun variants( bytes: ByteArray, declaredType: String - ): ProcessedImage { + ): Map { calls.incrementAndGet() if (permanent) { @@ -424,16 +510,37 @@ class AttachmentPipelineTest { attachmentRepository = application.get(), objectStorageService = application.get(), imageProcessingService = processor, - jobBrokerService = application.get(), config = SerialWorkerConfig(maxAttempts = 2) ) + val startedAt = suspendedTransaction { + AttachmentJobModel + .select(CurrentTimestamp) + .where(AttachmentJobModel.id eq job.jobId) + .single()[CurrentTimestamp] + } + assertTrue(service.process(job)) if (!permanent) { val pendingRetry = client.get(path) { auth(user) }.body() assertEquals("queued", pendingRetry.status) + + val availableAt = suspendedTransaction { + AttachmentJobModel + .select(AttachmentJobModel.nextAttemptAt) + .where(AttachmentJobModel.id eq job.jobId) + .single()[AttachmentJobModel.nextAttemptAt] + } + assertTrue(assertNotNull(availableAt) >= startedAt + 500.milliseconds) + + // Advance the stored deadline instead of waiting on wall clock in the test. + suspendedTransaction { + AttachmentJobModel.update(where = { AttachmentJobModel.id eq job.jobId }) { + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + } assertTrue(service.process(job.copy(attempt = 2))) } @@ -447,6 +554,24 @@ class AttachmentPipelineTest { assertTrue(service.process(job)) assertEquals(expectedCalls, calls.get(), "terminal redelivery must not run the processor again") + + val jobs = application.get() + + if (permanent) { + assertEquals( + JobReplayResult.PERMANENT_FAILURE, + suspendedTransaction { jobs.replay(job.jobId, "integration-test") } + ) + } else { + val replay = suspendedTransaction { + AttachmentModel.update(where = { AttachmentModel.id eq queued.id }) { + it[storageCleanupStarted] = true + } + jobs.replay(job.jobId, "integration-test") + } + + assertEquals(JobReplayResult.SOURCE_EXPIRED, replay) + } } } diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt index 65783a11..08c8d2d2 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageProcessingServiceTest.kt @@ -25,12 +25,102 @@ import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertNull import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds class ImageProcessingServiceTest { private val service = DefaultImageProcessingService() private val colors = listOf(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW) + @Test + fun `published variants apply orientation and preserve the centered crop`() = runBlocking { + + val source = BufferedImage(1600, 800, BufferedImage.TYPE_INT_RGB) + val graphics = source.createGraphics() + + try { + graphics.color = Color.RED + graphics.fillRect(0, 0, 400, 800) + graphics.color = Color.GREEN + graphics.fillRect(400, 0, 800, 800) + graphics.color = Color.BLUE + graphics.fillRect(1200, 0, 400, 800) + } finally { + graphics.dispose() + } + + val bytes = withPngExif(encode(source, "png"), 6) + val variants = service.variants(bytes, "image/png") + + assertEquals(setOf("thumbnail", "crop", "optimized"), variants.keys) + assertEquals(128 to 256, variants.getValue("thumbnail").let { it.width to it.height }) + + val crop = variants.getValue("crop") + val decodedCrop = decode(crop.bytes) + + assertEquals("image/jpeg", crop.contentType) + assertEquals(128 to 128, decodedCrop.width to decodedCrop.height) + assertColor(Color.GREEN, Color(decodedCrop.getRGB(8, 8))) + assertColor(Color.GREEN, Color(decodedCrop.getRGB(120, 120))) + + val optimized = variants.getValue("optimized") + val decodedOptimized = decodeWebp(optimized.bytes) + + assertEquals("image/webp", optimized.contentType) + assertEquals(640 to 1280, optimized.width to optimized.height) + assertEquals(640 to 1280, decodedOptimized.width to decodedOptimized.height) + assertColor(Color.RED, Color(decodedOptimized.getRGB(250, 100))) + assertColor(Color.GREEN, Color(decodedOptimized.getRGB(250, 640))) + assertColor(Color.BLUE, Color(decodedOptimized.getRGB(250, 1180))) + assertFalse(webpChunks(optimized.bytes).any { it in setOf("EXIF", "XMP ", "ICCP") }) + } + + @Test + fun `optimized WebP preserves alpha and never enlarges a small source`() = runBlocking { + + val source = BufferedImage(80, 40, BufferedImage.TYPE_INT_ARGB) + val graphics = source.createGraphics() + + try { + graphics.color = Color(255, 0, 0, 128) + graphics.fillRect(0, 0, 40, 40) + } finally { + graphics.dispose() + } + + val optimized = service.variants(encode(source, "png"), "image/png").getValue("optimized") + val decoded = decodeWebp(optimized.bytes) + + assertEquals(80 to 40, optimized.width to optimized.height) + assertEquals(80 to 40, decoded.width to decoded.height) + assertEquals(128, Color(decoded.getRGB(20, 20), true).alpha) + assertEquals(0, Color(decoded.getRGB(60, 20), true).alpha) + } + + @Test + fun `worker subprocess preserves permanent image failures`() = runBlocking { + + val invalidImage = assertFailsWith { + service.variants("not an image".toByteArray(), "image/png") + } + + assertEquals(ErrorCode.UNSUPPORTED_MEDIA_TYPE, invalidImage.code) + } + + @Test + fun `image subprocess stops at its deadline`() = runBlocking { + + assertFailsWith { + runImageProcess( + command = listOf("sleep", "10"), + input = ByteArray(0), + outputLimit = 16, + timeout = 100.milliseconds + ) + } + } + @Test fun `thumbnail fits the box floors dimensions and never enlarges the source`() = runBlocking { @@ -222,6 +312,46 @@ class ImageProcessingServiceTest { } } + private suspend fun decodeWebp( + bytes: ByteArray + ): BufferedImage { + + val decoded = runImageProcess( + command = listOf("dwebp", "-quiet", "-o", "-", "--", "-"), + input = bytes, + outputLimit = MAX_ATTACHMENT_BYTES, + timeout = 10.seconds + ) + + assertEquals(0, decoded.exitCode) + + return decode(decoded.output) + } + + private fun webpChunks( + bytes: ByteArray + ): List { + + assertEquals("RIFF", String(bytes, 0, 4, Charsets.US_ASCII)) + assertEquals("WEBP", String(bytes, 8, 4, Charsets.US_ASCII)) + + return buildList { + var offset = 12 + + while (offset < bytes.size) { + add(String(bytes, offset, 4, Charsets.US_ASCII)) + + val size = ByteBuffer.wrap(bytes, offset + 4, 4) + .order(ByteOrder.LITTLE_ENDIAN) + .int + + offset += 8 + size + (size and 1) + } + + assertEquals(bytes.size, offset) + } + } + private fun tiff(orientation: Int): ByteArray { return ByteBuffer.allocate(26) .order(ByteOrder.LITTLE_ENDIAN) diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt index ca127059..2c9f84ec 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/LegacyAttachmentMigrationTest.kt @@ -75,8 +75,8 @@ class LegacyAttachmentMigrationTest { attachment } - val variant = ready.variants.single() - val thumbnail = storage.getThumbnail(id) + val variant = ready.variants.single { it.name == "thumbnail" } + val thumbnail = storage.getVariant(id = id, name = "thumbnail") assertContentEquals(original, storage.get(id)) assertEquals("thumbnail", variant.name) -- 2.54.0