From 8a96df4a74e49dbc0a790bcef90938ba6282f609 Mon Sep 17 00:00:00 2001 From: Danil01042005 Date: Mon, 16 Nov 2026 21:07:01 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9B=D0=A06:=20=D0=BD=D0=B0=D0=B1=D0=BB?= =?UTF-8?q?=D1=8E=D0=B4=D0=B0=D0=B5=D0=BC=D0=BE=D1=81=D1=82=D1=8C,=20SLO?= =?UTF-8?q?=20=D0=B8=20=D0=B4=D0=B8=D0=B0=D0=B3=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BA=D0=B0=20=D0=BE=D1=82=D0=BA=D0=B0=D0=B7=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 16 + .gitattributes | 2 + .github/pull_request_template.md | 42 +- .gitignore | 1 + Dockerfile | 19 + REPORT.md | 112 ++- build.gradle | 72 ++ compose.local.yaml | 4 + compose.test.yaml | 17 + docker-compose.yml | 374 ++++++++ evidence/accepted-jobs.json | 25 + evidence/alert-rules.txt | 2 + evidence/backend-incident.json | 86 ++ evidence/backpressure.json | 22 + evidence/contract-check.txt | 1 + evidence/database-incident.json | 904 ++++++++++++++++++ evidence/docker-build-worker.txt | 76 ++ evidence/docker-build.txt | 223 +++++ evidence/environment.json | 174 ++++ evidence/failover.txt | 3 + evidence/fenced-pending.json | 7 + evidence/incident-backend.txt | 1 + evidence/incident-db.txt | 3 + evidence/incident-queue.txt | 1 + evidence/integrity.txt | 1 + evidence/java-build.txt | 15 + evidence/job-schema.json | 5 + evidence/load.json | 17 + evidence/load.txt | 17 + evidence/metric-inventory.json | 127 +++ evidence/observe.txt | 1 + evidence/openapi-validation.txt | 1 + evidence/persistence.txt | 2 + evidence/pipeline-ack_guard.txt | 1 + evidence/pipeline-boundaries.txt | 2 + evidence/pipeline-core.txt | 5 + evidence/pipeline-faults.json | 67 ++ evidence/pipeline-faults.txt | 16 + evidence/pipeline-filename.txt | 1 + evidence/pipeline-history_capacity.txt | 1 + evidence/pipeline-retention.txt | 5 + evidence/pipeline-upload_crash.txt | 1 + evidence/queue-incident.json | 543 +++++++++++ evidence/resources.json | 167 ++++ evidence/restart-after.txt | 15 + evidence/restart-before.txt | 15 + evidence/signal-link.json | 126 +++ evidence/smoke.txt | 1 + evidence/source-sha256.json | 163 ++++ evidence/stack-final.txt | 16 + evidence/stack-ordinary.txt | 56 ++ evidence/stack-test-profile.txt | 83 ++ evidence/stop-one-after.txt | 1 + evidence/stop-one-before.txt | 1 + evidence/style.txt | 4 + evidence/telemetry-persistence.json | 6 + evidence/telemetry-persistence.txt | 1 + gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 46175 bytes gradle/wrapper/gradle-wrapper.properties | 7 + gradlew | 248 +++++ gradlew.bat | 93 ++ infra/otel/alerts.yaml | 45 + infra/otel/collector.yaml | 62 ++ infra/otel/grafana/dashboards/messenger.json | 501 ++++++++++ .../provisioning/dashboards/provider.yaml | 6 + .../provisioning/datasources/datasources.yaml | 22 + infra/otel/loki.yaml | 29 + infra/otel/prometheus.yaml | 13 + infra/otel/runbook.md | 22 + infra/otel/tempo.yaml | 20 + nginx.conf | 48 + policy.json | 21 + pyproject.toml | 3 + requirements-tools.txt | 4 + scripts/bootstrap.ps1 | 12 + scripts/capture_evidence.py | 117 +++ scripts/restart.cmd | 1 + scripts/restart.ps1 | 9 + scripts/restart.sh | 3 + scripts/run_tests.ps1 | 47 + scripts/stop-one.cmd | 1 + scripts/stop-one.ps1 | 13 + scripts/stop-one.sh | 9 + scripts/style.py | 87 ++ settings.gradle | 1 + .../messenger/MessengerLab1Application.java | 17 + .../messenger/cache/CacheHealthTracker.java | 19 + .../mtusi/messenger/config/JacksonConfig.java | 32 + .../mtusi/messenger/config/MinioConfig.java | 32 + .../mtusi/messenger/config/OpenApiConfig.java | 15 + .../messenger/config/RateLimitConfig.java | 29 + .../mtusi/messenger/config/RedisConfig.java | 117 +++ .../messenger/config/SecurityConfig.java | 86 ++ .../config/properties/AppProperties.java | 6 + .../properties/IdempotencyProperties.java | 6 + .../config/properties/InstanceProperties.java | 6 + .../properties/RateLimitProperties.java | 6 + .../config/properties/RedisProperties.java | 16 + .../config/properties/S3Properties.java | 40 + .../config/properties/SessionProperties.java | 8 + .../config/properties/UploadProperties.java | 12 + .../controller/AttachmentController.java | 43 + .../messenger/controller/AttachmentsApi.java | 314 ++++++ .../mtusi/messenger/controller/AuthApi.java | 138 +++ .../messenger/controller/AuthController.java | 28 + .../messenger/controller/ChatController.java | 37 + .../mtusi/messenger/controller/ChatsApi.java | 298 ++++++ .../mtusi/messenger/controller/HealthApi.java | 92 ++ .../controller/HealthController.java | 24 + .../controller/MessageController.java | 39 + .../messenger/controller/MessagesApi.java | 228 +++++ .../messenger/controller/UserController.java | 27 + .../mtusi/messenger/controller/UsersApi.java | 178 ++++ .../GlobalExceptionHandler.java | 247 +++++ .../ru/mtusi/messenger/dto/AttachmentDTO.java | 417 ++++++++ .../java/ru/mtusi/messenger/dto/ChatDTO.java | 155 +++ .../ru/mtusi/messenger/dto/ChatPageDTO.java | 105 ++ .../ru/mtusi/messenger/dto/CreateChatDTO.java | 107 +++ .../mtusi/messenger/dto/CreateMessageDTO.java | 60 ++ .../ru/mtusi/messenger/dto/CreateUserDTO.java | 120 +++ .../java/ru/mtusi/messenger/dto/ErrorDTO.java | 73 ++ .../ru/mtusi/messenger/dto/ErrorErrorDTO.java | 156 +++ .../ru/mtusi/messenger/dto/HealthDTO.java | 103 ++ .../ru/mtusi/messenger/dto/MessageDTO.java | 197 ++++ .../mtusi/messenger/dto/MessagePageDTO.java | 105 ++ .../ru/mtusi/messenger/dto/SessionDTO.java | 172 ++++ .../java/ru/mtusi/messenger/dto/UserDTO.java | 140 +++ .../ru/mtusi/messenger/dto/VariantDTO.java | 258 +++++ .../messenger/exception/ApiErrorFactory.java | 80 ++ .../exception/ConflictException.java | 7 + .../exception/InvalidRequestException.java | 7 + .../exception/NotFoundException.java | 7 + .../exception/PayloadTooLargeException.java | 7 + .../exception/RateLimitedException.java | 7 + .../ServiceUnavailableException.java | 7 + .../exception/StaleAttachmentException.java | 11 + .../StorageUnavailableException.java | 7 + .../exception/UnauthorizedException.java | 7 + .../UnsupportedMediaTypeException.java | 7 + .../messenger/filter/InstanceIdFilter.java | 33 + .../messenger/filter/RateLimitFilter.java | 54 ++ .../messenger/filter/RequestIdFilter.java | 35 + .../messenger/filter/TelemetryFilter.java | 122 +++ .../messenger/mapper/AttachmentMapper.java | 64 ++ .../ru/mtusi/messenger/mapper/ChatMapper.java | 36 + .../mtusi/messenger/mapper/MessageMapper.java | 31 + .../mtusi/messenger/mapper/SessionMapper.java | 20 + .../ru/mtusi/messenger/mapper/UserMapper.java | 21 + .../ru/mtusi/messenger/model/Attachment.java | 87 ++ .../messenger/model/AttachmentStatus.java | 10 + .../java/ru/mtusi/messenger/model/Chat.java | 67 ++ .../mtusi/messenger/model/IdempotencyKey.java | 54 ++ .../ru/mtusi/messenger/model/Message.java | 63 ++ .../ru/mtusi/messenger/model/Session.java | 48 + .../java/ru/mtusi/messenger/model/User.java | 60 ++ .../repository/AttachmentRepository.java | 18 + .../messenger/repository/ChatRepository.java | 52 + .../repository/IdempotencyRepository.java | 46 + .../repository/MessageRepository.java | 42 + .../repository/SessionRepository.java | 12 + .../messenger/repository/UserRepository.java | 23 + .../security/ApiAuthenticationEntryPoint.java | 43 + .../messenger/security/BasicCredentials.java | 29 + .../security/SessionAuthenticationFilter.java | 64 ++ .../SessionAuthenticationProvider.java | 36 + .../security/SessionAuthenticationToken.java | 34 + .../messenger/security/SessionPrincipal.java | 5 + .../messenger/service/AttachmentService.java | 273 ++++++ .../service/AttachmentVariantService.java | 57 ++ .../mtusi/messenger/service/AuthService.java | 38 + .../mtusi/messenger/service/ChatService.java | 135 +++ .../messenger/service/HealthService.java | 63 ++ .../messenger/service/IdempotencyService.java | 83 ++ .../messenger/service/MessageService.java | 164 ++++ .../service/ProcessingJobService.java | 63 ++ .../service/SessionCacheService.java | 74 ++ .../messenger/service/SessionService.java | 82 ++ .../mtusi/messenger/service/UserService.java | 88 ++ .../mtusi/messenger/telemetry/Telemetry.java | 169 ++++ .../ru/mtusi/messenger/util/CursorCodec.java | 41 + .../mtusi/messenger/util/TokenGenerator.java | 17 + .../ru/mtusi/messenger/util/TokenHasher.java | 37 + .../IdempotencyPayloadConverter.java | 31 + .../validation/AttachmentValidator.java | 81 ++ .../messenger/validation/ChatValidator.java | 68 ++ .../validation/IdempotencyValidator.java | 41 + src/main/resources/application.yaml | 128 +++ src/main/resources/db/migration/V1__init.sql | 180 ++++ .../messenger/AttachmentIntegrationTest.java | 49 + .../MessengerLab1ApplicationTests.java | 23 + student-tests/incidents.py | 317 ++++++ student-tests/observe.py | 185 ++++ student-tests/pipeline_ack_guard.py | 15 + student-tests/pipeline_boundaries.py | 133 +++ student-tests/pipeline_core.py | 142 +++ student-tests/pipeline_faults.py | 373 ++++++++ student-tests/pipeline_filename.py | 39 + student-tests/pipeline_history_capacity.py | 53 + student-tests/pipeline_retention.py | 140 +++ student-tests/pipeline_support.py | 171 ++++ student-tests/pipeline_upload_crash.py | 86 ++ student-tests/telemetry_support.py | 84 ++ worker/Dockerfile | 8 + worker/pipeline/__init__.py | 0 worker/pipeline/broker.py | 42 + worker/pipeline/cleanup.py | 64 ++ worker/pipeline/config.py | 24 + worker/pipeline/faults.py | 16 + worker/pipeline/images.py | 63 ++ worker/pipeline/main.py | 161 ++++ worker/pipeline/operator.py | 104 ++ worker/pipeline/outbox.py | 61 ++ worker/pipeline/process.py | 91 ++ worker/pipeline/repository.py | 171 ++++ worker/pipeline/storage.py | 62 ++ worker/pipeline/telemetry.py | 187 ++++ worker/requirements.txt | 6 + 217 files changed, 15534 insertions(+), 34 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 Dockerfile create mode 100644 build.gradle create mode 100644 compose.local.yaml create mode 100644 compose.test.yaml create mode 100644 docker-compose.yml create mode 100644 evidence/accepted-jobs.json create mode 100644 evidence/alert-rules.txt create mode 100644 evidence/backend-incident.json create mode 100644 evidence/backpressure.json create mode 100644 evidence/contract-check.txt create mode 100644 evidence/database-incident.json create mode 100644 evidence/docker-build-worker.txt create mode 100644 evidence/docker-build.txt create mode 100644 evidence/environment.json create mode 100644 evidence/failover.txt create mode 100644 evidence/fenced-pending.json create mode 100644 evidence/incident-backend.txt create mode 100644 evidence/incident-db.txt create mode 100644 evidence/incident-queue.txt create mode 100644 evidence/integrity.txt create mode 100644 evidence/java-build.txt create mode 100644 evidence/job-schema.json create mode 100644 evidence/load.json create mode 100644 evidence/load.txt create mode 100644 evidence/metric-inventory.json create mode 100644 evidence/observe.txt create mode 100644 evidence/openapi-validation.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/pipeline-ack_guard.txt create mode 100644 evidence/pipeline-boundaries.txt create mode 100644 evidence/pipeline-core.txt create mode 100644 evidence/pipeline-faults.json create mode 100644 evidence/pipeline-faults.txt create mode 100644 evidence/pipeline-filename.txt create mode 100644 evidence/pipeline-history_capacity.txt create mode 100644 evidence/pipeline-retention.txt create mode 100644 evidence/pipeline-upload_crash.txt create mode 100644 evidence/queue-incident.json create mode 100644 evidence/resources.json create mode 100644 evidence/restart-after.txt create mode 100644 evidence/restart-before.txt create mode 100644 evidence/signal-link.json create mode 100644 evidence/smoke.txt create mode 100644 evidence/source-sha256.json create mode 100644 evidence/stack-final.txt create mode 100644 evidence/stack-ordinary.txt create mode 100644 evidence/stack-test-profile.txt create mode 100644 evidence/stop-one-after.txt create mode 100644 evidence/stop-one-before.txt create mode 100644 evidence/style.txt create mode 100644 evidence/telemetry-persistence.json create mode 100644 evidence/telemetry-persistence.txt 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/otel/alerts.yaml create mode 100644 infra/otel/collector.yaml create mode 100644 infra/otel/grafana/dashboards/messenger.json create mode 100644 infra/otel/grafana/provisioning/dashboards/provider.yaml create mode 100644 infra/otel/grafana/provisioning/datasources/datasources.yaml create mode 100644 infra/otel/loki.yaml create mode 100644 infra/otel/prometheus.yaml create mode 100644 infra/otel/runbook.md create mode 100644 infra/otel/tempo.yaml create mode 100644 nginx.conf create mode 100644 policy.json create mode 100644 pyproject.toml create mode 100644 requirements-tools.txt create mode 100644 scripts/bootstrap.ps1 create mode 100644 scripts/capture_evidence.py create mode 100644 scripts/restart.cmd create mode 100644 scripts/restart.ps1 create mode 100755 scripts/restart.sh create mode 100644 scripts/run_tests.ps1 create mode 100644 scripts/stop-one.cmd create mode 100644 scripts/stop-one.ps1 create mode 100755 scripts/stop-one.sh create mode 100644 scripts/style.py create mode 100644 settings.gradle create mode 100644 src/main/java/ru/mtusi/messenger/MessengerLab1Application.java create mode 100644 src/main/java/ru/mtusi/messenger/cache/CacheHealthTracker.java create mode 100644 src/main/java/ru/mtusi/messenger/config/JacksonConfig.java create mode 100644 src/main/java/ru/mtusi/messenger/config/MinioConfig.java create mode 100644 src/main/java/ru/mtusi/messenger/config/OpenApiConfig.java create mode 100644 src/main/java/ru/mtusi/messenger/config/RateLimitConfig.java create mode 100644 src/main/java/ru/mtusi/messenger/config/RedisConfig.java create mode 100644 src/main/java/ru/mtusi/messenger/config/SecurityConfig.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/AppProperties.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/IdempotencyProperties.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/InstanceProperties.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/RateLimitProperties.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/RedisProperties.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/S3Properties.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/SessionProperties.java create mode 100644 src/main/java/ru/mtusi/messenger/config/properties/UploadProperties.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/AttachmentController.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/AttachmentsApi.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/AuthApi.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/AuthController.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/ChatController.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/ChatsApi.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/HealthApi.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/HealthController.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/MessageController.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/MessagesApi.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/UserController.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/UsersApi.java create mode 100644 src/main/java/ru/mtusi/messenger/controller/controllerAdvice/GlobalExceptionHandler.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/AttachmentDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/ChatDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/ChatPageDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/CreateChatDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/CreateMessageDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/CreateUserDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/ErrorDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/ErrorErrorDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/HealthDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/MessageDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/MessagePageDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/SessionDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/UserDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/dto/VariantDTO.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/ApiErrorFactory.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/ConflictException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/InvalidRequestException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/NotFoundException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/PayloadTooLargeException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/RateLimitedException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/ServiceUnavailableException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/StaleAttachmentException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/StorageUnavailableException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/UnauthorizedException.java create mode 100644 src/main/java/ru/mtusi/messenger/exception/UnsupportedMediaTypeException.java create mode 100644 src/main/java/ru/mtusi/messenger/filter/InstanceIdFilter.java create mode 100644 src/main/java/ru/mtusi/messenger/filter/RateLimitFilter.java create mode 100644 src/main/java/ru/mtusi/messenger/filter/RequestIdFilter.java create mode 100644 src/main/java/ru/mtusi/messenger/filter/TelemetryFilter.java create mode 100644 src/main/java/ru/mtusi/messenger/mapper/AttachmentMapper.java create mode 100644 src/main/java/ru/mtusi/messenger/mapper/ChatMapper.java create mode 100644 src/main/java/ru/mtusi/messenger/mapper/MessageMapper.java create mode 100644 src/main/java/ru/mtusi/messenger/mapper/SessionMapper.java create mode 100644 src/main/java/ru/mtusi/messenger/mapper/UserMapper.java create mode 100644 src/main/java/ru/mtusi/messenger/model/Attachment.java create mode 100644 src/main/java/ru/mtusi/messenger/model/AttachmentStatus.java create mode 100644 src/main/java/ru/mtusi/messenger/model/Chat.java create mode 100644 src/main/java/ru/mtusi/messenger/model/IdempotencyKey.java create mode 100644 src/main/java/ru/mtusi/messenger/model/Message.java create mode 100644 src/main/java/ru/mtusi/messenger/model/Session.java create mode 100644 src/main/java/ru/mtusi/messenger/model/User.java create mode 100644 src/main/java/ru/mtusi/messenger/repository/AttachmentRepository.java create mode 100644 src/main/java/ru/mtusi/messenger/repository/ChatRepository.java create mode 100644 src/main/java/ru/mtusi/messenger/repository/IdempotencyRepository.java create mode 100644 src/main/java/ru/mtusi/messenger/repository/MessageRepository.java create mode 100644 src/main/java/ru/mtusi/messenger/repository/SessionRepository.java create mode 100644 src/main/java/ru/mtusi/messenger/repository/UserRepository.java create mode 100644 src/main/java/ru/mtusi/messenger/security/ApiAuthenticationEntryPoint.java create mode 100644 src/main/java/ru/mtusi/messenger/security/BasicCredentials.java create mode 100644 src/main/java/ru/mtusi/messenger/security/SessionAuthenticationFilter.java create mode 100644 src/main/java/ru/mtusi/messenger/security/SessionAuthenticationProvider.java create mode 100644 src/main/java/ru/mtusi/messenger/security/SessionAuthenticationToken.java create mode 100644 src/main/java/ru/mtusi/messenger/security/SessionPrincipal.java create mode 100644 src/main/java/ru/mtusi/messenger/service/AttachmentService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/AttachmentVariantService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/AuthService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/ChatService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/HealthService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/IdempotencyService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/MessageService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/ProcessingJobService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/SessionCacheService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/SessionService.java create mode 100644 src/main/java/ru/mtusi/messenger/service/UserService.java create mode 100644 src/main/java/ru/mtusi/messenger/telemetry/Telemetry.java create mode 100644 src/main/java/ru/mtusi/messenger/util/CursorCodec.java create mode 100644 src/main/java/ru/mtusi/messenger/util/TokenGenerator.java create mode 100644 src/main/java/ru/mtusi/messenger/util/TokenHasher.java create mode 100644 src/main/java/ru/mtusi/messenger/util/converter/IdempotencyPayloadConverter.java create mode 100644 src/main/java/ru/mtusi/messenger/validation/AttachmentValidator.java create mode 100644 src/main/java/ru/mtusi/messenger/validation/ChatValidator.java create mode 100644 src/main/java/ru/mtusi/messenger/validation/IdempotencyValidator.java create mode 100644 src/main/resources/application.yaml create mode 100644 src/main/resources/db/migration/V1__init.sql create mode 100644 src/test/java/ru/mtusi/messenger/AttachmentIntegrationTest.java create mode 100644 src/test/java/ru/mtusi/messenger/MessengerLab1ApplicationTests.java create mode 100644 student-tests/incidents.py create mode 100644 student-tests/observe.py create mode 100644 student-tests/pipeline_ack_guard.py create mode 100644 student-tests/pipeline_boundaries.py create mode 100644 student-tests/pipeline_core.py create mode 100644 student-tests/pipeline_faults.py create mode 100644 student-tests/pipeline_filename.py create mode 100644 student-tests/pipeline_history_capacity.py create mode 100644 student-tests/pipeline_retention.py create mode 100644 student-tests/pipeline_support.py create mode 100644 student-tests/pipeline_upload_crash.py create mode 100644 student-tests/telemetry_support.py create mode 100644 worker/Dockerfile create mode 100644 worker/pipeline/__init__.py create mode 100644 worker/pipeline/broker.py create mode 100644 worker/pipeline/cleanup.py create mode 100644 worker/pipeline/config.py create mode 100644 worker/pipeline/faults.py create mode 100644 worker/pipeline/images.py create mode 100644 worker/pipeline/main.py create mode 100644 worker/pipeline/operator.py create mode 100644 worker/pipeline/outbox.py create mode 100644 worker/pipeline/process.py create mode 100644 worker/pipeline/repository.py create mode 100644 worker/pipeline/storage.py create mode 100644 worker/pipeline/telemetry.py create mode 100644 worker/requirements.txt diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..713ed32 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{java,py}] +indent_style = space +indent_size = 4 +max_line_length = 100 + +[*.java] +ij_java_class_count_to_use_import_on_demand = 999 +ij_java_names_count_to_use_import_on_demand = 999 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..51de0b9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +gradlew text eol=lf +*.sh text eol=lf diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 59c9f4e..a383dd5 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,32 @@ ## Лабораторная 6 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +**Автор и группа** — Иванов Данил, БВТ2301 +**Целевая оценка** — 5 +**Трек** — 2, SLO и обнаружение инцидентов -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Перенёс API и защищённую фоновую обработку вложений из своей ЛР5. Две API и два worker передают телеметрию через OpenTelemetry Collector в Prometheus, Loki и Tempo. Grafana загружает dashboard и три источника данных из provisioning. W3C trace context проходит через outbox и Redis Streams, поэтому в Tempo видны запрос, dispatcher, попытки worker, S3 и PostgreSQL. Правила Prometheus проверяют 5xx, queue age и burn rate двух SLO. -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +Запуск из чистого клона на Windows при работающем Docker Desktop. + +```powershell +pwsh -File scripts/bootstrap.ps1 +docker compose -f docker-compose.yml -f compose.local.yaml up -d --build +$env:BASE_URL='http://127.0.0.1:18085' +py scripts/check_contract.py +py tests/smoke.py +``` + +Запускать smoke после готовности двух API. Grafana открывается на `http://127.0.0.1:13006/d/messenger-lab6`. Полный набор проверок запускается через `pwsh -File scripts/run_tests.ps1`, зависимости описаны в отчёте. + +В опыте с БД единичный 503 не вызвал алерты. Продолжительная блокировка вызвала обычный 5xx и оба API burn rate, причина подтверждена дочерним span PostgreSQL. Остановка воркеров накопила 16 задач. После восстановления все дошли до READY, queue lag и оба READY burn rate вернулись в inactive. Во время остановки Loki 30 сообщений подтвердились и были прочитаны обратно. Collector зарегистрировал потери части логов, после старта Loki получен свежий лог. Детальные команды, результаты, замеры и ограничения находятся в [REPORT.md](../REPORT.md). + +Ограничения — один Docker-хост, отдельные одиночные хранилища, ускоренные окна оповещений. При длительной недоступности backend возможна потеря телеметрии. Результаты SLI относятся к наблюдённым событиям. + +Источники — собственная ЛР5 `ef7e8c3`, контракт курса, официальная документация OpenTelemetry, Grafana, Prometheus и Google SRE Workbook. Codex использован для переноса, инструментирования, экспериментов и подготовки отчёта. + +- [x] Обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. +- [x] Контракт и публичные тесты не ослаблены. +- [x] Python-команды из make check и make test выполнены, вывод приложен. +- [x] Сценарии отказов уровня и трека воспроизведены. +- [x] Секреты и приватные ключи вне Git, отчёт не содержит токены. +- [x] README задания сохранён, REPORT.md описывает запуск и проверки решения. diff --git a/.gitignore b/.gitignore index b5a7189..e9909b7 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ coverage/ .vscode/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. +.gradle/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8391123 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM eclipse-temurin:21-jdk-jammy@sha256:432e54724c3e57d1846aeb886f4d339f3b0522931c4e64119c2797013b7cfc3c AS builder +WORKDIR /build +COPY gradle gradle +COPY gradlew settings.gradle build.gradle ./ +COPY contracts contracts +COPY src src +RUN --mount=type=cache,target=/root/.gradle,sharing=locked chmod +x gradlew && ./gradlew bootJar -x test --no-daemon + +FROM eclipse-temurin:21-jre-jammy@sha256:dff6eb46527bd293a3c06bbbe9e64e8c9f91f01905b5abbdad31761c03059d2c AS runner +WORKDIR /app +RUN groupadd --system spring && useradd --system --gid spring --no-create-home spring +COPY --from=builder /build/build/libs/*.jar app.jar +RUN chown spring:spring app.jar +USER spring +EXPOSE 8080 +HEALTHCHECK --interval=10s --timeout=3s --start-period=20s --retries=3 \ + CMD wget -qO- http://127.0.0.1:8080/actuator/health/readiness | grep -q UP || exit 1 +ENV JAVA_OPTS="-XX:MaxRAMPercentage=75 -Xss512k" +ENTRYPOINT ["sh", "-c", "exec java -Dfile.encoding=UTF-8 $JAVA_OPTS -jar app.jar"] \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index b97ae42..2159591 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,109 @@ # Отчёт — лабораторная 6 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор и группа: Иванов Данил, БВТ2301. +- Целевая оценка: **5**, трек 2 «SLO и обнаружение инцидентов». +- База: skeleton ЛР6 `470d9d6`. Перенос из собственной ЛР5 `ef7e8c3`, исходники и одна миграция V1. Ветка решения `solution/bvt2301-ivanov`. +- Источники: контракт [наблюдаемости](contracts/observability.md), [W3C Trace Context](https://www.w3.org/TR/trace-context/), [OTel Collector](https://opentelemetry.io/docs/collector/configuration/), [Loki OTLP](https://grafana.com/docs/loki/latest/send-data/otel/), [Prometheus histograms](https://prometheus.io/docs/practices/histograms/), [Google SRE Workbook](https://sre.google/workbook/alerting-on-slos/). Codex использован при переносе, инструментировании, тестировании и подготовке отчёта. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Docker Desktop с Linux containers и Compose v2, PowerShell 7, Python 3.10+ и JDK 21 для локальной сборки и проверки стиля. Docker собирает Java через Gradle Wrapper 9.3.0 и Python через pip. Поддерживаемый профиль эксперимента один локальный Docker-хост. -## Архитектура и решения +```powershell +pwsh -File scripts/bootstrap.ps1 +docker compose config --quiet +docker compose -f docker-compose.yml -f compose.local.yaml up -d --build +$env:BASE_URL='http://127.0.0.1:18085' +docker compose ps +py scripts/check_contract.py +py tests/smoke.py +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab5, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Дождаться двух healthy API, `GET /health/ready`, Grafana `/api/health` и готовности хранилищ. Grafana открывается на http://127.0.0.1:13006/d/messenger-lab6. Обычная публикация API составляет 8080. В локальном override публикуется 18085. В Grafana доступен локальный Viewer, внутренние OTLP и порты хранилищ доступны сервисам Compose. Локальные пароли генерирует bootstrap в `secrets/`. Flyway применяет V1 на новой PostgreSQL и координирует миграцию между API. MinIO bootstrap создаёт приватный бакет и ограниченную учётную запись. + +```powershell +py -m pip install -r requirements-tools.txt +pwsh -File scripts/run_tests.ps1 +``` + +Python-команды выше эквивалентны `make check` и `make test`. Полный runner запускает Java, OpenAPI, style, перенесённые регрессии ЛР5, проверку сигналов, инциденты, нагрузку, persistence и failover. Запускать его последовательно на учебном стенде. Он временно включает профили инъекций ЛР5, останавливает нужные контейнеры и восстанавливает обычный профиль в `finally`. Методика диагностики находится в [runbook](infra/otel/runbook.md). Остановить с сохранением данных можно через `docker compose down`. `docker compose down -v` намеренно удаляет данные этого учебного стенда. + +## Архитектура + +```text +HTTP → nginx → Java API ×2 → PostgreSQL и приватный MinIO staging + COMMIT вложение + job + outbox → 202 + ↓ + dispatcher → Redis Streams → Python worker ×2 + worker: read S3 → variants → COMMIT READY → ACK + +API / dispatcher / worker → OTLP HTTP protobuf → Collector + metrics → Prometheus exporter :8889 → Prometheus TSDB + logs → Loki native OTLP + traces → Tempo OTLP gRPC +Prometheus / Loki / Tempo ← запросы Grafana +``` + +Из ЛР5 перенесены контракты поведения, PostgreSQL как источник истины для сессий, сообщений и заданий, лимиты очереди, bounded decode, S3 variant manifest, leases и версии. Публичные файлы контракта и тестов ЛР6 сохранены. Транзакция upload фиксирует вложение, задание и outbox. В её W3C carrier записывается контекст запроса. Dispatcher создаёт producer span с родителем HTTP span и передаёт его контекст с сообщением. Consumer получает producer как родителя. Дочерний image process получает контекст consumer. Retry публикуется с исходным контекстом из outbox и виден в той же трассе. PostgreSQL fencing по версии, lease token и deadline защищает COMMIT результата. Metrics/logs/traces обслуживают диагностику и не участвуют в подтверждении записи. + +Компоненты и версии закреплены в [Compose](docker-compose.yml), [Dockerfile](Dockerfile) и [worker Dockerfile](worker/Dockerfile). API использует Java 21, Spring Boot 4.1.1 и OTel Java 1.62.0. Worker использует Python 3.12.12, Pillow 12.0.0 и OTel Python 1.34.1. Collector contrib 0.128.0, Prometheus 3.4.1, Loki 3.5.1, Tempo 2.8.1, Grafana 12.0.2. Business volumes pgdata, redisdata, brokerdata, minio-data. Отдельные volumes Prometheus, Loki, Tempo и Grafana сохраняют историю и настройки. Хранилища телеметрии имеют окно хранения 24 часа, Prometheus также ограничен 512 MB. Внешними точками входа на хосте служат loopback LB и Grafana. + +`.env.example` задаёт логические настройки. PostgreSQL и S3 получают локальные credentials из Compose secret files, Redis broker доступен по имени `broker`. API и worker задают отдельные `service.name`, версию 6.0.0, instance ID из контейнера и окружение lab. Compose задаёт внутренний `OTEL_EXPORTER_OTLP_ENDPOINT` и HTTP/protobuf. Лимиты job и retention задаются в Compose. Для Java Hikari отражает оперативные gauge значений used/waiting/capacity. Во время недоступности аутентификационной БД возвращается контрактный JSON 503 с X-Request-Id. + +Collector применяет memory_limiter и batch. OTLP exporters к Loki и Tempo имеют по 32 элемента очереди, один consumer, 2 с timeout и до 10 с retry. Java SDK имеет ограниченные очереди логов и трасс по 256. Python SDK использует очереди по 128, короткий timeout и ограниченный flush в дочернем процессе. Приложение передаёт технические имена операций, outcome и безопасный UUID запроса. Экспорт произвольных application logs, payload, SQL-параметров и HTTP-заголовков выключен. Логи имеют timestamp, severity, service, event и активные trace/span ID. Лейблы метрик HTTP ограничены методом, шаблоном маршрута и классом статуса. + +## SLI, SLO и запросы + +| Сигнал | Метрика и смысл | +| --- | --- | +| API RPS / ошибки | `lab_http_requests_total`, sum(rate) по репликам. Ошибки — 5xx на запросах под /api/v1/. Health исключён, клиентские 4xx входят только в знаменатель | +| API p50/p95/p99 | `histogram_quantile(p, sum by(le)(rate(lab_http_duration_seconds_bucket[1m])))`. Суммируются бакеты, единица — секунды | +| Worker outcomes и время | `lab_jobs_total` по outcome, `lab_job_duration_seconds_bucket` в секундах. Метрика относится к доставке, включая retry, timeout, crash и duplicate | +| Queue depth / age | `lab_queue_depth` и `lab_queue_oldest_seconds` из PostgreSQL по QUEUED/PROCESSING. Возраст считается от создания старейшей работы | +| Pool и saturation | Hikari `lab_db_pool_connections`, `sum(lab_worker_busy)/sum(lab_worker_capacity)`, RSS bytes и CPU seconds по процессам. Worker process RSS относится к родительскому процессу. Container RSS включает дочерний декодер и измеряется отдельно через docker stats | +| Acceptance → READY | `lab_ready_duration_seconds_bucket`, завершение публикации минус время создания durable job в транзакции. Точность timestamps ограничена границей COMMIT | + +Гипотеза трека — совместная проверка двух окон и минимального объёма наблюдений подавит одиночный сбой, а длительный сбой БД и задержка обработки дадут локальные алерты с диагностируемой причиной. + +- API SLO **99%** ответов без 5xx за 30 минут учебного трафика. Бюджет ошибок 1%. Это цель для бизнес-запросов, дошедших до Java API. Ответы, сформированные балансировщиком до попадания в приложение, измеряются отдельно проверкой failover. +- READY SLO **95%** наблюдённых завершений за 30 минут публикуются в течение **5 с** от принятия. Бюджет 5%. `lab_ready_sli_total` учитывает успешные в пределах 5 с, поздние READY и терминальные ошибки, наблюдённые worker. Незавершённая работа до выхода в terminal видна в отдельном сигнале queue age. Если процесс завершился после COMMIT и до отправки наблюдения либо telemetry backend недоступен, измерение SLI может потеряться, о чём судят по счётчикам Collector и реестру jobs. Метрика ориентирована на учебную диагностику. +- Burn rate = наблюдаемая доля плохих событий / бюджет. Fast правила требуют **5×** и превышение в обоих окнах **1m / 15s**, минимум 6 наблюдений в длинном окне, `for: 9s`. Slow правила требуют **2×** в **3m / 45s**, минимум 12, `for: 18s`. Учебные окна сокращены для воспроизведения, scrape и evaluation по 3 с. Базовые алерты API 5xx >10% за 30 с и age >8 с с ненулевым backlog заданы отдельно. Источник вычислений — [alerts.yaml](infra/otel/alerts.yaml). Локальные Grafana panels берут правила и метрики из Prometheus. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Проверки сохраняют небольшие JSON и вывод команд. Секреты, реальные сообщения, картинки и сырые дампы исключены. Скрипты лежат в `student-tests/`, публичные — в `tests/`. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Критерий | Команда и наблюдение | Доказательство | +| --- | --- | --- | +| Скелет и контракт | Python check и валидация OpenAPI | [check](evidence/contract-check.txt), [validate](evidence/openapi-validation.txt) | +| Сквозной бизнес-API | Публичный smoke с включённой телеметрией | [smoke](evidence/smoke.txt) | +| Компиляция Java и стиль | Gradle test, style.py | [Java](evidence/java-build.txt), [style](evidence/style.txt) | +| Импортируемые Grafana и три сигнала | observe.py запрашивает реальные данные через три provisioned datasource и проверяет перечень метрик | [observe](evidence/observe.txt), [metrics](evidence/metric-inventory.json), [dashboard](infra/otel/grafana/dashboards/messenger.json) | +| W3C, retry, логи и приватность | Один upload, принудительный transient retry, серверный span → producer → два consumer, S3 и PostgreSQL; marker отсутствует | [trace и события](evidence/signal-link.json), [runtime payload schema](evidence/job-schema.json) | +| Правила и runbook | Promtool проверяет 6 правил, runbook связывает панели, Loki и Tempo | [rules](evidence/alert-rules.txt), [runbook](infra/otel/runbook.md) | +| Краткий и длительный отказ БД | Один отменённый запрос, затем ограниченная блокировка sessions. 503 и длительный span lookup отличают зависимость от handler | [замеры](evidence/database-incident.json), [вывод](evidence/incident-db.txt) | +| Worker stop, queue age, READY SLO | 16 принятых jobs, рост возраста, оба burn rate, все READY после запуска | [замеры](evidence/queue-incident.json), [вывод](evidence/incident-queue.txt) | +| Отказ Loki и ограничение очереди | Все записи сообщений сохранены. Зарегистрированы ошибки экспорта, после старта найден новый лог | [замеры](evidence/backend-incident.json), [вывод](evidence/incident-backend.txt) | +| Нагрузка API | 4 клиента, 30 с. 1 794 подтверждения, 0 ошибок, 59,61 RPS, p50/p95/p99 — 62/141/219 мс | [нагрузка](evidence/load.json) | +| Регрессии ЛР5 и конкуренция | Проверки форматов, ACL, EXIF/PNG/WebP, защиты Message и скачивания, истёкшего lease и лимита истории | [core](evidence/pipeline-core.txt), [retention](evidence/pipeline-retention.txt), [блокировки](evidence/pipeline-boundaries.txt), [ACK](evidence/pipeline-ack_guard.txt), [имена](evidence/pipeline-filename.txt), [SIGKILL](evidence/pipeline-upload_crash.txt), [лимит истории](evidence/pipeline-history_capacity.txt) | +| Сбои и повтор pipeline | 13 сценариев с действительными остановками процессов, перезапуском Redis и проверкой версий. 19 подтверждённых ID, 0 пропавших или незавершённых | [faults](evidence/pipeline-faults.txt), [реестр](evidence/accepted-jobs.json), [backpressure](evidence/backpressure.json) | +| Persistence и история telemetry | Перезапуск business и telemetry сервисов. Прежняя сессия и подтверждённые данные сохранились. Loki нашёл исходный лог, Tempo вернул 14 spans | [persistence](evidence/persistence.txt), [до](evidence/restart-before.txt), [после](evidence/restart-after.txt), [история сигналов](evidence/telemetry-persistence.json) | +| Failover | Остановка обслуживающей API, 94 успешных записи, 0 ошибок, первое восстановление через 2,5 с | [failover](evidence/failover.txt), [до](evidence/stop-one-before.txt), [после](evidence/stop-one-after.txt) | +| Среда и артефакты | Повторное состояние двух healthy API, версии и лимиты контейнеров, SHA-256 исходников, выборка docker stats | [environment](evidence/environment.json), [resources](evidence/resources.json), [исходники](evidence/source-sha256.json) | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Эксперименты выполнены 16.11.2026 на синтетических данных. Docker VM — 8 CPU и около 7,65 GiB RAM. CPU/RAM зафиксированы дискретным снимком docker stats и в опыте с backpressure. Замеры одного стенда не определяют потолок пропускной способности. Быстрый fault profile ЛР5 для повторной выдачи использует lease 8 с, timeout 25 с и 3 попытки. Длительные инциденты SLO запускаются на обычном профиле. При сравнении времени учитываются дискретность scrape/evaluation и период экспортёра SDK. Нагрузочный тест closed loop даёт наблюдаемые latency/RPS клиента и включает только успешные ответы в квантилях. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Воздействие | Что наблюдалось | Восстановление | +| --- | --- | --- | +| Короткая блокировка БД и отмена одного ожидающего чтения | Один ответ 503, ни одного firing в наблюдаемом интервале. Ноль ложных срабатываний в одном контролируемом опыте | Трафик продолжился с прежней сессией | +| Длительная блокировка sessions | При 6 клиентах за весь опыт 1 887 ответов 200 и 55 ответов 503. HighError загорелся через 34,74 с, API Fast через 50,08 с, API Slow через 46,99 с от захвата lock. Span postgres.session.lookup длился 5,003 с и завершился ошибкой | Снимки inactive через 17,47 / 11,31 / 35,89 с от снятия lock соответственно | +| Остановка обоих workers | 16 принятых задач. QueueLag firing через 27,50 с от начала фазы, снимок age 19,78 с, backlog 16, экспонированная capacity 0. После запуска Ready Fast firing через 26,47 с, Slow через 35,75 с | Все 16 дошли до READY за 10,08 с после запуска. QueueLag проверен inactive на 35,75 с. READY Fast и Slow проверены inactive на 35,75 и 60,34 с | +| Остановка Loki | 30 сообщений подтверждены и прочитаны, 5 записей в счётчике ошибок экспорта дополнительно. Наблюдавшаяся очередь до 18 при capacity 32. Максимум process RSS Collector по дискретным пробам 215,03 MiB, включает отображённые страницы процесса | Loki восстановился, свежий лог записи коррелирует с новым trace ID. Другие telemetry pipelines продолжили работу | -## Ограничения и следующие шаги +Замеры обнаружения и восстановления соответствуют первой наблюдённой пробе скрипта и включают задержки экспорта, scrape и evaluation. В первом опыте проверка ошибочно требовала одновременный firing независимых правил fast и slow. Проверка исправлена, каждое правило отслеживается отдельно. Конфликт служебного поля Python logging `created` устранялся до успешного эксперимента очереди. Исходный код приложения и подтверждения финальных прогонов приведены выше. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +## Ограничения + +Локальный учебный стенд с одной БД, одним S3, одним брокером и одним экземпляром каждого telemetry backend. Сценарии показывают восстановление бизнес-операций в проверенных отказах и возможность потерять телеметрию при заполнении очереди или истечении retry. Burn rate измеряет наблюдаемый трафик и зависит от наличия свежих samples. Процентиль оценочный, его разрешение определяется границами бакетов. Всплеск нагрузки измеряется на одном Docker-хосте и не устанавливает предел производительности. Версионирование pipeline и идемпотентность эффекта сохраняются при повторной доставке. diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..e15a555 --- /dev/null +++ b/build.gradle @@ -0,0 +1,72 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '4.1.1' + id 'io.spring.dependency-management' version '1.1.7' +} + +group = 'ru.mtusi' +version = '0.0.1-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.62.0' + implementation 'io.opentelemetry:opentelemetry-exporter-otlp:1.62.0' + + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation 'org.springframework.boot:spring-boot-starter-webmvc' + + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:3.1.0' + implementation 'com.bucket4j:bucket4j-core:8.10.1' + implementation 'org.mapstruct:mapstruct:1.6.3' + implementation 'org.postgresql:postgresql:42.7.13' + implementation 'org.springframework.boot:spring-boot-starter-flyway' + runtimeOnly 'org.flywaydb:flyway-database-postgresql' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa:4.1.1' + implementation 'org.springframework.boot:spring-boot-starter-data-redis:4.1.1' + implementation 'org.springframework.security:spring-security-crypto' + implementation 'org.bouncycastle:bcprov-jdk18on:1.78.1' + implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server' + implementation 'org.springframework.boot:spring-boot-starter-cache' + implementation 'org.springframework.boot:spring-boot-starter-aspectj' + implementation 'org.springframework.boot:spring-boot-starter-jackson' + implementation 'io.minio:minio:9.0.3' + + + runtimeOnly 'io.netty:netty-transport-native-epoll::linux-x86_64' + + testImplementation 'org.springframework.boot:spring-boot-starter-test:4.1.1' + + testImplementation 'org.springframework.boot:spring-boot-starter-restclient-test' + testImplementation 'org.springframework.boot:spring-boot-resttestclient' + + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.3' + annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0' + annotationProcessor 'org.projectlombok:lombok' + testCompileOnly 'org.projectlombok:lombok' + testAnnotationProcessor 'org.projectlombok:lombok' + + testImplementation 'org.springframework.boot:spring-boot-starter-actuator-test' + testImplementation 'org.springframework.boot:spring-boot-starter-validation-test' + testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' +} + +tasks.withType(JavaCompile).configureEach { + options.compilerArgs << '-parameters' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/compose.local.yaml b/compose.local.yaml new file mode 100644 index 0000000..51fad49 --- /dev/null +++ b/compose.local.yaml @@ -0,0 +1,4 @@ +services: + lb: + ports: !override + - "127.0.0.1:18085:80" diff --git a/compose.test.yaml b/compose.test.yaml new file mode 100644 index 0000000..1f9aafc --- /dev/null +++ b/compose.test.yaml @@ -0,0 +1,17 @@ +services: + api: + environment: + JOB_MAX_ATTEMPTS: 3 + MAX_PENDING_JOBS: 6 + worker: + environment: + FAULT_INJECTION_ENABLED: "true" + JOB_MAX_ATTEMPTS: 3 + JOB_LEASE_SECONDS: 8 + JOB_TIMEOUT_SECONDS: 25 + dispatcher: + environment: + FAULT_INJECTION_ENABLED: "true" + JOB_MAX_ATTEMPTS: 3 + JOB_LEASE_SECONDS: 8 + JOB_TIMEOUT_SECONDS: 25 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..dcc3d98 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,374 @@ +services: + db: + image: postgres:16-alpine@sha256:0cefaabd882e42bcb2a2f3dc56d3ce4770560ba62fd7f197f1a2e008fd5731ab + restart: unless-stopped + environment: + POSTGRES_DB_FILE: /run/secrets/postgres_db + POSTGRES_USER_FILE: /run/secrets/postgres_user + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + volumes: + - pgdata:/var/lib/postgresql/data + secrets: + - postgres_db + - postgres_user + - postgres_password + networks: + - messenger-net + healthcheck: + # Добавлена очистка от \n и \r для надежности на Windows + test: ["CMD-SHELL", "pg_isready -U $$(cat /run/secrets/postgres_user | tr -d '\\n\\r') -d $$(cat /run/secrets/postgres_db | tr -d '\\n\\r')"] + interval: 5s + timeout: 5s + retries: 10 + start_period: 10s + deploy: + resources: + limits: + cpus: '1.0' + memory: 1G + + redis: + image: redis:7.4-alpine@sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f + restart: unless-stopped + command: redis-server --appendonly yes --appendfsync everysec --save "" + volumes: + - redisdata:/data + networks: + - messenger-net + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + + broker: + image: redis:7.4-alpine@sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f + restart: unless-stopped + command: redis-server --appendonly yes --appendfsync always --save "" + volumes: + - brokerdata:/data + networks: + - messenger-net + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + + worker: &pipeline-worker + build: + context: . + dockerfile: worker/Dockerfile + restart: unless-stopped + environment: &pipeline-environment + ROLE: worker + OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4318 + DB_HOST: db + BROKER_HOST: broker + S3_ENDPOINT: http://minio:9000 + S3_BUCKET: messenger-attachments + JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} + JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60} + JOB_LEASE_SECONDS: ${JOB_LEASE_SECONDS:-75} + STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} + FAULT_INJECTION_ENABLED: ${FAULT_INJECTION_ENABLED:-false} + secrets: + - postgres_db + - postgres_user + - postgres_password + - s3_access_key_id + - s3_secret_access_key + networks: + - messenger-net + depends_on: + api: + condition: service_healthy + broker: + condition: service_healthy + deploy: + replicas: ${WORKER_CONCURRENCY:-2} + resources: + limits: + cpus: '1.0' + memory: 1G + + dispatcher: + <<: *pipeline-worker + environment: + <<: *pipeline-environment + ROLE: dispatcher + deploy: + replicas: 1 + resources: + limits: + cpus: '0.5' + memory: 256M + + migrate: + profiles: [migration] + build: + context: . + dockerfile: Dockerfile + entrypoint: + - "sh" + - "-c" + - | + export SPRING_DATASOURCE_USERNAME="$$(cat /run/secrets/postgres_user)"; + export SPRING_DATASOURCE_PASSWORD="$$(cat /run/secrets/postgres_password)"; + export S3_ACCESS_KEY_ID="$$(cat /run/secrets/s3_access_key_id)"; + export S3_SECRET_ACCESS_KEY="$$(cat /run/secrets/s3_secret_access_key)"; + exec java -Dfile.encoding=UTF-8 -Dspring.main.web-application-type=none -Dspring.main.keep-alive=false -jar app.jar + environment: + SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/messenger + SPRING_FLYWAY_ENABLED: "true" + RECONCILER_ENABLED: "false" + SPRING_TASK_SCHEDULING_ENABLED: "false" # Отключает фоновые задачи, чтобы контейнер успешно завершился + secrets: + - postgres_user + - postgres_password + - s3_access_key_id + - s3_secret_access_key + depends_on: + db: + condition: service_healthy + restart: "no" + networks: + - messenger-net + healthcheck: + disable: true + + api: + stop_grace_period: 40s + build: + context: . + dockerfile: Dockerfile + restart: unless-stopped + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + minio-init: + condition: service_completed_successfully + entrypoint: + - "sh" + - "-c" + - | + export INSTANCE_ID="$$(cat /etc/hostname)"; + export OTEL_RESOURCE_ATTRIBUTES="deployment.environment.name=lab,service.version=6.0.0,service.instance.id=$$INSTANCE_ID"; + export SPRING_DATASOURCE_USERNAME="$$(cat /run/secrets/postgres_user)"; + export SPRING_DATASOURCE_PASSWORD="$$(cat /run/secrets/postgres_password)"; + export S3_ACCESS_KEY_ID="$$(cat /run/secrets/s3_access_key_id)"; + export S3_SECRET_ACCESS_KEY="$$(cat /run/secrets/s3_secret_access_key)"; + exec java -Dfile.encoding=UTF-8 $$JAVA_OPTS -jar app.jar + environment: + RECONCILER_INTERVAL_MS: ${RECONCILER_INTERVAL_MS:-3600000} + RECONCILER_DRY_RUN: ${RECONCILER_DRY_RUN:-false} + RECONCILER_GRACE_PERIOD_MINUTES: ${RECONCILER_GRACE_PERIOD_MINUTES:-15} + JAVA_OPTS: -Xmx320m -Xss512k + OTEL_SERVICE_NAME: messenger-api + OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4318 + OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf + OTEL_EXPORTER_OTLP_TIMEOUT: 1500 + OTEL_METRIC_EXPORT_INTERVAL: 5000 + OTEL_BSP_MAX_QUEUE_SIZE: 256 + OTEL_BSP_MAX_EXPORT_BATCH_SIZE: 64 + OTEL_BLRP_MAX_QUEUE_SIZE: 256 + OTEL_BLRP_MAX_EXPORT_BATCH_SIZE: 64 + OTEL_PROPAGATORS: tracecontext + JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} + MAX_PENDING_JOBS: ${MAX_PENDING_JOBS:-100} + RATE_PER_MINUTE: ${RATE_PER_MINUTE:-100000} + SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/messenger + SPRING_PROFILES_ACTIVE: ${APP_ENV:-lab} + SPRING_FLYWAY_ENABLED: "true" + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-400} + SESSION_STORE: ${SESSION_STORE:-postgres} + SPRING_DATA_REDIS_HOST: redis + SPRING_DATA_REDIS_PORT: 6379 + MAX_MESSAGES_PER_CHAT: ${MAX_MESSAGES_PER_CHAT:-10000} + S3_ENDPOINT: http://minio:9000 + S3_BUCKET: messenger-attachments + S3_REGION: us-east-1 + secrets: + - postgres_user + - postgres_password + - s3_access_key_id + - s3_secret_access_key + networks: + - messenger-net + deploy: + replicas: 2 + resources: + limits: + cpus: '1.0' + memory: 512M + healthcheck: + test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8080/health/ready | grep -q '\"status\":\"ok\"'"] + interval: 5s + timeout: 3s + retries: 20 + start_period: 40s + + lb: + image: nginx:1.27-alpine@sha256:65645c7bb6a0661892a8b03b89d0743208a18dd2f3f17a54ef4b76fb8e2f2a10 + restart: unless-stopped + ports: + - "127.0.0.1:8080:80" + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + api: + condition: service_healthy + networks: + - messenger-net + + minio: + image: pgsty/silo@sha256:7bb2aff6b89efe1cb1303c10fc21d579447434ed06649ac9ec05a5e2dbf30ad7 + command: server /data --console-address ":9001" + environment: + MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + MINIO_BROWSER: "off" + volumes: + - minio-data:/data + secrets: + - minio_root_user + - minio_root_password + networks: + - messenger-net + healthcheck: + test: ["CMD-SHELL", "curl -f http://localhost:9000/minio/health/ready || exit 1"] + interval: 5s + timeout: 5s + retries: 10 + start_period: 10s + + minio-init: + image: pgsty/mc@sha256:ce62c9053c69ce12a527a322345b5ad9f44b5f8a7b8189f82e516f2e3f30f1d8 + depends_on: + minio: + condition: service_healthy + entrypoint: > + /bin/sh -ec " + export MINIO_ROOT_USER=$$(cat /run/secrets/minio_root_user); + export MINIO_ROOT_PASSWORD=$$(cat /run/secrets/minio_root_password); + export S3_ACCESS_KEY_ID=$$(cat /run/secrets/s3_access_key_id); + export S3_SECRET_ACCESS_KEY=$$(cat /run/secrets/s3_secret_access_key); + until mc alias set local http://minio:9000 $$MINIO_ROOT_USER $$MINIO_ROOT_PASSWORD; do + echo 'Waiting for MinIO...' && sleep 2; + done; + mc mb --ignore-existing local/messenger-attachments; + mc anonymous set none local/messenger-attachments; + mc admin user add local $$S3_ACCESS_KEY_ID $$S3_SECRET_ACCESS_KEY; + mc admin policy create local app-uploads-policy /tmp/policy.json; + mc admin policy attach local app-uploads-policy --user $$S3_ACCESS_KEY_ID; + echo 'MinIO initialization completed (bucket: messenger-attachments)'; + " + volumes: + - ./policy.json:/tmp/policy.json:ro + secrets: + - minio_root_user + - minio_root_password + - s3_access_key_id + - s3_secret_access_key + networks: + - messenger-net + restart: "no" + + otel-collector: + image: otel/opentelemetry-collector-contrib:0.128.0@sha256:1ab0baba0ee3695d823c46653d8a6e8894896e668ce8bd7ebe002e948d827bc7 + command: ["--config=/etc/otelcol/collector.yaml"] + volumes: + - ./infra/otel/collector.yaml:/etc/otelcol/collector.yaml:ro + networks: [messenger-net] + mem_limit: 192m + restart: unless-stopped + + prometheus: + image: prom/prometheus:v3.4.1@sha256:9abc6cf6aea7710d163dbb28d8eeb7dc5baef01e38fa4cd146a406dd9f07f70d + command: + - --config.file=/etc/prometheus/prometheus.yaml + - --storage.tsdb.path=/prometheus + - --storage.tsdb.retention.time=24h + - --storage.tsdb.retention.size=512MB + volumes: + - ./infra/otel/prometheus.yaml:/etc/prometheus/prometheus.yaml:ro + - ./infra/otel/alerts.yaml:/etc/prometheus/alerts.yaml:ro + - prometheus-data:/prometheus + networks: [messenger-net] + mem_limit: 256m + restart: unless-stopped + + loki: + image: grafana/loki:3.5.1@sha256:a74594532eec4cc313401beedc4dd2708c43674c032084b1aeb87c14a5be1745 + command: ["-config.file=/etc/loki/loki.yaml"] + volumes: + - ./infra/otel/loki.yaml:/etc/loki/loki.yaml:ro + - loki-data:/loki + networks: [messenger-net] + mem_limit: 384m + restart: unless-stopped + + tempo: + image: grafana/tempo:2.8.1@sha256:bc9245fe3da4e63dc4c6862d9c2dad9bcd8be13d0ba4f7705fa6acda4c904d0e + command: ["-config.file=/etc/tempo/tempo.yaml"] + volumes: + - ./infra/otel/tempo.yaml:/etc/tempo/tempo.yaml:ro + - tempo-data:/var/tempo + networks: [messenger-net] + mem_limit: 256m + restart: unless-stopped + + grafana: + image: grafana/grafana:12.0.2@sha256:b5b59bfc7561634c2d7b136c4543d702ebcc94a3da477f21ff26f89ffd4214fa + environment: + GF_AUTH_ANONYMOUS_ENABLED: "true" + GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer + GF_AUTH_DISABLE_LOGIN_FORM: "true" + GF_SECURITY_ADMIN_PASSWORD__FILE: /run/secrets/grafana_admin_password + GF_ANALYTICS_REPORTING_ENABLED: "false" + GF_ANALYTICS_CHECK_FOR_UPDATES: "false" + GF_ANALYTICS_CHECK_FOR_PLUGIN_UPDATES: "false" + GF_PLUGINS_PREINSTALL_DISABLED: "true" + secrets: [grafana_admin_password] + ports: ["127.0.0.1:${GRAFANA_PORT:-13006}:3000"] + volumes: + - ./infra/otel/grafana/provisioning:/etc/grafana/provisioning:ro + - ./infra/otel/grafana/dashboards:/var/lib/grafana/dashboards:ro + - grafana-data:/var/lib/grafana + networks: [messenger-net] + mem_limit: 256m + restart: unless-stopped + +volumes: + prometheus-data: + loki-data: + tempo-data: + grafana-data: + pgdata: + redisdata: + minio-data: + brokerdata: + +networks: + messenger-net: + driver: bridge + +secrets: + grafana_admin_password: + file: ./secrets/grafana_admin_password.txt + postgres_db: + file: ./secrets/postgres_db.txt + postgres_user: + file: ./secrets/postgres_user.txt + postgres_password: + file: ./secrets/postgres_password.txt + minio_root_user: + file: ./secrets/minio_root_user.txt + minio_root_password: + file: ./secrets/minio_root_password.txt + s3_access_key_id: + file: ./secrets/s3_access_key_id.txt + s3_secret_access_key: + file: ./secrets/s3_secret_access_key.txt diff --git a/evidence/accepted-jobs.json b/evidence/accepted-jobs.json new file mode 100644 index 0000000..1d8202f --- /dev/null +++ b/evidence/accepted-jobs.json @@ -0,0 +1,25 @@ +{ + "accepted_ids": [ + "4ae0038c-40e8-4876-97d8-a0f60c4a96f3", + "55553045-1282-4d81-9554-f2d03432084d", + "ad3dbb4f-f786-489a-914f-770ff02108ee", + "7c01327e-cc02-4efd-9ead-c92b79bb8de0", + "e3d6ce50-0213-41ad-bb43-2ba44b018350", + "909f33db-3d27-4b2f-9245-512d024c3be8", + "3e9602ff-ae12-4c24-a187-9ea817cf3c16", + "2d9064b5-b6ed-43b2-b13d-504c354a6927", + "05d0a7f9-d3c4-4684-863c-0c19ebfb5c08", + "23827744-c11f-4d4a-910c-9f07a9bf155c", + "fe912003-c052-4dd1-9910-ee9f28d38dd7", + "5b556b69-95de-402a-bd57-28cc57d65c38", + "1bf4404d-ee92-471f-800e-d055a49352d1", + "72754073-c039-4797-ac0d-b86b3feb0a79", + "13022e75-3f97-46ff-a12d-315cec7b2113", + "3ee1ce6e-c59d-40cc-a5fa-19998f8bd230", + "61288e7d-dbdb-450f-bfeb-02441f02c18c", + "b5d77aef-8a6e-429f-9e65-58112adb2382", + "8ddf9bf1-69c9-4e59-91f7-48f7d08adb5d" + ], + "count": 19, + "missing_or_nonterminal": 0 +} \ No newline at end of file diff --git a/evidence/alert-rules.txt b/evidence/alert-rules.txt new file mode 100644 index 0000000..04bc697 --- /dev/null +++ b/evidence/alert-rules.txt @@ -0,0 +1,2 @@ +Checking /etc/prometheus/alerts.yaml + SUCCESS: 6 rules found diff --git a/evidence/backend-incident.json b/evidence/backend-incident.json new file mode 100644 index 0000000..85cf59c --- /dev/null +++ b/evidence/backend-incident.json @@ -0,0 +1,86 @@ +{ + "confirmed_and_read": 30, + "seconds": 35.7, + "collector_before": { + "send_failed_logs": 3.0, + "queue_size": 0.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + "collector_samples": [ + { + "send_failed_logs": 3.0, + "queue_size": 0.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 3.0, + "queue_size": 0.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 3.0, + "queue_size": 2.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 3.0, + "queue_size": 5.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 3.0, + "queue_size": 8.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 3.0, + "queue_size": 8.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 6.0, + "queue_size": 10.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 6.0, + "queue_size": 13.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 6.0, + "queue_size": 16.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 6.0, + "queue_size": 16.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + { + "send_failed_logs": 8.0, + "queue_size": 18.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + } + ], + "collector_after": { + "send_failed_logs": 8.0, + "queue_size": 18.0, + "queue_capacity": 32.0, + "rss_bytes": 225472512.0 + }, + "failed_export_log_records_delta": 5.0, + "restored_trace_id": "859117fe4d9cee32f70a40cff400b8b1" +} diff --git a/evidence/backpressure.json b/evidence/backpressure.json new file mode 100644 index 0000000..ed40e02 --- /dev/null +++ b/evidence/backpressure.json @@ -0,0 +1,22 @@ +{ + "accepted": 6, + "rejected": 4, + "concurrency": 10, + "image_bytes": 1442638, + "seconds_to_observed_ready": [ + 6.57799999999952, + 6.625, + 8.342999999999847, + 8.389999999999418, + 10.405999999999949, + 10.436999999999898 + ], + "stats_samples": [ + "mtusi2026lab6-lb-1 8.266MiB / 7.649GiB 0.02%\nmtusi2026lab6-api-2 392.4MiB / 512MiB 47.49%\nmtusi2026lab6-api-1 403.2MiB / 512MiB 0.68%\nmtusi2026lab6-db-1 50.62MiB / 1GiB 0.05%\nmtusi2026lab6-grafana-1 78.57MiB / 256MiB 0.30%\nmtusi2026lab6-prometheus-1 37.57MiB / 256MiB 0.29%\nmtusi2026lab6-loki-1 90.53MiB / 384MiB 1.58%\nmtusi2026lab6-redis-1 3.512MiB / 7.649GiB 0.54%\nmtusi2026lab6-tempo-1 108.4MiB / 256MiB 0.35%\nmtusi2026lab6-broker-1 4.148MiB / 7.649GiB 0.58%\nmtusi2026lab6-otel-collector-1 73.34MiB / 192MiB 0.35%\nmtusi2026lab6-minio-1 461.9MiB / 7.649GiB 12.22%", + "mtusi2026lab6-worker-2 60.76MiB / 1GiB 8.68%\nmtusi2026lab6-lb-1 8.262MiB / 7.649GiB 0.13%\nmtusi2026lab6-api-2 392.7MiB / 512MiB 2.95%\nmtusi2026lab6-api-1 403.5MiB / 512MiB 4.59%\nmtusi2026lab6-db-1 50.62MiB / 1GiB 4.39%\nmtusi2026lab6-grafana-1 78.57MiB / 256MiB 0.24%\nmtusi2026lab6-prometheus-1 37.58MiB / 256MiB 0.32%\nmtusi2026lab6-loki-1 90.54MiB / 384MiB 0.72%\nmtusi2026lab6-redis-1 3.512MiB / 7.649GiB 0.44%\nmtusi2026lab6-tempo-1 236MiB / 256MiB 14.22%\nmtusi2026lab6-broker-1 4.152MiB / 7.649GiB 3.40%\nmtusi2026lab6-otel-collector-1 73.34MiB / 192MiB 0.14%\nmtusi2026lab6-minio-1 460.2MiB / 7.649GiB 2.62%\nmtusi2026lab6-dispatcher-1 60.66MiB / 256MiB 48.16%\nmtusi2026lab6-worker-1 114.7MiB / 1GiB 82.36%", + "mtusi2026lab6-worker-2 117.8MiB / 1GiB 79.86%\nmtusi2026lab6-worker-1 121MiB / 1GiB 78.85%\nmtusi2026lab6-dispatcher-1 61.84MiB / 256MiB 3.97%\nmtusi2026lab6-lb-1 8.703MiB / 7.649GiB 0.38%\nmtusi2026lab6-api-2 393.2MiB / 512MiB 10.11%\nmtusi2026lab6-api-1 404.2MiB / 512MiB 11.28%\nmtusi2026lab6-db-1 51.31MiB / 1GiB 25.77%\nmtusi2026lab6-grafana-1 78.57MiB / 256MiB 0.67%\nmtusi2026lab6-prometheus-1 37.59MiB / 256MiB 0.34%\nmtusi2026lab6-loki-1 90.54MiB / 384MiB 1.08%\nmtusi2026lab6-redis-1 4.098MiB / 7.649GiB 0.46%\nmtusi2026lab6-tempo-1 236MiB / 256MiB 0.60%\nmtusi2026lab6-broker-1 4.152MiB / 7.649GiB 0.56%\nmtusi2026lab6-otel-collector-1 73.95MiB / 192MiB 1.26%\nmtusi2026lab6-minio-1 462.9MiB / 7.649GiB 0.41%", + "mtusi2026lab6-worker-2 121.7MiB / 1GiB 89.58%\nmtusi2026lab6-worker-1 123MiB / 1GiB 91.74%\nmtusi2026lab6-dispatcher-1 62.14MiB / 256MiB 5.08%\nmtusi2026lab6-lb-1 8.457MiB / 7.649GiB 0.23%\nmtusi2026lab6-api-2 393.4MiB / 512MiB 3.86%\nmtusi2026lab6-api-1 404.5MiB / 512MiB 5.50%\nmtusi2026lab6-db-1 54.63MiB / 1GiB 21.49%\nmtusi2026lab6-grafana-1 78.57MiB / 256MiB 0.47%\nmtusi2026lab6-prometheus-1 37.59MiB / 256MiB 0.17%\nmtusi2026lab6-loki-1 90.55MiB / 384MiB 1.07%\nmtusi2026lab6-redis-1 4.098MiB / 7.649GiB 0.46%\nmtusi2026lab6-tempo-1 236.3MiB / 256MiB 0.59%\nmtusi2026lab6-broker-1 4.969MiB / 7.649GiB 3.87%\nmtusi2026lab6-otel-collector-1 73.97MiB / 192MiB 1.04%\nmtusi2026lab6-minio-1 462.8MiB / 7.649GiB 0.06%", + "mtusi2026lab6-worker-2 66.96MiB / 1GiB 0.08%\nmtusi2026lab6-worker-1 67.96MiB / 1GiB 0.09%\nmtusi2026lab6-dispatcher-1 62.13MiB / 256MiB 4.01%\nmtusi2026lab6-lb-1 8.457MiB / 7.649GiB 0.00%\nmtusi2026lab6-api-2 393.4MiB / 512MiB 0.52%\nmtusi2026lab6-api-1 404.6MiB / 512MiB 0.70%\nmtusi2026lab6-db-1 51.46MiB / 1GiB 8.45%\nmtusi2026lab6-grafana-1 78.57MiB / 256MiB 0.20%\nmtusi2026lab6-prometheus-1 37.59MiB / 256MiB 0.16%\nmtusi2026lab6-loki-1 90.56MiB / 384MiB 1.55%\nmtusi2026lab6-redis-1 4.098MiB / 7.649GiB 0.42%\nmtusi2026lab6-tempo-1 236.3MiB / 256MiB 0.72%\nmtusi2026lab6-broker-1 4.719MiB / 7.649GiB 0.51%\nmtusi2026lab6-otel-collector-1 73.98MiB / 192MiB 0.89%\nmtusi2026lab6-minio-1 463MiB / 7.649GiB 0.06%" + ], + "method": "closed loop; sequential polls overestimate early completions" +} \ No newline at end of file diff --git a/evidence/contract-check.txt b/evidence/contract-check.txt new file mode 100644 index 0000000..b62af9c --- /dev/null +++ b/evidence/contract-check.txt @@ -0,0 +1 @@ +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/database-incident.json b/evidence/database-incident.json new file mode 100644 index 0000000..08fa383 --- /dev/null +++ b/evidence/database-incident.json @@ -0,0 +1,904 @@ +{ + "traffic_clients": 6, + "short": { + "503_count": 1, + "samples": [ + { + "seconds": 1.0, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 2.03, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 3.05, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 4.08, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 5.09, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 6.11, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 7.24, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 8.25, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 9.33, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 10.34, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 11.34, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 12.38, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 13.38, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 14.39, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 15.42, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 16.45, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 17.49, + "states": { + "ApiHighErrorRate": "inactive", + "AttachmentQueueLag": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive", + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + } + ], + "alert_firings": 0 + }, + "sustained": { + "firing": [ + { + "seconds": 0.03, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 1.03, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 2.06, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 3.09, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 4.12, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 5.14, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 6.17, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 7.19, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 8.19, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 9.22, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 10.24, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 11.27, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 12.28, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 13.3, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 14.33, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 15.34, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 16.36, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 17.38, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 18.41, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 19.42, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 20.44, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 21.45, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 22.45, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 23.49, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 24.52, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 25.53, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 26.56, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 27.58, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 28.59, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + }, + { + "seconds": 29.61, + "states": { + "ApiHighErrorRate": "pending", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 30.62, + "states": { + "ApiHighErrorRate": "pending", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 31.66, + "states": { + "ApiHighErrorRate": "pending", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 32.67, + "states": { + "ApiHighErrorRate": "pending", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 33.72, + "states": { + "ApiHighErrorRate": "pending", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 34.74, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 35.77, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 36.8, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 37.8, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 38.81, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 39.83, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 40.86, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 41.88, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 42.89, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 43.92, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 44.94, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 45.97, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "pending" + } + }, + { + "seconds": 46.99, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 48.02, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 49.05, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "pending", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 50.08, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + } + ], + "resolved": [ + { + "seconds": 0.03, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 1.05, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 2.06, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 3.09, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 4.12, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 5.19, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 6.2, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 7.23, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 8.25, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 9.28, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 10.3, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "firing", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 11.31, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 12.34, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 13.38, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 14.39, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 15.42, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 16.44, + "states": { + "ApiHighErrorRate": "firing", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 17.47, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 18.48, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 19.52, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 20.55, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 21.56, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 22.59, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 23.61, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 24.62, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 25.66, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 26.67, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 27.7, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 28.73, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 29.75, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 30.78, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 31.8, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 32.81, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 33.86, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 34.88, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "firing" + } + }, + { + "seconds": 35.89, + "states": { + "ApiHighErrorRate": "inactive", + "ApiBurnFast": "inactive", + "ApiBurnSlow": "inactive" + } + } + ], + "bounded_lock_seconds": 85, + "traffic_statuses": { + "200": 1887, + "503": 55 + }, + "cause": { + "trace_id": "fa511ccfa2322516828045fbcbf4506c", + "request_id": "981f1cb0-11a3-4cd2-ba5e-52bbfa826978", + "dependency": "postgres.session.lookup", + "duration_seconds": 5.003010099, + "status": { + "code": "STATUS_CODE_ERROR" + } + } + } +} diff --git a/evidence/docker-build-worker.txt b/evidence/docker-build-worker.txt new file mode 100644 index 0000000..3542418 --- /dev/null +++ b/evidence/docker-build-worker.txt @@ -0,0 +1,76 @@ + Image mtusi2026lab6-worker Building + Image mtusi2026lab6-dispatcher Building +#1 [internal] load local bake definitions +#1 reading from stdin 1.39kB 0.3s done +#1 DONE 0.3s + +#2 [dispatcher internal] load build definition from Dockerfile +#2 transferring dockerfile: 384B 0.0s done +#2 DONE 0.1s + +#3 [dispatcher internal] load metadata for docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +#3 DONE 0.2s + +#4 [dispatcher internal] load .dockerignore +#4 transferring context: 216B 0.0s done +#4 DONE 0.1s + +#4 [worker internal] load .dockerignore +#4 DONE 0.1s + +#5 [worker internal] load build context +#5 transferring context: 72.99kB 0.2s done +#5 DONE 0.4s + +#6 [worker 1/5] FROM docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +#6 resolve docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c 0.3s done +#6 DONE 0.3s + +#7 [dispatcher 2/5] WORKDIR /app +#7 CACHED + +#8 [dispatcher 3/5] COPY worker/requirements.txt ./ +#8 CACHED + +#9 [dispatcher 4/5] RUN pip install --no-cache-dir -r requirements.txt && useradd --system --uid 10001 worker +#9 CACHED + +#10 [dispatcher 5/5] COPY worker/pipeline ./pipeline +#10 DONE 0.4s + +#10 [dispatcher 5/5] COPY worker/pipeline ./pipeline +#10 DONE 0.4s + +#11 [dispatcher] exporting to image +#11 exporting layers +#11 exporting layers 0.7s done +#11 exporting manifest sha256:ff23c89faa8e5239c8aa7953211c3b286680fa7a59e62463fb4d41276302d605 +#11 exporting manifest sha256:ff23c89faa8e5239c8aa7953211c3b286680fa7a59e62463fb4d41276302d605 0.3s done +#11 exporting config sha256:bcb3d560d893db14cee78d977d0a501f52732890a7a1305a871ddf9ac7ef900e +#11 exporting config sha256:bcb3d560d893db14cee78d977d0a501f52732890a7a1305a871ddf9ac7ef900e 0.2s done +#11 exporting attestation manifest sha256:ef2068b4f85da50ea8299f541d65d20059ca1190c3b79a4ccdc17841e4761797 +#11 exporting attestation manifest sha256:ef2068b4f85da50ea8299f541d65d20059ca1190c3b79a4ccdc17841e4761797 0.3s done +#11 exporting manifest list sha256:9d7ab5df52a1163bf28d4e48db9baacdba5dcd9b89c4f83322fa32d7858f75fd +#11 exporting manifest list sha256:9d7ab5df52a1163bf28d4e48db9baacdba5dcd9b89c4f83322fa32d7858f75fd 0.2s done +#11 naming to docker.io/library/mtusi2026lab6-dispatcher:latest 0.1s done +#11 unpacking to docker.io/library/mtusi2026lab6-dispatcher:latest +#11 unpacking to docker.io/library/mtusi2026lab6-dispatcher:latest 0.3s done +#11 DONE 2.4s + +#12 [worker] exporting to image +#12 exporting layers 0.7s done +#12 exporting manifest sha256:40c456f7c8637c80873f5a793a693fd0d063ac4312b00077d36f700d072da8b2 0.2s done +#12 exporting config sha256:3495e4350553e2b0f38adc911ede04f1fd62c7fa96511650e3b74bcdb2ae8b29 0.2s done +#12 exporting attestation manifest sha256:4c47c3fc9f601e9edfbf2498b5be1d32a56c351b61809f0155815646a377a3b5 0.3s done +#12 exporting manifest list sha256:0d6faad5fef10fb0bf6695e49148bc90c27dca671ac6c3c42b644c505c03119a 0.2s done +#12 naming to docker.io/library/mtusi2026lab6-worker:latest 0.0s done +#12 unpacking to docker.io/library/mtusi2026lab6-worker:latest 0.3s done +#12 DONE 2.4s + +#13 [worker] resolving provenance for metadata file +#13 DONE 0.2s + +#14 [dispatcher] resolving provenance for metadata file +#14 DONE 0.1s + Image mtusi2026lab6-dispatcher Built + Image mtusi2026lab6-worker Built diff --git a/evidence/docker-build.txt b/evidence/docker-build.txt new file mode 100644 index 0000000..035ad02 --- /dev/null +++ b/evidence/docker-build.txt @@ -0,0 +1,223 @@ + Image mtusi2026lab6-api Building + Image mtusi2026lab6-worker Building + Image mtusi2026lab6-dispatcher Building +#1 [internal] load local bake definitions +#1 reading from stdin 1.40kB 0.0s done +#1 DONE 0.1s + +#2 [api internal] load build definition from Dockerfile +#2 DONE 0.0s + +#3 [dispatcher internal] load build definition from Dockerfile +#3 ... + +#2 [api internal] load build definition from Dockerfile +#2 transferring dockerfile: 1.02kB 0.0s done +#2 DONE 0.5s + +#3 [worker internal] load build definition from Dockerfile +#3 transferring dockerfile: 384B 0.0s done +#3 DONE 1.5s + +#4 [api internal] load metadata for docker.io/library/eclipse-temurin:21-jre-jammy@sha256:dff6eb46527bd293a3c06bbbe9e64e8c9f91f01905b5abbdad31761c03059d2c +#4 ... + +#3 [dispatcher internal] load build definition from Dockerfile +#3 DONE 1.5s + +#5 [worker internal] load metadata for docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +#5 ... + +#4 [api internal] load metadata for docker.io/library/eclipse-temurin:21-jre-jammy@sha256:dff6eb46527bd293a3c06bbbe9e64e8c9f91f01905b5abbdad31761c03059d2c +#4 DONE 1.9s + +#6 [api internal] load metadata for docker.io/library/eclipse-temurin:21-jdk-jammy@sha256:432e54724c3e57d1846aeb886f4d339f3b0522931c4e64119c2797013b7cfc3c +#6 DONE 2.1s + +#5 [dispatcher internal] load metadata for docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +#5 DONE 1.6s + +#7 [dispatcher internal] load .dockerignore +#7 transferring context: 216B 0.0s done +#7 DONE 0.5s + +#8 [api builder 1/8] FROM docker.io/library/eclipse-temurin:21-jdk-jammy@sha256:432e54724c3e57d1846aeb886f4d339f3b0522931c4e64119c2797013b7cfc3c +#8 resolve docker.io/library/eclipse-temurin:21-jdk-jammy@sha256:432e54724c3e57d1846aeb886f4d339f3b0522931c4e64119c2797013b7cfc3c 0.3s done +#8 extracting sha256:02163de04dd1fb14a97b06804be3fbc51b564fa8744b8f97153744e0cba5de4c 1.2s done +#8 extracting sha256:686d6b6bf302bb2c5d3c7fae03c2b9b9b48035005d1722429c794f50b0038e5d 1.5s done +#8 extracting sha256:900892cecaef198d90c4e27bfdba600947a23a33a336863dbd86267ffe566526 3.7s done +#8 extracting sha256:f60943985375bfbdaee941c029587e147f52f20d87fba1157ac3235a4be53c15 0.3s done +#8 extracting sha256:5c3cc5dbd0a9b59a8786543b3feebae2af7c76bec381661de9f1417b12dd1e2e 0.2s done +#8 DONE 164.1s + +#9 [api builder 2/8] WORKDIR /build +#9 DONE 19.0s + +#10 [api runner 1/5] FROM docker.io/library/eclipse-temurin:21-jre-jammy@sha256:dff6eb46527bd293a3c06bbbe9e64e8c9f91f01905b5abbdad31761c03059d2c +#10 resolve docker.io/library/eclipse-temurin:21-jre-jammy@sha256:dff6eb46527bd293a3c06bbbe9e64e8c9f91f01905b5abbdad31761c03059d2c 0.3s done +#10 extracting sha256:64c669fcec3e030edf6d2c12142164764d763f5ab9b4bf374598bc70d4f5ea6a 1.1s done +#10 extracting sha256:e67f4804db649285e2894079cc70663ad503ced9ccf52dd7a6cd756e54eb14d9 2.1s done +#10 extracting sha256:f258e5941cb21baf92f8436ef2b3297324c4d121261f0984db53b857a104dfce 0.1s done +#10 extracting sha256:6792de25975b4075fe4065cfaa458b54391deeb904dcd73ca17668e4fd078e51 0.6s done +#10 DONE 426.0s + +#11 [api runner 2/5] WORKDIR /app +#11 DONE 10.7s + +#12 [api runner 3/5] RUN groupadd --system spring && useradd --system --gid spring --no-create-home spring +#12 DONE 2.0s + +#13 [dispatcher internal] load build context +#13 transferring context: 72.98kB 0.1s done +#13 transferring context: 72.98kB 0.1s done +#13 DONE 1.7s + +#14 [dispatcher 1/5] FROM docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +#14 resolve docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +#14 ... + +#13 [worker internal] load build context +#13 DONE 1.7s + +#15 [api internal] load build context +#15 ... + +#14 [worker 1/5] FROM docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +#14 resolve docker.io/library/python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c 2.4s done +#14 DONE 2.6s + +#15 [api internal] load build context +#15 ... + +#16 [worker 2/5] WORKDIR /app +#16 CACHED + +#17 [worker 3/5] COPY worker/requirements.txt ./ +#17 CACHED + +#18 [worker 4/5] RUN pip install --no-cache-dir -r requirements.txt && useradd --system --uid 10001 worker +#18 CACHED + +#15 [api internal] load build context +#15 ... + +#16 [dispatcher 2/5] WORKDIR /app +#16 CACHED + +#17 [dispatcher 3/5] COPY worker/requirements.txt ./ +#17 CACHED + +#18 [dispatcher 4/5] RUN pip install --no-cache-dir -r requirements.txt && useradd --system --uid 10001 worker +#18 CACHED + +#19 [dispatcher 5/5] COPY worker/pipeline ./pipeline +#19 CACHED + +#15 [api internal] load build context +#15 transferring context: 511.65kB 0.4s done +#15 DONE 4.8s + +#20 [worker] exporting to image +#20 exporting layers done +#20 exporting manifest sha256:9b874acf0137febbcf16fa37596797b10824e44e8e36943d97857542776de374 0.3s done +#20 exporting config sha256:a3714fe0b589cff4059d13ff351c98e3445775a819b882dab7d20e2a878bf412 +#20 exporting config sha256:a3714fe0b589cff4059d13ff351c98e3445775a819b882dab7d20e2a878bf412 0.3s done +#20 ... + +#21 [api builder 3/8] COPY gradle gradle +#21 DONE 0.5s + +#22 [api builder 3/7] COPY gradle gradle +#22 CACHED + +#23 [api builder 4/7] COPY gradlew settings.gradle build.gradle ./ +#23 CACHED + +#24 [api builder 5/7] COPY contracts contracts +#24 DONE 5.4s + +#25 [dispatcher] exporting to image +#25 exporting layers done +#25 exporting manifest sha256:aa58e5fb8ffef90d7260386c33ec11f7b01f965adc70ced048837a29dd5d72c4 0.3s done +#25 exporting config sha256:ee034451127f62a0f3c3ccb9b03a1b0d73b91936d83d7dc0ca9cebb5354c84bb 0.1s done +#25 exporting attestation manifest sha256:5a7bed04c671e68161654e4f8e38ef72cf3fa17807f1bfd4a096275f0a29f2de 2.0s done +#25 exporting manifest list sha256:fd06987f7a452b5d06ed6bcd626578eaa264664ee921a4ce819f6ac1cbd0e05e +#25 exporting manifest list sha256:fd06987f7a452b5d06ed6bcd626578eaa264664ee921a4ce819f6ac1cbd0e05e 3.3s done +#25 naming to docker.io/library/mtusi2026lab6-dispatcher:latest +#25 ... + +#26 [api builder 6/7] COPY src src +#26 ... + +#25 [dispatcher] exporting to image +#25 naming to docker.io/library/mtusi2026lab6-dispatcher:latest 0.2s done +#25 unpacking to docker.io/library/mtusi2026lab6-dispatcher:latest 0.2s done +#25 DONE 7.3s + +#20 [worker] exporting to image +#20 exporting attestation manifest sha256:a76351b2d57bd9eb25f36fbfe752f68900a8fb4d726f2dc6945d9e1ace0dffd0 1.9s done +#20 exporting manifest list sha256:cad61213e510f0e7c2ffa0c66d135c339b9af27bbfc8aa698d1852b33929a5d9 3.4s done +#20 naming to docker.io/library/mtusi2026lab6-worker:latest 0.1s done +#20 unpacking to docker.io/library/mtusi2026lab6-worker:latest 0.2s done +#20 DONE 7.4s + +#26 [api builder 6/7] COPY src src +#26 DONE 1.9s + +#27 [api builder 7/7] RUN --mount=type=cache,target=/root/.gradle,sharing=locked chmod +x gradlew && ./gradlew bootJar -x test --no-daemon +#27 ... + +#28 [dispatcher] resolving provenance for metadata file +#28 DONE 0.2s + +#29 [worker] resolving provenance for metadata file +#29 DONE 0.1s + +#27 [api builder 7/7] RUN --mount=type=cache,target=/root/.gradle,sharing=locked chmod +x gradlew && ./gradlew bootJar -x test --no-daemon +#27 3.606 To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/9.3.0/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation. +#27 8.404 Daemon will be stopped at the end of the build +#27 58.00 +#27 58.00 > Task :compileJava +#27 58.00 Note: /build/src/main/java/ru/mtusi/messenger/controller/controllerAdvice/GlobalExceptionHandler.java uses or overrides a deprecated API. +#27 58.00 Note: Recompile with -Xlint:deprecation for details. +#27 59.60 +#27 59.60 > Task :processResources +#27 59.60 > Task :classes +#27 59.60 > Task :resolveMainClassName +#27 60.90 > Task :bootJar +#27 60.97 +#27 60.97 [Incubating] Problems report is available at: file:///build/build/reports/problems/problems-report.html +#27 60.97 +#27 60.97 BUILD SUCCESSFUL in 58s +#27 60.97 4 actionable tasks: 4 executed +#27 60.97 Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.3.0/userguide/configuration_cache_enabling.html +#27 DONE 63.4s + +#30 [api runner 4/5] COPY --from=builder /build/build/libs/*.jar app.jar +#30 DONE 2.8s + +#31 [api runner 5/5] RUN chown spring:spring app.jar +#31 DONE 3.9s + +#32 [api] exporting to image +#32 exporting layers +#32 exporting layers 12.2s done +#32 exporting manifest sha256:36907c93c682c19d0752269c9b558154a87f75e7d0c17f63435ba669f5d86ce7 +#32 exporting manifest sha256:36907c93c682c19d0752269c9b558154a87f75e7d0c17f63435ba669f5d86ce7 0.3s done +#32 exporting config sha256:821d105d27c4b8153d7c90f70d9bb45425e0fd3569123b0f45e549a1994c1d96 +#32 exporting config sha256:821d105d27c4b8153d7c90f70d9bb45425e0fd3569123b0f45e549a1994c1d96 0.2s done +#32 exporting attestation manifest sha256:2131a93db84b8535cad610d781b329cfb67f3b8c9c4976a4515c0f163872b1ad +#32 exporting attestation manifest sha256:2131a93db84b8535cad610d781b329cfb67f3b8c9c4976a4515c0f163872b1ad 0.4s done +#32 exporting manifest list sha256:4c3433f0afb65d077d64e577815de8e2f8c1d135287adeccc17f3602f56f3299 +#32 exporting manifest list sha256:4c3433f0afb65d077d64e577815de8e2f8c1d135287adeccc17f3602f56f3299 0.2s done +#32 naming to docker.io/library/mtusi2026lab6-api:latest +#32 naming to docker.io/library/mtusi2026lab6-api:latest 0.0s done +#32 unpacking to docker.io/library/mtusi2026lab6-api:latest +#32 unpacking to docker.io/library/mtusi2026lab6-api:latest 1.9s done +#32 DONE 15.4s + +#33 [api] resolving provenance for metadata file +#33 DONE 0.1s + Image mtusi2026lab6-worker Built + Image mtusi2026lab6-dispatcher Built + Image mtusi2026lab6-api Built diff --git a/evidence/environment.json b/evidence/environment.json new file mode 100644 index 0000000..5108522 --- /dev/null +++ b/evidence/environment.json @@ -0,0 +1,174 @@ +{ + "timestamp": "2026-11-16T21:06:36.187407+03:00", + "skeleton": "470d9d6", + "transfer_source": "ef7e8c3", + "engine": "version=29.9.2 cpus=8 memory_bytes=8212676608", + "services": [ + { + "service": "api", + "image": "mtusi2026lab6-api", + "image_id": "sha256:4c3433f0afb65d077d64e577815de8e2f8c1d135287adeccc17f3602f56f3299", + "started_at": "2026-11-16T18:04:37.603181695Z", + "health": "healthy", + "memory_limit_bytes": 536870912, + "nano_cpus": 1000000000, + "volumes": [] + }, + { + "service": "api", + "image": "mtusi2026lab6-api", + "image_id": "sha256:4c3433f0afb65d077d64e577815de8e2f8c1d135287adeccc17f3602f56f3299", + "started_at": "2026-11-16T18:05:46.59241268Z", + "health": "healthy", + "memory_limit_bytes": 536870912, + "nano_cpus": 1000000000, + "volumes": [] + }, + { + "service": "broker", + "image": "redis:7.4-alpine@sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f", + "image_id": "sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f", + "started_at": "2026-11-16T18:04:31.966805543Z", + "health": "healthy", + "memory_limit_bytes": 0, + "nano_cpus": 0, + "volumes": [ + "mtusi2026lab6_brokerdata" + ] + }, + { + "service": "db", + "image": "postgres:16-alpine@sha256:0cefaabd882e42bcb2a2f3dc56d3ce4770560ba62fd7f197f1a2e008fd5731ab", + "image_id": "sha256:0cefaabd882e42bcb2a2f3dc56d3ce4770560ba62fd7f197f1a2e008fd5731ab", + "started_at": "2026-11-16T18:04:33.227463421Z", + "health": "healthy", + "memory_limit_bytes": 1073741824, + "nano_cpus": 1000000000, + "volumes": [ + "mtusi2026lab6_pgdata" + ] + }, + { + "service": "dispatcher", + "image": "mtusi2026lab6-dispatcher", + "image_id": "sha256:9d7ab5df52a1163bf28d4e48db9baacdba5dcd9b89c4f83322fa32d7858f75fd", + "started_at": "2026-11-16T18:04:34.842754932Z", + "health": "running", + "memory_limit_bytes": 268435456, + "nano_cpus": 500000000, + "volumes": [] + }, + { + "service": "grafana", + "image": "grafana/grafana:12.0.2@sha256:b5b59bfc7561634c2d7b136c4543d702ebcc94a3da477f21ff26f89ffd4214fa", + "image_id": "sha256:b5b59bfc7561634c2d7b136c4543d702ebcc94a3da477f21ff26f89ffd4214fa", + "started_at": "2026-11-16T18:04:32.27534289Z", + "health": "running", + "memory_limit_bytes": 268435456, + "nano_cpus": 0, + "volumes": [ + "mtusi2026lab6_grafana-data" + ] + }, + { + "service": "lb", + "image": "nginx:1.27-alpine@sha256:65645c7bb6a0661892a8b03b89d0743208a18dd2f3f17a54ef4b76fb8e2f2a10", + "image_id": "sha256:65645c7bb6a0661892a8b03b89d0743208a18dd2f3f17a54ef4b76fb8e2f2a10", + "started_at": "2026-11-16T18:04:32.751031805Z", + "health": "running", + "memory_limit_bytes": 0, + "nano_cpus": 0, + "volumes": [] + }, + { + "service": "loki", + "image": "grafana/loki:3.5.1@sha256:a74594532eec4cc313401beedc4dd2708c43674c032084b1aeb87c14a5be1745", + "image_id": "sha256:a74594532eec4cc313401beedc4dd2708c43674c032084b1aeb87c14a5be1745", + "started_at": "2026-11-16T18:04:32.918094644Z", + "health": "running", + "memory_limit_bytes": 402653184, + "nano_cpus": 0, + "volumes": [ + "mtusi2026lab6_loki-data" + ] + }, + { + "service": "minio", + "image": "pgsty/silo@sha256:7bb2aff6b89efe1cb1303c10fc21d579447434ed06649ac9ec05a5e2dbf30ad7", + "image_id": "sha256:7bb2aff6b89efe1cb1303c10fc21d579447434ed06649ac9ec05a5e2dbf30ad7", + "started_at": "2026-11-16T18:04:32.058002577Z", + "health": "healthy", + "memory_limit_bytes": 0, + "nano_cpus": 0, + "volumes": [ + "mtusi2026lab6_minio-data" + ] + }, + { + "service": "otel-collector", + "image": "otel/opentelemetry-collector-contrib:0.128.0@sha256:1ab0baba0ee3695d823c46653d8a6e8894896e668ce8bd7ebe002e948d827bc7", + "image_id": "sha256:1ab0baba0ee3695d823c46653d8a6e8894896e668ce8bd7ebe002e948d827bc7", + "started_at": "2026-11-16T18:04:31.686604058Z", + "health": "running", + "memory_limit_bytes": 201326592, + "nano_cpus": 0, + "volumes": [] + }, + { + "service": "prometheus", + "image": "prom/prometheus:v3.4.1@sha256:9abc6cf6aea7710d163dbb28d8eeb7dc5baef01e38fa4cd146a406dd9f07f70d", + "image_id": "sha256:9abc6cf6aea7710d163dbb28d8eeb7dc5baef01e38fa4cd146a406dd9f07f70d", + "started_at": "2026-11-16T18:04:32.56102027Z", + "health": "running", + "memory_limit_bytes": 268435456, + "nano_cpus": 0, + "volumes": [ + "mtusi2026lab6_prometheus-data" + ] + }, + { + "service": "redis", + "image": "redis:7.4-alpine@sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f", + "image_id": "sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f", + "started_at": "2026-11-16T18:04:32.370360741Z", + "health": "healthy", + "memory_limit_bytes": 0, + "nano_cpus": 0, + "volumes": [ + "mtusi2026lab6_redisdata" + ] + }, + { + "service": "tempo", + "image": "grafana/tempo:2.8.1@sha256:bc9245fe3da4e63dc4c6862d9c2dad9bcd8be13d0ba4f7705fa6acda4c904d0e", + "image_id": "sha256:bc9245fe3da4e63dc4c6862d9c2dad9bcd8be13d0ba4f7705fa6acda4c904d0e", + "started_at": "2026-11-16T18:04:32.993632697Z", + "health": "running", + "memory_limit_bytes": 268435456, + "nano_cpus": 0, + "volumes": [ + "mtusi2026lab6_tempo-data" + ] + }, + { + "service": "worker", + "image": "mtusi2026lab6-worker", + "image_id": "sha256:0d6faad5fef10fb0bf6695e49148bc90c27dca671ac6c3c42b644c505c03119a", + "started_at": "2026-11-16T18:04:35.257045333Z", + "health": "running", + "memory_limit_bytes": 1073741824, + "nano_cpus": 1000000000, + "volumes": [] + }, + { + "service": "worker", + "image": "mtusi2026lab6-worker", + "image_id": "sha256:0d6faad5fef10fb0bf6695e49148bc90c27dca671ac6c3c42b644c505c03119a", + "started_at": "2026-11-16T18:04:35.099706108Z", + "health": "running", + "memory_limit_bytes": 1073741824, + "nano_cpus": 1000000000, + "volumes": [] + } + ] +} diff --git a/evidence/failover.txt b/evidence/failover.txt new file mode 100644 index 0000000..0c00a7a --- /dev/null +++ b/evidence/failover.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 94, "failed_attempts": 0, "first_recovery_seconds": 2.5} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/fenced-pending.json b/evidence/fenced-pending.json new file mode 100644 index 0000000..899e2c8 --- /dev/null +++ b/evidence/fenced-pending.json @@ -0,0 +1,7 @@ +{ + "attachment_id": "85844943-a35b-41de-bbbb-c94eca68e7f7", + "unfinished_delivery_retained": true, + "recovered_attempt": 2, + "one_manifest": true, + "s3_objects": 4 +} \ No newline at end of file diff --git a/evidence/incident-backend.txt b/evidence/incident-backend.txt new file mode 100644 index 0000000..2c89963 --- /dev/null +++ b/evidence/incident-backend.txt @@ -0,0 +1 @@ +PASS stopped Loki: bounded telemetry failure and saved business writes diff --git a/evidence/incident-db.txt b/evidence/incident-db.txt new file mode 100644 index 0000000..33ecada --- /dev/null +++ b/evidence/incident-db.txt @@ -0,0 +1,3 @@ +ERROR: canceling statement due to user request +ERROR: canceling statement due to user request +PASS short DB burst and sustained slowdown: real alerts and DB trace diff --git a/evidence/incident-queue.txt b/evidence/incident-queue.txt new file mode 100644 index 0000000..8bbff8e --- /dev/null +++ b/evidence/incident-queue.txt @@ -0,0 +1 @@ +PASS stopped worker: backlog, firing/resolved and slow READY burn diff --git a/evidence/integrity.txt b/evidence/integrity.txt new file mode 100644 index 0000000..fc27447 --- /dev/null +++ b/evidence/integrity.txt @@ -0,0 +1 @@ +PASS public starter/contract intact, generated secrets absent from Git-visible files diff --git a/evidence/java-build.txt b/evidence/java-build.txt new file mode 100644 index 0000000..7ecf885 --- /dev/null +++ b/evidence/java-build.txt @@ -0,0 +1,15 @@ +To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/9.3.0/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation. +Daemon will be stopped at the end of the build +> Task :compileJava +> Task :processResources UP-TO-DATE +> Task :classes +> Task :resolveMainClassName UP-TO-DATE +> Task :bootJar +> Task :compileTestJava UP-TO-DATE +> Task :processTestResources NO-SOURCE +> Task :testClasses UP-TO-DATE +> Task :test + +BUILD SUCCESSFUL in 28s +6 actionable tasks: 3 executed, 3 up-to-date +Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.3.0/userguide/configuration_cache_enabling.html diff --git a/evidence/job-schema.json b/evidence/job-schema.json new file mode 100644 index 0000000..c12364f --- /dev/null +++ b/evidence/job-schema.json @@ -0,0 +1,5 @@ +{ + "durable_templates_validated": 65, + "with_w3c_traceparent": 64, + "valid": true +} diff --git a/evidence/load.json b/evidence/load.json new file mode 100644 index 0000000..06e6362 --- /dev/null +++ b/evidence/load.json @@ -0,0 +1,17 @@ +{ + "lab": 6, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.094, + "concurrency": 4, + "successful_requests": 1794, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 59.613, + "successful_latency_ms": { + "p50": 62.0, + "p95": 141.0, + "p99": 219.0 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/load.txt b/evidence/load.txt new file mode 100644 index 0000000..06e6362 --- /dev/null +++ b/evidence/load.txt @@ -0,0 +1,17 @@ +{ + "lab": 6, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.094, + "concurrency": 4, + "successful_requests": 1794, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 59.613, + "successful_latency_ms": { + "p50": 62.0, + "p95": 141.0, + "p99": 219.0 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/metric-inventory.json b/evidence/metric-inventory.json new file mode 100644 index 0000000..b68d4b6 --- /dev/null +++ b/evidence/metric-inventory.json @@ -0,0 +1,127 @@ +{ + "lab_http_requests_total": { + "series": 41, + "label_keys": [ + "__name__", + "instance", + "job", + "method", + "otel_scope_name", + "route", + "status_class" + ] + }, + "lab_http_duration_seconds_bucket": { + "series": 533, + "label_keys": [ + "__name__", + "instance", + "job", + "le", + "method", + "otel_scope_name", + "route", + "status_class" + ] + }, + "lab_jobs_total": { + "series": 42, + "label_keys": [ + "__name__", + "instance", + "job", + "job_type", + "otel_scope_name", + "outcome" + ] + }, + "lab_job_duration_seconds_bucket": { + "series": 60, + "label_keys": [ + "__name__", + "instance", + "job", + "job_type", + "le", + "otel_scope_name", + "outcome" + ] + }, + "lab_ready_duration_seconds_bucket": { + "series": 45, + "label_keys": [ + "__name__", + "instance", + "job", + "job_type", + "le", + "otel_scope_name" + ] + }, + "lab_queue_depth": { + "series": 2, + "label_keys": [ + "__name__", + "instance", + "job", + "otel_scope_name", + "queue" + ] + }, + "lab_queue_oldest_seconds": { + "series": 2, + "label_keys": [ + "__name__", + "instance", + "job", + "otel_scope_name", + "queue" + ] + }, + "lab_db_pool_connections": { + "series": 12, + "label_keys": [ + "__name__", + "instance", + "job", + "otel_scope_name", + "state" + ] + }, + "lab_worker_busy": { + "series": 4, + "label_keys": [ + "__name__", + "instance", + "job", + "otel_scope_name" + ] + }, + "lab_worker_capacity": { + "series": 4, + "label_keys": [ + "__name__", + "instance", + "job", + "otel_scope_name" + ] + }, + "lab_process_cpu_seconds_total": { + "series": 10, + "label_keys": [ + "__name__", + "instance", + "job", + "otel_scope_name" + ] + }, + "lab_process_rss_bytes": { + "series": 10, + "label_keys": [ + "__name__", + "instance", + "job", + "otel_scope_name" + ] + } +} diff --git a/evidence/observe.txt b/evidence/observe.txt new file mode 100644 index 0000000..bb75712 --- /dev/null +++ b/evidence/observe.txt @@ -0,0 +1 @@ +PASS real upload -> publish -> consumer retry -> S3/DB; Loki/Tempo/Prometheus via Grafana diff --git a/evidence/openapi-validation.txt b/evidence/openapi-validation.txt new file mode 100644 index 0000000..248343e --- /dev/null +++ b/evidence/openapi-validation.txt @@ -0,0 +1 @@ +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 0000000..749fd29 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,2 @@ +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/pipeline-ack_guard.txt b/evidence/pipeline-ack_guard.txt new file mode 100644 index 0000000..c3e0580 --- /dev/null +++ b/evidence/pipeline-ack_guard.txt @@ -0,0 +1 @@ +PASS expired worker without dispatcher: no premature ACK; pending/index retained; recovery publishes exactly one result on attempt 2 diff --git a/evidence/pipeline-boundaries.txt b/evidence/pipeline-boundaries.txt new file mode 100644 index 0000000..012a821 --- /dev/null +++ b/evidence/pipeline-boundaries.txt @@ -0,0 +1,2 @@ +PASS message eligibility: status UPDATE blocked until Message COMMIT; subsequent FAILED attachment rejected with 409 +PASS download/cleanup barrier: content waits for exclusive cleanup, returns identical bytes, releases shared fence diff --git a/evidence/pipeline-core.txt b/evidence/pipeline-core.txt new file mode 100644 index 0000000..6e8631b --- /dev/null +++ b/evidence/pipeline-core.txt @@ -0,0 +1,5 @@ +PASS queued snapshot, link before ready, 409 before worker; original + 3 variants +PASS bytes/corruption/MIME/filename/ACL/cross-chat negatives +PASS alpha white, no small-image enlargement, EXIF rotation, crop and WebP +PASS 24 concurrent requests across 2 API replicas; single message effect +PASS core pipeline contract diff --git a/evidence/pipeline-faults.json b/evidence/pipeline-faults.json new file mode 100644 index 0000000..70b1958 --- /dev/null +++ b/evidence/pipeline-faults.json @@ -0,0 +1,67 @@ +[ + { + "scenario": "commit_publish_api_broker_restart", + "result": "PASS", + "seconds": 83.344 + }, + { + "scenario": "publish_confirm_db_mark_crash", + "result": "PASS", + "seconds": 20.297 + }, + { + "scenario": "24_concurrent_duplicate_deliveries", + "result": "PASS", + "seconds": 20.875 + }, + { + "scenario": "crash_before_effects", + "result": "PASS", + "seconds": 23.968 + }, + { + "scenario": "crash_after_effects", + "result": "PASS", + "seconds": 23.016 + }, + { + "scenario": "crash_after_commit", + "result": "PASS", + "seconds": 14.187 + }, + { + "scenario": "crash_before_ack", + "result": "PASS", + "seconds": 21.516 + }, + { + "scenario": "expired_lease_fences_old_worker", + "result": "PASS", + "seconds": 24.891 + }, + { + "scenario": "hard_process_timeout_recovery", + "result": "PASS", + "seconds": 36.281 + }, + { + "scenario": "new_version_then_stale_reordered_jobs", + "result": "PASS", + "seconds": 27.64 + }, + { + "scenario": "permanent_failure_explicit_replay", + "result": "PASS", + "seconds": 15.344 + }, + { + "scenario": "bounded_retry_explicit_replay", + "result": "PASS", + "seconds": 24.594 + }, + { + "scenario": "two_replica_atomic_backpressure_recovery", + "result": "PASS", + "seconds": 17.547 + } +] \ No newline at end of file diff --git a/evidence/pipeline-faults.txt b/evidence/pipeline-faults.txt new file mode 100644 index 0000000..99d2e6a --- /dev/null +++ b/evidence/pipeline-faults.txt @@ -0,0 +1,16 @@ +durability: 202 -> outbox COMMIT without publisher; API restart; publish; broker restart while worker stopped; same jobs ready +{"scenario": "commit_publish_api_broker_restart", "result": "PASS", "seconds": 83.344} +{"scenario": "publish_confirm_db_mark_crash", "result": "PASS", "seconds": 20.297} +{"scenario": "24_concurrent_duplicate_deliveries", "result": "PASS", "seconds": 20.875} +{"scenario": "crash_before_effects", "result": "PASS", "seconds": 23.968} +{"scenario": "crash_after_effects", "result": "PASS", "seconds": 23.016} +{"scenario": "crash_after_commit", "result": "PASS", "seconds": 14.187} +{"scenario": "crash_before_ack", "result": "PASS", "seconds": 21.516} +{"scenario": "expired_lease_fences_old_worker", "result": "PASS", "seconds": 24.891} +{"scenario": "hard_process_timeout_recovery", "result": "PASS", "seconds": 36.281} +{"scenario": "new_version_then_stale_reordered_jobs", "result": "PASS", "seconds": 27.64} +{"scenario": "permanent_failure_explicit_replay", "result": "PASS", "seconds": 15.344} +{"scenario": "bounded_retry_explicit_replay", "result": "PASS", "seconds": 24.594} +backpressure: 6 accepted, 4 rejected with 503, accepted jobs drained after recovery +{"scenario": "two_replica_atomic_backpressure_recovery", "result": "PASS", "seconds": 17.547} +PASS staging cleaned after safe result publication; no confirmed job lost; all pipeline fault invariants diff --git a/evidence/pipeline-filename.txt b/evidence/pipeline-filename.txt new file mode 100644 index 0000000..a391731 --- /dev/null +++ b/evidence/pipeline-filename.txt @@ -0,0 +1 @@ +PASS filename code-point limits: 255 ASCII and 255 emoji -> 202 and download 200; 256 emoji -> 400; long Content-Disposition accepted by LB diff --git a/evidence/pipeline-history_capacity.txt b/evidence/pipeline-history_capacity.txt new file mode 100644 index 0000000..8e7fd08 --- /dev/null +++ b/evidence/pipeline-history_capacity.txt @@ -0,0 +1 @@ +PASS history last-slot race: 10 simultaneous unique POSTs, exactly 1 committed and 9 rejected with 413; DB count equals configured limit diff --git a/evidence/pipeline-retention.txt b/evidence/pipeline-retention.txt new file mode 100644 index 0000000..e80b1d5 --- /dev/null +++ b/evidence/pipeline-retention.txt @@ -0,0 +1,5 @@ +PASS anonymous direct S3 GET of existing original returns 403 +injected 49-million-pixel complete PNG into trusted staging with matching metadata +PASS worker independently rejects >40 million pixels before full decode; FAILED on first attempt +fresh orphan retained by grace +PASS expired failed staging cleaned; unsafe replay refused; referenced ready bytes retained diff --git a/evidence/pipeline-upload_crash.txt b/evidence/pipeline-upload_crash.txt new file mode 100644 index 0000000..fcf3a42 --- /dev/null +++ b/evidence/pipeline-upload_crash.txt @@ -0,0 +1 @@ +PASS SIGKILL after S3 PUT before DB COMMIT: no confirmed response, DB rolled back, staged orphan unreferenced; same session uploads after recovery diff --git a/evidence/queue-incident.json b/evidence/queue-incident.json new file mode 100644 index 0000000..1941769 --- /dev/null +++ b/evidence/queue-incident.json @@ -0,0 +1,543 @@ +{ + "accepted": 16, + "queue_firing": [ + { + "seconds": 0.81, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 1.84, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 2.86, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 3.88, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 4.92, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 5.95, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 6.97, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 8.0, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 9.03, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 10.05, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 11.08, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 12.11, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 13.16, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 14.19, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 15.2, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 16.24, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 17.27, + "states": { + "AttachmentQueueLag": "inactive" + } + }, + { + "seconds": 18.3, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 19.33, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 20.34, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 21.36, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 22.39, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 23.41, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 24.44, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 25.45, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 26.49, + "states": { + "AttachmentQueueLag": "pending" + } + }, + { + "seconds": 27.5, + "states": { + "AttachmentQueueLag": "firing" + } + } + ], + "peak": { + "age_seconds": 19.775276, + "backlog": 16.0, + "reported_capacity": 0.0 + }, + "drained_seconds": 10.08, + "ready_burn_firing": [ + { + "seconds": 10.09, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 11.11, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 12.12, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 13.16, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 14.19, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 15.2, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 16.22, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 17.25, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + }, + { + "seconds": 18.31, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 19.33, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 20.34, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 21.36, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 22.39, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 23.41, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 24.42, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 25.44, + "states": { + "ReadyBurnFast": "pending", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 26.47, + "states": { + "ReadyBurnFast": "firing", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 27.5, + "states": { + "ReadyBurnFast": "firing", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 28.55, + "states": { + "ReadyBurnFast": "firing", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 29.58, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 30.59, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 31.62, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 32.66, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 33.69, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 34.72, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "pending" + } + }, + { + "seconds": 35.75, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + } + ], + "queue_resolved": [ + { + "seconds": 35.75, + "states": { + "AttachmentQueueLag": "inactive" + } + } + ], + "ready_burn_resolved": [ + { + "seconds": 35.75, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 36.78, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 37.81, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 38.83, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 39.84, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 40.88, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 41.89, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 42.92, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 43.94, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 44.95, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 45.98, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 47.01, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 48.03, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 49.06, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 50.08, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 51.09, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 52.11, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 53.12, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 54.16, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 55.2, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 56.23, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 57.26, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 58.3, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 59.31, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "firing" + } + }, + { + "seconds": 60.34, + "states": { + "ReadyBurnFast": "inactive", + "ReadyBurnSlow": "inactive" + } + } + ], + "all_ready": true +} diff --git a/evidence/resources.json b/evidence/resources.json new file mode 100644 index 0000000..caf5908 --- /dev/null +++ b/evidence/resources.json @@ -0,0 +1,167 @@ +[ + { + "BlockIO": "0B / 225kB", + "CPUPerc": "4.37%", + "Container": "ca125bc1ab260f066b6cd7fa547fb63df8f791ed06ed45fb5a945ff7c91f0c0e", + "ID": "ca125bc1ab26", + "MemPerc": "85.26%", + "MemUsage": "436.5MiB / 512MiB", + "Name": "mtusi2026lab6-api-1", + "NetIO": "783kB / 1.01MB", + "PIDs": "45" + }, + { + "BlockIO": "0B / 168kB", + "CPUPerc": "0.60%", + "Container": "05f31aaf15dade65b0313165a791e9eae523b97fe27a1f589c061e2f8941f6d7", + "ID": "05f31aaf15da", + "MemPerc": "78.47%", + "MemUsage": "401.8MiB / 512MiB", + "Name": "mtusi2026lab6-api-2", + "NetIO": "205kB / 238kB", + "PIDs": "44" + }, + { + "BlockIO": "1.17MB / 0B", + "CPUPerc": "3.43%", + "Container": "64111f70300ebe28bd795eb9fae98b908aabfa8bbc15a100cbb1205ccd8b5f6d", + "ID": "64111f70300e", + "MemPerc": "0.06%", + "MemUsage": "4.578MiB / 7.649GiB", + "Name": "mtusi2026lab6-broker-1", + "NetIO": "211kB / 124kB", + "PIDs": "6" + }, + { + "BlockIO": "53.2kB / 618kB", + "CPUPerc": "8.35%", + "Container": "3eb05c0c3be8dc3209f96d29766c753f2886d82e8eb2914a27ccead0e17ff6c6", + "ID": "3eb05c0c3be8", + "MemPerc": "4.26%", + "MemUsage": "43.59MiB / 1GiB", + "Name": "mtusi2026lab6-db-1", + "NetIO": "1.36MB / 1.49MB", + "PIDs": "16" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "7.03%", + "Container": "ef1edef5f865d086f570b3fe4753ed9949656a9bc7b31f115698523c741e48ae", + "ID": "ef1edef5f865", + "MemPerc": "22.85%", + "MemUsage": "58.51MiB / 256MiB", + "Name": "mtusi2026lab6-dispatcher-1", + "NetIO": "1.31MB / 959kB", + "PIDs": "4" + }, + { + "BlockIO": "3.67MB / 217kB", + "CPUPerc": "0.24%", + "Container": "5cfa7717b7a7ab3c14890490f4a6b1340507df674cf8eb4c1c69a43fd480a631", + "ID": "5cfa7717b7a7", + "MemPerc": "31.45%", + "MemUsage": "80.52MiB / 256MiB", + "Name": "mtusi2026lab6-grafana-1", + "NetIO": "21.5kB / 15.3kB", + "PIDs": "22" + }, + { + "BlockIO": "1.13MB / 4.1kB", + "CPUPerc": "0.00%", + "Container": "df656f660d6da9188636bdfea949b211ef67dfaf74e50791483ee488257aba80", + "ID": "df656f660d6d", + "MemPerc": "0.10%", + "MemUsage": "8.09MiB / 7.649GiB", + "Name": "mtusi2026lab6-lb-1", + "NetIO": "940kB / 990kB", + "PIDs": "9" + }, + { + "BlockIO": "3.19MB / 111kB", + "CPUPerc": "1.21%", + "Container": "4a0e6eb9fbc6c174bbf8592da5f294742595e7d3f68793dcfe7a0d77b0704d36", + "ID": "4a0e6eb9fbc6", + "MemPerc": "16.40%", + "MemUsage": "62.99MiB / 384MiB", + "Name": "mtusi2026lab6-loki-1", + "NetIO": "45.8kB / 9.36kB", + "PIDs": "13" + }, + { + "BlockIO": "9.04MB / 623kB", + "CPUPerc": "1.40%", + "Container": "4c69cf0985be420aeaeac5aba974f8b00fb4b41feefa53ed63b20fff22756830", + "ID": "4c69cf0985be", + "MemPerc": "2.48%", + "MemUsage": "194.2MiB / 7.649GiB", + "Name": "mtusi2026lab6-minio-1", + "NetIO": "536kB / 890kB", + "PIDs": "13" + }, + { + "BlockIO": "1.45MB / 0B", + "CPUPerc": "0.45%", + "Container": "e3b44951e44d4259758d6433d9343b3e1c5361dcd2ff398ebd1a4b88946d2b7b", + "ID": "e3b44951e44d", + "MemPerc": "28.22%", + "MemUsage": "54.19MiB / 192MiB", + "Name": "mtusi2026lab6-otel-collector-1", + "NetIO": "487kB / 281kB", + "PIDs": "12" + }, + { + "BlockIO": "2.08MB / 98.3kB", + "CPUPerc": "0.29%", + "Container": "97fd117dc63b8a2dbbb071139ffd8881a8e403e5405ecc290214ff7bd23ab8d7", + "ID": "97fd117dc63b", + "MemPerc": "14.24%", + "MemUsage": "36.46MiB / 256MiB", + "Name": "mtusi2026lab6-prometheus-1", + "NetIO": "171kB / 40.4kB", + "PIDs": "13" + }, + { + "BlockIO": "4.1kB / 0B", + "CPUPerc": "0.72%", + "Container": "bbf844ba619b0bea8aa50bace9db4d966527cf880e9f13d1dc0470eb4f03dac3", + "ID": "bbf844ba619b", + "MemPerc": "0.04%", + "MemUsage": "3.5MiB / 7.649GiB", + "Name": "mtusi2026lab6-redis-1", + "NetIO": "5.82kB / 937B", + "PIDs": "6" + }, + { + "BlockIO": "3.13MB / 504kB", + "CPUPerc": "0.38%", + "Container": "a66f26b3045359ab3e71d12e3899420721be615cbce713366f0b69d12ca1d08f", + "ID": "a66f26b30453", + "MemPerc": "52.48%", + "MemUsage": "134.3MiB / 256MiB", + "Name": "mtusi2026lab6-tempo-1", + "NetIO": "35.8kB / 14.7kB", + "PIDs": "14" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.18%", + "Container": "d7f3abb5618a77a1a51800da051b5518049868344387fa436735d5f5c78f3366", + "ID": "d7f3abb5618a", + "MemPerc": "5.64%", + "MemUsage": "57.72MiB / 1GiB", + "Name": "mtusi2026lab6-worker-1", + "NetIO": "70.9kB / 140kB", + "PIDs": "4" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.14%", + "Container": "9158f772815a31ce40f2ee3e9ac959c7a9b8784a7a4bc1b603b30a710b6da4ba", + "ID": "9158f772815a", + "MemPerc": "5.67%", + "MemUsage": "58.05MiB / 1GiB", + "Name": "mtusi2026lab6-worker-2", + "NetIO": "330kB / 444kB", + "PIDs": "4" + } +] diff --git a/evidence/restart-after.txt b/evidence/restart-after.txt new file mode 100644 index 0000000..6f60208 --- /dev/null +++ b/evidence/restart-after.txt @@ -0,0 +1,15 @@ +/mtusi2026lab6-api-1 2026-11-16T18:04:37.603181695Z +/mtusi2026lab6-api-2 2026-11-16T18:04:37.253020096Z +/mtusi2026lab6-broker-1 2026-11-16T18:04:31.966805543Z +/mtusi2026lab6-db-1 2026-11-16T18:04:33.227463421Z +/mtusi2026lab6-dispatcher-1 2026-11-16T18:04:34.842754932Z +/mtusi2026lab6-grafana-1 2026-11-16T18:04:32.27534289Z +/mtusi2026lab6-lb-1 2026-11-16T18:04:32.751031805Z +/mtusi2026lab6-loki-1 2026-11-16T18:04:32.918094644Z +/mtusi2026lab6-minio-1 2026-11-16T18:04:32.058002577Z +/mtusi2026lab6-otel-collector-1 2026-11-16T18:04:31.686604058Z +/mtusi2026lab6-prometheus-1 2026-11-16T18:04:32.56102027Z +/mtusi2026lab6-redis-1 2026-11-16T18:04:32.370360741Z +/mtusi2026lab6-tempo-1 2026-11-16T18:04:32.993632697Z +/mtusi2026lab6-worker-1 2026-11-16T18:04:35.257045333Z +/mtusi2026lab6-worker-2 2026-11-16T18:04:35.099706108Z diff --git a/evidence/restart-before.txt b/evidence/restart-before.txt new file mode 100644 index 0000000..55cde07 --- /dev/null +++ b/evidence/restart-before.txt @@ -0,0 +1,15 @@ +/mtusi2026lab6-api-1 2026-11-16T18:03:22.799944493Z +/mtusi2026lab6-api-2 2026-11-16T18:03:22.473205918Z +/mtusi2026lab6-broker-1 2026-11-16T17:58:30.668320823Z +/mtusi2026lab6-db-1 2026-11-16T17:52:46.060859992Z +/mtusi2026lab6-dispatcher-1 2026-11-16T18:04:20.075887125Z +/mtusi2026lab6-grafana-1 2026-11-16T17:52:44.857859824Z +/mtusi2026lab6-lb-1 2026-11-16T17:53:43.675823414Z +/mtusi2026lab6-loki-1 2026-11-16T17:52:45.648452157Z +/mtusi2026lab6-minio-1 2026-11-16T17:28:01.072461454Z +/mtusi2026lab6-otel-collector-1 2026-11-16T17:52:44.430157283Z +/mtusi2026lab6-prometheus-1 2026-11-16T17:52:45.274536244Z +/mtusi2026lab6-redis-1 2026-11-16T17:52:43.608879149Z +/mtusi2026lab6-tempo-1 2026-11-16T17:52:43.224860803Z +/mtusi2026lab6-worker-1 2026-11-16T18:04:20.653472809Z +/mtusi2026lab6-worker-2 2026-11-16T18:04:19.680572096Z diff --git a/evidence/signal-link.json b/evidence/signal-link.json new file mode 100644 index 0000000..0cc88e1 --- /dev/null +++ b/evidence/signal-link.json @@ -0,0 +1,126 @@ +{ + "trace_id": "a08035a4bdd0efa15ee8dfcc0814a041", + "request_id": "059de23d-0922-4795-84bd-dd7166543a2e", + "spans": [ + { + "name": "POST /api/v1/chats/{chat_id}/attachments", + "service": "messenger-api", + "spanId": "ySzQ7cp0lhc=", + "parentSpanId": null, + "kind": "SPAN_KIND_SERVER", + "status": {} + }, + { + "name": "postgres.session.lookup", + "service": "messenger-api", + "spanId": "S5ua9uX3Hz8=", + "parentSpanId": "ySzQ7cp0lhc=", + "kind": "SPAN_KIND_CLIENT", + "status": {} + }, + { + "name": "s3.staging.put", + "service": "messenger-api", + "spanId": "W65j5eorpic=", + "parentSpanId": "ySzQ7cp0lhc=", + "kind": "SPAN_KIND_CLIENT", + "status": {} + }, + { + "name": "attachments publish", + "service": "messenger-dispatcher", + "spanId": "1Pz7sSsC46Y=", + "parentSpanId": "ySzQ7cp0lhc=", + "kind": "SPAN_KIND_PRODUCER", + "status": {} + }, + { + "name": "attachments consume", + "service": "messenger-worker", + "spanId": "69iyCQQd/Ck=", + "parentSpanId": "1Pz7sSsC46Y=", + "kind": "SPAN_KIND_CONSUMER", + "status": { + "code": "STATUS_CODE_ERROR" + } + }, + { + "name": "attachment.process", + "service": "messenger-worker", + "spanId": "Rqg7ibEx3xo=", + "parentSpanId": "69iyCQQd/Ck=", + "kind": "SPAN_KIND_INTERNAL", + "status": {} + }, + { + "name": "postgres.claim", + "service": "messenger-worker", + "spanId": "NSfKu4KeKm8=", + "parentSpanId": "Rqg7ibEx3xo=", + "kind": "SPAN_KIND_INTERNAL", + "status": {} + }, + { + "name": "attachments publish", + "service": "messenger-dispatcher", + "spanId": "J+nVboJFSAk=", + "parentSpanId": "ySzQ7cp0lhc=", + "kind": "SPAN_KIND_PRODUCER", + "status": {} + }, + { + "name": "attachments consume", + "service": "messenger-worker", + "spanId": "AqkcWGF6mBI=", + "parentSpanId": "J+nVboJFSAk=", + "kind": "SPAN_KIND_CONSUMER", + "status": {} + }, + { + "name": "attachment.process", + "service": "messenger-worker", + "spanId": "NzYfVgeqm3Q=", + "parentSpanId": "AqkcWGF6mBI=", + "kind": "SPAN_KIND_INTERNAL", + "status": {} + }, + { + "name": "postgres.claim", + "service": "messenger-worker", + "spanId": "i+oqIAPzLhA=", + "parentSpanId": "NzYfVgeqm3Q=", + "kind": "SPAN_KIND_INTERNAL", + "status": {} + }, + { + "name": "s3.staging.read", + "service": "messenger-worker", + "spanId": "VYZ47B4qInA=", + "parentSpanId": "NzYfVgeqm3Q=", + "kind": "SPAN_KIND_INTERNAL", + "status": {} + }, + { + "name": "image.render_and_write", + "service": "messenger-worker", + "spanId": "vv1qEQ5DvKM=", + "parentSpanId": "NzYfVgeqm3Q=", + "kind": "SPAN_KIND_INTERNAL", + "status": {} + }, + { + "name": "postgres.publish_result", + "service": "messenger-worker", + "spanId": "h8VyMOuu2J4=", + "parentSpanId": "NzYfVgeqm3Q=", + "kind": "SPAN_KIND_INTERNAL", + "status": {} + } + ], + "worker_events": [ + "worker.delivery", + "worker.ready", + "worker.retry" + ], + "marker_absent": true +} diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 0000000..7edfd47 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1 @@ +PASS: lab6 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/source-sha256.json b/evidence/source-sha256.json new file mode 100644 index 0000000..24bd601 --- /dev/null +++ b/evidence/source-sha256.json @@ -0,0 +1,163 @@ +{ + "Dockerfile": "df1d8c5e1c81074d4fead2dc32b04ad884547e4cc840b0703731caba17856199", + "build.gradle": "36dbf09d9968ada1d2545f94e9bb8f502994da3fa9afb4b4f8c3f5aaa1016d54", + "compose.local.yaml": "ac71af616f79b7143146519c17f38f75ecd96d16fadf842824dd3e9141180632", + "compose.test.yaml": "6682c8f1f6fe539cea111cf0c3b0dce08a167144137f01a342a15c2d8b2c7cab", + "docker-compose.yml": "90ed9f03ed1c1f110c10230070450c0c28731943f5bb09aac1af0bb32860ca43", + "infra/otel/README.md": "709332a3e910f9d42cb2a44c8d24b13aff6ce0ac7b671840e3cc273ddb82d3d6", + "infra/otel/alerts.yaml": "d8a2d0441d67d783e1480dc9638deb3e1d234990e7c6456a8952882d797b9cdc", + "infra/otel/collector.yaml": "c364f62c46c9b521f38111ec0b37d7a44e0a7f62558922b2317130fef35a0cd9", + "infra/otel/grafana/dashboards/messenger.json": "aa087a7c3b4b52f168cd7376775cfed83538a263792d52a5774ceaa348f33901", + "infra/otel/grafana/provisioning/dashboards/provider.yaml": "8c697d182ee7d236c0969baca01197a9f781dfa8e3476e27d50e34923c99df6d", + "infra/otel/grafana/provisioning/datasources/datasources.yaml": "499a03f6bd8d60478ea2950ae5906dfdc1429647a0cc9e464bd9e929afb34fc2", + "infra/otel/loki.yaml": "6973cafcbdf83c2c8d1cda726ee1369120ea570f15f22c88c310fc3317c97b33", + "infra/otel/prometheus.yaml": "826a001023f4c7c45c3564d7bfbd4895a296b2a01a24e40e32d2d7da72d41b0a", + "infra/otel/runbook.md": "55364c44a0914a0a87d1330a4b0cd194b9b921da08447f44a97b9e9d9be97738", + "infra/otel/tempo.yaml": "5eff13657c42d66517407ea1b02cb9756204309ed2c94d9a2f726dc6701709bb", + "scripts/bootstrap.ps1": "f3f6e9ca42bdc1f778aa8f7e7e5d0c61bcf16bf3f35eb1a36b2d48274c60e22c", + "scripts/capture_evidence.py": "72abbab258d61c766f7751fe198496ea800391c2347b9722b049e39796eed5ad", + "scripts/check_contract.py": "61e300fe5c1074360418e24b4653b12f96808247a2beaf2a64859aa360f81493", + "scripts/restart.cmd": "a8b170bd67efd9fd2cbd0db837984f826cf0f09c9b73754798b79ff481b1d589", + "scripts/restart.example.sh": "7cbfc7f518b975ee3ca37a645359ff734ab0686789b795a9ac845789eb46fbd9", + "scripts/restart.ps1": "b78883af6d19424c401d8681a9beef3f6d8e1d015faf7bc02b1001ce2c621d29", + "scripts/restart.sh": "4b6969335a52eb8fed1a4864a740989b64a4df10fbf4997c25583525867fc4ca", + "scripts/run_tests.ps1": "64b719f178f74877c5f515a1a8c4126525f62699412708dc630f68c62478459d", + "scripts/stop-one.cmd": "b636287c36f4c8f52e7840513506565bab0a1d480bb9187fdb40d1c0101616fa", + "scripts/stop-one.example.sh": "6467c6e1515dee72982f85bced3f3862601ae0dbfafd4fd4818ef420f1dea040", + "scripts/stop-one.ps1": "c60f70430dbea3916c68011ad5ccbaf329a44a43f1e2d55f33597e4a495cc0b6", + "scripts/stop-one.sh": "fec1a774f4387676b8b6adf2398a41e7b0108a0bc745103a3079b23acfe6f668", + "scripts/style.py": "db987b555315d9eda5ffbe60a5472d76fc8df7a668e6c9db2223865fb9beee47", + "scripts/validate_openapi.py": "cd7a33221b1860c5d2330e52afa6cb222d375db671ae9ab379ba51a0d3a77946", + "src/main/java/ru/mtusi/messenger/MessengerLab1Application.java": "88c6dc51a1541bfb60067a346460ce03c54b38fbd2a948991800fed3cace3c36", + "src/main/java/ru/mtusi/messenger/cache/CacheHealthTracker.java": "3f061f664b84ea0492e2bb61e154a595e7b3c6d18cc673fa95da6d69944ef737", + "src/main/java/ru/mtusi/messenger/config/JacksonConfig.java": "39b987cd4529a9d34744bf55efc825b6cfb2bae93d77cc4330b73ed3475cc993", + "src/main/java/ru/mtusi/messenger/config/MinioConfig.java": "f83fa7eb639d3bb3a69a25396b5956a7ae7fd14086dce095d1555523fa4f82af", + "src/main/java/ru/mtusi/messenger/config/OpenApiConfig.java": "2abe3c50fcb7405743f7f75dde186961d8c6641f3afbfb5a5167f89245dc6155", + "src/main/java/ru/mtusi/messenger/config/RateLimitConfig.java": "61c0979d9198e02ae93e1d88b220f36fb9e40cb6014047dc01c6a4808143057e", + "src/main/java/ru/mtusi/messenger/config/RedisConfig.java": "38e4ab26af31cad16ad4c4d989948f2e7a4e4aff6774abe0d2659f407d129c88", + "src/main/java/ru/mtusi/messenger/config/SecurityConfig.java": "92ea3a1ab3d4a81badadef388b367e1a14a1216b27d595aa6dc751c465f7ae23", + "src/main/java/ru/mtusi/messenger/config/properties/AppProperties.java": "351ecfb59fb0c88fec25deaa0abd82c772b1496eb70d03f54e55c254b3b6816c", + "src/main/java/ru/mtusi/messenger/config/properties/IdempotencyProperties.java": "065ed8206d932c43a32568d60c6ba3f210073d27aa50322c3230a0f149cdd822", + "src/main/java/ru/mtusi/messenger/config/properties/InstanceProperties.java": "0ec19bb2d242f6831ca113d25b0183aac98b6dad67b7a535226f3a54053ba65d", + "src/main/java/ru/mtusi/messenger/config/properties/RateLimitProperties.java": "7fec3f5cf685f8381a7787b49f998a0c406f1aa83d15312607a115f8d98cba47", + "src/main/java/ru/mtusi/messenger/config/properties/RedisProperties.java": "a9e0b2c81b83f490935d40c81c0b48b8de044deb6ef1c7a35d08b665f88ada4a", + "src/main/java/ru/mtusi/messenger/config/properties/S3Properties.java": "9c53f5235bb5faad050a3e46b5ea3bbc3022b3ee644dd72816fe449aeb686df5", + "src/main/java/ru/mtusi/messenger/config/properties/SessionProperties.java": "fcd1a5d72655ad35dcd24101a6b68a49e1ea60b14498cd34a5090e2893141d90", + "src/main/java/ru/mtusi/messenger/config/properties/UploadProperties.java": "0af77902ba3345e8eb60dbbf3ee8e81b90e996647ed8ebff1d1102e04026eaba", + "src/main/java/ru/mtusi/messenger/controller/AttachmentController.java": "19705b6da3010c26e423e8cc100c522ad9c8c01bc1b07a494a0cd710086db404", + "src/main/java/ru/mtusi/messenger/controller/AttachmentsApi.java": "64bf2fc9c25bb161e28d900ba76e4b6d91c257ede01259ead9d63346ac26b80f", + "src/main/java/ru/mtusi/messenger/controller/AuthApi.java": "45acd2b55d8c289f0daf9f70c0fc38477a633d92ccac6bf5735b533a7115e1b9", + "src/main/java/ru/mtusi/messenger/controller/AuthController.java": "a5db3355b44a9f4cfe71d181b62668b9c7dcad2888b9ef33932034d23ba99873", + "src/main/java/ru/mtusi/messenger/controller/ChatController.java": "49ef6e0e2135dfcd35a61cec7849835d6f2c75cc41e9d01c0b08e823c70034ba", + "src/main/java/ru/mtusi/messenger/controller/ChatsApi.java": "dc4f021487ba1950094b0d53b3764c09252f1e3053b6930171093fa890cb8541", + "src/main/java/ru/mtusi/messenger/controller/HealthApi.java": "f5f3645ea4ecb7043bc721aa6f245f3e1a9e1dbeab3de1af24a2c3db30ef686e", + "src/main/java/ru/mtusi/messenger/controller/HealthController.java": "ca929dc4f5db7c3a380aa5a16e31755c5f98570a70a9c60f76e7a8b91096911e", + "src/main/java/ru/mtusi/messenger/controller/MessageController.java": "c1c42e223bd3cd945fd27f911edad74a14a87502f0efbc9b43565f52576baf3e", + "src/main/java/ru/mtusi/messenger/controller/MessagesApi.java": "8a085fabcef8a811bef855301304478f38b983c9fa2668c01db03259e433141f", + "src/main/java/ru/mtusi/messenger/controller/UserController.java": "52082cf75de23b57eef6b211207e1a8508e5e22e4ae3ffefc010d5fe3f6d21a0", + "src/main/java/ru/mtusi/messenger/controller/UsersApi.java": "2f95871d9334ae832c05b4a0a2b9b5faeddc44a036a2101fd8a64da033b56a21", + "src/main/java/ru/mtusi/messenger/controller/controllerAdvice/GlobalExceptionHandler.java": "154bc885ee66e06899a9479a507122289e67dca84f992b7fd79eec558fa3e6cb", + "src/main/java/ru/mtusi/messenger/dto/AttachmentDTO.java": "8a5ecfa0018bce8bb3be52cc9031613b23b0c4ca129a8dbca2898459c334f343", + "src/main/java/ru/mtusi/messenger/dto/ChatDTO.java": "b9dbd4a8f043c5e1287c53d20c1b7d0aa9d713888cbd592c06fdd56a61e8bdd8", + "src/main/java/ru/mtusi/messenger/dto/ChatPageDTO.java": "83306e7e8dd52eccb540302c5b96f49aee4d65200b984b19edd00ec03559ba85", + "src/main/java/ru/mtusi/messenger/dto/CreateChatDTO.java": "170cba1f52075043d61ca81077ad2e67fb85cbfb15599ec8dfbc85d341e76604", + "src/main/java/ru/mtusi/messenger/dto/CreateMessageDTO.java": "b76748a82ec6f1a88eca19e54034a65e152fb986262e37120ea9af45a2912ada", + "src/main/java/ru/mtusi/messenger/dto/CreateUserDTO.java": "f8903c32dc8eca5dd306f579b2ee2a8c7c4ae0aa512f4534a477a412bb41f7c6", + "src/main/java/ru/mtusi/messenger/dto/ErrorDTO.java": "29d73ad94c71db058faa3d915f32acaa939573687702c5a7153a6714061f00fe", + "src/main/java/ru/mtusi/messenger/dto/ErrorErrorDTO.java": "87e0e171ac72c43f7435e5b219d3fe6380a8c940e7bb87958e2df7f70625ccf3", + "src/main/java/ru/mtusi/messenger/dto/HealthDTO.java": "02306d373316c6f96278d97f7bd9fed76299b5eb79cffb5636f328d2d1de70d3", + "src/main/java/ru/mtusi/messenger/dto/MessageDTO.java": "7d8d0cddaa326323099120b29da74bd8d62dda97d1641c6f7ba687bd3b2e84bf", + "src/main/java/ru/mtusi/messenger/dto/MessagePageDTO.java": "4b4fd18119cfa542a5120142e6a03e310385525dd6e656fdcdde8d55a92382c2", + "src/main/java/ru/mtusi/messenger/dto/SessionDTO.java": "90827f5fd5fef55ae3e183369a4f239e109c02fcd8ccffa2e8e1846553b4739a", + "src/main/java/ru/mtusi/messenger/dto/UserDTO.java": "3629a3e031e9bcf84a602ac11bc95ae6a7870b25a0452543c24ae940d96a4cfb", + "src/main/java/ru/mtusi/messenger/dto/VariantDTO.java": "7d7bb208f49ba07386e97c6e02e3bda912034c921a6b3b22c4f534951c6483c5", + "src/main/java/ru/mtusi/messenger/exception/ApiErrorFactory.java": "62373a2a9e11fed20ae2a15b92446f3e3c6d4cea461e274bc5ff5a15fcb34f08", + "src/main/java/ru/mtusi/messenger/exception/ConflictException.java": "80b1b6602b9fb74ed763b07905cd5d084d76a53b46e8e1d31734e29ccf9c6e6d", + "src/main/java/ru/mtusi/messenger/exception/InvalidRequestException.java": "15c4970028f71a8ad6c8cbf317158754445e8f8b4b7414c88d9d56e59b803680", + "src/main/java/ru/mtusi/messenger/exception/NotFoundException.java": "0be747b28a29ed79c032a4256f1e2ac92365d0282219dcd8e0608fa3e45dfbdb", + "src/main/java/ru/mtusi/messenger/exception/PayloadTooLargeException.java": "14104310e0bed9c878c822d3269ec239dbbd9aa21916eb1b3df9ffe0a008bcfb", + "src/main/java/ru/mtusi/messenger/exception/RateLimitedException.java": "cce2f144983ba1540e359d34bd4169746ea1eb7869ba6ae5dd59e41acca19166", + "src/main/java/ru/mtusi/messenger/exception/ServiceUnavailableException.java": "79debd21b15f38917fa83340a44dd13543ee44317b17c2dbb706a3a4c44bc650", + "src/main/java/ru/mtusi/messenger/exception/StaleAttachmentException.java": "5288d953312f8f8bb6f9a98ff4150fa64271827b0934dd9b354c2fd7ff8f11d4", + "src/main/java/ru/mtusi/messenger/exception/StorageUnavailableException.java": "cdc2fe016cef32b0c93c8f51fb4b85b18cd8ed74051ff1fd4877882e469b23d9", + "src/main/java/ru/mtusi/messenger/exception/UnauthorizedException.java": "251dc2ee2419729e44410d62c97a98eff08698c801f519c76e1fc0a62e55c8a3", + "src/main/java/ru/mtusi/messenger/exception/UnsupportedMediaTypeException.java": "6a0573dba993ef765bc58ff0a4ce1aae0a1d7722cfecb8dc3858a7efc8245cad", + "src/main/java/ru/mtusi/messenger/filter/InstanceIdFilter.java": "3e0112a9f90b79c433a6fbdf77afc20b1bf0387e693496d5c428c9c595f409f2", + "src/main/java/ru/mtusi/messenger/filter/RateLimitFilter.java": "3d1e0ec75c63345acd42762a7c5b3e965acf3f254f92dc20a3b4667f2f56e089", + "src/main/java/ru/mtusi/messenger/filter/RequestIdFilter.java": "217c4f3ae7288ac92d56fda6f61fcae9d01876c0dc6b085e27758e5ad23e7397", + "src/main/java/ru/mtusi/messenger/filter/TelemetryFilter.java": "187e45dd0af3d5569e8bedd49210546432afb0f6924928f2717d43e514a3fbc0", + "src/main/java/ru/mtusi/messenger/mapper/AttachmentMapper.java": "75c549f7d843843ec502aeb0cff96f2b1cc841df8b8b66da1684dd0fc59c1128", + "src/main/java/ru/mtusi/messenger/mapper/ChatMapper.java": "761f6fa8d28c16a4b798f609671e82273a17452821e3bf971f131afe3d00f35f", + "src/main/java/ru/mtusi/messenger/mapper/MessageMapper.java": "099149f2060e90fdae382fa28fd3bfd3ec621b740a98a7a9bc8888f6ed649ed1", + "src/main/java/ru/mtusi/messenger/mapper/SessionMapper.java": "daebe3300899220c98d26d29728d14c3801c1ba5a061411a7265fd321c6ac0e7", + "src/main/java/ru/mtusi/messenger/mapper/UserMapper.java": "c05e950c85dcfedfed21ef5813b62bbdb8f642f2e172d5d4baea68ddaf294241", + "src/main/java/ru/mtusi/messenger/model/Attachment.java": "c0076e1b7ff8b401d340d686ecab9446cddbc4c04e55277f31da7ef866d8aab1", + "src/main/java/ru/mtusi/messenger/model/AttachmentStatus.java": "6aaa777b2b9042618c7bd25aa44a7c04e1983053d7272167bae16783a7072e79", + "src/main/java/ru/mtusi/messenger/model/Chat.java": "997e6acb77cd3f34cfe5745015fefb3356521fec1a53a633e2d81af4b1a5d8ea", + "src/main/java/ru/mtusi/messenger/model/IdempotencyKey.java": "d74360e291e3601afad564c463b85a9e89fef90351fa32d6cdd02a4fce4d9ffb", + "src/main/java/ru/mtusi/messenger/model/Message.java": "c58fc53563602ee75bec4b492783261bf5fad5b1f3803333a5f7e2a7ce62fd98", + "src/main/java/ru/mtusi/messenger/model/Session.java": "039980fd45fc2551f875958f34a40d6a155f9d10414dbe20875b25beaf16f96e", + "src/main/java/ru/mtusi/messenger/model/User.java": "01ab578d2ecfedb739e03fc0107f097e3c98a2c7e3c267d351837902723b79c8", + "src/main/java/ru/mtusi/messenger/repository/AttachmentRepository.java": "002f8318f97cb797068cc14b1826ece7dab096349e429b269c6395ba23f7f033", + "src/main/java/ru/mtusi/messenger/repository/ChatRepository.java": "2336e381b0271ea744a1d58ab10ce97644a1d242c8ec180bbb79f072c79b8d70", + "src/main/java/ru/mtusi/messenger/repository/IdempotencyRepository.java": "9e6fdb7b1c9ff7b32b182d223f06a2df5cbac851f875f285740cd7b71a78124b", + "src/main/java/ru/mtusi/messenger/repository/MessageRepository.java": "21b00bd89a161206826cfa681b20019ba3140adc841f60d0c4b51b63603bca04", + "src/main/java/ru/mtusi/messenger/repository/SessionRepository.java": "071c7d9c30dcc75356eb87324f309fc698aea72166545d0b06ac6052f8aa39ec", + "src/main/java/ru/mtusi/messenger/repository/UserRepository.java": "f7bcc2b1dd0cc1a3ae35297cca1daa23579bdad56f8c5c491a4e61bc60a7ddff", + "src/main/java/ru/mtusi/messenger/security/ApiAuthenticationEntryPoint.java": "ea207873b9717735bb28b2d41356e9a2104ad262c9fe33db0436f4a47fe98775", + "src/main/java/ru/mtusi/messenger/security/BasicCredentials.java": "0f381c0e02d881d8463ca91ccdfbcc20c74bc21125bf7983e5b326e7f96e2e69", + "src/main/java/ru/mtusi/messenger/security/SessionAuthenticationFilter.java": "7cfeea0056347d5b27b9ba40b05b93dbf955ccc5ba7863128b7b06ecdeebc3b9", + "src/main/java/ru/mtusi/messenger/security/SessionAuthenticationProvider.java": "6e6bfc710105c45e25e46f84e454eec7f7fc578cc80dd693b739d5f6833edccc", + "src/main/java/ru/mtusi/messenger/security/SessionAuthenticationToken.java": "231317a83df87a7dcd23114ce3ca42859f91c739f645f7b43209819797b2fe0d", + "src/main/java/ru/mtusi/messenger/security/SessionPrincipal.java": "fff3d1d27cf7079947315a5a7e2e960f5f7e245a96a98157430cad474e4ab9f6", + "src/main/java/ru/mtusi/messenger/service/AttachmentService.java": "3ffd7b3b6bcda3fbc60eb4934a98a0636652275affd4afa425939ec4adefff91", + "src/main/java/ru/mtusi/messenger/service/AttachmentVariantService.java": "015880d98f570597109f7eca26aedb3f6f24667c4b28ae62a9309420443793b1", + "src/main/java/ru/mtusi/messenger/service/AuthService.java": "8d93d2f458fabef523ed6af97d2f8ed11ecd2b9b01a4eba4533fc31e22c148e4", + "src/main/java/ru/mtusi/messenger/service/ChatService.java": "76f0e94e6ce6c6d3c9b8dfdda363d1b7a2f5017901e9cdeafc3a00f120f4a473", + "src/main/java/ru/mtusi/messenger/service/HealthService.java": "ad7a32cbeed901b4913865e1a66e48d45cc35d0eb0f945e1e2eed27c4029d9fa", + "src/main/java/ru/mtusi/messenger/service/IdempotencyService.java": "968b1d1f5037b19707a84d45cb8f3173b818c229dc3e3492ae9cb2eb53f21218", + "src/main/java/ru/mtusi/messenger/service/MessageService.java": "5a11b2dac973d6b428c54c02605cc8e331f487135355cbef025c46195494c2b6", + "src/main/java/ru/mtusi/messenger/service/ProcessingJobService.java": "256e785dbbf97beb7290f35d19c46149e3473ed5b6b27d40c75c5213e1e98209", + "src/main/java/ru/mtusi/messenger/service/SessionCacheService.java": "320e6ea2d6f9618ef9cdda57ae871f69c49eb17e7367b70ca0a7b06815c2966f", + "src/main/java/ru/mtusi/messenger/service/SessionService.java": "6f9b61ae0ea28e74d32930f5ac079967c406f439cecda5d287713aa4de8ac421", + "src/main/java/ru/mtusi/messenger/service/UserService.java": "cf9b9b2eeacba63314d2d883a0a0d59278c7a7a2897fca457b26eabf4e03c5e5", + "src/main/java/ru/mtusi/messenger/telemetry/Telemetry.java": "e40fe40c295986dd37ff8551014fb84a60c8095f90e039e60e3dd617716b1c9d", + "src/main/java/ru/mtusi/messenger/util/CursorCodec.java": "f218c8f1380a9d15a49a2c790a614a967ac31ebc5a353cd4949b5edc4ec338ba", + "src/main/java/ru/mtusi/messenger/util/TokenGenerator.java": "a8de6b5f2253b581f2cf1f9a2c04655ca5893ec1cc4b5e71058f2a2ae0617305", + "src/main/java/ru/mtusi/messenger/util/TokenHasher.java": "86d2579f53463d2a180e9de12144c656643399ca57d681582347b4a8f794484d", + "src/main/java/ru/mtusi/messenger/util/converter/IdempotencyPayloadConverter.java": "3fde1a9dd714ca64da80bc666a8d0bd8486c301e550cd0f55ac9327acba800ad", + "src/main/java/ru/mtusi/messenger/validation/AttachmentValidator.java": "91e529f3eac82500341b25ea5c109b671aec7af150386e18d20cd3e9ffee5072", + "src/main/java/ru/mtusi/messenger/validation/ChatValidator.java": "8fd4ba91aaa4e21dd2b372f17fdfd126d62a4039efacf1686f3e2b3143a5bd54", + "src/main/java/ru/mtusi/messenger/validation/IdempotencyValidator.java": "99fe04376e875a2e63b5c884e7128dcddceaa200622033a5c8e7f9b411291683", + "src/main/resources/application.yaml": "a0dcc8cb089e6bc66319aa5b6dd18805cbd786521375619abf13763afe111d8f", + "src/main/resources/db/migration/V1__init.sql": "b4a40c651fc3e8fb10c588f7374164dbe716bef508a3c7609e24a9284aaad894", + "src/test/java/ru/mtusi/messenger/AttachmentIntegrationTest.java": "8d4498036d995cd2a60c071e66dfb1d9d0a075580935c2456dd313a924a21044", + "src/test/java/ru/mtusi/messenger/MessengerLab1ApplicationTests.java": "d0e7aeee3b3c3c151b241a14e1c41c683401513bdd168e9d5b58fea4905f0132", + "student-tests/incidents.py": "44ba60cae219601472283ff7448b8c9a30590238fd5df581e53794c932ebefc1", + "student-tests/observe.py": "ff05da61f0ca5b8705985e6acb34d87b17c35d455e781aebd276b92f9f7e2871", + "student-tests/pipeline_ack_guard.py": "0477930ec4058f412c36f06427218d4786fb9d250b502d69fce658a74aa3dd44", + "student-tests/pipeline_boundaries.py": "051277b8fea2026403a64e951c48d67abfed4aa4bfc06862b01749a4c3ca463f", + "student-tests/pipeline_core.py": "2bbb8d77f0286fc551ea4b08a38268a027653d473bbe8bb13b53c8a11941aa98", + "student-tests/pipeline_faults.py": "90ecd9babe860e9d556cee0c416a96f71617d8c627620796b832be15289969a7", + "student-tests/pipeline_filename.py": "aaaa67ecd54e296181f490fb43a284dd51602b35b09ce7759ab42fbbccf57d9d", + "student-tests/pipeline_history_capacity.py": "eaf6fd80c0c09d9fda2da5ae4aff6111d9f49b4726893081a24e29c1dd23041e", + "student-tests/pipeline_retention.py": "2a2ddb427366b75b547934201d19135fb5fa3ffe4f2747375bd7269993e6e157", + "student-tests/pipeline_support.py": "1530715f9b77f40f4a65f577fe9e9b714206b992d75f4d0de9a5c6d621cc16c0", + "student-tests/pipeline_upload_crash.py": "a97912a371b17ca21c00c784cb93182a8ba876c7fa80eb4584128e1eef2b74dd", + "student-tests/telemetry_support.py": "2d3be7f873dff27cae9ddda15cd483c6fbf0a66fa3edd7d61e614c1584cf7809", + "worker/Dockerfile": "1c777cb030f4d85ad2e683dc49555746d5bf79c1e8106d1e74008479cd52ae36", + "worker/pipeline/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "worker/pipeline/broker.py": "2d35ede652cc5068399c54f11c249216ba507cd44e7dd473c2fcfb9de680c630", + "worker/pipeline/cleanup.py": "900f923625b362f0c8983b91ba9c10aee5cfe72f991990ef026f1da0f00708fd", + "worker/pipeline/config.py": "bba3c1b90b751c0c91c872864178c8339f4b9ee7dc8da92edb9102c0c7ec98c0", + "worker/pipeline/faults.py": "af5e7096351605a7e9353a409eb1ccc179281ee893bdc98f4d51240297a05bb0", + "worker/pipeline/images.py": "c56c7cdc4ddaf271d1f66bbad88ae2e25a34dc8416e74dddc23abbc135bc7177", + "worker/pipeline/main.py": "308e066811b0d31ec71b7782c2649ff52c2a8d616a8f656c60b7aaaa234549b4", + "worker/pipeline/operator.py": "8ac54e1a8c69d3ebbb43e68df75d4417a673525d9a40efd1729d6fea33d9937c", + "worker/pipeline/outbox.py": "859caa9cf95b36203a57f95a05c14ecd12c19f7720147c30690054be3eaae396", + "worker/pipeline/process.py": "80adbd495c8b96d8fae00e4e8442907fbf9d716827c536c29ad76f753aa5f5f9", + "worker/pipeline/repository.py": "a1f7f111b1ae45604cd8a13a58284af5f339b49f811f6325465944b2e1ecb8f5", + "worker/pipeline/storage.py": "89d5675f9354c6692fb0f8c646dc09a6bc76ea1055eaa53d768cf3e8d4d1e004", + "worker/pipeline/telemetry.py": "90f3495ad1957b8368a6a62929c192161de12bccdc5ba9e37bb913b518c245ed", + "worker/requirements.txt": "e95d64513a41e4568cccde0bc4869e5ccf07cbdad21344b91aab01ddf4523c83" +} diff --git a/evidence/stack-final.txt b/evidence/stack-final.txt new file mode 100644 index 0000000..49f1f7b --- /dev/null +++ b/evidence/stack-final.txt @@ -0,0 +1,16 @@ + Container mtusi2026lab6-api-1 Running + Container mtusi2026lab6-minio-1 Running + Container mtusi2026lab6-redis-1 Running + Container mtusi2026lab6-db-1 Running + Container mtusi2026lab6-minio-1 Waiting + Container mtusi2026lab6-minio-1 Healthy + Container mtusi2026lab6-minio-init-1 Starting + Container mtusi2026lab6-minio-init-1 Started + Container mtusi2026lab6-db-1 Waiting + Container mtusi2026lab6-redis-1 Waiting + Container mtusi2026lab6-minio-init-1 Waiting + Container mtusi2026lab6-redis-1 Healthy + Container mtusi2026lab6-db-1 Healthy + Container mtusi2026lab6-minio-init-1 Exited + Container mtusi2026lab6-api-2 Starting + Container mtusi2026lab6-api-2 Started diff --git a/evidence/stack-ordinary.txt b/evidence/stack-ordinary.txt new file mode 100644 index 0000000..5a6e000 --- /dev/null +++ b/evidence/stack-ordinary.txt @@ -0,0 +1,56 @@ + Container mtusi2026lab6-minio-1 Running + Container mtusi2026lab6-lb-1 Running + Container mtusi2026lab6-db-1 Running + Container mtusi2026lab6-grafana-1 Running + Container mtusi2026lab6-tempo-1 Running + Container mtusi2026lab6-loki-1 Running + Container mtusi2026lab6-otel-collector-1 Running + Container mtusi2026lab6-prometheus-1 Running + Container mtusi2026lab6-redis-1 Running + Container mtusi2026lab6-broker-1 Running + Container mtusi2026lab6-api-1 Recreate + Container mtusi2026lab6-api-2 Recreate + Container mtusi2026lab6-api-2 Recreated + Container mtusi2026lab6-dispatcher-1 Recreate + Container mtusi2026lab6-worker-2 Recreate + Container mtusi2026lab6-worker-1 Recreate + Container mtusi2026lab6-dispatcher-1 Recreated + Container mtusi2026lab6-worker-2 Recreated + Container mtusi2026lab6-worker-1 Recreated + Container mtusi2026lab6-api-1 Recreated + Container mtusi2026lab6-minio-1 Waiting + Container mtusi2026lab6-minio-1 Healthy + Container mtusi2026lab6-minio-init-1 Starting + Container mtusi2026lab6-minio-init-1 Started + Container mtusi2026lab6-db-1 Waiting + Container mtusi2026lab6-redis-1 Waiting + Container mtusi2026lab6-minio-init-1 Waiting + Container mtusi2026lab6-redis-1 Healthy + Container mtusi2026lab6-db-1 Healthy + Container mtusi2026lab6-minio-init-1 Exited + Container mtusi2026lab6-api-2 Starting + Container mtusi2026lab6-api-2 Started + Container mtusi2026lab6-api-1 Starting + Container mtusi2026lab6-api-1 Started + Container mtusi2026lab6-broker-1 Waiting + Container mtusi2026lab6-api-2 Waiting + Container mtusi2026lab6-api-1 Waiting + Container mtusi2026lab6-broker-1 Waiting + Container mtusi2026lab6-api-2 Waiting + Container mtusi2026lab6-api-1 Waiting + Container mtusi2026lab6-api-2 Waiting + Container mtusi2026lab6-api-1 Waiting + Container mtusi2026lab6-broker-1 Healthy + Container mtusi2026lab6-broker-1 Healthy + Container mtusi2026lab6-api-2 Healthy + Container mtusi2026lab6-api-1 Healthy + Container mtusi2026lab6-api-2 Healthy + Container mtusi2026lab6-api-1 Healthy + Container mtusi2026lab6-dispatcher-1 Starting + Container mtusi2026lab6-api-2 Healthy + Container mtusi2026lab6-api-1 Healthy + Container mtusi2026lab6-worker-1 Starting + Container mtusi2026lab6-dispatcher-1 Started + Container mtusi2026lab6-worker-1 Started + Container mtusi2026lab6-worker-2 Starting + Container mtusi2026lab6-worker-2 Started diff --git a/evidence/stack-test-profile.txt b/evidence/stack-test-profile.txt new file mode 100644 index 0000000..a99f483 --- /dev/null +++ b/evidence/stack-test-profile.txt @@ -0,0 +1,83 @@ + Container mtusi2026lab6-minio-1 Running + Container mtusi2026lab6-broker-1 Recreate + Container mtusi2026lab6-otel-collector-1 Recreate + Container mtusi2026lab6-db-1 Recreate + Container mtusi2026lab6-redis-1 Recreate + Container mtusi2026lab6-grafana-1 Recreate + Container mtusi2026lab6-loki-1 Recreate + Container mtusi2026lab6-prometheus-1 Recreate + Container mtusi2026lab6-tempo-1 Recreate + Container mtusi2026lab6-otel-collector-1 Recreated + Container mtusi2026lab6-redis-1 Recreated + Container mtusi2026lab6-broker-1 Recreated + Container mtusi2026lab6-tempo-1 Recreated + Container mtusi2026lab6-grafana-1 Recreated + Container mtusi2026lab6-loki-1 Recreated + Container mtusi2026lab6-prometheus-1 Recreated + Container mtusi2026lab6-db-1 Recreated + Container mtusi2026lab6-api-1 Recreate + Container mtusi2026lab6-api-2 Recreate + Container mtusi2026lab6-api-1 Recreated + Container mtusi2026lab6-api-2 Recreated + Container mtusi2026lab6-dispatcher-1 Recreate + Container mtusi2026lab6-worker-1 Recreate + Container mtusi2026lab6-worker-2 Recreate + Container mtusi2026lab6-lb-1 Recreate + Container mtusi2026lab6-lb-1 Recreated + Container mtusi2026lab6-worker-1 Recreated + Container mtusi2026lab6-worker-2 Recreated + Container mtusi2026lab6-dispatcher-1 Recreated + Container mtusi2026lab6-tempo-1 Starting + Container mtusi2026lab6-redis-1 Starting + Container mtusi2026lab6-broker-1 Starting + Container mtusi2026lab6-otel-collector-1 Starting + Container mtusi2026lab6-grafana-1 Starting + Container mtusi2026lab6-minio-1 Waiting + Container mtusi2026lab6-prometheus-1 Starting + Container mtusi2026lab6-loki-1 Starting + Container mtusi2026lab6-db-1 Starting + Container mtusi2026lab6-tempo-1 Started + Container mtusi2026lab6-minio-1 Healthy + Container mtusi2026lab6-minio-init-1 Starting + Container mtusi2026lab6-redis-1 Started + Container mtusi2026lab6-broker-1 Started + Container mtusi2026lab6-otel-collector-1 Started + Container mtusi2026lab6-grafana-1 Started + Container mtusi2026lab6-prometheus-1 Started + Container mtusi2026lab6-loki-1 Started + Container mtusi2026lab6-db-1 Started + Container mtusi2026lab6-minio-init-1 Started + Container mtusi2026lab6-redis-1 Waiting + Container mtusi2026lab6-minio-init-1 Waiting + Container mtusi2026lab6-db-1 Waiting + Container mtusi2026lab6-minio-init-1 Exited + Container mtusi2026lab6-redis-1 Healthy + Container mtusi2026lab6-db-1 Healthy + Container mtusi2026lab6-api-2 Starting + Container mtusi2026lab6-api-2 Started + Container mtusi2026lab6-api-1 Starting + Container mtusi2026lab6-api-1 Started + Container mtusi2026lab6-api-2 Waiting + Container mtusi2026lab6-api-1 Waiting + Container mtusi2026lab6-broker-1 Waiting + Container mtusi2026lab6-api-2 Waiting + Container mtusi2026lab6-api-1 Waiting + Container mtusi2026lab6-api-2 Waiting + Container mtusi2026lab6-api-1 Waiting + Container mtusi2026lab6-broker-1 Waiting + Container mtusi2026lab6-broker-1 Healthy + Container mtusi2026lab6-broker-1 Healthy + Container mtusi2026lab6-api-2 Healthy + Container mtusi2026lab6-api-1 Healthy + Container mtusi2026lab6-dispatcher-1 Starting + Container mtusi2026lab6-api-2 Healthy + Container mtusi2026lab6-api-1 Healthy + Container mtusi2026lab6-lb-1 Starting + Container mtusi2026lab6-api-2 Healthy + Container mtusi2026lab6-api-1 Healthy + Container mtusi2026lab6-worker-2 Starting + Container mtusi2026lab6-dispatcher-1 Started + Container mtusi2026lab6-lb-1 Started + Container mtusi2026lab6-worker-2 Started + Container mtusi2026lab6-worker-1 Starting + Container mtusi2026lab6-worker-1 Started diff --git a/evidence/stop-one-after.txt b/evidence/stop-one-after.txt new file mode 100644 index 0000000..cae48d6 --- /dev/null +++ b/evidence/stop-one-after.txt @@ -0,0 +1 @@ +/mtusi2026lab6-api-2 05f31aaf15da exited diff --git a/evidence/stop-one-before.txt b/evidence/stop-one-before.txt new file mode 100644 index 0000000..c244334 --- /dev/null +++ b/evidence/stop-one-before.txt @@ -0,0 +1 @@ +/mtusi2026lab6-api-2 05f31aaf15da running diff --git a/evidence/style.txt b/evidence/style.txt new file mode 100644 index 0000000..ce07bd2 --- /dev/null +++ b/evidence/style.txt @@ -0,0 +1,4 @@ +All done! ✨ 🍰 ✨ +27 files would be left unchanged. +PASS 14 controller operations: one service call, constructor DI, API Javadoc +PASS Java AOSP / Python Black style diff --git a/evidence/telemetry-persistence.json b/evidence/telemetry-persistence.json new file mode 100644 index 0000000..b83a356 --- /dev/null +++ b/evidence/telemetry-persistence.json @@ -0,0 +1,6 @@ +{ + "trace_id": "a08035a4bdd0efa15ee8dfcc0814a041", + "request_id": "059de23d-0922-4795-84bd-dd7166543a2e", + "span_count_after_restart": 14, + "loki_log_found": true +} diff --git a/evidence/telemetry-persistence.txt b/evidence/telemetry-persistence.txt new file mode 100644 index 0000000..13ed8a3 --- /dev/null +++ b/evidence/telemetry-persistence.txt @@ -0,0 +1 @@ +PASS original Loki log and Tempo spans after restart diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000000000000000000000000000000000..61285a659d17295f1de7c53e24fdf13ad755c379 GIT binary patch literal 46175 zcma&NWmKG9wk?cn;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jepb31OcpxVKAgDnDqH}GqWiE0P=4v zM*~~qfA#gBV5Y@bA7+3DzB?F~`&QR(f^X2@Ud?}D{yE%DCHvdM^n&(};grErGS5tZ z)0sC#(phgcEQtOOkp8?$H#Mq-ZUMzJ{sGV*DzM)jo;M|3Z%-!PEWbznP2b&=Q@riG zlk>lv|J75!(1^Wz<~L>kt`!-7SU%tHo&RgV{pS2{s#)D0Wse1JLHtLi=ug!I?>6S9 zLejN_$q!o>{RPthtd(^a_okAL;4NH8iCeh;A2p`Cpf{CVu0?u&n3B{j(0^wQ{z$Ut zF3L@@iQ8Q&Df3g5{|HR{ZyGUoac@%YUrSm1Fhqr4PyPM@@$21lzgbIt%?SF#R&{=X@po9`C;Xsy0dCeKT$g13uui+5 z0{puM;jR|cUB@?HjlbPHOP;@U{EOm-yBIgK!q+d^|FClJUt#>_!rsi?U8j_P7-95J z-TpMeeD`E;CZujp^Iu|r>h)Jyz`M?GhLx{#T0cxN{^!pBAj5SRyKy50$qLSTURK|Fca-~JC(R-+UE literal 0 HcmV?d00001 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..19a6bde --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.0-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..adff685 --- /dev/null +++ b/gradlew @@ -0,0 +1,248 @@ +#!/bin/sh + +# +# Copyright © 2015 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + 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 + + + +# 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" ) + + 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" \ + -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 0000000..c4bdd3a --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,93 @@ +@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 + + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -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/otel/alerts.yaml b/infra/otel/alerts.yaml new file mode 100644 index 0000000..656af7a --- /dev/null +++ b/infra/otel/alerts.yaml @@ -0,0 +1,45 @@ +groups: + - name: messenger + rules: + - alert: ApiHighErrorRate + expr: >- + sum(rate(lab_http_requests_total{status_class="5xx"}[30s])) / sum(rate(lab_http_requests_total[30s])) > 0.1 + for: 6s + annotations: + summary: "HTTP 5xx exceeds 10% in 30s" + runbook: "infra/otel/runbook.md#api" + - alert: AttachmentQueueLag + expr: >- + max(lab_queue_oldest_seconds{queue="attachments"}) > 8 and max(lab_queue_depth{queue="attachments"}) > 0 + for: 9s + annotations: + summary: "Oldest unfinished attachment exceeds eight seconds" + runbook: "infra/otel/runbook.md#queue" + - alert: ApiBurnFast + expr: >- + (sum(rate(lab_http_requests_total{status_class="5xx"}[1m])) / sum(rate(lab_http_requests_total[1m])) > 0.05) and (sum(rate(lab_http_requests_total{status_class="5xx"}[15s])) / sum(rate(lab_http_requests_total[15s])) > 0.05) and (sum(increase(lab_http_requests_total[1m])) >= 6) + for: 9s + annotations: + summary: "Api burn > 5x in 1m and 15s" + runbook: "infra/otel/runbook.md#api" + - alert: ApiBurnSlow + expr: >- + (sum(rate(lab_http_requests_total{status_class="5xx"}[3m])) / sum(rate(lab_http_requests_total[3m])) > 0.02) and (sum(rate(lab_http_requests_total{status_class="5xx"}[45s])) / sum(rate(lab_http_requests_total[45s])) > 0.02) and (sum(increase(lab_http_requests_total[3m])) >= 12) + for: 18s + annotations: + summary: "Api burn > 2x in 3m and 45s" + runbook: "infra/otel/runbook.md#api" + - alert: ReadyBurnFast + expr: >- + (sum(rate(lab_ready_sli_total{outcome="bad"}[1m])) / sum(rate(lab_ready_sli_total[1m])) > 0.25) and (sum(rate(lab_ready_sli_total{outcome="bad"}[15s])) / sum(rate(lab_ready_sli_total[15s])) > 0.25) and (sum(increase(lab_ready_sli_total[1m])) >= 6) + for: 9s + annotations: + summary: "Ready burn > 5x in 1m and 15s" + runbook: "infra/otel/runbook.md#queue" + - alert: ReadyBurnSlow + expr: >- + (sum(rate(lab_ready_sli_total{outcome="bad"}[3m])) / sum(rate(lab_ready_sli_total[3m])) > 0.1) and (sum(rate(lab_ready_sli_total{outcome="bad"}[45s])) / sum(rate(lab_ready_sli_total[45s])) > 0.1) and (sum(increase(lab_ready_sli_total[3m])) >= 12) + for: 18s + annotations: + summary: "Ready burn > 2x in 3m and 45s" + runbook: "infra/otel/runbook.md#queue" diff --git a/infra/otel/collector.yaml b/infra/otel/collector.yaml new file mode 100644 index 0000000..2549267 --- /dev/null +++ b/infra/otel/collector.yaml @@ -0,0 +1,62 @@ +receivers: + otlp: + protocols: + http: + endpoint: 0.0.0.0:4318 +processors: + memory_limiter: + check_interval: 1s + limit_mib: 128 + spike_limit_mib: 32 + batch: + timeout: 1s + send_batch_size: 128 + send_batch_max_size: 256 +exporters: + prometheus: + endpoint: 0.0.0.0:8889 + metric_expiration: 20s + otlp/tempo: + endpoint: tempo:4317 + tls: + insecure: true + timeout: 2s + sending_queue: + queue_size: 32 + num_consumers: 1 + retry_on_failure: + initial_interval: 1s + max_interval: 3s + max_elapsed_time: 10s + otlphttp/loki: + endpoint: http://loki:3100/otlp + timeout: 2s + sending_queue: + queue_size: 32 + num_consumers: 1 + retry_on_failure: + initial_interval: 1s + max_interval: 3s + max_elapsed_time: 10s +service: + telemetry: + metrics: + readers: + - pull: + exporter: + prometheus: + host: 0.0.0.0 + port: 8888 + pipelines: + metrics: + receivers: [otlp] + processors: [memory_limiter, batch] + exporters: [prometheus] + logs: + receivers: [otlp] + processors: [memory_limiter, batch] + exporters: [otlphttp/loki] + traces: + receivers: [otlp] + processors: [memory_limiter, batch] + exporters: [otlp/tempo] diff --git a/infra/otel/grafana/dashboards/messenger.json b/infra/otel/grafana/dashboards/messenger.json new file mode 100644 index 0000000..34b0685 --- /dev/null +++ b/infra/otel/grafana/dashboards/messenger.json @@ -0,0 +1,501 @@ +{ + "uid": "messenger-lab6", + "title": "Messenger Lab 6", + "schemaVersion": 39, + "version": 1, + "refresh": "5s", + "time": { + "from": "now-15m", + "to": "now" + }, + "panels": [ + { + "id": 1, + "title": "API requests / second", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 0, + "y": 0, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "reqps" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "sum by (status_class) (rate(lab_http_requests_total[1m]))", + "legendFormat": "{{status_class}}" + } + ] + }, + { + "id": 2, + "title": "API 5xx share", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 0, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "percentunit" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "(sum(rate(lab_http_requests_total{status_class=\"5xx\"}[1m])) or vector(0)) / sum(rate(lab_http_requests_total[1m]))", + "legendFormat": "5xx" + } + ] + }, + { + "id": 3, + "title": "HTTP latency across replicas", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 0, + "y": 8, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "s" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "histogram_quantile(0.5, sum by (le) (rate(lab_http_duration_seconds_bucket[1m])))", + "legendFormat": "p50" + }, + { + "refId": "B", + "expr": "histogram_quantile(0.95, sum by (le) (rate(lab_http_duration_seconds_bucket[1m])))", + "legendFormat": "p95" + }, + { + "refId": "C", + "expr": "histogram_quantile(0.99, sum by (le) (rate(lab_http_duration_seconds_bucket[1m])))", + "legendFormat": "p99" + } + ] + }, + { + "id": 4, + "title": "Worker delivery outcomes / second", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 8, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "ops" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "sum by (outcome) (rate(lab_jobs_total[1m]))", + "legendFormat": "{{outcome}}" + } + ] + }, + { + "id": 5, + "title": "Worker delivery latency", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 0, + "y": 16, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "s" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "histogram_quantile(0.5, sum by (le) (rate(lab_job_duration_seconds_bucket[1m])))", + "legendFormat": "p50" + }, + { + "refId": "B", + "expr": "histogram_quantile(0.95, sum by (le) (rate(lab_job_duration_seconds_bucket[1m])))", + "legendFormat": "p95" + }, + { + "refId": "C", + "expr": "histogram_quantile(0.99, sum by (le) (rate(lab_job_duration_seconds_bucket[1m])))", + "legendFormat": "p99" + } + ] + }, + { + "id": 6, + "title": "Acceptance to READY", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 16, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "s" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "histogram_quantile(0.5, sum by (le) (rate(lab_ready_duration_seconds_bucket[3m])))", + "legendFormat": "p50" + }, + { + "refId": "B", + "expr": "histogram_quantile(0.95, sum by (le) (rate(lab_ready_duration_seconds_bucket[3m])))", + "legendFormat": "p95" + }, + { + "refId": "C", + "expr": "histogram_quantile(0.99, sum by (le) (rate(lab_ready_duration_seconds_bucket[3m])))", + "legendFormat": "p99" + } + ] + }, + { + "id": 7, + "title": "Unfinished jobs", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 0, + "y": 24, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "short" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "max(lab_queue_depth)", + "legendFormat": "depth" + } + ] + }, + { + "id": 8, + "title": "Oldest unfinished job", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 24, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "s" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "max(lab_queue_oldest_seconds)", + "legendFormat": "age" + } + ] + }, + { + "id": 9, + "title": "DB pool per API", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 0, + "y": 32, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "short" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "lab_db_pool_connections", + "legendFormat": "{{instance}} {{state}}" + } + ] + }, + { + "id": 10, + "title": "Worker saturation", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 32, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "percentunit" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "sum(lab_worker_busy) / sum(lab_worker_capacity)", + "legendFormat": "busy/capacity" + } + ] + }, + { + "id": 11, + "title": "Process CPU", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 0, + "y": 40, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "short" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "rate(lab_process_cpu_seconds_total[1m])", + "legendFormat": "{{job}} {{instance}}" + } + ] + }, + { + "id": 12, + "title": "Process RSS", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 40, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "bytes" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "lab_process_rss_bytes", + "legendFormat": "{{job}} {{instance}}" + } + ] + }, + { + "id": 13, + "title": "Burn rates", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 0, + "y": 48, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "short" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "sum(rate(lab_http_requests_total{status_class=\"5xx\"}[1m])) / sum(rate(lab_http_requests_total[1m])) / 0.01", + "legendFormat": "API" + }, + { + "refId": "B", + "expr": "sum(rate(lab_ready_sli_total{outcome=\"bad\"}[1m])) / sum(rate(lab_ready_sli_total[1m])) / 0.05", + "legendFormat": "READY" + } + ] + }, + { + "id": 14, + "title": "Active alerts", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 48, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "short" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "ALERTS{alertstate=~\"pending|firing\"}", + "legendFormat": "{{alertname}} {{alertstate}}" + } + ] + }, + { + "id": 15, + "title": "Correlated API and worker logs", + "type": "logs", + "datasource": { + "type": "loki", + "uid": "loki" + }, + "gridPos": { + "x": 0, + "y": 56, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "short" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "expr": "{service_name=~\"messenger-(api|worker|dispatcher)\"}", + "legendFormat": "" + } + ] + }, + { + "id": 16, + "title": "SLO attainment over 30 minutes", + "type": "timeseries", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "x": 12, + "y": 56, + "w": 12, + "h": 8 + }, + "fieldConfig": { + "defaults": { + "unit": "percentunit" + }, + "overrides": [] + }, + "targets": [ + { + "refId": "A", + "legendFormat": "API success (target 99%)", + "expr": "1 - (sum(increase(lab_http_requests_total{status_class=\"5xx\"}[30m])) or vector(0)) / sum(increase(lab_http_requests_total[30m]))" + }, + { + "refId": "B", + "legendFormat": "READY within 5s (target 95%)", + "expr": "sum(increase(lab_ready_sli_total{outcome=\"good\"}[30m])) / sum(increase(lab_ready_sli_total[30m]))" + } + ] + } + ] +} diff --git a/infra/otel/grafana/provisioning/dashboards/provider.yaml b/infra/otel/grafana/provisioning/dashboards/provider.yaml new file mode 100644 index 0000000..3e9a5b6 --- /dev/null +++ b/infra/otel/grafana/provisioning/dashboards/provider.yaml @@ -0,0 +1,6 @@ +apiVersion: 1 +providers: + - name: messenger + type: file + options: + path: /var/lib/grafana/dashboards diff --git a/infra/otel/grafana/provisioning/datasources/datasources.yaml b/infra/otel/grafana/provisioning/datasources/datasources.yaml new file mode 100644 index 0000000..64d5744 --- /dev/null +++ b/infra/otel/grafana/provisioning/datasources/datasources.yaml @@ -0,0 +1,22 @@ +apiVersion: 1 +datasources: + - name: Prometheus + type: prometheus + uid: prometheus + url: http://prometheus:9090 + isDefault: true + - name: Loki + type: loki + uid: loki + url: http://loki:3100 + jsonData: + derivedFields: + - name: TraceID + matcherType: label + matcherRegex: trace_id + datasourceUid: tempo + url: '$${__value.raw}' + - name: Tempo + type: tempo + uid: tempo + url: http://tempo:3200 diff --git a/infra/otel/loki.yaml b/infra/otel/loki.yaml new file mode 100644 index 0000000..f4915db --- /dev/null +++ b/infra/otel/loki.yaml @@ -0,0 +1,29 @@ +auth_enabled: false +server: + http_listen_port: 3100 +common: + instance_addr: 127.0.0.1 + path_prefix: /loki + storage: + filesystem: + chunks_directory: /loki/chunks + rules_directory: /loki/rules + replication_factor: 1 + ring: + kvstore: + store: inmemory +schema_config: + configs: + - from: 2024-01-01 + store: tsdb + object_store: filesystem + schema: v13 + index: + prefix: index_ + period: 24h +limits_config: + allow_structured_metadata: true + retention_period: 24h +compactor: + retention_enabled: true + delete_request_store: filesystem diff --git a/infra/otel/prometheus.yaml b/infra/otel/prometheus.yaml new file mode 100644 index 0000000..29cbdf1 --- /dev/null +++ b/infra/otel/prometheus.yaml @@ -0,0 +1,13 @@ +global: + scrape_interval: 3s + evaluation_interval: 3s +rule_files: + - /etc/prometheus/alerts.yaml +scrape_configs: + - job_name: otlp + honor_labels: true + static_configs: + - targets: [otel-collector:8889] + - job_name: collector + static_configs: + - targets: [otel-collector:8888] diff --git a/infra/otel/runbook.md b/infra/otel/runbook.md new file mode 100644 index 0000000..11536f6 --- /dev/null +++ b/infra/otel/runbook.md @@ -0,0 +1,22 @@ +# Диагностика учебного стенда + +Откройте dashboard Messenger Lab 6 по адресу http://127.0.0.1:13006/d/messenger-lab6. Для определения времени инцидента используйте состояние правил Prometheus и отметки проб в evidence. Для локального запуска тестов используйте команды из REPORT.md. + +## API + +1. По панели API 5xx share найдите интервал деградации. Сопоставьте его с API latency и DB pool used/waiting. Проверьте наличие трафика перед интерпретацией доли ошибок. +2. В Explore выберите Loki. Запрос `{service_name="messenger-api"} | status_class="5xx"` покажет события завершения запросов. Найдите тот же `request_id`, что в HTTP-заголовке. Временно сузьте диапазон времени, чтобы исключить ошибки предыдущего опыта. +3. Скопируйте `trace_id` в Tempo или откройте ссылку TraceID. Длинный дочерний span `postgres.session.lookup` с ошибкой показывает, что запрос ждал PostgreSQL в ходе проверки сессии. Если задержка приходится на другое дочернее действие, смотрите его имя и статус. +4. Снимите операторскую блокировку и проверьте, что 200 восстановились, доля 5xx снизилась и правила перешли в inactive. При необходимости проверьте состояние DB-контейнера и ожидания в `pg_stat_activity`. + +## Queue + +1. При росте `Oldest unfinished job` сравните backlog с активностью worker. Большой backlog при исчезновении рядов capacity означает отсутствие свежей телеметрии воркеров. Подтвердите состояние контейнеров через `docker compose ps`. Активные воркеры при высоком busy/capacity указывают на насыщение. +2. По X-Request-Id принятого upload найдите `http.completed` в Loki. В Tempo проверьте producer `attachments publish` и consumer `attachments consume`. По `job.attempt` видно повторную обработку. Ошибки S3 и БД различаются по дочерним spans. +3. Восстановите работу воркеров. Проверьте исчезновение backlog, переход AttachmentQueueLag в inactive и завершение ранее принятых вложений. Ready burn может сработать после обработки накопившихся медленных вложений и завершится после выхода этих наблюдений из короткого окна. + +## Telemetry + +1. При отсутствии логов проверьте состояние Loki и запросы через datasource. Сопоставьте с Prometheus `up` для Collector и с `otelcol_exporter_send_failed_log_records_total`. +2. По `otelcol_exporter_queue_size` и `otelcol_exporter_queue_capacity` проверьте предел очереди. Рост ошибок экспорта после истечения retry window означает потерю наблюдений. +3. Проверьте HTTP-запись и чтение сообщений, затем восстановите Loki и найдите свежий `http.completed` в Grafana. SDK и Collector держат ограниченные очереди в памяти. diff --git a/infra/otel/tempo.yaml b/infra/otel/tempo.yaml new file mode 100644 index 0000000..71b5444 --- /dev/null +++ b/infra/otel/tempo.yaml @@ -0,0 +1,20 @@ +server: + http_listen_port: 3200 +distributor: + receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:4317 +ingester: + max_block_duration: 5m +compactor: + compaction: + block_retention: 24h +storage: + trace: + backend: local + local: + path: /var/tempo/blocks + wal: + path: /var/tempo/wal diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..169c66f --- /dev/null +++ b/nginx.conf @@ -0,0 +1,48 @@ +worker_processes auto; + +events { + worker_connections 1024; +} + +http { + access_log /tmp/access.log; + error_log /tmp/error.log warn; + + resolver 127.0.0.11 valid=10s ipv6=off; + resolver_timeout 5s; + + upstream api_backend { + zone api_backend 64k; + server api:8080 resolve max_fails=1 fail_timeout=5s; + keepalive 32; + } + + server { + listen 80; + server_name _; + + client_max_body_size 0; + client_body_timeout 15s; + + location / { + proxy_pass http://api_backend; + proxy_http_version 1.1; + proxy_request_buffering off; + proxy_buffer_size 8k; + proxy_set_header Connection ""; + proxy_set_header Host $http_host; + proxy_set_header X-Forwarded-Host $http_host; + proxy_set_header X-Forwarded-Port $server_port; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + proxy_next_upstream error timeout http_502 http_504; + proxy_next_upstream_tries 3; + + proxy_connect_timeout 1s; + proxy_send_timeout 10s; + proxy_read_timeout 10s; + } + } +} \ No newline at end of file diff --git a/policy.json b/policy.json new file mode 100644 index 0000000..e95a0bd --- /dev/null +++ b/policy.json @@ -0,0 +1,21 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:PutObject", + "s3:DeleteObject", + "s3:ListBucket", + "s3:GetBucketLocation", + "s3:AbortMultipartUpload", + "s3:ListMultipartUploadParts" + ], + "Resource": [ + "arn:aws:s3:::messenger-attachments", + "arn:aws:s3:::messenger-attachments/*" + ] + } + ] +} \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..28c2070 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[tool.black] +line-length = 100 +target-version = ['py39'] diff --git a/requirements-tools.txt b/requirements-tools.txt new file mode 100644 index 0000000..4b53a77 --- /dev/null +++ b/requirements-tools.txt @@ -0,0 +1,4 @@ +-r requirements-dev.txt +black==25.9.0 +Pillow==12.0.0 +jsonschema==4.26.0 diff --git a/scripts/bootstrap.ps1 b/scripts/bootstrap.ps1 new file mode 100644 index 0000000..b3eb3d8 --- /dev/null +++ b/scripts/bootstrap.ps1 @@ -0,0 +1,12 @@ +$ErrorActionPreference = 'Stop' +New-Item -ItemType Directory -Force secrets | Out-Null +$values = @{postgres_db='messenger'; postgres_user='messenger'; postgres_password=''; minio_root_user='labadmin'; minio_root_password=''; s3_access_key_id='labapi'; s3_secret_access_key=''; grafana_admin_password=''} +foreach ($name in $values.Keys) { + $path = Join-Path secrets ($name + '.txt') + if (!(Test-Path -LiteralPath $path)) { + $value = $values[$name] + if (!$value) { $value = [Convert]::ToBase64String([System.Security.Cryptography.RandomNumberGenerator]::GetBytes(32)) } + [System.IO.File]::WriteAllText((Join-Path (Get-Location) $path), $value) + } +} +if (!(Test-Path .env)) { Copy-Item .env.example .env } diff --git a/scripts/capture_evidence.py b/scripts/capture_evidence.py new file mode 100644 index 0000000..2f0cd79 --- /dev/null +++ b/scripts/capture_evidence.py @@ -0,0 +1,117 @@ +import hashlib +import json +import subprocess +from datetime import datetime +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] +COMPOSE = ["docker", "compose", "-f", "docker-compose.yml", "-f", "compose.local.yaml"] + + +def cmd(*args): + return subprocess.check_output(args, cwd=ROOT, text=True, encoding="utf-8").strip() + + +def save(name, value): + (ROOT / "evidence" / name).write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8") + + +def containers(): + ids = cmd(*COMPOSE, "ps", "-q").splitlines() + data = json.loads(cmd("docker", "inspect", *ids)) + return [ + { + "service": row["Config"]["Labels"]["com.docker.compose.service"], + "image": row["Config"]["Image"], + "image_id": row["Image"], + "started_at": row["State"]["StartedAt"], + "health": row["State"].get("Health", {}).get("Status", row["State"]["Status"]), + "memory_limit_bytes": row["HostConfig"]["Memory"], + "nano_cpus": row["HostConfig"]["NanoCpus"], + "volumes": [item["Name"] for item in row["Mounts"] if item["Type"] == "volume"], + } + for row in data + ] + + +def verify_sources(): + protected = ["README.md", "COURSE.md", "contracts", "tests", "lab.json"] + subprocess.run( + ["git", "diff", "--exit-code", "470d9d6", "--", *protected], cwd=ROOT, check=True + ) + names = cmd( + "git", "-c", "core.quotePath=false", "ls-files", "-c", "-o", "--exclude-standard" + ).splitlines() + root_secrets = ROOT / "secrets" + credentials = [ + path.read_bytes().strip() + for path in root_secrets.glob("*.txt") + if "password" in path.stem or "secret_access_key" in path.stem + ] + assert all(len(value) > 12 for value in credentials), "expected generated secrets" + source = {} + for name in names: + if not (ROOT / name).is_file(): + continue + content = (ROOT / name).read_bytes() + assert not any( + secret in content for secret in credentials + ), "generated credential in Git-visible file" + if name.startswith( + ("src/", "worker/", "infra/otel/", "student-tests/", "scripts/") + ) or name in ( + "docker-compose.yml", + "Dockerfile", + "compose.test.yaml", + "compose.local.yaml", + "build.gradle", + ): + source[name] = hashlib.sha256(content).hexdigest() + save("source-sha256.json", source) + print("PASS public starter/contract intact, generated secrets absent from Git-visible files") + + +def main(): + service_state = containers() + assert ( + len( + [row for row in service_state if row["service"] == "api" and row["health"] == "healthy"] + ) + == 2 + ) + assert len([row for row in service_state if row["service"] == "worker"]) == 2 + save( + "environment.json", + { + "timestamp": datetime.now().astimezone().isoformat(), + "skeleton": "470d9d6", + "transfer_source": "ef7e8c3", + "engine": cmd( + "docker", + "info", + "--format", + "version={{.ServerVersion}} cpus={{.NCPU}} memory_bytes={{.MemTotal}}", + ), + "services": service_state, + }, + ) + save( + "resources.json", + [ + json.loads(line) + for line in cmd( + "docker", + "stats", + "--no-stream", + "--format", + "{{json .}}", + *cmd(*COMPOSE, "ps", "-q").splitlines(), + ).splitlines() + ], + ) + verify_sources() + + +if __name__ == "__main__": + main() diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 0000000..e71fe90 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1 @@ +@powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0restart.ps1" diff --git a/scripts/restart.ps1 b/scripts/restart.ps1 new file mode 100644 index 0000000..a4a1069 --- /dev/null +++ b/scripts/restart.ps1 @@ -0,0 +1,9 @@ +$ErrorActionPreference = 'Stop' +$ids = docker compose ps -q +$before = docker inspect --format '{{.Name}} {{.State.StartedAt}}' $ids +$before | Set-Content evidence/restart-before.txt +docker compose restart db redis minio broker api worker dispatcher lb otel-collector prometheus loki tempo grafana +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } +$ids = docker compose ps -q +$after = docker inspect --format '{{.Name}} {{.State.StartedAt}}' $ids +$after | Set-Content evidence/restart-after.txt diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 0000000..279523a --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +docker compose restart db redis minio broker api worker dispatcher lb otel-collector prometheus loki tempo grafana diff --git a/scripts/run_tests.ps1 b/scripts/run_tests.ps1 new file mode 100644 index 0000000..19b048f --- /dev/null +++ b/scripts/run_tests.ps1 @@ -0,0 +1,47 @@ +$ErrorActionPreference = 'Stop' +Set-Location (Split-Path $PSScriptRoot -Parent) +$env:BASE_URL = 'http://127.0.0.1:18085' +$env:PYTHONPATH = (Resolve-Path tests).Path +$base = @('compose', '-f', 'docker-compose.yml', '-f', 'compose.local.yaml') +$proof = $base + @('-f', 'compose.test.yaml') + +function Invoke-Checked { + param([string]$Name, [string]$Command, [string[]]$Arguments) + & $Command @Arguments *> "evidence/$Name.txt" + if ($LASTEXITCODE -ne 0) { + Get-Content "evidence/$Name.txt" -Tail 25 + throw "$Name failed ($LASTEXITCODE)" + } + Write-Host "PASS $Name" +} + +function Wait-Api { + py -c "import sys; sys.path.insert(0,'student-tests'); from pipeline_support import ready_api; ready_api()" + if ($LASTEXITCODE -ne 0) { throw 'API readiness not confirmed' } +} + +try { + Invoke-Checked 'contract-check' 'py' @('scripts/check_contract.py') + Invoke-Checked 'openapi-validation' 'py' @('scripts/validate_openapi.py') + Invoke-Checked 'style' 'py' @('scripts/style.py') + Invoke-Checked 'java-build' '.\gradlew.bat' @('test', '--no-daemon') + Invoke-Checked 'stack-test-profile' 'docker' ($proof + @('up', '-d', '--build')) + Wait-Api + Invoke-Checked 'smoke' 'py' @('tests/smoke.py') + Invoke-Checked 'observe' 'py' @('student-tests/observe.py') + foreach ($name in @('core','faults','retention','boundaries','ack_guard','filename','upload_crash','history_capacity')) { + Invoke-Checked "pipeline-$name" 'py' @("student-tests/pipeline_$name.py") + } + Invoke-Checked 'stack-ordinary' 'docker' ($base + @('up', '-d')) + Wait-Api + Invoke-Checked 'incidents' 'py' @('student-tests/incidents.py') + Invoke-Checked 'load' 'py' @('tests/load.py', '--duration', '30', '--concurrency', '4', '--output', 'evidence/load.json') + Invoke-Checked 'persistence' 'py' @('tests/resilience.py', '--mode', 'persistence', '--action-script', 'scripts/restart.cmd') + Wait-Api + Invoke-Checked 'failover' 'py' @('tests/resilience.py', '--mode', 'failover', '--action-script', 'scripts/stop-one.cmd') + Invoke-Checked 'stack-final' 'docker' ($base + @('up', '-d', 'api')) + Wait-Api + Invoke-Checked 'environment' 'py' @('scripts/capture_evidence.py') +} finally { + docker @base up -d +} diff --git a/scripts/stop-one.cmd b/scripts/stop-one.cmd new file mode 100644 index 0000000..dcdcb84 --- /dev/null +++ b/scripts/stop-one.cmd @@ -0,0 +1 @@ +@powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0stop-one.ps1" diff --git a/scripts/stop-one.ps1 b/scripts/stop-one.ps1 new file mode 100644 index 0000000..27ce510 --- /dev/null +++ b/scripts/stop-one.ps1 @@ -0,0 +1,13 @@ +$ErrorActionPreference = 'Stop' +$replicas = docker compose ps -q api +foreach ($replica in $replicas) { + $alias = docker inspect --format '{{.Config.Hostname}}' $replica + if ($alias -eq $env:TARGET_INSTANCE_ID) { + docker inspect --format "{{.Name}} {{.Config.Hostname}} {{.State.Status}}" $replica | Set-Content evidence/stop-one-before.txt + docker stop --time 2 $replica + $result = $LASTEXITCODE + docker inspect --format "{{.Name}} {{.Config.Hostname}} {{.State.Status}}" $replica | Set-Content evidence/stop-one-after.txt + exit $result + } +} +exit 1 diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 0000000..60c335f --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -eu +for id in $(docker compose ps -q api); do + if [ "$(docker inspect --format '{{.Config.Hostname}}' "$id")" = "$TARGET_INSTANCE_ID" ]; then + docker stop -t 2 "$id" + exit 0 + fi +done +exit 1 diff --git a/scripts/style.py b/scripts/style.py new file mode 100644 index 0000000..08fdc65 --- /dev/null +++ b/scripts/style.py @@ -0,0 +1,87 @@ +import argparse +import hashlib +import os +import re +from pathlib import Path +import subprocess +import sys +import tempfile +import urllib.request + + +ROOT = Path(__file__).resolve().parents[1] +VERSION = "1.28.0" +SHA256 = "32342e7c1b4600f80df3471da46aee8012d3e1445d5ea1be1fb71289b07cc735" + + +def formatter(): + jar = Path(tempfile.gettempdir()) / f"google-java-format-{VERSION}-all-deps.jar" + if not jar.exists(): + url = ( + "https://repo.maven.apache.org/maven2/com/google/googlejavaformat/" + f"google-java-format/{VERSION}/{jar.name}" + ) + with urllib.request.urlopen(url, timeout=60) as response: + jar.write_bytes(response.read()) + if hashlib.sha256(jar.read_bytes()).hexdigest() != SHA256: + raise RuntimeError("Java formatter checksum mismatch") + return jar + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--apply", action="store_true") + args = parser.parse_args() + java_home = os.environ.get("JAVA_HOME") + java = str(Path(java_home) / "bin" / "java") if java_home else "java" + files = sorted(str(p.relative_to(ROOT)) for p in (ROOT / "src").rglob("*.java")) + mode = ["--replace"] if args.apply else ["--dry-run", "--set-exit-if-changed"] + subprocess.run([java, "-jar", str(formatter()), "--aosp", *mode, *files], cwd=ROOT, check=True) + python = [ + "worker/pipeline", + "scripts/style.py", + "scripts/capture_evidence.py", + ] + python += sorted(str(p.relative_to(ROOT)) for p in (ROOT / "student-tests").glob("*.py")) + subprocess.run( + [sys.executable, "-m", "black", *([] if args.apply else ["--check"]), *python], + cwd=ROOT, + check=True, + ) + check_controllers() + print("PASS Java AOSP / Python Black style") + + +def check_controllers(): + directory = ROOT / "src/main/java/ru/mtusi/messenger/controller" + operations = 0 + for path in directory.glob("*Controller.java"): + source = path.read_text(encoding="utf-8") + fields = re.findall(r"private final (\w+Service) (\w+);", source) + if "@RequiredArgsConstructor" not in source or len(fields) != 1: + raise AssertionError(f"Controller DI contract: {path.name}") + bodies = re.findall(r"@Override\s+public [^{}]+\{([^{}]*)\}", source) + if not bodies: + raise AssertionError(f"Controller has no operations: {path.name}") + service = fields[0][1] + for body in bodies: + if body.count(";") != 1 or len(re.findall(rf"\b{service}\.\w+\(", body)) != 1: + raise AssertionError(f"Controller must call one service: {path.name}") + if not re.fullmatch(rf"\s*(?:return )?{service}\.\w+\([^;]*\);\s*", body): + raise AssertionError(f"Controller contains extra logic: {path.name}") + operations += len(bodies) + for path in directory.glob("*Api.java"): + source = path.read_text(encoding="utf-8") + if source.count("/**") != source.count("@Operation(") + 1: + raise AssertionError(f"Missing API documentation: {path.name}") + for path in (ROOT / "src/main/java").rglob("*.java"): + source = path.read_text(encoding="utf-8") + if "@Autowired" in source: + raise AssertionError(f"Use constructor injection: {path.name}") + if re.search(r"^import .*\*;", source, re.MULTILINE): + raise AssertionError(f"Use explicit imports: {path.name}") + print(f"PASS {operations} controller operations: one service call, constructor DI, API Javadoc") + + +if __name__ == "__main__": + main() diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..b1a3182 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'messenger-lab6' diff --git a/src/main/java/ru/mtusi/messenger/MessengerLab1Application.java b/src/main/java/ru/mtusi/messenger/MessengerLab1Application.java new file mode 100644 index 0000000..664f1ba --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/MessengerLab1Application.java @@ -0,0 +1,17 @@ +package ru.mtusi.messenger; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.boot.security.autoconfigure.UserDetailsServiceAutoConfiguration; +import org.springframework.scheduling.annotation.EnableScheduling; + +@ConfigurationPropertiesScan +@SpringBootApplication(exclude = UserDetailsServiceAutoConfiguration.class) +@EnableScheduling +public class MessengerLab1Application { + + public static void main(String[] args) { + SpringApplication.run(MessengerLab1Application.class, args); + } +} diff --git a/src/main/java/ru/mtusi/messenger/cache/CacheHealthTracker.java b/src/main/java/ru/mtusi/messenger/cache/CacheHealthTracker.java new file mode 100644 index 0000000..16031c9 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/cache/CacheHealthTracker.java @@ -0,0 +1,19 @@ +package ru.mtusi.messenger.cache; + +import org.springframework.stereotype.Component; + +import java.util.concurrent.atomic.AtomicBoolean; + +@Component +public class CacheHealthTracker { + + private final AtomicBoolean stale = new AtomicBoolean(false); + + public void markStale() { + stale.set(true); + } + + public boolean consumeStale() { + return stale.getAndSet(false); + } +} diff --git a/src/main/java/ru/mtusi/messenger/config/JacksonConfig.java b/src/main/java/ru/mtusi/messenger/config/JacksonConfig.java new file mode 100644 index 0000000..29e5014 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/JacksonConfig.java @@ -0,0 +1,32 @@ +package ru.mtusi.messenger.config; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import tools.jackson.databind.DeserializationFeature; +import tools.jackson.databind.cfg.CoercionAction; +import tools.jackson.databind.cfg.CoercionInputShape; + +@Configuration +@Slf4j +public class JacksonConfig { + + @Bean + public JsonMapperBuilderCustomizer strictJacksonCustomizer() { + log.warn(">>> Strict JsonMapper customizer applied"); + return builder -> + builder.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .withCoercionConfigDefaults( + config -> { + config.setCoercion( + CoercionInputShape.Boolean, CoercionAction.Fail); + config.setCoercion( + CoercionInputShape.Integer, CoercionAction.Fail); + config.setCoercion( + CoercionInputShape.Float, CoercionAction.Fail); + }); + } +} diff --git a/src/main/java/ru/mtusi/messenger/config/MinioConfig.java b/src/main/java/ru/mtusi/messenger/config/MinioConfig.java new file mode 100644 index 0000000..be56c5f --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/MinioConfig.java @@ -0,0 +1,32 @@ +package ru.mtusi.messenger.config; + +import io.minio.MinioClient; + +import lombok.RequiredArgsConstructor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import ru.mtusi.messenger.config.properties.S3Properties; + +@Configuration +@RequiredArgsConstructor +public class MinioConfig { + private final S3Properties props; + + @Bean + public MinioClient minioClient() { + return MinioClient.builder() + .endpoint(props.endpoint()) + .httpClient( + new okhttp3.OkHttpClient.Builder() + .connectTimeout(java.time.Duration.ofSeconds(2)) + .readTimeout(java.time.Duration.ofSeconds(10)) + .writeTimeout(java.time.Duration.ofSeconds(10)) + .callTimeout(java.time.Duration.ofSeconds(20)) + .build()) + .region(props.region()) + .credentials(props.resolvedAccessKey(), props.resolvedSecretKey()) + .build(); + } +} diff --git a/src/main/java/ru/mtusi/messenger/config/OpenApiConfig.java b/src/main/java/ru/mtusi/messenger/config/OpenApiConfig.java new file mode 100644 index 0000000..ef82a28 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/OpenApiConfig.java @@ -0,0 +1,15 @@ +package ru.mtusi.messenger.config; + +import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; +import io.swagger.v3.oas.annotations.security.SecurityScheme; + +import org.springframework.context.annotation.Configuration; + +@Configuration +@SecurityScheme(name = "basicAuth", type = SecuritySchemeType.HTTP, scheme = "basic") +@SecurityScheme( + name = "bearerAuth", + type = SecuritySchemeType.HTTP, + scheme = "bearer", + bearerFormat = "opaque session token") +public class OpenApiConfig {} diff --git a/src/main/java/ru/mtusi/messenger/config/RateLimitConfig.java b/src/main/java/ru/mtusi/messenger/config/RateLimitConfig.java new file mode 100644 index 0000000..02db302 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/RateLimitConfig.java @@ -0,0 +1,29 @@ +package ru.mtusi.messenger.config; + +import io.github.bucket4j.Bandwidth; +import io.github.bucket4j.Bucket; + +import lombok.RequiredArgsConstructor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import ru.mtusi.messenger.config.properties.RateLimitProperties; + +import java.time.Duration; + +@Configuration +@RequiredArgsConstructor +public class RateLimitConfig { + private final RateLimitProperties props; + + @Bean + public Bucket rateLimitBucket() { + Bandwidth limit = + Bandwidth.builder() + .capacity(props.ratePerMinute()) + .refillGreedy(props.ratePerMinute(), Duration.ofMinutes(1)) + .build(); + return Bucket.builder().addLimit(limit).build(); + } +} diff --git a/src/main/java/ru/mtusi/messenger/config/RedisConfig.java b/src/main/java/ru/mtusi/messenger/config/RedisConfig.java new file mode 100644 index 0000000..77636b1 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/RedisConfig.java @@ -0,0 +1,117 @@ +package ru.mtusi.messenger.config; + +import io.lettuce.core.ClientOptions; +import io.lettuce.core.SocketOptions; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cache.Cache; +import org.springframework.cache.annotation.CachingConfigurer; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.interceptor.CacheErrorHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; + +import ru.mtusi.messenger.cache.CacheHealthTracker; +import ru.mtusi.messenger.config.properties.RedisProperties; +import ru.mtusi.messenger.config.properties.SessionProperties; +import ru.mtusi.messenger.service.SessionService; + +@Slf4j +@Configuration +@EnableCaching +@EnableConfigurationProperties(RedisProperties.class) +@RequiredArgsConstructor +public class RedisConfig implements CachingConfigurer { + private final SessionProperties sessionProperties; + private final RedisProperties redisProperties; + private final CacheHealthTracker cacheHealthTracker; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + RedisStandaloneConfiguration server = + new RedisStandaloneConfiguration(redisProperties.host(), redisProperties.port()); + + SocketOptions socketOptions = + SocketOptions.builder() + .connectTimeout(redisProperties.connectTimeout()) + .keepAlive( + SocketOptions.KeepAliveOptions.builder() + .enable() + .idle(redisProperties.keepAliveIdle()) + .interval(redisProperties.keepAliveInterval()) + .count(3) + .build()) + .tcpUserTimeout( + SocketOptions.TcpUserTimeoutOptions.builder() + .tcpUserTimeout(redisProperties.tcpUserTimeout()) + .enable() + .build()) + .build(); + + ClientOptions clientOptions = + ClientOptions.builder().socketOptions(socketOptions).autoReconnect(true).build(); + + LettuceClientConfiguration client = + LettuceClientConfiguration.builder() + .commandTimeout(redisProperties.commandTimeout()) + .shutdownTimeout(redisProperties.shutdownTimeout()) + .clientOptions(clientOptions) + .build(); + + return new LettuceConnectionFactory(server, client); + } + + @Bean + public RedisCacheManager cacheManager(RedisConnectionFactory cf) { + RedisCacheConfiguration cfg = + RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(sessionProperties.ttl()) + .disableCachingNullValues() + .serializeValuesWith( + RedisSerializationContext.SerializationPair.fromSerializer( + new JacksonJsonRedisSerializer<>( + SessionService.SessionRecord.class))); + return RedisCacheManager.builder(cf).cacheDefaults(cfg).build(); + } + + @Override + public CacheErrorHandler errorHandler() { + return new CacheErrorHandler() { + @Override + public void handleCacheGetError(RuntimeException e, Cache cache, Object key) { + cacheHealthTracker.markStale(); + log.warn("Cache GET failed ({}): {}", cache.getName(), e.getMessage()); + } + + @Override + public void handleCachePutError( + RuntimeException e, Cache cache, Object key, Object value) { + cacheHealthTracker.markStale(); + log.warn("Cache PUT failed ({}): {}", cache.getName(), e.getMessage()); + } + + @Override + public void handleCacheEvictError(RuntimeException e, Cache cache, Object key) { + cacheHealthTracker.markStale(); + log.warn("Cache EVICT failed ({}): {}", cache.getName(), e.getMessage()); + } + + @Override + public void handleCacheClearError(RuntimeException e, Cache cache) { + cacheHealthTracker.markStale(); + log.warn("Cache CLEAR failed ({}): {}", cache.getName(), e.getMessage()); + } + }; + } +} diff --git a/src/main/java/ru/mtusi/messenger/config/SecurityConfig.java b/src/main/java/ru/mtusi/messenger/config/SecurityConfig.java new file mode 100644 index 0000000..13b05b8 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/SecurityConfig.java @@ -0,0 +1,86 @@ +package ru.mtusi.messenger.config; + +import lombok.RequiredArgsConstructor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.ProviderManager; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.config.http.SessionCreationPolicy; +import org.springframework.security.crypto.argon2.Argon2PasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver; +import org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +import ru.mtusi.messenger.exception.ApiErrorFactory; +import ru.mtusi.messenger.security.ApiAuthenticationEntryPoint; +import ru.mtusi.messenger.security.SessionAuthenticationFilter; +import ru.mtusi.messenger.security.SessionAuthenticationProvider; + +@Configuration +@EnableWebSecurity +@RequiredArgsConstructor +public class SecurityConfig { + private final SessionAuthenticationProvider sessionAuthenticationProvider; + private final ApiAuthenticationEntryPoint entryPoint; + private final ApiErrorFactory errors; + + @Bean + public PasswordEncoder passwordEncoder() { + return Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8(); + } + + @Bean + public AuthenticationManager authenticationManager() { + return new ProviderManager(sessionAuthenticationProvider); + } + + @Bean + public SecurityFilterChain filterChain( + HttpSecurity http, + AuthenticationManager authenticationManager, + BearerTokenResolver bearerTokenResolver) + throws Exception { + SessionAuthenticationFilter sessionFilter = + new SessionAuthenticationFilter(bearerTokenResolver, authenticationManager, errors); + + http.csrf(AbstractHttpConfigurer::disable) + .httpBasic(AbstractHttpConfigurer::disable) + .formLogin(AbstractHttpConfigurer::disable) + .logout(AbstractHttpConfigurer::disable) + .sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) + .authorizeHttpRequests( + auth -> + auth.requestMatchers("/health/**") + .permitAll() + .requestMatchers( + "/swagger-ui.html", + "/swagger-ui/**", + "/v3/api-docs", + "/v3/api-docs/**") + .permitAll() + .requestMatchers(HttpMethod.POST, "/api/v1/users") + .permitAll() + .requestMatchers(HttpMethod.POST, "/api/v1/auth/sessions") + .permitAll() + .requestMatchers("/actuator/health", "/actuator/health/**") + .permitAll() + .anyRequest() + .authenticated()) + .addFilterBefore(sessionFilter, UsernamePasswordAuthenticationFilter.class) + .exceptionHandling(eh -> eh.authenticationEntryPoint(entryPoint)); + + return http.build(); + } + + @Bean + public BearerTokenResolver bearerTokenResolver() { + return new DefaultBearerTokenResolver(); + } +} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/AppProperties.java b/src/main/java/ru/mtusi/messenger/config/properties/AppProperties.java new file mode 100644 index 0000000..532fa1b --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/AppProperties.java @@ -0,0 +1,6 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "app.limits") +public record AppProperties(int maxMessagesPerChat) {} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/IdempotencyProperties.java b/src/main/java/ru/mtusi/messenger/config/properties/IdempotencyProperties.java new file mode 100644 index 0000000..3d7f9ee --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/IdempotencyProperties.java @@ -0,0 +1,6 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "app.idempotency") +public record IdempotencyProperties(int keyMaxLength, String keyPattern, int retentionHours) {} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/InstanceProperties.java b/src/main/java/ru/mtusi/messenger/config/properties/InstanceProperties.java new file mode 100644 index 0000000..1945da4 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/InstanceProperties.java @@ -0,0 +1,6 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "app.instance") +public record InstanceProperties(String id) {} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/RateLimitProperties.java b/src/main/java/ru/mtusi/messenger/config/properties/RateLimitProperties.java new file mode 100644 index 0000000..303aedc --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/RateLimitProperties.java @@ -0,0 +1,6 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "app.rate") +public record RateLimitProperties(int ratePerMinute, int retryAfterSeconds) {} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/RedisProperties.java b/src/main/java/ru/mtusi/messenger/config/properties/RedisProperties.java new file mode 100644 index 0000000..0595acc --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/RedisProperties.java @@ -0,0 +1,16 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.time.Duration; + +@ConfigurationProperties(prefix = "app.redis") +public record RedisProperties( + String host, + int port, + Duration commandTimeout, + Duration connectTimeout, + Duration shutdownTimeout, + Duration keepAliveIdle, + Duration keepAliveInterval, + Duration tcpUserTimeout) {} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/S3Properties.java b/src/main/java/ru/mtusi/messenger/config/properties/S3Properties.java new file mode 100644 index 0000000..5b41186 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/S3Properties.java @@ -0,0 +1,40 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.bind.DefaultValue; + +@ConfigurationProperties(prefix = "s3") +public record S3Properties( + String endpoint, + String bucket, + @DefaultValue("us-east-1") String region, + @DefaultValue("") String accessKey, + @DefaultValue("") String secretKey, + @DefaultValue("true") boolean pathStyleAccess, + @DefaultValue("300") int presignExpirySeconds) { + public String resolvedAccessKey() { + var file = System.getenv("S3_ACCESS_KEY_ID_FILE"); + if (file != null && !file.isBlank()) { + try { + return new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(file))) + .strip(); + } catch (Exception e) { + throw new RuntimeException("Failed to read S3_ACCESS_KEY_ID_FILE: " + file, e); + } + } + return accessKey; + } + + public String resolvedSecretKey() { + var file = System.getenv("S3_SECRET_ACCESS_KEY_FILE"); + if (file != null && !file.isBlank()) { + try { + return new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(file))) + .strip(); + } catch (Exception e) { + throw new RuntimeException("Failed to read S3_SECRET_ACCESS_KEY_FILE: " + file, e); + } + } + return secretKey; + } +} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/SessionProperties.java b/src/main/java/ru/mtusi/messenger/config/properties/SessionProperties.java new file mode 100644 index 0000000..e4c904d --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/SessionProperties.java @@ -0,0 +1,8 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.time.Duration; + +@ConfigurationProperties(prefix = "app.session") +public record SessionProperties(Duration ttl) {} diff --git a/src/main/java/ru/mtusi/messenger/config/properties/UploadProperties.java b/src/main/java/ru/mtusi/messenger/config/properties/UploadProperties.java new file mode 100644 index 0000000..8e99c1e --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/config/properties/UploadProperties.java @@ -0,0 +1,12 @@ +package ru.mtusi.messenger.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.bind.DefaultValue; + +import java.util.List; + +@ConfigurationProperties(prefix = "upload") +public record UploadProperties( + @DefaultValue("10485760") long maxBytes, + @DefaultValue("40000000") long maxPixels, + @DefaultValue({"image/png", "image/jpeg"}) List allowedContentTypes) {} diff --git a/src/main/java/ru/mtusi/messenger/controller/AttachmentController.java b/src/main/java/ru/mtusi/messenger/controller/AttachmentController.java new file mode 100644 index 0000000..921387c --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/AttachmentController.java @@ -0,0 +1,43 @@ +package ru.mtusi.messenger.controller; + +import jakarta.servlet.http.HttpServletRequest; + +import lombok.RequiredArgsConstructor; + +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.RestController; + +import ru.mtusi.messenger.dto.AttachmentDTO; +import ru.mtusi.messenger.security.SessionPrincipal; +import ru.mtusi.messenger.service.AttachmentService; + +import java.util.UUID; + +@RestController +@RequiredArgsConstructor +public class AttachmentController implements AttachmentsApi { + private final AttachmentService service; + + @Override + public AttachmentDTO uploadAttachment( + UUID chatId, + String filename, + HttpServletRequest body, + String contentType, + @AuthenticationPrincipal SessionPrincipal principal) { + return service.upload(chatId, principal.userId(), filename, body, contentType); + } + + @Override + public AttachmentDTO getAttachment( + UUID attachmentId, @AuthenticationPrincipal SessionPrincipal principal) { + return service.getMetadata(attachmentId, principal.userId()); + } + + @Override + public ResponseEntity downloadAttachment( + UUID attachmentId, String variant, SessionPrincipal principal) { + return service.downloadResponse(attachmentId, principal.userId(), variant); + } +} diff --git a/src/main/java/ru/mtusi/messenger/controller/AttachmentsApi.java b/src/main/java/ru/mtusi/messenger/controller/AttachmentsApi.java new file mode 100644 index 0000000..3fd0f0c --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/AttachmentsApi.java @@ -0,0 +1,314 @@ +package ru.mtusi.messenger.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.enums.ParameterIn; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; + +import jakarta.servlet.http.HttpServletRequest; + +import org.hibernate.validator.constraints.CodePointLength; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; + +import ru.mtusi.messenger.dto.AttachmentDTO; +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.security.SessionPrincipal; + +import java.util.UUID; + +/** HTTP-контракт; валидация и описание операций отделены от реализации. */ +@Validated +@Tag(name = "Attachments", description = "Загрузка и скачивание вложений (PNG/JPEG) в чатах.") +@RequestMapping("${openapi.mTUSIMessenger2.base-path:}") +public interface AttachmentsApi { + + /** + * Принимает PNG/JPEG до 10 MiB для участника чата. Возвращает 202 queued только после + * сохранения исходника и COMMIT вложения, задания и outbox; обработка выполняется отдельно. + */ + @Operation( + operationId = "uploadAttachment", + summary = "Принять один PNG/JPEG, до 10 MiB", + tags = {"Attachments"}, + responses = { + @ApiResponse( + responseCode = "202", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = AttachmentDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "413", + description = "Превышен лимит", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "415", + description = "Неподдерживаемый тип", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.POST, + value = "/api/v1/chats/{chat_id}/attachments", + consumes = {MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE}, + produces = {MediaType.APPLICATION_JSON_VALUE}) + @ResponseStatus(org.springframework.http.HttpStatus.ACCEPTED) + AttachmentDTO uploadAttachment( + @Parameter(name = "chat_id", required = true, in = ParameterIn.PATH) + @PathVariable("chat_id") + UUID chatId, + @Parameter(name = "filename", required = true, in = ParameterIn.QUERY) + @CodePointLength(min = 1, max = 255) + @RequestParam("filename") + String filename, + HttpServletRequest body, + @RequestHeader("Content-Type") String contentType, + @AuthenticationPrincipal SessionPrincipal principal); + + /** Возвращает согласованный снимок вложения; варианты доступны только в состоянии ready. */ + @Operation( + operationId = "getAttachment", + summary = "Метаданные вложения, доступ участникам чата", + tags = {"Attachments"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = AttachmentDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.GET, + value = "/api/v1/attachments/{attachment_id}", + produces = {MediaType.APPLICATION_JSON_VALUE}) + @ResponseStatus(org.springframework.http.HttpStatus.OK) + AttachmentDTO getAttachment( + @Parameter(name = "attachment_id", required = true, in = ParameterIn.PATH) + @PathVariable("attachment_id") + UUID attachmentId, + @AuthenticationPrincipal SessionPrincipal principal); + + /** + * Возвращает байты выбранного варианта участнику чата; до ready отвечает 409 без редиректа в + * S3. + */ + @Operation( + operationId = "downloadAttachment", + summary = "Скачать байты через авторизованный API", + tags = {"Attachments"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "image/png", + schema = @Schema(type = "string", format = "binary")), + @Content( + mediaType = "image/jpeg", + schema = @Schema(type = "string", format = "binary")), + @Content( + mediaType = "image/webp", + schema = @Schema(type = "string", format = "binary")) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.GET, + value = "/api/v1/attachments/{attachment_id}/content") + ResponseEntity downloadAttachment( + @Parameter(name = "attachment_id", required = true, in = ParameterIn.PATH) + @PathVariable("attachment_id") + UUID attachmentId, + @Parameter(name = "variant", in = ParameterIn.QUERY) + @RequestParam(value = "variant", required = false, defaultValue = "original") + String variant, + @AuthenticationPrincipal SessionPrincipal principal); +} diff --git a/src/main/java/ru/mtusi/messenger/controller/AuthApi.java b/src/main/java/ru/mtusi/messenger/controller/AuthApi.java new file mode 100644 index 0000000..bfd4a2d --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/AuthApi.java @@ -0,0 +1,138 @@ +package ru.mtusi.messenger.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; + +import jakarta.servlet.http.HttpServletRequest; + +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseStatus; + +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.dto.SessionDTO; + +/** HTTP-контракт; валидация и описание операций отделены от реализации. */ +@Validated +@Tag( + name = "Auth", + description = "Аутентификация: вход по Basic и выдача/отзыв сессионного Bearer-токена.") +@RequestMapping("${openapi.mTUSIMessenger2.base-path:}") +public interface AuthApi { + + /** Вход: Basic username:password; выдаёт сессию. */ + @Operation( + operationId = "createSession", + summary = "Вход: Basic username:password; выдаёт сессию", + tags = {"Auth"}, + responses = { + @ApiResponse( + responseCode = "201", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = SessionDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "basicAuth")}) + @RequestMapping( + method = RequestMethod.POST, + value = "/api/v1/auth/sessions", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.CREATED) + SessionDTO createSession(HttpServletRequest request); + + /** Отозвать текущую сессию. */ + @Operation( + operationId = "revokeSession", + summary = "Отозвать текущую сессию", + tags = {"Auth"}, + responses = { + @ApiResponse(responseCode = "204", description = "Успех"), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.DELETE, + value = "/api/v1/auth/sessions/current", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.NO_CONTENT) + void revokeSession(); +} diff --git a/src/main/java/ru/mtusi/messenger/controller/AuthController.java b/src/main/java/ru/mtusi/messenger/controller/AuthController.java new file mode 100644 index 0000000..7c4de8c --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/AuthController.java @@ -0,0 +1,28 @@ +package ru.mtusi.messenger.controller; + +import jakarta.servlet.http.HttpServletRequest; + +import lombok.RequiredArgsConstructor; + +import org.springframework.http.HttpHeaders; +import org.springframework.web.bind.annotation.RestController; + +import ru.mtusi.messenger.dto.SessionDTO; +import ru.mtusi.messenger.service.AuthService; + +@RestController +@RequiredArgsConstructor +public class AuthController implements AuthApi { + private final AuthService authService; + + @Override + public SessionDTO createSession(HttpServletRequest request) { + return authService.login( + request.getHeader(HttpHeaders.AUTHORIZATION), request.getContentLengthLong()); + } + + @Override + public void revokeSession() { + authService.logout(); + } +} diff --git a/src/main/java/ru/mtusi/messenger/controller/ChatController.java b/src/main/java/ru/mtusi/messenger/controller/ChatController.java new file mode 100644 index 0000000..7f60bd4 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/ChatController.java @@ -0,0 +1,37 @@ +package ru.mtusi.messenger.controller; + +import lombok.RequiredArgsConstructor; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.RestController; + +import ru.mtusi.messenger.dto.ChatDTO; +import ru.mtusi.messenger.dto.ChatPageDTO; +import ru.mtusi.messenger.dto.CreateChatDTO; +import ru.mtusi.messenger.security.SessionPrincipal; +import ru.mtusi.messenger.service.ChatService; + +import java.util.UUID; + +@RestController +@RequiredArgsConstructor +public class ChatController implements ChatsApi { + private final ChatService chatService; + + @Override + public ChatDTO createChat( + CreateChatDTO createChatDTO, @AuthenticationPrincipal SessionPrincipal principal) { + return chatService.createChat(principal.userId(), createChatDTO); + } + + @Override + public ChatDTO getChat(UUID chatId, @AuthenticationPrincipal SessionPrincipal principal) { + return chatService.getChatDTO(principal.userId(), chatId); + } + + @Override + public ChatPageDTO listChats( + Integer limit, String cursor, @AuthenticationPrincipal SessionPrincipal principal) { + return chatService.listChats(principal.userId(), limit, cursor); + } +} diff --git a/src/main/java/ru/mtusi/messenger/controller/ChatsApi.java b/src/main/java/ru/mtusi/messenger/controller/ChatsApi.java new file mode 100644 index 0000000..832907a --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/ChatsApi.java @@ -0,0 +1,298 @@ +package ru.mtusi.messenger.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.enums.ParameterIn; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.Size; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; + +import ru.mtusi.messenger.dto.ChatDTO; +import ru.mtusi.messenger.dto.ChatPageDTO; +import ru.mtusi.messenger.dto.CreateChatDTO; +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.security.SessionPrincipal; + +import java.util.UUID; + +/** HTTP-контракт; валидация и описание операций отделены от реализации. */ +@Validated +@Tag(name = "Chats", description = "Управление чатами и доступ к чатам текущего пользователя.") +@RequestMapping("${openapi.mTUSIMessenger2.base-path:}") +public interface ChatsApi { + + /** Создать чат; вызывающий должен входить в member_ids. */ + @Operation( + operationId = "createChat", + summary = "Создать чат; вызывающий должен входить в member_ids", + tags = {"Chats"}, + responses = { + @ApiResponse( + responseCode = "201", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ChatDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.POST, + value = "/api/v1/chats", + produces = {"application/json"}, + consumes = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.CREATED) + ChatDTO createChat( + @Parameter(name = "CreateChatDTO", description = "", required = true) + @Valid + @RequestBody + CreateChatDTO createChatDTO, + @AuthenticationPrincipal SessionPrincipal principal); + + /** Получить доступный чат. */ + @Operation( + operationId = "getChatDTO", + summary = "Получить доступный чат", + tags = {"Chats"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ChatDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.GET, + value = "/api/v1/chats/{chat_id}", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.OK) + ChatDTO getChat( + @Parameter(name = "chat_id", description = "", required = true, in = ParameterIn.PATH) + @PathVariable("chat_id") + UUID chatId, + @AuthenticationPrincipal SessionPrincipal principal); + + /** Чаты текущего пользователя. */ + @Operation( + operationId = "listChats", + summary = "Чаты текущего пользователя", + tags = {"Chats"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ChatPageDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.GET, + value = "/api/v1/chats", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.OK) + ChatPageDTO listChats( + @Min(1) + @Max(100) + @Parameter(name = "limit", description = "", in = ParameterIn.QUERY) + @Valid + @RequestParam(value = "limit", required = false, defaultValue = "50") + Integer limit, + @Size(min = 1) + @Parameter(name = "cursor", description = "", in = ParameterIn.QUERY) + @Valid + @RequestParam(value = "cursor", required = false) + String cursor, + @AuthenticationPrincipal SessionPrincipal principal); +} diff --git a/src/main/java/ru/mtusi/messenger/controller/HealthApi.java b/src/main/java/ru/mtusi/messenger/controller/HealthApi.java new file mode 100644 index 0000000..a534602 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/HealthApi.java @@ -0,0 +1,92 @@ +package ru.mtusi.messenger.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; + +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseStatus; + +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.dto.HealthDTO; + +/** HTTP-контракт; валидация и описание операций отделены от реализации. */ +@Validated +@Tag( + name = "Health", + description = "Проверки живости и готовности процесса. Публичные, без аутентификации.") +@RequestMapping("${openapi.mTUSIMessenger2.base-path:}") +public interface HealthApi { + + /** Процесс жив. */ + @Operation( + operationId = "liveness", + summary = "Процесс жив", + tags = {"Health"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = HealthDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }) + @RequestMapping( + method = RequestMethod.GET, + value = "/health/live", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.OK) + HealthDTO liveness(); + + /** Готов принимать трафик. */ + @Operation( + operationId = "readiness", + summary = "Готов принимать трафик", + tags = {"Health"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = HealthDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }) + @RequestMapping( + method = RequestMethod.GET, + value = "/health/ready", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.OK) + HealthDTO readiness(); +} diff --git a/src/main/java/ru/mtusi/messenger/controller/HealthController.java b/src/main/java/ru/mtusi/messenger/controller/HealthController.java new file mode 100644 index 0000000..376ee12 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/HealthController.java @@ -0,0 +1,24 @@ +package ru.mtusi.messenger.controller; + +import lombok.RequiredArgsConstructor; + +import org.springframework.web.bind.annotation.RestController; + +import ru.mtusi.messenger.dto.HealthDTO; +import ru.mtusi.messenger.service.HealthService; + +@RestController +@RequiredArgsConstructor +public class HealthController implements HealthApi { + private final HealthService healthService; + + @Override + public HealthDTO liveness() { + return healthService.liveness(); + } + + @Override + public HealthDTO readiness() { + return healthService.readiness(); + } +} diff --git a/src/main/java/ru/mtusi/messenger/controller/MessageController.java b/src/main/java/ru/mtusi/messenger/controller/MessageController.java new file mode 100644 index 0000000..31ab145 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/MessageController.java @@ -0,0 +1,39 @@ +package ru.mtusi.messenger.controller; + +import lombok.RequiredArgsConstructor; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.RestController; + +import ru.mtusi.messenger.dto.CreateMessageDTO; +import ru.mtusi.messenger.dto.MessageDTO; +import ru.mtusi.messenger.dto.MessagePageDTO; +import ru.mtusi.messenger.security.SessionPrincipal; +import ru.mtusi.messenger.service.MessageService; + +import java.util.UUID; + +@RestController +@RequiredArgsConstructor +public class MessageController implements MessagesApi { + private final MessageService messageService; + + @Override + public MessageDTO createMessage( + UUID chatId, + String idempotencyKey, + CreateMessageDTO createMessageDTO, + @AuthenticationPrincipal SessionPrincipal principal) { + return messageService.createMessage( + principal.userId(), chatId, createMessageDTO, idempotencyKey); + } + + @Override + public MessagePageDTO listMessages( + UUID chatId, + Integer limit, + String cursor, + @AuthenticationPrincipal SessionPrincipal principal) { + return messageService.listMessages(principal.userId(), chatId, limit, cursor); + } +} diff --git a/src/main/java/ru/mtusi/messenger/controller/MessagesApi.java b/src/main/java/ru/mtusi/messenger/controller/MessagesApi.java new file mode 100644 index 0000000..fc24bbf --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/MessagesApi.java @@ -0,0 +1,228 @@ +package ru.mtusi.messenger.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.enums.ParameterIn; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.Size; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; + +import ru.mtusi.messenger.dto.CreateMessageDTO; +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.dto.MessageDTO; +import ru.mtusi.messenger.dto.MessagePageDTO; +import ru.mtusi.messenger.security.SessionPrincipal; + +import java.util.UUID; + +/** HTTP-контракт; валидация и описание операций отделены от реализации. */ +@Validated +@Tag(name = "Messages", description = "Отправка и чтение сообщений внутри доступного чата.") +@RequestMapping("${openapi.mTUSIMessenger2.base-path:}") +public interface MessagesApi { + + /** + * Создаёт сообщение идемпотентно в пределах пользователя и чата. Повтор ключа с другим телом + * даёт 409; queued/processing вложения допустимы, failed отклоняются до COMMIT сообщения. + */ + @Operation( + operationId = "createMessage", + summary = "Отправить сообщение", + tags = {"Messages"}, + responses = { + @ApiResponse( + responseCode = "201", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = MessageDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.POST, + value = "/api/v1/chats/{chat_id}/messages", + produces = {"application/json"}, + consumes = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.CREATED) + MessageDTO createMessage( + @Parameter(name = "chat_id", description = "", required = true, in = ParameterIn.PATH) + @PathVariable("chat_id") + UUID chatId, + @Parameter(name = "Idempotency-Key", required = true, in = ParameterIn.HEADER) + @RequestHeader(value = "Idempotency-Key", required = true) + String idempotencyKey, + @Parameter(name = "CreateMessageDTO", description = "", required = true) + @Valid + @RequestBody + CreateMessageDTO createMessageDTO, + @AuthenticationPrincipal SessionPrincipal principal); + + /** Сообщения по (created_at, id) по возрастанию. */ + @Operation( + operationId = "listMessages", + summary = "Сообщения по (created_at, id) по возрастанию", + tags = {"Messages"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = MessagePageDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.GET, + value = "/api/v1/chats/{chat_id}/messages", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.OK) + MessagePageDTO listMessages( + @Parameter(name = "chat_id", description = "", required = true, in = ParameterIn.PATH) + @PathVariable("chat_id") + UUID chatId, + @Min(1) + @Max(100) + @Parameter(name = "limit", description = "", in = ParameterIn.QUERY) + @Valid + @RequestParam(value = "limit", required = false, defaultValue = "50") + Integer limit, + @Size(min = 1) + @Parameter(name = "cursor", description = "", in = ParameterIn.QUERY) + @Valid + @RequestParam(value = "cursor", required = false) + String cursor, + @AuthenticationPrincipal SessionPrincipal principal); +} diff --git a/src/main/java/ru/mtusi/messenger/controller/UserController.java b/src/main/java/ru/mtusi/messenger/controller/UserController.java new file mode 100644 index 0000000..719e69f --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/UserController.java @@ -0,0 +1,27 @@ +package ru.mtusi.messenger.controller; + +import lombok.RequiredArgsConstructor; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.RestController; + +import ru.mtusi.messenger.dto.CreateUserDTO; +import ru.mtusi.messenger.dto.UserDTO; +import ru.mtusi.messenger.security.SessionPrincipal; +import ru.mtusi.messenger.service.UserService; + +@RestController +@RequiredArgsConstructor +public class UserController implements UsersApi { + private final UserService userService; + + @Override + public UserDTO currentUser(@AuthenticationPrincipal SessionPrincipal principal) { + return userService.currentUser(principal.userId()); + } + + @Override + public UserDTO registerUser(CreateUserDTO createUserDTO) { + return userService.registerUser(createUserDTO); + } +} diff --git a/src/main/java/ru/mtusi/messenger/controller/UsersApi.java b/src/main/java/ru/mtusi/messenger/controller/UsersApi.java new file mode 100644 index 0000000..d438835 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/UsersApi.java @@ -0,0 +1,178 @@ +package ru.mtusi.messenger.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; + +import jakarta.validation.Valid; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseStatus; + +import ru.mtusi.messenger.dto.CreateUserDTO; +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.dto.UserDTO; +import ru.mtusi.messenger.security.SessionPrincipal; + +/** HTTP-контракт; валидация и описание операций отделены от реализации. */ +@Validated +@Tag( + name = "Users", + description = "Регистрация пользователей и доступ к профилю текущего пользователя.") +@RequestMapping("${openapi.mTUSIMessenger2.base-path:}") +public interface UsersApi { + + /** Текущий пользователь. */ + @Operation( + operationId = "currentUser", + summary = "Текущий пользователь", + tags = {"Users"}, + responses = { + @ApiResponse( + responseCode = "200", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = UserDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "401", + description = "Нет действительной аутентификации", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "404", + description = "Объект отсутствует или недоступен", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }, + security = {@SecurityRequirement(name = "bearerAuth")}) + @RequestMapping( + method = RequestMethod.GET, + value = "/api/v1/users/me", + produces = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.OK) + UserDTO currentUser(@AuthenticationPrincipal SessionPrincipal principal); + + /** Создать пользователя. */ + @Operation( + operationId = "registerUser", + summary = "Создать пользователя", + tags = {"Users"}, + responses = { + @ApiResponse( + responseCode = "201", + description = "Успех", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = UserDTO.class)) + }), + @ApiResponse( + responseCode = "400", + description = "Неверный запрос", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "409", + description = "Конфликт", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "429", + description = "Ограничение частоты запросов", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "503", + description = "Зависимость недоступна", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }), + @ApiResponse( + responseCode = "500", + description = "Неожиданная ошибка без внутренних деталей", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ErrorDTO.class)) + }) + }) + @RequestMapping( + method = RequestMethod.POST, + value = "/api/v1/users", + produces = {"application/json"}, + consumes = {"application/json"}) + @ResponseStatus(org.springframework.http.HttpStatus.CREATED) + UserDTO registerUser( + @Parameter(name = "CreateUserDTO", description = "", required = true) + @Valid + @RequestBody + CreateUserDTO createUserDTO); +} diff --git a/src/main/java/ru/mtusi/messenger/controller/controllerAdvice/GlobalExceptionHandler.java b/src/main/java/ru/mtusi/messenger/controller/controllerAdvice/GlobalExceptionHandler.java new file mode 100644 index 0000000..8da6d0c --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/controller/controllerAdvice/GlobalExceptionHandler.java @@ -0,0 +1,247 @@ +package ru.mtusi.messenger.controller.controllerAdvice; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.ConstraintViolationException; + +import lombok.RequiredArgsConstructor; + +import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataAccessResourceFailureException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.transaction.CannotCreateTransactionException; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingRequestHeaderException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.method.annotation.HandlerMethodValidationException; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; + +import ru.mtusi.messenger.config.properties.RateLimitProperties; +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.dto.ErrorErrorDTO; +import ru.mtusi.messenger.exception.ApiErrorFactory; +import ru.mtusi.messenger.exception.ConflictException; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.NotFoundException; +import ru.mtusi.messenger.exception.PayloadTooLargeException; +import ru.mtusi.messenger.exception.RateLimitedException; +import ru.mtusi.messenger.exception.ServiceUnavailableException; +import ru.mtusi.messenger.exception.StorageUnavailableException; +import ru.mtusi.messenger.exception.UnauthorizedException; +import ru.mtusi.messenger.exception.UnsupportedMediaTypeException; + +@RestControllerAdvice +@RequiredArgsConstructor +public class GlobalExceptionHandler { + private static final org.slf4j.Logger log = + org.slf4j.LoggerFactory.getLogger(GlobalExceptionHandler.class); + private final RateLimitProperties limitProperties; + private final ApiErrorFactory errors; + + @ExceptionHandler(InvalidRequestException.class) + public ResponseEntity invalidRequest( + InvalidRequestException e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + e.getMessage(), + req); + } + + @ExceptionHandler(UnauthorizedException.class) + public ResponseEntity unauthorized(UnauthorizedException e, HttpServletRequest req) { + boolean isLogin = "/api/v1/auth/sessions".equals(req.getRequestURI()); + String challenge = + isLogin + ? "Basic realm=\"messenger\", charset=\"UTF-8\"" + : "Bearer realm=\"messenger\""; + return ResponseEntity.status(HttpStatus.UNAUTHORIZED) + .header("WWW-Authenticate", challenge) + .body(errors.error(ErrorErrorDTO.CodeEnum.UNAUTHORIZED, e.getMessage(), req)); + } + + @ExceptionHandler(NotFoundException.class) + public ResponseEntity notFound(NotFoundException e, HttpServletRequest req) { + return respond(HttpStatus.NOT_FOUND, ErrorErrorDTO.CodeEnum.NOT_FOUND, e.getMessage(), req); + } + + @ExceptionHandler(ConflictException.class) + public ResponseEntity conflict(ConflictException e, HttpServletRequest req) { + return respond(HttpStatus.CONFLICT, ErrorErrorDTO.CodeEnum.CONFLICT, e.getMessage(), req); + } + + @ExceptionHandler(PayloadTooLargeException.class) + public ResponseEntity payloadTooLarge( + PayloadTooLargeException e, HttpServletRequest req) { + return respond( + HttpStatus.PAYLOAD_TOO_LARGE, + ErrorErrorDTO.CodeEnum.PAYLOAD_TOO_LARGE, + e.getMessage(), + req); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity validation( + MethodArgumentNotValidException e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Validation failed", + req); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity unreadable(Exception e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Malformed request body", + req); + } + + @ExceptionHandler(MissingServletRequestParameterException.class) + public ResponseEntity missingParam(Exception e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Missing parameter", + req); + } + + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + public ResponseEntity typeMismatch(Exception e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Invalid parameter type", + req); + } + + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) + public ResponseEntity mediaType(Exception e, HttpServletRequest req) { + return respond( + HttpStatus.UNSUPPORTED_MEDIA_TYPE, + ErrorErrorDTO.CodeEnum.UNSUPPORTED_MEDIA_TYPE, + "Unsupported media type", + req); + } + + @ExceptionHandler(HandlerMethodValidationException.class) + public ResponseEntity methodValidation(Exception e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Validation failed", + req); + } + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public ResponseEntity methodNotAllowed(Exception e, HttpServletRequest req) { + return respond( + HttpStatus.METHOD_NOT_ALLOWED, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Method not allowed", + req); + } + + @ExceptionHandler(RateLimitedException.class) + public ResponseEntity rateLimited(RateLimitedException e, HttpServletRequest req) { + return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS) + .header("Retry-After", String.valueOf(limitProperties.retryAfterSeconds())) + .body(errors.error(ErrorErrorDTO.CodeEnum.RATE_LIMITED, e.getMessage(), req)); + } + + @ExceptionHandler(ConstraintViolationException.class) + public ResponseEntity constraint( + ConstraintViolationException e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Validation failed", + req); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity fallback(Exception e, HttpServletRequest req) { + log.error("Unexpected API failure type={}", e.getClass().getName()); + return respond( + HttpStatus.INTERNAL_SERVER_ERROR, + ErrorErrorDTO.CodeEnum.INTERNAL_ERROR, + "Internal error", + req); + } + + @ExceptionHandler(ServiceUnavailableException.class) + public ResponseEntity serviceUnavailable( + ServiceUnavailableException e, HttpServletRequest req) { + return respond( + HttpStatus.SERVICE_UNAVAILABLE, + ErrorErrorDTO.CodeEnum.UNAVAILABLE, + e.getMessage(), + req); + } + + @ExceptionHandler({ + CannotCreateTransactionException.class, + DataAccessResourceFailureException.class + }) + public ResponseEntity dependencyUnavailable(Exception e, HttpServletRequest req) { + return respond( + HttpStatus.SERVICE_UNAVAILABLE, + ErrorErrorDTO.CodeEnum.UNAVAILABLE, + "Dependency unavailable", + req); + } + + @ExceptionHandler(DataAccessException.class) + public ResponseEntity dataAccess(DataAccessException e, HttpServletRequest req) { + return respond( + HttpStatus.INTERNAL_SERVER_ERROR, + ErrorErrorDTO.CodeEnum.INTERNAL_ERROR, + "Database error", + req); + } + + @ExceptionHandler(MissingRequestHeaderException.class) + public ResponseEntity missingHeader( + MissingRequestHeaderException e, HttpServletRequest req) { + return respond( + HttpStatus.BAD_REQUEST, + ErrorErrorDTO.CodeEnum.INVALID_REQUEST, + "Missing header: " + e.getHeaderName(), + req); + } + + @ExceptionHandler(UnsupportedMediaTypeException.class) + public ResponseEntity unsupportedMedia( + UnsupportedMediaTypeException e, HttpServletRequest req) { + return respond( + HttpStatus.UNSUPPORTED_MEDIA_TYPE, + ErrorErrorDTO.CodeEnum.UNSUPPORTED_MEDIA_TYPE, + e.getMessage(), + req); + } + + @ExceptionHandler(StorageUnavailableException.class) + public ResponseEntity storageUnavailable( + StorageUnavailableException e, HttpServletRequest req) { + return respond( + HttpStatus.SERVICE_UNAVAILABLE, + ErrorErrorDTO.CodeEnum.UNAVAILABLE, + e.getMessage(), + req); + } + + private ResponseEntity respond( + HttpStatus status, + ErrorErrorDTO.CodeEnum code, + String message, + HttpServletRequest req) { + return ResponseEntity.status(status).body(errors.error(code, message, req)); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/AttachmentDTO.java b/src/main/java/ru/mtusi/messenger/dto/AttachmentDTO.java new file mode 100644 index 0000000..5ca0bac --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/AttachmentDTO.java @@ -0,0 +1,417 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.annotation.Generated; +import jakarta.validation.Valid; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +@Generated( + value = "org.openapitools.codegen.languages.SpringCodegen", + comments = "Generator version: 7.10.0") +public class AttachmentDTO { + + private UUID id; + + private UUID chatId; + + private UUID ownerId; + + private String filename; + + public enum ContentTypeEnum { + IMAGE_PNG("image/png"), + IMAGE_JPEG("image/jpeg"); + + private final String value; + + ContentTypeEnum(String value) { + this.value = value; + } + + @JsonValue + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static ContentTypeEnum fromValue(String value) { + for (ContentTypeEnum b : ContentTypeEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + private ContentTypeEnum contentType; + + private Long sizeBytes; + + private String sha256; + + public enum StatusEnum { + QUEUED("queued"), + PROCESSING("processing"), + READY("ready"), + FAILED("failed"); + + private final String value; + + StatusEnum(String value) { + this.value = value; + } + + @JsonValue + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static StatusEnum fromValue(String value) { + for (StatusEnum b : StatusEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + private StatusEnum status; + + @Valid private List<@Valid VariantDTO> variants = new ArrayList<>(); + + private String errorCode; + + private Instant createdAt; + + private Instant updatedAt; + + public AttachmentDTO() { + super(); + } + + public AttachmentDTO( + UUID id, + UUID chatId, + UUID ownerId, + String filename, + ContentTypeEnum contentType, + Long sizeBytes, + String sha256, + StatusEnum status, + List<@Valid VariantDTO> variants, + String errorCode, + Instant createdAt, + Instant updatedAt) { + this.id = id; + this.chatId = chatId; + this.ownerId = ownerId; + this.filename = filename; + this.contentType = contentType; + this.sizeBytes = sizeBytes; + this.sha256 = sha256; + this.status = status; + this.variants = variants; + this.errorCode = errorCode; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + + public AttachmentDTO id(UUID id) { + this.id = id; + return this; + } + + @Valid + @Schema(name = "id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("id") + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public AttachmentDTO chatId(UUID chatId) { + this.chatId = chatId; + return this; + } + + @Valid + @Schema(name = "chat_id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("chat_id") + public UUID getChatId() { + return chatId; + } + + public void setChatId(UUID chatId) { + this.chatId = chatId; + } + + public AttachmentDTO ownerId(UUID ownerId) { + this.ownerId = ownerId; + return this; + } + + @Valid + @Schema(name = "owner_id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("owner_id") + public UUID getOwnerId() { + return ownerId; + } + + public void setOwnerId(UUID ownerId) { + this.ownerId = ownerId; + } + + public AttachmentDTO filename(String filename) { + this.filename = filename; + return this; + } + + @NotNull + @Size(min = 1, max = 255) + @Schema(name = "filename", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("filename") + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public AttachmentDTO contentType(ContentTypeEnum contentType) { + this.contentType = contentType; + return this; + } + + @NotNull + @Schema(name = "content_type", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("content_type") + public ContentTypeEnum getContentType() { + return contentType; + } + + public void setContentType(ContentTypeEnum contentType) { + this.contentType = contentType; + } + + public AttachmentDTO sizeBytes(Long sizeBytes) { + this.sizeBytes = sizeBytes; + return this; + } + + @NotNull + @Min(1L) + @Max(10485760L) + @Schema(name = "size_bytes", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("size_bytes") + public Long getSizeBytes() { + return sizeBytes; + } + + public void setSizeBytes(Long sizeBytes) { + this.sizeBytes = sizeBytes; + } + + public AttachmentDTO sha256(String sha256) { + this.sha256 = sha256; + return this; + } + + @NotNull + @Pattern(regexp = "^[a-f0-9]{64}$") + @Schema(name = "sha256", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("sha256") + public String getSha256() { + return sha256; + } + + public void setSha256(String sha256) { + this.sha256 = sha256; + } + + public AttachmentDTO status(StatusEnum status) { + this.status = status; + return this; + } + + @NotNull + @Schema(name = "status", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("status") + public StatusEnum getStatus() { + return status; + } + + public void setStatus(StatusEnum status) { + this.status = status; + } + + public AttachmentDTO variants(List<@Valid VariantDTO> variants) { + this.variants = variants; + return this; + } + + public AttachmentDTO addVariantsItem(VariantDTO variantsItem) { + if (this.variants == null) { + this.variants = new ArrayList<>(); + } + this.variants.add(variantsItem); + return this; + } + + @NotNull + @Valid + @Schema(name = "variants", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("variants") + public List<@Valid VariantDTO> getVariants() { + return variants; + } + + public void setVariants(List<@Valid VariantDTO> variants) { + this.variants = variants; + } + + public AttachmentDTO errorCode(String errorCode) { + this.errorCode = errorCode; + return this; + } + + @Schema(name = "error_code", requiredMode = Schema.RequiredMode.REQUIRED, nullable = true) + @JsonProperty("error_code") + public String getErrorCode() { + return errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public AttachmentDTO createdAt(Instant createdAt) { + this.createdAt = createdAt; + return this; + } + + @NotNull + @Schema(name = "created_at", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("created_at") + public Instant getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + public AttachmentDTO updatedAt(Instant updatedAt) { + this.updatedAt = updatedAt; + return this; + } + + @NotNull + @Schema(name = "updated_at", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("updated_at") + public Instant getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Instant updatedAt) { + this.updatedAt = updatedAt; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AttachmentDTO attachment = (AttachmentDTO) o; + return Objects.equals(this.id, attachment.id) + && Objects.equals(this.chatId, attachment.chatId) + && Objects.equals(this.ownerId, attachment.ownerId) + && Objects.equals(this.filename, attachment.filename) + && Objects.equals(this.contentType, attachment.contentType) + && Objects.equals(this.sizeBytes, attachment.sizeBytes) + && Objects.equals(this.sha256, attachment.sha256) + && Objects.equals(this.status, attachment.status) + && Objects.equals(this.variants, attachment.variants) + && Objects.equals(this.errorCode, attachment.errorCode) + && Objects.equals(this.createdAt, attachment.createdAt) + && Objects.equals(this.updatedAt, attachment.updatedAt); + } + + @Override + public int hashCode() { + return Objects.hash( + id, + chatId, + ownerId, + filename, + contentType, + sizeBytes, + sha256, + status, + variants, + errorCode, + createdAt, + updatedAt); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class AttachmentDTO {\n"); + sb.append(" id: ").append(toIndentedString(id)).append("\n"); + sb.append(" chatId: ").append(toIndentedString(chatId)).append("\n"); + sb.append(" ownerId: ").append(toIndentedString(ownerId)).append("\n"); + sb.append(" filename: ").append(toIndentedString(filename)).append("\n"); + sb.append(" contentType: ").append(toIndentedString(contentType)).append("\n"); + sb.append(" sizeBytes: ").append(toIndentedString(sizeBytes)).append("\n"); + sb.append(" sha256: ").append(toIndentedString(sha256)).append("\n"); + sb.append(" status: ").append(toIndentedString(status)).append("\n"); + sb.append(" variants: ").append(toIndentedString(variants)).append("\n"); + sb.append(" errorCode: ").append(toIndentedString(errorCode)).append("\n"); + sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n"); + sb.append(" updatedAt: ").append(toIndentedString(updatedAt)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/ChatDTO.java b/src/main/java/ru/mtusi/messenger/dto/ChatDTO.java new file mode 100644 index 0000000..33ab2cb --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/ChatDTO.java @@ -0,0 +1,155 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.annotation.Generated; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +import org.hibernate.validator.constraints.CodePointLength; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +@Generated( + value = "org.openapitools.codegen.languages.SpringCodegen", + comments = "Generator version: 7.10.0") +public class ChatDTO { + + private UUID id; + + private String title; + + @Valid private List memberIds = new ArrayList<>(); + + private Instant createdAt; + + public ChatDTO() { + super(); + } + + public ChatDTO(String title, List memberIds) { + this.title = title; + this.memberIds = memberIds; + } + + public ChatDTO id(UUID id) { + this.id = id; + return this; + } + + @Valid + @Schema(name = "id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("id") + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public ChatDTO title(String title) { + this.title = title; + return this; + } + + @NotNull + @CodePointLength(min = 1, max = 100) + @Schema(name = "title", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("title") + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public ChatDTO memberIds(List memberIds) { + this.memberIds = memberIds; + return this; + } + + public ChatDTO addMemberIdsItem(UUID memberIdsItem) { + if (this.memberIds == null) { + this.memberIds = new ArrayList<>(); + } + this.memberIds.add(memberIdsItem); + return this; + } + + @NotNull + @Valid + @Size(min = 1, max = 100) + @Schema(name = "member_ids", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("member_ids") + public List getMemberIds() { + return memberIds; + } + + public void setMemberIds(List memberIds) { + this.memberIds = memberIds; + } + + public ChatDTO createdAt(Instant createdAt) { + this.createdAt = createdAt; + return this; + } + + @NotNull + @Schema(name = "created_at", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("created_at") + public Instant getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ChatDTO createChat = (ChatDTO) o; + return Objects.equals(this.id, createChat.id) + && Objects.equals(this.title, createChat.title) + && Objects.equals(this.memberIds, createChat.memberIds) + && Objects.equals(this.createdAt, createChat.createdAt); + } + + @Override + public int hashCode() { + return Objects.hash(id, title, memberIds, createdAt); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class ChatDTO {\n"); + sb.append(" id: ").append(toIndentedString(id)).append("\n"); + sb.append(" title: ").append(toIndentedString(title)).append("\n"); + sb.append(" memberIds: ").append(toIndentedString(memberIds)).append("\n"); + sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/ChatPageDTO.java b/src/main/java/ru/mtusi/messenger/dto/ChatPageDTO.java new file mode 100644 index 0000000..bd67e75 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/ChatPageDTO.java @@ -0,0 +1,105 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class ChatPageDTO { + + @Valid private List<@Valid ChatDTO> items = new ArrayList<>(); + + private String nextCursor = null; + + public ChatPageDTO() { + super(); + } + + public ChatPageDTO(List<@Valid ChatDTO> items, String nextCursor) { + this.items = items; + this.nextCursor = nextCursor; + } + + public ChatPageDTO items(List<@Valid ChatDTO> items) { + this.items = items; + return this; + } + + public ChatPageDTO addItemsItem(ChatDTO itemsItem) { + if (this.items == null) { + this.items = new ArrayList<>(); + } + this.items.add(itemsItem); + return this; + } + + @NotNull + @Valid + @Schema(name = "items", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("items") + public List<@Valid ChatDTO> getItems() { + return items; + } + + public void setItems(List<@Valid ChatDTO> items) { + this.items = items; + } + + public ChatPageDTO nextCursor(String nextCursor) { + this.nextCursor = nextCursor; + return this; + } + + @JsonInclude(JsonInclude.Include.ALWAYS) + @Schema(name = "next_cursor", requiredMode = Schema.RequiredMode.NOT_REQUIRED) + @JsonProperty("next_cursor") + public String getNextCursor() { + return nextCursor; + } + + public void setNextCursor(String nextCursor) { + this.nextCursor = nextCursor; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ChatPageDTO chatPageDTO = (ChatPageDTO) o; + return Objects.equals(this.items, chatPageDTO.items) + && Objects.equals(this.nextCursor, chatPageDTO.nextCursor); + } + + @Override + public int hashCode() { + return Objects.hash(items, nextCursor); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class ChatPageDTO {\n"); + sb.append(" items: ").append(toIndentedString(items)).append("\n"); + sb.append(" nextCursor: ").append(toIndentedString(nextCursor)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/CreateChatDTO.java b/src/main/java/ru/mtusi/messenger/dto/CreateChatDTO.java new file mode 100644 index 0000000..32bb755 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/CreateChatDTO.java @@ -0,0 +1,107 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.annotation.Generated; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +import org.hibernate.validator.constraints.CodePointLength; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +@Generated( + value = "org.openapitools.codegen.languages.SpringCodegen", + comments = "Generator version: 7.10.0") +public class CreateChatDTO { + + private String title; + + @Valid private List memberIds = new ArrayList<>(); + + public CreateChatDTO() { + super(); + } + + public CreateChatDTO(String title, List memberIds) { + this.title = title; + this.memberIds = memberIds; + } + + public CreateChatDTO title(String title) { + this.title = title; + return this; + } + + @NotNull + @CodePointLength(min = 1, max = 100) + @Schema(name = "title", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("title") + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public CreateChatDTO memberIds(List memberIds) { + this.memberIds = memberIds; + return this; + } + + public CreateChatDTO addMemberIdsItem(UUID memberIdsItem) { + if (this.memberIds == null) { + this.memberIds = new ArrayList<>(); + } + this.memberIds.add(memberIdsItem); + return this; + } + + @NotNull + @Valid + @Size(min = 1, max = 100) + @Schema(name = "member_ids", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("member_ids") + public List getMemberIds() { + return memberIds; + } + + public void setMemberIds(List memberIds) { + this.memberIds = memberIds; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CreateChatDTO that = (CreateChatDTO) o; + return Objects.equals(title, that.title) && Objects.equals(memberIds, that.memberIds); + } + + @Override + public int hashCode() { + return Objects.hash(title, memberIds); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class CreateChatDTO {\n"); + sb.append(" title: ").append(toIndentedString(title)).append("\n"); + sb.append(" memberIds: ").append(toIndentedString(memberIds)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) return "null"; + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/CreateMessageDTO.java b/src/main/java/ru/mtusi/messenger/dto/CreateMessageDTO.java new file mode 100644 index 0000000..533cac0 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/CreateMessageDTO.java @@ -0,0 +1,60 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.constraints.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +@Schema(name = "CreateMessage") +public class CreateMessageDTO { + + private String text; + + private List attachmentIds = new ArrayList<>(); + + public CreateMessageDTO() {} + + public CreateMessageDTO text(String text) { + this.text = text; + return this; + } + + @NotNull + @Schema(name = "text", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("text") + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + @Schema(name = "attachment_ids", requiredMode = Schema.RequiredMode.NOT_REQUIRED) + @JsonProperty("attachment_ids") + public List getAttachmentIds() { + return attachmentIds; + } + + public void setAttachmentIds(List attachmentIds) { + this.attachmentIds = attachmentIds; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CreateMessageDTO d)) return false; + return Objects.equals(text, d.text) && Objects.equals(attachmentIds, d.attachmentIds); + } + + @Override + public int hashCode() { + return Objects.hash(text, attachmentIds); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/CreateUserDTO.java b/src/main/java/ru/mtusi/messenger/dto/CreateUserDTO.java new file mode 100644 index 0000000..7872083 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/CreateUserDTO.java @@ -0,0 +1,120 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.annotation.Generated; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.CodePointLength; + +import java.util.Objects; + +@Generated( + value = "org.openapitools.codegen.languages.SpringCodegen", + comments = "Generator version: 7.10.0") +public class CreateUserDTO { + + private String username; + + private String displayName; + + private String password; + + public CreateUserDTO() { + super(); + } + + public CreateUserDTO(String username, String displayName, String password) { + this.username = username; + this.displayName = displayName; + this.password = password; + } + + public CreateUserDTO username(String username) { + this.username = username; + return this; + } + + @NotNull + @Pattern(regexp = "^[a-z0-9_]{3,32}$") + @Schema(name = "username", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("username") + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public CreateUserDTO displayName(String displayName) { + this.displayName = displayName; + return this; + } + + @NotNull + @CodePointLength(min = 1, max = 100) + @Schema(name = "display_name", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("display_name") + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public CreateUserDTO password(String password) { + this.password = password; + return this; + } + + @NotNull + @CodePointLength(min = 12, max = 128) + @Schema( + name = "password", + requiredMode = Schema.RequiredMode.REQUIRED, + accessMode = Schema.AccessMode.WRITE_ONLY) + @JsonProperty(value = "password", access = JsonProperty.Access.WRITE_ONLY) + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CreateUserDTO createUser = (CreateUserDTO) o; + return Objects.equals(this.username, createUser.username) + && Objects.equals(this.displayName, createUser.displayName) + && Objects.equals(this.password, createUser.password); + } + + @Override + public int hashCode() { + return Objects.hash(username, displayName, password); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class CreateUserDTO {\n"); + sb.append(" username: ").append(toIndentedString(username)).append("\n"); + sb.append(" displayName: ").append(toIndentedString(displayName)).append("\n"); + sb.append(" password: ").append(password != null ? "***" : "null").append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) return "null"; + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/ErrorDTO.java b/src/main/java/ru/mtusi/messenger/dto/ErrorDTO.java new file mode 100644 index 0000000..1d17bcc --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/ErrorDTO.java @@ -0,0 +1,73 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; + +import java.util.Objects; + +public class ErrorDTO { + + private ErrorErrorDTO error; + + public ErrorDTO() { + super(); + } + + public ErrorDTO(ErrorErrorDTO error) { + this.error = error; + } + + public ErrorDTO error(ErrorErrorDTO error) { + this.error = error; + return this; + } + + @NotNull + @Valid + @Schema(name = "error", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("error") + public ErrorErrorDTO getError() { + return error; + } + + public void setError(ErrorErrorDTO error) { + this.error = error; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ErrorDTO errorDTO = (ErrorDTO) o; + return Objects.equals(this.error, errorDTO.error); + } + + @Override + public int hashCode() { + return Objects.hash(error); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class ErrorDTO {\n"); + sb.append(" error: ").append(toIndentedString(error)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/ErrorErrorDTO.java b/src/main/java/ru/mtusi/messenger/dto/ErrorErrorDTO.java new file mode 100644 index 0000000..108eb4f --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/ErrorErrorDTO.java @@ -0,0 +1,156 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +import java.util.Objects; + +public class ErrorErrorDTO { + + public enum CodeEnum { + INVALID_REQUEST("invalid_request"), + UNAUTHORIZED("unauthorized"), + NOT_FOUND("not_found"), + CONFLICT("conflict"), + PAYLOAD_TOO_LARGE("payload_too_large"), + UNSUPPORTED_MEDIA_TYPE("unsupported_media_type"), + RATE_LIMITED("rate_limited"), + UNAVAILABLE("unavailable"), + INTERNAL_ERROR("internal_error"); + + private String value; + + CodeEnum(String value) { + this.value = value; + } + + @JsonValue + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static CodeEnum fromValue(String value) { + for (CodeEnum b : CodeEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + private CodeEnum code; + + private String message; + + private String requestId; + + public ErrorErrorDTO() { + super(); + } + + public ErrorErrorDTO(CodeEnum code, String message, String requestId) { + this.code = code; + this.message = message; + this.requestId = requestId; + } + + public ErrorErrorDTO code(CodeEnum code) { + this.code = code; + return this; + } + + @NotNull + @Schema(name = "code", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("code") + public CodeEnum getCode() { + return code; + } + + public void setCode(CodeEnum code) { + this.code = code; + } + + public ErrorErrorDTO message(String message) { + this.message = message; + return this; + } + + @NotNull + @Size(min = 1) + @Schema(name = "message", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("message") + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public ErrorErrorDTO requestId(String requestId) { + this.requestId = requestId; + return this; + } + + @NotNull + @Size(min = 1) + @Schema(name = "request_id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("request_id") + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ErrorErrorDTO errorErrorDTO = (ErrorErrorDTO) o; + return Objects.equals(this.code, errorErrorDTO.code) + && Objects.equals(this.message, errorErrorDTO.message) + && Objects.equals(this.requestId, errorErrorDTO.requestId); + } + + @Override + public int hashCode() { + return Objects.hash(code, message, requestId); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class ErrorErrorDTO {\n"); + sb.append(" code: ").append(toIndentedString(code)).append("\n"); + sb.append(" message: ").append(toIndentedString(message)).append("\n"); + sb.append(" requestId: ").append(toIndentedString(requestId)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/HealthDTO.java b/src/main/java/ru/mtusi/messenger/dto/HealthDTO.java new file mode 100644 index 0000000..455e043 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/HealthDTO.java @@ -0,0 +1,103 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.constraints.NotNull; + +import java.util.Objects; + +public class HealthDTO { + + public enum StatusEnum { + OK("ok"); + + private String value; + + StatusEnum(String value) { + this.value = value; + } + + @JsonValue + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static StatusEnum fromValue(String value) { + for (StatusEnum b : StatusEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + private StatusEnum status; + + public HealthDTO() { + super(); + } + + public HealthDTO(StatusEnum status) { + this.status = status; + } + + public HealthDTO status(StatusEnum status) { + this.status = status; + return this; + } + + @NotNull + @Schema(name = "status", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("status") + public StatusEnum getStatus() { + return status; + } + + public void setStatus(StatusEnum status) { + this.status = status; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + HealthDTO healthDTO = (HealthDTO) o; + return Objects.equals(this.status, healthDTO.status); + } + + @Override + public int hashCode() { + return Objects.hash(status); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class HealthDTO {\n"); + sb.append(" status: ").append(toIndentedString(status)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/MessageDTO.java b/src/main/java/ru/mtusi/messenger/dto/MessageDTO.java new file mode 100644 index 0000000..5b1aa25 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/MessageDTO.java @@ -0,0 +1,197 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; + +import org.hibernate.validator.constraints.CodePointLength; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +public class MessageDTO { + + private UUID id; + + private UUID chatId; + + private UUID senderId; + + private String text; + + private Instant createdAt; + + private List attachmentIds = new ArrayList<>(); + + public MessageDTO() { + super(); + } + + public MessageDTO(UUID id, UUID chatId, UUID senderId, String text, Instant createdAt) { + this.id = id; + this.chatId = chatId; + this.senderId = senderId; + this.text = text; + this.createdAt = createdAt; + } + + public MessageDTO( + UUID id, + UUID chatId, + UUID senderId, + String text, + Instant createdAt, + List attachmentIds) { + this.id = id; + this.chatId = chatId; + this.senderId = senderId; + this.text = text; + this.createdAt = createdAt; + this.attachmentIds = attachmentIds != null ? attachmentIds : new ArrayList<>(); + } + + public MessageDTO id(UUID id) { + this.id = id; + return this; + } + + @NotNull + @Valid + @Schema(name = "id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("id") + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public MessageDTO chatId(UUID chatId) { + this.chatId = chatId; + return this; + } + + @NotNull + @Valid + @Schema(name = "chat_id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("chat_id") + public UUID getChatId() { + return chatId; + } + + public void setChatId(UUID chatId) { + this.chatId = chatId; + } + + public MessageDTO senderId(UUID senderId) { + this.senderId = senderId; + return this; + } + + @NotNull + @Valid + @Schema(name = "sender_id", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("sender_id") + public UUID getSenderId() { + return senderId; + } + + public void setSenderId(UUID senderId) { + this.senderId = senderId; + } + + public MessageDTO text(String text) { + this.text = text; + return this; + } + + @NotNull + @CodePointLength(min = 1, max = 4000) + @Schema(name = "text", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("text") + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public MessageDTO createdAt(Instant createdAt) { + this.createdAt = createdAt; + return this; + } + + @NotNull + @Valid + @Schema(name = "created_at", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("created_at") + public Instant getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + @Valid + @Schema(name = "attachment_ids", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("attachment_ids") + public List getAttachmentIds() { + return attachmentIds; + } + + public void setAttachmentIds(List attachmentIds) { + this.attachmentIds = attachmentIds; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + MessageDTO messageDTO = (MessageDTO) o; + return Objects.equals(this.id, messageDTO.id) + && Objects.equals(this.chatId, messageDTO.chatId) + && Objects.equals(this.senderId, messageDTO.senderId) + && Objects.equals(this.text, messageDTO.text) + && Objects.equals(this.createdAt, messageDTO.createdAt) + && Objects.equals(this.attachmentIds, messageDTO.attachmentIds); + } + + @Override + public int hashCode() { + return Objects.hash(id, chatId, senderId, text, createdAt, attachmentIds); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class MessageDTO {\n"); + sb.append(" id: ").append(toIndentedString(id)).append("\n"); + sb.append(" chatId: ").append(toIndentedString(chatId)).append("\n"); + sb.append(" senderId: ").append(toIndentedString(senderId)).append("\n"); + sb.append(" text: ").append(toIndentedString(text)).append("\n"); + sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n"); + sb.append(" attachmentIds: ").append(toIndentedString(attachmentIds)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/MessagePageDTO.java b/src/main/java/ru/mtusi/messenger/dto/MessagePageDTO.java new file mode 100644 index 0000000..6b0f70e --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/MessagePageDTO.java @@ -0,0 +1,105 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class MessagePageDTO { + + @Valid private List<@Valid MessageDTO> items = new ArrayList<>(); + + private String nextCursor = null; + + public MessagePageDTO() { + super(); + } + + public MessagePageDTO(List<@Valid MessageDTO> items, String nextCursor) { + this.items = items; + this.nextCursor = nextCursor; + } + + public MessagePageDTO items(List<@Valid MessageDTO> items) { + this.items = items; + return this; + } + + public MessagePageDTO addItemsItem(MessageDTO itemsItem) { + if (this.items == null) { + this.items = new ArrayList<>(); + } + this.items.add(itemsItem); + return this; + } + + @NotNull + @Valid + @Schema(name = "items", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("items") + public List<@Valid MessageDTO> getItems() { + return items; + } + + public void setItems(List<@Valid MessageDTO> items) { + this.items = items; + } + + public MessagePageDTO nextCursor(String nextCursor) { + this.nextCursor = nextCursor; + return this; + } + + @JsonInclude(JsonInclude.Include.ALWAYS) + @Schema(name = "next_cursor", requiredMode = Schema.RequiredMode.NOT_REQUIRED) + @JsonProperty("next_cursor") + public String getNextCursor() { + return nextCursor; + } + + public void setNextCursor(String nextCursor) { + this.nextCursor = nextCursor; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + MessagePageDTO messagePageDTO = (MessagePageDTO) o; + return Objects.equals(this.items, messagePageDTO.items) + && Objects.equals(this.nextCursor, messagePageDTO.nextCursor); + } + + @Override + public int hashCode() { + return Objects.hash(items, nextCursor); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class MessagePageDTO {\n"); + sb.append(" items: ").append(toIndentedString(items)).append("\n"); + sb.append(" nextCursor: ").append(toIndentedString(nextCursor)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/SessionDTO.java b/src/main/java/ru/mtusi/messenger/dto/SessionDTO.java new file mode 100644 index 0000000..ac5ea61 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/SessionDTO.java @@ -0,0 +1,172 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +import java.time.Instant; +import java.util.Objects; + +public class SessionDTO { + + private String token; + + public enum TokenTypeEnum { + BEARER("Bearer"); + + private String value; + + TokenTypeEnum(String value) { + this.value = value; + } + + @JsonValue + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static TokenTypeEnum fromValue(String value) { + for (TokenTypeEnum b : TokenTypeEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + private TokenTypeEnum tokenType; + + private Instant expiresAt; + + private UserDTO user; + + public SessionDTO() { + super(); + } + + public SessionDTO(String token, TokenTypeEnum tokenType, Instant expiresAt, UserDTO user) { + this.token = token; + this.tokenType = tokenType; + this.expiresAt = expiresAt; + this.user = user; + } + + public SessionDTO token(String token) { + this.token = token; + return this; + } + + @NotNull + @Size(min = 32) + @Schema(name = "token", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("token") + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public SessionDTO tokenType(TokenTypeEnum tokenType) { + this.tokenType = tokenType; + return this; + } + + @NotNull + @Schema(name = "token_type", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("token_type") + public TokenTypeEnum getTokenType() { + return tokenType; + } + + public void setTokenType(TokenTypeEnum tokenType) { + this.tokenType = tokenType; + } + + public SessionDTO expiresAt(Instant expiresAt) { + this.expiresAt = expiresAt; + return this; + } + + @NotNull + @Valid + @Schema(name = "expires_at", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("expires_at") + public Instant getExpiresAt() { + return expiresAt; + } + + public void setExpiresAt(Instant expiresAt) { + this.expiresAt = expiresAt; + } + + public SessionDTO user(UserDTO user) { + this.user = user; + return this; + } + + @NotNull + @Valid + @Schema(name = "user", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("user") + public UserDTO getUser() { + return user; + } + + public void setUser(UserDTO user) { + this.user = user; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SessionDTO sessionDTO = (SessionDTO) o; + return Objects.equals(this.token, sessionDTO.token) + && Objects.equals(this.tokenType, sessionDTO.tokenType) + && Objects.equals(this.expiresAt, sessionDTO.expiresAt) + && Objects.equals(this.user, sessionDTO.user); + } + + @Override + public int hashCode() { + return Objects.hash(token, tokenType, expiresAt, user); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class SessionDTO {\n"); + sb.append(" token: ").append(toIndentedString(token)).append("\n"); + sb.append(" tokenType: ").append(toIndentedString(tokenType)).append("\n"); + sb.append(" expiresAt: ").append(toIndentedString(expiresAt)).append("\n"); + sb.append(" user: ").append(toIndentedString(user)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/UserDTO.java b/src/main/java/ru/mtusi/messenger/dto/UserDTO.java new file mode 100644 index 0000000..d32ca90 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/UserDTO.java @@ -0,0 +1,140 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.CodePointLength; + +import java.time.Instant; +import java.util.Objects; +import java.util.UUID; + +public class UserDTO { + + private UUID id; + + private String username; + + private String displayName; + + private Instant createdAt; + + public UserDTO() { + super(); + } + + public UserDTO(String username, String displayName) { + this.username = username; + this.displayName = displayName; + } + + public UserDTO username(String username) { + this.username = username; + return this; + } + + @Valid + @Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED) + @JsonProperty("id") + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public UserDTO id(UUID id) { + this.id = id; + return this; + } + + @NotNull + @Pattern(regexp = "^[a-z0-9_]{3,32}$") + @Schema(name = "username", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("username") + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public UserDTO displayName(String displayName) { + this.displayName = displayName; + return this; + } + + @NotNull + @CodePointLength(min = 1, max = 100) + @Schema(name = "display_name", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("display_name") + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public UserDTO createdAt(Instant createdAt) { + this.createdAt = createdAt; + return this; + } + + @NotNull + @Schema(name = "created_at", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("created_at") + public Instant getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + UserDTO userDTO = (UserDTO) o; + return Objects.equals(this.id, userDTO.id) + && Objects.equals(this.username, userDTO.username) + && Objects.equals(this.displayName, userDTO.displayName) + && Objects.equals(this.createdAt, userDTO.createdAt); + } + + @Override + public int hashCode() { + return Objects.hash(id, username, displayName, createdAt); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class UserDTO {\n"); + sb.append(" id: ").append(toIndentedString(id)).append("\n"); + sb.append(" username: ").append(toIndentedString(username)).append("\n"); + sb.append(" displayName: ").append(toIndentedString(displayName)).append("\n"); + sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/dto/VariantDTO.java b/src/main/java/ru/mtusi/messenger/dto/VariantDTO.java new file mode 100644 index 0000000..f5ab84f --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/dto/VariantDTO.java @@ -0,0 +1,258 @@ +package ru.mtusi.messenger.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; + +import io.swagger.v3.oas.annotations.media.Schema; + +import jakarta.annotation.Generated; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; + +import java.util.Objects; + +@Generated( + value = "org.openapitools.codegen.languages.SpringCodegen", + comments = "Generator version: 7.10.0") +public class VariantDTO { + + public enum NameEnum { + THUMBNAIL("thumbnail"), + CROP("crop"), + OPTIMIZED("optimized"); + + private final String value; + + NameEnum(String value) { + this.value = value; + } + + @JsonValue + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static NameEnum fromValue(String value) { + for (NameEnum b : NameEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + private NameEnum name; + + public enum ContentTypeEnum { + IMAGE_JPEG("image/jpeg"), + IMAGE_PNG("image/png"), + IMAGE_WEBP("image/webp"); + + private final String value; + + ContentTypeEnum(String value) { + this.value = value; + } + + @JsonValue + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static ContentTypeEnum fromValue(String value) { + for (ContentTypeEnum b : ContentTypeEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + private ContentTypeEnum contentType; + + private Long sizeBytes; + + private Integer width; + + private Integer height; + + private String sha256; + + public VariantDTO() { + super(); + } + + public VariantDTO( + NameEnum name, + ContentTypeEnum contentType, + Long sizeBytes, + Integer width, + Integer height, + String sha256) { + this.name = name; + this.contentType = contentType; + this.sizeBytes = sizeBytes; + this.width = width; + this.height = height; + this.sha256 = sha256; + } + + public VariantDTO name(NameEnum name) { + this.name = name; + return this; + } + + @NotNull + @Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("name") + public NameEnum getName() { + return name; + } + + public void setName(NameEnum name) { + this.name = name; + } + + public VariantDTO contentType(ContentTypeEnum contentType) { + this.contentType = contentType; + return this; + } + + @NotNull + @Schema(name = "content_type", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("content_type") + public ContentTypeEnum getContentType() { + return contentType; + } + + public void setContentType(ContentTypeEnum contentType) { + this.contentType = contentType; + } + + public VariantDTO sizeBytes(Long sizeBytes) { + this.sizeBytes = sizeBytes; + return this; + } + + @NotNull + @Min(1L) + @Schema(name = "size_bytes", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("size_bytes") + public Long getSizeBytes() { + return sizeBytes; + } + + public void setSizeBytes(Long sizeBytes) { + this.sizeBytes = sizeBytes; + } + + public VariantDTO width(Integer width) { + this.width = width; + return this; + } + + @NotNull + @Min(1) + @Schema(name = "width", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("width") + public Integer getWidth() { + return width; + } + + public void setWidth(Integer width) { + this.width = width; + } + + public VariantDTO height(Integer height) { + this.height = height; + return this; + } + + @NotNull + @Min(1) + @Schema(name = "height", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("height") + public Integer getHeight() { + return height; + } + + public void setHeight(Integer height) { + this.height = height; + } + + public VariantDTO sha256(String sha256) { + this.sha256 = sha256; + return this; + } + + @NotNull + @Pattern(regexp = "^[a-f0-9]{64}$") + @Schema(name = "sha256", requiredMode = Schema.RequiredMode.REQUIRED) + @JsonProperty("sha256") + public String getSha256() { + return sha256; + } + + public void setSha256(String sha256) { + this.sha256 = sha256; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + VariantDTO variant = (VariantDTO) o; + return Objects.equals(this.name, variant.name) + && Objects.equals(this.contentType, variant.contentType) + && Objects.equals(this.sizeBytes, variant.sizeBytes) + && Objects.equals(this.width, variant.width) + && Objects.equals(this.height, variant.height) + && Objects.equals(this.sha256, variant.sha256); + } + + @Override + public int hashCode() { + return Objects.hash(name, contentType, sizeBytes, width, height, sha256); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class VariantDTO {\n"); + sb.append(" name: ").append(toIndentedString(name)).append("\n"); + sb.append(" contentType: ").append(toIndentedString(contentType)).append("\n"); + sb.append(" sizeBytes: ").append(toIndentedString(sizeBytes)).append("\n"); + sb.append(" width: ").append(toIndentedString(width)).append("\n"); + sb.append(" height: ").append(toIndentedString(height)).append("\n"); + sb.append(" sha256: ").append(toIndentedString(sha256)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/ApiErrorFactory.java b/src/main/java/ru/mtusi/messenger/exception/ApiErrorFactory.java new file mode 100644 index 0000000..9d9efc8 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/ApiErrorFactory.java @@ -0,0 +1,80 @@ +package ru.mtusi.messenger.exception; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import lombok.RequiredArgsConstructor; + +import org.slf4j.MDC; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; + +import ru.mtusi.messenger.dto.ErrorDTO; +import ru.mtusi.messenger.dto.ErrorErrorDTO; + +import tools.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.UUID; + +@Component +@RequiredArgsConstructor +public class ApiErrorFactory { + private static final String DEFAULT_MESSAGE = "Error"; + private static final String REQUEST_ID_ATTR = "requestId"; + private static final String REQUEST_ID_MDC = "requestId"; + + private final ObjectMapper objectMapper; + + public ErrorDTO error(ErrorErrorDTO.CodeEnum code, String message, HttpServletRequest request) { + ErrorErrorDTO inner = new ErrorErrorDTO(); + inner.setCode(code); + inner.setMessage(normalizeMessage(message)); + inner.setRequestId(resolveRequestId(request)); + + ErrorDTO outer = new ErrorDTO(); + outer.setError(inner); + return outer; + } + + public void writeError( + HttpServletResponse response, + HttpServletRequest request, + HttpStatus status, + String wwwAuthenticate, + ErrorErrorDTO.CodeEnum code, + String message) + throws IOException { + String requestId = resolveRequestId(request); + response.setStatus(status.value()); + response.setHeader("X-Request-Id", requestId); + if (wwwAuthenticate != null) { + response.setHeader(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticate); + } + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + objectMapper.writeValue(response.getWriter(), error(code, message, request)); + } + + public String resolveRequestId(HttpServletRequest request) { + Object attr = request.getAttribute(REQUEST_ID_ATTR); + if (attr instanceof String s && !s.isBlank()) { + return s; + } + String mdc = MDC.get(REQUEST_ID_MDC); + if (mdc != null && !mdc.isBlank()) { + request.setAttribute(REQUEST_ID_ATTR, mdc); + return mdc; + } + String generated = UUID.randomUUID().toString(); + request.setAttribute(REQUEST_ID_ATTR, generated); + return generated; + } + + private String normalizeMessage(String message) { + return (message == null || message.isBlank()) ? DEFAULT_MESSAGE : message; + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/ConflictException.java b/src/main/java/ru/mtusi/messenger/exception/ConflictException.java new file mode 100644 index 0000000..aedbacb --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/ConflictException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class ConflictException extends RuntimeException { + public ConflictException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/InvalidRequestException.java b/src/main/java/ru/mtusi/messenger/exception/InvalidRequestException.java new file mode 100644 index 0000000..3b6ab25 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/InvalidRequestException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class InvalidRequestException extends RuntimeException { + public InvalidRequestException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/NotFoundException.java b/src/main/java/ru/mtusi/messenger/exception/NotFoundException.java new file mode 100644 index 0000000..ebd7eca --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/NotFoundException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class NotFoundException extends RuntimeException { + public NotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/PayloadTooLargeException.java b/src/main/java/ru/mtusi/messenger/exception/PayloadTooLargeException.java new file mode 100644 index 0000000..1dab465 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/PayloadTooLargeException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class PayloadTooLargeException extends RuntimeException { + public PayloadTooLargeException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/RateLimitedException.java b/src/main/java/ru/mtusi/messenger/exception/RateLimitedException.java new file mode 100644 index 0000000..7d2b891 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/RateLimitedException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class RateLimitedException extends RuntimeException { + public RateLimitedException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/ServiceUnavailableException.java b/src/main/java/ru/mtusi/messenger/exception/ServiceUnavailableException.java new file mode 100644 index 0000000..c59fa53 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/ServiceUnavailableException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class ServiceUnavailableException extends RuntimeException { + public ServiceUnavailableException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/StaleAttachmentException.java b/src/main/java/ru/mtusi/messenger/exception/StaleAttachmentException.java new file mode 100644 index 0000000..e5a488d --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/StaleAttachmentException.java @@ -0,0 +1,11 @@ +package ru.mtusi.messenger.exception; + +public class StaleAttachmentException extends RuntimeException { + public StaleAttachmentException(String message) { + super(message); + } + + public StaleAttachmentException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/StorageUnavailableException.java b/src/main/java/ru/mtusi/messenger/exception/StorageUnavailableException.java new file mode 100644 index 0000000..70d92ce --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/StorageUnavailableException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class StorageUnavailableException extends RuntimeException { + public StorageUnavailableException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/UnauthorizedException.java b/src/main/java/ru/mtusi/messenger/exception/UnauthorizedException.java new file mode 100644 index 0000000..6f79b51 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/UnauthorizedException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class UnauthorizedException extends RuntimeException { + public UnauthorizedException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/exception/UnsupportedMediaTypeException.java b/src/main/java/ru/mtusi/messenger/exception/UnsupportedMediaTypeException.java new file mode 100644 index 0000000..6dcd389 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/exception/UnsupportedMediaTypeException.java @@ -0,0 +1,7 @@ +package ru.mtusi.messenger.exception; + +public class UnsupportedMediaTypeException extends RuntimeException { + public UnsupportedMediaTypeException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/mtusi/messenger/filter/InstanceIdFilter.java b/src/main/java/ru/mtusi/messenger/filter/InstanceIdFilter.java new file mode 100644 index 0000000..5c88ff2 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/filter/InstanceIdFilter.java @@ -0,0 +1,33 @@ +package ru.mtusi.messenger.filter; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import lombok.RequiredArgsConstructor; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import ru.mtusi.messenger.config.properties.InstanceProperties; + +import java.io.IOException; + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +@RequiredArgsConstructor +public class InstanceIdFilter extends OncePerRequestFilter { + + private final InstanceProperties props; + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + response.setHeader("X-Instance-Id", props.id()); + chain.doFilter(request, response); + } +} diff --git a/src/main/java/ru/mtusi/messenger/filter/RateLimitFilter.java b/src/main/java/ru/mtusi/messenger/filter/RateLimitFilter.java new file mode 100644 index 0000000..5fdae8a --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/filter/RateLimitFilter.java @@ -0,0 +1,54 @@ +package ru.mtusi.messenger.filter; + +import io.github.bucket4j.Bucket; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import lombok.RequiredArgsConstructor; + +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import ru.mtusi.messenger.config.properties.RateLimitProperties; +import ru.mtusi.messenger.dto.ErrorErrorDTO; +import ru.mtusi.messenger.exception.ApiErrorFactory; + +import java.io.IOException; + +@Component +@Order(2) +@RequiredArgsConstructor +public class RateLimitFilter extends OncePerRequestFilter { + private final RateLimitProperties limitProperties; + private final Bucket bucket; + private final ApiErrorFactory errors; + + @Override + protected void doFilterInternal( + HttpServletRequest req, HttpServletResponse res, FilterChain chain) + throws ServletException, IOException { + if (!req.getRequestURI().startsWith("/api/")) { + chain.doFilter(req, res); + return; + } + + if (bucket.tryConsume(1)) { + chain.doFilter(req, res); + return; + } + + res.setHeader("Retry-After", String.valueOf(limitProperties.retryAfterSeconds())); + errors.writeError( + res, + req, + HttpStatus.TOO_MANY_REQUESTS, + null, + ErrorErrorDTO.CodeEnum.RATE_LIMITED, + "Rate limit exceeded. Try again later."); + } +} diff --git a/src/main/java/ru/mtusi/messenger/filter/RequestIdFilter.java b/src/main/java/ru/mtusi/messenger/filter/RequestIdFilter.java new file mode 100644 index 0000000..8af5c1b --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/filter/RequestIdFilter.java @@ -0,0 +1,35 @@ +package ru.mtusi.messenger.filter; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.slf4j.MDC; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import java.io.IOException; +import java.util.UUID; + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE + 1) +public class RequestIdFilter extends OncePerRequestFilter { + + @Override + protected void doFilterInternal( + HttpServletRequest req, HttpServletResponse res, FilterChain chain) + throws ServletException, IOException { + String rid = UUID.randomUUID().toString(); + res.setHeader("X-Request-Id", rid); + req.setAttribute("requestId", rid); + MDC.put("requestId", rid); + try { + chain.doFilter(req, res); + } finally { + MDC.clear(); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/filter/TelemetryFilter.java b/src/main/java/ru/mtusi/messenger/filter/TelemetryFilter.java new file mode 100644 index 0000000..15879f3 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/filter/TelemetryFilter.java @@ -0,0 +1,122 @@ +package ru.mtusi.messenger.filter; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.StatusCode; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; +import org.springframework.web.servlet.HandlerMapping; + +import ru.mtusi.messenger.telemetry.Telemetry; + +import java.io.IOException; +import java.util.List; +import java.util.Set; + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE + 3) +public class TelemetryFilter extends OncePerRequestFilter { + private static final Set METHODS = + Set.of("GET", "POST", "DELETE", "PUT", "PATCH", "HEAD", "OPTIONS"); + private static final Set ROUTES = + Set.of( + "/api/v1/users", + "/api/v1/users/me", + "/api/v1/auth/sessions", + "/api/v1/auth/sessions/current", + "/api/v1/chats", + "/api/v1/chats/{chat_id}", + "/api/v1/chats/{chat_id}/messages", + "/api/v1/chats/{chat_id}/attachments", + "/api/v1/attachments/{attachment_id}", + "/api/v1/attachments/{attachment_id}/content"); + + private final Telemetry telemetry; + private final LongCounter requests; + private final DoubleHistogram duration; + + public TelemetryFilter(Telemetry telemetry) { + this.telemetry = telemetry; + var meter = telemetry.openTelemetry().getMeter("ru.mtusi.messenger"); + requests = meter.counterBuilder("lab_http_requests").build(); + duration = + meter.histogramBuilder("lab_http_duration") + .setUnit("s") + .setExplicitBucketBoundariesAdvice( + List.of( + 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0, + 10.0, 30.0)) + .build(); + } + + @Override + protected boolean shouldNotFilter(HttpServletRequest request) { + return !request.getRequestURI().startsWith("/api/v1/"); + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + long started = System.nanoTime(); + Span span = telemetry.server(request); + try (var scope = span.makeCurrent()) { + int status = 500; + try { + chain.doFilter(request, response); + status = response.getStatus(); + } finally { + record(request, response, span, status, (System.nanoTime() - started) / 1e9); + } + } finally { + span.end(); + } + } + + private void record( + HttpServletRequest request, + HttpServletResponse response, + Span span, + int status, + double seconds) { + String method = METHODS.contains(request.getMethod()) ? request.getMethod() : "OTHER"; + String pattern = + String.valueOf( + request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)); + String route = ROUTES.contains(pattern) ? pattern : "unmatched"; + Attributes labels = + Attributes.builder() + .put("method", method) + .put("route", route) + .put("status_class", status / 100 + "xx") + .build(); + requests.add(1, labels); + duration.record(seconds, labels); + span.updateName(method + " " + route); + span.setAllAttributes(labels); + span.setAttribute("http.response.status_code", status); + if (status >= 500) { + span.setStatus(StatusCode.ERROR); + } + String id = response.getHeader("X-Request-Id"); + Attributes log = + labels.toBuilder() + .put("event", "http.completed") + .put("http.response.status_code", status) + .put("duration_seconds", seconds) + .put(AttributeKey.stringKey("request_id"), id == null ? "" : id) + .build(); + telemetry.log("http.completed", status >= 500, log); + } +} diff --git a/src/main/java/ru/mtusi/messenger/mapper/AttachmentMapper.java b/src/main/java/ru/mtusi/messenger/mapper/AttachmentMapper.java new file mode 100644 index 0000000..6c1614e --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/mapper/AttachmentMapper.java @@ -0,0 +1,64 @@ +package ru.mtusi.messenger.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Named; + +import ru.mtusi.messenger.dto.AttachmentDTO; +import ru.mtusi.messenger.model.Attachment; +import ru.mtusi.messenger.model.AttachmentStatus; +import ru.mtusi.messenger.model.Chat; +import ru.mtusi.messenger.model.User; + +import java.util.Collections; + +@Mapper(componentModel = "spring") +public interface AttachmentMapper { + @Mapping(target = "chatId", source = "chat.id") + @Mapping(target = "ownerId", source = "owner.id") + @Mapping(target = "sha256", source = "checksum") + @Mapping(target = "status", source = "status", qualifiedByName = "mapStatus") + @Mapping(target = "contentType", source = "contentType", qualifiedByName = "mapContentType") + @Mapping(target = "variants", expression = "java(emptyVariants())") + @Mapping(target = "errorCode", source = "errorCode") + AttachmentDTO toDto(Attachment entity); + + @Named("mapStatus") + default AttachmentDTO.StatusEnum mapStatus(AttachmentStatus status) { + return switch (status) { + case QUEUED -> AttachmentDTO.StatusEnum.QUEUED; + case PROCESSING -> AttachmentDTO.StatusEnum.PROCESSING; + case READY -> AttachmentDTO.StatusEnum.READY; + default -> AttachmentDTO.StatusEnum.FAILED; + }; + } + + @Named("mapContentType") + default AttachmentDTO.ContentTypeEnum mapContentType(String contentType) { + if (contentType == null) { + return null; + } + return AttachmentDTO.ContentTypeEnum.fromValue(contentType); + } + + default java.util.List emptyVariants() { + return Collections.emptyList(); + } + + @Mapping(target = "id", ignore = true) + @Mapping(target = "createdAt", ignore = true) + @Mapping(target = "updatedAt", ignore = true) + @Mapping(target = "status", constant = "QUEUED") + @Mapping(target = "objectKey", source = "objectKey") + @Mapping(target = "stagingKey", source = "objectKey") + @Mapping(target = "errorCode", ignore = true) + @Mapping(target = "pipelineVersion", constant = "1L") + Attachment toEntity( + String objectKey, + String filename, + String contentType, + Long sizeBytes, + String checksum, + Chat chat, + User owner); +} diff --git a/src/main/java/ru/mtusi/messenger/mapper/ChatMapper.java b/src/main/java/ru/mtusi/messenger/mapper/ChatMapper.java new file mode 100644 index 0000000..79234ec --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/mapper/ChatMapper.java @@ -0,0 +1,36 @@ +package ru.mtusi.messenger.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Named; + +import ru.mtusi.messenger.dto.ChatDTO; +import ru.mtusi.messenger.dto.CreateChatDTO; +import ru.mtusi.messenger.model.Chat; +import ru.mtusi.messenger.model.User; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +@Mapper(componentModel = "spring") +public interface ChatMapper { + + @Mapping(target = "id", ignore = true) + @Mapping(target = "createdAt", ignore = true) + @Mapping(target = "members", ignore = true) + @Mapping(target = "messages", ignore = true) + Chat toChat(CreateChatDTO dto); + + @Mapping(target = "memberIds", source = "members", qualifiedByName = "usersToIds") + ChatDTO toChatDto(Chat chat); + + @Named("usersToIds") + default List usersToIds(Set users) { + if (users == null) return new ArrayList<>(); + List ids = new ArrayList<>(users.size()); + for (User u : users) ids.add(u.getId()); + return ids; + } +} diff --git a/src/main/java/ru/mtusi/messenger/mapper/MessageMapper.java b/src/main/java/ru/mtusi/messenger/mapper/MessageMapper.java new file mode 100644 index 0000000..afb0c04 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/mapper/MessageMapper.java @@ -0,0 +1,31 @@ +package ru.mtusi.messenger.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +import ru.mtusi.messenger.dto.CreateMessageDTO; +import ru.mtusi.messenger.dto.MessageDTO; +import ru.mtusi.messenger.model.Message; + +@Mapper(componentModel = "spring") +public interface MessageMapper { + + @Mapping(target = "id", ignore = true) + @Mapping(target = "chat", ignore = true) + @Mapping(target = "sender", ignore = true) + @Mapping(target = "createdAt", ignore = true) + @Mapping(target = "attachmentIds", ignore = true) + Message toMessage(MessageDTO dto); + + @Mapping(target = "chatId", source = "chat.id") + @Mapping(target = "senderId", source = "sender.id") + @Mapping(target = "attachmentIds", source = "attachmentIds") + MessageDTO toMessageDTO(Message entity); + + @Mapping(target = "id", ignore = true) + @Mapping(target = "chat", ignore = true) + @Mapping(target = "sender", ignore = true) + @Mapping(target = "createdAt", ignore = true) + @Mapping(target = "attachmentIds", source = "attachmentIds") + Message toMessage(CreateMessageDTO dto); +} diff --git a/src/main/java/ru/mtusi/messenger/mapper/SessionMapper.java b/src/main/java/ru/mtusi/messenger/mapper/SessionMapper.java new file mode 100644 index 0000000..cbc0727 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/mapper/SessionMapper.java @@ -0,0 +1,20 @@ +package ru.mtusi.messenger.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +import ru.mtusi.messenger.dto.SessionDTO; +import ru.mtusi.messenger.dto.UserDTO; +import ru.mtusi.messenger.model.Session; + +@Mapper(componentModel = "spring") +public interface SessionMapper { + + @Mapping(target = "token", source = "tokenRaw") + @Mapping( + target = "tokenType", + expression = "java(ru.mtusi.messenger.dto.SessionDTO.TokenTypeEnum.BEARER)") + @Mapping(target = "expiresAt", source = "session.expiresAt") + @Mapping(target = "user", source = "userDTO") + SessionDTO toSessionDTO(Session session, String tokenRaw, UserDTO userDTO); +} diff --git a/src/main/java/ru/mtusi/messenger/mapper/UserMapper.java b/src/main/java/ru/mtusi/messenger/mapper/UserMapper.java new file mode 100644 index 0000000..d232ac7 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/mapper/UserMapper.java @@ -0,0 +1,21 @@ +package ru.mtusi.messenger.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +import ru.mtusi.messenger.dto.CreateUserDTO; +import ru.mtusi.messenger.dto.UserDTO; +import ru.mtusi.messenger.model.User; + +@Mapper(componentModel = "spring") +public interface UserMapper { + + @Mapping(target = "id", ignore = true) + @Mapping(target = "createdAt", ignore = true) + @Mapping(target = "passwordHash", ignore = true) + @Mapping(target = "chats", ignore = true) + @Mapping(target = "messages", ignore = true) + User toUser(CreateUserDTO dto); + + UserDTO toUserDto(User user); +} diff --git a/src/main/java/ru/mtusi/messenger/model/Attachment.java b/src/main/java/ru/mtusi/messenger/model/Attachment.java new file mode 100644 index 0000000..7a0f84a --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/model/Attachment.java @@ -0,0 +1,87 @@ +package ru.mtusi.messenger.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.PrePersist; +import jakarta.persistence.PreUpdate; +import jakarta.persistence.Table; + +import lombok.Getter; +import lombok.Setter; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +@Entity +@Table(name = "attachments") +@Getter +@Setter +public class Attachment { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @Column(name = "object_key", nullable = false, unique = true, length = 512) + private String objectKey; + + @Column(nullable = false, length = 255) + private String filename; + + @Column(name = "content_type", nullable = false, length = 100) + private String contentType; + + @Column(name = "size_bytes", nullable = false) + private Long sizeBytes; + + @Column(nullable = false, length = 64) + private String checksum; + + @Enumerated(EnumType.STRING) + @Column(nullable = false, length = 20) + private AttachmentStatus status; + + @Column(name = "staging_key", length = 512) + private String stagingKey; + + @Column(name = "error_code", length = 64) + private String errorCode; + + @Column(name = "pipeline_version", nullable = false) + private long pipelineVersion = 1; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "chat_id", nullable = false) + private Chat chat; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "owner_id", nullable = false) + private User owner; + + @Column(name = "created_at", nullable = false) + private Instant createdAt; + + @Column(name = "updated_at", nullable = false) + private Instant updatedAt; + + @PrePersist + private void onCreate() { + Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS); + this.createdAt = now; + this.updatedAt = now; + } + + @PreUpdate + private void onUpdate() { + this.updatedAt = Instant.now().truncatedTo(ChronoUnit.MILLIS); + } +} diff --git a/src/main/java/ru/mtusi/messenger/model/AttachmentStatus.java b/src/main/java/ru/mtusi/messenger/model/AttachmentStatus.java new file mode 100644 index 0000000..b9e9e53 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/model/AttachmentStatus.java @@ -0,0 +1,10 @@ +package ru.mtusi.messenger.model; + +public enum AttachmentStatus { + UPLOADING, + QUEUED, + PROCESSING, + READY, + FAILED, + BROKEN +} diff --git a/src/main/java/ru/mtusi/messenger/model/Chat.java b/src/main/java/ru/mtusi/messenger/model/Chat.java new file mode 100644 index 0000000..98bd9a2 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/model/Chat.java @@ -0,0 +1,67 @@ +package ru.mtusi.messenger.model; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; +import jakarta.persistence.PrePersist; +import jakarta.persistence.Table; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +@Entity +@Table(name = "chats") +@Getter +@Setter +@NoArgsConstructor +public class Chat { + + @Id + @Column(nullable = false, updatable = false) + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @Column(nullable = false, length = 100) + private String title; + + @Column(name = "created_at", nullable = false, updatable = false) + private Instant createdAt; + + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable( + name = "chat_members", + joinColumns = @JoinColumn(name = "chat_id"), + inverseJoinColumns = @JoinColumn(name = "user_id")) + private Set members = new HashSet<>(); + + @OneToMany( + mappedBy = "chat", + fetch = FetchType.LAZY, + cascade = CascadeType.ALL, + orphanRemoval = true) + @OrderBy("createdAt ASC, id ASC") + private List messages = new ArrayList<>(); + + @PrePersist + private void onCreate() { + this.createdAt = Instant.now().truncatedTo(ChronoUnit.MILLIS); + } +} diff --git a/src/main/java/ru/mtusi/messenger/model/IdempotencyKey.java b/src/main/java/ru/mtusi/messenger/model/IdempotencyKey.java new file mode 100644 index 0000000..da767ed --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/model/IdempotencyKey.java @@ -0,0 +1,54 @@ +package ru.mtusi.messenger.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +import lombok.Getter; +import lombok.Setter; + +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; + +import java.time.Instant; +import java.util.UUID; + +@Entity +@Table(name = "idempotency_keys") +@Getter +@Setter +public class IdempotencyKey { + + @Id + @Column(nullable = false, updatable = false) + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @Column(name = "user_id", nullable = false) + private UUID userId; + + @Column(nullable = false, length = 16) + private String method; + + @Column(name = "chat_id", nullable = false) + private UUID chatId; + + @Column(name = "idempotency_key", nullable = false, length = 128) + private String idempotencyKey; + + @Column(name = "request_hash", nullable = false, length = 64) + private String requestHash; + + @Column(name = "status_code") + private Short statusCode; + + @JdbcTypeCode(SqlTypes.JSON) + @Column(name = "response_body", columnDefinition = "jsonb") + private String responseBody; + + @Column(name = "created_at", nullable = false) + private Instant createdAt; +} diff --git a/src/main/java/ru/mtusi/messenger/model/Message.java b/src/main/java/ru/mtusi/messenger/model/Message.java new file mode 100644 index 0000000..e3ab7fe --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/model/Message.java @@ -0,0 +1,63 @@ +package ru.mtusi.messenger.model; + +import jakarta.persistence.CollectionTable; +import jakarta.persistence.Column; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OrderColumn; +import jakarta.persistence.PrePersist; +import jakarta.persistence.Table; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Entity +@Table(name = "messages") +@Getter +@Setter +@NoArgsConstructor +public class Message { + + @Id + @Column(nullable = false, updatable = false) + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "chat_id", nullable = false) + private Chat chat; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "sender_id", nullable = false) + private User sender; + + @Column(nullable = false, length = 4000) + private String text; + + @Column(name = "created_at", nullable = false, updatable = false) + private Instant createdAt; + + @ElementCollection + @CollectionTable(name = "message_attachments", joinColumns = @JoinColumn(name = "message_id")) + @Column(name = "attachment_id") + @OrderColumn(name = "attachment_order") + private List attachmentIds = new ArrayList<>(); + + @PrePersist + private void onCreate() { + this.createdAt = Instant.now().truncatedTo(ChronoUnit.MILLIS); + } +} diff --git a/src/main/java/ru/mtusi/messenger/model/Session.java b/src/main/java/ru/mtusi/messenger/model/Session.java new file mode 100644 index 0000000..d004bc1 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/model/Session.java @@ -0,0 +1,48 @@ +package ru.mtusi.messenger.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.PrePersist; +import jakarta.persistence.Table; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +@Entity +@Table(name = "sessions") +@Getter +@Setter +@NoArgsConstructor +public class Session { + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @Column(name = "user_id", nullable = false) + private UUID userId; + + @Column(name = "token_hash", nullable = false, unique = true) + private byte[] tokenHash; + + @Column(name = "created_at", nullable = false) + private Instant createdAt; + + @Column(name = "expires_at", nullable = false) + private Instant expiresAt; + + @Column(name = "revoked_at") + private Instant revokedAt; + + @PrePersist + private void onCreate() { + this.createdAt = Instant.now().truncatedTo(ChronoUnit.MILLIS); + } +} diff --git a/src/main/java/ru/mtusi/messenger/model/User.java b/src/main/java/ru/mtusi/messenger/model/User.java new file mode 100644 index 0000000..d5f7e47 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/model/User.java @@ -0,0 +1,60 @@ +package ru.mtusi.messenger.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; +import jakarta.persistence.PrePersist; +import jakarta.persistence.Table; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +@Entity +@Table(name = "users") +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class User { + + @Id() + @Column(nullable = false, updatable = false) + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + + @Column(nullable = false, unique = true, length = 32) + private String username; + + @Column(name = "display_name", nullable = false, length = 100) + private String displayName; + + @Column(name = "password_hash", nullable = false, length = 255) + private String passwordHash; + + @Column(name = "created_at", nullable = false, updatable = false) + private Instant createdAt; + + @ManyToMany(mappedBy = "members", fetch = FetchType.LAZY) + private Set chats = new HashSet<>(); + + @OneToMany(mappedBy = "sender", fetch = FetchType.LAZY) + private Set messages = new HashSet<>(); + + @PrePersist + private void onCreate() { + this.createdAt = Instant.now().truncatedTo(ChronoUnit.MILLIS); + } +} diff --git a/src/main/java/ru/mtusi/messenger/repository/AttachmentRepository.java b/src/main/java/ru/mtusi/messenger/repository/AttachmentRepository.java new file mode 100644 index 0000000..b53b036 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/repository/AttachmentRepository.java @@ -0,0 +1,18 @@ +package ru.mtusi.messenger.repository; + +import jakarta.persistence.LockModeType; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Lock; +import org.springframework.data.jpa.repository.Query; + +import ru.mtusi.messenger.model.Attachment; + +import java.util.Optional; +import java.util.UUID; + +public interface AttachmentRepository extends JpaRepository { + @Lock(LockModeType.PESSIMISTIC_READ) + @Query("select attachment from Attachment attachment where attachment.id = :id") + Optional findForMessage(UUID id); +} diff --git a/src/main/java/ru/mtusi/messenger/repository/ChatRepository.java b/src/main/java/ru/mtusi/messenger/repository/ChatRepository.java new file mode 100644 index 0000000..9e6d58f --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/repository/ChatRepository.java @@ -0,0 +1,52 @@ +package ru.mtusi.messenger.repository; + +import jakarta.persistence.LockModeType; + +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Lock; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import ru.mtusi.messenger.model.Chat; + +import java.time.Instant; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface ChatRepository extends JpaRepository { + + @Lock(LockModeType.PESSIMISTIC_WRITE) + @Query("select c from Chat c where c.id = :chatId") + Optional findForMessageCreation(@Param("chatId") UUID chatId); + + @EntityGraph(attributePaths = "members") + @Query( + """ + select distinct c from Chat c + join c.members m + where m.id = :userId + order by c.createdAt asc, c.id asc + """) + List findPageByMember(@Param("userId") UUID userId, Pageable pageable); + + @EntityGraph(attributePaths = "members") + @Query( + """ + select distinct c from Chat c + join c.members m + where m.id = :userId + and (c.createdAt > :cursorCreated + or (c.createdAt = :cursorCreated and c.id > :cursorId)) + order by c.createdAt asc, c.id asc + """) + List findPageByMemberAfterCursor( + @Param("userId") UUID userId, + @Param("cursorCreated") Instant cursorCreated, + @Param("cursorId") UUID cursorId, + Pageable pageable); + + boolean existsByIdAndMembersId(UUID chatId, UUID userId); +} diff --git a/src/main/java/ru/mtusi/messenger/repository/IdempotencyRepository.java b/src/main/java/ru/mtusi/messenger/repository/IdempotencyRepository.java new file mode 100644 index 0000000..413f887 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/repository/IdempotencyRepository.java @@ -0,0 +1,46 @@ +package ru.mtusi.messenger.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import ru.mtusi.messenger.model.IdempotencyKey; + +import java.util.Optional; +import java.util.UUID; + +@Repository +public interface IdempotencyRepository extends JpaRepository { + + @Modifying(clearAutomatically = true, flushAutomatically = true) + @Query( + value = + """ + INSERT INTO idempotency_keys + (user_id, method, chat_id, idempotency_key, request_hash, created_at) + VALUES (:userId, :method, :chatId, :key, :hash, NOW()) + ON CONFLICT (user_id, method, chat_id, idempotency_key) DO NOTHING + """, + nativeQuery = true) + int tryClaim(UUID userId, String method, UUID chatId, String key, String hash); + + @Modifying(clearAutomatically = true, flushAutomatically = true) + @Query( + value = + """ + UPDATE idempotency_keys + SET status_code = :statusCode, + response_body = CAST(:body AS jsonb) + WHERE user_id = :userId + AND method = :method + AND chat_id = :chatId + AND idempotency_key = :key + """, + nativeQuery = true) + int commitResponse( + UUID userId, String method, UUID chatId, String key, int statusCode, String body); + + Optional findByUserIdAndMethodAndChatIdAndIdempotencyKey( + UUID userId, String method, UUID chatId, String idempotencyKey); +} diff --git a/src/main/java/ru/mtusi/messenger/repository/MessageRepository.java b/src/main/java/ru/mtusi/messenger/repository/MessageRepository.java new file mode 100644 index 0000000..f0bdf83 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/repository/MessageRepository.java @@ -0,0 +1,42 @@ +package ru.mtusi.messenger.repository; + +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import ru.mtusi.messenger.model.Message; + +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +public interface MessageRepository extends JpaRepository { + + long countByChatId(UUID chatId); + + @EntityGraph(attributePaths = "sender") + @Query( + """ + select m from Message m + where m.chat.id = :chatId + order by m.createdAt asc, m.id asc + """) + List findPageByChat(@Param("chatId") UUID chatId, Pageable pageable); + + @EntityGraph(attributePaths = "sender") + @Query( + """ + select m from Message m + where m.chat.id = :chatId + and (m.createdAt > :cursorCreated + or (m.createdAt = :cursorCreated and m.id > :cursorId)) + order by m.createdAt asc, m.id asc + """) + List findPageByChatAfterCursor( + @Param("chatId") UUID chatId, + @Param("cursorCreated") Instant cursorCreated, + @Param("cursorId") UUID cursorId, + Pageable pageable); +} diff --git a/src/main/java/ru/mtusi/messenger/repository/SessionRepository.java b/src/main/java/ru/mtusi/messenger/repository/SessionRepository.java new file mode 100644 index 0000000..b4ea902 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/repository/SessionRepository.java @@ -0,0 +1,12 @@ +package ru.mtusi.messenger.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import ru.mtusi.messenger.model.Session; + +import java.util.Optional; +import java.util.UUID; + +public interface SessionRepository extends JpaRepository { + Optional findByTokenHash(byte[] tokenHash); +} diff --git a/src/main/java/ru/mtusi/messenger/repository/UserRepository.java b/src/main/java/ru/mtusi/messenger/repository/UserRepository.java new file mode 100644 index 0000000..73e3304 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/repository/UserRepository.java @@ -0,0 +1,23 @@ +package ru.mtusi.messenger.repository; + +import jakarta.persistence.LockModeType; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Lock; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import ru.mtusi.messenger.model.User; + +import java.util.Optional; +import java.util.Set; +import java.util.UUID; + +public interface UserRepository extends JpaRepository { + + Optional findByUsername(String username); + + @Lock(LockModeType.PESSIMISTIC_READ) + @Query("select u from User u where u.id in :ids") + Set findAllByIdForShare(@Param("ids") Set ids); +} diff --git a/src/main/java/ru/mtusi/messenger/security/ApiAuthenticationEntryPoint.java b/src/main/java/ru/mtusi/messenger/security/ApiAuthenticationEntryPoint.java new file mode 100644 index 0000000..def07ff --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/security/ApiAuthenticationEntryPoint.java @@ -0,0 +1,43 @@ +package ru.mtusi.messenger.security; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import lombok.RequiredArgsConstructor; + +import org.springframework.http.HttpStatus; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.AuthenticationEntryPoint; +import org.springframework.stereotype.Component; + +import ru.mtusi.messenger.dto.ErrorErrorDTO; +import ru.mtusi.messenger.exception.ApiErrorFactory; + +import java.io.IOException; + +@Component +@RequiredArgsConstructor +public class ApiAuthenticationEntryPoint implements AuthenticationEntryPoint { + private final ApiErrorFactory errors; + + @Override + public void commence( + HttpServletRequest request, + HttpServletResponse response, + AuthenticationException authException) + throws IOException { + boolean isLogin = "/api/v1/auth/sessions".equals(request.getRequestURI()); + String challenge = + isLogin + ? "Basic realm=\"messenger\", charset=\"UTF-8\"" + : "Bearer realm=\"messenger\""; + + errors.writeError( + response, + request, + HttpStatus.UNAUTHORIZED, + challenge, + ErrorErrorDTO.CodeEnum.UNAUTHORIZED, + "No valid authentication"); + } +} diff --git a/src/main/java/ru/mtusi/messenger/security/BasicCredentials.java b/src/main/java/ru/mtusi/messenger/security/BasicCredentials.java new file mode 100644 index 0000000..bc5d423 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/security/BasicCredentials.java @@ -0,0 +1,29 @@ +package ru.mtusi.messenger.security; + +import ru.mtusi.messenger.exception.UnauthorizedException; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +public record BasicCredentials(String username, String password) { + + public static BasicCredentials parse(String header) { + if (header == null || !header.regionMatches(true, 0, "Basic ", 0, 6)) { + throw new UnauthorizedException("Basic authentication required"); + } + String decoded; + try { + decoded = + new String( + Base64.getDecoder().decode(header.substring(6).trim()), + StandardCharsets.UTF_8); + } catch (IllegalArgumentException e) { + throw new UnauthorizedException("Invalid Basic encoding"); + } + int i = decoded.indexOf(':'); + if (i < 0) { + throw new UnauthorizedException("Invalid Basic credentials"); + } + return new BasicCredentials(decoded.substring(0, i), decoded.substring(i + 1)); + } +} diff --git a/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationFilter.java b/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationFilter.java new file mode 100644 index 0000000..db44d8a --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationFilter.java @@ -0,0 +1,64 @@ +package ru.mtusi.messenger.security; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import lombok.RequiredArgsConstructor; + +import org.springframework.dao.DataAccessException; +import org.springframework.http.HttpStatus; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver; +import org.springframework.transaction.TransactionException; +import org.springframework.web.filter.OncePerRequestFilter; + +import ru.mtusi.messenger.dto.ErrorErrorDTO; +import ru.mtusi.messenger.exception.ApiErrorFactory; + +import java.io.IOException; + +@RequiredArgsConstructor +public class SessionAuthenticationFilter extends OncePerRequestFilter { + private final BearerTokenResolver bearerTokenResolver; + private final AuthenticationManager authenticationManager; + private final ApiErrorFactory errors; + + @Override + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws ServletException, IOException { + try { + String token = bearerTokenResolver.resolve(request); + if (token != null && !token.isBlank()) { + try { + Authentication auth = + authenticationManager.authenticate( + new SessionAuthenticationToken(token)); + SecurityContext context = SecurityContextHolder.createEmptyContext(); + context.setAuthentication(auth); + SecurityContextHolder.setContext(context); + } catch (DataAccessException | TransactionException e) { + errors.writeError( + response, + request, + HttpStatus.SERVICE_UNAVAILABLE, + null, + ErrorErrorDTO.CodeEnum.UNAVAILABLE, + "Session storage unavailable"); + return; + } catch (AuthenticationException e) { + SecurityContextHolder.clearContext(); + } + } + chain.doFilter(request, response); + } finally { + SecurityContextHolder.clearContext(); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationProvider.java b/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationProvider.java new file mode 100644 index 0000000..b15d1a7 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationProvider.java @@ -0,0 +1,36 @@ +package ru.mtusi.messenger.security; + +import lombok.RequiredArgsConstructor; + +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.stereotype.Component; + +import ru.mtusi.messenger.exception.UnauthorizedException; +import ru.mtusi.messenger.service.SessionService; + +@Component +@RequiredArgsConstructor +public class SessionAuthenticationProvider implements AuthenticationProvider { + private final SessionService sessionService; + + @Override + public Authentication authenticate(Authentication authentication) + throws AuthenticationException { + String token = (String) authentication.getCredentials(); + try { + var rec = sessionService.requireActive(token); + return new SessionAuthenticationToken( + new SessionPrincipal(rec.userId(), rec.sessionId())); + } catch (UnauthorizedException e) { + throw new BadCredentialsException(e.getMessage(), e); + } + } + + @Override + public boolean supports(Class authentication) { + return SessionAuthenticationToken.class.isAssignableFrom(authentication); + } +} diff --git a/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationToken.java b/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationToken.java new file mode 100644 index 0000000..05d6440 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/security/SessionAuthenticationToken.java @@ -0,0 +1,34 @@ +package ru.mtusi.messenger.security; + +import org.springframework.security.authentication.AbstractAuthenticationToken; + +import java.util.List; + +public class SessionAuthenticationToken extends AbstractAuthenticationToken { + private final String rawToken; + private final SessionPrincipal principal; + + public SessionAuthenticationToken(String rawToken) { + super(List.of()); + this.rawToken = rawToken; + this.principal = null; + setAuthenticated(false); + } + + public SessionAuthenticationToken(SessionPrincipal principal) { + super(List.of()); + this.rawToken = null; + this.principal = principal; + setAuthenticated(true); + } + + @Override + public Object getCredentials() { + return rawToken; + } + + @Override + public Object getPrincipal() { + return principal; + } +} diff --git a/src/main/java/ru/mtusi/messenger/security/SessionPrincipal.java b/src/main/java/ru/mtusi/messenger/security/SessionPrincipal.java new file mode 100644 index 0000000..444652c --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/security/SessionPrincipal.java @@ -0,0 +1,5 @@ +package ru.mtusi.messenger.security; + +import java.util.UUID; + +public record SessionPrincipal(UUID userId, UUID sessionId) {} diff --git a/src/main/java/ru/mtusi/messenger/service/AttachmentService.java b/src/main/java/ru/mtusi/messenger/service/AttachmentService.java new file mode 100644 index 0000000..051b530 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/AttachmentService.java @@ -0,0 +1,273 @@ +package ru.mtusi.messenger.service; + +import io.minio.GetObjectArgs; +import io.minio.MinioClient; +import io.minio.PutObjectArgs; +import io.minio.RemoveObjectArgs; + +import jakarta.servlet.http.HttpServletRequest; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.dao.DataAccessException; +import org.springframework.http.ContentDisposition; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.config.properties.S3Properties; +import ru.mtusi.messenger.dto.AttachmentDTO; +import ru.mtusi.messenger.exception.ConflictException; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.NotFoundException; +import ru.mtusi.messenger.exception.PayloadTooLargeException; +import ru.mtusi.messenger.exception.ServiceUnavailableException; +import ru.mtusi.messenger.exception.StorageUnavailableException; +import ru.mtusi.messenger.exception.UnsupportedMediaTypeException; +import ru.mtusi.messenger.mapper.AttachmentMapper; +import ru.mtusi.messenger.model.Attachment; +import ru.mtusi.messenger.model.AttachmentStatus; +import ru.mtusi.messenger.model.Chat; +import ru.mtusi.messenger.model.User; +import ru.mtusi.messenger.repository.AttachmentRepository; +import ru.mtusi.messenger.telemetry.Telemetry; +import ru.mtusi.messenger.validation.AttachmentValidator; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.HexFormat; +import java.util.UUID; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; + +@Service +@RequiredArgsConstructor +@Slf4j +public class AttachmentService { + private final Telemetry telemetry; + private final Semaphore uploads = new Semaphore(1, true); + private final JdbcTemplate jdbc; + private final ProcessingJobService jobs; + private final AttachmentVariantService variants; + private final UserService userService; + private final ChatService chatService; + private final MinioClient minio; + private final AttachmentRepository attachmentRepository; + private final S3Properties s3Props; + private final AttachmentValidator validator; + private final AttachmentMapper attachmentMapper; + + @Transactional + public AttachmentDTO upload( + UUID chatId, + UUID caller, + String filename, + HttpServletRequest request, + String declaredType) { + acquireUploadSlot(); + try { + return performUpload(chatId, caller, filename, request, declaredType); + } finally { + uploads.release(); + } + } + + private void acquireUploadSlot() { + try { + if (!uploads.tryAcquire(30, TimeUnit.SECONDS)) + throw new ServiceUnavailableException("Upload capacity exhausted"); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ServiceUnavailableException("Upload interrupted"); + } + } + + private AttachmentDTO performUpload( + UUID chatId, + UUID caller, + String filename, + HttpServletRequest request, + String declaredType) { + jdbc.execute("select pg_advisory_xact_lock_shared(741204)"); + Chat chat = chatService.getChat(caller, chatId); + String safeFilename = validator.sanitizeFilename(filename); + byte[] body = readBounded(request); + String contentType = validator.validate(body, safeFilename); + if (!contentType.equals(declaredType)) + throw new UnsupportedMediaTypeException("MIME mismatch"); + jobs.requireCapacity(); + String objectKey = "staging/" + UUID.randomUUID(); + String checksum = + telemetry.dependency( + "s3.staging.put", () -> uploadToS3(body, objectKey, contentType)); + return saveAttachmentQueued( + objectKey, safeFilename, contentType, body, checksum, chat, caller); + } + + private byte[] readBounded(HttpServletRequest request) { + try { + byte[] bytes = request.getInputStream().readNBytes(10485761); + if (bytes.length > 10485760) throw new PayloadTooLargeException("File exceeds limit"); + return bytes; + } catch (IOException e) { + throw new InvalidRequestException("Incomplete upload"); + } + } + + AttachmentDTO saveAttachmentQueued( + String objectKey, + String safeFilename, + String contentType, + byte[] body, + String checksum, + Chat chat, + UUID caller) { + try { + Attachment attachment = + buildAttachment( + objectKey, safeFilename, contentType, body, checksum, chat, caller); + attachment.setStatus(AttachmentStatus.QUEUED); + attachmentRepository.saveAndFlush(attachment); + jobs.enqueue(attachment.getId()); + return attachmentMapper.toDto(attachment); + } catch (DataAccessException e) { + compensateS3Upload(objectKey); + throw e; + } + } + + @Transactional( + readOnly = true, + isolation = org.springframework.transaction.annotation.Isolation.REPEATABLE_READ) + public AttachmentDTO getMetadata(UUID attachmentId, UUID userId) { + Attachment attachment = requireAttachmentForMember(attachmentId, userId); + AttachmentDTO result = attachmentMapper.toDto(attachment); + result.setVariants(variants.metadata(attachment)); + return result; + } + + public DownloadedAttachment download(UUID attachmentId, UUID userId, String variant) { + Attachment attachment = requireAttachmentForMember(attachmentId, userId); + if (attachment.getStatus() != AttachmentStatus.READY) + throw new ConflictException("Attachment not ready"); + return streamFromS3(attachment, variant); + } + + void compensateS3Upload(String objectKey) { + try { + minio.removeObject( + RemoveObjectArgs.builder().bucket(s3Props.bucket()).object(objectKey).build()); + log.info("Compensated S3 upload: removed objectKey={}", objectKey); + } catch (Exception e) { + log.error( + "Failed to compensate S3 upload for objectKey={}: {}", + objectKey, + e.getMessage(), + e); + } + } + + private String uploadToS3(byte[] body, String objectKey, String contentType) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + try (var digestIn = + new java.security.DigestInputStream( + new java.io.ByteArrayInputStream(body), digest)) { + minio.putObject( + PutObjectArgs.builder().bucket(s3Props.bucket()).object(objectKey).stream( + digestIn, (long) body.length, -1L) + .contentType(contentType) + .build()); + } + return HexFormat.of().formatHex(digest.digest()); + } catch (Exception e) { + log.error("S3 upload failed for objectKey: {}", objectKey, e); + throw new StorageUnavailableException("S3 upload failed", e); + } + } + + private Attachment buildAttachment( + String objectKey, + String filename, + String contentType, + byte[] body, + String checksum, + Chat chat, + UUID caller) { + User owner = userService.getReferenceUserById(caller); + return attachmentMapper.toEntity( + objectKey, filename, contentType, (long) body.length, checksum, chat, owner); + } + + private Attachment requireAttachmentForMember(UUID attachmentId, UUID userId) { + Attachment attachment = + attachmentRepository + .findById(attachmentId) + .orElseThrow(() -> new NotFoundException("Attachment not found")); + assertMember(attachment.getChat().getId(), userId); + return attachment; + } + + private DownloadedAttachment streamFromS3(Attachment attachment, String variant) { + var content = variants.content(attachment, variant); + try { + var stream = + minio.getObject( + GetObjectArgs.builder() + .bucket(s3Props.bucket()) + .object(content.objectKey()) + .build()); + return new DownloadedAttachment( + stream, content.contentType(), attachment.getFilename()); + } catch (Exception e) { + throw translateS3Exception(e, attachment.getId(), attachment.getObjectKey()); + } + } + + private RuntimeException translateS3Exception( + Exception e, UUID attachmentId, String objectKey) { + if (e instanceof io.minio.errors.ErrorResponseException minioEx) { + if ("NoSuchKey".equals(minioEx.errorResponse().code())) { + log.warn("S3 object missing for attachment {}", attachmentId); + return new NotFoundException("Attachment content not found"); + } + } + log.error("S3 download failed for {}", objectKey, e); + return new StorageUnavailableException("S3 download failed", e); + } + + private void assertMember(UUID chatId, UUID userId) { + boolean isMember = chatService.existsByIdAndMembersId(chatId, userId); + if (!isMember) { + throw new NotFoundException("Attachment not found"); + } + } + + @Transactional( + readOnly = true, + isolation = org.springframework.transaction.annotation.Isolation.READ_COMMITTED) + public ResponseEntity downloadResponse(UUID id, UUID user, String variant) { + jdbc.execute("SELECT pg_advisory_xact_lock_shared(741204)"); + var file = download(id, user, variant); + try (var in = file.stream()) { + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType(file.contentType())) + .header( + "Content-Disposition", + ContentDisposition.attachment() + .filename(file.filename(), StandardCharsets.UTF_8) + .build() + .toString()) + .body(in.readNBytes(10485761)); + } catch (IOException e) { + throw new StorageUnavailableException("Download failed", e); + } + } + + public record DownloadedAttachment(InputStream stream, String contentType, String filename) {} +} diff --git a/src/main/java/ru/mtusi/messenger/service/AttachmentVariantService.java b/src/main/java/ru/mtusi/messenger/service/AttachmentVariantService.java new file mode 100644 index 0000000..ceef319 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/AttachmentVariantService.java @@ -0,0 +1,57 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; + +import ru.mtusi.messenger.dto.VariantDTO; +import ru.mtusi.messenger.exception.NotFoundException; +import ru.mtusi.messenger.model.Attachment; +import ru.mtusi.messenger.model.AttachmentStatus; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class AttachmentVariantService { + private final JdbcTemplate jdbc; + + public List metadata(Attachment attachment) { + if (attachment.getStatus() != AttachmentStatus.READY) return List.of(); + return jdbc.query( + """ + SELECT * FROM attachment_variants + WHERE attachment_id = ? AND pipeline_version = ? ORDER BY name + """, + (row, index) -> + new VariantDTO( + VariantDTO.NameEnum.fromValue(row.getString("name")), + VariantDTO.ContentTypeEnum.fromValue(row.getString("content_type")), + row.getLong("size_bytes"), + row.getInt("width"), + row.getInt("height"), + row.getString("sha256")), + attachment.getId(), + attachment.getPipelineVersion()); + } + + public Content content(Attachment attachment, String variant) { + if ("original".equals(variant)) + return new Content(attachment.getObjectKey(), attachment.getContentType()); + List rows = + jdbc.query( + """ + SELECT object_key, content_type FROM attachment_variants + WHERE attachment_id = ? AND pipeline_version = ? AND name = ? + """, + (row, index) -> new Content(row.getString(1), row.getString(2)), + attachment.getId(), + attachment.getPipelineVersion(), + variant); + if (rows.isEmpty()) throw new NotFoundException("Variant not found"); + return rows.getFirst(); + } + + public record Content(String objectKey, String contentType) {} +} diff --git a/src/main/java/ru/mtusi/messenger/service/AuthService.java b/src/main/java/ru/mtusi/messenger/service/AuthService.java new file mode 100644 index 0000000..69c8930 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/AuthService.java @@ -0,0 +1,38 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +import ru.mtusi.messenger.dto.SessionDTO; +import ru.mtusi.messenger.dto.UserDTO; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.UnauthorizedException; +import ru.mtusi.messenger.security.BasicCredentials; +import ru.mtusi.messenger.security.SessionPrincipal; + +@Service +@RequiredArgsConstructor +public class AuthService { + private final UserService userService; + private final SessionService sessionService; + + public SessionDTO login(String authorizationHeader, long contentLength) { + if (contentLength > 0) { + throw new InvalidRequestException("Request body is not allowed"); + } + var creds = BasicCredentials.parse(authorizationHeader); + UserDTO userDTO = userService.authenticate(creds.username(), creds.password()); + return sessionService.createSession(userDTO); + } + + public void logout() { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + if (auth == null || !(auth.getPrincipal() instanceof SessionPrincipal sp)) { + throw new UnauthorizedException("No active session"); + } + sessionService.revoke(sp.sessionId()); + } +} diff --git a/src/main/java/ru/mtusi/messenger/service/ChatService.java b/src/main/java/ru/mtusi/messenger/service/ChatService.java new file mode 100644 index 0000000..896e859 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/ChatService.java @@ -0,0 +1,135 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.dto.ChatDTO; +import ru.mtusi.messenger.dto.ChatPageDTO; +import ru.mtusi.messenger.dto.CreateChatDTO; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.NotFoundException; +import ru.mtusi.messenger.mapper.ChatMapper; +import ru.mtusi.messenger.model.Chat; +import ru.mtusi.messenger.model.User; +import ru.mtusi.messenger.repository.ChatRepository; +import ru.mtusi.messenger.util.CursorCodec; +import ru.mtusi.messenger.validation.ChatValidator; + +import java.util.List; +import java.util.Set; +import java.util.UUID; + +@Service +@Slf4j +@RequiredArgsConstructor +public class ChatService { + private final ChatMapper chatMapper; + private final ChatValidator chatValidator; + private final ChatRepository chatRepository; + + @Transactional + public ChatDTO createChat(UUID caller, CreateChatDTO createChatDTO) { + Set members = chatValidator.validateCreate(caller, createChatDTO); + Chat chat = buildChat(createChatDTO, members); + chat = persistChat(chat); + return chatMapper.toChatDto(chat); + } + + private Chat buildChat(CreateChatDTO createChatDTO, Set members) { + Chat chat = chatMapper.toChat(createChatDTO); + chat.setMembers(members); + return chat; + } + + private Chat persistChat(Chat chat) { + try { + return chatRepository.saveAndFlush(chat); + } catch (DataIntegrityViolationException e) { + throw new InvalidRequestException("Cannot create chat: invalid data"); + } + } + + @Transactional(readOnly = true) + public ChatDTO getChatDTO(UUID caller, UUID chatId) { + Chat chat = requireChatForMember(caller, chatId); + return chatMapper.toChatDto(chat); + } + + @Transactional(readOnly = true) + public Chat getChat(UUID caller, UUID chatId) { + return requireChatForMember(caller, chatId); + } + + private Chat requireChatForMember(UUID caller, UUID chatId) { + Chat chat = + chatRepository + .findById(chatId) + .orElseThrow(() -> new NotFoundException("Chat not found")); + if (chat.getMembers().stream().noneMatch(m -> m.getId().equals(caller))) { + throw new NotFoundException("Chat not found"); + } + return chat; + } + + @Transactional(readOnly = true) + public ChatPageDTO listChats(UUID userId, Integer limit, String cursor) { + int size = resolveSize(limit); + CursorCodec.Cursor decoded = parseChatsCursor(cursor); + List rows = loadChatPage(userId, decoded, size); + return buildPage(rows, size); + } + + private int resolveSize(Integer limit) { + return (limit == null) ? 50 : limit; + } + + private List loadChatPage(UUID userId, CursorCodec.Cursor cursor, int size) { + if (cursor == null) { + return chatRepository.findPageByMember(userId, Pageable.ofSize(size + 1)); + } + return chatRepository.findPageByMemberAfterCursor( + userId, + cursor.boundary().createdAt(), + cursor.boundary().id(), + Pageable.ofSize(size + 1)); + } + + private CursorCodec.Cursor parseChatsCursor(String cursor) { + if (cursor == null) return null; + if (cursor.isEmpty()) throw new InvalidRequestException("Cursor must not be empty"); + CursorCodec.Cursor decoded = CursorCodec.decode(cursor); + if (decoded.scopeId() != null) throw new InvalidRequestException("Invalid cursor"); + return decoded; + } + + private ChatPageDTO buildPage(List rows, int size) { + List items = rows.stream().limit(size).map(chatMapper::toChatDto).toList(); + + ChatPageDTO page = new ChatPageDTO(); + page.setItems(items); + page.setNextCursor(buildNextCursor(rows, size)); + return page; + } + + private String buildNextCursor(List rows, int size) { + if (rows.size() <= size) { + return null; + } + Chat last = rows.get(size - 1); + return CursorCodec.encode(null, boundaryOf(last)); + } + + private CursorCodec.PageBoundary boundaryOf(Chat chat) { + return new CursorCodec.PageBoundary(chat.getCreatedAt(), chat.getId()); + } + + @Transactional + public boolean existsByIdAndMembersId(UUID chatId, UUID userId) { + return chatRepository.existsByIdAndMembersId(chatId, userId); + } +} diff --git a/src/main/java/ru/mtusi/messenger/service/HealthService.java b/src/main/java/ru/mtusi/messenger/service/HealthService.java new file mode 100644 index 0000000..84e1db7 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/HealthService.java @@ -0,0 +1,63 @@ +package ru.mtusi.messenger.service; + +import com.zaxxer.hikari.HikariDataSource; + +import lombok.RequiredArgsConstructor; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; + +import ru.mtusi.messenger.dto.HealthDTO; +import ru.mtusi.messenger.exception.ServiceUnavailableException; + +import javax.sql.DataSource; + +@Service +@RequiredArgsConstructor +public class HealthService { + + private final JdbcTemplate jdbcTemplate; + private final DataSource dataSource; + private final io.minio.MinioClient minio; + private final ru.mtusi.messenger.config.properties.S3Properties s3; + + public HealthDTO readiness() { + requirePoolAvailable(); + requireDatabaseReachable(); + try { + if (!minio.bucketExists( + io.minio.BucketExistsArgs.builder().bucket(s3.bucket()).build())) + throw new ServiceUnavailableException("Storage unavailable"); + } catch (Exception e) { + throw new ServiceUnavailableException("Storage unavailable"); + } + return ok(); + } + + public HealthDTO liveness() { + return ok(); + } + + private void requirePoolAvailable() { + if (dataSource instanceof HikariDataSource hikari) { + var pool = hikari.getHikariPoolMXBean(); + if (pool != null && pool.getActiveConnections() >= hikari.getMaximumPoolSize()) { + throw new ServiceUnavailableException("Database connection pool exhausted"); + } + } + } + + private void requireDatabaseReachable() { + try { + jdbcTemplate.queryForObject("SELECT 1", Integer.class); + } catch (Exception e) { + throw new ServiceUnavailableException("Database unavailable"); + } + } + + private HealthDTO ok() { + HealthDTO h = new HealthDTO(); + h.setStatus(HealthDTO.StatusEnum.OK); + return h; + } +} diff --git a/src/main/java/ru/mtusi/messenger/service/IdempotencyService.java b/src/main/java/ru/mtusi/messenger/service/IdempotencyService.java new file mode 100644 index 0000000..c91889a --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/IdempotencyService.java @@ -0,0 +1,83 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.exception.ConflictException; +import ru.mtusi.messenger.model.IdempotencyKey; +import ru.mtusi.messenger.repository.IdempotencyRepository; +import ru.mtusi.messenger.util.converter.IdempotencyPayloadConverter; +import ru.mtusi.messenger.validation.IdempotencyValidator; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.HexFormat; +import java.util.UUID; +import java.util.function.Supplier; + +@Service +@RequiredArgsConstructor +public class IdempotencyService { + + private final IdempotencyRepository idempotencyRepository; + private final IdempotencyPayloadConverter converter; + private final IdempotencyValidator validator; + + @Transactional + public T execute( + UUID userId, + String method, + UUID chatId, + String key, + Object requestBody, + Class responseType, + Supplier action) { + validator.validateKey(key); + String hash = hashRequest(requestBody); + + int claimed = idempotencyRepository.tryClaim(userId, method, chatId, key, hash); + if (claimed == 0) { + return replay(userId, method, chatId, key, hash, responseType); + } + + T result = action.get(); + idempotencyRepository.commitResponse( + userId, method, chatId, key, 201, converter.toJson(result)); + return result; + } + + private T replay( + UUID userId, + String method, + UUID chatId, + String key, + String hash, + Class responseType) { + IdempotencyKey stored = + idempotencyRepository + .findByUserIdAndMethodAndChatIdAndIdempotencyKey( + userId, method, chatId, key) + .orElseThrow( + () -> + new IllegalStateException( + "Idempotency row not visible after conflict")); + if (!stored.getRequestHash().equals(hash)) + throw new ConflictException("Idempotency-Key reused with a different request body"); + if (stored.getStatusCode() == null || stored.getStatusCode() != 201) + throw new ConflictException("Idempotency-Key has unexpected status"); + return converter.fromJson(stored.getResponseBody(), responseType); + } + + private String hashRequest(Object body) { + try { + byte[] digest = + MessageDigest.getInstance("SHA-256") + .digest(converter.toJson(body).getBytes(StandardCharsets.UTF_8)); + return HexFormat.of().formatHex(digest); + } catch (Exception e) { + throw new IllegalStateException("Cannot hash request body", e); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/service/MessageService.java b/src/main/java/ru/mtusi/messenger/service/MessageService.java new file mode 100644 index 0000000..1aee971 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/MessageService.java @@ -0,0 +1,164 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.config.properties.AppProperties; +import ru.mtusi.messenger.dto.CreateMessageDTO; +import ru.mtusi.messenger.dto.MessageDTO; +import ru.mtusi.messenger.dto.MessagePageDTO; +import ru.mtusi.messenger.exception.ConflictException; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.NotFoundException; +import ru.mtusi.messenger.exception.PayloadTooLargeException; +import ru.mtusi.messenger.mapper.MessageMapper; +import ru.mtusi.messenger.model.AttachmentStatus; +import ru.mtusi.messenger.model.Chat; +import ru.mtusi.messenger.model.Message; +import ru.mtusi.messenger.model.User; +import ru.mtusi.messenger.repository.AttachmentRepository; +import ru.mtusi.messenger.repository.ChatRepository; +import ru.mtusi.messenger.repository.MessageRepository; +import ru.mtusi.messenger.util.CursorCodec; + +import java.util.List; +import java.util.UUID; + +@Service +@Slf4j +@RequiredArgsConstructor +public class MessageService { + private final AttachmentRepository attachments; + private final IdempotencyService idempotencyService; + private final MessageRepository messageRepository; + private final ChatRepository chatRepository; + private final ChatService chatService; + private final UserService userService; + private final MessageMapper messageMapper; + private final AppProperties appProperties; + + @Transactional + public MessageDTO createMessage( + UUID senderId, UUID chatId, CreateMessageDTO dto, String idempotencyKey) { + Chat chat = chatService.getChat(senderId, chatId); + + return idempotencyService.execute( + senderId, + "POST", + chatId, + idempotencyKey, + dto, + MessageDTO.class, + () -> doCreate(senderId, chatId, dto, chat)); + } + + private MessageDTO doCreate(UUID senderId, UUID chatId, CreateMessageDTO dto, Chat chat) { + validateCreate(chatId, dto); + requireHistoryCapacity(chatId); + User sender = userService.getReferenceUserById(senderId); + Message message = buildMessage(dto, sender, chat); + try { + message = messageRepository.saveAndFlush(message); + } catch (DataIntegrityViolationException e) { + throw new NotFoundException("Cannot create message: referenced entity is missing"); + } + log.info("Created message id={} in chat={}", message.getId(), chatId); + return messageMapper.toMessageDTO(message); + } + + private void validateCreate(UUID chatId, CreateMessageDTO dto) { + if (dto.getText() == null + || (dto.getText().isBlank() + && (dto.getAttachmentIds() == null || dto.getAttachmentIds().isEmpty()))) { + throw new InvalidRequestException("Empty text"); + } + var ids = dto.getAttachmentIds(); + if (ids == null + || ids.size() > 10 + || ids.contains(null) + || new java.util.HashSet<>(ids).size() != ids.size() + || dto.getText().codePointCount(0, dto.getText().length()) > 4000) { + throw new InvalidRequestException("Invalid message"); + } + validateAttachments(chatId, ids); + } + + private void validateAttachments(UUID chatId, List ids) { + for (UUID id : ids.stream().sorted().toList()) { + var attachment = + attachments + .findForMessage(id) + .filter(a -> a.getChat().getId().equals(chatId)) + .orElseThrow(() -> new NotFoundException("Attachment not found")); + if ((attachment.getStatus() == AttachmentStatus.FAILED + || attachment.getStatus() == AttachmentStatus.BROKEN)) { + throw new ConflictException("Attachment not ready"); + } + } + } + + private void requireHistoryCapacity(UUID chatId) { + chatRepository + .findForMessageCreation(chatId) + .orElseThrow(() -> new NotFoundException("Chat not found")); + if (messageRepository.countByChatId(chatId) >= appProperties.maxMessagesPerChat()) { + throw new PayloadTooLargeException("Chat history limit reached"); + } + } + + private Message buildMessage(CreateMessageDTO createMessageDTO, User sender, Chat chat) { + Message message = messageMapper.toMessage(createMessageDTO); + message.setChat(chat); + message.setSender(sender); + return message; + } + + @Transactional(readOnly = true) + public MessagePageDTO listMessages(UUID userId, UUID chatId, Integer limit, String cursor) { + int size = (limit == null) ? 50 : limit; + chatService.getChatDTO(userId, chatId); + CursorCodec.Cursor decoded = parseMessagesCursor(cursor, chatId); + List rows = + (decoded == null) + ? messageRepository.findPageByChat(chatId, Pageable.ofSize(size + 1)) + : messageRepository.findPageByChatAfterCursor( + chatId, + decoded.boundary().createdAt(), + decoded.boundary().id(), + Pageable.ofSize(size + 1)); + return buildPage(rows, size, chatId); + } + + private CursorCodec.Cursor parseMessagesCursor(String cursor, UUID chatId) { + if (cursor == null) return null; + if (cursor.isEmpty()) throw new InvalidRequestException("Cursor must not be empty"); + CursorCodec.Cursor decoded = CursorCodec.decode(cursor); + if (decoded.scopeId() == null || !decoded.scopeId().equals(chatId)) + throw new InvalidRequestException("Cursor from another chat"); + return decoded; + } + + private MessagePageDTO buildPage(List rows, int size, UUID chatId) { + List items = + rows.stream().limit(size).map(messageMapper::toMessageDTO).toList(); + + MessagePageDTO page = new MessagePageDTO(); + page.setItems(items); + page.setNextCursor(buildNextCursor(rows, size, chatId)); + return page; + } + + private String buildNextCursor(List rows, int size, UUID chatId) { + if (rows.size() <= size) { + return null; + } + Message last = rows.get(size - 1); + return CursorCodec.encode( + chatId, new CursorCodec.PageBoundary(last.getCreatedAt(), last.getId())); + } +} diff --git a/src/main/java/ru/mtusi/messenger/service/ProcessingJobService.java b/src/main/java/ru/mtusi/messenger/service/ProcessingJobService.java new file mode 100644 index 0000000..b010ad2 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/ProcessingJobService.java @@ -0,0 +1,63 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.exception.ServiceUnavailableException; +import ru.mtusi.messenger.telemetry.Telemetry; + +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class ProcessingJobService { + private final Telemetry telemetry; + private final JdbcTemplate jdbc; + + @Value("${pipeline.max-pending:100}") + private int maxPending; + + @Value("${pipeline.max-attempts:5}") + private int maxAttempts; + + @Transactional(propagation = Propagation.MANDATORY) + public void requireCapacity() { + jdbc.execute("SELECT pg_advisory_xact_lock(741205)"); + Long pending = + jdbc.queryForObject( + "SELECT count(*) FROM processing_jobs WHERE status IN" + + " ('QUEUED','PROCESSING')", + Long.class); + if (pending == null || pending >= maxPending) { + throw new ServiceUnavailableException("Processing queue capacity exhausted"); + } + } + + @Transactional(propagation = Propagation.MANDATORY) + public void enqueue(UUID attachmentId) { + var context = telemetry.carrier(); + jdbc.update( + """ + WITH job AS ( + INSERT INTO processing_jobs(attachment_id, pipeline_version, attempt_limit) + VALUES (?, 1, ?) RETURNING * + ) + INSERT INTO processing_outbox(job_id, payload) + SELECT job_id, jsonb_strip_nulls(jsonb_build_object('schema_version',1,'job_id',job_id, + 'type','attachment.process.v1','attachment_id',attachment_id, + 'pipeline_version',pipeline_version,'attempt',1, + 'created_at',to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'), + 'traceparent',CAST(? AS text),'tracestate',CAST(? AS text))) + FROM job + """, + attachmentId, + maxAttempts, + context.get("traceparent"), + context.get("tracestate")); + } +} diff --git a/src/main/java/ru/mtusi/messenger/service/SessionCacheService.java b/src/main/java/ru/mtusi/messenger/service/SessionCacheService.java new file mode 100644 index 0000000..de0dd44 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/SessionCacheService.java @@ -0,0 +1,74 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.cache.CacheHealthTracker; +import ru.mtusi.messenger.repository.SessionRepository; +import ru.mtusi.messenger.telemetry.Telemetry; + +@Slf4j +@Service +@RequiredArgsConstructor +public class SessionCacheService { + private final Telemetry telemetry; + + public static final String SESSIONS = "sessions"; + + private final SessionRepository sessionRepository; + private final CacheManager cacheManager; + private final CacheHealthTracker cacheHealthTracker; + + @Transactional(readOnly = true) + public SessionService.SessionRecord lookupByHash(String hex) { + byte[] hash = hexToBytes(hex); + return telemetry.dependency( + "postgres.session.lookup", + () -> + sessionRepository + .findByTokenHash(hash) + .filter(s -> s.getRevokedAt() == null) + .map( + s -> + new SessionService.SessionRecord( + s.getId(), s.getUserId(), s.getExpiresAt())) + .orElse(null)); + } + + @CacheEvict(value = SESSIONS, key = "#hex") + public void evict(String hex) {} + + public void dropCacheIfStale() { + if (!cacheHealthTracker.consumeStale()) { + return; + } + try { + Cache cache = cacheManager.getCache(SESSIONS); + if (cache != null) { + cache.clear(); + } + log.info("Cache '{}' cleared after stale flag", SESSIONS); + } catch (RuntimeException e) { + log.warn("Cache clear failed: {}", e.getMessage()); + cacheHealthTracker.markStale(); + } + } + + private static byte[] hexToBytes(String hex) { + int len = hex.length(); + byte[] out = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + out[i / 2] = + (byte) + ((Character.digit(hex.charAt(i), 16) << 4) + + Character.digit(hex.charAt(i + 1), 16)); + } + return out; + } +} diff --git a/src/main/java/ru/mtusi/messenger/service/SessionService.java b/src/main/java/ru/mtusi/messenger/service/SessionService.java new file mode 100644 index 0000000..b27b8fa --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/SessionService.java @@ -0,0 +1,82 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.config.properties.SessionProperties; +import ru.mtusi.messenger.dto.SessionDTO; +import ru.mtusi.messenger.dto.UserDTO; +import ru.mtusi.messenger.exception.UnauthorizedException; +import ru.mtusi.messenger.mapper.SessionMapper; +import ru.mtusi.messenger.model.Session; +import ru.mtusi.messenger.repository.SessionRepository; +import ru.mtusi.messenger.util.TokenGenerator; +import ru.mtusi.messenger.util.TokenHasher; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +@Slf4j +@RequiredArgsConstructor +@Service +public class SessionService { + private final TokenGenerator tokenGenerator; + private final TokenHasher tokenHasher; + private final SessionProperties sessionProperties; + private final SessionRepository sessionRepository; + private final SessionMapper sessionMapper; + private final SessionCacheService sessionCacheService; + + @Transactional + public SessionDTO createSession(UserDTO userDTO) { + String token = tokenGenerator.generate(); + Session session = sessionRepository.save(buildSession(userDTO, token)); + return sessionMapper.toSessionDTO(session, token, userDTO); + } + + public SessionRecord requireActive(String token) { + sessionCacheService.dropCacheIfStale(); + String hex = tokenHasher.hash(token); + SessionRecord record = sessionCacheService.lookupByHash(hex); + requireNotExpired(record, hex); + return record; + } + + private void requireNotExpired(SessionRecord record, String hex) { + if (record == null || isExpired(record)) { + sessionCacheService.evict(hex); + throw new UnauthorizedException("Invalid or expired session"); + } + } + + private boolean isExpired(SessionRecord record) { + return record.expiresAt().isBefore(Instant.now()); + } + + @Transactional + public void revoke(UUID sessionId) { + sessionRepository.findById(sessionId).ifPresent(this::markRevokedAndEvict); + } + + private void markRevokedAndEvict(Session session) { + if (session.getRevokedAt() == null) { + session.setRevokedAt(Instant.now().truncatedTo(ChronoUnit.MILLIS)); + } + sessionCacheService.evict(tokenHasher.hash(session.getTokenHash())); + } + + private Session buildSession(UserDTO userDTO, String token) { + Session session = new Session(); + session.setUserId(userDTO.getId()); + session.setTokenHash(tokenHasher.sha256(token)); + session.setExpiresAt( + Instant.now().plus(sessionProperties.ttl()).truncatedTo(ChronoUnit.MILLIS)); + return session; + } + + public record SessionRecord(UUID sessionId, UUID userId, Instant expiresAt) {} +} diff --git a/src/main/java/ru/mtusi/messenger/service/UserService.java b/src/main/java/ru/mtusi/messenger/service/UserService.java new file mode 100644 index 0000000..5151dda --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/service/UserService.java @@ -0,0 +1,88 @@ +package ru.mtusi.messenger.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import ru.mtusi.messenger.dto.CreateUserDTO; +import ru.mtusi.messenger.dto.UserDTO; +import ru.mtusi.messenger.exception.ConflictException; +import ru.mtusi.messenger.exception.NotFoundException; +import ru.mtusi.messenger.exception.UnauthorizedException; +import ru.mtusi.messenger.mapper.UserMapper; +import ru.mtusi.messenger.model.User; +import ru.mtusi.messenger.repository.UserRepository; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +@Slf4j +public class UserService { + private final PasswordEncoder passwordEncoder; + private final UserMapper userMapper; + private final UserRepository userRepository; + + @Transactional + public Set requireUsers(Collection ids) { + Set uniqueIds = new HashSet<>(ids); + Set users = userRepository.findAllByIdForShare(uniqueIds); + if (users.size() != uniqueIds.size()) { + throw new NotFoundException("Users not found: " + findMissing(uniqueIds, users)); + } + return users; + } + + private Set findMissing(Set requested, Set found) { + Set foundIds = found.stream().map(User::getId).collect(Collectors.toSet()); + + Set missing = new HashSet<>(requested); + missing.removeAll(foundIds); + return missing; + } + + @Transactional(readOnly = true) + public UserDTO authenticate(String username, String password) { + User user = + userRepository + .findByUsername(username) + .orElseThrow(() -> new UnauthorizedException("Bad credentials")); + + if (!passwordEncoder.matches(password, user.getPasswordHash())) { + throw new UnauthorizedException("Bad credentials"); + } + return userMapper.toUserDto(user); + } + + @Transactional + public UserDTO registerUser(CreateUserDTO createUserDTO) { + User user = userMapper.toUser(createUserDTO); + user.setPasswordHash(passwordEncoder.encode(createUserDTO.getPassword())); + try { + return userMapper.toUserDto(userRepository.saveAndFlush(user)); + } catch (DataIntegrityViolationException exception) { + throw new ConflictException("Username already exists"); + } + } + + @Transactional(readOnly = true) + public UserDTO currentUser(UUID userId) { + return userMapper.toUserDto( + userRepository + .findById(userId) + .orElseThrow(() -> new NotFoundException("User not found"))); + } + + @Transactional + public User getReferenceUserById(UUID senderId) { + return userRepository.getReferenceById(senderId); + } +} diff --git a/src/main/java/ru/mtusi/messenger/telemetry/Telemetry.java b/src/main/java/ru/mtusi/messenger/telemetry/Telemetry.java new file mode 100644 index 0000000..2210966 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/telemetry/Telemetry.java @@ -0,0 +1,169 @@ +package ru.mtusi.messenger.telemetry; + +import com.sun.management.OperatingSystemMXBean; +import com.zaxxer.hikari.HikariDataSource; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.logs.Severity; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapGetter; +import io.opentelemetry.context.propagation.TextMapSetter; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; + +import jakarta.servlet.http.HttpServletRequest; + +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + +import javax.sql.DataSource; + +@Component +public class Telemetry implements AutoCloseable { + private static final String SCOPE = "ru.mtusi.messenger"; + private static final TextMapSetter> SETTER = Map::put; + private static final TextMapGetter HEADERS = + new TextMapGetter<>() { + @Override + public Iterable keys(HttpServletRequest request) { + return Collections.list(request.getHeaderNames()); + } + + @Override + public String get(HttpServletRequest request, String key) { + return request.getHeader(key); + } + }; + + private final OpenTelemetrySdk sdk; + private final OpenTelemetry otel; + + public Telemetry(DataSource dataSource) { + sdk = + System.getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == null + ? null + : AutoConfiguredOpenTelemetrySdk.builder() + .disableShutdownHook() + .build() + .getOpenTelemetrySdk(); + otel = sdk == null ? OpenTelemetry.noop() : sdk; + registerProcess(); + if (dataSource instanceof HikariDataSource pool) { + registerPool(pool); + } + } + + public OpenTelemetry openTelemetry() { + return otel; + } + + public Span server(HttpServletRequest request) { + Context parent = + otel.getPropagators() + .getTextMapPropagator() + .extract(Context.root(), request, HEADERS); + return otel.getTracer(SCOPE) + .spanBuilder("HTTP request") + .setParent(parent) + .setSpanKind(SpanKind.SERVER) + .startSpan(); + } + + public Map carrier() { + Map value = new HashMap<>(); + otel.getPropagators().getTextMapPropagator().inject(Context.current(), value, SETTER); + return value; + } + + public T dependency(String name, Supplier operation) { + Span span = + otel.getTracer(SCOPE).spanBuilder(name).setSpanKind(SpanKind.CLIENT).startSpan(); + try (var scope = span.makeCurrent()) { + return operation.get(); + } catch (RuntimeException error) { + span.setStatus(StatusCode.ERROR); + span.setAttribute("error.type", error.getClass().getSimpleName()); + throw error; + } finally { + span.end(); + } + } + + public void log(String event, boolean error, Attributes attributes) { + otel.getLogsBridge() + .get(SCOPE) + .logRecordBuilder() + .setSeverity(error ? Severity.ERROR : Severity.INFO) + .setContext(Context.current()) + .setBody(event) + .setAllAttributes(attributes) + .emit(); + } + + private void registerProcess() { + var meter = otel.getMeter(SCOPE); + if (ManagementFactory.getOperatingSystemMXBean() instanceof OperatingSystemMXBean process) { + meter.counterBuilder("lab_process_cpu") + .ofDoubles() + .setUnit("s") + .buildWithCallback(result -> result.record(process.getProcessCpuTime() / 1e9)); + } + meter.gaugeBuilder("lab_process_rss") + .ofLongs() + .setUnit("By") + .buildWithCallback( + result -> { + try (var lines = Files.lines(Path.of("/proc/self/status"))) { + lines.filter(line -> line.startsWith("VmRSS:")) + .findFirst() + .ifPresent( + line -> + result.record( + Long.parseLong(line.split("\s+")[1]) + * 1024)); + } catch (IOException ignored) { + } + }); + } + + private void registerPool(HikariDataSource pool) { + var meter = otel.getMeter(SCOPE); + meter.gaugeBuilder("lab_db_pool_connections") + .ofLongs() + .buildWithCallback( + result -> { + var state = pool.getHikariPoolMXBean(); + if (state != null) { + result.record( + state.getActiveConnections(), + Attributes.of(AttributeKey.stringKey("state"), "used")); + result.record( + state.getThreadsAwaitingConnection(), + Attributes.of(AttributeKey.stringKey("state"), "waiting")); + result.record( + pool.getMaximumPoolSize(), + Attributes.of(AttributeKey.stringKey("state"), "capacity")); + } + }); + } + + @Override + public void close() { + if (sdk != null) { + sdk.close(); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/util/CursorCodec.java b/src/main/java/ru/mtusi/messenger/util/CursorCodec.java new file mode 100644 index 0000000..a2da636 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/util/CursorCodec.java @@ -0,0 +1,41 @@ +package ru.mtusi.messenger.util; + +import ru.mtusi.messenger.exception.InvalidRequestException; + +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.Base64; +import java.util.UUID; + +public final class CursorCodec { + + private CursorCodec() {} + + public record PageBoundary(Instant createdAt, UUID id) {} + + public record Cursor(UUID scopeId, PageBoundary boundary) {} + + public static String encode(UUID scopeId, PageBoundary boundary) { + String raw = + (scopeId == null ? "" : scopeId.toString()) + + "|" + + boundary.createdAt() + + "|" + + boundary.id(); + return Base64.getUrlEncoder() + .withoutPadding() + .encodeToString(raw.getBytes(StandardCharsets.UTF_8)); + } + + public static Cursor decode(String cursor) { + try { + String raw = new String(Base64.getUrlDecoder().decode(cursor), StandardCharsets.UTF_8); + String[] p = raw.split("\\|", -1); + UUID scopeId = p[0].isEmpty() ? null : UUID.fromString(p[0]); + PageBoundary boundary = new PageBoundary(Instant.parse(p[1]), UUID.fromString(p[2])); + return new Cursor(scopeId, boundary); + } catch (Exception e) { + throw new InvalidRequestException("Invalid cursor"); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/util/TokenGenerator.java b/src/main/java/ru/mtusi/messenger/util/TokenGenerator.java new file mode 100644 index 0000000..7ccc499 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/util/TokenGenerator.java @@ -0,0 +1,17 @@ +package ru.mtusi.messenger.util; + +import org.springframework.stereotype.Component; + +import java.security.SecureRandom; +import java.util.Base64; + +@Component +public class TokenGenerator { + private final SecureRandom rng = new SecureRandom(); + + public String generate() { + byte[] raw = new byte[32]; + rng.nextBytes(raw); + return Base64.getUrlEncoder().withoutPadding().encodeToString(raw); + } +} diff --git a/src/main/java/ru/mtusi/messenger/util/TokenHasher.java b/src/main/java/ru/mtusi/messenger/util/TokenHasher.java new file mode 100644 index 0000000..c7c103c --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/util/TokenHasher.java @@ -0,0 +1,37 @@ +package ru.mtusi.messenger.util; + +import org.springframework.stereotype.Component; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +@Component("tokenHasher") +public class TokenHasher { + + public byte[] sha256(String token) { + try { + return MessageDigest.getInstance("SHA-256") + .digest(token.getBytes(StandardCharsets.UTF_8)); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("SHA-256 not available", e); + } + } + + public String hash(String token) { + return toHex(sha256(token)); + } + + public String hash(byte[] bytes) { + return toHex(bytes); + } + + private String toHex(byte[] bytes) { + StringBuilder sb = new StringBuilder(bytes.length * 2); + for (byte b : bytes) { + sb.append(Character.forDigit((b >> 4) & 0xF, 16)); + sb.append(Character.forDigit(b & 0xF, 16)); + } + return sb.toString(); + } +} diff --git a/src/main/java/ru/mtusi/messenger/util/converter/IdempotencyPayloadConverter.java b/src/main/java/ru/mtusi/messenger/util/converter/IdempotencyPayloadConverter.java new file mode 100644 index 0000000..93df952 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/util/converter/IdempotencyPayloadConverter.java @@ -0,0 +1,31 @@ +package ru.mtusi.messenger.util.converter; + +import lombok.RequiredArgsConstructor; + +import org.springframework.stereotype.Component; + +import tools.jackson.core.JacksonException; +import tools.jackson.databind.json.JsonMapper; + +@Component +@RequiredArgsConstructor +public class IdempotencyPayloadConverter { + + private final JsonMapper jsonMapper; + + public String toJson(Object payload) { + try { + return jsonMapper.writeValueAsString(payload); + } catch (JacksonException e) { + throw new IllegalStateException("Cannot serialize idempotency payload", e); + } + } + + public T fromJson(String json, Class type) { + try { + return jsonMapper.readValue(json, type); + } catch (JacksonException e) { + throw new IllegalStateException("Corrupt idempotency payload", e); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/validation/AttachmentValidator.java b/src/main/java/ru/mtusi/messenger/validation/AttachmentValidator.java new file mode 100644 index 0000000..c6d7116 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/validation/AttachmentValidator.java @@ -0,0 +1,81 @@ +package ru.mtusi.messenger.validation; + +import lombok.RequiredArgsConstructor; + +import org.springframework.stereotype.Component; + +import ru.mtusi.messenger.config.properties.UploadProperties; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.PayloadTooLargeException; +import ru.mtusi.messenger.exception.UnsupportedMediaTypeException; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Locale; + +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.MemoryCacheImageInputStream; + +@Component +@RequiredArgsConstructor +public class AttachmentValidator { + private final UploadProperties props; + + public String validate(byte[] bytes, String filename) { + if (bytes.length > props.maxBytes()) { + throw new PayloadTooLargeException("File exceeds max size"); + } + try (var input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes))) { + var readers = ImageIO.getImageReaders(input); + if (!readers.hasNext()) { + throw new UnsupportedMediaTypeException("Only PNG/JPEG allowed"); + } + ImageReader reader = readers.next(); + try { + reader.setInput(input, true, true); + return inspect(reader); + } finally { + reader.dispose(); + } + } catch (IOException e) { + throw new UnsupportedMediaTypeException("Corrupted or unsupported image"); + } + } + + private String inspect(ImageReader reader) throws IOException { + String mime = contentType(reader.getFormatName()); + validateDimensions(reader.getWidth(0), reader.getHeight(0)); + reader.read(0); + return mime; + } + + private String contentType(String format) { + return switch (format.toLowerCase(Locale.ROOT)) { + case "png" -> "image/png"; + case "jpeg", "jpg" -> "image/jpeg"; + default -> throw new UnsupportedMediaTypeException("Only PNG/JPEG allowed"); + }; + } + + private void validateDimensions(int width, int height) { + if (width <= 0 || height <= 0) { + throw new UnsupportedMediaTypeException("Invalid image dimensions"); + } + if ((long) width * height > props.maxPixels()) { + throw new PayloadTooLargeException("Image dimensions exceed limit"); + } + } + + public String sanitizeFilename(String filename) { + if (filename == null + || filename.isBlank() + || filename.codePointCount(0, filename.length()) > 255 + || filename.contains("/") + || filename.contains("\\") + || filename.codePoints().anyMatch(Character::isISOControl)) { + throw new InvalidRequestException("Invalid filename"); + } + return filename; + } +} diff --git a/src/main/java/ru/mtusi/messenger/validation/ChatValidator.java b/src/main/java/ru/mtusi/messenger/validation/ChatValidator.java new file mode 100644 index 0000000..3661322 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/validation/ChatValidator.java @@ -0,0 +1,68 @@ +package ru.mtusi.messenger.validation; + +import lombok.RequiredArgsConstructor; + +import org.springframework.stereotype.Component; + +import ru.mtusi.messenger.dto.CreateChatDTO; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.NotFoundException; +import ru.mtusi.messenger.model.User; +import ru.mtusi.messenger.service.UserService; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +@Component +@RequiredArgsConstructor +public class ChatValidator { + + private final UserService userService; + + public Set validateCreate(UUID caller, CreateChatDTO createChatDTO) { + List memberIds = createChatDTO.getMemberIds(); + String title = createChatDTO.getTitle(); + + requireNotBlank(title, "Title must not be blank"); + requireNonEmpty(memberIds, "member_ids must not be empty"); + requireNoDuplicates(memberIds, "member_ids contains duplicates"); + requireContains(memberIds, caller, "Вызывающий должен быть участником"); + + return requireAllUsersExist(memberIds); + } + + private void requireNotBlank(String value, String message) { + if (value == null || value.trim().isEmpty()) { + throw new InvalidRequestException(message); + } + } + + private void requireNonEmpty(List ids, String message) { + if (ids == null || ids.isEmpty()) { + throw new InvalidRequestException(message); + } + } + + private void requireNoDuplicates(List ids, String message) { + Set unique = new HashSet<>(ids); + if (unique.size() != ids.size()) { + throw new InvalidRequestException(message); + } + } + + private void requireContains(List ids, UUID value, String message) { + if (!ids.contains(value)) { + throw new InvalidRequestException(message); + } + } + + private Set requireAllUsersExist(List ids) { + try { + return userService.requireUsers(ids); + } catch (NotFoundException e) { + throw new InvalidRequestException(e.getMessage()); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/validation/IdempotencyValidator.java b/src/main/java/ru/mtusi/messenger/validation/IdempotencyValidator.java new file mode 100644 index 0000000..b846f17 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/validation/IdempotencyValidator.java @@ -0,0 +1,41 @@ +package ru.mtusi.messenger.validation; + +import lombok.RequiredArgsConstructor; + +import org.springframework.stereotype.Component; + +import ru.mtusi.messenger.config.properties.IdempotencyProperties; +import ru.mtusi.messenger.exception.InvalidRequestException; + +import java.util.regex.Pattern; + +@Component +@RequiredArgsConstructor +public class IdempotencyValidator { + + private final IdempotencyProperties properties; + + public void validateKey(String key) { + requireNotBlank(key); + requireMaxLength(key); + requirePattern(key); + } + + private void requireNotBlank(String key) { + if (key == null || key.isEmpty()) { + throw new InvalidRequestException("Idempotency-Key header is required"); + } + } + + private void requireMaxLength(String key) { + if (key.length() > properties.keyMaxLength()) { + throw new InvalidRequestException("Idempotency-Key is too long"); + } + } + + private void requirePattern(String key) { + if (!Pattern.matches(properties.keyPattern(), key)) { + throw new InvalidRequestException("Idempotency-Key contains invalid characters"); + } + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..f04c943 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,128 @@ +spring: + application: + name: messenger-lab6 + config: + import: "optional:configtree:/run/secrets/" + lifecycle: + timeout-per-shutdown-phase: 30s + jackson: + default-property-inclusion: always + datasource: + driver-class-name: org.postgresql.Driver + url: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/messenger} + username: ${SPRING_DATASOURCE_USERNAME:messenger} + password: ${SPRING_DATASOURCE_PASSWORD:change-me-local-only} + hikari: + maximum-pool-size: 20 + minimum-idle: 5 + connection-timeout: 2000 + connection-init-sql: "SET statement_timeout = 5000" + validation-timeout: 1000 + idle-timeout: 60000 + max-lifetime: 1800000 + jpa: + hibernate: + ddl-auto: validate + open-in-view: false + properties: + hibernate: + jdbc: + time_zone: UTC + dialect: org.hibernate.dialect.PostgreSQLDialect + flyway: + enabled: true + locations: classpath:db/migration + data: + redis: + host: ${SPRING_DATA_REDIS_HOST:localhost} + port: ${SPRING_DATA_REDIS_PORT:6379} + cache: + type: redis + + servlet: + multipart: + max-file-size: ${MAX_UPLOAD_BYTES:10485760} + max-request-size: ${MAX_UPLOAD_BYTES:10485760} + file-size-threshold: 0 + resolve-lazily: false + +server: + address: ${APP_HOST:0.0.0.0} + port: ${APP_PORT:8080} + shutdown: graceful + +management: + endpoints: + web: + exposure: + include: health,metrics,caches + endpoint: + health: + probes: + enabled: true + show-details: always + caches: + enabled: true + + +app: + idempotency: + key-max-length: ${IDEMPOTENCY_KEY_MAX_LENGTH:128} + key-pattern: '${IDEMPOTENCY_KEY_PATTERN:^[A-Za-z0-9._:-]+$}' + retention-hours: ${IDEMPOTENCY_RETENTION_HOURS:24} + instance: + id: ${INSTANCE_ID:${HOSTNAME:${random.uuid}}} + limits: + max-messages-per-chat: ${MAX_MESSAGES_PER_CHAT:10000} + rate: + rate-per-minute: ${RATE_PER_MINUTE:600} + retry-after-seconds: ${RETRY_AFTER_SECONDS:60} + session: + ttl: ${SESSION_TTL_SECONDS:400}s + redis: + host: ${SPRING_DATA_REDIS_HOST:localhost} + port: ${SPRING_DATA_REDIS_PORT:6379} + command-timeout: ${REDIS_COMMAND_TIMEOUT:500ms} + connect-timeout: ${REDIS_CONNECT_TIMEOUT:500ms} + shutdown-timeout: ${REDIS_SHUTDOWN_TIMEOUT:100ms} + keep-alive-idle: ${REDIS_KEEP_ALIVE_IDLE:30s} + keep-alive-interval: ${REDIS_KEEP_ALIVE_INTERVAL:10s} + tcp-user-timeout: ${REDIS_TCP_USER_TIMEOUT:10s} + +s3: + endpoint: ${S3_ENDPOINT:http://minio:9000} + bucket: ${S3_BUCKET:messenger-attachments} + region: ${S3_REGION:us-east-1} + access-key: ${S3_ACCESS_KEY_ID:} + secret-key: ${S3_SECRET_ACCESS_KEY:} + path-style-access: true + presign-expiry-seconds: ${S3_PRESIGN_EXPIRY_SECONDS:300} + +upload: + max-bytes: ${MAX_UPLOAD_BYTES:10485760} + max-pixels: ${MAX_IMAGE_PIXELS:40000000} + allowed-content-types: + - image/png + - image/jpeg + +reconciler: + enabled: ${RECONCILER_ENABLED:true} + interval-ms: ${RECONCILER_INTERVAL_MS:3600000} + grace-period-minutes: ${RECONCILER_GRACE_PERIOD_MINUTES:15} + dry-run: ${RECONCILER_DRY_RUN:false} + batch-size: ${RECONCILER_BATCH_SIZE:100} + max-s3-retries: ${RECONCILER_MAX_S3_RETRIES:3} + lock-at-most-for: ${RECONCILER_LOCK_AT_MOST_FOR:30m} + lock-at-least-for: ${RECONCILER_LOCK_AT_LEAST_FOR:1m} + +logging: + pattern: + console: "%d{ISO8601} %-5level [%X{requestId}] %logger{20} - %msg%n" + level: + org.springframework.cache: TRACE + org.springframework.cache.interceptor: TRACE + software.amazon.awssdk: DEBUG + +pipeline: + max-pending: ${MAX_PENDING_JOBS:100} + max-attempts: ${JOB_MAX_ATTEMPTS:5} diff --git a/src/main/resources/db/migration/V1__init.sql b/src/main/resources/db/migration/V1__init.sql new file mode 100644 index 0000000..84f1c9a --- /dev/null +++ b/src/main/resources/db/migration/V1__init.sql @@ -0,0 +1,180 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +CREATE TABLE users ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + username VARCHAR(32) NOT NULL, + display_name VARCHAR(100) NOT NULL, + password_hash VARCHAR(255) NOT NULL, + created_at TIMESTAMPTZ NOT NULL, + CONSTRAINT uq_users_username UNIQUE (username), + CONSTRAINT ck_users_username_format + CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT ck_users_display_name_not_blank + CHECK (length(btrim(display_name)) > 0) +); + +CREATE TABLE chats ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + title VARCHAR(100) NOT NULL, + created_at TIMESTAMPTZ NOT NULL, + CONSTRAINT ck_chats_title_not_blank CHECK (length(btrim(title)) > 0) +); + +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, + PRIMARY KEY (chat_id, user_id) +); + +CREATE TABLE messages ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + text VARCHAR(4000) NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE sessions ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + token_hash BYTEA NOT NULL, + created_at TIMESTAMPTZ NOT NULL, + expires_at TIMESTAMPTZ NOT NULL, + revoked_at TIMESTAMPTZ, + CONSTRAINT uq_sessions_token_hash UNIQUE (token_hash), + CONSTRAINT ck_sessions_expiry CHECK (expires_at > created_at), + CONSTRAINT ck_sessions_token_len CHECK (octet_length(token_hash) = 32) +); + +CREATE TABLE idempotency_keys ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + method VARCHAR(16) NOT NULL, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + idempotency_key VARCHAR(128) NOT NULL, + request_hash VARCHAR(64) NOT NULL, + status_code SMALLINT, + response_body JSONB, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT uq_idempotency_scope UNIQUE (user_id, method, chat_id, idempotency_key), + CONSTRAINT ck_idem_key_format CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'), + CONSTRAINT ck_idem_status_body_paired CHECK ((status_code IS NULL) = (response_body IS NULL)) +); + +CREATE TABLE attachments ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + object_key VARCHAR(512) NOT NULL, + filename VARCHAR(255) NOT NULL, + content_type VARCHAR(100) NOT NULL, + size_bytes BIGINT NOT NULL, + checksum VARCHAR(64) NOT NULL, + status VARCHAR(20) NOT NULL DEFAULT 'READY' + CONSTRAINT attachments_status_check + CHECK (status IN ('UPLOADING', 'QUEUED', 'PROCESSING', 'READY', 'FAILED', 'BROKEN')), + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + staging_key VARCHAR(512), + error_code VARCHAR(64), + pipeline_version BIGINT NOT NULL DEFAULT 1, + CONSTRAINT uq_attachments_object_key UNIQUE (object_key) +); + +CREATE TABLE shedlock ( + name VARCHAR(64) NOT NULL PRIMARY KEY, + lock_until TIMESTAMP NOT NULL, + locked_at TIMESTAMP NOT NULL, + locked_by VARCHAR(255) NOT NULL +); + +CREATE INDEX idx_messages_chat_created_asc_id + ON messages (chat_id, created_at ASC, id ASC); + +CREATE INDEX idx_chat_members_user + ON chat_members (user_id); + +CREATE INDEX idx_chats_created_asc_id + ON chats (created_at ASC, id ASC); + +CREATE INDEX idx_sessions_user_active + ON sessions (user_id) + WHERE revoked_at IS NULL; + +CREATE INDEX idx_sessions_expires_active + ON sessions (expires_at) + WHERE revoked_at IS NULL; + +CREATE INDEX idx_idempotency_created + ON idempotency_keys (created_at); + +CREATE INDEX idx_attachments_object_key ON attachments(object_key); +CREATE INDEX idx_attachments_chat_id ON attachments(chat_id); +CREATE INDEX idx_attachments_owner_id ON attachments(owner_id); +CREATE INDEX idx_attachments_status ON attachments(status); +CREATE INDEX idx_attachments_created_at ON attachments(created_at); + +CREATE TABLE message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id UUID NOT NULL CONSTRAINT fk_message_attachment REFERENCES attachments(id), + attachment_order INTEGER NOT NULL, + PRIMARY KEY (message_id, attachment_order) +); + +CREATE INDEX idx_message_attachments_attachment_id ON message_attachments(attachment_id); + +CREATE TABLE processing_jobs ( + job_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + attachment_id UUID NOT NULL REFERENCES attachments(id), + pipeline_version BIGINT NOT NULL, + status VARCHAR(20) NOT NULL DEFAULT 'QUEUED', + attempt INTEGER NOT NULL DEFAULT 0, + attempt_limit INTEGER NOT NULL DEFAULT 5, + lease_token UUID, + lease_until TIMESTAMPTZ, + next_at TIMESTAMPTZ NOT NULL DEFAULT now(), + next_publish_at TIMESTAMPTZ NOT NULL DEFAULT now(), + error_code VARCHAR(64), + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + completed_at TIMESTAMPTZ, + UNIQUE (attachment_id, pipeline_version), + CHECK (status IN ('QUEUED','PROCESSING','READY','FAILED','SUPERSEDED')) +); +CREATE TABLE processing_outbox ( + job_id UUID PRIMARY KEY REFERENCES processing_jobs(job_id), + payload JSONB NOT NULL, + published_at TIMESTAMPTZ, + publish_count INTEGER NOT NULL DEFAULT 0 +); +CREATE TABLE attachment_variants ( + attachment_id UUID NOT NULL REFERENCES attachments(id), + pipeline_version BIGINT NOT NULL, + name VARCHAR(20) NOT NULL, + object_key VARCHAR(512) NOT NULL UNIQUE, + content_type VARCHAR(100) NOT NULL, + size_bytes BIGINT NOT NULL, + width INTEGER NOT NULL, + height INTEGER NOT NULL, + sha256 VARCHAR(64) NOT NULL, + PRIMARY KEY (attachment_id, pipeline_version, name) +); +CREATE TABLE pipeline_events ( + seq BIGSERIAL PRIMARY KEY, + job_id UUID REFERENCES processing_jobs(job_id), + event VARCHAR(64) NOT NULL, + attempt INTEGER NOT NULL DEFAULT 0, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); +CREATE TABLE pipeline_faults ( + attachment_id UUID NOT NULL REFERENCES attachments(id), + pipeline_version BIGINT NOT NULL, + point VARCHAR(64) NOT NULL, + action VARCHAR(20) NOT NULL, + remaining INTEGER NOT NULL DEFAULT 1, + hits INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY (attachment_id, pipeline_version, point) +); +CREATE INDEX processing_jobs_pending ON processing_jobs (next_at, next_publish_at) WHERE status IN ('QUEUED','PROCESSING'); +CREATE INDEX pipeline_events_job ON pipeline_events(job_id); + +CREATE INDEX idx_attachments_staging_key ON attachments(staging_key) WHERE staging_key IS NOT NULL; diff --git a/src/test/java/ru/mtusi/messenger/AttachmentIntegrationTest.java b/src/test/java/ru/mtusi/messenger/AttachmentIntegrationTest.java new file mode 100644 index 0000000..90de413 --- /dev/null +++ b/src/test/java/ru/mtusi/messenger/AttachmentIntegrationTest.java @@ -0,0 +1,49 @@ +package ru.mtusi.messenger; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +import ru.mtusi.messenger.config.properties.UploadProperties; +import ru.mtusi.messenger.exception.InvalidRequestException; +import ru.mtusi.messenger.exception.PayloadTooLargeException; +import ru.mtusi.messenger.exception.UnsupportedMediaTypeException; +import ru.mtusi.messenger.validation.AttachmentValidator; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +class AttachmentIntegrationTest { + private final AttachmentValidator validator = + new AttachmentValidator( + new UploadProperties(10485760, 40000000, List.of("image/png", "image/jpeg"))); + + @Test + void acceptsRealFixture() throws Exception { + assertThat( + validator.validate( + Files.readAllBytes(Path.of("tests/fixtures/sample.png")), + "sample.png")) + .isEqualTo("image/png"); + } + + @Test + void rejectsControlCharactersWithoutSilentlyRenaming() { + for (String name : List.of("a/b.png", "a\\b.png", "a\n.png", "a\u007f.png")) { + assertThatThrownBy(() -> validator.sanitizeFilename(name)) + .isInstanceOf(InvalidRequestException.class); + } + assertThat(validator.sanitizeFilename("photo..png")).isEqualTo("photo..png"); + } + + @Test + void rejectsTruncatedImageAndOversizedBody() throws Exception { + byte[] png = Files.readAllBytes(Path.of("tests/fixtures/sample.png")); + assertThatThrownBy(() -> validator.validate(java.util.Arrays.copyOf(png, 40), "x.png")) + .isInstanceOf(UnsupportedMediaTypeException.class); + assertThatThrownBy(() -> validator.validate(new byte[10485761], "x.png")) + .isInstanceOf(PayloadTooLargeException.class); + } +} diff --git a/src/test/java/ru/mtusi/messenger/MessengerLab1ApplicationTests.java b/src/test/java/ru/mtusi/messenger/MessengerLab1ApplicationTests.java new file mode 100644 index 0000000..b7033ea --- /dev/null +++ b/src/test/java/ru/mtusi/messenger/MessengerLab1ApplicationTests.java @@ -0,0 +1,23 @@ +package ru.mtusi.messenger; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +import ru.mtusi.messenger.util.CursorCodec; + +import java.time.Instant; +import java.util.UUID; + +class MessengerLab1ApplicationTests { + @Test + void cursorPreservesScopeAndBoundary() { + UUID scope = UUID.randomUUID(); + var boundary = + new CursorCodec.PageBoundary( + Instant.parse("2026-01-01T00:00:00Z"), UUID.randomUUID()); + var decoded = CursorCodec.decode(CursorCodec.encode(scope, boundary)); + assertThat(decoded.scopeId()).isEqualTo(scope); + assertThat(decoded.boundary()).isEqualTo(boundary); + } +} diff --git a/student-tests/incidents.py b/student-tests/incidents.py new file mode 100644 index 0000000..c06a9ec --- /dev/null +++ b/student-tests/incidents.py @@ -0,0 +1,317 @@ +import os +import subprocess +import threading +import time +from concurrent.futures import ThreadPoolExecutor +from contextlib import contextmanager +from urllib.error import HTTPError, URLError + +from client import Client, check, run +from observe import trace_for, upload +from pipeline_support import COMPOSE, compose, ready_api, sql, wait_ready +from telemetry_support import logs, save, spans, states, value, wait_until + + +API_ALERTS = ["ApiHighErrorRate", "ApiBurnFast", "ApiBurnSlow"] +READY_ALERTS = ["ReadyBurnFast", "ReadyBurnSlow"] + + +class Traffic: + def __init__(self, actor): + self.actor = actor + self.stop = threading.Event() + self.lock = threading.Lock() + self.counts = {} + self.error = None + self.slowest_ok = 0 + + def run(self): + client = Client(timeout=12) + while not self.stop.is_set(): + started = time.monotonic() + try: + response = client.call( + "GET", "/api/v1/users/me", actor=self.actor, expected=[200, 503] + ) + status = response["status"] + elapsed = time.monotonic() - started + with self.lock: + self.counts[status] = self.counts.get(status, 0) + 1 + if status == 200: + self.slowest_ok = max(self.slowest_ok, elapsed) + else: + self.error = response["headers"]["X-Request-Id"] + except Exception as error: + with self.lock: + name = type(error).__name__ + self.counts[name] = self.counts.get(name, 0) + 1 + self.stop.wait(0.2) + + @contextmanager + def start(self, clients=6): + with ThreadPoolExecutor(max_workers=clients) as pool: + workers = [pool.submit(self.run) for _ in range(clients)] + try: + yield self + finally: + self.stop.set() + for worker in workers: + worker.result() + + def snapshot(self): + with self.lock: + return dict(self.counts) + + +@contextmanager +def session_lock(seconds): + command = ( + 'psql -v ON_ERROR_STOP=1 -U "$(cat /run/secrets/postgres_user)"' + ' -d "$(cat /run/secrets/postgres_db)" -At' + ) + with subprocess.Popen( + COMPOSE + ["exec", "-T", "db", "sh", "-c", command], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + text=True, + ) as process: + process.stdin.write( + f"SET application_name='lab6-incident'; BEGIN; LOCK TABLE sessions IN ACCESS EXCLUSIVE MODE; " + f"SELECT pg_sleep({seconds}); ROLLBACK;\n" + ) + process.stdin.close() + try: + wait_until( + lambda: sql( + "SELECT count(*) FROM pg_stat_activity WHERE application_name='lab6-incident'" + " AND wait_event='PgSleep';" + ) + == "1", + 15, + "database lock not acquired", + ) + yield time.monotonic() + finally: + sql( + "SELECT pg_cancel_backend(pid) FROM pg_stat_activity" + " WHERE application_name='lab6-incident' AND state='active';" + ) + process.wait(timeout=15) + + +def monitor(names, firing, timeout, started): + samples = [] + + reached = set() + target = "firing" if firing else "inactive" + + def check_state(): + snapshot = states() + samples.append( + { + "seconds": round(time.monotonic() - started, 2), + "states": {name: snapshot[name] for name in names}, + } + ) + reached.update(name for name in names if snapshot[name] == target) + return ( + len(reached) == len(names) + if firing + else all(snapshot[name] == target for name in names) + ) + + wait_until(check_state, timeout, "unexpected alert states: " + str(names)) + return samples + + +def find_failed_dependency(request_id, since): + trace_id = wait_until(lambda: trace_for(request_id, since), 35, "DB incident log missing") + + def dependency(): + try: + rows = spans(trace_id) + except HTTPError as error: + if error.code == 404: + return None + raise + for row in rows: + if row["name"] == "postgres.session.lookup": + duration = (int(row["endTimeUnixNano"]) - int(row["startTimeUnixNano"])) / 1e9 + if duration >= 3: + return { + "trace_id": trace_id, + "request_id": request_id, + "dependency": row["name"], + "duration_seconds": duration, + "status": row.get("status"), + } + return None + + return wait_until(dependency, 35, "slow PostgreSQL span missing") + + +def database_incidents(actor): + with Traffic(actor).start() as traffic: + time.sleep(15) + wait_until(lambda: traffic.snapshot().get(200, 0) >= 35, 15, "no warm traffic") + before = traffic.snapshot().get(503, 0) + with session_lock(3) as started: + canceled = sql( + "SELECT pg_cancel_backend(pid) FROM (SELECT pid FROM pg_stat_activity" + " WHERE wait_event_type='Lock' AND query LIKE '%sessions%' LIMIT 1) AS blocked;" + ) + check(canceled == "t", "short incident did not cancel a DB read") + brief = [] + for _ in range(17): + brief.append({"seconds": round(time.monotonic() - started, 2), "states": states()}) + time.sleep(1) + short_errors = traffic.snapshot().get(503, 0) - before + check(short_errors > 0, "short fault produced no observed errors") + check( + not any(sample["states"][name] == "firing" for sample in brief for name in API_ALERTS), + "short burst fired an API alert", + ) + since = time.time() - 2 + with session_lock(85) as started: + detection = monitor(API_ALERTS, True, 75, started) + request_id = traffic.error + released = time.monotonic() + recovery = monitor(API_ALERTS, False, 95, released) + check( + traffic.snapshot().get(200, 0) > 30 and traffic.snapshot().get(503, 0) > short_errors, + "sustained incident did not affect HTTP", + ) + sample = find_failed_dependency(request_id, since) + return { + "traffic_clients": 6, + "short": {"503_count": short_errors, "samples": brief, "alert_firings": 0}, + "sustained": { + "firing": detection, + "resolved": recovery, + "bounded_lock_seconds": 85, + "traffic_statuses": traffic.snapshot(), + "cause": sample, + }, + } + + +def queue_incident(client, actor, chat): + compose("stop", "worker") + started = time.monotonic() + attachments = [] + try: + for index in range(16): + attachments.append(upload(client, actor, chat, "lag-" + str(index))["json"]) + provisional = monitor(["AttachmentQueueLag"], True, 45, started) + peak = { + "age_seconds": value("max(lab_queue_oldest_seconds)"), + "backlog": value("max(lab_queue_depth)"), + "reported_capacity": value("sum(lab_worker_capacity)"), + } + check(peak["backlog"] >= 16, "observability reported no backlog") + finally: + compose("start", "worker") + released = time.monotonic() + for attachment in attachments: + wait_ready(client, actor, attachment) + drain = round(time.monotonic() - released, 2) + ready_firing = monitor(READY_ALERTS, True, 45, released) + resolved = monitor(["AttachmentQueueLag"], False, 30, released) + ready_resolved = monitor(READY_ALERTS, False, 90, released) + return { + "accepted": len(attachments), + "queue_firing": provisional, + "peak": peak, + "drained_seconds": drain, + "ready_burn_firing": ready_firing, + "queue_resolved": resolved, + "ready_burn_resolved": ready_resolved, + "all_ready": True, + } + + +def collector_snapshot(): + metric = "otelcol_exporter_send_failed_log_records_total" + return { + "send_failed_logs": value("sum(" + metric + '{exporter="otlphttp/loki"})'), + "queue_size": value('max(otelcol_exporter_queue_size{exporter="otlphttp/loki"})'), + "queue_capacity": value('max(otelcol_exporter_queue_capacity{exporter="otlphttp/loki"})'), + "rss_bytes": value("otelcol_process_memory_rss_bytes"), + } + + +def backend_incident(client, actor, chat): + before = collector_snapshot() + acknowledged = [] + samples = [] + compose("stop", "loki") + started = time.monotonic() + try: + for index in range(30): + acknowledged.append( + client.message(actor, chat, "backend proof " + str(index))["json"]["id"] + ) + if index % 3 == 0: + samples.append(collector_snapshot()) + time.sleep(0.7) + samples.append(collector_snapshot()) + finally: + compose("start", "loki") + page = client.call( + "GET", f"/api/v1/chats/{chat['id']}/messages?limit=100", actor=actor, schema="MessagePage" + )["json"] + check( + set(acknowledged) <= {item["id"] for item in page["items"]}, + "message loss during telemetry outage", + ) + after = collector_snapshot() + failed = ( + max([after["send_failed_logs"], *[s["send_failed_logs"] for s in samples]]) + - before["send_failed_logs"] + ) + check(failed > 0, "Collector exporter drops were not observed") + wait_until(loki_ready, 75, "Loki failed to recover") + since = time.time() - 2 + response = client.message(actor, chat, "telemetry restored") + restored_trace = wait_until( + lambda: trace_for(response["headers"]["X-Request-Id"], since), + 45, + "new business log not exported after Loki recovery", + ) + return { + "confirmed_and_read": len(acknowledged), + "seconds": round(time.monotonic() - started, 2), + "collector_before": before, + "collector_samples": samples, + "collector_after": after, + "failed_export_log_records_delta": failed, + "restored_trace_id": restored_trace, + } + + +def loki_ready(): + try: + logs('{service_name="messenger-api"}', limit=1) + return True + except (HTTPError, URLError, TimeoutError): + return False + + +def main(): + ready_api() + client = Client(timeout=12) + actor, _, _, chat = client.bootstrap() + mode = os.getenv("INCIDENT", "all") + if mode in ("all", "db"): + save("database-incident.json", database_incidents(actor)) + print("PASS short DB burst and sustained slowdown: real alerts and DB trace", flush=True) + if mode in ("all", "queue"): + save("queue-incident.json", queue_incident(client, actor, chat)) + print("PASS stopped worker: backlog, firing/resolved and slow READY burn", flush=True) + if mode in ("all", "backend"): + save("backend-incident.json", backend_incident(client, actor, chat)) + print("PASS stopped Loki: bounded telemetry failure and saved business writes", flush=True) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/observe.py b/student-tests/observe.py new file mode 100644 index 0000000..1c7f60d --- /dev/null +++ b/student-tests/observe.py @@ -0,0 +1,185 @@ +import time +import uuid +from pathlib import Path +from urllib.error import HTTPError + +from client import Client, check, run +from pipeline_support import compose, fault, job, ready_api, wait_ready +from telemetry_support import fetch, logs, prom, save, spans, wait_until + + +def upload(client, actor, chat, name): + return client.call( + "POST", + f"/api/v1/chats/{chat['id']}/attachments?filename={name}.png", + actor=actor, + raw=Path("tests/fixtures/sample.png").read_bytes(), + headers={"Content-Type": "image/png"}, + expected=202, + schema="Attachment", + ) + + +def trace_for(request_id, since): + rows = logs('{service_name="messenger-api"} | request_id="' + request_id + '"', since) + for row in rows: + trace_id = row["stream"].get("trace_id") + for entry in row["values"]: + metadata = entry[2] if len(entry) > 2 else {} + trace_id = trace_id or metadata.get("trace_id") + if trace_id: + return trace_id + return None + + +def complete_trace(trace_id): + try: + rows = spans(trace_id) + except HTTPError as error: + if error.code == 404: + return None + raise + required = { + "POST /api/v1/chats/{chat_id}/attachments", + "attachments publish", + "attachments consume", + "s3.staging.read", + "postgres.publish_result", + } + return ( + rows + if required <= {row["name"] for row in rows} + and sum(row["name"] == "attachments consume" for row in rows) >= 2 + else None + ) + + +def verify_trace(rows): + indexed = {s["spanId"]: s for s in rows} + consumers = [s for s in rows if s["name"] == "attachments consume"] + assert len(consumers) >= 2, "retry absent from trace" + for consumer in consumers: + producer = indexed[consumer["parentSpanId"]] + assert producer["name"] == "attachments publish", "consumer parent is not producer" + assert ( + indexed[producer["parentSpanId"]]["kind"] == "SPAN_KIND_SERVER" + or str(indexed[producer["parentSpanId"]]["kind"]) == "2" + ) + return [ + { + key: row.get(key) + for key in ("name", "service", "spanId", "parentSpanId", "kind", "status") + } + for row in rows + ] + + +def verify_metrics(): + names = [ + "lab_http_requests_total", + "lab_http_duration_seconds_bucket", + "lab_jobs_total", + "lab_job_duration_seconds_bucket", + "lab_ready_duration_seconds_bucket", + "lab_queue_depth", + "lab_queue_oldest_seconds", + "lab_db_pool_connections", + "lab_worker_busy", + "lab_worker_capacity", + "lab_process_cpu_seconds_total", + "lab_process_rss_bytes", + ] + return wait_until( + lambda: collect_metrics(names), + 50, + "required metric series missing", + ) + + +def collect_metrics(names): + result = prom('{__name__=~"' + "|".join(names) + '"}') + present = {row["metric"]["__name__"] for row in result} + if present != set(names): + return None + forbidden = { + "user_id", + "chat_id", + "message_id", + "attachment_id", + "request_id", + "filename", + "url", + } + for row in result: + assert not forbidden & set(row["metric"]), "forbidden metric attributes" + return { + name: { + "series": sum(row["metric"]["__name__"] == name for row in result), + "label_keys": sorted( + { + key + for row in result + if row["metric"]["__name__"] == name + for key in row["metric"] + } + ), + } + for name in names + } + + +def main(): + ready_api() + client = Client(timeout=12) + actor, _, _, chat = client.bootstrap() + marker = "proof" + uuid.uuid4().hex + client.message(actor, chat, marker) + since = time.time() - 2 + compose("stop", "worker") + try: + response = upload(client, actor, chat, marker) + attachment = response["json"] + fault(attachment, "before_effects", "transient") + finally: + compose("start", "worker") + wait_ready(client, actor, attachment) + check(job(attachment)["attempt"] == 2, "proof job did not retry") + request_id = response["headers"]["X-Request-Id"] + trace_id = wait_until( + lambda: trace_for(request_id, since), 45, "API request ID missing from Loki" + ) + rows = wait_until(lambda: complete_trace(trace_id), 60, "correlated trace missing from Tempo") + worker_logs = wait_until( + lambda: logs('{service_name="messenger-worker"} | trace_id="' + trace_id + '"', since), + 30, + "worker logs missing for trace", + ) + events = sorted({entry[1] for row in worker_logs for entry in row["values"]}) + check("worker.retry" in events and "worker.ready" in events, "retry/ready logs missing") + check( + not logs('{service_name=~"messenger-.+"} |= "' + marker + '"', since - 10), + "sensitive marker in logs", + ) + check(marker not in str(rows), "sensitive marker in trace") + check( + fetch("/api/dashboards/uid/messenger-lab6")["dashboard"]["title"] == "Messenger Lab 6", + "dashboard not provisioned", + ) + save( + "signal-link.json", + { + "trace_id": trace_id, + "request_id": request_id, + "spans": verify_trace(rows), + "worker_events": events, + "marker_absent": True, + }, + ) + save("metric-inventory.json", verify_metrics()) + print( + "PASS real upload -> publish -> consumer retry -> S3/DB; Loki/Tempo/Prometheus via Grafana" + ) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_ack_guard.py b/student-tests/pipeline_ack_guard.py new file mode 100644 index 0000000..7eba8fd --- /dev/null +++ b/student-tests/pipeline_ack_guard.py @@ -0,0 +1,15 @@ +from client import run +from pipeline_faults import Experiment + + +def main(): + experiment = Experiment() + experiment.fenced_without_dispatcher() + print( + "PASS expired worker without dispatcher: no premature ACK; pending/index retained; recovery publishes exactly one result on attempt 2", + flush=True, + ) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_boundaries.py b/student-tests/pipeline_boundaries.py new file mode 100644 index 0000000..77771ae --- /dev/null +++ b/student-tests/pipeline_boundaries.py @@ -0,0 +1,133 @@ +from concurrent.futures import ThreadPoolExecutor +from client import Client, check, run +from pipeline_support import compose, sql, ready_api, wait_ready, wait_for, operator, COMPOSE +import subprocess +import uuid + + +def eligibility(c, a, chat, attachment): + sql( + "create function proof_message_guard() returns trigger language plpgsql as $$ begin if NEW.chat_id='" + + chat["id"] + + "'::uuid then perform pg_sleep(4); end if; return NEW; end; $$; create trigger proof_message_guard before insert on messages for each row execute function proof_message_guard();" + ) + try: + with ThreadPoolExecutor(max_workers=1) as pool: + request = pool.submit(c.message, a, chat, "guard", None, [attachment["id"]]) + wait_for( + lambda: sql("select count(*) from pg_stat_activity where wait_event='PgSleep';") + != "0", + 10, + ) + code = """import psycopg +from pipeline.repository import Repository +from pipeline.config import Settings +try: + with Repository(Settings()).connection() as db: + db.execute("SET lock_timeout='250ms'") + db.execute("UPDATE attachments SET status='FAILED',error_code='injected_failure' WHERE id=%s",('ATTACHMENT',)) +except psycopg.errors.LockNotAvailable: + print('blocked_by_message_transaction',flush=True) +else: + raise AssertionError('status changed between validation and commit') +""".replace( + "ATTACHMENT", attachment["id"] + ) + output = compose("exec", "-T", "dispatcher", "python", "-c", code) + check(output == "blocked_by_message_transaction", "status guard missing") + check( + request.result()["json"]["attachment_ids"] == [attachment["id"]], + "message not created", + ) + finally: + sql("drop trigger proof_message_guard on messages; drop function proof_message_guard();") + sql( + "begin;update attachments set status='FAILED',error_code='injected_failure' where id='" + + attachment["id"] + + "';update processing_jobs set status='FAILED',error_code='injected_failure' where attachment_id='" + + attachment["id"] + + "';commit;" + ) + c.call( + "POST", + "/api/v1/chats/" + chat["id"] + "/messages", + 409, + actor=a, + body={"text": "", "attachment_ids": [attachment["id"]]}, + headers={"Idempotency-Key": uuid.uuid4().hex}, + ) + operator("replay", attachment) + print( + "PASS message eligibility: status UPDATE blocked until Message COMMIT; subsequent FAILED attachment rejected with 409", + flush=True, + ) + + +def download_guard(c, actor, attachment, body): + code = """import time +from pipeline.repository import Repository +from pipeline.config import Settings +with Repository(Settings()).connection() as db: + db.execute('SELECT pg_advisory_xact_lock(741204)') + print('cleanup_locked',flush=True) + time.sleep(4) +""" + holder = subprocess.Popen( + COMPOSE + ["exec", "-T", "dispatcher", "python", "-c", code], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + try: + check(holder.stdout.readline().strip() == "cleanup_locked", "cleanup fence not acquired") + with ThreadPoolExecutor(max_workers=1) as pool: + request = pool.submit( + c.call, + "GET", + "/api/v1/attachments/" + attachment["id"] + "/content", + 200, + None, + actor, + ) + wait_for( + lambda: sql( + "select count(*) from pg_stat_activity where wait_event='advisory' and query ilike '%pg_advisory_xact_lock_shared(741204)%';" + ) + != "0", + 3, + "download did not wait on cleanup fence", + ) + check(not request.done(), "download bypassed cleanup fence") + _, errors = holder.communicate(timeout=10) + check(holder.returncode == 0, "cleanup holder failed") + check(request.result(timeout=10)["bytes"] == body, "download changed bytes") + check( + sql("select pg_try_advisory_xact_lock(741204);") == "t", + "download leaked transaction fence", + ) + finally: + if holder.poll() is None: + holder.kill() + holder.wait() + print( + "PASS download/cleanup barrier: content waits for exclusive cleanup, returns identical bytes, releases shared fence", + flush=True, + ) + + +def main(): + ready_api() + c = Client(base_url="http://127.0.0.1:18085", timeout=30) + a, b, e, chat = c.bootstrap() + compose("stop", "worker") + try: + attachment, body = c.attachment(a, chat) + eligibility(c, a, chat, attachment) + finally: + compose("start", "worker") + wait_ready(c, a, attachment) + download_guard(c, b, attachment, body) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_core.py b/student-tests/pipeline_core.py new file mode 100644 index 0000000..2fd0034 --- /dev/null +++ b/student-tests/pipeline_core.py @@ -0,0 +1,142 @@ +from client import Client, check, run +from pipeline_support import ready_api, wait_ready, metadata, compose, broker, sql +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path +from PIL import Image +import hashlib +import io +import os +import time +import uuid + + +def main(): + ready_api() + c = Client(base_url="http://127.0.0.1:18085", timeout=30) + a, b, e, chat = c.bootstrap() + png = Path("tests/fixtures/sample.png").read_bytes() + path = "/api/v1/chats/" + chat["id"] + "/attachments?filename=proof.png" + + def upload(data=png, mime="image/png", expected=202): + return c.call( + "POST", + path, + expected, + actor=a, + raw=data, + headers={"Content-Type": mime}, + schema="Attachment" if expected == 202 else None, + ) + + compose("stop", "worker") + attachment = upload()["json"] + check(attachment["status"] == "queued", "upload response not queued snapshot") + c.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", 409, actor=b) + c.message(a, chat, "", attachments=[attachment["id"]]) + compose("start", "worker") + result = wait_ready(c, b, attachment) + verify(c, b, attachment, png, result) + print( + "PASS queued snapshot, link before ready, 409 before worker; original + 3 variants", + flush=True, + ) + negative(c, a, e, chat, attachment, upload, png) + fixtures(c, a, b, upload) + concurrent(c, a, chat, attachment) + print("PASS core pipeline contract", flush=True) + + +def verify(c, actor, attachment, original, result): + base = "/api/v1/attachments/" + attachment["id"] + "/content" + check(c.call("GET", base, actor=actor)["bytes"] == original, "original changed") + check( + {v["name"] for v in result["variants"]} == {"thumbnail", "crop", "optimized"}, + "variant set differs", + ) + for variant in result["variants"]: + response = c.call("GET", base + "?variant=" + variant["name"], actor=actor) + check( + hashlib.sha256(response["bytes"]).hexdigest() == variant["sha256"], "variant checksum" + ) + check(len(response["bytes"]) == variant["size_bytes"], "variant size") + check(response["headers"].get_content_type() == variant["content_type"], "variant MIME") + with Image.open(io.BytesIO(response["bytes"])) as image: + check(image.size == (variant["width"], variant["height"]), "variant dimensions") + check(not image.getexif(), "EXIF retained") + + +def negative(c, a, e, chat, attachment, upload, png): + upload(png[:33], expected=415) + upload(png, "image/jpeg", 415) + upload(b"x" * 10485761, expected=413) + c.call( + "POST", + "/api/v1/chats/" + chat["id"] + "/attachments?filename=..%2Fx.png", + 400, + actor=a, + raw=png, + headers={"Content-Type": "image/png"}, + ) + for suffix in ("", "/content"): + c.call("GET", "/api/v1/attachments/" + attachment["id"] + suffix, 404, actor=e) + other = c.call( + "POST", + "/api/v1/chats", + 201, + actor=a, + body={"title": "other", "member_ids": [a["id"]]}, + schema="Chat", + )["json"] + c.call( + "POST", + "/api/v1/chats/" + other["id"] + "/messages", + 404, + actor=a, + body={"text": "x", "attachment_ids": [attachment["id"]]}, + headers={"Idempotency-Key": uuid.uuid4().hex}, + ) + print("PASS bytes/corruption/MIME/filename/ACL/cross-chat negatives", flush=True) + + +def fixtures(c, a, b, upload): + output = io.BytesIO() + Image.new("RGBA", (8, 4), (255, 0, 0, 0)).save(output, format="PNG") + attachment = upload(output.getvalue())["json"] + result = wait_ready(c, b, attachment) + data = c.call( + "GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail", actor=b + )["bytes"] + with Image.open(io.BytesIO(data)) as image: + check( + image.size == (8, 4) and min(image.getpixel((0, 0))) >= 250, "alpha/upscale incorrect" + ) + output = io.BytesIO() + image = Image.new("RGB", (80, 40), (20, 80, 160)) + exif = image.getexif() + exif[274] = 6 + image.save(output, format="JPEG", exif=exif) + attachment = upload(output.getvalue(), "image/jpeg")["json"] + result = wait_ready(c, b, attachment) + thumb = next(v for v in result["variants"] if v["name"] == "thumbnail") + check((thumb["width"], thumb["height"]) == (40, 80), "EXIF orientation ignored") + verify(c, b, attachment, output.getvalue(), result) + print("PASS alpha white, no small-image enlargement, EXIF rotation, crop and WebP", flush=True) + + +def concurrent(c, a, chat, attachment): + key = uuid.uuid4().hex + + def send(_): + return c.message(a, chat, "concurrent", key=key, attachments=[attachment["id"]]) + + with ThreadPoolExecutor(max_workers=12) as pool: + replies = list(pool.map(send, range(24))) + check(len({r["json"]["id"] for r in replies}) == 1, "duplicate message effects") + check( + len({r["headers"]["X-Instance-Id"] for r in replies}) == 2, "two API replicas not observed" + ) + print("PASS 24 concurrent requests across 2 API replicas; single message effect", flush=True) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_faults.py b/student-tests/pipeline_faults.py new file mode 100644 index 0000000..32628e3 --- /dev/null +++ b/student-tests/pipeline_faults.py @@ -0,0 +1,373 @@ +from client import Client, check, run +from pipeline_support import ( + compose, + sql, + broker, + wait_for, + ready_api, + metadata, + wait_ready, + job, + payload, + inject_duplicate, + fault, + fault_hit, + operator, + manifest, + events, +) +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path +from PIL import Image +import io +import json +import os +import subprocess +import threading +import time + + +class Experiment: + def __init__(self): + ready_api() + self.client = Client(base_url="http://127.0.0.1:18085", timeout=30) + self.actor, self.member, self.outsider, self.chat = self.client.bootstrap() + self.body = Path("tests/fixtures/sample.png").read_bytes() + self.rows = [] + self.accepted = [] + + def upload(self, body=None, expected=202): + response = self.client.call( + "POST", + "/api/v1/chats/" + self.chat["id"] + "/attachments?filename=fault.png", + expected, + actor=self.actor, + raw=self.body if body is None else body, + headers={"Content-Type": "image/png"}, + schema="Attachment" if expected == 202 or isinstance(expected, list) else None, + ) + if response["status"] == 202: + self.accepted.append(response["json"]["id"]) + return response["json"] + return response + + def run_case(self, name, action): + started = time.monotonic() + action() + row = {"scenario": name, "result": "PASS", "seconds": round(time.monotonic() - started, 3)} + self.rows.append(row) + print(json.dumps(row), flush=True) + Path("evidence/pipeline-faults.json").write_text(json.dumps(self.rows, indent=2)) + + def settled(self, attachment): + wait_ready(self.client, self.actor, attachment, 120) + check(events(attachment, "ready") == 1, "multiple published effects") + check( + int( + sql( + "select count(*) from attachment_variants where attachment_id='" + + attachment["id"] + + "';" + ) + ) + == 3, + "duplicate variants", + ) + data = self.client.call( + "GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=self.member + )["bytes"] + check(data == self.body, "original changed") + + def prepare(self, point, action, count=1): + compose("stop", "worker") + attachment = self.upload() + fault(attachment, point, action, count) + compose("start", "worker") + return attachment + + def durability(self): + compose("stop", "worker", "dispatcher") + accepted = [self.upload() for _ in range(2)] + for attachment in accepted: + self.client.message(self.actor, self.chat, "", attachments=[attachment["id"]]) + check(job(attachment)["status"] == "QUEUED", "worker ran while stopped") + check( + sql( + "select published_at IS NULL from processing_outbox where job_id='" + + job(attachment)["job_id"] + + "';" + ) + == "t", + "unexpected publish", + ) + compose("restart", "api") + ready_api() + fault(accepted[0], "before_publish", "crash") + compose("start", "dispatcher") + wait_for(lambda: fault_hit(accepted[0], "before_publish")) + wait_for( + lambda: all( + job(a)["job_id"] in broker("XRANGE", "attachment-jobs", "-", "+") for a in accepted + ) + ) + before = broker("XLEN", "attachment-jobs") + time.sleep(12) + check( + broker("XLEN", "attachment-jobs") == before, + "watchdog multiplied queued broker messages", + ) + compose("restart", "broker") + wait_for(lambda: broker("PING") == "PONG") + history = broker("XRANGE", "attachment-jobs", "-", "+") + check(all(job(a)["job_id"] in history for a in accepted), "broker restart lost messages") + compose("start", "worker") + for attachment in accepted: + self.settled(attachment) + print( + "durability: 202 -> outbox COMMIT without publisher; API restart; publish; broker restart while worker stopped; same jobs ready", + flush=True, + ) + + def publisher_confirm_window(self): + compose("stop", "worker", "dispatcher") + attachment = self.upload() + fault(attachment, "after_publish", "crash") + compose("start", "dispatcher") + wait_for(lambda: fault_hit(attachment, "after_publish")) + compose("start", "worker") + self.settled(attachment) + wait_for(lambda: broker("XLEN", "attachment-jobs") == "0", 60) + check(self.object_count(attachment) == 4, "publisher crash duplicated effect") + + def duplicates(self): + attachment = self.upload() + self.settled(attachment) + value = payload(attachment) + before = manifest(attachment) + attempt = job(attachment)["attempt"] + with ThreadPoolExecutor(max_workers=8) as pool: + list(pool.map(lambda _: inject_duplicate(value), range(24))) + wait_for(lambda: broker("XLEN", "attachment-jobs") == "0") + check( + manifest(attachment) == before and job(attachment)["attempt"] == attempt, + "ready duplicate executed", + ) + check(self.object_count(attachment) == 4, "duplicate S3 object set") + + def object_count(self, attachment): + code = ( + "from pipeline.storage import Storage;from pipeline.config import Settings;s=Storage(Settings());print(len(s.client.list_objects_v2(Bucket=s.settings.bucket,Prefix='results/" + + attachment["id"] + + "/').get('Contents',[])))" + ) + return int(compose("exec", "-T", "dispatcher", "python", "-c", code)) + + def crash(self, point): + attachment = self.prepare(point, "crash") + wait_for(lambda: fault_hit(attachment, point), 60) + self.settled(attachment) + wait_for(lambda: broker("XLEN", "attachment-jobs") == "0", 60) + check(self.object_count(attachment) == 4, "crash redelivery multiplied S3 objects") + expected = 2 if point in ("before_effects", "after_effects") else 1 + check(job(attachment)["attempt"] == expected, "unexpected managed attempt count") + + def lease_fencing(self): + attachment = self.prepare("after_effects", "pause") + wait_for(lambda: fault_hit(attachment, "after_effects"), 60) + self.settled(attachment) + wait_for(lambda: events(attachment, "fenced_stale_result") >= 1, 30) + check(job(attachment)["attempt"] == 2, "lease did not recover work") + check(self.object_count(attachment) == 4, "lease recovery multiplied objects") + + def timeout_recovery(self): + attachment = self.prepare("before_effects", "timeout") + wait_for(lambda: fault_hit(attachment, "before_effects"), 60) + self.settled(attachment) + wait_for(lambda: events(attachment, "worker_timeout") >= 1, 40) + check(self.object_count(attachment) == 4, "timeout recovery multiplied effects") + + def fenced_without_dispatcher(self): + attachment = self.prepare("after_effects", "pause") + wait_for(lambda: fault_hit(attachment, "after_effects"), 60) + compose("stop", "dispatcher") + try: + time.sleep(13) + check(job(attachment)["status"] == "PROCESSING", "test did not hold recovery window") + check(events(attachment, "ready") == 0, "expired worker published result") + check( + int(broker("XLEN", "attachment-jobs")) >= 1, + "unfinished current delivery acknowledged", + ) + check( + int(broker("HLEN", "job-publications")) >= 1, "unfinished publication index removed" + ) + finally: + compose("start", "dispatcher") + self.settled(attachment) + wait_for(lambda: broker("XLEN", "attachment-jobs") == "0", 60) + check( + job(attachment)["attempt"] == 2, "lease recovery did not schedule next managed attempt" + ) + check(self.object_count(attachment) == 4, "recovery multiplied S3 effects") + Path("evidence/fenced-pending.json").write_text( + json.dumps( + { + "attachment_id": attachment["id"], + "unfinished_delivery_retained": True, + "recovered_attempt": 2, + "one_manifest": True, + "s3_objects": 4, + }, + indent=2, + ) + ) + + def version_fencing(self): + attachment = self.prepare("after_effects", "pause") + wait_for(lambda: fault_hit(attachment, "after_effects"), 60) + old = payload(attachment) + operator("upgrade", attachment, 2) + wait_ready(self.client, self.actor, attachment, 90) + check(job(attachment)["pipeline_version"] == 2, "new version missing") + before = manifest(attachment) + wait_for(lambda: events(attachment, "fenced_stale_result") >= 1, 30) + new = payload(attachment) + for value in [new, old, new, old, old]: + inject_duplicate(value) + wait_for(lambda: broker("XLEN", "attachment-jobs") == "0", 60) + check(manifest(attachment) == before, "old result overwrote new version") + check("/v2/" in before and "/v1/" not in before, "wrong manifest version") + check(events(attachment, "ready") == 1, "old in-flight version published") + + def failure_replay(self, action): + attachment = self.prepare("before_effects", action, 3 if action == "transient" else 1) + wait_for(lambda: metadata(self.client, self.actor, attachment)["status"] == "failed", 90) + result = metadata(self.client, self.actor, attachment) + check( + result["error_code"] + == ("retry_exhausted" if action == "transient" else "invalid_image"), + "unsafe failure code", + ) + original = job(attachment) + check( + original["attempt"] == (3 if action == "transient" else 1), "retry budget not enforced" + ) + self.client.call( + "GET", "/api/v1/attachments/" + attachment["id"] + "/content", 409, actor=self.actor + ) + response = operator("replay", attachment) + check(response["replayed"], "replay rejected") + self.settled(attachment) + check(job(attachment)["job_id"] == original["job_id"], "replay replaced logical job") + check(job(attachment)["attempt"] == original["attempt"] + 1, "attempt counter reset") + before = manifest(attachment) + check(not operator("replay", attachment)["replayed"], "successful job replayed") + check(manifest(attachment) == before, "noop replay changed effect") + + def backpressure(self): + compose("stop", "worker", "dispatcher") + output = io.BytesIO() + Image.frombytes("RGB", (800, 600), os.urandom(800 * 600 * 3)).save(output, format="PNG") + body = output.getvalue() + samples = [] + sample_errors = [] + done = threading.Event() + + def sample(): + while not done.is_set(): + try: + value = compose( + "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}} {{.CPUPerc}}" + ) + check(bool(value), "empty stats sample") + samples.append(value) + except Exception as error: + sample_errors.append(type(error).__name__) + return + + sampler = threading.Thread(target=sample) + sampler.start() + try: + with ThreadPoolExecutor(max_workers=10) as pool: + responses = list( + pool.map(lambda _: self.upload(body, expected=[202, 503]), range(10)) + ) + accepted = [r for r in responses if "id" in r] + check(len(accepted) == 6, "admission capacity not atomic across replicas") + started = time.monotonic() + compose("start", "dispatcher", "worker") + latencies = [] + for attachment in accepted: + wait_ready(self.client, self.actor, attachment, 90) + latencies.append(time.monotonic() - started) + finally: + done.set() + sampler.join() + check(not sample_errors and samples, "memory sampling failed") + check(any("worker" in value for value in samples), "worker RSS not sampled") + summary = { + "accepted": len(accepted), + "rejected": 10 - len(accepted), + "concurrency": 10, + "image_bytes": len(body), + "seconds_to_observed_ready": latencies, + "stats_samples": samples, + "method": "closed loop; sequential polls overestimate early completions", + } + Path("evidence/backpressure.json").write_text(json.dumps(summary, indent=2)) + print( + "backpressure: 6 accepted, 4 rejected with 503, accepted jobs drained after recovery", + flush=True, + ) + + +def main(): + experiment = Experiment() + experiment.run_case("commit_publish_api_broker_restart", experiment.durability) + experiment.run_case("publish_confirm_db_mark_crash", experiment.publisher_confirm_window) + experiment.run_case("24_concurrent_duplicate_deliveries", experiment.duplicates) + for point in ["before_effects", "after_effects", "after_commit", "before_ack"]: + experiment.run_case("crash_" + point, lambda point=point: experiment.crash(point)) + experiment.run_case("expired_lease_fences_old_worker", experiment.lease_fencing) + experiment.run_case("hard_process_timeout_recovery", experiment.timeout_recovery) + experiment.run_case("new_version_then_stale_reordered_jobs", experiment.version_fencing) + experiment.run_case( + "permanent_failure_explicit_replay", lambda: experiment.failure_replay("permanent") + ) + experiment.run_case( + "bounded_retry_explicit_replay", lambda: experiment.failure_replay("transient") + ) + experiment.run_case("two_replica_atomic_backpressure_recovery", experiment.backpressure) + wait_for( + lambda: sql( + "select count(*) from attachments where status='READY' and staging_key IS NOT NULL;" + ) + == "0", + 90, + "ready staging not cleaned", + ) + ids = ",".join("'" + value + "'" for value in experiment.accepted) + missing = sql( + "select count(*) from attachments a left join processing_jobs j on j.attachment_id=a.id and j.pipeline_version=a.pipeline_version left join processing_outbox o using(job_id) where a.id in (" + + ids + + ") and (j.job_id is null or o.job_id is null or a.status not in ('READY','FAILED') or j.status<>a.status);" + ) + check(missing == "0", "confirmed task lost or not terminal") + Path("evidence/accepted-jobs.json").write_text( + json.dumps( + { + "accepted_ids": experiment.accepted, + "count": len(experiment.accepted), + "missing_or_nonterminal": int(missing), + }, + indent=2, + ) + ) + print( + "PASS staging cleaned after safe result publication; no confirmed job lost; all pipeline fault invariants", + flush=True, + ) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_filename.py b/student-tests/pipeline_filename.py new file mode 100644 index 0000000..a1860e4 --- /dev/null +++ b/student-tests/pipeline_filename.py @@ -0,0 +1,39 @@ +from client import Client, check, run +from pathlib import Path +from urllib.parse import quote + + +def main(): + c = Client(base_url="http://127.0.0.1:18085", timeout=30) + a, b, e, chat = c.bootstrap() + body = Path("tests/fixtures/sample.png").read_bytes() + path = "/api/v1/chats/" + chat["id"] + "/attachments?filename=" + for name in ["a" * 255, "\U0001f600" * 255]: + attachment = c.call( + "POST", + path + quote(name), + 202, + actor=a, + raw=body, + headers={"Content-Type": "image/png"}, + schema="Attachment", + )["json"] + check(attachment["filename"] == name, "filename changed") + c.wait_ready(a, attachment) + result = c.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=b) + check(result["bytes"] == body, "Unicode filename download failed") + c.call( + "POST", + path + quote("\U0001f600" * 256), + 400, + actor=a, + raw=body, + headers={"Content-Type": "image/png"}, + ) + print( + "PASS filename code-point limits: 255 ASCII and 255 emoji -> 202 and download 200; 256 emoji -> 400; long Content-Disposition accepted by LB" + ) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_history_capacity.py b/student-tests/pipeline_history_capacity.py new file mode 100644 index 0000000..0868cef --- /dev/null +++ b/student-tests/pipeline_history_capacity.py @@ -0,0 +1,53 @@ +from concurrent.futures import ThreadPoolExecutor +import threading +import uuid + +from client import Client, check, run +from pipeline_support import compose, ready_api, sql + + +def main(): + ready_api() + client = Client(base_url="http://127.0.0.1:18085", timeout=30) + actor, _, _, chat = client.bootstrap() + limit = int(compose("exec", "-T", "api", "printenv", "MAX_MESSAGES_PER_CHAT")) + check(1 <= limit <= 10000, "unexpected test history capacity") + sql( + "insert into messages(id,chat_id,sender_id,text,created_at) " + "select gen_random_uuid(),'" + chat["id"] + "','" + actor["id"] + "','seed',now() " + "from generate_series(1," + str(limit - 1) + ");" + ) + barrier = threading.Barrier(10) + + def send(_): + barrier.wait() + return client.call( + "POST", + "/api/v1/chats/" + chat["id"] + "/messages", + [201, 413], + actor=actor, + body={"text": "last slot", "attachment_ids": []}, + headers={"Idempotency-Key": uuid.uuid4().hex}, + ) + + with ThreadPoolExecutor(max_workers=10) as pool: + responses = list(pool.map(send, range(10))) + instances = {item["headers"].get("X-Instance-Id") for item in responses} + check( + len(instances) == 2 and None not in instances, + "last-slot race did not reach both API replicas", + ) + confirmed = [item for item in responses if item["status"] == 201] + rejected = [item for item in responses if item["status"] == 413] + check(len(confirmed) == 1 and len(rejected) == 9, "history capacity raced across API replicas") + check( + int(sql("select count(*) from messages where chat_id='" + chat["id"] + "';")) == limit, + "history capacity exceeded in DB", + ) + print( + "PASS history last-slot race: 10 simultaneous unique POSTs, exactly 1 committed and 9 rejected with 413; DB count equals configured limit" + ) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_retention.py b/student-tests/pipeline_retention.py new file mode 100644 index 0000000..f09558d --- /dev/null +++ b/student-tests/pipeline_retention.py @@ -0,0 +1,140 @@ +from client import Client, check, run +from pipeline_support import compose, sql, broker, ready_api, wait_ready, wait_for, metadata, job +import hashlib +import json +import uuid + + +def execute(code, data=None): + return compose("exec", "-T", "dispatcher", "python", "-c", code, data=data) + + +def main(): + ready_api() + c = Client(base_url="http://127.0.0.1:18085", timeout=30) + a, b, e, chat = c.bootstrap() + protected, original = c.attachment(a, chat) + wait_ready(c, a, protected) + key = sql("select object_key from attachments where id='" + protected["id"] + "';") + status = compose( + "exec", + "-T", + "minio", + "curl", + "-s", + "-o", + "/dev/null", + "-w", + "%{http_code}", + "http://localhost:9000/messenger-attachments/" + key, + ) + check(status == "403", "bucket permits anonymous access") + print("PASS anonymous direct S3 GET of existing original returns 403", flush=True) + compose("stop", "worker") + attachment, _ = c.attachment(a, chat) + code = r"""import hashlib,struct,zlib +from pipeline.storage import Storage +from pipeline.config import Settings +from pipeline.repository import Repository +s=Settings();storage=Storage(s);repo=Repository(s) +def chunk(kind,data):return struct.pack('>I',len(data))+kind+data+struct.pack('>I',zlib.crc32(kind+data)&0xffffffff) +compressor=zlib.compressobj();parts=[];row=b'\0'+b'\0'*(7000*3) +for _ in range(7000):parts.append(compressor.compress(row)) +parts.append(compressor.flush()) +data=b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',7000,7000,8,2,0,0,0))+chunk(b'IDAT',b''.join(parts))+chunk(b'IEND',b'') +with repo.connection() as db: + item=db.execute('select staging_key from attachments where id=%s',('ATTACHMENT',)).fetchone() + storage.client.put_object(Bucket=s.bucket,Key=item['staging_key'],Body=data,ContentType='image/png') + db.execute('update attachments set size_bytes=%s,checksum=%s where id=%s',(len(data),hashlib.sha256(data).hexdigest(),'ATTACHMENT')) +print('injected 49-million-pixel complete PNG into trusted staging with matching metadata') +""".replace( + "ATTACHMENT", attachment["id"] + ) + print(execute(code), flush=True) + compose("start", "worker") + wait_for(lambda: metadata(c, a, attachment)["status"] == "failed", 60) + check( + metadata(c, a, attachment)["error_code"] == "invalid_image", + "worker pixel guard did not reject", + ) + check(job(attachment)["attempt"] == 1, "permanent pixel error retried") + check( + sql( + "select count(*) from attachment_variants where attachment_id='" + + attachment["id"] + + "';" + ) + == "0", + "invalid image published variants", + ) + print( + "PASS worker independently rejects >40 million pixels before full decode; FAILED on first attempt", + flush=True, + ) + sql( + "update attachments set updated_at=now()-interval '2 days' where id='" + + attachment["id"] + + "';" + ) + fresh = "staging/fresh-proof-" + uuid.uuid4().hex + code = r"""from pipeline.storage import Storage +from pipeline.repository import Repository +from pipeline.cleanup import Cleanup +from pipeline.config import Settings +s=Settings();storage=Storage(s);repo=Repository(s) +storage.client.put_object(Bucket=s.bucket,Key='FRESH',Body=b'fresh-proof') +Cleanup(repo,storage).run() +storage.client.head_object(Bucket=s.bucket,Key='FRESH') +print('fresh orphan retained by grace') +storage.client.delete_object(Bucket=s.bucket,Key='FRESH') +""".replace( + "FRESH", fresh + ) + print(execute(code), flush=True) + check( + metadata(c, a, attachment)["error_code"] == "staging_expired", + "expired failed staging retained", + ) + check( + sql("select staging_key IS NULL from attachments where id='" + attachment["id"] + "';") + == "t", + "staging reference retained", + ) + command = [ + "docker", + "compose", + "-f", + "docker-compose.yml", + "-f", + "compose.local.yaml", + "-f", + "compose.test.yaml", + "exec", + "-T", + "dispatcher", + "python", + "-m", + "pipeline.operator", + "replay", + attachment["id"], + ] + import subprocess + + response = subprocess.run(command, capture_output=True, text=True, timeout=30) + check( + response.returncode != 0 and "staging_expired" in response.stderr, "expired replay accepted" + ) + check(metadata(c, a, attachment)["status"] == "failed", "unsafe replay changed status") + check( + c.call("GET", "/api/v1/attachments/" + protected["id"] + "/content", actor=b)["bytes"] + == original, + "cleanup deleted referenced original", + ) + print( + "PASS expired failed staging cleaned; unsafe replay refused; referenced ready bytes retained", + flush=True, + ) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/pipeline_support.py b/student-tests/pipeline_support.py new file mode 100644 index 0000000..30f41f9 --- /dev/null +++ b/student-tests/pipeline_support.py @@ -0,0 +1,171 @@ +import json +import subprocess +import time +from client import Client, check + +COMPOSE = [ + "docker", + "compose", + "-f", + "docker-compose.yml", + "-f", + "compose.local.yaml", + "-f", + "compose.test.yaml", +] + + +def compose(*args, data=None): + result = subprocess.run( + COMPOSE + list(args), input=data, text=True, capture_output=True, timeout=120 + ) + check(result.returncode == 0, "Compose operator action failed: " + args[0]) + return result.stdout.strip() + + +def sql(statement): + return compose( + "exec", + "-T", + "db", + "sh", + "-c", + 'psql -v ON_ERROR_STOP=1 -U "$(cat /run/secrets/postgres_user)" -d "$(cat /run/secrets/postgres_db)" -At', + data=statement, + ) + + +def broker(*args): + return compose("exec", "-T", "broker", "redis-cli", *args) + + +def wait_for(predicate, timeout=90, message="condition timed out"): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + if predicate(): + return + time.sleep(0.25) + raise AssertionError(message) + + +def ready_api(): + observed = set() + + def probe(): + try: + states = compose("ps", "--format", "{{.Health}}", "api").splitlines() + if len(states) != 2 or any(state != "healthy" for state in states): + return False + response = Client(base_url="http://127.0.0.1:18085").call( + "GET", "/health/ready", schema="Health" + ) + observed.add(response["headers"].get("X-Instance-Id")) + return len(observed - {None}) == 2 + except Exception: + return False + + wait_for(probe, 120, "API not ready") + + +def metadata(client, actor, attachment): + return client.call( + "GET", "/api/v1/attachments/" + attachment["id"], actor=actor, schema="Attachment" + )["json"] + + +def wait_ready(client, actor, attachment, timeout=90): + def probe(): + value = metadata(client, actor, attachment) + check(value["status"] != "failed", "attachment failed") + return value["status"] == "ready" + + wait_for(probe, timeout, "attachment not ready") + return metadata(client, actor, attachment) + + +def job(attachment): + text = sql( + "select row_to_json(j) from processing_jobs j where attachment_id='" + + attachment["id"] + + "' order by pipeline_version desc limit 1;" + ) + return json.loads(text) + + +def payload(attachment, version=None): + row = job(attachment) + text = sql("select payload from processing_outbox where job_id='" + row["job_id"] + "';") + value = json.loads(text) + if version is not None: + value["pipeline_version"] = version + return value + + +def inject_duplicate(value): + return broker("XADD", "attachment-jobs", "*", "job", json.dumps(value)) + + +def fault(attachment, point, action, count=1): + version = job(attachment)["pipeline_version"] + sql( + "INSERT INTO pipeline_faults(attachment_id,pipeline_version,point,action,remaining) VALUES ('" + + attachment["id"] + + "'," + + str(version) + + ",'" + + point + + "','" + + action + + "'," + + str(count) + + ");" + ) + + +def fault_hit(attachment, point): + return ( + sql( + "select coalesce(sum(hits),0) from pipeline_faults where attachment_id='" + + attachment["id"] + + "' and point='" + + point + + "';" + ) + != "0" + ) + + +def operator(action, attachment, version=2): + text = compose( + "exec", + "-T", + "dispatcher", + "python", + "-m", + "pipeline.operator", + action, + attachment["id"], + "--version", + str(version), + ) + return json.loads(text) + + +def manifest(attachment): + return sql( + "select name,object_key,sha256 from attachment_variants where attachment_id='" + + attachment["id"] + + "' order by pipeline_version,name;" + ) + + +def events(attachment, event): + return int( + sql( + "select count(*) from pipeline_events e join processing_jobs j using(job_id) where j.attachment_id='" + + attachment["id"] + + "' and e.event='" + + event + + "';" + ) + ) diff --git a/student-tests/pipeline_upload_crash.py b/student-tests/pipeline_upload_crash.py new file mode 100644 index 0000000..8388396 --- /dev/null +++ b/student-tests/pipeline_upload_crash.py @@ -0,0 +1,86 @@ +from concurrent.futures import ThreadPoolExecutor +import json +import uuid +from pathlib import Path + +from client import Client, check, run +from pipeline_support import compose, ready_api, sql, wait_for, wait_ready + + +def staging_keys(): + code = """import json +from pipeline.config import Settings +from pipeline.storage import Storage +s=Storage(Settings()) +print(json.dumps([o['Key'] for p in s.client.get_paginator('list_objects_v2').paginate(Bucket=s.settings.bucket,Prefix='staging/') for o in p.get('Contents',[])])) +""" + return set(json.loads(compose("exec", "-T", "dispatcher", "python", "-c", code))) + + +def main(): + ready_api() + client = Client(base_url="http://127.0.0.1:18085", timeout=45) + actor, _, _, chat = client.bootstrap() + filename = "crash-" + uuid.uuid4().hex + ".png" + before = staging_keys() + sql( + "create function proof_upload_crash() returns trigger language plpgsql as $$ begin " + "if NEW.filename='" + + filename + + "' then perform pg_sleep(45); end if; return NEW; end; $$; " + "create trigger proof_upload_crash before insert on attachments " + "for each row execute function proof_upload_crash();" + ) + try: + with ThreadPoolExecutor(max_workers=1) as pool: + request = pool.submit( + client.call, + "POST", + "/api/v1/chats/" + chat["id"] + "/attachments?filename=" + filename, + 202, + actor=actor, + raw=Path("tests/fixtures/sample.png").read_bytes(), + headers={"Content-Type": "image/png"}, + schema="Attachment", + ) + wait_for( + lambda: sql("select count(*) from pg_stat_activity where wait_event='PgSleep';") + != "0", + 15, + "upload did not reach S3-before-COMMIT window", + ) + orphan = staging_keys() - before + check(len(orphan) == 1, "no staged source before attachment COMMIT") + compose("kill", "-s", "SIGKILL", "api") + try: + request.result(timeout=20) + except Exception: + pass + else: + raise AssertionError("killed upload returned a confirmed attachment") + check( + sql("select count(*) from attachments where filename='" + filename + "';") == "0", + "uncommitted attachment survived process death", + ) + for key in orphan: + check( + sql("select count(*) from attachments where staging_key='" + key + "';") == "0", + "orphan unexpectedly referenced", + ) + finally: + sql("drop trigger proof_upload_crash on attachments; drop function proof_upload_crash();") + compose("start", "api") + ready_api() + attachment, body = client.attachment(actor, chat) + wait_ready(client, actor, attachment) + downloaded = client.call( + "GET", "/api/v1/attachments/" + attachment["id"] + "/content", 200, actor=actor + ) + check(downloaded["bytes"] == body, "upload after API recovery corrupted") + print( + "PASS SIGKILL after S3 PUT before DB COMMIT: no confirmed response, DB rolled back, staged orphan unreferenced; same session uploads after recovery" + ) + + +if __name__ == "__main__": + run(main) diff --git a/student-tests/telemetry_support.py b/student-tests/telemetry_support.py new file mode 100644 index 0000000..c8526ea --- /dev/null +++ b/student-tests/telemetry_support.py @@ -0,0 +1,84 @@ +import json +import os +import time +from pathlib import Path +from urllib.parse import urlencode +from urllib.request import urlopen + +from client import check + + +GRAFANA = os.getenv("GRAFANA_URL", "http://127.0.0.1:13006") +EVIDENCE = Path("evidence") + + +def fetch(path, **params): + suffix = "?" + urlencode(params) if params else "" + with urlopen(GRAFANA + path + suffix, timeout=8) as response: + return json.load(response) + + +def datasource(name, path, **params): + return fetch(f"/api/datasources/proxy/uid/{name}{path}", **params) + + +def prom(expression): + response = datasource("prometheus", "/api/v1/query", query=expression) + check(response["status"] == "success", "PromQL query failed") + return response["data"]["result"] + + +def value(expression, default=0.0): + values = prom(expression) + return float(values[0]["value"][1]) if values else default + + +def states(): + return { + row["name"]: row["state"] + for row in datasource("prometheus", "/api/v1/rules")["data"]["groups"][0]["rules"] + } + + +def logs(query, since=None, limit=100): + result = datasource( + "loki", + "/loki/api/v1/query_range", + query=query, + start=int((since or time.time() - 180) * 1e9), + limit=limit, + ) + return result["data"]["result"] + + +def spans(trace_id): + trace = datasource("tempo", "/api/traces/" + trace_id) + return [ + dict( + span, + service=next( + a["value"]["stringValue"] + for a in batch["resource"]["attributes"] + if a["key"] == "service.name" + ), + ) + for batch in trace.get("batches", trace.get("resourceSpans", [])) + for scope in batch["scopeSpans"] + for span in scope["spans"] + ] + + +def save(name, data): + (EVIDENCE / name).write_text( + json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8" + ) + + +def wait_until(probe, timeout, description): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + result = probe() + if result: + return result + time.sleep(1) + raise AssertionError(description) diff --git a/worker/Dockerfile b/worker/Dockerfile new file mode 100644 index 0000000..bf91ae4 --- /dev/null +++ b/worker/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.12.12-slim@sha256:f3fa41d74a768c2fce8016b98c191ae8c1bacd8f1152870a3f9f87d350920b7c +WORKDIR /app +COPY worker/requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt && useradd --system --uid 10001 worker +COPY worker/pipeline ./pipeline +USER worker +ENV PYTHONUNBUFFERED=1 +CMD ["python", "-m", "pipeline.main"] diff --git a/worker/pipeline/__init__.py b/worker/pipeline/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/worker/pipeline/broker.py b/worker/pipeline/broker.py new file mode 100644 index 0000000..bb3b3ab --- /dev/null +++ b/worker/pipeline/broker.py @@ -0,0 +1,42 @@ +import json + +PUBLISH = """ +local previous = redis.call('HGET', KEYS[2], ARGV[1]) +if previous then + local attempt, id = string.match(previous, '^(%d+)|(%d+%-%d+)$') + if attempt == ARGV[2] and #redis.call('XRANGE', KEYS[1], id, id, 'COUNT', 1) > 0 then + return {id, 0} + end +end +local id = redis.call('XADD', KEYS[1], '*', 'job', ARGV[3]) +redis.call('HSET', KEYS[2], ARGV[1], ARGV[2] .. '|' .. id) +return {id, 1} +""" + +ACK = """ +redis.call('XACK', KEYS[1], ARGV[1], ARGV[2]) +redis.call('XDEL', KEYS[1], ARGV[2]) +local previous = redis.call('HGET', KEYS[2], ARGV[3]) +if previous then + local attempt, id = string.match(previous, '^(%d+)|(%d+%-%d+)$') + if id == ARGV[2] then redis.call('HDEL', KEYS[2], ARGV[3]) end +end +return 1 +""" + + +def publish(broker, settings, payload): + result = broker.eval( + PUBLISH, + 2, + settings.stream, + "job-publications", + payload["job_id"], + payload["attempt"], + json.dumps(payload), + ) + return bool(result[1]) + + +def acknowledge(broker, settings, message_id, job_id=""): + broker.eval(ACK, 2, settings.stream, "job-publications", settings.group, message_id, job_id) diff --git a/worker/pipeline/cleanup.py b/worker/pipeline/cleanup.py new file mode 100644 index 0000000..701f6ca --- /dev/null +++ b/worker/pipeline/cleanup.py @@ -0,0 +1,64 @@ +from datetime import datetime, timezone, timedelta + + +class Cleanup: + def __init__(self, repository, storage): + self.repository = repository + self.storage = storage + + def run(self): + with self.repository.connection() as db: + row = db.execute("SELECT pg_try_advisory_xact_lock(741204) AS acquired").fetchone() + if not row["acquired"]: + return + self._clear_failed(db) + self._clear_ready(db) + self._scan(db) + + def _clear_ready(self, db): + rows = db.execute( + """SELECT id,staging_key FROM attachments WHERE status='READY' + AND staging_key IS NOT NULL LIMIT 100 FOR UPDATE SKIP LOCKED""" + ).fetchall() + for row in rows: + self.storage.client.delete_object( + Bucket=self.storage.settings.bucket, Key=row["staging_key"] + ) + db.execute("UPDATE attachments SET staging_key=NULL WHERE id=%s", (row["id"],)) + + def _clear_failed(self, db): + rows = db.execute( + """SELECT id,staging_key,pipeline_version FROM attachments WHERE status='FAILED' + AND staging_key IS NOT NULL AND updated_at < now() - %s * interval '1 second' + LIMIT 100 FOR UPDATE SKIP LOCKED""", + (self.storage.settings.retention,), + ).fetchall() + for row in rows: + self.storage.client.delete_object( + Bucket=self.storage.settings.bucket, Key=row["staging_key"] + ) + db.execute( + "UPDATE attachments SET staging_key=NULL,error_code='staging_expired' WHERE id=%s", + (row["id"],), + ) + db.execute( + "UPDATE processing_jobs SET error_code='staging_expired' WHERE attachment_id=%s AND pipeline_version=%s", + (row["id"], row["pipeline_version"]), + ) + + def _scan(self, db): + cutoff = datetime.now(timezone.utc) - timedelta(seconds=self.storage.settings.retention) + pages = self.storage.client.get_paginator("list_objects_v2") + for page in pages.paginate(Bucket=self.storage.settings.bucket): + for item in page.get("Contents", []): + if item["LastModified"] < cutoff and not self._referenced(db, item["Key"]): + self.storage.client.delete_object( + Bucket=self.storage.settings.bucket, Key=item["Key"] + ) + + def _referenced(self, db, key): + return db.execute( + """SELECT 1 FROM attachments WHERE object_key=%s OR staging_key=%s + UNION ALL SELECT 1 FROM attachment_variants WHERE object_key=%s LIMIT 1""", + (key, key, key), + ).fetchone() diff --git a/worker/pipeline/config.py b/worker/pipeline/config.py new file mode 100644 index 0000000..c660ccd --- /dev/null +++ b/worker/pipeline/config.py @@ -0,0 +1,24 @@ +import os +from dataclasses import dataclass +from pathlib import Path + + +def secret(name): + return Path("/run/secrets/" + name).read_text().strip() + + +@dataclass(frozen=True) +class Settings: + db_host: str = os.getenv("DB_HOST", "db") + broker_host: str = os.getenv("BROKER_HOST", "broker") + bucket: str = os.getenv("S3_BUCKET", "messenger-attachments") + endpoint: str = os.getenv("S3_ENDPOINT", "http://minio:9000") + max_attempts: int = int(os.getenv("JOB_MAX_ATTEMPTS", "5")) + timeout: int = int(os.getenv("JOB_TIMEOUT_SECONDS", "60")) + retention: int = int(os.getenv("STAGING_RETENTION_SECONDS", "86400")) + lease: int = int(os.getenv("JOB_LEASE_SECONDS", "75")) + faults: bool = os.getenv("FAULT_INJECTION_ENABLED", "false").lower() == "true" + max_bytes: int = 10485760 + max_pixels: int = 40000000 + stream: str = "attachment-jobs" + group: str = "image-workers" diff --git a/worker/pipeline/faults.py b/worker/pipeline/faults.py new file mode 100644 index 0000000..096b5d1 --- /dev/null +++ b/worker/pipeline/faults.py @@ -0,0 +1,16 @@ +import os +import time + + +def inject(repository, job, point): + action = repository.fault(job, point) + if action == "crash": + os._exit(71) + if action == "pause": + time.sleep(12) + if action == "timeout": + time.sleep(repository.settings.timeout + 5) + if action == "transient": + raise TimeoutError("injected_transient") + if action == "permanent": + raise ValueError("injected_permanent") diff --git a/worker/pipeline/images.py b/worker/pipeline/images.py new file mode 100644 index 0000000..f6c8a89 --- /dev/null +++ b/worker/pipeline/images.py @@ -0,0 +1,63 @@ +import io +import warnings +from PIL import Image, ImageOps + + +class ImagePipeline: + def __init__(self, settings, version=1): + self.settings = settings + self.jpeg_quality = 85 if version == 1 else 90 + self.webp_quality = 80 if version == 1 else 85 + Image.MAX_IMAGE_PIXELS = settings.max_pixels + warnings.simplefilter("error", Image.DecompressionBombWarning) + + def render(self, data): + with Image.open(io.BytesIO(data)) as source: + self.validate(source) + source.load() + ImageOps.exif_transpose(source, in_place=True) + image = self.flatten(source) + try: + yield self.scaled(image, "thumbnail", 256, "JPEG", self.jpeg_quality) + yield self.crop(image) + yield self.scaled(image, "optimized", 1280, "WEBP", self.webp_quality) + finally: + image.close() + + def validate(self, image): + if image.format not in ("PNG", "JPEG"): + raise ValueError("invalid_image") + if image.width * image.height > self.settings.max_pixels: + raise ValueError("image_too_large") + + def flatten(self, image): + if "A" in image.getbands() or "transparency" in image.info: + with image.convert("RGBA") as alpha: + result = Image.new("RGB", alpha.size, "white") + result.paste(alpha, mask=alpha.getchannel("A")) + return result + return image.convert("RGB") + + def scaled(self, image, name, limit, format_name, quality): + divisor = max(limit, image.width, image.height) + size = (max(1, image.width * limit // divisor), max(1, image.height * limit // divisor)) + with image.resize(size, Image.Resampling.LANCZOS) as resized: + return self.encode(resized, name, format_name, quality) + + def crop(self, image): + with ImageOps.fit( + image, (128, 128), method=Image.Resampling.LANCZOS, centering=(0.5, 0.5) + ) as cropped: + return self.encode(cropped, "crop", "JPEG", self.jpeg_quality) + + def encode(self, image, name, format_name, quality): + output = io.BytesIO() + image.info.clear() + image.save(output, format=format_name, quality=quality) + return { + "name": name, + "data": output.getvalue(), + "width": image.width, + "height": image.height, + "content_type": "image/jpeg" if format_name == "JPEG" else "image/webp", + } diff --git a/worker/pipeline/main.py b/worker/pipeline/main.py new file mode 100644 index 0000000..c8e54e2 --- /dev/null +++ b/worker/pipeline/main.py @@ -0,0 +1,161 @@ +import json +import os +import subprocess +import sys +import time +import uuid +from datetime import datetime +import redis +from .config import Settings +from .repository import Repository +from .cleanup import Cleanup +from .storage import Storage +from .outbox import OutboxDispatcher +from .faults import inject +from .broker import acknowledge +from opentelemetry.trace import SpanKind, Status, StatusCode +from .telemetry import PROPAGATOR, PipelineMetrics, child_environment, event, setup, span + + +def validate_message(payload): + required = { + "schema_version", + "job_id", + "type", + "attachment_id", + "pipeline_version", + "attempt", + "created_at", + } + allowed = required | {"traceparent", "tracestate"} + if not isinstance(payload, dict) or not required.issubset(payload) or set(payload) - allowed: + raise ValueError("invalid_job_fields") + if ( + type(payload["schema_version"]) is not int + or payload["schema_version"] != 1 + or payload["type"] != "attachment.process.v1" + ): + raise ValueError("invalid_job_type") + if any(not isinstance(payload[key], str) for key in ("job_id", "attachment_id", "created_at")): + raise ValueError("invalid_job_strings") + uuid.UUID(payload["job_id"]) + uuid.UUID(payload["attachment_id"]) + for key in ("pipeline_version", "attempt"): + if type(payload[key]) is not int or payload[key] < 1: + raise ValueError("invalid_job_counter") + if datetime.fromisoformat(payload["created_at"].replace("Z", "+00:00")).tzinfo is None: + raise ValueError("invalid_job_time") + + +def process_delivery(settings, payload): + result = subprocess.run( + [sys.executable, "-m", "pipeline.process", json.dumps(payload)], + timeout=settings.timeout, + env=child_environment(), + stdout=subprocess.PIPE, + text=True, + ) + outcomes = [json.loads(line) for line in result.stdout.splitlines()] + outcome = outcomes[0] if outcomes else {"outcome": "crash"} + return result.returncode, outcome + + +def handle(broker, settings, repository, item, metrics): + message_id, fields = item + try: + payload = json.loads(fields["job"]) + validate_message(payload) + except (ValueError, KeyError, TypeError): + acknowledge(broker, settings, message_id) + event("worker.rejected_job", error=True) + return + with span( + "attachments consume", + SpanKind.CONSUMER, + PROPAGATOR.extract(payload), + { + "messaging.destination.name": "attachments", + "job.attempt": payload["attempt"], + "pipeline.version": payload["pipeline_version"], + }, + ) as active: + started = time.monotonic() + metrics.busy = 1 + try: + code, result = process_delivery(settings, payload) + except subprocess.TimeoutExpired: + with repository.connection() as db: + repository.event(db, payload, "worker_timeout") + code, result = 124, {"outcome": "timeout"} + finally: + metrics.busy = 0 + metrics.complete(started, result) + outcome = result["outcome"] + failed = outcome in ("retry", "failed", "timeout", "crash") + active.set_attribute("job.outcome", outcome) + if failed: + active.set_status(Status(StatusCode.ERROR)) + event("worker.delivery", error=failed, outcome=outcome, attempt=payload["attempt"]) + if code == 0: + inject(repository, payload, "before_ack") + acknowledge(broker, settings, message_id, payload["job_id"]) + elif code == 75: + time.sleep(0.25) + + +def messages(broker, settings, consumer): + pending = broker.xreadgroup(settings.group, consumer, {settings.stream: "0"}, count=1) + if pending and pending[0][1]: + return pending[0][1] + claimed = broker.xautoclaim( + settings.stream, settings.group, consumer, settings.lease * 1000, "0-0", count=1 + ) + if claimed[1]: + return claimed[1] + rows = broker.xreadgroup(settings.group, consumer, {settings.stream: ">"}, count=1, block=1000) + return rows[0][1] if rows else [] + + +def consume(broker, settings, repository, consumer, metrics): + try: + broker.xgroup_create(settings.stream, settings.group, id="0", mkstream=True) + except redis.ResponseError as error: + if "BUSYGROUP" not in str(error): + raise + for item in messages(broker, settings, consumer): + handle(broker, settings, repository, item, metrics) + + +def main(): + role = os.getenv("ROLE", "worker") + setup(role, metrics=True) + metrics = PipelineMetrics(role) + settings = Settings() + repository = Repository(settings) + broker = redis.Redis( + host=settings.broker_host, decode_responses=True, socket_connect_timeout=3, socket_timeout=3 + ) + cleanup = Cleanup(repository, Storage(settings)) + consumer = str(uuid.uuid4()) + dispatcher = OutboxDispatcher(repository, broker) + cleaned = 0 + while True: + try: + if role == "dispatcher": + dispatcher.run() + metrics.observe_queue(repository) + if time.monotonic() - cleaned > 60: + cleanup.run() + cleaned = time.monotonic() + time.sleep(0.5) + else: + consume(broker, settings, repository, consumer, metrics) + except Exception as error: + print( + json.dumps({"event": "loop_error", "error_type": type(error).__name__}), flush=True + ) + time.sleep(1) + + +if __name__ == "__main__": + main() diff --git a/worker/pipeline/operator.py b/worker/pipeline/operator.py new file mode 100644 index 0000000..b97f1d4 --- /dev/null +++ b/worker/pipeline/operator.py @@ -0,0 +1,104 @@ +import argparse +import json +import uuid +from .config import Settings +from .repository import Repository + + +def enqueue(db, attachment, version, settings): + job_id = uuid.uuid4() + row = db.execute( + """INSERT INTO processing_jobs(job_id,attachment_id,pipeline_version,attempt_limit) + VALUES (%s,%s,%s,%s) RETURNING created_at""", + (job_id, attachment, version, settings.max_attempts), + ).fetchone() + payload = { + "schema_version": 1, + "job_id": str(job_id), + "type": "attachment.process.v1", + "attachment_id": str(attachment), + "pipeline_version": version, + "attempt": 1, + "created_at": row["created_at"].isoformat().replace("+00:00", "Z"), + } + db.execute( + "INSERT INTO processing_outbox(job_id,payload) VALUES (%s,%s::jsonb)", + (job_id, json.dumps(payload)), + ) + return {"job_id": job_id, "attempt": 0} + + +def upgrade(repository, attachment, version): + with repository.connection() as db: + db.execute("SELECT pg_advisory_xact_lock_shared(741204)") + current = db.execute( + "SELECT * FROM attachments WHERE id=%s FOR UPDATE", (attachment,) + ).fetchone() + if not current or version <= current["pipeline_version"]: + raise ValueError("version_must_increase") + db.execute( + "UPDATE processing_jobs SET status='SUPERSEDED',lease_until=NULL WHERE attachment_id=%s AND status IN ('QUEUED','PROCESSING','FAILED')", + (attachment,), + ) + db.execute( + "UPDATE attachments SET pipeline_version=%s,status='QUEUED',error_code=NULL,updated_at=now() WHERE id=%s", + (version, attachment), + ) + job = enqueue(db, attachment, version, repository.settings) + repository.event(db, job, "operator_upgrade") + return {"event": "upgraded", "version": version} + + +def replay(repository, attachment): + with repository.connection() as db: + db.execute("SELECT pg_advisory_xact_lock_shared(741204)") + current = db.execute( + "SELECT * FROM attachments WHERE id=%s FOR UPDATE", (attachment,) + ).fetchone() + if not current: + raise ValueError("attachment_not_found") + job = db.execute( + "SELECT * FROM processing_jobs WHERE attachment_id=%s AND pipeline_version=%s FOR UPDATE", + (attachment, current["pipeline_version"]), + ).fetchone() + if job["status"] == "READY": + repository.event(db, job, "operator_replay_noop") + return {"event": "already_ready", "replayed": False} + if job["error_code"] == "staging_expired": + raise ValueError("staging_expired") + if job["status"] != "FAILED": + raise ValueError("only_failed_can_be_replayed") + reset(db, job, repository.settings.max_attempts) + repository.event(db, job, "operator_replay") + return {"event": "replayed", "replayed": True} + + +def reset(db, job, budget): + db.execute( + """UPDATE processing_jobs SET status='QUEUED',error_code=NULL,lease_token=NULL, + lease_until=NULL,completed_at=NULL,attempt_limit=attempt+%s,next_at=now(),next_publish_at=now() WHERE job_id=%s""", + (budget, job["job_id"]), + ) + db.execute( + "UPDATE attachments SET status='QUEUED',error_code=NULL,updated_at=now() WHERE id=%s", + (job["attachment_id"],), + ) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["upgrade", "replay"]) + parser.add_argument("attachment", type=uuid.UUID) + parser.add_argument("--version", type=int, default=2) + args = parser.parse_args() + repository = Repository(Settings()) + result = ( + upgrade(repository, args.attachment, args.version) + if args.action == "upgrade" + else replay(repository, args.attachment) + ) + print(json.dumps(result), flush=True) + + +if __name__ == "__main__": + main() diff --git a/worker/pipeline/outbox.py b/worker/pipeline/outbox.py new file mode 100644 index 0000000..cddafe9 --- /dev/null +++ b/worker/pipeline/outbox.py @@ -0,0 +1,61 @@ +from .faults import inject +from .broker import publish +from opentelemetry.trace import SpanKind +from .telemetry import PROPAGATOR, current_carrier, event, span + + +class OutboxDispatcher: + def __init__(self, repository, broker): + self.repository = repository + self.broker = broker + + def run(self): + self.recover_expired() + with self.repository.connection() as db: + rows = db.execute( + """SELECT j.*,o.payload,o.published_at AS previous_publish FROM processing_jobs j + JOIN attachments a ON a.id=j.attachment_id AND a.pipeline_version=j.pipeline_version + JOIN processing_outbox o ON o.job_id=j.job_id + WHERE j.status='QUEUED' AND j.next_at<=now() AND j.next_publish_at<=now() + ORDER BY j.created_at LIMIT 20 FOR UPDATE OF o SKIP LOCKED""" + ).fetchall() + for job in rows: + self.publish(db, job) + + def publish(self, db, job): + inject(self.repository, job, "before_publish") + payload = dict(job["payload"], attempt=job["attempt"] + 1) + with span( + "attachments publish", + SpanKind.PRODUCER, + PROPAGATOR.extract(payload), + {"messaging.destination.name": "attachments", "job.attempt": payload["attempt"]}, + ): + current_carrier(payload) + created = publish(self.broker, self.repository.settings, payload) + event("outbox.published", attempt=payload["attempt"], publication_created=created) + inject(self.repository, job, "after_publish") + db.execute( + "UPDATE processing_jobs SET next_publish_at=now()+interval '10 seconds' WHERE job_id=%s", + (job["job_id"],), + ) + db.execute( + "UPDATE processing_outbox SET published_at=now(),publish_count=publish_count+1 WHERE job_id=%s", + (job["job_id"],), + ) + if created or job["previous_publish"] is None: + self.repository.event(db, dict(job, attempt=payload["attempt"]), "published") + + def recover_expired(self): + with self.repository.connection() as db: + rows = db.execute( + """SELECT j.* FROM processing_jobs j + JOIN attachments a ON a.id=j.attachment_id AND a.pipeline_version=j.pipeline_version + WHERE j.status='PROCESSING' AND j.lease_until= job["attempt_limit"] + code = "retry_exhausted" if terminal else "lease_expired" + self.repository.set_failure(db, job, "FAILED" if terminal else "QUEUED", code, 1) + self.repository.event(db, job, "lease_expired") diff --git a/worker/pipeline/process.py b/worker/pipeline/process.py new file mode 100644 index 0000000..49f5648 --- /dev/null +++ b/worker/pipeline/process.py @@ -0,0 +1,91 @@ +import hashlib +import json +import resource +import sys +from PIL import UnidentifiedImageError, Image +from .config import Settings +from .repository import Repository +from .storage import Storage +from .images import ImagePipeline +from .faults import inject +from .telemetry import child_context, event, setup, span + + +def write_effects(storage, job, data, settings): + prefix = f"results/{job['attachment_id']}/v{job['pipeline_version']}" + original = prefix + "/original-" + job["checksum"] + storage.write_result(original, data, job["content_type"]) + variants = [] + for variant in ImagePipeline(settings, job["pipeline_version"]).render(data): + body = variant.pop("data") + key = prefix + "/" + variant["name"] + "-" + hashlib.sha256(body).hexdigest() + item = storage.write_result(key, body, variant["content_type"]) + variants.append( + dict(item, name=variant["name"], width=variant["width"], height=variant["height"]) + ) + return original, variants + + +def process(repository, settings, job): + storage = Storage(settings) + with repository.connection() as fence: + fence.execute("SELECT pg_advisory_xact_lock_shared(741204)") + inject(repository, job, "before_effects") + with span("s3.staging.read"): + data = storage.read_original(job) + with span("image.render_and_write"): + original, variants = write_effects(storage, job, data, settings) + inject(repository, job, "after_effects") + with span("postgres.publish_result"): + ready_seconds = repository.publish_result(job, original, variants) + if ready_seconds is not False: + report("ready", ready_seconds=ready_seconds) + else: + report("deferred") + inject(repository, job, "after_commit") + if ready_seconds is not False: + storage.remove_staging(job, repository) + + +def report(outcome, **fields): + print(json.dumps({"outcome": outcome, **fields}), flush=True) + event("worker." + outcome, error=outcome in ("retry", "failed")) + + +def execute(message, settings): + repository = Repository(settings) + with span("postgres.claim"): + job = repository.claim(message) + if not job: + done = repository.delivery_done(message) + report("duplicate" if done else "deferred") + return 0 if done else 75 + try: + process(repository, settings, job) + except ( + ValueError, + UnidentifiedImageError, + Image.DecompressionBombError, + Image.DecompressionBombWarning, + MemoryError, + ): + report(repository.fail(job, permanent=True, code="invalid_image") or "deferred") + except Exception: + report(repository.fail(job) or "deferred") + return 0 if repository.delivery_done(message) else 75 + + +def main(): + resource.setrlimit(resource.RLIMIT_AS, (768 * 1024 * 1024, 768 * 1024 * 1024)) + providers = setup("worker") + message = json.loads(sys.argv[1]) + try: + with span("attachment.process", parent=child_context()): + return execute(message, Settings()) + finally: + for provider in providers: + provider.force_flush(1200) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/worker/pipeline/repository.py b/worker/pipeline/repository.py new file mode 100644 index 0000000..70b2ebc --- /dev/null +++ b/worker/pipeline/repository.py @@ -0,0 +1,171 @@ +import json +import random +import uuid +import psycopg +from psycopg.rows import dict_row +from .config import secret + + +class Repository: + def __init__(self, settings): + self.settings = settings + + def connection(self): + return psycopg.connect( + host=self.settings.db_host, + dbname=secret("postgres_db"), + user=secret("postgres_user"), + password=secret("postgres_password"), + connect_timeout=3, + options="-c statement_timeout=10000", + row_factory=dict_row, + ) + + def event(self, db, job, name): + db.execute( + "INSERT INTO pipeline_events(job_id,event,attempt) VALUES (%s,%s,%s)", + (job["job_id"], name, job.get("attempt", 0)), + ) + + def claim(self, message): + with self.connection() as db: + job = self._lock_job(db, message) + if ( + not job + or job["status"] != "QUEUED" + or not job["due"] + or message["attempt"] != job["attempt"] + 1 + ): + return None + token = uuid.uuid4() + db.execute( + """UPDATE processing_jobs SET status='PROCESSING', attempt=attempt+1, + lease_token=%s, lease_until=now() + %s * interval '1 second' WHERE job_id=%s""", + (token, self.settings.lease, job["job_id"]), + ) + db.execute( + "UPDATE attachments SET status='PROCESSING',updated_at=now() WHERE id=%s", + (job["attachment_id"],), + ) + job.update(lease_token=token, attempt=job["attempt"] + 1) + self.event(db, job, "claimed") + return job + + def _lock_job(self, db, message): + return db.execute( + """SELECT j.*, a.object_key, a.staging_key, a.content_type, + a.checksum, a.size_bytes, j.next_at <= now() AS due, j.lease_until > now() AS live + FROM attachments a JOIN processing_jobs j ON a.id=j.attachment_id + WHERE j.job_id=%s AND j.attachment_id=%s AND j.pipeline_version=%s + AND a.pipeline_version=j.pipeline_version + FOR UPDATE OF a,j SKIP LOCKED""", + (message["job_id"], message["attachment_id"], message["pipeline_version"]), + ).fetchone() + + def delivery_done(self, message): + with self.connection() as db: + row = db.execute( + """SELECT j.status,j.attempt,a.pipeline_version FROM processing_jobs j + JOIN attachments a ON a.id=j.attachment_id WHERE j.job_id=%s AND j.attachment_id=%s""", + (message["job_id"], message["attachment_id"]), + ).fetchone() + if not row or row["pipeline_version"] != message["pipeline_version"]: + return True + if row["status"] not in ("QUEUED", "PROCESSING"): + return True + expected = row["attempt"] + (1 if row["status"] == "QUEUED" else 0) + return message["attempt"] != expected + + def publish_result(self, job, original_key, variants): + with self.connection() as db: + current = self._lock_job(db, job) + if not self._owns(current, job): + self.event(db, job, "fenced_stale_result") + return False + self._save_variants(db, job, variants) + db.execute( + """UPDATE attachments SET object_key=%s,status='READY',error_code=NULL, + updated_at=now() WHERE id=%s AND pipeline_version=%s""", + (original_key, job["attachment_id"], job["pipeline_version"]), + ) + result = db.execute( + """UPDATE processing_jobs SET status='READY',completed_at=now(), + lease_until=NULL,error_code=NULL WHERE job_id=%s + RETURNING EXTRACT(EPOCH FROM completed_at-created_at) AS ready_seconds""", + (job["job_id"],), + ) + self.event(db, job, "ready") + return float(result.fetchone()["ready_seconds"]) + + def _owns(self, current, job): + return ( + current + and current["status"] == "PROCESSING" + and current["lease_token"] == job["lease_token"] + and current["live"] + ) + + def _save_variants(self, db, job, variants): + for item in variants: + db.execute( + """INSERT INTO attachment_variants + (attachment_id,pipeline_version,name,object_key,content_type,size_bytes,width,height,sha256) + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING""", + ( + job["attachment_id"], + job["pipeline_version"], + item["name"], + item["object_key"], + item["content_type"], + item["size_bytes"], + item["width"], + item["height"], + item["sha256"], + ), + ) + + def fail(self, job, permanent=False, code="processing_failed"): + with self.connection() as db: + current = self._lock_job(db, job) + if not self._owns(current, job): + return + terminal = permanent or job["attempt"] >= job["attempt_limit"] + status = "FAILED" if terminal else "QUEUED" + delay = min(30, 2 ** job["attempt"]) + random.uniform(0, 1) + code = code if permanent else ("retry_exhausted" if terminal else code) + self.set_failure(db, job, status, code, delay) + self.event(db, job, "failed" if terminal else "retry_scheduled") + return "failed" if terminal else "retry" + + def set_failure(self, db, job, status, code, delay): + db.execute( + """UPDATE processing_jobs SET status=%s,error_code=%s,lease_until=NULL, + next_at=now() + %s * interval '1 second',next_publish_at=now(), + completed_at=CASE WHEN %s='FAILED' THEN now() ELSE NULL END WHERE job_id=%s""", + (status, code, delay, status, job["job_id"]), + ) + db.execute( + "UPDATE attachments SET status=%s,error_code=%s,updated_at=now() WHERE id=%s", + (status, code if status == "FAILED" else None, job["attachment_id"]), + ) + + def clear_staging(self, job): + with self.connection() as db: + result = db.execute( + """UPDATE attachments SET staging_key=NULL WHERE id=%s AND pipeline_version=%s + AND status='READY' AND staging_key=%s""", + (job["attachment_id"], job["pipeline_version"], job["staging_key"]), + ) + return result.rowcount == 1 + + def fault(self, job, point): + if not self.settings.faults: + return None + with self.connection() as db: + row = db.execute( + """UPDATE pipeline_faults SET remaining=remaining-1,hits=hits+1 + WHERE attachment_id=%s AND pipeline_version=%s AND point=%s AND remaining>0 + RETURNING action""", + (job["attachment_id"], job["pipeline_version"], point), + ).fetchone() + return row["action"] if row else None diff --git a/worker/pipeline/storage.py b/worker/pipeline/storage.py new file mode 100644 index 0000000..6ed47db --- /dev/null +++ b/worker/pipeline/storage.py @@ -0,0 +1,62 @@ +import hashlib +import boto3 +from botocore.config import Config +from botocore.exceptions import ClientError +from .config import secret + + +class Storage: + def __init__(self, settings): + self.settings = settings + self.client = boto3.client( + "s3", + endpoint_url=settings.endpoint, + region_name="us-east-1", + aws_access_key_id=secret("s3_access_key_id"), + aws_secret_access_key=secret("s3_secret_access_key"), + config=Config( + connect_timeout=3, + read_timeout=10, + retries={"max_attempts": 1}, + s3={"addressing_style": "path"}, + ), + ) + + def read_original(self, job): + key = job["staging_key"] or job["object_key"] + try: + data = self._read(key) + except ClientError as error: + if error.response["Error"]["Code"] != "NoSuchKey" or key == job["object_key"]: + raise + data = self._read(job["object_key"]) + if ( + len(data) > self.settings.max_bytes + or len(data) != job["size_bytes"] + or hashlib.sha256(data).hexdigest() != job["checksum"] + ): + raise ValueError("invalid_staging") + return data + + def _read(self, key): + response = self.client.get_object(Bucket=self.settings.bucket, Key=key) + with response["Body"] as stream: + return stream.read(self.settings.max_bytes + 1) + + def write_result(self, key, data, content_type): + self.client.put_object( + Bucket=self.settings.bucket, Key=key, Body=data, ContentType=content_type + ) + return { + "object_key": key, + "content_type": content_type, + "size_bytes": len(data), + "sha256": hashlib.sha256(data).hexdigest(), + } + + def remove_staging(self, job, repository): + if not job["staging_key"]: + return + if not repository.clear_staging(job): + return + self.client.delete_object(Bucket=self.settings.bucket, Key=job["staging_key"]) diff --git a/worker/pipeline/telemetry.py b/worker/pipeline/telemetry.py new file mode 100644 index 0000000..a380dcb --- /dev/null +++ b/worker/pipeline/telemetry.py @@ -0,0 +1,187 @@ +import logging +import os +import socket +import time +from contextlib import contextmanager +from pathlib import Path + +from opentelemetry import trace +from opentelemetry._logs import set_logger_provider +from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter +from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter +from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter +from opentelemetry.metrics import Observation, get_meter +from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler +from opentelemetry.sdk._logs.export import BatchLogRecordProcessor +from opentelemetry.sdk.metrics import MeterProvider +from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader +from opentelemetry.sdk.metrics.view import ExplicitBucketHistogramAggregation, View +from opentelemetry.sdk.resources import Resource +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor +from opentelemetry.trace import SpanKind, Status, StatusCode +from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator + + +PROPAGATOR = TraceContextTextMapPropagator() +LOGGER = logging.getLogger("pipeline.telemetry") +BUCKETS = [0.05, 0.1, 0.25, 0.5, 1, 2, 3, 5, 10, 20, 30, 60, 120, 300] + + +def setup(role, metrics=False): + endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT") + if not endpoint: + return () + resource = Resource.create( + { + "service.name": "messenger-" + role, + "service.version": "6.0.0", + "service.instance.id": socket.gethostname(), + "deployment.environment.name": "lab", + } + ) + tracer = TracerProvider(resource=resource) + tracer.add_span_processor( + BatchSpanProcessor( + OTLPSpanExporter(timeout=1), max_queue_size=128, max_export_batch_size=32 + ) + ) + trace.set_tracer_provider(tracer) + logger = LoggerProvider(resource=resource) + logger.add_log_record_processor( + BatchLogRecordProcessor( + OTLPLogExporter(timeout=1), max_queue_size=128, max_export_batch_size=32 + ) + ) + set_logger_provider(logger) + LOGGER.addHandler(LoggingHandler(logger_provider=logger)) + LOGGER.setLevel(logging.INFO) + LOGGER.propagate = False + if not metrics: + return tracer, logger + reader = PeriodicExportingMetricReader( + OTLPMetricExporter(timeout=1), export_interval_millis=5000, export_timeout_millis=1500 + ) + meter = MeterProvider( + resource=resource, + metric_readers=[reader], + views=[ + View( + instrument_name="lab_*_duration", + aggregation=ExplicitBucketHistogramAggregation(BUCKETS), + ) + ], + ) + from opentelemetry.metrics import set_meter_provider + + set_meter_provider(meter) + return tracer, logger, meter + + +def event(name, error=False, **fields): + LOGGER.log(logging.ERROR if error else logging.INFO, name, extra={"event": name, **fields}) + + +@contextmanager +def span(name, kind=SpanKind.INTERNAL, parent=None, attributes=None): + with trace.get_tracer("messenger.pipeline").start_as_current_span( + name, + context=parent, + kind=kind, + attributes=attributes, + record_exception=False, + set_status_on_exception=False, + ) as active: + try: + yield active + except Exception as error: + active.set_status(Status(StatusCode.ERROR)) + active.set_attribute("error.type", type(error).__name__) + raise + + +def child_environment(): + carrier = {} + PROPAGATOR.inject(carrier) + return { + **os.environ, + "PIPELINE_TRACEPARENT": carrier.get("traceparent", ""), + "PIPELINE_TRACESTATE": carrier.get("tracestate", ""), + } + + +def child_context(): + return PROPAGATOR.extract( + { + "traceparent": os.getenv("PIPELINE_TRACEPARENT", ""), + "tracestate": os.getenv("PIPELINE_TRACESTATE", ""), + } + ) + + +def current_carrier(payload): + payload.pop("tracestate", None) + PROPAGATOR.inject(payload) + + +class PipelineMetrics: + def __init__(self, role): + meter = get_meter("messenger.pipeline") + self.jobs = meter.create_counter("lab_jobs") + self.duration = meter.create_histogram("lab_job_duration", unit="s") + self.ready = meter.create_histogram("lab_ready_duration", unit="s") + self.terminal = meter.create_counter("lab_ready_sli") + self.busy = 0 + self.depth = 0 + self.age = 0.0 + for outcome in ("ready", "duplicate", "retry", "failed", "timeout", "crash", "deferred"): + self.jobs.add(0, {"job_type": "attachment", "outcome": outcome}) + for outcome in ("good", "bad"): + self.terminal.add(0, {"outcome": outcome}) + if role == "worker": + meter.create_observable_gauge("lab_worker_busy", [self.observe_busy]) + meter.create_observable_gauge("lab_worker_capacity", [lambda _: [Observation(1)]]) + else: + meter.create_observable_gauge("lab_queue_depth", [self.observe_depth]) + meter.create_observable_gauge("lab_queue_oldest", [self.observe_age], unit="s") + meter.create_observable_counter("lab_process_cpu", [observe_cpu], unit="s") + meter.create_observable_gauge("lab_process_rss", [observe_rss], unit="By") + + def observe_busy(self, _): + return [Observation(self.busy)] + + def observe_depth(self, _): + return [Observation(self.depth, {"queue": "attachments"})] + + def observe_age(self, _): + return [Observation(self.age, {"queue": "attachments"})] + + def observe_queue(self, repository): + with repository.connection() as db: + row = db.execute( + """SELECT count(*) AS depth, COALESCE(EXTRACT(EPOCH FROM + clock_timestamp() - min(created_at)),0) AS age + FROM processing_jobs WHERE status IN ('QUEUED','PROCESSING')""" + ).fetchone() + self.depth, self.age = row["depth"], max(0.0, float(row["age"])) + + def complete(self, started, result): + outcome = result["outcome"] + attributes = {"job_type": "attachment", "outcome": outcome} + self.jobs.add(1, attributes) + self.duration.record(time.monotonic() - started, attributes) + if outcome == "ready": + seconds = result["ready_seconds"] + self.ready.record(seconds, {"job_type": "attachment"}) + self.terminal.add(1, {"outcome": "good" if seconds <= 5 else "bad"}) + elif outcome == "failed": + self.terminal.add(1, {"outcome": "bad"}) + + +def observe_cpu(_): + return [Observation(time.process_time())] + + +def observe_rss(_): + rss_pages = int(Path("/proc/self/statm").read_text().split()[1]) + return [Observation(rss_pages * os.sysconf("SC_PAGE_SIZE"))] diff --git a/worker/requirements.txt b/worker/requirements.txt new file mode 100644 index 0000000..782c5ad --- /dev/null +++ b/worker/requirements.txt @@ -0,0 +1,6 @@ +Pillow==12.0.0 +redis==6.4.0 +psycopg[binary]==3.2.10 +boto3==1.40.59 +opentelemetry-sdk==1.34.1 +opentelemetry-exporter-otlp-proto-http==1.34.1 -- 2.54.0 From 53183c42be482233cee0cf158c46c5ddd91b5211 Mon Sep 17 00:00:00 2001 From: Danil01042005 Date: Mon, 16 Nov 2026 21:14:03 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D1=83=20=D0=BD=D0=B0=20?= =?UTF-8?q?MR=20=D0=B2=20=D0=BE=D1=82=D1=87=D1=91=D1=82=20=D0=9B=D0=A06?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 1 + 1 file changed, 1 insertion(+) diff --git a/REPORT.md b/REPORT.md index 2159591..51f80a6 100644 --- a/REPORT.md +++ b/REPORT.md @@ -3,6 +3,7 @@ - Автор и группа: Иванов Данил, БВТ2301. - Целевая оценка: **5**, трек 2 «SLO и обнаружение инцидентов». - База: skeleton ЛР6 `470d9d6`. Перенос из собственной ЛР5 `ef7e8c3`, исходники и одна миграция V1. Ветка решения `solution/bvt2301-ivanov`. +- Проверенная реализация: `8a96df4`. [MR №31 в main репозитория преподавателя](https://gitea.privcloud.tech/mtusi2026/mtusi2026lab6/pulls/31). - Источники: контракт [наблюдаемости](contracts/observability.md), [W3C Trace Context](https://www.w3.org/TR/trace-context/), [OTel Collector](https://opentelemetry.io/docs/collector/configuration/), [Loki OTLP](https://grafana.com/docs/loki/latest/send-data/otel/), [Prometheus histograms](https://prometheus.io/docs/practices/histograms/), [Google SRE Workbook](https://sre.google/workbook/alerting-on-slos/). Codex использован при переносе, инструментировании, тестировании и подготовке отчёта. ## Запуск из чистого clone -- 2.54.0