Skip to content

Commit

Permalink
Transactions are now rolled back when something goes wrong during pre…
Browse files Browse the repository at this point in the history
…paration.
  • Loading branch information
ppanopticon committed May 17, 2024
1 parent 6f1998b commit 5b65898
Showing 1 changed file with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,6 @@ internal interface TransactionalGrpcService {
metadata.queryId
}

/* Obtain transaction context. */
val transactionContext = if (metadata.transactionId <= 0L) {
if (readOnly) { /* Start new transaction. */
this.manager.startTransaction(TransactionType.USER_IMPLICIT_READONLY)
} else {
this.manager.startTransaction(TransactionType.USER_IMPLICIT_EXCLUSIVE)
}
} else { /* Reuse existing transaction. */
val txn = this.manager[metadata.transactionId]
if (txn === null || txn.type.autoCommit) {
throw Status.FAILED_PRECONDITION.withDescription( "Execution failed because transaction ${metadata.transactionId} could not be resumed because it doesn't exist or has the wrong type.").asException()
}
txn
}

/* Parse all the query hints provided by the user. */
val hints = mutableSetOf<QueryHint>()
if (metadata.noOptimiseHint) {
Expand Down Expand Up @@ -106,6 +91,22 @@ internal interface TransactionalGrpcService {
))
}

/* Obtain transaction context. */
val transactionContext = if (metadata.transactionId <= 0L) {
if (readOnly) { /* Start new transaction. */
this.manager.startTransaction(TransactionType.USER_IMPLICIT_READONLY)
} else {
this.manager.startTransaction(TransactionType.USER_IMPLICIT_EXCLUSIVE)
}
} else { /* Reuse existing transaction. */
val txn = this.manager[metadata.transactionId]
if (txn === null || txn.type.autoCommit) {
throw Status.FAILED_PRECONDITION.withDescription( "Execution failed because transaction ${metadata.transactionId} could not be resumed because it doesn't exist or has the wrong type.").asException()
}
txn
}

/* Return query context. */
return DefaultQueryContext(queryId, this.catalogue, transactionContext, hints)
}

Expand Down Expand Up @@ -135,6 +136,12 @@ internal interface TransactionalGrpcService {
LOGGER.debug("[${context.txn.transactionId}, ${context.queryId}] Preparation of ${context.physical.firstOrNull()?.name} completed successfully in $d.")
p to d
} catch (e: Throwable) {
/* Rollback transaction */
if (context.txn.type.autoRollback) {
context.txn.rollback()
}

/* Handle error. */
throw context.handleError(e, false)
}

Expand Down

0 comments on commit 5b65898

Please sign in to comment.