diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt index 3ae6e239..d553d5e9 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt @@ -55,7 +55,7 @@ class NotLaggingQuorum(val maxLag: Long = 0) : CallQuorum { upstream: Upstream, providedUpstreamId: String? ): Boolean { - val lagging = upstream.getLag() > maxLag + val lagging = upstream.getLag()?.run { this > maxLag } ?: true if (!lagging) { result.set(response) sig = signature @@ -72,7 +72,7 @@ class NotLaggingQuorum(val maxLag: Long = 0) : CallQuorum { upstream: Upstream ) { this.rpcError = error.error - val lagging = upstream.getLag() > maxLag + val lagging = upstream.getLag()?.run { this > maxLag } ?: true if (!lagging && result.get() == null) { failed.set(true) } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/DefaultUpstream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/DefaultUpstream.kt index 5c4a24a7..4cec8fbc 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/DefaultUpstream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/DefaultUpstream.kt @@ -29,7 +29,7 @@ import java.util.concurrent.atomic.AtomicReference abstract class DefaultUpstream( private val id: String, private val hash: Byte, - defaultLag: Long, + defaultLag: Long?, defaultAvail: UpstreamAvailability, private val options: UpstreamsConfig.Options, private val role: UpstreamsConfig.UpstreamRole, @@ -47,7 +47,7 @@ abstract class DefaultUpstream( node: QuorumForLabels.QuorumItem?, chainConfig: ChainsConfig.ChainConfig ) : - this(id, hash, Long.MAX_VALUE, UpstreamAvailability.UNAVAILABLE, options, role, targets, node, chainConfig) + this(id, hash, null, UpstreamAvailability.UNAVAILABLE, options, role, targets, node, chainConfig) protected val log = LoggerFactory.getLogger(this::class.java) @@ -97,12 +97,15 @@ abstract class DefaultUpstream( } } - private fun statusByLag(lag: Long, proposed: UpstreamAvailability): UpstreamAvailability { + private fun statusByLag(lag: Long?, proposed: UpstreamAvailability): UpstreamAvailability { if (options.disableValidation) { // if we specifically told that this upstream should be _always valid_ then skip // the status calculation and trust the proposed value as is return proposed } + if (lag == null) { + return UpstreamAvailability.UNAVAILABLE + } return if (proposed == UpstreamAvailability.OK) { when { lag > chainConfig.syncingLagSize -> UpstreamAvailability.SYNCING @@ -140,7 +143,7 @@ abstract class DefaultUpstream( } } - override fun getLag(): Long { + override fun getLag(): Long? { return this.status.get().lag } @@ -169,7 +172,7 @@ abstract class DefaultUpstream( return id } - data class Status(val lag: Long, val avail: UpstreamAvailability, val status: UpstreamAvailability) + data class Status(val lag: Long?, val avail: UpstreamAvailability, val status: UpstreamAvailability) private data class UpstreamChangeState( val status: UpstreamAvailability, diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/Multistream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/Multistream.kt index 252ccdd1..17665770 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/Multistream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/Multistream.kt @@ -122,7 +122,9 @@ abstract class Multistream( removeUpstreamMeters(upstreamId) meters[upstreamId] = listOf( - Gauge.builder("$metrics.lag", upstream) { it.getLag().toDouble() } + Gauge.builder("$metrics.lag", upstream) { + it.getLag()?.toDouble() ?: Double.NaN + } .tag("chain", chain.chainCode) .tag("upstream", upstreamId) .register(Metrics.globalRegistry) @@ -374,11 +376,7 @@ abstract class Multistream( // by default, when no lag is available it uses Long.MAX_VALUE, and it doesn't make sense to print // status with such value. use NA (as Not Available) instead val value = it.getLag() - if (value == Long.MAX_VALUE) { - "NA" - } else { - value.toString() - } + value?.toString() ?: "NA" } val weak = upstreams.plus(removed.values) .filter { it.getStatus() != UpstreamAvailability.OK } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/Upstream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/Upstream.kt index f42275a4..ab66781b 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/Upstream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/Upstream.kt @@ -35,7 +35,7 @@ interface Upstream { fun getOptions(): UpstreamsConfig.Options fun getRole(): UpstreamsConfig.UpstreamRole fun setLag(lag: Long) - fun getLag(): Long + fun getLag(): Long? fun getLabels(): Collection fun getMethods(): CallMethods fun getId(): String