This commit is contained in:
EgorAnti
2026-11-19 07:57:14 +03:00
parent 77a43fa9cf
commit 75d2f78913
39 changed files with 2382 additions and 93 deletions
@@ -0,0 +1,57 @@
package com.example.mtusi_messenger.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitJobConfig {
@Bean
DirectExchange attachmentExchange(@Value("${messenger.jobs.exchange}") String name) {
return new DirectExchange(name, true, false);
}
@Bean
Queue attachmentQueue(
@Value("${messenger.jobs.queue}") String name,
@Value("${messenger.jobs.dead-letter-exchange}") String deadLetterExchange,
@Value("${messenger.jobs.dead-letter-routing-key}") String deadLetterRoutingKey) {
return QueueBuilder.durable(name)
.withArgument("x-dead-letter-exchange", deadLetterExchange)
.withArgument("x-dead-letter-routing-key", deadLetterRoutingKey)
.build();
}
@Bean
DirectExchange attachmentDeadLetterExchange(
@Value("${messenger.jobs.dead-letter-exchange}") String name) {
return new DirectExchange(name, true, false);
}
@Bean
Queue attachmentDeadLetterQueue(@Value("${messenger.jobs.dead-letter-queue}") String name) {
return QueueBuilder.durable(name).build();
}
@Bean
Binding attachmentBinding(
@Qualifier("attachmentQueue") Queue attachmentQueue,
@Qualifier("attachmentExchange") DirectExchange attachmentExchange,
@Value("${messenger.jobs.routing-key}") String routingKey) {
return BindingBuilder.bind(attachmentQueue).to(attachmentExchange).with(routingKey);
}
@Bean
Binding attachmentDeadLetterBinding(
@Qualifier("attachmentDeadLetterQueue") Queue queue,
@Qualifier("attachmentDeadLetterExchange") DirectExchange exchange,
@Value("${messenger.jobs.dead-letter-routing-key}") String routingKey) {
return BindingBuilder.bind(queue).to(exchange).with(routingKey);
}
}
@@ -1,7 +1,6 @@
package com.example.mtusi_messenger.controller;
import com.example.mtusi_messenger.dto.AttachmentResponse;
import com.example.mtusi_messenger.exception.ApiExceptions;
import com.example.mtusi_messenger.model.Attachment;
import com.example.mtusi_messenger.security.SessionPrincipal;
import com.example.mtusi_messenger.service.AttachmentService;
@@ -47,7 +46,7 @@ public class AttachmentController {
) throws IOException {
Attachment attachment = attachments.upload(parse(chatId), principal.user().id(), filename,
contentType, request.getContentLengthLong(), request.getInputStream());
return ResponseEntity.status(HttpStatus.CREATED).body(AttachmentResponse.from(attachment));
return ResponseEntity.status(HttpStatus.ACCEPTED).body(AttachmentResponse.from(attachment));
}
@GetMapping("/attachments/{attachmentId}")
@@ -64,15 +63,13 @@ public class AttachmentController {
@PathVariable String attachmentId,
@RequestParam(defaultValue = "original") String variant
) {
if (!"original".equals(variant)) {
throw ApiExceptions.invalid("Unsupported attachment variant");
}
AttachmentService.Download download = attachments.download(parse(attachmentId), principal.user().id());
AttachmentService.Download download = attachments.download(
parse(attachmentId), principal.user().id(), variant);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(download.attachment().contentType()))
.contentType(MediaType.parseMediaType(download.contentType()))
.contentLength(download.bytes().length)
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.inline()
.filename(download.attachment().filename(), StandardCharsets.UTF_8).build().toString())
.filename(download.filename(), StandardCharsets.UTF_8).build().toString())
.body(download.bytes());
}
}
@@ -14,7 +14,7 @@ public record AttachmentResponse(UUID id,
long size_bytes,
String sha256,
String status,
List<Object> variants,
List<AttachmentVariantResponse> variants,
String error_code,
Instant created_at,
Instant updated_at) {
@@ -27,9 +27,9 @@ public record AttachmentResponse(UUID id,
attachment.contentType(),
attachment.sizeBytes(),
attachment.sha256(),
"ready",
List.of(),
null,
attachment.status(),
attachment.variants().stream().map(AttachmentVariantResponse::from).toList(),
attachment.errorCode(),
attachment.createdAt(),
attachment.updatedAt());
}
@@ -0,0 +1,16 @@
package com.example.mtusi_messenger.dto;
import com.example.mtusi_messenger.model.AttachmentVariant;
public record AttachmentVariantResponse(String name,
String content_type,
long size_bytes,
int width,
int height,
String sha256) {
public static AttachmentVariantResponse from(AttachmentVariant variant) {
return new AttachmentVariantResponse(
variant.name(), variant.contentType(), variant.sizeBytes(),
variant.width(), variant.height(), variant.sha256());
}
}
@@ -1,6 +1,7 @@
package com.example.mtusi_messenger.model;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
public record Attachment(UUID id,
@@ -11,6 +12,27 @@ public record Attachment(UUID id,
long sizeBytes,
String sha256,
String objectKey,
String stagingKey,
String status,
List<AttachmentVariant> variants,
String errorCode,
Instant createdAt,
Instant updatedAt) {
public Attachment {
variants = variants == null ? List.of() : List.copyOf(variants);
}
public Attachment(UUID id,
UUID chatId,
UUID ownerId,
String filename,
String contentType,
long sizeBytes,
String sha256,
String objectKey,
Instant createdAt,
Instant updatedAt) {
this(id, chatId, ownerId, filename, contentType, sizeBytes, sha256, objectKey,
null, "ready", List.of(), null, createdAt, updatedAt);
}
}
@@ -0,0 +1,37 @@
package com.example.mtusi_messenger.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Instant;
import java.util.UUID;
public record AttachmentJob(
@JsonProperty("schema_version") int schemaVersion,
@JsonProperty("job_id") UUID jobId,
String type,
@JsonProperty("attachment_id") UUID attachmentId,
@JsonProperty("pipeline_version") int pipelineVersion,
int attempt,
@JsonProperty("created_at") Instant createdAt) {
public static final int SCHEMA_VERSION = 1;
public static final int PIPELINE_VERSION = 1;
public static final String TYPE = "attachment.process.v1";
public AttachmentJob {
if (schemaVersion != SCHEMA_VERSION || !TYPE.equals(type)
|| pipelineVersion < 1 || attempt < 1) {
throw new IllegalArgumentException("Unsupported attachment job message");
}
}
public static AttachmentJob first(UUID jobId, UUID attachmentId, Instant createdAt) {
return new AttachmentJob(SCHEMA_VERSION, jobId, TYPE, attachmentId,
PIPELINE_VERSION, 1, createdAt);
}
public AttachmentJob nextAttempt(int nextAttempt) {
return new AttachmentJob(schemaVersion, jobId, type, attachmentId,
pipelineVersion, nextAttempt, createdAt);
}
}
@@ -0,0 +1,10 @@
package com.example.mtusi_messenger.model;
public record AttachmentVariant(String name,
String contentType,
long sizeBytes,
int width,
int height,
String sha256,
String objectKey) {
}
@@ -0,0 +1,65 @@
package com.example.mtusi_messenger.queue;
import com.example.mtusi_messenger.model.AttachmentJob;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.time.Duration;
@Component
public class AttachmentJobPublisher {
private final RabbitTemplate rabbit;
private final ObjectMapper mapper;
private final String exchange;
private final String routingKey;
private final long confirmTimeoutMillis;
public AttachmentJobPublisher(
RabbitTemplate rabbit,
ObjectMapper mapper,
@Value("${messenger.jobs.exchange}") String exchange,
@Value("${messenger.jobs.routing-key}") String routingKey,
@Value("${messenger.jobs.publish-confirm-timeout:5s}") Duration confirmTimeout) {
this.rabbit = rabbit;
this.mapper = mapper;
this.exchange = exchange;
this.routingKey = routingKey;
this.confirmTimeoutMillis = confirmTimeout.toMillis();
this.rabbit.setMandatory(true);
}
public void publish(AttachmentJob job) {
try {
byte[] body = mapper.writeValueAsBytes(job);
MessageProperties properties = new MessageProperties();
properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
properties.setContentEncoding("UTF-8");
properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
properties.setMessageId(job.jobId().toString());
properties.setHeader("schema_version", job.schemaVersion());
Message message = new Message(body, properties);
rabbit.invoke(operations -> {
operations.send(exchange, routingKey, message);
operations.waitForConfirmsOrDie(confirmTimeoutMillis);
return null;
});
} catch (JsonProcessingException error) {
throw new JobPublishException("Cannot encode attachment job", error);
} catch (RuntimeException error) {
throw new JobPublishException("RabbitMQ did not confirm attachment job", error);
}
}
public static final class JobPublishException extends RuntimeException {
public JobPublishException(String message, Throwable cause) {
super(message, cause);
}
}
}
@@ -0,0 +1,55 @@
package com.example.mtusi_messenger.queue;
import com.example.mtusi_messenger.repository.AttachmentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;
import java.time.Clock;
@Component
@ConditionalOnProperty(name = "messenger.role", havingValue = "api", matchIfMissing = true)
public class ExpiredLeaseReaper {
private static final Logger log = LoggerFactory.getLogger(ExpiredLeaseReaper.class);
private final AttachmentRepository attachments;
private final RetryPolicy retryPolicy;
private final TransactionTemplate transaction;
private final int maxAttempts;
private final Clock clock = Clock.systemUTC();
public ExpiredLeaseReaper(
AttachmentRepository attachments,
RetryPolicy retryPolicy,
TransactionTemplate transaction,
@Value("${messenger.jobs.max-attempts:5}") int maxAttempts) {
this.attachments = attachments;
this.retryPolicy = retryPolicy;
this.transaction = transaction;
this.maxAttempts = maxAttempts;
}
@Scheduled(fixedDelayString = "${messenger.jobs.lease-reaper-interval:5s}")
public void recoverExpired() {
for (var job : attachments.findExpiredJobs(50)) {
try {
transaction.executeWithoutResult(status -> {
if (job.attempt() >= maxAttempts) {
attachments.failExpired(job.jobId(), job.attachmentId(), "retry_exhausted");
} else {
int nextAttempt = job.attempt() + 1;
attachments.requeueExpired(
job.jobId(), job.attachmentId(), nextAttempt, "processing_timeout",
clock.instant().plus(retryPolicy.delayForAttempt(nextAttempt)));
}
});
} catch (RuntimeException error) {
log.warn("Expired attachment lease recovery postponed for job {}", job.jobId());
return;
}
}
}
}
@@ -0,0 +1,39 @@
package com.example.mtusi_messenger.queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;
@Component
public class RetryPolicy {
private final long baseDelayMillis;
private final long maxDelayMillis;
public RetryPolicy(
@Value("${messenger.jobs.retry-base-delay:1s}") Duration baseDelay,
@Value("${messenger.jobs.retry-max-delay:30s}") Duration maxDelay) {
if (baseDelay.isNegative() || baseDelay.isZero() || maxDelay.compareTo(baseDelay) < 0) {
throw new IllegalArgumentException("Invalid retry delay configuration");
}
this.baseDelayMillis = baseDelay.toMillis();
this.maxDelayMillis = maxDelay.toMillis();
}
public Duration delayForAttempt(int nextAttempt) {
int exponent = Math.max(0, Math.min(30, nextAttempt - 2));
long exponential;
try {
exponential = Math.multiplyExact(baseDelayMillis, 1L << exponent);
} catch (ArithmeticException overflow) {
exponential = maxDelayMillis;
}
long ceiling = Math.min(maxDelayMillis, exponential);
long floor = Math.max(1, ceiling / 2);
long jittered = floor == ceiling
? ceiling
: ThreadLocalRandom.current().nextLong(floor, ceiling + 1);
return Duration.ofMillis(jittered);
}
}
@@ -0,0 +1,34 @@
package com.example.mtusi_messenger.queue;
import com.example.mtusi_messenger.repository.AttachmentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "messenger.role", havingValue = "api", matchIfMissing = true)
public class UnpublishedJobRepublisher {
private static final Logger log = LoggerFactory.getLogger(UnpublishedJobRepublisher.class);
private final AttachmentRepository attachments;
private final AttachmentJobPublisher publisher;
public UnpublishedJobRepublisher(AttachmentRepository attachments, AttachmentJobPublisher publisher) {
this.attachments = attachments;
this.publisher = publisher;
}
@Scheduled(fixedDelayString = "${messenger.jobs.republish-interval:5s}")
public void publishPending() {
for (var job : attachments.findUnpublishedJobs(50)) {
try {
publisher.publish(job);
attachments.markPublished(job.jobId());
} catch (RuntimeException error) {
log.warn("Attachment job publication postponed for job {}", job.jobId());
return;
}
}
}
}
@@ -1,15 +1,37 @@
package com.example.mtusi_messenger.repository;
import com.example.mtusi_messenger.model.Attachment;
import com.example.mtusi_messenger.model.AttachmentJob;
import com.example.mtusi_messenger.model.AttachmentVariant;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.Set;
import java.time.Duration;
import java.time.Instant;
public interface AttachmentRepository {
Attachment save(Attachment attachment);
Attachment saveQueued(Attachment attachment, AttachmentJob job);
Optional<Attachment> findById(UUID id);
List<Attachment> findAllByIdsInChat(UUID chatId, List<UUID> ids);
Set<String> findAllObjectKeys();
Set<String> findActiveStagingKeys();
void markPublished(UUID jobId);
boolean markPublishFaultInjected(UUID jobId);
List<AttachmentJob> findUnpublishedJobs(int limit);
int currentAttempt(UUID jobId);
boolean claim(UUID jobId, UUID attachmentId, int attempt, UUID leaseToken, Duration leaseDuration);
boolean markQueuedForRetry(UUID jobId, UUID attachmentId, UUID leaseToken,
int nextAttempt, String errorCode, Instant nextAttemptAt);
CompletionResult markReady(UUID jobId, UUID attachmentId, UUID leaseToken, int pipelineVersion,
List<AttachmentVariant> variants, boolean injectCrashAfterCommit);
boolean markFailed(UUID jobId, UUID attachmentId, UUID leaseToken, String errorCode);
List<AttachmentJob> findExpiredJobs(int limit);
boolean requeueExpired(UUID jobId, UUID attachmentId, int nextAttempt,
String errorCode, Instant nextAttemptAt);
boolean failExpired(UUID jobId, UUID attachmentId, String errorCode);
record CompletionResult(boolean completed, boolean crashAfterCommit) {
}
}
@@ -1,22 +1,28 @@
package com.example.mtusi_messenger.repository;
import com.example.mtusi_messenger.model.Attachment;
import com.example.mtusi_messenger.model.AttachmentJob;
import com.example.mtusi_messenger.model.AttachmentVariant;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.Set;
import java.util.HashSet;
import java.util.UUID;
@Repository
public class PostgresAttachmentRepository implements AttachmentRepository {
private static final String COLUMNS = """
id, chat_id, owner_id, filename, content_type, size_bytes,
sha256, object_key, created_at, updated_at
sha256, object_key, staging_key, status, error_code, created_at, updated_at
""";
private final JdbcTemplate jdbc;
@@ -25,16 +31,22 @@ public class PostgresAttachmentRepository implements AttachmentRepository {
}
@Override
public Attachment save(Attachment attachment) {
public Attachment saveQueued(Attachment attachment, AttachmentJob job) {
jdbc.update("""
INSERT INTO attachments
(id, chat_id, owner_id, filename, content_type, size_bytes,
sha256, object_key, status, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'ready', ?, ?)
sha256, object_key, staging_key, status, error_code, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'queued', NULL, ?, ?)
""", attachment.id(), attachment.chatId(), attachment.ownerId(),
attachment.filename(), attachment.contentType(), attachment.sizeBytes(),
attachment.sha256(), attachment.objectKey(), Timestamp.from(attachment.createdAt()),
Timestamp.from(attachment.updatedAt()));
attachment.sha256(), attachment.objectKey(), attachment.stagingKey(),
Timestamp.from(attachment.createdAt()), Timestamp.from(attachment.updatedAt()));
jdbc.update("""
INSERT INTO attachment_jobs
(id, attachment_id, pipeline_version, attempt, status, published, created_at, updated_at)
VALUES (?, ?, ?, ?, 'queued', false, ?, ?)
""", job.jobId(), job.attachmentId(), job.pipelineVersion(), job.attempt(),
Timestamp.from(job.createdAt()), Timestamp.from(job.createdAt()));
return attachment;
}
@@ -56,19 +68,236 @@ public class PostgresAttachmentRepository implements AttachmentRepository {
args[i + 1] = ids.get(i);
}
return jdbc.query("SELECT " + COLUMNS + " FROM attachments WHERE chat_id = ? AND id IN ("
+ placeholders + ") AND status = 'ready'",
+ placeholders + ") AND status <> 'failed'",
(rs, rowNum) -> map(rs), args);
}
@Override
public Set<String> findAllObjectKeys() {
return new HashSet<>(jdbc.query("SELECT object_key FROM attachments",
Set<String> keys = new HashSet<>(jdbc.query(
"SELECT object_key FROM attachments WHERE status = 'ready'",
(rs, rowNum) -> rs.getString("object_key")));
keys.addAll(jdbc.query("SELECT object_key FROM attachment_variants",
(rs, rowNum) -> rs.getString("object_key")));
return keys;
}
private static Attachment map(java.sql.ResultSet rs) throws java.sql.SQLException {
@Override
public Set<String> findActiveStagingKeys() {
return new HashSet<>(jdbc.query("""
SELECT staging_key
FROM attachments
WHERE status IN ('queued', 'processing') AND staging_key IS NOT NULL
""", (rs, rowNum) -> rs.getString("staging_key")));
}
@Override
public void markPublished(UUID jobId) {
jdbc.update("""
UPDATE attachment_jobs
SET published = true, updated_at = clock_timestamp()
WHERE id = ? AND status = 'queued'
""", jobId);
}
@Override
public boolean markPublishFaultInjected(UUID jobId) {
return jdbc.update("""
UPDATE attachment_jobs
SET publish_fault_injected = true, updated_at = clock_timestamp()
WHERE id = ? AND publish_fault_injected = false AND status = 'queued'
""", jobId) == 1;
}
@Override
public List<AttachmentJob> findUnpublishedJobs(int limit) {
return jdbc.query("""
SELECT id, attachment_id, pipeline_version, attempt, created_at
FROM attachment_jobs
WHERE status = 'queued' AND published = false
AND next_attempt_at <= clock_timestamp()
ORDER BY next_attempt_at, created_at, id
LIMIT ?
""", (rs, rowNum) -> mapJob(rs), limit);
}
@Override
public int currentAttempt(UUID jobId) {
Integer attempt = jdbc.query("SELECT attempt FROM attachment_jobs WHERE id = ?",
(rs, rowNum) -> rs.getInt("attempt"), jobId).stream().findFirst().orElse(null);
return attempt == null ? 1 : attempt;
}
@Override
public boolean claim(UUID jobId, UUID attachmentId, int attempt,
UUID leaseToken, Duration leaseDuration) {
int claimed = jdbc.update("""
UPDATE attachment_jobs
SET status = 'processing', attempt = GREATEST(attempt, ?),
lease_token = ?,
lease_until = clock_timestamp() + (? * interval '1 millisecond'),
updated_at = clock_timestamp()
WHERE id = ? AND attachment_id = ?
AND (status = 'queued'
OR (status = 'processing' AND lease_until < clock_timestamp()))
""", attempt, leaseToken, leaseDuration.toMillis(), jobId, attachmentId);
if (claimed == 0) {
return false;
}
jdbc.update("""
UPDATE attachments
SET status = 'processing', error_code = NULL, updated_at = clock_timestamp()
WHERE id = ? AND status NOT IN ('ready', 'failed')
""", attachmentId);
return true;
}
@Override
public boolean markQueuedForRetry(UUID jobId, UUID attachmentId, UUID leaseToken,
int nextAttempt, String errorCode, Instant nextAttemptAt) {
int updated = jdbc.update("""
UPDATE attachment_jobs
SET status = 'queued', attempt = ?, published = false,
last_error_code = ?, next_attempt_at = ?,
lease_token = NULL, lease_until = NULL, updated_at = clock_timestamp()
WHERE id = ? AND attachment_id = ? AND status = 'processing'
AND lease_token = ?
""", nextAttempt, errorCode, Timestamp.from(nextAttemptAt),
jobId, attachmentId, leaseToken);
if (updated == 0) {
return false;
}
jdbc.update("""
UPDATE attachments
SET status = 'queued', error_code = NULL, updated_at = clock_timestamp()
WHERE id = ? AND status NOT IN ('ready', 'failed')
""", attachmentId);
return true;
}
@Override
public CompletionResult markReady(UUID jobId, UUID attachmentId, UUID leaseToken, int pipelineVersion,
List<AttachmentVariant> variants, boolean injectCrashAfterCommit) {
boolean triggerCrash = injectCrashAfterCommit && Boolean.TRUE.equals(jdbc.query("""
SELECT NOT result_fault_injected
FROM attachment_jobs
WHERE id = ? AND attachment_id = ? AND status = 'processing' AND lease_token = ?
FOR UPDATE
""", (rs, rowNum) -> rs.getBoolean(1), jobId, attachmentId, leaseToken)
.stream().findFirst().orElse(false));
int completed = jdbc.update("""
UPDATE attachment_jobs
SET status = 'ready', published = true, last_error_code = NULL,
lease_token = NULL, lease_until = NULL,
result_fault_injected = result_fault_injected OR ?,
updated_at = clock_timestamp()
WHERE id = ? AND attachment_id = ? AND status = 'processing'
AND lease_token = ?
""", triggerCrash, jobId, attachmentId, leaseToken);
if (completed == 0) {
return new CompletionResult(false, false);
}
for (AttachmentVariant variant : variants) {
jdbc.update("""
INSERT INTO attachment_variants
(attachment_id, pipeline_version, name, content_type, size_bytes,
width, height, sha256, object_key, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, clock_timestamp())
ON CONFLICT (attachment_id, pipeline_version, name) DO UPDATE SET
content_type = EXCLUDED.content_type,
size_bytes = EXCLUDED.size_bytes,
width = EXCLUDED.width,
height = EXCLUDED.height,
sha256 = EXCLUDED.sha256,
object_key = EXCLUDED.object_key
""", attachmentId, pipelineVersion, variant.name(), variant.contentType(),
variant.sizeBytes(), variant.width(), variant.height(), variant.sha256(),
variant.objectKey());
}
jdbc.update("""
UPDATE attachments
SET status = 'ready', error_code = NULL, updated_at = clock_timestamp()
WHERE id = ? AND status <> 'failed'
""", attachmentId);
return new CompletionResult(true, triggerCrash);
}
@Override
public boolean markFailed(UUID jobId, UUID attachmentId, UUID leaseToken, String errorCode) {
int updated = jdbc.update("""
UPDATE attachment_jobs
SET status = 'failed', published = true, last_error_code = ?,
lease_token = NULL, lease_until = NULL,
updated_at = clock_timestamp()
WHERE id = ? AND attachment_id = ? AND status = 'processing'
AND lease_token = ?
""", errorCode, jobId, attachmentId, leaseToken);
if (updated == 0) {
return false;
}
jdbc.update("""
UPDATE attachments
SET status = 'failed', error_code = ?, updated_at = clock_timestamp()
WHERE id = ? AND status <> 'ready'
""", errorCode, attachmentId);
return true;
}
@Override
public List<AttachmentJob> findExpiredJobs(int limit) {
return jdbc.query("""
SELECT id, attachment_id, pipeline_version, attempt, created_at
FROM attachment_jobs
WHERE status = 'processing' AND lease_until < clock_timestamp()
ORDER BY lease_until, id
LIMIT ?
""", (rs, rowNum) -> mapJob(rs), limit);
}
@Override
public boolean requeueExpired(UUID jobId, UUID attachmentId, int nextAttempt,
String errorCode, Instant nextAttemptAt) {
int updated = jdbc.update("""
UPDATE attachment_jobs
SET status = 'queued', attempt = ?, published = false,
last_error_code = ?, next_attempt_at = ?,
lease_token = NULL, lease_until = NULL, updated_at = clock_timestamp()
WHERE id = ? AND attachment_id = ? AND status = 'processing'
AND lease_until < clock_timestamp()
""", nextAttempt, errorCode, Timestamp.from(nextAttemptAt), jobId, attachmentId);
if (updated == 1) {
jdbc.update("""
UPDATE attachments SET status = 'queued', error_code = NULL,
updated_at = clock_timestamp()
WHERE id = ? AND status = 'processing'
""", attachmentId);
}
return updated == 1;
}
@Override
public boolean failExpired(UUID jobId, UUID attachmentId, String errorCode) {
int updated = jdbc.update("""
UPDATE attachment_jobs
SET status = 'failed', published = true, last_error_code = ?,
lease_token = NULL, lease_until = NULL, updated_at = clock_timestamp()
WHERE id = ? AND attachment_id = ? AND status = 'processing'
AND lease_until < clock_timestamp()
""", errorCode, jobId, attachmentId);
if (updated == 1) {
jdbc.update("""
UPDATE attachments SET status = 'failed', error_code = ?,
updated_at = clock_timestamp()
WHERE id = ? AND status <> 'ready'
""", errorCode, attachmentId);
}
return updated == 1;
}
private Attachment map(ResultSet rs) throws SQLException {
UUID id = rs.getObject("id", UUID.class);
return new Attachment(
rs.getObject("id", UUID.class),
id,
rs.getObject("chat_id", UUID.class),
rs.getObject("owner_id", UUID.class),
rs.getString("filename"),
@@ -76,7 +305,34 @@ public class PostgresAttachmentRepository implements AttachmentRepository {
rs.getLong("size_bytes"),
rs.getString("sha256"),
rs.getString("object_key"),
rs.getString("staging_key"),
rs.getString("status"),
findVariants(id),
rs.getString("error_code"),
rs.getTimestamp("created_at").toInstant(),
rs.getTimestamp("updated_at").toInstant());
}
private List<AttachmentVariant> findVariants(UUID attachmentId) {
return jdbc.query("""
SELECT name, content_type, size_bytes, width, height, sha256, object_key
FROM attachment_variants
WHERE attachment_id = ? AND pipeline_version = 1
ORDER BY name
""", (rs, rowNum) -> new AttachmentVariant(
rs.getString("name"), rs.getString("content_type"), rs.getLong("size_bytes"),
rs.getInt("width"), rs.getInt("height"), rs.getString("sha256"),
rs.getString("object_key")), attachmentId);
}
private static AttachmentJob mapJob(ResultSet rs) throws SQLException {
return new AttachmentJob(
AttachmentJob.SCHEMA_VERSION,
rs.getObject("id", UUID.class),
AttachmentJob.TYPE,
rs.getObject("attachment_id", UUID.class),
rs.getInt("pipeline_version"),
rs.getInt("attempt"),
rs.getTimestamp("created_at").toInstant());
}
}
@@ -6,6 +6,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -16,6 +17,7 @@ import java.util.HashSet;
import java.util.Set;
@Component
@ConditionalOnProperty(name = "messenger.role", havingValue = "api", matchIfMissing = true)
public class AttachmentReconciler {
private static final Logger log = LoggerFactory.getLogger(AttachmentReconciler.class);
private static final String PREFIX = "attachments/";
@@ -2,6 +2,9 @@ package com.example.mtusi_messenger.service;
import com.example.mtusi_messenger.exception.ApiExceptions;
import com.example.mtusi_messenger.model.Attachment;
import com.example.mtusi_messenger.model.AttachmentJob;
import com.example.mtusi_messenger.model.AttachmentVariant;
import com.example.mtusi_messenger.queue.AttachmentJobPublisher;
import com.example.mtusi_messenger.repository.AttachmentRepository;
import com.example.mtusi_messenger.storage.ObjectStorage;
import com.example.mtusi_messenger.storage.ObjectStorageException;
@@ -34,25 +37,30 @@ public class AttachmentService {
private final AttachmentRepository attachments;
private final ChatService chats;
private final ObjectStorage storage;
private final AttachmentJobPublisher publisher;
private final TransactionTemplate transaction;
private final long maxUploadBytes;
private final long maxImagePixels;
private final Semaphore uploadPermits;
private final long uploadPermitWaitMillis;
private final boolean injectCrashAfterOutboxCommit;
public AttachmentService(
AttachmentRepository attachments,
ChatService chats,
ObjectStorage storage,
AttachmentJobPublisher publisher,
TransactionTemplate transaction,
@Value("${messenger.attachments.max-upload-bytes:10485760}") long maxUploadBytes,
@Value("${messenger.attachments.max-image-pixels:40000000}") long maxImagePixels,
@Value("${messenger.attachments.max-concurrent-uploads:3}") int maxConcurrentUploads,
@Value("${messenger.attachments.upload-permit-wait-millis:10000}") long uploadPermitWaitMillis
@Value("${messenger.attachments.upload-permit-wait-millis:10000}") long uploadPermitWaitMillis,
@Value("${messenger.jobs.fault-crash-after-outbox-commit:false}") boolean injectCrashAfterOutboxCommit
) {
this.attachments = attachments;
this.chats = chats;
this.storage = storage;
this.publisher = publisher;
this.transaction = transaction;
this.maxUploadBytes = maxUploadBytes;
this.maxImagePixels = maxImagePixels;
@@ -61,6 +69,7 @@ public class AttachmentService {
}
this.uploadPermits = new Semaphore(maxConcurrentUploads, true);
this.uploadPermitWaitMillis = uploadPermitWaitMillis;
this.injectCrashAfterOutboxCommit = injectCrashAfterOutboxCommit;
}
public Attachment upload(UUID chatId,
@@ -108,16 +117,36 @@ public class AttachmentService {
UUID id = UuidCreator.getTimeOrderedEpochPlus1();
String objectKey = "attachments/" + id + "/original";
String stagingKey = "staging/" + id + "/original";
Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
AttachmentJob job = AttachmentJob.first(UUID.randomUUID(), id, now);
Attachment attachment = new Attachment(id, chatId, ownerId, safeFilename, contentType,
bytes.length, sha256(bytes), objectKey, now, now);
bytes.length, sha256(bytes), objectKey, stagingKey, "queued", List.of(),
null, now, now);
storage.put(objectKey, bytes, contentType);
storage.put(stagingKey, bytes, contentType);
try {
return transaction.execute(status -> attachments.save(attachment));
} catch (RuntimeException databaseFailure) {
Attachment saved = transaction.execute(status -> attachments.saveQueued(attachment, job));
if (injectCrashAfterOutboxCommit && attachments.markPublishFaultInjected(job.jobId())) {
// Opt-in fault injection for the exact DB COMMIT -> broker publish window.
// The persisted outbox row remains unpublished and is recovered after restart.
Runtime.getRuntime().halt(87);
}
try {
storage.delete(objectKey);
publisher.publish(job);
attachments.markPublished(job.jobId());
return saved;
} catch (RuntimeException publishFailure) {
// The database job and staging object are intentionally retained.
// The scheduled republisher closes the DB-commit -> broker window.
throw ApiExceptions.unavailable("Attachment was staged; queue publication is pending");
}
} catch (RuntimeException databaseFailure) {
if (databaseFailure instanceof com.example.mtusi_messenger.exception.ApiException) {
throw databaseFailure;
}
try {
storage.delete(stagingKey);
} catch (RuntimeException cleanupFailure) {
databaseFailure.addSuppressed(cleanupFailure);
}
@@ -133,8 +162,28 @@ public class AttachmentService {
}
public Download download(UUID attachmentId, UUID userId) {
return download(attachmentId, userId, "original");
}
public Download download(UUID attachmentId, UUID userId, String variantName) {
Attachment attachment = getAccessible(attachmentId, userId);
return new Download(attachment, storage.get(attachment.objectKey()));
if (!"ready".equals(attachment.status())) {
throw ApiExceptions.conflict("Attachment content is not ready");
}
if ("original".equals(variantName)) {
return new Download(attachment, attachment.contentType(), attachment.filename(),
storage.get(attachment.objectKey()));
}
if (!List.of("thumbnail", "crop", "optimized").contains(variantName)) {
throw ApiExceptions.invalid("Unsupported attachment variant");
}
AttachmentVariant variant = attachment.variants().stream()
.filter(candidate -> candidate.name().equals(variantName))
.findFirst()
.orElseThrow(() -> ApiExceptions.notFound("Attachment variant not found"));
String extension = "image/webp".equals(variant.contentType()) ? ".webp" : ".jpg";
return new Download(attachment, variant.contentType(), variantName + extension,
storage.get(variant.objectKey()));
}
public void validateForMessage(UUID chatId, List<UUID> attachmentIds) {
@@ -234,6 +283,6 @@ public class AttachmentService {
}
}
public record Download(Attachment attachment, byte[] bytes) {
public record Download(Attachment attachment, String contentType, String filename, byte[] bytes) {
}
}
@@ -0,0 +1,65 @@
package com.example.mtusi_messenger.service;
import com.example.mtusi_messenger.repository.AttachmentRepository;
import com.example.mtusi_messenger.storage.ObjectStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Clock;
import java.time.Duration;
import java.util.Set;
@Component
@ConditionalOnProperty(name = "messenger.role", havingValue = "api", matchIfMissing = true)
public class StagingCleaner {
private static final Logger log = LoggerFactory.getLogger(StagingCleaner.class);
private static final String PREFIX = "staging/";
private final AttachmentRepository attachments;
private final ObjectStorage storage;
private final Duration retention;
private final Clock clock;
@Autowired
public StagingCleaner(
AttachmentRepository attachments,
ObjectStorage storage,
@Value("${messenger.attachments.staging-retention-seconds:86400}") long retentionSeconds) {
this(attachments, storage, Duration.ofSeconds(retentionSeconds), Clock.systemUTC());
}
StagingCleaner(
AttachmentRepository attachments,
ObjectStorage storage,
Duration retention,
Clock clock) {
if (retention.isZero() || retention.isNegative()) {
throw new IllegalArgumentException("Staging retention must be positive");
}
this.attachments = attachments;
this.storage = storage;
this.retention = retention;
this.clock = clock;
}
@Scheduled(
fixedDelayString = "${messenger.attachments.reconcile-interval-millis:600000}",
initialDelayString = "${messenger.attachments.reconcile-initial-delay-millis:60000}")
public void cleanup() {
try {
Set<String> active = attachments.findActiveStagingKeys();
var deleteBefore = clock.instant().minus(retention);
for (ObjectStorage.StoredObject object : storage.list(PREFIX)) {
if (!active.contains(object.objectKey()) && object.lastModified().isBefore(deleteBefore)) {
storage.delete(object.objectKey());
}
}
} catch (RuntimeException error) {
log.warn("Staging cleanup postponed: {}", error.getMessage());
}
}
}
@@ -0,0 +1,239 @@
package com.example.mtusi_messenger.worker;
import com.example.mtusi_messenger.model.Attachment;
import com.example.mtusi_messenger.model.AttachmentJob;
import com.example.mtusi_messenger.model.AttachmentVariant;
import com.example.mtusi_messenger.queue.RetryPolicy;
import com.example.mtusi_messenger.repository.AttachmentRepository;
import com.example.mtusi_messenger.storage.ObjectStorage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import jakarta.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Clock;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@Component
@ConditionalOnProperty(name = "messenger.role", havingValue = "worker")
public class AttachmentWorker {
private static final Logger log = LoggerFactory.getLogger(AttachmentWorker.class);
private final ObjectMapper mapper;
private final AttachmentRepository attachments;
private final ObjectStorage storage;
private final ImageVariantProcessor images;
private final RetryPolicy retryPolicy;
private final TransactionTemplate transaction;
private final int maxAttempts;
private final Duration jobTimeout;
private final Duration leaseDuration;
private final boolean injectCrashAfterResultCommit;
private final ExecutorService processingPool;
private final Clock clock = Clock.systemUTC();
public AttachmentWorker(
ObjectMapper mapper,
AttachmentRepository attachments,
ObjectStorage storage,
ImageVariantProcessor images,
RetryPolicy retryPolicy,
TransactionTemplate transaction,
@Value("${messenger.jobs.max-attempts:5}") int maxAttempts,
@Value("${messenger.jobs.timeout:60s}") Duration jobTimeout,
@Value("${messenger.jobs.lease-duration:90s}") Duration leaseDuration,
@Value("${messenger.jobs.fault-crash-after-result-commit:false}") boolean injectCrashAfterResultCommit,
@Value("${spring.rabbitmq.listener.simple.concurrency:2}") int concurrency) {
if (maxAttempts < 1 || jobTimeout.isZero() || jobTimeout.isNegative()
|| leaseDuration.compareTo(jobTimeout) <= 0 || concurrency < 1) {
throw new IllegalArgumentException("Invalid attachment worker limits");
}
this.mapper = mapper;
this.attachments = attachments;
this.storage = storage;
this.images = images;
this.retryPolicy = retryPolicy;
this.transaction = transaction;
this.maxAttempts = maxAttempts;
this.jobTimeout = jobTimeout;
this.leaseDuration = leaseDuration;
this.injectCrashAfterResultCommit = injectCrashAfterResultCommit;
this.processingPool = Executors.newFixedThreadPool(concurrency,
Thread.ofPlatform().name("attachment-image-", 0).factory());
}
@RabbitListener(queues = "${messenger.jobs.queue}")
public void consume(Message message, Channel channel) throws IOException {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
AttachmentJob job;
try {
job = mapper.readValue(message.getBody(), AttachmentJob.class);
} catch (RuntimeException | IOException invalidMessage) {
log.error("Rejecting malformed attachment job message {}",
message.getMessageProperties().getMessageId());
channel.basicReject(deliveryTag, false);
return;
}
Attachment attachment = attachments.findById(job.attachmentId()).orElse(null);
if (attachment == null || "ready".equals(attachment.status()) || "failed".equals(attachment.status())) {
channel.basicAck(deliveryTag, false);
return;
}
int attempt = Math.max(job.attempt(), attachments.currentAttempt(job.jobId()));
UUID leaseToken = UUID.randomUUID();
boolean claimed = Boolean.TRUE.equals(transaction.execute(status ->
attachments.claim(job.jobId(), job.attachmentId(), attempt, leaseToken, leaseDuration)));
if (!claimed) {
// A live worker owns the lease, or a terminal state won the race.
// If the owner died, the DB lease reaper will create a new outbox delivery.
channel.basicAck(deliveryTag, false);
return;
}
Future<ProcessOutcome> future = processingPool.submit(() -> process(job, leaseToken));
try {
ProcessOutcome outcome = future.get(jobTimeout.toMillis(), TimeUnit.MILLISECONDS);
if (outcome.crashAfterCommit()) {
log.error("Fault injection: halting worker after durable result and before consumer ack");
Runtime.getRuntime().halt(86);
}
channel.basicAck(deliveryTag, false);
} catch (TimeoutException timeout) {
future.cancel(true);
retryOrFail(job, leaseToken, attempt, "processing_timeout", deliveryTag, channel);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
future.cancel(true);
channel.basicNack(deliveryTag, false, true);
} catch (ExecutionException execution) {
Throwable cause = execution.getCause();
if (cause instanceof ImageVariantProcessor.ProcessingException imageFailure
&& imageFailure.permanent()) {
failPermanent(job, leaseToken, imageFailure.errorCode(), deliveryTag, channel);
} else {
String code = cause instanceof ImageVariantProcessor.ProcessingException imageFailure
? imageFailure.errorCode() : "processing_failed";
retryOrFail(job, leaseToken, attempt, code, deliveryTag, channel);
}
}
}
private ProcessOutcome process(AttachmentJob job, UUID leaseToken) {
Attachment attachment = attachments.findById(job.attachmentId())
.orElseThrow(() -> new ImageVariantProcessor.ProcessingException(
"attachment_missing", true, "Attachment metadata is missing", null));
byte[] original = storage.get(attachment.stagingKey());
if (original.length != attachment.sizeBytes() || !sha256(original).equals(attachment.sha256())) {
throw new ImageVariantProcessor.ProcessingException(
"invalid_image", true, "Staging checksum mismatch", null);
}
storage.put(attachment.objectKey(), original, attachment.contentType());
List<ImageVariantProcessor.GeneratedVariant> generated = images.createVariants(original);
List<AttachmentVariant> variants = new ArrayList<>(generated.size());
for (ImageVariantProcessor.GeneratedVariant item : generated) {
String objectKey = "attachments/" + attachment.id() + "/"
+ item.name() + "-v" + job.pipelineVersion() + "." + item.extension();
storage.put(objectKey, item.bytes(), item.contentType());
variants.add(new AttachmentVariant(
item.name(), item.contentType(), item.bytes().length,
item.width(), item.height(), sha256(item.bytes()), objectKey));
}
AttachmentRepository.CompletionResult completion = transaction.execute(status ->
attachments.markReady(job.jobId(), job.attachmentId(), leaseToken,
job.pipelineVersion(), variants, injectCrashAfterResultCommit));
if (completion == null || !completion.completed()) {
return new ProcessOutcome(false);
}
try {
storage.delete(attachment.stagingKey());
} catch (RuntimeException cleanupFailure) {
log.warn("Staging cleanup postponed for attachment {}", attachment.id());
}
return new ProcessOutcome(completion.crashAfterCommit());
}
private void failPermanent(
AttachmentJob job, UUID leaseToken, String errorCode,
long deliveryTag, Channel channel) throws IOException {
boolean failed = Boolean.TRUE.equals(transaction.execute(status ->
attachments.markFailed(job.jobId(), job.attachmentId(), leaseToken, errorCode)));
if (failed) {
channel.basicReject(deliveryTag, false);
} else {
channel.basicAck(deliveryTag, false);
}
}
private void retryOrFail(
AttachmentJob job, UUID leaseToken, int attempt, String errorCode,
long deliveryTag, Channel channel) throws IOException {
try {
if (attempt >= maxAttempts) {
boolean failed = Boolean.TRUE.equals(transaction.execute(status ->
attachments.markFailed(job.jobId(), job.attachmentId(), leaseToken,
"retry_exhausted")));
if (failed) {
log.error("Attachment job {} exhausted {} attempts: {}",
job.jobId(), attempt, errorCode);
channel.basicReject(deliveryTag, false);
} else {
channel.basicAck(deliveryTag, false);
}
return;
}
int nextAttempt = attempt + 1;
boolean queued = Boolean.TRUE.equals(transaction.execute(status ->
attachments.markQueuedForRetry(
job.jobId(), job.attachmentId(), leaseToken, nextAttempt, errorCode,
clock.instant().plus(retryPolicy.delayForAttempt(nextAttempt)))));
// The DB outbox is now the durable retry carrier; the current broker
// delivery can be acknowledged without a DB/broker dual write.
channel.basicAck(deliveryTag, false);
if (!queued) {
log.info("Retry skipped because job {} lost its lease or became terminal", job.jobId());
}
} catch (RuntimeException databaseFailure) {
channel.basicNack(deliveryTag, false, true);
}
}
@PreDestroy
void shutdownPool() {
processingPool.shutdownNow();
}
private static String sha256(byte[] bytes) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
} catch (NoSuchAlgorithmException impossible) {
throw new IllegalStateException("SHA-256 is unavailable", impossible);
}
}
private record ProcessOutcome(boolean crashAfterCommit) {
}
}
@@ -0,0 +1,254 @@
package com.example.mtusi_messenger.worker;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@Component
public class ImageVariantProcessor {
private static final long MEBIBYTE = 1024L * 1024L;
private static final long OUTPUT_ALLOWANCE_BYTES = 64L * MEBIBYTE;
private final long maxPixels;
private final long memoryBudgetBytes;
private final Semaphore memoryPermits;
private final Duration memoryWait;
public ImageVariantProcessor(
@Value("${messenger.jobs.worker-max-pixels:40000000}") long maxPixels,
@Value("${messenger.jobs.worker-memory-budget-bytes:536870912}") long memoryBudgetBytes,
@Value("${messenger.jobs.memory-wait:2s}") Duration memoryWait) {
if (maxPixels < 1 || memoryBudgetBytes < MEBIBYTE || memoryWait.isNegative()) {
throw new IllegalArgumentException("Invalid worker image resource limits");
}
this.maxPixels = maxPixels;
this.memoryBudgetBytes = memoryBudgetBytes;
this.memoryPermits = new Semaphore((int) Math.min(Integer.MAX_VALUE,
Math.max(1, memoryBudgetBytes / MEBIBYTE)), true);
this.memoryWait = memoryWait;
}
public List<GeneratedVariant> createVariants(byte[] original) {
ImageInfo info = inspect(original);
long estimatedBytes;
try {
estimatedBytes = Math.addExact(Math.multiplyExact(info.pixels(), 12L),
OUTPUT_ALLOWANCE_BYTES);
} catch (ArithmeticException overflow) {
throw permanent("image_too_large", "Image memory estimate overflow", overflow);
}
if (estimatedBytes > memoryBudgetBytes) {
throw permanent("image_too_large", "Image exceeds worker memory budget", null);
}
int permits = (int) Math.max(1, (estimatedBytes + MEBIBYTE - 1) / MEBIBYTE);
boolean acquired;
try {
acquired = memoryPermits.tryAcquire(permits, memoryWait.toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw temporary("worker_interrupted", "Interrupted while waiting for memory budget", interrupted);
}
if (!acquired) {
throw temporary("worker_capacity", "Worker memory budget is currently exhausted", null);
}
try {
return transform(original);
} finally {
memoryPermits.release(permits);
}
}
private ImageInfo inspect(byte[] bytes) {
ImageIO.setUseCache(false);
try (var input = ImageIO.createImageInputStream(new ByteArrayInputStream(bytes))) {
if (input == null) {
throw permanent("invalid_image", "Image input is invalid", null);
}
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (!readers.hasNext()) {
throw permanent("invalid_image", "Unsupported image encoding", null);
}
ImageReader reader = readers.next();
try {
reader.setInput(input, true, true);
int width = reader.getWidth(0);
int height = reader.getHeight(0);
long pixels = Math.multiplyExact((long) width, height);
if (width < 1 || height < 1 || pixels > maxPixels) {
throw permanent("image_too_large", "Image exceeds worker pixel limit", null);
}
return new ImageInfo(width, height, pixels);
} finally {
reader.dispose();
}
} catch (ProcessingException error) {
throw error;
} catch (IOException | RuntimeException error) {
throw permanent("invalid_image", "Image header cannot be read", error);
}
}
private List<GeneratedVariant> transform(byte[] original) {
try {
BufferedImage oriented = Thumbnails.of(new ByteArrayInputStream(original))
.useExifOrientation(true)
.scale(1.0)
.asBufferedImage();
if (oriented == null || oriented.getWidth() < 1 || oriented.getHeight() < 1) {
throw permanent("invalid_image", "Image cannot be decoded", null);
}
try {
List<GeneratedVariant> variants = new ArrayList<>(3);
BufferedImage thumbnail = fit(oriented, 256, BufferedImage.TYPE_INT_RGB, true);
variants.add(encode("thumbnail", "image/jpeg", "jpg", thumbnail, "jpeg", 0.85f));
thumbnail.flush();
BufferedImage crop = cropSquare(oriented, 128);
variants.add(encode("crop", "image/jpeg", "jpg", crop, "jpeg", 0.88f));
crop.flush();
int webpType = oriented.getColorModel().hasAlpha()
? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
BufferedImage optimized = fit(oriented, 1280, webpType, !oriented.getColorModel().hasAlpha());
variants.add(encode("optimized", "image/webp", "webp", optimized, "webp", 0.82f));
optimized.flush();
return List.copyOf(variants);
} finally {
oriented.flush();
}
} catch (ProcessingException error) {
throw error;
} catch (IOException | RuntimeException error) {
throw permanent("invalid_image", "Image cannot be transformed", error);
}
}
private static BufferedImage fit(BufferedImage source, int maxSide, int type, boolean whiteBackground) {
double scale = Math.min(1.0, Math.min(
(double) maxSide / source.getWidth(),
(double) maxSide / source.getHeight()));
int width = Math.max(1, (int) Math.floor(source.getWidth() * scale));
int height = Math.max(1, (int) Math.floor(source.getHeight() * scale));
BufferedImage target = new BufferedImage(width, height, type);
Graphics2D graphics = qualityGraphics(target, whiteBackground);
try {
graphics.drawImage(source, 0, 0, width, height, null);
} finally {
graphics.dispose();
}
return target;
}
private static BufferedImage cropSquare(BufferedImage source, int side) {
int sourceSide = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - sourceSide) / 2;
int y = (source.getHeight() - sourceSide) / 2;
BufferedImage target = new BufferedImage(side, side, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = qualityGraphics(target, true);
try {
graphics.drawImage(source, 0, 0, side, side,
x, y, x + sourceSide, y + sourceSide, null);
} finally {
graphics.dispose();
}
return target;
}
private static Graphics2D qualityGraphics(BufferedImage target, boolean whiteBackground) {
Graphics2D graphics = target.createGraphics();
if (whiteBackground) {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, target.getWidth(), target.getHeight());
}
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
return graphics;
}
private static GeneratedVariant encode(
String name, String contentType, String extension,
BufferedImage image, String format, float quality) throws IOException {
Iterator<javax.imageio.ImageWriter> writers = ImageIO.getImageWritersByFormatName(format);
if (!writers.hasNext()) {
throw new IOException(format + " writer is unavailable");
}
var writer = writers.next();
try (var output = new ByteArrayOutputStream();
var imageOutput = ImageIO.createImageOutputStream(output)) {
writer.setOutput(imageOutput);
ImageWriteParam parameters = writer.getDefaultWriteParam();
if (parameters.canWriteCompressed()) {
parameters.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
String[] compressionTypes = parameters.getCompressionTypes();
if (compressionTypes != null && compressionTypes.length > 0) {
parameters.setCompressionType(compressionTypes[0]);
}
parameters.setCompressionQuality(quality);
}
// Source metadata is intentionally omitted for every generated variant.
writer.write(null, new IIOImage(image, null, null), parameters);
imageOutput.flush();
return new GeneratedVariant(name, contentType, extension, output.toByteArray(),
image.getWidth(), image.getHeight());
} finally {
writer.dispose();
}
}
private static ProcessingException permanent(String code, String message, Throwable cause) {
return new ProcessingException(code, true, message, cause);
}
private static ProcessingException temporary(String code, String message, Throwable cause) {
return new ProcessingException(code, false, message, cause);
}
private record ImageInfo(int width, int height, long pixels) {
}
public record GeneratedVariant(String name, String contentType, String extension,
byte[] bytes, int width, int height) {
}
public static final class ProcessingException extends RuntimeException {
private final String errorCode;
private final boolean permanent;
public ProcessingException(
String errorCode, boolean permanent, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
this.permanent = permanent;
}
public String errorCode() {
return errorCode;
}
public boolean permanent() {
return permanent;
}
}
}