|
| 1 | +package cromwell.backend.standard.callcaching |
| 2 | + |
| 3 | +import akka.event.LoggingAdapter |
| 4 | +import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache} |
| 5 | +import com.typesafe.config.Config |
| 6 | +import cromwell.core.{CacheConfig, HasWorkflowIdAndSources} |
| 7 | +import mouse.boolean._ |
| 8 | +import net.ceedubs.ficus.Ficus._ |
| 9 | + |
| 10 | +import scala.concurrent.duration._ |
| 11 | +import scala.language.postfixOps |
| 12 | + |
| 13 | +object CallCachingBlacklistManager { |
| 14 | + object Defaults { |
| 15 | + object Groupings { |
| 16 | + val Concurrency = 10000 |
| 17 | + val Size = 1000L |
| 18 | + val Ttl = 2 hours |
| 19 | + } |
| 20 | + object Hits { |
| 21 | + val Concurrency = 10000 |
| 22 | + val Size = 20000L |
| 23 | + val Ttl = 1 hour |
| 24 | + } |
| 25 | + object Buckets { |
| 26 | + val Concurrency = 10000 |
| 27 | + val Size = 1000L |
| 28 | + val Ttl = 1 hour |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class CallCachingBlacklistManager(rootConfig: Config, logger: LoggingAdapter) { |
| 34 | + |
| 35 | + // Defined if "call-caching.blacklist-cache.enabled = true". |
| 36 | + private val blacklistCacheConfig: Option[Unit] = |
| 37 | + rootConfig.getOrElse("call-caching.blacklist-cache.enabled", false).option(()) |
| 38 | + |
| 39 | + // Defined if `blacklistCacheConfig` is defined and "call-caching.blacklist-cache.groupings.workflow-option" is defined. |
| 40 | + private val blacklistGroupingWorkflowOptionKey: Option[String] = for { |
| 41 | + _ <- blacklistCacheConfig // Only return a groupings cache if blacklisting is enabled. |
| 42 | + workflowOption <- rootConfig.as[Option[String]]("call-caching.blacklist-cache.groupings.workflow-option") |
| 43 | + } yield workflowOption |
| 44 | + |
| 45 | + // Defined if `blacklistGroupingWorkflowOptionKey` is defined. |
| 46 | + private val blacklistGroupingCacheConfig: Option[CacheConfig] = { |
| 47 | + import CallCachingBlacklistManager.Defaults.Groupings._ |
| 48 | + for { |
| 49 | + _ <- blacklistGroupingWorkflowOptionKey |
| 50 | + groupingsOption = rootConfig.as[Option[Config]] ("call-caching.blacklist-cache.groupings") |
| 51 | + conf = CacheConfig.config(groupingsOption, defaultConcurrency = Concurrency, defaultSize = Size, defaultTtl = Ttl) |
| 52 | + } yield conf |
| 53 | + } |
| 54 | + |
| 55 | + // Defined if `blacklistCacheConfig` is defined. |
| 56 | + private val blacklistBucketCacheConfig: Option[CacheConfig] = { |
| 57 | + import CallCachingBlacklistManager.Defaults.Buckets._ |
| 58 | + for { |
| 59 | + _ <- blacklistCacheConfig |
| 60 | + bucketsOption = rootConfig.as[Option[Config]]("call-caching.blacklist-cache.buckets") |
| 61 | + conf = CacheConfig.config(bucketsOption, defaultConcurrency = Concurrency, defaultSize = Size, defaultTtl = Ttl) |
| 62 | + } yield conf |
| 63 | + } |
| 64 | + |
| 65 | + // Defined if `blacklistCacheConfig` is defined. |
| 66 | + private val blacklistHitCacheConfig: Option[CacheConfig] = { |
| 67 | + import CallCachingBlacklistManager.Defaults.Hits._ |
| 68 | + for { |
| 69 | + _ <- blacklistCacheConfig |
| 70 | + hitsOption = rootConfig.as[Option[Config]]("call-caching.blacklist-cache.hits") |
| 71 | + conf = CacheConfig.config(hitsOption, defaultConcurrency = Concurrency, defaultSize = Size, defaultTtl = Ttl) |
| 72 | + } yield conf |
| 73 | + } |
| 74 | + |
| 75 | + // If configuration allows, build a cache of blacklist groupings to BlacklistCaches. |
| 76 | + private val blacklistGroupingsCache: Option[LoadingCache[String, BlacklistCache]] = { |
| 77 | + def buildBlacklistGroupingsCache(groupingConfig: CacheConfig, bucketConfig: CacheConfig, hitConfig: CacheConfig): LoadingCache[String, BlacklistCache] = { |
| 78 | + val emptyBlacklistCacheLoader = new CacheLoader[String, BlacklistCache]() { |
| 79 | + override def load(key: String): BlacklistCache = new GroupingBlacklistCache( |
| 80 | + bucketCacheConfig = bucketConfig, |
| 81 | + hitCacheConfig = hitConfig, |
| 82 | + group = key |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + CacheBuilder. |
| 87 | + newBuilder(). |
| 88 | + concurrencyLevel(groupingConfig.concurrency). |
| 89 | + maximumSize(groupingConfig.size). |
| 90 | + expireAfterWrite(groupingConfig.ttl.length, groupingConfig.ttl.unit). |
| 91 | + build[String, BlacklistCache](emptyBlacklistCacheLoader) |
| 92 | + } |
| 93 | + |
| 94 | + for { |
| 95 | + groupingsConfig <- blacklistGroupingCacheConfig |
| 96 | + bucketsConfig <- blacklistBucketCacheConfig |
| 97 | + hitsConfig <- blacklistHitCacheConfig |
| 98 | + } yield buildBlacklistGroupingsCache(groupingsConfig, bucketsConfig, hitsConfig) |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * If configured return a group blacklist cache, otherwise if configured return a root workflow cache, |
| 103 | + * otherwise return nothing. |
| 104 | + */ |
| 105 | + def blacklistCacheFor(workflow: HasWorkflowIdAndSources): Option[BlacklistCache] = { |
| 106 | + // If configuration is set up for blacklist groups and a blacklist group is specified in workflow options, |
| 107 | + // get the BlacklistCache for the group. |
| 108 | + val groupBlacklistCache: Option[BlacklistCache] = for { |
| 109 | + groupings <- blacklistGroupingsCache |
| 110 | + groupKey <- blacklistGroupingWorkflowOptionKey |
| 111 | + groupFromWorkflowOptions <- workflow.sources.workflowOptions.get(groupKey).toOption |
| 112 | + } yield groupings.get(groupFromWorkflowOptions) |
| 113 | + |
| 114 | + // Build a blacklist cache for a single, ungrouped root workflow. |
| 115 | + def rootWorkflowBlacklistCache: Option[BlacklistCache] = for { |
| 116 | + bucketConfig <- blacklistBucketCacheConfig |
| 117 | + hitConfig <- blacklistHitCacheConfig |
| 118 | + } yield new RootWorkflowBlacklistCache(bucketCacheConfig = bucketConfig, hitCacheConfig = hitConfig) |
| 119 | + |
| 120 | + // Return the group blacklist cache if available, otherwise a blacklist cache for the root workflow. |
| 121 | + val maybeCache = groupBlacklistCache orElse rootWorkflowBlacklistCache |
| 122 | + maybeCache collect { |
| 123 | + case group: GroupingBlacklistCache => |
| 124 | + logger.info("Workflow {} using group blacklist cache '{}' containing blacklist status for {} hits and {} buckets.", |
| 125 | + workflow.id, group.group, group.hitCache.size(), group.bucketCache.size()) |
| 126 | + case _: RootWorkflowBlacklistCache => |
| 127 | + logger.info("Workflow {} using root workflow blacklist cache.", workflow.id) |
| 128 | + } |
| 129 | + maybeCache |
| 130 | + } |
| 131 | +} |
0 commit comments