-
Notifications
You must be signed in to change notification settings - Fork 6
/
dag_session.go
218 lines (182 loc) · 4.36 KB
/
dag_session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package recovery
import (
"context"
"sync"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-verifcid"
)
// getter implements NodeGetter capable for restoring missing DAG nodes using redundant nodes.
// It is important for restoration to traverse the whole DAG using the only one getter, cause it is the only way to
// remember reverse links to prnts, unless DAGService interface is changed.
type dagSession struct {
ctx context.Context
r Recoverer
ex exchange.Interface
bs blockstore.Blockstore
prnts map[cid.Cid]Node
pl sync.Mutex
f exchange.Fetcher
fo sync.Once
}
func NewDagSession(ctx context.Context, r Recoverer, ex exchange.Interface, bs blockstore.Blockstore) format.NodeGetter {
return &dagSession{
ctx: ctx,
r: r,
ex: ex,
bs: bs,
prnts: make(map[cid.Cid]Node),
}
}
func (ds *dagSession) Get(ctx context.Context, id cid.Cid) (format.Node, error) {
err := verifcid.ValidateCid(id)
if err != nil {
return nil, err
}
// 1. Try to get from the Blockstore.
b, err := ds.bs.Get(id)
switch err {
default:
return nil, err
case nil:
return ds.decode(b)
case blockstore.ErrNotFound:
}
// 2. Try to recover.
nd, err := ds.recover(ctx, id)
if err != format.ErrNotFound {
return nd, err
}
// 3. Try to get from the network.
b, err = ds.fetcher().GetBlock(ctx, id)
switch err {
default:
return nil, err
case nil:
return ds.decode(b)
case blockstore.ErrNotFound:
}
// 4. Fail :(
return nil, format.ErrNotFound
}
func (ds *dagSession) GetMany(ctx context.Context, in []cid.Cid) <-chan *format.NodeOption {
out := make(chan *format.NodeOption, len(in))
ids := make([]cid.Cid, len(in))
copy(ids, in)
go func() {
defer close(out)
i := 0
for _, id := range ids {
err := verifcid.ValidateCid(id)
if err != nil {
continue
}
b, err := ds.bs.Get(id)
if err == nil {
nd, err := ds.decode(b)
select {
case out <- &format.NodeOption{Node: nd, Err: err}:
continue
case <-ctx.Done():
return
}
}
// TODO: Recovery might be long blocking, so run it async
nd, err := ds.recover(ctx, id)
if err == nil {
select {
case out <- &format.NodeOption{Node: nd}:
continue
case <-ctx.Done():
return
}
}
ids[i] = id
i++
}
ids = ids[:i]
if len(ids) == 0 {
return
}
bs, err := ds.fetcher().GetBlocks(ctx, ids)
if err != nil {
return
}
for b := range bs {
nd, err := ds.decode(b)
select {
case out <- &format.NodeOption{Node: nd, Err: err}:
continue
case <-ctx.Done():
return
}
}
}()
return out
}
func (ds *dagSession) recover(ctx context.Context, id cid.Cid) (format.Node, error) {
prnt := ds.getParentFor(id)
if prnt == nil {
return nil, format.ErrNotFound
}
nds, err := ds.r.Recover(ctx, prnt, id)
if err != nil {
log.Warnf("Recovery attempt failed(%s): %s", id, err)
return nil, format.ErrNotFound
}
select {
case no := <-nds:
if no.Err != nil {
log.Warnf("Recovery attempt failed(%s): %s", id, err)
return nil, format.ErrNotFound
}
log.Infof("Successful recovery(%s)", id)
return ds.decode(no.Node)
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (ds *dagSession) fetcher() exchange.Fetcher {
ds.fo.Do(func() {
ex, ok := ds.ex.(exchange.SessionExchange)
if !ok {
ds.f = ds.ex
return
}
ds.f = ex.NewSession(ds.ctx)
})
return ds.f
}
// decode transforms block into the Node and caches it, if it's a recovery one.
func (ds *dagSession) decode(b blocks.Block) (format.Node, error) {
nd, err := format.Decode(b)
if err != nil {
return nil, err
}
rn, ok := nd.(Node)
if !ok {
return nd, nil
}
ds.pl.Lock()
ds.prnts[rn.Cid()] = rn.Copy().(Node) // it is better to make a copy here, since node can be altered by the caller.
ds.pl.Unlock()
return rn.Proto(), nil
}
// getParentFor tries to find the parent gotten within the session for the given CID.
func (ds *dagSession) getParentFor(id cid.Cid) Node {
ds.pl.Lock()
defer ds.pl.Unlock()
for _, n := range ds.prnts {
for _, l := range n.Links() {
if l.Cid.Equals(id) {
// restoration reconstructs all linked nodes at fo hence parent can't be reused, so uncache it.
// delete(ds.prnts, p)
return n
}
}
}
return nil
}