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

Refactor account processing implementation to be more efficient #553

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* Removes the logic for calculating `running_count` and `total_count` in the `missed_blocks` [#548](https://github.com/provenance-io/explorer-service/pull/548)
* Remove update block latency procedure call [#550](https://github.com/provenance-io/explorer-service/pull/550)
* Docker images at tag `latest` for main branch merges [#551](https://github.com/provenance-io/explorer-service/pull/551)
* Refactor account processing implementation to be more efficient [#552](https://github.com/provenance-io/explorer-service/issues/552)

## [v5.11.0](https://github.com/provenance-io/explorer-service/releases/tag/v5.11.0) - 2024-08-27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ import io.provenance.explorer.service.PricingService
import io.provenance.explorer.service.TokenService
import io.provenance.explorer.service.ValidatorService
import io.provenance.explorer.service.getBlock
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
Expand Down Expand Up @@ -445,32 +439,25 @@ class ScheduledTaskService(
}

@Scheduled(initialDelay = 5000L, fixedDelay = 5000L)
fun startAccountProcess() = runBlocking {
ProcessQueueRecord.reset(ProcessQueueType.ACCOUNT)
val producer = startAccountProcess()
repeat(5) { accountProcessor(producer) }
fun startAccountProcess() {
processAccountRecords()
}

@OptIn(ExperimentalCoroutinesApi::class)
fun CoroutineScope.startAccountProcess() = produce {
while (true) {
ProcessQueueRecord.findByType(ProcessQueueType.ACCOUNT).firstOrNull()?.let {
fun processAccountRecords() {
ProcessQueueRecord.reset(ProcessQueueType.ACCOUNT)
val records = ProcessQueueRecord.findByType(ProcessQueueType.ACCOUNT)
runBlocking {
nullpointer0x00 marked this conversation as resolved.
Show resolved Hide resolved
for (record in records) {
try {
transaction { it.apply { this.processing = true } }
send(it.processValue)
transaction { record.apply { this.processing = true } }
accountService.updateTokenCounts(record.processValue)
Copy link

@scirner22 scirner22 Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now - updateTokenCounts is suspended. I think you'll want to do the following because as it stands right now the subsequent line is ran before updateTokenCounts completes, and also whether it throws an exception or not.

Suggested change
accountService.updateTokenCounts(record.processValue)
runBlocking { accountService.updateTokenCounts(record.processValue) }

Copy link

@scirner22 scirner22 Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated my suggested change above so that there's no blocking calls inside of the runBlocking, even this isn't idiomatic, but at least it's correct from a concurrency perspective and alleviates the need for a larger change surface area.

With this, the runBlocking on 449 can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good catch. I was hoping not to use runBlocking at all, but all the grpc client methods use suspend. I decided I would look into why this pattern is used instead of change it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a great model, but requires up and down the stack to adhere to it. In this case, the jdbc driver is thread based calls so it requires constantly bridging blocking and nonblocking code.

ProcessQueueRecord.delete(ProcessQueueType.ACCOUNT, record.processValue)
} catch (_: Exception) {
}
}
}
}

fun CoroutineScope.accountProcessor(channel: ReceiveChannel<String>) = launch(Dispatchers.IO) {
for (msg in channel) {
accountService.updateTokenCounts(msg)
ProcessQueueRecord.delete(ProcessQueueType.ACCOUNT, msg)
}
}

@Scheduled(cron = "0 0 0 * * *") // Every beginning of every day
fun calculateValidatorMetrics() {
val (year, quarter) = DateTime.now().minusMinutes(5).let { it.year to it.monthOfYear.monthToQuarter() }
Expand Down
Loading