Move cache Keys to 64bit for a better dispersion and lower collision frequency (#2077)

* - change Key for cache to 64bits.

* - change Key for cache to 64bits.
This commit is contained in:
Francois Tur 2018-08-31 17:26:43 -04:00 committed by Yong Tang
parent d00e8c3918
commit 4c6c9d4b27
6 changed files with 43 additions and 41 deletions

30
plugin/cache/cache.go vendored
View file

@ -60,24 +60,24 @@ func New() *Cache {
// Return key under which we store the item, -1 will be returned if we don't store the
// message.
// Currently we do not cache Truncated, errors zone transfers or dynamic update messages.
func key(m *dns.Msg, t response.Type, do bool) int {
func key(m *dns.Msg, t response.Type, do bool) (bool, uint64) {
// We don't store truncated responses.
if m.Truncated {
return -1
return false, 0
}
// Nor errors or Meta or Update
if t == response.OtherError || t == response.Meta || t == response.Update {
return -1
return false, 0
}
return int(hash(m.Question[0].Name, m.Question[0].Qtype, do))
return true, hash(m.Question[0].Name, m.Question[0].Qtype, do)
}
var one = []byte("1")
var zero = []byte("0")
func hash(qname string, qtype uint16, do bool) uint32 {
h := fnv.New32()
func hash(qname string, qtype uint16, do bool) uint64 {
h := fnv.New64()
if do {
h.Write(one)
@ -97,7 +97,7 @@ func hash(qname string, qtype uint16, do bool) uint32 {
h.Write([]byte{c})
}
return h.Sum32()
return h.Sum64()
}
// ResponseWriter is a response writer that caches the reply message.
@ -152,7 +152,7 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
}
// key returns empty string for anything we don't want to cache.
key := key(res, mt, do)
hasKey, key := key(res, mt, do)
duration := w.pttl
if mt == response.NameError || mt == response.NoData {
@ -164,7 +164,7 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
duration = msgTTL
}
if key != -1 && duration > 0 {
if hasKey && duration > 0 {
if w.state.Match(res) {
w.set(res, key, mt, duration)
cacheSize.WithLabelValues(w.server, Success).Set(float64(w.pcache.Len()))
@ -195,19 +195,17 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
return w.ResponseWriter.WriteMsg(res)
}
func (w *ResponseWriter) set(m *dns.Msg, key int, mt response.Type, duration time.Duration) {
if key == -1 || duration == 0 {
return
}
func (w *ResponseWriter) set(m *dns.Msg, key uint64, mt response.Type, duration time.Duration) {
// duration is expected > 0
// and key is valid
switch mt {
case response.NoError, response.Delegation:
i := newItem(m, w.now(), duration)
w.pcache.Add(uint32(key), i)
w.pcache.Add(key, i)
case response.NameError, response.NoData:
i := newItem(m, w.now(), duration)
w.ncache.Add(uint32(key), i)
w.ncache.Add(key, i)
case response.OtherError:
// don't cache these