Short nodeId (#541)

This commit is contained in:
KirillPamPam
2024-08-06 13:46:17 +04:00
committed by GitHub
parent 642e3851f8
commit 0f143c905e
20 changed files with 108 additions and 54 deletions

View File

@@ -252,8 +252,8 @@ class UpstreamsConfigReader(
return false
}
return upstream.nodeId?.let {
if (it !in 1..255) {
log.warn("Invalid node-id: $it. Must be in range [1, 255].")
if (it !in 1..65535) {
log.warn("Invalid node-id: $it. Must be in range [1, 65535].")
false
} else if (!knownNodeIds.add(it)) {
log.warn("Duplicated node-id: $it. Must be in unique.")

View File

@@ -587,13 +587,23 @@ open class NativeCall(
val bytes = result.value
if (bytes.last() == quoteCode && result.resolvedUpstreamData.isNotEmpty()) {
val suffix = result.resolvedUpstreamData[0].nodeId
.toUByte()
.toString(16).padStart(2, padChar = '0').toByteArray()
bytes[bytes.lastIndex] = suffix.first()
return bytes + suffix.last() + quoteCode
.toUShort()
.toString(16).padStart(4, padChar = '0').toByteArray()
return resultArray(bytes, suffix)
}
return bytes
}
private fun resultArray(bytes: ByteArray, suffix: ByteArray): ByteArray {
val newBytes = ByteArray(bytes.size + suffix.size)
newBytes[newBytes.size - 1] = quoteCode
var index = bytes.size - 1
System.arraycopy(bytes, 0, newBytes, 0, bytes.size - 1)
for (byteVal in suffix) newBytes[index++] = byteVal
return newBytes
}
}
interface RequestDecorator {
@@ -608,7 +618,7 @@ open class NativeCall(
override fun processRequest(request: CallParams): CallParams {
if (request is ListParams) {
val filterId = request.list.first().toString()
val sanitized = filterId.substring(0, filterId.lastIndex - 1)
val sanitized = filterId.substring(0, filterId.lastIndex - 3)
return ListParams(listOf(sanitized))
}
return request

View File

@@ -24,7 +24,7 @@ open class GenericUpstreamCreator(
private val connectorFactoryCreatorResolver: ConnectorFactoryCreatorResolver,
private val versionRules: Supplier<CompatibleVersionsRules?>,
) : UpstreamCreator(chainsConfig, indexConfig, callTargets) {
private val hashes: MutableMap<Byte, Boolean> = HashMap()
private val hashes = HashSet<Short>()
override fun createUpstream(
upstreamsConfig: UpstreamsConfig.Upstream<*>,

View File

@@ -22,26 +22,28 @@ abstract class UpstreamCreator(
protected val log: Logger = LoggerFactory.getLogger(this::class.java)
companion object {
fun getHash(nodeId: Int?, obj: Any, hashes: MutableMap<Byte, Boolean>): Byte =
nodeId?.toByte() ?: (obj.hashCode() % 255).let {
if (it == 0) 1 else it
}.let { nonZeroHash ->
listOf<Function<Int, Int>>(
Function { i -> i },
Function { i -> (-i) },
Function { i -> 127 - abs(i) },
Function { i -> abs(i) - 128 },
).map {
it.apply(nonZeroHash).toByte()
}.firstOrNull {
hashes[it] != true
}?.let {
hashes[it] = true
it
} ?: (Byte.MIN_VALUE..Byte.MAX_VALUE).first {
it != 0 && hashes[it.toByte()] != true
}.toByte()
}
fun getHash(nodeId: Int?, obj: Any, hashes: MutableSet<Short>): Short {
val hash = nodeId?.toShort()
?: run {
(obj.hashCode() % 65535)
.let { if (it == 0) 1 else it }
.let { nonZeroHash ->
listOf<Function<Int, Int>>(
Function { i -> i },
Function { i -> (-i) },
Function { i -> 32767 - abs(i) },
Function { i -> abs(i) - 32768 },
)
.map { it.apply(nonZeroHash).toShort() }
.firstOrNull { !hashes.contains(it) }
}
?: (Short.MIN_VALUE..Short.MAX_VALUE).first {
it != 0 && !hashes.contains(it.toShort())
}.toShort()
}
return hash.also { hashes.add(it) }
}
}
fun createUpstream(

View File

@@ -33,7 +33,7 @@ import java.util.concurrent.atomic.AtomicReference
abstract class DefaultUpstream(
private val id: String,
private val hash: Byte,
private val hash: Short,
defaultLag: Long?,
defaultAvail: UpstreamAvailability,
private val options: ChainOptions.Options,
@@ -46,7 +46,7 @@ abstract class DefaultUpstream(
constructor(
id: String,
hash: Byte,
hash: Short,
options: ChainOptions.Options,
role: UpstreamsConfig.UpstreamRole,
targets: CallMethods?,
@@ -153,7 +153,7 @@ abstract class DefaultUpstream(
sendUpstreamStateEvent(UpstreamChangeEvent.ChangeType.UPDATED)
}
override fun nodeId(): Byte = hash
override fun nodeId(): Short = hash
open fun getQuorumByLabel(): QuorumForLabels {
return node?.let { QuorumForLabels(it.copy(labels = fromMap(it.labels))) }

View File

@@ -82,7 +82,7 @@ sealed class MatchesResponse {
) : MatchesResponse()
data class SameNodeResponse(
val upstreamHash: Byte,
val upstreamHash: Short,
) : MatchesResponse()
object AvailabilityResponse : MatchesResponse()

View File

@@ -389,7 +389,7 @@ abstract class Multistream(
return false
}
override fun nodeId(): Byte = 0
override fun nodeId(): Short = 0
override fun updateLowerBound(lowerBound: Long, type: LowerBoundType) {
// NOOP

View File

@@ -612,7 +612,7 @@ class Selector {
}
}
class SameNodeMatcher(private val upstreamHash: Byte) : Matcher() {
class SameNodeMatcher(private val upstreamHash: Short) : Matcher() {
override fun matchesWithCause(up: Upstream): MatchesResponse =
if (up.nodeId() == upstreamHash) {

View File

@@ -60,10 +60,10 @@ interface Upstream : Lifecycle {
fun <T : Upstream> cast(selfType: Class<T>): T
fun nodeId(): Byte
fun nodeId(): Short
data class UpstreamSettingsData(
val nodeId: Byte,
val nodeId: Short,
val id: String,
val nodeVersion: String,
) {

View File

@@ -33,7 +33,7 @@ abstract class BitcoinUpstream(
node: QuorumForLabels.QuorumItem,
val esploraClient: EsploraClient? = null,
chainConfig: ChainsConfig.ChainConfig,
) : DefaultUpstream(id, 0.toByte(), options, role, callMethods, node, chainConfig, chain) {
) : DefaultUpstream(id, 0.toShort(), options, role, callMethods, node, chainConfig, chain) {
constructor(
id: String,

View File

@@ -97,11 +97,11 @@ class EthereumCallSelector(
}
val filterId = list[0].toString()
if (filterId.length < 4) {
return Mono.just(Selector.SameNodeMatcher(0.toByte()))
return Mono.just(Selector.SameNodeMatcher(0.toShort()))
}
val hashHex = filterId.substring(filterId.length - 2)
val hashHex = filterId.substring(filterId.length - 4)
val nodeId = hashHex.toInt(16)
return Mono.just(Selector.SameNodeMatcher(nodeId.toByte()))
return Mono.just(Selector.SameNodeMatcher(nodeId.toShort()))
}
private fun blockTagSelector(params: String, pos: Int, paramName: String?, head: Head): Mono<Selector.Matcher> {

View File

@@ -41,7 +41,7 @@ import java.util.function.Supplier
open class GenericUpstream(
id: String,
chain: Chain,
hash: Byte,
hash: Short,
options: ChainOptions.Options,
role: UpstreamsConfig.UpstreamRole,
targets: CallMethods?,
@@ -58,7 +58,7 @@ open class GenericUpstream(
constructor(
config: UpstreamsConfig.Upstream<*>,
chain: Chain,
hash: Byte,
hash: Short,
options: ChainOptions.Options,
node: QuorumForLabels.QuorumItem?,
chainConfig: ChainsConfig.ChainConfig,

View File

@@ -56,7 +56,7 @@ import java.util.function.Function
open class GenericGrpcUpstream(
parentId: String,
hash: Byte,
hash: Short,
role: UpstreamsConfig.UpstreamRole,
chain: Chain,
private val remote: ReactorBlockchainGrpc.ReactorBlockchainStub,

View File

@@ -32,7 +32,7 @@ class GrpcUpstreamCreator(
@Value("\${spring.application.max-metadata-size}")
private var maxMetadataSize: Int = Defaults.maxMetadataSize
private val hashes: MutableMap<Byte, Boolean> = HashMap()
private val hashes = HashSet<Short>()
companion object {
val grpcUpstreamsScheduler: Scheduler = Schedulers.fromExecutorService(

View File

@@ -69,7 +69,7 @@ import kotlin.concurrent.withLock
class GrpcUpstreams(
private val id: String,
private val hash: Byte,
private val hash: Short,
private val role: UpstreamsConfig.UpstreamRole,
private val host: String,
private val port: Int,