From 447fe170ce89cd7b075bbc7f32ea80c754c8aced Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Thu, 19 Sep 2024 11:49:01 -0600 Subject: [PATCH] Remove the logic for calculating `running_count` and `total_count` in the `missed_blocks` (#548) * remove logic to update running count and total count * add temp logging * add temp counter * comment out calc latency function * remove tmp logging, uncomment latency calc * fix formatting * add issue link to todo * add change log entry --- CHANGELOG.md | 1 + .../explorer/domain/entities/Blocks.kt | 77 +------------------ .../explorer/service/ValidatorService.kt | 2 +- 3 files changed, 6 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86a15720..43c785d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * Add limit of 1 to missing block queries [#544](https://github.com/provenance-io/explorer-service/pull/544) +* Removes the logic for calculating `running_count` and `total_count` in the `missed_blocks` [#548](https://github.com/provenance-io/explorer-service/pull/548) ## [v5.11.0](https://github.com/provenance-io/explorer-service/releases/tag/v5.11.0) - 2024-08-27 diff --git a/service/src/main/kotlin/io/provenance/explorer/domain/entities/Blocks.kt b/service/src/main/kotlin/io/provenance/explorer/domain/entities/Blocks.kt index 01512d59..5eff818a 100644 --- a/service/src/main/kotlin/io/provenance/explorer/domain/entities/Blocks.kt +++ b/service/src/main/kotlin/io/provenance/explorer/domain/entities/Blocks.kt @@ -2,7 +2,6 @@ package io.provenance.explorer.domain.entities import cosmos.base.tendermint.v1beta1.Query import io.provenance.explorer.OBJECT_MAPPER -import io.provenance.explorer.domain.core.logger import io.provenance.explorer.domain.core.sql.DateTrunc import io.provenance.explorer.domain.core.sql.Distinct import io.provenance.explorer.domain.core.sql.ExtractDOW @@ -58,9 +57,7 @@ import org.jetbrains.exposed.sql.jodatime.datetime import org.jetbrains.exposed.sql.leftJoin import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.selectAll -import org.jetbrains.exposed.sql.statements.BatchUpdateStatement import org.jetbrains.exposed.sql.sum -import org.jetbrains.exposed.sql.transactions.TransactionManager import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.update import org.joda.time.DateTime @@ -263,79 +260,13 @@ class MissedBlocksRecord(id: EntityID) : IntEntity(id) { query.exec(arguments).map { it.getString("val_cons_address") } } - fun findLatestForVal(valconsAddr: String) = transaction { - MissedBlocksRecord.find { MissedBlocksTable.valConsAddr eq valconsAddr } - .orderBy(Pair(MissedBlocksTable.blockHeight, SortOrder.DESC)) - .limit(1) - .firstOrNull() - } - - fun findForValFirstUnderHeight(valconsAddr: String, height: Int) = transaction { - MissedBlocksRecord - .find { (MissedBlocksTable.valConsAddr eq valconsAddr) and (MissedBlocksTable.blockHeight lessEq height) } - .orderBy(Pair(MissedBlocksTable.blockHeight, SortOrder.DESC)) - .limit(1) - .firstOrNull() - } - - fun calculateMissedAndInsert(height: Int, valconsAddr: String) = transaction { - val startTime = System.currentTimeMillis() - - val (running, total, updateFromHeight) = findLatestForVal(valconsAddr)?.let { rec -> - when { - rec.blockHeight == height - 1 -> listOf(rec.runningCount, rec.totalCount, null) - rec.blockHeight > height -> - when (val last = findForValFirstUnderHeight(valconsAddr, height - 1)) { - null -> listOf(0, 0, height) - else -> listOf( - if (last.blockHeight == height - 1) last.runningCount else 0, - last.totalCount, - height - ) - } - else -> listOf(0, rec.totalCount, null) - } - } ?: listOf(0, 0, null) - + fun insert(height: Int, valconsAddr: String) = transaction { MissedBlocksTable.insertIgnore { it[this.blockHeight] = height it[this.valConsAddr] = valconsAddr - it[this.runningCount] = running!! + 1 - it[this.totalCount] = total!! + 1 - } - - if (updateFromHeight != null) { - updateRecords(updateFromHeight, valconsAddr, running!! + 1, total!! + 1) - } - - val endTime = System.currentTimeMillis() - val duration = endTime - startTime - - // TODO: remove warning if problem is fixed after adding limit to queries See: https://github.com/provenance-io/explorer-service/issues/546 - if (duration > 1000) { - logger().warn("Processing calculateMissedAndInsert took ${duration}ms for height: $height and valconsAddr: $valconsAddr") - } - } - - fun updateRecords(height: Int, valconsAddr: String, currRunning: Int, currTotal: Int) = transaction { - val records = MissedBlocksRecord - .find { (MissedBlocksTable.valConsAddr eq valconsAddr) and (MissedBlocksTable.blockHeight greater height) } - .orderBy(Pair(MissedBlocksTable.blockHeight, SortOrder.ASC)) - - BatchUpdateStatement(MissedBlocksTable).apply { - var lastHeight = height - var lastRunning = currRunning - var lastTotal = currTotal - records.forEach { - addBatch(it.id) - val running = if (lastHeight == it.blockHeight - 1) lastRunning + 1 else 1 - this[MissedBlocksTable.runningCount] = running - this[MissedBlocksTable.totalCount] = lastTotal + 1 - lastHeight = it.blockHeight - lastRunning = running - lastTotal += 1 - } - execute(TransactionManager.current()) + // TODO: remove these column from database See: https://github.com/provenance-io/explorer-service/issues/549 + it[this.runningCount] = -1 + it[this.totalCount] = -1 } } } diff --git a/service/src/main/kotlin/io/provenance/explorer/service/ValidatorService.kt b/service/src/main/kotlin/io/provenance/explorer/service/ValidatorService.kt index 27942e04..61c3f3b8 100644 --- a/service/src/main/kotlin/io/provenance/explorer/service/ValidatorService.kt +++ b/service/src/main/kotlin/io/provenance/explorer/service/ValidatorService.kt @@ -502,7 +502,7 @@ class ValidatorService( currentVals.validatorsList.forEach { vali -> if (!signatures.contains(vali.address)) { - MissedBlocksRecord.calculateMissedAndInsert(lastBlock.height.toInt(), vali.address) + MissedBlocksRecord.insert(lastBlock.height.toInt(), vali.address) } } }