allthingssecurity opened a new pull request, #30813:
URL: https://github.com/apache/camel/pull/30813

   # Description
   
   [CAMEL-27457](https://issues.apache.org/jira/browse/CAMEL-27457)
   
   Two problems of `AWS2S3StreamUploadProducer.doStop` with 
`streamingUploadMode=true`.
   
   **1. After a stop and start of the same producer, the next exchange is 
lost.** `doStop` uploads and completes the open
   upload (`uploadAggregate`) but kept the field. When the same producer 
instance was started again, the next exchange
   smaller than the batch was added to the buffer of the completed upload and 
returned success. Its bytes were never
   stored: the next flush called `uploadPart` for the completed upload id, S3 
answered NoSuchUpload and the next stop
   failed (without `streamingUploadTimeout`, every later batch flush fails the 
same way, since `uploadPart` throws before
   the aggregate is reset). No S3 failure is needed. The same instance is 
started again when a producer is driven
   directly (a `ProducerTemplate`, user code) or through JMX; a route restart 
creates a new producer.
   
   **2. A stop that fails on S3 leaves the timeout task running.** The S3 calls 
in `doStop` have no catch, and the
   shutdown of the `streamingUploadTimeout` executor came after them, not in a 
finally. When the upload of the stop failed
   (for example a 503 after the SDK's retries), `stop()` threw, the producer 
was FAILED (a later `stop()` does nothing),
   and the `S3_Streaming_Upload_Timeout_Checker` thread kept running: it later 
uploaded and completed the aggregate of the
   stopped producer, kept the producer referenced until the CamelContext shut 
down, and a `start()` created a second task.
   
   Also, a run of the timeout task that was waiting for the lock while `doStop` 
held it ran after `doStop` returned: the
   executor shutdown does not stop a run that has started.
   
   The change, in `doStop` only:
   
   - it first shuts the timeout executor down (as before, but before the 
uploads instead of after them) and waits for a
     run of the task that has started. No new run starts, and a flush the task 
is running finishes before the stop uploads
     what is left, so no run of the task happens after the stop, whether the 
uploads of the stop succeed or fail. The wait
     is bounded by the `shutdownAwaitTermination` of the 
`ExecutorServiceManager` (10 seconds by default); after it the
     stop logs a WARN and goes on. It then takes the lock as before, so a flush 
that is still running (a slow S3 call)
     is waited for there, without a bound, as before the change.
   - it then completes the open uploads as before, and in a finally sets 
`uploadAggregate` to null and clears the timestamp
     windows (the windows were cleared only after a successful stop). After a 
failed upload the stop still fails and reports
     it, as before; the restarted producer starts a new upload instead of 
adding to the failed one.
   
   Not changed: when the `uploadPart` of the stop fails, its multipart upload 
stays open on S3, as when any `uploadPart`
   fails today (only a failing `completeMultipartUpload` aborts). The buffered 
data of that upload is not stored, and with
   timestamp grouping the stop ends at the first upload that fails, so the 
windows after it are not tried; the finally
   drops all of them. Before, they stayed in the stopped producer, where only a 
start of the same instance, or the timeout
   task left running (the leak above), could still upload them; after a route 
restart, which creates a new producer, they
   were lost as well. The failure handling of the flushes is a separate change 
(see below). No upgrade guide entry: a stop
   that succeeds behaves as before.
   
   Found with a TLA+ model of the producer, the timeout task and the producer 
lifecycle (stop, start, a stop that fails,
   transient S3 failures): on `main` it finds the exchange added to the 
completed upload after a restart without any
   failure, two live timeout tasks after a failed stop and a start, and a 
timeout run after the stop.
   
   Tests: new `AWS2S3StreamUploadStopStartTest` (package-private, mocked 
`S3Client` that, like S3, refuses parts for a
   completed or aborted upload; Awaitility, no sleeps):
   
   - `anExchangeAfterAStopAndStartIsUploadedToANewObject`: send, stop, start, 
send, stop; the second exchange must be
     stored in an object of its own and the second stop must succeed;
   - `aStopThatFailsOnS3StopsTheTimeoutTask`: `streamingUploadTimeout` set; the 
`uploadPart` of the stop fails; the stop
     must throw, the timeout thread of the producer must end, a start must run 
exactly one, and the next exchange is
     uploaded.
   
   On `main` both fail (the second stop throws NoSuchUpload; the timeout thread 
is still alive 5 seconds after the failed
   stop). With the change both pass, and the camel-aws2-s3 unit tests pass (151 
run, 0 failures, 0 skipped; run offline). The new class,
   20 times in one JVM: 40 run, 0 failures. With the two related changes below 
merged as well, the module passes too (160
   run, 0 failures).
   
   Related changes on the same class, each its own PR and merging cleanly with 
this one: the timestamp grouping path
   writing the bytes of an exchange whole, and the failure handling of the 
timeout flush and of `uploadPart` (data of
   exchanges that succeeded is kept when a flush fails).
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27457](https://issues.apache.org/jira/browse/CAMEL-27457).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built and tested the affected module, including the formatter and 
import-sort plugins. I did not run the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to