-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathexecutor.go
More file actions
257 lines (229 loc) · 7.7 KB
/
executor.go
File metadata and controls
257 lines (229 loc) · 7.7 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package executor
import (
"bytes"
"context"
"sync/atomic"
logging "github.com/ipfs/go-log/v2"
"github.com/ipfs/go-peertaskqueue/peertask"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
"github.com/ipld/go-ipld-prime/traversal"
"github.com/libp2p/go-libp2p/core/peer"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/donotsendfirstblocks"
"github.com/ipfs/go-graphsync/ipldutil"
gsmsg "github.com/ipfs/go-graphsync/message"
"github.com/ipfs/go-graphsync/requestmanager/hooks"
"github.com/ipfs/go-graphsync/requestmanager/types"
)
var log = logging.Logger("gs_request_executor")
// Manager is an interface the Executor uses to interact with the request manager
type Manager interface {
SendRequest(peer.ID, gsmsg.GraphSyncRequest)
GetRequestTask(peer.ID, *peertask.Task, chan RequestTask)
ReleaseRequestTask(peer.ID, *peertask.Task, error)
}
// BlockHooks run for each block loaded
type BlockHooks interface {
ProcessBlockHooks(p peer.ID, response graphsync.ResponseData, block graphsync.BlockData) hooks.UpdateResult
}
// ReconciledLoader is an interface that can be used to load blocks from a local store or a remote request
type ReconciledLoader interface {
SetRemoteOnline(online bool)
RetryLastLoad() types.AsyncLoadResult
BlockReadOpener(lctx linking.LinkContext, link datamodel.Link) types.AsyncLoadResult
}
// Executor handles actually executing graphsync requests and verifying them.
// It has control of requests when they are in the "running" state, while
// the manager is in charge when requests are queued or paused
type Executor struct {
manager Manager
blockHooks BlockHooks
}
// NewExecutor returns a new executor
func NewExecutor(
manager Manager,
blockHooks BlockHooks) *Executor {
return &Executor{
manager: manager,
blockHooks: blockHooks,
}
}
func (e *Executor) ExecuteTask(ctx context.Context, pid peer.ID, task *peertask.Task) bool {
requestTaskChan := make(chan RequestTask)
var requestTask RequestTask
e.manager.GetRequestTask(pid, task, requestTaskChan)
select {
case requestTask = <-requestTaskChan:
case <-ctx.Done():
return true
}
if requestTask.Empty {
log.Info("Empty task on peer request stack")
return false
}
_, span := otel.Tracer("graphsync").Start(trace.ContextWithSpan(ctx, requestTask.Span), "executeTask")
defer span.End()
log.Debugw("beginning request execution", "id", requestTask.Request.ID(), "peer", pid.String(), "root_cid", requestTask.Request.Root().String())
err := e.traverse(requestTask)
if err != nil {
span.RecordError(err)
if !ipldutil.IsContextCancelErr(err) {
e.manager.SendRequest(requestTask.P, gsmsg.NewCancelRequest(requestTask.Request.ID()))
requestTask.ReconciledLoader.SetRemoteOnline(false)
if !isPausedErr(err) {
span.SetStatus(codes.Error, err.Error())
select {
case <-requestTask.Ctx.Done():
case requestTask.InProgressErr <- err:
}
}
}
}
e.manager.ReleaseRequestTask(pid, task, err)
log.Debugw("finishing response execution", "id", requestTask.Request.ID(), "peer", pid.String(), "root_cid", requestTask.Request.Root().String())
return false
}
// RequestTask are parameters for a single request execution
type RequestTask struct {
Ctx context.Context
Span trace.Span
Request gsmsg.GraphSyncRequest
LastResponse *atomic.Value
DoNotSendFirstBlocks int64
PauseMessages <-chan struct{}
Traverser ipldutil.Traverser
P peer.ID
InProgressErr chan error
Empty bool
ReconciledLoader ReconciledLoader
}
func (e *Executor) traverse(rt RequestTask) error {
requestSent := false
// for initial request, start remote right away
for {
// check if traversal is complete
isComplete, err := rt.Traverser.IsComplete()
if isComplete {
return err
}
// get current link request
lnk, linkContext := rt.Traverser.CurrentRequest()
// attempt to load
log.Debugf("will load link=%s", lnk)
result := rt.ReconciledLoader.BlockReadOpener(linkContext, lnk)
// if we've only loaded locally so far and hit a missing block
// initiate remote request and retry the load operation from remote
if _, ok := result.Err.(graphsync.RemoteMissingBlockErr); ok && !requestSent {
requestSent = true
// tell the loader we're online now
rt.ReconciledLoader.SetRemoteOnline(true)
if err := e.startRemoteRequest(rt); err != nil {
return err
}
// retry the load
result = rt.ReconciledLoader.RetryLastLoad()
}
log.Debugf("successfully loaded link=%s, nBlocksRead=%d", lnk, rt.Traverser.NBlocksTraversed())
// advance the traversal based on results
err = e.advanceTraversal(rt, result)
if err != nil {
return err
}
// check for interrupts and run block hooks
err = e.processResult(rt, lnk, result)
if err != nil {
return err
}
}
}
func (e *Executor) processBlockHooks(p peer.ID, response graphsync.ResponseData, block graphsync.BlockData) error {
result := e.blockHooks.ProcessBlockHooks(p, response, block)
if len(result.Extensions) > 0 {
updateRequest := gsmsg.NewUpdateRequest(response.RequestID(), result.Extensions...)
e.manager.SendRequest(p, updateRequest)
}
return result.Err
}
func (e *Executor) onNewBlock(rt RequestTask, block graphsync.BlockData) error {
response := rt.LastResponse.Load().(gsmsg.GraphSyncResponse)
return e.processBlockHooks(rt.P, response, block)
}
func (e *Executor) advanceTraversal(rt RequestTask, result types.AsyncLoadResult) error {
if result.Err != nil {
// before processing result check for context cancel to avoid sending an additional error
select {
case <-rt.Ctx.Done():
return ipldutil.ContextCancelError{}
default:
}
select {
case <-rt.Ctx.Done():
return ipldutil.ContextCancelError{}
case rt.InProgressErr <- result.Err:
if _, ok := result.Err.(graphsync.RemoteMissingBlockErr); ok {
rt.Traverser.Error(traversal.SkipMe{})
} else {
rt.Traverser.Error(result.Err)
}
return nil
}
}
return rt.Traverser.Advance(bytes.NewBuffer(result.Data))
}
func (e *Executor) processResult(rt RequestTask, link datamodel.Link, result types.AsyncLoadResult) error {
var err error
if result.Err == nil {
err = e.onNewBlock(rt, &blockData{link, result.Local, uint64(len(result.Data)), int64(rt.Traverser.NBlocksTraversed())})
}
select {
case <-rt.PauseMessages:
if err == nil {
err = hooks.ErrPaused{}
}
default:
}
return err
}
func (e *Executor) startRemoteRequest(rt RequestTask) error {
request := rt.Request
doNotSendFirstBlocks := max(rt.DoNotSendFirstBlocks, int64(rt.Traverser.NBlocksTraversed()))
if doNotSendFirstBlocks > 0 {
doNotSendFirstBlocksData := donotsendfirstblocks.EncodeDoNotSendFirstBlocks(doNotSendFirstBlocks)
request = rt.Request.ReplaceExtensions([]graphsync.ExtensionData{{Name: graphsync.ExtensionsDoNotSendFirstBlocks, Data: doNotSendFirstBlocksData}})
}
log.Debugw("starting remote request", "id", rt.Request.ID(), "peer", rt.P.String(), "root_cid", rt.Request.Root().String())
e.manager.SendRequest(rt.P, request)
return nil
}
func isPausedErr(err error) bool {
_, isPaused := err.(hooks.ErrPaused)
return isPaused
}
type blockData struct {
link datamodel.Link
local bool
size uint64
index int64
}
// Link is the link/cid for the block
func (bd *blockData) Link() datamodel.Link {
return bd.link
}
// BlockSize specifies the size of the block
func (bd *blockData) BlockSize() uint64 {
return bd.size
}
// BlockSize specifies the amount of data actually transmitted over the network
func (bd *blockData) BlockSizeOnWire() uint64 {
if bd.local {
return 0
}
return bd.size
}
func (bd *blockData) Index() int64 {
return bd.index
}