2023-01-05 12:45:09 +01:00
|
|
|
package syncer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/alitto/pond"
|
2023-08-10 12:10:19 +02:00
|
|
|
"github.com/inethi/inethi-cic-chain-events/internal/pipeline"
|
|
|
|
"github.com/inethi/inethi-cic-chain-events/internal/store"
|
2023-01-05 12:45:09 +01:00
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/zerodha/logf"
|
|
|
|
)
|
|
|
|
|
2023-01-18 20:40:14 +01:00
|
|
|
const (
|
|
|
|
headBlockLag = 5
|
|
|
|
)
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
type (
|
|
|
|
JanitorOpts struct {
|
|
|
|
BatchSize uint64
|
|
|
|
Logg logf.Logger
|
|
|
|
Pipeline *pipeline.Pipeline
|
|
|
|
Pool *pond.WorkerPool
|
|
|
|
Stats *Stats
|
|
|
|
Store store.Store[pgx.Rows]
|
|
|
|
SweepInterval time.Duration
|
|
|
|
}
|
2023-01-05 12:45:09 +01:00
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
Janitor struct {
|
|
|
|
batchSize uint64
|
|
|
|
pipeline *pipeline.Pipeline
|
|
|
|
logg logf.Logger
|
|
|
|
pool *pond.WorkerPool
|
|
|
|
stats *Stats
|
|
|
|
store store.Store[pgx.Rows]
|
|
|
|
sweepInterval time.Duration
|
|
|
|
}
|
|
|
|
)
|
2023-01-05 12:45:09 +01:00
|
|
|
|
|
|
|
func NewJanitor(o JanitorOpts) *Janitor {
|
|
|
|
return &Janitor{
|
|
|
|
batchSize: o.BatchSize,
|
|
|
|
logg: o.Logg,
|
|
|
|
pipeline: o.Pipeline,
|
|
|
|
pool: o.Pool,
|
|
|
|
stats: o.Stats,
|
|
|
|
store: o.Store,
|
|
|
|
sweepInterval: o.SweepInterval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Janitor) Start(ctx context.Context) error {
|
2023-02-24 11:28:30 +01:00
|
|
|
ticker := time.NewTicker(j.sweepInterval)
|
2023-01-05 12:45:09 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2023-01-11 09:13:59 +01:00
|
|
|
case <-ctx.Done():
|
|
|
|
j.logg.Info("janitor: shutdown signal received")
|
|
|
|
return nil
|
2023-02-24 11:28:30 +01:00
|
|
|
case <-ticker.C:
|
2023-03-08 15:30:40 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), jobTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-01-11 09:13:59 +01:00
|
|
|
j.logg.Debug("janitor: starting sweep")
|
2023-03-08 15:30:40 +01:00
|
|
|
if err := j.QueueMissingBlocks(ctx); err != nil {
|
2023-01-11 09:13:59 +01:00
|
|
|
j.logg.Error("janitor: queue missing blocks error", "error", err)
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 09:13:59 +01:00
|
|
|
// QueueMissingBlocks searches for missing block and queues the block for processing.
|
|
|
|
// It will run twice for a given search range and only after, raise the lower bound.
|
|
|
|
func (j *Janitor) QueueMissingBlocks(ctx context.Context) error {
|
2023-01-05 12:45:09 +01:00
|
|
|
if j.stats.GetHeadCursor() == 0 {
|
2023-01-11 09:13:59 +01:00
|
|
|
j.logg.Warn("janitor: (skipping) awaiting head synchronization")
|
2023-01-05 12:45:09 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:40:14 +01:00
|
|
|
if j.pool.WaitingTasks() != 0 {
|
|
|
|
j.logg.Debug("janitor: (skipping) queue has pending jobs", "pending_jobs", j.pool.WaitingTasks())
|
2023-01-05 12:45:09 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lowerBound, upperBound, err := j.store.GetSearchBounds(
|
2023-01-11 09:13:59 +01:00
|
|
|
ctx,
|
2023-01-05 12:45:09 +01:00
|
|
|
j.batchSize,
|
|
|
|
j.stats.GetHeadCursor(),
|
2023-01-18 20:40:14 +01:00
|
|
|
headBlockLag,
|
2023-01-05 12:45:09 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-11 09:13:59 +01:00
|
|
|
rows, err := j.store.GetMissingBlocks(ctx, lowerBound, upperBound)
|
2023-01-05 12:45:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:52:29 +01:00
|
|
|
missingBlockCountReport := j.stats.GetHeadCursor() - lowerBound
|
|
|
|
if missingBlockCountReport > 10 {
|
2023-03-09 09:33:09 +01:00
|
|
|
j.logg.Info("janitor: missing blocks", "count", missingBlockCountReport)
|
2023-03-08 15:52:29 +01:00
|
|
|
}
|
2023-01-19 11:36:21 +01:00
|
|
|
|
2023-01-05 12:45:09 +01:00
|
|
|
rowsProcessed := 0
|
|
|
|
for rows.Next() {
|
2023-01-18 20:40:14 +01:00
|
|
|
var blockNumber uint64
|
|
|
|
if err := rows.Scan(&blockNumber); err != nil {
|
2023-01-05 12:45:09 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
j.pool.Submit(func() {
|
2023-01-18 20:40:14 +01:00
|
|
|
if err := j.pipeline.Run(ctx, blockNumber); err != nil {
|
2023-01-11 09:13:59 +01:00
|
|
|
j.logg.Error("janitor: pipeline run error", "error", err)
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
rowsProcessed++
|
|
|
|
}
|
|
|
|
|
2023-01-19 11:36:21 +01:00
|
|
|
j.logg.Debug("janitor: missing blocks to be processed", "count", rowsProcessed)
|
2023-01-05 12:45:09 +01:00
|
|
|
if rowsProcessed == 0 {
|
2023-01-18 20:40:14 +01:00
|
|
|
j.logg.Info("janitor: no gap! rasing lower bound", "new_lower_bound", upperBound)
|
2023-01-05 12:45:09 +01:00
|
|
|
j.stats.UpdateLowerBound(upperBound)
|
2023-01-11 09:13:59 +01:00
|
|
|
j.store.SetSearchLowerBound(ctx, upperBound)
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Err() != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|