Duplicate key during snapshot

Hi all,

I have changed my configuration to Asynchronous cluster for all my listeners, and now, i have an error during snapshotting when i launch a lot of commands :

`
com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: contacts.snapshotevents.$aggregateIdentifier_1_type_1_sequenceNumber_1 dup key: { : “1”, : “GroupTree”, : 16207 }
at com.mongodb.CommandResult.getException(CommandResult.java:98)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:142)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:183)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:249)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:205)
at com.mongodb.DBCollection.insert(DBCollection.java:57)
at com.mongodb.DBCollection.insert(DBCollection.java:100)
at org.axonframework.eventstore.mongo.MongoEventStore.appendSnapshotEvent(MongoEventStore.java:194)
at org.axonframework.eventsourcing.AbstractSnapshotter$CreateSnapshotTask.run(AbstractSnapshotter.java:101)
at org.axonframework.eventsourcing.AbstractSnapshotter$TransactionalRunnableWrapper.run(AbstractSnapshotter.java:173)
at org.axonframework.eventsourcing.AbstractSnapshotter$SilentTask.run(AbstractSnapshotter.java:193)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

`

The error is displaying several times during operation.

My configuration :

`
<axon:cluster id=“defaultCluster” default=“true” />

<axon:cluster id=“replayingCluster”>
<axon:replay-config event-store=“eventStore” />
axon:selectors
<axon:package prefix=“mybasepackage.interfaces.query” />
</axon:selectors>
</axon:cluster>

<axon:cluster id=“asyncCluster”>







axon:selectors
<axon:package prefix=“mybasepackage” />
</axon:selectors>
</axon:cluster>

<axon:command-bus id=“commandBus” />

<axon:event-bus id=“eventBus” />

<axon:event-sourcing-repository id=“groupTreeRepository” aggregate-type=“mybasepackage.group.GroupTree”>
<axon:snapshotter-trigger event-count-threshold=“50” snapshotter-ref=“snapshotter” />
</axon:event-sourcing-repository>

[…]

`

Do you have an idea ?

Best regards,

Baptiste.

Hi,

snapshotting is an asynchronous process. It’s possible that two requests for the same snapshot are trigger in short succession. Currently, Axon will process both requests and has a chance to create two snapshots with the same sequence number. That’s what generates the duplicate key exception.

This reminds me that I wanted to include a feature that ignores the creation of a snapshot based on a single event. Will create an issue to track it.

Cheers,

Allard

Ok. if i understand this is just warning without consequence, may be my event-count-threshold value is too small.

Hi,

it can happen with any event-count-threshold. If two commands load the same aggregate is quick succession, it is possible that the second time happens before the snapshot is created, causing the trigger to fire again.
A snapshotter always creates a snapshot of the current state, not the state as it was when it was triggered.

Hooe this clarifies it a bit.
Cheers,

Allard