Skip to content

Commit

Permalink
chore(clouddriver): Short-lived memory clouddriver cache (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert committed Dec 19, 2017
1 parent a650426 commit 04decca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,70 @@
package com.netflix.spinnaker.keel.clouddriver

import com.google.common.cache.Cache
import com.google.common.cache.CacheBuilder
import com.netflix.spinnaker.keel.clouddriver.model.Network
import com.netflix.spinnaker.keel.clouddriver.model.SecurityGroup
import java.util.concurrent.TimeUnit

class MemoryCloudDriverCache(
private val cloudDriver: CloudDriverService
) : CloudDriverCache {

private val securityGroups = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(30, TimeUnit.SECONDS)
.build<String, SecurityGroup>()

private val networks = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(30, TimeUnit.SECONDS)
.build<String, Network>()

private val availabilityZones = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(30, TimeUnit.SECONDS)
.build<String, Set<String>>()

override fun securityGroupBy(account: String, id: String): SecurityGroup =
cloudDriver
.getSecurityGroups(account)
.firstOrNull { it.id == id }
?: throw ResourceNotFound("Security group with id $id not found in the $account account")
securityGroups.getOrNotFound("$account:$id", "Security group with id $id not found in the $account account") {
cloudDriver
.getSecurityGroups(account)
.firstOrNull { it.id == id }
}

override fun networkBy(id: String): Network =
cloudDriver
.listNetworks()["aws"]
?.firstOrNull { it.id == id }
?: throw ResourceNotFound("VPC network with id $id not found")
networks.getOrNotFound(id, "VPC network with id $id not found") {
cloudDriver
.listNetworks()["aws"]
?.firstOrNull { it.id == id }
}

// TODO rz - caches here aren't very efficient
override fun networkBy(name: String, account: String, region: String): Network =
cloudDriver
.listNetworks()["aws"]
?.firstOrNull { it.name == name && it.account == account && it.region == region }
?: throw ResourceNotFound("VPC network named $name not found in $region")
networks.getOrNotFound("$name:$account:$region", "VPC network named $name not found in $region") {
cloudDriver
.listNetworks()["aws"]
?.firstOrNull { it.name == name && it.account == account && it.region == region }
}

override fun availabilityZonesBy(account: String, vpcId: String, region: String): Set<String> =
cloudDriver
.listSubnets("aws")
.filter { it.account == account && it.vpcId == vpcId && it.region == region }
.map { it.availabilityZone }
.toSet()
availabilityZones.get("$account:$vpcId:$region") {
cloudDriver
.listSubnets("aws")
.filter { it.account == account && it.vpcId == vpcId && it.region == region }
.map { it.availabilityZone }
.toSet()
}

private fun <T> Cache<String, T>.getOrNotFound(key: String, notFoundMessage: String, loader: () -> T?): T {
var v = getIfPresent(key)
if (v == null) {
v = loader.invoke()
if (v == null) {
throw ResourceNotFound(notFoundMessage)
}
put(key, loader.invoke())
}
return v
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ import org.springframework.stereotype.Component
import java.time.Duration
import javax.annotation.PostConstruct

interface SchedulerAgent {
fun run()
}

/**
* Starts the convergence schedule, and ensures that it stays scheduled through failures.
*/
@Component
class QueueBackedSchedulerAgent(
private val queue: Queue,
@Value("\${scheduler.retry.onStart.ms:30000}") private val ensureSchedulerFrequency: Long
) : SchedulerAgent {
) {

private val log = LoggerFactory.getLogger(javaClass)

Expand All @@ -44,7 +40,7 @@ class QueueBackedSchedulerAgent(
}

@Scheduled(fixedDelayString = "\${scheduler.retry.frequency.ms:30000}")
override fun run() {
fun run() {
queue.ensure(ScheduleConvergence(), Duration.ofSeconds(30))
}
}

0 comments on commit 04decca

Please sign in to comment.