YongGoose opened a new pull request, #1056:
URL: https://github.com/apache/burr/pull/1056

   Closes #1055.
   
   When two processes (or two application objects) run the same `app_id`, both 
save the same step. The persisters now all answer that the same way: the first 
save wins, the other raises `burr.core.persistence.PersistenceConflictError`, 
and the persister stays usable.
   
   ## Changes
   
   - **`PersistenceConflictError`** in `burr.core.persistence`. It is a 
`ValueError` subclass, so code that caught the old Redis/MongoDB `ValueError` 
keeps working, and it carries `partition_key`, `app_id` and `sequence_id`. The 
abstract `save` docstrings mention it.
   - **SQLite and aiosqlite:** roll back the failed INSERT and raise the new 
error for a `UNIQUE constraint` failure. Other integrity errors are rolled back 
and re-raised unchanged.
   - **psycopg2:** roll back on `UniqueViolation`, then raise the new error. 
Before, the connection stayed in an aborted transaction and every later call 
failed.
   - **asyncpg:** map `UniqueViolationError` to the new error.
   - **Redis (sync and async):** the existence check, `HSET` and `ZADD` run in 
one `WATCH`/`MULTI` transaction. A `WatchError` means another writer saved the 
key in between, so it maps to the new error.
   - **MongoDB:** the first `save` creates a unique index on `(partition_key, 
app_id, sequence_id)`, and `DuplicateKeyError` maps to the new error. If the 
index cannot be created (for example, the collection already holds duplicates), 
it logs one warning and keeps today's check.
   - **Docs:** a short section in the state persistence page, and the error in 
the persister reference.
   
   One thing that is not in the issue: with SQLite, the failed INSERT also left 
the losing connection's transaction open. That connection kept the database's 
write lock, so the **winner's** next save failed with `database is locked` once 
the timeout ran out. The rollback fixes that too.
   
   ## Not changed
   
   - `InMemoryPersister` still appends duplicates. It is documented as 
test-only.
   - Aerospike already reconciles duplicate saves and raises its own error when 
the contents differ.
   - The SQL tables' primary key includes `position`, so two saves of one 
`sequence_id` with different action names both succeed. Changing the key needs 
a table migration, so I left it out.
   - Nothing locks a step while it runs (#786), so the writer that gets the 
error has already run the action.
   
   ## Testing
   
   All in Docker (`postgres:15`, `mongo:7.0`, `redis:7`, the same services as 
the CI persister job):
   
   - 13 new tests: a conflict test per persister, plus race tests for psycopg2, 
Redis (sync and async) and MongoDB. Each race test runs 50 rounds of two 
persisters saving the same step at once and checks that exactly one save wins 
and the stored state is the winner's.
   - On `main`, with only the new error class added, all 13 fail. With this 
change they pass. With the SQLite rollback removed, the SQLite test fails with 
`database is locked`.
   - `tests/core/test_persistence.py` and `tests/integrations/persisters/`: 159 
passed on Python 3.10 and 3.12. I didn't run the Aerospike tests (the client 
isn't installed in my image).
   - The full suite, as CI runs it: 789 passed, 2 skipped (Python 3.12).
   - The docs build with Sphinx. The new links resolve to the reference entry.
   - pre-commit passes.
   
   🤖 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