forked from application-research/estuary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshuttle.go
412 lines (348 loc) · 10.4 KB
/
shuttle.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"context"
"fmt"
"net/url"
"time"
"go.opentelemetry.io/otel/trace"
"golang.org/x/xerrors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
drpc "github.com/application-research/estuary/drpc"
"github.com/application-research/estuary/util"
"github.com/application-research/filclient"
"github.com/filecoin-project/go-address"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/libp2p/go-libp2p-core/peer"
)
type Shuttle struct {
gorm.Model
Handle string `gorm:"unique"`
Token string
LastConnection time.Time
Host string
PeerID string
Private bool
Open bool
Priority int
}
type ShuttleConnection struct {
handle string
cmds chan *drpc.Command
closing chan struct{}
hostname string
addrInfo peer.AddrInfo
address address.Address
private bool
spaceLow bool
blockstoreSize uint64
blockstoreFree uint64
pinCount int64
pinQueueLength int64
}
func (dc *ShuttleConnection) sendMessage(ctx context.Context, cmd *drpc.Command) error {
select {
case dc.cmds <- cmd:
return nil
case <-dc.closing:
return ErrNoShuttleConnection
case <-ctx.Done():
return ctx.Err()
}
}
func (cm *ContentManager) registerShuttleConnection(handle string, hello *drpc.Hello) (chan *drpc.Command, func(), error) {
cm.shuttlesLk.Lock()
defer cm.shuttlesLk.Unlock()
_, ok := cm.shuttles[handle]
if ok {
log.Warn("registering shuttle but found existing connection")
return nil, nil, fmt.Errorf("shuttle already connected")
}
_, err := url.Parse(hello.Host)
if err != nil {
log.Errorf("shuttle had invalid hostname %q: %s", hello.Host, err)
hello.Host = ""
}
if err := cm.DB.Model(Shuttle{}).Where("handle = ?", handle).UpdateColumns(map[string]interface{}{
"host": hello.Host,
"peer_id": hello.AddrInfo.ID.String(),
"last_connection": time.Now(),
"private": hello.Private,
}).Error; err != nil {
return nil, nil, err
}
d := &ShuttleConnection{
handle: handle,
address: hello.Address,
addrInfo: hello.AddrInfo,
hostname: hello.Host,
cmds: make(chan *drpc.Command, 32),
closing: make(chan struct{}),
private: hello.Private,
}
cm.shuttles[handle] = d
return d.cmds, func() {
close(d.closing)
cm.shuttlesLk.Lock()
outd, ok := cm.shuttles[handle]
if ok {
if outd == d {
delete(cm.shuttles, handle)
}
}
cm.shuttlesLk.Unlock()
}, nil
}
var ErrNilParams = fmt.Errorf("shuttle message had nil params")
func (cm *ContentManager) processShuttleMessage(handle string, msg *drpc.Message) error {
ctx := context.TODO()
// if the message contains a trace continue it here.
if msg.HasTraceCarrier() {
if sc := msg.TraceCarrier.AsSpanContext(); sc.IsValid() {
ctx = trace.ContextWithRemoteSpanContext(ctx, sc)
}
}
ctx, span := cm.tracer.Start(ctx, "processShuttleMessage")
defer span.End()
log.Infof("handling shuttle message: %s", msg.Op)
switch msg.Op {
case drpc.OP_UpdatePinStatus:
ups := msg.Params.UpdatePinStatus
if ups == nil {
return ErrNilParams
}
cm.UpdatePinStatus(handle, ups.DBID, ups.Status)
return nil
case drpc.OP_PinComplete:
param := msg.Params.PinComplete
if param == nil {
return ErrNilParams
}
if err := cm.handlePinningComplete(ctx, handle, param); err != nil {
log.Errorw("handling pin complete message failed", "shuttle", handle, "err", err)
}
return nil
case drpc.OP_CommPComplete:
param := msg.Params.CommPComplete
if param == nil {
return ErrNilParams
}
if err := cm.handleRpcCommPComplete(ctx, handle, param); err != nil {
log.Errorf("handling commp complete message from shuttle %s: %s", handle, err)
}
return nil
case drpc.OP_TransferStarted:
param := msg.Params.TransferStarted
if param == nil {
return ErrNilParams
}
if err := cm.handleRpcTransferStarted(ctx, handle, param); err != nil {
log.Errorf("handling transfer started message from shuttle %s: %s", handle, err)
}
return nil
case drpc.OP_TransferStatus:
param := msg.Params.TransferStatus
if param == nil {
return ErrNilParams
}
if err := cm.handleRpcTransferStatus(ctx, handle, param); err != nil {
log.Errorf("handling transfer status message from shuttle %s: %s", handle, err)
}
return nil
case drpc.OP_ShuttleUpdate:
param := msg.Params.ShuttleUpdate
if param == nil {
return ErrNilParams
}
if err := cm.handleRpcShuttleUpdate(ctx, handle, param); err != nil {
log.Errorf("handling shuttle update message from shuttle %s: %s", handle, err)
}
return nil
case drpc.OP_GarbageCheck:
param := msg.Params.GarbageCheck
if param == nil {
return ErrNilParams
}
if err := cm.handleRpcGarbageCheck(ctx, handle, param); err != nil {
log.Errorf("handling garbage check message from shuttle %s: %s", handle, err)
}
return nil
case drpc.OP_SplitComplete:
param := msg.Params.SplitComplete
if param == nil {
return ErrNilParams
}
if err := cm.handleRpcSplitComplete(ctx, handle, param); err != nil {
log.Errorf("handling split complete message from shuttle %s: %s", handle, err)
}
return nil
default:
return fmt.Errorf("unrecognized message op: %q", msg.Op)
}
}
var ErrNoShuttleConnection = fmt.Errorf("no connection to requested shuttle")
func (cm *ContentManager) sendShuttleCommand(ctx context.Context, handle string, cmd *drpc.Command) error {
if handle == "" {
return fmt.Errorf("attempted to send command to empty shuttle handle")
}
cm.shuttlesLk.Lock()
d, ok := cm.shuttles[handle]
cm.shuttlesLk.Unlock()
if ok {
return d.sendMessage(ctx, cmd)
}
return ErrNoShuttleConnection
}
func (cm *ContentManager) shuttleIsOnline(handle string) bool {
cm.shuttlesLk.Lock()
d, ok := cm.shuttles[handle]
cm.shuttlesLk.Unlock()
if !ok {
return false
}
select {
case <-d.closing:
return false
default:
return true
}
}
func (cm *ContentManager) shuttleAddrInfo(handle string) *peer.AddrInfo {
cm.shuttlesLk.Lock()
defer cm.shuttlesLk.Unlock()
d, ok := cm.shuttles[handle]
if ok {
return &d.addrInfo
}
return nil
}
func (cm *ContentManager) shuttleHostName(handle string) string {
cm.shuttlesLk.Lock()
defer cm.shuttlesLk.Unlock()
d, ok := cm.shuttles[handle]
if ok {
return d.hostname
}
return ""
}
func (cm *ContentManager) shuttleStorageStats(handle string) *util.ShuttleStorageStats {
cm.shuttlesLk.Lock()
defer cm.shuttlesLk.Unlock()
d, ok := cm.shuttles[handle]
if !ok {
return nil
}
return &util.ShuttleStorageStats{
BlockstoreSize: d.blockstoreSize,
BlockstoreFree: d.blockstoreFree,
PinCount: d.pinCount,
PinQueueLength: d.pinQueueLength,
}
}
func (cm *ContentManager) handleRpcCommPComplete(ctx context.Context, handle string, resp *drpc.CommPComplete) error {
ctx, span := cm.tracer.Start(ctx, "handleRpcCommPComplete")
defer span.End()
opcr := PieceCommRecord{
Data: util.DbCID{resp.Data},
Piece: util.DbCID{resp.CommP},
Size: resp.Size,
CarSize: resp.CarSize,
}
if err := cm.DB.Clauses(clause.OnConflict{DoNothing: true}).Create(&opcr).Error; err != nil {
return err
}
return nil
}
func (cm *ContentManager) handleRpcTransferStarted(ctx context.Context, handle string, param *drpc.TransferStarted) error {
if err := cm.DB.Model(contentDeal{}).Where("id = ?", param.DealDBID).UpdateColumns(map[string]interface{}{
"dt_chan": param.Chanid,
"transfer_started": time.Now(),
"transfer_finished": time.Time{},
}).Error; err != nil {
return xerrors.Errorf("failed to update deal with channel ID: %w", err)
}
log.Infow("Started data transfer on shuttle", "chanid", param.Chanid, "shuttle", handle)
return nil
}
func (cm *ContentManager) handleRpcTransferStatus(ctx context.Context, handle string, param *drpc.TransferStatus) error {
log.Infof("handling transfer status rpc update: %d %v", param.DealDBID, param.State == nil)
var cd contentDeal
if param.DealDBID != 0 {
if err := cm.DB.First(&cd, "id = ?", param.DealDBID).Error; err != nil {
return err
}
} else if param.State != nil {
if err := cm.DB.First(&cd, "dt_chan = ?", param.State.TransferID).Error; err != nil {
return err
}
} else {
return fmt.Errorf("received transfer status update with no identifiers")
}
if param.Failed {
miner, err := cd.MinerAddr()
if err != nil {
return err
}
if oerr := cm.recordDealFailure(&DealFailureError{
Miner: miner,
Phase: "start-data-transfer-remote",
Message: fmt.Sprintf("failure from shuttle %s: %s", handle, param.Message),
Content: cd.Content,
}); oerr != nil {
return oerr
}
cm.updateTransferStatus(ctx, handle, cd.ID, &filclient.ChannelState{
Status: datatransfer.Failed,
Message: fmt.Sprintf("failure from shuttle %s: %s", handle, param.Message),
})
return nil
}
cm.updateTransferStatus(ctx, handle, cd.ID, param.State)
return nil
}
func (cm *ContentManager) handleRpcShuttleUpdate(ctx context.Context, handle string, param *drpc.ShuttleUpdate) error {
cm.shuttlesLk.Lock()
defer cm.shuttlesLk.Unlock()
d, ok := cm.shuttles[handle]
if !ok {
return fmt.Errorf("shuttle connection not found while handling update for %q", handle)
}
d.spaceLow = (param.BlockstoreFree < (param.BlockstoreSize / 10))
d.blockstoreFree = param.BlockstoreFree
d.blockstoreSize = param.BlockstoreSize
d.pinCount = param.NumPins
d.pinQueueLength = int64(param.PinQueueSize)
return nil
}
func (cm *ContentManager) handleRpcGarbageCheck(ctx context.Context, handle string, param *drpc.GarbageCheck) error {
var tounpin []uint
for _, c := range param.Contents {
var cont Content
if err := cm.DB.First(&cont, "id = ?", c).Error; err != nil {
if xerrors.Is(err, gorm.ErrRecordNotFound) {
tounpin = append(tounpin, c)
} else {
return err
}
}
if cont.Location != handle || cont.Offloaded {
tounpin = append(tounpin, c)
}
}
return cm.sendUnpinCmd(ctx, handle, tounpin)
}
func (cm *ContentManager) handleRpcSplitComplete(ctx context.Context, handle string, param *drpc.SplitComplete) error {
if param.ID == 0 {
return fmt.Errorf("split complete send with ID = 0")
}
// TODO: do some sanity checks that the sub pieces were all made successfully...
if err := cm.DB.Model(Content{}).Where("id = ?", param.ID).UpdateColumns(map[string]interface{}{
"dag_split": true,
}).Error; err != nil {
return fmt.Errorf("failed to update content for split complete: %w", err)
}
if err := cm.DB.Delete(&ObjRef{}, "content = ?", param.ID).Error; err != nil {
return fmt.Errorf("failed to delete object references for newly split object: %w", err)
}
return nil
}