Fix a data race in the block logger (#1533)

This commit is contained in:
Elichai Turkel 2021-02-17 17:05:25 +02:00 committed by GitHub
parent 2908a46441
commit 00a023620d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 17 deletions

View File

@ -11,27 +11,38 @@ import (
"github.com/kaspanet/kaspad/util/mstime" "github.com/kaspanet/kaspad/util/mstime"
) )
var ( // BlockLogger is a type tracking the amount of blocks/headers/transactions to log the time it took to receive them
type BlockLogger struct {
receivedLogBlocks int64 receivedLogBlocks int64
receivedLogHeaders int64 receivedLogHeaders int64
receivedLogTransactions int64 receivedLogTransactions int64
lastBlockLogTime time.Time lastBlockLogTime time.Time
) }
// NewBlockLogger creates a new instance with zeroed blocks/headers/transactions/time counters.
func NewBlockLogger() *BlockLogger {
return &BlockLogger{
receivedLogBlocks: 0,
receivedLogHeaders: 0,
receivedLogTransactions: 0,
lastBlockLogTime: time.Now(),
}
}
// LogBlock logs a new block blue score as an information message // LogBlock logs a new block blue score as an information message
// to show progress to the user. In order to prevent spam, it limits logging to // to show progress to the user. In order to prevent spam, it limits logging to
// one message every 10 seconds with duration and totals included. // one message every 10 seconds with duration and totals included.
func LogBlock(block *externalapi.DomainBlock) { func (bl *BlockLogger) LogBlock(block *externalapi.DomainBlock) {
if len(block.Transactions) == 0 { if len(block.Transactions) == 0 {
receivedLogHeaders++ bl.receivedLogHeaders++
} else { } else {
receivedLogBlocks++ bl.receivedLogBlocks++
} }
receivedLogTransactions += int64(len(block.Transactions)) bl.receivedLogTransactions += int64(len(block.Transactions))
now := time.Now() now := time.Now()
duration := now.Sub(lastBlockLogTime) duration := now.Sub(bl.lastBlockLogTime)
if duration < time.Second*10 { if duration < time.Second*10 {
return return
} }
@ -41,26 +52,26 @@ func LogBlock(block *externalapi.DomainBlock) {
// Log information about new block blue score. // Log information about new block blue score.
blockStr := "blocks" blockStr := "blocks"
if receivedLogBlocks == 1 { if bl.receivedLogBlocks == 1 {
blockStr = "block" blockStr = "block"
} }
txStr := "transactions" txStr := "transactions"
if receivedLogTransactions == 1 { if bl.receivedLogTransactions == 1 {
txStr = "transaction" txStr = "transaction"
} }
headerStr := "headers" headerStr := "headers"
if receivedLogBlocks == 1 { if bl.receivedLogBlocks == 1 {
headerStr = "header" headerStr = "header"
} }
log.Infof("Processed %d %s and %d %s in the last %s (%d %s, %s)", log.Infof("Processed %d %s and %d %s in the last %s (%d %s, %s)",
receivedLogBlocks, blockStr, receivedLogHeaders, headerStr, truncatedDuration, receivedLogTransactions, bl.receivedLogBlocks, blockStr, bl.receivedLogHeaders, headerStr, truncatedDuration, bl.receivedLogTransactions,
txStr, mstime.UnixMilliseconds(block.Header.TimeInMilliseconds())) txStr, mstime.UnixMilliseconds(block.Header.TimeInMilliseconds()))
receivedLogBlocks = 0 bl.receivedLogBlocks = 0
receivedLogHeaders = 0 bl.receivedLogHeaders = 0
receivedLogTransactions = 0 bl.receivedLogTransactions = 0
lastBlockLogTime = now bl.lastBlockLogTime = now
} }

View File

@ -3,6 +3,7 @@ package blockprocessor
import ( import (
"github.com/kaspanet/kaspad/domain/consensus/model" "github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi" "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/processes/blockprocessor/blocklogger"
"github.com/kaspanet/kaspad/infrastructure/logger" "github.com/kaspanet/kaspad/infrastructure/logger"
"time" "time"
) )
@ -13,6 +14,7 @@ type blockProcessor struct {
genesisHash *externalapi.DomainHash genesisHash *externalapi.DomainHash
targetTimePerBlock time.Duration targetTimePerBlock time.Duration
databaseContext model.DBManager databaseContext model.DBManager
blockLogger *blocklogger.BlockLogger
consensusStateManager model.ConsensusStateManager consensusStateManager model.ConsensusStateManager
pruningManager model.PruningManager pruningManager model.PruningManager
@ -49,6 +51,7 @@ func New(
genesisHash *externalapi.DomainHash, genesisHash *externalapi.DomainHash,
targetTimePerBlock time.Duration, targetTimePerBlock time.Duration,
databaseContext model.DBManager, databaseContext model.DBManager,
consensusStateManager model.ConsensusStateManager, consensusStateManager model.ConsensusStateManager,
pruningManager model.PruningManager, pruningManager model.PruningManager,
blockValidator model.BlockValidator, blockValidator model.BlockValidator,
@ -81,6 +84,7 @@ func New(
genesisHash: genesisHash, genesisHash: genesisHash,
targetTimePerBlock: targetTimePerBlock, targetTimePerBlock: targetTimePerBlock,
databaseContext: databaseContext, databaseContext: databaseContext,
blockLogger: blocklogger.NewBlockLogger(),
pruningManager: pruningManager, pruningManager: pruningManager,
blockValidator: blockValidator, blockValidator: blockValidator,
dagTopologyManager: dagTopologyManager, dagTopologyManager: dagTopologyManager,

View File

@ -2,7 +2,6 @@ package blockprocessor
import ( import (
"fmt" "fmt"
"github.com/kaspanet/kaspad/domain/consensus/processes/blockprocessor/blocklogger"
"github.com/kaspanet/kaspad/util/difficulty" "github.com/kaspanet/kaspad/util/difficulty"
"github.com/kaspanet/kaspad/domain/consensus/model" "github.com/kaspanet/kaspad/domain/consensus/model"
@ -144,7 +143,7 @@ func (bp *blockProcessor) validateAndInsertBlock(block *externalapi.DomainBlock,
return nil, logClosureErr return nil, logClosureErr
} }
blocklogger.LogBlock(block) bp.blockLogger.LogBlock(block)
return &externalapi.BlockInsertionResult{ return &externalapi.BlockInsertionResult{
VirtualSelectedParentChainChanges: selectedParentChainChanges, VirtualSelectedParentChainChanges: selectedParentChainChanges,