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

Use cache in HistoryStorage #366

Merged
merged 7 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ resolvers ++= Seq("Sonatype Releases" at "https://oss.sonatype.org/content/repos
val scorexVersion = "941a63a8-SNAPSHOT"

libraryDependencies ++= Seq(
"com.github.cb372" %% "scalacache-guava" % "0.+",
"org.scorexfoundation" %% "scrypto" % "2.1.2",
"org.scorexfoundation" %% "sigma-state" % "0.9.4",
"org.scala-lang.modules" %% "scala-async" % "0.9.7",
Expand Down
12 changes: 9 additions & 3 deletions lock.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ dependencyOverrides in ThisBuild ++= Seq(
"ch.qos.logback" % "logback-classic" % "1.3.0-alpha4",
"ch.qos.logback" % "logback-core" % "1.3.0-alpha4",
"com.chuusai" % "shapeless_2.12" % "2.3.2",
"com.google.code.findbugs" % "jsr305" % "3.0.2",
"com.google.guava" % "guava" % "21.0",
"com.github.cb372" % "scalacache-core_2.12" % "0.24.2",
"com.github.cb372" % "scalacache-guava_2.12" % "0.24.2",
"com.google.code.findbugs" % "jsr305" % "1.3.9",
"com.google.errorprone" % "error_prone_annotations" % "2.1.3",
"com.google.guava" % "guava" % "25.1-jre",
"com.google.j2objc" % "j2objc-annotations" % "1.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ain't we going to add a lot of dependencies to just use some trivial FP sugar? I would like to propose to use Guava directly.

"com.iheart" % "ficus_2.12" % "1.4.3",
"com.lihaoyi" % "fastparse-utils_2.12" % "1.0.0",
"com.lihaoyi" % "fastparse_2.12" % "1.0.0",
Expand Down Expand Up @@ -38,6 +42,8 @@ dependencyOverrides in ThisBuild ++= Seq(
"org.bitbucket.inkytonik.kiama" % "kiama_2.12" % "2.1.0",
"org.bitlet" % "weupnp" % "0.1.4",
"org.bouncycastle" % "bcprov-jdk15on" % "1.59",
"org.checkerframework" % "checker-qual" % "2.0.0",
"org.codehaus.mojo" % "animal-sniffer-annotations" % "1.14",
"org.reactivestreams" % "reactive-streams" % "1.0.2",
"org.rogach" % "scallop_2.12" % "2.1.1",
"org.rudogma" % "supertagged_2.12" % "1.4",
Expand All @@ -61,4 +67,4 @@ dependencyOverrides in ThisBuild ++= Seq(
"org.typelevel" % "macro-compat_2.12" % "1.1.1",
"org.whispersystems" % "curve25519-java" % "0.5.0"
)
// LIBRARY_DEPENDENCIES_HASH 20410da74eb095d244152f4fabc1f8990cdda9c9
// LIBRARY_DEPENDENCIES_HASH 8bb0ca25c7db6df31a36bb390028ea568eb8547b
5 changes: 5 additions & 0 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ ergo {
keepPoolSize = 1
}

cache {
# Number of recently used modifiers that will be kept in memory
historyStorageCacheSize = 100
}

# Chain-specific settings. Change only if you are going to launch a new chain!
chain {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ object ErgoHistory extends ScorexLogging {
historyFolder.mkdirs()
val indexStore = new LSMStore(historyFolder, keepVersions = 0)
val objectsStore = new FilesObjectsStore(historyFolder.getAbsolutePath)
val db = new HistoryStorage(indexStore, objectsStore)
val db = new HistoryStorage(indexStore, objectsStore, settings.cacheSettings)
val nodeSettings = settings.nodeSettings

val history: ErgoHistory = (nodeSettings.verifyTransactions, nodeSettings.PoPoWBootstrap) match {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package org.ergoplatform.nodeView.history.storage

import com.google.common.cache.CacheBuilder
import io.iohk.iodb.{ByteArrayWrapper, Store}
import org.ergoplatform.modifiers.ErgoPersistentModifier
import org.ergoplatform.modifiers.history.HistoryModifierSerializer
import org.ergoplatform.settings.{Algos, CacheSettings}
import scorex.core.ModifierId
import scorex.core.utils.{ScorexEncoding, ScorexLogging}

import scala.util.{Failure, Success}
import scala.util.Failure
import scalacache._
import scalacache.guava._
import scalacache.modes.try_.mode

class HistoryStorage(indexStore: Store, objectsStore: ObjectsStore) extends ScorexLogging with AutoCloseable
with ScorexEncoding {
class HistoryStorage(indexStore: Store, objectsStore: ObjectsStore, config: CacheSettings) extends ScorexLogging
with AutoCloseable with ScorexEncoding {

def modifierById(id: ModifierId): Option[ErgoPersistentModifier] = objectsStore.get(id)
.flatMap { bBytes =>
HistoryModifierSerializer.parseBytes(bBytes) match {
case Success(b) =>
Some(b)
case Failure(e) =>
private val underlyingGuavaCache = CacheBuilder.newBuilder()
.maximumSize(config.historyStorageCacheSize)
.build[String, Entry[ErgoPersistentModifier]]
implicit val modifiersCache: Cache[ErgoPersistentModifier] = GuavaCache(underlyingGuavaCache)

def modifierById(id: ModifierId): Option[ErgoPersistentModifier] = {
objectsStore.get(id).flatMap { bBytes =>
cachingF(Algos.encode(id))(ttl = None) {
HistoryModifierSerializer.parseBytes(bBytes).recoverWith { case e =>
log.warn(s"Failed to parse modifier ${encoder.encode(id)} from db (bytes are: ${bBytes.mkString("-")}): ", e)
None
}
Failure(e)
}
}.toOption
}
}


def getIndex(id: ByteArrayWrapper): Option[ByteArrayWrapper] = indexStore.get(id)

Expand Down
11 changes: 11 additions & 0 deletions src/main/scala/org/ergoplatform/settings/CacheSettings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.ergoplatform.settings

/**
* Configuration file for different caches
* @see src/main/resources/application.conf for parameters description
*/
case class CacheSettings(historyStorageCacheSize: Int)

object CacheSettings {
val default: CacheSettings = CacheSettings(100)
}
4 changes: 3 additions & 1 deletion src/main/scala/org/ergoplatform/settings/ErgoSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ case class ErgoSettings(directory: String,
chainSettings: ChainSettings,
testingSettings: TestingSettings,
nodeSettings: NodeConfigurationSettings,
cacheSettings: CacheSettings,
scorexSettings: ScorexSettings)

object ErgoSettings extends ScorexLogging
Expand All @@ -35,14 +36,15 @@ object ErgoSettings extends ScorexLogging
val nodeSettings = config.as[NodeConfigurationSettings](s"$configPath.node")
val chainSettings = config.as[ChainSettings](s"$configPath.chain")
val testingSettings = config.as[TestingSettings](s"$configPath.testing")
val cacheSettings = config.as[CacheSettings](s"$configPath.cache")
val scorexSettings = config.as[ScorexSettings](scorexConfigPath)

if (nodeSettings.stateType == Digest && nodeSettings.mining) {
log.error("Malformed configuration file was provided! Mining is not possible with digest state. Aborting!")
ErgoApp.forceStopApplication()
}

consistentSettings(ErgoSettings(directory, chainSettings, testingSettings, nodeSettings, scorexSettings))
consistentSettings(ErgoSettings(directory, chainSettings, testingSettings, nodeSettings, cacheSettings, scorexSettings))
}

private def readConfigFromPath(userConfigPath: Option[String]): Config = {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/org/ergoplatform/api/routes/Stubs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ trait Stubs extends ErgoGenerators with ErgoTestHelpers with ChainGenerator with

val dir = createTempDir
val fullHistorySettings: ErgoSettings = ErgoSettings(dir.getAbsolutePath, chainSettings, testingSettings,
nodeSettings, scorexSettings)
nodeSettings, CacheSettings.default, scorexSettings)

ErgoHistory.readOrGenerate(fullHistorySettings, timeProvider)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ trait HistorySpecification extends ErgoPropertyTest {

val dir = createTempDir
val fullHistorySettings: ErgoSettings = ErgoSettings(dir.getAbsolutePath, chainSettings, testingSettings,
nodeSettings, scorexSettings)
nodeSettings, CacheSettings.default, scorexSettings)

ErgoHistory.readOrGenerate(fullHistorySettings, timeProvider)
}
Expand Down