shashank created CAMEL-27430:
--------------------------------

             Summary: camel-support - FileIdempotentRepository: a remove or a 
trunk rewrites the file store in place, so a JVM stop or an I/O error during 
the rewrite loses the stored keys
                 Key: CAMEL-27430
                 URL: https://issues.apache.org/jira/browse/CAMEL-27430
             Project: Camel
          Issue Type: Bug
          Components: camel-core
            Reporter: shashank
            Assignee: shashank


h3. What happens

{{FileIdempotentRepository}} (camel-support) rewrites its file store when a key 
is removed ({{removeFromStore}}) and when the oldest keys are dropped because 
the file is bigger than {{maxFileStoreSize}} ({{trunkStore}}). Both open the 
store with {{new FileOutputStream(fileStore)}}, which truncates it to 0 bytes 
first, and then write every remaining key and its delimiter as two unbuffered 
writes, with no temporary file and no fsync. A remove is a common call: the 
Idempotent Consumer removes the key of every failed exchange 
({{removeOnFailure=true}} is the default), and the file consumer's idempotent 
read lock removes it on rollback.

* *The JVM stops while the store is rewritten* (kill -9, OOM killer, a crash): 
every key not yet rewritten is gone, and after the restart the messages with 
those keys are processed again. With 200,000 UUID keys (7.4 MB) a remove takes 
about 1.5 s; a SIGKILL 50 ms after it started left 143,670 bytes, and 196,116 
of the 199,999 kept keys were lost.
* *A write fails while the store is rewritten* (disk full, quota, an I/O error 
of a network file system): the exception is thrown, but the file holds only the 
keys written so far while the cache still holds all of them, so the running 
repository looks fine until a restart (or an LRU eviction).
* *Dropping the oldest keys fails inside {{add}}*: the key was appended and 
cached before {{trunkStore}} ran, so {{add}} throws with the key already 
stored. The redelivery's {{add}} returns false and the message is skipped as a 
duplicate, which is what CAMEL-25681 fixed for a failing append.
* {{appendToStore}} writes the key and the delimiter in two writes. If the JVM 
stops between them, the next key appended after the restart is merged into the 
torn one ({{k2k3}}) and is lost on the next restart; if the delimiter write 
fails, {{add}} throws, but the retry finds the key without its delimiter and 
returns false.

h3. How to reproduce

A child JVM runs the real repository; a debugger stops it on a line of the 
rewrite and kills it or makes that write throw. All seven cases lose keys (for 
example a crash right after the truncation loses all 9 kept keys; a failing 
second write of a remove leaves {{k1\n}} while the cache holds 9 keys). Found 
with a TLA+ model of the store (adds, removes, trunk, IOException, crash and 
restart, LRU eviction): "every accepted key is in the file when no operation 
runs" and "a key whose add threw is not taken for a duplicate" are both 
violated.

h3. Fix

Rewrite into a temporary file next to the store (the store name with {{.tmp}} 
added, in the directory of the real file when the store is a symbolic link, 
with the permissions of the store), buffered, flushed and fsynced, then move it 
over the store with {{ATOMIC_MOVE}}, as {{FileStateRepository}} does since 
CAMEL-25213. On a failure the temporary file is deleted and the store is 
unchanged. If the temporary file cannot be created or cannot be moved over the 
store (any failed move leaves the store unchanged; for example a store that is 
a single file mounted into a container fails with EBUSY), rewrite in place as 
before and log a WARN. Append the key and its delimiter in one write. In 
{{add}}, a failing trunk no longer fails the add: the key is stored, so the 
failure is logged and the next add drops the oldest keys.

_Filed with Claude Code on behalf of allthingssecurity._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to