Commit 44357788 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent bbe11527
...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
"sync" "sync"
"unsafe" "unsafe"
// "github.com/kylelemons/godebug/pretty"
"lab.nexedi.com/kirr/neo/go/zodb" "lab.nexedi.com/kirr/neo/go/zodb"
) )
...@@ -103,7 +104,12 @@ type revCacheEntry struct { ...@@ -103,7 +104,12 @@ type revCacheEntry struct {
// NewCache creates new cache backed up by loader // NewCache creates new cache backed up by loader
// XXX +sizeMax // XXX +sizeMax
func NewCache(loader storLoader) *Cache { func NewCache(loader storLoader) *Cache {
return &Cache{loader: loader, entryMap: make(map[zodb.Oid]*oidCacheEntry)} c := &Cache{
loader: loader,
entryMap: make(map[zodb.Oid]*oidCacheEntry),
}
c.lru.Init()
return c
} }
// newRevEntry creates new revCacheEntry with .before and inserts it into .rcev @i. // newRevEntry creates new revCacheEntry with .before and inserts it into .rcev @i.
...@@ -182,6 +188,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, serial zodb.Tid, err error) { ...@@ -182,6 +188,7 @@ func (c *Cache) Load(xid zodb.Xid) (data []byte, serial zodb.Tid, err error) {
// rce is already in cache - use it // rce is already in cache - use it
if !rceNew { if !rceNew {
//panic(0)
<-rce.ready <-rce.ready
c.gcMu.Lock() c.gcMu.Lock()
rce.inLRU.MoveBefore(&c.lru) rce.inLRU.MoveBefore(&c.lru)
...@@ -375,7 +382,14 @@ func (c *Cache) loadRCE(rce *revCacheEntry, oid zodb.Oid) { ...@@ -375,7 +382,14 @@ func (c *Cache) loadRCE(rce *revCacheEntry, oid zodb.Oid) {
// update lru & cache size // update lru & cache size
c.gcMu.Lock() c.gcMu.Lock()
//xv1 := map[string]interface{}{"lru": &c.lru, "rce": &rce.inLRU}
//fmt.Printf("aaa:\n%s\n", pretty.Sprint(xv1))
rce.inLRU.MoveBefore(&c.lru) rce.inLRU.MoveBefore(&c.lru)
//xv2 := map[string]interface{}{"lru": &c.lru, "rce": &rce.inLRU}
//fmt.Printf("\n--------\n%s\n\n\n", pretty.Sprint(xv2))
//panic(1)
c.size += δsize c.size += δsize
if c.size > c.sizeMax { if c.size > c.sizeMax {
// XXX -> run gc // XXX -> run gc
......
...@@ -108,6 +108,8 @@ func xideq(oid zodb.Oid, tid zodb.Tid) zodb.Xid { ...@@ -108,6 +108,8 @@ func xideq(oid zodb.Oid, tid zodb.Tid) zodb.Xid {
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
// XXX hack; place=ok? // XXX hack; place=ok?
pretty.CompareConfig.PrintStringers = true pretty.CompareConfig.PrintStringers = true
debug := pretty.DefaultConfig
debug.IncludeUnexported = true
// XXX <100 <90 <80 // XXX <100 <90 <80
// q<110 -> a) 110 <= cache.before b) otherwise // q<110 -> a) 110 <= cache.before b) otherwise
...@@ -172,19 +174,39 @@ func TestCache(t *testing.T) { ...@@ -172,19 +174,39 @@ func TestCache(t *testing.T) {
t.Helper() t.Helper()
oce := c.entryMap[oid] oce := c.entryMap[oid]
if !reflect.DeepEqual(oce.rcev, rcev) { if !reflect.DeepEqual(oce.rcev, rcev) {
t.Fatalf("oce(%v):\n%s", oid, pretty.Compare(rcev, oce.rcev)) t.Fatalf("oce(%v):\n%s\n", oid, pretty.Compare(rcev, oce.rcev))
}
}
checkMRU := func(mruvOk ...*revCacheEntry) {
t.Helper()
var mruv []*revCacheEntry
for hp, h := &c.lru, c.lru.prev; h != &c.lru; hp, h = h, h.prev {
xv := []interface{}{&c.lru, h.rceFromInLRU()}
debug.Print(xv) // &c.lru, h.rceFromInLRU())
if h.next != hp {
t.Fatalf("LRU list .next/.prev broken for\nh:\n%s\n\nhp:\n%s\n",
debug.Sprint(h), debug.Sprint(hp))
}
mruv = append(mruv, h.rceFromInLRU())
}
if !reflect.DeepEqual(mruv, mruvOk) {
t.Fatalf("MRU:\n%s\n", pretty.Compare(mruv, mruvOk))
} }
} }
// ---- verify cache behaviour for must be loaded/merged entries ---- // ---- verify cache behaviour for must be loaded/merged entries ----
// (this excercises mostly loadRCE/tryMerge) // (this excercises mostly loadRCE/tryMerge)
checkMRU()
// load <3 -> new rce entry // load <3 -> new rce entry
checkLoad(xidlt(1,3), nil, 0, &zodb.ErrXidMissing{xidlt(1,3)}) checkLoad(xidlt(1,3), nil, 0, &zodb.ErrXidMissing{xidlt(1,3)})
oce1 := c.entryMap[1] oce1 := c.entryMap[1]
ok1(len(oce1.rcev) == 1) ok1(len(oce1.rcev) == 1)
rce1_b3 := oce1.rcev[0] rce1_b3 := oce1.rcev[0]
checkRCE(rce1_b3, 3, 0, nil, &zodb.ErrXidMissing{xidlt(1,3)}) checkRCE(rce1_b3, 3, 0, nil, &zodb.ErrXidMissing{xidlt(1,3)})
checkMRU(rce1_b3)
// load <4 -> <3 merged with <4 // load <4 -> <3 merged with <4
checkLoad(xidlt(1,4), nil, 0, &zodb.ErrXidMissing{xidlt(1,4)}) checkLoad(xidlt(1,4), nil, 0, &zodb.ErrXidMissing{xidlt(1,4)})
...@@ -192,6 +214,7 @@ func TestCache(t *testing.T) { ...@@ -192,6 +214,7 @@ func TestCache(t *testing.T) {
rce1_b4 := oce1.rcev[0] rce1_b4 := oce1.rcev[0]
ok1(rce1_b4 != rce1_b3) // rce1_b3 was merged into rce1_b4 ok1(rce1_b4 != rce1_b3) // rce1_b3 was merged into rce1_b4
checkRCE(rce1_b4, 4, 0, nil, &zodb.ErrXidMissing{xidlt(1,4)}) checkRCE(rce1_b4, 4, 0, nil, &zodb.ErrXidMissing{xidlt(1,4)})
checkMRU(rce1_b4)
// load <2 -> <2 merged with <4 // load <2 -> <2 merged with <4
checkLoad(xidlt(1,2), nil, 0, &zodb.ErrXidMissing{xidlt(1,2)}) checkLoad(xidlt(1,2), nil, 0, &zodb.ErrXidMissing{xidlt(1,2)})
......
...@@ -49,7 +49,7 @@ func (a *listHead) MoveBefore(b *listHead) { ...@@ -49,7 +49,7 @@ func (a *listHead) MoveBefore(b *listHead) {
a.Delete() a.Delete()
a.next = b a.next = b
b.prev = a
a.prev = b.prev a.prev = b.prev
b.prev = a
a.prev.next = a a.prev.next = a
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment