Skip to content

Commit 04decca

Browse files
authored
chore(clouddriver): Short-lived memory clouddriver cache (#37)
1 parent a650426 commit 04decca

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,70 @@
11
package com.netflix.spinnaker.keel.clouddriver
22

3+
import com.google.common.cache.Cache
4+
import com.google.common.cache.CacheBuilder
35
import com.netflix.spinnaker.keel.clouddriver.model.Network
46
import com.netflix.spinnaker.keel.clouddriver.model.SecurityGroup
7+
import java.util.concurrent.TimeUnit
58

69
class MemoryCloudDriverCache(
710
private val cloudDriver: CloudDriverService
811
) : CloudDriverCache {
12+
13+
private val securityGroups = CacheBuilder.newBuilder()
14+
.maximumSize(1000)
15+
.expireAfterWrite(30, TimeUnit.SECONDS)
16+
.build<String, SecurityGroup>()
17+
18+
private val networks = CacheBuilder.newBuilder()
19+
.maximumSize(1000)
20+
.expireAfterWrite(30, TimeUnit.SECONDS)
21+
.build<String, Network>()
22+
23+
private val availabilityZones = CacheBuilder.newBuilder()
24+
.maximumSize(1000)
25+
.expireAfterWrite(30, TimeUnit.SECONDS)
26+
.build<String, Set<String>>()
27+
928
override fun securityGroupBy(account: String, id: String): SecurityGroup =
10-
cloudDriver
11-
.getSecurityGroups(account)
12-
.firstOrNull { it.id == id }
13-
?: throw ResourceNotFound("Security group with id $id not found in the $account account")
29+
securityGroups.getOrNotFound("$account:$id", "Security group with id $id not found in the $account account") {
30+
cloudDriver
31+
.getSecurityGroups(account)
32+
.firstOrNull { it.id == id }
33+
}
1434

1535
override fun networkBy(id: String): Network =
16-
cloudDriver
17-
.listNetworks()["aws"]
18-
?.firstOrNull { it.id == id }
19-
?: throw ResourceNotFound("VPC network with id $id not found")
36+
networks.getOrNotFound(id, "VPC network with id $id not found") {
37+
cloudDriver
38+
.listNetworks()["aws"]
39+
?.firstOrNull { it.id == id }
40+
}
2041

42+
// TODO rz - caches here aren't very efficient
2143
override fun networkBy(name: String, account: String, region: String): Network =
22-
cloudDriver
23-
.listNetworks()["aws"]
24-
?.firstOrNull { it.name == name && it.account == account && it.region == region }
25-
?: throw ResourceNotFound("VPC network named $name not found in $region")
44+
networks.getOrNotFound("$name:$account:$region", "VPC network named $name not found in $region") {
45+
cloudDriver
46+
.listNetworks()["aws"]
47+
?.firstOrNull { it.name == name && it.account == account && it.region == region }
48+
}
2649

2750
override fun availabilityZonesBy(account: String, vpcId: String, region: String): Set<String> =
28-
cloudDriver
29-
.listSubnets("aws")
30-
.filter { it.account == account && it.vpcId == vpcId && it.region == region }
31-
.map { it.availabilityZone }
32-
.toSet()
51+
availabilityZones.get("$account:$vpcId:$region") {
52+
cloudDriver
53+
.listSubnets("aws")
54+
.filter { it.account == account && it.vpcId == vpcId && it.region == region }
55+
.map { it.availabilityZone }
56+
.toSet()
57+
}
58+
59+
private fun <T> Cache<String, T>.getOrNotFound(key: String, notFoundMessage: String, loader: () -> T?): T {
60+
var v = getIfPresent(key)
61+
if (v == null) {
62+
v = loader.invoke()
63+
if (v == null) {
64+
throw ResourceNotFound(notFoundMessage)
65+
}
66+
put(key, loader.invoke())
67+
}
68+
return v
69+
}
3370
}

keel-scheduler/src/main/kotlin/com/netflix/spinnaker/keel/scheduler/Scheduler.kt

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ import org.springframework.stereotype.Component
2323
import java.time.Duration
2424
import javax.annotation.PostConstruct
2525

26-
interface SchedulerAgent {
27-
fun run()
28-
}
29-
3026
/**
3127
* Starts the convergence schedule, and ensures that it stays scheduled through failures.
3228
*/
3329
@Component
3430
class QueueBackedSchedulerAgent(
3531
private val queue: Queue,
3632
@Value("\${scheduler.retry.onStart.ms:30000}") private val ensureSchedulerFrequency: Long
37-
) : SchedulerAgent {
33+
) {
3834

3935
private val log = LoggerFactory.getLogger(javaClass)
4036

@@ -44,7 +40,7 @@ class QueueBackedSchedulerAgent(
4440
}
4541

4642
@Scheduled(fixedDelayString = "\${scheduler.retry.frequency.ms:30000}")
47-
override fun run() {
43+
fun run() {
4844
queue.ensure(ScheduleConvergence(), Duration.ofSeconds(30))
4945
}
5046
}

0 commit comments

Comments
 (0)