Turn off caches for main, fix cache not enrichment blocks (#160)

This commit is contained in:
KirillPamPam
2023-03-15 13:38:07 +04:00
committed by GitHub
parent 26d518d98e
commit a0dfb86c54
9 changed files with 77 additions and 27 deletions

View File

@@ -39,6 +39,9 @@ open class BlocksMemCache(
}
open fun add(block: BlockContainer) {
if (!block.enriched) {
return
}
mapping.put(block.hash, block)
}

View File

@@ -39,7 +39,8 @@ open class Caches(
private val redisBlocksByHash: BlocksRedisCache?,
private val redisTxsByHash: TxRedisCache?,
private val redisReceipts: ReceiptRedisCache?,
private val redisHeightByHashCache: HeightByHashRedisCache?
private val redisHeightByHashCache: HeightByHashRedisCache?,
private val cacheEnabled: Boolean
) {
companion object {
@@ -89,6 +90,9 @@ open class Caches(
}
open fun cacheReceipt(tag: Tag, data: DefaultContainer<TransactionReceiptJson>) {
if (!cacheEnabled) {
return
}
val currentHeight = head?.getCurrentHeight()
if (currentHeight != null && data.height != null && memReceipts.acceptsRecentBlocks(currentHeight - data.height)) {
memReceipts.add(data).subscribe()
@@ -98,6 +102,9 @@ open class Caches(
}
fun cache(tag: Tag, tx: TxContainer) {
if (!cacheEnabled) {
return
}
// do not cache transactions that are not in a block yet
if (tx.blockId == null) {
return
@@ -110,6 +117,9 @@ open class Caches(
}
fun cache(tag: Tag, block: BlockContainer) {
if (!cacheEnabled) {
return
}
val job = ArrayList<Mono<Void>>()
redisHeightByHashCache?.add(block)?.let(job::add)
@@ -129,7 +139,6 @@ open class Caches(
blockOnlyContainer = block
}
memoizeBlock(blockOnlyContainer)
memBlocksByHash.add(blockOnlyContainer)
redisBlocksByHash?.add(blockOnlyContainer)?.let(job::add)
// now cache only transactions
@@ -198,7 +207,7 @@ open class Caches(
return receiptByHash
}
fun getLastHeightByHash(): Reader<BlockId, Long> {
open fun getLastHeightByHash(): Reader<BlockId, Long> {
return memHeightByHash
}
@@ -227,6 +236,7 @@ open class Caches(
private var redisTxsByHash: TxRedisCache? = null
private var redisReceiptCache: ReceiptRedisCache? = null
private var redisHeightByHashCache: HeightByHashRedisCache? = null
private var cacheEnabled: Boolean = true
fun setBlockByHash(cache: BlocksMemCache): Builder {
blocksByHash = cache
@@ -268,6 +278,11 @@ open class Caches(
return this
}
fun setCacheEnabled(cacheEnabled: Boolean): Builder {
this.cacheEnabled = cacheEnabled
return this
}
fun build(): Caches {
if (blocksByHash == null) {
blocksByHash = BlocksMemCache()
@@ -283,7 +298,7 @@ open class Caches(
}
return Caches(
blocksByHash!!, blocksByHeight!!, txsByHash!!, receipts!!,
redisBlocksByHash, redisTxsByHash, redisReceiptCache, redisHeightByHashCache
redisBlocksByHash, redisTxsByHash, redisReceiptCache, redisHeightByHashCache, cacheEnabled
)
}
}

View File

@@ -97,6 +97,7 @@ open class CachesFactory(
caches.setReceipts(ReceiptRedisCache(redis.reactive(), chain))
caches.setHeightByHash(HeightByHashRedisCache(redis.reactive(), chain))
}
caches.setCacheEnabled(cacheConfig.requestsCacheEnabled)
return caches.build()
}

View File

@@ -22,7 +22,7 @@ import io.emeraldpay.dshackle.reader.Reader
import org.slf4j.LoggerFactory
import reactor.core.publisher.Mono
class HeightByHashMemCache(
open class HeightByHashMemCache(
maxSize: Int = 256
) : Reader<BlockId, Long> {

View File

@@ -39,6 +39,7 @@ class BlockContainer(
val nodeRating: Int = 0,
val upstreamId: String = ""
) : SourceContainer(json, parsed) {
val enriched: Boolean = transactions.isNotEmpty()
companion object {
@JvmStatic

View File

@@ -164,10 +164,10 @@ class EthereumCallSelector(
private fun blockByHashFromCache(blockHash: String): Mono<Selector.Matcher> {
return try {
caches.getBlocksByHash()
caches.getLastHeightByHash()
.read(BlockId.from(blockHash))
.onErrorResume { Mono.empty() }
.map { Selector.HeightMatcher(it.height) }
.map { Selector.HeightMatcher(it) }
} catch (e: DecoderException) {
log.warn("Invalid blockHash: $blockHash")
Mono.empty()