solution: refactor and redesign API access, now with AggregateUpstream providing base direct/routed apis, plus actual chain upstreams providing specific readers

This commit is contained in:
Igor Artamonov
2020-05-13 22:29:21 -04:00
parent a539ad3b84
commit 042408b4e6
102 changed files with 1649 additions and 2255 deletions

View File

@@ -16,8 +16,10 @@
*/
package io.emeraldpay.dshackle.upstream
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.cache.Caches
import io.emeraldpay.dshackle.reader.Reader
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import io.emeraldpay.grpc.Chain
import org.slf4j.LoggerFactory
import org.springframework.context.Lifecycle
@@ -29,16 +31,15 @@ import java.time.Duration
/**
* General interface to upstream(s) to a single chain
*/
abstract class ChainUpstreams<U : UpstreamApi>(
abstract class ChainUpstreams(
val chain: Chain,
private val upstreams: MutableList<Upstream<U>>,
caches: Caches,
objectMapper: ObjectMapper
) : AggregatedUpstream<U>(objectMapper, caches), Lifecycle {
private val upstreams: MutableList<Upstream>,
caches: Caches
) : AggregatedUpstream(caches), Lifecycle {
private val log = LoggerFactory.getLogger(ChainUpstreams::class.java)
private var seq = 0
protected var lagObserver: HeadLagObserver<U>? = null
protected var lagObserver: HeadLagObserver? = null
private var subscription: Disposable? = null
open fun init() {
@@ -75,11 +76,11 @@ abstract class ChainUpstreams<U : UpstreamApi>(
lagObserver?.stop()
}
override fun getAll(): List<Upstream<U>> {
override fun getAll(): List<Upstream> {
return upstreams
}
override fun addUpstream(upstream: Upstream<U>) {
override fun addUpstream(upstream: Upstream) {
upstreams.add(upstream)
setHead(updateHead())
onUpstreamsUpdated()
@@ -92,7 +93,7 @@ abstract class ChainUpstreams<U : UpstreamApi>(
}
}
override fun getApis(matcher: Selector.Matcher): ApiSource<U> {
override fun getApiSource(matcher: Selector.Matcher): ApiSource {
val i = seq++
if (seq >= Int.MAX_VALUE / 2) {
seq = 0
@@ -100,11 +101,11 @@ abstract class ChainUpstreams<U : UpstreamApi>(
return FilteredApis(upstreams, matcher, i)
}
override fun getApi(matcher: Selector.Matcher): Mono<U> {
val apis = getApis(matcher)
override fun getDirectApi(matcher: Selector.Matcher): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
val apis = getApiSource(matcher)
apis.request(1)
return Mono.from(apis)
.switchIfEmpty(Mono.error<U>(Exception("No API available")))
.switchIfEmpty(Mono.error(Exception("No API available")))
}
override fun setLag(lag: Long) {