Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Staged Stream Sync Logger #4847

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions api/service/stagedstreamsync/beacon_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -38,16 +37,15 @@ type (
}
)

func newBeaconHelper(bc blockChain, blockC <-chan *types.Block, insertHook func()) *beaconHelper {
func newBeaconHelper(bc blockChain, logger zerolog.Logger, blockC <-chan *types.Block, insertHook func()) *beaconHelper {
return &beaconHelper{
bc: bc,
blockC: blockC,
insertHook: insertHook,
lastMileCache: newBlocksByNumber(lastMileCap),
insertC: make(chan insertTask, 1),
closeC: make(chan struct{}),
logger: utils.Logger().With().
Str("module", "staged stream sync").
logger: logger.With().
Str("sub-module", "beacon helper").
Logger(),
}
Expand Down
4 changes: 3 additions & 1 deletion api/service/stagedstreamsync/download_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func newDownloadManager(chain blockChain, targetBN uint64, batchSize int, logger
rq: newResultQueue(),
details: make(map[uint64]*DownloadDetails),
batchSize: batchSize,
logger: logger,
logger: logger.With().
Str("sub-module", "download manager").
Logger(),
}
}

Expand Down
21 changes: 16 additions & 5 deletions api/service/stagedstreamsync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ func NewDownloader(host p2p.Host, bc core.BlockChain, nodeConfig *nodeconfig.Con

host.AddStreamProtocol(sp)

logger := utils.Logger().With().
Str("module", "StagedStreamSync").
Uint32("ShardID", bc.ShardID()).
Uint64("currentHeight", bc.CurrentBlock().NumberU64()).
Interface("currentHeadHash", bc.CurrentBlock().Hash()).
Logger()

var bh *beaconHelper
if config.BHConfig != nil && bc.ShardID() == shard.BeaconChainShardID {
bh = newBeaconHelper(bc, config.BHConfig.BlockC, config.BHConfig.InsertHook)
bh = newBeaconHelper(bc, logger, config.BHConfig.BlockC, config.BHConfig.InsertHook)
}

logger := utils.Logger().With().
Str("module", "staged stream sync").
Uint32("ShardID", bc.ShardID()).Logger()

ctx, cancel := context.WithCancel(context.Background())

// create an instance of staged sync for the downloader
Expand Down Expand Up @@ -204,7 +207,15 @@ func (d *Downloader) waitForEnoughStreams(requiredStreams int) (bool, int) {
trigger()

case <-checkCh:
d.logger.Debug().
Int("requiredStreams", requiredStreams).
Int("NumStreams", d.syncProtocol.NumStreams()).
Msg("check stream connections...")
if d.syncProtocol.NumStreams() >= requiredStreams {
d.logger.Info().
Int("requiredStreams", requiredStreams).
Int("NumStreams", d.syncProtocol.NumStreams()).
Msg("it has enough stream connections and will continue syncing")
return true, d.syncProtocol.NumStreams()
}
case <-d.closeC:
Expand Down
5 changes: 3 additions & 2 deletions api/service/stagedstreamsync/receipt_download_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func newReceiptDownloadManager(tx kv.RwTx, chain blockChain, targetBN uint64, lo
retries: newPrioritizedNumbers(),
rdd: make(map[uint64]ReceiptDownloadDetails),
received: make(map[uint64]Received),

logger: logger,
logger: logger.With().
Str("sub-module", "receipt download manager").
Logger(),
}
}

Expand Down
3 changes: 1 addition & 2 deletions api/service/stagedstreamsync/short_range_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
syncProto "github.com/harmony-one/harmony/p2p/stream/protocols/sync"
sttypes "github.com/harmony-one/harmony/p2p/stream/types"
"github.com/pkg/errors"
Expand Down Expand Up @@ -138,7 +137,7 @@ func (sh *srHelper) getBlocksByHashes(ctx context.Context, hashes []common.Hash,

func (sh *srHelper) checkPrerequisites() error {
if sh.syncProtocol.NumStreams() < sh.config.Concurrency {
utils.Logger().Info().
sh.logger.Info().
Int("available streams", sh.syncProtocol.NumStreams()).
Interface("concurrency", sh.config.Concurrency).
Msg("not enough streams to do concurrent processes")
Expand Down
31 changes: 18 additions & 13 deletions api/service/stagedstreamsync/stage_blockhashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/internal/utils"
sttypes "github.com/harmony-one/harmony/p2p/stream/types"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)

// Hash frequency and stream tracking
Expand All @@ -33,6 +33,7 @@ type StageBlockHashesCfg struct {
isBeaconShard bool
cachedb kv.RwDB
logProgress bool
logger zerolog.Logger
}

func NewStageBlockHashes(cfg StageBlockHashesCfg) *StageBlockHashes {
Expand All @@ -41,12 +42,16 @@ func NewStageBlockHashes(cfg StageBlockHashesCfg) *StageBlockHashes {
}
}

func NewStageBlockHashesCfg(bc core.BlockChain, db kv.RwDB, concurrency int, protocol syncProtocol, isBeaconShard bool, logProgress bool) StageBlockHashesCfg {
func NewStageBlockHashesCfg(bc core.BlockChain, db kv.RwDB, concurrency int, protocol syncProtocol, isBeaconShard bool, logger zerolog.Logger, logProgress bool) StageBlockHashesCfg {
return StageBlockHashesCfg{
bc: bc,
db: db,
isBeaconShard: isBeaconShard,
logProgress: logProgress,
logger: logger.With().
Str("stage", "StageBlockHashes").
Str("mode", "long range").
Logger(),
}
}

Expand Down Expand Up @@ -125,7 +130,7 @@ func (bh *StageBlockHashes) Exec(ctx context.Context, firstCycle bool, invalidBl
}

// create download manager instant
hdm := newDownloadManager(bh.configs.bc, targetHeight, BlockHashesPerRequest, s.state.logger)
hdm := newDownloadManager(bh.configs.bc, targetHeight, BlockHashesPerRequest, bh.configs.logger)

// Fetch block hashes from neighbors
if err := bh.runBlockHashWorkerLoop(ctx, tx, hdm, s, startTime, currentHead, targetHeight); err != nil {
Expand All @@ -138,19 +143,19 @@ func (bh *StageBlockHashes) Exec(ctx context.Context, firstCycle bool, invalidBl
func (bh *StageBlockHashes) downloadBlockHashes(ctx context.Context, bns []uint64) ([]common.Hash, sttypes.StreamID, error) {
hashes, stid, err := bh.configs.protocol.GetBlockHashes(ctx, bns)
if hashes == nil {
utils.Logger().Warn().
bh.configs.logger.Warn().
Interface("bns", bns).
Msg("[StageBlockHashes] downloadBlockHashes Nil Response")
bh.configs.protocol.RemoveStream(stid)
return []common.Hash{}, stid, errors.New("nil response for hashes")
}
utils.Logger().Info().
bh.configs.logger.Info().
Int("request size", len(bns)).
Int("received size", len(hashes)).
Interface("stid", stid).
Msg("[StageBlockHashes] downloadBlockHashes received hashes")
if len(hashes) > len(bns) {
utils.Logger().Warn().
bh.configs.logger.Warn().
Int("request size", len(bns)).
Int("received size", len(hashes)).
Interface("stid", stid).
Expand Down Expand Up @@ -240,7 +245,7 @@ func (bh *StageBlockHashes) runBlockHashWorkerLoop(ctx context.Context,

// save progress
if err := bh.saveProgress(ctx, s, currProgress, tx); err != nil {
utils.Logger().Error().
bh.configs.logger.Error().
Err(err).
Msgf("[STAGED_STREAM_SYNC] saving progress for block hashes stage failed")
return err
Expand Down Expand Up @@ -347,7 +352,7 @@ func (bh *StageBlockHashes) saveBlockHashes(ctx context.Context, tx kv.RwTx, has
blkKey := marshalData(bn)

if err := tx.Put(BlockHashesBucket, blkKey, hash.Bytes()); err != nil {
utils.Logger().Error().
bh.configs.logger.Error().
Err(err).
Uint64("block number", bn).
Msg("[STAGED_STREAM_SYNC] adding block hash to db failed")
Expand Down Expand Up @@ -377,7 +382,7 @@ func (bh *StageBlockHashes) saveProgress(ctx context.Context, s *StageState, pro

// save progress
if err = s.Update(tx, progress); err != nil {
utils.Logger().Error().
bh.configs.logger.Error().
Err(err).
Msgf("[STAGED_STREAM_SYNC] saving progress for block hashes stage failed")
return ErrSavingHashesProgressFail
Expand Down Expand Up @@ -450,15 +455,15 @@ func (bh *StageBlockHashes) Revert(ctx context.Context, firstCycle bool, u *Reve

// clean block hashes db
if err = tx.ClearBucket(BlockHashesBucket); err != nil {
utils.Logger().Error().
bh.configs.logger.Error().
Err(err).
Msgf("[STAGED_STREAM_SYNC] clear block hashes bucket after revert failed")
return err
}

// clean cache db as well
if err := bh.clearCache(); err != nil {
utils.Logger().Error().
bh.configs.logger.Error().
Err(err).
Msgf("[STAGED_STREAM_SYNC] clear block hashes cache failed")
return err
Expand All @@ -467,14 +472,14 @@ func (bh *StageBlockHashes) Revert(ctx context.Context, firstCycle bool, u *Reve
// save progress
currentHead := bh.configs.bc.CurrentBlock().NumberU64()
if err = s.Update(tx, currentHead); err != nil {
utils.Logger().Error().
bh.configs.logger.Error().
Err(err).
Msgf("[STAGED_STREAM_SYNC] saving progress for block hashes stage after revert failed")
return err
}

if err = u.Done(tx); err != nil {
utils.Logger().Error().
bh.configs.logger.Error().
Err(err).
Msgf("[STAGED_STREAM_SYNC] reset after revert failed")
return err
Expand Down
Loading