Commit cb6fd5a8 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb: Prepare to rework/fix LiveCache control

We are going to rework LiveCache to support both retaining objects in
cache even though if all pointers to them went away (currently such
objects disappear from cache), and to tell the cache that such and such
object should not be cached at all, e.g. not to evict all other objects
on one-time large reads.

This will be done by LiveCacheControl giving cache policy classification
for an object. Prepare for that and change

	WantEvict(obj) -> bool

to

	PCacheClassify(obj) PCachePolicy

which can give more information to LiveCache about how to handle an
object.

This is only a preparatory patch as there is neither no new
functionality nor fixes here.
parent 1a2772e3
......@@ -122,15 +122,28 @@ type LiveCache struct {
// LiveCacheControl is the interface that allows applications to influence
// Connection's decisions with respect to Connection's live cache.
type LiveCacheControl interface {
// WantEvict is called when object is going to be evicted from live
// cache on deactivation and made ghost.
// PCacheClassify is called to classify an object and returns live
// cache policy that should be used for this object.
PCacheClassify(obj IPersistent) PCachePolicy
}
// PCachePolicy describes live caching policy for a persistent object.
//
// It is | combination of PCache* flags with 0 meaning "use default policy".
//
// See LiveCacheControl for how to apply a caching policy.
type PCachePolicy int
const (
// keep object state in cache.
//
// If !ok the object will remain live.
// This prevents object state to go away when !dirty object is no
// longer used.
//
// NOTE on invalidation invalidated objects are evicted from live cache
// Note: on invalidation, state of invalidated objects is discarded
// unconditionally.
WantEvict(obj IPersistent) (ok bool)
}
PCacheKeepState PCachePolicy = 1 << iota
)
// ----------------------------------------
......
......@@ -262,10 +262,13 @@ func (obj *Persistent) PDeactivate() {
// no constant load/unload on object access. XXX -> MRU cache?
// NOTE wcfs manages its objects explicitly and does not need this.
var cp PCachePolicy
if cc := obj.jar.cache.control; cc != nil {
if !cc.WantEvict(obj.instance) {
return
}
cp = cc.PCacheClassify(obj.instance)
}
if cp & PCacheKeepState != 0 {
return
}
// already ghost
......
......@@ -192,19 +192,14 @@ func TestPersistentBasic(t *testing.T) {
// ---- TestPersistentDB ----
// zcacheControl is simple live cache control that prevents specified objects
// to be evicted from live cache.
// zcacheControl is simple live cache control that is organized as `{} oid ->
// PCachePolicy` table.
type zcacheControl struct {
keep []Oid // objects that must not be evicted
pCachePolicy map[Oid]PCachePolicy
}
func (cc *zcacheControl) WantEvict(obj IPersistent) bool {
for _, oid := range cc.keep {
if obj.POid() == oid {
return false
}
}
return true
func (cc *zcacheControl) PCacheClassify(obj IPersistent) PCachePolicy {
return cc.pCachePolicy[obj.POid()] // default -> 0
}
// tDB represents testing database.
......@@ -496,9 +491,13 @@ func testPersistentDB(t0 *testing.T, rawcache bool) {
assert.Equal(db.δtail.Head(), at1)
// do not evict obj2 from live cache. obj1 is ok to be evicted.
zcc := &zcacheControl{map[Oid]PCachePolicy{
102: PCacheKeepState,
}}
zcache1 := t.conn.Cache()
zcache1.Lock()
zcache1.SetControl(&zcacheControl{[]Oid{102}})
zcache1.SetControl(zcc)
zcache1.Unlock()
// get objects
......@@ -620,7 +619,7 @@ func testPersistentDB(t0 *testing.T, rawcache bool) {
// pin obj2 into live cache, similarly to conn1
rzcache := t.conn.Cache()
rzcache.Lock()
rzcache.SetControl(&zcacheControl{[]Oid{102}})
rzcache.SetControl(zcc)
rzcache.Unlock()
// it should see latest data
......
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