Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(framework-provider-aws): ensure storeSnapshot is idempotent #1419

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 29 additions & 42 deletions packages/framework-provider-aws/src/library/events-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,50 +114,37 @@ export async function storeSnapshot(
snapshotEnvelope: NonPersistedEntitySnapshotEnvelope,
config: BoosterConfig
): Promise<EntitySnapshotEnvelope> {
try {
const logger = getLogger(config, 'EventsAdapter#storeSnapshot')
logger.debug('Storing the following snapshot:', snapshotEnvelope)
const logger = getLogger(config, 'EventsAdapter#storeSnapshot')
logger.debug('Storing the following snapshot:', snapshotEnvelope)

const partitionKey = partitionKeyForEntitySnapshot(snapshotEnvelope.entityTypeName, snapshotEnvelope.entityID)
/**
* The sort key of the snapshot matches the sort key of the last event that generated it.
* Entity snapshots can be potentially created by competing processes, and this way
* of storing the data makes snapshot creation an idempotent operation, allowing us to
* aggressively cache snapshots. If the snapshot already exists, it will be silently overwritten.
*/
const sortKey = snapshotEnvelope.snapshottedEventCreatedAt
const persistableSnapshot = {
...snapshotEnvelope,
createdAt: snapshotEnvelope.snapshottedEventCreatedAt,
persistedAt: new Date().toISOString(),
}
await dynamoDB
.put({
TableName: config.resourceNames.eventsStore,
ConditionExpression: `${eventsStoreAttributes.partitionKey} <> :partitionKey AND ${eventsStoreAttributes.sortKey} <> :sortKey`,
ExpressionAttributeValues: {
':partitionKey': partitionKey,
':sortKey': sortKey,
},
Item: {
...persistableSnapshot,
[eventsStoreAttributes.partitionKey]: partitionKey,
[eventsStoreAttributes.sortKey]: sortKey,
[eventsStoreAttributes.indexByEntity.partitionKey]: partitionKeyForIndexByEntity(
snapshotEnvelope.entityTypeName,
snapshotEnvelope.kind
),
},
})
.promise()
return persistableSnapshot
} catch (e) {
const error = e as Error
if (error.name == 'ConditionalCheckFailedException') {
throw new OptimisticConcurrencyUnexpectedVersionError(error.message)
}
throw e
const partitionKey = partitionKeyForEntitySnapshot(snapshotEnvelope.entityTypeName, snapshotEnvelope.entityID)
/**
* The sort key of the snapshot matches the sort key of the last event that generated it.
* Entity snapshots can be potentially created by competing processes, and this way
* of storing the data makes snapshot creation an idempotent operation, allowing us to
* aggressively cache snapshots. If the snapshot already exists, it will be silently overwritten.
*/
const sortKey = snapshotEnvelope.snapshottedEventCreatedAt
const persistableSnapshot = {
...snapshotEnvelope,
createdAt: snapshotEnvelope.snapshottedEventCreatedAt,
persistedAt: new Date().toISOString(),
}
await dynamoDB
.put({
TableName: config.resourceNames.eventsStore,
Item: {
...persistableSnapshot,
[eventsStoreAttributes.partitionKey]: partitionKey,
[eventsStoreAttributes.sortKey]: sortKey,
[eventsStoreAttributes.indexByEntity.partitionKey]: partitionKeyForIndexByEntity(
snapshotEnvelope.entityTypeName,
snapshotEnvelope.kind
),
},
})
.promise()
return persistableSnapshot
}

async function persistEvent(
Expand Down