tobia80/zio-entity

Concurrent appending of events cause constraint violation

Opened this issue · 1 comments

It looks like this part:

https://github.com/thehonesttech/zio-entity/blob/71dccddd986280c70ef265f9fbe01a1850b1a08e/core/src/main/scala/zio/entity/core/KeyedAlgebraCombinators.scala#L32-L45

cause issues when events are appended in a concurrent way.

Consider two threads/fibers: T1, T2
T1 and T2 read offset (say offset = 2)
after that offset is used to insert data into store (postgres):

eventJournal.append(key, offset, events)

T1 and T2 tries to insert with offset = 2 and one ends with exception like this:

ava.sql.BatchUpdateException: Batch entry 0 INSERT INTO ledger_event (key, seq_nr, event, tags) VALUES (?, 6, ?, ?) was aborted: ERROR: duplicate key value violates unique constraint "ledger_event_key_seq_nr_uindex"
  Detail: Key (key, seq_nr)=(\x7b2276616c7565223a7b2276616c7565223a2230636634626635362d396166362d343930612d386132372d303833333735623135383730227d7d, 2) already exists.  Call getNextException to see other errors in the batch.

Thanks for the issue! Can you please provide a concrete example? in an entity no parallel execution should happen calling append so the append is supposed to run in sequence