Fix remove upstream, dispose lower sub (#373)

This commit is contained in:
KirillPamPam
2023-12-27 12:24:19 +04:00
committed by GitHub
parent eb774d73e8
commit e19b5ec150
2 changed files with 16 additions and 9 deletions

View File

@@ -2,11 +2,13 @@ package io.emeraldpay.dshackle.config.reload
import io.emeraldpay.dshackle.Chain import io.emeraldpay.dshackle.Chain
import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.Global
import io.emeraldpay.dshackle.Global.Companion.chainById
import io.emeraldpay.dshackle.config.UpstreamsConfig import io.emeraldpay.dshackle.config.UpstreamsConfig
import io.emeraldpay.dshackle.startup.ConfiguredUpstreams import io.emeraldpay.dshackle.startup.ConfiguredUpstreams
import io.emeraldpay.dshackle.startup.UpstreamChangeEvent import io.emeraldpay.dshackle.startup.UpstreamChangeEvent
import io.emeraldpay.dshackle.upstream.CurrentMultistreamHolder import io.emeraldpay.dshackle.upstream.CurrentMultistreamHolder
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import java.util.stream.Collectors
@Component @Component
open class ReloadConfigUpstreamService( open class ReloadConfigUpstreamService(
@@ -20,19 +22,21 @@ open class ReloadConfigUpstreamService(
upstreamsToAdd: Set<Pair<String, Chain>>, upstreamsToAdd: Set<Pair<String, Chain>>,
newUpstreamsConfig: UpstreamsConfig, newUpstreamsConfig: UpstreamsConfig,
) { ) {
val newUpstreamsCount = newUpstreamsConfig.upstreams.stream()
.collect(
Collectors.groupingBy(
{ chainById(it.chain) },
Collectors.counting(),
),
)
val usedChains = removeUpstreams(chainsToReload, upstreamsToRemove) val usedChains = removeUpstreams(chainsToReload, upstreamsToRemove)
addUpstreams(newUpstreamsConfig, chainsToReload, upstreamsToAdd.map { it.first }.toSet()) addUpstreams(newUpstreamsConfig, chainsToReload, upstreamsToAdd.map { it.first }.toSet())
usedChains.forEach { chain -> usedChains.forEach { chain ->
val upstreamsToRemovePerChain = upstreamsToRemove.filter { it.second == chain } if (newUpstreamsCount[chain] == null) {
val upstreamsToAddPerChain = upstreamsToAdd.filter { it.second == chain } multistreamHolder.getUpstream(chain).stop()
if (upstreamsToAddPerChain.isEmpty() && upstreamsToRemovePerChain.isNotEmpty()) {
multistreamHolder.getUpstream(chain)
.run {
this.stop()
}
} }
} }
} }

View File

@@ -48,6 +48,7 @@ open class GenericUpstream(
private val validator: UpstreamValidator? = validatorBuilder(chain, this, getOptions(), chainConfig) private val validator: UpstreamValidator? = validatorBuilder(chain, this, getOptions(), chainConfig)
private var validatorSubscription: Disposable? = null private var validatorSubscription: Disposable? = null
private var validationSettingsSubscription: Disposable? = null private var validationSettingsSubscription: Disposable? = null
private var lowerBlockDetectorSubscription: Disposable? = null
private val hasLiveSubscriptionHead: AtomicBoolean = AtomicBoolean(false) private val hasLiveSubscriptionHead: AtomicBoolean = AtomicBoolean(false)
protected val connector: GenericConnector = connectorFactory.create(this, chain) protected val connector: GenericConnector = connectorFactory.create(this, chain)
@@ -180,6 +181,8 @@ open class GenericUpstream(
validatorSubscription = null validatorSubscription = null
livenessSubscription?.dispose() livenessSubscription?.dispose()
livenessSubscription = null livenessSubscription = null
lowerBlockDetectorSubscription?.dispose()
lowerBlockDetectorSubscription = null
disposeValidationSettingsSubscription() disposeValidationSettingsSubscription()
connector.stop() connector.stop()
} }
@@ -197,7 +200,7 @@ open class GenericUpstream(
} }
private fun detectLowerBlock() { private fun detectLowerBlock() {
lowerBoundBlockDetector.lowerBlock() lowerBlockDetectorSubscription = lowerBoundBlockDetector.lowerBlock()
.subscribe { .subscribe {
stateEventStream.emitNext( stateEventStream.emitNext(
UpstreamChangeEvent(chain, this, UpstreamChangeEvent.ChangeType.UPDATED), UpstreamChangeEvent(chain, this, UpstreamChangeEvent.ChangeType.UPDATED),