@cryptotaxi247 / kubo / commits / d2e0d73bd

flush pinning improvements

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jan 11, 2016 at 05:37 UTC d2e0d73bd52de0f6a6925b4e195a7f85d4098fe1
4 files changed +48 -10
core/core.go
+5 -2
@@ -483,8 +483,11 @@ func (n *IpfsNode) loadFilesRoot() error {
483 return err
484 }
485
486 - err = n.Pinning.Pin(n.Context(), nnd, true)
487 - if err != nil {
486 + if err := n.Pinning.Pin(n.Context(), nnd, true); err != nil {
487 + return err
488 + }
489 +
490 + if err := n.Pinning.Flush(); err != nil {
491 return err
492 }
493
mfs/ops.go
+21 -1
@@ -197,6 +197,9 @@ func DirLookup(d *Directory, pth string) (FSNode, error) {
197
198 func FlushPath(r *Root, pth string) error {
199 parts := path.SplitList(strings.Trim(pth, "/"))
200 + if len(parts) == 1 && parts[0] == "" {
201 + parts = nil
202 + }
203
204 d, ok := r.GetValue().(*Directory)
205 if !ok {
@@ -214,12 +217,24 @@ func FlushPath(r *Root, pth string) error {
217 }
218
219 r.repub.Update(k)
220 + r.repub.WaitPub()
221 +
222 return nil
223 }
224
225 func flushPathRec(d *Directory, parts []string) (*dag.Node, error) {
226 if len(parts) == 0 {
222 - return d.GetNode()
227 + nd, err := d.GetNode()
228 + if err != nil {
229 + return nil, err
230 + }
231 +
232 + _, err = d.dserv.Add(nd)
233 + if err != nil {
234 + return nil, err
235 + }
236 +
237 + return nd, nil
238 }
239
240 d.Lock()
@@ -243,6 +258,11 @@ func flushPathRec(d *Directory, parts []string) (*dag.Node, error) {
258 return nil, err
259 }
260
261 + _, err = d.dserv.Add(newnode)
262 + if err != nil {
263 + return nil, err
264 + }
265 +
266 d.node = newnode
267 return newnode, nil
268 case *File:
mfs/system.go
+15 -4
@@ -165,7 +165,7 @@ type Republisher struct {
165 TimeoutShort time.Duration
166 Publish chan struct{}
167 pubfunc PubFunc
168 - pubnowch chan struct{}
168 + pubnowch chan chan struct{}
169
170 ctx context.Context
171 cancel func()
@@ -190,7 +190,7 @@ func NewRepublisher(ctx context.Context, pf PubFunc, tshort, tlong time.Duration
190 TimeoutLong: tlong,
191 Publish: make(chan struct{}, 1),
192 pubfunc: pf,
193 - pubnowch: make(chan struct{}),
193 + pubnowch: make(chan chan struct{}),
194 ctx: ctx,
195 cancel: cancel,
196 }
@@ -204,11 +204,17 @@ func (p *Republisher) setVal(k key.Key) {
204
205 func (p *Republisher) pubNow() {
206 select {
207 - case p.pubnowch <- struct{}{}:
207 + case p.pubnowch <- nil:
208 default:
209 }
210 }
211
212 +func (p *Republisher) WaitPub() {
213 + wait := make(chan struct{})
214 + p.pubnowch <- wait
215 + <-wait
216 +}
217 +
218 func (p *Republisher) Close() error {
219 err := p.publish(p.ctx)
220 p.cancel()
@@ -235,6 +241,8 @@ func (np *Republisher) Run() {
241 longer := time.After(np.TimeoutLong)
242
243 wait:
244 + var pubnowresp chan struct{}
245 +
246 select {
247 case <-np.ctx.Done():
248 return
@@ -243,10 +251,13 @@ func (np *Republisher) Run() {
251 goto wait
252 case <-quick:
253 case <-longer:
246 - case <-np.pubnowch:
254 + case pubnowresp = <-np.pubnowch:
255 }
256
257 err := np.publish(np.ctx)
258 + if pubnowresp != nil {
259 + pubnowresp <- struct{}{}
260 + }
261 if err != nil {
262 log.Error("republishRoot error: %s", err)
263 }
test/sharness/t0250-files-api.sh
+7 -3
@@ -336,9 +336,13 @@ test_files_api() {
336 test_cmp root_hash_exp root_hash
337 '
338
339 - test_expect_success "root hash is pinned" '
340 - ipfs pin ls
341 - return 1
339 + test_expect_success "flush root succeeds" '
340 + ipfs files flush /
341 + '
342 +
343 + test_expect_success "root hash is pinned after flush" '
344 + ipfs pin ls > pins &&
345 + grep $EXP_ROOT_HASH pins || (cat pins && exit 1)
346 '
347
348 # test mv