ENT-2355 Insert transactions without checking for duplicates (#1350)

* ENT-2355 Make transactions storage optimistic if in a flow and the flow has never restored from a checkpoint.

(cherry picked from commit 9a2e9b0)

* ENT-2355 Insert transactions with optimism.

* Added some comments.
This commit is contained in:
Rick Parker 2018-08-23 11:06:14 +01:00 committed by GitHub
parent 866e2a2a13
commit 759419fcc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -101,8 +101,16 @@ class DBTransactionStorage(cacheSizeBytes: Long, private val database: CordaPers
override fun addTransaction(transaction: SignedTransaction): Boolean = database.transaction {
txStorage.concurrent {
addWithDuplicatesAllowed(transaction.id, transaction.toTxCacheValue()).apply {
updatesPublisher.bufferUntilDatabaseCommit().onNext(transaction)
// Be optimistic if we are a flow and never restarted (i.e. cannot have been to the flow hospital due to primary key collision).
val optimistic = FlowStateMachineImpl.currentStateMachine()?.isNotRestarted ?: false
if (optimistic) {
set(transaction.id, transaction.toTxCacheValue()).apply {
updatesPublisher.bufferUntilDatabaseCommit().onNext(transaction)
}
} else {
addWithDuplicatesAllowed(transaction.id, transaction.toTxCacheValue()).apply {
updatesPublisher.bufferUntilDatabaseCommit().onNext(transaction)
}
}
}
}

View File

@ -87,6 +87,9 @@ class FlowStateMachineImpl<R>(override val id: StateMachineRunId,
internal var transientValues: TransientReference<TransientValues>? = null
internal var transientState: TransientReference<StateMachineState>? = null
// Utilise the behaviour of `ourSenderUUID` to indicate whether we have restarted ever. Can be used to make certain code paths optimistic.
internal val isNotRestarted: Boolean get() = (ourSenderUUID != null)
/**
* What sender identifier to put on messages sent by this flow. This will either be the identifier for the current
* state machine manager / messaging client, or null to indicate this flow is restored from a checkpoint and