提交 0bfcf7ea 作者: Łukasz Magiera

namecache: remove pin for now

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 4f56358a
...@@ -55,7 +55,7 @@ Follows an IPNS name by periodically resolving in the backround. ...@@ -55,7 +55,7 @@ Follows an IPNS name by periodically resolving in the backround.
return cmdkit.Errorf(cmdkit.ErrClient, "IPNS Namecache is not available") return cmdkit.Errorf(cmdkit.ErrClient, "IPNS Namecache is not available")
} }
pin, _ := req.Options["pin"].(bool) prefetch, _ := req.Options["prefetch"].(bool)
refrS, _ := req.Options["refresh-interval"].(string) refrS, _ := req.Options["refresh-interval"].(string)
refr := nc.DefaultFollowInterval refr := nc.DefaultFollowInterval
...@@ -67,7 +67,7 @@ Follows an IPNS name by periodically resolving in the backround. ...@@ -67,7 +67,7 @@ Follows an IPNS name by periodically resolving in the backround.
} }
for _, name := range req.Arguments { for _, name := range req.Arguments {
err = n.Namecache.Follow(name, pin, refr) err = n.Namecache.Follow(name, prefetch, refr)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"time" "time"
namesys "github.com/ipfs/go-ipfs/namesys" namesys "github.com/ipfs/go-ipfs/namesys"
pin "github.com/ipfs/go-ipfs/pin"
uio "gx/ipfs/QmQ1JnYpnzkaurjW1yxkQxC2w3K1PorNE1nv1vaP5Le7sq/go-unixfs/io" uio "gx/ipfs/QmQ1JnYpnzkaurjW1yxkQxC2w3K1PorNE1nv1vaP5Le7sq/go-unixfs/io"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
...@@ -29,8 +28,8 @@ var log = logging.Logger("namecache") ...@@ -29,8 +28,8 @@ var log = logging.Logger("namecache")
// NameCache represents a following cache of names // NameCache represents a following cache of names
type NameCache interface { type NameCache interface {
// Follow starts following name, pinning it if dopin is true // Follow starts following name
Follow(name string, dopin bool, followInterval time.Duration) error Follow(name string, prefetch bool, followInterval time.Duration) error
// Unofollow cancels a follow // Unofollow cancels a follow
Unfollow(name string) error Unfollow(name string) error
// ListFollows returns a list of followed names // ListFollows returns a list of followed names
...@@ -39,7 +38,6 @@ type NameCache interface { ...@@ -39,7 +38,6 @@ type NameCache interface {
type nameCache struct { type nameCache struct {
nsys namesys.NameSystem nsys namesys.NameSystem
pinning pin.Pinner
dag ipld.NodeGetter dag ipld.NodeGetter
bstore bstore.GCBlockstore bstore bstore.GCBlockstore
...@@ -48,11 +46,10 @@ type nameCache struct { ...@@ -48,11 +46,10 @@ type nameCache struct {
mx sync.Mutex mx sync.Mutex
} }
func NewNameCache(ctx context.Context, nsys namesys.NameSystem, pinning pin.Pinner, dag ipld.NodeGetter, bstore bstore.GCBlockstore) NameCache { func NewNameCache(ctx context.Context, nsys namesys.NameSystem, dag ipld.NodeGetter, bstore bstore.GCBlockstore) NameCache {
return &nameCache{ return &nameCache{
ctx: ctx, ctx: ctx,
nsys: nsys, nsys: nsys,
pinning: pinning,
dag: dag, dag: dag,
bstore: bstore, bstore: bstore,
follows: make(map[string]func()), follows: make(map[string]func()),
...@@ -61,7 +58,7 @@ func NewNameCache(ctx context.Context, nsys namesys.NameSystem, pinning pin.Pinn ...@@ -61,7 +58,7 @@ func NewNameCache(ctx context.Context, nsys namesys.NameSystem, pinning pin.Pinn
// Follow spawns a goroutine that periodically resolves a name // Follow spawns a goroutine that periodically resolves a name
// and (when dopin is true) pins it in the background // and (when dopin is true) pins it in the background
func (nc *nameCache) Follow(name string, dopin bool, followInterval time.Duration) error { func (nc *nameCache) Follow(name string, prefetch bool, followInterval time.Duration) error {
nc.mx.Lock() nc.mx.Lock()
defer nc.mx.Unlock() defer nc.mx.Unlock()
...@@ -74,7 +71,7 @@ func (nc *nameCache) Follow(name string, dopin bool, followInterval time.Duratio ...@@ -74,7 +71,7 @@ func (nc *nameCache) Follow(name string, dopin bool, followInterval time.Duratio
} }
ctx, cancel := context.WithCancel(nc.ctx) ctx, cancel := context.WithCancel(nc.ctx)
go nc.followName(ctx, name, dopin, followInterval) go nc.followName(ctx, name, prefetch, followInterval)
nc.follows[name] = cancel nc.follows[name] = cancel
return nil return nil
...@@ -112,10 +109,9 @@ func (nc *nameCache) ListFollows() []string { ...@@ -112,10 +109,9 @@ func (nc *nameCache) ListFollows() []string {
return follows return follows
} }
func (nc *nameCache) followName(ctx context.Context, name string, dopin bool, followInterval time.Duration) { func (nc *nameCache) followName(ctx context.Context, name string, prefetch bool, followInterval time.Duration) {
// if cid != nil, we have created a new pin that is updated on changes and // if cid != nil, we have prefetched data under the node
// unpinned on cancel c, err := nc.resolveAndFetch(ctx, name, prefetch)
c, err := nc.resolveAndPin(ctx, name, dopin)
if err != nil { if err != nil {
log.Errorf("Error following %s: %s", name, err.Error()) log.Errorf("Error following %s: %s", name, err.Error())
} }
...@@ -129,7 +125,7 @@ func (nc *nameCache) followName(ctx context.Context, name string, dopin bool, fo ...@@ -129,7 +125,7 @@ func (nc *nameCache) followName(ctx context.Context, name string, dopin bool, fo
if c != cid.Undef { if c != cid.Undef {
c, err = nc.resolveAndUpdate(ctx, name, c) c, err = nc.resolveAndUpdate(ctx, name, c)
} else { } else {
c, err = nc.resolveAndPin(ctx, name, dopin) c, err = nc.resolveAndFetch(ctx, name, prefetch)
} }
if err != nil { if err != nil {
...@@ -137,24 +133,18 @@ func (nc *nameCache) followName(ctx context.Context, name string, dopin bool, fo ...@@ -137,24 +133,18 @@ func (nc *nameCache) followName(ctx context.Context, name string, dopin bool, fo
} }
case <-ctx.Done(): case <-ctx.Done():
if c != cid.Undef {
err = nc.unpin(c)
if err != nil {
log.Errorf("Error unpinning %s: %s", name, err.Error())
}
}
return return
} }
} }
} }
func (nc *nameCache) resolveAndPin(ctx context.Context, name string, dopin bool) (cid.Cid, error) { func (nc *nameCache) resolveAndFetch(ctx context.Context, name string, prefetch bool) (cid.Cid, error) {
ptr, err := nc.resolve(ctx, name) ptr, err := nc.resolve(ctx, name)
if err != nil { if err != nil {
return cid.Undef, err return cid.Undef, err
} }
if !dopin { if !prefetch {
return cid.Undef, nil return cid.Undef, nil
} }
...@@ -165,30 +155,15 @@ func (nc *nameCache) resolveAndPin(ctx context.Context, name string, dopin bool) ...@@ -165,30 +155,15 @@ func (nc *nameCache) resolveAndPin(ctx context.Context, name string, dopin bool)
defer nc.bstore.PinLock().Unlock() defer nc.bstore.PinLock().Unlock()
_, pinned, err := nc.pinning.IsPinned(c)
if pinned || err != nil {
return cid.Undef, err
}
n, err := nc.pathToNode(ctx, ptr) n, err := nc.pathToNode(ctx, ptr)
if err != nil { if err != nil {
return cid.Undef, err return cid.Undef, err
} }
log.Debugf("pinning %s", c.String())
err = nc.pinning.Pin(ctx, n, true)
if err != nil {
return cid.Undef, err
}
err = nc.pinning.Flush()
return c, err return c, err
} }
func (nc *nameCache) resolveAndUpdate(ctx context.Context, name string, oldcid cid.Cid) (cid.Cid, error) { func (nc *nameCache) resolveAndUpdate(ctx context.Context, name string, oldcid cid.Cid) (cid.Cid, error) {
ptr, err := nc.resolve(ctx, name) ptr, err := nc.resolve(ctx, name)
if err != nil { if err != nil {
return cid.Undef, err return cid.Undef, err
...@@ -203,31 +178,11 @@ func (nc *nameCache) resolveAndUpdate(ctx context.Context, name string, oldcid c ...@@ -203,31 +178,11 @@ func (nc *nameCache) resolveAndUpdate(ctx context.Context, name string, oldcid c
return oldcid, nil return oldcid, nil
} }
defer nc.bstore.PinLock().Unlock() // TODO: handle prefetching
log.Debugf("Updating pin %s -> %s", oldcid.String(), newcid.String())
err = nc.pinning.Update(ctx, oldcid, newcid, true)
if err != nil {
return oldcid, err
}
err = nc.pinning.Flush()
return newcid, err return newcid, err
} }
func (nc *nameCache) unpin(cid cid.Cid) error {
defer nc.bstore.PinLock().Unlock()
err := nc.pinning.Unpin(nc.ctx, cid, true)
if err != nil {
return err
}
return nc.pinning.Flush()
}
func (nc *nameCache) resolve(ctx context.Context, name string) (path.Path, error) { func (nc *nameCache) resolve(ctx context.Context, name string) (path.Path, error) {
log.Debugf("resolving %s", name) log.Debugf("resolving %s", name)
...@@ -241,6 +196,8 @@ func (nc *nameCache) resolve(ctx context.Context, name string) (path.Path, error ...@@ -241,6 +196,8 @@ func (nc *nameCache) resolve(ctx context.Context, name string) (path.Path, error
log.Debugf("resolved %s to %s", name, p) log.Debugf("resolved %s to %s", name, p)
// TODO: handle prefetching
return p, nil return p, nil
} }
......
...@@ -20,7 +20,7 @@ type persistent struct { ...@@ -20,7 +20,7 @@ type persistent struct {
} }
type follow struct { type follow struct {
Pin bool Prefetch bool
Deadline time.Time Deadline time.Time
} }
...@@ -37,7 +37,7 @@ func NewPersistentCache(base NameCache, d ds.Datastore) (NameCache, error) { ...@@ -37,7 +37,7 @@ func NewPersistentCache(base NameCache, d ds.Datastore) (NameCache, error) {
if err := json.Unmarshal(e.Value, &f); err != nil { if err := json.Unmarshal(e.Value, &f); err != nil {
return nil, err return nil, err
} }
if err := base.Follow(e.Key, f.Pin, time.Now().Sub(f.Deadline)); err != nil { if err := base.Follow(e.Key, f.Prefetch, time.Now().Sub(f.Deadline)); err != nil {
return nil, err return nil, err
} }
} }
...@@ -49,16 +49,16 @@ func NewPersistentCache(base NameCache, d ds.Datastore) (NameCache, error) { ...@@ -49,16 +49,16 @@ func NewPersistentCache(base NameCache, d ds.Datastore) (NameCache, error) {
}, nil }, nil
} }
func (p *persistent) Follow(name string, dopin bool, followInterval time.Duration) error { func (p *persistent) Follow(name string, prefetch bool, followInterval time.Duration) error {
b, err := json.Marshal(&follow{ b, err := json.Marshal(&follow{
Pin: dopin, Prefetch: prefetch,
Deadline: time.Now().Add(followInterval), Deadline: time.Now().Add(followInterval),
}) })
if err != nil { if err != nil {
return err return err
} }
if err := p.NameCache.Follow(name, dopin, followInterval); err != nil { if err := p.NameCache.Follow(name, prefetch, followInterval); err != nil {
return err return err
} }
return p.ds.Put(ds.NewKey(name), b) return p.ds.Put(ds.NewKey(name), b)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论