Skip to content

Commit

Permalink
Fix chaincode retry state transition (#101)
Browse files Browse the repository at this point in the history
- the retrial of 'dead' transactions (expired transactions that come back from the chain query as not found) was getting filtered out by the EventService, due to the 'transition' between ENVELOPE_CHAINCODE -> ENVELOPE_CHAINCODE not being listed as allowed
- Also added in an MDC fix for the event handler threads
  • Loading branch information
celloman authored Dec 6, 2021
1 parent d039c9e commit 3dac4e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import com.google.protobuf.util.JsonFormat
import io.p8e.proto.Events.P8eEvent
import io.p8e.proto.Events.P8eEvent.Event
import io.p8e.util.ThreadPoolFactory
import io.p8e.util.or
import io.p8e.util.orThrow
import io.p8e.util.timed
import io.provenance.p8e.shared.extension.logger
import io.provenance.engine.domain.EventRecord
import io.provenance.engine.domain.EventStatus
import io.provenance.engine.domain.EventTable
import io.provenance.p8e.shared.domain.EnvelopeRecord
import io.provenance.p8e.shared.util.P8eMDC
import org.jetbrains.exposed.sql.SizedIterable
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.and
Expand Down Expand Up @@ -43,7 +46,7 @@ class EventService() {
Event.ENVELOPE_FRAGMENT to listOf(Event.ENVELOPE_CHAINCODE, Event.ENVELOPE_ERROR),
Event.ENVELOPE_REQUEST to listOf(Event.ENVELOPE_MAILBOX_OUTBOUND),
Event.ENVELOPE_MAILBOX_OUTBOUND to listOf(Event.SCOPE_INDEX, Event.SCOPE_INDEX_FRAGMENT),
Event.ENVELOPE_CHAINCODE to listOf(Event.SCOPE_INDEX, Event.SCOPE_INDEX_FRAGMENT, Event.ENVELOPE_ERROR),
Event.ENVELOPE_CHAINCODE to listOf(Event.SCOPE_INDEX, Event.SCOPE_INDEX_FRAGMENT, Event.ENVELOPE_ERROR, Event.ENVELOPE_CHAINCODE),
Event.SCOPE_INDEX to listOf(Event.ENVELOPE_RESPONSE),
Event.SCOPE_INDEX_FRAGMENT to listOf(Event.ENVELOPE_RESPONSE),
Event.ENVELOPE_RESPONSE to listOf(), // end of the line
Expand All @@ -64,6 +67,8 @@ class EventService() {
val validTransitions = VALID_STATE_TRANSITIONS[it.event]
if (validTransitions == null || validTransitions.contains(event.event)) {
it.update(event, status, created, created).also(::submitToChannel)
} else {
log.info("Skipping invalid event transition of ${it.event} -> ${event.event}")
}
} ?: EventRecord.insert(event, envelopeUuid, status, created, created).also(::submitToChannel)

Expand Down Expand Up @@ -201,6 +206,11 @@ class NotificationHandler(private val event: Event) {
fun notification(tableEvent: EventRecord) {
thread(pool = computeExecutor(tableEvent.event)) {
try {
transaction { EnvelopeRecord.findById(tableEvent.envelopeUuid) }?.let {
P8eMDC.set(it, clear = true)
}.or {
log.warn("Failed to find envelope record for MDC (envelopeUuid: ${tableEvent.envelopeUuid}, eventUuid: ${tableEvent.eventUuid.value})")
}
when (tableEvent.status) {
EventStatus.CREATED -> {
log.info("Received CREATED event: [${JsonFormat.printer().print(tableEvent.payload)}")
Expand Down
20 changes: 20 additions & 0 deletions p8e-api/src/test/kotlin/service/EventServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,24 @@ class EventServiceTest {
Assert.assertEquals(Event.ENVELOPE_RESPONSE, updatedRecord.event)
Assert.assertEquals("some-other-test-message".toByteString(), updatedRecord.payload.message)
}

@Test
fun `Verify ENVELOPE_CHAINCODE to ENVELOPE_CHAINCODE is a valid transition`() {
// CHAINCODE -> CHAINCODE transition for TransactionStatusService.retryDead case
val event = Events.P8eEvent.newBuilder()
.setEvent(Event.ENVELOPE_CHAINCODE)
.setMessage("some-test-message".toByteString())
.build()

transaction { EventRecord.insert(event, envelopeRecord.uuid.value) }

val event2 = Events.P8eEvent.newBuilder()
.setEvent(Event.ENVELOPE_CHAINCODE)
.setMessage("some-other-test-message".toByteString())
.build()
val updatedRecord = transaction { eventService.submitEvent(event2, envelopeRecord.uuid.value, EventStatus.CREATED, createdTime) }

// event should be updated, since you can go from ENVELOPE_CHAINCODE -> ENVELOPE_CHAINCODE
Assert.assertEquals("some-other-test-message".toByteString(), updatedRecord.payload.message)
}
}

0 comments on commit 3dac4e8

Please sign in to comment.