This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 699326ac98f5 CAMEL-27456: camel-aws2-s3 - streaming upload with
timestamp grouping writes the bytes of an exchange whole (#30812)
699326ac98f5 is described below
commit 699326ac98f5c2c0ac77c6006056941db72daad2
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Nov 26 14:40:22 2026 +0530
CAMEL-27456: camel-aws2-s3 - streaming upload with timestamp grouping
writes the bytes of an exchange whole (#30812)
With streamingUploadMode and timestampGroupingEnabled, the producer read
the body of an exchange in chunks
(bufferSize, or partSize with multiPartUpload) and took the producer lock
once per chunk. Another exchange of the
same window could write its bytes between two chunks, so the stored object
held the bytes of two exchanges mixed
(4096 bytes of A, then B, then the rest of A), and the
streamingUploadTimeout flush could run between two chunks and
store the first part of an exchange in one object and the rest in the next
one.
The lock is now held for the whole exchange, as CAMEL-26539 does without
timestamp grouping: the body of an exchange is
read while the other exchanges of the producer (and the timeout flush)
wait. The window lookup and the message count
of the window (state.index++) are now under the lock too. No S3 call is
added under the lock: the calls of one
exchange were already made under it, one chunk at a time.
The upgrade guide entry of CAMEL-26539 now covers timestamp grouping.
* CAMEL-27456: camel-aws2-s3 - review follow-up: the test looks only at the
timeout task thread of its own CamelContext
- AWS2S3StreamUploadTimestampGroupingConcurrentExchangesTest:
timeoutTaskWaits() matches only the threads of the test's
CamelContext (named "Camel (camel-N) thread #M - ..."), so a timeout task
of another test in the same JVM cannot
satisfy the wait.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../aws2/s3/stream/AWS2S3StreamUploadProducer.java | 15 +-
...adTimestampGroupingConcurrentExchangesTest.java | 284 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 6 +-
3 files changed, 300 insertions(+), 5 deletions(-)
diff --git
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/stream/AWS2S3StreamUploadProducer.java
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/stream/AWS2S3StreamUploadProducer.java
index c398b92915f5..a3a632bc9fbd 100644
---
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/stream/AWS2S3StreamUploadProducer.java
+++
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/stream/AWS2S3StreamUploadProducer.java
@@ -442,6 +442,18 @@ public class AWS2S3StreamUploadProducer extends
DefaultProducer {
// Calculate the timestamp window
long timestampWindow = getTimestampWindow(messageTimestamp);
+ // one exchange at a time, as without timestamp grouping: the bytes of
an exchange are written to the upload of
+ // its window whole, and neither another exchange nor the timeout
flush can write or upload between its chunks
+ lock.lock();
+ try {
+ writeWithTimestampGrouping(exchange, is, timestampWindow);
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ private void writeWithTimestampGrouping(final Exchange exchange, final
InputStream is, final long timestampWindow)
+ throws Exception {
byte[] b;
int maxRead = (getConfiguration().isMultiPartUpload()
? Math.toIntExact(getConfiguration().getPartSize()) :
getConfiguration().getBufferSize());
@@ -458,7 +470,6 @@ public class AWS2S3StreamUploadProducer extends
DefaultProducer {
if (getConfiguration().isMultiPartUpload())
maxRead -= b.length;
- lock.lock();
try {
// Get or create upload state for this timestamp window
if (!timestampBasedUploads.containsKey(timestampWindow)) {
@@ -541,8 +552,6 @@ public class AWS2S3StreamUploadProducer extends
DefaultProducer {
timestampBasedUploads.remove(timestampWindow);
}
throw e;
- } finally {
- lock.unlock();
}
}
}
diff --git
a/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/stream/AWS2S3StreamUploadTimestampGroupingConcurrentExchangesTest.java
b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/stream/AWS2S3StreamUploadTimestampGroupingConcurrentExchangesTest.java
new file mode 100644
index 000000000000..74120ed6931c
--- /dev/null
+++
b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/stream/AWS2S3StreamUploadTimestampGroupingConcurrentExchangesTest.java
@@ -0,0 +1,284 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://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.
+ */
+package org.apache.camel.component.aws2.s3.stream;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.aws2.s3.AWS2S3Configuration;
+import org.apache.camel.component.aws2.s3.AWS2S3Endpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import software.amazon.awssdk.core.sync.RequestBody;
+import software.amazon.awssdk.http.SdkHttpResponse;
+import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest;
+import software.amazon.awssdk.services.s3.model.AbortMultipartUploadResponse;
+import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest;
+import
software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
+import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest;
+import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;
+import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
+import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
+import software.amazon.awssdk.services.s3.model.UploadPartRequest;
+import software.amazon.awssdk.services.s3.model.UploadPartResponse;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+/**
+ * With timestamp grouping, the bytes of an exchange must be stored whole:
neither another exchange of the same window
+ * nor the timeout flush may write or upload between two reads of its body.
The body of the first exchange stops after
+ * its first read (4096 bytes) until the test releases it. The mocked S3Client
keeps the completed objects by key.
+ */
+class AWS2S3StreamUploadTimestampGroupingConcurrentExchangesTest {
+
+ private static final long TIMESTAMP = 1_700_000_000_000L;
+ private static final int READ = 4096;
+
+ @Mock
+ private AWS2S3Endpoint endpoint;
+
+ @Mock
+ private AWS2S3Configuration configuration;
+
+ @Mock
+ private S3Client s3Client;
+
+ // the bucket: completed objects by key
+ private final Map<String, String> bucket = new ConcurrentHashMap<>();
+ private final Map<String, String> keysByUploadId = new
ConcurrentHashMap<>();
+ private final Map<String, ByteArrayOutputStream> partsByUploadId = new
ConcurrentHashMap<>();
+ private final AtomicInteger uploadIds = new AtomicInteger();
+ // a failure of an exchange sent from another thread
+ private final AtomicReference<Throwable> failure = new AtomicReference<>();
+
+ // the body of the first exchange waits after its first read until released
+ private final CountDownLatch firstReadDone = new CountDownLatch(1);
+ private final CountDownLatch releaseBody = new CountDownLatch(1);
+
+ private DefaultCamelContext camelContext;
+ private AWS2S3StreamUploadProducer producer;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ camelContext = new DefaultCamelContext();
+ camelContext.start();
+
+ when(endpoint.getConfiguration()).thenReturn(configuration);
+ when(endpoint.getCamelContext()).thenReturn(camelContext);
+ when(endpoint.getS3Client()).thenReturn(s3Client);
+
+ when(configuration.getBucketName()).thenReturn("test-bucket");
+ when(configuration.getKeyName()).thenReturn("camel.txt");
+ when(configuration.isMultiPartUpload()).thenReturn(false);
+ when(configuration.getPartSize()).thenReturn(25L * 1024 * 1024);
+
when(configuration.getNamingStrategy()).thenReturn(AWSS3NamingStrategyEnum.progressive);
+
when(configuration.getRestartingPolicy()).thenReturn(AWSS3RestartingPolicyEnum.override);
+ when(configuration.isTimestampGroupingEnabled()).thenReturn(true);
+
when(configuration.getTimestampHeaderName()).thenReturn(Exchange.MESSAGE_TIMESTAMP);
+ when(configuration.getTimestampWindowSizeMillis()).thenReturn(300000L);
+ when(configuration.getBatchMessageNumber()).thenReturn(10);
+ when(configuration.getBatchSize()).thenReturn(1000000);
+ // the producer reads a body 4096 bytes at a time
+ when(configuration.getBufferSize()).thenReturn(READ);
+
+ SdkHttpResponse httpResponse =
SdkHttpResponse.builder().statusCode(200).build();
+
+ when(s3Client.listObjectsV2(any(ListObjectsV2Request.class)))
+
.thenReturn(ListObjectsV2Response.builder().contents(Collections.emptyList()).isTruncated(false).build());
+
+
when(s3Client.createMultipartUpload(any(CreateMultipartUploadRequest.class))).thenAnswer(invocation
-> {
+ CreateMultipartUploadRequest request = invocation.getArgument(0);
+ String uploadId = "upload-" + uploadIds.incrementAndGet();
+ keysByUploadId.put(uploadId, request.key());
+ partsByUploadId.put(uploadId, new ByteArrayOutputStream());
+ return
CreateMultipartUploadResponse.builder().uploadId(uploadId).sdkHttpResponse(httpResponse).build();
+ });
+
+ when(s3Client.uploadPart(any(UploadPartRequest.class),
any(RequestBody.class))).thenAnswer(invocation -> {
+ UploadPartRequest request = invocation.getArgument(0);
+ RequestBody body = invocation.getArgument(1);
+ ByteArrayOutputStream parts =
partsByUploadId.get(request.uploadId());
+ synchronized (parts) {
+
parts.write(body.contentStreamProvider().newStream().readAllBytes());
+ }
+ return
UploadPartResponse.builder().eTag("etag").sdkHttpResponse(httpResponse).build();
+ });
+
+
when(s3Client.completeMultipartUpload(any(CompleteMultipartUploadRequest.class))).thenAnswer(invocation
-> {
+ CompleteMultipartUploadRequest request = invocation.getArgument(0);
+ bucket.put(keysByUploadId.get(request.uploadId()),
+
partsByUploadId.get(request.uploadId()).toString(StandardCharsets.UTF_8));
+ return
CompleteMultipartUploadResponse.builder().eTag("final-etag").sdkHttpResponse(httpResponse).build();
+ });
+
+
when(s3Client.abortMultipartUpload(any(AbortMultipartUploadRequest.class)))
+ .thenReturn(AbortMultipartUploadResponse.builder().build());
+
+ producer = new AWS2S3StreamUploadProducer(endpoint);
+ }
+
+ @AfterEach
+ void tearDown() {
+ releaseBody.countDown();
+ camelContext.stop();
+ }
+
+ @Test
+ void anotherExchangeOfTheWindowIsNotWrittenBetweenTheReadsOfAnExchange()
throws Exception {
+ producer.start();
+
+ Thread first = sendSlowBody('A');
+ Thread second = new Thread(() -> send("B".repeat(2 * READ)), "second");
+ second.start();
+ // the second exchange either ran (the first one holds no lock between
its reads) or waits for the first one
+ await().atMost(20, TimeUnit.SECONDS)
+ .until(() -> !second.isAlive() || second.getState() ==
Thread.State.WAITING);
+ releaseBody.countDown();
+ first.join(20000);
+ second.join(20000);
+ assertFalse(first.isAlive() || second.isAlive(), "the exchanges did
not complete");
+ producer.stop();
+
+ assertNull(failure.get(), () -> "an exchange failed: " +
failure.get());
+ assertEquals(List.of("8192 x A, 8192 x B"), objects(),
+ "the window is one object with the bytes of each exchange in
one piece");
+ }
+
+ @Test
+ void theTimeoutFlushDoesNotCutAnExchangeIntoTwoObjects() throws Exception {
+ when(configuration.getStreamingUploadTimeout()).thenReturn(50L);
+ producer.start();
+
+ Thread first = sendSlowBody('A');
+ // the timeout task either uploaded the first read of the exchange or
waits for the exchange
+ await().atMost(20, TimeUnit.SECONDS).until(() -> !bucket.isEmpty() ||
timeoutTaskWaits());
+ releaseBody.countDown();
+ first.join(20000);
+ assertFalse(first.isAlive(), "the exchange did not complete");
+ producer.stop();
+
+ assertNull(failure.get(), () -> "the exchange failed: " +
failure.get());
+ assertEquals(List.of("8192 x A"), objects(), "the exchange is stored
whole in one object");
+ }
+
+ /**
+ * Sends an exchange of the window whose body of 8192 times the given
character waits after its first 4096 bytes,
+ * and returns once the producer read them.
+ */
+ private Thread sendSlowBody(char c) throws InterruptedException {
+ InputStream body = new InputStream() {
+ private int pos;
+
+ @Override
+ public int read() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (pos >= 2 * READ) {
+ return -1;
+ }
+ if (pos == READ) {
+ firstReadDone.countDown();
+ try {
+ if (!releaseBody.await(20, TimeUnit.SECONDS)) {
+ throw new IOException("the body was not released");
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new IOException(e);
+ }
+ }
+ int n = Math.min(len, READ - pos % READ);
+ Arrays.fill(b, off, off + n, (byte) c);
+ pos += n;
+ return n;
+ }
+ };
+ Thread thread = new Thread(() -> send(body), "first");
+ thread.start();
+ assertTrue(firstReadDone.await(20, TimeUnit.SECONDS), "the producer
did not read the body");
+ return thread;
+ }
+
+ private void send(Object body) {
+ Exchange exchange = new DefaultExchange(camelContext);
+ exchange.getIn().setBody(body);
+ exchange.getIn().setHeader(Exchange.MESSAGE_TIMESTAMP, TIMESTAMP);
+ try {
+ producer.process(exchange);
+ } catch (Throwable e) {
+ failure.set(e);
+ }
+ }
+
+ private boolean timeoutTaskWaits() {
+ // only the threads of this CamelContext (named "Camel (camel-N)
thread #M - ..."), not those of another test
+ String threadsOfContext = "(" + camelContext.getName() + ")";
+ return Thread.getAllStackTraces().entrySet().stream()
+ .anyMatch(e -> e.getKey().getName().contains(threadsOfContext)
+ && e.getKey().getState() == Thread.State.WAITING
+ && Arrays.stream(e.getValue())
+ .anyMatch(frame ->
frame.getClassName().endsWith("$StreamingUploadTimeoutTask")));
+ }
+
+ /**
+ * The objects of the bucket, each as its runs of one character, such as
"4096 x A, 8192 x B".
+ */
+ private List<String> objects() {
+ List<String> objects = new ArrayList<>();
+ for (String content : bucket.values()) {
+ List<String> runs = new ArrayList<>();
+ int start = 0;
+ for (int i = 1; i <= content.length(); i++) {
+ if (i == content.length() || content.charAt(i) !=
content.charAt(start)) {
+ runs.add((i - start) + " x " + content.charAt(start));
+ start = i;
+ }
+ }
+ objects.add(String.join(", ", runs));
+ }
+ return objects;
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 5620c4ce877d..1a782932fc07 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -6985,8 +6985,10 @@ gets the UUID (or the timestamp), the first one too:
`camel-<uuid>.txt`. The pro
=== camel-aws2-s3 - streaming upload writes concurrent exchanges one at a time
-With `streamingUploadMode=true` (without `timestampGroupingEnabled`), the
producer now writes one exchange at a time,
-so exchanges sent concurrently (concurrent consumers, parallel splits,
`threads()`) no longer lose objects or parts.
+With `streamingUploadMode=true`, the producer now writes one exchange at a
time, so exchanges sent concurrently
+(concurrent consumers, parallel splits, `threads()`) no longer lose objects or
parts. With `timestampGroupingEnabled`,
+the bytes of an exchange are no longer mixed with the bytes of another
exchange of the same window, and the
+`streamingUploadTimeout` flush no longer cuts an exchange into two objects.
The body of an exchange is read while the other exchanges of the same producer
wait, and bodies that complete an
upload of their own (`batchSize` or more, or `batchMessageNumber=1`) are no
longer uploaded in parallel. To upload
large bodies in parallel, use the producer without `streamingUploadMode`.