plugin/cache: Fix prefetching issues (#1363)

* Improve plugin/cache metrics

* Add coredns_cache_prefetch_total metric to track number of prefetches.
* Remove unnecessary Cache.get() call which would incorrectly increment
  cache counters.
* Initialize all counters and gauges at zero.

* Allow prefetching of a single request per ttl

The original implementation didn't allow prefetching queries which are
only requested once during the duration of a TTL. The minimum amount of
queries which had to be seen was therefore capped at 2.

This change also implements a real prefetch test. The existing test was
a noop and always passed regardless of any prefetch implementation.

* Fix prefetching for items with a short TTL

The default prefetch threshold (percentage) is 10% of the lifetime of a
cache item. With the previous implementation, this disabled prefetching
for all items with a TTL < 10s (the resulting percentage would be 0, at
which point a cached item is already discarded).

This change uses a time based threshold calculation and ensures that
a prefetch is triggered at a TTL of 1 at the latest.

* Fix wrong duration reporting of cached responses

The logging and metrics plugins (among others) included the duration of
a cache prefetch in the request latency of client request. This change
fixes this wrong reporting and executes the prefetch request in a
goroutine in the background.
This commit is contained in:
Tobias Schmidt 2018-01-17 08:35:22 +01:00 committed by Miek Gieben
parent fe0767987e
commit dd9fc8962c
8 changed files with 212 additions and 74 deletions

13
plugin/cache/setup.go vendored
View file

@ -41,21 +41,25 @@ func setup(c *caddy.Controller) error {
x.MustRegister(cacheCapacity)
x.MustRegister(cacheHits)
x.MustRegister(cacheMisses)
x.MustRegister(cachePrefetches)
}
})
return nil
})
// Export the capacity for the metrics. This only happens once, because this is a re-load change only.
// Initialize all counters and gauges.
cacheSize.WithLabelValues(Success)
cacheSize.WithLabelValues(Denial)
cacheCapacity.WithLabelValues(Success).Set(float64(ca.pcap))
cacheCapacity.WithLabelValues(Denial).Set(float64(ca.ncap))
cacheHits.WithLabelValues(Success)
cacheHits.WithLabelValues(Denial)
return nil
}
func cacheParse(c *caddy.Controller) (*Cache, error) {
ca := &Cache{pcap: defaultCap, ncap: defaultCap, pttl: maxTTL, nttl: maxNTTL, prefetch: 0, duration: 1 * time.Minute}
ca := New()
for c.Next() {
// cache [ttl] [zones..]
@ -140,8 +144,6 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
}
ca.prefetch = amount
ca.duration = 1 * time.Minute
ca.percentage = 10
if len(args) > 1 {
dur, err := time.ParseDuration(args[1])
if err != nil {
@ -174,7 +176,6 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
for i := range origins {
origins[i] = plugin.Host(origins[i]).Normalize()
}
ca.Zones = origins
ca.pcache = cache.New(ca.pcap)