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

Reintroduce caching into models #350

Merged
merged 7 commits into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion app/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import collins.util.security.AuthenticationAccessor
import collins.util.security.AuthenticationProvider
import collins.util.security.AuthenticationProviderConfig

import collins.models.cache.Cache
import collins.logging.LoggingHelper
import collins.util.config.Registry
import collins.solr.SolrHelper
Expand All @@ -39,14 +40,15 @@ object Global extends GlobalSettings with AuthenticationAccessor with CryptoAcce

override def onStart(app: Application) {
Registry.setupRegistry(app)
Cache.setupCache()
setAuthentication(AuthenticationProvider.get(AuthenticationProviderConfig.authType))
setCryptoKey(CryptoConfig.key)
LoggingHelper.setupLogging(app)
SolrHelper.setupSolr()
MetricsReporter.setupMetrics()
Callback.setupCallbacks()
}

override def onStop(app: Application) {
DB.shutdown()
Registry.terminateRegistry()
Expand Down
21 changes: 21 additions & 0 deletions app/collins/cache/CacheConfig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package collins.cache

import com.google.common.cache.CacheBuilderSpec

import collins.util.config.Configurable

object CacheConfig extends Configurable {

override val namespace = "cache"
override val referenceConfigFilename = "cache_reference.conf"

def enabled = getBoolean("enabled", true)
def specification = getString("specification", "maximumSize=10000,expireAfterWrite=10s,recordStats")

override protected def validateConfig() {
if (enabled) {
logger.debug("Validating domain model cache specification")
CacheBuilderSpec.parse(specification)
}
}
}
3 changes: 3 additions & 0 deletions app/collins/cache/GuavaCacheFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collins.cache
import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache
import com.google.common.cache.{ Cache => BasicCache }

/**
* Creates an instance of Guava cache with the provided specification and cache loader.
Expand All @@ -11,4 +12,6 @@ object GuavaCacheFactory {
def create[K <: AnyRef, V <: AnyRef](specification: String, loader: => CacheLoader[K, V]): LoadingCache[K, V] =
CacheBuilder.from(specification)
.build(loader)

def create[K <: AnyRef, V <: AnyRef](specification: String): BasicCache[K, V] = CacheBuilder.from(specification).build()
}
2 changes: 1 addition & 1 deletion app/collins/callbacks/CallbackMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ case class CallbackMatcher(conditional: MatchConditional, fn: PropertyChangeEven
val states = conditional.states.toSet
val value = Option(fn(pce))
value.filter(_.isInstanceOf[AssetView]).map(_.asInstanceOf[AssetView]).map { v =>
State.findById(v.state).map { state =>
State.findById(v.stateId).map { state =>
states.map(_.toUpperCase).contains(state.name.toUpperCase)
}.getOrElse(false)
}.getOrElse(true)
Expand Down
13 changes: 12 additions & 1 deletion app/collins/controllers/Admin.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package collins.controllers

import collins.models.cache.Cache

import collins.solr.Solr
import collins.util.Stats

Expand All @@ -15,7 +17,16 @@ object Admin extends SecureWebController {
Ok(html.admin.logs())
}(Permissions.AssetLogApi.GetAll)

def populateSolr = SecureAction {implicit req =>
def cache = SecureAction { implicit req =>
Ok(html.admin.cache(Cache.stats))
}(Permissions.Admin.Cache)

def clearCache = SecureAction { implicit req =>
Cache.clear()
Redirect(routes.Admin.cache)
}(Permissions.Admin.ClearCache)

def populateSolr = SecureAction { implicit req =>
Solr.populate()
Redirect(collins.app.routes.Resources.index).flashing("error" -> "Repopulating Solr index in the background. May take a few minutes to complete")
}(Permissions.Admin.ClearCache)
Expand Down
2 changes: 1 addition & 1 deletion app/collins/controllers/IpmiApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait IpmiApi {
val g = IpAddress.toLong(gateway.get)
val n = IpAddress.toLong(netmask.get)
val p = IpmiInfo.encryptPassword(password.get)
IpmiInfo(asset.getId, username.get, p, g, a, n)
IpmiInfo(asset.id, username.get, p, g, a, n)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/collins/controllers/Permissions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object Permissions {
object Admin extends PermSpec("controllers.Admin") {
def Spec = spec(AdminSpec)
def Stats = spec("stats", Spec)
def Cache = spec("cache", Spec)
def ClearCache = spec("clearCache", Stats)
}

Expand Down
8 changes: 4 additions & 4 deletions app/collins/controllers/actions/asset/ProvisionUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ trait Provisions extends ProvisionUtil with AssetAction { self: SecureAction =>
val slId = SoftLayer.softLayerId(asset).get
if (attribs.nonEmpty) {
val lifeCycle = new AssetLifecycle(userOption(), tattler)
lifeCycle.updateAssetAttributes(Asset.findById(asset.getId).get, attribs)
lifeCycle.updateAssetAttributes(Asset.findById(asset.id).get, attribs)
}

BackgroundProcessor.send(ActivationProcessor(slId)(request)) { res =>
processProvisionAction(res) {
case true =>
val newAsset = Asset.findById(asset.getId).get
val newAsset = Asset.findById(asset.id).get
Asset.partialUpdate(newAsset, None, AStatus.New.map(_.id))
setAsset(newAsset)
tattle("Asset successfully activated", false)
Expand Down Expand Up @@ -271,9 +271,9 @@ trait Provisions extends ProvisionUtil with AssetAction { self: SecureAction =>
if (attribs.nonEmpty) {
val lifeCycle = new AssetLifecycle(userOption(), tattler)
lifeCycle.updateAssetAttributes(
Asset.findById(asset.getId).get, attribs
Asset.findById(asset.id).get, attribs
)
setAsset(Asset.findById(asset.getId))
setAsset(Asset.findById(asset.id))
}
BackgroundProcessor.send(ProvisionerRun(pRequest)) { res =>
processProvisionAction(res) { result =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ case class UpdateStatusAction(
protected def checkStateConflict(
asset: Asset, statusOpt: Option[AssetStatus], stateOpt: Option[State]
): Tuple2[Option[AssetStatus],Option[State]] = {
val status = statusOpt.getOrElse(asset.getStatus())
val state = stateOpt.getOrElse(State.findById(asset.state).getOrElse(State.empty))
val status = statusOpt.getOrElse(asset.status)
val state = stateOpt.getOrElse(asset.state.getOrElse(State.empty))
if (state.status == State.ANY_STATUS || state.status == status.id) {
(None, None)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ case class FindAssetsByPoolAction(
override def validate(): Validation =
Right(ActionDataHolder(convertPoolName(pool)))

override def execute(rd: RequestDataHolder) = Future {
override def execute(rd: RequestDataHolder) = Future {
rd match {
case ActionDataHolder(cleanPool) =>
IpAddresses.findInPool(cleanPool) match {
Expand All @@ -35,7 +35,7 @@ case class FindAssetsByPoolAction(
RequestDataHolder.error404("No such pool or no assets in pool")
)
case list =>
val jsList = list.map(e => Asset.findById(e.asset_id).get.toJsValue).toList
val jsList = list.map(e => Asset.findById(e.assetId).get.toJsValue).toList
ResponseData(Status.Ok, JsObject(Seq("ASSETS" -> JsArray(jsList))))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ case class UpdateAction(
}.getOrElse {
if (address.isDefined && gateway.isDefined && netmask.isDefined) {
val p = convertPoolName(pool.getOrElse(IpAddressConfig.DefaultPoolName))
IpAddresses(asset.getId, gateway.get, address.get, netmask.get, p)
IpAddresses(asset.id, gateway.get, address.get, netmask.get, p)
} else {
throw new Exception("If creating a new IP the address, gateway and netmask must be specified")
}
Expand Down
2 changes: 1 addition & 1 deletion app/collins/controllers/actions/logs/SolrFindAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ case class SolrFindAction(
}

protected def getLogs(adh: ActionDataHolder): Page[AssetLog] = {
(new AssetLogSearchQuery(adh.query, adh.params)).getPage.fold(
(new AssetLogSearchQuery(adh.query, adh.params)).getPage(AssetLog.findByIds(_)).fold(
err => throw new Exception(err),
res => res
)
Expand Down
3 changes: 2 additions & 1 deletion app/collins/graphs/MetricsCacheLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import play.api.Logger

import com.google.common.cache.CacheLoader

import collins.models.Asset
import collins.models.shared.PageParams
import collins.models.shared.SortDirection
import collins.solr.AssetDocType
Expand Down Expand Up @@ -43,7 +44,7 @@ case class MetricsCacheLoader() extends CacheLoader[MetricsQuery, Set[String]] {
},
expr => {
val cq = AssetSearchQuery(expr, PageParams(0, 1, SortDirection.SortAsc, "TAG"))
cq.getPage().fold(
cq.getPage(Asset.findByTags(_)).fold(
err => {
logger.warn("Error executing CQL query: %s".format(err))
false
Expand Down
Loading