responsemanager.go 15.5 KB
Newer Older
1 2 3 4
package responsemanager

import (
	"context"
5
	"errors"
6
	"math"
7 8
	"time"

Hannah Howard's avatar
Hannah Howard committed
9 10 11 12
	logging "github.com/ipfs/go-log"
	"github.com/ipfs/go-peertaskqueue/peertask"
	ipld "github.com/ipld/go-ipld-prime"
	"github.com/libp2p/go-libp2p-core/peer"
13

14
	"github.com/ipfs/go-graphsync"
15
	"github.com/ipfs/go-graphsync/ipldutil"
16
	gsmsg "github.com/ipfs/go-graphsync/message"
17
	"github.com/ipfs/go-graphsync/notifications"
Hannah Howard's avatar
Hannah Howard committed
18
	"github.com/ipfs/go-graphsync/responsemanager/hooks"
19 20 21
	"github.com/ipfs/go-graphsync/responsemanager/peerresponsemanager"
)

22 23
var log = logging.Logger("graphsync")

24 25 26 27 28 29
const (
	maxInProcessRequests = 6
	thawSpeed            = time.Millisecond * 100
)

type inProgressResponseStatus struct {
30 31 32 33 34 35 36 37 38
	ctx        context.Context
	cancelFn   func()
	request    gsmsg.GraphSyncRequest
	loader     ipld.Loader
	traverser  ipldutil.Traverser
	signals    signals
	updates    []gsmsg.GraphSyncRequest
	isPaused   bool
	subscriber notifications.MappableSubscriber
39 40 41 42
}

type responseKey struct {
	p         peer.ID
43
	requestID graphsync.RequestID
44 45
}

46
type signals struct {
47
	pauseSignal  chan struct{}
Hannah Howard's avatar
Hannah Howard committed
48
	updateSignal chan struct{}
49
	errSignal    chan error
50 51 52
}

type responseTaskData struct {
53 54 55 56 57 58 59
	empty      bool
	subscriber notifications.MappableSubscriber
	ctx        context.Context
	request    gsmsg.GraphSyncRequest
	loader     ipld.Loader
	traverser  ipldutil.Traverser
	signals    signals
60 61
}

62 63 64
// QueryQueue is an interface that can receive new selector query tasks
// and prioritize them as needed, and pop them off later
type QueryQueue interface {
65 66 67 68
	PushTasks(to peer.ID, tasks ...peertask.Task)
	PopTasks(targetMinWork int) (peer.ID, []*peertask.Task, int)
	Remove(topic peertask.Topic, p peer.ID)
	TasksDone(to peer.ID, tasks ...*peertask.Task)
69 70 71
	ThawRound()
}

72 73
// RequestHooks is an interface for processing request hooks
type RequestHooks interface {
Hannah Howard's avatar
Hannah Howard committed
74
	ProcessRequestHooks(p peer.ID, request graphsync.RequestData) hooks.RequestResult
75 76 77 78
}

// BlockHooks is an interface for processing block hooks
type BlockHooks interface {
Hannah Howard's avatar
Hannah Howard committed
79 80 81 82 83 84
	ProcessBlockHooks(p peer.ID, request graphsync.RequestData, blockData graphsync.BlockData) hooks.BlockResult
}

// UpdateHooks is an interface for processing update hooks
type UpdateHooks interface {
	ProcessUpdateHooks(p peer.ID, request graphsync.RequestData, update graphsync.RequestData) hooks.UpdateResult
85 86
}

87 88 89
// CompletedListeners is an interface for notifying listeners that responses are complete
type CompletedListeners interface {
	NotifyCompletedListeners(p peer.ID, request graphsync.RequestData, status graphsync.ResponseStatusCode)
90 91
}

92 93 94 95 96
// CancelledListeners is an interface for notifying listeners that requestor cancelled
type CancelledListeners interface {
	NotifyCancelledListeners(p peer.ID, request graphsync.RequestData)
}

97 98 99 100 101 102 103 104 105 106
// BlockSentListeners is an interface for notifying listeners that of a block send occuring over the wire
type BlockSentListeners interface {
	NotifyBlockSentListeners(p peer.ID, request graphsync.RequestData, block graphsync.BlockData)
}

// NetworkErrorListeners is an interface for notifying listeners that an error occurred sending a data on the wire
type NetworkErrorListeners interface {
	NotifyNetworkErrorListeners(p peer.ID, request graphsync.RequestData, err error)
}

107 108 109 110 111 112 113 114 115 116 117 118
// PeerManager is an interface that returns sender interfaces for peer responses.
type PeerManager interface {
	SenderForPeer(p peer.ID) peerresponsemanager.PeerResponseSender
}

type responseManagerMessage interface {
	handle(rm *ResponseManager)
}

// ResponseManager handles incoming requests from the network, initiates selector
// traversals, and transmits responses
type ResponseManager struct {
119 120 121 122 123 124 125 126 127 128 129 130 131
	ctx                   context.Context
	cancelFn              context.CancelFunc
	peerManager           PeerManager
	queryQueue            QueryQueue
	updateHooks           UpdateHooks
	cancelledListeners    CancelledListeners
	completedListeners    CompletedListeners
	blockSentListeners    BlockSentListeners
	networkErrorListeners NetworkErrorListeners
	messages              chan responseManagerMessage
	workSignal            chan struct{}
	qe                    *queryExecutor
	inProgressResponses   map[responseKey]*inProgressResponseStatus
132 133 134 135 136
}

// New creates a new response manager from the given context, loader,
// bridge to IPLD interface, peerManager, and queryQueue.
func New(ctx context.Context,
137
	loader ipld.Loader,
138
	peerManager PeerManager,
139 140
	queryQueue QueryQueue,
	requestHooks RequestHooks,
Hannah Howard's avatar
Hannah Howard committed
141
	blockHooks BlockHooks,
142
	updateHooks UpdateHooks,
143
	completedListeners CompletedListeners,
144
	cancelledListeners CancelledListeners,
145 146
	blockSentListeners BlockSentListeners,
	networkErrorListeners NetworkErrorListeners,
147
) *ResponseManager {
148
	ctx, cancelFn := context.WithCancel(ctx)
149 150 151
	messages := make(chan responseManagerMessage, 16)
	workSignal := make(chan struct{}, 1)
	qe := &queryExecutor{
152 153 154 155 156 157 158 159 160 161 162
		requestHooks:       requestHooks,
		blockHooks:         blockHooks,
		updateHooks:        updateHooks,
		cancelledListeners: cancelledListeners,
		peerManager:        peerManager,
		loader:             loader,
		queryQueue:         queryQueue,
		messages:           messages,
		ctx:                ctx,
		workSignal:         workSignal,
		ticker:             time.NewTicker(thawSpeed),
163
	}
164
	return &ResponseManager{
165 166 167 168 169 170 171 172 173 174 175 176 177
		ctx:                   ctx,
		cancelFn:              cancelFn,
		peerManager:           peerManager,
		queryQueue:            queryQueue,
		updateHooks:           updateHooks,
		cancelledListeners:    cancelledListeners,
		completedListeners:    completedListeners,
		blockSentListeners:    blockSentListeners,
		networkErrorListeners: networkErrorListeners,
		messages:              messages,
		workSignal:            workSignal,
		qe:                    qe,
		inProgressResponses:   make(map[responseKey]*inProgressResponseStatus),
178 179 180 181 182 183 184 185 186
	}
}

type processRequestMessage struct {
	p        peer.ID
	requests []gsmsg.GraphSyncRequest
}

// ProcessRequests processes incoming requests for the given peer
187
func (rm *ResponseManager) ProcessRequests(ctx context.Context, p peer.ID, requests []gsmsg.GraphSyncRequest) {
188 189 190
	select {
	case rm.messages <- &processRequestMessage{p, requests}:
	case <-rm.ctx.Done():
191
	case <-ctx.Done():
192 193 194
	}
}

195
type unpauseRequestMessage struct {
196 197 198 199 200 201 202 203 204 205 206 207 208
	p          peer.ID
	requestID  graphsync.RequestID
	response   chan error
	extensions []graphsync.ExtensionData
}

// UnpauseResponse unpauses a response that was previously paused
func (rm *ResponseManager) UnpauseResponse(p peer.ID, requestID graphsync.RequestID, extensions ...graphsync.ExtensionData) error {
	response := make(chan error, 1)
	return rm.sendSyncMessage(&unpauseRequestMessage{p, requestID, response, extensions}, response)
}

type pauseRequestMessage struct {
209 210 211
	p         peer.ID
	requestID graphsync.RequestID
	response  chan error
212 213
}

214 215
// PauseResponse pauses an in progress response (may take 1 or more blocks to process)
func (rm *ResponseManager) PauseResponse(p peer.ID, requestID graphsync.RequestID) error {
216
	response := make(chan error, 1)
217 218 219
	return rm.sendSyncMessage(&pauseRequestMessage{p, requestID, response}, response)
}

220
type errorRequestMessage struct {
221 222
	p         peer.ID
	requestID graphsync.RequestID
223
	err       error
224 225 226 227 228 229
	response  chan error
}

// CancelResponse cancels an in progress response
func (rm *ResponseManager) CancelResponse(p peer.ID, requestID graphsync.RequestID) error {
	response := make(chan error, 1)
230
	return rm.sendSyncMessage(&errorRequestMessage{p, requestID, errCancelledByCommand, response}, response)
231 232 233
}

func (rm *ResponseManager) sendSyncMessage(message responseManagerMessage, response chan error) error {
234 235 236
	select {
	case <-rm.ctx.Done():
		return errors.New("Context Cancelled")
237
	case rm.messages <- message:
238 239 240 241 242 243
	}
	select {
	case <-rm.ctx.Done():
		return errors.New("Context Cancelled")
	case err := <-response:
		return err
244 245 246
	}
}

247
type synchronizeMessage struct {
248
	sync chan error
249 250 251 252
}

// this is a test utility method to force all messages to get processed
func (rm *ResponseManager) synchronize() {
253 254
	sync := make(chan error)
	_ = rm.sendSyncMessage(&synchronizeMessage{sync}, sync)
255 256 257 258
}

type responseDataRequest struct {
	key          responseKey
259
	taskDataChan chan responseTaskData
260 261
}

262
type finishTaskRequest struct {
263 264 265
	key    responseKey
	status graphsync.ResponseStatusCode
	err    error
266 267 268 269 270 271
}

type setResponseDataRequest struct {
	key       responseKey
	loader    ipld.Loader
	traverser ipldutil.Traverser
272 273
}

Hannah Howard's avatar
Hannah Howard committed
274 275 276 277 278
type responseUpdateRequest struct {
	key        responseKey
	updateChan chan []gsmsg.GraphSyncRequest
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
// Startup starts processing for the WantManager.
func (rm *ResponseManager) Startup() {
	go rm.run()
}

// Shutdown ends processing for the want manager.
func (rm *ResponseManager) Shutdown() {
	rm.cancelFn()
}

func (rm *ResponseManager) cleanupInProcessResponses() {
	for _, response := range rm.inProgressResponses {
		response.cancelFn()
	}
}

func (rm *ResponseManager) run() {
	defer rm.cleanupInProcessResponses()
	for i := 0; i < maxInProcessRequests; i++ {
298
		go rm.qe.processQueriesWorker()
299 300 301 302 303 304 305 306 307 308 309 310
	}

	for {
		select {
		case <-rm.ctx.Done():
			return
		case message := <-rm.messages:
			message.handle(rm)
		}
	}
}

Hannah Howard's avatar
Hannah Howard committed
311 312 313 314 315 316 317 318 319
func (rm *ResponseManager) processUpdate(key responseKey, update gsmsg.GraphSyncRequest) {
	response, ok := rm.inProgressResponses[key]
	if !ok {
		log.Warnf("received update for non existent request, peer %s, request ID %d", key.p.Pretty(), key.requestID)
		return
	}
	if !response.isPaused {
		response.updates = append(response.updates, update)
		select {
320
		case response.signals.updateSignal <- struct{}{}:
Hannah Howard's avatar
Hannah Howard committed
321 322 323 324 325 326
		default:
		}
		return
	}
	result := rm.updateHooks.ProcessUpdateHooks(key.p, response.request, update)
	peerResponseSender := rm.peerManager.SenderForPeer(key.p)
Hannah Howard's avatar
Hannah Howard committed
327 328 329 330 331 332
	err := peerResponseSender.Transaction(key.requestID, func(transaction peerresponsemanager.PeerResponseTransactionSender) error {
		for _, extension := range result.Extensions {
			transaction.SendExtensionData(extension)
		}
		if result.Err != nil {
			transaction.FinishWithError(graphsync.RequestFailedUnknown)
333
			transaction.AddNotifee(notifications.Notifee{Topic: graphsync.RequestFailedUnknown, Subscriber: response.subscriber})
Hannah Howard's avatar
Hannah Howard committed
334 335 336 337 338
		}
		return nil
	})
	if err != nil {
		log.Errorf("Error processing update: %s", err)
Hannah Howard's avatar
Hannah Howard committed
339 340 341 342 343 344 345 346 347 348
	}
	if result.Err != nil {
		delete(rm.inProgressResponses, key)
		response.cancelFn()
		return
	}
	if result.Unpause {
		err := rm.unpauseRequest(key.p, key.requestID)
		if err != nil {
			log.Warnf("error unpausing request: %s", err.Error())
349 350
		}
	}
Hannah Howard's avatar
Hannah Howard committed
351

352 353
}

354
func (rm *ResponseManager) unpauseRequest(p peer.ID, requestID graphsync.RequestID, extensions ...graphsync.ExtensionData) error {
Hannah Howard's avatar
Hannah Howard committed
355 356 357 358 359 360 361 362 363
	key := responseKey{p, requestID}
	inProgressResponse, ok := rm.inProgressResponses[key]
	if !ok {
		return errors.New("could not find request")
	}
	if !inProgressResponse.isPaused {
		return errors.New("request is not paused")
	}
	inProgressResponse.isPaused = false
364 365 366 367 368 369 370 371 372
	if len(extensions) > 0 {
		peerResponseSender := rm.peerManager.SenderForPeer(key.p)
		_ = peerResponseSender.Transaction(requestID, func(transaction peerresponsemanager.PeerResponseTransactionSender) error {
			for _, extension := range extensions {
				transaction.SendExtensionData(extension)
			}
			return nil
		})
	}
Hannah Howard's avatar
Hannah Howard committed
373 374 375 376 377 378 379 380
	rm.queryQueue.PushTasks(p, peertask.Task{Topic: key, Priority: math.MaxInt32, Work: 1})
	select {
	case rm.workSignal <- struct{}{}:
	default:
	}
	return nil
}

381
func (rm *ResponseManager) abortRequest(p peer.ID, requestID graphsync.RequestID, err error) error {
382 383 384 385 386 387 388 389 390
	key := responseKey{p, requestID}
	rm.queryQueue.Remove(key, key.p)
	response, ok := rm.inProgressResponses[key]
	if !ok {
		return errors.New("could not find request")
	}

	if response.isPaused {
		peerResponseSender := rm.peerManager.SenderForPeer(key.p)
391 392
		if isContextErr(err) {

393 394
			rm.cancelledListeners.NotifyCancelledListeners(p, response.request)
			peerResponseSender.FinishWithCancel(requestID)
395 396
		} else if err != errNetworkError {
			peerResponseSender.FinishWithError(requestID, graphsync.RequestCancelled, notifications.Notifee{Topic: graphsync.RequestCancelled, Subscriber: response.subscriber})
397
		}
398 399 400 401 402
		delete(rm.inProgressResponses, key)
		response.cancelFn()
		return nil
	}
	select {
403
	case response.signals.errSignal <- err:
404 405 406 407 408
	default:
	}
	return nil
}

409 410 411 412
func (prm *processRequestMessage) handle(rm *ResponseManager) {
	for _, request := range prm.requests {
		key := responseKey{p: prm.p, requestID: request.ID()}
		if request.IsCancel() {
413
			_ = rm.abortRequest(prm.p, request.ID(), ipldutil.ContextCancelError{})
414 415 416 417 418 419 420
			continue
		}
		if request.IsUpdate() {
			rm.processUpdate(key, request)
			continue
		}
		ctx, cancelFn := context.WithCancel(rm.ctx)
421 422 423 424 425 426 427 428 429
		sub := notifications.NewMappableSubscriber(&subscriber{
			p:                     key.p,
			request:               request,
			ctx:                   rm.ctx,
			messages:              rm.messages,
			blockSentListeners:    rm.blockSentListeners,
			completedListeners:    rm.completedListeners,
			networkErrorListeners: rm.networkErrorListeners,
		}, notifications.IdentityTransform)
430 431
		rm.inProgressResponses[key] =
			&inProgressResponseStatus{
432 433 434 435
				ctx:        ctx,
				cancelFn:   cancelFn,
				subscriber: sub,
				request:    request,
436 437 438
				signals: signals{
					pauseSignal:  make(chan struct{}, 1),
					updateSignal: make(chan struct{}, 1),
439
					errSignal:    make(chan error, 1),
440
				},
441 442 443 444 445 446 447 448 449 450
			}
		// TODO: Use a better work estimation metric.
		rm.queryQueue.PushTasks(prm.p, peertask.Task{Topic: key, Priority: int(request.Priority()), Work: 1})
		select {
		case rm.workSignal <- struct{}{}:
		default:
		}
	}
}

451 452
func (rdr *responseDataRequest) handle(rm *ResponseManager) {
	response, ok := rm.inProgressResponses[rdr.key]
453
	var taskData responseTaskData
454
	if ok {
455
		taskData = responseTaskData{false, response.subscriber, response.ctx, response.request, response.loader, response.traverser, response.signals}
456
	} else {
457
		taskData = responseTaskData{empty: true}
458 459 460 461 462 463 464
	}
	select {
	case <-rm.ctx.Done():
	case rdr.taskDataChan <- taskData:
	}
}

465 466
func (ftr *finishTaskRequest) handle(rm *ResponseManager) {
	response, ok := rm.inProgressResponses[ftr.key]
467 468 469
	if !ok {
		return
	}
470
	if _, ok := ftr.err.(hooks.ErrPaused); ok {
471 472 473 474 475 476 477
		response.isPaused = true
		return
	}
	if ftr.err != nil {
		log.Infof("response failed: %w", ftr.err)
	}
	delete(rm.inProgressResponses, ftr.key)
478 479 480
	response.cancelFn()
}

481 482 483 484 485 486 487 488 489
func (srdr *setResponseDataRequest) handle(rm *ResponseManager) {
	response, ok := rm.inProgressResponses[srdr.key]
	if !ok {
		return
	}
	response.loader = srdr.loader
	response.traverser = srdr.traverser
}

Hannah Howard's avatar
Hannah Howard committed
490 491 492 493 494 495 496 497 498
func (rur *responseUpdateRequest) handle(rm *ResponseManager) {
	response, ok := rm.inProgressResponses[rur.key]
	var updates []gsmsg.GraphSyncRequest
	if ok {
		updates = response.updates
		response.updates = nil
	} else {
		updates = nil
	}
499 500
	select {
	case <-rm.ctx.Done():
Hannah Howard's avatar
Hannah Howard committed
501
	case rur.updateChan <- updates:
502 503
	}
}
504

Hannah Howard's avatar
Hannah Howard committed
505
func (sm *synchronizeMessage) handle(rm *ResponseManager) {
506
	select {
Hannah Howard's avatar
Hannah Howard committed
507
	case <-rm.ctx.Done():
508
	case sm.sync <- nil:
509 510 511 512
	}
}

func (urm *unpauseRequestMessage) handle(rm *ResponseManager) {
513
	err := rm.unpauseRequest(urm.p, urm.requestID, urm.extensions...)
514 515 516 517 518
	select {
	case <-rm.ctx.Done():
	case urm.response <- err:
	}
}
519 520 521 522 523 524 525 526 527 528 529

func (prm *pauseRequestMessage) pauseRequest(rm *ResponseManager) error {
	key := responseKey{prm.p, prm.requestID}
	inProgressResponse, ok := rm.inProgressResponses[key]
	if !ok {
		return errors.New("could not find request")
	}
	if inProgressResponse.isPaused {
		return errors.New("request is already paused")
	}
	select {
530
	case inProgressResponse.signals.pauseSignal <- struct{}{}:
531 532 533 534 535 536 537 538 539 540 541 542 543
	default:
	}
	return nil
}

func (prm *pauseRequestMessage) handle(rm *ResponseManager) {
	err := prm.pauseRequest(rm)
	select {
	case <-rm.ctx.Done():
	case prm.response <- err:
	}
}

544 545
func (crm *errorRequestMessage) handle(rm *ResponseManager) {
	err := rm.abortRequest(crm.p, crm.requestID, crm.err)
546 547
	select {
	case <-rm.ctx.Done():
548
	case crm.response <- err:
549 550
	}
}