-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(clouddriver): Short-lived memory clouddriver cache (#37)
- Loading branch information
1 parent
a650426
commit 04decca
Showing
2 changed files
with
56 additions
and
23 deletions.
There are no files selected for viewing
71 changes: 54 additions & 17 deletions
71
...uddriver/src/main/kotlin/com/netflix/spinnaker/keel/clouddriver/MemoryCloudDriverCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters