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

@kulla wip: Replay transactions on deadlock #1542

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions packages/server/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@
this.state = { type: 'OutsideOfTransaction' }
}

public async withTransaction(fn: () => Promise<void>) {
const transaction = await this.beginTransaction()

try {
await fn()

await transaction.commit()
} catch (error: unknown) {
console.log(error)

Check failure on line 27 in packages/server/src/database.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

if (this.state.type !== 'OutsideOfTransaction') {
console.log(

Check failure on line 30 in packages/server/src/database.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement
await this.state.transaction.query('SHOW ENGINE INNODB STATUS;'),
)
}

await transaction.rollback()

if (
error instanceof Error &&
'code' in error &&
error.code === 'ER_LOCK_DEADLOCK'
) {
console.log('deadlock detected, retrying')

Check failure on line 42 in packages/server/src/database.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

await this.withTransaction(fn)
} else {
throw error
}
}
}

public async beginTransaction() {
if (this.state.type === 'OutsideOfTransaction') {
const transaction = await this.pool.getConnection()
Expand Down
10 changes: 2 additions & 8 deletions packages/server/src/schema/events/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export async function createEvent(
const abstractEventPayload = toDatabaseRepresentation(payload)
const { type, actorId, objectId, instance } = abstractEventPayload

const transaction = await database.beginTransaction()

try {
await database.withTransaction(async () => {
const { insertId: eventId } = await database.mutate(
`
INSERT INTO event_log (actor_id, event_id, uuid_id, instance_id)
Expand Down Expand Up @@ -83,11 +81,7 @@ export async function createEvent(
const event = { ...abstractEventPayload, id: eventId }

await createNotifications(event, { database })

await transaction.commit()
} finally {
await transaction.rollback()
}
})
}

async function createNotifications(
Expand Down
Loading