disable local router caches

This commit is contained in:
a10zn8
2022-12-12 17:10:39 +04:00
parent e144a9b0de
commit 2c666eed99
13 changed files with 53 additions and 23 deletions

View File

@@ -17,6 +17,8 @@ package io.emeraldpay.dshackle.config
class CacheConfig {
var requestsCacheEnabled = true
var redis: Redis? = null
class Redis(

View File

@@ -33,6 +33,9 @@ class CacheConfigReader : YamlConfigReader(), ConfigReader<CacheConfig> {
override fun read(input: MappingNode?): CacheConfig? {
return getMapping(input, "cache")?.let { node ->
val config = CacheConfig()
getValueAsBool(node, "requests-cache-enabled")?.let {
config.requestsCacheEnabled = it
}
getMapping(node, "redis")?.let { redisNode ->
val redis = CacheConfig.Redis()
val enabled = getValueAsBool(redisNode, "enabled") ?: true
@@ -52,7 +55,7 @@ class CacheConfigReader : YamlConfigReader(), ConfigReader<CacheConfig> {
config.redis = redis
}
}
if (config.redis == null) {
if (config.redis == null && config.requestsCacheEnabled) {
return null
}
config

View File

@@ -23,6 +23,7 @@ import io.emeraldpay.dshackle.BlockchainType
import io.emeraldpay.dshackle.Chain
import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.SilentException
import io.emeraldpay.dshackle.config.CacheConfig
import io.emeraldpay.dshackle.quorum.CallQuorum
import io.emeraldpay.dshackle.quorum.NotLaggingQuorum
import io.emeraldpay.dshackle.quorum.QuorumReaderFactory
@@ -55,7 +56,8 @@ import java.util.concurrent.atomic.AtomicInteger
@Service
open class NativeCall(
private val multistreamHolder: MultistreamHolder,
private val signer: ResponseSigner
private val signer: ResponseSigner,
cacheConfig: CacheConfig
) {
private val log = LoggerFactory.getLogger(NativeCall::class.java)
@@ -63,6 +65,8 @@ open class NativeCall(
private val nullValue: ByteArray = "null".toByteArray()
private val localRouterEnabled = cacheConfig.requestsCacheEnabled
var quorumReaderFactory: QuorumReaderFactory = QuorumReaderFactory.default()
private val ethereumCallSelectors = EnumMap<Chain, EthereumCallSelector>(Chain::class.java)
@@ -121,9 +125,6 @@ open class NativeCall(
result.setErrorMessage(error.message).setErrorCode(error.id)
}
} else {
if (it.result == null || it.result.isEmpty() || nullValue.contentEquals(it.result)) {
log.warn("Empty result [${it.result}] on building response, method ${it.ctx?.payload?.method}, params ${it.ctx?.payload?.params}")
}
result.payload = ByteString.copyFrom(it.result)
}
if (it.nonce != null && it.signature != null) {
@@ -273,7 +274,7 @@ open class NativeCall(
if (method in DefaultEthereumMethods.newFilterMethods) CreateFilterDecorator() else NoneResultDecorator()
fun fetch(ctx: ValidCallContext<ParsedCallDetails>): Mono<CallResult> {
return ctx.upstream.getRoutedApi(ctx.matcher)
return ctx.upstream.getRoutedApi(localRouterEnabled)
.flatMap { api ->
api.read(JsonRpcRequest(ctx.payload.method, ctx.payload.params, ctx.nonce, ctx.forwardedSelector))
.flatMap(JsonRpcResponse::requireResult)

View File

@@ -148,7 +148,7 @@ abstract class Multistream(
/**
* Finds an API that leverages caches and other optimizations/transformations of the request.
*/
abstract fun getRoutedApi(matcher: Selector.Matcher): Mono<Reader<JsonRpcRequest, JsonRpcResponse>>
abstract fun getRoutedApi(localEnabled: Boolean): Mono<Reader<JsonRpcRequest, JsonRpcResponse>>
override fun getApi(): Reader<JsonRpcRequest, JsonRpcResponse> {
throw NotImplementedError("Immediate direct API is not implemented for Aggregated Upstream")

View File

@@ -106,7 +106,7 @@ open class BitcoinMultistream(
.switchIfEmpty(Mono.error(Exception("No API available for $chain")))
}
override fun getRoutedApi(matcher: Selector.Matcher): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
override fun getRoutedApi(localEnabled: Boolean): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
return Mono.just(callRouter)
}

View File

@@ -154,8 +154,8 @@ open class EthereumMultistream(
return this as T
}
override fun getRoutedApi(matcher: Selector.Matcher): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
return Mono.just(LocalCallRouter(reader, getMethods(), getHead()))
override fun getRoutedApi(localEnabled: Boolean): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
return Mono.just(LocalCallRouter(reader, getMethods(), getHead(), localEnabled))
}
override fun getSubscribe(): EthereumSubscribe {

View File

@@ -39,7 +39,8 @@ import java.math.BigInteger
class LocalCallRouter(
private val reader: EthereumCachingReader,
private val methods: CallMethods,
private val head: Head
private val head: Head,
private val localEnabled: Boolean
) : Reader<JsonRpcRequest, JsonRpcResponse> {
companion object {
@@ -56,7 +57,9 @@ class LocalCallRouter(
return Mono.just(methods.executeHardcoded(key.method))
.map { JsonRpcResponse(it, null) }
}
if (!localEnabled) {
return Mono.empty()
}
if (!methods.isCallable(key.method)) {
return Mono.error(RpcException(RpcResponseError.CODE_METHOD_NOT_EXIST, "Unsupported method"))
}

View File

@@ -147,8 +147,8 @@ open class EthereumPosMultiStream(
return this as T
}
override fun getRoutedApi(matcher: Selector.Matcher): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
return Mono.just(LocalCallRouter(reader, getMethods(), getHead()))
override fun getRoutedApi(localEnabled: Boolean): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
return Mono.just(LocalCallRouter(reader, getMethods(), getHead(), localEnabled))
}
override fun getSubscribe(): EthereumSubscribe {