solution: allow configuring a quorum for custom methods

This commit is contained in:
Igor Artamonov
2021-08-04 14:58:28 -04:00
parent 96109d4013
commit 2f2a553cd4
12 changed files with 166 additions and 14 deletions

View File

@@ -18,7 +18,7 @@ package io.emeraldpay.dshackle
import java.io.File
class FileResolver(
open class FileResolver(
private val baseDir: File
) {

View File

@@ -33,7 +33,7 @@ import kotlin.system.exitProcess
@Repository
class CachesFactory(
open class CachesFactory(
@Autowired private val cacheConfig: CacheConfig
) {

View File

@@ -23,7 +23,7 @@ import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
class UpstreamsConfig {
open class UpstreamsConfig {
var defaultOptions: MutableList<DefaultOptions> = ArrayList<DefaultOptions>()
var upstreams: MutableList<Upstream<*>> = ArrayList<Upstream<*>>()
@@ -172,6 +172,7 @@ class UpstreamsConfig {
)
class Method(
val name: String
val name: String,
val quorum: String? = null
)
}

View File

@@ -232,7 +232,8 @@ class UpstreamsConfigReader(
val enabled = getList<MappingNode>(mnode, "enabled")?.value?.map { m ->
getValueAsString(m, "name")?.let { name ->
UpstreamsConfig.Method(
name = name
name = name,
quorum = getValueAsString(m, "quorum")
)
}
}?.filterNotNull()?.toSet() ?: emptySet()

View File

@@ -130,12 +130,18 @@ open class ConfiguredUpstreams(
return defaultOptions
}
private fun buildMethods(config: UpstreamsConfig.Upstream<*>, chain: Chain): CallMethods {
fun buildMethods(config: UpstreamsConfig.Upstream<*>, chain: Chain): CallMethods {
return if (config.methods != null) {
ManagedCallMethods(currentUpstreams.getDefaultMethods(chain),
config.methods!!.enabled.map { it.name }.toSet(),
config.methods!!.disabled.map { it.name }.toSet()
)
).also {
config.methods!!.enabled.forEach { m ->
if (m.quorum != null) {
it.setQuorum(m.name, m.quorum)
}
}
}
} else {
currentUpstreams.getDefaultMethods(chain)
}

View File

@@ -42,7 +42,7 @@ import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
@Repository
class CurrentMultistreamHolder(
open class CurrentMultistreamHolder(
@Autowired private val cachesFactory: CachesFactory
) : MultistreamHolder {

View File

@@ -18,8 +18,11 @@ package io.emeraldpay.dshackle.upstream.calls
import io.emeraldpay.dshackle.quorum.AlwaysQuorum
import io.emeraldpay.dshackle.quorum.CallQuorum
import io.emeraldpay.dshackle.quorum.NonEmptyQuorum
import io.emeraldpay.dshackle.quorum.NotLaggingQuorum
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.collections.HashMap
/**
* Wrapper on top of another configuration, that may disable or enable additional methods on top of it.
@@ -34,20 +37,41 @@ class ManagedCallMethods(
companion object {
private val log = LoggerFactory.getLogger(ManagedCallMethods::class.java)
private val defaultQuorum = AlwaysQuorum()
}
private val delegated = delegate.getSupportedMethods().sorted()
private val allAllowed: Set<String> = Collections.unmodifiableSet(
enabled + delegated - disabled
)
private val quorum: MutableMap<String, CallQuorum> = HashMap()
init {
enabled.forEach { m ->
quorum[m] = defaultQuorum
}
}
fun setQuorum(method: String, quorumId: String) {
val quorum = when (quorumId) {
"always" -> AlwaysQuorum()
"no-lag", "not-lagging", "no_lag", "not_lagging" -> NotLaggingQuorum(0)
"not-empty", "not_empty", "non-empty", "non_empty" -> NonEmptyQuorum()
else -> {
log.warn("Unknown quorum: $quorumId for custom method $method")
return
}
}
this.quorum[method] = quorum
}
override fun getQuorumFor(method: String): CallQuorum {
return when {
Collections.binarySearch(delegated, method) >= 0 -> delegate.getQuorumFor(method)
enabled.contains(method) -> AlwaysQuorum()
enabled.contains(method) -> quorum[method] ?: defaultQuorum
else -> {
log.warn("Getting quorum for unknown method")
AlwaysQuorum()
defaultQuorum
}
}
}