2019-10-02 14:10:37 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package super_node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-05-02 05:02:05 +00:00
|
|
|
utils "github.com/vulcanize/vulcanizedb/libraries/shared/utilities"
|
2020-01-31 18:03:37 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2019-10-02 14:10:37 +00:00
|
|
|
)
|
|
|
|
|
2019-10-14 14:41:29 +00:00
|
|
|
const (
|
2019-11-04 19:14:05 +00:00
|
|
|
DefaultMaxBatchSize uint64 = 100
|
2020-03-18 00:42:53 +00:00
|
|
|
DefaultMaxBatchNumber int64 = 50
|
2019-10-14 14:41:29 +00:00
|
|
|
)
|
2019-11-01 19:03:28 +00:00
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
// BackFillInterface for filling in gaps in the super node
|
|
|
|
type BackFillInterface interface {
|
|
|
|
// Method for the super node to periodically check for and fill in gaps in its data using an archival node
|
2020-04-23 20:56:37 +00:00
|
|
|
BackFill(wg *sync.WaitGroup)
|
2020-05-12 19:53:50 +00:00
|
|
|
Stop() error
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BackFillService for filling in gaps in the super node
|
|
|
|
type BackFillService struct {
|
2020-01-17 23:16:01 +00:00
|
|
|
// Interface for converting payloads into IPLD object payloads
|
|
|
|
Converter shared.PayloadConverter
|
|
|
|
// Interface for publishing the IPLD payloads to IPFS
|
|
|
|
Publisher shared.IPLDPublisher
|
|
|
|
// Interface for indexing the CIDs of the published IPLDs in Postgres
|
|
|
|
Indexer shared.CIDIndexer
|
2019-10-02 14:10:37 +00:00
|
|
|
// Interface for searching and retrieving CIDs from Postgres index
|
2020-01-17 23:16:01 +00:00
|
|
|
Retriever shared.CIDRetriever
|
|
|
|
// Interface for fetching payloads over at historical blocks; over http
|
|
|
|
Fetcher shared.PayloadFetcher
|
2020-02-13 22:50:56 +00:00
|
|
|
// Channel for forwarding backfill payloads to the ScreenAndServe process
|
2020-02-20 22:12:52 +00:00
|
|
|
ScreenAndServeChan chan shared.ConvertedData
|
2019-10-02 14:10:37 +00:00
|
|
|
// Check frequency
|
|
|
|
GapCheckFrequency time.Duration
|
2020-01-17 23:16:01 +00:00
|
|
|
// Size of batch fetches
|
2019-11-01 19:03:28 +00:00
|
|
|
BatchSize uint64
|
2020-03-18 00:42:53 +00:00
|
|
|
// Number of goroutines
|
|
|
|
BatchNumber int64
|
2020-02-13 22:50:56 +00:00
|
|
|
// Channel for receiving quit signal
|
|
|
|
QuitChan chan bool
|
2020-02-25 22:38:27 +00:00
|
|
|
// Chain type
|
2020-05-12 19:53:50 +00:00
|
|
|
chain shared.ChainType
|
2020-04-04 01:36:37 +00:00
|
|
|
// Headers with times_validated lower than this will be resynced
|
|
|
|
validationLevel int
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBackFillService returns a new BackFillInterface
|
2020-02-25 22:38:27 +00:00
|
|
|
func NewBackFillService(settings *Config, screenAndServeChan chan shared.ConvertedData) (BackFillInterface, error) {
|
2020-05-06 21:16:56 +00:00
|
|
|
publisher, err := NewIPLDPublisher(settings.Chain, settings.IPFSPath, settings.BackFillDBConn, settings.IPFSMode)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-06 21:16:56 +00:00
|
|
|
indexer, err := NewCIDIndexer(settings.Chain, settings.BackFillDBConn, settings.IPFSMode)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-31 18:03:37 +00:00
|
|
|
converter, err := NewPayloadConverter(settings.Chain)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-06 21:16:56 +00:00
|
|
|
retriever, err := NewCIDRetriever(settings.Chain, settings.BackFillDBConn)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-19 23:26:23 +00:00
|
|
|
fetcher, err := NewPaylaodFetcher(settings.Chain, settings.HTTPClient, settings.Timeout)
|
2019-10-08 19:51:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
batchSize := settings.BatchSize
|
|
|
|
if batchSize == 0 {
|
|
|
|
batchSize = DefaultMaxBatchSize
|
|
|
|
}
|
2020-03-18 00:42:53 +00:00
|
|
|
batchNumber := int64(settings.BatchNumber)
|
|
|
|
if batchNumber == 0 {
|
|
|
|
batchNumber = DefaultMaxBatchNumber
|
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
return &BackFillService{
|
2020-02-13 22:50:56 +00:00
|
|
|
Indexer: indexer,
|
|
|
|
Converter: converter,
|
|
|
|
Publisher: publisher,
|
|
|
|
Retriever: retriever,
|
|
|
|
Fetcher: fetcher,
|
|
|
|
GapCheckFrequency: settings.Frequency,
|
|
|
|
BatchSize: batchSize,
|
2020-03-18 00:42:53 +00:00
|
|
|
BatchNumber: int64(batchNumber),
|
2020-02-13 22:50:56 +00:00
|
|
|
ScreenAndServeChan: screenAndServeChan,
|
2020-05-12 19:53:50 +00:00
|
|
|
QuitChan: make(chan bool),
|
|
|
|
chain: settings.Chain,
|
2020-04-04 01:36:37 +00:00
|
|
|
validationLevel: settings.ValidationLevel,
|
2019-10-08 19:51:38 +00:00
|
|
|
}, nil
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 20:56:37 +00:00
|
|
|
// BackFill periodically checks for and fills in gaps in the super node db
|
|
|
|
func (bfs *BackFillService) BackFill(wg *sync.WaitGroup) {
|
2019-10-02 14:10:37 +00:00
|
|
|
ticker := time.NewTicker(bfs.GapCheckFrequency)
|
|
|
|
go func() {
|
2020-05-12 19:53:50 +00:00
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
2019-10-02 14:10:37 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-02-13 22:50:56 +00:00
|
|
|
case <-bfs.QuitChan:
|
2020-05-12 19:53:50 +00:00
|
|
|
log.Infof("quiting %s FillGapsInSuperNode process", bfs.chain.String())
|
2019-10-02 14:10:37 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2020-05-21 20:58:07 +00:00
|
|
|
gaps, err := bfs.Retriever.RetrieveGapsInData(bfs.validationLevel)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s super node db backFill RetrieveGapsInData error: %v", bfs.chain.String(), err)
|
|
|
|
continue
|
|
|
|
}
|
2020-05-12 19:53:50 +00:00
|
|
|
// spin up worker goroutines for this search pass
|
|
|
|
// we start and kill a new batch of workers for each pass
|
|
|
|
// so that we know each of the previous workers is done before we search for new gaps
|
|
|
|
heightsChan := make(chan []uint64)
|
|
|
|
for i := 1; i <= int(bfs.BatchNumber); i++ {
|
|
|
|
go bfs.backFill(wg, i, heightsChan)
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
for _, gap := range gaps {
|
2020-05-12 19:53:50 +00:00
|
|
|
log.Infof("backFilling %s data from %d to %d", bfs.chain.String(), gap.Start, gap.Stop)
|
|
|
|
blockRangeBins, err := utils.GetBlockHeightBins(gap.Start, gap.Stop, bfs.BatchSize)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err != nil {
|
2020-05-12 19:53:50 +00:00
|
|
|
log.Errorf("%s super node db backFill GetBlockHeightBins error: %v", bfs.chain.String(), err)
|
2020-05-12 01:49:42 +00:00
|
|
|
continue
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
2020-05-12 19:53:50 +00:00
|
|
|
for _, heights := range blockRangeBins {
|
|
|
|
select {
|
|
|
|
case <-bfs.QuitChan:
|
|
|
|
log.Infof("quiting %s BackFill process", bfs.chain.String())
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
heightsChan <- heights
|
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 19:53:50 +00:00
|
|
|
// send a quit signal to each worker
|
|
|
|
// this blocks until each worker has finished its current task and is free to receive from the quit channel
|
|
|
|
for i := 1; i <= int(bfs.BatchNumber); i++ {
|
|
|
|
bfs.QuitChan <- true
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-05-12 19:53:50 +00:00
|
|
|
log.Infof("%s BackFill goroutine successfully spun up", bfs.chain.String())
|
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
|
2020-05-12 19:53:50 +00:00
|
|
|
func (bfs *BackFillService) backFill(wg *sync.WaitGroup, id int, heightChan chan []uint64) {
|
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
2020-03-18 00:42:53 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-05-12 19:53:50 +00:00
|
|
|
case heights := <-heightChan:
|
|
|
|
log.Debugf("%s backFill worker %d processing section from %d to %d", bfs.chain.String(), id, heights[0], heights[len(heights)-1])
|
|
|
|
payloads, err := bfs.Fetcher.FetchAt(heights)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s backFill worker %d fetcher error: %s", bfs.chain.String(), id, err.Error())
|
2020-03-18 00:42:53 +00:00
|
|
|
}
|
2020-05-12 19:53:50 +00:00
|
|
|
for _, payload := range payloads {
|
|
|
|
ipldPayload, err := bfs.Converter.Convert(payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s backFill worker %d converter error: %s", bfs.chain.String(), id, err.Error())
|
|
|
|
}
|
|
|
|
// If there is a ScreenAndServe process listening, forward converted payload to it
|
|
|
|
select {
|
|
|
|
case bfs.ScreenAndServeChan <- ipldPayload:
|
|
|
|
log.Debugf("%s backFill worker %d forwarded converted payload to server", bfs.chain.String(), id)
|
|
|
|
default:
|
|
|
|
log.Debugf("%s backFill worker %d unable to forward converted payload to server; no channel ready to receive", bfs.chain.String(), id)
|
|
|
|
}
|
|
|
|
cidPayload, err := bfs.Publisher.Publish(ipldPayload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s backFill worker %d publisher error: %s", bfs.chain.String(), id, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := bfs.Indexer.Index(cidPayload); err != nil {
|
|
|
|
log.Errorf("%s backFill worker %d indexer error: %s", bfs.chain.String(), id, err.Error())
|
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
2020-05-12 19:53:50 +00:00
|
|
|
log.Infof("%s backFill worker %d finished section from %d to %d", bfs.chain.String(), id, heights[0], heights[len(heights)-1])
|
|
|
|
case <-bfs.QuitChan:
|
|
|
|
log.Infof("%s backFill worker %d shutting down", bfs.chain.String(), id)
|
|
|
|
return
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
2020-03-18 00:42:53 +00:00
|
|
|
}
|
2019-11-01 19:03:28 +00:00
|
|
|
}
|
2020-05-12 19:53:50 +00:00
|
|
|
|
|
|
|
func (bfs *BackFillService) Stop() error {
|
|
|
|
log.Infof("Stopping %s backFill service", bfs.chain.String())
|
|
|
|
close(bfs.QuitChan)
|
|
|
|
return nil
|
|
|
|
}
|