Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EthService performance fix for AtomicReference update loops #775

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import io.iohk.ethereum.jsonrpc.{FilterManager => FM}
import monix.eval.Task
import org.bouncycastle.util.encoders.Hex

import scala.collection.concurrent.{ Map => ConcurrentMap, TrieMap }
import scala.concurrent.duration.FiniteDuration
import scala.language.existentials
import scala.reflect.ClassTag
Expand Down Expand Up @@ -226,8 +227,7 @@ class EthService(

import EthService._

val hashRate: AtomicReference[Map[ByteString, (BigInt, Date)]] =
new AtomicReference[Map[ByteString, (BigInt, Date)]](Map())
val hashRate: ConcurrentMap[ByteString, (BigInt, Date)] = new TrieMap[ByteString, (BigInt, Date)]()
val lastActive = new AtomicReference[Option[Date]](None)

private[this] def consensus = ledger.consensus
Expand Down Expand Up @@ -481,11 +481,9 @@ class EthService(
def submitHashRate(req: SubmitHashRateRequest): ServiceResponse[SubmitHashRateResponse] =
ifEthash(req) { req =>
reportActive()
hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
val now = new Date
removeObsoleteHashrates(now, t + (req.id -> (req.hashRate, now)))
})

val now = new Date
removeObsoleteHashrates(now)
hashRate.put(req.id, (req.hashRate -> now))
SubmitHashRateResponse(true)
}

Expand Down Expand Up @@ -527,21 +525,16 @@ class EthService(

def getHashRate(req: GetHashRateRequest): ServiceResponse[GetHashRateResponse] =
ifEthash(req) { _ =>
val hashRates: Map[ByteString, (BigInt, Date)] = hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
removeObsoleteHashrates(new Date, t)
})

removeObsoleteHashrates(new Date)
//sum all reported hashRates
GetHashRateResponse(hashRates.mapValues { case (hr, _) => hr }.values.sum)
GetHashRateResponse(hashRate.map { case (_, (hr, _)) => hr }.sum)
}

// NOTE This is called from places that guarantee we are running Ethash consensus.
private def removeObsoleteHashrates(
now: Date,
rates: Map[ByteString, (BigInt, Date)]
): Map[ByteString, (BigInt, Date)] = {
rates.filter { case (_, (_, reported)) =>
Duration.between(reported.toInstant, now.toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
private def removeObsoleteHashrates(now: Date): Unit = {
hashRate.retain {
case (_, (_, reported)) =>
Duration.between(reported.toInstant, now.toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
}
}

Expand Down