From bc0a34f01bb488c3e318bf89649211d5dec4746d Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Fri, 9 Dec 2016 20:58:35 +0900 Subject: [PATCH 1/3] cache: update LRU cache when putting same key but different value --- distributor/lru.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/distributor/lru.go b/distributor/lru.go index f99907d..2ec959e 100644 --- a/distributor/lru.go +++ b/distributor/lru.go @@ -32,8 +32,12 @@ func (lru *cache) Put(key string, value interface{}) { } lru.mut.Lock() defer lru.mut.Unlock() - if _, ok := lru.get(key); ok { - return + if v, ok := lru.get(key); ok { + if v == value { + return + } else { + lru.priority.Remove(lru.priority.Front()) + } } if len(lru.cache) == lru.maxSize { lru.removeOldest() From 5a65cfc81e6b19785aa781e26be4cfd49569ec46 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Sat, 10 Dec 2016 17:27:49 +0900 Subject: [PATCH 2/3] cache: update the value after finding it rather than front value --- distributor/lru.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/distributor/lru.go b/distributor/lru.go index 2ec959e..6c0d6a0 100644 --- a/distributor/lru.go +++ b/distributor/lru.go @@ -2,6 +2,7 @@ package distributor import ( "container/list" + "reflect" "sync" ) @@ -33,10 +34,17 @@ func (lru *cache) Put(key string, value interface{}) { lru.mut.Lock() defer lru.mut.Unlock() if v, ok := lru.get(key); ok { - if v == value { + if reflect.DeepEqual(v, value) { return } else { - lru.priority.Remove(lru.priority.Front()) + // Actually find() is not necessary, but it makes sure to remove + // correct element and the find loop would be finished soon, since + // the element is in the front now. + if old := lru.find(lru.priority.Front(), v); old != nil { + lru.priority.Remove(old) + } else { + clog.Errorf("read cache is corrupted. Please restart the process.") + } } } if len(lru.cache) == lru.maxSize { @@ -67,3 +75,13 @@ func (lru *cache) removeOldest() { last := lru.priority.Remove(lru.priority.Back()) delete(lru.cache, last.(kv).key) } + +func (lru *cache) find(e *list.Element, v interface{}) *list.Element { + if e == nil { + return nil + } else if e.Value.(kv).value == v { + return e + } else { + return lru.find(e.Next(), v) + } +} From f9c8925869c74f852370f28ff48d4b710dfd72e8 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Tue, 13 Dec 2016 21:07:30 +0900 Subject: [PATCH 3/3] cache: update to use Fatalf --- distributor/lru.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/distributor/lru.go b/distributor/lru.go index 6c0d6a0..9797b56 100644 --- a/distributor/lru.go +++ b/distributor/lru.go @@ -43,7 +43,7 @@ func (lru *cache) Put(key string, value interface{}) { if old := lru.find(lru.priority.Front(), v); old != nil { lru.priority.Remove(old) } else { - clog.Errorf("read cache is corrupted. Please restart the process.") + clog.Fatalf("read cache is corrupted. Please restart the process.") } } } @@ -79,9 +79,9 @@ func (lru *cache) removeOldest() { func (lru *cache) find(e *list.Element, v interface{}) *list.Element { if e == nil { return nil - } else if e.Value.(kv).value == v { + } + if reflect.DeepEqual(e.Value.(kv).value, v) { return e - } else { - return lru.find(e.Next(), v) } + return lru.find(e.Next(), v) }